1 // SPDX-License-Identifier: GPL-2.0-only
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 <asm/kprobes.h>
17 #include <linux/bpf.h>
21 static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
23 memset32(area, BREAKPOINT_INSTRUCTION, size / 4);
26 int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg, long exit_addr)
28 if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) {
30 } else if (ctx->alt_exit_addr) {
31 if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4))))
33 PPC_JMP(ctx->alt_exit_addr);
35 ctx->alt_exit_addr = ctx->idx * 4;
36 bpf_jit_build_epilogue(image, ctx);
42 struct powerpc64_jit_data {
43 struct bpf_binary_header *header;
47 struct codegen_context ctx;
50 bool bpf_jit_needs_zext(void)
55 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
62 struct powerpc64_jit_data *jit_data;
63 struct codegen_context cgctx;
66 struct bpf_binary_header *bpf_hdr;
67 struct bpf_prog *org_fp = fp;
68 struct bpf_prog *tmp_fp;
69 bool bpf_blinded = false;
70 bool extra_pass = false;
74 if (!fp->jit_requested)
77 tmp_fp = bpf_jit_blind_constants(org_fp);
81 if (tmp_fp != org_fp) {
86 jit_data = fp->aux->jit_data;
88 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
93 fp->aux->jit_data = jit_data;
97 addrs = jit_data->addrs;
99 cgctx = jit_data->ctx;
100 image = jit_data->image;
101 bpf_hdr = jit_data->header;
102 proglen = jit_data->proglen;
104 /* During extra pass, ensure index is reset before repopulating extable entries */
105 cgctx.exentry_idx = 0;
109 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
115 memset(&cgctx, 0, sizeof(struct codegen_context));
116 bpf_jit_init_reg_mapping(&cgctx);
118 /* Make sure that the stack is quadword aligned. */
119 cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
121 /* Scouting faux-generate pass 0 */
122 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0, false)) {
123 /* We hit something illegal or unsupported. */
129 * If we have seen a tail call, we need a second pass.
130 * This is because bpf_jit_emit_common_epilogue() is called
131 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
132 * We also need a second pass if we ended up with too large
133 * a program so as to ensure BPF_EXIT branches are in range.
135 if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
137 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0, false)) {
143 bpf_jit_realloc_regs(&cgctx);
145 * Pretend to build prologue, given the features we've seen. This will
146 * update ctgtx.idx as it pretends to output instructions, then we can
147 * calculate total size from idx.
149 bpf_jit_build_prologue(0, &cgctx);
150 addrs[fp->len] = cgctx.idx * 4;
151 bpf_jit_build_epilogue(0, &cgctx);
153 fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
154 extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry);
156 proglen = cgctx.idx * 4;
157 alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
159 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns);
166 fp->aux->extable = (void *)image + FUNCTION_DESCR_SIZE + proglen + fixup_len;
169 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
171 /* Code generation passes 1-2 */
172 for (pass = 1; pass < 3; pass++) {
173 /* Now build the prologue, body code & epilogue for real. */
175 cgctx.alt_exit_addr = 0;
176 bpf_jit_build_prologue(code_base, &cgctx);
177 if (bpf_jit_build_body(fp, code_base, &cgctx, addrs, pass, extra_pass)) {
178 bpf_jit_binary_free(bpf_hdr);
182 bpf_jit_build_epilogue(code_base, &cgctx);
184 if (bpf_jit_enable > 1)
185 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
186 proglen - (cgctx.idx * 4), cgctx.seen);
189 if (bpf_jit_enable > 1)
191 * Note that we output the base address of the code_base
192 * rather than image, since opcodes are in code_base.
194 bpf_jit_dump(flen, proglen, pass, code_base);
196 #ifdef CONFIG_PPC64_ELF_ABI_V1
197 /* Function descriptor nastiness: Address + TOC */
198 ((u64 *)image)[0] = (u64)code_base;
199 ((u64 *)image)[1] = local_paca->kernel_toc;
202 fp->bpf_func = (void *)image;
204 fp->jited_len = proglen + FUNCTION_DESCR_SIZE;
206 bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + bpf_hdr->size);
207 if (!fp->is_func || extra_pass) {
208 bpf_jit_binary_lock_ro(bpf_hdr);
209 bpf_prog_fill_jited_linfo(fp, addrs);
213 fp->aux->jit_data = NULL;
215 jit_data->addrs = addrs;
216 jit_data->ctx = cgctx;
217 jit_data->proglen = proglen;
218 jit_data->image = image;
219 jit_data->header = bpf_hdr;
224 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
230 * The caller should check for (BPF_MODE(code) == BPF_PROBE_MEM) before calling
231 * this function, as this only applies to BPF_PROBE_MEM, for now.
233 int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct codegen_context *ctx,
234 int insn_idx, int jmp_off, int dst_reg)
238 struct exception_table_entry *ex;
241 /* Populate extable entries only in the last pass */
245 if (!fp->aux->extable ||
246 WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries))
249 pc = (unsigned long)&image[insn_idx];
251 fixup = (void *)fp->aux->extable -
252 (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) +
253 (ctx->exentry_idx * BPF_FIXUP_LEN * 4);
255 fixup[0] = PPC_RAW_LI(dst_reg, 0);
256 if (IS_ENABLED(CONFIG_PPC32))
257 fixup[1] = PPC_RAW_LI(dst_reg - 1, 0); /* clear higher 32-bit register too */
259 fixup[BPF_FIXUP_LEN - 1] =
260 PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]);
262 ex = &fp->aux->extable[ctx->exentry_idx];
264 offset = pc - (long)&ex->insn;
265 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
269 offset = (long)fixup - (long)&ex->fixup;
270 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))