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/code-patching.h>
25 static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
27 memset32(area, BREAKPOINT_INSTRUCTION, size / 4);
30 int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg, long exit_addr)
32 if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) {
34 } else if (ctx->alt_exit_addr) {
35 if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4))))
37 PPC_JMP(ctx->alt_exit_addr);
39 ctx->alt_exit_addr = ctx->idx * 4;
40 bpf_jit_build_epilogue(image, ctx);
46 struct powerpc_jit_data {
47 /* address of rw header */
48 struct bpf_binary_header *hdr;
49 /* address of ro final header */
50 struct bpf_binary_header *fhdr;
54 struct codegen_context ctx;
57 bool bpf_jit_needs_zext(void)
62 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
69 struct powerpc_jit_data *jit_data;
70 struct codegen_context cgctx;
73 struct bpf_binary_header *fhdr = NULL;
74 struct bpf_binary_header *hdr = NULL;
75 struct bpf_prog *org_fp = fp;
76 struct bpf_prog *tmp_fp;
77 bool bpf_blinded = false;
78 bool extra_pass = false;
84 if (!fp->jit_requested)
87 tmp_fp = bpf_jit_blind_constants(org_fp);
91 if (tmp_fp != org_fp) {
96 jit_data = fp->aux->jit_data;
98 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
103 fp->aux->jit_data = jit_data;
107 addrs = jit_data->addrs;
109 cgctx = jit_data->ctx;
111 * JIT compiled to a writable location (image/code_base) first.
112 * It is then moved to the readonly final location (fimage/fcode_base)
113 * using instruction patching.
115 fimage = jit_data->fimage;
116 fhdr = jit_data->fhdr;
117 proglen = jit_data->proglen;
119 image = (void *)hdr + ((void *)fimage - (void *)fhdr);
121 /* During extra pass, ensure index is reset before repopulating extable entries */
122 cgctx.exentry_idx = 0;
126 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
132 memset(&cgctx, 0, sizeof(struct codegen_context));
133 bpf_jit_init_reg_mapping(&cgctx);
135 /* Make sure that the stack is quadword aligned. */
136 cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
138 /* Scouting faux-generate pass 0 */
139 if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) {
140 /* We hit something illegal or unsupported. */
146 * If we have seen a tail call, we need a second pass.
147 * This is because bpf_jit_emit_common_epilogue() is called
148 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
149 * We also need a second pass if we ended up with too large
150 * a program so as to ensure BPF_EXIT branches are in range.
152 if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
154 if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) {
160 bpf_jit_realloc_regs(&cgctx);
162 * Pretend to build prologue, given the features we've seen. This will
163 * update ctgtx.idx as it pretends to output instructions, then we can
164 * calculate total size from idx.
166 bpf_jit_build_prologue(NULL, &cgctx);
167 addrs[fp->len] = cgctx.idx * 4;
168 bpf_jit_build_epilogue(NULL, &cgctx);
170 fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
171 extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry);
173 proglen = cgctx.idx * 4;
174 alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
176 fhdr = bpf_jit_binary_pack_alloc(alloclen, &fimage, 4, &hdr, &image,
177 bpf_jit_fill_ill_insns);
184 fp->aux->extable = (void *)fimage + FUNCTION_DESCR_SIZE + proglen + fixup_len;
187 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
188 fcode_base = (u32 *)(fimage + FUNCTION_DESCR_SIZE);
190 /* Code generation passes 1-2 */
191 for (pass = 1; pass < 3; pass++) {
192 /* Now build the prologue, body code & epilogue for real. */
194 cgctx.alt_exit_addr = 0;
195 bpf_jit_build_prologue(code_base, &cgctx);
196 if (bpf_jit_build_body(fp, code_base, fcode_base, &cgctx, addrs, pass,
198 bpf_arch_text_copy(&fhdr->size, &hdr->size, sizeof(hdr->size));
199 bpf_jit_binary_pack_free(fhdr, hdr);
203 bpf_jit_build_epilogue(code_base, &cgctx);
205 if (bpf_jit_enable > 1)
206 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
207 proglen - (cgctx.idx * 4), cgctx.seen);
210 if (bpf_jit_enable > 1)
212 * Note that we output the base address of the code_base
213 * rather than image, since opcodes are in code_base.
215 bpf_jit_dump(flen, proglen, pass, code_base);
217 #ifdef CONFIG_PPC64_ELF_ABI_V1
218 /* Function descriptor nastiness: Address + TOC */
219 ((u64 *)image)[0] = (u64)fcode_base;
220 ((u64 *)image)[1] = local_paca->kernel_toc;
223 fp->bpf_func = (void *)fimage;
225 fp->jited_len = proglen + FUNCTION_DESCR_SIZE;
227 if (!fp->is_func || extra_pass) {
228 if (bpf_jit_binary_pack_finalize(fp, fhdr, hdr)) {
232 bpf_prog_fill_jited_linfo(fp, addrs);
236 fp->aux->jit_data = NULL;
238 jit_data->addrs = addrs;
239 jit_data->ctx = cgctx;
240 jit_data->proglen = proglen;
241 jit_data->fimage = fimage;
242 jit_data->fhdr = fhdr;
248 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
254 * The caller should check for (BPF_MODE(code) == BPF_PROBE_MEM) before calling
255 * this function, as this only applies to BPF_PROBE_MEM, for now.
257 int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, u32 *fimage, int pass,
258 struct codegen_context *ctx, int insn_idx, int jmp_off,
263 struct exception_table_entry *ex, *ex_entry;
266 /* Populate extable entries only in the last pass */
270 if (!fp->aux->extable ||
271 WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries))
275 * Program is first written to image before copying to the
276 * final location (fimage). Accordingly, update in the image first.
277 * As all offsets used are relative, copying as is to the
278 * final location should be alright.
280 pc = (unsigned long)&image[insn_idx];
281 ex = (void *)fp->aux->extable - (void *)fimage + (void *)image;
284 (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) +
285 (ctx->exentry_idx * BPF_FIXUP_LEN * 4);
287 fixup[0] = PPC_RAW_LI(dst_reg, 0);
288 if (IS_ENABLED(CONFIG_PPC32))
289 fixup[1] = PPC_RAW_LI(dst_reg - 1, 0); /* clear higher 32-bit register too */
291 fixup[BPF_FIXUP_LEN - 1] =
292 PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]);
294 ex_entry = &ex[ctx->exentry_idx];
296 offset = pc - (long)&ex_entry->insn;
297 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
299 ex_entry->insn = offset;
301 offset = (long)fixup - (long)&ex_entry->fixup;
302 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
304 ex_entry->fixup = offset;
310 void *bpf_arch_text_copy(void *dst, void *src, size_t len)
314 if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst)))
315 return ERR_PTR(-EINVAL);
317 mutex_lock(&text_mutex);
318 err = patch_instructions(dst, src, len, false);
319 mutex_unlock(&text_mutex);
321 return err ? ERR_PTR(err) : dst;
324 int bpf_arch_text_invalidate(void *dst, size_t len)
326 u32 insn = BREAKPOINT_INSTRUCTION;
329 if (WARN_ON_ONCE(core_kernel_text((unsigned long)dst)))
332 mutex_lock(&text_mutex);
333 ret = patch_instructions(dst, &insn, len, true);
334 mutex_unlock(&text_mutex);
339 void bpf_jit_free(struct bpf_prog *fp)
342 struct powerpc_jit_data *jit_data = fp->aux->jit_data;
343 struct bpf_binary_header *hdr;
346 * If we fail the final pass of JIT (from jit_subprogs),
347 * the program may not be finalized yet. Call finalize here
351 bpf_jit_binary_pack_finalize(fp, jit_data->fhdr, jit_data->hdr);
352 kvfree(jit_data->addrs);
355 hdr = bpf_jit_binary_pack_hdr(fp);
356 bpf_jit_binary_pack_free(hdr, NULL);
357 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
360 bpf_prog_unlock_free(fp);