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 <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;
107 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
113 memset(&cgctx, 0, sizeof(struct codegen_context));
114 bpf_jit_init_reg_mapping(&cgctx);
116 /* Make sure that the stack is quadword aligned. */
117 cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
119 /* Scouting faux-generate pass 0 */
120 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0, false)) {
121 /* We hit something illegal or unsupported. */
127 * If we have seen a tail call, we need a second pass.
128 * This is because bpf_jit_emit_common_epilogue() is called
129 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
130 * We also need a second pass if we ended up with too large
131 * a program so as to ensure BPF_EXIT branches are in range.
133 if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
135 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0, false)) {
141 bpf_jit_realloc_regs(&cgctx);
143 * Pretend to build prologue, given the features we've seen. This will
144 * update ctgtx.idx as it pretends to output instructions, then we can
145 * calculate total size from idx.
147 bpf_jit_build_prologue(0, &cgctx);
148 addrs[fp->len] = cgctx.idx * 4;
149 bpf_jit_build_epilogue(0, &cgctx);
151 fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
152 extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry);
154 proglen = cgctx.idx * 4;
155 alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
157 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns);
164 fp->aux->extable = (void *)image + FUNCTION_DESCR_SIZE + proglen + fixup_len;
167 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
169 /* Code generation passes 1-2 */
170 for (pass = 1; pass < 3; pass++) {
171 /* Now build the prologue, body code & epilogue for real. */
173 cgctx.alt_exit_addr = 0;
174 bpf_jit_build_prologue(code_base, &cgctx);
175 if (bpf_jit_build_body(fp, code_base, &cgctx, addrs, pass, extra_pass)) {
176 bpf_jit_binary_free(bpf_hdr);
180 bpf_jit_build_epilogue(code_base, &cgctx);
182 if (bpf_jit_enable > 1)
183 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
184 proglen - (cgctx.idx * 4), cgctx.seen);
187 if (bpf_jit_enable > 1)
189 * Note that we output the base address of the code_base
190 * rather than image, since opcodes are in code_base.
192 bpf_jit_dump(flen, proglen, pass, code_base);
194 #ifdef CONFIG_PPC64_ELF_ABI_V1
195 /* Function descriptor nastiness: Address + TOC */
196 ((u64 *)image)[0] = (u64)code_base;
197 ((u64 *)image)[1] = local_paca->kernel_toc;
200 fp->bpf_func = (void *)image;
202 fp->jited_len = proglen + FUNCTION_DESCR_SIZE;
204 bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + bpf_hdr->size);
205 if (!fp->is_func || extra_pass) {
206 bpf_jit_binary_lock_ro(bpf_hdr);
207 bpf_prog_fill_jited_linfo(fp, addrs);
211 fp->aux->jit_data = NULL;
213 jit_data->addrs = addrs;
214 jit_data->ctx = cgctx;
215 jit_data->proglen = proglen;
216 jit_data->image = image;
217 jit_data->header = bpf_hdr;
222 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
228 * The caller should check for (BPF_MODE(code) == BPF_PROBE_MEM) before calling
229 * this function, as this only applies to BPF_PROBE_MEM, for now.
231 int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct codegen_context *ctx,
232 int insn_idx, int jmp_off, int dst_reg)
236 struct exception_table_entry *ex;
239 /* Populate extable entries only in the last pass */
243 if (!fp->aux->extable ||
244 WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries))
247 pc = (unsigned long)&image[insn_idx];
249 fixup = (void *)fp->aux->extable -
250 (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) +
251 (ctx->exentry_idx * BPF_FIXUP_LEN * 4);
253 fixup[0] = PPC_RAW_LI(dst_reg, 0);
254 if (IS_ENABLED(CONFIG_PPC32))
255 fixup[1] = PPC_RAW_LI(dst_reg - 1, 0); /* clear higher 32-bit register too */
257 fixup[BPF_FIXUP_LEN - 1] =
258 PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]);
260 ex = &fp->aux->extable[ctx->exentry_idx];
262 offset = pc - (long)&ex->insn;
263 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
267 offset = (long)fixup - (long)&ex->fixup;
268 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))