1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * eBPF JIT compiler 4 * 5 * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com> 6 * IBM Corporation 7 * 8 * Based on the powerpc classic BPF JIT compiler by Matt Evans 9 */ 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> 18 19 #include "bpf_jit.h" 20 21 static void bpf_jit_fill_ill_insns(void *area, unsigned int size) 22 { 23 memset32(area, BREAKPOINT_INSTRUCTION, size / 4); 24 } 25 26 int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg, long exit_addr) 27 { 28 if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) { 29 PPC_JMP(exit_addr); 30 } else if (ctx->alt_exit_addr) { 31 if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4)))) 32 return -1; 33 PPC_JMP(ctx->alt_exit_addr); 34 } else { 35 ctx->alt_exit_addr = ctx->idx * 4; 36 bpf_jit_build_epilogue(image, ctx); 37 } 38 39 return 0; 40 } 41 42 struct powerpc64_jit_data { 43 struct bpf_binary_header *header; 44 u32 *addrs; 45 u8 *image; 46 u32 proglen; 47 struct codegen_context ctx; 48 }; 49 50 bool bpf_jit_needs_zext(void) 51 { 52 return true; 53 } 54 55 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) 56 { 57 u32 proglen; 58 u32 alloclen; 59 u8 *image = NULL; 60 u32 *code_base; 61 u32 *addrs; 62 struct powerpc64_jit_data *jit_data; 63 struct codegen_context cgctx; 64 int pass; 65 int flen; 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; 71 u32 extable_len; 72 u32 fixup_len; 73 74 if (!fp->jit_requested) 75 return org_fp; 76 77 tmp_fp = bpf_jit_blind_constants(org_fp); 78 if (IS_ERR(tmp_fp)) 79 return org_fp; 80 81 if (tmp_fp != org_fp) { 82 bpf_blinded = true; 83 fp = tmp_fp; 84 } 85 86 jit_data = fp->aux->jit_data; 87 if (!jit_data) { 88 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL); 89 if (!jit_data) { 90 fp = org_fp; 91 goto out; 92 } 93 fp->aux->jit_data = jit_data; 94 } 95 96 flen = fp->len; 97 addrs = jit_data->addrs; 98 if (addrs) { 99 cgctx = jit_data->ctx; 100 image = jit_data->image; 101 bpf_hdr = jit_data->header; 102 proglen = jit_data->proglen; 103 extra_pass = true; 104 goto skip_init_ctx; 105 } 106 107 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL); 108 if (addrs == NULL) { 109 fp = org_fp; 110 goto out_addrs; 111 } 112 113 memset(&cgctx, 0, sizeof(struct codegen_context)); 114 bpf_jit_init_reg_mapping(&cgctx); 115 116 /* Make sure that the stack is quadword aligned. */ 117 cgctx.stack_size = round_up(fp->aux->stack_depth, 16); 118 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. */ 122 fp = org_fp; 123 goto out_addrs; 124 } 125 126 /* 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. 132 */ 133 if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) { 134 cgctx.idx = 0; 135 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0, false)) { 136 fp = org_fp; 137 goto out_addrs; 138 } 139 } 140 141 bpf_jit_realloc_regs(&cgctx); 142 /* 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. 146 */ 147 bpf_jit_build_prologue(0, &cgctx); 148 addrs[fp->len] = cgctx.idx * 4; 149 bpf_jit_build_epilogue(0, &cgctx); 150 151 fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4; 152 extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry); 153 154 proglen = cgctx.idx * 4; 155 alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len; 156 157 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns); 158 if (!bpf_hdr) { 159 fp = org_fp; 160 goto out_addrs; 161 } 162 163 if (extable_len) 164 fp->aux->extable = (void *)image + FUNCTION_DESCR_SIZE + proglen + fixup_len; 165 166 skip_init_ctx: 167 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE); 168 169 /* Code generation passes 1-2 */ 170 for (pass = 1; pass < 3; pass++) { 171 /* Now build the prologue, body code & epilogue for real. */ 172 cgctx.idx = 0; 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); 177 fp = org_fp; 178 goto out_addrs; 179 } 180 bpf_jit_build_epilogue(code_base, &cgctx); 181 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); 185 } 186 187 if (bpf_jit_enable > 1) 188 /* 189 * Note that we output the base address of the code_base 190 * rather than image, since opcodes are in code_base. 191 */ 192 bpf_jit_dump(flen, proglen, pass, code_base); 193 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; 198 #endif 199 200 fp->bpf_func = (void *)image; 201 fp->jited = 1; 202 fp->jited_len = proglen + FUNCTION_DESCR_SIZE; 203 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); 208 out_addrs: 209 kfree(addrs); 210 kfree(jit_data); 211 fp->aux->jit_data = NULL; 212 } else { 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; 218 } 219 220 out: 221 if (bpf_blinded) 222 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp); 223 224 return fp; 225 } 226 227 /* 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. 230 */ 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) 233 { 234 off_t offset; 235 unsigned long pc; 236 struct exception_table_entry *ex; 237 u32 *fixup; 238 239 /* Populate extable entries only in the last pass */ 240 if (pass != 2) 241 return 0; 242 243 if (!fp->aux->extable || 244 WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries)) 245 return -EINVAL; 246 247 pc = (unsigned long)&image[insn_idx]; 248 249 fixup = (void *)fp->aux->extable - 250 (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) + 251 (ctx->exentry_idx * BPF_FIXUP_LEN * 4); 252 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 */ 256 257 fixup[BPF_FIXUP_LEN - 1] = 258 PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]); 259 260 ex = &fp->aux->extable[ctx->exentry_idx]; 261 262 offset = pc - (long)&ex->insn; 263 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN)) 264 return -ERANGE; 265 ex->insn = offset; 266 267 offset = (long)fixup - (long)&ex->fixup; 268 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN)) 269 return -ERANGE; 270 ex->fixup = offset; 271 272 ctx->exentry_idx++; 273 return 0; 274 } 275