bpf_jit_comp64.c 30 KB

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