bpf_jit_comp.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * BPF JIT compiler for ARM64
  4. *
  5. * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com>
  6. */
  7. #define pr_fmt(fmt) "bpf_jit: " fmt
  8. #include <linux/arm-smccc.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/bpf.h>
  11. #include <linux/filter.h>
  12. #include <linux/memory.h>
  13. #include <linux/printk.h>
  14. #include <linux/slab.h>
  15. #include <asm/asm-extable.h>
  16. #include <asm/byteorder.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/debug-monitors.h>
  20. #include <asm/insn.h>
  21. #include <asm/patching.h>
  22. #include <asm/set_memory.h>
  23. #include "bpf_jit.h"
  24. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  25. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  26. #define TCCNT_PTR (MAX_BPF_JIT_REG + 2)
  27. #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
  28. #define ARENA_VM_START (MAX_BPF_JIT_REG + 5)
  29. #define check_imm(bits, imm) do { \
  30. if ((((imm) > 0) && ((imm) >> (bits))) || \
  31. (((imm) < 0) && (~(imm) >> (bits)))) { \
  32. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  33. i, imm, imm); \
  34. return -EINVAL; \
  35. } \
  36. } while (0)
  37. #define check_imm19(imm) check_imm(19, imm)
  38. #define check_imm26(imm) check_imm(26, imm)
  39. /* Map BPF registers to A64 registers */
  40. static const int bpf2a64[] = {
  41. /* return value from in-kernel function, and exit value from eBPF */
  42. [BPF_REG_0] = A64_R(7),
  43. /* arguments from eBPF program to in-kernel function */
  44. [BPF_REG_1] = A64_R(0),
  45. [BPF_REG_2] = A64_R(1),
  46. [BPF_REG_3] = A64_R(2),
  47. [BPF_REG_4] = A64_R(3),
  48. [BPF_REG_5] = A64_R(4),
  49. /* callee saved registers that in-kernel function will preserve */
  50. [BPF_REG_6] = A64_R(19),
  51. [BPF_REG_7] = A64_R(20),
  52. [BPF_REG_8] = A64_R(21),
  53. [BPF_REG_9] = A64_R(22),
  54. /* read-only frame pointer to access stack */
  55. [BPF_REG_FP] = A64_R(25),
  56. /* temporary registers for BPF JIT */
  57. [TMP_REG_1] = A64_R(10),
  58. [TMP_REG_2] = A64_R(11),
  59. [TMP_REG_3] = A64_R(12),
  60. /* tail_call_cnt_ptr */
  61. [TCCNT_PTR] = A64_R(26),
  62. /* temporary register for blinding constants */
  63. [BPF_REG_AX] = A64_R(9),
  64. /* callee saved register for kern_vm_start address */
  65. [ARENA_VM_START] = A64_R(28),
  66. };
  67. struct jit_ctx {
  68. const struct bpf_prog *prog;
  69. int idx;
  70. int epilogue_offset;
  71. int *offset;
  72. int exentry_idx;
  73. int nr_used_callee_reg;
  74. u8 used_callee_reg[8]; /* r6~r9, fp, arena_vm_start */
  75. __le32 *image;
  76. __le32 *ro_image;
  77. u32 stack_size;
  78. u64 user_vm_start;
  79. u64 arena_vm_start;
  80. bool fp_used;
  81. bool write;
  82. };
  83. struct bpf_plt {
  84. u32 insn_ldr; /* load target */
  85. u32 insn_br; /* branch to target */
  86. u64 target; /* target value */
  87. };
  88. #define PLT_TARGET_SIZE sizeof_field(struct bpf_plt, target)
  89. #define PLT_TARGET_OFFSET offsetof(struct bpf_plt, target)
  90. static inline void emit(const u32 insn, struct jit_ctx *ctx)
  91. {
  92. if (ctx->image != NULL && ctx->write)
  93. ctx->image[ctx->idx] = cpu_to_le32(insn);
  94. ctx->idx++;
  95. }
  96. static inline void emit_a64_mov_i(const int is64, const int reg,
  97. const s32 val, struct jit_ctx *ctx)
  98. {
  99. u16 hi = val >> 16;
  100. u16 lo = val & 0xffff;
  101. if (hi & 0x8000) {
  102. if (hi == 0xffff) {
  103. emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
  104. } else {
  105. emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
  106. if (lo != 0xffff)
  107. emit(A64_MOVK(is64, reg, lo, 0), ctx);
  108. }
  109. } else {
  110. emit(A64_MOVZ(is64, reg, lo, 0), ctx);
  111. if (hi)
  112. emit(A64_MOVK(is64, reg, hi, 16), ctx);
  113. }
  114. }
  115. static int i64_i16_blocks(const u64 val, bool inverse)
  116. {
  117. return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  118. (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  119. (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  120. (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
  121. }
  122. static inline void emit_a64_mov_i64(const int reg, const u64 val,
  123. struct jit_ctx *ctx)
  124. {
  125. u64 nrm_tmp = val, rev_tmp = ~val;
  126. bool inverse;
  127. int shift;
  128. if (!(nrm_tmp >> 32))
  129. return emit_a64_mov_i(0, reg, (u32)val, ctx);
  130. inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
  131. shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
  132. (fls64(nrm_tmp) - 1)), 16), 0);
  133. if (inverse)
  134. emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
  135. else
  136. emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  137. shift -= 16;
  138. while (shift >= 0) {
  139. if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
  140. emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  141. shift -= 16;
  142. }
  143. }
  144. static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
  145. {
  146. if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
  147. emit(insn, ctx);
  148. }
  149. /*
  150. * Kernel addresses in the vmalloc space use at most 48 bits, and the
  151. * remaining bits are guaranteed to be 0x1. So we can compose the address
  152. * with a fixed length movn/movk/movk sequence.
  153. */
  154. static inline void emit_addr_mov_i64(const int reg, const u64 val,
  155. struct jit_ctx *ctx)
  156. {
  157. u64 tmp = val;
  158. int shift = 0;
  159. emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
  160. while (shift < 32) {
  161. tmp >>= 16;
  162. shift += 16;
  163. emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
  164. }
  165. }
  166. static bool should_emit_indirect_call(long target, const struct jit_ctx *ctx)
  167. {
  168. long offset;
  169. /* when ctx->ro_image is not allocated or the target is unknown,
  170. * emit indirect call
  171. */
  172. if (!ctx->ro_image || !target)
  173. return true;
  174. offset = target - (long)&ctx->ro_image[ctx->idx];
  175. return offset < -SZ_128M || offset >= SZ_128M;
  176. }
  177. static void emit_direct_call(u64 target, struct jit_ctx *ctx)
  178. {
  179. u32 insn;
  180. unsigned long pc;
  181. pc = (unsigned long)&ctx->ro_image[ctx->idx];
  182. insn = aarch64_insn_gen_branch_imm(pc, target, AARCH64_INSN_BRANCH_LINK);
  183. emit(insn, ctx);
  184. }
  185. static void emit_indirect_call(u64 target, struct jit_ctx *ctx)
  186. {
  187. u8 tmp;
  188. tmp = bpf2a64[TMP_REG_1];
  189. emit_addr_mov_i64(tmp, target, ctx);
  190. emit(A64_BLR(tmp), ctx);
  191. }
  192. static void emit_call(u64 target, struct jit_ctx *ctx)
  193. {
  194. if (should_emit_indirect_call((long)target, ctx))
  195. emit_indirect_call(target, ctx);
  196. else
  197. emit_direct_call(target, ctx);
  198. }
  199. static inline int bpf2a64_offset(int bpf_insn, int off,
  200. const struct jit_ctx *ctx)
  201. {
  202. /* BPF JMP offset is relative to the next instruction */
  203. bpf_insn++;
  204. /*
  205. * Whereas arm64 branch instructions encode the offset
  206. * from the branch itself, so we must subtract 1 from the
  207. * instruction offset.
  208. */
  209. return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
  210. }
  211. static void jit_fill_hole(void *area, unsigned int size)
  212. {
  213. __le32 *ptr;
  214. /* We are guaranteed to have aligned memory. */
  215. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  216. *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
  217. }
  218. int bpf_arch_text_invalidate(void *dst, size_t len)
  219. {
  220. if (!aarch64_insn_set(dst, AARCH64_BREAK_FAULT, len))
  221. return -EINVAL;
  222. return 0;
  223. }
  224. static inline int epilogue_offset(const struct jit_ctx *ctx)
  225. {
  226. int to = ctx->epilogue_offset;
  227. int from = ctx->idx;
  228. return to - from;
  229. }
  230. static bool is_addsub_imm(u32 imm)
  231. {
  232. /* Either imm12 or shifted imm12. */
  233. return !(imm & ~0xfff) || !(imm & ~0xfff000);
  234. }
  235. /*
  236. * There are 3 types of AArch64 LDR/STR (immediate) instruction:
  237. * Post-index, Pre-index, Unsigned offset.
  238. *
  239. * For BPF ldr/str, the "unsigned offset" type is sufficient.
  240. *
  241. * "Unsigned offset" type LDR(immediate) format:
  242. *
  243. * 3 2 1 0
  244. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  245. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  246. * |x x|1 1 1 0 0 1 0 1| imm12 | Rn | Rt |
  247. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  248. * scale
  249. *
  250. * "Unsigned offset" type STR(immediate) format:
  251. * 3 2 1 0
  252. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  253. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  254. * |x x|1 1 1 0 0 1 0 0| imm12 | Rn | Rt |
  255. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  256. * scale
  257. *
  258. * The offset is calculated from imm12 and scale in the following way:
  259. *
  260. * offset = (u64)imm12 << scale
  261. */
  262. static bool is_lsi_offset(int offset, int scale)
  263. {
  264. if (offset < 0)
  265. return false;
  266. if (offset > (0xFFF << scale))
  267. return false;
  268. if (offset & ((1 << scale) - 1))
  269. return false;
  270. return true;
  271. }
  272. /* generated main prog prologue:
  273. * bti c // if CONFIG_ARM64_BTI_KERNEL
  274. * mov x9, lr
  275. * nop // POKE_OFFSET
  276. * paciasp // if CONFIG_ARM64_PTR_AUTH_KERNEL
  277. * stp x29, lr, [sp, #-16]!
  278. * mov x29, sp
  279. * stp xzr, x26, [sp, #-16]!
  280. * mov x26, sp
  281. * // PROLOGUE_OFFSET
  282. * // save callee-saved registers
  283. */
  284. static void prepare_bpf_tail_call_cnt(struct jit_ctx *ctx)
  285. {
  286. const bool is_main_prog = !bpf_is_subprog(ctx->prog);
  287. const u8 ptr = bpf2a64[TCCNT_PTR];
  288. if (is_main_prog) {
  289. /* Initialize tail_call_cnt. */
  290. emit(A64_PUSH(A64_ZR, ptr, A64_SP), ctx);
  291. emit(A64_MOV(1, ptr, A64_SP), ctx);
  292. } else
  293. emit(A64_PUSH(ptr, ptr, A64_SP), ctx);
  294. }
  295. static void find_used_callee_regs(struct jit_ctx *ctx)
  296. {
  297. int i;
  298. const struct bpf_prog *prog = ctx->prog;
  299. const struct bpf_insn *insn = &prog->insnsi[0];
  300. int reg_used = 0;
  301. for (i = 0; i < prog->len; i++, insn++) {
  302. if (insn->dst_reg == BPF_REG_6 || insn->src_reg == BPF_REG_6)
  303. reg_used |= 1;
  304. if (insn->dst_reg == BPF_REG_7 || insn->src_reg == BPF_REG_7)
  305. reg_used |= 2;
  306. if (insn->dst_reg == BPF_REG_8 || insn->src_reg == BPF_REG_8)
  307. reg_used |= 4;
  308. if (insn->dst_reg == BPF_REG_9 || insn->src_reg == BPF_REG_9)
  309. reg_used |= 8;
  310. if (insn->dst_reg == BPF_REG_FP || insn->src_reg == BPF_REG_FP) {
  311. ctx->fp_used = true;
  312. reg_used |= 16;
  313. }
  314. }
  315. i = 0;
  316. if (reg_used & 1)
  317. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_6];
  318. if (reg_used & 2)
  319. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_7];
  320. if (reg_used & 4)
  321. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_8];
  322. if (reg_used & 8)
  323. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_9];
  324. if (reg_used & 16)
  325. ctx->used_callee_reg[i++] = bpf2a64[BPF_REG_FP];
  326. if (ctx->arena_vm_start)
  327. ctx->used_callee_reg[i++] = bpf2a64[ARENA_VM_START];
  328. ctx->nr_used_callee_reg = i;
  329. }
  330. /* Save callee-saved registers */
  331. static void push_callee_regs(struct jit_ctx *ctx)
  332. {
  333. int reg1, reg2, i;
  334. /*
  335. * Program acting as exception boundary should save all ARM64
  336. * Callee-saved registers as the exception callback needs to recover
  337. * all ARM64 Callee-saved registers in its epilogue.
  338. */
  339. if (ctx->prog->aux->exception_boundary) {
  340. emit(A64_PUSH(A64_R(19), A64_R(20), A64_SP), ctx);
  341. emit(A64_PUSH(A64_R(21), A64_R(22), A64_SP), ctx);
  342. emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);
  343. emit(A64_PUSH(A64_R(25), A64_R(26), A64_SP), ctx);
  344. emit(A64_PUSH(A64_R(27), A64_R(28), A64_SP), ctx);
  345. ctx->fp_used = true;
  346. } else {
  347. find_used_callee_regs(ctx);
  348. for (i = 0; i + 1 < ctx->nr_used_callee_reg; i += 2) {
  349. reg1 = ctx->used_callee_reg[i];
  350. reg2 = ctx->used_callee_reg[i + 1];
  351. emit(A64_PUSH(reg1, reg2, A64_SP), ctx);
  352. }
  353. if (i < ctx->nr_used_callee_reg) {
  354. reg1 = ctx->used_callee_reg[i];
  355. /* keep SP 16-byte aligned */
  356. emit(A64_PUSH(reg1, A64_ZR, A64_SP), ctx);
  357. }
  358. }
  359. }
  360. /* Restore callee-saved registers */
  361. static void pop_callee_regs(struct jit_ctx *ctx)
  362. {
  363. struct bpf_prog_aux *aux = ctx->prog->aux;
  364. int reg1, reg2, i;
  365. /*
  366. * Program acting as exception boundary pushes R23 and R24 in addition
  367. * to BPF callee-saved registers. Exception callback uses the boundary
  368. * program's stack frame, so recover these extra registers in the above
  369. * two cases.
  370. */
  371. if (aux->exception_boundary || aux->exception_cb) {
  372. emit(A64_POP(A64_R(27), A64_R(28), A64_SP), ctx);
  373. emit(A64_POP(A64_R(25), A64_R(26), A64_SP), ctx);
  374. emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);
  375. emit(A64_POP(A64_R(21), A64_R(22), A64_SP), ctx);
  376. emit(A64_POP(A64_R(19), A64_R(20), A64_SP), ctx);
  377. } else {
  378. i = ctx->nr_used_callee_reg - 1;
  379. if (ctx->nr_used_callee_reg % 2 != 0) {
  380. reg1 = ctx->used_callee_reg[i];
  381. emit(A64_POP(reg1, A64_ZR, A64_SP), ctx);
  382. i--;
  383. }
  384. while (i > 0) {
  385. reg1 = ctx->used_callee_reg[i - 1];
  386. reg2 = ctx->used_callee_reg[i];
  387. emit(A64_POP(reg1, reg2, A64_SP), ctx);
  388. i -= 2;
  389. }
  390. }
  391. }
  392. #define BTI_INSNS (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) ? 1 : 0)
  393. #define PAC_INSNS (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) ? 1 : 0)
  394. /* Offset of nop instruction in bpf prog entry to be poked */
  395. #define POKE_OFFSET (BTI_INSNS + 1)
  396. /* Tail call offset to jump into */
  397. #define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 4)
  398. static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
  399. {
  400. const struct bpf_prog *prog = ctx->prog;
  401. const bool is_main_prog = !bpf_is_subprog(prog);
  402. const u8 fp = bpf2a64[BPF_REG_FP];
  403. const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
  404. const int idx0 = ctx->idx;
  405. int cur_offset;
  406. /*
  407. * BPF prog stack layout
  408. *
  409. * high
  410. * original A64_SP => 0:+-----+ BPF prologue
  411. * |FP/LR|
  412. * current A64_FP => -16:+-----+
  413. * | ... | callee saved registers
  414. * BPF fp register => -64:+-----+ <= (BPF_FP)
  415. * | |
  416. * | ... | BPF prog stack
  417. * | |
  418. * +-----+ <= (BPF_FP - prog->aux->stack_depth)
  419. * |RSVD | padding
  420. * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
  421. * | |
  422. * | ... | Function call stack
  423. * | |
  424. * +-----+
  425. * low
  426. *
  427. */
  428. /* bpf function may be invoked by 3 instruction types:
  429. * 1. bl, attached via freplace to bpf prog via short jump
  430. * 2. br, attached via freplace to bpf prog via long jump
  431. * 3. blr, working as a function pointer, used by emit_call.
  432. * So BTI_JC should used here to support both br and blr.
  433. */
  434. emit_bti(A64_BTI_JC, ctx);
  435. emit(A64_MOV(1, A64_R(9), A64_LR), ctx);
  436. emit(A64_NOP, ctx);
  437. if (!prog->aux->exception_cb) {
  438. /* Sign lr */
  439. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  440. emit(A64_PACIASP, ctx);
  441. /* Save FP and LR registers to stay align with ARM64 AAPCS */
  442. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  443. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  444. prepare_bpf_tail_call_cnt(ctx);
  445. if (!ebpf_from_cbpf && is_main_prog) {
  446. cur_offset = ctx->idx - idx0;
  447. if (cur_offset != PROLOGUE_OFFSET) {
  448. pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
  449. cur_offset, PROLOGUE_OFFSET);
  450. return -1;
  451. }
  452. /* BTI landing pad for the tail call, done with a BR */
  453. emit_bti(A64_BTI_J, ctx);
  454. }
  455. push_callee_regs(ctx);
  456. } else {
  457. /*
  458. * Exception callback receives FP of Main Program as third
  459. * parameter
  460. */
  461. emit(A64_MOV(1, A64_FP, A64_R(2)), ctx);
  462. /*
  463. * Main Program already pushed the frame record and the
  464. * callee-saved registers. The exception callback will not push
  465. * anything and re-use the main program's stack.
  466. *
  467. * 12 registers are on the stack
  468. */
  469. emit(A64_SUB_I(1, A64_SP, A64_FP, 96), ctx);
  470. }
  471. if (ctx->fp_used)
  472. /* Set up BPF prog stack base register */
  473. emit(A64_MOV(1, fp, A64_SP), ctx);
  474. /* Stack must be multiples of 16B */
  475. ctx->stack_size = round_up(prog->aux->stack_depth, 16);
  476. /* Set up function call stack */
  477. if (ctx->stack_size)
  478. emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  479. if (ctx->arena_vm_start)
  480. emit_a64_mov_i64(arena_vm_base, ctx->arena_vm_start, ctx);
  481. return 0;
  482. }
  483. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  484. {
  485. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  486. const u8 r2 = bpf2a64[BPF_REG_2];
  487. const u8 r3 = bpf2a64[BPF_REG_3];
  488. const u8 tmp = bpf2a64[TMP_REG_1];
  489. const u8 prg = bpf2a64[TMP_REG_2];
  490. const u8 tcc = bpf2a64[TMP_REG_3];
  491. const u8 ptr = bpf2a64[TCCNT_PTR];
  492. size_t off;
  493. __le32 *branch1 = NULL;
  494. __le32 *branch2 = NULL;
  495. __le32 *branch3 = NULL;
  496. /* if (index >= array->map.max_entries)
  497. * goto out;
  498. */
  499. off = offsetof(struct bpf_array, map.max_entries);
  500. emit_a64_mov_i64(tmp, off, ctx);
  501. emit(A64_LDR32(tmp, r2, tmp), ctx);
  502. emit(A64_MOV(0, r3, r3), ctx);
  503. emit(A64_CMP(0, r3, tmp), ctx);
  504. branch1 = ctx->image + ctx->idx;
  505. emit(A64_NOP, ctx);
  506. /*
  507. * if ((*tail_call_cnt_ptr) >= MAX_TAIL_CALL_CNT)
  508. * goto out;
  509. */
  510. emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
  511. emit(A64_LDR64I(tcc, ptr, 0), ctx);
  512. emit(A64_CMP(1, tcc, tmp), ctx);
  513. branch2 = ctx->image + ctx->idx;
  514. emit(A64_NOP, ctx);
  515. /* (*tail_call_cnt_ptr)++; */
  516. emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
  517. /* prog = array->ptrs[index];
  518. * if (prog == NULL)
  519. * goto out;
  520. */
  521. off = offsetof(struct bpf_array, ptrs);
  522. emit_a64_mov_i64(tmp, off, ctx);
  523. emit(A64_ADD(1, tmp, r2, tmp), ctx);
  524. emit(A64_LSL(1, prg, r3, 3), ctx);
  525. emit(A64_LDR64(prg, tmp, prg), ctx);
  526. branch3 = ctx->image + ctx->idx;
  527. emit(A64_NOP, ctx);
  528. /* Update tail_call_cnt if the slot is populated. */
  529. emit(A64_STR64I(tcc, ptr, 0), ctx);
  530. /* restore SP */
  531. if (ctx->stack_size)
  532. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  533. pop_callee_regs(ctx);
  534. /* goto *(prog->bpf_func + prologue_offset); */
  535. off = offsetof(struct bpf_prog, bpf_func);
  536. emit_a64_mov_i64(tmp, off, ctx);
  537. emit(A64_LDR64(tmp, prg, tmp), ctx);
  538. emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
  539. emit(A64_BR(tmp), ctx);
  540. if (ctx->image) {
  541. off = &ctx->image[ctx->idx] - branch1;
  542. *branch1 = cpu_to_le32(A64_B_(A64_COND_CS, off));
  543. off = &ctx->image[ctx->idx] - branch2;
  544. *branch2 = cpu_to_le32(A64_B_(A64_COND_CS, off));
  545. off = &ctx->image[ctx->idx] - branch3;
  546. *branch3 = cpu_to_le32(A64_CBZ(1, prg, off));
  547. }
  548. return 0;
  549. }
  550. #ifdef CONFIG_ARM64_LSE_ATOMICS
  551. static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  552. {
  553. const u8 code = insn->code;
  554. const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
  555. const u8 dst = bpf2a64[insn->dst_reg];
  556. const u8 src = bpf2a64[insn->src_reg];
  557. const u8 tmp = bpf2a64[TMP_REG_1];
  558. const u8 tmp2 = bpf2a64[TMP_REG_2];
  559. const bool isdw = BPF_SIZE(code) == BPF_DW;
  560. const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC;
  561. const s16 off = insn->off;
  562. u8 reg = dst;
  563. if (off || arena) {
  564. if (off) {
  565. emit_a64_mov_i(1, tmp, off, ctx);
  566. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  567. reg = tmp;
  568. }
  569. if (arena) {
  570. emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx);
  571. reg = tmp;
  572. }
  573. }
  574. switch (insn->imm) {
  575. /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
  576. case BPF_ADD:
  577. emit(A64_STADD(isdw, reg, src), ctx);
  578. break;
  579. case BPF_AND:
  580. emit(A64_MVN(isdw, tmp2, src), ctx);
  581. emit(A64_STCLR(isdw, reg, tmp2), ctx);
  582. break;
  583. case BPF_OR:
  584. emit(A64_STSET(isdw, reg, src), ctx);
  585. break;
  586. case BPF_XOR:
  587. emit(A64_STEOR(isdw, reg, src), ctx);
  588. break;
  589. /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
  590. case BPF_ADD | BPF_FETCH:
  591. emit(A64_LDADDAL(isdw, src, reg, src), ctx);
  592. break;
  593. case BPF_AND | BPF_FETCH:
  594. emit(A64_MVN(isdw, tmp2, src), ctx);
  595. emit(A64_LDCLRAL(isdw, src, reg, tmp2), ctx);
  596. break;
  597. case BPF_OR | BPF_FETCH:
  598. emit(A64_LDSETAL(isdw, src, reg, src), ctx);
  599. break;
  600. case BPF_XOR | BPF_FETCH:
  601. emit(A64_LDEORAL(isdw, src, reg, src), ctx);
  602. break;
  603. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  604. case BPF_XCHG:
  605. emit(A64_SWPAL(isdw, src, reg, src), ctx);
  606. break;
  607. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  608. case BPF_CMPXCHG:
  609. emit(A64_CASAL(isdw, src, reg, bpf2a64[BPF_REG_0]), ctx);
  610. break;
  611. default:
  612. pr_err_once("unknown atomic op code %02x\n", insn->imm);
  613. return -EINVAL;
  614. }
  615. return 0;
  616. }
  617. #else
  618. static inline int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  619. {
  620. return -EINVAL;
  621. }
  622. #endif
  623. static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  624. {
  625. const u8 code = insn->code;
  626. const u8 dst = bpf2a64[insn->dst_reg];
  627. const u8 src = bpf2a64[insn->src_reg];
  628. const u8 tmp = bpf2a64[TMP_REG_1];
  629. const u8 tmp2 = bpf2a64[TMP_REG_2];
  630. const u8 tmp3 = bpf2a64[TMP_REG_3];
  631. const int i = insn - ctx->prog->insnsi;
  632. const s32 imm = insn->imm;
  633. const s16 off = insn->off;
  634. const bool isdw = BPF_SIZE(code) == BPF_DW;
  635. u8 reg;
  636. s32 jmp_offset;
  637. if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
  638. /* ll_sc based atomics don't support unsafe pointers yet. */
  639. pr_err_once("unknown atomic opcode %02x\n", code);
  640. return -EINVAL;
  641. }
  642. if (!off) {
  643. reg = dst;
  644. } else {
  645. emit_a64_mov_i(1, tmp, off, ctx);
  646. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  647. reg = tmp;
  648. }
  649. if (imm == BPF_ADD || imm == BPF_AND ||
  650. imm == BPF_OR || imm == BPF_XOR) {
  651. /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
  652. emit(A64_LDXR(isdw, tmp2, reg), ctx);
  653. if (imm == BPF_ADD)
  654. emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
  655. else if (imm == BPF_AND)
  656. emit(A64_AND(isdw, tmp2, tmp2, src), ctx);
  657. else if (imm == BPF_OR)
  658. emit(A64_ORR(isdw, tmp2, tmp2, src), ctx);
  659. else
  660. emit(A64_EOR(isdw, tmp2, tmp2, src), ctx);
  661. emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
  662. jmp_offset = -3;
  663. check_imm19(jmp_offset);
  664. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  665. } else if (imm == (BPF_ADD | BPF_FETCH) ||
  666. imm == (BPF_AND | BPF_FETCH) ||
  667. imm == (BPF_OR | BPF_FETCH) ||
  668. imm == (BPF_XOR | BPF_FETCH)) {
  669. /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
  670. const u8 ax = bpf2a64[BPF_REG_AX];
  671. emit(A64_MOV(isdw, ax, src), ctx);
  672. emit(A64_LDXR(isdw, src, reg), ctx);
  673. if (imm == (BPF_ADD | BPF_FETCH))
  674. emit(A64_ADD(isdw, tmp2, src, ax), ctx);
  675. else if (imm == (BPF_AND | BPF_FETCH))
  676. emit(A64_AND(isdw, tmp2, src, ax), ctx);
  677. else if (imm == (BPF_OR | BPF_FETCH))
  678. emit(A64_ORR(isdw, tmp2, src, ax), ctx);
  679. else
  680. emit(A64_EOR(isdw, tmp2, src, ax), ctx);
  681. emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
  682. jmp_offset = -3;
  683. check_imm19(jmp_offset);
  684. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  685. emit(A64_DMB_ISH, ctx);
  686. } else if (imm == BPF_XCHG) {
  687. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  688. emit(A64_MOV(isdw, tmp2, src), ctx);
  689. emit(A64_LDXR(isdw, src, reg), ctx);
  690. emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
  691. jmp_offset = -2;
  692. check_imm19(jmp_offset);
  693. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  694. emit(A64_DMB_ISH, ctx);
  695. } else if (imm == BPF_CMPXCHG) {
  696. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  697. const u8 r0 = bpf2a64[BPF_REG_0];
  698. emit(A64_MOV(isdw, tmp2, r0), ctx);
  699. emit(A64_LDXR(isdw, r0, reg), ctx);
  700. emit(A64_EOR(isdw, tmp3, r0, tmp2), ctx);
  701. jmp_offset = 4;
  702. check_imm19(jmp_offset);
  703. emit(A64_CBNZ(isdw, tmp3, jmp_offset), ctx);
  704. emit(A64_STLXR(isdw, src, reg, tmp3), ctx);
  705. jmp_offset = -4;
  706. check_imm19(jmp_offset);
  707. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  708. emit(A64_DMB_ISH, ctx);
  709. } else {
  710. pr_err_once("unknown atomic op code %02x\n", imm);
  711. return -EINVAL;
  712. }
  713. return 0;
  714. }
  715. void dummy_tramp(void);
  716. asm (
  717. " .pushsection .text, \"ax\", @progbits\n"
  718. " .global dummy_tramp\n"
  719. " .type dummy_tramp, %function\n"
  720. "dummy_tramp:"
  721. #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
  722. " bti j\n" /* dummy_tramp is called via "br x10" */
  723. #endif
  724. " mov x10, x30\n"
  725. " mov x30, x9\n"
  726. " ret x10\n"
  727. " .size dummy_tramp, .-dummy_tramp\n"
  728. " .popsection\n"
  729. );
  730. /* build a plt initialized like this:
  731. *
  732. * plt:
  733. * ldr tmp, target
  734. * br tmp
  735. * target:
  736. * .quad dummy_tramp
  737. *
  738. * when a long jump trampoline is attached, target is filled with the
  739. * trampoline address, and when the trampoline is removed, target is
  740. * restored to dummy_tramp address.
  741. */
  742. static void build_plt(struct jit_ctx *ctx)
  743. {
  744. const u8 tmp = bpf2a64[TMP_REG_1];
  745. struct bpf_plt *plt = NULL;
  746. /* make sure target is 64-bit aligned */
  747. if ((ctx->idx + PLT_TARGET_OFFSET / AARCH64_INSN_SIZE) % 2)
  748. emit(A64_NOP, ctx);
  749. plt = (struct bpf_plt *)(ctx->image + ctx->idx);
  750. /* plt is called via bl, no BTI needed here */
  751. emit(A64_LDR64LIT(tmp, 2 * AARCH64_INSN_SIZE), ctx);
  752. emit(A64_BR(tmp), ctx);
  753. if (ctx->image)
  754. plt->target = (u64)&dummy_tramp;
  755. }
  756. /* Clobbers BPF registers 1-4, aka x0-x3 */
  757. static void __maybe_unused build_bhb_mitigation(struct jit_ctx *ctx)
  758. {
  759. const u8 r1 = bpf2a64[BPF_REG_1]; /* aka x0 */
  760. u8 k = get_spectre_bhb_loop_value();
  761. if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY) ||
  762. cpu_mitigations_off() || __nospectre_bhb ||
  763. arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE)
  764. return;
  765. if (capable(CAP_SYS_ADMIN))
  766. return;
  767. if (supports_clearbhb(SCOPE_SYSTEM)) {
  768. emit(aarch64_insn_gen_hint(AARCH64_INSN_HINT_CLEARBHB), ctx);
  769. return;
  770. }
  771. if (k) {
  772. emit_a64_mov_i64(r1, k, ctx);
  773. emit(A64_B(1), ctx);
  774. emit(A64_SUBS_I(true, r1, r1, 1), ctx);
  775. emit(A64_B_(A64_COND_NE, -2), ctx);
  776. emit(aarch64_insn_gen_dsb(AARCH64_INSN_MB_ISH), ctx);
  777. emit(aarch64_insn_get_isb_value(), ctx);
  778. }
  779. if (is_spectre_bhb_fw_mitigated()) {
  780. emit(A64_ORR_I(false, r1, AARCH64_INSN_REG_ZR,
  781. ARM_SMCCC_ARCH_WORKAROUND_3), ctx);
  782. switch (arm_smccc_1_1_get_conduit()) {
  783. case SMCCC_CONDUIT_HVC:
  784. emit(aarch64_insn_get_hvc_value(), ctx);
  785. break;
  786. case SMCCC_CONDUIT_SMC:
  787. emit(aarch64_insn_get_smc_value(), ctx);
  788. break;
  789. default:
  790. pr_err_once("Firmware mitigation enabled with unknown conduit\n");
  791. }
  792. }
  793. }
  794. static void build_epilogue(struct jit_ctx *ctx, bool was_classic)
  795. {
  796. const u8 r0 = bpf2a64[BPF_REG_0];
  797. const u8 ptr = bpf2a64[TCCNT_PTR];
  798. /* We're done with BPF stack */
  799. if (ctx->stack_size)
  800. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  801. pop_callee_regs(ctx);
  802. emit(A64_POP(A64_ZR, ptr, A64_SP), ctx);
  803. if (was_classic)
  804. build_bhb_mitigation(ctx);
  805. /* Restore FP/LR registers */
  806. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  807. /* Move the return value from bpf:r0 (aka x7) to x0 */
  808. emit(A64_MOV(1, A64_R(0), r0), ctx);
  809. /* Authenticate lr */
  810. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  811. emit(A64_AUTIASP, ctx);
  812. emit(A64_RET(A64_LR), ctx);
  813. }
  814. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  815. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  816. #define DONT_CLEAR 5 /* Unused ARM64 register from BPF's POV */
  817. bool ex_handler_bpf(const struct exception_table_entry *ex,
  818. struct pt_regs *regs)
  819. {
  820. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  821. int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  822. if (dst_reg != DONT_CLEAR)
  823. regs->regs[dst_reg] = 0;
  824. regs->pc = (unsigned long)&ex->fixup - offset;
  825. return true;
  826. }
  827. /* For accesses to BTF pointers, add an entry to the exception table */
  828. static int add_exception_handler(const struct bpf_insn *insn,
  829. struct jit_ctx *ctx,
  830. int dst_reg)
  831. {
  832. off_t ins_offset;
  833. off_t fixup_offset;
  834. unsigned long pc;
  835. struct exception_table_entry *ex;
  836. if (!ctx->image)
  837. /* First pass */
  838. return 0;
  839. if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
  840. BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
  841. BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
  842. BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
  843. return 0;
  844. if (!ctx->prog->aux->extable ||
  845. WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
  846. return -EINVAL;
  847. ex = &ctx->prog->aux->extable[ctx->exentry_idx];
  848. pc = (unsigned long)&ctx->ro_image[ctx->idx - 1];
  849. /*
  850. * This is the relative offset of the instruction that may fault from
  851. * the exception table itself. This will be written to the exception
  852. * table and if this instruction faults, the destination register will
  853. * be set to '0' and the execution will jump to the next instruction.
  854. */
  855. ins_offset = pc - (long)&ex->insn;
  856. if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN))
  857. return -ERANGE;
  858. /*
  859. * Since the extable follows the program, the fixup offset is always
  860. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  861. * to keep things simple, and put the destination register in the upper
  862. * bits. We don't need to worry about buildtime or runtime sort
  863. * modifying the upper bits because the table is already sorted, and
  864. * isn't part of the main exception table.
  865. *
  866. * The fixup_offset is set to the next instruction from the instruction
  867. * that may fault. The execution will jump to this after handling the
  868. * fault.
  869. */
  870. fixup_offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
  871. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, fixup_offset))
  872. return -ERANGE;
  873. /*
  874. * The offsets above have been calculated using the RO buffer but we
  875. * need to use the R/W buffer for writes.
  876. * switch ex to rw buffer for writing.
  877. */
  878. ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image);
  879. ex->insn = ins_offset;
  880. if (BPF_CLASS(insn->code) != BPF_LDX)
  881. dst_reg = DONT_CLEAR;
  882. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, fixup_offset) |
  883. FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  884. ex->type = EX_TYPE_BPF;
  885. ctx->exentry_idx++;
  886. return 0;
  887. }
  888. /* JITs an eBPF instruction.
  889. * Returns:
  890. * 0 - successfully JITed an 8-byte eBPF instruction.
  891. * >0 - successfully JITed a 16-byte eBPF instruction.
  892. * <0 - failed to JIT.
  893. */
  894. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  895. bool extra_pass)
  896. {
  897. const u8 code = insn->code;
  898. u8 dst = bpf2a64[insn->dst_reg];
  899. u8 src = bpf2a64[insn->src_reg];
  900. const u8 tmp = bpf2a64[TMP_REG_1];
  901. const u8 tmp2 = bpf2a64[TMP_REG_2];
  902. const u8 fp = bpf2a64[BPF_REG_FP];
  903. const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
  904. const s16 off = insn->off;
  905. const s32 imm = insn->imm;
  906. const int i = insn - ctx->prog->insnsi;
  907. const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
  908. BPF_CLASS(code) == BPF_JMP;
  909. u8 jmp_cond;
  910. s32 jmp_offset;
  911. u32 a64_insn;
  912. u8 src_adj;
  913. u8 dst_adj;
  914. int off_adj;
  915. int ret;
  916. bool sign_extend;
  917. switch (code) {
  918. /* dst = src */
  919. case BPF_ALU | BPF_MOV | BPF_X:
  920. case BPF_ALU64 | BPF_MOV | BPF_X:
  921. if (insn_is_cast_user(insn)) {
  922. emit(A64_MOV(0, tmp, src), ctx); // 32-bit mov clears the upper 32 bits
  923. emit_a64_mov_i(0, dst, ctx->user_vm_start >> 32, ctx);
  924. emit(A64_LSL(1, dst, dst, 32), ctx);
  925. emit(A64_CBZ(1, tmp, 2), ctx);
  926. emit(A64_ORR(1, tmp, dst, tmp), ctx);
  927. emit(A64_MOV(1, dst, tmp), ctx);
  928. break;
  929. } else if (insn_is_mov_percpu_addr(insn)) {
  930. if (dst != src)
  931. emit(A64_MOV(1, dst, src), ctx);
  932. if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
  933. emit(A64_MRS_TPIDR_EL2(tmp), ctx);
  934. else
  935. emit(A64_MRS_TPIDR_EL1(tmp), ctx);
  936. emit(A64_ADD(1, dst, dst, tmp), ctx);
  937. break;
  938. }
  939. switch (insn->off) {
  940. case 0:
  941. emit(A64_MOV(is64, dst, src), ctx);
  942. break;
  943. case 8:
  944. emit(A64_SXTB(is64, dst, src), ctx);
  945. break;
  946. case 16:
  947. emit(A64_SXTH(is64, dst, src), ctx);
  948. break;
  949. case 32:
  950. emit(A64_SXTW(is64, dst, src), ctx);
  951. break;
  952. }
  953. break;
  954. /* dst = dst OP src */
  955. case BPF_ALU | BPF_ADD | BPF_X:
  956. case BPF_ALU64 | BPF_ADD | BPF_X:
  957. emit(A64_ADD(is64, dst, dst, src), ctx);
  958. break;
  959. case BPF_ALU | BPF_SUB | BPF_X:
  960. case BPF_ALU64 | BPF_SUB | BPF_X:
  961. emit(A64_SUB(is64, dst, dst, src), ctx);
  962. break;
  963. case BPF_ALU | BPF_AND | BPF_X:
  964. case BPF_ALU64 | BPF_AND | BPF_X:
  965. emit(A64_AND(is64, dst, dst, src), ctx);
  966. break;
  967. case BPF_ALU | BPF_OR | BPF_X:
  968. case BPF_ALU64 | BPF_OR | BPF_X:
  969. emit(A64_ORR(is64, dst, dst, src), ctx);
  970. break;
  971. case BPF_ALU | BPF_XOR | BPF_X:
  972. case BPF_ALU64 | BPF_XOR | BPF_X:
  973. emit(A64_EOR(is64, dst, dst, src), ctx);
  974. break;
  975. case BPF_ALU | BPF_MUL | BPF_X:
  976. case BPF_ALU64 | BPF_MUL | BPF_X:
  977. emit(A64_MUL(is64, dst, dst, src), ctx);
  978. break;
  979. case BPF_ALU | BPF_DIV | BPF_X:
  980. case BPF_ALU64 | BPF_DIV | BPF_X:
  981. if (!off)
  982. emit(A64_UDIV(is64, dst, dst, src), ctx);
  983. else
  984. emit(A64_SDIV(is64, dst, dst, src), ctx);
  985. break;
  986. case BPF_ALU | BPF_MOD | BPF_X:
  987. case BPF_ALU64 | BPF_MOD | BPF_X:
  988. if (!off)
  989. emit(A64_UDIV(is64, tmp, dst, src), ctx);
  990. else
  991. emit(A64_SDIV(is64, tmp, dst, src), ctx);
  992. emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
  993. break;
  994. case BPF_ALU | BPF_LSH | BPF_X:
  995. case BPF_ALU64 | BPF_LSH | BPF_X:
  996. emit(A64_LSLV(is64, dst, dst, src), ctx);
  997. break;
  998. case BPF_ALU | BPF_RSH | BPF_X:
  999. case BPF_ALU64 | BPF_RSH | BPF_X:
  1000. emit(A64_LSRV(is64, dst, dst, src), ctx);
  1001. break;
  1002. case BPF_ALU | BPF_ARSH | BPF_X:
  1003. case BPF_ALU64 | BPF_ARSH | BPF_X:
  1004. emit(A64_ASRV(is64, dst, dst, src), ctx);
  1005. break;
  1006. /* dst = -dst */
  1007. case BPF_ALU | BPF_NEG:
  1008. case BPF_ALU64 | BPF_NEG:
  1009. emit(A64_NEG(is64, dst, dst), ctx);
  1010. break;
  1011. /* dst = BSWAP##imm(dst) */
  1012. case BPF_ALU | BPF_END | BPF_FROM_LE:
  1013. case BPF_ALU | BPF_END | BPF_FROM_BE:
  1014. case BPF_ALU64 | BPF_END | BPF_FROM_LE:
  1015. #ifdef CONFIG_CPU_BIG_ENDIAN
  1016. if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_BE)
  1017. goto emit_bswap_uxt;
  1018. #else /* !CONFIG_CPU_BIG_ENDIAN */
  1019. if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_LE)
  1020. goto emit_bswap_uxt;
  1021. #endif
  1022. switch (imm) {
  1023. case 16:
  1024. emit(A64_REV16(is64, dst, dst), ctx);
  1025. /* zero-extend 16 bits into 64 bits */
  1026. emit(A64_UXTH(is64, dst, dst), ctx);
  1027. break;
  1028. case 32:
  1029. emit(A64_REV32(0, dst, dst), ctx);
  1030. /* upper 32 bits already cleared */
  1031. break;
  1032. case 64:
  1033. emit(A64_REV64(dst, dst), ctx);
  1034. break;
  1035. }
  1036. break;
  1037. emit_bswap_uxt:
  1038. switch (imm) {
  1039. case 16:
  1040. /* zero-extend 16 bits into 64 bits */
  1041. emit(A64_UXTH(is64, dst, dst), ctx);
  1042. break;
  1043. case 32:
  1044. /* zero-extend 32 bits into 64 bits */
  1045. emit(A64_UXTW(is64, dst, dst), ctx);
  1046. break;
  1047. case 64:
  1048. /* nop */
  1049. break;
  1050. }
  1051. break;
  1052. /* dst = imm */
  1053. case BPF_ALU | BPF_MOV | BPF_K:
  1054. case BPF_ALU64 | BPF_MOV | BPF_K:
  1055. emit_a64_mov_i(is64, dst, imm, ctx);
  1056. break;
  1057. /* dst = dst OP imm */
  1058. case BPF_ALU | BPF_ADD | BPF_K:
  1059. case BPF_ALU64 | BPF_ADD | BPF_K:
  1060. if (is_addsub_imm(imm)) {
  1061. emit(A64_ADD_I(is64, dst, dst, imm), ctx);
  1062. } else if (is_addsub_imm(-imm)) {
  1063. emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
  1064. } else {
  1065. emit_a64_mov_i(is64, tmp, imm, ctx);
  1066. emit(A64_ADD(is64, dst, dst, tmp), ctx);
  1067. }
  1068. break;
  1069. case BPF_ALU | BPF_SUB | BPF_K:
  1070. case BPF_ALU64 | BPF_SUB | BPF_K:
  1071. if (is_addsub_imm(imm)) {
  1072. emit(A64_SUB_I(is64, dst, dst, imm), ctx);
  1073. } else if (is_addsub_imm(-imm)) {
  1074. emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
  1075. } else {
  1076. emit_a64_mov_i(is64, tmp, imm, ctx);
  1077. emit(A64_SUB(is64, dst, dst, tmp), ctx);
  1078. }
  1079. break;
  1080. case BPF_ALU | BPF_AND | BPF_K:
  1081. case BPF_ALU64 | BPF_AND | BPF_K:
  1082. a64_insn = A64_AND_I(is64, dst, dst, imm);
  1083. if (a64_insn != AARCH64_BREAK_FAULT) {
  1084. emit(a64_insn, ctx);
  1085. } else {
  1086. emit_a64_mov_i(is64, tmp, imm, ctx);
  1087. emit(A64_AND(is64, dst, dst, tmp), ctx);
  1088. }
  1089. break;
  1090. case BPF_ALU | BPF_OR | BPF_K:
  1091. case BPF_ALU64 | BPF_OR | BPF_K:
  1092. a64_insn = A64_ORR_I(is64, dst, dst, imm);
  1093. if (a64_insn != AARCH64_BREAK_FAULT) {
  1094. emit(a64_insn, ctx);
  1095. } else {
  1096. emit_a64_mov_i(is64, tmp, imm, ctx);
  1097. emit(A64_ORR(is64, dst, dst, tmp), ctx);
  1098. }
  1099. break;
  1100. case BPF_ALU | BPF_XOR | BPF_K:
  1101. case BPF_ALU64 | BPF_XOR | BPF_K:
  1102. a64_insn = A64_EOR_I(is64, dst, dst, imm);
  1103. if (a64_insn != AARCH64_BREAK_FAULT) {
  1104. emit(a64_insn, ctx);
  1105. } else {
  1106. emit_a64_mov_i(is64, tmp, imm, ctx);
  1107. emit(A64_EOR(is64, dst, dst, tmp), ctx);
  1108. }
  1109. break;
  1110. case BPF_ALU | BPF_MUL | BPF_K:
  1111. case BPF_ALU64 | BPF_MUL | BPF_K:
  1112. emit_a64_mov_i(is64, tmp, imm, ctx);
  1113. emit(A64_MUL(is64, dst, dst, tmp), ctx);
  1114. break;
  1115. case BPF_ALU | BPF_DIV | BPF_K:
  1116. case BPF_ALU64 | BPF_DIV | BPF_K:
  1117. emit_a64_mov_i(is64, tmp, imm, ctx);
  1118. if (!off)
  1119. emit(A64_UDIV(is64, dst, dst, tmp), ctx);
  1120. else
  1121. emit(A64_SDIV(is64, dst, dst, tmp), ctx);
  1122. break;
  1123. case BPF_ALU | BPF_MOD | BPF_K:
  1124. case BPF_ALU64 | BPF_MOD | BPF_K:
  1125. emit_a64_mov_i(is64, tmp2, imm, ctx);
  1126. if (!off)
  1127. emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
  1128. else
  1129. emit(A64_SDIV(is64, tmp, dst, tmp2), ctx);
  1130. emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
  1131. break;
  1132. case BPF_ALU | BPF_LSH | BPF_K:
  1133. case BPF_ALU64 | BPF_LSH | BPF_K:
  1134. emit(A64_LSL(is64, dst, dst, imm), ctx);
  1135. break;
  1136. case BPF_ALU | BPF_RSH | BPF_K:
  1137. case BPF_ALU64 | BPF_RSH | BPF_K:
  1138. emit(A64_LSR(is64, dst, dst, imm), ctx);
  1139. break;
  1140. case BPF_ALU | BPF_ARSH | BPF_K:
  1141. case BPF_ALU64 | BPF_ARSH | BPF_K:
  1142. emit(A64_ASR(is64, dst, dst, imm), ctx);
  1143. break;
  1144. /* JUMP off */
  1145. case BPF_JMP | BPF_JA:
  1146. case BPF_JMP32 | BPF_JA:
  1147. if (BPF_CLASS(code) == BPF_JMP)
  1148. jmp_offset = bpf2a64_offset(i, off, ctx);
  1149. else
  1150. jmp_offset = bpf2a64_offset(i, imm, ctx);
  1151. check_imm26(jmp_offset);
  1152. emit(A64_B(jmp_offset), ctx);
  1153. break;
  1154. /* IF (dst COND src) JUMP off */
  1155. case BPF_JMP | BPF_JEQ | BPF_X:
  1156. case BPF_JMP | BPF_JGT | BPF_X:
  1157. case BPF_JMP | BPF_JLT | BPF_X:
  1158. case BPF_JMP | BPF_JGE | BPF_X:
  1159. case BPF_JMP | BPF_JLE | BPF_X:
  1160. case BPF_JMP | BPF_JNE | BPF_X:
  1161. case BPF_JMP | BPF_JSGT | BPF_X:
  1162. case BPF_JMP | BPF_JSLT | BPF_X:
  1163. case BPF_JMP | BPF_JSGE | BPF_X:
  1164. case BPF_JMP | BPF_JSLE | BPF_X:
  1165. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1166. case BPF_JMP32 | BPF_JGT | BPF_X:
  1167. case BPF_JMP32 | BPF_JLT | BPF_X:
  1168. case BPF_JMP32 | BPF_JGE | BPF_X:
  1169. case BPF_JMP32 | BPF_JLE | BPF_X:
  1170. case BPF_JMP32 | BPF_JNE | BPF_X:
  1171. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1172. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1173. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1174. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1175. emit(A64_CMP(is64, dst, src), ctx);
  1176. emit_cond_jmp:
  1177. jmp_offset = bpf2a64_offset(i, off, ctx);
  1178. check_imm19(jmp_offset);
  1179. switch (BPF_OP(code)) {
  1180. case BPF_JEQ:
  1181. jmp_cond = A64_COND_EQ;
  1182. break;
  1183. case BPF_JGT:
  1184. jmp_cond = A64_COND_HI;
  1185. break;
  1186. case BPF_JLT:
  1187. jmp_cond = A64_COND_CC;
  1188. break;
  1189. case BPF_JGE:
  1190. jmp_cond = A64_COND_CS;
  1191. break;
  1192. case BPF_JLE:
  1193. jmp_cond = A64_COND_LS;
  1194. break;
  1195. case BPF_JSET:
  1196. case BPF_JNE:
  1197. jmp_cond = A64_COND_NE;
  1198. break;
  1199. case BPF_JSGT:
  1200. jmp_cond = A64_COND_GT;
  1201. break;
  1202. case BPF_JSLT:
  1203. jmp_cond = A64_COND_LT;
  1204. break;
  1205. case BPF_JSGE:
  1206. jmp_cond = A64_COND_GE;
  1207. break;
  1208. case BPF_JSLE:
  1209. jmp_cond = A64_COND_LE;
  1210. break;
  1211. default:
  1212. return -EFAULT;
  1213. }
  1214. emit(A64_B_(jmp_cond, jmp_offset), ctx);
  1215. break;
  1216. case BPF_JMP | BPF_JSET | BPF_X:
  1217. case BPF_JMP32 | BPF_JSET | BPF_X:
  1218. emit(A64_TST(is64, dst, src), ctx);
  1219. goto emit_cond_jmp;
  1220. /* IF (dst COND imm) JUMP off */
  1221. case BPF_JMP | BPF_JEQ | BPF_K:
  1222. case BPF_JMP | BPF_JGT | BPF_K:
  1223. case BPF_JMP | BPF_JLT | BPF_K:
  1224. case BPF_JMP | BPF_JGE | BPF_K:
  1225. case BPF_JMP | BPF_JLE | BPF_K:
  1226. case BPF_JMP | BPF_JNE | BPF_K:
  1227. case BPF_JMP | BPF_JSGT | BPF_K:
  1228. case BPF_JMP | BPF_JSLT | BPF_K:
  1229. case BPF_JMP | BPF_JSGE | BPF_K:
  1230. case BPF_JMP | BPF_JSLE | BPF_K:
  1231. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1232. case BPF_JMP32 | BPF_JGT | BPF_K:
  1233. case BPF_JMP32 | BPF_JLT | BPF_K:
  1234. case BPF_JMP32 | BPF_JGE | BPF_K:
  1235. case BPF_JMP32 | BPF_JLE | BPF_K:
  1236. case BPF_JMP32 | BPF_JNE | BPF_K:
  1237. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1238. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1239. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1240. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1241. if (is_addsub_imm(imm)) {
  1242. emit(A64_CMP_I(is64, dst, imm), ctx);
  1243. } else if (is_addsub_imm(-imm)) {
  1244. emit(A64_CMN_I(is64, dst, -imm), ctx);
  1245. } else {
  1246. emit_a64_mov_i(is64, tmp, imm, ctx);
  1247. emit(A64_CMP(is64, dst, tmp), ctx);
  1248. }
  1249. goto emit_cond_jmp;
  1250. case BPF_JMP | BPF_JSET | BPF_K:
  1251. case BPF_JMP32 | BPF_JSET | BPF_K:
  1252. a64_insn = A64_TST_I(is64, dst, imm);
  1253. if (a64_insn != AARCH64_BREAK_FAULT) {
  1254. emit(a64_insn, ctx);
  1255. } else {
  1256. emit_a64_mov_i(is64, tmp, imm, ctx);
  1257. emit(A64_TST(is64, dst, tmp), ctx);
  1258. }
  1259. goto emit_cond_jmp;
  1260. /* function call */
  1261. case BPF_JMP | BPF_CALL:
  1262. {
  1263. const u8 r0 = bpf2a64[BPF_REG_0];
  1264. bool func_addr_fixed;
  1265. u64 func_addr;
  1266. u32 cpu_offset;
  1267. /* Implement helper call to bpf_get_smp_processor_id() inline */
  1268. if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
  1269. cpu_offset = offsetof(struct thread_info, cpu);
  1270. emit(A64_MRS_SP_EL0(tmp), ctx);
  1271. if (is_lsi_offset(cpu_offset, 2)) {
  1272. emit(A64_LDR32I(r0, tmp, cpu_offset), ctx);
  1273. } else {
  1274. emit_a64_mov_i(1, tmp2, cpu_offset, ctx);
  1275. emit(A64_LDR32(r0, tmp, tmp2), ctx);
  1276. }
  1277. break;
  1278. }
  1279. /* Implement helper call to bpf_get_current_task/_btf() inline */
  1280. if (insn->src_reg == 0 && (insn->imm == BPF_FUNC_get_current_task ||
  1281. insn->imm == BPF_FUNC_get_current_task_btf)) {
  1282. emit(A64_MRS_SP_EL0(r0), ctx);
  1283. break;
  1284. }
  1285. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  1286. &func_addr, &func_addr_fixed);
  1287. if (ret < 0)
  1288. return ret;
  1289. emit_call(func_addr, ctx);
  1290. emit(A64_MOV(1, r0, A64_R(0)), ctx);
  1291. break;
  1292. }
  1293. /* tail call */
  1294. case BPF_JMP | BPF_TAIL_CALL:
  1295. if (emit_bpf_tail_call(ctx))
  1296. return -EFAULT;
  1297. break;
  1298. /* function return */
  1299. case BPF_JMP | BPF_EXIT:
  1300. /* Optimization: when last instruction is EXIT,
  1301. simply fallthrough to epilogue. */
  1302. if (i == ctx->prog->len - 1)
  1303. break;
  1304. jmp_offset = epilogue_offset(ctx);
  1305. check_imm26(jmp_offset);
  1306. emit(A64_B(jmp_offset), ctx);
  1307. break;
  1308. /* dst = imm64 */
  1309. case BPF_LD | BPF_IMM | BPF_DW:
  1310. {
  1311. const struct bpf_insn insn1 = insn[1];
  1312. u64 imm64;
  1313. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  1314. if (bpf_pseudo_func(insn))
  1315. emit_addr_mov_i64(dst, imm64, ctx);
  1316. else
  1317. emit_a64_mov_i64(dst, imm64, ctx);
  1318. return 1;
  1319. }
  1320. /* LDX: dst = (u64)*(unsigned size *)(src + off) */
  1321. case BPF_LDX | BPF_MEM | BPF_W:
  1322. case BPF_LDX | BPF_MEM | BPF_H:
  1323. case BPF_LDX | BPF_MEM | BPF_B:
  1324. case BPF_LDX | BPF_MEM | BPF_DW:
  1325. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  1326. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  1327. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  1328. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  1329. /* LDXS: dst_reg = (s64)*(signed size *)(src_reg + off) */
  1330. case BPF_LDX | BPF_MEMSX | BPF_B:
  1331. case BPF_LDX | BPF_MEMSX | BPF_H:
  1332. case BPF_LDX | BPF_MEMSX | BPF_W:
  1333. case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
  1334. case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
  1335. case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
  1336. case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
  1337. case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
  1338. case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
  1339. case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
  1340. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1341. emit(A64_ADD(1, tmp2, src, arena_vm_base), ctx);
  1342. src = tmp2;
  1343. }
  1344. if (src == fp) {
  1345. src_adj = A64_SP;
  1346. off_adj = off + ctx->stack_size;
  1347. } else {
  1348. src_adj = src;
  1349. off_adj = off;
  1350. }
  1351. sign_extend = (BPF_MODE(insn->code) == BPF_MEMSX ||
  1352. BPF_MODE(insn->code) == BPF_PROBE_MEMSX);
  1353. switch (BPF_SIZE(code)) {
  1354. case BPF_W:
  1355. if (is_lsi_offset(off_adj, 2)) {
  1356. if (sign_extend)
  1357. emit(A64_LDRSWI(dst, src_adj, off_adj), ctx);
  1358. else
  1359. emit(A64_LDR32I(dst, src_adj, off_adj), ctx);
  1360. } else {
  1361. emit_a64_mov_i(1, tmp, off, ctx);
  1362. if (sign_extend)
  1363. emit(A64_LDRSW(dst, src, tmp), ctx);
  1364. else
  1365. emit(A64_LDR32(dst, src, tmp), ctx);
  1366. }
  1367. break;
  1368. case BPF_H:
  1369. if (is_lsi_offset(off_adj, 1)) {
  1370. if (sign_extend)
  1371. emit(A64_LDRSHI(dst, src_adj, off_adj), ctx);
  1372. else
  1373. emit(A64_LDRHI(dst, src_adj, off_adj), ctx);
  1374. } else {
  1375. emit_a64_mov_i(1, tmp, off, ctx);
  1376. if (sign_extend)
  1377. emit(A64_LDRSH(dst, src, tmp), ctx);
  1378. else
  1379. emit(A64_LDRH(dst, src, tmp), ctx);
  1380. }
  1381. break;
  1382. case BPF_B:
  1383. if (is_lsi_offset(off_adj, 0)) {
  1384. if (sign_extend)
  1385. emit(A64_LDRSBI(dst, src_adj, off_adj), ctx);
  1386. else
  1387. emit(A64_LDRBI(dst, src_adj, off_adj), ctx);
  1388. } else {
  1389. emit_a64_mov_i(1, tmp, off, ctx);
  1390. if (sign_extend)
  1391. emit(A64_LDRSB(dst, src, tmp), ctx);
  1392. else
  1393. emit(A64_LDRB(dst, src, tmp), ctx);
  1394. }
  1395. break;
  1396. case BPF_DW:
  1397. if (is_lsi_offset(off_adj, 3)) {
  1398. emit(A64_LDR64I(dst, src_adj, off_adj), ctx);
  1399. } else {
  1400. emit_a64_mov_i(1, tmp, off, ctx);
  1401. emit(A64_LDR64(dst, src, tmp), ctx);
  1402. }
  1403. break;
  1404. }
  1405. ret = add_exception_handler(insn, ctx, dst);
  1406. if (ret)
  1407. return ret;
  1408. break;
  1409. /* speculation barrier */
  1410. case BPF_ST | BPF_NOSPEC:
  1411. /*
  1412. * Nothing required here.
  1413. *
  1414. * In case of arm64, we rely on the firmware mitigation of
  1415. * Speculative Store Bypass as controlled via the ssbd kernel
  1416. * parameter. Whenever the mitigation is enabled, it works
  1417. * for all of the kernel code with no need to provide any
  1418. * additional instructions.
  1419. */
  1420. break;
  1421. /* ST: *(size *)(dst + off) = imm */
  1422. case BPF_ST | BPF_MEM | BPF_W:
  1423. case BPF_ST | BPF_MEM | BPF_H:
  1424. case BPF_ST | BPF_MEM | BPF_B:
  1425. case BPF_ST | BPF_MEM | BPF_DW:
  1426. case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
  1427. case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
  1428. case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
  1429. case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
  1430. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1431. emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx);
  1432. dst = tmp2;
  1433. }
  1434. if (dst == fp) {
  1435. dst_adj = A64_SP;
  1436. off_adj = off + ctx->stack_size;
  1437. } else {
  1438. dst_adj = dst;
  1439. off_adj = off;
  1440. }
  1441. /* Load imm to a register then store it */
  1442. emit_a64_mov_i(1, tmp, imm, ctx);
  1443. switch (BPF_SIZE(code)) {
  1444. case BPF_W:
  1445. if (is_lsi_offset(off_adj, 2)) {
  1446. emit(A64_STR32I(tmp, dst_adj, off_adj), ctx);
  1447. } else {
  1448. emit_a64_mov_i(1, tmp2, off, ctx);
  1449. emit(A64_STR32(tmp, dst, tmp2), ctx);
  1450. }
  1451. break;
  1452. case BPF_H:
  1453. if (is_lsi_offset(off_adj, 1)) {
  1454. emit(A64_STRHI(tmp, dst_adj, off_adj), ctx);
  1455. } else {
  1456. emit_a64_mov_i(1, tmp2, off, ctx);
  1457. emit(A64_STRH(tmp, dst, tmp2), ctx);
  1458. }
  1459. break;
  1460. case BPF_B:
  1461. if (is_lsi_offset(off_adj, 0)) {
  1462. emit(A64_STRBI(tmp, dst_adj, off_adj), ctx);
  1463. } else {
  1464. emit_a64_mov_i(1, tmp2, off, ctx);
  1465. emit(A64_STRB(tmp, dst, tmp2), ctx);
  1466. }
  1467. break;
  1468. case BPF_DW:
  1469. if (is_lsi_offset(off_adj, 3)) {
  1470. emit(A64_STR64I(tmp, dst_adj, off_adj), ctx);
  1471. } else {
  1472. emit_a64_mov_i(1, tmp2, off, ctx);
  1473. emit(A64_STR64(tmp, dst, tmp2), ctx);
  1474. }
  1475. break;
  1476. }
  1477. ret = add_exception_handler(insn, ctx, dst);
  1478. if (ret)
  1479. return ret;
  1480. break;
  1481. /* STX: *(size *)(dst + off) = src */
  1482. case BPF_STX | BPF_MEM | BPF_W:
  1483. case BPF_STX | BPF_MEM | BPF_H:
  1484. case BPF_STX | BPF_MEM | BPF_B:
  1485. case BPF_STX | BPF_MEM | BPF_DW:
  1486. case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
  1487. case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
  1488. case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
  1489. case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
  1490. if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
  1491. emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx);
  1492. dst = tmp2;
  1493. }
  1494. if (dst == fp) {
  1495. dst_adj = A64_SP;
  1496. off_adj = off + ctx->stack_size;
  1497. } else {
  1498. dst_adj = dst;
  1499. off_adj = off;
  1500. }
  1501. switch (BPF_SIZE(code)) {
  1502. case BPF_W:
  1503. if (is_lsi_offset(off_adj, 2)) {
  1504. emit(A64_STR32I(src, dst_adj, off_adj), ctx);
  1505. } else {
  1506. emit_a64_mov_i(1, tmp, off, ctx);
  1507. emit(A64_STR32(src, dst, tmp), ctx);
  1508. }
  1509. break;
  1510. case BPF_H:
  1511. if (is_lsi_offset(off_adj, 1)) {
  1512. emit(A64_STRHI(src, dst_adj, off_adj), ctx);
  1513. } else {
  1514. emit_a64_mov_i(1, tmp, off, ctx);
  1515. emit(A64_STRH(src, dst, tmp), ctx);
  1516. }
  1517. break;
  1518. case BPF_B:
  1519. if (is_lsi_offset(off_adj, 0)) {
  1520. emit(A64_STRBI(src, dst_adj, off_adj), ctx);
  1521. } else {
  1522. emit_a64_mov_i(1, tmp, off, ctx);
  1523. emit(A64_STRB(src, dst, tmp), ctx);
  1524. }
  1525. break;
  1526. case BPF_DW:
  1527. if (is_lsi_offset(off_adj, 3)) {
  1528. emit(A64_STR64I(src, dst_adj, off_adj), ctx);
  1529. } else {
  1530. emit_a64_mov_i(1, tmp, off, ctx);
  1531. emit(A64_STR64(src, dst, tmp), ctx);
  1532. }
  1533. break;
  1534. }
  1535. ret = add_exception_handler(insn, ctx, dst);
  1536. if (ret)
  1537. return ret;
  1538. break;
  1539. case BPF_STX | BPF_ATOMIC | BPF_W:
  1540. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1541. case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
  1542. case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
  1543. if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
  1544. ret = emit_lse_atomic(insn, ctx);
  1545. else
  1546. ret = emit_ll_sc_atomic(insn, ctx);
  1547. if (ret)
  1548. return ret;
  1549. ret = add_exception_handler(insn, ctx, dst);
  1550. if (ret)
  1551. return ret;
  1552. break;
  1553. default:
  1554. pr_err_once("unknown opcode %02x\n", code);
  1555. return -EINVAL;
  1556. }
  1557. return 0;
  1558. }
  1559. static int build_body(struct jit_ctx *ctx, bool extra_pass)
  1560. {
  1561. const struct bpf_prog *prog = ctx->prog;
  1562. int i;
  1563. /*
  1564. * - offset[0] offset of the end of prologue,
  1565. * start of the 1st instruction.
  1566. * - offset[1] - offset of the end of 1st instruction,
  1567. * start of the 2nd instruction
  1568. * [....]
  1569. * - offset[3] - offset of the end of 3rd instruction,
  1570. * start of 4th instruction
  1571. */
  1572. for (i = 0; i < prog->len; i++) {
  1573. const struct bpf_insn *insn = &prog->insnsi[i];
  1574. int ret;
  1575. ctx->offset[i] = ctx->idx;
  1576. ret = build_insn(insn, ctx, extra_pass);
  1577. if (ret > 0) {
  1578. i++;
  1579. ctx->offset[i] = ctx->idx;
  1580. continue;
  1581. }
  1582. if (ret)
  1583. return ret;
  1584. }
  1585. /*
  1586. * offset is allocated with prog->len + 1 so fill in
  1587. * the last element with the offset after the last
  1588. * instruction (end of program)
  1589. */
  1590. ctx->offset[i] = ctx->idx;
  1591. return 0;
  1592. }
  1593. static int validate_code(struct jit_ctx *ctx)
  1594. {
  1595. int i;
  1596. for (i = 0; i < ctx->idx; i++) {
  1597. u32 a64_insn = le32_to_cpu(ctx->image[i]);
  1598. if (a64_insn == AARCH64_BREAK_FAULT)
  1599. return -1;
  1600. }
  1601. return 0;
  1602. }
  1603. static int validate_ctx(struct jit_ctx *ctx)
  1604. {
  1605. if (validate_code(ctx))
  1606. return -1;
  1607. if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
  1608. return -1;
  1609. return 0;
  1610. }
  1611. static inline void bpf_flush_icache(void *start, void *end)
  1612. {
  1613. flush_icache_range((unsigned long)start, (unsigned long)end);
  1614. }
  1615. struct arm64_jit_data {
  1616. struct bpf_binary_header *header;
  1617. u8 *ro_image;
  1618. struct bpf_binary_header *ro_header;
  1619. struct jit_ctx ctx;
  1620. };
  1621. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1622. {
  1623. int image_size, prog_size, extable_size, extable_align, extable_offset;
  1624. struct bpf_prog *tmp, *orig_prog = prog;
  1625. struct bpf_binary_header *header;
  1626. struct bpf_binary_header *ro_header;
  1627. struct arm64_jit_data *jit_data;
  1628. bool was_classic = bpf_prog_was_classic(prog);
  1629. bool tmp_blinded = false;
  1630. bool extra_pass = false;
  1631. struct jit_ctx ctx;
  1632. u8 *image_ptr;
  1633. u8 *ro_image_ptr;
  1634. int body_idx;
  1635. int exentry_idx;
  1636. if (!prog->jit_requested)
  1637. return orig_prog;
  1638. tmp = bpf_jit_blind_constants(prog);
  1639. /* If blinding was requested and we failed during blinding,
  1640. * we must fall back to the interpreter.
  1641. */
  1642. if (IS_ERR(tmp))
  1643. return orig_prog;
  1644. if (tmp != prog) {
  1645. tmp_blinded = true;
  1646. prog = tmp;
  1647. }
  1648. jit_data = prog->aux->jit_data;
  1649. if (!jit_data) {
  1650. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  1651. if (!jit_data) {
  1652. prog = orig_prog;
  1653. goto out;
  1654. }
  1655. prog->aux->jit_data = jit_data;
  1656. }
  1657. if (jit_data->ctx.offset) {
  1658. ctx = jit_data->ctx;
  1659. ro_image_ptr = jit_data->ro_image;
  1660. ro_header = jit_data->ro_header;
  1661. header = jit_data->header;
  1662. image_ptr = (void *)header + ((void *)ro_image_ptr
  1663. - (void *)ro_header);
  1664. extra_pass = true;
  1665. prog_size = sizeof(u32) * ctx.idx;
  1666. goto skip_init_ctx;
  1667. }
  1668. memset(&ctx, 0, sizeof(ctx));
  1669. ctx.prog = prog;
  1670. ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
  1671. if (ctx.offset == NULL) {
  1672. prog = orig_prog;
  1673. goto out_off;
  1674. }
  1675. ctx.user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena);
  1676. ctx.arena_vm_start = bpf_arena_get_kern_vm_start(prog->aux->arena);
  1677. /* Pass 1: Estimate the maximum image size.
  1678. *
  1679. * BPF line info needs ctx->offset[i] to be the offset of
  1680. * instruction[i] in jited image, so build prologue first.
  1681. */
  1682. if (build_prologue(&ctx, was_classic)) {
  1683. prog = orig_prog;
  1684. goto out_off;
  1685. }
  1686. if (build_body(&ctx, extra_pass)) {
  1687. prog = orig_prog;
  1688. goto out_off;
  1689. }
  1690. ctx.epilogue_offset = ctx.idx;
  1691. build_epilogue(&ctx, was_classic);
  1692. build_plt(&ctx);
  1693. extable_align = __alignof__(struct exception_table_entry);
  1694. extable_size = prog->aux->num_exentries *
  1695. sizeof(struct exception_table_entry);
  1696. /* Now we know the maximum image size. */
  1697. prog_size = sizeof(u32) * ctx.idx;
  1698. /* also allocate space for plt target */
  1699. extable_offset = round_up(prog_size + PLT_TARGET_SIZE, extable_align);
  1700. image_size = extable_offset + extable_size;
  1701. ro_header = bpf_jit_binary_pack_alloc(image_size, &ro_image_ptr,
  1702. sizeof(u32), &header, &image_ptr,
  1703. jit_fill_hole);
  1704. if (!ro_header) {
  1705. prog = orig_prog;
  1706. goto out_off;
  1707. }
  1708. /* Pass 2: Determine jited position and result for each instruction */
  1709. /*
  1710. * Use the image(RW) for writing the JITed instructions. But also save
  1711. * the ro_image(RX) for calculating the offsets in the image. The RW
  1712. * image will be later copied to the RX image from where the program
  1713. * will run. The bpf_jit_binary_pack_finalize() will do this copy in the
  1714. * final step.
  1715. */
  1716. ctx.image = (__le32 *)image_ptr;
  1717. ctx.ro_image = (__le32 *)ro_image_ptr;
  1718. if (extable_size)
  1719. prog->aux->extable = (void *)ro_image_ptr + extable_offset;
  1720. skip_init_ctx:
  1721. ctx.idx = 0;
  1722. ctx.exentry_idx = 0;
  1723. ctx.write = true;
  1724. build_prologue(&ctx, was_classic);
  1725. /* Record exentry_idx and body_idx before first build_body */
  1726. exentry_idx = ctx.exentry_idx;
  1727. body_idx = ctx.idx;
  1728. /* Dont write body instructions to memory for now */
  1729. ctx.write = false;
  1730. if (build_body(&ctx, extra_pass)) {
  1731. prog = orig_prog;
  1732. goto out_free_hdr;
  1733. }
  1734. ctx.epilogue_offset = ctx.idx;
  1735. ctx.exentry_idx = exentry_idx;
  1736. ctx.idx = body_idx;
  1737. ctx.write = true;
  1738. /* Pass 3: Adjust jump offset and write final image */
  1739. if (build_body(&ctx, extra_pass) ||
  1740. WARN_ON_ONCE(ctx.idx != ctx.epilogue_offset)) {
  1741. prog = orig_prog;
  1742. goto out_free_hdr;
  1743. }
  1744. build_epilogue(&ctx, was_classic);
  1745. build_plt(&ctx);
  1746. /* Extra pass to validate JITed code. */
  1747. if (validate_ctx(&ctx)) {
  1748. prog = orig_prog;
  1749. goto out_free_hdr;
  1750. }
  1751. /* update the real prog size */
  1752. prog_size = sizeof(u32) * ctx.idx;
  1753. /* And we're done. */
  1754. if (bpf_jit_enable > 1)
  1755. bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
  1756. if (!prog->is_func || extra_pass) {
  1757. /* The jited image may shrink since the jited result for
  1758. * BPF_CALL to subprog may be changed from indirect call
  1759. * to direct call.
  1760. */
  1761. if (extra_pass && ctx.idx > jit_data->ctx.idx) {
  1762. pr_err_once("multi-func JIT bug %d > %d\n",
  1763. ctx.idx, jit_data->ctx.idx);
  1764. prog->bpf_func = NULL;
  1765. prog->jited = 0;
  1766. prog->jited_len = 0;
  1767. goto out_free_hdr;
  1768. }
  1769. if (WARN_ON(bpf_jit_binary_pack_finalize(ro_header, header))) {
  1770. /* ro_header has been freed */
  1771. ro_header = NULL;
  1772. prog = orig_prog;
  1773. goto out_off;
  1774. }
  1775. /*
  1776. * The instructions have now been copied to the ROX region from
  1777. * where they will execute. Now the data cache has to be cleaned to
  1778. * the PoU and the I-cache has to be invalidated for the VAs.
  1779. */
  1780. bpf_flush_icache(ro_header, ctx.ro_image + ctx.idx);
  1781. } else {
  1782. jit_data->ctx = ctx;
  1783. jit_data->ro_image = ro_image_ptr;
  1784. jit_data->header = header;
  1785. jit_data->ro_header = ro_header;
  1786. }
  1787. prog->bpf_func = (void *)ctx.ro_image;
  1788. prog->jited = 1;
  1789. prog->jited_len = prog_size;
  1790. if (!prog->is_func || extra_pass) {
  1791. int i;
  1792. /* offset[prog->len] is the size of program */
  1793. for (i = 0; i <= prog->len; i++)
  1794. ctx.offset[i] *= AARCH64_INSN_SIZE;
  1795. bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
  1796. out_off:
  1797. kvfree(ctx.offset);
  1798. kfree(jit_data);
  1799. prog->aux->jit_data = NULL;
  1800. }
  1801. out:
  1802. if (tmp_blinded)
  1803. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1804. tmp : orig_prog);
  1805. return prog;
  1806. out_free_hdr:
  1807. if (header) {
  1808. bpf_arch_text_copy(&ro_header->size, &header->size,
  1809. sizeof(header->size));
  1810. bpf_jit_binary_pack_free(ro_header, header);
  1811. }
  1812. goto out_off;
  1813. }
  1814. bool bpf_jit_supports_kfunc_call(void)
  1815. {
  1816. return true;
  1817. }
  1818. void *bpf_arch_text_copy(void *dst, void *src, size_t len)
  1819. {
  1820. if (!aarch64_insn_copy(dst, src, len))
  1821. return ERR_PTR(-EINVAL);
  1822. return dst;
  1823. }
  1824. u64 bpf_jit_alloc_exec_limit(void)
  1825. {
  1826. return VMALLOC_END - VMALLOC_START;
  1827. }
  1828. /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
  1829. bool bpf_jit_supports_subprog_tailcalls(void)
  1830. {
  1831. return true;
  1832. }
  1833. static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l,
  1834. int args_off, int retval_off, int run_ctx_off,
  1835. bool save_ret)
  1836. {
  1837. __le32 *branch;
  1838. u64 enter_prog;
  1839. u64 exit_prog;
  1840. struct bpf_prog *p = l->link.prog;
  1841. int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
  1842. enter_prog = (u64)bpf_trampoline_enter(p);
  1843. exit_prog = (u64)bpf_trampoline_exit(p);
  1844. if (l->cookie == 0) {
  1845. /* if cookie is zero, one instruction is enough to store it */
  1846. emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
  1847. } else {
  1848. emit_a64_mov_i64(A64_R(10), l->cookie, ctx);
  1849. emit(A64_STR64I(A64_R(10), A64_SP, run_ctx_off + cookie_off),
  1850. ctx);
  1851. }
  1852. /* save p to callee saved register x19 to avoid loading p with mov_i64
  1853. * each time.
  1854. */
  1855. emit_addr_mov_i64(A64_R(19), (const u64)p, ctx);
  1856. /* arg1: prog */
  1857. emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
  1858. /* arg2: &run_ctx */
  1859. emit(A64_ADD_I(1, A64_R(1), A64_SP, run_ctx_off), ctx);
  1860. emit_call(enter_prog, ctx);
  1861. /* save return value to callee saved register x20 */
  1862. emit(A64_MOV(1, A64_R(20), A64_R(0)), ctx);
  1863. /* if (__bpf_prog_enter(prog) == 0)
  1864. * goto skip_exec_of_prog;
  1865. */
  1866. branch = ctx->image + ctx->idx;
  1867. emit(A64_NOP, ctx);
  1868. emit(A64_ADD_I(1, A64_R(0), A64_SP, args_off), ctx);
  1869. if (!p->jited)
  1870. emit_addr_mov_i64(A64_R(1), (const u64)p->insnsi, ctx);
  1871. emit_call((const u64)p->bpf_func, ctx);
  1872. if (save_ret)
  1873. emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
  1874. if (ctx->image) {
  1875. int offset = &ctx->image[ctx->idx] - branch;
  1876. *branch = cpu_to_le32(A64_CBZ(1, A64_R(0), offset));
  1877. }
  1878. /* arg1: prog */
  1879. emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
  1880. /* arg2: start time */
  1881. emit(A64_MOV(1, A64_R(1), A64_R(20)), ctx);
  1882. /* arg3: &run_ctx */
  1883. emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
  1884. emit_call(exit_prog, ctx);
  1885. }
  1886. static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_links *tl,
  1887. int args_off, int retval_off, int run_ctx_off,
  1888. __le32 **branches)
  1889. {
  1890. int i;
  1891. /* The first fmod_ret program will receive a garbage return value.
  1892. * Set this to 0 to avoid confusing the program.
  1893. */
  1894. emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
  1895. for (i = 0; i < tl->nr_links; i++) {
  1896. invoke_bpf_prog(ctx, tl->links[i], args_off, retval_off,
  1897. run_ctx_off, true);
  1898. /* if (*(u64 *)(sp + retval_off) != 0)
  1899. * goto do_fexit;
  1900. */
  1901. emit(A64_LDR64I(A64_R(10), A64_SP, retval_off), ctx);
  1902. /* Save the location of branch, and generate a nop.
  1903. * This nop will be replaced with a cbnz later.
  1904. */
  1905. branches[i] = ctx->image + ctx->idx;
  1906. emit(A64_NOP, ctx);
  1907. }
  1908. }
  1909. static void save_args(struct jit_ctx *ctx, int args_off, int nregs)
  1910. {
  1911. int i;
  1912. for (i = 0; i < nregs; i++) {
  1913. emit(A64_STR64I(i, A64_SP, args_off), ctx);
  1914. args_off += 8;
  1915. }
  1916. }
  1917. static void restore_args(struct jit_ctx *ctx, int args_off, int nregs)
  1918. {
  1919. int i;
  1920. for (i = 0; i < nregs; i++) {
  1921. emit(A64_LDR64I(i, A64_SP, args_off), ctx);
  1922. args_off += 8;
  1923. }
  1924. }
  1925. static bool is_struct_ops_tramp(const struct bpf_tramp_links *fentry_links)
  1926. {
  1927. return fentry_links->nr_links == 1 &&
  1928. fentry_links->links[0]->link.type == BPF_LINK_TYPE_STRUCT_OPS;
  1929. }
  1930. /* Based on the x86's implementation of arch_prepare_bpf_trampoline().
  1931. *
  1932. * bpf prog and function entry before bpf trampoline hooked:
  1933. * mov x9, lr
  1934. * nop
  1935. *
  1936. * bpf prog and function entry after bpf trampoline hooked:
  1937. * mov x9, lr
  1938. * bl <bpf_trampoline or plt>
  1939. *
  1940. */
  1941. static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
  1942. struct bpf_tramp_links *tlinks, void *func_addr,
  1943. int nregs, u32 flags)
  1944. {
  1945. int i;
  1946. int stack_size;
  1947. int retaddr_off;
  1948. int regs_off;
  1949. int retval_off;
  1950. int args_off;
  1951. int nregs_off;
  1952. int ip_off;
  1953. int run_ctx_off;
  1954. struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
  1955. struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
  1956. struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
  1957. bool save_ret;
  1958. __le32 **branches = NULL;
  1959. bool is_struct_ops = is_struct_ops_tramp(fentry);
  1960. /* trampoline stack layout:
  1961. * [ parent ip ]
  1962. * [ FP ]
  1963. * SP + retaddr_off [ self ip ]
  1964. * [ FP ]
  1965. *
  1966. * [ padding ] align SP to multiples of 16
  1967. *
  1968. * [ x20 ] callee saved reg x20
  1969. * SP + regs_off [ x19 ] callee saved reg x19
  1970. *
  1971. * SP + retval_off [ return value ] BPF_TRAMP_F_CALL_ORIG or
  1972. * BPF_TRAMP_F_RET_FENTRY_RET
  1973. *
  1974. * [ arg reg N ]
  1975. * [ ... ]
  1976. * SP + args_off [ arg reg 1 ]
  1977. *
  1978. * SP + nregs_off [ arg regs count ]
  1979. *
  1980. * SP + ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag
  1981. *
  1982. * SP + run_ctx_off [ bpf_tramp_run_ctx ]
  1983. */
  1984. stack_size = 0;
  1985. run_ctx_off = stack_size;
  1986. /* room for bpf_tramp_run_ctx */
  1987. stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
  1988. ip_off = stack_size;
  1989. /* room for IP address argument */
  1990. if (flags & BPF_TRAMP_F_IP_ARG)
  1991. stack_size += 8;
  1992. nregs_off = stack_size;
  1993. /* room for args count */
  1994. stack_size += 8;
  1995. args_off = stack_size;
  1996. /* room for args */
  1997. stack_size += nregs * 8;
  1998. /* room for return value */
  1999. retval_off = stack_size;
  2000. save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
  2001. if (save_ret)
  2002. stack_size += 8;
  2003. /* room for callee saved registers, currently x19 and x20 are used */
  2004. regs_off = stack_size;
  2005. stack_size += 16;
  2006. /* round up to multiples of 16 to avoid SPAlignmentFault */
  2007. stack_size = round_up(stack_size, 16);
  2008. /* return address locates above FP */
  2009. retaddr_off = stack_size + 8;
  2010. /* bpf trampoline may be invoked by 3 instruction types:
  2011. * 1. bl, attached to bpf prog or kernel function via short jump
  2012. * 2. br, attached to bpf prog or kernel function via long jump
  2013. * 3. blr, working as a function pointer, used by struct_ops.
  2014. * So BTI_JC should used here to support both br and blr.
  2015. */
  2016. emit_bti(A64_BTI_JC, ctx);
  2017. /* x9 is not set for struct_ops */
  2018. if (!is_struct_ops) {
  2019. /* frame for parent function */
  2020. emit(A64_PUSH(A64_FP, A64_R(9), A64_SP), ctx);
  2021. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  2022. }
  2023. /* frame for patched function for tracing, or caller for struct_ops */
  2024. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  2025. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  2026. /* allocate stack space */
  2027. emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
  2028. if (flags & BPF_TRAMP_F_IP_ARG) {
  2029. /* save ip address of the traced function */
  2030. emit_addr_mov_i64(A64_R(10), (const u64)func_addr, ctx);
  2031. emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx);
  2032. }
  2033. /* save arg regs count*/
  2034. emit(A64_MOVZ(1, A64_R(10), nregs, 0), ctx);
  2035. emit(A64_STR64I(A64_R(10), A64_SP, nregs_off), ctx);
  2036. /* save arg regs */
  2037. save_args(ctx, args_off, nregs);
  2038. /* save callee saved registers */
  2039. emit(A64_STR64I(A64_R(19), A64_SP, regs_off), ctx);
  2040. emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
  2041. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2042. /* for the first pass, assume the worst case */
  2043. if (!ctx->image)
  2044. ctx->idx += 4;
  2045. else
  2046. emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
  2047. emit_call((const u64)__bpf_tramp_enter, ctx);
  2048. }
  2049. for (i = 0; i < fentry->nr_links; i++)
  2050. invoke_bpf_prog(ctx, fentry->links[i], args_off,
  2051. retval_off, run_ctx_off,
  2052. flags & BPF_TRAMP_F_RET_FENTRY_RET);
  2053. if (fmod_ret->nr_links) {
  2054. branches = kcalloc(fmod_ret->nr_links, sizeof(__le32 *),
  2055. GFP_KERNEL);
  2056. if (!branches)
  2057. return -ENOMEM;
  2058. invoke_bpf_mod_ret(ctx, fmod_ret, args_off, retval_off,
  2059. run_ctx_off, branches);
  2060. }
  2061. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2062. restore_args(ctx, args_off, nregs);
  2063. /* call original func */
  2064. emit(A64_LDR64I(A64_R(10), A64_SP, retaddr_off), ctx);
  2065. emit(A64_ADR(A64_LR, AARCH64_INSN_SIZE * 2), ctx);
  2066. emit(A64_RET(A64_R(10)), ctx);
  2067. /* store return value */
  2068. emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
  2069. /* reserve a nop for bpf_tramp_image_put */
  2070. im->ip_after_call = ctx->ro_image + ctx->idx;
  2071. emit(A64_NOP, ctx);
  2072. }
  2073. /* update the branches saved in invoke_bpf_mod_ret with cbnz */
  2074. for (i = 0; i < fmod_ret->nr_links && ctx->image != NULL; i++) {
  2075. int offset = &ctx->image[ctx->idx] - branches[i];
  2076. *branches[i] = cpu_to_le32(A64_CBNZ(1, A64_R(10), offset));
  2077. }
  2078. for (i = 0; i < fexit->nr_links; i++)
  2079. invoke_bpf_prog(ctx, fexit->links[i], args_off, retval_off,
  2080. run_ctx_off, false);
  2081. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  2082. im->ip_epilogue = ctx->ro_image + ctx->idx;
  2083. /* for the first pass, assume the worst case */
  2084. if (!ctx->image)
  2085. ctx->idx += 4;
  2086. else
  2087. emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
  2088. emit_call((const u64)__bpf_tramp_exit, ctx);
  2089. }
  2090. if (flags & BPF_TRAMP_F_RESTORE_REGS)
  2091. restore_args(ctx, args_off, nregs);
  2092. /* restore callee saved register x19 and x20 */
  2093. emit(A64_LDR64I(A64_R(19), A64_SP, regs_off), ctx);
  2094. emit(A64_LDR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
  2095. if (save_ret)
  2096. emit(A64_LDR64I(A64_R(0), A64_SP, retval_off), ctx);
  2097. /* reset SP */
  2098. emit(A64_MOV(1, A64_SP, A64_FP), ctx);
  2099. if (is_struct_ops) {
  2100. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  2101. emit(A64_RET(A64_LR), ctx);
  2102. } else {
  2103. /* pop frames */
  2104. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  2105. emit(A64_POP(A64_FP, A64_R(9), A64_SP), ctx);
  2106. if (flags & BPF_TRAMP_F_SKIP_FRAME) {
  2107. /* skip patched function, return to parent */
  2108. emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
  2109. emit(A64_RET(A64_R(9)), ctx);
  2110. } else {
  2111. /* return to patched function */
  2112. emit(A64_MOV(1, A64_R(10), A64_LR), ctx);
  2113. emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
  2114. emit(A64_RET(A64_R(10)), ctx);
  2115. }
  2116. }
  2117. kfree(branches);
  2118. return ctx->idx;
  2119. }
  2120. static int btf_func_model_nregs(const struct btf_func_model *m)
  2121. {
  2122. int nregs = m->nr_args;
  2123. int i;
  2124. /* extra registers needed for struct argument */
  2125. for (i = 0; i < MAX_BPF_FUNC_ARGS; i++) {
  2126. /* The arg_size is at most 16 bytes, enforced by the verifier. */
  2127. if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
  2128. nregs += (m->arg_size[i] + 7) / 8 - 1;
  2129. }
  2130. return nregs;
  2131. }
  2132. int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
  2133. struct bpf_tramp_links *tlinks, void *func_addr)
  2134. {
  2135. struct jit_ctx ctx = {
  2136. .image = NULL,
  2137. .idx = 0,
  2138. };
  2139. struct bpf_tramp_image im;
  2140. int nregs, ret;
  2141. nregs = btf_func_model_nregs(m);
  2142. /* the first 8 registers are used for arguments */
  2143. if (nregs > 8)
  2144. return -ENOTSUPP;
  2145. ret = prepare_trampoline(&ctx, &im, tlinks, func_addr, nregs, flags);
  2146. if (ret < 0)
  2147. return ret;
  2148. return ret < 0 ? ret : ret * AARCH64_INSN_SIZE;
  2149. }
  2150. void *arch_alloc_bpf_trampoline(unsigned int size)
  2151. {
  2152. return bpf_prog_pack_alloc(size, jit_fill_hole);
  2153. }
  2154. void arch_free_bpf_trampoline(void *image, unsigned int size)
  2155. {
  2156. bpf_prog_pack_free(image, size);
  2157. }
  2158. int arch_protect_bpf_trampoline(void *image, unsigned int size)
  2159. {
  2160. return 0;
  2161. }
  2162. int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
  2163. void *ro_image_end, const struct btf_func_model *m,
  2164. u32 flags, struct bpf_tramp_links *tlinks,
  2165. void *func_addr)
  2166. {
  2167. int ret, nregs;
  2168. void *image, *tmp;
  2169. u32 size = ro_image_end - ro_image;
  2170. /* image doesn't need to be in module memory range, so we can
  2171. * use kvmalloc.
  2172. */
  2173. image = kvmalloc(size, GFP_KERNEL);
  2174. if (!image)
  2175. return -ENOMEM;
  2176. struct jit_ctx ctx = {
  2177. .image = image,
  2178. .ro_image = ro_image,
  2179. .idx = 0,
  2180. .write = true,
  2181. };
  2182. nregs = btf_func_model_nregs(m);
  2183. /* the first 8 registers are used for arguments */
  2184. if (nregs > 8)
  2185. return -ENOTSUPP;
  2186. jit_fill_hole(image, (unsigned int)(ro_image_end - ro_image));
  2187. ret = prepare_trampoline(&ctx, im, tlinks, func_addr, nregs, flags);
  2188. if (ret > 0 && validate_code(&ctx) < 0) {
  2189. ret = -EINVAL;
  2190. goto out;
  2191. }
  2192. if (ret > 0)
  2193. ret *= AARCH64_INSN_SIZE;
  2194. tmp = bpf_arch_text_copy(ro_image, image, size);
  2195. if (IS_ERR(tmp)) {
  2196. ret = PTR_ERR(tmp);
  2197. goto out;
  2198. }
  2199. bpf_flush_icache(ro_image, ro_image + size);
  2200. out:
  2201. kvfree(image);
  2202. return ret;
  2203. }
  2204. static bool is_long_jump(void *ip, void *target)
  2205. {
  2206. long offset;
  2207. /* NULL target means this is a NOP */
  2208. if (!target)
  2209. return false;
  2210. offset = (long)target - (long)ip;
  2211. return offset < -SZ_128M || offset >= SZ_128M;
  2212. }
  2213. static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,
  2214. void *addr, void *plt, u32 *insn)
  2215. {
  2216. void *target;
  2217. if (!addr) {
  2218. *insn = aarch64_insn_gen_nop();
  2219. return 0;
  2220. }
  2221. if (is_long_jump(ip, addr))
  2222. target = plt;
  2223. else
  2224. target = addr;
  2225. *insn = aarch64_insn_gen_branch_imm((unsigned long)ip,
  2226. (unsigned long)target,
  2227. type);
  2228. return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT;
  2229. }
  2230. /* Replace the branch instruction from @ip to @old_addr in a bpf prog or a bpf
  2231. * trampoline with the branch instruction from @ip to @new_addr. If @old_addr
  2232. * or @new_addr is NULL, the old or new instruction is NOP.
  2233. *
  2234. * When @ip is the bpf prog entry, a bpf trampoline is being attached or
  2235. * detached. Since bpf trampoline and bpf prog are allocated separately with
  2236. * vmalloc, the address distance may exceed 128MB, the maximum branch range.
  2237. * So long jump should be handled.
  2238. *
  2239. * When a bpf prog is constructed, a plt pointing to empty trampoline
  2240. * dummy_tramp is placed at the end:
  2241. *
  2242. * bpf_prog:
  2243. * mov x9, lr
  2244. * nop // patchsite
  2245. * ...
  2246. * ret
  2247. *
  2248. * plt:
  2249. * ldr x10, target
  2250. * br x10
  2251. * target:
  2252. * .quad dummy_tramp // plt target
  2253. *
  2254. * This is also the state when no trampoline is attached.
  2255. *
  2256. * When a short-jump bpf trampoline is attached, the patchsite is patched
  2257. * to a bl instruction to the trampoline directly:
  2258. *
  2259. * bpf_prog:
  2260. * mov x9, lr
  2261. * bl <short-jump bpf trampoline address> // patchsite
  2262. * ...
  2263. * ret
  2264. *
  2265. * plt:
  2266. * ldr x10, target
  2267. * br x10
  2268. * target:
  2269. * .quad dummy_tramp // plt target
  2270. *
  2271. * When a long-jump bpf trampoline is attached, the plt target is filled with
  2272. * the trampoline address and the patchsite is patched to a bl instruction to
  2273. * the plt:
  2274. *
  2275. * bpf_prog:
  2276. * mov x9, lr
  2277. * bl plt // patchsite
  2278. * ...
  2279. * ret
  2280. *
  2281. * plt:
  2282. * ldr x10, target
  2283. * br x10
  2284. * target:
  2285. * .quad <long-jump bpf trampoline address> // plt target
  2286. *
  2287. * The dummy_tramp is used to prevent another CPU from jumping to unknown
  2288. * locations during the patching process, making the patching process easier.
  2289. */
  2290. int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
  2291. void *old_addr, void *new_addr)
  2292. {
  2293. int ret;
  2294. u32 old_insn;
  2295. u32 new_insn;
  2296. u32 replaced;
  2297. struct bpf_plt *plt = NULL;
  2298. unsigned long size = 0UL;
  2299. unsigned long offset = ~0UL;
  2300. enum aarch64_insn_branch_type branch_type;
  2301. char namebuf[KSYM_NAME_LEN];
  2302. void *image = NULL;
  2303. u64 plt_target = 0ULL;
  2304. bool poking_bpf_entry;
  2305. if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
  2306. /* Only poking bpf text is supported. Since kernel function
  2307. * entry is set up by ftrace, we reply on ftrace to poke kernel
  2308. * functions.
  2309. */
  2310. return -ENOTSUPP;
  2311. image = ip - offset;
  2312. /* zero offset means we're poking bpf prog entry */
  2313. poking_bpf_entry = (offset == 0UL);
  2314. /* bpf prog entry, find plt and the real patchsite */
  2315. if (poking_bpf_entry) {
  2316. /* plt locates at the end of bpf prog */
  2317. plt = image + size - PLT_TARGET_OFFSET;
  2318. /* skip to the nop instruction in bpf prog entry:
  2319. * bti c // if BTI enabled
  2320. * mov x9, x30
  2321. * nop
  2322. */
  2323. ip = image + POKE_OFFSET * AARCH64_INSN_SIZE;
  2324. }
  2325. /* long jump is only possible at bpf prog entry */
  2326. if (WARN_ON((is_long_jump(ip, new_addr) || is_long_jump(ip, old_addr)) &&
  2327. !poking_bpf_entry))
  2328. return -EINVAL;
  2329. if (poke_type == BPF_MOD_CALL)
  2330. branch_type = AARCH64_INSN_BRANCH_LINK;
  2331. else
  2332. branch_type = AARCH64_INSN_BRANCH_NOLINK;
  2333. if (gen_branch_or_nop(branch_type, ip, old_addr, plt, &old_insn) < 0)
  2334. return -EFAULT;
  2335. if (gen_branch_or_nop(branch_type, ip, new_addr, plt, &new_insn) < 0)
  2336. return -EFAULT;
  2337. if (is_long_jump(ip, new_addr))
  2338. plt_target = (u64)new_addr;
  2339. else if (is_long_jump(ip, old_addr))
  2340. /* if the old target is a long jump and the new target is not,
  2341. * restore the plt target to dummy_tramp, so there is always a
  2342. * legal and harmless address stored in plt target, and we'll
  2343. * never jump from plt to an unknown place.
  2344. */
  2345. plt_target = (u64)&dummy_tramp;
  2346. if (plt_target) {
  2347. /* non-zero plt_target indicates we're patching a bpf prog,
  2348. * which is read only.
  2349. */
  2350. if (set_memory_rw(PAGE_MASK & ((uintptr_t)&plt->target), 1))
  2351. return -EFAULT;
  2352. WRITE_ONCE(plt->target, plt_target);
  2353. set_memory_ro(PAGE_MASK & ((uintptr_t)&plt->target), 1);
  2354. /* since plt target points to either the new trampoline
  2355. * or dummy_tramp, even if another CPU reads the old plt
  2356. * target value before fetching the bl instruction to plt,
  2357. * it will be brought back by dummy_tramp, so no barrier is
  2358. * required here.
  2359. */
  2360. }
  2361. /* if the old target and the new target are both long jumps, no
  2362. * patching is required
  2363. */
  2364. if (old_insn == new_insn)
  2365. return 0;
  2366. mutex_lock(&text_mutex);
  2367. if (aarch64_insn_read(ip, &replaced)) {
  2368. ret = -EFAULT;
  2369. goto out;
  2370. }
  2371. if (replaced != old_insn) {
  2372. ret = -EFAULT;
  2373. goto out;
  2374. }
  2375. /* We call aarch64_insn_patch_text_nosync() to replace instruction
  2376. * atomically, so no other CPUs will fetch a half-new and half-old
  2377. * instruction. But there is chance that another CPU executes the
  2378. * old instruction after the patching operation finishes (e.g.,
  2379. * pipeline not flushed, or icache not synchronized yet).
  2380. *
  2381. * 1. when a new trampoline is attached, it is not a problem for
  2382. * different CPUs to jump to different trampolines temporarily.
  2383. *
  2384. * 2. when an old trampoline is freed, we should wait for all other
  2385. * CPUs to exit the trampoline and make sure the trampoline is no
  2386. * longer reachable, since bpf_tramp_image_put() function already
  2387. * uses percpu_ref and task-based rcu to do the sync, no need to call
  2388. * the sync version here, see bpf_tramp_image_put() for details.
  2389. */
  2390. ret = aarch64_insn_patch_text_nosync(ip, new_insn);
  2391. out:
  2392. mutex_unlock(&text_mutex);
  2393. return ret;
  2394. }
  2395. bool bpf_jit_supports_ptr_xchg(void)
  2396. {
  2397. return true;
  2398. }
  2399. bool bpf_jit_supports_exceptions(void)
  2400. {
  2401. /* We unwind through both kernel frames starting from within bpf_throw
  2402. * call and BPF frames. Therefore we require FP unwinder to be enabled
  2403. * to walk kernel frames and reach BPF frames in the stack trace.
  2404. * ARM64 kernel is aways compiled with CONFIG_FRAME_POINTER=y
  2405. */
  2406. return true;
  2407. }
  2408. bool bpf_jit_supports_arena(void)
  2409. {
  2410. return true;
  2411. }
  2412. bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
  2413. {
  2414. if (!in_arena)
  2415. return true;
  2416. switch (insn->code) {
  2417. case BPF_STX | BPF_ATOMIC | BPF_W:
  2418. case BPF_STX | BPF_ATOMIC | BPF_DW:
  2419. if (!cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
  2420. return false;
  2421. }
  2422. return true;
  2423. }
  2424. bool bpf_jit_supports_percpu_insn(void)
  2425. {
  2426. return true;
  2427. }
  2428. bool bpf_jit_inlines_helper_call(s32 imm)
  2429. {
  2430. switch (imm) {
  2431. case BPF_FUNC_get_smp_processor_id:
  2432. case BPF_FUNC_get_current_task:
  2433. case BPF_FUNC_get_current_task_btf:
  2434. return true;
  2435. default:
  2436. return false;
  2437. }
  2438. }
  2439. void bpf_jit_free(struct bpf_prog *prog)
  2440. {
  2441. if (prog->jited) {
  2442. struct arm64_jit_data *jit_data = prog->aux->jit_data;
  2443. struct bpf_binary_header *hdr;
  2444. /*
  2445. * If we fail the final pass of JIT (from jit_subprogs),
  2446. * the program may not be finalized yet. Call finalize here
  2447. * before freeing it.
  2448. */
  2449. if (jit_data) {
  2450. bpf_jit_binary_pack_finalize(jit_data->ro_header, jit_data->header);
  2451. kfree(jit_data);
  2452. }
  2453. hdr = bpf_jit_binary_pack_hdr(prog);
  2454. bpf_jit_binary_pack_free(hdr, NULL);
  2455. WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
  2456. }
  2457. bpf_prog_unlock_free(prog);
  2458. }