1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * bpf_jit_comp64.c: eBPF JIT compiler
4 *
5 * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
6 * IBM Corporation
7 *
8 * Based on the powerpc classic BPF JIT compiler by Matt Evans
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
19 #include "bpf_jit64.h"
20
bpf_jit_fill_ill_insns(void * area,unsigned int size)21 static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
22 {
23 memset32(area, BREAKPOINT_INSTRUCTION, size/4);
24 }
25
bpf_flush_icache(void * start,void * end)26 static inline void bpf_flush_icache(void *start, void *end)
27 {
28 smp_wmb();
29 flush_icache_range((unsigned long)start, (unsigned long)end);
30 }
31
bpf_is_seen_register(struct codegen_context * ctx,int i)32 static inline bool bpf_is_seen_register(struct codegen_context *ctx, int i)
33 {
34 return (ctx->seen & (1 << (31 - b2p[i])));
35 }
36
bpf_set_seen_register(struct codegen_context * ctx,int i)37 static inline void bpf_set_seen_register(struct codegen_context *ctx, int i)
38 {
39 ctx->seen |= (1 << (31 - b2p[i]));
40 }
41
bpf_has_stack_frame(struct codegen_context * ctx)42 static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
43 {
44 /*
45 * We only need a stack frame if:
46 * - we call other functions (kernel helpers), or
47 * - the bpf program uses its stack area
48 * The latter condition is deduced from the usage of BPF_REG_FP
49 */
50 return ctx->seen & SEEN_FUNC || bpf_is_seen_register(ctx, BPF_REG_FP);
51 }
52
53 /*
54 * When not setting up our own stackframe, the redzone usage is:
55 *
56 * [ prev sp ] <-------------
57 * [ ... ] |
58 * sp (r1) ---> [ stack pointer ] --------------
59 * [ nv gpr save area ] 6*8
60 * [ tail_call_cnt ] 8
61 * [ local_tmp_var ] 8
62 * [ unused red zone ] 208 bytes protected
63 */
bpf_jit_stack_local(struct codegen_context * ctx)64 static int bpf_jit_stack_local(struct codegen_context *ctx)
65 {
66 if (bpf_has_stack_frame(ctx))
67 return STACK_FRAME_MIN_SIZE + ctx->stack_size;
68 else
69 return -(BPF_PPC_STACK_SAVE + 16);
70 }
71
bpf_jit_stack_tailcallcnt(struct codegen_context * ctx)72 static int bpf_jit_stack_tailcallcnt(struct codegen_context *ctx)
73 {
74 return bpf_jit_stack_local(ctx) + 8;
75 }
76
bpf_jit_stack_offsetof(struct codegen_context * ctx,int reg)77 static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
78 {
79 if (reg >= BPF_PPC_NVR_MIN && reg < 32)
80 return (bpf_has_stack_frame(ctx) ?
81 (BPF_PPC_STACKFRAME + ctx->stack_size) : 0)
82 - (8 * (32 - reg));
83
84 pr_err("BPF JIT is asking about unknown registers");
85 BUG();
86 }
87
bpf_jit_build_prologue(u32 * image,struct codegen_context * ctx)88 static void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)
89 {
90 int i;
91
92 /*
93 * Initialize tail_call_cnt if we do tail calls.
94 * Otherwise, put in NOPs so that it can be skipped when we are
95 * invoked through a tail call.
96 */
97 if (ctx->seen & SEEN_TAILCALL) {
98 EMIT(PPC_RAW_LI(b2p[TMP_REG_1], 0));
99 /* this goes in the redzone */
100 PPC_BPF_STL(b2p[TMP_REG_1], 1, -(BPF_PPC_STACK_SAVE + 8));
101 } else {
102 EMIT(PPC_RAW_NOP());
103 EMIT(PPC_RAW_NOP());
104 }
105
106 #define BPF_TAILCALL_PROLOGUE_SIZE 8
107
108 if (bpf_has_stack_frame(ctx)) {
109 /*
110 * We need a stack frame, but we don't necessarily need to
111 * save/restore LR unless we call other functions
112 */
113 if (ctx->seen & SEEN_FUNC) {
114 EMIT(PPC_INST_MFLR | __PPC_RT(R0));
115 PPC_BPF_STL(0, 1, PPC_LR_STKOFF);
116 }
117
118 PPC_BPF_STLU(1, 1, -(BPF_PPC_STACKFRAME + ctx->stack_size));
119 }
120
121 /*
122 * Back up non-volatile regs -- BPF registers 6-10
123 * If we haven't created our own stack frame, we save these
124 * in the protected zone below the previous stack frame
125 */
126 for (i = BPF_REG_6; i <= BPF_REG_10; i++)
127 if (bpf_is_seen_register(ctx, i))
128 PPC_BPF_STL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
129
130 /* Setup frame pointer to point to the bpf stack area */
131 if (bpf_is_seen_register(ctx, BPF_REG_FP))
132 EMIT(PPC_RAW_ADDI(b2p[BPF_REG_FP], 1,
133 STACK_FRAME_MIN_SIZE + ctx->stack_size));
134 }
135
bpf_jit_emit_common_epilogue(u32 * image,struct codegen_context * ctx)136 static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx)
137 {
138 int i;
139
140 /* Restore NVRs */
141 for (i = BPF_REG_6; i <= BPF_REG_10; i++)
142 if (bpf_is_seen_register(ctx, i))
143 PPC_BPF_LL(b2p[i], 1, bpf_jit_stack_offsetof(ctx, b2p[i]));
144
145 /* Tear down our stack frame */
146 if (bpf_has_stack_frame(ctx)) {
147 EMIT(PPC_RAW_ADDI(1, 1, BPF_PPC_STACKFRAME + ctx->stack_size));
148 if (ctx->seen & SEEN_FUNC) {
149 PPC_BPF_LL(0, 1, PPC_LR_STKOFF);
150 EMIT(PPC_RAW_MTLR(0));
151 }
152 }
153 }
154
bpf_jit_build_epilogue(u32 * image,struct codegen_context * ctx)155 static void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
156 {
157 bpf_jit_emit_common_epilogue(image, ctx);
158
159 /* Move result to r3 */
160 EMIT(PPC_RAW_MR(3, b2p[BPF_REG_0]));
161
162 EMIT(PPC_RAW_BLR());
163 }
164
bpf_jit_emit_func_call_hlp(u32 * image,struct codegen_context * ctx,u64 func)165 static void bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx,
166 u64 func)
167 {
168 #ifdef PPC64_ELF_ABI_v1
169 /* func points to the function descriptor */
170 PPC_LI64(b2p[TMP_REG_2], func);
171 /* Load actual entry point from function descriptor */
172 PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_2], 0);
173 /* ... and move it to LR */
174 EMIT(PPC_RAW_MTLR(b2p[TMP_REG_1]));
175 /*
176 * Load TOC from function descriptor at offset 8.
177 * We can clobber r2 since we get called through a
178 * function pointer (so caller will save/restore r2)
179 * and since we don't use a TOC ourself.
180 */
181 PPC_BPF_LL(2, b2p[TMP_REG_2], 8);
182 #else
183 /* We can clobber r12 */
184 PPC_FUNC_ADDR(12, func);
185 EMIT(PPC_RAW_MTLR(12));
186 #endif
187 EMIT(PPC_RAW_BLRL());
188 }
189
bpf_jit_emit_func_call_rel(u32 * image,struct codegen_context * ctx,u64 func)190 static void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx,
191 u64 func)
192 {
193 unsigned int i, ctx_idx = ctx->idx;
194
195 /* Load function address into r12 */
196 PPC_LI64(12, func);
197
198 /* For bpf-to-bpf function calls, the callee's address is unknown
199 * until the last extra pass. As seen above, we use PPC_LI64() to
200 * load the callee's address, but this may optimize the number of
201 * instructions required based on the nature of the address.
202 *
203 * Since we don't want the number of instructions emitted to change,
204 * we pad the optimized PPC_LI64() call with NOPs to guarantee that
205 * we always have a five-instruction sequence, which is the maximum
206 * that PPC_LI64() can emit.
207 */
208 for (i = ctx->idx - ctx_idx; i < 5; i++)
209 EMIT(PPC_RAW_NOP());
210
211 #ifdef PPC64_ELF_ABI_v1
212 /*
213 * Load TOC from function descriptor at offset 8.
214 * We can clobber r2 since we get called through a
215 * function pointer (so caller will save/restore r2)
216 * and since we don't use a TOC ourself.
217 */
218 PPC_BPF_LL(2, 12, 8);
219 /* Load actual entry point from function descriptor */
220 PPC_BPF_LL(12, 12, 0);
221 #endif
222
223 EMIT(PPC_RAW_MTLR(12));
224 EMIT(PPC_RAW_BLRL());
225 }
226
bpf_jit_emit_tail_call(u32 * image,struct codegen_context * ctx,u32 out)227 static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
228 {
229 /*
230 * By now, the eBPF program has already setup parameters in r3, r4 and r5
231 * r3/BPF_REG_1 - pointer to ctx -- passed as is to the next bpf program
232 * r4/BPF_REG_2 - pointer to bpf_array
233 * r5/BPF_REG_3 - index in bpf_array
234 */
235 int b2p_bpf_array = b2p[BPF_REG_2];
236 int b2p_index = b2p[BPF_REG_3];
237
238 /*
239 * if (index >= array->map.max_entries)
240 * goto out;
241 */
242 EMIT(PPC_RAW_LWZ(b2p[TMP_REG_1], b2p_bpf_array, offsetof(struct bpf_array, map.max_entries)));
243 EMIT(PPC_RAW_RLWINM(b2p_index, b2p_index, 0, 0, 31));
244 EMIT(PPC_RAW_CMPLW(b2p_index, b2p[TMP_REG_1]));
245 PPC_BCC(COND_GE, out);
246
247 /*
248 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
249 * goto out;
250 */
251 PPC_BPF_LL(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
252 EMIT(PPC_RAW_CMPLWI(b2p[TMP_REG_1], MAX_TAIL_CALL_CNT));
253 PPC_BCC(COND_GT, out);
254
255 /*
256 * tail_call_cnt++;
257 */
258 EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], 1));
259 PPC_BPF_STL(b2p[TMP_REG_1], 1, bpf_jit_stack_tailcallcnt(ctx));
260
261 /* prog = array->ptrs[index]; */
262 EMIT(PPC_RAW_MULI(b2p[TMP_REG_1], b2p_index, 8));
263 EMIT(PPC_RAW_ADD(b2p[TMP_REG_1], b2p[TMP_REG_1], b2p_bpf_array));
264 PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_array, ptrs));
265
266 /*
267 * if (prog == NULL)
268 * goto out;
269 */
270 EMIT(PPC_RAW_CMPLDI(b2p[TMP_REG_1], 0));
271 PPC_BCC(COND_EQ, out);
272
273 /* goto *(prog->bpf_func + prologue_size); */
274 PPC_BPF_LL(b2p[TMP_REG_1], b2p[TMP_REG_1], offsetof(struct bpf_prog, bpf_func));
275 #ifdef PPC64_ELF_ABI_v1
276 /* skip past the function descriptor */
277 EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1],
278 FUNCTION_DESCR_SIZE + BPF_TAILCALL_PROLOGUE_SIZE));
279 #else
280 EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], b2p[TMP_REG_1], BPF_TAILCALL_PROLOGUE_SIZE));
281 #endif
282 EMIT(PPC_RAW_MTCTR(b2p[TMP_REG_1]));
283
284 /* tear down stack, restore NVRs, ... */
285 bpf_jit_emit_common_epilogue(image, ctx);
286
287 EMIT(PPC_RAW_BCTR());
288 /* out: */
289 }
290
291 /* Assemble the body code between the prologue & epilogue */
bpf_jit_build_body(struct bpf_prog * fp,u32 * image,struct codegen_context * ctx,u32 * addrs,bool extra_pass)292 static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
293 struct codegen_context *ctx,
294 u32 *addrs, bool extra_pass)
295 {
296 const struct bpf_insn *insn = fp->insnsi;
297 int flen = fp->len;
298 int i, ret;
299
300 /* Start of epilogue code - will only be valid 2nd pass onwards */
301 u32 exit_addr = addrs[flen];
302
303 for (i = 0; i < flen; i++) {
304 u32 code = insn[i].code;
305 u32 dst_reg = b2p[insn[i].dst_reg];
306 u32 src_reg = b2p[insn[i].src_reg];
307 s16 off = insn[i].off;
308 s32 imm = insn[i].imm;
309 bool func_addr_fixed;
310 u64 func_addr;
311 u64 imm64;
312 u32 true_cond;
313 u32 tmp_idx;
314
315 /*
316 * addrs[] maps a BPF bytecode address into a real offset from
317 * the start of the body code.
318 */
319 addrs[i] = ctx->idx * 4;
320
321 /*
322 * As an optimization, we note down which non-volatile registers
323 * are used so that we can only save/restore those in our
324 * prologue and epilogue. We do this here regardless of whether
325 * the actual BPF instruction uses src/dst registers or not
326 * (for instance, BPF_CALL does not use them). The expectation
327 * is that those instructions will have src_reg/dst_reg set to
328 * 0. Even otherwise, we just lose some prologue/epilogue
329 * optimization but everything else should work without
330 * any issues.
331 */
332 if (dst_reg >= BPF_PPC_NVR_MIN && dst_reg < 32)
333 bpf_set_seen_register(ctx, insn[i].dst_reg);
334 if (src_reg >= BPF_PPC_NVR_MIN && src_reg < 32)
335 bpf_set_seen_register(ctx, insn[i].src_reg);
336
337 switch (code) {
338 /*
339 * Arithmetic operations: ADD/SUB/MUL/DIV/MOD/NEG
340 */
341 case BPF_ALU | BPF_ADD | BPF_X: /* (u32) dst += (u32) src */
342 case BPF_ALU64 | BPF_ADD | BPF_X: /* dst += src */
343 EMIT(PPC_RAW_ADD(dst_reg, dst_reg, src_reg));
344 goto bpf_alu32_trunc;
345 case BPF_ALU | BPF_SUB | BPF_X: /* (u32) dst -= (u32) src */
346 case BPF_ALU64 | BPF_SUB | BPF_X: /* dst -= src */
347 EMIT(PPC_RAW_SUB(dst_reg, dst_reg, src_reg));
348 goto bpf_alu32_trunc;
349 case BPF_ALU | BPF_ADD | BPF_K: /* (u32) dst += (u32) imm */
350 case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */
351 if (!imm) {
352 goto bpf_alu32_trunc;
353 } else if (imm >= -32768 && imm < 32768) {
354 EMIT(PPC_RAW_ADDI(dst_reg, dst_reg, IMM_L(imm)));
355 } else {
356 PPC_LI32(b2p[TMP_REG_1], imm);
357 EMIT(PPC_RAW_ADD(dst_reg, dst_reg, b2p[TMP_REG_1]));
358 }
359 goto bpf_alu32_trunc;
360 case BPF_ALU | BPF_SUB | BPF_K: /* (u32) dst -= (u32) imm */
361 case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */
362 if (!imm) {
363 goto bpf_alu32_trunc;
364 } else if (imm > -32768 && imm <= 32768) {
365 EMIT(PPC_RAW_ADDI(dst_reg, dst_reg, IMM_L(-imm)));
366 } else {
367 PPC_LI32(b2p[TMP_REG_1], imm);
368 EMIT(PPC_RAW_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]));
369 }
370 goto bpf_alu32_trunc;
371 case BPF_ALU | BPF_MUL | BPF_X: /* (u32) dst *= (u32) src */
372 case BPF_ALU64 | BPF_MUL | BPF_X: /* dst *= src */
373 if (BPF_CLASS(code) == BPF_ALU)
374 EMIT(PPC_RAW_MULW(dst_reg, dst_reg, src_reg));
375 else
376 EMIT(PPC_RAW_MULD(dst_reg, dst_reg, src_reg));
377 goto bpf_alu32_trunc;
378 case BPF_ALU | BPF_MUL | BPF_K: /* (u32) dst *= (u32) imm */
379 case BPF_ALU64 | BPF_MUL | BPF_K: /* dst *= imm */
380 if (imm >= -32768 && imm < 32768)
381 EMIT(PPC_RAW_MULI(dst_reg, dst_reg, IMM_L(imm)));
382 else {
383 PPC_LI32(b2p[TMP_REG_1], imm);
384 if (BPF_CLASS(code) == BPF_ALU)
385 EMIT(PPC_RAW_MULW(dst_reg, dst_reg,
386 b2p[TMP_REG_1]));
387 else
388 EMIT(PPC_RAW_MULD(dst_reg, dst_reg,
389 b2p[TMP_REG_1]));
390 }
391 goto bpf_alu32_trunc;
392 case BPF_ALU | BPF_DIV | BPF_X: /* (u32) dst /= (u32) src */
393 case BPF_ALU | BPF_MOD | BPF_X: /* (u32) dst %= (u32) src */
394 if (BPF_OP(code) == BPF_MOD) {
395 EMIT(PPC_RAW_DIVWU(b2p[TMP_REG_1], dst_reg, src_reg));
396 EMIT(PPC_RAW_MULW(b2p[TMP_REG_1], src_reg,
397 b2p[TMP_REG_1]));
398 EMIT(PPC_RAW_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]));
399 } else
400 EMIT(PPC_RAW_DIVWU(dst_reg, dst_reg, src_reg));
401 goto bpf_alu32_trunc;
402 case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
403 case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
404 if (BPF_OP(code) == BPF_MOD) {
405 EMIT(PPC_RAW_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg));
406 EMIT(PPC_RAW_MULD(b2p[TMP_REG_1], src_reg,
407 b2p[TMP_REG_1]));
408 EMIT(PPC_RAW_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]));
409 } else
410 EMIT(PPC_RAW_DIVDU(dst_reg, dst_reg, src_reg));
411 break;
412 case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
413 case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
414 case BPF_ALU64 | BPF_MOD | BPF_K: /* dst %= imm */
415 case BPF_ALU64 | BPF_DIV | BPF_K: /* dst /= imm */
416 if (imm == 0)
417 return -EINVAL;
418 if (imm == 1) {
419 if (BPF_OP(code) == BPF_DIV) {
420 goto bpf_alu32_trunc;
421 } else {
422 EMIT(PPC_RAW_LI(dst_reg, 0));
423 break;
424 }
425 }
426
427 PPC_LI32(b2p[TMP_REG_1], imm);
428 switch (BPF_CLASS(code)) {
429 case BPF_ALU:
430 if (BPF_OP(code) == BPF_MOD) {
431 EMIT(PPC_RAW_DIVWU(b2p[TMP_REG_2],
432 dst_reg,
433 b2p[TMP_REG_1]));
434 EMIT(PPC_RAW_MULW(b2p[TMP_REG_1],
435 b2p[TMP_REG_1],
436 b2p[TMP_REG_2]));
437 EMIT(PPC_RAW_SUB(dst_reg, dst_reg,
438 b2p[TMP_REG_1]));
439 } else
440 EMIT(PPC_RAW_DIVWU(dst_reg, dst_reg,
441 b2p[TMP_REG_1]));
442 break;
443 case BPF_ALU64:
444 if (BPF_OP(code) == BPF_MOD) {
445 EMIT(PPC_RAW_DIVDU(b2p[TMP_REG_2],
446 dst_reg,
447 b2p[TMP_REG_1]));
448 EMIT(PPC_RAW_MULD(b2p[TMP_REG_1],
449 b2p[TMP_REG_1],
450 b2p[TMP_REG_2]));
451 EMIT(PPC_RAW_SUB(dst_reg, dst_reg,
452 b2p[TMP_REG_1]));
453 } else
454 EMIT(PPC_RAW_DIVDU(dst_reg, dst_reg,
455 b2p[TMP_REG_1]));
456 break;
457 }
458 goto bpf_alu32_trunc;
459 case BPF_ALU | BPF_NEG: /* (u32) dst = -dst */
460 case BPF_ALU64 | BPF_NEG: /* dst = -dst */
461 EMIT(PPC_RAW_NEG(dst_reg, dst_reg));
462 goto bpf_alu32_trunc;
463
464 /*
465 * Logical operations: AND/OR/XOR/[A]LSH/[A]RSH
466 */
467 case BPF_ALU | BPF_AND | BPF_X: /* (u32) dst = dst & src */
468 case BPF_ALU64 | BPF_AND | BPF_X: /* dst = dst & src */
469 EMIT(PPC_RAW_AND(dst_reg, dst_reg, src_reg));
470 goto bpf_alu32_trunc;
471 case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
472 case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
473 if (!IMM_H(imm))
474 EMIT(PPC_RAW_ANDI(dst_reg, dst_reg, IMM_L(imm)));
475 else {
476 /* Sign-extended */
477 PPC_LI32(b2p[TMP_REG_1], imm);
478 EMIT(PPC_RAW_AND(dst_reg, dst_reg, b2p[TMP_REG_1]));
479 }
480 goto bpf_alu32_trunc;
481 case BPF_ALU | BPF_OR | BPF_X: /* dst = (u32) dst | (u32) src */
482 case BPF_ALU64 | BPF_OR | BPF_X: /* dst = dst | src */
483 EMIT(PPC_RAW_OR(dst_reg, dst_reg, src_reg));
484 goto bpf_alu32_trunc;
485 case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
486 case BPF_ALU64 | BPF_OR | BPF_K:/* dst = dst | imm */
487 if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
488 /* Sign-extended */
489 PPC_LI32(b2p[TMP_REG_1], imm);
490 EMIT(PPC_RAW_OR(dst_reg, dst_reg, b2p[TMP_REG_1]));
491 } else {
492 if (IMM_L(imm))
493 EMIT(PPC_RAW_ORI(dst_reg, dst_reg, IMM_L(imm)));
494 if (IMM_H(imm))
495 EMIT(PPC_RAW_ORIS(dst_reg, dst_reg, IMM_H(imm)));
496 }
497 goto bpf_alu32_trunc;
498 case BPF_ALU | BPF_XOR | BPF_X: /* (u32) dst ^= src */
499 case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
500 EMIT(PPC_RAW_XOR(dst_reg, dst_reg, src_reg));
501 goto bpf_alu32_trunc;
502 case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
503 case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
504 if (imm < 0 && BPF_CLASS(code) == BPF_ALU64) {
505 /* Sign-extended */
506 PPC_LI32(b2p[TMP_REG_1], imm);
507 EMIT(PPC_RAW_XOR(dst_reg, dst_reg, b2p[TMP_REG_1]));
508 } else {
509 if (IMM_L(imm))
510 EMIT(PPC_RAW_XORI(dst_reg, dst_reg, IMM_L(imm)));
511 if (IMM_H(imm))
512 EMIT(PPC_RAW_XORIS(dst_reg, dst_reg, IMM_H(imm)));
513 }
514 goto bpf_alu32_trunc;
515 case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
516 /* slw clears top 32 bits */
517 EMIT(PPC_RAW_SLW(dst_reg, dst_reg, src_reg));
518 /* skip zero extension move, but set address map. */
519 if (insn_is_zext(&insn[i + 1]))
520 addrs[++i] = ctx->idx * 4;
521 break;
522 case BPF_ALU64 | BPF_LSH | BPF_X: /* dst <<= src; */
523 EMIT(PPC_RAW_SLD(dst_reg, dst_reg, src_reg));
524 break;
525 case BPF_ALU | BPF_LSH | BPF_K: /* (u32) dst <<== (u32) imm */
526 /* with imm 0, we still need to clear top 32 bits */
527 EMIT(PPC_RAW_SLWI(dst_reg, dst_reg, imm));
528 if (insn_is_zext(&insn[i + 1]))
529 addrs[++i] = ctx->idx * 4;
530 break;
531 case BPF_ALU64 | BPF_LSH | BPF_K: /* dst <<== imm */
532 if (imm != 0)
533 EMIT(PPC_RAW_SLDI(dst_reg, dst_reg, imm));
534 break;
535 case BPF_ALU | BPF_RSH | BPF_X: /* (u32) dst >>= (u32) src */
536 EMIT(PPC_RAW_SRW(dst_reg, dst_reg, src_reg));
537 if (insn_is_zext(&insn[i + 1]))
538 addrs[++i] = ctx->idx * 4;
539 break;
540 case BPF_ALU64 | BPF_RSH | BPF_X: /* dst >>= src */
541 EMIT(PPC_RAW_SRD(dst_reg, dst_reg, src_reg));
542 break;
543 case BPF_ALU | BPF_RSH | BPF_K: /* (u32) dst >>= (u32) imm */
544 EMIT(PPC_RAW_SRWI(dst_reg, dst_reg, imm));
545 if (insn_is_zext(&insn[i + 1]))
546 addrs[++i] = ctx->idx * 4;
547 break;
548 case BPF_ALU64 | BPF_RSH | BPF_K: /* dst >>= imm */
549 if (imm != 0)
550 EMIT(PPC_RAW_SRDI(dst_reg, dst_reg, imm));
551 break;
552 case BPF_ALU | BPF_ARSH | BPF_X: /* (s32) dst >>= src */
553 EMIT(PPC_RAW_SRAW(dst_reg, dst_reg, src_reg));
554 goto bpf_alu32_trunc;
555 case BPF_ALU64 | BPF_ARSH | BPF_X: /* (s64) dst >>= src */
556 EMIT(PPC_RAW_SRAD(dst_reg, dst_reg, src_reg));
557 break;
558 case BPF_ALU | BPF_ARSH | BPF_K: /* (s32) dst >>= imm */
559 EMIT(PPC_RAW_SRAWI(dst_reg, dst_reg, imm));
560 goto bpf_alu32_trunc;
561 case BPF_ALU64 | BPF_ARSH | BPF_K: /* (s64) dst >>= imm */
562 if (imm != 0)
563 EMIT(PPC_RAW_SRADI(dst_reg, dst_reg, imm));
564 break;
565
566 /*
567 * MOV
568 */
569 case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
570 case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
571 if (imm == 1) {
572 /* special mov32 for zext */
573 EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 0, 31));
574 break;
575 }
576 EMIT(PPC_RAW_MR(dst_reg, src_reg));
577 goto bpf_alu32_trunc;
578 case BPF_ALU | BPF_MOV | BPF_K: /* (u32) dst = imm */
579 case BPF_ALU64 | BPF_MOV | BPF_K: /* dst = (s64) imm */
580 PPC_LI32(dst_reg, imm);
581 if (imm < 0)
582 goto bpf_alu32_trunc;
583 else if (insn_is_zext(&insn[i + 1]))
584 addrs[++i] = ctx->idx * 4;
585 break;
586
587 bpf_alu32_trunc:
588 /* Truncate to 32-bits */
589 if (BPF_CLASS(code) == BPF_ALU && !fp->aux->verifier_zext)
590 EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 0, 31));
591 break;
592
593 /*
594 * BPF_FROM_BE/LE
595 */
596 case BPF_ALU | BPF_END | BPF_FROM_LE:
597 case BPF_ALU | BPF_END | BPF_FROM_BE:
598 #ifdef __BIG_ENDIAN__
599 if (BPF_SRC(code) == BPF_FROM_BE)
600 goto emit_clear;
601 #else /* !__BIG_ENDIAN__ */
602 if (BPF_SRC(code) == BPF_FROM_LE)
603 goto emit_clear;
604 #endif
605 switch (imm) {
606 case 16:
607 /* Rotate 8 bits left & mask with 0x0000ff00 */
608 EMIT(PPC_RAW_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 16, 23));
609 /* Rotate 8 bits right & insert LSB to reg */
610 EMIT(PPC_RAW_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 24, 31));
611 /* Move result back to dst_reg */
612 EMIT(PPC_RAW_MR(dst_reg, b2p[TMP_REG_1]));
613 break;
614 case 32:
615 /*
616 * Rotate word left by 8 bits:
617 * 2 bytes are already in their final position
618 * -- byte 2 and 4 (of bytes 1, 2, 3 and 4)
619 */
620 EMIT(PPC_RAW_RLWINM(b2p[TMP_REG_1], dst_reg, 8, 0, 31));
621 /* Rotate 24 bits and insert byte 1 */
622 EMIT(PPC_RAW_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 0, 7));
623 /* Rotate 24 bits and insert byte 3 */
624 EMIT(PPC_RAW_RLWIMI(b2p[TMP_REG_1], dst_reg, 24, 16, 23));
625 EMIT(PPC_RAW_MR(dst_reg, b2p[TMP_REG_1]));
626 break;
627 case 64:
628 /*
629 * Way easier and faster(?) to store the value
630 * into stack and then use ldbrx
631 *
632 * ctx->seen will be reliable in pass2, but
633 * the instructions generated will remain the
634 * same across all passes
635 */
636 PPC_BPF_STL(dst_reg, 1, bpf_jit_stack_local(ctx));
637 EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], 1, bpf_jit_stack_local(ctx)));
638 EMIT(PPC_RAW_LDBRX(dst_reg, 0, b2p[TMP_REG_1]));
639 break;
640 }
641 break;
642
643 emit_clear:
644 switch (imm) {
645 case 16:
646 /* zero-extend 16 bits into 64 bits */
647 EMIT(PPC_RAW_RLDICL(dst_reg, dst_reg, 0, 48));
648 if (insn_is_zext(&insn[i + 1]))
649 addrs[++i] = ctx->idx * 4;
650 break;
651 case 32:
652 if (!fp->aux->verifier_zext)
653 /* zero-extend 32 bits into 64 bits */
654 EMIT(PPC_RAW_RLDICL(dst_reg, dst_reg, 0, 32));
655 break;
656 case 64:
657 /* nop */
658 break;
659 }
660 break;
661
662 /*
663 * BPF_ST NOSPEC (speculation barrier)
664 */
665 case BPF_ST | BPF_NOSPEC:
666 break;
667
668 /*
669 * BPF_ST(X)
670 */
671 case BPF_STX | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = src */
672 case BPF_ST | BPF_MEM | BPF_B: /* *(u8 *)(dst + off) = imm */
673 if (BPF_CLASS(code) == BPF_ST) {
674 EMIT(PPC_RAW_LI(b2p[TMP_REG_1], imm));
675 src_reg = b2p[TMP_REG_1];
676 }
677 EMIT(PPC_RAW_STB(src_reg, dst_reg, off));
678 break;
679 case BPF_STX | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = src */
680 case BPF_ST | BPF_MEM | BPF_H: /* (u16 *)(dst + off) = imm */
681 if (BPF_CLASS(code) == BPF_ST) {
682 EMIT(PPC_RAW_LI(b2p[TMP_REG_1], imm));
683 src_reg = b2p[TMP_REG_1];
684 }
685 EMIT(PPC_RAW_STH(src_reg, dst_reg, off));
686 break;
687 case BPF_STX | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = src */
688 case BPF_ST | BPF_MEM | BPF_W: /* *(u32 *)(dst + off) = imm */
689 if (BPF_CLASS(code) == BPF_ST) {
690 PPC_LI32(b2p[TMP_REG_1], imm);
691 src_reg = b2p[TMP_REG_1];
692 }
693 EMIT(PPC_RAW_STW(src_reg, dst_reg, off));
694 break;
695 case BPF_STX | BPF_MEM | BPF_DW: /* (u64 *)(dst + off) = src */
696 case BPF_ST | BPF_MEM | BPF_DW: /* *(u64 *)(dst + off) = imm */
697 if (BPF_CLASS(code) == BPF_ST) {
698 PPC_LI32(b2p[TMP_REG_1], imm);
699 src_reg = b2p[TMP_REG_1];
700 }
701 PPC_BPF_STL(src_reg, dst_reg, off);
702 break;
703
704 /*
705 * BPF_STX XADD (atomic_add)
706 */
707 /* *(u32 *)(dst + off) += src */
708 case BPF_STX | BPF_XADD | BPF_W:
709 /* Get EA into TMP_REG_1 */
710 EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], dst_reg, off));
711 tmp_idx = ctx->idx * 4;
712 /* load value from memory into TMP_REG_2 */
713 EMIT(PPC_RAW_LWARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0));
714 /* add value from src_reg into this */
715 EMIT(PPC_RAW_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg));
716 /* store result back */
717 EMIT(PPC_RAW_STWCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]));
718 /* we're done if this succeeded */
719 PPC_BCC_SHORT(COND_NE, tmp_idx);
720 break;
721 /* *(u64 *)(dst + off) += src */
722 case BPF_STX | BPF_XADD | BPF_DW:
723 EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], dst_reg, off));
724 tmp_idx = ctx->idx * 4;
725 EMIT(PPC_RAW_LDARX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1], 0));
726 EMIT(PPC_RAW_ADD(b2p[TMP_REG_2], b2p[TMP_REG_2], src_reg));
727 EMIT(PPC_RAW_STDCX(b2p[TMP_REG_2], 0, b2p[TMP_REG_1]));
728 PPC_BCC_SHORT(COND_NE, tmp_idx);
729 break;
730
731 /*
732 * BPF_LDX
733 */
734 /* dst = *(u8 *)(ul) (src + off) */
735 case BPF_LDX | BPF_MEM | BPF_B:
736 EMIT(PPC_RAW_LBZ(dst_reg, src_reg, off));
737 if (insn_is_zext(&insn[i + 1]))
738 addrs[++i] = ctx->idx * 4;
739 break;
740 /* dst = *(u16 *)(ul) (src + off) */
741 case BPF_LDX | BPF_MEM | BPF_H:
742 EMIT(PPC_RAW_LHZ(dst_reg, src_reg, off));
743 if (insn_is_zext(&insn[i + 1]))
744 addrs[++i] = ctx->idx * 4;
745 break;
746 /* dst = *(u32 *)(ul) (src + off) */
747 case BPF_LDX | BPF_MEM | BPF_W:
748 EMIT(PPC_RAW_LWZ(dst_reg, src_reg, off));
749 if (insn_is_zext(&insn[i + 1]))
750 addrs[++i] = ctx->idx * 4;
751 break;
752 /* dst = *(u64 *)(ul) (src + off) */
753 case BPF_LDX | BPF_MEM | BPF_DW:
754 PPC_BPF_LL(dst_reg, src_reg, off);
755 break;
756
757 /*
758 * Doubleword load
759 * 16 byte instruction that uses two 'struct bpf_insn'
760 */
761 case BPF_LD | BPF_IMM | BPF_DW: /* dst = (u64) imm */
762 imm64 = ((u64)(u32) insn[i].imm) |
763 (((u64)(u32) insn[i+1].imm) << 32);
764 /* Adjust for two bpf instructions */
765 addrs[++i] = ctx->idx * 4;
766 PPC_LI64(dst_reg, imm64);
767 break;
768
769 /*
770 * Return/Exit
771 */
772 case BPF_JMP | BPF_EXIT:
773 /*
774 * If this isn't the very last instruction, branch to
775 * the epilogue. If we _are_ the last instruction,
776 * we'll just fall through to the epilogue.
777 */
778 if (i != flen - 1)
779 PPC_JMP(exit_addr);
780 /* else fall through to the epilogue */
781 break;
782
783 /*
784 * Call kernel helper or bpf function
785 */
786 case BPF_JMP | BPF_CALL:
787 ctx->seen |= SEEN_FUNC;
788
789 ret = bpf_jit_get_func_addr(fp, &insn[i], extra_pass,
790 &func_addr, &func_addr_fixed);
791 if (ret < 0)
792 return ret;
793
794 if (func_addr_fixed)
795 bpf_jit_emit_func_call_hlp(image, ctx, func_addr);
796 else
797 bpf_jit_emit_func_call_rel(image, ctx, func_addr);
798 /* move return value from r3 to BPF_REG_0 */
799 EMIT(PPC_RAW_MR(b2p[BPF_REG_0], 3));
800 break;
801
802 /*
803 * Jumps and branches
804 */
805 case BPF_JMP | BPF_JA:
806 PPC_JMP(addrs[i + 1 + off]);
807 break;
808
809 case BPF_JMP | BPF_JGT | BPF_K:
810 case BPF_JMP | BPF_JGT | BPF_X:
811 case BPF_JMP | BPF_JSGT | BPF_K:
812 case BPF_JMP | BPF_JSGT | BPF_X:
813 case BPF_JMP32 | BPF_JGT | BPF_K:
814 case BPF_JMP32 | BPF_JGT | BPF_X:
815 case BPF_JMP32 | BPF_JSGT | BPF_K:
816 case BPF_JMP32 | BPF_JSGT | BPF_X:
817 true_cond = COND_GT;
818 goto cond_branch;
819 case BPF_JMP | BPF_JLT | BPF_K:
820 case BPF_JMP | BPF_JLT | BPF_X:
821 case BPF_JMP | BPF_JSLT | BPF_K:
822 case BPF_JMP | BPF_JSLT | BPF_X:
823 case BPF_JMP32 | BPF_JLT | BPF_K:
824 case BPF_JMP32 | BPF_JLT | BPF_X:
825 case BPF_JMP32 | BPF_JSLT | BPF_K:
826 case BPF_JMP32 | BPF_JSLT | BPF_X:
827 true_cond = COND_LT;
828 goto cond_branch;
829 case BPF_JMP | BPF_JGE | BPF_K:
830 case BPF_JMP | BPF_JGE | BPF_X:
831 case BPF_JMP | BPF_JSGE | BPF_K:
832 case BPF_JMP | BPF_JSGE | BPF_X:
833 case BPF_JMP32 | BPF_JGE | BPF_K:
834 case BPF_JMP32 | BPF_JGE | BPF_X:
835 case BPF_JMP32 | BPF_JSGE | BPF_K:
836 case BPF_JMP32 | BPF_JSGE | BPF_X:
837 true_cond = COND_GE;
838 goto cond_branch;
839 case BPF_JMP | BPF_JLE | BPF_K:
840 case BPF_JMP | BPF_JLE | BPF_X:
841 case BPF_JMP | BPF_JSLE | BPF_K:
842 case BPF_JMP | BPF_JSLE | BPF_X:
843 case BPF_JMP32 | BPF_JLE | BPF_K:
844 case BPF_JMP32 | BPF_JLE | BPF_X:
845 case BPF_JMP32 | BPF_JSLE | BPF_K:
846 case BPF_JMP32 | BPF_JSLE | BPF_X:
847 true_cond = COND_LE;
848 goto cond_branch;
849 case BPF_JMP | BPF_JEQ | BPF_K:
850 case BPF_JMP | BPF_JEQ | BPF_X:
851 case BPF_JMP32 | BPF_JEQ | BPF_K:
852 case BPF_JMP32 | BPF_JEQ | BPF_X:
853 true_cond = COND_EQ;
854 goto cond_branch;
855 case BPF_JMP | BPF_JNE | BPF_K:
856 case BPF_JMP | BPF_JNE | BPF_X:
857 case BPF_JMP32 | BPF_JNE | BPF_K:
858 case BPF_JMP32 | BPF_JNE | BPF_X:
859 true_cond = COND_NE;
860 goto cond_branch;
861 case BPF_JMP | BPF_JSET | BPF_K:
862 case BPF_JMP | BPF_JSET | BPF_X:
863 case BPF_JMP32 | BPF_JSET | BPF_K:
864 case BPF_JMP32 | BPF_JSET | BPF_X:
865 true_cond = COND_NE;
866 /* Fall through */
867
868 cond_branch:
869 switch (code) {
870 case BPF_JMP | BPF_JGT | BPF_X:
871 case BPF_JMP | BPF_JLT | BPF_X:
872 case BPF_JMP | BPF_JGE | BPF_X:
873 case BPF_JMP | BPF_JLE | BPF_X:
874 case BPF_JMP | BPF_JEQ | BPF_X:
875 case BPF_JMP | BPF_JNE | BPF_X:
876 case BPF_JMP32 | BPF_JGT | BPF_X:
877 case BPF_JMP32 | BPF_JLT | BPF_X:
878 case BPF_JMP32 | BPF_JGE | BPF_X:
879 case BPF_JMP32 | BPF_JLE | BPF_X:
880 case BPF_JMP32 | BPF_JEQ | BPF_X:
881 case BPF_JMP32 | BPF_JNE | BPF_X:
882 /* unsigned comparison */
883 if (BPF_CLASS(code) == BPF_JMP32)
884 EMIT(PPC_RAW_CMPLW(dst_reg, src_reg));
885 else
886 EMIT(PPC_RAW_CMPLD(dst_reg, src_reg));
887 break;
888 case BPF_JMP | BPF_JSGT | BPF_X:
889 case BPF_JMP | BPF_JSLT | BPF_X:
890 case BPF_JMP | BPF_JSGE | BPF_X:
891 case BPF_JMP | BPF_JSLE | BPF_X:
892 case BPF_JMP32 | BPF_JSGT | BPF_X:
893 case BPF_JMP32 | BPF_JSLT | BPF_X:
894 case BPF_JMP32 | BPF_JSGE | BPF_X:
895 case BPF_JMP32 | BPF_JSLE | BPF_X:
896 /* signed comparison */
897 if (BPF_CLASS(code) == BPF_JMP32)
898 EMIT(PPC_RAW_CMPW(dst_reg, src_reg));
899 else
900 EMIT(PPC_RAW_CMPD(dst_reg, src_reg));
901 break;
902 case BPF_JMP | BPF_JSET | BPF_X:
903 case BPF_JMP32 | BPF_JSET | BPF_X:
904 if (BPF_CLASS(code) == BPF_JMP) {
905 EMIT(PPC_RAW_AND_DOT(b2p[TMP_REG_1], dst_reg,
906 src_reg));
907 } else {
908 int tmp_reg = b2p[TMP_REG_1];
909
910 EMIT(PPC_RAW_AND(tmp_reg, dst_reg, src_reg));
911 EMIT(PPC_RAW_RLWINM_DOT(tmp_reg, tmp_reg, 0, 0,
912 31));
913 }
914 break;
915 case BPF_JMP | BPF_JNE | BPF_K:
916 case BPF_JMP | BPF_JEQ | BPF_K:
917 case BPF_JMP | BPF_JGT | BPF_K:
918 case BPF_JMP | BPF_JLT | BPF_K:
919 case BPF_JMP | BPF_JGE | BPF_K:
920 case BPF_JMP | BPF_JLE | BPF_K:
921 case BPF_JMP32 | BPF_JNE | BPF_K:
922 case BPF_JMP32 | BPF_JEQ | BPF_K:
923 case BPF_JMP32 | BPF_JGT | BPF_K:
924 case BPF_JMP32 | BPF_JLT | BPF_K:
925 case BPF_JMP32 | BPF_JGE | BPF_K:
926 case BPF_JMP32 | BPF_JLE | BPF_K:
927 {
928 bool is_jmp32 = BPF_CLASS(code) == BPF_JMP32;
929
930 /*
931 * Need sign-extended load, so only positive
932 * values can be used as imm in cmpldi
933 */
934 if (imm >= 0 && imm < 32768) {
935 if (is_jmp32)
936 EMIT(PPC_RAW_CMPLWI(dst_reg, imm));
937 else
938 EMIT(PPC_RAW_CMPLDI(dst_reg, imm));
939 } else {
940 /* sign-extending load */
941 PPC_LI32(b2p[TMP_REG_1], imm);
942 /* ... but unsigned comparison */
943 if (is_jmp32)
944 EMIT(PPC_RAW_CMPLW(dst_reg,
945 b2p[TMP_REG_1]));
946 else
947 EMIT(PPC_RAW_CMPLD(dst_reg,
948 b2p[TMP_REG_1]));
949 }
950 break;
951 }
952 case BPF_JMP | BPF_JSGT | BPF_K:
953 case BPF_JMP | BPF_JSLT | BPF_K:
954 case BPF_JMP | BPF_JSGE | BPF_K:
955 case BPF_JMP | BPF_JSLE | BPF_K:
956 case BPF_JMP32 | BPF_JSGT | BPF_K:
957 case BPF_JMP32 | BPF_JSLT | BPF_K:
958 case BPF_JMP32 | BPF_JSGE | BPF_K:
959 case BPF_JMP32 | BPF_JSLE | BPF_K:
960 {
961 bool is_jmp32 = BPF_CLASS(code) == BPF_JMP32;
962
963 /*
964 * signed comparison, so any 16-bit value
965 * can be used in cmpdi
966 */
967 if (imm >= -32768 && imm < 32768) {
968 if (is_jmp32)
969 EMIT(PPC_RAW_CMPWI(dst_reg, imm));
970 else
971 EMIT(PPC_RAW_CMPDI(dst_reg, imm));
972 } else {
973 PPC_LI32(b2p[TMP_REG_1], imm);
974 if (is_jmp32)
975 EMIT(PPC_RAW_CMPW(dst_reg,
976 b2p[TMP_REG_1]));
977 else
978 EMIT(PPC_RAW_CMPD(dst_reg,
979 b2p[TMP_REG_1]));
980 }
981 break;
982 }
983 case BPF_JMP | BPF_JSET | BPF_K:
984 case BPF_JMP32 | BPF_JSET | BPF_K:
985 /* andi does not sign-extend the immediate */
986 if (imm >= 0 && imm < 32768)
987 /* PPC_ANDI is _only/always_ dot-form */
988 EMIT(PPC_RAW_ANDI(b2p[TMP_REG_1], dst_reg, imm));
989 else {
990 int tmp_reg = b2p[TMP_REG_1];
991
992 PPC_LI32(tmp_reg, imm);
993 if (BPF_CLASS(code) == BPF_JMP) {
994 EMIT(PPC_RAW_AND_DOT(tmp_reg, dst_reg,
995 tmp_reg));
996 } else {
997 EMIT(PPC_RAW_AND(tmp_reg, dst_reg,
998 tmp_reg));
999 EMIT(PPC_RAW_RLWINM_DOT(tmp_reg, tmp_reg,
1000 0, 0, 31));
1001 }
1002 }
1003 break;
1004 }
1005 PPC_BCC(true_cond, addrs[i + 1 + off]);
1006 break;
1007
1008 /*
1009 * Tail call
1010 */
1011 case BPF_JMP | BPF_TAIL_CALL:
1012 ctx->seen |= SEEN_TAILCALL;
1013 bpf_jit_emit_tail_call(image, ctx, addrs[i + 1]);
1014 break;
1015
1016 default:
1017 /*
1018 * The filter contains something cruel & unusual.
1019 * We don't handle it, but also there shouldn't be
1020 * anything missing from our list.
1021 */
1022 pr_err_ratelimited("eBPF filter opcode %04x (@%d) unsupported\n",
1023 code, i);
1024 return -ENOTSUPP;
1025 }
1026 }
1027
1028 /* Set end-of-body-code address for exit. */
1029 addrs[i] = ctx->idx * 4;
1030
1031 return 0;
1032 }
1033
1034 /* Fix the branch target addresses for subprog calls */
bpf_jit_fixup_subprog_calls(struct bpf_prog * fp,u32 * image,struct codegen_context * ctx,u32 * addrs)1035 static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,
1036 struct codegen_context *ctx, u32 *addrs)
1037 {
1038 const struct bpf_insn *insn = fp->insnsi;
1039 bool func_addr_fixed;
1040 u64 func_addr;
1041 u32 tmp_idx;
1042 int i, ret;
1043
1044 for (i = 0; i < fp->len; i++) {
1045 /*
1046 * During the extra pass, only the branch target addresses for
1047 * the subprog calls need to be fixed. All other instructions
1048 * can left untouched.
1049 *
1050 * The JITed image length does not change because we already
1051 * ensure that the JITed instruction sequence for these calls
1052 * are of fixed length by padding them with NOPs.
1053 */
1054 if (insn[i].code == (BPF_JMP | BPF_CALL) &&
1055 insn[i].src_reg == BPF_PSEUDO_CALL) {
1056 ret = bpf_jit_get_func_addr(fp, &insn[i], true,
1057 &func_addr,
1058 &func_addr_fixed);
1059 if (ret < 0)
1060 return ret;
1061
1062 /*
1063 * Save ctx->idx as this would currently point to the
1064 * end of the JITed image and set it to the offset of
1065 * the instruction sequence corresponding to the
1066 * subprog call temporarily.
1067 */
1068 tmp_idx = ctx->idx;
1069 ctx->idx = addrs[i] / 4;
1070 bpf_jit_emit_func_call_rel(image, ctx, func_addr);
1071
1072 /*
1073 * Restore ctx->idx here. This is safe as the length
1074 * of the JITed sequence remains unchanged.
1075 */
1076 ctx->idx = tmp_idx;
1077 }
1078 }
1079
1080 return 0;
1081 }
1082
1083 struct powerpc64_jit_data {
1084 struct bpf_binary_header *header;
1085 u32 *addrs;
1086 u8 *image;
1087 u32 proglen;
1088 struct codegen_context ctx;
1089 };
1090
bpf_jit_needs_zext(void)1091 bool bpf_jit_needs_zext(void)
1092 {
1093 return true;
1094 }
1095
bpf_int_jit_compile(struct bpf_prog * fp)1096 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
1097 {
1098 u32 proglen;
1099 u32 alloclen;
1100 u8 *image = NULL;
1101 u32 *code_base;
1102 u32 *addrs;
1103 struct powerpc64_jit_data *jit_data;
1104 struct codegen_context cgctx;
1105 int pass;
1106 int flen;
1107 struct bpf_binary_header *bpf_hdr;
1108 struct bpf_prog *org_fp = fp;
1109 struct bpf_prog *tmp_fp;
1110 bool bpf_blinded = false;
1111 bool extra_pass = false;
1112
1113 if (!fp->jit_requested)
1114 return org_fp;
1115
1116 tmp_fp = bpf_jit_blind_constants(org_fp);
1117 if (IS_ERR(tmp_fp))
1118 return org_fp;
1119
1120 if (tmp_fp != org_fp) {
1121 bpf_blinded = true;
1122 fp = tmp_fp;
1123 }
1124
1125 jit_data = fp->aux->jit_data;
1126 if (!jit_data) {
1127 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
1128 if (!jit_data) {
1129 fp = org_fp;
1130 goto out;
1131 }
1132 fp->aux->jit_data = jit_data;
1133 }
1134
1135 flen = fp->len;
1136 addrs = jit_data->addrs;
1137 if (addrs) {
1138 cgctx = jit_data->ctx;
1139 image = jit_data->image;
1140 bpf_hdr = jit_data->header;
1141 proglen = jit_data->proglen;
1142 alloclen = proglen + FUNCTION_DESCR_SIZE;
1143 extra_pass = true;
1144 goto skip_init_ctx;
1145 }
1146
1147 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
1148 if (addrs == NULL) {
1149 fp = org_fp;
1150 goto out_addrs;
1151 }
1152
1153 memset(&cgctx, 0, sizeof(struct codegen_context));
1154
1155 /* Make sure that the stack is quadword aligned. */
1156 cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
1157
1158 /* Scouting faux-generate pass 0 */
1159 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
1160 /* We hit something illegal or unsupported. */
1161 fp = org_fp;
1162 goto out_addrs;
1163 }
1164
1165 /*
1166 * If we have seen a tail call, we need a second pass.
1167 * This is because bpf_jit_emit_common_epilogue() is called
1168 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
1169 */
1170 if (cgctx.seen & SEEN_TAILCALL) {
1171 cgctx.idx = 0;
1172 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
1173 fp = org_fp;
1174 goto out_addrs;
1175 }
1176 }
1177
1178 /*
1179 * Pretend to build prologue, given the features we've seen. This will
1180 * update ctgtx.idx as it pretends to output instructions, then we can
1181 * calculate total size from idx.
1182 */
1183 bpf_jit_build_prologue(0, &cgctx);
1184 bpf_jit_build_epilogue(0, &cgctx);
1185
1186 proglen = cgctx.idx * 4;
1187 alloclen = proglen + FUNCTION_DESCR_SIZE;
1188
1189 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4,
1190 bpf_jit_fill_ill_insns);
1191 if (!bpf_hdr) {
1192 fp = org_fp;
1193 goto out_addrs;
1194 }
1195
1196 skip_init_ctx:
1197 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
1198
1199 if (extra_pass) {
1200 /*
1201 * Do not touch the prologue and epilogue as they will remain
1202 * unchanged. Only fix the branch target address for subprog
1203 * calls in the body.
1204 *
1205 * This does not change the offsets and lengths of the subprog
1206 * call instruction sequences and hence, the size of the JITed
1207 * image as well.
1208 */
1209 bpf_jit_fixup_subprog_calls(fp, code_base, &cgctx, addrs);
1210
1211 /* There is no need to perform the usual passes. */
1212 goto skip_codegen_passes;
1213 }
1214
1215 /* Code generation passes 1-2 */
1216 for (pass = 1; pass < 3; pass++) {
1217 /* Now build the prologue, body code & epilogue for real. */
1218 cgctx.idx = 0;
1219 bpf_jit_build_prologue(code_base, &cgctx);
1220 bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass);
1221 bpf_jit_build_epilogue(code_base, &cgctx);
1222
1223 if (bpf_jit_enable > 1)
1224 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
1225 proglen - (cgctx.idx * 4), cgctx.seen);
1226 }
1227
1228 skip_codegen_passes:
1229 if (bpf_jit_enable > 1)
1230 /*
1231 * Note that we output the base address of the code_base
1232 * rather than image, since opcodes are in code_base.
1233 */
1234 bpf_jit_dump(flen, proglen, pass, code_base);
1235
1236 #ifdef PPC64_ELF_ABI_v1
1237 /* Function descriptor nastiness: Address + TOC */
1238 ((u64 *)image)[0] = (u64)code_base;
1239 ((u64 *)image)[1] = local_paca->kernel_toc;
1240 #endif
1241
1242 fp->bpf_func = (void *)image;
1243 fp->jited = 1;
1244 fp->jited_len = alloclen;
1245
1246 bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
1247 if (!fp->is_func || extra_pass) {
1248 bpf_prog_fill_jited_linfo(fp, addrs);
1249 out_addrs:
1250 kfree(addrs);
1251 kfree(jit_data);
1252 fp->aux->jit_data = NULL;
1253 } else {
1254 jit_data->addrs = addrs;
1255 jit_data->ctx = cgctx;
1256 jit_data->proglen = proglen;
1257 jit_data->image = image;
1258 jit_data->header = bpf_hdr;
1259 }
1260
1261 out:
1262 if (bpf_blinded)
1263 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
1264
1265 return fp;
1266 }
1267
1268 /* Overriding bpf_jit_free() as we don't set images read-only. */
bpf_jit_free(struct bpf_prog * fp)1269 void bpf_jit_free(struct bpf_prog *fp)
1270 {
1271 unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1272 struct bpf_binary_header *bpf_hdr = (void *)addr;
1273
1274 if (fp->jited)
1275 bpf_jit_binary_free(bpf_hdr);
1276
1277 bpf_prog_unlock_free(fp);
1278 }
1279