bpf_jit_comp32.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * eBPF JIT compiler for PPC32
  4. *
  5. * Copyright 2020 Christophe Leroy <[email protected]>
  6. * CS GROUP France
  7. *
  8. * Based on PPC64 eBPF JIT compiler by Naveen N. Rao
  9. */
  10. #include <linux/moduleloader.h>
  11. #include <asm/cacheflush.h>
  12. #include <asm/asm-compat.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/filter.h>
  15. #include <linux/if_vlan.h>
  16. #include <asm/kprobes.h>
  17. #include <linux/bpf.h>
  18. #include "bpf_jit.h"
  19. /*
  20. * Stack layout:
  21. *
  22. * [ prev sp ] <-------------
  23. * [ nv gpr save area ] 16 * 4 |
  24. * fp (r31) --> [ ebpf stack space ] upto 512 |
  25. * [ frame header ] 16 |
  26. * sp (r1) ---> [ stack pointer ] --------------
  27. */
  28. /* for gpr non volatile registers r17 to r31 (14) + tail call */
  29. #define BPF_PPC_STACK_SAVE (15 * 4 + 4)
  30. /* stack frame, ensure this is quadword aligned */
  31. #define BPF_PPC_STACKFRAME(ctx) (STACK_FRAME_MIN_SIZE + BPF_PPC_STACK_SAVE + (ctx)->stack_size)
  32. #define PPC_EX32(r, i) EMIT(PPC_RAW_LI((r), (i) < 0 ? -1 : 0))
  33. /* PPC NVR range -- update this if we ever use NVRs below r17 */
  34. #define BPF_PPC_NVR_MIN _R17
  35. #define BPF_PPC_TC _R16
  36. /* BPF register usage */
  37. #define TMP_REG (MAX_BPF_JIT_REG + 0)
  38. /* BPF to ppc register mappings */
  39. void bpf_jit_init_reg_mapping(struct codegen_context *ctx)
  40. {
  41. /* function return value */
  42. ctx->b2p[BPF_REG_0] = _R12;
  43. /* function arguments */
  44. ctx->b2p[BPF_REG_1] = _R4;
  45. ctx->b2p[BPF_REG_2] = _R6;
  46. ctx->b2p[BPF_REG_3] = _R8;
  47. ctx->b2p[BPF_REG_4] = _R10;
  48. ctx->b2p[BPF_REG_5] = _R22;
  49. /* non volatile registers */
  50. ctx->b2p[BPF_REG_6] = _R24;
  51. ctx->b2p[BPF_REG_7] = _R26;
  52. ctx->b2p[BPF_REG_8] = _R28;
  53. ctx->b2p[BPF_REG_9] = _R30;
  54. /* frame pointer aka BPF_REG_10 */
  55. ctx->b2p[BPF_REG_FP] = _R18;
  56. /* eBPF jit internal registers */
  57. ctx->b2p[BPF_REG_AX] = _R20;
  58. ctx->b2p[TMP_REG] = _R31; /* 32 bits */
  59. }
  60. static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
  61. {
  62. if ((reg >= BPF_PPC_NVR_MIN && reg < 32) || reg == BPF_PPC_TC)
  63. return BPF_PPC_STACKFRAME(ctx) - 4 * (32 - reg);
  64. WARN(true, "BPF JIT is asking about unknown registers, will crash the stack");
  65. /* Use the hole we have left for alignment */
  66. return BPF_PPC_STACKFRAME(ctx) - 4;
  67. }
  68. #define SEEN_VREG_MASK 0x1ff80000 /* Volatile registers r3-r12 */
  69. #define SEEN_NVREG_FULL_MASK 0x0003ffff /* Non volatile registers r14-r31 */
  70. #define SEEN_NVREG_TEMP_MASK 0x00001e01 /* BPF_REG_5, BPF_REG_AX, TMP_REG */
  71. static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
  72. {
  73. /*
  74. * We only need a stack frame if:
  75. * - we call other functions (kernel helpers), or
  76. * - we use non volatile registers, or
  77. * - we use tail call counter
  78. * - the bpf program uses its stack area
  79. * The latter condition is deduced from the usage of BPF_REG_FP
  80. */
  81. return ctx->seen & (SEEN_FUNC | SEEN_TAILCALL | SEEN_NVREG_FULL_MASK) ||
  82. bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_FP));
  83. }
  84. void bpf_jit_realloc_regs(struct codegen_context *ctx)
  85. {
  86. unsigned int nvreg_mask;
  87. if (ctx->seen & SEEN_FUNC)
  88. nvreg_mask = SEEN_NVREG_TEMP_MASK;
  89. else
  90. nvreg_mask = SEEN_NVREG_FULL_MASK;
  91. while (ctx->seen & nvreg_mask &&
  92. (ctx->seen & SEEN_VREG_MASK) != SEEN_VREG_MASK) {
  93. int old = 32 - fls(ctx->seen & (nvreg_mask & 0xaaaaaaab));
  94. int new = 32 - fls(~ctx->seen & (SEEN_VREG_MASK & 0xaaaaaaaa));
  95. int i;
  96. for (i = BPF_REG_0; i <= TMP_REG; i++) {
  97. if (ctx->b2p[i] != old)
  98. continue;
  99. ctx->b2p[i] = new;
  100. bpf_set_seen_register(ctx, new);
  101. bpf_clear_seen_register(ctx, old);
  102. if (i != TMP_REG) {
  103. bpf_set_seen_register(ctx, new - 1);
  104. bpf_clear_seen_register(ctx, old - 1);
  105. }
  106. break;
  107. }
  108. }
  109. }
  110. void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
  111. {
  112. int i;
  113. /* Initialize tail_call_cnt, to be skipped if we do tail calls. */
  114. EMIT(PPC_RAW_LI(_R4, 0));
  115. #define BPF_TAILCALL_PROLOGUE_SIZE 4
  116. if (bpf_has_stack_frame(ctx))
  117. EMIT(PPC_RAW_STWU(_R1, _R1, -BPF_PPC_STACKFRAME(ctx)));
  118. if (ctx->seen & SEEN_TAILCALL)
  119. EMIT(PPC_RAW_STW(_R4, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
  120. /* First arg comes in as a 32 bits pointer. */
  121. EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3));
  122. EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0));
  123. /*
  124. * We need a stack frame, but we don't necessarily need to
  125. * save/restore LR unless we call other functions
  126. */
  127. if (ctx->seen & SEEN_FUNC)
  128. EMIT(PPC_RAW_MFLR(_R0));
  129. /*
  130. * Back up non-volatile regs -- registers r18-r31
  131. */
  132. for (i = BPF_PPC_NVR_MIN; i <= 31; i++)
  133. if (bpf_is_seen_register(ctx, i))
  134. EMIT(PPC_RAW_STW(i, _R1, bpf_jit_stack_offsetof(ctx, i)));
  135. /* If needed retrieve arguments 9 and 10, ie 5th 64 bits arg.*/
  136. if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) {
  137. EMIT(PPC_RAW_LWZ(bpf_to_ppc(BPF_REG_5) - 1, _R1, BPF_PPC_STACKFRAME(ctx)) + 8);
  138. EMIT(PPC_RAW_LWZ(bpf_to_ppc(BPF_REG_5), _R1, BPF_PPC_STACKFRAME(ctx)) + 12);
  139. }
  140. /* Setup frame pointer to point to the bpf stack area */
  141. if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_FP))) {
  142. EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_FP) - 1, 0));
  143. EMIT(PPC_RAW_ADDI(bpf_to_ppc(BPF_REG_FP), _R1,
  144. STACK_FRAME_MIN_SIZE + ctx->stack_size));
  145. }
  146. if (ctx->seen & SEEN_FUNC)
  147. EMIT(PPC_RAW_STW(_R0, _R1, BPF_PPC_STACKFRAME(ctx) + PPC_LR_STKOFF));
  148. }
  149. static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
  150. {
  151. int i;
  152. /* Restore NVRs */
  153. for (i = BPF_PPC_NVR_MIN; i <= 31; i++)
  154. if (bpf_is_seen_register(ctx, i))
  155. EMIT(PPC_RAW_LWZ(i, _R1, bpf_jit_stack_offsetof(ctx, i)));
  156. if (ctx->seen & SEEN_FUNC)
  157. EMIT(PPC_RAW_LWZ(_R0, _R1, BPF_PPC_STACKFRAME(ctx) + PPC_LR_STKOFF));
  158. /* Tear down our stack frame */
  159. if (bpf_has_stack_frame(ctx))
  160. EMIT(PPC_RAW_ADDI(_R1, _R1, BPF_PPC_STACKFRAME(ctx)));
  161. if (ctx->seen & SEEN_FUNC)
  162. EMIT(PPC_RAW_MTLR(_R0));
  163. }
  164. void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
  165. {
  166. EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0)));
  167. bpf_jit_emit_common_epilogue(image, ctx);
  168. EMIT(PPC_RAW_BLR());
  169. }
  170. int bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 func)
  171. {
  172. s32 rel = (s32)func - (s32)(image + ctx->idx);
  173. if (image && rel < 0x2000000 && rel >= -0x2000000) {
  174. PPC_BL(func);
  175. EMIT(PPC_RAW_NOP());
  176. EMIT(PPC_RAW_NOP());
  177. EMIT(PPC_RAW_NOP());
  178. } else {
  179. /* Load function address into r0 */
  180. EMIT(PPC_RAW_LIS(_R0, IMM_H(func)));
  181. EMIT(PPC_RAW_ORI(_R0, _R0, IMM_L(func)));
  182. EMIT(PPC_RAW_MTCTR(_R0));
  183. EMIT(PPC_RAW_BCTRL());
  184. }
  185. return 0;
  186. }
  187. static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
  188. {
  189. /*
  190. * By now, the eBPF program has already setup parameters in r3-r6
  191. * r3-r4/BPF_REG_1 - pointer to ctx -- passed as is to the next bpf program
  192. * r5-r6/BPF_REG_2 - pointer to bpf_array
  193. * r7-r8/BPF_REG_3 - index in bpf_array
  194. */
  195. int b2p_bpf_array = bpf_to_ppc(BPF_REG_2);
  196. int b2p_index = bpf_to_ppc(BPF_REG_3);
  197. /*
  198. * if (index >= array->map.max_entries)
  199. * goto out;
  200. */
  201. EMIT(PPC_RAW_LWZ(_R0, b2p_bpf_array, offsetof(struct bpf_array, map.max_entries)));
  202. EMIT(PPC_RAW_CMPLW(b2p_index, _R0));
  203. EMIT(PPC_RAW_LWZ(_R0, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
  204. PPC_BCC_SHORT(COND_GE, out);
  205. /*
  206. * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
  207. * goto out;
  208. */
  209. EMIT(PPC_RAW_CMPLWI(_R0, MAX_TAIL_CALL_CNT));
  210. /* tail_call_cnt++; */
  211. EMIT(PPC_RAW_ADDIC(_R0, _R0, 1));
  212. PPC_BCC_SHORT(COND_GE, out);
  213. /* prog = array->ptrs[index]; */
  214. EMIT(PPC_RAW_RLWINM(_R3, b2p_index, 2, 0, 29));
  215. EMIT(PPC_RAW_ADD(_R3, _R3, b2p_bpf_array));
  216. EMIT(PPC_RAW_LWZ(_R3, _R3, offsetof(struct bpf_array, ptrs)));
  217. /*
  218. * if (prog == NULL)
  219. * goto out;
  220. */
  221. EMIT(PPC_RAW_CMPLWI(_R3, 0));
  222. PPC_BCC_SHORT(COND_EQ, out);
  223. /* goto *(prog->bpf_func + prologue_size); */
  224. EMIT(PPC_RAW_LWZ(_R3, _R3, offsetof(struct bpf_prog, bpf_func)));
  225. EMIT(PPC_RAW_ADDIC(_R3, _R3, BPF_TAILCALL_PROLOGUE_SIZE));
  226. EMIT(PPC_RAW_MTCTR(_R3));
  227. EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_1)));
  228. /* Put tail_call_cnt in r4 */
  229. EMIT(PPC_RAW_MR(_R4, _R0));
  230. /* tear restore NVRs, ... */
  231. bpf_jit_emit_common_epilogue(image, ctx);
  232. EMIT(PPC_RAW_BCTR());
  233. /* out: */
  234. return 0;
  235. }
  236. /* Assemble the body code between the prologue & epilogue */
  237. int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *ctx,
  238. u32 *addrs, int pass)
  239. {
  240. const struct bpf_insn *insn = fp->insnsi;
  241. int flen = fp->len;
  242. int i, ret;
  243. /* Start of epilogue code - will only be valid 2nd pass onwards */
  244. u32 exit_addr = addrs[flen];
  245. for (i = 0; i < flen; i++) {
  246. u32 code = insn[i].code;
  247. u32 dst_reg = bpf_to_ppc(insn[i].dst_reg);
  248. u32 dst_reg_h = dst_reg - 1;
  249. u32 src_reg = bpf_to_ppc(insn[i].src_reg);
  250. u32 src_reg_h = src_reg - 1;
  251. u32 ax_reg = bpf_to_ppc(BPF_REG_AX);
  252. u32 tmp_reg = bpf_to_ppc(TMP_REG);
  253. u32 size = BPF_SIZE(code);
  254. u32 save_reg, ret_reg;
  255. s16 off = insn[i].off;
  256. s32 imm = insn[i].imm;
  257. bool func_addr_fixed;
  258. u64 func_addr;
  259. u32 true_cond;
  260. u32 tmp_idx;
  261. int j;
  262. /*
  263. * addrs[] maps a BPF bytecode address into a real offset from
  264. * the start of the body code.
  265. */
  266. addrs[i] = ctx->idx * 4;
  267. /*
  268. * As an optimization, we note down which registers
  269. * are used so that we can only save/restore those in our
  270. * prologue and epilogue. We do this here regardless of whether
  271. * the actual BPF instruction uses src/dst registers or not
  272. * (for instance, BPF_CALL does not use them). The expectation
  273. * is that those instructions will have src_reg/dst_reg set to
  274. * 0. Even otherwise, we just lose some prologue/epilogue
  275. * optimization but everything else should work without
  276. * any issues.
  277. */
  278. if (dst_reg >= 3 && dst_reg < 32) {
  279. bpf_set_seen_register(ctx, dst_reg);
  280. bpf_set_seen_register(ctx, dst_reg_h);
  281. }
  282. if (src_reg >= 3 && src_reg < 32) {
  283. bpf_set_seen_register(ctx, src_reg);
  284. bpf_set_seen_register(ctx, src_reg_h);
  285. }
  286. switch (code) {
  287. /*
  288. * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
  289. */
  290. case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
  291. EMIT(PPC_RAW_ADD(dst_reg, dst_reg, src_reg));
  292. break;
  293. case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
  294. EMIT(PPC_RAW_ADDC(dst_reg, dst_reg, src_reg));
  295. EMIT(PPC_RAW_ADDE(dst_reg_h, dst_reg_h, src_reg_h));
  296. break;
  297. case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
  298. EMIT(PPC_RAW_SUB(dst_reg, dst_reg, src_reg));
  299. break;
  300. case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
  301. EMIT(PPC_RAW_SUBFC(dst_reg, src_reg, dst_reg));
  302. EMIT(PPC_RAW_SUBFE(dst_reg_h, src_reg_h, dst_reg_h));
  303. break;
  304. case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
  305. imm = -imm;
  306. fallthrough;
  307. case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
  308. if (IMM_HA(imm) & 0xffff)
  309. EMIT(PPC_RAW_ADDIS(dst_reg, dst_reg, IMM_HA(imm)));
  310. if (IMM_L(imm))
  311. EMIT(PPC_RAW_ADDI(dst_reg, dst_reg, IMM_L(imm)));
  312. break;
  313. case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
  314. imm = -imm;
  315. fallthrough;
  316. case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
  317. if (!imm)
  318. break;
  319. if (imm >= -32768 && imm < 32768) {
  320. EMIT(PPC_RAW_ADDIC(dst_reg, dst_reg, imm));
  321. } else {
  322. PPC_LI32(_R0, imm);
  323. EMIT(PPC_RAW_ADDC(dst_reg, dst_reg, _R0));
  324. }
  325. if (imm >= 0 || (BPF_OP(code) == BPF_SUB && imm == 0x80000000))
  326. EMIT(PPC_RAW_ADDZE(dst_reg_h, dst_reg_h));
  327. else
  328. EMIT(PPC_RAW_ADDME(dst_reg_h, dst_reg_h));
  329. break;
  330. case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
  331. bpf_set_seen_register(ctx, tmp_reg);
  332. EMIT(PPC_RAW_MULW(_R0, dst_reg, src_reg_h));
  333. EMIT(PPC_RAW_MULW(dst_reg_h, dst_reg_h, src_reg));
  334. EMIT(PPC_RAW_MULHWU(tmp_reg, dst_reg, src_reg));
  335. EMIT(PPC_RAW_MULW(dst_reg, dst_reg, src_reg));
  336. EMIT(PPC_RAW_ADD(dst_reg_h, dst_reg_h, _R0));
  337. EMIT(PPC_RAW_ADD(dst_reg_h, dst_reg_h, tmp_reg));
  338. break;
  339. case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
  340. EMIT(PPC_RAW_MULW(dst_reg, dst_reg, src_reg));
  341. break;
  342. case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
  343. if (imm >= -32768 && imm < 32768) {
  344. EMIT(PPC_RAW_MULI(dst_reg, dst_reg, imm));
  345. } else {
  346. PPC_LI32(_R0, imm);
  347. EMIT(PPC_RAW_MULW(dst_reg, dst_reg, _R0));
  348. }
  349. break;
  350. case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
  351. if (!imm) {
  352. PPC_LI32(dst_reg, 0);
  353. PPC_LI32(dst_reg_h, 0);
  354. break;
  355. }
  356. if (imm == 1)
  357. break;
  358. if (imm == -1) {
  359. EMIT(PPC_RAW_SUBFIC(dst_reg, dst_reg, 0));
  360. EMIT(PPC_RAW_SUBFZE(dst_reg_h, dst_reg_h));
  361. break;
  362. }
  363. bpf_set_seen_register(ctx, tmp_reg);
  364. PPC_LI32(tmp_reg, imm);
  365. EMIT(PPC_RAW_MULW(dst_reg_h, dst_reg_h, tmp_reg));
  366. if (imm < 0)
  367. EMIT(PPC_RAW_SUB(dst_reg_h, dst_reg_h, dst_reg));
  368. EMIT(PPC_RAW_MULHWU(_R0, dst_reg, tmp_reg));
  369. EMIT(PPC_RAW_MULW(dst_reg, dst_reg, tmp_reg));
  370. EMIT(PPC_RAW_ADD(dst_reg_h, dst_reg_h, _R0));
  371. break;
  372. case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
  373. EMIT(PPC_RAW_DIVWU(dst_reg, dst_reg, src_reg));
  374. break;
  375. case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
  376. EMIT(PPC_RAW_DIVWU(_R0, dst_reg, src_reg));
  377. EMIT(PPC_RAW_MULW(_R0, src_reg, _R0));
  378. EMIT(PPC_RAW_SUB(dst_reg, dst_reg, _R0));
  379. break;
  380. case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
  381. return -EOPNOTSUPP;
  382. case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
  383. return -EOPNOTSUPP;
  384. case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
  385. if (!imm)
  386. return -EINVAL;
  387. if (imm == 1)
  388. break;
  389. PPC_LI32(_R0, imm);
  390. EMIT(PPC_RAW_DIVWU(dst_reg, dst_reg, _R0));
  391. break;
  392. case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
  393. if (!imm)
  394. return -EINVAL;
  395. if (!is_power_of_2((u32)imm)) {
  396. bpf_set_seen_register(ctx, tmp_reg);
  397. PPC_LI32(tmp_reg, imm);
  398. EMIT(PPC_RAW_DIVWU(_R0, dst_reg, tmp_reg));
  399. EMIT(PPC_RAW_MULW(_R0, tmp_reg, _R0));
  400. EMIT(PPC_RAW_SUB(dst_reg, dst_reg, _R0));
  401. break;
  402. }
  403. if (imm == 1)
  404. EMIT(PPC_RAW_LI(dst_reg, 0));
  405. else
  406. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 32 - ilog2((u32)imm), 31));
  407. break;
  408. case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
  409. if (!imm)
  410. return -EINVAL;
  411. if (imm < 0)
  412. imm = -imm;
  413. if (!is_power_of_2(imm))
  414. return -EOPNOTSUPP;
  415. if (imm == 1)
  416. EMIT(PPC_RAW_LI(dst_reg, 0));
  417. else
  418. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 32 - ilog2(imm), 31));
  419. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  420. break;
  421. case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
  422. if (!imm)
  423. return -EINVAL;
  424. if (!is_power_of_2(abs(imm)))
  425. return -EOPNOTSUPP;
  426. if (imm < 0) {
  427. EMIT(PPC_RAW_SUBFIC(dst_reg, dst_reg, 0));
  428. EMIT(PPC_RAW_SUBFZE(dst_reg_h, dst_reg_h));
  429. imm = -imm;
  430. }
  431. if (imm == 1)
  432. break;
  433. imm = ilog2(imm);
  434. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 32 - imm, imm, 31));
  435. EMIT(PPC_RAW_RLWIMI(dst_reg, dst_reg_h, 32 - imm, 0, imm - 1));
  436. EMIT(PPC_RAW_SRAWI(dst_reg_h, dst_reg_h, imm));
  437. break;
  438. case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
  439. EMIT(PPC_RAW_NEG(dst_reg, dst_reg));
  440. break;
  441. case BPF_ALU64 | BPF_NEG: /* dst = -dst */
  442. EMIT(PPC_RAW_SUBFIC(dst_reg, dst_reg, 0));
  443. EMIT(PPC_RAW_SUBFZE(dst_reg_h, dst_reg_h));
  444. break;
  445. /*
  446. * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
  447. */
  448. case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
  449. EMIT(PPC_RAW_AND(dst_reg, dst_reg, src_reg));
  450. EMIT(PPC_RAW_AND(dst_reg_h, dst_reg_h, src_reg_h));
  451. break;
  452. case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
  453. EMIT(PPC_RAW_AND(dst_reg, dst_reg, src_reg));
  454. break;
  455. case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
  456. if (imm >= 0)
  457. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  458. fallthrough;
  459. case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
  460. if (!IMM_H(imm)) {
  461. EMIT(PPC_RAW_ANDI(dst_reg, dst_reg, IMM_L(imm)));
  462. } else if (!IMM_L(imm)) {
  463. EMIT(PPC_RAW_ANDIS(dst_reg, dst_reg, IMM_H(imm)));
  464. } else if (imm == (((1 << fls(imm)) - 1) ^ ((1 << (ffs(i) - 1)) - 1))) {
  465. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0,
  466. 32 - fls(imm), 32 - ffs(imm)));
  467. } else {
  468. PPC_LI32(_R0, imm);
  469. EMIT(PPC_RAW_AND(dst_reg, dst_reg, _R0));
  470. }
  471. break;
  472. case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
  473. EMIT(PPC_RAW_OR(dst_reg, dst_reg, src_reg));
  474. EMIT(PPC_RAW_OR(dst_reg_h, dst_reg_h, src_reg_h));
  475. break;
  476. case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
  477. EMIT(PPC_RAW_OR(dst_reg, dst_reg, src_reg));
  478. break;
  479. case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
  480. /* Sign-extended */
  481. if (imm < 0)
  482. EMIT(PPC_RAW_LI(dst_reg_h, -1));
  483. fallthrough;
  484. case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
  485. if (IMM_L(imm))
  486. EMIT(PPC_RAW_ORI(dst_reg, dst_reg, IMM_L(imm)));
  487. if (IMM_H(imm))
  488. EMIT(PPC_RAW_ORIS(dst_reg, dst_reg, IMM_H(imm)));
  489. break;
  490. case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
  491. if (dst_reg == src_reg) {
  492. EMIT(PPC_RAW_LI(dst_reg, 0));
  493. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  494. } else {
  495. EMIT(PPC_RAW_XOR(dst_reg, dst_reg, src_reg));
  496. EMIT(PPC_RAW_XOR(dst_reg_h, dst_reg_h, src_reg_h));
  497. }
  498. break;
  499. case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
  500. if (dst_reg == src_reg)
  501. EMIT(PPC_RAW_LI(dst_reg, 0));
  502. else
  503. EMIT(PPC_RAW_XOR(dst_reg, dst_reg, src_reg));
  504. break;
  505. case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
  506. if (imm < 0)
  507. EMIT(PPC_RAW_NOR(dst_reg_h, dst_reg_h, dst_reg_h));
  508. fallthrough;
  509. case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
  510. if (IMM_L(imm))
  511. EMIT(PPC_RAW_XORI(dst_reg, dst_reg, IMM_L(imm)));
  512. if (IMM_H(imm))
  513. EMIT(PPC_RAW_XORIS(dst_reg, dst_reg, IMM_H(imm)));
  514. break;
  515. case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
  516. EMIT(PPC_RAW_SLW(dst_reg, dst_reg, src_reg));
  517. break;
  518. case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
  519. bpf_set_seen_register(ctx, tmp_reg);
  520. EMIT(PPC_RAW_SUBFIC(_R0, src_reg, 32));
  521. EMIT(PPC_RAW_SLW(dst_reg_h, dst_reg_h, src_reg));
  522. EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
  523. EMIT(PPC_RAW_SRW(_R0, dst_reg, _R0));
  524. EMIT(PPC_RAW_SLW(tmp_reg, dst_reg, tmp_reg));
  525. EMIT(PPC_RAW_OR(dst_reg_h, dst_reg_h, _R0));
  526. EMIT(PPC_RAW_SLW(dst_reg, dst_reg, src_reg));
  527. EMIT(PPC_RAW_OR(dst_reg_h, dst_reg_h, tmp_reg));
  528. break;
  529. case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<= (u32) imm */
  530. if (!imm)
  531. break;
  532. EMIT(PPC_RAW_SLWI(dst_reg, dst_reg, imm));
  533. break;
  534. case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<= imm */
  535. if (imm < 0)
  536. return -EINVAL;
  537. if (!imm)
  538. break;
  539. if (imm < 32) {
  540. EMIT(PPC_RAW_RLWINM(dst_reg_h, dst_reg_h, imm, 0, 31 - imm));
  541. EMIT(PPC_RAW_RLWIMI(dst_reg_h, dst_reg, imm, 32 - imm, 31));
  542. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, imm, 0, 31 - imm));
  543. break;
  544. }
  545. if (imm < 64)
  546. EMIT(PPC_RAW_RLWINM(dst_reg_h, dst_reg, imm, 0, 31 - imm));
  547. else
  548. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  549. EMIT(PPC_RAW_LI(dst_reg, 0));
  550. break;
  551. case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
  552. EMIT(PPC_RAW_SRW(dst_reg, dst_reg, src_reg));
  553. break;
  554. case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
  555. bpf_set_seen_register(ctx, tmp_reg);
  556. EMIT(PPC_RAW_SUBFIC(_R0, src_reg, 32));
  557. EMIT(PPC_RAW_SRW(dst_reg, dst_reg, src_reg));
  558. EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
  559. EMIT(PPC_RAW_SLW(_R0, dst_reg_h, _R0));
  560. EMIT(PPC_RAW_SRW(tmp_reg, dst_reg_h, tmp_reg));
  561. EMIT(PPC_RAW_OR(dst_reg, dst_reg, _R0));
  562. EMIT(PPC_RAW_SRW(dst_reg_h, dst_reg_h, src_reg));
  563. EMIT(PPC_RAW_OR(dst_reg, dst_reg, tmp_reg));
  564. break;
  565. case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
  566. if (!imm)
  567. break;
  568. EMIT(PPC_RAW_SRWI(dst_reg, dst_reg, imm));
  569. break;
  570. case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
  571. if (imm < 0)
  572. return -EINVAL;
  573. if (!imm)
  574. break;
  575. if (imm < 32) {
  576. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 32 - imm, imm, 31));
  577. EMIT(PPC_RAW_RLWIMI(dst_reg, dst_reg_h, 32 - imm, 0, imm - 1));
  578. EMIT(PPC_RAW_RLWINM(dst_reg_h, dst_reg_h, 32 - imm, imm, 31));
  579. break;
  580. }
  581. if (imm < 64)
  582. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg_h, 64 - imm, imm - 32, 31));
  583. else
  584. EMIT(PPC_RAW_LI(dst_reg, 0));
  585. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  586. break;
  587. case BPF_ALU | BPF_ARSH | BPF_X: /* (s32) dst >>= src */
  588. EMIT(PPC_RAW_SRAW(dst_reg, dst_reg, src_reg));
  589. break;
  590. case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
  591. bpf_set_seen_register(ctx, tmp_reg);
  592. EMIT(PPC_RAW_SUBFIC(_R0, src_reg, 32));
  593. EMIT(PPC_RAW_SRW(dst_reg, dst_reg, src_reg));
  594. EMIT(PPC_RAW_SLW(_R0, dst_reg_h, _R0));
  595. EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
  596. EMIT(PPC_RAW_OR(dst_reg, dst_reg, _R0));
  597. EMIT(PPC_RAW_RLWINM(_R0, tmp_reg, 0, 26, 26));
  598. EMIT(PPC_RAW_SRAW(tmp_reg, dst_reg_h, tmp_reg));
  599. EMIT(PPC_RAW_SRAW(dst_reg_h, dst_reg_h, src_reg));
  600. EMIT(PPC_RAW_SLW(tmp_reg, tmp_reg, _R0));
  601. EMIT(PPC_RAW_OR(dst_reg, dst_reg, tmp_reg));
  602. break;
  603. case BPF_ALU | BPF_ARSH | BPF_K: /* (s32) dst >>= imm */
  604. if (!imm)
  605. break;
  606. EMIT(PPC_RAW_SRAWI(dst_reg, dst_reg, imm));
  607. break;
  608. case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
  609. if (imm < 0)
  610. return -EINVAL;
  611. if (!imm)
  612. break;
  613. if (imm < 32) {
  614. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 32 - imm, imm, 31));
  615. EMIT(PPC_RAW_RLWIMI(dst_reg, dst_reg_h, 32 - imm, 0, imm - 1));
  616. EMIT(PPC_RAW_SRAWI(dst_reg_h, dst_reg_h, imm));
  617. break;
  618. }
  619. if (imm < 64)
  620. EMIT(PPC_RAW_SRAWI(dst_reg, dst_reg_h, imm - 32));
  621. else
  622. EMIT(PPC_RAW_SRAWI(dst_reg, dst_reg_h, 31));
  623. EMIT(PPC_RAW_SRAWI(dst_reg_h, dst_reg_h, 31));
  624. break;
  625. /*
  626. * MOV
  627. */
  628. case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
  629. if (dst_reg == src_reg)
  630. break;
  631. EMIT(PPC_RAW_MR(dst_reg, src_reg));
  632. EMIT(PPC_RAW_MR(dst_reg_h, src_reg_h));
  633. break;
  634. case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
  635. /* special mov32 for zext */
  636. if (imm == 1)
  637. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  638. else if (dst_reg != src_reg)
  639. EMIT(PPC_RAW_MR(dst_reg, src_reg));
  640. break;
  641. case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
  642. PPC_LI32(dst_reg, imm);
  643. PPC_EX32(dst_reg_h, imm);
  644. break;
  645. case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
  646. PPC_LI32(dst_reg, imm);
  647. break;
  648. /*
  649. * BPF_FROM_BE/LE
  650. */
  651. case BPF_ALU | BPF_END | BPF_FROM_LE:
  652. switch (imm) {
  653. case 16:
  654. /* Copy 16 bits to upper part */
  655. EMIT(PPC_RAW_RLWIMI(dst_reg, dst_reg, 16, 0, 15));
  656. /* Rotate 8 bits right & mask */
  657. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 24, 16, 31));
  658. break;
  659. case 32:
  660. /*
  661. * Rotate word left by 8 bits:
  662. * 2 bytes are already in their final position
  663. * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
  664. */
  665. EMIT(PPC_RAW_RLWINM(_R0, dst_reg, 8, 0, 31));
  666. /* Rotate 24 bits and insert byte 1 */
  667. EMIT(PPC_RAW_RLWIMI(_R0, dst_reg, 24, 0, 7));
  668. /* Rotate 24 bits and insert byte 3 */
  669. EMIT(PPC_RAW_RLWIMI(_R0, dst_reg, 24, 16, 23));
  670. EMIT(PPC_RAW_MR(dst_reg, _R0));
  671. break;
  672. case 64:
  673. bpf_set_seen_register(ctx, tmp_reg);
  674. EMIT(PPC_RAW_RLWINM(tmp_reg, dst_reg, 8, 0, 31));
  675. EMIT(PPC_RAW_RLWINM(_R0, dst_reg_h, 8, 0, 31));
  676. /* Rotate 24 bits and insert byte 1 */
  677. EMIT(PPC_RAW_RLWIMI(tmp_reg, dst_reg, 24, 0, 7));
  678. EMIT(PPC_RAW_RLWIMI(_R0, dst_reg_h, 24, 0, 7));
  679. /* Rotate 24 bits and insert byte 3 */
  680. EMIT(PPC_RAW_RLWIMI(tmp_reg, dst_reg, 24, 16, 23));
  681. EMIT(PPC_RAW_RLWIMI(_R0, dst_reg_h, 24, 16, 23));
  682. EMIT(PPC_RAW_MR(dst_reg, _R0));
  683. EMIT(PPC_RAW_MR(dst_reg_h, tmp_reg));
  684. break;
  685. }
  686. break;
  687. case BPF_ALU | BPF_END | BPF_FROM_BE:
  688. switch (imm) {
  689. case 16:
  690. /* zero-extend 16 bits into 32 bits */
  691. EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 16, 31));
  692. break;
  693. case 32:
  694. case 64:
  695. /* nop */
  696. break;
  697. }
  698. break;
  699. /*
  700. * BPF_ST NOSPEC (speculation barrier)
  701. */
  702. case BPF_ST | BPF_NOSPEC:
  703. break;
  704. /*
  705. * BPF_ST(X)
  706. */
  707. case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
  708. EMIT(PPC_RAW_STB(src_reg, dst_reg, off));
  709. break;
  710. case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
  711. PPC_LI32(_R0, imm);
  712. EMIT(PPC_RAW_STB(_R0, dst_reg, off));
  713. break;
  714. case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
  715. EMIT(PPC_RAW_STH(src_reg, dst_reg, off));
  716. break;
  717. case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
  718. PPC_LI32(_R0, imm);
  719. EMIT(PPC_RAW_STH(_R0, dst_reg, off));
  720. break;
  721. case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
  722. EMIT(PPC_RAW_STW(src_reg, dst_reg, off));
  723. break;
  724. case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
  725. PPC_LI32(_R0, imm);
  726. EMIT(PPC_RAW_STW(_R0, dst_reg, off));
  727. break;
  728. case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
  729. EMIT(PPC_RAW_STW(src_reg_h, dst_reg, off));
  730. EMIT(PPC_RAW_STW(src_reg, dst_reg, off + 4));
  731. break;
  732. case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
  733. PPC_LI32(_R0, imm);
  734. EMIT(PPC_RAW_STW(_R0, dst_reg, off + 4));
  735. PPC_EX32(_R0, imm);
  736. EMIT(PPC_RAW_STW(_R0, dst_reg, off));
  737. break;
  738. /*
  739. * BPF_STX ATOMIC (atomic ops)
  740. */
  741. case BPF_STX | BPF_ATOMIC | BPF_W:
  742. save_reg = _R0;
  743. ret_reg = src_reg;
  744. bpf_set_seen_register(ctx, tmp_reg);
  745. bpf_set_seen_register(ctx, ax_reg);
  746. /* Get offset into TMP_REG */
  747. EMIT(PPC_RAW_LI(tmp_reg, off));
  748. tmp_idx = ctx->idx * 4;
  749. /* load value from memory into r0 */
  750. EMIT(PPC_RAW_LWARX(_R0, tmp_reg, dst_reg, 0));
  751. /* Save old value in BPF_REG_AX */
  752. if (imm & BPF_FETCH)
  753. EMIT(PPC_RAW_MR(ax_reg, _R0));
  754. switch (imm) {
  755. case BPF_ADD:
  756. case BPF_ADD | BPF_FETCH:
  757. EMIT(PPC_RAW_ADD(_R0, _R0, src_reg));
  758. break;
  759. case BPF_AND:
  760. case BPF_AND | BPF_FETCH:
  761. EMIT(PPC_RAW_AND(_R0, _R0, src_reg));
  762. break;
  763. case BPF_OR:
  764. case BPF_OR | BPF_FETCH:
  765. EMIT(PPC_RAW_OR(_R0, _R0, src_reg));
  766. break;
  767. case BPF_XOR:
  768. case BPF_XOR | BPF_FETCH:
  769. EMIT(PPC_RAW_XOR(_R0, _R0, src_reg));
  770. break;
  771. case BPF_CMPXCHG:
  772. /*
  773. * Return old value in BPF_REG_0 for BPF_CMPXCHG &
  774. * in src_reg for other cases.
  775. */
  776. ret_reg = bpf_to_ppc(BPF_REG_0);
  777. /* Compare with old value in BPF_REG_0 */
  778. EMIT(PPC_RAW_CMPW(bpf_to_ppc(BPF_REG_0), _R0));
  779. /* Don't set if different from old value */
  780. PPC_BCC_SHORT(COND_NE, (ctx->idx + 3) * 4);
  781. fallthrough;
  782. case BPF_XCHG:
  783. save_reg = src_reg;
  784. break;
  785. default:
  786. pr_err_ratelimited("eBPF filter atomic op code %02x (@%d) unsupported\n",
  787. code, i);
  788. return -EOPNOTSUPP;
  789. }
  790. /* store new value */
  791. EMIT(PPC_RAW_STWCX(save_reg, tmp_reg, dst_reg));
  792. /* we're done if this succeeded */
  793. PPC_BCC_SHORT(COND_NE, tmp_idx);
  794. /* For the BPF_FETCH variant, get old data into src_reg */
  795. if (imm & BPF_FETCH) {
  796. EMIT(PPC_RAW_MR(ret_reg, ax_reg));
  797. if (!fp->aux->verifier_zext)
  798. EMIT(PPC_RAW_LI(ret_reg - 1, 0)); /* higher 32-bit */
  799. }
  800. break;
  801. case BPF_STX | BPF_ATOMIC | BPF_DW: /* *(u64 *)(dst + off) += src */
  802. return -EOPNOTSUPP;
  803. /*
  804. * BPF_LDX
  805. */
  806. case BPF_LDX | BPF_MEM | BPF_B: /* dst = *(u8 *)(ul) (src + off) */
  807. case BPF_LDX | BPF_PROBE_MEM | BPF_B:
  808. case BPF_LDX | BPF_MEM | BPF_H: /* dst = *(u16 *)(ul) (src + off) */
  809. case BPF_LDX | BPF_PROBE_MEM | BPF_H:
  810. case BPF_LDX | BPF_MEM | BPF_W: /* dst = *(u32 *)(ul) (src + off) */
  811. case BPF_LDX | BPF_PROBE_MEM | BPF_W:
  812. case BPF_LDX | BPF_MEM | BPF_DW: /* dst = *(u64 *)(ul) (src + off) */
  813. case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
  814. /*
  815. * As PTR_TO_BTF_ID that uses BPF_PROBE_MEM mode could either be a valid
  816. * kernel pointer or NULL but not a userspace address, execute BPF_PROBE_MEM
  817. * load only if addr is kernel address (see is_kernel_addr()), otherwise
  818. * set dst_reg=0 and move on.
  819. */
  820. if (BPF_MODE(code) == BPF_PROBE_MEM) {
  821. PPC_LI32(_R0, TASK_SIZE - off);
  822. EMIT(PPC_RAW_CMPLW(src_reg, _R0));
  823. PPC_BCC_SHORT(COND_GT, (ctx->idx + 4) * 4);
  824. EMIT(PPC_RAW_LI(dst_reg, 0));
  825. /*
  826. * For BPF_DW case, "li reg_h,0" would be needed when
  827. * !fp->aux->verifier_zext. Emit NOP otherwise.
  828. *
  829. * Note that "li reg_h,0" is emitted for BPF_B/H/W case,
  830. * if necessary. So, jump there insted of emitting an
  831. * additional "li reg_h,0" instruction.
  832. */
  833. if (size == BPF_DW && !fp->aux->verifier_zext)
  834. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  835. else
  836. EMIT(PPC_RAW_NOP());
  837. /*
  838. * Need to jump two instructions instead of one for BPF_DW case
  839. * as there are two load instructions for dst_reg_h & dst_reg
  840. * respectively.
  841. */
  842. if (size == BPF_DW)
  843. PPC_JMP((ctx->idx + 3) * 4);
  844. else
  845. PPC_JMP((ctx->idx + 2) * 4);
  846. }
  847. switch (size) {
  848. case BPF_B:
  849. EMIT(PPC_RAW_LBZ(dst_reg, src_reg, off));
  850. break;
  851. case BPF_H:
  852. EMIT(PPC_RAW_LHZ(dst_reg, src_reg, off));
  853. break;
  854. case BPF_W:
  855. EMIT(PPC_RAW_LWZ(dst_reg, src_reg, off));
  856. break;
  857. case BPF_DW:
  858. EMIT(PPC_RAW_LWZ(dst_reg_h, src_reg, off));
  859. EMIT(PPC_RAW_LWZ(dst_reg, src_reg, off + 4));
  860. break;
  861. }
  862. if (size != BPF_DW && !fp->aux->verifier_zext)
  863. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  864. if (BPF_MODE(code) == BPF_PROBE_MEM) {
  865. int insn_idx = ctx->idx - 1;
  866. int jmp_off = 4;
  867. /*
  868. * In case of BPF_DW, two lwz instructions are emitted, one
  869. * for higher 32-bit and another for lower 32-bit. So, set
  870. * ex->insn to the first of the two and jump over both
  871. * instructions in fixup.
  872. *
  873. * Similarly, with !verifier_zext, two instructions are
  874. * emitted for BPF_B/H/W case. So, set ex->insn to the
  875. * instruction that could fault and skip over both
  876. * instructions.
  877. */
  878. if (size == BPF_DW || !fp->aux->verifier_zext) {
  879. insn_idx -= 1;
  880. jmp_off += 4;
  881. }
  882. ret = bpf_add_extable_entry(fp, image, pass, ctx, insn_idx,
  883. jmp_off, dst_reg);
  884. if (ret)
  885. return ret;
  886. }
  887. break;
  888. /*
  889. * Doubleword load
  890. * 16 byte instruction that uses two 'struct bpf_insn'
  891. */
  892. case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
  893. tmp_idx = ctx->idx;
  894. PPC_LI32(dst_reg_h, (u32)insn[i + 1].imm);
  895. PPC_LI32(dst_reg, (u32)insn[i].imm);
  896. /* padding to allow full 4 instructions for later patching */
  897. for (j = ctx->idx - tmp_idx; j < 4; j++)
  898. EMIT(PPC_RAW_NOP());
  899. /* Adjust for two bpf instructions */
  900. addrs[++i] = ctx->idx * 4;
  901. break;
  902. /*
  903. * Return/Exit
  904. */
  905. case BPF_JMP | BPF_EXIT:
  906. /*
  907. * If this isn't the very last instruction, branch to
  908. * the epilogue. If we _are_ the last instruction,
  909. * we'll just fall through to the epilogue.
  910. */
  911. if (i != flen - 1) {
  912. ret = bpf_jit_emit_exit_insn(image, ctx, _R0, exit_addr);
  913. if (ret)
  914. return ret;
  915. }
  916. /* else fall through to the epilogue */
  917. break;
  918. /*
  919. * Call kernel helper or bpf function
  920. */
  921. case BPF_JMP | BPF_CALL:
  922. ctx->seen |= SEEN_FUNC;
  923. ret = bpf_jit_get_func_addr(fp, &insn[i], false,
  924. &func_addr, &func_addr_fixed);
  925. if (ret < 0)
  926. return ret;
  927. if (bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_5))) {
  928. EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5) - 1, _R1, 8));
  929. EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5), _R1, 12));
  930. }
  931. ret = bpf_jit_emit_func_call_rel(image, ctx, func_addr);
  932. if (ret)
  933. return ret;
  934. EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0) - 1, _R3));
  935. EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0), _R4));
  936. break;
  937. /*
  938. * Jumps and branches
  939. */
  940. case BPF_JMP | BPF_JA:
  941. PPC_JMP(addrs[i + 1 + off]);
  942. break;
  943. case BPF_JMP | BPF_JGT | BPF_K:
  944. case BPF_JMP | BPF_JGT | BPF_X:
  945. case BPF_JMP | BPF_JSGT | BPF_K:
  946. case BPF_JMP | BPF_JSGT | BPF_X:
  947. case BPF_JMP32 | BPF_JGT | BPF_K:
  948. case BPF_JMP32 | BPF_JGT | BPF_X:
  949. case BPF_JMP32 | BPF_JSGT | BPF_K:
  950. case BPF_JMP32 | BPF_JSGT | BPF_X:
  951. true_cond = COND_GT;
  952. goto cond_branch;
  953. case BPF_JMP | BPF_JLT | BPF_K:
  954. case BPF_JMP | BPF_JLT | BPF_X:
  955. case BPF_JMP | BPF_JSLT | BPF_K:
  956. case BPF_JMP | BPF_JSLT | BPF_X:
  957. case BPF_JMP32 | BPF_JLT | BPF_K:
  958. case BPF_JMP32 | BPF_JLT | BPF_X:
  959. case BPF_JMP32 | BPF_JSLT | BPF_K:
  960. case BPF_JMP32 | BPF_JSLT | BPF_X:
  961. true_cond = COND_LT;
  962. goto cond_branch;
  963. case BPF_JMP | BPF_JGE | BPF_K:
  964. case BPF_JMP | BPF_JGE | BPF_X:
  965. case BPF_JMP | BPF_JSGE | BPF_K:
  966. case BPF_JMP | BPF_JSGE | BPF_X:
  967. case BPF_JMP32 | BPF_JGE | BPF_K:
  968. case BPF_JMP32 | BPF_JGE | BPF_X:
  969. case BPF_JMP32 | BPF_JSGE | BPF_K:
  970. case BPF_JMP32 | BPF_JSGE | BPF_X:
  971. true_cond = COND_GE;
  972. goto cond_branch;
  973. case BPF_JMP | BPF_JLE | BPF_K:
  974. case BPF_JMP | BPF_JLE | BPF_X:
  975. case BPF_JMP | BPF_JSLE | BPF_K:
  976. case BPF_JMP | BPF_JSLE | BPF_X:
  977. case BPF_JMP32 | BPF_JLE | BPF_K:
  978. case BPF_JMP32 | BPF_JLE | BPF_X:
  979. case BPF_JMP32 | BPF_JSLE | BPF_K:
  980. case BPF_JMP32 | BPF_JSLE | BPF_X:
  981. true_cond = COND_LE;
  982. goto cond_branch;
  983. case BPF_JMP | BPF_JEQ | BPF_K:
  984. case BPF_JMP | BPF_JEQ | BPF_X:
  985. case BPF_JMP32 | BPF_JEQ | BPF_K:
  986. case BPF_JMP32 | BPF_JEQ | BPF_X:
  987. true_cond = COND_EQ;
  988. goto cond_branch;
  989. case BPF_JMP | BPF_JNE | BPF_K:
  990. case BPF_JMP | BPF_JNE | BPF_X:
  991. case BPF_JMP32 | BPF_JNE | BPF_K:
  992. case BPF_JMP32 | BPF_JNE | BPF_X:
  993. true_cond = COND_NE;
  994. goto cond_branch;
  995. case BPF_JMP | BPF_JSET | BPF_K:
  996. case BPF_JMP | BPF_JSET | BPF_X:
  997. case BPF_JMP32 | BPF_JSET | BPF_K:
  998. case BPF_JMP32 | BPF_JSET | BPF_X:
  999. true_cond = COND_NE;
  1000. /* fallthrough; */
  1001. cond_branch:
  1002. switch (code) {
  1003. case BPF_JMP | BPF_JGT | BPF_X:
  1004. case BPF_JMP | BPF_JLT | BPF_X:
  1005. case BPF_JMP | BPF_JGE | BPF_X:
  1006. case BPF_JMP | BPF_JLE | BPF_X:
  1007. case BPF_JMP | BPF_JEQ | BPF_X:
  1008. case BPF_JMP | BPF_JNE | BPF_X:
  1009. /* unsigned comparison */
  1010. EMIT(PPC_RAW_CMPLW(dst_reg_h, src_reg_h));
  1011. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1012. EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
  1013. break;
  1014. case BPF_JMP32 | BPF_JGT | BPF_X:
  1015. case BPF_JMP32 | BPF_JLT | BPF_X:
  1016. case BPF_JMP32 | BPF_JGE | BPF_X:
  1017. case BPF_JMP32 | BPF_JLE | BPF_X:
  1018. case BPF_JMP32 | BPF_JEQ | BPF_X:
  1019. case BPF_JMP32 | BPF_JNE | BPF_X:
  1020. /* unsigned comparison */
  1021. EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
  1022. break;
  1023. case BPF_JMP | BPF_JSGT | BPF_X:
  1024. case BPF_JMP | BPF_JSLT | BPF_X:
  1025. case BPF_JMP | BPF_JSGE | BPF_X:
  1026. case BPF_JMP | BPF_JSLE | BPF_X:
  1027. /* signed comparison */
  1028. EMIT(PPC_RAW_CMPW(dst_reg_h, src_reg_h));
  1029. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1030. EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
  1031. break;
  1032. case BPF_JMP32 | BPF_JSGT | BPF_X:
  1033. case BPF_JMP32 | BPF_JSLT | BPF_X:
  1034. case BPF_JMP32 | BPF_JSGE | BPF_X:
  1035. case BPF_JMP32 | BPF_JSLE | BPF_X:
  1036. /* signed comparison */
  1037. EMIT(PPC_RAW_CMPW(dst_reg, src_reg));
  1038. break;
  1039. case BPF_JMP | BPF_JSET | BPF_X:
  1040. EMIT(PPC_RAW_AND_DOT(_R0, dst_reg_h, src_reg_h));
  1041. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1042. EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, src_reg));
  1043. break;
  1044. case BPF_JMP32 | BPF_JSET | BPF_X: {
  1045. EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, src_reg));
  1046. break;
  1047. case BPF_JMP | BPF_JNE | BPF_K:
  1048. case BPF_JMP | BPF_JEQ | BPF_K:
  1049. case BPF_JMP | BPF_JGT | BPF_K:
  1050. case BPF_JMP | BPF_JLT | BPF_K:
  1051. case BPF_JMP | BPF_JGE | BPF_K:
  1052. case BPF_JMP | BPF_JLE | BPF_K:
  1053. /*
  1054. * Need sign-extended load, so only positive
  1055. * values can be used as imm in cmplwi
  1056. */
  1057. if (imm >= 0 && imm < 32768) {
  1058. EMIT(PPC_RAW_CMPLWI(dst_reg_h, 0));
  1059. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1060. EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
  1061. } else {
  1062. /* sign-extending load ... but unsigned comparison */
  1063. PPC_EX32(_R0, imm);
  1064. EMIT(PPC_RAW_CMPLW(dst_reg_h, _R0));
  1065. PPC_LI32(_R0, imm);
  1066. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1067. EMIT(PPC_RAW_CMPLW(dst_reg, _R0));
  1068. }
  1069. break;
  1070. case BPF_JMP32 | BPF_JNE | BPF_K:
  1071. case BPF_JMP32 | BPF_JEQ | BPF_K:
  1072. case BPF_JMP32 | BPF_JGT | BPF_K:
  1073. case BPF_JMP32 | BPF_JLT | BPF_K:
  1074. case BPF_JMP32 | BPF_JGE | BPF_K:
  1075. case BPF_JMP32 | BPF_JLE | BPF_K:
  1076. if (imm >= 0 && imm < 65536) {
  1077. EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
  1078. } else {
  1079. PPC_LI32(_R0, imm);
  1080. EMIT(PPC_RAW_CMPLW(dst_reg, _R0));
  1081. }
  1082. break;
  1083. }
  1084. case BPF_JMP | BPF_JSGT | BPF_K:
  1085. case BPF_JMP | BPF_JSLT | BPF_K:
  1086. case BPF_JMP | BPF_JSGE | BPF_K:
  1087. case BPF_JMP | BPF_JSLE | BPF_K:
  1088. if (imm >= 0 && imm < 65536) {
  1089. EMIT(PPC_RAW_CMPWI(dst_reg_h, imm < 0 ? -1 : 0));
  1090. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1091. EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
  1092. } else {
  1093. /* sign-extending load */
  1094. EMIT(PPC_RAW_CMPWI(dst_reg_h, imm < 0 ? -1 : 0));
  1095. PPC_LI32(_R0, imm);
  1096. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1097. EMIT(PPC_RAW_CMPLW(dst_reg, _R0));
  1098. }
  1099. break;
  1100. case BPF_JMP32 | BPF_JSGT | BPF_K:
  1101. case BPF_JMP32 | BPF_JSLT | BPF_K:
  1102. case BPF_JMP32 | BPF_JSGE | BPF_K:
  1103. case BPF_JMP32 | BPF_JSLE | BPF_K:
  1104. /*
  1105. * signed comparison, so any 16-bit value
  1106. * can be used in cmpwi
  1107. */
  1108. if (imm >= -32768 && imm < 32768) {
  1109. EMIT(PPC_RAW_CMPWI(dst_reg, imm));
  1110. } else {
  1111. /* sign-extending load */
  1112. PPC_LI32(_R0, imm);
  1113. EMIT(PPC_RAW_CMPW(dst_reg, _R0));
  1114. }
  1115. break;
  1116. case BPF_JMP | BPF_JSET | BPF_K:
  1117. /* andi does not sign-extend the immediate */
  1118. if (imm >= 0 && imm < 32768) {
  1119. /* PPC_ANDI is _only/always_ dot-form */
  1120. EMIT(PPC_RAW_ANDI(_R0, dst_reg, imm));
  1121. } else {
  1122. PPC_LI32(_R0, imm);
  1123. if (imm < 0) {
  1124. EMIT(PPC_RAW_CMPWI(dst_reg_h, 0));
  1125. PPC_BCC_SHORT(COND_NE, (ctx->idx + 2) * 4);
  1126. }
  1127. EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, _R0));
  1128. }
  1129. break;
  1130. case BPF_JMP32 | BPF_JSET | BPF_K:
  1131. /* andi does not sign-extend the immediate */
  1132. if (imm >= 0 && imm < 32768) {
  1133. /* PPC_ANDI is _only/always_ dot-form */
  1134. EMIT(PPC_RAW_ANDI(_R0, dst_reg, imm));
  1135. } else {
  1136. PPC_LI32(_R0, imm);
  1137. EMIT(PPC_RAW_AND_DOT(_R0, dst_reg, _R0));
  1138. }
  1139. break;
  1140. }
  1141. PPC_BCC(true_cond, addrs[i + 1 + off]);
  1142. break;
  1143. /*
  1144. * Tail call
  1145. */
  1146. case BPF_JMP | BPF_TAIL_CALL:
  1147. ctx->seen |= SEEN_TAILCALL;
  1148. ret = bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
  1149. if (ret < 0)
  1150. return ret;
  1151. break;
  1152. default:
  1153. /*
  1154. * The filter contains something cruel & unusual.
  1155. * We don't handle it, but also there shouldn't be
  1156. * anything missing from our list.
  1157. */
  1158. pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n", code, i);
  1159. return -EOPNOTSUPP;
  1160. }
  1161. if (BPF_CLASS(code) == BPF_ALU && !fp->aux->verifier_zext &&
  1162. !insn_is_zext(&insn[i + 1]) && !(BPF_OP(code) == BPF_END && imm == 64))
  1163. EMIT(PPC_RAW_LI(dst_reg_h, 0));
  1164. }
  1165. /* Set end-of-body-code address for exit. */
  1166. addrs[i] = ctx->idx * 4;
  1167. return 0;
  1168. }