bpf_jit_comp64.c 31 KB

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