bpf_jit_comp.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  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/bitfield.h>
  9. #include <linux/bpf.h>
  10. #include <linux/filter.h>
  11. #include <linux/memory.h>
  12. #include <linux/printk.h>
  13. #include <linux/slab.h>
  14. #include <asm/asm-extable.h>
  15. #include <asm/byteorder.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/debug-monitors.h>
  18. #include <asm/insn.h>
  19. #include <asm/patching.h>
  20. #include <asm/set_memory.h>
  21. #include "bpf_jit.h"
  22. #ifdef CONFIG_RKP
  23. #include <linux/rkp.h>
  24. #endif
  25. #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
  26. #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
  27. #define TCALL_CNT (MAX_BPF_JIT_REG + 2)
  28. #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
  29. #define FP_BOTTOM (MAX_BPF_JIT_REG + 4)
  30. #define check_imm(bits, imm) do { \
  31. if ((((imm) > 0) && ((imm) >> (bits))) || \
  32. (((imm) < 0) && (~(imm) >> (bits)))) { \
  33. pr_info("[%2d] imm=%d(0x%x) out of range\n", \
  34. i, imm, imm); \
  35. return -EINVAL; \
  36. } \
  37. } while (0)
  38. #define check_imm19(imm) check_imm(19, imm)
  39. #define check_imm26(imm) check_imm(26, imm)
  40. /* Map BPF registers to A64 registers */
  41. static const int bpf2a64[] = {
  42. /* return value from in-kernel function, and exit value from eBPF */
  43. [BPF_REG_0] = A64_R(7),
  44. /* arguments from eBPF program to in-kernel function */
  45. [BPF_REG_1] = A64_R(0),
  46. [BPF_REG_2] = A64_R(1),
  47. [BPF_REG_3] = A64_R(2),
  48. [BPF_REG_4] = A64_R(3),
  49. [BPF_REG_5] = A64_R(4),
  50. /* callee saved registers that in-kernel function will preserve */
  51. [BPF_REG_6] = A64_R(19),
  52. [BPF_REG_7] = A64_R(20),
  53. [BPF_REG_8] = A64_R(21),
  54. [BPF_REG_9] = A64_R(22),
  55. /* read-only frame pointer to access stack */
  56. [BPF_REG_FP] = A64_R(25),
  57. /* temporary registers for BPF JIT */
  58. [TMP_REG_1] = A64_R(10),
  59. [TMP_REG_2] = A64_R(11),
  60. [TMP_REG_3] = A64_R(12),
  61. /* tail_call_cnt */
  62. [TCALL_CNT] = A64_R(26),
  63. /* temporary register for blinding constants */
  64. [BPF_REG_AX] = A64_R(9),
  65. [FP_BOTTOM] = A64_R(27),
  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. __le32 *image;
  74. u32 stack_size;
  75. int fpb_offset;
  76. };
  77. struct bpf_plt {
  78. u32 insn_ldr; /* load target */
  79. u32 insn_br; /* branch to target */
  80. u64 target; /* target value */
  81. };
  82. #define PLT_TARGET_SIZE sizeof_field(struct bpf_plt, target)
  83. #define PLT_TARGET_OFFSET offsetof(struct bpf_plt, target)
  84. static inline void emit(const u32 insn, struct jit_ctx *ctx)
  85. {
  86. if (ctx->image != NULL)
  87. ctx->image[ctx->idx] = cpu_to_le32(insn);
  88. ctx->idx++;
  89. }
  90. static inline void emit_a64_mov_i(const int is64, const int reg,
  91. const s32 val, struct jit_ctx *ctx)
  92. {
  93. u16 hi = val >> 16;
  94. u16 lo = val & 0xffff;
  95. if (hi & 0x8000) {
  96. if (hi == 0xffff) {
  97. emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
  98. } else {
  99. emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
  100. if (lo != 0xffff)
  101. emit(A64_MOVK(is64, reg, lo, 0), ctx);
  102. }
  103. } else {
  104. emit(A64_MOVZ(is64, reg, lo, 0), ctx);
  105. if (hi)
  106. emit(A64_MOVK(is64, reg, hi, 16), ctx);
  107. }
  108. }
  109. static int i64_i16_blocks(const u64 val, bool inverse)
  110. {
  111. return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  112. (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  113. (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
  114. (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
  115. }
  116. static inline void emit_a64_mov_i64(const int reg, const u64 val,
  117. struct jit_ctx *ctx)
  118. {
  119. u64 nrm_tmp = val, rev_tmp = ~val;
  120. bool inverse;
  121. int shift;
  122. if (!(nrm_tmp >> 32))
  123. return emit_a64_mov_i(0, reg, (u32)val, ctx);
  124. inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
  125. shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
  126. (fls64(nrm_tmp) - 1)), 16), 0);
  127. if (inverse)
  128. emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
  129. else
  130. emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  131. shift -= 16;
  132. while (shift >= 0) {
  133. if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
  134. emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
  135. shift -= 16;
  136. }
  137. }
  138. static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
  139. {
  140. if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
  141. emit(insn, ctx);
  142. }
  143. /*
  144. * Kernel addresses in the vmalloc space use at most 48 bits, and the
  145. * remaining bits are guaranteed to be 0x1. So we can compose the address
  146. * with a fixed length movn/movk/movk sequence.
  147. */
  148. static inline void emit_addr_mov_i64(const int reg, const u64 val,
  149. struct jit_ctx *ctx)
  150. {
  151. u64 tmp = val;
  152. int shift = 0;
  153. emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
  154. while (shift < 32) {
  155. tmp >>= 16;
  156. shift += 16;
  157. emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
  158. }
  159. }
  160. static inline void emit_call(u64 target, struct jit_ctx *ctx)
  161. {
  162. u8 tmp = bpf2a64[TMP_REG_1];
  163. emit_addr_mov_i64(tmp, target, ctx);
  164. emit(A64_BLR(tmp), ctx);
  165. }
  166. static inline int bpf2a64_offset(int bpf_insn, int off,
  167. const struct jit_ctx *ctx)
  168. {
  169. /* BPF JMP offset is relative to the next instruction */
  170. bpf_insn++;
  171. /*
  172. * Whereas arm64 branch instructions encode the offset
  173. * from the branch itself, so we must subtract 1 from the
  174. * instruction offset.
  175. */
  176. return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
  177. }
  178. static void jit_fill_hole(void *area, unsigned int size)
  179. {
  180. __le32 *ptr;
  181. /* We are guaranteed to have aligned memory. */
  182. for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
  183. *ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
  184. }
  185. static inline int epilogue_offset(const struct jit_ctx *ctx)
  186. {
  187. int to = ctx->epilogue_offset;
  188. int from = ctx->idx;
  189. return to - from;
  190. }
  191. static bool is_addsub_imm(u32 imm)
  192. {
  193. /* Either imm12 or shifted imm12. */
  194. return !(imm & ~0xfff) || !(imm & ~0xfff000);
  195. }
  196. /*
  197. * There are 3 types of AArch64 LDR/STR (immediate) instruction:
  198. * Post-index, Pre-index, Unsigned offset.
  199. *
  200. * For BPF ldr/str, the "unsigned offset" type is sufficient.
  201. *
  202. * "Unsigned offset" type LDR(immediate) format:
  203. *
  204. * 3 2 1 0
  205. * 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
  206. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  207. * |x x|1 1 1 0 0 1 0 1| imm12 | Rn | Rt |
  208. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  209. * scale
  210. *
  211. * "Unsigned offset" type STR(immediate) format:
  212. * 3 2 1 0
  213. * 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
  214. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  215. * |x x|1 1 1 0 0 1 0 0| imm12 | Rn | Rt |
  216. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  217. * scale
  218. *
  219. * The offset is calculated from imm12 and scale in the following way:
  220. *
  221. * offset = (u64)imm12 << scale
  222. */
  223. static bool is_lsi_offset(int offset, int scale)
  224. {
  225. if (offset < 0)
  226. return false;
  227. if (offset > (0xFFF << scale))
  228. return false;
  229. if (offset & ((1 << scale) - 1))
  230. return false;
  231. return true;
  232. }
  233. /* generated prologue:
  234. * bti c // if CONFIG_ARM64_BTI_KERNEL
  235. * mov x9, lr
  236. * nop // POKE_OFFSET
  237. * paciasp // if CONFIG_ARM64_PTR_AUTH_KERNEL
  238. * stp x29, lr, [sp, #-16]!
  239. * mov x29, sp
  240. * stp x19, x20, [sp, #-16]!
  241. * stp x21, x22, [sp, #-16]!
  242. * stp x25, x26, [sp, #-16]!
  243. * stp x27, x28, [sp, #-16]!
  244. * mov x25, sp
  245. * mov tcc, #0
  246. * // PROLOGUE_OFFSET
  247. */
  248. #define BTI_INSNS (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) ? 1 : 0)
  249. #define PAC_INSNS (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) ? 1 : 0)
  250. /* Offset of nop instruction in bpf prog entry to be poked */
  251. #define POKE_OFFSET (BTI_INSNS + 1)
  252. /* Tail call offset to jump into */
  253. #define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 8)
  254. static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
  255. {
  256. const struct bpf_prog *prog = ctx->prog;
  257. const bool is_main_prog = prog->aux->func_idx == 0;
  258. const u8 r6 = bpf2a64[BPF_REG_6];
  259. const u8 r7 = bpf2a64[BPF_REG_7];
  260. const u8 r8 = bpf2a64[BPF_REG_8];
  261. const u8 r9 = bpf2a64[BPF_REG_9];
  262. const u8 fp = bpf2a64[BPF_REG_FP];
  263. const u8 tcc = bpf2a64[TCALL_CNT];
  264. const u8 fpb = bpf2a64[FP_BOTTOM];
  265. const int idx0 = ctx->idx;
  266. int cur_offset;
  267. /*
  268. * BPF prog stack layout
  269. *
  270. * high
  271. * original A64_SP => 0:+-----+ BPF prologue
  272. * |FP/LR|
  273. * current A64_FP => -16:+-----+
  274. * | ... | callee saved registers
  275. * BPF fp register => -64:+-----+ <= (BPF_FP)
  276. * | |
  277. * | ... | BPF prog stack
  278. * | |
  279. * +-----+ <= (BPF_FP - prog->aux->stack_depth)
  280. * |RSVD | padding
  281. * current A64_SP => +-----+ <= (BPF_FP - ctx->stack_size)
  282. * | |
  283. * | ... | Function call stack
  284. * | |
  285. * +-----+
  286. * low
  287. *
  288. */
  289. /* bpf function may be invoked by 3 instruction types:
  290. * 1. bl, attached via freplace to bpf prog via short jump
  291. * 2. br, attached via freplace to bpf prog via long jump
  292. * 3. blr, working as a function pointer, used by emit_call.
  293. * So BTI_JC should used here to support both br and blr.
  294. */
  295. emit_bti(A64_BTI_JC, ctx);
  296. emit(A64_MOV(1, A64_R(9), A64_LR), ctx);
  297. emit(A64_NOP, ctx);
  298. /* Sign lr */
  299. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  300. emit(A64_PACIASP, ctx);
  301. /* Save FP and LR registers to stay align with ARM64 AAPCS */
  302. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  303. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  304. /* Save callee-saved registers */
  305. emit(A64_PUSH(r6, r7, A64_SP), ctx);
  306. emit(A64_PUSH(r8, r9, A64_SP), ctx);
  307. emit(A64_PUSH(fp, tcc, A64_SP), ctx);
  308. emit(A64_PUSH(fpb, A64_R(28), A64_SP), ctx);
  309. /* Set up BPF prog stack base register */
  310. emit(A64_MOV(1, fp, A64_SP), ctx);
  311. if (!ebpf_from_cbpf && is_main_prog) {
  312. /* Initialize tail_call_cnt */
  313. emit(A64_MOVZ(1, tcc, 0, 0), ctx);
  314. cur_offset = ctx->idx - idx0;
  315. if (cur_offset != PROLOGUE_OFFSET) {
  316. pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
  317. cur_offset, PROLOGUE_OFFSET);
  318. return -1;
  319. }
  320. /* BTI landing pad for the tail call, done with a BR */
  321. emit_bti(A64_BTI_J, ctx);
  322. }
  323. emit(A64_SUB_I(1, fpb, fp, ctx->fpb_offset), ctx);
  324. /* Stack must be multiples of 16B */
  325. ctx->stack_size = round_up(prog->aux->stack_depth, 16);
  326. /* Set up function call stack */
  327. emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  328. return 0;
  329. }
  330. static int out_offset = -1; /* initialized on the first pass of build_body() */
  331. static int emit_bpf_tail_call(struct jit_ctx *ctx)
  332. {
  333. /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
  334. const u8 r2 = bpf2a64[BPF_REG_2];
  335. const u8 r3 = bpf2a64[BPF_REG_3];
  336. const u8 tmp = bpf2a64[TMP_REG_1];
  337. const u8 prg = bpf2a64[TMP_REG_2];
  338. const u8 tcc = bpf2a64[TCALL_CNT];
  339. const int idx0 = ctx->idx;
  340. #define cur_offset (ctx->idx - idx0)
  341. #define jmp_offset (out_offset - (cur_offset))
  342. size_t off;
  343. /* if (index >= array->map.max_entries)
  344. * goto out;
  345. */
  346. off = offsetof(struct bpf_array, map.max_entries);
  347. emit_a64_mov_i64(tmp, off, ctx);
  348. emit(A64_LDR32(tmp, r2, tmp), ctx);
  349. emit(A64_MOV(0, r3, r3), ctx);
  350. emit(A64_CMP(0, r3, tmp), ctx);
  351. emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
  352. /*
  353. * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
  354. * goto out;
  355. * tail_call_cnt++;
  356. */
  357. emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
  358. emit(A64_CMP(1, tcc, tmp), ctx);
  359. emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
  360. emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
  361. /* prog = array->ptrs[index];
  362. * if (prog == NULL)
  363. * goto out;
  364. */
  365. off = offsetof(struct bpf_array, ptrs);
  366. emit_a64_mov_i64(tmp, off, ctx);
  367. emit(A64_ADD(1, tmp, r2, tmp), ctx);
  368. emit(A64_LSL(1, prg, r3, 3), ctx);
  369. emit(A64_LDR64(prg, tmp, prg), ctx);
  370. emit(A64_CBZ(1, prg, jmp_offset), ctx);
  371. /* goto *(prog->bpf_func + prologue_offset); */
  372. off = offsetof(struct bpf_prog, bpf_func);
  373. emit_a64_mov_i64(tmp, off, ctx);
  374. emit(A64_LDR64(tmp, prg, tmp), ctx);
  375. emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
  376. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  377. emit(A64_BR(tmp), ctx);
  378. /* out: */
  379. if (out_offset == -1)
  380. out_offset = cur_offset;
  381. if (cur_offset != out_offset) {
  382. pr_err_once("tail_call out_offset = %d, expected %d!\n",
  383. cur_offset, out_offset);
  384. return -1;
  385. }
  386. return 0;
  387. #undef cur_offset
  388. #undef jmp_offset
  389. }
  390. #ifdef CONFIG_ARM64_LSE_ATOMICS
  391. static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  392. {
  393. const u8 code = insn->code;
  394. const u8 dst = bpf2a64[insn->dst_reg];
  395. const u8 src = bpf2a64[insn->src_reg];
  396. const u8 tmp = bpf2a64[TMP_REG_1];
  397. const u8 tmp2 = bpf2a64[TMP_REG_2];
  398. const bool isdw = BPF_SIZE(code) == BPF_DW;
  399. const s16 off = insn->off;
  400. u8 reg;
  401. if (!off) {
  402. reg = dst;
  403. } else {
  404. emit_a64_mov_i(1, tmp, off, ctx);
  405. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  406. reg = tmp;
  407. }
  408. switch (insn->imm) {
  409. /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
  410. case BPF_ADD:
  411. emit(A64_STADD(isdw, reg, src), ctx);
  412. break;
  413. case BPF_AND:
  414. emit(A64_MVN(isdw, tmp2, src), ctx);
  415. emit(A64_STCLR(isdw, reg, tmp2), ctx);
  416. break;
  417. case BPF_OR:
  418. emit(A64_STSET(isdw, reg, src), ctx);
  419. break;
  420. case BPF_XOR:
  421. emit(A64_STEOR(isdw, reg, src), ctx);
  422. break;
  423. /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
  424. case BPF_ADD | BPF_FETCH:
  425. emit(A64_LDADDAL(isdw, src, reg, src), ctx);
  426. break;
  427. case BPF_AND | BPF_FETCH:
  428. emit(A64_MVN(isdw, tmp2, src), ctx);
  429. emit(A64_LDCLRAL(isdw, src, reg, tmp2), ctx);
  430. break;
  431. case BPF_OR | BPF_FETCH:
  432. emit(A64_LDSETAL(isdw, src, reg, src), ctx);
  433. break;
  434. case BPF_XOR | BPF_FETCH:
  435. emit(A64_LDEORAL(isdw, src, reg, src), ctx);
  436. break;
  437. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  438. case BPF_XCHG:
  439. emit(A64_SWPAL(isdw, src, reg, src), ctx);
  440. break;
  441. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  442. case BPF_CMPXCHG:
  443. emit(A64_CASAL(isdw, src, reg, bpf2a64[BPF_REG_0]), ctx);
  444. break;
  445. default:
  446. pr_err_once("unknown atomic op code %02x\n", insn->imm);
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. #else
  452. static inline int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  453. {
  454. return -EINVAL;
  455. }
  456. #endif
  457. static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
  458. {
  459. const u8 code = insn->code;
  460. const u8 dst = bpf2a64[insn->dst_reg];
  461. const u8 src = bpf2a64[insn->src_reg];
  462. const u8 tmp = bpf2a64[TMP_REG_1];
  463. const u8 tmp2 = bpf2a64[TMP_REG_2];
  464. const u8 tmp3 = bpf2a64[TMP_REG_3];
  465. const int i = insn - ctx->prog->insnsi;
  466. const s32 imm = insn->imm;
  467. const s16 off = insn->off;
  468. const bool isdw = BPF_SIZE(code) == BPF_DW;
  469. u8 reg;
  470. s32 jmp_offset;
  471. if (!off) {
  472. reg = dst;
  473. } else {
  474. emit_a64_mov_i(1, tmp, off, ctx);
  475. emit(A64_ADD(1, tmp, tmp, dst), ctx);
  476. reg = tmp;
  477. }
  478. if (imm == BPF_ADD || imm == BPF_AND ||
  479. imm == BPF_OR || imm == BPF_XOR) {
  480. /* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
  481. emit(A64_LDXR(isdw, tmp2, reg), ctx);
  482. if (imm == BPF_ADD)
  483. emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
  484. else if (imm == BPF_AND)
  485. emit(A64_AND(isdw, tmp2, tmp2, src), ctx);
  486. else if (imm == BPF_OR)
  487. emit(A64_ORR(isdw, tmp2, tmp2, src), ctx);
  488. else
  489. emit(A64_EOR(isdw, tmp2, tmp2, src), ctx);
  490. emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
  491. jmp_offset = -3;
  492. check_imm19(jmp_offset);
  493. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  494. } else if (imm == (BPF_ADD | BPF_FETCH) ||
  495. imm == (BPF_AND | BPF_FETCH) ||
  496. imm == (BPF_OR | BPF_FETCH) ||
  497. imm == (BPF_XOR | BPF_FETCH)) {
  498. /* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
  499. const u8 ax = bpf2a64[BPF_REG_AX];
  500. emit(A64_MOV(isdw, ax, src), ctx);
  501. emit(A64_LDXR(isdw, src, reg), ctx);
  502. if (imm == (BPF_ADD | BPF_FETCH))
  503. emit(A64_ADD(isdw, tmp2, src, ax), ctx);
  504. else if (imm == (BPF_AND | BPF_FETCH))
  505. emit(A64_AND(isdw, tmp2, src, ax), ctx);
  506. else if (imm == (BPF_OR | BPF_FETCH))
  507. emit(A64_ORR(isdw, tmp2, src, ax), ctx);
  508. else
  509. emit(A64_EOR(isdw, tmp2, src, ax), ctx);
  510. emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
  511. jmp_offset = -3;
  512. check_imm19(jmp_offset);
  513. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  514. emit(A64_DMB_ISH, ctx);
  515. } else if (imm == BPF_XCHG) {
  516. /* src_reg = atomic_xchg(dst_reg + off, src_reg); */
  517. emit(A64_MOV(isdw, tmp2, src), ctx);
  518. emit(A64_LDXR(isdw, src, reg), ctx);
  519. emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
  520. jmp_offset = -2;
  521. check_imm19(jmp_offset);
  522. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  523. emit(A64_DMB_ISH, ctx);
  524. } else if (imm == BPF_CMPXCHG) {
  525. /* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
  526. const u8 r0 = bpf2a64[BPF_REG_0];
  527. emit(A64_MOV(isdw, tmp2, r0), ctx);
  528. emit(A64_LDXR(isdw, r0, reg), ctx);
  529. emit(A64_EOR(isdw, tmp3, r0, tmp2), ctx);
  530. jmp_offset = 4;
  531. check_imm19(jmp_offset);
  532. emit(A64_CBNZ(isdw, tmp3, jmp_offset), ctx);
  533. emit(A64_STLXR(isdw, src, reg, tmp3), ctx);
  534. jmp_offset = -4;
  535. check_imm19(jmp_offset);
  536. emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
  537. emit(A64_DMB_ISH, ctx);
  538. } else {
  539. pr_err_once("unknown atomic op code %02x\n", imm);
  540. return -EINVAL;
  541. }
  542. return 0;
  543. }
  544. void dummy_tramp(void);
  545. asm (
  546. " .pushsection .text, \"ax\", @progbits\n"
  547. " .global dummy_tramp\n"
  548. " .type dummy_tramp, %function\n"
  549. "dummy_tramp:"
  550. #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
  551. " bti j\n" /* dummy_tramp is called via "br x10" */
  552. #endif
  553. " mov x10, x30\n"
  554. " mov x30, x9\n"
  555. " ret x10\n"
  556. " .size dummy_tramp, .-dummy_tramp\n"
  557. " .popsection\n"
  558. );
  559. /* build a plt initialized like this:
  560. *
  561. * plt:
  562. * ldr tmp, target
  563. * br tmp
  564. * target:
  565. * .quad dummy_tramp
  566. *
  567. * when a long jump trampoline is attached, target is filled with the
  568. * trampoline address, and when the trampoline is removed, target is
  569. * restored to dummy_tramp address.
  570. */
  571. static void build_plt(struct jit_ctx *ctx)
  572. {
  573. const u8 tmp = bpf2a64[TMP_REG_1];
  574. struct bpf_plt *plt = NULL;
  575. /* make sure target is 64-bit aligned */
  576. if ((ctx->idx + PLT_TARGET_OFFSET / AARCH64_INSN_SIZE) % 2)
  577. emit(A64_NOP, ctx);
  578. plt = (struct bpf_plt *)(ctx->image + ctx->idx);
  579. /* plt is called via bl, no BTI needed here */
  580. emit(A64_LDR64LIT(tmp, 2 * AARCH64_INSN_SIZE), ctx);
  581. emit(A64_BR(tmp), ctx);
  582. if (ctx->image)
  583. plt->target = (u64)&dummy_tramp;
  584. }
  585. static void build_epilogue(struct jit_ctx *ctx)
  586. {
  587. const u8 r0 = bpf2a64[BPF_REG_0];
  588. const u8 r6 = bpf2a64[BPF_REG_6];
  589. const u8 r7 = bpf2a64[BPF_REG_7];
  590. const u8 r8 = bpf2a64[BPF_REG_8];
  591. const u8 r9 = bpf2a64[BPF_REG_9];
  592. const u8 fp = bpf2a64[BPF_REG_FP];
  593. const u8 fpb = bpf2a64[FP_BOTTOM];
  594. /* We're done with BPF stack */
  595. emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
  596. /* Restore x27 and x28 */
  597. emit(A64_POP(fpb, A64_R(28), A64_SP), ctx);
  598. /* Restore fs (x25) and x26 */
  599. emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
  600. /* Restore callee-saved register */
  601. emit(A64_POP(r8, r9, A64_SP), ctx);
  602. emit(A64_POP(r6, r7, A64_SP), ctx);
  603. /* Restore FP/LR registers */
  604. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  605. /* Set return value */
  606. emit(A64_MOV(1, A64_R(0), r0), ctx);
  607. /* Authenticate lr */
  608. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  609. emit(A64_AUTIASP, ctx);
  610. emit(A64_RET(A64_LR), ctx);
  611. }
  612. #define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
  613. #define BPF_FIXUP_REG_MASK GENMASK(31, 27)
  614. bool ex_handler_bpf(const struct exception_table_entry *ex,
  615. struct pt_regs *regs)
  616. {
  617. off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
  618. int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
  619. regs->regs[dst_reg] = 0;
  620. regs->pc = (unsigned long)&ex->fixup - offset;
  621. return true;
  622. }
  623. /* For accesses to BTF pointers, add an entry to the exception table */
  624. static int add_exception_handler(const struct bpf_insn *insn,
  625. struct jit_ctx *ctx,
  626. int dst_reg)
  627. {
  628. off_t offset;
  629. unsigned long pc;
  630. struct exception_table_entry *ex;
  631. if (!ctx->image)
  632. /* First pass */
  633. return 0;
  634. if (BPF_MODE(insn->code) != BPF_PROBE_MEM)
  635. return 0;
  636. if (!ctx->prog->aux->extable ||
  637. WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
  638. return -EINVAL;
  639. ex = &ctx->prog->aux->extable[ctx->exentry_idx];
  640. pc = (unsigned long)&ctx->image[ctx->idx - 1];
  641. offset = pc - (long)&ex->insn;
  642. if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
  643. return -ERANGE;
  644. ex->insn = offset;
  645. /*
  646. * Since the extable follows the program, the fixup offset is always
  647. * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
  648. * to keep things simple, and put the destination register in the upper
  649. * bits. We don't need to worry about buildtime or runtime sort
  650. * modifying the upper bits because the table is already sorted, and
  651. * isn't part of the main exception table.
  652. */
  653. offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
  654. if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
  655. return -ERANGE;
  656. ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
  657. FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
  658. ex->type = EX_TYPE_BPF;
  659. ctx->exentry_idx++;
  660. return 0;
  661. }
  662. /* JITs an eBPF instruction.
  663. * Returns:
  664. * 0 - successfully JITed an 8-byte eBPF instruction.
  665. * >0 - successfully JITed a 16-byte eBPF instruction.
  666. * <0 - failed to JIT.
  667. */
  668. static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
  669. bool extra_pass)
  670. {
  671. const u8 code = insn->code;
  672. const u8 dst = bpf2a64[insn->dst_reg];
  673. const u8 src = bpf2a64[insn->src_reg];
  674. const u8 tmp = bpf2a64[TMP_REG_1];
  675. const u8 tmp2 = bpf2a64[TMP_REG_2];
  676. const u8 fp = bpf2a64[BPF_REG_FP];
  677. const u8 fpb = bpf2a64[FP_BOTTOM];
  678. const s16 off = insn->off;
  679. const s32 imm = insn->imm;
  680. const int i = insn - ctx->prog->insnsi;
  681. const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
  682. BPF_CLASS(code) == BPF_JMP;
  683. u8 jmp_cond;
  684. s32 jmp_offset;
  685. u32 a64_insn;
  686. u8 src_adj;
  687. u8 dst_adj;
  688. int off_adj;
  689. int ret;
  690. switch (code) {
  691. /* dst = src */
  692. case BPF_ALU | BPF_MOV | BPF_X:
  693. case BPF_ALU64 | BPF_MOV | BPF_X:
  694. emit(A64_MOV(is64, dst, src), ctx);
  695. break;
  696. /* dst = dst OP src */
  697. case BPF_ALU | BPF_ADD | BPF_X:
  698. case BPF_ALU64 | BPF_ADD | BPF_X:
  699. emit(A64_ADD(is64, dst, dst, src), ctx);
  700. break;
  701. case BPF_ALU | BPF_SUB | BPF_X:
  702. case BPF_ALU64 | BPF_SUB | BPF_X:
  703. emit(A64_SUB(is64, dst, dst, src), ctx);
  704. break;
  705. case BPF_ALU | BPF_AND | BPF_X:
  706. case BPF_ALU64 | BPF_AND | BPF_X:
  707. emit(A64_AND(is64, dst, dst, src), ctx);
  708. break;
  709. case BPF_ALU | BPF_OR | BPF_X:
  710. case BPF_ALU64 | BPF_OR | BPF_X:
  711. emit(A64_ORR(is64, dst, dst, src), ctx);
  712. break;
  713. case BPF_ALU | BPF_XOR | BPF_X:
  714. case BPF_ALU64 | BPF_XOR | BPF_X:
  715. emit(A64_EOR(is64, dst, dst, src), ctx);
  716. break;
  717. case BPF_ALU | BPF_MUL | BPF_X:
  718. case BPF_ALU64 | BPF_MUL | BPF_X:
  719. emit(A64_MUL(is64, dst, dst, src), ctx);
  720. break;
  721. case BPF_ALU | BPF_DIV | BPF_X:
  722. case BPF_ALU64 | BPF_DIV | BPF_X:
  723. emit(A64_UDIV(is64, dst, dst, src), ctx);
  724. break;
  725. case BPF_ALU | BPF_MOD | BPF_X:
  726. case BPF_ALU64 | BPF_MOD | BPF_X:
  727. emit(A64_UDIV(is64, tmp, dst, src), ctx);
  728. emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
  729. break;
  730. case BPF_ALU | BPF_LSH | BPF_X:
  731. case BPF_ALU64 | BPF_LSH | BPF_X:
  732. emit(A64_LSLV(is64, dst, dst, src), ctx);
  733. break;
  734. case BPF_ALU | BPF_RSH | BPF_X:
  735. case BPF_ALU64 | BPF_RSH | BPF_X:
  736. emit(A64_LSRV(is64, dst, dst, src), ctx);
  737. break;
  738. case BPF_ALU | BPF_ARSH | BPF_X:
  739. case BPF_ALU64 | BPF_ARSH | BPF_X:
  740. emit(A64_ASRV(is64, dst, dst, src), ctx);
  741. break;
  742. /* dst = -dst */
  743. case BPF_ALU | BPF_NEG:
  744. case BPF_ALU64 | BPF_NEG:
  745. emit(A64_NEG(is64, dst, dst), ctx);
  746. break;
  747. /* dst = BSWAP##imm(dst) */
  748. case BPF_ALU | BPF_END | BPF_FROM_LE:
  749. case BPF_ALU | BPF_END | BPF_FROM_BE:
  750. #ifdef CONFIG_CPU_BIG_ENDIAN
  751. if (BPF_SRC(code) == BPF_FROM_BE)
  752. goto emit_bswap_uxt;
  753. #else /* !CONFIG_CPU_BIG_ENDIAN */
  754. if (BPF_SRC(code) == BPF_FROM_LE)
  755. goto emit_bswap_uxt;
  756. #endif
  757. switch (imm) {
  758. case 16:
  759. emit(A64_REV16(is64, dst, dst), ctx);
  760. /* zero-extend 16 bits into 64 bits */
  761. emit(A64_UXTH(is64, dst, dst), ctx);
  762. break;
  763. case 32:
  764. emit(A64_REV32(is64, dst, dst), ctx);
  765. /* upper 32 bits already cleared */
  766. break;
  767. case 64:
  768. emit(A64_REV64(dst, dst), ctx);
  769. break;
  770. }
  771. break;
  772. emit_bswap_uxt:
  773. switch (imm) {
  774. case 16:
  775. /* zero-extend 16 bits into 64 bits */
  776. emit(A64_UXTH(is64, dst, dst), ctx);
  777. break;
  778. case 32:
  779. /* zero-extend 32 bits into 64 bits */
  780. emit(A64_UXTW(is64, dst, dst), ctx);
  781. break;
  782. case 64:
  783. /* nop */
  784. break;
  785. }
  786. break;
  787. /* dst = imm */
  788. case BPF_ALU | BPF_MOV | BPF_K:
  789. case BPF_ALU64 | BPF_MOV | BPF_K:
  790. emit_a64_mov_i(is64, dst, imm, ctx);
  791. break;
  792. /* dst = dst OP imm */
  793. case BPF_ALU | BPF_ADD | BPF_K:
  794. case BPF_ALU64 | BPF_ADD | BPF_K:
  795. if (is_addsub_imm(imm)) {
  796. emit(A64_ADD_I(is64, dst, dst, imm), ctx);
  797. } else if (is_addsub_imm(-imm)) {
  798. emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
  799. } else {
  800. emit_a64_mov_i(is64, tmp, imm, ctx);
  801. emit(A64_ADD(is64, dst, dst, tmp), ctx);
  802. }
  803. break;
  804. case BPF_ALU | BPF_SUB | BPF_K:
  805. case BPF_ALU64 | BPF_SUB | BPF_K:
  806. if (is_addsub_imm(imm)) {
  807. emit(A64_SUB_I(is64, dst, dst, imm), ctx);
  808. } else if (is_addsub_imm(-imm)) {
  809. emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
  810. } else {
  811. emit_a64_mov_i(is64, tmp, imm, ctx);
  812. emit(A64_SUB(is64, dst, dst, tmp), ctx);
  813. }
  814. break;
  815. case BPF_ALU | BPF_AND | BPF_K:
  816. case BPF_ALU64 | BPF_AND | BPF_K:
  817. a64_insn = A64_AND_I(is64, dst, dst, imm);
  818. if (a64_insn != AARCH64_BREAK_FAULT) {
  819. emit(a64_insn, ctx);
  820. } else {
  821. emit_a64_mov_i(is64, tmp, imm, ctx);
  822. emit(A64_AND(is64, dst, dst, tmp), ctx);
  823. }
  824. break;
  825. case BPF_ALU | BPF_OR | BPF_K:
  826. case BPF_ALU64 | BPF_OR | BPF_K:
  827. a64_insn = A64_ORR_I(is64, dst, dst, imm);
  828. if (a64_insn != AARCH64_BREAK_FAULT) {
  829. emit(a64_insn, ctx);
  830. } else {
  831. emit_a64_mov_i(is64, tmp, imm, ctx);
  832. emit(A64_ORR(is64, dst, dst, tmp), ctx);
  833. }
  834. break;
  835. case BPF_ALU | BPF_XOR | BPF_K:
  836. case BPF_ALU64 | BPF_XOR | BPF_K:
  837. a64_insn = A64_EOR_I(is64, dst, dst, imm);
  838. if (a64_insn != AARCH64_BREAK_FAULT) {
  839. emit(a64_insn, ctx);
  840. } else {
  841. emit_a64_mov_i(is64, tmp, imm, ctx);
  842. emit(A64_EOR(is64, dst, dst, tmp), ctx);
  843. }
  844. break;
  845. case BPF_ALU | BPF_MUL | BPF_K:
  846. case BPF_ALU64 | BPF_MUL | BPF_K:
  847. emit_a64_mov_i(is64, tmp, imm, ctx);
  848. emit(A64_MUL(is64, dst, dst, tmp), ctx);
  849. break;
  850. case BPF_ALU | BPF_DIV | BPF_K:
  851. case BPF_ALU64 | BPF_DIV | BPF_K:
  852. emit_a64_mov_i(is64, tmp, imm, ctx);
  853. emit(A64_UDIV(is64, dst, dst, tmp), ctx);
  854. break;
  855. case BPF_ALU | BPF_MOD | BPF_K:
  856. case BPF_ALU64 | BPF_MOD | BPF_K:
  857. emit_a64_mov_i(is64, tmp2, imm, ctx);
  858. emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
  859. emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
  860. break;
  861. case BPF_ALU | BPF_LSH | BPF_K:
  862. case BPF_ALU64 | BPF_LSH | BPF_K:
  863. emit(A64_LSL(is64, dst, dst, imm), ctx);
  864. break;
  865. case BPF_ALU | BPF_RSH | BPF_K:
  866. case BPF_ALU64 | BPF_RSH | BPF_K:
  867. emit(A64_LSR(is64, dst, dst, imm), ctx);
  868. break;
  869. case BPF_ALU | BPF_ARSH | BPF_K:
  870. case BPF_ALU64 | BPF_ARSH | BPF_K:
  871. emit(A64_ASR(is64, dst, dst, imm), ctx);
  872. break;
  873. /* JUMP off */
  874. case BPF_JMP | BPF_JA:
  875. jmp_offset = bpf2a64_offset(i, off, ctx);
  876. check_imm26(jmp_offset);
  877. emit(A64_B(jmp_offset), ctx);
  878. break;
  879. /* IF (dst COND src) JUMP off */
  880. case BPF_JMP | BPF_JEQ | BPF_X:
  881. case BPF_JMP | BPF_JGT | BPF_X:
  882. case BPF_JMP | BPF_JLT | BPF_X:
  883. case BPF_JMP | BPF_JGE | BPF_X:
  884. case BPF_JMP | BPF_JLE | BPF_X:
  885. case BPF_JMP | BPF_JNE | BPF_X:
  886. case BPF_JMP | BPF_JSGT | BPF_X:
  887. case BPF_JMP | BPF_JSLT | BPF_X:
  888. case BPF_JMP | BPF_JSGE | BPF_X:
  889. case BPF_JMP | BPF_JSLE | BPF_X:
  890. case BPF_JMP32 | BPF_JEQ | BPF_X:
  891. case BPF_JMP32 | BPF_JGT | BPF_X:
  892. case BPF_JMP32 | BPF_JLT | BPF_X:
  893. case BPF_JMP32 | BPF_JGE | BPF_X:
  894. case BPF_JMP32 | BPF_JLE | BPF_X:
  895. case BPF_JMP32 | BPF_JNE | BPF_X:
  896. case BPF_JMP32 | BPF_JSGT | BPF_X:
  897. case BPF_JMP32 | BPF_JSLT | BPF_X:
  898. case BPF_JMP32 | BPF_JSGE | BPF_X:
  899. case BPF_JMP32 | BPF_JSLE | BPF_X:
  900. emit(A64_CMP(is64, dst, src), ctx);
  901. emit_cond_jmp:
  902. jmp_offset = bpf2a64_offset(i, off, ctx);
  903. check_imm19(jmp_offset);
  904. switch (BPF_OP(code)) {
  905. case BPF_JEQ:
  906. jmp_cond = A64_COND_EQ;
  907. break;
  908. case BPF_JGT:
  909. jmp_cond = A64_COND_HI;
  910. break;
  911. case BPF_JLT:
  912. jmp_cond = A64_COND_CC;
  913. break;
  914. case BPF_JGE:
  915. jmp_cond = A64_COND_CS;
  916. break;
  917. case BPF_JLE:
  918. jmp_cond = A64_COND_LS;
  919. break;
  920. case BPF_JSET:
  921. case BPF_JNE:
  922. jmp_cond = A64_COND_NE;
  923. break;
  924. case BPF_JSGT:
  925. jmp_cond = A64_COND_GT;
  926. break;
  927. case BPF_JSLT:
  928. jmp_cond = A64_COND_LT;
  929. break;
  930. case BPF_JSGE:
  931. jmp_cond = A64_COND_GE;
  932. break;
  933. case BPF_JSLE:
  934. jmp_cond = A64_COND_LE;
  935. break;
  936. default:
  937. return -EFAULT;
  938. }
  939. emit(A64_B_(jmp_cond, jmp_offset), ctx);
  940. break;
  941. case BPF_JMP | BPF_JSET | BPF_X:
  942. case BPF_JMP32 | BPF_JSET | BPF_X:
  943. emit(A64_TST(is64, dst, src), ctx);
  944. goto emit_cond_jmp;
  945. /* IF (dst COND imm) JUMP off */
  946. case BPF_JMP | BPF_JEQ | BPF_K:
  947. case BPF_JMP | BPF_JGT | BPF_K:
  948. case BPF_JMP | BPF_JLT | BPF_K:
  949. case BPF_JMP | BPF_JGE | BPF_K:
  950. case BPF_JMP | BPF_JLE | BPF_K:
  951. case BPF_JMP | BPF_JNE | BPF_K:
  952. case BPF_JMP | BPF_JSGT | BPF_K:
  953. case BPF_JMP | BPF_JSLT | BPF_K:
  954. case BPF_JMP | BPF_JSGE | BPF_K:
  955. case BPF_JMP | BPF_JSLE | BPF_K:
  956. case BPF_JMP32 | BPF_JEQ | BPF_K:
  957. case BPF_JMP32 | BPF_JGT | BPF_K:
  958. case BPF_JMP32 | BPF_JLT | BPF_K:
  959. case BPF_JMP32 | BPF_JGE | BPF_K:
  960. case BPF_JMP32 | BPF_JLE | BPF_K:
  961. case BPF_JMP32 | BPF_JNE | BPF_K:
  962. case BPF_JMP32 | BPF_JSGT | BPF_K:
  963. case BPF_JMP32 | BPF_JSLT | BPF_K:
  964. case BPF_JMP32 | BPF_JSGE | BPF_K:
  965. case BPF_JMP32 | BPF_JSLE | BPF_K:
  966. if (is_addsub_imm(imm)) {
  967. emit(A64_CMP_I(is64, dst, imm), ctx);
  968. } else if (is_addsub_imm(-imm)) {
  969. emit(A64_CMN_I(is64, dst, -imm), ctx);
  970. } else {
  971. emit_a64_mov_i(is64, tmp, imm, ctx);
  972. emit(A64_CMP(is64, dst, tmp), ctx);
  973. }
  974. goto emit_cond_jmp;
  975. case BPF_JMP | BPF_JSET | BPF_K:
  976. case BPF_JMP32 | BPF_JSET | BPF_K:
  977. a64_insn = A64_TST_I(is64, dst, imm);
  978. if (a64_insn != AARCH64_BREAK_FAULT) {
  979. emit(a64_insn, ctx);
  980. } else {
  981. emit_a64_mov_i(is64, tmp, imm, ctx);
  982. emit(A64_TST(is64, dst, tmp), ctx);
  983. }
  984. goto emit_cond_jmp;
  985. /* function call */
  986. case BPF_JMP | BPF_CALL:
  987. {
  988. const u8 r0 = bpf2a64[BPF_REG_0];
  989. bool func_addr_fixed;
  990. u64 func_addr;
  991. ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
  992. &func_addr, &func_addr_fixed);
  993. if (ret < 0)
  994. return ret;
  995. emit_call(func_addr, ctx);
  996. emit(A64_MOV(1, r0, A64_R(0)), ctx);
  997. break;
  998. }
  999. /* tail call */
  1000. case BPF_JMP | BPF_TAIL_CALL:
  1001. if (emit_bpf_tail_call(ctx))
  1002. return -EFAULT;
  1003. break;
  1004. /* function return */
  1005. case BPF_JMP | BPF_EXIT:
  1006. /* Optimization: when last instruction is EXIT,
  1007. simply fallthrough to epilogue. */
  1008. if (i == ctx->prog->len - 1)
  1009. break;
  1010. jmp_offset = epilogue_offset(ctx);
  1011. check_imm26(jmp_offset);
  1012. emit(A64_B(jmp_offset), ctx);
  1013. break;
  1014. /* dst = imm64 */
  1015. case BPF_LD | BPF_IMM | BPF_DW:
  1016. {
  1017. const struct bpf_insn insn1 = insn[1];
  1018. u64 imm64;
  1019. imm64 = (u64)insn1.imm << 32 | (u32)imm;
  1020. if (bpf_pseudo_func(insn))
  1021. emit_addr_mov_i64(dst, imm64, ctx);
  1022. else
  1023. emit_a64_mov_i64(dst, imm64, ctx);
  1024. return 1;
  1025. }
  1026. /* LDX: dst = *(size *)(src + off) */
  1027. case BPF_LDX | BPF_MEM | BPF_W:
  1028. case BPF_LDX | BPF_MEM | BPF_H:
  1029. case BPF_LDX | BPF_MEM | BPF_B:
  1030. case BPF_LDX | BPF_MEM | BPF_DW:
  1031. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  1032. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  1033. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  1034. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  1035. if (ctx->fpb_offset > 0 && src == fp) {
  1036. src_adj = fpb;
  1037. off_adj = off + ctx->fpb_offset;
  1038. } else {
  1039. src_adj = src;
  1040. off_adj = off;
  1041. }
  1042. switch (BPF_SIZE(code)) {
  1043. case BPF_W:
  1044. if (is_lsi_offset(off_adj, 2)) {
  1045. emit(A64_LDR32I(dst, src_adj, off_adj), ctx);
  1046. } else {
  1047. emit_a64_mov_i(1, tmp, off, ctx);
  1048. emit(A64_LDR32(dst, src, tmp), ctx);
  1049. }
  1050. break;
  1051. case BPF_H:
  1052. if (is_lsi_offset(off_adj, 1)) {
  1053. emit(A64_LDRHI(dst, src_adj, off_adj), ctx);
  1054. } else {
  1055. emit_a64_mov_i(1, tmp, off, ctx);
  1056. emit(A64_LDRH(dst, src, tmp), ctx);
  1057. }
  1058. break;
  1059. case BPF_B:
  1060. if (is_lsi_offset(off_adj, 0)) {
  1061. emit(A64_LDRBI(dst, src_adj, off_adj), ctx);
  1062. } else {
  1063. emit_a64_mov_i(1, tmp, off, ctx);
  1064. emit(A64_LDRB(dst, src, tmp), ctx);
  1065. }
  1066. break;
  1067. case BPF_DW:
  1068. if (is_lsi_offset(off_adj, 3)) {
  1069. emit(A64_LDR64I(dst, src_adj, off_adj), ctx);
  1070. } else {
  1071. emit_a64_mov_i(1, tmp, off, ctx);
  1072. emit(A64_LDR64(dst, src, tmp), ctx);
  1073. }
  1074. break;
  1075. }
  1076. ret = add_exception_handler(insn, ctx, dst);
  1077. if (ret)
  1078. return ret;
  1079. break;
  1080. /* speculation barrier */
  1081. case BPF_ST | BPF_NOSPEC:
  1082. /*
  1083. * Nothing required here.
  1084. *
  1085. * In case of arm64, we rely on the firmware mitigation of
  1086. * Speculative Store Bypass as controlled via the ssbd kernel
  1087. * parameter. Whenever the mitigation is enabled, it works
  1088. * for all of the kernel code with no need to provide any
  1089. * additional instructions.
  1090. */
  1091. break;
  1092. /* ST: *(size *)(dst + off) = imm */
  1093. case BPF_ST | BPF_MEM | BPF_W:
  1094. case BPF_ST | BPF_MEM | BPF_H:
  1095. case BPF_ST | BPF_MEM | BPF_B:
  1096. case BPF_ST | BPF_MEM | BPF_DW:
  1097. if (ctx->fpb_offset > 0 && dst == fp) {
  1098. dst_adj = fpb;
  1099. off_adj = off + ctx->fpb_offset;
  1100. } else {
  1101. dst_adj = dst;
  1102. off_adj = off;
  1103. }
  1104. /* Load imm to a register then store it */
  1105. emit_a64_mov_i(1, tmp, imm, ctx);
  1106. switch (BPF_SIZE(code)) {
  1107. case BPF_W:
  1108. if (is_lsi_offset(off_adj, 2)) {
  1109. emit(A64_STR32I(tmp, dst_adj, off_adj), ctx);
  1110. } else {
  1111. emit_a64_mov_i(1, tmp2, off, ctx);
  1112. emit(A64_STR32(tmp, dst, tmp2), ctx);
  1113. }
  1114. break;
  1115. case BPF_H:
  1116. if (is_lsi_offset(off_adj, 1)) {
  1117. emit(A64_STRHI(tmp, dst_adj, off_adj), ctx);
  1118. } else {
  1119. emit_a64_mov_i(1, tmp2, off, ctx);
  1120. emit(A64_STRH(tmp, dst, tmp2), ctx);
  1121. }
  1122. break;
  1123. case BPF_B:
  1124. if (is_lsi_offset(off_adj, 0)) {
  1125. emit(A64_STRBI(tmp, dst_adj, off_adj), ctx);
  1126. } else {
  1127. emit_a64_mov_i(1, tmp2, off, ctx);
  1128. emit(A64_STRB(tmp, dst, tmp2), ctx);
  1129. }
  1130. break;
  1131. case BPF_DW:
  1132. if (is_lsi_offset(off_adj, 3)) {
  1133. emit(A64_STR64I(tmp, dst_adj, off_adj), ctx);
  1134. } else {
  1135. emit_a64_mov_i(1, tmp2, off, ctx);
  1136. emit(A64_STR64(tmp, dst, tmp2), ctx);
  1137. }
  1138. break;
  1139. }
  1140. break;
  1141. /* STX: *(size *)(dst + off) = src */
  1142. case BPF_STX | BPF_MEM | BPF_W:
  1143. case BPF_STX | BPF_MEM | BPF_H:
  1144. case BPF_STX | BPF_MEM | BPF_B:
  1145. case BPF_STX | BPF_MEM | BPF_DW:
  1146. if (ctx->fpb_offset > 0 && dst == fp) {
  1147. dst_adj = fpb;
  1148. off_adj = off + ctx->fpb_offset;
  1149. } else {
  1150. dst_adj = dst;
  1151. off_adj = off;
  1152. }
  1153. switch (BPF_SIZE(code)) {
  1154. case BPF_W:
  1155. if (is_lsi_offset(off_adj, 2)) {
  1156. emit(A64_STR32I(src, dst_adj, off_adj), ctx);
  1157. } else {
  1158. emit_a64_mov_i(1, tmp, off, ctx);
  1159. emit(A64_STR32(src, dst, tmp), ctx);
  1160. }
  1161. break;
  1162. case BPF_H:
  1163. if (is_lsi_offset(off_adj, 1)) {
  1164. emit(A64_STRHI(src, dst_adj, off_adj), ctx);
  1165. } else {
  1166. emit_a64_mov_i(1, tmp, off, ctx);
  1167. emit(A64_STRH(src, dst, tmp), ctx);
  1168. }
  1169. break;
  1170. case BPF_B:
  1171. if (is_lsi_offset(off_adj, 0)) {
  1172. emit(A64_STRBI(src, dst_adj, off_adj), ctx);
  1173. } else {
  1174. emit_a64_mov_i(1, tmp, off, ctx);
  1175. emit(A64_STRB(src, dst, tmp), ctx);
  1176. }
  1177. break;
  1178. case BPF_DW:
  1179. if (is_lsi_offset(off_adj, 3)) {
  1180. emit(A64_STR64I(src, dst_adj, off_adj), ctx);
  1181. } else {
  1182. emit_a64_mov_i(1, tmp, off, ctx);
  1183. emit(A64_STR64(src, dst, tmp), ctx);
  1184. }
  1185. break;
  1186. }
  1187. break;
  1188. case BPF_STX | BPF_ATOMIC | BPF_W:
  1189. case BPF_STX | BPF_ATOMIC | BPF_DW:
  1190. if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
  1191. ret = emit_lse_atomic(insn, ctx);
  1192. else
  1193. ret = emit_ll_sc_atomic(insn, ctx);
  1194. if (ret)
  1195. return ret;
  1196. break;
  1197. default:
  1198. pr_err_once("unknown opcode %02x\n", code);
  1199. return -EINVAL;
  1200. }
  1201. return 0;
  1202. }
  1203. /*
  1204. * Return 0 if FP may change at runtime, otherwise find the minimum negative
  1205. * offset to FP, converts it to positive number, and align down to 8 bytes.
  1206. */
  1207. static int find_fpb_offset(struct bpf_prog *prog)
  1208. {
  1209. int i;
  1210. int offset = 0;
  1211. for (i = 0; i < prog->len; i++) {
  1212. const struct bpf_insn *insn = &prog->insnsi[i];
  1213. const u8 class = BPF_CLASS(insn->code);
  1214. const u8 mode = BPF_MODE(insn->code);
  1215. const u8 src = insn->src_reg;
  1216. const u8 dst = insn->dst_reg;
  1217. const s32 imm = insn->imm;
  1218. const s16 off = insn->off;
  1219. switch (class) {
  1220. case BPF_STX:
  1221. case BPF_ST:
  1222. /* fp holds atomic operation result */
  1223. if (class == BPF_STX && mode == BPF_ATOMIC &&
  1224. ((imm == BPF_XCHG ||
  1225. imm == (BPF_FETCH | BPF_ADD) ||
  1226. imm == (BPF_FETCH | BPF_AND) ||
  1227. imm == (BPF_FETCH | BPF_XOR) ||
  1228. imm == (BPF_FETCH | BPF_OR)) &&
  1229. src == BPF_REG_FP))
  1230. return 0;
  1231. if (mode == BPF_MEM && dst == BPF_REG_FP &&
  1232. off < offset)
  1233. offset = insn->off;
  1234. break;
  1235. case BPF_JMP32:
  1236. case BPF_JMP:
  1237. break;
  1238. case BPF_LDX:
  1239. case BPF_LD:
  1240. /* fp holds load result */
  1241. if (dst == BPF_REG_FP)
  1242. return 0;
  1243. if (class == BPF_LDX && mode == BPF_MEM &&
  1244. src == BPF_REG_FP && off < offset)
  1245. offset = off;
  1246. break;
  1247. case BPF_ALU:
  1248. case BPF_ALU64:
  1249. default:
  1250. /* fp holds ALU result */
  1251. if (dst == BPF_REG_FP)
  1252. return 0;
  1253. }
  1254. }
  1255. if (offset < 0) {
  1256. /*
  1257. * safely be converted to a positive 'int', since insn->off
  1258. * is 's16'
  1259. */
  1260. offset = -offset;
  1261. /* align down to 8 bytes */
  1262. offset = ALIGN_DOWN(offset, 8);
  1263. }
  1264. return offset;
  1265. }
  1266. static int build_body(struct jit_ctx *ctx, bool extra_pass)
  1267. {
  1268. const struct bpf_prog *prog = ctx->prog;
  1269. int i;
  1270. /*
  1271. * - offset[0] offset of the end of prologue,
  1272. * start of the 1st instruction.
  1273. * - offset[1] - offset of the end of 1st instruction,
  1274. * start of the 2nd instruction
  1275. * [....]
  1276. * - offset[3] - offset of the end of 3rd instruction,
  1277. * start of 4th instruction
  1278. */
  1279. for (i = 0; i < prog->len; i++) {
  1280. const struct bpf_insn *insn = &prog->insnsi[i];
  1281. int ret;
  1282. if (ctx->image == NULL)
  1283. ctx->offset[i] = ctx->idx;
  1284. ret = build_insn(insn, ctx, extra_pass);
  1285. if (ret > 0) {
  1286. i++;
  1287. if (ctx->image == NULL)
  1288. ctx->offset[i] = ctx->idx;
  1289. continue;
  1290. }
  1291. if (ret)
  1292. return ret;
  1293. }
  1294. /*
  1295. * offset is allocated with prog->len + 1 so fill in
  1296. * the last element with the offset after the last
  1297. * instruction (end of program)
  1298. */
  1299. if (ctx->image == NULL)
  1300. ctx->offset[i] = ctx->idx;
  1301. return 0;
  1302. }
  1303. static int validate_code(struct jit_ctx *ctx)
  1304. {
  1305. int i;
  1306. for (i = 0; i < ctx->idx; i++) {
  1307. u32 a64_insn = le32_to_cpu(ctx->image[i]);
  1308. if (a64_insn == AARCH64_BREAK_FAULT)
  1309. return -1;
  1310. }
  1311. return 0;
  1312. }
  1313. static int validate_ctx(struct jit_ctx *ctx)
  1314. {
  1315. if (validate_code(ctx))
  1316. return -1;
  1317. if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
  1318. return -1;
  1319. return 0;
  1320. }
  1321. static inline void bpf_flush_icache(void *start, void *end)
  1322. {
  1323. flush_icache_range((unsigned long)start, (unsigned long)end);
  1324. }
  1325. struct arm64_jit_data {
  1326. struct bpf_binary_header *header;
  1327. u8 *image;
  1328. struct jit_ctx ctx;
  1329. };
  1330. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  1331. {
  1332. int image_size, prog_size, extable_size, extable_align, extable_offset;
  1333. struct bpf_prog *tmp, *orig_prog = prog;
  1334. struct bpf_binary_header *header;
  1335. struct arm64_jit_data *jit_data;
  1336. bool was_classic = bpf_prog_was_classic(prog);
  1337. bool tmp_blinded = false;
  1338. bool extra_pass = false;
  1339. struct jit_ctx ctx;
  1340. u8 *image_ptr;
  1341. if (!prog->jit_requested)
  1342. return orig_prog;
  1343. tmp = bpf_jit_blind_constants(prog);
  1344. /* If blinding was requested and we failed during blinding,
  1345. * we must fall back to the interpreter.
  1346. */
  1347. if (IS_ERR(tmp))
  1348. return orig_prog;
  1349. if (tmp != prog) {
  1350. tmp_blinded = true;
  1351. prog = tmp;
  1352. }
  1353. jit_data = prog->aux->jit_data;
  1354. if (!jit_data) {
  1355. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  1356. if (!jit_data) {
  1357. prog = orig_prog;
  1358. goto out;
  1359. }
  1360. prog->aux->jit_data = jit_data;
  1361. }
  1362. if (jit_data->ctx.offset) {
  1363. ctx = jit_data->ctx;
  1364. image_ptr = jit_data->image;
  1365. header = jit_data->header;
  1366. extra_pass = true;
  1367. prog_size = sizeof(u32) * ctx.idx;
  1368. goto skip_init_ctx;
  1369. }
  1370. memset(&ctx, 0, sizeof(ctx));
  1371. ctx.prog = prog;
  1372. ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
  1373. if (ctx.offset == NULL) {
  1374. prog = orig_prog;
  1375. goto out_off;
  1376. }
  1377. ctx.fpb_offset = find_fpb_offset(prog);
  1378. /*
  1379. * 1. Initial fake pass to compute ctx->idx and ctx->offset.
  1380. *
  1381. * BPF line info needs ctx->offset[i] to be the offset of
  1382. * instruction[i] in jited image, so build prologue first.
  1383. */
  1384. if (build_prologue(&ctx, was_classic)) {
  1385. prog = orig_prog;
  1386. goto out_off;
  1387. }
  1388. if (build_body(&ctx, extra_pass)) {
  1389. prog = orig_prog;
  1390. goto out_off;
  1391. }
  1392. ctx.epilogue_offset = ctx.idx;
  1393. build_epilogue(&ctx);
  1394. build_plt(&ctx);
  1395. extable_align = __alignof__(struct exception_table_entry);
  1396. extable_size = prog->aux->num_exentries *
  1397. sizeof(struct exception_table_entry);
  1398. /* Now we know the actual image size. */
  1399. prog_size = sizeof(u32) * ctx.idx;
  1400. /* also allocate space for plt target */
  1401. extable_offset = round_up(prog_size + PLT_TARGET_SIZE, extable_align);
  1402. image_size = extable_offset + extable_size;
  1403. header = bpf_jit_binary_alloc(image_size, &image_ptr,
  1404. sizeof(u32), jit_fill_hole);
  1405. if (header == NULL) {
  1406. prog = orig_prog;
  1407. goto out_off;
  1408. }
  1409. /* 2. Now, the actual pass. */
  1410. ctx.image = (__le32 *)image_ptr;
  1411. if (extable_size)
  1412. prog->aux->extable = (void *)image_ptr + extable_offset;
  1413. skip_init_ctx:
  1414. ctx.idx = 0;
  1415. ctx.exentry_idx = 0;
  1416. build_prologue(&ctx, was_classic);
  1417. if (build_body(&ctx, extra_pass)) {
  1418. bpf_jit_binary_free(header);
  1419. prog = orig_prog;
  1420. goto out_off;
  1421. }
  1422. build_epilogue(&ctx);
  1423. build_plt(&ctx);
  1424. /* 3. Extra pass to validate JITed code. */
  1425. if (validate_ctx(&ctx)) {
  1426. bpf_jit_binary_free(header);
  1427. prog = orig_prog;
  1428. goto out_off;
  1429. }
  1430. /* And we're done. */
  1431. if (bpf_jit_enable > 1)
  1432. bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
  1433. bpf_flush_icache(header, ctx.image + ctx.idx);
  1434. if (!prog->is_func || extra_pass) {
  1435. if (extra_pass && ctx.idx != jit_data->ctx.idx) {
  1436. pr_err_once("multi-func JIT bug %d != %d\n",
  1437. ctx.idx, jit_data->ctx.idx);
  1438. bpf_jit_binary_free(header);
  1439. prog->bpf_func = NULL;
  1440. prog->jited = 0;
  1441. prog->jited_len = 0;
  1442. goto out_off;
  1443. }
  1444. bpf_jit_binary_lock_ro(header);
  1445. } else {
  1446. jit_data->ctx = ctx;
  1447. jit_data->image = image_ptr;
  1448. jit_data->header = header;
  1449. }
  1450. prog->bpf_func = (void *)ctx.image;
  1451. prog->jited = 1;
  1452. prog->jited_len = prog_size;
  1453. #ifdef CONFIG_RKP
  1454. uh_call(UH_APP_RKP, RKP_BPF_LOAD, (u64)header, (u64)header->size, 0, 0);
  1455. #endif
  1456. if (!prog->is_func || extra_pass) {
  1457. int i;
  1458. /* offset[prog->len] is the size of program */
  1459. for (i = 0; i <= prog->len; i++)
  1460. ctx.offset[i] *= AARCH64_INSN_SIZE;
  1461. bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
  1462. out_off:
  1463. kvfree(ctx.offset);
  1464. kfree(jit_data);
  1465. prog->aux->jit_data = NULL;
  1466. }
  1467. out:
  1468. if (tmp_blinded)
  1469. bpf_jit_prog_release_other(prog, prog == orig_prog ?
  1470. tmp : orig_prog);
  1471. return prog;
  1472. }
  1473. bool bpf_jit_supports_kfunc_call(void)
  1474. {
  1475. return true;
  1476. }
  1477. u64 bpf_jit_alloc_exec_limit(void)
  1478. {
  1479. return VMALLOC_END - VMALLOC_START;
  1480. }
  1481. void *bpf_jit_alloc_exec(unsigned long size)
  1482. {
  1483. /* Memory is intended to be executable, reset the pointer tag. */
  1484. return kasan_reset_tag(vmalloc(size));
  1485. }
  1486. void bpf_jit_free_exec(void *addr)
  1487. {
  1488. return vfree(addr);
  1489. }
  1490. /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
  1491. bool bpf_jit_supports_subprog_tailcalls(void)
  1492. {
  1493. return true;
  1494. }
  1495. static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l,
  1496. int args_off, int retval_off, int run_ctx_off,
  1497. bool save_ret)
  1498. {
  1499. __le32 *branch;
  1500. u64 enter_prog;
  1501. u64 exit_prog;
  1502. struct bpf_prog *p = l->link.prog;
  1503. int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
  1504. enter_prog = (u64)bpf_trampoline_enter(p);
  1505. exit_prog = (u64)bpf_trampoline_exit(p);
  1506. if (l->cookie == 0) {
  1507. /* if cookie is zero, one instruction is enough to store it */
  1508. emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
  1509. } else {
  1510. emit_a64_mov_i64(A64_R(10), l->cookie, ctx);
  1511. emit(A64_STR64I(A64_R(10), A64_SP, run_ctx_off + cookie_off),
  1512. ctx);
  1513. }
  1514. /* save p to callee saved register x19 to avoid loading p with mov_i64
  1515. * each time.
  1516. */
  1517. emit_addr_mov_i64(A64_R(19), (const u64)p, ctx);
  1518. /* arg1: prog */
  1519. emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
  1520. /* arg2: &run_ctx */
  1521. emit(A64_ADD_I(1, A64_R(1), A64_SP, run_ctx_off), ctx);
  1522. emit_call(enter_prog, ctx);
  1523. /* if (__bpf_prog_enter(prog) == 0)
  1524. * goto skip_exec_of_prog;
  1525. */
  1526. branch = ctx->image + ctx->idx;
  1527. emit(A64_NOP, ctx);
  1528. /* save return value to callee saved register x20 */
  1529. emit(A64_MOV(1, A64_R(20), A64_R(0)), ctx);
  1530. emit(A64_ADD_I(1, A64_R(0), A64_SP, args_off), ctx);
  1531. if (!p->jited)
  1532. emit_addr_mov_i64(A64_R(1), (const u64)p->insnsi, ctx);
  1533. emit_call((const u64)p->bpf_func, ctx);
  1534. if (save_ret)
  1535. emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
  1536. if (ctx->image) {
  1537. int offset = &ctx->image[ctx->idx] - branch;
  1538. *branch = cpu_to_le32(A64_CBZ(1, A64_R(0), offset));
  1539. }
  1540. /* arg1: prog */
  1541. emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
  1542. /* arg2: start time */
  1543. emit(A64_MOV(1, A64_R(1), A64_R(20)), ctx);
  1544. /* arg3: &run_ctx */
  1545. emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
  1546. emit_call(exit_prog, ctx);
  1547. }
  1548. static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_links *tl,
  1549. int args_off, int retval_off, int run_ctx_off,
  1550. __le32 **branches)
  1551. {
  1552. int i;
  1553. /* The first fmod_ret program will receive a garbage return value.
  1554. * Set this to 0 to avoid confusing the program.
  1555. */
  1556. emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
  1557. for (i = 0; i < tl->nr_links; i++) {
  1558. invoke_bpf_prog(ctx, tl->links[i], args_off, retval_off,
  1559. run_ctx_off, true);
  1560. /* if (*(u64 *)(sp + retval_off) != 0)
  1561. * goto do_fexit;
  1562. */
  1563. emit(A64_LDR64I(A64_R(10), A64_SP, retval_off), ctx);
  1564. /* Save the location of branch, and generate a nop.
  1565. * This nop will be replaced with a cbnz later.
  1566. */
  1567. branches[i] = ctx->image + ctx->idx;
  1568. emit(A64_NOP, ctx);
  1569. }
  1570. }
  1571. static void save_args(struct jit_ctx *ctx, int args_off, int nargs)
  1572. {
  1573. int i;
  1574. for (i = 0; i < nargs; i++) {
  1575. emit(A64_STR64I(i, A64_SP, args_off), ctx);
  1576. args_off += 8;
  1577. }
  1578. }
  1579. static void restore_args(struct jit_ctx *ctx, int args_off, int nargs)
  1580. {
  1581. int i;
  1582. for (i = 0; i < nargs; i++) {
  1583. emit(A64_LDR64I(i, A64_SP, args_off), ctx);
  1584. args_off += 8;
  1585. }
  1586. }
  1587. /* Based on the x86's implementation of arch_prepare_bpf_trampoline().
  1588. *
  1589. * bpf prog and function entry before bpf trampoline hooked:
  1590. * mov x9, lr
  1591. * nop
  1592. *
  1593. * bpf prog and function entry after bpf trampoline hooked:
  1594. * mov x9, lr
  1595. * bl <bpf_trampoline or plt>
  1596. *
  1597. */
  1598. static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
  1599. struct bpf_tramp_links *tlinks, void *orig_call,
  1600. int nargs, u32 flags)
  1601. {
  1602. int i;
  1603. int stack_size;
  1604. int retaddr_off;
  1605. int regs_off;
  1606. int retval_off;
  1607. int args_off;
  1608. int nargs_off;
  1609. int ip_off;
  1610. int run_ctx_off;
  1611. struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
  1612. struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
  1613. struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
  1614. bool save_ret;
  1615. __le32 **branches = NULL;
  1616. /* trampoline stack layout:
  1617. * [ parent ip ]
  1618. * [ FP ]
  1619. * SP + retaddr_off [ self ip ]
  1620. * [ FP ]
  1621. *
  1622. * [ padding ] align SP to multiples of 16
  1623. *
  1624. * [ x20 ] callee saved reg x20
  1625. * SP + regs_off [ x19 ] callee saved reg x19
  1626. *
  1627. * SP + retval_off [ return value ] BPF_TRAMP_F_CALL_ORIG or
  1628. * BPF_TRAMP_F_RET_FENTRY_RET
  1629. *
  1630. * [ argN ]
  1631. * [ ... ]
  1632. * SP + args_off [ arg1 ]
  1633. *
  1634. * SP + nargs_off [ args count ]
  1635. *
  1636. * SP + ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag
  1637. *
  1638. * SP + run_ctx_off [ bpf_tramp_run_ctx ]
  1639. */
  1640. stack_size = 0;
  1641. run_ctx_off = stack_size;
  1642. /* room for bpf_tramp_run_ctx */
  1643. stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
  1644. ip_off = stack_size;
  1645. /* room for IP address argument */
  1646. if (flags & BPF_TRAMP_F_IP_ARG)
  1647. stack_size += 8;
  1648. nargs_off = stack_size;
  1649. /* room for args count */
  1650. stack_size += 8;
  1651. args_off = stack_size;
  1652. /* room for args */
  1653. stack_size += nargs * 8;
  1654. /* room for return value */
  1655. retval_off = stack_size;
  1656. save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
  1657. if (save_ret)
  1658. stack_size += 8;
  1659. /* room for callee saved registers, currently x19 and x20 are used */
  1660. regs_off = stack_size;
  1661. stack_size += 16;
  1662. /* round up to multiples of 16 to avoid SPAlignmentFault */
  1663. stack_size = round_up(stack_size, 16);
  1664. /* return address locates above FP */
  1665. retaddr_off = stack_size + 8;
  1666. /* bpf trampoline may be invoked by 3 instruction types:
  1667. * 1. bl, attached to bpf prog or kernel function via short jump
  1668. * 2. br, attached to bpf prog or kernel function via long jump
  1669. * 3. blr, working as a function pointer, used by struct_ops.
  1670. * So BTI_JC should used here to support both br and blr.
  1671. */
  1672. emit_bti(A64_BTI_JC, ctx);
  1673. /* frame for parent function */
  1674. emit(A64_PUSH(A64_FP, A64_R(9), A64_SP), ctx);
  1675. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  1676. /* frame for patched function */
  1677. emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
  1678. emit(A64_MOV(1, A64_FP, A64_SP), ctx);
  1679. /* allocate stack space */
  1680. emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
  1681. if (flags & BPF_TRAMP_F_IP_ARG) {
  1682. /* save ip address of the traced function */
  1683. emit_addr_mov_i64(A64_R(10), (const u64)orig_call, ctx);
  1684. emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx);
  1685. }
  1686. /* save args count*/
  1687. emit(A64_MOVZ(1, A64_R(10), nargs, 0), ctx);
  1688. emit(A64_STR64I(A64_R(10), A64_SP, nargs_off), ctx);
  1689. /* save args */
  1690. save_args(ctx, args_off, nargs);
  1691. /* save callee saved registers */
  1692. emit(A64_STR64I(A64_R(19), A64_SP, regs_off), ctx);
  1693. emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
  1694. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  1695. emit_addr_mov_i64(A64_R(0), (const u64)im, ctx);
  1696. emit_call((const u64)__bpf_tramp_enter, ctx);
  1697. }
  1698. for (i = 0; i < fentry->nr_links; i++)
  1699. invoke_bpf_prog(ctx, fentry->links[i], args_off,
  1700. retval_off, run_ctx_off,
  1701. flags & BPF_TRAMP_F_RET_FENTRY_RET);
  1702. if (fmod_ret->nr_links) {
  1703. branches = kcalloc(fmod_ret->nr_links, sizeof(__le32 *),
  1704. GFP_KERNEL);
  1705. if (!branches)
  1706. return -ENOMEM;
  1707. invoke_bpf_mod_ret(ctx, fmod_ret, args_off, retval_off,
  1708. run_ctx_off, branches);
  1709. }
  1710. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  1711. restore_args(ctx, args_off, nargs);
  1712. /* call original func */
  1713. emit(A64_LDR64I(A64_R(10), A64_SP, retaddr_off), ctx);
  1714. emit(A64_ADR(A64_LR, AARCH64_INSN_SIZE * 2), ctx);
  1715. emit(A64_RET(A64_R(10)), ctx);
  1716. /* store return value */
  1717. emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
  1718. /* reserve a nop for bpf_tramp_image_put */
  1719. im->ip_after_call = ctx->image + ctx->idx;
  1720. emit(A64_NOP, ctx);
  1721. }
  1722. /* update the branches saved in invoke_bpf_mod_ret with cbnz */
  1723. for (i = 0; i < fmod_ret->nr_links && ctx->image != NULL; i++) {
  1724. int offset = &ctx->image[ctx->idx] - branches[i];
  1725. *branches[i] = cpu_to_le32(A64_CBNZ(1, A64_R(10), offset));
  1726. }
  1727. for (i = 0; i < fexit->nr_links; i++)
  1728. invoke_bpf_prog(ctx, fexit->links[i], args_off, retval_off,
  1729. run_ctx_off, false);
  1730. if (flags & BPF_TRAMP_F_CALL_ORIG) {
  1731. im->ip_epilogue = ctx->image + ctx->idx;
  1732. emit_addr_mov_i64(A64_R(0), (const u64)im, ctx);
  1733. emit_call((const u64)__bpf_tramp_exit, ctx);
  1734. }
  1735. if (flags & BPF_TRAMP_F_RESTORE_REGS)
  1736. restore_args(ctx, args_off, nargs);
  1737. /* restore callee saved register x19 and x20 */
  1738. emit(A64_LDR64I(A64_R(19), A64_SP, regs_off), ctx);
  1739. emit(A64_LDR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
  1740. if (save_ret)
  1741. emit(A64_LDR64I(A64_R(0), A64_SP, retval_off), ctx);
  1742. /* reset SP */
  1743. emit(A64_MOV(1, A64_SP, A64_FP), ctx);
  1744. /* pop frames */
  1745. emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
  1746. emit(A64_POP(A64_FP, A64_R(9), A64_SP), ctx);
  1747. if (flags & BPF_TRAMP_F_SKIP_FRAME) {
  1748. /* skip patched function, return to parent */
  1749. emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
  1750. emit(A64_RET(A64_R(9)), ctx);
  1751. } else {
  1752. /* return to patched function */
  1753. emit(A64_MOV(1, A64_R(10), A64_LR), ctx);
  1754. emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
  1755. emit(A64_RET(A64_R(10)), ctx);
  1756. }
  1757. if (ctx->image)
  1758. bpf_flush_icache(ctx->image, ctx->image + ctx->idx);
  1759. kfree(branches);
  1760. return ctx->idx;
  1761. }
  1762. int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image,
  1763. void *image_end, const struct btf_func_model *m,
  1764. u32 flags, struct bpf_tramp_links *tlinks,
  1765. void *orig_call)
  1766. {
  1767. int i, ret;
  1768. int nargs = m->nr_args;
  1769. int max_insns = ((long)image_end - (long)image) / AARCH64_INSN_SIZE;
  1770. struct jit_ctx ctx = {
  1771. .image = NULL,
  1772. .idx = 0,
  1773. };
  1774. /* the first 8 arguments are passed by registers */
  1775. if (nargs > 8)
  1776. return -ENOTSUPP;
  1777. /* don't support struct argument */
  1778. for (i = 0; i < MAX_BPF_FUNC_ARGS; i++) {
  1779. if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
  1780. return -ENOTSUPP;
  1781. }
  1782. ret = prepare_trampoline(&ctx, im, tlinks, orig_call, nargs, flags);
  1783. if (ret < 0)
  1784. return ret;
  1785. if (ret > max_insns)
  1786. return -EFBIG;
  1787. ctx.image = image;
  1788. ctx.idx = 0;
  1789. jit_fill_hole(image, (unsigned int)(image_end - image));
  1790. ret = prepare_trampoline(&ctx, im, tlinks, orig_call, nargs, flags);
  1791. if (ret > 0 && validate_code(&ctx) < 0)
  1792. ret = -EINVAL;
  1793. if (ret > 0)
  1794. ret *= AARCH64_INSN_SIZE;
  1795. return ret;
  1796. }
  1797. static bool is_long_jump(void *ip, void *target)
  1798. {
  1799. long offset;
  1800. /* NULL target means this is a NOP */
  1801. if (!target)
  1802. return false;
  1803. offset = (long)target - (long)ip;
  1804. return offset < -SZ_128M || offset >= SZ_128M;
  1805. }
  1806. static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,
  1807. void *addr, void *plt, u32 *insn)
  1808. {
  1809. void *target;
  1810. if (!addr) {
  1811. *insn = aarch64_insn_gen_nop();
  1812. return 0;
  1813. }
  1814. if (is_long_jump(ip, addr))
  1815. target = plt;
  1816. else
  1817. target = addr;
  1818. *insn = aarch64_insn_gen_branch_imm((unsigned long)ip,
  1819. (unsigned long)target,
  1820. type);
  1821. return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT;
  1822. }
  1823. /* Replace the branch instruction from @ip to @old_addr in a bpf prog or a bpf
  1824. * trampoline with the branch instruction from @ip to @new_addr. If @old_addr
  1825. * or @new_addr is NULL, the old or new instruction is NOP.
  1826. *
  1827. * When @ip is the bpf prog entry, a bpf trampoline is being attached or
  1828. * detached. Since bpf trampoline and bpf prog are allocated separately with
  1829. * vmalloc, the address distance may exceed 128MB, the maximum branch range.
  1830. * So long jump should be handled.
  1831. *
  1832. * When a bpf prog is constructed, a plt pointing to empty trampoline
  1833. * dummy_tramp is placed at the end:
  1834. *
  1835. * bpf_prog:
  1836. * mov x9, lr
  1837. * nop // patchsite
  1838. * ...
  1839. * ret
  1840. *
  1841. * plt:
  1842. * ldr x10, target
  1843. * br x10
  1844. * target:
  1845. * .quad dummy_tramp // plt target
  1846. *
  1847. * This is also the state when no trampoline is attached.
  1848. *
  1849. * When a short-jump bpf trampoline is attached, the patchsite is patched
  1850. * to a bl instruction to the trampoline directly:
  1851. *
  1852. * bpf_prog:
  1853. * mov x9, lr
  1854. * bl <short-jump bpf trampoline address> // patchsite
  1855. * ...
  1856. * ret
  1857. *
  1858. * plt:
  1859. * ldr x10, target
  1860. * br x10
  1861. * target:
  1862. * .quad dummy_tramp // plt target
  1863. *
  1864. * When a long-jump bpf trampoline is attached, the plt target is filled with
  1865. * the trampoline address and the patchsite is patched to a bl instruction to
  1866. * the plt:
  1867. *
  1868. * bpf_prog:
  1869. * mov x9, lr
  1870. * bl plt // patchsite
  1871. * ...
  1872. * ret
  1873. *
  1874. * plt:
  1875. * ldr x10, target
  1876. * br x10
  1877. * target:
  1878. * .quad <long-jump bpf trampoline address> // plt target
  1879. *
  1880. * The dummy_tramp is used to prevent another CPU from jumping to unknown
  1881. * locations during the patching process, making the patching process easier.
  1882. */
  1883. int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
  1884. void *old_addr, void *new_addr)
  1885. {
  1886. int ret;
  1887. u32 old_insn;
  1888. u32 new_insn;
  1889. u32 replaced;
  1890. struct bpf_plt *plt = NULL;
  1891. unsigned long size = 0UL;
  1892. unsigned long offset = ~0UL;
  1893. enum aarch64_insn_branch_type branch_type;
  1894. char namebuf[KSYM_NAME_LEN];
  1895. void *image = NULL;
  1896. u64 plt_target = 0ULL;
  1897. bool poking_bpf_entry;
  1898. if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
  1899. /* Only poking bpf text is supported. Since kernel function
  1900. * entry is set up by ftrace, we reply on ftrace to poke kernel
  1901. * functions.
  1902. */
  1903. return -ENOTSUPP;
  1904. image = ip - offset;
  1905. /* zero offset means we're poking bpf prog entry */
  1906. poking_bpf_entry = (offset == 0UL);
  1907. /* bpf prog entry, find plt and the real patchsite */
  1908. if (poking_bpf_entry) {
  1909. /* plt locates at the end of bpf prog */
  1910. plt = image + size - PLT_TARGET_OFFSET;
  1911. /* skip to the nop instruction in bpf prog entry:
  1912. * bti c // if BTI enabled
  1913. * mov x9, x30
  1914. * nop
  1915. */
  1916. ip = image + POKE_OFFSET * AARCH64_INSN_SIZE;
  1917. }
  1918. /* long jump is only possible at bpf prog entry */
  1919. if (WARN_ON((is_long_jump(ip, new_addr) || is_long_jump(ip, old_addr)) &&
  1920. !poking_bpf_entry))
  1921. return -EINVAL;
  1922. if (poke_type == BPF_MOD_CALL)
  1923. branch_type = AARCH64_INSN_BRANCH_LINK;
  1924. else
  1925. branch_type = AARCH64_INSN_BRANCH_NOLINK;
  1926. if (gen_branch_or_nop(branch_type, ip, old_addr, plt, &old_insn) < 0)
  1927. return -EFAULT;
  1928. if (gen_branch_or_nop(branch_type, ip, new_addr, plt, &new_insn) < 0)
  1929. return -EFAULT;
  1930. if (is_long_jump(ip, new_addr))
  1931. plt_target = (u64)new_addr;
  1932. else if (is_long_jump(ip, old_addr))
  1933. /* if the old target is a long jump and the new target is not,
  1934. * restore the plt target to dummy_tramp, so there is always a
  1935. * legal and harmless address stored in plt target, and we'll
  1936. * never jump from plt to an unknown place.
  1937. */
  1938. plt_target = (u64)&dummy_tramp;
  1939. if (plt_target) {
  1940. /* non-zero plt_target indicates we're patching a bpf prog,
  1941. * which is read only.
  1942. */
  1943. if (set_memory_rw(PAGE_MASK & ((uintptr_t)&plt->target), 1))
  1944. return -EFAULT;
  1945. WRITE_ONCE(plt->target, plt_target);
  1946. set_memory_ro(PAGE_MASK & ((uintptr_t)&plt->target), 1);
  1947. /* since plt target points to either the new trampoline
  1948. * or dummy_tramp, even if another CPU reads the old plt
  1949. * target value before fetching the bl instruction to plt,
  1950. * it will be brought back by dummy_tramp, so no barrier is
  1951. * required here.
  1952. */
  1953. }
  1954. /* if the old target and the new target are both long jumps, no
  1955. * patching is required
  1956. */
  1957. if (old_insn == new_insn)
  1958. return 0;
  1959. mutex_lock(&text_mutex);
  1960. if (aarch64_insn_read(ip, &replaced)) {
  1961. ret = -EFAULT;
  1962. goto out;
  1963. }
  1964. if (replaced != old_insn) {
  1965. ret = -EFAULT;
  1966. goto out;
  1967. }
  1968. /* We call aarch64_insn_patch_text_nosync() to replace instruction
  1969. * atomically, so no other CPUs will fetch a half-new and half-old
  1970. * instruction. But there is chance that another CPU executes the
  1971. * old instruction after the patching operation finishes (e.g.,
  1972. * pipeline not flushed, or icache not synchronized yet).
  1973. *
  1974. * 1. when a new trampoline is attached, it is not a problem for
  1975. * different CPUs to jump to different trampolines temporarily.
  1976. *
  1977. * 2. when an old trampoline is freed, we should wait for all other
  1978. * CPUs to exit the trampoline and make sure the trampoline is no
  1979. * longer reachable, since bpf_tramp_image_put() function already
  1980. * uses percpu_ref and task-based rcu to do the sync, no need to call
  1981. * the sync version here, see bpf_tramp_image_put() for details.
  1982. */
  1983. ret = aarch64_insn_patch_text_nosync(ip, new_insn);
  1984. out:
  1985. mutex_unlock(&text_mutex);
  1986. return ret;
  1987. }