|
| 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 | +/* 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) |
| 29 | +{ |
| 30 | + const struct bpf_insn *insn = fp->insnsi; |
| 31 | + bool func_addr_fixed; |
| 32 | + u64 func_addr; |
| 33 | + u32 tmp_idx; |
| 34 | + int i, ret; |
| 35 | + |
| 36 | + for (i = 0; i < fp->len; i++) { |
| 37 | + /* |
| 38 | + * During the extra pass, only the branch target addresses for |
| 39 | + * the subprog calls need to be fixed. All other instructions |
| 40 | + * can left untouched. |
| 41 | + * |
| 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. |
| 45 | + */ |
| 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, |
| 49 | + &func_addr, |
| 50 | + &func_addr_fixed); |
| 51 | + if (ret < 0) |
| 52 | + return ret; |
| 53 | + |
| 54 | + /* |
| 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. |
| 59 | + */ |
| 60 | + tmp_idx = ctx->idx; |
| 61 | + ctx->idx = addrs[i] / 4; |
| 62 | + bpf_jit_emit_func_call_rel(image, ctx, func_addr); |
| 63 | + |
| 64 | + /* |
| 65 | + * Restore ctx->idx here. This is safe as the length |
| 66 | + * of the JITed sequence remains unchanged. |
| 67 | + */ |
| 68 | + ctx->idx = tmp_idx; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +struct powerpc64_jit_data { |
| 76 | + struct bpf_binary_header *header; |
| 77 | + u32 *addrs; |
| 78 | + u8 *image; |
| 79 | + u32 proglen; |
| 80 | + struct codegen_context ctx; |
| 81 | +}; |
| 82 | + |
| 83 | +bool bpf_jit_needs_zext(void) |
| 84 | +{ |
| 85 | + return true; |
| 86 | +} |
| 87 | + |
| 88 | +struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) |
| 89 | +{ |
| 90 | + u32 proglen; |
| 91 | + u32 alloclen; |
| 92 | + u8 *image = NULL; |
| 93 | + u32 *code_base; |
| 94 | + u32 *addrs; |
| 95 | + struct powerpc64_jit_data *jit_data; |
| 96 | + struct codegen_context cgctx; |
| 97 | + int pass; |
| 98 | + int flen; |
| 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; |
| 104 | + |
| 105 | + if (!fp->jit_requested) |
| 106 | + return org_fp; |
| 107 | + |
| 108 | + tmp_fp = bpf_jit_blind_constants(org_fp); |
| 109 | + if (IS_ERR(tmp_fp)) |
| 110 | + return org_fp; |
| 111 | + |
| 112 | + if (tmp_fp != org_fp) { |
| 113 | + bpf_blinded = true; |
| 114 | + fp = tmp_fp; |
| 115 | + } |
| 116 | + |
| 117 | + jit_data = fp->aux->jit_data; |
| 118 | + if (!jit_data) { |
| 119 | + jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL); |
| 120 | + if (!jit_data) { |
| 121 | + fp = org_fp; |
| 122 | + goto out; |
| 123 | + } |
| 124 | + fp->aux->jit_data = jit_data; |
| 125 | + } |
| 126 | + |
| 127 | + flen = fp->len; |
| 128 | + addrs = jit_data->addrs; |
| 129 | + if (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; |
| 135 | + extra_pass = true; |
| 136 | + goto skip_init_ctx; |
| 137 | + } |
| 138 | + |
| 139 | + addrs = kcalloc(flen + 1, sizeof(*addrs), GFP_KERNEL); |
| 140 | + if (addrs == NULL) { |
| 141 | + fp = org_fp; |
| 142 | + goto out_addrs; |
| 143 | + } |
| 144 | + |
| 145 | + memset(&cgctx, 0, sizeof(struct codegen_context)); |
| 146 | + |
| 147 | + /* Make sure that the stack is quadword aligned. */ |
| 148 | + cgctx.stack_size = round_up(fp->aux->stack_depth, 16); |
| 149 | + |
| 150 | + /* Scouting faux-generate pass 0 */ |
| 151 | + if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) { |
| 152 | + /* We hit something illegal or unsupported. */ |
| 153 | + fp = org_fp; |
| 154 | + goto out_addrs; |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + * If we have seen a tail call, we need a second pass. |
| 159 | + * This is because bpf_jit_emit_common_epilogue() is called |
| 160 | + * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. |
| 161 | + */ |
| 162 | + if (cgctx.seen & SEEN_TAILCALL) { |
| 163 | + cgctx.idx = 0; |
| 164 | + if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) { |
| 165 | + fp = org_fp; |
| 166 | + goto out_addrs; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /* |
| 171 | + * Pretend to build prologue, given the features we've seen. This will |
| 172 | + * update ctgtx.idx as it pretends to output instructions, then we can |
| 173 | + * calculate total size from idx. |
| 174 | + */ |
| 175 | + bpf_jit_build_prologue(0, &cgctx); |
| 176 | + bpf_jit_build_epilogue(0, &cgctx); |
| 177 | + |
| 178 | + proglen = cgctx.idx * 4; |
| 179 | + alloclen = proglen + FUNCTION_DESCR_SIZE; |
| 180 | + |
| 181 | + bpf_hdr = bpf_jit_binary_alloc(alloclen, &image, 4, bpf_jit_fill_ill_insns); |
| 182 | + if (!bpf_hdr) { |
| 183 | + fp = org_fp; |
| 184 | + goto out_addrs; |
| 185 | + } |
| 186 | + |
| 187 | +skip_init_ctx: |
| 188 | + code_base = (u32 *)(image + FUNCTION_DESCR_SIZE); |
| 189 | + |
| 190 | + if (extra_pass) { |
| 191 | + /* |
| 192 | + * Do not touch the prologue and epilogue as they will remain |
| 193 | + * unchanged. Only fix the branch target address for subprog |
| 194 | + * calls in the body. |
| 195 | + * |
| 196 | + * This does not change the offsets and lengths of the subprog |
| 197 | + * call instruction sequences and hence, the size of the JITed |
| 198 | + * image as well. |
| 199 | + */ |
| 200 | + bpf_jit_fixup_subprog_calls(fp, code_base, &cgctx, addrs); |
| 201 | + |
| 202 | + /* There is no need to perform the usual passes. */ |
| 203 | + goto skip_codegen_passes; |
| 204 | + } |
| 205 | + |
| 206 | + /* Code generation passes 1-2 */ |
| 207 | + for (pass = 1; pass < 3; pass++) { |
| 208 | + /* Now build the prologue, body code & epilogue for real. */ |
| 209 | + cgctx.idx = 0; |
| 210 | + bpf_jit_build_prologue(code_base, &cgctx); |
| 211 | + bpf_jit_build_body(fp, code_base, &cgctx, addrs, extra_pass); |
| 212 | + bpf_jit_build_epilogue(code_base, &cgctx); |
| 213 | + |
| 214 | + if (bpf_jit_enable > 1) |
| 215 | + pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass, |
| 216 | + proglen - (cgctx.idx * 4), cgctx.seen); |
| 217 | + } |
| 218 | + |
| 219 | +skip_codegen_passes: |
| 220 | + if (bpf_jit_enable > 1) |
| 221 | + /* |
| 222 | + * Note that we output the base address of the code_base |
| 223 | + * rather than image, since opcodes are in code_base. |
| 224 | + */ |
| 225 | + bpf_jit_dump(flen, proglen, pass, code_base); |
| 226 | + |
| 227 | +#ifdef PPC64_ELF_ABI_v1 |
| 228 | + /* Function descriptor nastiness: Address + TOC */ |
| 229 | + ((u64 *)image)[0] = (u64)code_base; |
| 230 | + ((u64 *)image)[1] = local_paca->kernel_toc; |
| 231 | +#endif |
| 232 | + |
| 233 | + fp->bpf_func = (void *)image; |
| 234 | + fp->jited = 1; |
| 235 | + fp->jited_len = alloclen; |
| 236 | + |
| 237 | + bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE)); |
| 238 | + if (!fp->is_func || extra_pass) { |
| 239 | + bpf_prog_fill_jited_linfo(fp, addrs); |
| 240 | +out_addrs: |
| 241 | + kfree(addrs); |
| 242 | + kfree(jit_data); |
| 243 | + fp->aux->jit_data = NULL; |
| 244 | + } else { |
| 245 | + jit_data->addrs = addrs; |
| 246 | + jit_data->ctx = cgctx; |
| 247 | + jit_data->proglen = proglen; |
| 248 | + jit_data->image = image; |
| 249 | + jit_data->header = bpf_hdr; |
| 250 | + } |
| 251 | + |
| 252 | +out: |
| 253 | + if (bpf_blinded) |
| 254 | + bpf_jit_prog_release_other(fp, fp == org_fp ? tmp_fp : org_fp); |
| 255 | + |
| 256 | + return fp; |
| 257 | +} |
| 258 | + |
| 259 | +/* Overriding bpf_jit_free() as we don't set images read-only. */ |
| 260 | +void bpf_jit_free(struct bpf_prog *fp) |
| 261 | +{ |
| 262 | + unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK; |
| 263 | + struct bpf_binary_header *bpf_hdr = (void *)addr; |
| 264 | + |
| 265 | + if (fp->jited) |
| 266 | + bpf_jit_binary_free(bpf_hdr); |
| 267 | + |
| 268 | + bpf_prog_unlock_free(fp); |
| 269 | +} |
0 commit comments