1 // SPDX-License-Identifier: GPL-2.0
3 * Common functionality for RV32 and RV64 BPF JIT compilers
10 #include <linux/filter.h>
11 #include <linux/memory.h>
12 #include <asm/patch.h>
16 /* Number of iterations to try until offsets converge. */
17 #define NR_JIT_ITERATIONS 32
19 static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)
21 const struct bpf_prog *prog = ctx->prog;
24 for (i = 0; i < prog->len; i++) {
25 const struct bpf_insn *insn = &prog->insnsi[i];
28 ret = bpf_jit_emit_insn(insn, ctx, extra_pass);
29 /* BPF_LD | BPF_IMM | BPF_DW: skip the next instruction. */
33 offset[i] = ctx->ninsns;
40 bool bpf_jit_needs_zext(void)
45 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
47 unsigned int prog_size = 0, extable_size = 0;
48 bool tmp_blinded = false, extra_pass = false;
49 struct bpf_prog *tmp, *orig_prog = prog;
50 int pass = 0, prev_ninsns = 0, i;
51 struct rv_jit_data *jit_data;
52 struct rv_jit_context *ctx;
54 if (!prog->jit_requested)
57 tmp = bpf_jit_blind_constants(prog);
65 jit_data = prog->aux->jit_data;
67 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
72 prog->aux->jit_data = jit_data;
79 prog_size = sizeof(*ctx->insns) * ctx->ninsns;
84 ctx->offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
90 if (build_body(ctx, extra_pass, NULL)) {
95 for (i = 0; i < prog->len; i++) {
97 ctx->offset[i] = prev_ninsns;
100 for (i = 0; i < NR_JIT_ITERATIONS; i++) {
104 bpf_jit_build_prologue(ctx, bpf_is_subprog(prog));
105 ctx->prologue_len = ctx->ninsns;
107 if (build_body(ctx, extra_pass, ctx->offset)) {
112 ctx->epilogue_offset = ctx->ninsns;
113 bpf_jit_build_epilogue(ctx);
115 if (ctx->ninsns == prev_ninsns) {
116 if (jit_data->header)
118 /* obtain the actual image size */
119 extable_size = prog->aux->num_exentries *
120 sizeof(struct exception_table_entry);
121 prog_size = sizeof(*ctx->insns) * ctx->ninsns;
123 jit_data->ro_header =
124 bpf_jit_binary_pack_alloc(prog_size + extable_size,
125 &jit_data->ro_image, sizeof(u32),
126 &jit_data->header, &jit_data->image,
128 if (!jit_data->ro_header) {
134 * Use the image(RW) for writing the JITed instructions. But also save
135 * the ro_image(RX) for calculating the offsets in the image. The RW
136 * image will be later copied to the RX image from where the program
137 * will run. The bpf_jit_binary_pack_finalize() will do this copy in the
140 ctx->ro_insns = (u16 *)jit_data->ro_image;
141 ctx->insns = (u16 *)jit_data->image;
143 * Now, when the image is allocated, the image can
144 * potentially shrink more (auipc/jalr -> jal).
147 prev_ninsns = ctx->ninsns;
150 if (i == NR_JIT_ITERATIONS) {
151 pr_err("bpf-jit: image did not converge in <%d passes!\n", i);
157 prog->aux->extable = (void *)ctx->ro_insns + prog_size;
164 bpf_jit_build_prologue(ctx, bpf_is_subprog(prog));
165 if (build_body(ctx, extra_pass, NULL)) {
169 bpf_jit_build_epilogue(ctx);
171 if (bpf_jit_enable > 1)
172 bpf_jit_dump(prog->len, prog_size, pass, ctx->insns);
174 prog->bpf_func = (void *)ctx->ro_insns + cfi_get_offset();
176 prog->jited_len = prog_size - cfi_get_offset();
178 if (!prog->is_func || extra_pass) {
179 if (WARN_ON(bpf_jit_binary_pack_finalize(prog, jit_data->ro_header,
180 jit_data->header))) {
181 /* ro_header has been freed */
182 jit_data->ro_header = NULL;
187 * The instructions have now been copied to the ROX region from
188 * where they will execute.
189 * Write any modified data cache blocks out to memory and
190 * invalidate the corresponding blocks in the instruction cache.
192 bpf_flush_icache(jit_data->ro_header, ctx->ro_insns + ctx->ninsns);
193 for (i = 0; i < prog->len; i++)
194 ctx->offset[i] = ninsns_rvoff(ctx->offset[i]);
195 bpf_prog_fill_jited_linfo(prog, ctx->offset);
199 prog->aux->jit_data = NULL;
204 bpf_jit_prog_release_other(prog, prog == orig_prog ?
209 if (jit_data->header) {
210 bpf_arch_text_copy(&jit_data->ro_header->size, &jit_data->header->size,
211 sizeof(jit_data->header->size));
212 bpf_jit_binary_pack_free(jit_data->ro_header, jit_data->header);
217 u64 bpf_jit_alloc_exec_limit(void)
219 return BPF_JIT_REGION_SIZE;
222 void *bpf_jit_alloc_exec(unsigned long size)
224 return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
225 BPF_JIT_REGION_END, GFP_KERNEL,
226 PAGE_KERNEL, 0, NUMA_NO_NODE,
227 __builtin_return_address(0));
230 void bpf_jit_free_exec(void *addr)
235 void *bpf_arch_text_copy(void *dst, void *src, size_t len)
239 mutex_lock(&text_mutex);
240 ret = patch_text_nosync(dst, src, len);
241 mutex_unlock(&text_mutex);
244 return ERR_PTR(-EINVAL);
249 int bpf_arch_text_invalidate(void *dst, size_t len)
253 mutex_lock(&text_mutex);
254 ret = patch_text_set_nosync(dst, 0, len);
255 mutex_unlock(&text_mutex);
260 void bpf_jit_free(struct bpf_prog *prog)
263 struct rv_jit_data *jit_data = prog->aux->jit_data;
264 struct bpf_binary_header *hdr;
267 * If we fail the final pass of JIT (from jit_subprogs),
268 * the program may not be finalized yet. Call finalize here
272 bpf_jit_binary_pack_finalize(prog, jit_data->ro_header, jit_data->header);
275 hdr = bpf_jit_binary_pack_hdr(prog);
276 bpf_jit_binary_pack_free(hdr, NULL);
277 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
280 bpf_prog_unlock_free(prog);