bpf_jit_comp.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BPF JIT compiler
  4. *
  5. * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
  6. * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  7. */
  8. #include <linux/netdevice.h>
  9. #include <linux/filter.h>
  10. #include <linux/if_vlan.h>
  11. #include <linux/bpf.h>
  12. #include <linux/memory.h>
  13. #include <linux/sort.h>
  14. #include <asm/extable.h>
  15. #include <asm/ftrace.h>
  16. #include <asm/set_memory.h>
  17. #include <asm/nospec-branch.h>
  18. #include <asm/text-patching.h>
  19. #include <asm/unwind.h>
  20. #include <asm/cfi.h>
  21. static bool all_callee_regs_used[4] = {true, true, true, true};
  22. static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
  23. {
  24. if (len == 1)
  25. *ptr = bytes;
  26. else if (len == 2)
  27. *(u16 *)ptr = bytes;
  28. else {
  29. *(u32 *)ptr = bytes;
  30. barrier();
  31. }
  32. return ptr + len;
  33. }
  34. #define EMIT(bytes, len) \
  35. do { prog = emit_code(prog, bytes, len); } while (0)
  36. #define EMIT1(b1) EMIT(b1, 1)
  37. #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
  38. #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
  39. #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
  40. #define EMIT1_off32(b1, off) \
  41. do { EMIT1(b1); EMIT(off, 4); } while (0)
  42. #define EMIT2_off32(b1, b2, off) \
  43. do { EMIT2(b1, b2); EMIT(off, 4); } while (0)
  44. #define EMIT3_off32(b1, b2, b3, off) \
  45. do { EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
  46. #define EMIT4_off32(b1, b2, b3, b4, off) \
  47. do { EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
  48. #ifdef CONFIG_X86_KERNEL_IBT
  49. #define EMIT_ENDBR() EMIT(gen_endbr(), 4)
  50. #define EMIT_ENDBR_POISON() EMIT(gen_endbr_poison(), 4)
  51. #else
  52. #define EMIT_ENDBR()
  53. #define EMIT_ENDBR_POISON()
  54. #endif
  55. static bool is_imm8(int value)
  56. {
  57. return value <= 127 && value >= -128;
  58. }
  59. /*
  60. * Let us limit the positive offset to be <= 123.
  61. * This is to ensure eventual jit convergence For the following patterns:
  62. * ...
  63. * pass4, final_proglen=4391:
  64. * ...
  65. * 20e: 48 85 ff test rdi,rdi
  66. * 211: 74 7d je 0x290
  67. * 213: 48 8b 77 00 mov rsi,QWORD PTR [rdi+0x0]
  68. * ...
  69. * 289: 48 85 ff test rdi,rdi
  70. * 28c: 74 17 je 0x2a5
  71. * 28e: e9 7f ff ff ff jmp 0x212
  72. * 293: bf 03 00 00 00 mov edi,0x3
  73. * Note that insn at 0x211 is 2-byte cond jump insn for offset 0x7d (-125)
  74. * and insn at 0x28e is 5-byte jmp insn with offset -129.
  75. *
  76. * pass5, final_proglen=4392:
  77. * ...
  78. * 20e: 48 85 ff test rdi,rdi
  79. * 211: 0f 84 80 00 00 00 je 0x297
  80. * 217: 48 8b 77 00 mov rsi,QWORD PTR [rdi+0x0]
  81. * ...
  82. * 28d: 48 85 ff test rdi,rdi
  83. * 290: 74 1a je 0x2ac
  84. * 292: eb 84 jmp 0x218
  85. * 294: bf 03 00 00 00 mov edi,0x3
  86. * Note that insn at 0x211 is 6-byte cond jump insn now since its offset
  87. * becomes 0x80 based on previous round (0x293 - 0x213 = 0x80).
  88. * At the same time, insn at 0x292 is a 2-byte insn since its offset is
  89. * -124.
  90. *
  91. * pass6 will repeat the same code as in pass4 and this will prevent
  92. * eventual convergence.
  93. *
  94. * To fix this issue, we need to break je (2->6 bytes) <-> jmp (5->2 bytes)
  95. * cycle in the above. In the above example je offset <= 0x7c should work.
  96. *
  97. * For other cases, je <-> je needs offset <= 0x7b to avoid no convergence
  98. * issue. For jmp <-> je and jmp <-> jmp cases, jmp offset <= 0x7c should
  99. * avoid no convergence issue.
  100. *
  101. * Overall, let us limit the positive offset for 8bit cond/uncond jmp insn
  102. * to maximum 123 (0x7b). This way, the jit pass can eventually converge.
  103. */
  104. static bool is_imm8_jmp_offset(int value)
  105. {
  106. return value <= 123 && value >= -128;
  107. }
  108. static bool is_simm32(s64 value)
  109. {
  110. return value == (s64)(s32)value;
  111. }
  112. static bool is_uimm32(u64 value)
  113. {
  114. return value == (u64)(u32)value;
  115. }
  116. /* mov dst, src */
  117. #define EMIT_mov(DST, SRC) \
  118. do { \
  119. if (DST != SRC) \
  120. EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
  121. } while (0)
  122. static int bpf_size_to_x86_bytes(int bpf_size)
  123. {
  124. if (bpf_size == BPF_W)
  125. return 4;
  126. else if (bpf_size == BPF_H)
  127. return 2;
  128. else if (bpf_size == BPF_B)
  129. return 1;
  130. else if (bpf_size == BPF_DW)
  131. return 4; /* imm32 */
  132. else
  133. return 0;
  134. }
  135. /*
  136. * List of x86 cond jumps opcodes (. + s8)
  137. * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
  138. */
  139. #define X86_JB 0x72
  140. #define X86_JAE 0x73
  141. #define X86_JE 0x74
  142. #define X86_JNE 0x75
  143. #define X86_JBE 0x76
  144. #define X86_JA 0x77
  145. #define X86_JL 0x7C
  146. #define X86_JGE 0x7D
  147. #define X86_JLE 0x7E
  148. #define X86_JG 0x7F
  149. /* Pick a register outside of BPF range for JIT internal work */
  150. #define AUX_REG (MAX_BPF_JIT_REG + 1)
  151. #define X86_REG_R9 (MAX_BPF_JIT_REG + 2)
  152. #define X86_REG_R12 (MAX_BPF_JIT_REG + 3)
  153. /*
  154. * The following table maps BPF registers to x86-64 registers.
  155. *
  156. * x86-64 register R12 is unused, since if used as base address
  157. * register in load/store instructions, it always needs an
  158. * extra byte of encoding and is callee saved.
  159. *
  160. * x86-64 register R9 is not used by BPF programs, but can be used by BPF
  161. * trampoline. x86-64 register R10 is used for blinding (if enabled).
  162. */
  163. static const int reg2hex[] = {
  164. [BPF_REG_0] = 0, /* RAX */
  165. [BPF_REG_1] = 7, /* RDI */
  166. [BPF_REG_2] = 6, /* RSI */
  167. [BPF_REG_3] = 2, /* RDX */
  168. [BPF_REG_4] = 1, /* RCX */
  169. [BPF_REG_5] = 0, /* R8 */
  170. [BPF_REG_6] = 3, /* RBX callee saved */
  171. [BPF_REG_7] = 5, /* R13 callee saved */
  172. [BPF_REG_8] = 6, /* R14 callee saved */
  173. [BPF_REG_9] = 7, /* R15 callee saved */
  174. [BPF_REG_FP] = 5, /* RBP readonly */
  175. [BPF_REG_AX] = 2, /* R10 temp register */
  176. [AUX_REG] = 3, /* R11 temp register */
  177. [X86_REG_R9] = 1, /* R9 register, 6th function argument */
  178. [X86_REG_R12] = 4, /* R12 callee saved */
  179. };
  180. static const int reg2pt_regs[] = {
  181. [BPF_REG_0] = offsetof(struct pt_regs, ax),
  182. [BPF_REG_1] = offsetof(struct pt_regs, di),
  183. [BPF_REG_2] = offsetof(struct pt_regs, si),
  184. [BPF_REG_3] = offsetof(struct pt_regs, dx),
  185. [BPF_REG_4] = offsetof(struct pt_regs, cx),
  186. [BPF_REG_5] = offsetof(struct pt_regs, r8),
  187. [BPF_REG_6] = offsetof(struct pt_regs, bx),
  188. [BPF_REG_7] = offsetof(struct pt_regs, r13),
  189. [BPF_REG_8] = offsetof(struct pt_regs, r14),
  190. [BPF_REG_9] = offsetof(struct pt_regs, r15),
  191. };
  192. /*
  193. * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
  194. * which need extra byte of encoding.
  195. * rax,rcx,...,rbp have simpler encoding
  196. */
  197. static bool is_ereg(u32 reg)
  198. {
  199. return (1 << reg) & (BIT(BPF_REG_5) |
  200. BIT(AUX_REG) |
  201. BIT(BPF_REG_7) |
  202. BIT(BPF_REG_8) |
  203. BIT(BPF_REG_9) |
  204. BIT(X86_REG_R9) |
  205. BIT(X86_REG_R12) |
  206. BIT(BPF_REG_AX));
  207. }
  208. /*
  209. * is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
  210. * lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
  211. * of encoding. al,cl,dl,bl have simpler encoding.
  212. */
  213. static bool is_ereg_8l(u32 reg)
  214. {
  215. return is_ereg(reg) ||
  216. (1 << reg) & (BIT(BPF_REG_1) |
  217. BIT(BPF_REG_2) |
  218. BIT(BPF_REG_FP));
  219. }
  220. static bool is_axreg(u32 reg)
  221. {
  222. return reg == BPF_REG_0;
  223. }
  224. /* Add modifiers if 'reg' maps to x86-64 registers R8..R15 */
  225. static u8 add_1mod(u8 byte, u32 reg)
  226. {
  227. if (is_ereg(reg))
  228. byte |= 1;
  229. return byte;
  230. }
  231. static u8 add_2mod(u8 byte, u32 r1, u32 r2)
  232. {
  233. if (is_ereg(r1))
  234. byte |= 1;
  235. if (is_ereg(r2))
  236. byte |= 4;
  237. return byte;
  238. }
  239. static u8 add_3mod(u8 byte, u32 r1, u32 r2, u32 index)
  240. {
  241. if (is_ereg(r1))
  242. byte |= 1;
  243. if (is_ereg(index))
  244. byte |= 2;
  245. if (is_ereg(r2))
  246. byte |= 4;
  247. return byte;
  248. }
  249. /* Encode 'dst_reg' register into x86-64 opcode 'byte' */
  250. static u8 add_1reg(u8 byte, u32 dst_reg)
  251. {
  252. return byte + reg2hex[dst_reg];
  253. }
  254. /* Encode 'dst_reg' and 'src_reg' registers into x86-64 opcode 'byte' */
  255. static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
  256. {
  257. return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
  258. }
  259. /* Some 1-byte opcodes for binary ALU operations */
  260. static u8 simple_alu_opcodes[] = {
  261. [BPF_ADD] = 0x01,
  262. [BPF_SUB] = 0x29,
  263. [BPF_AND] = 0x21,
  264. [BPF_OR] = 0x09,
  265. [BPF_XOR] = 0x31,
  266. [BPF_LSH] = 0xE0,
  267. [BPF_RSH] = 0xE8,
  268. [BPF_ARSH] = 0xF8,
  269. };
  270. static void jit_fill_hole(void *area, unsigned int size)
  271. {
  272. /* Fill whole space with INT3 instructions */
  273. memset(area, 0xcc, size);
  274. }
  275. int bpf_arch_text_invalidate(void *dst, size_t len)
  276. {
  277. return IS_ERR_OR_NULL(text_poke_set(dst, 0xcc, len));
  278. }
  279. struct jit_context {
  280. int cleanup_addr; /* Epilogue code offset */
  281. /*
  282. * Program specific offsets of labels in the code; these rely on the
  283. * JIT doing at least 2 passes, recording the position on the first
  284. * pass, only to generate the correct offset on the second pass.
  285. */
  286. int tail_call_direct_label;
  287. int tail_call_indirect_label;
  288. };
  289. /* Maximum number of bytes emitted while JITing one eBPF insn */
  290. #define BPF_MAX_INSN_SIZE 128
  291. #define BPF_INSN_SAFETY 64
  292. /* Number of bytes emit_patch() needs to generate instructions */
  293. #define X86_PATCH_SIZE 5
  294. /* Number of bytes that will be skipped on tailcall */
  295. #define X86_TAIL_CALL_OFFSET (12 + ENDBR_INSN_SIZE)
  296. static void push_r12(u8 **pprog)
  297. {
  298. u8 *prog = *pprog;
  299. EMIT2(0x41, 0x54); /* push r12 */
  300. *pprog = prog;
  301. }
  302. static void push_callee_regs(u8 **pprog, bool *callee_regs_used)
  303. {
  304. u8 *prog = *pprog;
  305. if (callee_regs_used[0])
  306. EMIT1(0x53); /* push rbx */
  307. if (callee_regs_used[1])
  308. EMIT2(0x41, 0x55); /* push r13 */
  309. if (callee_regs_used[2])
  310. EMIT2(0x41, 0x56); /* push r14 */
  311. if (callee_regs_used[3])
  312. EMIT2(0x41, 0x57); /* push r15 */
  313. *pprog = prog;
  314. }
  315. static void pop_r12(u8 **pprog)
  316. {
  317. u8 *prog = *pprog;
  318. EMIT2(0x41, 0x5C); /* pop r12 */
  319. *pprog = prog;
  320. }
  321. static void pop_callee_regs(u8 **pprog, bool *callee_regs_used)
  322. {
  323. u8 *prog = *pprog;
  324. if (callee_regs_used[3])
  325. EMIT2(0x41, 0x5F); /* pop r15 */
  326. if (callee_regs_used[2])
  327. EMIT2(0x41, 0x5E); /* pop r14 */
  328. if (callee_regs_used[1])
  329. EMIT2(0x41, 0x5D); /* pop r13 */
  330. if (callee_regs_used[0])
  331. EMIT1(0x5B); /* pop rbx */
  332. *pprog = prog;
  333. }
  334. static void emit_nops(u8 **pprog, int len)
  335. {
  336. u8 *prog = *pprog;
  337. int i, noplen;
  338. while (len > 0) {
  339. noplen = len;
  340. if (noplen > ASM_NOP_MAX)
  341. noplen = ASM_NOP_MAX;
  342. for (i = 0; i < noplen; i++)
  343. EMIT1(x86_nops[noplen][i]);
  344. len -= noplen;
  345. }
  346. *pprog = prog;
  347. }
  348. /*
  349. * Emit the various CFI preambles, see asm/cfi.h and the comments about FineIBT
  350. * in arch/x86/kernel/alternative.c
  351. */
  352. static void emit_fineibt(u8 **pprog, u32 hash)
  353. {
  354. u8 *prog = *pprog;
  355. EMIT_ENDBR();
  356. EMIT3_off32(0x41, 0x81, 0xea, hash); /* subl $hash, %r10d */
  357. EMIT2(0x74, 0x07); /* jz.d8 +7 */
  358. EMIT2(0x0f, 0x0b); /* ud2 */
  359. EMIT1(0x90); /* nop */
  360. EMIT_ENDBR_POISON();
  361. *pprog = prog;
  362. }
  363. static void emit_kcfi(u8 **pprog, u32 hash)
  364. {
  365. u8 *prog = *pprog;
  366. EMIT1_off32(0xb8, hash); /* movl $hash, %eax */
  367. #ifdef CONFIG_CALL_PADDING
  368. EMIT1(0x90);
  369. EMIT1(0x90);
  370. EMIT1(0x90);
  371. EMIT1(0x90);
  372. EMIT1(0x90);
  373. EMIT1(0x90);
  374. EMIT1(0x90);
  375. EMIT1(0x90);
  376. EMIT1(0x90);
  377. EMIT1(0x90);
  378. EMIT1(0x90);
  379. #endif
  380. EMIT_ENDBR();
  381. *pprog = prog;
  382. }
  383. static void emit_cfi(u8 **pprog, u32 hash)
  384. {
  385. u8 *prog = *pprog;
  386. switch (cfi_mode) {
  387. case CFI_FINEIBT:
  388. emit_fineibt(&prog, hash);
  389. break;
  390. case CFI_KCFI:
  391. emit_kcfi(&prog, hash);
  392. break;
  393. default:
  394. EMIT_ENDBR();
  395. break;
  396. }
  397. *pprog = prog;
  398. }
  399. static void emit_prologue_tail_call(u8 **pprog, bool is_subprog)
  400. {
  401. u8 *prog = *pprog;
  402. if (!is_subprog) {
  403. /* cmp rax, MAX_TAIL_CALL_CNT */
  404. EMIT4(0x48, 0x83, 0xF8, MAX_TAIL_CALL_CNT);
  405. EMIT2(X86_JA, 6); /* ja 6 */
  406. /* rax is tail_call_cnt if <= MAX_TAIL_CALL_CNT.
  407. * case1: entry of main prog.
  408. * case2: tail callee of main prog.
  409. */
  410. EMIT1(0x50); /* push rax */
  411. /* Make rax as tail_call_cnt_ptr. */
  412. EMIT3(0x48, 0x89, 0xE0); /* mov rax, rsp */
  413. EMIT2(0xEB, 1); /* jmp 1 */
  414. /* rax is tail_call_cnt_ptr if > MAX_TAIL_CALL_CNT.
  415. * case: tail callee of subprog.
  416. */
  417. EMIT1(0x50); /* push rax */
  418. /* push tail_call_cnt_ptr */
  419. EMIT1(0x50); /* push rax */
  420. } else { /* is_subprog */
  421. /* rax is tail_call_cnt_ptr. */
  422. EMIT1(0x50); /* push rax */
  423. EMIT1(0x50); /* push rax */
  424. }
  425. *pprog = prog;
  426. }
  427. /*
  428. * Emit x86-64 prologue code for BPF program.
  429. * bpf_tail_call helper will skip the first X86_TAIL_CALL_OFFSET bytes
  430. * while jumping to another program
  431. */
  432. static void emit_prologue(u8 **pprog, u32 stack_depth, bool ebpf_from_cbpf,
  433. bool tail_call_reachable, bool is_subprog,
  434. bool is_exception_cb)
  435. {
  436. u8 *prog = *pprog;
  437. emit_cfi(&prog, is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash);
  438. /* BPF trampoline can be made to work without these nops,
  439. * but let's waste 5 bytes for now and optimize later
  440. */
  441. emit_nops(&prog, X86_PATCH_SIZE);
  442. if (!ebpf_from_cbpf) {
  443. if (tail_call_reachable && !is_subprog)
  444. /* When it's the entry of the whole tailcall context,
  445. * zeroing rax means initialising tail_call_cnt.
  446. */
  447. EMIT3(0x48, 0x31, 0xC0); /* xor rax, rax */
  448. else
  449. /* Keep the same instruction layout. */
  450. emit_nops(&prog, 3); /* nop3 */
  451. }
  452. /* Exception callback receives FP as third parameter */
  453. if (is_exception_cb) {
  454. EMIT3(0x48, 0x89, 0xF4); /* mov rsp, rsi */
  455. EMIT3(0x48, 0x89, 0xD5); /* mov rbp, rdx */
  456. /* The main frame must have exception_boundary as true, so we
  457. * first restore those callee-saved regs from stack, before
  458. * reusing the stack frame.
  459. */
  460. pop_callee_regs(&prog, all_callee_regs_used);
  461. pop_r12(&prog);
  462. /* Reset the stack frame. */
  463. EMIT3(0x48, 0x89, 0xEC); /* mov rsp, rbp */
  464. } else {
  465. EMIT1(0x55); /* push rbp */
  466. EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
  467. }
  468. /* X86_TAIL_CALL_OFFSET is here */
  469. EMIT_ENDBR();
  470. /* sub rsp, rounded_stack_depth */
  471. if (stack_depth)
  472. EMIT3_off32(0x48, 0x81, 0xEC, round_up(stack_depth, 8));
  473. if (tail_call_reachable)
  474. emit_prologue_tail_call(&prog, is_subprog);
  475. *pprog = prog;
  476. }
  477. static int emit_patch(u8 **pprog, void *func, void *ip, u8 opcode)
  478. {
  479. u8 *prog = *pprog;
  480. s64 offset;
  481. offset = func - (ip + X86_PATCH_SIZE);
  482. if (!is_simm32(offset)) {
  483. pr_err("Target call %p is out of range\n", func);
  484. return -ERANGE;
  485. }
  486. EMIT1_off32(opcode, offset);
  487. *pprog = prog;
  488. return 0;
  489. }
  490. static int emit_call(u8 **pprog, void *func, void *ip)
  491. {
  492. return emit_patch(pprog, func, ip, 0xE8);
  493. }
  494. static int emit_rsb_call(u8 **pprog, void *func, void *ip)
  495. {
  496. OPTIMIZER_HIDE_VAR(func);
  497. ip += x86_call_depth_emit_accounting(pprog, func, ip);
  498. return emit_patch(pprog, func, ip, 0xE8);
  499. }
  500. static int emit_jump(u8 **pprog, void *func, void *ip)
  501. {
  502. return emit_patch(pprog, func, ip, 0xE9);
  503. }
  504. static int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
  505. void *old_addr, void *new_addr)
  506. {
  507. const u8 *nop_insn = x86_nops[5];
  508. u8 old_insn[X86_PATCH_SIZE];
  509. u8 new_insn[X86_PATCH_SIZE];
  510. u8 *prog;
  511. int ret;
  512. memcpy(old_insn, nop_insn, X86_PATCH_SIZE);
  513. if (old_addr) {
  514. prog = old_insn;
  515. ret = t == BPF_MOD_CALL ?
  516. emit_call(&prog, old_addr, ip) :
  517. emit_jump(&prog, old_addr, ip);
  518. if (ret)
  519. return ret;
  520. }
  521. memcpy(new_insn, nop_insn, X86_PATCH_SIZE);
  522. if (new_addr) {
  523. prog = new_insn;
  524. ret = t == BPF_MOD_CALL ?
  525. emit_call(&prog, new_addr, ip) :
  526. emit_jump(&prog, new_addr, ip);
  527. if (ret)
  528. return ret;
  529. }
  530. ret = -EBUSY;
  531. mutex_lock(&text_mutex);
  532. if (memcmp(ip, old_insn, X86_PATCH_SIZE))
  533. goto out;
  534. ret = 1;
  535. if (memcmp(ip, new_insn, X86_PATCH_SIZE)) {
  536. text_poke_bp(ip, new_insn, X86_PATCH_SIZE, NULL);
  537. ret = 0;
  538. }
  539. out:
  540. mutex_unlock(&text_mutex);
  541. return ret;
  542. }
  543. int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
  544. void *old_addr, void *new_addr)
  545. {
  546. if (!is_kernel_text((long)ip) &&
  547. !is_bpf_text_address((long)ip))
  548. /* BPF poking in modules is not supported */
  549. return -EINVAL;
  550. /*
  551. * See emit_prologue(), for IBT builds the trampoline hook is preceded
  552. * with an ENDBR instruction.
  553. */
  554. if (is_endbr(*(u32 *)ip))
  555. ip += ENDBR_INSN_SIZE;
  556. return __bpf_arch_text_poke(ip, t, old_addr, new_addr);
  557. }
  558. #define EMIT_LFENCE() EMIT3(0x0F, 0xAE, 0xE8)
  559. static void emit_indirect_jump(u8 **pprog, int reg, u8 *ip)
  560. {
  561. u8 *prog = *pprog;
  562. if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_LFENCE)) {
  563. EMIT_LFENCE();
  564. EMIT2(0xFF, 0xE0 + reg);
  565. } else if (cpu_feature_enabled(X86_FEATURE_RETPOLINE)) {
  566. OPTIMIZER_HIDE_VAR(reg);
  567. if (cpu_feature_enabled(X86_FEATURE_CALL_DEPTH))
  568. emit_jump(&prog, &__x86_indirect_jump_thunk_array[reg], ip);
  569. else
  570. emit_jump(&prog, &__x86_indirect_thunk_array[reg], ip);
  571. } else {
  572. EMIT2(0xFF, 0xE0 + reg); /* jmp *%\reg */
  573. if (IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) || IS_ENABLED(CONFIG_MITIGATION_SLS))
  574. EMIT1(0xCC); /* int3 */
  575. }
  576. *pprog = prog;
  577. }
  578. static void emit_return(u8 **pprog, u8 *ip)
  579. {
  580. u8 *prog = *pprog;
  581. if (cpu_feature_enabled(X86_FEATURE_RETHUNK)) {
  582. emit_jump(&prog, x86_return_thunk, ip);
  583. } else {
  584. EMIT1(0xC3); /* ret */
  585. if (IS_ENABLED(CONFIG_MITIGATION_SLS))
  586. EMIT1(0xCC); /* int3 */
  587. }
  588. *pprog = prog;
  589. }
  590. #define BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack) (-16 - round_up(stack, 8))
  591. /*
  592. * Generate the following code:
  593. *
  594. * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
  595. * if (index >= array->map.max_entries)
  596. * goto out;
  597. * if ((*tcc_ptr)++ >= MAX_TAIL_CALL_CNT)
  598. * goto out;
  599. * prog = array->ptrs[index];
  600. * if (prog == NULL)
  601. * goto out;
  602. * goto *(prog->bpf_func + prologue_size);
  603. * out:
  604. */
  605. static void emit_bpf_tail_call_indirect(struct bpf_prog *bpf_prog,
  606. u8 **pprog, bool *callee_regs_used,
  607. u32 stack_depth, u8 *ip,
  608. struct jit_context *ctx)
  609. {
  610. int tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack_depth);
  611. u8 *prog = *pprog, *start = *pprog;
  612. int offset;
  613. /*
  614. * rdi - pointer to ctx
  615. * rsi - pointer to bpf_array
  616. * rdx - index in bpf_array
  617. */
  618. /*
  619. * if (index >= array->map.max_entries)
  620. * goto out;
  621. */
  622. EMIT2(0x89, 0xD2); /* mov edx, edx */
  623. EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
  624. offsetof(struct bpf_array, map.max_entries));
  625. offset = ctx->tail_call_indirect_label - (prog + 2 - start);
  626. EMIT2(X86_JBE, offset); /* jbe out */
  627. /*
  628. * if ((*tcc_ptr)++ >= MAX_TAIL_CALL_CNT)
  629. * goto out;
  630. */
  631. EMIT3_off32(0x48, 0x8B, 0x85, tcc_ptr_off); /* mov rax, qword ptr [rbp - tcc_ptr_off] */
  632. EMIT4(0x48, 0x83, 0x38, MAX_TAIL_CALL_CNT); /* cmp qword ptr [rax], MAX_TAIL_CALL_CNT */
  633. offset = ctx->tail_call_indirect_label - (prog + 2 - start);
  634. EMIT2(X86_JAE, offset); /* jae out */
  635. /* prog = array->ptrs[index]; */
  636. EMIT4_off32(0x48, 0x8B, 0x8C, 0xD6, /* mov rcx, [rsi + rdx * 8 + offsetof(...)] */
  637. offsetof(struct bpf_array, ptrs));
  638. /*
  639. * if (prog == NULL)
  640. * goto out;
  641. */
  642. EMIT3(0x48, 0x85, 0xC9); /* test rcx,rcx */
  643. offset = ctx->tail_call_indirect_label - (prog + 2 - start);
  644. EMIT2(X86_JE, offset); /* je out */
  645. /* Inc tail_call_cnt if the slot is populated. */
  646. EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
  647. if (bpf_prog->aux->exception_boundary) {
  648. pop_callee_regs(&prog, all_callee_regs_used);
  649. pop_r12(&prog);
  650. } else {
  651. pop_callee_regs(&prog, callee_regs_used);
  652. if (bpf_arena_get_kern_vm_start(bpf_prog->aux->arena))
  653. pop_r12(&prog);
  654. }
  655. /* Pop tail_call_cnt_ptr. */
  656. EMIT1(0x58); /* pop rax */
  657. /* Pop tail_call_cnt, if it's main prog.
  658. * Pop tail_call_cnt_ptr, if it's subprog.
  659. */
  660. EMIT1(0x58); /* pop rax */
  661. if (stack_depth)
  662. EMIT3_off32(0x48, 0x81, 0xC4, /* add rsp, sd */
  663. round_up(stack_depth, 8));
  664. /* goto *(prog->bpf_func + X86_TAIL_CALL_OFFSET); */
  665. EMIT4(0x48, 0x8B, 0x49, /* mov rcx, qword ptr [rcx + 32] */
  666. offsetof(struct bpf_prog, bpf_func));
  667. EMIT4(0x48, 0x83, 0xC1, /* add rcx, X86_TAIL_CALL_OFFSET */
  668. X86_TAIL_CALL_OFFSET);
  669. /*
  670. * Now we're ready to jump into next BPF program
  671. * rdi == ctx (1st arg)
  672. * rcx == prog->bpf_func + X86_TAIL_CALL_OFFSET
  673. */
  674. emit_indirect_jump(&prog, 1 /* rcx */, ip + (prog - start));
  675. /* out: */
  676. ctx->tail_call_indirect_label = prog - start;
  677. *pprog = prog;
  678. }
  679. static void emit_bpf_tail_call_direct(struct bpf_prog *bpf_prog,
  680. struct bpf_jit_poke_descriptor *poke,
  681. u8 **pprog, u8 *ip,
  682. bool *callee_regs_used, u32 stack_depth,
  683. struct jit_context *ctx)
  684. {
  685. int tcc_ptr_off = BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack_depth);
  686. u8 *prog = *pprog, *start = *pprog;
  687. int offset;
  688. /*
  689. * if ((*tcc_ptr)++ >= MAX_TAIL_CALL_CNT)
  690. * goto out;
  691. */
  692. EMIT3_off32(0x48, 0x8B, 0x85, tcc_ptr_off); /* mov rax, qword ptr [rbp - tcc_ptr_off] */
  693. EMIT4(0x48, 0x83, 0x38, MAX_TAIL_CALL_CNT); /* cmp qword ptr [rax], MAX_TAIL_CALL_CNT */
  694. offset = ctx->tail_call_direct_label - (prog + 2 - start);
  695. EMIT2(X86_JAE, offset); /* jae out */
  696. poke->tailcall_bypass = ip + (prog - start);
  697. poke->adj_off = X86_TAIL_CALL_OFFSET;
  698. poke->tailcall_target = ip + ctx->tail_call_direct_label - X86_PATCH_SIZE;
  699. poke->bypass_addr = (u8 *)poke->tailcall_target + X86_PATCH_SIZE;
  700. emit_jump(&prog, (u8 *)poke->tailcall_target + X86_PATCH_SIZE,
  701. poke->tailcall_bypass);
  702. /* Inc tail_call_cnt if the slot is populated. */
  703. EMIT4(0x48, 0x83, 0x00, 0x01); /* add qword ptr [rax], 1 */
  704. if (bpf_prog->aux->exception_boundary) {
  705. pop_callee_regs(&prog, all_callee_regs_used);
  706. pop_r12(&prog);
  707. } else {
  708. pop_callee_regs(&prog, callee_regs_used);
  709. if (bpf_arena_get_kern_vm_start(bpf_prog->aux->arena))
  710. pop_r12(&prog);
  711. }
  712. /* Pop tail_call_cnt_ptr. */
  713. EMIT1(0x58); /* pop rax */
  714. /* Pop tail_call_cnt, if it's main prog.
  715. * Pop tail_call_cnt_ptr, if it's subprog.
  716. */
  717. EMIT1(0x58); /* pop rax */
  718. if (stack_depth)
  719. EMIT3_off32(0x48, 0x81, 0xC4, round_up(stack_depth, 8));
  720. emit_nops(&prog, X86_PATCH_SIZE);
  721. /* out: */
  722. ctx->tail_call_direct_label = prog - start;
  723. *pprog = prog;
  724. }
  725. static void bpf_tail_call_direct_fixup(struct bpf_prog *prog)
  726. {
  727. struct bpf_jit_poke_descriptor *poke;
  728. struct bpf_array *array;
  729. struct bpf_prog *target;
  730. int i, ret;
  731. for (i = 0; i < prog->aux->size_poke_tab; i++) {
  732. poke = &prog->aux->poke_tab[i];
  733. if (poke->aux && poke->aux != prog->aux)
  734. continue;
  735. WARN_ON_ONCE(READ_ONCE(poke->tailcall_target_stable));
  736. if (poke->reason != BPF_POKE_REASON_TAIL_CALL)
  737. continue;
  738. array = container_of(poke->tail_call.map, struct bpf_array, map);
  739. mutex_lock(&array->aux->poke_mutex);
  740. target = array->ptrs[poke->tail_call.key];
  741. if (target) {
  742. ret = __bpf_arch_text_poke(poke->tailcall_target,
  743. BPF_MOD_JUMP, NULL,
  744. (u8 *)target->bpf_func +
  745. poke->adj_off);
  746. BUG_ON(ret < 0);
  747. ret = __bpf_arch_text_poke(poke->tailcall_bypass,
  748. BPF_MOD_JUMP,
  749. (u8 *)poke->tailcall_target +
  750. X86_PATCH_SIZE, NULL);
  751. BUG_ON(ret < 0);
  752. }
  753. WRITE_ONCE(poke->tailcall_target_stable, true);
  754. mutex_unlock(&array->aux->poke_mutex);
  755. }
  756. }
  757. static void emit_mov_imm32(u8 **pprog, bool sign_propagate,
  758. u32 dst_reg, const u32 imm32)
  759. {
  760. u8 *prog = *pprog;
  761. u8 b1, b2, b3;
  762. /*
  763. * Optimization: if imm32 is positive, use 'mov %eax, imm32'
  764. * (which zero-extends imm32) to save 2 bytes.
  765. */
  766. if (sign_propagate && (s32)imm32 < 0) {
  767. /* 'mov %rax, imm32' sign extends imm32 */
  768. b1 = add_1mod(0x48, dst_reg);
  769. b2 = 0xC7;
  770. b3 = 0xC0;
  771. EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
  772. goto done;
  773. }
  774. /*
  775. * Optimization: if imm32 is zero, use 'xor %eax, %eax'
  776. * to save 3 bytes.
  777. */
  778. if (imm32 == 0) {
  779. if (is_ereg(dst_reg))
  780. EMIT1(add_2mod(0x40, dst_reg, dst_reg));
  781. b2 = 0x31; /* xor */
  782. b3 = 0xC0;
  783. EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
  784. goto done;
  785. }
  786. /* mov %eax, imm32 */
  787. if (is_ereg(dst_reg))
  788. EMIT1(add_1mod(0x40, dst_reg));
  789. EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
  790. done:
  791. *pprog = prog;
  792. }
  793. static void emit_mov_imm64(u8 **pprog, u32 dst_reg,
  794. const u32 imm32_hi, const u32 imm32_lo)
  795. {
  796. u64 imm64 = ((u64)imm32_hi << 32) | (u32)imm32_lo;
  797. u8 *prog = *pprog;
  798. if (is_uimm32(imm64)) {
  799. /*
  800. * For emitting plain u32, where sign bit must not be
  801. * propagated LLVM tends to load imm64 over mov32
  802. * directly, so save couple of bytes by just doing
  803. * 'mov %eax, imm32' instead.
  804. */
  805. emit_mov_imm32(&prog, false, dst_reg, imm32_lo);
  806. } else if (is_simm32(imm64)) {
  807. emit_mov_imm32(&prog, true, dst_reg, imm32_lo);
  808. } else {
  809. /* movabsq rax, imm64 */
  810. EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
  811. EMIT(imm32_lo, 4);
  812. EMIT(imm32_hi, 4);
  813. }
  814. *pprog = prog;
  815. }
  816. static void emit_mov_reg(u8 **pprog, bool is64, u32 dst_reg, u32 src_reg)
  817. {
  818. u8 *prog = *pprog;
  819. if (is64) {
  820. /* mov dst, src */
  821. EMIT_mov(dst_reg, src_reg);
  822. } else {
  823. /* mov32 dst, src */
  824. if (is_ereg(dst_reg) || is_ereg(src_reg))
  825. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  826. EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
  827. }
  828. *pprog = prog;
  829. }
  830. static void emit_movsx_reg(u8 **pprog, int num_bits, bool is64, u32 dst_reg,
  831. u32 src_reg)
  832. {
  833. u8 *prog = *pprog;
  834. if (is64) {
  835. /* movs[b,w,l]q dst, src */
  836. if (num_bits == 8)
  837. EMIT4(add_2mod(0x48, src_reg, dst_reg), 0x0f, 0xbe,
  838. add_2reg(0xC0, src_reg, dst_reg));
  839. else if (num_bits == 16)
  840. EMIT4(add_2mod(0x48, src_reg, dst_reg), 0x0f, 0xbf,
  841. add_2reg(0xC0, src_reg, dst_reg));
  842. else if (num_bits == 32)
  843. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x63,
  844. add_2reg(0xC0, src_reg, dst_reg));
  845. } else {
  846. /* movs[b,w]l dst, src */
  847. if (num_bits == 8) {
  848. EMIT4(add_2mod(0x40, src_reg, dst_reg), 0x0f, 0xbe,
  849. add_2reg(0xC0, src_reg, dst_reg));
  850. } else if (num_bits == 16) {
  851. if (is_ereg(dst_reg) || is_ereg(src_reg))
  852. EMIT1(add_2mod(0x40, src_reg, dst_reg));
  853. EMIT3(add_2mod(0x0f, src_reg, dst_reg), 0xbf,
  854. add_2reg(0xC0, src_reg, dst_reg));
  855. }
  856. }
  857. *pprog = prog;
  858. }
  859. /* Emit the suffix (ModR/M etc) for addressing *(ptr_reg + off) and val_reg */
  860. static void emit_insn_suffix(u8 **pprog, u32 ptr_reg, u32 val_reg, int off)
  861. {
  862. u8 *prog = *pprog;
  863. if (is_imm8(off)) {
  864. /* 1-byte signed displacement.
  865. *
  866. * If off == 0 we could skip this and save one extra byte, but
  867. * special case of x86 R13 which always needs an offset is not
  868. * worth the hassle
  869. */
  870. EMIT2(add_2reg(0x40, ptr_reg, val_reg), off);
  871. } else {
  872. /* 4-byte signed displacement */
  873. EMIT1_off32(add_2reg(0x80, ptr_reg, val_reg), off);
  874. }
  875. *pprog = prog;
  876. }
  877. static void emit_insn_suffix_SIB(u8 **pprog, u32 ptr_reg, u32 val_reg, u32 index_reg, int off)
  878. {
  879. u8 *prog = *pprog;
  880. if (is_imm8(off)) {
  881. EMIT3(add_2reg(0x44, BPF_REG_0, val_reg), add_2reg(0, ptr_reg, index_reg) /* SIB */, off);
  882. } else {
  883. EMIT2_off32(add_2reg(0x84, BPF_REG_0, val_reg), add_2reg(0, ptr_reg, index_reg) /* SIB */, off);
  884. }
  885. *pprog = prog;
  886. }
  887. /*
  888. * Emit a REX byte if it will be necessary to address these registers
  889. */
  890. static void maybe_emit_mod(u8 **pprog, u32 dst_reg, u32 src_reg, bool is64)
  891. {
  892. u8 *prog = *pprog;
  893. if (is64)
  894. EMIT1(add_2mod(0x48, dst_reg, src_reg));
  895. else if (is_ereg(dst_reg) || is_ereg(src_reg))
  896. EMIT1(add_2mod(0x40, dst_reg, src_reg));
  897. *pprog = prog;
  898. }
  899. /*
  900. * Similar version of maybe_emit_mod() for a single register
  901. */
  902. static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64)
  903. {
  904. u8 *prog = *pprog;
  905. if (is64)
  906. EMIT1(add_1mod(0x48, reg));
  907. else if (is_ereg(reg))
  908. EMIT1(add_1mod(0x40, reg));
  909. *pprog = prog;
  910. }
  911. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  912. static void emit_ldx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
  913. {
  914. u8 *prog = *pprog;
  915. switch (size) {
  916. case BPF_B:
  917. /* Emit 'movzx rax, byte ptr [rax + off]' */
  918. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
  919. break;
  920. case BPF_H:
  921. /* Emit 'movzx rax, word ptr [rax + off]' */
  922. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
  923. break;
  924. case BPF_W:
  925. /* Emit 'mov eax, dword ptr [rax+0x14]' */
  926. if (is_ereg(dst_reg) || is_ereg(src_reg))
  927. EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
  928. else
  929. EMIT1(0x8B);
  930. break;
  931. case BPF_DW:
  932. /* Emit 'mov rax, qword ptr [rax+0x14]' */
  933. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
  934. break;
  935. }
  936. emit_insn_suffix(&prog, src_reg, dst_reg, off);
  937. *pprog = prog;
  938. }
  939. /* LDSX: dst_reg = *(s8*)(src_reg + off) */
  940. static void emit_ldsx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
  941. {
  942. u8 *prog = *pprog;
  943. switch (size) {
  944. case BPF_B:
  945. /* Emit 'movsx rax, byte ptr [rax + off]' */
  946. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xBE);
  947. break;
  948. case BPF_H:
  949. /* Emit 'movsx rax, word ptr [rax + off]' */
  950. EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xBF);
  951. break;
  952. case BPF_W:
  953. /* Emit 'movsx rax, dword ptr [rax+0x14]' */
  954. EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x63);
  955. break;
  956. }
  957. emit_insn_suffix(&prog, src_reg, dst_reg, off);
  958. *pprog = prog;
  959. }
  960. static void emit_ldx_index(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, u32 index_reg, int off)
  961. {
  962. u8 *prog = *pprog;
  963. switch (size) {
  964. case BPF_B:
  965. /* movzx rax, byte ptr [rax + r12 + off] */
  966. EMIT3(add_3mod(0x40, src_reg, dst_reg, index_reg), 0x0F, 0xB6);
  967. break;
  968. case BPF_H:
  969. /* movzx rax, word ptr [rax + r12 + off] */
  970. EMIT3(add_3mod(0x40, src_reg, dst_reg, index_reg), 0x0F, 0xB7);
  971. break;
  972. case BPF_W:
  973. /* mov eax, dword ptr [rax + r12 + off] */
  974. EMIT2(add_3mod(0x40, src_reg, dst_reg, index_reg), 0x8B);
  975. break;
  976. case BPF_DW:
  977. /* mov rax, qword ptr [rax + r12 + off] */
  978. EMIT2(add_3mod(0x48, src_reg, dst_reg, index_reg), 0x8B);
  979. break;
  980. }
  981. emit_insn_suffix_SIB(&prog, src_reg, dst_reg, index_reg, off);
  982. *pprog = prog;
  983. }
  984. static void emit_ldx_r12(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
  985. {
  986. emit_ldx_index(pprog, size, dst_reg, src_reg, X86_REG_R12, off);
  987. }
  988. /* STX: *(u8*)(dst_reg + off) = src_reg */
  989. static void emit_stx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
  990. {
  991. u8 *prog = *pprog;
  992. switch (size) {
  993. case BPF_B:
  994. /* Emit 'mov byte ptr [rax + off], al' */
  995. if (is_ereg(dst_reg) || is_ereg_8l(src_reg))
  996. /* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
  997. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
  998. else
  999. EMIT1(0x88);
  1000. break;
  1001. case BPF_H:
  1002. if (is_ereg(dst_reg) || is_ereg(src_reg))
  1003. EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
  1004. else
  1005. EMIT2(0x66, 0x89);
  1006. break;
  1007. case BPF_W:
  1008. if (is_ereg(dst_reg) || is_ereg(src_reg))
  1009. EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
  1010. else
  1011. EMIT1(0x89);
  1012. break;
  1013. case BPF_DW:
  1014. EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
  1015. break;
  1016. }
  1017. emit_insn_suffix(&prog, dst_reg, src_reg, off);
  1018. *pprog = prog;
  1019. }
  1020. /* STX: *(u8*)(dst_reg + index_reg + off) = src_reg */
  1021. static void emit_stx_index(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, u32 index_reg, int off)
  1022. {
  1023. u8 *prog = *pprog;
  1024. switch (size) {
  1025. case BPF_B:
  1026. /* mov byte ptr [rax + r12 + off], al */
  1027. EMIT2(add_3mod(0x40, dst_reg, src_reg, index_reg), 0x88);
  1028. break;
  1029. case BPF_H:
  1030. /* mov word ptr [rax + r12 + off], ax */
  1031. EMIT3(0x66, add_3mod(0x40, dst_reg, src_reg, index_reg), 0x89);
  1032. break;
  1033. case BPF_W:
  1034. /* mov dword ptr [rax + r12 + 1], eax */
  1035. EMIT2(add_3mod(0x40, dst_reg, src_reg, index_reg), 0x89);
  1036. break;
  1037. case BPF_DW:
  1038. /* mov qword ptr [rax + r12 + 1], rax */
  1039. EMIT2(add_3mod(0x48, dst_reg, src_reg, index_reg), 0x89);
  1040. break;
  1041. }
  1042. emit_insn_suffix_SIB(&prog, dst_reg, src_reg, index_reg, off);
  1043. *pprog = prog;
  1044. }
  1045. static void emit_stx_r12(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
  1046. {
  1047. emit_stx_index(pprog, size, dst_reg, src_reg, X86_REG_R12, off);
  1048. }
  1049. /* ST: *(u8*)(dst_reg + index_reg + off) = imm32 */
  1050. static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int off, int imm)
  1051. {
  1052. u8 *prog = *pprog;
  1053. switch (size) {
  1054. case BPF_B:
  1055. /* mov byte ptr [rax + r12 + off], imm8 */
  1056. EMIT2(add_3mod(0x40, dst_reg, 0, index_reg), 0xC6);
  1057. break;
  1058. case BPF_H:
  1059. /* mov word ptr [rax + r12 + off], imm16 */
  1060. EMIT3(0x66, add_3mod(0x40, dst_reg, 0, index_reg), 0xC7);
  1061. break;
  1062. case BPF_W:
  1063. /* mov dword ptr [rax + r12 + 1], imm32 */
  1064. EMIT2(add_3mod(0x40, dst_reg, 0, index_reg), 0xC7);
  1065. break;
  1066. case BPF_DW:
  1067. /* mov qword ptr [rax + r12 + 1], imm32 */
  1068. EMIT2(add_3mod(0x48, dst_reg, 0, index_reg), 0xC7);
  1069. break;
  1070. }
  1071. emit_insn_suffix_SIB(&prog, dst_reg, 0, index_reg, off);
  1072. EMIT(imm, bpf_size_to_x86_bytes(size));
  1073. *pprog = prog;
  1074. }
  1075. static void emit_st_r12(u8 **pprog, u32 size, u32 dst_reg, int off, int imm)
  1076. {
  1077. emit_st_index(pprog, size, dst_reg, X86_REG_R12, off, imm);
  1078. }
  1079. static int emit_atomic(u8 **pprog, u8 atomic_op,
  1080. u32 dst_reg, u32 src_reg, s16 off, u8 bpf_size)
  1081. {
  1082. u8 *prog = *pprog;
  1083. EMIT1(0xF0); /* lock prefix */
  1084. maybe_emit_mod(&prog, dst_reg, src_reg, bpf_size == BPF_DW);
  1085. /* emit opcode */
  1086. switch (atomic_op) {
  1087. case BPF_ADD:
  1088. case BPF_AND:
  1089. case BPF_OR:
  1090. case BPF_XOR:
  1091. /* lock *(u32/u64*)(dst_reg + off) <op>= src_reg */
  1092. EMIT1(simple_alu_opcodes[atomic_op]);
  1093. break;
  1094. case BPF_ADD | BPF_FETCH:
  1095. /* src_reg = atomic_fetch_add(dst_reg + off, src_reg); */
  1096. EMIT2(0x0F, 0xC1);
  1097. break;
  1098. case BPF_XCHG:
  1099. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  1100. EMIT1(0x87);
  1101. break;
  1102. case BPF_CMPXCHG:
  1103. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  1104. EMIT2(0x0F, 0xB1);
  1105. break;
  1106. default:
  1107. pr_err("bpf_jit: unknown atomic opcode %02x\n", atomic_op);
  1108. return -EFAULT;
  1109. }
  1110. emit_insn_suffix(&prog, dst_reg, src_reg, off);
  1111. *pprog = prog;
  1112. return 0;
  1113. }
  1114. static int emit_atomic_index(u8 **pprog, u8 atomic_op, u32 size,
  1115. u32 dst_reg, u32 src_reg, u32 index_reg, int off)
  1116. {
  1117. u8 *prog = *pprog;
  1118. EMIT1(0xF0); /* lock prefix */
  1119. switch (size) {
  1120. case BPF_W:
  1121. EMIT1(add_3mod(0x40, dst_reg, src_reg, index_reg));
  1122. break;
  1123. case BPF_DW:
  1124. EMIT1(add_3mod(0x48, dst_reg, src_reg, index_reg));
  1125. break;
  1126. default:
  1127. pr_err("bpf_jit: 1 and 2 byte atomics are not supported\n");
  1128. return -EFAULT;
  1129. }
  1130. /* emit opcode */
  1131. switch (atomic_op) {
  1132. case BPF_ADD:
  1133. case BPF_AND:
  1134. case BPF_OR:
  1135. case BPF_XOR:
  1136. /* lock *(u32/u64*)(dst_reg + idx_reg + off) <op>= src_reg */
  1137. EMIT1(simple_alu_opcodes[atomic_op]);
  1138. break;
  1139. case BPF_ADD | BPF_FETCH:
  1140. /* src_reg = atomic_fetch_add(dst_reg + idx_reg + off, src_reg); */
  1141. EMIT2(0x0F, 0xC1);
  1142. break;
  1143. case BPF_XCHG:
  1144. /* src_reg = atomic_xchg(dst_reg + idx_reg + off, src_reg); */
  1145. EMIT1(0x87);
  1146. break;
  1147. case BPF_CMPXCHG:
  1148. /* r0 = atomic_cmpxchg(dst_reg + idx_reg + off, r0, src_reg); */
  1149. EMIT2(0x0F, 0xB1);
  1150. break;
  1151. default:
  1152. pr_err("bpf_jit: unknown atomic opcode %02x\n", atomic_op);
  1153. return -EFAULT;
  1154. }
  1155. emit_insn_suffix_SIB(&prog, dst_reg, src_reg, index_reg, off);
  1156. *pprog = prog;
  1157. return 0;
  1158. }
  1159. #define DONT_CLEAR 1
  1160. bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)
  1161. {
  1162. u32 reg = x->fixup >> 8;
  1163. /* jump over faulting load and clear dest register */
  1164. if (reg != DONT_CLEAR)
  1165. *(unsigned long *)((void *)regs + reg) = 0;
  1166. regs->ip += x->fixup & 0xff;
  1167. return true;
  1168. }
  1169. static void detect_reg_usage(struct bpf_insn *insn, int insn_cnt,
  1170. bool *regs_used)
  1171. {
  1172. int i;
  1173. for (i = 1; i <= insn_cnt; i++, insn++) {
  1174. if (insn->dst_reg == BPF_REG_6 || insn->src_reg == BPF_REG_6)
  1175. regs_used[0] = true;
  1176. if (insn->dst_reg == BPF_REG_7 || insn->src_reg == BPF_REG_7)
  1177. regs_used[1] = true;
  1178. if (insn->dst_reg == BPF_REG_8 || insn->src_reg == BPF_REG_8)
  1179. regs_used[2] = true;
  1180. if (insn->dst_reg == BPF_REG_9 || insn->src_reg == BPF_REG_9)
  1181. regs_used[3] = true;
  1182. }
  1183. }
  1184. /* emit the 3-byte VEX prefix
  1185. *
  1186. * r: same as rex.r, extra bit for ModRM reg field
  1187. * x: same as rex.x, extra bit for SIB index field
  1188. * b: same as rex.b, extra bit for ModRM r/m, or SIB base
  1189. * m: opcode map select, encoding escape bytes e.g. 0x0f38
  1190. * w: same as rex.w (32 bit or 64 bit) or opcode specific
  1191. * src_reg2: additional source reg (encoded as BPF reg)
  1192. * l: vector length (128 bit or 256 bit) or reserved
  1193. * pp: opcode prefix (none, 0x66, 0xf2 or 0xf3)
  1194. */
  1195. static void emit_3vex(u8 **pprog, bool r, bool x, bool b, u8 m,
  1196. bool w, u8 src_reg2, bool l, u8 pp)
  1197. {
  1198. u8 *prog = *pprog;
  1199. const u8 b0 = 0xc4; /* first byte of 3-byte VEX prefix */
  1200. u8 b1, b2;
  1201. u8 vvvv = reg2hex[src_reg2];
  1202. /* reg2hex gives only the lower 3 bit of vvvv */
  1203. if (is_ereg(src_reg2))
  1204. vvvv |= 1 << 3;
  1205. /*
  1206. * 2nd byte of 3-byte VEX prefix
  1207. * ~ means bit inverted encoding
  1208. *
  1209. * 7 0
  1210. * +---+---+---+---+---+---+---+---+
  1211. * |~R |~X |~B | m |
  1212. * +---+---+---+---+---+---+---+---+
  1213. */
  1214. b1 = (!r << 7) | (!x << 6) | (!b << 5) | (m & 0x1f);
  1215. /*
  1216. * 3rd byte of 3-byte VEX prefix
  1217. *
  1218. * 7 0
  1219. * +---+---+---+---+---+---+---+---+
  1220. * | W | ~vvvv | L | pp |
  1221. * +---+---+---+---+---+---+---+---+
  1222. */
  1223. b2 = (w << 7) | ((~vvvv & 0xf) << 3) | (l << 2) | (pp & 3);
  1224. EMIT3(b0, b1, b2);
  1225. *pprog = prog;
  1226. }
  1227. /* emit BMI2 shift instruction */
  1228. static void emit_shiftx(u8 **pprog, u32 dst_reg, u8 src_reg, bool is64, u8 op)
  1229. {
  1230. u8 *prog = *pprog;
  1231. bool r = is_ereg(dst_reg);
  1232. u8 m = 2; /* escape code 0f38 */
  1233. emit_3vex(&prog, r, false, r, m, is64, src_reg, false, op);
  1234. EMIT2(0xf7, add_2reg(0xC0, dst_reg, dst_reg));
  1235. *pprog = prog;
  1236. }
  1237. #define INSN_SZ_DIFF (((addrs[i] - addrs[i - 1]) - (prog - temp)))
  1238. #define __LOAD_TCC_PTR(off) \
  1239. EMIT3_off32(0x48, 0x8B, 0x85, off)
  1240. /* mov rax, qword ptr [rbp - rounded_stack_depth - 16] */
  1241. #define LOAD_TAIL_CALL_CNT_PTR(stack) \
  1242. __LOAD_TCC_PTR(BPF_TAIL_CALL_CNT_PTR_STACK_OFF(stack))
  1243. static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image,
  1244. int oldproglen, struct jit_context *ctx, bool jmp_padding)
  1245. {
  1246. bool tail_call_reachable = bpf_prog->aux->tail_call_reachable;
  1247. struct bpf_insn *insn = bpf_prog->insnsi;
  1248. bool callee_regs_used[4] = {};
  1249. int insn_cnt = bpf_prog->len;
  1250. bool seen_exit = false;
  1251. u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
  1252. u64 arena_vm_start, user_vm_start;
  1253. int i, excnt = 0;
  1254. int ilen, proglen = 0;
  1255. u8 *prog = temp;
  1256. int err;
  1257. arena_vm_start = bpf_arena_get_kern_vm_start(bpf_prog->aux->arena);
  1258. user_vm_start = bpf_arena_get_user_vm_start(bpf_prog->aux->arena);
  1259. detect_reg_usage(insn, insn_cnt, callee_regs_used);
  1260. emit_prologue(&prog, bpf_prog->aux->stack_depth,
  1261. bpf_prog_was_classic(bpf_prog), tail_call_reachable,
  1262. bpf_is_subprog(bpf_prog), bpf_prog->aux->exception_cb);
  1263. /* Exception callback will clobber callee regs for its own use, and
  1264. * restore the original callee regs from main prog's stack frame.
  1265. */
  1266. if (bpf_prog->aux->exception_boundary) {
  1267. /* We also need to save r12, which is not mapped to any BPF
  1268. * register, as we throw after entry into the kernel, which may
  1269. * overwrite r12.
  1270. */
  1271. push_r12(&prog);
  1272. push_callee_regs(&prog, all_callee_regs_used);
  1273. } else {
  1274. if (arena_vm_start)
  1275. push_r12(&prog);
  1276. push_callee_regs(&prog, callee_regs_used);
  1277. }
  1278. if (arena_vm_start)
  1279. emit_mov_imm64(&prog, X86_REG_R12,
  1280. arena_vm_start >> 32, (u32) arena_vm_start);
  1281. ilen = prog - temp;
  1282. if (rw_image)
  1283. memcpy(rw_image + proglen, temp, ilen);
  1284. proglen += ilen;
  1285. addrs[0] = proglen;
  1286. prog = temp;
  1287. for (i = 1; i <= insn_cnt; i++, insn++) {
  1288. const s32 imm32 = insn->imm;
  1289. u32 dst_reg = insn->dst_reg;
  1290. u32 src_reg = insn->src_reg;
  1291. u8 b2 = 0, b3 = 0;
  1292. u8 *start_of_ldx;
  1293. s64 jmp_offset;
  1294. s16 insn_off;
  1295. u8 jmp_cond;
  1296. u8 *func;
  1297. int nops;
  1298. switch (insn->code) {
  1299. /* ALU */
  1300. case BPF_ALU | BPF_ADD | BPF_X:
  1301. case BPF_ALU | BPF_SUB | BPF_X:
  1302. case BPF_ALU | BPF_AND | BPF_X:
  1303. case BPF_ALU | BPF_OR | BPF_X:
  1304. case BPF_ALU | BPF_XOR | BPF_X:
  1305. case BPF_ALU64 | BPF_ADD | BPF_X:
  1306. case BPF_ALU64 | BPF_SUB | BPF_X:
  1307. case BPF_ALU64 | BPF_AND | BPF_X:
  1308. case BPF_ALU64 | BPF_OR | BPF_X:
  1309. case BPF_ALU64 | BPF_XOR | BPF_X:
  1310. maybe_emit_mod(&prog, dst_reg, src_reg,
  1311. BPF_CLASS(insn->code) == BPF_ALU64);
  1312. b2 = simple_alu_opcodes[BPF_OP(insn->code)];
  1313. EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
  1314. break;
  1315. case BPF_ALU64 | BPF_MOV | BPF_X:
  1316. if (insn_is_cast_user(insn)) {
  1317. if (dst_reg != src_reg)
  1318. /* 32-bit mov */
  1319. emit_mov_reg(&prog, false, dst_reg, src_reg);
  1320. /* shl dst_reg, 32 */
  1321. maybe_emit_1mod(&prog, dst_reg, true);
  1322. EMIT3(0xC1, add_1reg(0xE0, dst_reg), 32);
  1323. /* or dst_reg, user_vm_start */
  1324. maybe_emit_1mod(&prog, dst_reg, true);
  1325. if (is_axreg(dst_reg))
  1326. EMIT1_off32(0x0D, user_vm_start >> 32);
  1327. else
  1328. EMIT2_off32(0x81, add_1reg(0xC8, dst_reg), user_vm_start >> 32);
  1329. /* rol dst_reg, 32 */
  1330. maybe_emit_1mod(&prog, dst_reg, true);
  1331. EMIT3(0xC1, add_1reg(0xC0, dst_reg), 32);
  1332. /* xor r11, r11 */
  1333. EMIT3(0x4D, 0x31, 0xDB);
  1334. /* test dst_reg32, dst_reg32; check if lower 32-bit are zero */
  1335. maybe_emit_mod(&prog, dst_reg, dst_reg, false);
  1336. EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg));
  1337. /* cmove r11, dst_reg; if so, set dst_reg to zero */
  1338. /* WARNING: Intel swapped src/dst register encoding in CMOVcc !!! */
  1339. maybe_emit_mod(&prog, AUX_REG, dst_reg, true);
  1340. EMIT3(0x0F, 0x44, add_2reg(0xC0, AUX_REG, dst_reg));
  1341. break;
  1342. } else if (insn_is_mov_percpu_addr(insn)) {
  1343. /* mov <dst>, <src> (if necessary) */
  1344. EMIT_mov(dst_reg, src_reg);
  1345. #ifdef CONFIG_SMP
  1346. /* add <dst>, gs:[<off>] */
  1347. EMIT2(0x65, add_1mod(0x48, dst_reg));
  1348. EMIT3(0x03, add_2reg(0x04, 0, dst_reg), 0x25);
  1349. EMIT((u32)(unsigned long)&this_cpu_off, 4);
  1350. #endif
  1351. break;
  1352. }
  1353. fallthrough;
  1354. case BPF_ALU | BPF_MOV | BPF_X:
  1355. if (insn->off == 0)
  1356. emit_mov_reg(&prog,
  1357. BPF_CLASS(insn->code) == BPF_ALU64,
  1358. dst_reg, src_reg);
  1359. else
  1360. emit_movsx_reg(&prog, insn->off,
  1361. BPF_CLASS(insn->code) == BPF_ALU64,
  1362. dst_reg, src_reg);
  1363. break;
  1364. /* neg dst */
  1365. case BPF_ALU | BPF_NEG:
  1366. case BPF_ALU64 | BPF_NEG:
  1367. maybe_emit_1mod(&prog, dst_reg,
  1368. BPF_CLASS(insn->code) == BPF_ALU64);
  1369. EMIT2(0xF7, add_1reg(0xD8, dst_reg));
  1370. break;
  1371. case BPF_ALU | BPF_ADD | BPF_K:
  1372. case BPF_ALU | BPF_SUB | BPF_K:
  1373. case BPF_ALU | BPF_AND | BPF_K:
  1374. case BPF_ALU | BPF_OR | BPF_K:
  1375. case BPF_ALU | BPF_XOR | BPF_K:
  1376. case BPF_ALU64 | BPF_ADD | BPF_K:
  1377. case BPF_ALU64 | BPF_SUB | BPF_K:
  1378. case BPF_ALU64 | BPF_AND | BPF_K:
  1379. case BPF_ALU64 | BPF_OR | BPF_K:
  1380. case BPF_ALU64 | BPF_XOR | BPF_K:
  1381. maybe_emit_1mod(&prog, dst_reg,
  1382. BPF_CLASS(insn->code) == BPF_ALU64);
  1383. /*
  1384. * b3 holds 'normal' opcode, b2 short form only valid
  1385. * in case dst is eax/rax.
  1386. */
  1387. switch (BPF_OP(insn->code)) {
  1388. case BPF_ADD:
  1389. b3 = 0xC0;
  1390. b2 = 0x05;
  1391. break;
  1392. case BPF_SUB:
  1393. b3 = 0xE8;
  1394. b2 = 0x2D;
  1395. break;
  1396. case BPF_AND:
  1397. b3 = 0xE0;
  1398. b2 = 0x25;
  1399. break;
  1400. case BPF_OR:
  1401. b3 = 0xC8;
  1402. b2 = 0x0D;
  1403. break;
  1404. case BPF_XOR:
  1405. b3 = 0xF0;
  1406. b2 = 0x35;
  1407. break;
  1408. }
  1409. if (is_imm8(imm32))
  1410. EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
  1411. else if (is_axreg(dst_reg))
  1412. EMIT1_off32(b2, imm32);
  1413. else
  1414. EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
  1415. break;
  1416. case BPF_ALU64 | BPF_MOV | BPF_K:
  1417. case BPF_ALU | BPF_MOV | BPF_K:
  1418. emit_mov_imm32(&prog, BPF_CLASS(insn->code) == BPF_ALU64,
  1419. dst_reg, imm32);
  1420. break;
  1421. case BPF_LD | BPF_IMM | BPF_DW:
  1422. emit_mov_imm64(&prog, dst_reg, insn[1].imm, insn[0].imm);
  1423. insn++;
  1424. i++;
  1425. break;
  1426. /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
  1427. case BPF_ALU | BPF_MOD | BPF_X:
  1428. case BPF_ALU | BPF_DIV | BPF_X:
  1429. case BPF_ALU | BPF_MOD | BPF_K:
  1430. case BPF_ALU | BPF_DIV | BPF_K:
  1431. case BPF_ALU64 | BPF_MOD | BPF_X:
  1432. case BPF_ALU64 | BPF_DIV | BPF_X:
  1433. case BPF_ALU64 | BPF_MOD | BPF_K:
  1434. case BPF_ALU64 | BPF_DIV | BPF_K: {
  1435. bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
  1436. if (dst_reg != BPF_REG_0)
  1437. EMIT1(0x50); /* push rax */
  1438. if (dst_reg != BPF_REG_3)
  1439. EMIT1(0x52); /* push rdx */
  1440. if (BPF_SRC(insn->code) == BPF_X) {
  1441. if (src_reg == BPF_REG_0 ||
  1442. src_reg == BPF_REG_3) {
  1443. /* mov r11, src_reg */
  1444. EMIT_mov(AUX_REG, src_reg);
  1445. src_reg = AUX_REG;
  1446. }
  1447. } else {
  1448. /* mov r11, imm32 */
  1449. EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
  1450. src_reg = AUX_REG;
  1451. }
  1452. if (dst_reg != BPF_REG_0)
  1453. /* mov rax, dst_reg */
  1454. emit_mov_reg(&prog, is64, BPF_REG_0, dst_reg);
  1455. if (insn->off == 0) {
  1456. /*
  1457. * xor edx, edx
  1458. * equivalent to 'xor rdx, rdx', but one byte less
  1459. */
  1460. EMIT2(0x31, 0xd2);
  1461. /* div src_reg */
  1462. maybe_emit_1mod(&prog, src_reg, is64);
  1463. EMIT2(0xF7, add_1reg(0xF0, src_reg));
  1464. } else {
  1465. if (BPF_CLASS(insn->code) == BPF_ALU)
  1466. EMIT1(0x99); /* cdq */
  1467. else
  1468. EMIT2(0x48, 0x99); /* cqo */
  1469. /* idiv src_reg */
  1470. maybe_emit_1mod(&prog, src_reg, is64);
  1471. EMIT2(0xF7, add_1reg(0xF8, src_reg));
  1472. }
  1473. if (BPF_OP(insn->code) == BPF_MOD &&
  1474. dst_reg != BPF_REG_3)
  1475. /* mov dst_reg, rdx */
  1476. emit_mov_reg(&prog, is64, dst_reg, BPF_REG_3);
  1477. else if (BPF_OP(insn->code) == BPF_DIV &&
  1478. dst_reg != BPF_REG_0)
  1479. /* mov dst_reg, rax */
  1480. emit_mov_reg(&prog, is64, dst_reg, BPF_REG_0);
  1481. if (dst_reg != BPF_REG_3)
  1482. EMIT1(0x5A); /* pop rdx */
  1483. if (dst_reg != BPF_REG_0)
  1484. EMIT1(0x58); /* pop rax */
  1485. break;
  1486. }
  1487. case BPF_ALU | BPF_MUL | BPF_K:
  1488. case BPF_ALU64 | BPF_MUL | BPF_K:
  1489. maybe_emit_mod(&prog, dst_reg, dst_reg,
  1490. BPF_CLASS(insn->code) == BPF_ALU64);
  1491. if (is_imm8(imm32))
  1492. /* imul dst_reg, dst_reg, imm8 */
  1493. EMIT3(0x6B, add_2reg(0xC0, dst_reg, dst_reg),
  1494. imm32);
  1495. else
  1496. /* imul dst_reg, dst_reg, imm32 */
  1497. EMIT2_off32(0x69,
  1498. add_2reg(0xC0, dst_reg, dst_reg),
  1499. imm32);
  1500. break;
  1501. case BPF_ALU | BPF_MUL | BPF_X:
  1502. case BPF_ALU64 | BPF_MUL | BPF_X:
  1503. maybe_emit_mod(&prog, src_reg, dst_reg,
  1504. BPF_CLASS(insn->code) == BPF_ALU64);
  1505. /* imul dst_reg, src_reg */
  1506. EMIT3(0x0F, 0xAF, add_2reg(0xC0, src_reg, dst_reg));
  1507. break;
  1508. /* Shifts */
  1509. case BPF_ALU | BPF_LSH | BPF_K:
  1510. case BPF_ALU | BPF_RSH | BPF_K:
  1511. case BPF_ALU | BPF_ARSH | BPF_K:
  1512. case BPF_ALU64 | BPF_LSH | BPF_K:
  1513. case BPF_ALU64 | BPF_RSH | BPF_K:
  1514. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1515. maybe_emit_1mod(&prog, dst_reg,
  1516. BPF_CLASS(insn->code) == BPF_ALU64);
  1517. b3 = simple_alu_opcodes[BPF_OP(insn->code)];
  1518. if (imm32 == 1)
  1519. EMIT2(0xD1, add_1reg(b3, dst_reg));
  1520. else
  1521. EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
  1522. break;
  1523. case BPF_ALU | BPF_LSH | BPF_X:
  1524. case BPF_ALU | BPF_RSH | BPF_X:
  1525. case BPF_ALU | BPF_ARSH | BPF_X:
  1526. case BPF_ALU64 | BPF_LSH | BPF_X:
  1527. case BPF_ALU64 | BPF_RSH | BPF_X:
  1528. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1529. /* BMI2 shifts aren't better when shift count is already in rcx */
  1530. if (boot_cpu_has(X86_FEATURE_BMI2) && src_reg != BPF_REG_4) {
  1531. /* shrx/sarx/shlx dst_reg, dst_reg, src_reg */
  1532. bool w = (BPF_CLASS(insn->code) == BPF_ALU64);
  1533. u8 op;
  1534. switch (BPF_OP(insn->code)) {
  1535. case BPF_LSH:
  1536. op = 1; /* prefix 0x66 */
  1537. break;
  1538. case BPF_RSH:
  1539. op = 3; /* prefix 0xf2 */
  1540. break;
  1541. case BPF_ARSH:
  1542. op = 2; /* prefix 0xf3 */
  1543. break;
  1544. }
  1545. emit_shiftx(&prog, dst_reg, src_reg, w, op);
  1546. break;
  1547. }
  1548. if (src_reg != BPF_REG_4) { /* common case */
  1549. /* Check for bad case when dst_reg == rcx */
  1550. if (dst_reg == BPF_REG_4) {
  1551. /* mov r11, dst_reg */
  1552. EMIT_mov(AUX_REG, dst_reg);
  1553. dst_reg = AUX_REG;
  1554. } else {
  1555. EMIT1(0x51); /* push rcx */
  1556. }
  1557. /* mov rcx, src_reg */
  1558. EMIT_mov(BPF_REG_4, src_reg);
  1559. }
  1560. /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
  1561. maybe_emit_1mod(&prog, dst_reg,
  1562. BPF_CLASS(insn->code) == BPF_ALU64);
  1563. b3 = simple_alu_opcodes[BPF_OP(insn->code)];
  1564. EMIT2(0xD3, add_1reg(b3, dst_reg));
  1565. if (src_reg != BPF_REG_4) {
  1566. if (insn->dst_reg == BPF_REG_4)
  1567. /* mov dst_reg, r11 */
  1568. EMIT_mov(insn->dst_reg, AUX_REG);
  1569. else
  1570. EMIT1(0x59); /* pop rcx */
  1571. }
  1572. break;
  1573. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1574. case BPF_ALU64 | BPF_END | BPF_FROM_LE:
  1575. switch (imm32) {
  1576. case 16:
  1577. /* Emit 'ror %ax, 8' to swap lower 2 bytes */
  1578. EMIT1(0x66);
  1579. if (is_ereg(dst_reg))
  1580. EMIT1(0x41);
  1581. EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
  1582. /* Emit 'movzwl eax, ax' */
  1583. if (is_ereg(dst_reg))
  1584. EMIT3(0x45, 0x0F, 0xB7);
  1585. else
  1586. EMIT2(0x0F, 0xB7);
  1587. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  1588. break;
  1589. case 32:
  1590. /* Emit 'bswap eax' to swap lower 4 bytes */
  1591. if (is_ereg(dst_reg))
  1592. EMIT2(0x41, 0x0F);
  1593. else
  1594. EMIT1(0x0F);
  1595. EMIT1(add_1reg(0xC8, dst_reg));
  1596. break;
  1597. case 64:
  1598. /* Emit 'bswap rax' to swap 8 bytes */
  1599. EMIT3(add_1mod(0x48, dst_reg), 0x0F,
  1600. add_1reg(0xC8, dst_reg));
  1601. break;
  1602. }
  1603. break;
  1604. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1605. switch (imm32) {
  1606. case 16:
  1607. /*
  1608. * Emit 'movzwl eax, ax' to zero extend 16-bit
  1609. * into 64 bit
  1610. */
  1611. if (is_ereg(dst_reg))
  1612. EMIT3(0x45, 0x0F, 0xB7);
  1613. else
  1614. EMIT2(0x0F, 0xB7);
  1615. EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
  1616. break;
  1617. case 32:
  1618. /* Emit 'mov eax, eax' to clear upper 32-bits */
  1619. if (is_ereg(dst_reg))
  1620. EMIT1(0x45);
  1621. EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
  1622. break;
  1623. case 64:
  1624. /* nop */
  1625. break;
  1626. }
  1627. break;
  1628. /* speculation barrier */
  1629. case BPF_ST | BPF_NOSPEC:
  1630. EMIT_LFENCE();
  1631. break;
  1632. /* ST: *(u8*)(dst_reg + off) = imm */
  1633. case BPF_ST | BPF_MEM | BPF_B:
  1634. if (is_ereg(dst_reg))
  1635. EMIT2(0x41, 0xC6);
  1636. else
  1637. EMIT1(0xC6);
  1638. goto st;
  1639. case BPF_ST | BPF_MEM | BPF_H:
  1640. if (is_ereg(dst_reg))
  1641. EMIT3(0x66, 0x41, 0xC7);
  1642. else
  1643. EMIT2(0x66, 0xC7);
  1644. goto st;
  1645. case BPF_ST | BPF_MEM | BPF_W:
  1646. if (is_ereg(dst_reg))
  1647. EMIT2(0x41, 0xC7);
  1648. else
  1649. EMIT1(0xC7);
  1650. goto st;
  1651. case BPF_ST | BPF_MEM | BPF_DW:
  1652. EMIT2(add_1mod(0x48, dst_reg), 0xC7);
  1653. st: if (is_imm8(insn->off))
  1654. EMIT2(add_1reg(0x40, dst_reg), insn->off);
  1655. else
  1656. EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
  1657. EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
  1658. break;
  1659. /* STX: *(u8*)(dst_reg + off) = src_reg */
  1660. case BPF_STX | BPF_MEM | BPF_B:
  1661. case BPF_STX | BPF_MEM | BPF_H:
  1662. case BPF_STX | BPF_MEM | BPF_W:
  1663. case BPF_STX | BPF_MEM | BPF_DW:
  1664. emit_stx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
  1665. break;
  1666. case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
  1667. case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
  1668. case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
  1669. case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
  1670. start_of_ldx = prog;
  1671. emit_st_r12(&prog, BPF_SIZE(insn->code), dst_reg, insn->off, insn->imm);
  1672. goto populate_extable;
  1673. /* LDX: dst_reg = *(u8*)(src_reg + r12 + off) */
  1674. case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
  1675. case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
  1676. case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
  1677. case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
  1678. case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
  1679. case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
  1680. case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
  1681. case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
  1682. start_of_ldx = prog;
  1683. if (BPF_CLASS(insn->code) == BPF_LDX)
  1684. emit_ldx_r12(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
  1685. else
  1686. emit_stx_r12(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
  1687. populate_extable:
  1688. {
  1689. struct exception_table_entry *ex;
  1690. u8 *_insn = image + proglen + (start_of_ldx - temp);
  1691. s64 delta;
  1692. if (!bpf_prog->aux->extable)
  1693. break;
  1694. if (excnt >= bpf_prog->aux->num_exentries) {
  1695. pr_err("mem32 extable bug\n");
  1696. return -EFAULT;
  1697. }
  1698. ex = &bpf_prog->aux->extable[excnt++];
  1699. delta = _insn - (u8 *)&ex->insn;
  1700. /* switch ex to rw buffer for writes */
  1701. ex = (void *)rw_image + ((void *)ex - (void *)image);
  1702. ex->insn = delta;
  1703. ex->data = EX_TYPE_BPF;
  1704. ex->fixup = (prog - start_of_ldx) |
  1705. ((BPF_CLASS(insn->code) == BPF_LDX ? reg2pt_regs[dst_reg] : DONT_CLEAR) << 8);
  1706. }
  1707. break;
  1708. /* LDX: dst_reg = *(u8*)(src_reg + off) */
  1709. case BPF_LDX | BPF_MEM | BPF_B:
  1710. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  1711. case BPF_LDX | BPF_MEM | BPF_H:
  1712. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  1713. case BPF_LDX | BPF_MEM | BPF_W:
  1714. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  1715. case BPF_LDX | BPF_MEM | BPF_DW:
  1716. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  1717. /* LDXS: dst_reg = *(s8*)(src_reg + off) */
  1718. case BPF_LDX | BPF_MEMSX | BPF_B:
  1719. case BPF_LDX | BPF_MEMSX | BPF_H:
  1720. case BPF_LDX | BPF_MEMSX | BPF_W:
  1721. case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
  1722. case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
  1723. case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
  1724. insn_off = insn->off;
  1725. if (BPF_MODE(insn->code) == BPF_PROBE_MEM ||
  1726. BPF_MODE(insn->code) == BPF_PROBE_MEMSX) {
  1727. /* Conservatively check that src_reg + insn->off is a kernel address:
  1728. * src_reg + insn->off > TASK_SIZE_MAX + PAGE_SIZE
  1729. * and
  1730. * src_reg + insn->off < VSYSCALL_ADDR
  1731. */
  1732. u64 limit = TASK_SIZE_MAX + PAGE_SIZE - VSYSCALL_ADDR;
  1733. u8 *end_of_jmp;
  1734. /* movabsq r10, VSYSCALL_ADDR */
  1735. emit_mov_imm64(&prog, BPF_REG_AX, (long)VSYSCALL_ADDR >> 32,
  1736. (u32)(long)VSYSCALL_ADDR);
  1737. /* mov src_reg, r11 */
  1738. EMIT_mov(AUX_REG, src_reg);
  1739. if (insn->off) {
  1740. /* add r11, insn->off */
  1741. maybe_emit_1mod(&prog, AUX_REG, true);
  1742. EMIT2_off32(0x81, add_1reg(0xC0, AUX_REG), insn->off);
  1743. }
  1744. /* sub r11, r10 */
  1745. maybe_emit_mod(&prog, AUX_REG, BPF_REG_AX, true);
  1746. EMIT2(0x29, add_2reg(0xC0, AUX_REG, BPF_REG_AX));
  1747. /* movabsq r10, limit */
  1748. emit_mov_imm64(&prog, BPF_REG_AX, (long)limit >> 32,
  1749. (u32)(long)limit);
  1750. /* cmp r10, r11 */
  1751. maybe_emit_mod(&prog, AUX_REG, BPF_REG_AX, true);
  1752. EMIT2(0x39, add_2reg(0xC0, AUX_REG, BPF_REG_AX));
  1753. /* if unsigned '>', goto load */
  1754. EMIT2(X86_JA, 0);
  1755. end_of_jmp = prog;
  1756. /* xor dst_reg, dst_reg */
  1757. emit_mov_imm32(&prog, false, dst_reg, 0);
  1758. /* jmp byte_after_ldx */
  1759. EMIT2(0xEB, 0);
  1760. /* populate jmp_offset for JAE above to jump to start_of_ldx */
  1761. start_of_ldx = prog;
  1762. end_of_jmp[-1] = start_of_ldx - end_of_jmp;
  1763. }
  1764. if (BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
  1765. BPF_MODE(insn->code) == BPF_MEMSX)
  1766. emit_ldsx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off);
  1767. else
  1768. emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off);
  1769. if (BPF_MODE(insn->code) == BPF_PROBE_MEM ||
  1770. BPF_MODE(insn->code) == BPF_PROBE_MEMSX) {
  1771. struct exception_table_entry *ex;
  1772. u8 *_insn = image + proglen + (start_of_ldx - temp);
  1773. s64 delta;
  1774. /* populate jmp_offset for JMP above */
  1775. start_of_ldx[-1] = prog - start_of_ldx;
  1776. if (!bpf_prog->aux->extable)
  1777. break;
  1778. if (excnt >= bpf_prog->aux->num_exentries) {
  1779. pr_err("ex gen bug\n");
  1780. return -EFAULT;
  1781. }
  1782. ex = &bpf_prog->aux->extable[excnt++];
  1783. delta = _insn - (u8 *)&ex->insn;
  1784. if (!is_simm32(delta)) {
  1785. pr_err("extable->insn doesn't fit into 32-bit\n");
  1786. return -EFAULT;
  1787. }
  1788. /* switch ex to rw buffer for writes */
  1789. ex = (void *)rw_image + ((void *)ex - (void *)image);
  1790. ex->insn = delta;
  1791. ex->data = EX_TYPE_BPF;
  1792. if (dst_reg > BPF_REG_9) {
  1793. pr_err("verifier error\n");
  1794. return -EFAULT;
  1795. }
  1796. /*
  1797. * Compute size of x86 insn and its target dest x86 register.
  1798. * ex_handler_bpf() will use lower 8 bits to adjust
  1799. * pt_regs->ip to jump over this x86 instruction
  1800. * and upper bits to figure out which pt_regs to zero out.
  1801. * End result: x86 insn "mov rbx, qword ptr [rax+0x14]"
  1802. * of 4 bytes will be ignored and rbx will be zero inited.
  1803. */
  1804. ex->fixup = (prog - start_of_ldx) | (reg2pt_regs[dst_reg] << 8);
  1805. }
  1806. break;
  1807. case BPF_STX | BPF_ATOMIC | BPF_W:
  1808. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1809. if (insn->imm == (BPF_AND | BPF_FETCH) ||
  1810. insn->imm == (BPF_OR | BPF_FETCH) ||
  1811. insn->imm == (BPF_XOR | BPF_FETCH)) {
  1812. bool is64 = BPF_SIZE(insn->code) == BPF_DW;
  1813. u32 real_src_reg = src_reg;
  1814. u32 real_dst_reg = dst_reg;
  1815. u8 *branch_target;
  1816. /*
  1817. * Can't be implemented with a single x86 insn.
  1818. * Need to do a CMPXCHG loop.
  1819. */
  1820. /* Will need RAX as a CMPXCHG operand so save R0 */
  1821. emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);
  1822. if (src_reg == BPF_REG_0)
  1823. real_src_reg = BPF_REG_AX;
  1824. if (dst_reg == BPF_REG_0)
  1825. real_dst_reg = BPF_REG_AX;
  1826. branch_target = prog;
  1827. /* Load old value */
  1828. emit_ldx(&prog, BPF_SIZE(insn->code),
  1829. BPF_REG_0, real_dst_reg, insn->off);
  1830. /*
  1831. * Perform the (commutative) operation locally,
  1832. * put the result in the AUX_REG.
  1833. */
  1834. emit_mov_reg(&prog, is64, AUX_REG, BPF_REG_0);
  1835. maybe_emit_mod(&prog, AUX_REG, real_src_reg, is64);
  1836. EMIT2(simple_alu_opcodes[BPF_OP(insn->imm)],
  1837. add_2reg(0xC0, AUX_REG, real_src_reg));
  1838. /* Attempt to swap in new value */
  1839. err = emit_atomic(&prog, BPF_CMPXCHG,
  1840. real_dst_reg, AUX_REG,
  1841. insn->off,
  1842. BPF_SIZE(insn->code));
  1843. if (WARN_ON(err))
  1844. return err;
  1845. /*
  1846. * ZF tells us whether we won the race. If it's
  1847. * cleared we need to try again.
  1848. */
  1849. EMIT2(X86_JNE, -(prog - branch_target) - 2);
  1850. /* Return the pre-modification value */
  1851. emit_mov_reg(&prog, is64, real_src_reg, BPF_REG_0);
  1852. /* Restore R0 after clobbering RAX */
  1853. emit_mov_reg(&prog, true, BPF_REG_0, BPF_REG_AX);
  1854. break;
  1855. }
  1856. err = emit_atomic(&prog, insn->imm, dst_reg, src_reg,
  1857. insn->off, BPF_SIZE(insn->code));
  1858. if (err)
  1859. return err;
  1860. break;
  1861. case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
  1862. case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
  1863. start_of_ldx = prog;
  1864. err = emit_atomic_index(&prog, insn->imm, BPF_SIZE(insn->code),
  1865. dst_reg, src_reg, X86_REG_R12, insn->off);
  1866. if (err)
  1867. return err;
  1868. goto populate_extable;
  1869. /* call */
  1870. case BPF_JMP | BPF_CALL: {
  1871. u8 *ip = image + addrs[i - 1];
  1872. func = (u8 *) __bpf_call_base + imm32;
  1873. if (tail_call_reachable) {
  1874. LOAD_TAIL_CALL_CNT_PTR(bpf_prog->aux->stack_depth);
  1875. ip += 7;
  1876. }
  1877. if (!imm32)
  1878. return -EINVAL;
  1879. ip += x86_call_depth_emit_accounting(&prog, func, ip);
  1880. if (emit_call(&prog, func, ip))
  1881. return -EINVAL;
  1882. break;
  1883. }
  1884. case BPF_JMP | BPF_TAIL_CALL:
  1885. if (imm32)
  1886. emit_bpf_tail_call_direct(bpf_prog,
  1887. &bpf_prog->aux->poke_tab[imm32 - 1],
  1888. &prog, image + addrs[i - 1],
  1889. callee_regs_used,
  1890. bpf_prog->aux->stack_depth,
  1891. ctx);
  1892. else
  1893. emit_bpf_tail_call_indirect(bpf_prog,
  1894. &prog,
  1895. callee_regs_used,
  1896. bpf_prog->aux->stack_depth,
  1897. image + addrs[i - 1],
  1898. ctx);
  1899. break;
  1900. /* cond jump */
  1901. case BPF_JMP | BPF_JEQ | BPF_X:
  1902. case BPF_JMP | BPF_JNE | BPF_X:
  1903. case BPF_JMP | BPF_JGT | BPF_X:
  1904. case BPF_JMP | BPF_JLT | BPF_X:
  1905. case BPF_JMP | BPF_JGE | BPF_X:
  1906. case BPF_JMP | BPF_JLE | BPF_X:
  1907. case BPF_JMP | BPF_JSGT | BPF_X:
  1908. case BPF_JMP | BPF_JSLT | BPF_X:
  1909. case BPF_JMP | BPF_JSGE | BPF_X:
  1910. case BPF_JMP | BPF_JSLE | BPF_X:
  1911. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1912. case BPF_JMP32 | BPF_JNE | BPF_X:
  1913. case BPF_JMP32 | BPF_JGT | BPF_X:
  1914. case BPF_JMP32 | BPF_JLT | BPF_X:
  1915. case BPF_JMP32 | BPF_JGE | BPF_X:
  1916. case BPF_JMP32 | BPF_JLE | BPF_X:
  1917. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1918. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1919. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1920. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1921. /* cmp dst_reg, src_reg */
  1922. maybe_emit_mod(&prog, dst_reg, src_reg,
  1923. BPF_CLASS(insn->code) == BPF_JMP);
  1924. EMIT2(0x39, add_2reg(0xC0, dst_reg, src_reg));
  1925. goto emit_cond_jmp;
  1926. case BPF_JMP | BPF_JSET | BPF_X:
  1927. case BPF_JMP32 | BPF_JSET | BPF_X:
  1928. /* test dst_reg, src_reg */
  1929. maybe_emit_mod(&prog, dst_reg, src_reg,
  1930. BPF_CLASS(insn->code) == BPF_JMP);
  1931. EMIT2(0x85, add_2reg(0xC0, dst_reg, src_reg));
  1932. goto emit_cond_jmp;
  1933. case BPF_JMP | BPF_JSET | BPF_K:
  1934. case BPF_JMP32 | BPF_JSET | BPF_K:
  1935. /* test dst_reg, imm32 */
  1936. maybe_emit_1mod(&prog, dst_reg,
  1937. BPF_CLASS(insn->code) == BPF_JMP);
  1938. EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
  1939. goto emit_cond_jmp;
  1940. case BPF_JMP | BPF_JEQ | BPF_K:
  1941. case BPF_JMP | BPF_JNE | BPF_K:
  1942. case BPF_JMP | BPF_JGT | BPF_K:
  1943. case BPF_JMP | BPF_JLT | BPF_K:
  1944. case BPF_JMP | BPF_JGE | BPF_K:
  1945. case BPF_JMP | BPF_JLE | BPF_K:
  1946. case BPF_JMP | BPF_JSGT | BPF_K:
  1947. case BPF_JMP | BPF_JSLT | BPF_K:
  1948. case BPF_JMP | BPF_JSGE | BPF_K:
  1949. case BPF_JMP | BPF_JSLE | BPF_K:
  1950. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1951. case BPF_JMP32 | BPF_JNE | BPF_K:
  1952. case BPF_JMP32 | BPF_JGT | BPF_K:
  1953. case BPF_JMP32 | BPF_JLT | BPF_K:
  1954. case BPF_JMP32 | BPF_JGE | BPF_K:
  1955. case BPF_JMP32 | BPF_JLE | BPF_K:
  1956. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1957. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1958. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1959. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1960. /* test dst_reg, dst_reg to save one extra byte */
  1961. if (imm32 == 0) {
  1962. maybe_emit_mod(&prog, dst_reg, dst_reg,
  1963. BPF_CLASS(insn->code) == BPF_JMP);
  1964. EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg));
  1965. goto emit_cond_jmp;
  1966. }
  1967. /* cmp dst_reg, imm8/32 */
  1968. maybe_emit_1mod(&prog, dst_reg,
  1969. BPF_CLASS(insn->code) == BPF_JMP);
  1970. if (is_imm8(imm32))
  1971. EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
  1972. else
  1973. EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
  1974. emit_cond_jmp: /* Convert BPF opcode to x86 */
  1975. switch (BPF_OP(insn->code)) {
  1976. case BPF_JEQ:
  1977. jmp_cond = X86_JE;
  1978. break;
  1979. case BPF_JSET:
  1980. case BPF_JNE:
  1981. jmp_cond = X86_JNE;
  1982. break;
  1983. case BPF_JGT:
  1984. /* GT is unsigned '>', JA in x86 */
  1985. jmp_cond = X86_JA;
  1986. break;
  1987. case BPF_JLT:
  1988. /* LT is unsigned '<', JB in x86 */
  1989. jmp_cond = X86_JB;
  1990. break;
  1991. case BPF_JGE:
  1992. /* GE is unsigned '>=', JAE in x86 */
  1993. jmp_cond = X86_JAE;
  1994. break;
  1995. case BPF_JLE:
  1996. /* LE is unsigned '<=', JBE in x86 */
  1997. jmp_cond = X86_JBE;
  1998. break;
  1999. case BPF_JSGT:
  2000. /* Signed '>', GT in x86 */
  2001. jmp_cond = X86_JG;
  2002. break;
  2003. case BPF_JSLT:
  2004. /* Signed '<', LT in x86 */
  2005. jmp_cond = X86_JL;
  2006. break;
  2007. case BPF_JSGE:
  2008. /* Signed '>=', GE in x86 */
  2009. jmp_cond = X86_JGE;
  2010. break;
  2011. case BPF_JSLE:
  2012. /* Signed '<=', LE in x86 */
  2013. jmp_cond = X86_JLE;
  2014. break;
  2015. default: /* to silence GCC warning */
  2016. return -EFAULT;
  2017. }
  2018. jmp_offset = addrs[i + insn->off] - addrs[i];
  2019. if (is_imm8_jmp_offset(jmp_offset)) {
  2020. if (jmp_padding) {
  2021. /* To keep the jmp_offset valid, the extra bytes are
  2022. * padded before the jump insn, so we subtract the
  2023. * 2 bytes of jmp_cond insn from INSN_SZ_DIFF.
  2024. *
  2025. * If the previous pass already emits an imm8
  2026. * jmp_cond, then this BPF insn won't shrink, so
  2027. * "nops" is 0.
  2028. *
  2029. * On the other hand, if the previous pass emits an
  2030. * imm32 jmp_cond, the extra 4 bytes(*) is padded to
  2031. * keep the image from shrinking further.
  2032. *
  2033. * (*) imm32 jmp_cond is 6 bytes, and imm8 jmp_cond
  2034. * is 2 bytes, so the size difference is 4 bytes.
  2035. */
  2036. nops = INSN_SZ_DIFF - 2;
  2037. if (nops != 0 && nops != 4) {
  2038. pr_err("unexpected jmp_cond padding: %d bytes\n",
  2039. nops);
  2040. return -EFAULT;
  2041. }
  2042. emit_nops(&prog, nops);
  2043. }
  2044. EMIT2(jmp_cond, jmp_offset);
  2045. } else if (is_simm32(jmp_offset)) {
  2046. EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
  2047. } else {
  2048. pr_err("cond_jmp gen bug %llx\n", jmp_offset);
  2049. return -EFAULT;
  2050. }
  2051. break;
  2052. case BPF_JMP | BPF_JA:
  2053. case BPF_JMP32 | BPF_JA:
  2054. if (BPF_CLASS(insn->code) == BPF_JMP) {
  2055. if (insn->off == -1)
  2056. /* -1 jmp instructions will always jump
  2057. * backwards two bytes. Explicitly handling
  2058. * this case avoids wasting too many passes
  2059. * when there are long sequences of replaced
  2060. * dead code.
  2061. */
  2062. jmp_offset = -2;
  2063. else
  2064. jmp_offset = addrs[i + insn->off] - addrs[i];
  2065. } else {
  2066. if (insn->imm == -1)
  2067. jmp_offset = -2;
  2068. else
  2069. jmp_offset = addrs[i + insn->imm] - addrs[i];
  2070. }
  2071. if (!jmp_offset) {
  2072. /*
  2073. * If jmp_padding is enabled, the extra nops will
  2074. * be inserted. Otherwise, optimize out nop jumps.
  2075. */
  2076. if (jmp_padding) {
  2077. /* There are 3 possible conditions.
  2078. * (1) This BPF_JA is already optimized out in
  2079. * the previous run, so there is no need
  2080. * to pad any extra byte (0 byte).
  2081. * (2) The previous pass emits an imm8 jmp,
  2082. * so we pad 2 bytes to match the previous
  2083. * insn size.
  2084. * (3) Similarly, the previous pass emits an
  2085. * imm32 jmp, and 5 bytes is padded.
  2086. */
  2087. nops = INSN_SZ_DIFF;
  2088. if (nops != 0 && nops != 2 && nops != 5) {
  2089. pr_err("unexpected nop jump padding: %d bytes\n",
  2090. nops);
  2091. return -EFAULT;
  2092. }
  2093. emit_nops(&prog, nops);
  2094. }
  2095. break;
  2096. }
  2097. emit_jmp:
  2098. if (is_imm8_jmp_offset(jmp_offset)) {
  2099. if (jmp_padding) {
  2100. /* To avoid breaking jmp_offset, the extra bytes
  2101. * are padded before the actual jmp insn, so
  2102. * 2 bytes is subtracted from INSN_SZ_DIFF.
  2103. *
  2104. * If the previous pass already emits an imm8
  2105. * jmp, there is nothing to pad (0 byte).
  2106. *
  2107. * If it emits an imm32 jmp (5 bytes) previously
  2108. * and now an imm8 jmp (2 bytes), then we pad
  2109. * (5 - 2 = 3) bytes to stop the image from
  2110. * shrinking further.
  2111. */
  2112. nops = INSN_SZ_DIFF - 2;
  2113. if (nops != 0 && nops != 3) {
  2114. pr_err("unexpected jump padding: %d bytes\n",
  2115. nops);
  2116. return -EFAULT;
  2117. }
  2118. emit_nops(&prog, INSN_SZ_DIFF - 2);
  2119. }
  2120. EMIT2(0xEB, jmp_offset);
  2121. } else if (is_simm32(jmp_offset)) {
  2122. EMIT1_off32(0xE9, jmp_offset);
  2123. } else {
  2124. pr_err("jmp gen bug %llx\n", jmp_offset);
  2125. return -EFAULT;
  2126. }
  2127. break;
  2128. case BPF_JMP | BPF_EXIT:
  2129. if (seen_exit) {
  2130. jmp_offset = ctx->cleanup_addr - addrs[i];
  2131. goto emit_jmp;
  2132. }
  2133. seen_exit = true;
  2134. /* Update cleanup_addr */
  2135. ctx->cleanup_addr = proglen;
  2136. if (bpf_prog->aux->exception_boundary) {
  2137. pop_callee_regs(&prog, all_callee_regs_used);
  2138. pop_r12(&prog);
  2139. } else {
  2140. pop_callee_regs(&prog, callee_regs_used);
  2141. if (arena_vm_start)
  2142. pop_r12(&prog);
  2143. }
  2144. EMIT1(0xC9); /* leave */
  2145. emit_return(&prog, image + addrs[i - 1] + (prog - temp));
  2146. break;
  2147. default:
  2148. /*
  2149. * By design x86-64 JIT should support all BPF instructions.
  2150. * This error will be seen if new instruction was added
  2151. * to the interpreter, but not to the JIT, or if there is
  2152. * junk in bpf_prog.
  2153. */
  2154. pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
  2155. return -EINVAL;
  2156. }
  2157. ilen = prog - temp;
  2158. if (ilen > BPF_MAX_INSN_SIZE) {
  2159. pr_err("bpf_jit: fatal insn size error\n");
  2160. return -EFAULT;
  2161. }
  2162. if (image) {
  2163. /*
  2164. * When populating the image, assert that:
  2165. *
  2166. * i) We do not write beyond the allocated space, and
  2167. * ii) addrs[i] did not change from the prior run, in order
  2168. * to validate assumptions made for computing branch
  2169. * displacements.
  2170. */
  2171. if (unlikely(proglen + ilen > oldproglen ||
  2172. proglen + ilen != addrs[i])) {
  2173. pr_err("bpf_jit: fatal error\n");
  2174. return -EFAULT;
  2175. }
  2176. memcpy(rw_image + proglen, temp, ilen);
  2177. }
  2178. proglen += ilen;
  2179. addrs[i] = proglen;
  2180. prog = temp;
  2181. }
  2182. if (image && excnt != bpf_prog->aux->num_exentries) {
  2183. pr_err("extable is not populated\n");
  2184. return -EFAULT;
  2185. }
  2186. return proglen;
  2187. }
  2188. static void clean_stack_garbage(const struct btf_func_model *m,
  2189. u8 **pprog, int nr_stack_slots,
  2190. int stack_size)
  2191. {
  2192. int arg_size, off;
  2193. u8 *prog;
  2194. /* Generally speaking, the compiler will pass the arguments
  2195. * on-stack with "push" instruction, which will take 8-byte
  2196. * on the stack. In this case, there won't be garbage values
  2197. * while we copy the arguments from origin stack frame to current
  2198. * in BPF_DW.
  2199. *
  2200. * However, sometimes the compiler will only allocate 4-byte on
  2201. * the stack for the arguments. For now, this case will only
  2202. * happen if there is only one argument on-stack and its size
  2203. * not more than 4 byte. In this case, there will be garbage
  2204. * values on the upper 4-byte where we store the argument on
  2205. * current stack frame.
  2206. *
  2207. * arguments on origin stack:
  2208. *
  2209. * stack_arg_1(4-byte) xxx(4-byte)
  2210. *
  2211. * what we copy:
  2212. *
  2213. * stack_arg_1(8-byte): stack_arg_1(origin) xxx
  2214. *
  2215. * and the xxx is the garbage values which we should clean here.
  2216. */
  2217. if (nr_stack_slots != 1)
  2218. return;
  2219. /* the size of the last argument */
  2220. arg_size = m->arg_size[m->nr_args - 1];
  2221. if (arg_size <= 4) {
  2222. off = -(stack_size - 4);
  2223. prog = *pprog;
  2224. /* mov DWORD PTR [rbp + off], 0 */
  2225. if (!is_imm8(off))
  2226. EMIT2_off32(0xC7, 0x85, off);
  2227. else
  2228. EMIT3(0xC7, 0x45, off);
  2229. EMIT(0, 4);
  2230. *pprog = prog;
  2231. }
  2232. }
  2233. /* get the count of the regs that are used to pass arguments */
  2234. static int get_nr_used_regs(const struct btf_func_model *m)
  2235. {
  2236. int i, arg_regs, nr_used_regs = 0;
  2237. for (i = 0; i < min_t(int, m->nr_args, MAX_BPF_FUNC_ARGS); i++) {
  2238. arg_regs = (m->arg_size[i] + 7) / 8;
  2239. if (nr_used_regs + arg_regs <= 6)
  2240. nr_used_regs += arg_regs;
  2241. if (nr_used_regs >= 6)
  2242. break;
  2243. }
  2244. return nr_used_regs;
  2245. }
  2246. static void save_args(const struct btf_func_model *m, u8 **prog,
  2247. int stack_size, bool for_call_origin)
  2248. {
  2249. int arg_regs, first_off = 0, nr_regs = 0, nr_stack_slots = 0;
  2250. int i, j;
  2251. /* Store function arguments to stack.
  2252. * For a function that accepts two pointers the sequence will be:
  2253. * mov QWORD PTR [rbp-0x10],rdi
  2254. * mov QWORD PTR [rbp-0x8],rsi
  2255. */
  2256. for (i = 0; i < min_t(int, m->nr_args, MAX_BPF_FUNC_ARGS); i++) {
  2257. arg_regs = (m->arg_size[i] + 7) / 8;
  2258. /* According to the research of Yonghong, struct members
  2259. * should be all in register or all on the stack.
  2260. * Meanwhile, the compiler will pass the argument on regs
  2261. * if the remaining regs can hold the argument.
  2262. *
  2263. * Disorder of the args can happen. For example:
  2264. *
  2265. * struct foo_struct {
  2266. * long a;
  2267. * int b;
  2268. * };
  2269. * int foo(char, char, char, char, char, struct foo_struct,
  2270. * char);
  2271. *
  2272. * the arg1-5,arg7 will be passed by regs, and arg6 will
  2273. * by stack.
  2274. */
  2275. if (nr_regs + arg_regs > 6) {
  2276. /* copy function arguments from origin stack frame
  2277. * into current stack frame.
  2278. *
  2279. * The starting address of the arguments on-stack
  2280. * is:
  2281. * rbp + 8(push rbp) +
  2282. * 8(return addr of origin call) +
  2283. * 8(return addr of the caller)
  2284. * which means: rbp + 24
  2285. */
  2286. for (j = 0; j < arg_regs; j++) {
  2287. emit_ldx(prog, BPF_DW, BPF_REG_0, BPF_REG_FP,
  2288. nr_stack_slots * 8 + 0x18);
  2289. emit_stx(prog, BPF_DW, BPF_REG_FP, BPF_REG_0,
  2290. -stack_size);
  2291. if (!nr_stack_slots)
  2292. first_off = stack_size;
  2293. stack_size -= 8;
  2294. nr_stack_slots++;
  2295. }
  2296. } else {
  2297. /* Only copy the arguments on-stack to current
  2298. * 'stack_size' and ignore the regs, used to
  2299. * prepare the arguments on-stack for origin call.
  2300. */
  2301. if (for_call_origin) {
  2302. nr_regs += arg_regs;
  2303. continue;
  2304. }
  2305. /* copy the arguments from regs into stack */
  2306. for (j = 0; j < arg_regs; j++) {
  2307. emit_stx(prog, BPF_DW, BPF_REG_FP,
  2308. nr_regs == 5 ? X86_REG_R9 : BPF_REG_1 + nr_regs,
  2309. -stack_size);
  2310. stack_size -= 8;
  2311. nr_regs++;
  2312. }
  2313. }
  2314. }
  2315. clean_stack_garbage(m, prog, nr_stack_slots, first_off);
  2316. }
  2317. static void restore_regs(const struct btf_func_model *m, u8 **prog,
  2318. int stack_size)
  2319. {
  2320. int i, j, arg_regs, nr_regs = 0;
  2321. /* Restore function arguments from stack.
  2322. * For a function that accepts two pointers the sequence will be:
  2323. * EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10]
  2324. * EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8]
  2325. *
  2326. * The logic here is similar to what we do in save_args()
  2327. */
  2328. for (i = 0; i < min_t(int, m->nr_args, MAX_BPF_FUNC_ARGS); i++) {
  2329. arg_regs = (m->arg_size[i] + 7) / 8;
  2330. if (nr_regs + arg_regs <= 6) {
  2331. for (j = 0; j < arg_regs; j++) {
  2332. emit_ldx(prog, BPF_DW,
  2333. nr_regs == 5 ? X86_REG_R9 : BPF_REG_1 + nr_regs,
  2334. BPF_REG_FP,
  2335. -stack_size);
  2336. stack_size -= 8;
  2337. nr_regs++;
  2338. }
  2339. } else {
  2340. stack_size -= 8 * arg_regs;
  2341. }
  2342. if (nr_regs >= 6)
  2343. break;
  2344. }
  2345. }
  2346. static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
  2347. struct bpf_tramp_link *l, int stack_size,
  2348. int run_ctx_off, bool save_ret,
  2349. void *image, void *rw_image)
  2350. {
  2351. u8 *prog = *pprog;
  2352. u8 *jmp_insn;
  2353. int ctx_cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
  2354. struct bpf_prog *p = l->link.prog;
  2355. u64 cookie = l->cookie;
  2356. /* mov rdi, cookie */
  2357. emit_mov_imm64(&prog, BPF_REG_1, (long) cookie >> 32, (u32) (long) cookie);
  2358. /* Prepare struct bpf_tramp_run_ctx.
  2359. *
  2360. * bpf_tramp_run_ctx is already preserved by
  2361. * arch_prepare_bpf_trampoline().
  2362. *
  2363. * mov QWORD PTR [rbp - run_ctx_off + ctx_cookie_off], rdi
  2364. */
  2365. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_1, -run_ctx_off + ctx_cookie_off);
  2366. /* arg1: mov rdi, progs[i] */
  2367. emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32, (u32) (long) p);
  2368. /* arg2: lea rsi, [rbp - ctx_cookie_off] */
  2369. if (!is_imm8(-run_ctx_off))
  2370. EMIT3_off32(0x48, 0x8D, 0xB5, -run_ctx_off);
  2371. else
  2372. EMIT4(0x48, 0x8D, 0x75, -run_ctx_off);
  2373. if (emit_rsb_call(&prog, bpf_trampoline_enter(p), image + (prog - (u8 *)rw_image)))
  2374. return -EINVAL;
  2375. /* remember prog start time returned by __bpf_prog_enter */
  2376. emit_mov_reg(&prog, true, BPF_REG_6, BPF_REG_0);
  2377. /* if (__bpf_prog_enter*(prog) == 0)
  2378. * goto skip_exec_of_prog;
  2379. */
  2380. EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */
  2381. /* emit 2 nops that will be replaced with JE insn */
  2382. jmp_insn = prog;
  2383. emit_nops(&prog, 2);
  2384. /* arg1: lea rdi, [rbp - stack_size] */
  2385. if (!is_imm8(-stack_size))
  2386. EMIT3_off32(0x48, 0x8D, 0xBD, -stack_size);
  2387. else
  2388. EMIT4(0x48, 0x8D, 0x7D, -stack_size);
  2389. /* arg2: progs[i]->insnsi for interpreter */
  2390. if (!p->jited)
  2391. emit_mov_imm64(&prog, BPF_REG_2,
  2392. (long) p->insnsi >> 32,
  2393. (u32) (long) p->insnsi);
  2394. /* call JITed bpf program or interpreter */
  2395. if (emit_rsb_call(&prog, p->bpf_func, image + (prog - (u8 *)rw_image)))
  2396. return -EINVAL;
  2397. /*
  2398. * BPF_TRAMP_MODIFY_RETURN trampolines can modify the return
  2399. * of the previous call which is then passed on the stack to
  2400. * the next BPF program.
  2401. *
  2402. * BPF_TRAMP_FENTRY trampoline may need to return the return
  2403. * value of BPF_PROG_TYPE_STRUCT_OPS prog.
  2404. */
  2405. if (save_ret)
  2406. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
  2407. /* replace 2 nops with JE insn, since jmp target is known */
  2408. jmp_insn[0] = X86_JE;
  2409. jmp_insn[1] = prog - jmp_insn - 2;
  2410. /* arg1: mov rdi, progs[i] */
  2411. emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32, (u32) (long) p);
  2412. /* arg2: mov rsi, rbx <- start time in nsec */
  2413. emit_mov_reg(&prog, true, BPF_REG_2, BPF_REG_6);
  2414. /* arg3: lea rdx, [rbp - run_ctx_off] */
  2415. if (!is_imm8(-run_ctx_off))
  2416. EMIT3_off32(0x48, 0x8D, 0x95, -run_ctx_off);
  2417. else
  2418. EMIT4(0x48, 0x8D, 0x55, -run_ctx_off);
  2419. if (emit_rsb_call(&prog, bpf_trampoline_exit(p), image + (prog - (u8 *)rw_image)))
  2420. return -EINVAL;
  2421. *pprog = prog;
  2422. return 0;
  2423. }
  2424. static void emit_align(u8 **pprog, u32 align)
  2425. {
  2426. u8 *target, *prog = *pprog;
  2427. target = PTR_ALIGN(prog, align);
  2428. if (target != prog)
  2429. emit_nops(&prog, target - prog);
  2430. *pprog = prog;
  2431. }
  2432. static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
  2433. {
  2434. u8 *prog = *pprog;
  2435. s64 offset;
  2436. offset = func - (ip + 2 + 4);
  2437. if (!is_simm32(offset)) {
  2438. pr_err("Target %p is out of range\n", func);
  2439. return -EINVAL;
  2440. }
  2441. EMIT2_off32(0x0F, jmp_cond + 0x10, offset);
  2442. *pprog = prog;
  2443. return 0;
  2444. }
  2445. static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
  2446. struct bpf_tramp_links *tl, int stack_size,
  2447. int run_ctx_off, bool save_ret,
  2448. void *image, void *rw_image)
  2449. {
  2450. int i;
  2451. u8 *prog = *pprog;
  2452. for (i = 0; i < tl->nr_links; i++) {
  2453. if (invoke_bpf_prog(m, &prog, tl->links[i], stack_size,
  2454. run_ctx_off, save_ret, image, rw_image))
  2455. return -EINVAL;
  2456. }
  2457. *pprog = prog;
  2458. return 0;
  2459. }
  2460. static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
  2461. struct bpf_tramp_links *tl, int stack_size,
  2462. int run_ctx_off, u8 **branches,
  2463. void *image, void *rw_image)
  2464. {
  2465. u8 *prog = *pprog;
  2466. int i;
  2467. /* The first fmod_ret program will receive a garbage return value.
  2468. * Set this to 0 to avoid confusing the program.
  2469. */
  2470. emit_mov_imm32(&prog, false, BPF_REG_0, 0);
  2471. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
  2472. for (i = 0; i < tl->nr_links; i++) {
  2473. if (invoke_bpf_prog(m, &prog, tl->links[i], stack_size, run_ctx_off, true,
  2474. image, rw_image))
  2475. return -EINVAL;
  2476. /* mod_ret prog stored return value into [rbp - 8]. Emit:
  2477. * if (*(u64 *)(rbp - 8) != 0)
  2478. * goto do_fexit;
  2479. */
  2480. /* cmp QWORD PTR [rbp - 0x8], 0x0 */
  2481. EMIT4(0x48, 0x83, 0x7d, 0xf8); EMIT1(0x00);
  2482. /* Save the location of the branch and Generate 6 nops
  2483. * (4 bytes for an offset and 2 bytes for the jump) These nops
  2484. * are replaced with a conditional jump once do_fexit (i.e. the
  2485. * start of the fexit invocation) is finalized.
  2486. */
  2487. branches[i] = prog;
  2488. emit_nops(&prog, 4 + 2);
  2489. }
  2490. *pprog = prog;
  2491. return 0;
  2492. }
  2493. /* mov rax, qword ptr [rbp - rounded_stack_depth - 8] */
  2494. #define LOAD_TRAMP_TAIL_CALL_CNT_PTR(stack) \
  2495. __LOAD_TCC_PTR(-round_up(stack, 8) - 8)
  2496. /* Example:
  2497. * __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
  2498. * its 'struct btf_func_model' will be nr_args=2
  2499. * The assembly code when eth_type_trans is executing after trampoline:
  2500. *
  2501. * push rbp
  2502. * mov rbp, rsp
  2503. * sub rsp, 16 // space for skb and dev
  2504. * push rbx // temp regs to pass start time
  2505. * mov qword ptr [rbp - 16], rdi // save skb pointer to stack
  2506. * mov qword ptr [rbp - 8], rsi // save dev pointer to stack
  2507. * call __bpf_prog_enter // rcu_read_lock and preempt_disable
  2508. * mov rbx, rax // remember start time in bpf stats are enabled
  2509. * lea rdi, [rbp - 16] // R1==ctx of bpf prog
  2510. * call addr_of_jited_FENTRY_prog
  2511. * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
  2512. * mov rsi, rbx // prog start time
  2513. * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
  2514. * mov rdi, qword ptr [rbp - 16] // restore skb pointer from stack
  2515. * mov rsi, qword ptr [rbp - 8] // restore dev pointer from stack
  2516. * pop rbx
  2517. * leave
  2518. * ret
  2519. *
  2520. * eth_type_trans has 5 byte nop at the beginning. These 5 bytes will be
  2521. * replaced with 'call generated_bpf_trampoline'. When it returns
  2522. * eth_type_trans will continue executing with original skb and dev pointers.
  2523. *
  2524. * The assembly code when eth_type_trans is called from trampoline:
  2525. *
  2526. * push rbp
  2527. * mov rbp, rsp
  2528. * sub rsp, 24 // space for skb, dev, return value
  2529. * push rbx // temp regs to pass start time
  2530. * mov qword ptr [rbp - 24], rdi // save skb pointer to stack
  2531. * mov qword ptr [rbp - 16], rsi // save dev pointer to stack
  2532. * call __bpf_prog_enter // rcu_read_lock and preempt_disable
  2533. * mov rbx, rax // remember start time if bpf stats are enabled
  2534. * lea rdi, [rbp - 24] // R1==ctx of bpf prog
  2535. * call addr_of_jited_FENTRY_prog // bpf prog can access skb and dev
  2536. * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
  2537. * mov rsi, rbx // prog start time
  2538. * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
  2539. * mov rdi, qword ptr [rbp - 24] // restore skb pointer from stack
  2540. * mov rsi, qword ptr [rbp - 16] // restore dev pointer from stack
  2541. * call eth_type_trans+5 // execute body of eth_type_trans
  2542. * mov qword ptr [rbp - 8], rax // save return value
  2543. * call __bpf_prog_enter // rcu_read_lock and preempt_disable
  2544. * mov rbx, rax // remember start time in bpf stats are enabled
  2545. * lea rdi, [rbp - 24] // R1==ctx of bpf prog
  2546. * call addr_of_jited_FEXIT_prog // bpf prog can access skb, dev, return value
  2547. * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
  2548. * mov rsi, rbx // prog start time
  2549. * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
  2550. * mov rax, qword ptr [rbp - 8] // restore eth_type_trans's return value
  2551. * pop rbx
  2552. * leave
  2553. * add rsp, 8 // skip eth_type_trans's frame
  2554. * ret // return to its caller
  2555. */
  2556. static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_image,
  2557. void *rw_image_end, void *image,
  2558. const struct btf_func_model *m, u32 flags,
  2559. struct bpf_tramp_links *tlinks,
  2560. void *func_addr)
  2561. {
  2562. int i, ret, nr_regs = m->nr_args, stack_size = 0;
  2563. int regs_off, nregs_off, ip_off, run_ctx_off, arg_stack_off, rbx_off;
  2564. struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
  2565. struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
  2566. struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
  2567. void *orig_call = func_addr;
  2568. u8 **branches = NULL;
  2569. u8 *prog;
  2570. bool save_ret;
  2571. /*
  2572. * F_INDIRECT is only compatible with F_RET_FENTRY_RET, it is
  2573. * explicitly incompatible with F_CALL_ORIG | F_SKIP_FRAME | F_IP_ARG
  2574. * because @func_addr.
  2575. */
  2576. WARN_ON_ONCE((flags & BPF_TRAMP_F_INDIRECT) &&
  2577. (flags & ~(BPF_TRAMP_F_INDIRECT | BPF_TRAMP_F_RET_FENTRY_RET)));
  2578. /* extra registers for struct arguments */
  2579. for (i = 0; i < m->nr_args; i++) {
  2580. if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
  2581. nr_regs += (m->arg_size[i] + 7) / 8 - 1;
  2582. }
  2583. /* x86-64 supports up to MAX_BPF_FUNC_ARGS arguments. 1-6
  2584. * are passed through regs, the remains are through stack.
  2585. */
  2586. if (nr_regs > MAX_BPF_FUNC_ARGS)
  2587. return -ENOTSUPP;
  2588. /* Generated trampoline stack layout:
  2589. *
  2590. * RBP + 8 [ return address ]
  2591. * RBP + 0 [ RBP ]
  2592. *
  2593. * RBP - 8 [ return value ] BPF_TRAMP_F_CALL_ORIG or
  2594. * BPF_TRAMP_F_RET_FENTRY_RET flags
  2595. *
  2596. * [ reg_argN ] always
  2597. * [ ... ]
  2598. * RBP - regs_off [ reg_arg1 ] program's ctx pointer
  2599. *
  2600. * RBP - nregs_off [ regs count ] always
  2601. *
  2602. * RBP - ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag
  2603. *
  2604. * RBP - rbx_off [ rbx value ] always
  2605. *
  2606. * RBP - run_ctx_off [ bpf_tramp_run_ctx ]
  2607. *
  2608. * [ stack_argN ] BPF_TRAMP_F_CALL_ORIG
  2609. * [ ... ]
  2610. * [ stack_arg2 ]
  2611. * RBP - arg_stack_off [ stack_arg1 ]
  2612. * RSP [ tail_call_cnt_ptr ] BPF_TRAMP_F_TAIL_CALL_CTX
  2613. */
  2614. /* room for return value of orig_call or fentry prog */
  2615. save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
  2616. if (save_ret)
  2617. stack_size += 8;
  2618. stack_size += nr_regs * 8;
  2619. regs_off = stack_size;
  2620. /* regs count */
  2621. stack_size += 8;
  2622. nregs_off = stack_size;
  2623. if (flags & BPF_TRAMP_F_IP_ARG)
  2624. stack_size += 8; /* room for IP address argument */
  2625. ip_off = stack_size;
  2626. stack_size += 8;
  2627. rbx_off = stack_size;
  2628. stack_size += (sizeof(struct bpf_tramp_run_ctx) + 7) & ~0x7;
  2629. run_ctx_off = stack_size;
  2630. if (nr_regs > 6 && (flags & BPF_TRAMP_F_CALL_ORIG)) {
  2631. /* the space that used to pass arguments on-stack */
  2632. stack_size += (nr_regs - get_nr_used_regs(m)) * 8;
  2633. /* make sure the stack pointer is 16-byte aligned if we
  2634. * need pass arguments on stack, which means
  2635. * [stack_size + 8(rbp) + 8(rip) + 8(origin rip)]
  2636. * should be 16-byte aligned. Following code depend on
  2637. * that stack_size is already 8-byte aligned.
  2638. */
  2639. stack_size += (stack_size % 16) ? 0 : 8;
  2640. }
  2641. arg_stack_off = stack_size;
  2642. if (flags & BPF_TRAMP_F_SKIP_FRAME) {
  2643. /* skip patched call instruction and point orig_call to actual
  2644. * body of the kernel function.
  2645. */
  2646. if (is_endbr(*(u32 *)orig_call))
  2647. orig_call += ENDBR_INSN_SIZE;
  2648. orig_call += X86_PATCH_SIZE;
  2649. }
  2650. prog = rw_image;
  2651. if (flags & BPF_TRAMP_F_INDIRECT) {
  2652. /*
  2653. * Indirect call for bpf_struct_ops
  2654. */
  2655. emit_cfi(&prog, cfi_get_func_hash(func_addr));
  2656. } else {
  2657. /*
  2658. * Direct-call fentry stub, as such it needs accounting for the
  2659. * __fentry__ call.
  2660. */
  2661. x86_call_depth_emit_accounting(&prog, NULL, image);
  2662. }
  2663. EMIT1(0x55); /* push rbp */
  2664. EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
  2665. if (!is_imm8(stack_size)) {
  2666. /* sub rsp, stack_size */
  2667. EMIT3_off32(0x48, 0x81, 0xEC, stack_size);
  2668. } else {
  2669. /* sub rsp, stack_size */
  2670. EMIT4(0x48, 0x83, 0xEC, stack_size);
  2671. }
  2672. if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
  2673. EMIT1(0x50); /* push rax */
  2674. /* mov QWORD PTR [rbp - rbx_off], rbx */
  2675. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_6, -rbx_off);
  2676. /* Store number of argument registers of the traced function:
  2677. * mov rax, nr_regs
  2678. * mov QWORD PTR [rbp - nregs_off], rax
  2679. */
  2680. emit_mov_imm64(&prog, BPF_REG_0, 0, (u32) nr_regs);
  2681. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -nregs_off);
  2682. if (flags & BPF_TRAMP_F_IP_ARG) {
  2683. /* Store IP address of the traced function:
  2684. * movabsq rax, func_addr
  2685. * mov QWORD PTR [rbp - ip_off], rax
  2686. */
  2687. emit_mov_imm64(&prog, BPF_REG_0, (long) func_addr >> 32, (u32) (long) func_addr);
  2688. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -ip_off);
  2689. }
  2690. save_args(m, &prog, regs_off, false);
  2691. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2692. /* arg1: mov rdi, im */
  2693. emit_mov_imm64(&prog, BPF_REG_1, (long) im >> 32, (u32) (long) im);
  2694. if (emit_rsb_call(&prog, __bpf_tramp_enter,
  2695. image + (prog - (u8 *)rw_image))) {
  2696. ret = -EINVAL;
  2697. goto cleanup;
  2698. }
  2699. }
  2700. if (fentry->nr_links) {
  2701. if (invoke_bpf(m, &prog, fentry, regs_off, run_ctx_off,
  2702. flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image))
  2703. return -EINVAL;
  2704. }
  2705. if (fmod_ret->nr_links) {
  2706. branches = kcalloc(fmod_ret->nr_links, sizeof(u8 *),
  2707. GFP_KERNEL);
  2708. if (!branches)
  2709. return -ENOMEM;
  2710. if (invoke_bpf_mod_ret(m, &prog, fmod_ret, regs_off,
  2711. run_ctx_off, branches, image, rw_image)) {
  2712. ret = -EINVAL;
  2713. goto cleanup;
  2714. }
  2715. }
  2716. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2717. restore_regs(m, &prog, regs_off);
  2718. save_args(m, &prog, arg_stack_off, true);
  2719. if (flags & BPF_TRAMP_F_TAIL_CALL_CTX) {
  2720. /* Before calling the original function, load the
  2721. * tail_call_cnt_ptr from stack to rax.
  2722. */
  2723. LOAD_TRAMP_TAIL_CALL_CNT_PTR(stack_size);
  2724. }
  2725. if (flags & BPF_TRAMP_F_ORIG_STACK) {
  2726. emit_ldx(&prog, BPF_DW, BPF_REG_6, BPF_REG_FP, 8);
  2727. EMIT2(0xff, 0xd3); /* call *rbx */
  2728. } else {
  2729. /* call original function */
  2730. if (emit_rsb_call(&prog, orig_call, image + (prog - (u8 *)rw_image))) {
  2731. ret = -EINVAL;
  2732. goto cleanup;
  2733. }
  2734. }
  2735. /* remember return value in a stack for bpf prog to access */
  2736. emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
  2737. im->ip_after_call = image + (prog - (u8 *)rw_image);
  2738. emit_nops(&prog, X86_PATCH_SIZE);
  2739. }
  2740. if (fmod_ret->nr_links) {
  2741. /* From Intel 64 and IA-32 Architectures Optimization
  2742. * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
  2743. * Coding Rule 11: All branch targets should be 16-byte
  2744. * aligned.
  2745. */
  2746. emit_align(&prog, 16);
  2747. /* Update the branches saved in invoke_bpf_mod_ret with the
  2748. * aligned address of do_fexit.
  2749. */
  2750. for (i = 0; i < fmod_ret->nr_links; i++) {
  2751. emit_cond_near_jump(&branches[i], image + (prog - (u8 *)rw_image),
  2752. image + (branches[i] - (u8 *)rw_image), X86_JNE);
  2753. }
  2754. }
  2755. if (fexit->nr_links) {
  2756. if (invoke_bpf(m, &prog, fexit, regs_off, run_ctx_off,
  2757. false, image, rw_image)) {
  2758. ret = -EINVAL;
  2759. goto cleanup;
  2760. }
  2761. }
  2762. if (flags & BPF_TRAMP_F_RESTORE_REGS)
  2763. restore_regs(m, &prog, regs_off);
  2764. /* This needs to be done regardless. If there were fmod_ret programs,
  2765. * the return value is only updated on the stack and still needs to be
  2766. * restored to R0.
  2767. */
  2768. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2769. im->ip_epilogue = image + (prog - (u8 *)rw_image);
  2770. /* arg1: mov rdi, im */
  2771. emit_mov_imm64(&prog, BPF_REG_1, (long) im >> 32, (u32) (long) im);
  2772. if (emit_rsb_call(&prog, __bpf_tramp_exit, image + (prog - (u8 *)rw_image))) {
  2773. ret = -EINVAL;
  2774. goto cleanup;
  2775. }
  2776. } else if (flags & BPF_TRAMP_F_TAIL_CALL_CTX) {
  2777. /* Before running the original function, load the
  2778. * tail_call_cnt_ptr from stack to rax.
  2779. */
  2780. LOAD_TRAMP_TAIL_CALL_CNT_PTR(stack_size);
  2781. }
  2782. /* restore return value of orig_call or fentry prog back into RAX */
  2783. if (save_ret)
  2784. emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, -8);
  2785. emit_ldx(&prog, BPF_DW, BPF_REG_6, BPF_REG_FP, -rbx_off);
  2786. EMIT1(0xC9); /* leave */
  2787. if (flags & BPF_TRAMP_F_SKIP_FRAME) {
  2788. /* skip our return address and return to parent */
  2789. EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */
  2790. }
  2791. emit_return(&prog, image + (prog - (u8 *)rw_image));
  2792. /* Make sure the trampoline generation logic doesn't overflow */
  2793. if (WARN_ON_ONCE(prog > (u8 *)rw_image_end - BPF_INSN_SAFETY)) {
  2794. ret = -EFAULT;
  2795. goto cleanup;
  2796. }
  2797. ret = prog - (u8 *)rw_image + BPF_INSN_SAFETY;
  2798. cleanup:
  2799. kfree(branches);
  2800. return ret;
  2801. }
  2802. void *arch_alloc_bpf_trampoline(unsigned int size)
  2803. {
  2804. return bpf_prog_pack_alloc(size, jit_fill_hole);
  2805. }
  2806. void arch_free_bpf_trampoline(void *image, unsigned int size)
  2807. {
  2808. bpf_prog_pack_free(image, size);
  2809. }
  2810. int arch_protect_bpf_trampoline(void *image, unsigned int size)
  2811. {
  2812. return 0;
  2813. }
  2814. int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,
  2815. const struct btf_func_model *m, u32 flags,
  2816. struct bpf_tramp_links *tlinks,
  2817. void *func_addr)
  2818. {
  2819. void *rw_image, *tmp;
  2820. int ret;
  2821. u32 size = image_end - image;
  2822. /* rw_image doesn't need to be in module memory range, so we can
  2823. * use kvmalloc.
  2824. */
  2825. rw_image = kvmalloc(size, GFP_KERNEL);
  2826. if (!rw_image)
  2827. return -ENOMEM;
  2828. ret = __arch_prepare_bpf_trampoline(im, rw_image, rw_image + size, image, m,
  2829. flags, tlinks, func_addr);
  2830. if (ret < 0)
  2831. goto out;
  2832. tmp = bpf_arch_text_copy(image, rw_image, size);
  2833. if (IS_ERR(tmp))
  2834. ret = PTR_ERR(tmp);
  2835. out:
  2836. kvfree(rw_image);
  2837. return ret;
  2838. }
  2839. int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
  2840. struct bpf_tramp_links *tlinks, void *func_addr)
  2841. {
  2842. struct bpf_tramp_image im;
  2843. void *image;
  2844. int ret;
  2845. /* Allocate a temporary buffer for __arch_prepare_bpf_trampoline().
  2846. * This will NOT cause fragmentation in direct map, as we do not
  2847. * call set_memory_*() on this buffer.
  2848. *
  2849. * We cannot use kvmalloc here, because we need image to be in
  2850. * module memory range.
  2851. */
  2852. image = bpf_jit_alloc_exec(PAGE_SIZE);
  2853. if (!image)
  2854. return -ENOMEM;
  2855. ret = __arch_prepare_bpf_trampoline(&im, image, image + PAGE_SIZE, image,
  2856. m, flags, tlinks, func_addr);
  2857. bpf_jit_free_exec(image);
  2858. return ret;
  2859. }
  2860. static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs, u8 *image, u8 *buf)
  2861. {
  2862. u8 *jg_reloc, *prog = *pprog;
  2863. int pivot, err, jg_bytes = 1;
  2864. s64 jg_offset;
  2865. if (a == b) {
  2866. /* Leaf node of recursion, i.e. not a range of indices
  2867. * anymore.
  2868. */
  2869. EMIT1(add_1mod(0x48, BPF_REG_3)); /* cmp rdx,func */
  2870. if (!is_simm32(progs[a]))
  2871. return -1;
  2872. EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3),
  2873. progs[a]);
  2874. err = emit_cond_near_jump(&prog, /* je func */
  2875. (void *)progs[a], image + (prog - buf),
  2876. X86_JE);
  2877. if (err)
  2878. return err;
  2879. emit_indirect_jump(&prog, 2 /* rdx */, image + (prog - buf));
  2880. *pprog = prog;
  2881. return 0;
  2882. }
  2883. /* Not a leaf node, so we pivot, and recursively descend into
  2884. * the lower and upper ranges.
  2885. */
  2886. pivot = (b - a) / 2;
  2887. EMIT1(add_1mod(0x48, BPF_REG_3)); /* cmp rdx,func */
  2888. if (!is_simm32(progs[a + pivot]))
  2889. return -1;
  2890. EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3), progs[a + pivot]);
  2891. if (pivot > 2) { /* jg upper_part */
  2892. /* Require near jump. */
  2893. jg_bytes = 4;
  2894. EMIT2_off32(0x0F, X86_JG + 0x10, 0);
  2895. } else {
  2896. EMIT2(X86_JG, 0);
  2897. }
  2898. jg_reloc = prog;
  2899. err = emit_bpf_dispatcher(&prog, a, a + pivot, /* emit lower_part */
  2900. progs, image, buf);
  2901. if (err)
  2902. return err;
  2903. /* From Intel 64 and IA-32 Architectures Optimization
  2904. * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
  2905. * Coding Rule 11: All branch targets should be 16-byte
  2906. * aligned.
  2907. */
  2908. emit_align(&prog, 16);
  2909. jg_offset = prog - jg_reloc;
  2910. emit_code(jg_reloc - jg_bytes, jg_offset, jg_bytes);
  2911. err = emit_bpf_dispatcher(&prog, a + pivot + 1, /* emit upper_part */
  2912. b, progs, image, buf);
  2913. if (err)
  2914. return err;
  2915. *pprog = prog;
  2916. return 0;
  2917. }
  2918. static int cmp_ips(const void *a, const void *b)
  2919. {
  2920. const s64 *ipa = a;
  2921. const s64 *ipb = b;
  2922. if (*ipa > *ipb)
  2923. return 1;
  2924. if (*ipa < *ipb)
  2925. return -1;
  2926. return 0;
  2927. }
  2928. int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs)
  2929. {
  2930. u8 *prog = buf;
  2931. sort(funcs, num_funcs, sizeof(funcs[0]), cmp_ips, NULL);
  2932. return emit_bpf_dispatcher(&prog, 0, num_funcs - 1, funcs, image, buf);
  2933. }
  2934. struct x64_jit_data {
  2935. struct bpf_binary_header *rw_header;
  2936. struct bpf_binary_header *header;
  2937. int *addrs;
  2938. u8 *image;
  2939. int proglen;
  2940. struct jit_context ctx;
  2941. };
  2942. #define MAX_PASSES 20
  2943. #define PADDING_PASSES (MAX_PASSES - 5)
  2944. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  2945. {
  2946. struct bpf_binary_header *rw_header = NULL;
  2947. struct bpf_binary_header *header = NULL;
  2948. struct bpf_prog *tmp, *orig_prog = prog;
  2949. struct x64_jit_data *jit_data;
  2950. int proglen, oldproglen = 0;
  2951. struct jit_context ctx = {};
  2952. bool tmp_blinded = false;
  2953. bool extra_pass = false;
  2954. bool padding = false;
  2955. u8 *rw_image = NULL;
  2956. u8 *image = NULL;
  2957. int *addrs;
  2958. int pass;
  2959. int i;
  2960. if (!prog->jit_requested)
  2961. return orig_prog;
  2962. tmp = bpf_jit_blind_constants(prog);
  2963. /*
  2964. * If blinding was requested and we failed during blinding,
  2965. * we must fall back to the interpreter.
  2966. */
  2967. if (IS_ERR(tmp))
  2968. return orig_prog;
  2969. if (tmp != prog) {
  2970. tmp_blinded = true;
  2971. prog = tmp;
  2972. }
  2973. jit_data = prog->aux->jit_data;
  2974. if (!jit_data) {
  2975. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  2976. if (!jit_data) {
  2977. prog = orig_prog;
  2978. goto out;
  2979. }
  2980. prog->aux->jit_data = jit_data;
  2981. }
  2982. addrs = jit_data->addrs;
  2983. if (addrs) {
  2984. ctx = jit_data->ctx;
  2985. oldproglen = jit_data->proglen;
  2986. image = jit_data->image;
  2987. header = jit_data->header;
  2988. rw_header = jit_data->rw_header;
  2989. rw_image = (void *)rw_header + ((void *)image - (void *)header);
  2990. extra_pass = true;
  2991. padding = true;
  2992. goto skip_init_addrs;
  2993. }
  2994. addrs = kvmalloc_array(prog->len + 1, sizeof(*addrs), GFP_KERNEL);
  2995. if (!addrs) {
  2996. prog = orig_prog;
  2997. goto out_addrs;
  2998. }
  2999. /*
  3000. * Before first pass, make a rough estimation of addrs[]
  3001. * each BPF instruction is translated to less than 64 bytes
  3002. */
  3003. for (proglen = 0, i = 0; i <= prog->len; i++) {
  3004. proglen += 64;
  3005. addrs[i] = proglen;
  3006. }
  3007. ctx.cleanup_addr = proglen;
  3008. skip_init_addrs:
  3009. /*
  3010. * JITed image shrinks with every pass and the loop iterates
  3011. * until the image stops shrinking. Very large BPF programs
  3012. * may converge on the last pass. In such case do one more
  3013. * pass to emit the final image.
  3014. */
  3015. for (pass = 0; pass < MAX_PASSES || image; pass++) {
  3016. if (!padding && pass >= PADDING_PASSES)
  3017. padding = true;
  3018. proglen = do_jit(prog, addrs, image, rw_image, oldproglen, &ctx, padding);
  3019. if (proglen <= 0) {
  3020. out_image:
  3021. image = NULL;
  3022. if (header) {
  3023. bpf_arch_text_copy(&header->size, &rw_header->size,
  3024. sizeof(rw_header->size));
  3025. bpf_jit_binary_pack_free(header, rw_header);
  3026. }
  3027. /* Fall back to interpreter mode */
  3028. prog = orig_prog;
  3029. if (extra_pass) {
  3030. prog->bpf_func = NULL;
  3031. prog->jited = 0;
  3032. prog->jited_len = 0;
  3033. }
  3034. goto out_addrs;
  3035. }
  3036. if (image) {
  3037. if (proglen != oldproglen) {
  3038. pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
  3039. proglen, oldproglen);
  3040. goto out_image;
  3041. }
  3042. break;
  3043. }
  3044. if (proglen == oldproglen) {
  3045. /*
  3046. * The number of entries in extable is the number of BPF_LDX
  3047. * insns that access kernel memory via "pointer to BTF type".
  3048. * The verifier changed their opcode from LDX|MEM|size
  3049. * to LDX|PROBE_MEM|size to make JITing easier.
  3050. */
  3051. u32 align = __alignof__(struct exception_table_entry);
  3052. u32 extable_size = prog->aux->num_exentries *
  3053. sizeof(struct exception_table_entry);
  3054. /* allocate module memory for x86 insns and extable */
  3055. header = bpf_jit_binary_pack_alloc(roundup(proglen, align) + extable_size,
  3056. &image, align, &rw_header, &rw_image,
  3057. jit_fill_hole);
  3058. if (!header) {
  3059. prog = orig_prog;
  3060. goto out_addrs;
  3061. }
  3062. prog->aux->extable = (void *) image + roundup(proglen, align);
  3063. }
  3064. oldproglen = proglen;
  3065. cond_resched();
  3066. }
  3067. if (bpf_jit_enable > 1)
  3068. bpf_jit_dump(prog->len, proglen, pass + 1, rw_image);
  3069. if (image) {
  3070. if (!prog->is_func || extra_pass) {
  3071. /*
  3072. * bpf_jit_binary_pack_finalize fails in two scenarios:
  3073. * 1) header is not pointing to proper module memory;
  3074. * 2) the arch doesn't support bpf_arch_text_copy().
  3075. *
  3076. * Both cases are serious bugs and justify WARN_ON.
  3077. */
  3078. if (WARN_ON(bpf_jit_binary_pack_finalize(header, rw_header))) {
  3079. /* header has been freed */
  3080. header = NULL;
  3081. goto out_image;
  3082. }
  3083. bpf_tail_call_direct_fixup(prog);
  3084. } else {
  3085. jit_data->addrs = addrs;
  3086. jit_data->ctx = ctx;
  3087. jit_data->proglen = proglen;
  3088. jit_data->image = image;
  3089. jit_data->header = header;
  3090. jit_data->rw_header = rw_header;
  3091. }
  3092. /*
  3093. * ctx.prog_offset is used when CFI preambles put code *before*
  3094. * the function. See emit_cfi(). For FineIBT specifically this code
  3095. * can also be executed and bpf_prog_kallsyms_add() will
  3096. * generate an additional symbol to cover this, hence also
  3097. * decrement proglen.
  3098. */
  3099. prog->bpf_func = (void *)image + cfi_get_offset();
  3100. prog->jited = 1;
  3101. prog->jited_len = proglen - cfi_get_offset();
  3102. } else {
  3103. prog = orig_prog;
  3104. }
  3105. if (!image || !prog->is_func || extra_pass) {
  3106. if (image)
  3107. bpf_prog_fill_jited_linfo(prog, addrs + 1);
  3108. out_addrs:
  3109. kvfree(addrs);
  3110. kfree(jit_data);
  3111. prog->aux->jit_data = NULL;
  3112. }
  3113. out:
  3114. if (tmp_blinded)
  3115. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  3116. tmp : orig_prog);
  3117. return prog;
  3118. }
  3119. bool bpf_jit_supports_kfunc_call(void)
  3120. {
  3121. return true;
  3122. }
  3123. void *bpf_arch_text_copy(void *dst, void *src, size_t len)
  3124. {
  3125. if (text_poke_copy(dst, src, len) == NULL)
  3126. return ERR_PTR(-EINVAL);
  3127. return dst;
  3128. }
  3129. /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
  3130. bool bpf_jit_supports_subprog_tailcalls(void)
  3131. {
  3132. return true;
  3133. }
  3134. bool bpf_jit_supports_percpu_insn(void)
  3135. {
  3136. return true;
  3137. }
  3138. void bpf_jit_free(struct bpf_prog *prog)
  3139. {
  3140. if (prog->jited) {
  3141. struct x64_jit_data *jit_data = prog->aux->jit_data;
  3142. struct bpf_binary_header *hdr;
  3143. /*
  3144. * If we fail the final pass of JIT (from jit_subprogs),
  3145. * the program may not be finalized yet. Call finalize here
  3146. * before freeing it.
  3147. */
  3148. if (jit_data) {
  3149. bpf_jit_binary_pack_finalize(jit_data->header,
  3150. jit_data->rw_header);
  3151. kvfree(jit_data->addrs);
  3152. kfree(jit_data);
  3153. }
  3154. prog->bpf_func = (void *)prog->bpf_func - cfi_get_offset();
  3155. hdr = bpf_jit_binary_pack_hdr(prog);
  3156. bpf_jit_binary_pack_free(hdr, NULL);
  3157. WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
  3158. }
  3159. bpf_prog_unlock_free(prog);
  3160. }
  3161. bool bpf_jit_supports_exceptions(void)
  3162. {
  3163. /* We unwind through both kernel frames (starting from within bpf_throw
  3164. * call) and BPF frames. Therefore we require ORC unwinder to be enabled
  3165. * to walk kernel frames and reach BPF frames in the stack trace.
  3166. */
  3167. return IS_ENABLED(CONFIG_UNWINDER_ORC);
  3168. }
  3169. void arch_bpf_stack_walk(bool (*consume_fn)(void *cookie, u64 ip, u64 sp, u64 bp), void *cookie)
  3170. {
  3171. #if defined(CONFIG_UNWINDER_ORC)
  3172. struct unwind_state state;
  3173. unsigned long addr;
  3174. for (unwind_start(&state, current, NULL, NULL); !unwind_done(&state);
  3175. unwind_next_frame(&state)) {
  3176. addr = unwind_get_return_address(&state);
  3177. if (!addr || !consume_fn(cookie, (u64)addr, (u64)state.sp, (u64)state.bp))
  3178. break;
  3179. }
  3180. return;
  3181. #endif
  3182. WARN(1, "verification of programs using bpf_throw should have failed\n");
  3183. }
  3184. void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
  3185. struct bpf_prog *new, struct bpf_prog *old)
  3186. {
  3187. u8 *old_addr, *new_addr, *old_bypass_addr;
  3188. int ret;
  3189. old_bypass_addr = old ? NULL : poke->bypass_addr;
  3190. old_addr = old ? (u8 *)old->bpf_func + poke->adj_off : NULL;
  3191. new_addr = new ? (u8 *)new->bpf_func + poke->adj_off : NULL;
  3192. /*
  3193. * On program loading or teardown, the program's kallsym entry
  3194. * might not be in place, so we use __bpf_arch_text_poke to skip
  3195. * the kallsyms check.
  3196. */
  3197. if (new) {
  3198. ret = __bpf_arch_text_poke(poke->tailcall_target,
  3199. BPF_MOD_JUMP,
  3200. old_addr, new_addr);
  3201. BUG_ON(ret < 0);
  3202. if (!old) {
  3203. ret = __bpf_arch_text_poke(poke->tailcall_bypass,
  3204. BPF_MOD_JUMP,
  3205. poke->bypass_addr,
  3206. NULL);
  3207. BUG_ON(ret < 0);
  3208. }
  3209. } else {
  3210. ret = __bpf_arch_text_poke(poke->tailcall_bypass,
  3211. BPF_MOD_JUMP,
  3212. old_bypass_addr,
  3213. poke->bypass_addr);
  3214. BUG_ON(ret < 0);
  3215. /* let other CPUs finish the execution of program
  3216. * so that it will not possible to expose them
  3217. * to invalid nop, stack unwind, nop state
  3218. */
  3219. if (!ret)
  3220. synchronize_rcu();
  3221. ret = __bpf_arch_text_poke(poke->tailcall_target,
  3222. BPF_MOD_JUMP,
  3223. old_addr, NULL);
  3224. BUG_ON(ret < 0);
  3225. }
  3226. }
  3227. bool bpf_jit_supports_arena(void)
  3228. {
  3229. return true;
  3230. }
  3231. bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
  3232. {
  3233. if (!in_arena)
  3234. return true;
  3235. switch (insn->code) {
  3236. case BPF_STX | BPF_ATOMIC | BPF_W:
  3237. case BPF_STX | BPF_ATOMIC | BPF_DW:
  3238. if (insn->imm == (BPF_AND | BPF_FETCH) ||
  3239. insn->imm == (BPF_OR | BPF_FETCH) ||
  3240. insn->imm == (BPF_XOR | BPF_FETCH))
  3241. return false;
  3242. }
  3243. return true;
  3244. }
  3245. bool bpf_jit_supports_ptr_xchg(void)
  3246. {
  3247. return true;
  3248. }
  3249. /* x86-64 JIT emits its own code to filter user addresses so return 0 here */
  3250. u64 bpf_arch_uaddress_limit(void)
  3251. {
  3252. return 0;
  3253. }