bpf_jit_comp32.c 42 KB

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