1 // SPDX-License-Identifier: GPL-2.0
3 * Common functionality for RV32 and RV64 BPF JIT compilers
10 #include <linux/filter.h>
13 /* Number of iterations to try until offsets converge. */
14 #define NR_JIT_ITERATIONS 32
16 static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)
18 const struct bpf_prog *prog = ctx->prog;
21 for (i = 0; i < prog->len; i++) {
22 const struct bpf_insn *insn = &prog->insnsi[i];
25 ret = bpf_jit_emit_insn(insn, ctx, extra_pass);
26 /* BPF_LD | BPF_IMM | BPF_DW: skip the next instruction. */
30 offset[i] = ctx->ninsns;
37 bool bpf_jit_needs_zext(void)
42 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
44 unsigned int prog_size = 0, extable_size = 0;
45 bool tmp_blinded = false, extra_pass = false;
46 struct bpf_prog *tmp, *orig_prog = prog;
47 int pass = 0, prev_ninsns = 0, prologue_len, i;
48 struct rv_jit_data *jit_data;
49 struct rv_jit_context *ctx;
51 if (!prog->jit_requested)
54 tmp = bpf_jit_blind_constants(prog);
62 jit_data = prog->aux->jit_data;
64 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
69 prog->aux->jit_data = jit_data;
76 prog_size = sizeof(*ctx->insns) * ctx->ninsns;
81 ctx->offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
86 for (i = 0; i < prog->len; i++) {
88 ctx->offset[i] = prev_ninsns;
91 for (i = 0; i < NR_JIT_ITERATIONS; i++) {
94 if (build_body(ctx, extra_pass, ctx->offset)) {
98 ctx->body_len = ctx->ninsns;
99 bpf_jit_build_prologue(ctx);
100 ctx->epilogue_offset = ctx->ninsns;
101 bpf_jit_build_epilogue(ctx);
103 if (ctx->ninsns == prev_ninsns) {
104 if (jit_data->header)
106 /* obtain the actual image size */
107 extable_size = prog->aux->num_exentries *
108 sizeof(struct exception_table_entry);
109 prog_size = sizeof(*ctx->insns) * ctx->ninsns;
112 bpf_jit_binary_alloc(prog_size + extable_size,
116 if (!jit_data->header) {
121 ctx->insns = (u16 *)jit_data->image;
123 * Now, when the image is allocated, the image can
124 * potentially shrink more (auipc/jalr -> jal).
127 prev_ninsns = ctx->ninsns;
130 if (i == NR_JIT_ITERATIONS) {
131 pr_err("bpf-jit: image did not converge in <%d passes!\n", i);
132 if (jit_data->header)
133 bpf_jit_binary_free(jit_data->header);
139 prog->aux->extable = (void *)ctx->insns + prog_size;
146 bpf_jit_build_prologue(ctx);
147 if (build_body(ctx, extra_pass, NULL)) {
148 bpf_jit_binary_free(jit_data->header);
152 bpf_jit_build_epilogue(ctx);
154 if (bpf_jit_enable > 1)
155 bpf_jit_dump(prog->len, prog_size, pass, ctx->insns);
157 prog->bpf_func = (void *)ctx->insns;
159 prog->jited_len = prog_size;
161 bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
163 if (!prog->is_func || extra_pass) {
164 bpf_jit_binary_lock_ro(jit_data->header);
165 prologue_len = ctx->epilogue_offset - ctx->body_len;
166 for (i = 0; i < prog->len; i++)
167 ctx->offset[i] = ninsns_rvoff(prologue_len +
169 bpf_prog_fill_jited_linfo(prog, ctx->offset);
173 prog->aux->jit_data = NULL;
178 bpf_jit_prog_release_other(prog, prog == orig_prog ?
183 u64 bpf_jit_alloc_exec_limit(void)
185 return BPF_JIT_REGION_SIZE;
188 void *bpf_jit_alloc_exec(unsigned long size)
190 return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
191 BPF_JIT_REGION_END, GFP_KERNEL,
192 PAGE_KERNEL, 0, NUMA_NO_NODE,
193 __builtin_return_address(0));
196 void bpf_jit_free_exec(void *addr)