0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/moduleloader.h>
0011 #include <asm/cacheflush.h>
0012 #include <asm/asm-compat.h>
0013 #include <linux/netdevice.h>
0014 #include <linux/filter.h>
0015 #include <linux/if_vlan.h>
0016 #include <asm/kprobes.h>
0017 #include <linux/bpf.h>
0018
0019 #include "bpf_jit.h"
0020
0021 static void bpf_jit_fill_ill_insns(void *area, unsigned int size)
0022 {
0023 memset32(area, BREAKPOINT_INSTRUCTION, size / 4);
0024 }
0025
0026
0027 static int bpf_jit_fixup_addresses(struct bpf_prog *fp, u32 *image,
0028 struct codegen_context *ctx, u32 *addrs)
0029 {
0030 const struct bpf_insn *insn = fp->insnsi;
0031 bool func_addr_fixed;
0032 u64 func_addr;
0033 u32 tmp_idx;
0034 int i, j, ret;
0035
0036 for (i = 0; i < fp->len; i++) {
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 if (insn[i].code == (BPF_JMP | BPF_CALL) &&
0047 insn[i].src_reg == BPF_PSEUDO_CALL) {
0048 ret = bpf_jit_get_func_addr(fp, &insn[i], true,
0049 &func_addr,
0050 &func_addr_fixed);
0051 if (ret < 0)
0052 return ret;
0053
0054
0055
0056
0057
0058
0059
0060 tmp_idx = ctx->idx;
0061 ctx->idx = addrs[i] / 4;
0062 ret = bpf_jit_emit_func_call_rel(image, ctx, func_addr);
0063 if (ret)
0064 return ret;
0065
0066
0067
0068
0069
0070 ctx->idx = tmp_idx;
0071 } else if (insn[i].code == (BPF_LD | BPF_IMM | BPF_DW)) {
0072 tmp_idx = ctx->idx;
0073 ctx->idx = addrs[i] / 4;
0074 #ifdef CONFIG_PPC32
0075 PPC_LI32(bpf_to_ppc(insn[i].dst_reg) - 1, (u32)insn[i + 1].imm);
0076 PPC_LI32(bpf_to_ppc(insn[i].dst_reg), (u32)insn[i].imm);
0077 for (j = ctx->idx - addrs[i] / 4; j < 4; j++)
0078 EMIT(PPC_RAW_NOP());
0079 #else
0080 func_addr = ((u64)(u32)insn[i].imm) | (((u64)(u32)insn[i + 1].imm) << 32);
0081 PPC_LI64(bpf_to_ppc(insn[i].dst_reg), func_addr);
0082
0083 for (j = ctx->idx - addrs[i] / 4; j < 5; j++)
0084 EMIT(PPC_RAW_NOP());
0085 #endif
0086 ctx->idx = tmp_idx;
0087 i++;
0088 }
0089 }
0090
0091 return 0;
0092 }
0093
0094 int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg, long exit_addr)
0095 {
0096 if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) {
0097 PPC_JMP(exit_addr);
0098 } else if (ctx->alt_exit_addr) {
0099 if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4))))
0100 return -1;
0101 PPC_JMP(ctx->alt_exit_addr);
0102 } else {
0103 ctx->alt_exit_addr = ctx->idx * 4;
0104 bpf_jit_build_epilogue(image, ctx);
0105 }
0106
0107 return 0;
0108 }
0109
0110 struct powerpc64_jit_data {
0111 struct bpf_binary_header *header;
0112 u32 *addrs;
0113 u8 *image;
0114 u32 proglen;
0115 struct codegen_context ctx;
0116 };
0117
0118 bool bpf_jit_needs_zext(void)
0119 {
0120 return true;
0121 }
0122
0123 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
0124 {
0125 u32 proglen;
0126 u32 alloclen;
0127 u8 *image = NULL;
0128 u32 *code_base;
0129 u32 *addrs;
0130 struct powerpc64_jit_data *jit_data;
0131 struct codegen_context cgctx;
0132 int pass;
0133 int flen;
0134 struct bpf_binary_header *bpf_hdr;
0135 struct bpf_prog *org_fp = fp;
0136 struct bpf_prog *tmp_fp;
0137 bool bpf_blinded = false;
0138 bool extra_pass = false;
0139 u32 extable_len;
0140 u32 fixup_len;
0141
0142 if (!fp->jit_requested)
0143 return org_fp;
0144
0145 tmp_fp = bpf_jit_blind_constants(org_fp);
0146 if (IS_ERR(tmp_fp))
0147 return org_fp;
0148
0149 if (tmp_fp != org_fp) {
0150 bpf_blinded = true;
0151 fp = tmp_fp;
0152 }
0153
0154 jit_data = fp->aux->jit_data;
0155 if (!jit_data) {
0156 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
0157 if (!jit_data) {
0158 fp = org_fp;
0159 goto out;
0160 }
0161 fp->aux->jit_data = jit_data;
0162 }
0163
0164 flen = fp->len;
0165 addrs = jit_data->addrs;
0166 if (addrs) {
0167 cgctx = jit_data->ctx;
0168 image = jit_data->image;
0169 bpf_hdr = jit_data->header;
0170 proglen = jit_data->proglen;
0171 extra_pass = true;
0172 goto skip_init_ctx;
0173 }
0174
0175 addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL);
0176 if (addrs == NULL) {
0177 fp = org_fp;
0178 goto out_addrs;
0179 }
0180
0181 memset(&cgctx, 0, sizeof(struct codegen_context));
0182 bpf_jit_init_reg_mapping(&cgctx);
0183
0184
0185 cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
0186
0187
0188 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0)) {
0189
0190 fp = org_fp;
0191 goto out_addrs;
0192 }
0193
0194
0195
0196
0197
0198
0199
0200
0201 if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
0202 cgctx.idx = 0;
0203 if (bpf_jit_build_body(fp, 0, &cgctx, addrs, 0)) {
0204 fp = org_fp;
0205 goto out_addrs;
0206 }
0207 }
0208
0209 bpf_jit_realloc_regs(&cgctx);
0210
0211
0212
0213
0214
0215 bpf_jit_build_prologue(0, &cgctx);
0216 addrs[fp->len] = cgctx.idx * 4;
0217 bpf_jit_build_epilogue(0, &cgctx);
0218
0219 fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
0220 extable_len = fp->aux->num_exentries * sizeof(struct exception_table_entry);
0221
0222 proglen = cgctx.idx * 4;
0223 alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
0224
0225 bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns);
0226 if (!bpf_hdr) {
0227 fp = org_fp;
0228 goto out_addrs;
0229 }
0230
0231 if (extable_len)
0232 fp->aux->extable = (void *)image + FUNCTION_DESCR_SIZE + proglen + fixup_len;
0233
0234 skip_init_ctx:
0235 code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
0236
0237 if (extra_pass) {
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247 bpf_jit_fixup_addresses(fp, code_base, &cgctx, addrs);
0248
0249
0250 goto skip_codegen_passes;
0251 }
0252
0253
0254 for (pass = 1; pass < 3; pass++) {
0255
0256 cgctx.idx = 0;
0257 cgctx.alt_exit_addr = 0;
0258 bpf_jit_build_prologue(code_base, &cgctx);
0259 if (bpf_jit_build_body(fp, code_base, &cgctx, addrs, pass)) {
0260 bpf_jit_binary_free(bpf_hdr);
0261 fp = org_fp;
0262 goto out_addrs;
0263 }
0264 bpf_jit_build_epilogue(code_base, &cgctx);
0265
0266 if (bpf_jit_enable > 1)
0267 pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
0268 proglen - (cgctx.idx * 4), cgctx.seen);
0269 }
0270
0271 skip_codegen_passes:
0272 if (bpf_jit_enable > 1)
0273
0274
0275
0276
0277 bpf_jit_dump(flen, proglen, pass, code_base);
0278
0279 #ifdef CONFIG_PPC64_ELF_ABI_V1
0280
0281 ((u64 *)image)[0] = (u64)code_base;
0282 ((u64 *)image)[1] = local_paca->kernel_toc;
0283 #endif
0284
0285 fp->bpf_func = (void *)image;
0286 fp->jited = 1;
0287 fp->jited_len = proglen + FUNCTION_DESCR_SIZE;
0288
0289 bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + bpf_hdr->size);
0290 if (!fp->is_func || extra_pass) {
0291 bpf_jit_binary_lock_ro(bpf_hdr);
0292 bpf_prog_fill_jited_linfo(fp, addrs);
0293 out_addrs:
0294 kfree(addrs);
0295 kfree(jit_data);
0296 fp->aux->jit_data = NULL;
0297 } else {
0298 jit_data->addrs = addrs;
0299 jit_data->ctx = cgctx;
0300 jit_data->proglen = proglen;
0301 jit_data->image = image;
0302 jit_data->header = bpf_hdr;
0303 }
0304
0305 out:
0306 if (bpf_blinded)
0307 bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp);
0308
0309 return fp;
0310 }
0311
0312
0313
0314
0315
0316 int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, int pass, struct codegen_context *ctx,
0317 int insn_idx, int jmp_off, int dst_reg)
0318 {
0319 off_t offset;
0320 unsigned long pc;
0321 struct exception_table_entry *ex;
0322 u32 *fixup;
0323
0324
0325 if (pass != 2)
0326 return 0;
0327
0328 if (!fp->aux->extable ||
0329 WARN_ON_ONCE(ctx->exentry_idx >= fp->aux->num_exentries))
0330 return -EINVAL;
0331
0332 pc = (unsigned long)&image[insn_idx];
0333
0334 fixup = (void *)fp->aux->extable -
0335 (fp->aux->num_exentries * BPF_FIXUP_LEN * 4) +
0336 (ctx->exentry_idx * BPF_FIXUP_LEN * 4);
0337
0338 fixup[0] = PPC_RAW_LI(dst_reg, 0);
0339 if (IS_ENABLED(CONFIG_PPC32))
0340 fixup[1] = PPC_RAW_LI(dst_reg - 1, 0);
0341
0342 fixup[BPF_FIXUP_LEN - 1] =
0343 PPC_RAW_BRANCH((long)(pc + jmp_off) - (long)&fixup[BPF_FIXUP_LEN - 1]);
0344
0345 ex = &fp->aux->extable[ctx->exentry_idx];
0346
0347 offset = pc - (long)&ex->insn;
0348 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
0349 return -ERANGE;
0350 ex->insn = offset;
0351
0352 offset = (long)fixup - (long)&ex->fixup;
0353 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
0354 return -ERANGE;
0355 ex->fixup = offset;
0356
0357 ctx->exentry_idx++;
0358 return 0;
0359 }