bpf_jit_comp64.c 30 KB

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