bpf_jit_comp64.c 30 KB

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