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 /* Fix the branch target addresses for subprog calls */
27 static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,
28 struct codegen_context *ctx, u32 *addrs)
30 const struct bpf_insn *insn = fp->insnsi;
36 for (i = 0; i < fp->len; i++) {
38 * During the extra pass, only the branch target addresses for
39 * the subprog calls need to be fixed. All other instructions
42 * The JITed image length does not change because we already
43 * ensure that the JITed instruction sequence for these calls
44 * are of fixed length by padding them with NOPs.
46 if (insn[i].code == (BPF_JMP | BPF_CALL) &&
47 insn[i].src_reg == BPF_PSEUDO_CALL) {
48 ret = bpf_jit_get_func_addr(fp, &insn[i], true,
55 * Save ctx->idx as this would currently point to the
56 * end of the JITed image and set it to the offset of
57 * the instruction sequence corresponding to the
58 * subprog call temporarily.
61 ctx->idx = addrs[i] / 4;
62 bpf_jit_emit_func_call_rel(image, ctx, func_addr);
65 * Restore ctx->idx here. This is safe as the length
66 * of the JITed sequence remains unchanged.
75 struct powerpc64_jit_data {
76 struct bpf_binary_header *header;
80 struct codegen_context ctx;
83 bool bpf_jit_needs_zext(void)
88 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
95 struct powerpc64_jit_data *jit_data;
96 struct codegen_context cgctx;
99 struct bpf_binary_header *bpf_hdr;
100 struct bpf_prog *org_fp = fp;
101 struct bpf_prog *tmp_fp;
102 bool bpf_blinded = false;
103 bool extra_pass = false;
105 if (!fp->jit_requested)
108 tmp_fp = bpf_jit_blind_constants(org_fp);
112 if (tmp_fp != org_fp) {
117 jit_data = fp->aux->jit_data;
119 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
124 fp->aux->jit_data = jit_data;
128 addrs = jit_data->addrs;
130 cgctx = jit_data->ctx;
131 image = jit_data->image;
132 bpf_hdr = jit_data->header;
133 proglen = jit_data->proglen;
134 alloclen = proglen + FUNCTION_DESCR_SIZE;
139 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
145 memset(&cgctx, 0, sizeof(struct codegen_context));
146 memcpy(cgctx.b2p, b2p, sizeof(cgctx.b2p));
148 /* Make sure that the stack is quadword aligned. */
149 cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
151 /* Scouting faux-generate pass 0 */
152 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
153 /* We hit something illegal or unsupported. */
159 * If we have seen a tail call, we need a second pass.
160 * This is because bpf_jit_emit_common_epilogue() is called
161 * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen.
163 if (cgctx.seen & SEEN_TAILCALL) {
165 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) {
171 bpf_jit_realloc_regs(&cgctx);
173 * Pretend to build prologue, given the features we've seen. This will
174 * update ctgtx.idx as it pretends to output instructions, then we can
175 * calculate total size from idx.
177 bpf_jit_build_prologue(0, &cgctx);
178 bpf_jit_build_epilogue(0, &cgctx);
180 proglen = cgctx.idx * 4;
181 alloclen = proglen + FUNCTION_DESCR_SIZE;
183 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns);
190 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
194 * Do not touch the prologue and epilogue as they will remain
195 * unchanged. Only fix the branch target address for subprog
198 * This does not change the offsets and lengths of the subprog
199 * call instruction sequences and hence, the size of the JITed
202 bpf_jit_fixup_subprog_calls(fp, code_base, &cgctx, addrs);
204 /* There is no need to perform the usual passes. */
205 goto skip_codegen_passes;
208 /* Code generation passes 1-2 */
209 for (pass = 1; pass < 3; pass++) {
210 /* Now build the prologue, body code & epilogue for real. */
212 bpf_jit_build_prologue(code_base, &cgctx);
213 bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass);
214 bpf_jit_build_epilogue(code_base, &cgctx);
216 if (bpf_jit_enable > 1)
217 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
218 proglen - (cgctx.idx * 4), cgctx.seen);
222 if (bpf_jit_enable > 1)
224 * Note that we output the base address of the code_base
225 * rather than image, since opcodes are in code_base.
227 bpf_jit_dump(flen, proglen, pass, code_base);
229 #ifdef PPC64_ELF_ABI_v1
230 /* Function descriptor nastiness: Address + TOC */
231 ((u64 *)image)[0] = (u64)code_base;
232 ((u64 *)image)[1] = local_paca->kernel_toc;
235 fp->bpf_func = (void *)image;
237 fp->jited_len = alloclen;
239 bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
240 bpf_jit_binary_lock_ro(bpf_hdr);
241 if (!fp->is_func || extra_pass) {
242 bpf_prog_fill_jited_linfo(fp, addrs);
246 fp->aux->jit_data = NULL;
248 jit_data->addrs = addrs;
249 jit_data->ctx = cgctx;
250 jit_data->proglen = proglen;
251 jit_data->image = image;
252 jit_data->header = bpf_hdr;
257 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);