bpf_jit_comp64.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. * bpf_jit_comp64.c: eBPF JIT compiler
  3. *
  4. * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
  5. * IBM Corporation
  6. *
  7. * Based on the powerpc classic BPF JIT compiler by Matt Evans
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. */
  14. #include <linux/moduleloader.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/asm-compat.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/filter.h>
  19. #include <linux/if_vlan.h>
  20. #include <asm/kprobes.h>
  21. #include <linux/bpf.h>
  22. #include "bpf_jit64.h"
  23. static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
  24. {
  25. memset32(area, BREAKPOINT_INSTRUCTION, size/4);
  26. }
  27. static inline void bpf_flush_icache(void *start, void *end)
  28. {
  29. smp_wmb();
  30. flush_icache_range((unsigned long)start, (unsigned long)end);
  31. }
  32. static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i)
  33. {
  34. return (ctx->seen & (1 << (31 - b2p[i])));
  35. }
  36. static inline void bpf_set_seen_register(struct codegen_context *ctx, int i)
  37. {
  38. ctx->seen |= (1 << (31 - b2p[i]));
  39. }
  40. static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
  41. {
  42. /*
  43. * We only need a stack frame if:
  44. * - we call other functions (kernel helpers), or
  45. * - the bpf program uses its stack area
  46. * The latter condition is deduced from the usage of BPF_REG_FP
  47. */
  48. return ctx->seen & SEEN_FUNC || bpf_is_seen_register(ctx, BPF_REG_FP);
  49. }
  50. /*
  51. * When not setting up our own stackframe, the redzone usage is:
  52. *
  53. * [ prev sp ] <-------------
  54. * [ ... ] |
  55. * sp (r1) ---> [ stack pointer ] --------------
  56. * [ nv gpr save area ] 6*8
  57. * [ tail_call_cnt ] 8
  58. * [ local_tmp_var ] 8
  59. * [ unused red zone ] 208 bytes protected
  60. */
  61. static int bpf_jit_stack_local(struct codegen_context *ctx)
  62. {
  63. if (bpf_has_stack_frame(ctx))
  64. return STACK_FRAME_MIN_SIZE + ctx->stack_size;
  65. else
  66. return -(BPF_PPC_STACK_SAVE + 16);
  67. }
  68. static int bpf_jit_stack_tailcallcnt(struct codegen_context *ctx)
  69. {
  70. return bpf_jit_stack_local(ctx) + 8;
  71. }
  72. static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
  73. {
  74. if (reg >= BPF_PPC_NVR_MIN && reg < 32)
  75. return (bpf_has_stack_frame(ctx) ?
  76. (BPF_PPC_STACKFRAME + ctx->stack_size) : 0)
  77. - (8 * (32 - reg));
  78. pr_err("BPF JIT is asking about unknown registers");
  79. BUG();
  80. }
  81. static void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
  82. {
  83. int i;
  84. /*
  85. * Initialize tail_call_cnt if we do tail calls.
  86. * Otherwise, put in NOPs so that it can be skipped when we are
  87. * invoked through a tail call.
  88. */
  89. if (ctx->seen & SEEN_TAILCALL) {
  90. PPC_LI(b2p[TMP_REG_1], 0);
  91. /* this goes in the redzone */
  92. PPC_BPF_STL(b2p[TMP_REG_1], 1, -(BPF_PPC_STACK_SAVE + 8));
  93. } else {
  94. PPC_NOP();
  95. PPC_NOP();
  96. }
  97. #define BPF_TAILCALL_PROLOGUE_SIZE 8
  98. if (bpf_has_stack_frame(ctx)) {
  99. /*
  100. * We need a stack frame, but we don't necessarily need to
  101. * save/restore LR unless we call other functions
  102. */
  103. if (ctx->seen & SEEN_FUNC) {
  104. EMIT(PPC_INST_MFLR | __PPC_RT(R0));
  105. PPC_BPF_STL(0, 1, PPC_LR_STKOFF);
  106. }
  107. PPC_BPF_STLU(1, 1, -(BPF_PPC_STACKFRAME + ctx->stack_size));
  108. }
  109. /*
  110. * Back up non-volatile regs -- BPF registers 6-10
  111. * If we haven't created our own stack frame, we save these
  112. * in the protected zone below the previous stack frame
  113. */
  114. for (i = BPF_REG_6; i <= BPF_REG_10; i++)
  115. if (bpf_is_seen_register(ctx, i))
  116. PPC_BPF_STL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
  117. /* Setup frame pointer to point to the bpf stack area */
  118. if (bpf_is_seen_register(ctx, BPF_REG_FP))
  119. PPC_ADDI(b2p[BPF_REG_FP], 1,
  120. STACK_FRAME_MIN_SIZE + ctx->stack_size);
  121. }
  122. static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
  123. {
  124. int i;
  125. /* Restore NVRs */
  126. for (i = BPF_REG_6; i <= BPF_REG_10; i++)
  127. if (bpf_is_seen_register(ctx, i))
  128. PPC_BPF_LL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
  129. /* Tear down our stack frame */
  130. if (bpf_has_stack_frame(ctx)) {
  131. PPC_ADDI(1, 1, BPF_PPC_STACKFRAME + ctx->stack_size);
  132. if (ctx->seen & SEEN_FUNC) {
  133. PPC_BPF_LL(0, 1, PPC_LR_STKOFF);
  134. PPC_MTLR(0);
  135. }
  136. }
  137. }
  138. static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
  139. {
  140. bpf_jit_emit_common_epilogue(image, ctx);
  141. /* Move result to r3 */
  142. PPC_MR(3, b2p[BPF_REG_0]);
  143. PPC_BLR();
  144. }
  145. static void bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx,
  146. u64 func)
  147. {
  148. #ifdef PPC64_ELF_ABI_v1
  149. /* func points to the function descriptor */
  150. PPC_LI64(b2p[TMP_REG_2], func);
  151. /* Load actual entry point from function descriptor */
  152. PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_2], 0);
  153. /* ... and move it to LR */
  154. PPC_MTLR(b2p[TMP_REG_1]);
  155. /*
  156. * Load TOC from function descriptor at offset 8.
  157. * We can clobber r2 since we get called through a
  158. * function pointer (so caller will save/restore r2)
  159. * and since we don't use a TOC ourself.
  160. */
  161. PPC_BPF_LL(2, b2p[TMP_REG_2], 8);
  162. #else
  163. /* We can clobber r12 */
  164. PPC_FUNC_ADDR(12, func);
  165. PPC_MTLR(12);
  166. #endif
  167. PPC_BLRL();
  168. }
  169. static void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx,
  170. u64 func)
  171. {
  172. unsigned int i, ctx_idx = ctx->idx;
  173. /* Load function address into r12 */
  174. PPC_LI64(12, func);
  175. /* For bpf-to-bpf function calls, the callee's address is unknown
  176. * until the last extra pass. As seen above, we use PPC_LI64() to
  177. * load the callee's address, but this may optimize the number of
  178. * instructions required based on the nature of the address.
  179. *
  180. * Since we don't want the number of instructions emitted to change,
  181. * we pad the optimized PPC_LI64() call with NOPs to guarantee that
  182. * we always have a five-instruction sequence, which is the maximum
  183. * that PPC_LI64() can emit.
  184. */
  185. for (i = ctx->idx - ctx_idx; i < 5; i++)
  186. PPC_NOP();
  187. #ifdef PPC64_ELF_ABI_v1
  188. /*
  189. * Load TOC from function descriptor at offset 8.
  190. * We can clobber r2 since we get called through a
  191. * function pointer (so caller will save/restore r2)
  192. * and since we don't use a TOC ourself.
  193. */
  194. PPC_BPF_LL(2, 12, 8);
  195. /* Load actual entry point from function descriptor */
  196. PPC_BPF_LL(12, 12, 0);
  197. #endif
  198. PPC_MTLR(12);
  199. PPC_BLRL();
  200. }
  201. static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
  202. {
  203. /*
  204. * By now, the eBPF program has already setup parameters in r3, r4 and r5
  205. * r3/BPF_REG_1 - pointer to ctx -- passed as is to the next bpf program
  206. * r4/BPF_REG_2 - pointer to bpf_array
  207. * r5/BPF_REG_3 - index in bpf_array
  208. */
  209. int b2p_bpf_array = b2p[BPF_REG_2];
  210. int b2p_index = b2p[BPF_REG_3];
  211. /*
  212. * if (index >= array->map.max_entries)
  213. * goto out;
  214. */
  215. PPC_LWZ(b2p[TMP_REG_1], b2p_bpf_array, offsetof(struct bpf_array, map.max_entries));
  216. PPC_RLWINM(b2p_index, b2p_index, 0, 0, 31);
  217. PPC_CMPLW(b2p_index, b2p[TMP_REG_1]);
  218. PPC_BCC(COND_GE, out);
  219. /*
  220. * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
  221. * goto out;
  222. */
  223. PPC_LD(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
  224. PPC_CMPLWI(b2p[TMP_REG_1], MAX_TAIL_CALL_CNT);
  225. PPC_BCC(COND_GT, out);
  226. /*
  227. * tail_call_cnt++;
  228. */
  229. PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], 1);
  230. PPC_BPF_STL(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
  231. /* prog = array->ptrs[index]; */
  232. PPC_MULI(b2p[TMP_REG_1], b2p_index, 8);
  233. PPC_ADD(b2p[TMP_REG_1], b2p[TMP_REG_1], b2p_bpf_array);
  234. PPC_LD(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_array, ptrs));
  235. /*
  236. * if (prog == NULL)
  237. * goto out;
  238. */
  239. PPC_CMPLDI(b2p[TMP_REG_1], 0);
  240. PPC_BCC(COND_EQ, out);
  241. /* goto *(prog->bpf_func + prologue_size); */
  242. PPC_LD(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_prog, bpf_func));
  243. #ifdef PPC64_ELF_ABI_v1
  244. /* skip past the function descriptor */
  245. PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1],
  246. FUNCTION_DESCR_SIZE + BPF_TAILCALL_PROLOGUE_SIZE);
  247. #else
  248. PPC_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], BPF_TAILCALL_PROLOGUE_SIZE);
  249. #endif
  250. PPC_MTCTR(b2p[TMP_REG_1]);
  251. /* tear down stack, restore NVRs, ... */
  252. bpf_jit_emit_common_epilogue(image, ctx);
  253. PPC_BCTR();
  254. /* out: */
  255. }
  256. /* Assemble the body code between the prologue & epilogue */
  257. static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
  258. struct codegen_context *ctx,
  259. u32 *addrs, bool extra_pass)
  260. {
  261. const struct bpf_insn *insn = fp->insnsi;
  262. int flen = fp->len;
  263. int i, ret;
  264. /* Start of epilogue code - will only be valid 2nd pass onwards */
  265. u32 exit_addr = addrs[flen];
  266. for (i = 0; i < flen; i++) {
  267. u32 code = insn[i].code;
  268. u32 dst_reg = b2p[insn[i].dst_reg];
  269. u32 src_reg = b2p[insn[i].src_reg];
  270. s16 off = insn[i].off;
  271. s32 imm = insn[i].imm;
  272. bool func_addr_fixed;
  273. u64 func_addr;
  274. u64 imm64;
  275. u32 true_cond;
  276. u32 tmp_idx;
  277. /*
  278. * addrs[] maps a BPF bytecode address into a real offset from
  279. * the start of the body code.
  280. */
  281. addrs[i] = ctx->idx * 4;
  282. /*
  283. * As an optimization, we note down which non-volatile registers
  284. * are used so that we can only save/restore those in our
  285. * prologue and epilogue. We do this here regardless of whether
  286. * the actual BPF instruction uses src/dst registers or not
  287. * (for instance, BPF_CALL does not use them). The expectation
  288. * is that those instructions will have src_reg/dst_reg set to
  289. * 0. Even otherwise, we just lose some prologue/epilogue
  290. * optimization but everything else should work without
  291. * any issues.
  292. */
  293. if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32)
  294. bpf_set_seen_register(ctx, insn[i].dst_reg);
  295. if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32)
  296. bpf_set_seen_register(ctx, insn[i].src_reg);
  297. switch (code) {
  298. /*
  299. * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
  300. */
  301. case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
  302. case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
  303. PPC_ADD(dst_reg, dst_reg, src_reg);
  304. goto bpf_alu32_trunc;
  305. case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
  306. case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
  307. PPC_SUB(dst_reg, dst_reg, src_reg);
  308. goto bpf_alu32_trunc;
  309. case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
  310. case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
  311. case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
  312. case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
  313. if (BPF_OP(code) == BPF_SUB)
  314. imm = -imm;
  315. if (imm) {
  316. if (imm >= -32768 && imm < 32768)
  317. PPC_ADDI(dst_reg, dst_reg, IMM_L(imm));
  318. else {
  319. PPC_LI32(b2p[TMP_REG_1], imm);
  320. PPC_ADD(dst_reg, dst_reg, b2p[TMP_REG_1]);
  321. }
  322. }
  323. goto bpf_alu32_trunc;
  324. case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
  325. case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
  326. if (BPF_CLASS(code) == BPF_ALU)
  327. PPC_MULW(dst_reg, dst_reg, src_reg);
  328. else
  329. PPC_MULD(dst_reg, dst_reg, src_reg);
  330. goto bpf_alu32_trunc;
  331. case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
  332. case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
  333. if (imm >= -32768 && imm < 32768)
  334. PPC_MULI(dst_reg, dst_reg, IMM_L(imm));
  335. else {
  336. PPC_LI32(b2p[TMP_REG_1], imm);
  337. if (BPF_CLASS(code) == BPF_ALU)
  338. PPC_MULW(dst_reg, dst_reg,
  339. b2p[TMP_REG_1]);
  340. else
  341. PPC_MULD(dst_reg, dst_reg,
  342. b2p[TMP_REG_1]);
  343. }
  344. goto bpf_alu32_trunc;
  345. case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
  346. case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
  347. if (BPF_OP(code) == BPF_MOD) {
  348. PPC_DIVWU(b2p[TMP_REG_1], dst_reg, src_reg);
  349. PPC_MULW(b2p[TMP_REG_1], src_reg,
  350. b2p[TMP_REG_1]);
  351. PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
  352. } else
  353. PPC_DIVWU(dst_reg, dst_reg, src_reg);
  354. goto bpf_alu32_trunc;
  355. case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
  356. case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
  357. if (BPF_OP(code) == BPF_MOD) {
  358. PPC_DIVD(b2p[TMP_REG_1], dst_reg, src_reg);
  359. PPC_MULD(b2p[TMP_REG_1], src_reg,
  360. b2p[TMP_REG_1]);
  361. PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
  362. } else
  363. PPC_DIVD(dst_reg, dst_reg, src_reg);
  364. break;
  365. case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
  366. case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
  367. case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
  368. case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
  369. if (imm == 0)
  370. return -EINVAL;
  371. else if (imm == 1)
  372. goto bpf_alu32_trunc;
  373. PPC_LI32(b2p[TMP_REG_1], imm);
  374. switch (BPF_CLASS(code)) {
  375. case BPF_ALU:
  376. if (BPF_OP(code) == BPF_MOD) {
  377. PPC_DIVWU(b2p[TMP_REG_2], dst_reg,
  378. b2p[TMP_REG_1]);
  379. PPC_MULW(b2p[TMP_REG_1],
  380. b2p[TMP_REG_1],
  381. b2p[TMP_REG_2]);
  382. PPC_SUB(dst_reg, dst_reg,
  383. b2p[TMP_REG_1]);
  384. } else
  385. PPC_DIVWU(dst_reg, dst_reg,
  386. b2p[TMP_REG_1]);
  387. break;
  388. case BPF_ALU64:
  389. if (BPF_OP(code) == BPF_MOD) {
  390. PPC_DIVD(b2p[TMP_REG_2], dst_reg,
  391. b2p[TMP_REG_1]);
  392. PPC_MULD(b2p[TMP_REG_1],
  393. b2p[TMP_REG_1],
  394. b2p[TMP_REG_2]);
  395. PPC_SUB(dst_reg, dst_reg,
  396. b2p[TMP_REG_1]);
  397. } else
  398. PPC_DIVD(dst_reg, dst_reg,
  399. b2p[TMP_REG_1]);
  400. break;
  401. }
  402. goto bpf_alu32_trunc;
  403. case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
  404. case BPF_ALU64 | BPF_NEG: /* dst = -dst */
  405. PPC_NEG(dst_reg, dst_reg);
  406. goto bpf_alu32_trunc;
  407. /*
  408. * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
  409. */
  410. case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
  411. case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
  412. PPC_AND(dst_reg, dst_reg, src_reg);
  413. goto bpf_alu32_trunc;
  414. case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
  415. case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
  416. if (!IMM_H(imm))
  417. PPC_ANDI(dst_reg, dst_reg, IMM_L(imm));
  418. else {
  419. /* Sign-extended */
  420. PPC_LI32(b2p[TMP_REG_1], imm);
  421. PPC_AND(dst_reg, dst_reg, b2p[TMP_REG_1]);
  422. }
  423. goto bpf_alu32_trunc;
  424. case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
  425. case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
  426. PPC_OR(dst_reg, dst_reg, src_reg);
  427. goto bpf_alu32_trunc;
  428. case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
  429. case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
  430. if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
  431. /* Sign-extended */
  432. PPC_LI32(b2p[TMP_REG_1], imm);
  433. PPC_OR(dst_reg, dst_reg, b2p[TMP_REG_1]);
  434. } else {
  435. if (IMM_L(imm))
  436. PPC_ORI(dst_reg, dst_reg, IMM_L(imm));
  437. if (IMM_H(imm))
  438. PPC_ORIS(dst_reg, dst_reg, IMM_H(imm));
  439. }
  440. goto bpf_alu32_trunc;
  441. case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
  442. case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
  443. PPC_XOR(dst_reg, dst_reg, src_reg);
  444. goto bpf_alu32_trunc;
  445. case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
  446. case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
  447. if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
  448. /* Sign-extended */
  449. PPC_LI32(b2p[TMP_REG_1], imm);
  450. PPC_XOR(dst_reg, dst_reg, b2p[TMP_REG_1]);
  451. } else {
  452. if (IMM_L(imm))
  453. PPC_XORI(dst_reg, dst_reg, IMM_L(imm));
  454. if (IMM_H(imm))
  455. PPC_XORIS(dst_reg, dst_reg, IMM_H(imm));
  456. }
  457. goto bpf_alu32_trunc;
  458. case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
  459. /* slw clears top 32 bits */
  460. PPC_SLW(dst_reg, dst_reg, src_reg);
  461. break;
  462. case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
  463. PPC_SLD(dst_reg, dst_reg, src_reg);
  464. break;
  465. case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<== (u32) imm */
  466. /* with imm 0, we still need to clear top 32 bits */
  467. PPC_SLWI(dst_reg, dst_reg, imm);
  468. break;
  469. case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<== imm */
  470. if (imm != 0)
  471. PPC_SLDI(dst_reg, dst_reg, imm);
  472. break;
  473. case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
  474. PPC_SRW(dst_reg, dst_reg, src_reg);
  475. break;
  476. case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
  477. PPC_SRD(dst_reg, dst_reg, src_reg);
  478. break;
  479. case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
  480. PPC_SRWI(dst_reg, dst_reg, imm);
  481. break;
  482. case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
  483. if (imm != 0)
  484. PPC_SRDI(dst_reg, dst_reg, imm);
  485. break;
  486. case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
  487. PPC_SRAD(dst_reg, dst_reg, src_reg);
  488. break;
  489. case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
  490. if (imm != 0)
  491. PPC_SRADI(dst_reg, dst_reg, imm);
  492. break;
  493. /*
  494. * MOV
  495. */
  496. case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
  497. case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
  498. PPC_MR(dst_reg, src_reg);
  499. goto bpf_alu32_trunc;
  500. case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
  501. case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
  502. PPC_LI32(dst_reg, imm);
  503. if (imm < 0)
  504. goto bpf_alu32_trunc;
  505. break;
  506. bpf_alu32_trunc:
  507. /* Truncate to 32-bits */
  508. if (BPF_CLASS(code) == BPF_ALU)
  509. PPC_RLWINM(dst_reg, dst_reg, 0, 0, 31);
  510. break;
  511. /*
  512. * BPF_FROM_BE/LE
  513. */
  514. case BPF_ALU | BPF_END | BPF_FROM_LE:
  515. case BPF_ALU | BPF_END | BPF_FROM_BE:
  516. #ifdef __BIG_ENDIAN__
  517. if (BPF_SRC(code) == BPF_FROM_BE)
  518. goto emit_clear;
  519. #else /* !__BIG_ENDIAN__ */
  520. if (BPF_SRC(code) == BPF_FROM_LE)
  521. goto emit_clear;
  522. #endif
  523. switch (imm) {
  524. case 16:
  525. /* Rotate 8 bits left & mask with 0x0000ff00 */
  526. PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 16, 23);
  527. /* Rotate 8 bits right & insert LSB to reg */
  528. PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 24, 31);
  529. /* Move result back to dst_reg */
  530. PPC_MR(dst_reg, b2p[TMP_REG_1]);
  531. break;
  532. case 32:
  533. /*
  534. * Rotate word left by 8 bits:
  535. * 2 bytes are already in their final position
  536. * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
  537. */
  538. PPC_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 0, 31);
  539. /* Rotate 24 bits and insert byte 1 */
  540. PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 0, 7);
  541. /* Rotate 24 bits and insert byte 3 */
  542. PPC_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 16, 23);
  543. PPC_MR(dst_reg, b2p[TMP_REG_1]);
  544. break;
  545. case 64:
  546. /*
  547. * Way easier and faster(?) to store the value
  548. * into stack and then use ldbrx
  549. *
  550. * ctx->seen will be reliable in pass2, but
  551. * the instructions generated will remain the
  552. * same across all passes
  553. */
  554. PPC_STD(dst_reg, 1, bpf_jit_stack_local(ctx));
  555. PPC_ADDI(b2p[TMP_REG_1], 1, bpf_jit_stack_local(ctx));
  556. PPC_LDBRX(dst_reg, 0, b2p[TMP_REG_1]);
  557. break;
  558. }
  559. break;
  560. emit_clear:
  561. switch (imm) {
  562. case 16:
  563. /* zero-extend 16 bits into 64 bits */
  564. PPC_RLDICL(dst_reg, dst_reg, 0, 48);
  565. break;
  566. case 32:
  567. /* zero-extend 32 bits into 64 bits */
  568. PPC_RLDICL(dst_reg, dst_reg, 0, 32);
  569. break;
  570. case 64:
  571. /* nop */
  572. break;
  573. }
  574. break;
  575. /*
  576. * BPF_ST(X)
  577. */
  578. case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
  579. case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
  580. if (BPF_CLASS(code) == BPF_ST) {
  581. PPC_LI(b2p[TMP_REG_1], imm);
  582. src_reg = b2p[TMP_REG_1];
  583. }
  584. PPC_STB(src_reg, dst_reg, off);
  585. break;
  586. case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
  587. case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
  588. if (BPF_CLASS(code) == BPF_ST) {
  589. PPC_LI(b2p[TMP_REG_1], imm);
  590. src_reg = b2p[TMP_REG_1];
  591. }
  592. PPC_STH(src_reg, dst_reg, off);
  593. break;
  594. case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
  595. case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
  596. if (BPF_CLASS(code) == BPF_ST) {
  597. PPC_LI32(b2p[TMP_REG_1], imm);
  598. src_reg = b2p[TMP_REG_1];
  599. }
  600. PPC_STW(src_reg, dst_reg, off);
  601. break;
  602. case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
  603. case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
  604. if (BPF_CLASS(code) == BPF_ST) {
  605. PPC_LI32(b2p[TMP_REG_1], imm);
  606. src_reg = b2p[TMP_REG_1];
  607. }
  608. PPC_STD(src_reg, dst_reg, off);
  609. break;
  610. /*
  611. * BPF_STX XADD (atomic_add)
  612. */
  613. /* *(u32 *)(dst + off) += src */
  614. case BPF_STX | BPF_XADD | BPF_W:
  615. /* Get EA into TMP_REG_1 */
  616. PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
  617. tmp_idx = ctx->idx * 4;
  618. /* load value from memory into TMP_REG_2 */
  619. PPC_BPF_LWARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
  620. /* add value from src_reg into this */
  621. PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
  622. /* store result back */
  623. PPC_BPF_STWCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
  624. /* we're done if this succeeded */
  625. PPC_BCC_SHORT(COND_NE, tmp_idx);
  626. break;
  627. /* *(u64 *)(dst + off) += src */
  628. case BPF_STX | BPF_XADD | BPF_DW:
  629. PPC_ADDI(b2p[TMP_REG_1], dst_reg, off);
  630. tmp_idx = ctx->idx * 4;
  631. PPC_BPF_LDARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0);
  632. PPC_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg);
  633. PPC_BPF_STDCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]);
  634. PPC_BCC_SHORT(COND_NE, tmp_idx);
  635. break;
  636. /*
  637. * BPF_LDX
  638. */
  639. /* dst = *(u8 *)(ul) (src + off) */
  640. case BPF_LDX | BPF_MEM | BPF_B:
  641. PPC_LBZ(dst_reg, src_reg, off);
  642. break;
  643. /* dst = *(u16 *)(ul) (src + off) */
  644. case BPF_LDX | BPF_MEM | BPF_H:
  645. PPC_LHZ(dst_reg, src_reg, off);
  646. break;
  647. /* dst = *(u32 *)(ul) (src + off) */
  648. case BPF_LDX | BPF_MEM | BPF_W:
  649. PPC_LWZ(dst_reg, src_reg, off);
  650. break;
  651. /* dst = *(u64 *)(ul) (src + off) */
  652. case BPF_LDX | BPF_MEM | BPF_DW:
  653. PPC_LD(dst_reg, src_reg, off);
  654. break;
  655. /*
  656. * Doubleword load
  657. * 16 byte instruction that uses two 'struct bpf_insn'
  658. */
  659. case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
  660. imm64 = ((u64)(u32) insn[i].imm) |
  661. (((u64)(u32) insn[i+1].imm) << 32);
  662. /* Adjust for two bpf instructions */
  663. addrs[++i] = ctx->idx * 4;
  664. PPC_LI64(dst_reg, imm64);
  665. break;
  666. /*
  667. * Return/Exit
  668. */
  669. case BPF_JMP | BPF_EXIT:
  670. /*
  671. * If this isn't the very last instruction, branch to
  672. * the epilogue. If we _are_ the last instruction,
  673. * we'll just fall through to the epilogue.
  674. */
  675. if (i != flen - 1)
  676. PPC_JMP(exit_addr);
  677. /* else fall through to the epilogue */
  678. break;
  679. /*
  680. * Call kernel helper or bpf function
  681. */
  682. case BPF_JMP | BPF_CALL:
  683. ctx->seen |= SEEN_FUNC;
  684. ret = bpf_jit_get_func_addr(fp, &insn[i], extra_pass,
  685. &func_addr, &func_addr_fixed);
  686. if (ret < 0)
  687. return ret;
  688. if (func_addr_fixed)
  689. bpf_jit_emit_func_call_hlp(image, ctx, func_addr);
  690. else
  691. bpf_jit_emit_func_call_rel(image, ctx, func_addr);
  692. /* move return value from r3 to BPF_REG_0 */
  693. PPC_MR(b2p[BPF_REG_0], 3);
  694. break;
  695. /*
  696. * Jumps and branches
  697. */
  698. case BPF_JMP | BPF_JA:
  699. PPC_JMP(addrs[i + 1 + off]);
  700. break;
  701. case BPF_JMP | BPF_JGT | BPF_K:
  702. case BPF_JMP | BPF_JGT | BPF_X:
  703. case BPF_JMP | BPF_JSGT | BPF_K:
  704. case BPF_JMP | BPF_JSGT | BPF_X:
  705. true_cond = COND_GT;
  706. goto cond_branch;
  707. case BPF_JMP | BPF_JLT | BPF_K:
  708. case BPF_JMP | BPF_JLT | BPF_X:
  709. case BPF_JMP | BPF_JSLT | BPF_K:
  710. case BPF_JMP | BPF_JSLT | BPF_X:
  711. true_cond = COND_LT;
  712. goto cond_branch;
  713. case BPF_JMP | BPF_JGE | BPF_K:
  714. case BPF_JMP | BPF_JGE | BPF_X:
  715. case BPF_JMP | BPF_JSGE | BPF_K:
  716. case BPF_JMP | BPF_JSGE | BPF_X:
  717. true_cond = COND_GE;
  718. goto cond_branch;
  719. case BPF_JMP | BPF_JLE | BPF_K:
  720. case BPF_JMP | BPF_JLE | BPF_X:
  721. case BPF_JMP | BPF_JSLE | BPF_K:
  722. case BPF_JMP | BPF_JSLE | BPF_X:
  723. true_cond = COND_LE;
  724. goto cond_branch;
  725. case BPF_JMP | BPF_JEQ | BPF_K:
  726. case BPF_JMP | BPF_JEQ | BPF_X:
  727. true_cond = COND_EQ;
  728. goto cond_branch;
  729. case BPF_JMP | BPF_JNE | BPF_K:
  730. case BPF_JMP | BPF_JNE | BPF_X:
  731. true_cond = COND_NE;
  732. goto cond_branch;
  733. case BPF_JMP | BPF_JSET | BPF_K:
  734. case BPF_JMP | BPF_JSET | BPF_X:
  735. true_cond = COND_NE;
  736. /* Fall through */
  737. cond_branch:
  738. switch (code) {
  739. case BPF_JMP | BPF_JGT | BPF_X:
  740. case BPF_JMP | BPF_JLT | BPF_X:
  741. case BPF_JMP | BPF_JGE | BPF_X:
  742. case BPF_JMP | BPF_JLE | BPF_X:
  743. case BPF_JMP | BPF_JEQ | BPF_X:
  744. case BPF_JMP | BPF_JNE | BPF_X:
  745. /* unsigned comparison */
  746. PPC_CMPLD(dst_reg, src_reg);
  747. break;
  748. case BPF_JMP | BPF_JSGT | BPF_X:
  749. case BPF_JMP | BPF_JSLT | BPF_X:
  750. case BPF_JMP | BPF_JSGE | BPF_X:
  751. case BPF_JMP | BPF_JSLE | BPF_X:
  752. /* signed comparison */
  753. PPC_CMPD(dst_reg, src_reg);
  754. break;
  755. case BPF_JMP | BPF_JSET | BPF_X:
  756. PPC_AND_DOT(b2p[TMP_REG_1], dst_reg, src_reg);
  757. break;
  758. case BPF_JMP | BPF_JNE | BPF_K:
  759. case BPF_JMP | BPF_JEQ | BPF_K:
  760. case BPF_JMP | BPF_JGT | BPF_K:
  761. case BPF_JMP | BPF_JLT | BPF_K:
  762. case BPF_JMP | BPF_JGE | BPF_K:
  763. case BPF_JMP | BPF_JLE | BPF_K:
  764. /*
  765. * Need sign-extended load, so only positive
  766. * values can be used as imm in cmpldi
  767. */
  768. if (imm >= 0 && imm < 32768)
  769. PPC_CMPLDI(dst_reg, imm);
  770. else {
  771. /* sign-extending load */
  772. PPC_LI32(b2p[TMP_REG_1], imm);
  773. /* ... but unsigned comparison */
  774. PPC_CMPLD(dst_reg, b2p[TMP_REG_1]);
  775. }
  776. break;
  777. case BPF_JMP | BPF_JSGT | BPF_K:
  778. case BPF_JMP | BPF_JSLT | BPF_K:
  779. case BPF_JMP | BPF_JSGE | BPF_K:
  780. case BPF_JMP | BPF_JSLE | BPF_K:
  781. /*
  782. * signed comparison, so any 16-bit value
  783. * can be used in cmpdi
  784. */
  785. if (imm >= -32768 && imm < 32768)
  786. PPC_CMPDI(dst_reg, imm);
  787. else {
  788. PPC_LI32(b2p[TMP_REG_1], imm);
  789. PPC_CMPD(dst_reg, b2p[TMP_REG_1]);
  790. }
  791. break;
  792. case BPF_JMP | BPF_JSET | BPF_K:
  793. /* andi does not sign-extend the immediate */
  794. if (imm >= 0 && imm < 32768)
  795. /* PPC_ANDI is _only/always_ dot-form */
  796. PPC_ANDI(b2p[TMP_REG_1], dst_reg, imm);
  797. else {
  798. PPC_LI32(b2p[TMP_REG_1], imm);
  799. PPC_AND_DOT(b2p[TMP_REG_1], dst_reg,
  800. b2p[TMP_REG_1]);
  801. }
  802. break;
  803. }
  804. PPC_BCC(true_cond, addrs[i + 1 + off]);
  805. break;
  806. /*
  807. * Tail call
  808. */
  809. case BPF_JMP | BPF_TAIL_CALL:
  810. ctx->seen |= SEEN_TAILCALL;
  811. bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
  812. break;
  813. default:
  814. /*
  815. * The filter contains something cruel & unusual.
  816. * We don't handle it, but also there shouldn't be
  817. * anything missing from our list.
  818. */
  819. pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n",
  820. code, i);
  821. return -ENOTSUPP;
  822. }
  823. }
  824. /* Set end-of-body-code address for exit. */
  825. addrs[i] = ctx->idx * 4;
  826. return 0;
  827. }
  828. /* Fix the branch target addresses for subprog calls */
  829. static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,
  830. struct codegen_context *ctx, u32 *addrs)
  831. {
  832. const struct bpf_insn *insn = fp->insnsi;
  833. bool func_addr_fixed;
  834. u64 func_addr;
  835. u32 tmp_idx;
  836. int i, ret;
  837. for (i = 0; i < fp->len; i++) {
  838. /*
  839. * During the extra pass, only the branch target addresses for
  840. * the subprog calls need to be fixed. All other instructions
  841. * can left untouched.
  842. *
  843. * The JITed image length does not change because we already
  844. * ensure that the JITed instruction sequence for these calls
  845. * are of fixed length by padding them with NOPs.
  846. */
  847. if (insn[i].code == (BPF_JMP | BPF_CALL) &&
  848. insn[i].src_reg == BPF_PSEUDO_CALL) {
  849. ret = bpf_jit_get_func_addr(fp, &insn[i], true,
  850. &func_addr,
  851. &func_addr_fixed);
  852. if (ret < 0)
  853. return ret;
  854. /*
  855. * Save ctx->idx as this would currently point to the
  856. * end of the JITed image and set it to the offset of
  857. * the instruction sequence corresponding to the
  858. * subprog call temporarily.
  859. */
  860. tmp_idx = ctx->idx;
  861. ctx->idx = addrs[i] / 4;
  862. bpf_jit_emit_func_call_rel(image, ctx, func_addr);
  863. /*
  864. * Restore ctx->idx here. This is safe as the length
  865. * of the JITed sequence remains unchanged.
  866. */
  867. ctx->idx = tmp_idx;
  868. }
  869. }
  870. return 0;
  871. }
  872. struct powerpc64_jit_data {
  873. struct bpf_binary_header *header;
  874. u32 *addrs;
  875. u8 *image;
  876. u32 proglen;
  877. struct codegen_context ctx;
  878. };
  879. struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
  880. {
  881. u32 proglen;
  882. u32 alloclen;
  883. u8 *image = NULL;
  884. u32 *code_base;
  885. u32 *addrs;
  886. struct powerpc64_jit_data *jit_data;
  887. struct codegen_context cgctx;
  888. int pass;
  889. int flen;
  890. struct bpf_binary_header *bpf_hdr;
  891. struct bpf_prog *org_fp = fp;
  892. struct bpf_prog *tmp_fp;
  893. bool bpf_blinded = false;
  894. bool extra_pass = false;
  895. if (!fp->jit_requested)
  896. return org_fp;
  897. tmp_fp = bpf_jit_blind_constants(org_fp);
  898. if (IS_ERR(tmp_fp))
  899. return org_fp;
  900. if (tmp_fp != org_fp) {
  901. bpf_blinded = true;
  902. fp = tmp_fp;
  903. }
  904. jit_data = fp->aux->jit_data;
  905. if (!jit_data) {
  906. jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
  907. if (!jit_data) {
  908. fp = org_fp;
  909. goto out;
  910. }
  911. fp->aux->jit_data = jit_data;
  912. }
  913. flen = fp->len;
  914. addrs = jit_data->addrs;
  915. if (addrs) {
  916. cgctx = jit_data->ctx;
  917. image = jit_data->image;
  918. bpf_hdr = jit_data->header;
  919. proglen = jit_data->proglen;
  920. alloclen = proglen + FUNCTION_DESCR_SIZE;
  921. extra_pass = true;
  922. goto skip_init_ctx;
  923. }
  924. addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
  925. if (addrs == NULL) {
  926. fp = org_fp;
  927. goto out_addrs;
  928. }
  929. memset(&cgctx, 0, sizeof(struct codegen_context));
  930. /* Make sure that the stack is quadword aligned. */
  931. cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
  932. /* Scouting faux-generate pass 0 */
  933. if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
  934. /* We hit something illegal or unsupported. */
  935. fp = org_fp;
  936. goto out_addrs;
  937. }
  938. /*
  939. * Pretend to build prologue, given the features we've seen. This will
  940. * update ctgtx.idx as it pretends to output instructions, then we can
  941. * calculate total size from idx.
  942. */
  943. bpf_jit_build_prologue(0, &cgctx);
  944. bpf_jit_build_epilogue(0, &cgctx);
  945. proglen = cgctx.idx * 4;
  946. alloclen = proglen + FUNCTION_DESCR_SIZE;
  947. bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4,
  948. bpf_jit_fill_ill_insns);
  949. if (!bpf_hdr) {
  950. fp = org_fp;
  951. goto out_addrs;
  952. }
  953. skip_init_ctx:
  954. code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
  955. if (extra_pass) {
  956. /*
  957. * Do not touch the prologue and epilogue as they will remain
  958. * unchanged. Only fix the branch target address for subprog
  959. * calls in the body.
  960. *
  961. * This does not change the offsets and lengths of the subprog
  962. * call instruction sequences and hence, the size of the JITed
  963. * image as well.
  964. */
  965. bpf_jit_fixup_subprog_calls(fp, code_base, &cgctx, addrs);
  966. /* There is no need to perform the usual passes. */
  967. goto skip_codegen_passes;
  968. }
  969. /* Code generation passes 1-2 */
  970. for (pass = 1; pass < 3; pass++) {
  971. /* Now build the prologue, body code & epilogue for real. */
  972. cgctx.idx = 0;
  973. bpf_jit_build_prologue(code_base, &cgctx);
  974. bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass);
  975. bpf_jit_build_epilogue(code_base, &cgctx);
  976. if (bpf_jit_enable > 1)
  977. pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
  978. proglen - (cgctx.idx * 4), cgctx.seen);
  979. }
  980. skip_codegen_passes:
  981. if (bpf_jit_enable > 1)
  982. /*
  983. * Note that we output the base address of the code_base
  984. * rather than image, since opcodes are in code_base.
  985. */
  986. bpf_jit_dump(flen, proglen, pass, code_base);
  987. #ifdef PPC64_ELF_ABI_v1
  988. /* Function descriptor nastiness: Address + TOC */
  989. ((u64 *)image)[0] = (u64)code_base;
  990. ((u64 *)image)[1] = local_paca->kernel_toc;
  991. #endif
  992. fp->bpf_func = (void *)image;
  993. fp->jited = 1;
  994. fp->jited_len = alloclen;
  995. bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
  996. if (!fp->is_func || extra_pass) {
  997. out_addrs:
  998. kfree(addrs);
  999. kfree(jit_data);
  1000. fp->aux->jit_data = NULL;
  1001. } else {
  1002. jit_data->addrs = addrs;
  1003. jit_data->ctx = cgctx;
  1004. jit_data->proglen = proglen;
  1005. jit_data->image = image;
  1006. jit_data->header = bpf_hdr;
  1007. }
  1008. out:
  1009. if (bpf_blinded)
  1010. bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
  1011. return fp;
  1012. }
  1013. /* Overriding bpf_jit_free() as we don't set images read-only. */
  1014. void bpf_jit_free(struct bpf_prog *fp)
  1015. {
  1016. unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
  1017. struct bpf_binary_header *bpf_hdr = (void *)addr;
  1018. if (fp->jited)
  1019. bpf_jit_binary_free(bpf_hdr);
  1020. bpf_prog_unlock_free(fp);
  1021. }