1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common functionality for RV32 and RV64 BPF JIT compilers
4 *
5 * Copyright (c) 2019 Björn Töpel <bjorn.topel@gmail.com>
6 *
7 */
8
9 #include <linux/bpf.h>
10 #include <linux/filter.h>
11 #include "bpf_jit.h"
12
13 /* Number of iterations to try until offsets converge. */
14 #define NR_JIT_ITERATIONS 16
15
build_body(struct rv_jit_context * ctx,bool extra_pass,int * offset)16 static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)
17 {
18 const struct bpf_prog *prog = ctx->prog;
19 int i;
20
21 for (i = 0; i < prog->len; i++) {
22 const struct bpf_insn *insn = &prog->insnsi[i];
23 int ret;
24
25 ret = bpf_jit_emit_insn(insn, ctx, extra_pass);
26 /* BPF_LD | BPF_IMM | BPF_DW: skip the next instruction. */
27 if (ret > 0)
28 i++;
29 if (offset)
30 offset[i] = ctx->ninsns;
31 if (ret < 0)
32 return ret;
33 }
34 return 0;
35 }
36
bpf_jit_needs_zext(void)37 bool bpf_jit_needs_zext(void)
38 {
39 return true;
40 }
41
bpf_int_jit_compile(struct bpf_prog * prog)42 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
43 {
44 bool tmp_blinded = false, extra_pass = false;
45 struct bpf_prog *tmp, *orig_prog = prog;
46 int pass = 0, prev_ninsns = 0, i;
47 struct rv_jit_data *jit_data;
48 struct rv_jit_context *ctx;
49 unsigned int image_size = 0;
50
51 if (!prog->jit_requested)
52 return orig_prog;
53
54 tmp = bpf_jit_blind_constants(prog);
55 if (IS_ERR(tmp))
56 return orig_prog;
57 if (tmp != prog) {
58 tmp_blinded = true;
59 prog = tmp;
60 }
61
62 jit_data = prog->aux->jit_data;
63 if (!jit_data) {
64 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
65 if (!jit_data) {
66 prog = orig_prog;
67 goto out;
68 }
69 prog->aux->jit_data = jit_data;
70 }
71
72 ctx = &jit_data->ctx;
73
74 if (ctx->offset) {
75 extra_pass = true;
76 image_size = sizeof(*ctx->insns) * ctx->ninsns;
77 goto skip_init_ctx;
78 }
79
80 ctx->prog = prog;
81 ctx->offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
82 if (!ctx->offset) {
83 prog = orig_prog;
84 goto out_offset;
85 }
86
87 if (build_body(ctx, extra_pass, NULL)) {
88 prog = orig_prog;
89 goto out_offset;
90 }
91
92 for (i = 0; i < prog->len; i++) {
93 prev_ninsns += 32;
94 ctx->offset[i] = prev_ninsns;
95 }
96
97 for (i = 0; i < NR_JIT_ITERATIONS; i++) {
98 pass++;
99 ctx->ninsns = 0;
100
101 bpf_jit_build_prologue(ctx);
102 ctx->prologue_len = ctx->ninsns;
103
104 if (build_body(ctx, extra_pass, ctx->offset)) {
105 prog = orig_prog;
106 goto out_offset;
107 }
108
109 ctx->epilogue_offset = ctx->ninsns;
110 bpf_jit_build_epilogue(ctx);
111
112 if (ctx->ninsns == prev_ninsns) {
113 if (jit_data->header)
114 break;
115
116 image_size = sizeof(*ctx->insns) * ctx->ninsns;
117 jit_data->header =
118 bpf_jit_binary_alloc(image_size,
119 &jit_data->image,
120 sizeof(u32),
121 bpf_fill_ill_insns);
122 if (!jit_data->header) {
123 prog = orig_prog;
124 goto out_offset;
125 }
126
127 ctx->insns = (u16 *)jit_data->image;
128 /*
129 * Now, when the image is allocated, the image can
130 * potentially shrink more (auipc/jalr -> jal).
131 */
132 }
133 prev_ninsns = ctx->ninsns;
134 }
135
136 if (i == NR_JIT_ITERATIONS) {
137 pr_err("bpf-jit: image did not converge in <%d passes!\n", i);
138 if (jit_data->header)
139 bpf_jit_binary_free(jit_data->header);
140 prog = orig_prog;
141 goto out_offset;
142 }
143
144 skip_init_ctx:
145 pass++;
146 ctx->ninsns = 0;
147
148 bpf_jit_build_prologue(ctx);
149 if (build_body(ctx, extra_pass, NULL)) {
150 bpf_jit_binary_free(jit_data->header);
151 prog = orig_prog;
152 goto out_offset;
153 }
154 bpf_jit_build_epilogue(ctx);
155
156 if (bpf_jit_enable > 1)
157 bpf_jit_dump(prog->len, image_size, pass, ctx->insns);
158
159 prog->bpf_func = (void *)ctx->insns;
160 prog->jited = 1;
161 prog->jited_len = image_size;
162
163 bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
164
165 if (!prog->is_func || extra_pass) {
166 bpf_jit_binary_lock_ro(jit_data->header);
167 for (i = 0; i < prog->len; i++)
168 ctx->offset[i] = ninsns_rvoff(ctx->offset[i]);
169 bpf_prog_fill_jited_linfo(prog, ctx->offset);
170 out_offset:
171 kfree(ctx->offset);
172 kfree(jit_data);
173 prog->aux->jit_data = NULL;
174 }
175 out:
176
177 if (tmp_blinded)
178 bpf_jit_prog_release_other(prog, prog == orig_prog ?
179 tmp : orig_prog);
180 return prog;
181 }
182
bpf_jit_alloc_exec_limit(void)183 u64 bpf_jit_alloc_exec_limit(void)
184 {
185 return BPF_JIT_REGION_SIZE;
186 }
187
bpf_jit_alloc_exec(unsigned long size)188 void *bpf_jit_alloc_exec(unsigned long size)
189 {
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));
194 }
195
bpf_jit_free_exec(void * addr)196 void bpf_jit_free_exec(void *addr)
197 {
198 return vfree(addr);
199 }
200