1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
8 * Based on the powerpc classic BPF JIT compiler by Matt Evans
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 <linux/kernel.h>
17 #include <linux/memory.h>
18 #include <linux/bpf.h>
20 #include <asm/kprobes.h>
21 #include <asm/text-patching.h>
25 /* These offsets are from bpf prog end and stay the same across progs */
26 static int bpf_jit_ool_stub
, bpf_jit_long_branch_stub
;
28 static void bpf_jit_fill_ill_insns(void *area
, unsigned int size
)
30 memset32(area
, BREAKPOINT_INSTRUCTION
, size
/ 4);
33 void dummy_tramp(void);
36 " .pushsection .text, \"ax\", @progbits ;"
37 " .global dummy_tramp ;"
38 " .type dummy_tramp, @function ;"
40 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
43 /* LR is always in r11, so we don't need a 'mflr r11' here */
48 " .size dummy_tramp, .-dummy_tramp ;"
52 void bpf_jit_build_fentry_stubs(u32
*image
, struct codegen_context
*ctx
)
54 int ool_stub_idx
, long_branch_stub_idx
;
60 * mtlr r0 // only with CONFIG_PPC_FTRACE_OUT_OF_LINE
63 ool_stub_idx
= ctx
->idx
;
64 EMIT(PPC_RAW_MFLR(_R0
));
66 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE
))
67 EMIT(PPC_RAW_MTLR(_R0
));
68 WARN_ON_ONCE(!is_offset_in_branch_range(4 - (long)ctx
->idx
* 4));
69 EMIT(PPC_RAW_BRANCH(4 - (long)ctx
->idx
* 4));
73 * .long <dummy_tramp_addr>
79 * mtlr r11 // needed to retain ftrace ABI
83 *((unsigned long *)&image
[ctx
->idx
]) = (unsigned long)dummy_tramp
;
85 long_branch_stub_idx
= ctx
->idx
;
86 EMIT(PPC_RAW_MFLR(_R11
));
88 EMIT(PPC_RAW_MFLR(_R12
));
89 EMIT(PPC_RAW_LL(_R12
, _R12
, -8-SZL
));
90 EMIT(PPC_RAW_MTCTR(_R12
));
91 EMIT(PPC_RAW_MTLR(_R11
));
94 if (!bpf_jit_ool_stub
) {
95 bpf_jit_ool_stub
= (ctx
->idx
- ool_stub_idx
) * 4;
96 bpf_jit_long_branch_stub
= (ctx
->idx
- long_branch_stub_idx
) * 4;
100 int bpf_jit_emit_exit_insn(u32
*image
, struct codegen_context
*ctx
, int tmp_reg
, long exit_addr
)
102 if (!exit_addr
|| is_offset_in_branch_range(exit_addr
- (ctx
->idx
* 4))) {
104 } else if (ctx
->alt_exit_addr
) {
105 if (WARN_ON(!is_offset_in_branch_range((long)ctx
->alt_exit_addr
- (ctx
->idx
* 4))))
107 PPC_JMP(ctx
->alt_exit_addr
);
109 ctx
->alt_exit_addr
= ctx
->idx
* 4;
110 bpf_jit_build_epilogue(image
, ctx
);
116 struct powerpc_jit_data
{
117 /* address of rw header */
118 struct bpf_binary_header
*hdr
;
119 /* address of ro final header */
120 struct bpf_binary_header
*fhdr
;
124 struct codegen_context ctx
;
127 bool bpf_jit_needs_zext(void)
132 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*fp
)
139 struct powerpc_jit_data
*jit_data
;
140 struct codegen_context cgctx
;
143 struct bpf_binary_header
*fhdr
= NULL
;
144 struct bpf_binary_header
*hdr
= NULL
;
145 struct bpf_prog
*org_fp
= fp
;
146 struct bpf_prog
*tmp_fp
;
147 bool bpf_blinded
= false;
148 bool extra_pass
= false;
154 if (!fp
->jit_requested
)
157 tmp_fp
= bpf_jit_blind_constants(org_fp
);
161 if (tmp_fp
!= org_fp
) {
166 jit_data
= fp
->aux
->jit_data
;
168 jit_data
= kzalloc(sizeof(*jit_data
), GFP_KERNEL
);
173 fp
->aux
->jit_data
= jit_data
;
177 addrs
= jit_data
->addrs
;
179 cgctx
= jit_data
->ctx
;
181 * JIT compiled to a writable location (image/code_base) first.
182 * It is then moved to the readonly final location (fimage/fcode_base)
183 * using instruction patching.
185 fimage
= jit_data
->fimage
;
186 fhdr
= jit_data
->fhdr
;
187 proglen
= jit_data
->proglen
;
189 image
= (void *)hdr
+ ((void *)fimage
- (void *)fhdr
);
191 /* During extra pass, ensure index is reset before repopulating extable entries */
192 cgctx
.exentry_idx
= 0;
196 addrs
= kcalloc(flen
+ 1, sizeof(*addrs
), GFP_KERNEL
);
202 memset(&cgctx
, 0, sizeof(struct codegen_context
));
203 bpf_jit_init_reg_mapping(&cgctx
);
205 /* Make sure that the stack is quadword aligned. */
206 cgctx
.stack_size
= round_up(fp
->aux
->stack_depth
, 16);
208 /* Scouting faux-generate pass 0 */
209 if (bpf_jit_build_body(fp
, NULL
, NULL
, &cgctx
, addrs
, 0, false)) {
210 /* We hit something illegal or unsupported. */
216 * If we have seen a tail call, we need a second pass.
217 * This is because bpf_jit_emit_common_epilogue() is called
218 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
219 * We also need a second pass if we ended up with too large
220 * a program so as to ensure BPF_EXIT branches are in range.
222 if (cgctx
.seen
& SEEN_TAILCALL
|| !is_offset_in_branch_range((long)cgctx
.idx
* 4)) {
224 if (bpf_jit_build_body(fp
, NULL
, NULL
, &cgctx
, addrs
, 0, false)) {
230 bpf_jit_realloc_regs(&cgctx
);
232 * Pretend to build prologue, given the features we've seen. This will
233 * update ctgtx.idx as it pretends to output instructions, then we can
234 * calculate total size from idx.
236 bpf_jit_build_prologue(NULL
, &cgctx
);
237 addrs
[fp
->len
] = cgctx
.idx
* 4;
238 bpf_jit_build_epilogue(NULL
, &cgctx
);
240 fixup_len
= fp
->aux
->num_exentries
* BPF_FIXUP_LEN
* 4;
241 extable_len
= fp
->aux
->num_exentries
* sizeof(struct exception_table_entry
);
243 proglen
= cgctx
.idx
* 4;
244 alloclen
= proglen
+ FUNCTION_DESCR_SIZE
+ fixup_len
+ extable_len
;
246 fhdr
= bpf_jit_binary_pack_alloc(alloclen
, &fimage
, 4, &hdr
, &image
,
247 bpf_jit_fill_ill_insns
);
254 fp
->aux
->extable
= (void *)fimage
+ FUNCTION_DESCR_SIZE
+ proglen
+ fixup_len
;
257 code_base
= (u32
*)(image
+ FUNCTION_DESCR_SIZE
);
258 fcode_base
= (u32
*)(fimage
+ FUNCTION_DESCR_SIZE
);
260 /* Code generation passes 1-2 */
261 for (pass
= 1; pass
< 3; pass
++) {
262 /* Now build the prologue, body code & epilogue for real. */
264 cgctx
.alt_exit_addr
= 0;
265 bpf_jit_build_prologue(code_base
, &cgctx
);
266 if (bpf_jit_build_body(fp
, code_base
, fcode_base
, &cgctx
, addrs
, pass
,
268 bpf_arch_text_copy(&fhdr
->size
, &hdr
->size
, sizeof(hdr
->size
));
269 bpf_jit_binary_pack_free(fhdr
, hdr
);
273 bpf_jit_build_epilogue(code_base
, &cgctx
);
275 if (bpf_jit_enable
> 1)
276 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass
,
277 proglen
- (cgctx
.idx
* 4), cgctx
.seen
);
280 if (bpf_jit_enable
> 1)
282 * Note that we output the base address of the code_base
283 * rather than image, since opcodes are in code_base.
285 bpf_jit_dump(flen
, proglen
, pass
, code_base
);
287 #ifdef CONFIG_PPC64_ELF_ABI_V1
288 /* Function descriptor nastiness: Address + TOC */
289 ((u64
*)image
)[0] = (u64
)fcode_base
;
290 ((u64
*)image
)[1] = local_paca
->kernel_toc
;
293 fp
->bpf_func
= (void *)fimage
;
295 fp
->jited_len
= cgctx
.idx
* 4 + FUNCTION_DESCR_SIZE
;
297 if (!fp
->is_func
|| extra_pass
) {
298 if (bpf_jit_binary_pack_finalize(fhdr
, hdr
)) {
302 bpf_prog_fill_jited_linfo(fp
, addrs
);
306 fp
->aux
->jit_data
= NULL
;
308 jit_data
->addrs
= addrs
;
309 jit_data
->ctx
= cgctx
;
310 jit_data
->proglen
= proglen
;
311 jit_data
->fimage
= fimage
;
312 jit_data
->fhdr
= fhdr
;
318 bpf_jit_prog_release_other(fp
, fp
== org_fp
? tmp_fp
: org_fp
);
324 * The caller should check for (BPF_MODE(code) == BPF_PROBE_MEM) before calling
325 * this function, as this only applies to BPF_PROBE_MEM, for now.
327 int bpf_add_extable_entry(struct bpf_prog
*fp
, u32
*image
, u32
*fimage
, int pass
,
328 struct codegen_context
*ctx
, int insn_idx
, int jmp_off
,
333 struct exception_table_entry
*ex
, *ex_entry
;
336 /* Populate extable entries only in the last pass */
340 if (!fp
->aux
->extable
||
341 WARN_ON_ONCE(ctx
->exentry_idx
>= fp
->aux
->num_exentries
))
345 * Program is first written to image before copying to the
346 * final location (fimage). Accordingly, update in the image first.
347 * As all offsets used are relative, copying as is to the
348 * final location should be alright.
350 pc
= (unsigned long)&image
[insn_idx
];
351 ex
= (void *)fp
->aux
->extable
- (void *)fimage
+ (void *)image
;
354 (fp
->aux
->num_exentries
* BPF_FIXUP_LEN
* 4) +
355 (ctx
->exentry_idx
* BPF_FIXUP_LEN
* 4);
357 fixup
[0] = PPC_RAW_LI(dst_reg
, 0);
358 if (IS_ENABLED(CONFIG_PPC32
))
359 fixup
[1] = PPC_RAW_LI(dst_reg
- 1, 0); /* clear higher 32-bit register too */
361 fixup
[BPF_FIXUP_LEN
- 1] =
362 PPC_RAW_BRANCH((long)(pc
+ jmp_off
) - (long)&fixup
[BPF_FIXUP_LEN
- 1]);
364 ex_entry
= &ex
[ctx
->exentry_idx
];
366 offset
= pc
- (long)&ex_entry
->insn
;
367 if (WARN_ON_ONCE(offset
>= 0 || offset
< INT_MIN
))
369 ex_entry
->insn
= offset
;
371 offset
= (long)fixup
- (long)&ex_entry
->fixup
;
372 if (WARN_ON_ONCE(offset
>= 0 || offset
< INT_MIN
))
374 ex_entry
->fixup
= offset
;
380 void *bpf_arch_text_copy(void *dst
, void *src
, size_t len
)
384 if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst
)))
385 return ERR_PTR(-EINVAL
);
387 mutex_lock(&text_mutex
);
388 err
= patch_instructions(dst
, src
, len
, false);
389 mutex_unlock(&text_mutex
);
391 return err
? ERR_PTR(err
) : dst
;
394 int bpf_arch_text_invalidate(void *dst
, size_t len
)
396 u32 insn
= BREAKPOINT_INSTRUCTION
;
399 if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst
)))
402 mutex_lock(&text_mutex
);
403 ret
= patch_instructions(dst
, &insn
, len
, true);
404 mutex_unlock(&text_mutex
);
409 void bpf_jit_free(struct bpf_prog
*fp
)
412 struct powerpc_jit_data
*jit_data
= fp
->aux
->jit_data
;
413 struct bpf_binary_header
*hdr
;
416 * If we fail the final pass of JIT (from jit_subprogs),
417 * the program may not be finalized yet. Call finalize here
421 bpf_jit_binary_pack_finalize(jit_data
->fhdr
, jit_data
->hdr
);
422 kvfree(jit_data
->addrs
);
425 hdr
= bpf_jit_binary_pack_hdr(fp
);
426 bpf_jit_binary_pack_free(hdr
, NULL
);
427 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp
));
430 bpf_prog_unlock_free(fp
);
433 bool bpf_jit_supports_kfunc_call(void)
438 bool bpf_jit_supports_far_kfunc_call(void)
440 return IS_ENABLED(CONFIG_PPC64
);
443 void *arch_alloc_bpf_trampoline(unsigned int size
)
445 return bpf_prog_pack_alloc(size
, bpf_jit_fill_ill_insns
);
448 void arch_free_bpf_trampoline(void *image
, unsigned int size
)
450 bpf_prog_pack_free(image
, size
);
453 int arch_protect_bpf_trampoline(void *image
, unsigned int size
)
458 static int invoke_bpf_prog(u32
*image
, u32
*ro_image
, struct codegen_context
*ctx
,
459 struct bpf_tramp_link
*l
, int regs_off
, int retval_off
,
460 int run_ctx_off
, bool save_ret
)
462 struct bpf_prog
*p
= l
->link
.prog
;
463 ppc_inst_t branch_insn
;
468 if (IS_ENABLED(CONFIG_PPC64
)) {
469 PPC_LI64(_R3
, l
->cookie
);
470 EMIT(PPC_RAW_STD(_R3
, _R1
, run_ctx_off
+ offsetof(struct bpf_tramp_run_ctx
,
473 PPC_LI32(_R3
, l
->cookie
>> 32);
474 PPC_LI32(_R4
, l
->cookie
);
475 EMIT(PPC_RAW_STW(_R3
, _R1
,
476 run_ctx_off
+ offsetof(struct bpf_tramp_run_ctx
, bpf_cookie
)));
477 EMIT(PPC_RAW_STW(_R4
, _R1
,
478 run_ctx_off
+ offsetof(struct bpf_tramp_run_ctx
, bpf_cookie
) + 4));
481 /* __bpf_prog_enter(p, &bpf_tramp_run_ctx) */
483 EMIT(PPC_RAW_MR(_R25
, _R3
));
484 EMIT(PPC_RAW_ADDI(_R4
, _R1
, run_ctx_off
));
485 ret
= bpf_jit_emit_func_call_rel(image
, ro_image
, ctx
,
486 (unsigned long)bpf_trampoline_enter(p
));
490 /* Remember prog start time returned by __bpf_prog_enter */
491 EMIT(PPC_RAW_MR(_R26
, _R3
));
494 * if (__bpf_prog_enter(p) == 0)
495 * goto skip_exec_of_prog;
497 * Emit a nop to be later patched with conditional branch, once offset is known
499 EMIT(PPC_RAW_CMPLI(_R3
, 0));
503 /* p->bpf_func(ctx) */
504 EMIT(PPC_RAW_ADDI(_R3
, _R1
, regs_off
));
506 PPC_LI_ADDR(_R4
, (unsigned long)p
->insnsi
);
507 if (!create_branch(&branch_insn
, (u32
*)&ro_image
[ctx
->idx
], (unsigned long)p
->bpf_func
,
510 image
[ctx
->idx
] = ppc_inst_val(branch_insn
);
513 EMIT(PPC_RAW_LL(_R12
, _R25
, offsetof(struct bpf_prog
, bpf_func
)));
514 EMIT(PPC_RAW_MTCTR(_R12
));
515 EMIT(PPC_RAW_BCTRL());
519 EMIT(PPC_RAW_STL(_R3
, _R1
, retval_off
));
523 if (create_cond_branch(&branch_insn
, &image
[jmp_idx
],
524 (unsigned long)&image
[ctx
->idx
], COND_EQ
<< 16))
526 image
[jmp_idx
] = ppc_inst_val(branch_insn
);
529 /* __bpf_prog_exit(p, start_time, &bpf_tramp_run_ctx) */
530 EMIT(PPC_RAW_MR(_R3
, _R25
));
531 EMIT(PPC_RAW_MR(_R4
, _R26
));
532 EMIT(PPC_RAW_ADDI(_R5
, _R1
, run_ctx_off
));
533 ret
= bpf_jit_emit_func_call_rel(image
, ro_image
, ctx
,
534 (unsigned long)bpf_trampoline_exit(p
));
539 static int invoke_bpf_mod_ret(u32
*image
, u32
*ro_image
, struct codegen_context
*ctx
,
540 struct bpf_tramp_links
*tl
, int regs_off
, int retval_off
,
541 int run_ctx_off
, u32
*branches
)
546 * The first fmod_ret program will receive a garbage return value.
547 * Set this to 0 to avoid confusing the program.
549 EMIT(PPC_RAW_LI(_R3
, 0));
550 EMIT(PPC_RAW_STL(_R3
, _R1
, retval_off
));
551 for (i
= 0; i
< tl
->nr_links
; i
++) {
552 if (invoke_bpf_prog(image
, ro_image
, ctx
, tl
->links
[i
], regs_off
, retval_off
,
557 * mod_ret prog stored return value after prog ctx. Emit:
558 * if (*(u64 *)(ret_val) != 0)
561 EMIT(PPC_RAW_LL(_R3
, _R1
, retval_off
));
562 EMIT(PPC_RAW_CMPLI(_R3
, 0));
565 * Save the location of the branch and generate a nop, which is
566 * replaced with a conditional jump once do_fexit (i.e. the
567 * start of the fexit invocation) is finalized.
569 branches
[i
] = ctx
->idx
;
576 static void bpf_trampoline_setup_tail_call_cnt(u32
*image
, struct codegen_context
*ctx
,
577 int func_frame_offset
, int r4_off
)
579 if (IS_ENABLED(CONFIG_PPC64
)) {
580 /* See bpf_jit_stack_tailcallcnt() */
581 int tailcallcnt_offset
= 6 * 8;
583 EMIT(PPC_RAW_LL(_R3
, _R1
, func_frame_offset
- tailcallcnt_offset
));
584 EMIT(PPC_RAW_STL(_R3
, _R1
, -tailcallcnt_offset
));
586 /* See bpf_jit_stack_offsetof() and BPF_PPC_TC */
587 EMIT(PPC_RAW_LL(_R4
, _R1
, r4_off
));
591 static void bpf_trampoline_restore_tail_call_cnt(u32
*image
, struct codegen_context
*ctx
,
592 int func_frame_offset
, int r4_off
)
594 if (IS_ENABLED(CONFIG_PPC64
)) {
595 /* See bpf_jit_stack_tailcallcnt() */
596 int tailcallcnt_offset
= 6 * 8;
598 EMIT(PPC_RAW_LL(_R3
, _R1
, -tailcallcnt_offset
));
599 EMIT(PPC_RAW_STL(_R3
, _R1
, func_frame_offset
- tailcallcnt_offset
));
601 /* See bpf_jit_stack_offsetof() and BPF_PPC_TC */
602 EMIT(PPC_RAW_STL(_R4
, _R1
, r4_off
));
606 static void bpf_trampoline_save_args(u32
*image
, struct codegen_context
*ctx
, int func_frame_offset
,
607 int nr_regs
, int regs_off
)
609 int param_save_area_offset
;
611 param_save_area_offset
= func_frame_offset
; /* the two frames we alloted */
612 param_save_area_offset
+= STACK_FRAME_MIN_SIZE
; /* param save area is past frame header */
614 for (int i
= 0; i
< nr_regs
; i
++) {
616 EMIT(PPC_RAW_STL(_R3
+ i
, _R1
, regs_off
+ i
* SZL
));
618 EMIT(PPC_RAW_LL(_R3
, _R1
, param_save_area_offset
+ i
* SZL
));
619 EMIT(PPC_RAW_STL(_R3
, _R1
, regs_off
+ i
* SZL
));
624 /* Used when restoring just the register parameters when returning back */
625 static void bpf_trampoline_restore_args_regs(u32
*image
, struct codegen_context
*ctx
,
626 int nr_regs
, int regs_off
)
628 for (int i
= 0; i
< nr_regs
&& i
< 8; i
++)
629 EMIT(PPC_RAW_LL(_R3
+ i
, _R1
, regs_off
+ i
* SZL
));
632 /* Used when we call into the traced function. Replicate parameter save area */
633 static void bpf_trampoline_restore_args_stack(u32
*image
, struct codegen_context
*ctx
,
634 int func_frame_offset
, int nr_regs
, int regs_off
)
636 int param_save_area_offset
;
638 param_save_area_offset
= func_frame_offset
; /* the two frames we alloted */
639 param_save_area_offset
+= STACK_FRAME_MIN_SIZE
; /* param save area is past frame header */
641 for (int i
= 8; i
< nr_regs
; i
++) {
642 EMIT(PPC_RAW_LL(_R3
, _R1
, param_save_area_offset
+ i
* SZL
));
643 EMIT(PPC_RAW_STL(_R3
, _R1
, STACK_FRAME_MIN_SIZE
+ i
* SZL
));
645 bpf_trampoline_restore_args_regs(image
, ctx
, nr_regs
, regs_off
);
648 static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image
*im
, void *rw_image
,
649 void *rw_image_end
, void *ro_image
,
650 const struct btf_func_model
*m
, u32 flags
,
651 struct bpf_tramp_links
*tlinks
,
654 int regs_off
, nregs_off
, ip_off
, run_ctx_off
, retval_off
, nvr_off
, alt_lr_off
, r4_off
= 0;
655 int i
, ret
, nr_regs
, bpf_frame_size
= 0, bpf_dummy_frame_size
= 0, func_frame_offset
;
656 struct bpf_tramp_links
*fmod_ret
= &tlinks
[BPF_TRAMP_MODIFY_RETURN
];
657 struct bpf_tramp_links
*fentry
= &tlinks
[BPF_TRAMP_FENTRY
];
658 struct bpf_tramp_links
*fexit
= &tlinks
[BPF_TRAMP_FEXIT
];
659 struct codegen_context codegen_ctx
, *ctx
;
660 u32
*image
= (u32
*)rw_image
;
661 ppc_inst_t branch_insn
;
662 u32
*branches
= NULL
;
665 if (IS_ENABLED(CONFIG_PPC32
))
668 nr_regs
= m
->nr_args
;
669 /* Extra registers for struct arguments */
670 for (i
= 0; i
< m
->nr_args
; i
++)
671 if (m
->arg_size
[i
] > SZL
)
672 nr_regs
+= round_up(m
->arg_size
[i
], SZL
) / SZL
- 1;
674 if (nr_regs
> MAX_BPF_FUNC_ARGS
)
678 memset(ctx
, 0, sizeof(*ctx
));
681 * Generated stack layout:
683 * func prev back chain [ back chain ]
685 * bpf prog redzone/tailcallcnt [ ... ] 64 bytes (64-bit powerpc)
687 * LR save area [ r0 save (64-bit) ] | header
688 * [ r0 save (32-bit) ] |
689 * dummy frame for unwind [ back chain 1 ] --
690 * [ padding ] align stack frame
691 * r4_off [ r4 (tailcallcnt) ] optional - 32-bit powerpc
692 * alt_lr_off [ real lr (ool stub)] optional - actual lr
694 * nvr_off [ r25 ] nvr save area
695 * retval_off [ return value ]
698 * regs_off [ reg_arg1 ] prog ctx context
699 * nregs_off [ args count ]
700 * ip_off [ traced function ]
702 * run_ctx_off [ bpf_tramp_run_ctx ]
705 * param_save_area [ reg_arg1 ] min 8 doublewords, per ABI
706 * [ TOC save (64-bit) ] --
707 * [ LR save (64-bit) ] | header
708 * [ LR save (32-bit) ] |
709 * bpf trampoline frame [ back chain 2 ] --
713 /* Minimum stack frame header */
714 bpf_frame_size
= STACK_FRAME_MIN_SIZE
;
717 * Room for parameter save area.
719 * As per the ABI, this is required if we call into the traced
720 * function (BPF_TRAMP_F_CALL_ORIG):
721 * - if the function takes more than 8 arguments for the rest to spill onto the stack
722 * - or, if the function has variadic arguments
723 * - or, if this functions's prototype was not available to the caller
725 * Reserve space for at least 8 registers for now. This can be optimized later.
727 bpf_frame_size
+= (nr_regs
> 8 ? nr_regs
: 8) * SZL
;
729 /* Room for struct bpf_tramp_run_ctx */
730 run_ctx_off
= bpf_frame_size
;
731 bpf_frame_size
+= round_up(sizeof(struct bpf_tramp_run_ctx
), SZL
);
733 /* Room for IP address argument */
734 ip_off
= bpf_frame_size
;
735 if (flags
& BPF_TRAMP_F_IP_ARG
)
736 bpf_frame_size
+= SZL
;
738 /* Room for args count */
739 nregs_off
= bpf_frame_size
;
740 bpf_frame_size
+= SZL
;
743 regs_off
= bpf_frame_size
;
744 bpf_frame_size
+= nr_regs
* SZL
;
746 /* Room for return value of func_addr or fentry prog */
747 retval_off
= bpf_frame_size
;
748 save_ret
= flags
& (BPF_TRAMP_F_CALL_ORIG
| BPF_TRAMP_F_RET_FENTRY_RET
);
750 bpf_frame_size
+= SZL
;
752 /* Room for nvr save area */
753 nvr_off
= bpf_frame_size
;
754 bpf_frame_size
+= 2 * SZL
;
756 /* Optional save area for actual LR in case of ool ftrace */
757 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE
)) {
758 alt_lr_off
= bpf_frame_size
;
759 bpf_frame_size
+= SZL
;
762 if (IS_ENABLED(CONFIG_PPC32
)) {
764 r4_off
= bpf_frame_size
;
765 bpf_frame_size
+= SZL
;
767 r4_off
= regs_off
+ SZL
;
771 /* Padding to align stack frame, if any */
772 bpf_frame_size
= round_up(bpf_frame_size
, SZL
* 2);
774 /* Dummy frame size for proper unwind - includes 64-bytes red zone for 64-bit powerpc */
775 bpf_dummy_frame_size
= STACK_FRAME_MIN_SIZE
+ 64;
777 /* Offset to the traced function's stack frame */
778 func_frame_offset
= bpf_dummy_frame_size
+ bpf_frame_size
;
780 /* Create dummy frame for unwind, store original return value */
781 EMIT(PPC_RAW_STL(_R0
, _R1
, PPC_LR_STKOFF
));
782 /* Protect red zone where tail call count goes */
783 EMIT(PPC_RAW_STLU(_R1
, _R1
, -bpf_dummy_frame_size
));
785 /* Create our stack frame */
786 EMIT(PPC_RAW_STLU(_R1
, _R1
, -bpf_frame_size
));
788 /* 64-bit: Save TOC and load kernel TOC */
789 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2
) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL
)) {
790 EMIT(PPC_RAW_STD(_R2
, _R1
, 24));
794 /* 32-bit: save tail call count in r4 */
795 if (IS_ENABLED(CONFIG_PPC32
) && nr_regs
< 2)
796 EMIT(PPC_RAW_STL(_R4
, _R1
, r4_off
));
798 bpf_trampoline_save_args(image
, ctx
, func_frame_offset
, nr_regs
, regs_off
);
800 /* Save our return address */
801 EMIT(PPC_RAW_MFLR(_R3
));
802 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE
))
803 EMIT(PPC_RAW_STL(_R3
, _R1
, alt_lr_off
));
805 EMIT(PPC_RAW_STL(_R3
, _R1
, bpf_frame_size
+ PPC_LR_STKOFF
));
808 * Save ip address of the traced function.
809 * We could recover this from LR, but we will need to address for OOL trampoline,
810 * and optional GEP area.
812 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE
) || flags
& BPF_TRAMP_F_IP_ARG
) {
813 EMIT(PPC_RAW_LWZ(_R4
, _R3
, 4));
814 EMIT(PPC_RAW_SLWI(_R4
, _R4
, 6));
815 EMIT(PPC_RAW_SRAWI(_R4
, _R4
, 6));
816 EMIT(PPC_RAW_ADD(_R3
, _R3
, _R4
));
817 EMIT(PPC_RAW_ADDI(_R3
, _R3
, 4));
820 if (flags
& BPF_TRAMP_F_IP_ARG
)
821 EMIT(PPC_RAW_STL(_R3
, _R1
, ip_off
));
823 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE
))
824 /* Fake our LR for unwind */
825 EMIT(PPC_RAW_STL(_R3
, _R1
, bpf_frame_size
+ PPC_LR_STKOFF
));
827 /* Save function arg count -- see bpf_get_func_arg_cnt() */
828 EMIT(PPC_RAW_LI(_R3
, nr_regs
));
829 EMIT(PPC_RAW_STL(_R3
, _R1
, nregs_off
));
832 EMIT(PPC_RAW_STL(_R25
, _R1
, nvr_off
));
833 EMIT(PPC_RAW_STL(_R26
, _R1
, nvr_off
+ SZL
));
835 if (flags
& BPF_TRAMP_F_CALL_ORIG
) {
836 PPC_LI_ADDR(_R3
, (unsigned long)im
);
837 ret
= bpf_jit_emit_func_call_rel(image
, ro_image
, ctx
,
838 (unsigned long)__bpf_tramp_enter
);
843 for (i
= 0; i
< fentry
->nr_links
; i
++)
844 if (invoke_bpf_prog(image
, ro_image
, ctx
, fentry
->links
[i
], regs_off
, retval_off
,
845 run_ctx_off
, flags
& BPF_TRAMP_F_RET_FENTRY_RET
))
848 if (fmod_ret
->nr_links
) {
849 branches
= kcalloc(fmod_ret
->nr_links
, sizeof(u32
), GFP_KERNEL
);
853 if (invoke_bpf_mod_ret(image
, ro_image
, ctx
, fmod_ret
, regs_off
, retval_off
,
854 run_ctx_off
, branches
)) {
860 /* Call the traced function */
861 if (flags
& BPF_TRAMP_F_CALL_ORIG
) {
863 * The address in LR save area points to the correct point in the original function
864 * with both PPC_FTRACE_OUT_OF_LINE as well as with traditional ftrace instruction
867 EMIT(PPC_RAW_LL(_R3
, _R1
, bpf_frame_size
+ PPC_LR_STKOFF
));
868 EMIT(PPC_RAW_MTCTR(_R3
));
870 /* Replicate tail_call_cnt before calling the original BPF prog */
871 if (flags
& BPF_TRAMP_F_TAIL_CALL_CTX
)
872 bpf_trampoline_setup_tail_call_cnt(image
, ctx
, func_frame_offset
, r4_off
);
875 bpf_trampoline_restore_args_stack(image
, ctx
, func_frame_offset
, nr_regs
, regs_off
);
877 /* Restore TOC for 64-bit */
878 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2
) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL
))
879 EMIT(PPC_RAW_LD(_R2
, _R1
, 24));
880 EMIT(PPC_RAW_BCTRL());
881 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2
) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL
))
884 /* Store return value for bpf prog to access */
885 EMIT(PPC_RAW_STL(_R3
, _R1
, retval_off
));
887 /* Restore updated tail_call_cnt */
888 if (flags
& BPF_TRAMP_F_TAIL_CALL_CTX
)
889 bpf_trampoline_restore_tail_call_cnt(image
, ctx
, func_frame_offset
, r4_off
);
891 /* Reserve space to patch branch instruction to skip fexit progs */
892 im
->ip_after_call
= &((u32
*)ro_image
)[ctx
->idx
];
896 /* Update branches saved in invoke_bpf_mod_ret with address of do_fexit */
897 for (i
= 0; i
< fmod_ret
->nr_links
&& image
; i
++) {
898 if (create_cond_branch(&branch_insn
, &image
[branches
[i
]],
899 (unsigned long)&image
[ctx
->idx
], COND_NE
<< 16)) {
904 image
[branches
[i
]] = ppc_inst_val(branch_insn
);
907 for (i
= 0; i
< fexit
->nr_links
; i
++)
908 if (invoke_bpf_prog(image
, ro_image
, ctx
, fexit
->links
[i
], regs_off
, retval_off
,
909 run_ctx_off
, false)) {
914 if (flags
& BPF_TRAMP_F_CALL_ORIG
) {
915 im
->ip_epilogue
= &((u32
*)ro_image
)[ctx
->idx
];
916 PPC_LI_ADDR(_R3
, im
);
917 ret
= bpf_jit_emit_func_call_rel(image
, ro_image
, ctx
,
918 (unsigned long)__bpf_tramp_exit
);
923 if (flags
& BPF_TRAMP_F_RESTORE_REGS
)
924 bpf_trampoline_restore_args_regs(image
, ctx
, nr_regs
, regs_off
);
926 /* Restore return value of func_addr or fentry prog */
928 EMIT(PPC_RAW_LL(_R3
, _R1
, retval_off
));
930 /* Restore nv regs */
931 EMIT(PPC_RAW_LL(_R26
, _R1
, nvr_off
+ SZL
));
932 EMIT(PPC_RAW_LL(_R25
, _R1
, nvr_off
));
935 if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2
) && !IS_ENABLED(CONFIG_PPC_KERNEL_PCREL
))
936 EMIT(PPC_RAW_LD(_R2
, _R1
, 24));
937 if (flags
& BPF_TRAMP_F_SKIP_FRAME
) {
938 /* Skip the traced function and return to parent */
939 EMIT(PPC_RAW_ADDI(_R1
, _R1
, func_frame_offset
));
940 EMIT(PPC_RAW_LL(_R0
, _R1
, PPC_LR_STKOFF
));
941 EMIT(PPC_RAW_MTLR(_R0
));
944 if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE
)) {
945 EMIT(PPC_RAW_LL(_R0
, _R1
, alt_lr_off
));
946 EMIT(PPC_RAW_MTLR(_R0
));
947 EMIT(PPC_RAW_ADDI(_R1
, _R1
, func_frame_offset
));
948 EMIT(PPC_RAW_LL(_R0
, _R1
, PPC_LR_STKOFF
));
951 EMIT(PPC_RAW_LL(_R0
, _R1
, bpf_frame_size
+ PPC_LR_STKOFF
));
952 EMIT(PPC_RAW_MTCTR(_R0
));
953 EMIT(PPC_RAW_ADDI(_R1
, _R1
, func_frame_offset
));
954 EMIT(PPC_RAW_LL(_R0
, _R1
, PPC_LR_STKOFF
));
955 EMIT(PPC_RAW_MTLR(_R0
));
956 EMIT(PPC_RAW_BCTR());
960 /* Make sure the trampoline generation logic doesn't overflow */
961 if (image
&& WARN_ON_ONCE(&image
[ctx
->idx
] > (u32
*)rw_image_end
- BPF_INSN_SAFETY
)) {
965 ret
= ctx
->idx
* 4 + BPF_INSN_SAFETY
* 4;
972 int arch_bpf_trampoline_size(const struct btf_func_model
*m
, u32 flags
,
973 struct bpf_tramp_links
*tlinks
, void *func_addr
)
975 struct bpf_tramp_image im
;
980 * Allocate a temporary buffer for __arch_prepare_bpf_trampoline().
981 * This will NOT cause fragmentation in direct map, as we do not
982 * call set_memory_*() on this buffer.
984 * We cannot use kvmalloc here, because we need image to be in
985 * module memory range.
987 image
= bpf_jit_alloc_exec(PAGE_SIZE
);
991 ret
= __arch_prepare_bpf_trampoline(&im
, image
, image
+ PAGE_SIZE
, image
,
992 m
, flags
, tlinks
, func_addr
);
993 bpf_jit_free_exec(image
);
998 int arch_prepare_bpf_trampoline(struct bpf_tramp_image
*im
, void *image
, void *image_end
,
999 const struct btf_func_model
*m
, u32 flags
,
1000 struct bpf_tramp_links
*tlinks
,
1003 u32 size
= image_end
- image
;
1004 void *rw_image
, *tmp
;
1008 * rw_image doesn't need to be in module memory range, so we can
1011 rw_image
= kvmalloc(size
, GFP_KERNEL
);
1015 ret
= __arch_prepare_bpf_trampoline(im
, rw_image
, rw_image
+ size
, image
, m
,
1016 flags
, tlinks
, func_addr
);
1020 if (bpf_jit_enable
> 1)
1021 bpf_jit_dump(1, ret
- BPF_INSN_SAFETY
* 4, 1, rw_image
);
1023 tmp
= bpf_arch_text_copy(image
, rw_image
, size
);
1032 static int bpf_modify_inst(void *ip
, ppc_inst_t old_inst
, ppc_inst_t new_inst
)
1034 ppc_inst_t org_inst
;
1036 if (copy_inst_from_kernel_nofault(&org_inst
, ip
)) {
1037 pr_err("0x%lx: fetching instruction failed\n", (unsigned long)ip
);
1041 if (!ppc_inst_equal(org_inst
, old_inst
)) {
1042 pr_err("0x%lx: expected (%08lx) != found (%08lx)\n",
1043 (unsigned long)ip
, ppc_inst_as_ulong(old_inst
), ppc_inst_as_ulong(org_inst
));
1047 if (ppc_inst_equal(old_inst
, new_inst
))
1050 return patch_instruction(ip
, new_inst
);
1053 static void do_isync(void *info __maybe_unused
)
1059 * A 3-step process for bpf prog entry:
1060 * 1. At bpf prog entry, a single nop/b:
1063 * 2. Out-of-line stub:
1066 * [b|bl] <bpf_prog>/<long_branch_stub>
1067 * mtlr r0 // CONFIG_PPC_FTRACE_OUT_OF_LINE only
1069 * 3. Long branch stub:
1071 * .long <branch_addr>/<dummy_tramp>
1077 * mtlr r11 // needed to retain ftrace ABI
1080 * dummy_tramp is used to reduce synchronization requirements.
1082 * When attaching a bpf trampoline to a bpf prog, we do not need any
1083 * synchronization here since we always have a valid branch target regardless
1084 * of the order in which the above stores are seen. dummy_tramp ensures that
1085 * the long_branch stub goes to a valid destination on other cpus, even when
1086 * the branch to the long_branch stub is seen before the updated trampoline
1089 * However, when detaching a bpf trampoline from a bpf prog, or if changing
1090 * the bpf trampoline address, we need synchronization to ensure that other
1091 * cpus can no longer branch into the older trampoline so that it can be
1092 * safely freed. bpf_tramp_image_put() uses rcu_tasks to ensure all cpus
1093 * make forward progress, but we still need to ensure that other cpus
1094 * execute isync (or some CSI) so that they don't go back into the
1097 int bpf_arch_text_poke(void *ip
, enum bpf_text_poke_type poke_type
,
1098 void *old_addr
, void *new_addr
)
1100 unsigned long bpf_func
, bpf_func_end
, size
, offset
;
1101 ppc_inst_t old_inst
, new_inst
;
1102 int ret
= 0, branch_flags
;
1103 char name
[KSYM_NAME_LEN
];
1105 if (IS_ENABLED(CONFIG_PPC32
))
1108 bpf_func
= (unsigned long)ip
;
1109 branch_flags
= poke_type
== BPF_MOD_CALL
? BRANCH_SET_LINK
: 0;
1111 /* We currently only support poking bpf programs */
1112 if (!__bpf_address_lookup(bpf_func
, &size
, &offset
, name
)) {
1113 pr_err("%s (0x%lx): kernel/modules are not supported\n", __func__
, bpf_func
);
1118 * If we are not poking at bpf prog entry, then we are simply patching in/out
1119 * an unconditional branch instruction at im->ip_after_call
1122 if (poke_type
!= BPF_MOD_JUMP
) {
1123 pr_err("%s (0x%lx): calls are not supported in bpf prog body\n", __func__
,
1127 old_inst
= ppc_inst(PPC_RAW_NOP());
1129 if (create_branch(&old_inst
, ip
, (unsigned long)old_addr
, 0))
1131 new_inst
= ppc_inst(PPC_RAW_NOP());
1133 if (create_branch(&new_inst
, ip
, (unsigned long)new_addr
, 0))
1135 mutex_lock(&text_mutex
);
1136 ret
= bpf_modify_inst(ip
, old_inst
, new_inst
);
1137 mutex_unlock(&text_mutex
);
1139 /* Make sure all cpus see the new instruction */
1140 smp_call_function(do_isync
, NULL
, 1);
1144 bpf_func_end
= bpf_func
+ size
;
1146 /* Address of the jmp/call instruction in the out-of-line stub */
1147 ip
= (void *)(bpf_func_end
- bpf_jit_ool_stub
+ 4);
1149 if (!is_offset_in_branch_range((long)ip
- 4 - bpf_func
)) {
1150 pr_err("%s (0x%lx): bpf prog too large, ool stub out of branch range\n", __func__
,
1155 old_inst
= ppc_inst(PPC_RAW_NOP());
1157 if (is_offset_in_branch_range(ip
- old_addr
))
1158 create_branch(&old_inst
, ip
, (unsigned long)old_addr
, branch_flags
);
1160 create_branch(&old_inst
, ip
, bpf_func_end
- bpf_jit_long_branch_stub
,
1163 new_inst
= ppc_inst(PPC_RAW_NOP());
1165 if (is_offset_in_branch_range(ip
- new_addr
))
1166 create_branch(&new_inst
, ip
, (unsigned long)new_addr
, branch_flags
);
1168 create_branch(&new_inst
, ip
, bpf_func_end
- bpf_jit_long_branch_stub
,
1172 mutex_lock(&text_mutex
);
1175 * 1. Update the address in the long branch stub:
1176 * If new_addr is out of range, we will have to use the long branch stub, so patch new_addr
1177 * here. Otherwise, revert to dummy_tramp, but only if we had patched old_addr here.
1179 if ((new_addr
&& !is_offset_in_branch_range(new_addr
- ip
)) ||
1180 (old_addr
&& !is_offset_in_branch_range(old_addr
- ip
)))
1181 ret
= patch_ulong((void *)(bpf_func_end
- bpf_jit_long_branch_stub
- SZL
),
1182 (new_addr
&& !is_offset_in_branch_range(new_addr
- ip
)) ?
1183 (unsigned long)new_addr
: (unsigned long)dummy_tramp
);
1187 /* 2. Update the branch/call in the out-of-line stub */
1188 ret
= bpf_modify_inst(ip
, old_inst
, new_inst
);
1192 /* 3. Update instruction at bpf prog entry */
1193 ip
= (void *)bpf_func
;
1194 if (!old_addr
|| !new_addr
) {
1196 old_inst
= ppc_inst(PPC_RAW_NOP());
1197 create_branch(&new_inst
, ip
, bpf_func_end
- bpf_jit_ool_stub
, 0);
1199 new_inst
= ppc_inst(PPC_RAW_NOP());
1200 create_branch(&old_inst
, ip
, bpf_func_end
- bpf_jit_ool_stub
, 0);
1202 ret
= bpf_modify_inst(ip
, old_inst
, new_inst
);
1206 mutex_unlock(&text_mutex
);
1209 * Sync only if we are not attaching a trampoline to a bpf prog so the older
1210 * trampoline can be freed safely.
1213 smp_call_function(do_isync
, NULL
, 1);