bpfjit.c revision 1.1.2.3 1 1.1.2.2 yamt /*-
2 1.1.2.2 yamt * Copyright (c) 2011-2012 Alexander Nasonov.
3 1.1.2.2 yamt * All rights reserved.
4 1.1.2.2 yamt *
5 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
6 1.1.2.2 yamt * modification, are permitted provided that the following conditions
7 1.1.2.2 yamt * are met:
8 1.1.2.2 yamt *
9 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
11 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in
13 1.1.2.2 yamt * the documentation and/or other materials provided with the
14 1.1.2.2 yamt * distribution.
15 1.1.2.2 yamt *
16 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 1.1.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 1.1.2.2 yamt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 1.1.2.2 yamt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 1.1.2.2 yamt * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 1.1.2.2 yamt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 1.1.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 1.1.2.2 yamt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 1.1.2.2 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 1.1.2.2 yamt * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.2.2 yamt * SUCH DAMAGE.
28 1.1.2.2 yamt */
29 1.1.2.2 yamt
30 1.1.2.3 yamt #include <sys/cdefs.h>
31 1.1.2.3 yamt #ifdef _KERNEL
32 1.1.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: bpfjit.c,v 1.1.2.3 2013/01/16 05:33:48 yamt Exp $");
33 1.1.2.3 yamt #else
34 1.1.2.3 yamt __RCSID("$NetBSD: bpfjit.c,v 1.1.2.3 2013/01/16 05:33:48 yamt Exp $");
35 1.1.2.3 yamt #endif
36 1.1.2.3 yamt
37 1.1.2.2 yamt #include <net/bpfjit.h>
38 1.1.2.2 yamt
39 1.1.2.2 yamt #ifndef _KERNEL
40 1.1.2.2 yamt #include <assert.h>
41 1.1.2.2 yamt #define BPFJIT_ASSERT(c) assert(c)
42 1.1.2.2 yamt #else
43 1.1.2.2 yamt #define BPFJIT_ASSERT(c) KASSERT(c)
44 1.1.2.2 yamt #endif
45 1.1.2.2 yamt
46 1.1.2.2 yamt #ifndef _KERNEL
47 1.1.2.2 yamt #include <stdlib.h>
48 1.1.2.2 yamt #define BPFJIT_MALLOC(sz) malloc(sz)
49 1.1.2.2 yamt #define BPFJIT_FREE(p) free(p)
50 1.1.2.2 yamt #else
51 1.1.2.2 yamt #include <sys/malloc.h>
52 1.1.2.2 yamt #define BPFJIT_MALLOC(sz) kern_malloc(sz, M_WAITOK)
53 1.1.2.2 yamt #define BPFJIT_FREE(p) kern_free(p)
54 1.1.2.2 yamt #endif
55 1.1.2.2 yamt
56 1.1.2.2 yamt #ifndef _KERNEL
57 1.1.2.2 yamt #include <limits.h>
58 1.1.2.2 yamt #include <stdbool.h>
59 1.1.2.2 yamt #include <stddef.h>
60 1.1.2.2 yamt #include <stdint.h>
61 1.1.2.2 yamt #else
62 1.1.2.2 yamt #include <machine/limits.h>
63 1.1.2.2 yamt #include <sys/null.h>
64 1.1.2.2 yamt #include <sys/types.h>
65 1.1.2.2 yamt #include <sys/atomic.h>
66 1.1.2.2 yamt #include <sys/module.h>
67 1.1.2.2 yamt #endif
68 1.1.2.2 yamt
69 1.1.2.2 yamt #include <sys/queue.h>
70 1.1.2.2 yamt #include <sys/types.h>
71 1.1.2.2 yamt
72 1.1.2.2 yamt #include <sljitLir.h>
73 1.1.2.2 yamt
74 1.1.2.2 yamt #if !defined(_KERNEL) && defined(SLJIT_VERBOSE) && SLJIT_VERBOSE
75 1.1.2.2 yamt #include <stdio.h> /* for stderr */
76 1.1.2.2 yamt #endif
77 1.1.2.2 yamt
78 1.1.2.2 yamt
79 1.1.2.2 yamt #define BPFJIT_A SLJIT_TEMPORARY_REG1
80 1.1.2.2 yamt #define BPFJIT_X SLJIT_TEMPORARY_EREG1
81 1.1.2.2 yamt #define BPFJIT_TMP1 SLJIT_TEMPORARY_REG2
82 1.1.2.2 yamt #define BPFJIT_TMP2 SLJIT_TEMPORARY_REG3
83 1.1.2.2 yamt #define BPFJIT_BUF SLJIT_SAVED_REG1
84 1.1.2.2 yamt #define BPFJIT_WIRELEN SLJIT_SAVED_REG2
85 1.1.2.2 yamt #define BPFJIT_BUFLEN SLJIT_SAVED_REG3
86 1.1.2.2 yamt #define BPFJIT_KERN_TMP SLJIT_TEMPORARY_EREG2
87 1.1.2.2 yamt
88 1.1.2.2 yamt /*
89 1.1.2.2 yamt * Flags for bpfjit_optimization_hints().
90 1.1.2.2 yamt */
91 1.1.2.2 yamt #define BPFJIT_INIT_X 0x10000
92 1.1.2.2 yamt #define BPFJIT_INIT_A 0x20000
93 1.1.2.2 yamt
94 1.1.2.2 yamt
95 1.1.2.2 yamt /*
96 1.1.2.2 yamt * Node of bj_jumps list.
97 1.1.2.2 yamt */
98 1.1.2.2 yamt struct bpfjit_jump
99 1.1.2.2 yamt {
100 1.1.2.2 yamt struct sljit_jump *bj_jump;
101 1.1.2.2 yamt SLIST_ENTRY(bpfjit_jump) bj_entries;
102 1.1.2.2 yamt uint32_t bj_safe_length;
103 1.1.2.2 yamt };
104 1.1.2.2 yamt
105 1.1.2.2 yamt /*
106 1.1.2.2 yamt * Data for BPF_JMP instruction.
107 1.1.2.2 yamt */
108 1.1.2.2 yamt struct bpfjit_jump_data
109 1.1.2.2 yamt {
110 1.1.2.2 yamt /*
111 1.1.2.2 yamt * These entries make up bj_jumps list:
112 1.1.2.2 yamt * bj_jtf[0] - when coming from jt path,
113 1.1.2.2 yamt * bj_jtf[1] - when coming from jf path.
114 1.1.2.2 yamt */
115 1.1.2.2 yamt struct bpfjit_jump bj_jtf[2];
116 1.1.2.2 yamt };
117 1.1.2.2 yamt
118 1.1.2.2 yamt /*
119 1.1.2.2 yamt * Data for "read from packet" instructions.
120 1.1.2.2 yamt * See also read_pkt_insn() function below.
121 1.1.2.2 yamt */
122 1.1.2.2 yamt struct bpfjit_read_pkt_data
123 1.1.2.2 yamt {
124 1.1.2.2 yamt /*
125 1.1.2.2 yamt * If positive, emit "if (buflen < bj_check_length) return 0".
126 1.1.2.2 yamt * We assume that buflen is never equal to UINT32_MAX (otherwise,
127 1.1.2.2 yamt * we need a special bool variable to emit unconditional "return 0").
128 1.1.2.2 yamt */
129 1.1.2.2 yamt uint32_t bj_check_length;
130 1.1.2.2 yamt };
131 1.1.2.2 yamt
132 1.1.2.2 yamt /*
133 1.1.2.2 yamt * Additional (optimization-related) data for bpf_insn.
134 1.1.2.2 yamt */
135 1.1.2.2 yamt struct bpfjit_insn_data
136 1.1.2.2 yamt {
137 1.1.2.2 yamt /* List of jumps to this insn. */
138 1.1.2.2 yamt SLIST_HEAD(, bpfjit_jump) bj_jumps;
139 1.1.2.2 yamt
140 1.1.2.2 yamt union {
141 1.1.2.2 yamt struct bpfjit_jump_data bj_jdata;
142 1.1.2.2 yamt struct bpfjit_read_pkt_data bj_rdata;
143 1.1.2.2 yamt } bj_aux;
144 1.1.2.2 yamt
145 1.1.2.2 yamt bool bj_unreachable;
146 1.1.2.2 yamt };
147 1.1.2.2 yamt
148 1.1.2.2 yamt #ifdef _KERNEL
149 1.1.2.2 yamt
150 1.1.2.2 yamt uint32_t m_xword(const struct mbuf *, uint32_t, int *);
151 1.1.2.2 yamt uint32_t m_xhalf(const struct mbuf *, uint32_t, int *);
152 1.1.2.2 yamt uint32_t m_xbyte(const struct mbuf *, uint32_t, int *);
153 1.1.2.2 yamt
154 1.1.2.2 yamt MODULE(MODULE_CLASS_MISC, bpfjit, "sljit")
155 1.1.2.2 yamt
156 1.1.2.2 yamt static int
157 1.1.2.2 yamt bpfjit_modcmd(modcmd_t cmd, void *arg)
158 1.1.2.2 yamt {
159 1.1.2.2 yamt
160 1.1.2.2 yamt switch (cmd) {
161 1.1.2.2 yamt case MODULE_CMD_INIT:
162 1.1.2.2 yamt bpfjit_module_ops.bj_free_code = &bpfjit_free_code;
163 1.1.2.2 yamt membar_producer();
164 1.1.2.2 yamt bpfjit_module_ops.bj_generate_code = &bpfjit_generate_code;
165 1.1.2.2 yamt membar_producer();
166 1.1.2.2 yamt return 0;
167 1.1.2.2 yamt
168 1.1.2.2 yamt case MODULE_CMD_FINI:
169 1.1.2.2 yamt return EOPNOTSUPP;
170 1.1.2.2 yamt
171 1.1.2.2 yamt default:
172 1.1.2.2 yamt return ENOTTY;
173 1.1.2.2 yamt }
174 1.1.2.2 yamt }
175 1.1.2.2 yamt #endif
176 1.1.2.2 yamt
177 1.1.2.2 yamt static uint32_t
178 1.1.2.2 yamt read_width(struct bpf_insn *pc)
179 1.1.2.2 yamt {
180 1.1.2.2 yamt
181 1.1.2.2 yamt switch (BPF_SIZE(pc->code)) {
182 1.1.2.2 yamt case BPF_W:
183 1.1.2.2 yamt return 4;
184 1.1.2.2 yamt case BPF_H:
185 1.1.2.2 yamt return 2;
186 1.1.2.2 yamt case BPF_B:
187 1.1.2.2 yamt return 1;
188 1.1.2.2 yamt default:
189 1.1.2.2 yamt BPFJIT_ASSERT(false);
190 1.1.2.2 yamt return 0;
191 1.1.2.2 yamt }
192 1.1.2.2 yamt }
193 1.1.2.2 yamt
194 1.1.2.2 yamt /*
195 1.1.2.2 yamt * Get offset of M[k] on the stack.
196 1.1.2.2 yamt */
197 1.1.2.3 yamt static size_t
198 1.1.2.3 yamt mem_local_offset(uint32_t k, unsigned int minm)
199 1.1.2.2 yamt {
200 1.1.2.3 yamt size_t moff = (k - minm) * sizeof(uint32_t);
201 1.1.2.2 yamt
202 1.1.2.2 yamt #ifdef _KERNEL
203 1.1.2.2 yamt /*
204 1.1.2.2 yamt * 4 bytes for the third argument of m_xword/m_xhalf/m_xbyte.
205 1.1.2.2 yamt */
206 1.1.2.2 yamt return sizeof(uint32_t) + moff;
207 1.1.2.2 yamt #else
208 1.1.2.2 yamt return moff;
209 1.1.2.2 yamt #endif
210 1.1.2.2 yamt }
211 1.1.2.2 yamt
212 1.1.2.2 yamt /*
213 1.1.2.2 yamt * Generate code for BPF_LD+BPF_B+BPF_ABS A <- P[k:1].
214 1.1.2.2 yamt */
215 1.1.2.2 yamt static int
216 1.1.2.2 yamt emit_read8(struct sljit_compiler* compiler, uint32_t k)
217 1.1.2.2 yamt {
218 1.1.2.2 yamt
219 1.1.2.2 yamt return sljit_emit_op1(compiler,
220 1.1.2.2 yamt SLJIT_MOV_UB,
221 1.1.2.2 yamt BPFJIT_A, 0,
222 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k);
223 1.1.2.2 yamt }
224 1.1.2.2 yamt
225 1.1.2.2 yamt /*
226 1.1.2.2 yamt * Generate code for BPF_LD+BPF_H+BPF_ABS A <- P[k:2].
227 1.1.2.2 yamt */
228 1.1.2.2 yamt static int
229 1.1.2.2 yamt emit_read16(struct sljit_compiler* compiler, uint32_t k)
230 1.1.2.2 yamt {
231 1.1.2.2 yamt int status;
232 1.1.2.2 yamt
233 1.1.2.2 yamt /* tmp1 = buf[k]; */
234 1.1.2.2 yamt status = sljit_emit_op1(compiler,
235 1.1.2.2 yamt SLJIT_MOV_UB,
236 1.1.2.2 yamt BPFJIT_TMP1, 0,
237 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k);
238 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
239 1.1.2.2 yamt return status;
240 1.1.2.2 yamt
241 1.1.2.2 yamt /* A = buf[k+1]; */
242 1.1.2.2 yamt status = sljit_emit_op1(compiler,
243 1.1.2.2 yamt SLJIT_MOV_UB,
244 1.1.2.2 yamt BPFJIT_A, 0,
245 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k+1);
246 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
247 1.1.2.2 yamt return status;
248 1.1.2.2 yamt
249 1.1.2.2 yamt /* tmp1 = tmp1 << 8; */
250 1.1.2.2 yamt status = sljit_emit_op2(compiler,
251 1.1.2.2 yamt SLJIT_SHL,
252 1.1.2.2 yamt BPFJIT_TMP1, 0,
253 1.1.2.2 yamt BPFJIT_TMP1, 0,
254 1.1.2.2 yamt SLJIT_IMM, 8);
255 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
256 1.1.2.2 yamt return status;
257 1.1.2.2 yamt
258 1.1.2.2 yamt /* A = A + tmp1; */
259 1.1.2.2 yamt status = sljit_emit_op2(compiler,
260 1.1.2.2 yamt SLJIT_ADD,
261 1.1.2.2 yamt BPFJIT_A, 0,
262 1.1.2.2 yamt BPFJIT_A, 0,
263 1.1.2.2 yamt BPFJIT_TMP1, 0);
264 1.1.2.2 yamt return status;
265 1.1.2.2 yamt }
266 1.1.2.2 yamt
267 1.1.2.2 yamt /*
268 1.1.2.2 yamt * Generate code for BPF_LD+BPF_W+BPF_ABS A <- P[k:4].
269 1.1.2.2 yamt */
270 1.1.2.2 yamt static int
271 1.1.2.2 yamt emit_read32(struct sljit_compiler* compiler, uint32_t k)
272 1.1.2.2 yamt {
273 1.1.2.2 yamt int status;
274 1.1.2.2 yamt
275 1.1.2.2 yamt /* tmp1 = buf[k]; */
276 1.1.2.2 yamt status = sljit_emit_op1(compiler,
277 1.1.2.2 yamt SLJIT_MOV_UB,
278 1.1.2.2 yamt BPFJIT_TMP1, 0,
279 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k);
280 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
281 1.1.2.2 yamt return status;
282 1.1.2.2 yamt
283 1.1.2.2 yamt /* tmp2 = buf[k+1]; */
284 1.1.2.2 yamt status = sljit_emit_op1(compiler,
285 1.1.2.2 yamt SLJIT_MOV_UB,
286 1.1.2.2 yamt BPFJIT_TMP2, 0,
287 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k+1);
288 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
289 1.1.2.2 yamt return status;
290 1.1.2.2 yamt
291 1.1.2.2 yamt /* A = buf[k+3]; */
292 1.1.2.2 yamt status = sljit_emit_op1(compiler,
293 1.1.2.2 yamt SLJIT_MOV_UB,
294 1.1.2.2 yamt BPFJIT_A, 0,
295 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k+3);
296 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
297 1.1.2.2 yamt return status;
298 1.1.2.2 yamt
299 1.1.2.2 yamt /* tmp1 = tmp1 << 24; */
300 1.1.2.2 yamt status = sljit_emit_op2(compiler,
301 1.1.2.2 yamt SLJIT_SHL,
302 1.1.2.2 yamt BPFJIT_TMP1, 0,
303 1.1.2.2 yamt BPFJIT_TMP1, 0,
304 1.1.2.2 yamt SLJIT_IMM, 24);
305 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
306 1.1.2.2 yamt return status;
307 1.1.2.2 yamt
308 1.1.2.2 yamt /* A = A + tmp1; */
309 1.1.2.2 yamt status = sljit_emit_op2(compiler,
310 1.1.2.2 yamt SLJIT_ADD,
311 1.1.2.2 yamt BPFJIT_A, 0,
312 1.1.2.2 yamt BPFJIT_A, 0,
313 1.1.2.2 yamt BPFJIT_TMP1, 0);
314 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
315 1.1.2.2 yamt return status;
316 1.1.2.2 yamt
317 1.1.2.2 yamt /* tmp1 = buf[k+2]; */
318 1.1.2.2 yamt status = sljit_emit_op1(compiler,
319 1.1.2.2 yamt SLJIT_MOV_UB,
320 1.1.2.2 yamt BPFJIT_TMP1, 0,
321 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k+2);
322 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
323 1.1.2.2 yamt return status;
324 1.1.2.2 yamt
325 1.1.2.2 yamt /* tmp2 = tmp2 << 16; */
326 1.1.2.2 yamt status = sljit_emit_op2(compiler,
327 1.1.2.2 yamt SLJIT_SHL,
328 1.1.2.2 yamt BPFJIT_TMP2, 0,
329 1.1.2.2 yamt BPFJIT_TMP2, 0,
330 1.1.2.2 yamt SLJIT_IMM, 16);
331 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
332 1.1.2.2 yamt return status;
333 1.1.2.2 yamt
334 1.1.2.2 yamt /* A = A + tmp2; */
335 1.1.2.2 yamt status = sljit_emit_op2(compiler,
336 1.1.2.2 yamt SLJIT_ADD,
337 1.1.2.2 yamt BPFJIT_A, 0,
338 1.1.2.2 yamt BPFJIT_A, 0,
339 1.1.2.2 yamt BPFJIT_TMP2, 0);
340 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
341 1.1.2.2 yamt return status;
342 1.1.2.2 yamt
343 1.1.2.2 yamt /* tmp1 = tmp1 << 8; */
344 1.1.2.2 yamt status = sljit_emit_op2(compiler,
345 1.1.2.2 yamt SLJIT_SHL,
346 1.1.2.2 yamt BPFJIT_TMP1, 0,
347 1.1.2.2 yamt BPFJIT_TMP1, 0,
348 1.1.2.2 yamt SLJIT_IMM, 8);
349 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
350 1.1.2.2 yamt return status;
351 1.1.2.2 yamt
352 1.1.2.2 yamt /* A = A + tmp1; */
353 1.1.2.2 yamt status = sljit_emit_op2(compiler,
354 1.1.2.2 yamt SLJIT_ADD,
355 1.1.2.2 yamt BPFJIT_A, 0,
356 1.1.2.2 yamt BPFJIT_A, 0,
357 1.1.2.2 yamt BPFJIT_TMP1, 0);
358 1.1.2.2 yamt return status;
359 1.1.2.2 yamt }
360 1.1.2.2 yamt
361 1.1.2.2 yamt #ifdef _KERNEL
362 1.1.2.2 yamt /*
363 1.1.2.2 yamt * Generate m_xword/m_xhalf/m_xbyte call.
364 1.1.2.2 yamt *
365 1.1.2.2 yamt * pc is one of:
366 1.1.2.2 yamt * BPF_LD+BPF_W+BPF_ABS A <- P[k:4]
367 1.1.2.2 yamt * BPF_LD+BPF_H+BPF_ABS A <- P[k:2]
368 1.1.2.2 yamt * BPF_LD+BPF_B+BPF_ABS A <- P[k:1]
369 1.1.2.2 yamt * BPF_LD+BPF_W+BPF_IND A <- P[X+k:4]
370 1.1.2.2 yamt * BPF_LD+BPF_H+BPF_IND A <- P[X+k:2]
371 1.1.2.2 yamt * BPF_LD+BPF_B+BPF_IND A <- P[X+k:1]
372 1.1.2.2 yamt * BPF_LDX+BPF_B+BPF_MSH X <- 4*(P[k:1]&0xf)
373 1.1.2.2 yamt *
374 1.1.2.2 yamt * dst must be BPFJIT_A for BPF_LD instructions and BPFJIT_X
375 1.1.2.2 yamt * or any of BPFJIT_TMP* registrers for BPF_MSH instruction.
376 1.1.2.2 yamt */
377 1.1.2.2 yamt static int
378 1.1.2.2 yamt emit_xcall(struct sljit_compiler* compiler, struct bpf_insn *pc,
379 1.1.2.2 yamt int dst, sljit_w dstw, struct sljit_jump **ret0_jump,
380 1.1.2.2 yamt uint32_t (*fn)(const struct mbuf *, uint32_t, int *))
381 1.1.2.2 yamt {
382 1.1.2.2 yamt #if BPFJIT_X != SLJIT_TEMPORARY_EREG1 || \
383 1.1.2.2 yamt BPFJIT_X == SLJIT_RETURN_REG
384 1.1.2.2 yamt #error "Not supported assignment of registers."
385 1.1.2.2 yamt #endif
386 1.1.2.2 yamt int status;
387 1.1.2.2 yamt
388 1.1.2.2 yamt /*
389 1.1.2.2 yamt * The third argument of fn is an address on stack.
390 1.1.2.2 yamt */
391 1.1.2.2 yamt const int arg3_offset = 0;
392 1.1.2.2 yamt
393 1.1.2.2 yamt if (BPF_CLASS(pc->code) == BPF_LDX) {
394 1.1.2.2 yamt /* save A */
395 1.1.2.2 yamt status = sljit_emit_op1(compiler,
396 1.1.2.2 yamt SLJIT_MOV,
397 1.1.2.2 yamt BPFJIT_KERN_TMP, 0,
398 1.1.2.2 yamt BPFJIT_A, 0);
399 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
400 1.1.2.2 yamt return status;
401 1.1.2.2 yamt }
402 1.1.2.2 yamt
403 1.1.2.2 yamt /*
404 1.1.2.2 yamt * Prepare registers for fn(buf, k, &err) call.
405 1.1.2.2 yamt */
406 1.1.2.2 yamt status = sljit_emit_op1(compiler,
407 1.1.2.2 yamt SLJIT_MOV,
408 1.1.2.2 yamt SLJIT_TEMPORARY_REG1, 0,
409 1.1.2.2 yamt BPFJIT_BUF, 0);
410 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
411 1.1.2.2 yamt return status;
412 1.1.2.2 yamt
413 1.1.2.2 yamt if (BPF_CLASS(pc->code) == BPF_LD && BPF_MODE(pc->code) == BPF_IND) {
414 1.1.2.2 yamt status = sljit_emit_op2(compiler,
415 1.1.2.2 yamt SLJIT_ADD,
416 1.1.2.2 yamt SLJIT_TEMPORARY_REG2, 0,
417 1.1.2.2 yamt BPFJIT_X, 0,
418 1.1.2.2 yamt SLJIT_IMM, (uint32_t)pc->k);
419 1.1.2.2 yamt } else {
420 1.1.2.2 yamt status = sljit_emit_op1(compiler,
421 1.1.2.2 yamt SLJIT_MOV,
422 1.1.2.2 yamt SLJIT_TEMPORARY_REG2, 0,
423 1.1.2.2 yamt SLJIT_IMM, (uint32_t)pc->k);
424 1.1.2.2 yamt }
425 1.1.2.2 yamt
426 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
427 1.1.2.2 yamt return status;
428 1.1.2.2 yamt
429 1.1.2.2 yamt status = sljit_get_local_base(compiler,
430 1.1.2.2 yamt SLJIT_TEMPORARY_REG3, 0, arg3_offset);
431 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
432 1.1.2.2 yamt return status;
433 1.1.2.2 yamt
434 1.1.2.2 yamt /* fn(buf, k, &err); */
435 1.1.2.2 yamt status = sljit_emit_ijump(compiler,
436 1.1.2.2 yamt SLJIT_CALL3,
437 1.1.2.2 yamt SLJIT_IMM, SLJIT_FUNC_OFFSET(fn));
438 1.1.2.2 yamt
439 1.1.2.2 yamt if (BPF_CLASS(pc->code) == BPF_LDX) {
440 1.1.2.2 yamt
441 1.1.2.2 yamt /* move return value to dst */
442 1.1.2.2 yamt BPFJIT_ASSERT(dst != SLJIT_RETURN_REG);
443 1.1.2.2 yamt status = sljit_emit_op1(compiler,
444 1.1.2.2 yamt SLJIT_MOV,
445 1.1.2.2 yamt dst, dstw,
446 1.1.2.2 yamt SLJIT_RETURN_REG, 0);
447 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
448 1.1.2.2 yamt return status;
449 1.1.2.2 yamt
450 1.1.2.2 yamt /* restore A */
451 1.1.2.2 yamt status = sljit_emit_op1(compiler,
452 1.1.2.2 yamt SLJIT_MOV,
453 1.1.2.2 yamt BPFJIT_A, 0,
454 1.1.2.2 yamt BPFJIT_KERN_TMP, 0);
455 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
456 1.1.2.2 yamt return status;
457 1.1.2.2 yamt
458 1.1.2.2 yamt } else if (dst != SLJIT_RETURN_REG) {
459 1.1.2.2 yamt status = sljit_emit_op1(compiler,
460 1.1.2.2 yamt SLJIT_MOV,
461 1.1.2.2 yamt dst, dstw,
462 1.1.2.2 yamt SLJIT_RETURN_REG, 0);
463 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
464 1.1.2.2 yamt return status;
465 1.1.2.2 yamt }
466 1.1.2.2 yamt
467 1.1.2.2 yamt /* tmp3 = *err; */
468 1.1.2.2 yamt status = sljit_emit_op1(compiler,
469 1.1.2.2 yamt SLJIT_MOV_UI,
470 1.1.2.2 yamt SLJIT_TEMPORARY_REG3, 0,
471 1.1.2.2 yamt SLJIT_MEM1(SLJIT_LOCALS_REG), arg3_offset);
472 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
473 1.1.2.2 yamt return status;
474 1.1.2.2 yamt
475 1.1.2.2 yamt /* if (tmp3 != 0) return 0; */
476 1.1.2.2 yamt *ret0_jump = sljit_emit_cmp(compiler,
477 1.1.2.2 yamt SLJIT_C_NOT_EQUAL,
478 1.1.2.2 yamt SLJIT_TEMPORARY_REG3, 0,
479 1.1.2.2 yamt SLJIT_IMM, 0);
480 1.1.2.2 yamt if (*ret0_jump == NULL)
481 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
482 1.1.2.2 yamt
483 1.1.2.2 yamt return status;
484 1.1.2.2 yamt }
485 1.1.2.2 yamt #endif
486 1.1.2.2 yamt
487 1.1.2.2 yamt /*
488 1.1.2.2 yamt * Generate code for
489 1.1.2.2 yamt * BPF_LD+BPF_W+BPF_ABS A <- P[k:4]
490 1.1.2.2 yamt * BPF_LD+BPF_H+BPF_ABS A <- P[k:2]
491 1.1.2.2 yamt * BPF_LD+BPF_B+BPF_ABS A <- P[k:1]
492 1.1.2.2 yamt * BPF_LD+BPF_W+BPF_IND A <- P[X+k:4]
493 1.1.2.2 yamt * BPF_LD+BPF_H+BPF_IND A <- P[X+k:2]
494 1.1.2.2 yamt * BPF_LD+BPF_B+BPF_IND A <- P[X+k:1]
495 1.1.2.2 yamt */
496 1.1.2.2 yamt static int
497 1.1.2.2 yamt emit_pkt_read(struct sljit_compiler* compiler,
498 1.1.2.2 yamt struct bpf_insn *pc, struct sljit_jump *to_mchain_jump,
499 1.1.2.2 yamt struct sljit_jump **ret0, size_t *ret0_size)
500 1.1.2.2 yamt {
501 1.1.2.2 yamt int status;
502 1.1.2.2 yamt uint32_t width;
503 1.1.2.2 yamt struct sljit_jump *jump;
504 1.1.2.2 yamt #ifdef _KERNEL
505 1.1.2.2 yamt struct sljit_label *label;
506 1.1.2.2 yamt struct sljit_jump *over_mchain_jump;
507 1.1.2.2 yamt const bool check_zero_buflen = (to_mchain_jump != NULL);
508 1.1.2.2 yamt #endif
509 1.1.2.2 yamt const uint32_t k = pc->k;
510 1.1.2.2 yamt
511 1.1.2.2 yamt #ifdef _KERNEL
512 1.1.2.2 yamt if (to_mchain_jump == NULL) {
513 1.1.2.2 yamt to_mchain_jump = sljit_emit_cmp(compiler,
514 1.1.2.2 yamt SLJIT_C_EQUAL,
515 1.1.2.2 yamt BPFJIT_BUFLEN, 0,
516 1.1.2.2 yamt SLJIT_IMM, 0);
517 1.1.2.2 yamt if (to_mchain_jump == NULL)
518 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
519 1.1.2.2 yamt }
520 1.1.2.2 yamt #endif
521 1.1.2.2 yamt
522 1.1.2.2 yamt width = read_width(pc);
523 1.1.2.2 yamt
524 1.1.2.2 yamt if (BPF_MODE(pc->code) == BPF_IND) {
525 1.1.2.2 yamt /* tmp1 = buflen - (pc->k + width); */
526 1.1.2.2 yamt status = sljit_emit_op2(compiler,
527 1.1.2.2 yamt SLJIT_SUB,
528 1.1.2.2 yamt BPFJIT_TMP1, 0,
529 1.1.2.2 yamt BPFJIT_BUFLEN, 0,
530 1.1.2.2 yamt SLJIT_IMM, k + width);
531 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
532 1.1.2.2 yamt return status;
533 1.1.2.2 yamt
534 1.1.2.2 yamt /* buf += X; */
535 1.1.2.2 yamt status = sljit_emit_op2(compiler,
536 1.1.2.2 yamt SLJIT_ADD,
537 1.1.2.2 yamt BPFJIT_BUF, 0,
538 1.1.2.2 yamt BPFJIT_BUF, 0,
539 1.1.2.2 yamt BPFJIT_X, 0);
540 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
541 1.1.2.2 yamt return status;
542 1.1.2.2 yamt
543 1.1.2.2 yamt /* if (tmp1 < X) return 0; */
544 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
545 1.1.2.2 yamt SLJIT_C_LESS,
546 1.1.2.2 yamt BPFJIT_TMP1, 0,
547 1.1.2.2 yamt BPFJIT_X, 0);
548 1.1.2.2 yamt if (jump == NULL)
549 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
550 1.1.2.2 yamt ret0[(*ret0_size)++] = jump;
551 1.1.2.2 yamt }
552 1.1.2.2 yamt
553 1.1.2.2 yamt switch (width) {
554 1.1.2.2 yamt case 4:
555 1.1.2.2 yamt status = emit_read32(compiler, k);
556 1.1.2.2 yamt break;
557 1.1.2.2 yamt case 2:
558 1.1.2.2 yamt status = emit_read16(compiler, k);
559 1.1.2.2 yamt break;
560 1.1.2.2 yamt case 1:
561 1.1.2.2 yamt status = emit_read8(compiler, k);
562 1.1.2.2 yamt break;
563 1.1.2.2 yamt }
564 1.1.2.2 yamt
565 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
566 1.1.2.2 yamt return status;
567 1.1.2.2 yamt
568 1.1.2.2 yamt if (BPF_MODE(pc->code) == BPF_IND) {
569 1.1.2.2 yamt /* buf -= X; */
570 1.1.2.2 yamt status = sljit_emit_op2(compiler,
571 1.1.2.2 yamt SLJIT_SUB,
572 1.1.2.2 yamt BPFJIT_BUF, 0,
573 1.1.2.2 yamt BPFJIT_BUF, 0,
574 1.1.2.2 yamt BPFJIT_X, 0);
575 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
576 1.1.2.2 yamt return status;
577 1.1.2.2 yamt }
578 1.1.2.2 yamt
579 1.1.2.2 yamt #ifdef _KERNEL
580 1.1.2.2 yamt over_mchain_jump = sljit_emit_jump(compiler, SLJIT_JUMP);
581 1.1.2.2 yamt if (over_mchain_jump == NULL)
582 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
583 1.1.2.2 yamt
584 1.1.2.2 yamt /* entry point to mchain handler */
585 1.1.2.2 yamt label = sljit_emit_label(compiler);
586 1.1.2.2 yamt if (label == NULL)
587 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
588 1.1.2.2 yamt sljit_set_label(to_mchain_jump, label);
589 1.1.2.2 yamt
590 1.1.2.2 yamt if (check_zero_buflen) {
591 1.1.2.2 yamt /* if (buflen != 0) return 0; */
592 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
593 1.1.2.2 yamt SLJIT_C_NOT_EQUAL,
594 1.1.2.2 yamt BPFJIT_BUFLEN, 0,
595 1.1.2.2 yamt SLJIT_IMM, 0);
596 1.1.2.2 yamt if (jump == NULL)
597 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
598 1.1.2.2 yamt ret0[(*ret0_size)++] = jump;
599 1.1.2.2 yamt }
600 1.1.2.2 yamt
601 1.1.2.2 yamt switch (width) {
602 1.1.2.2 yamt case 4:
603 1.1.2.2 yamt status = emit_xcall(compiler, pc, BPFJIT_A, 0, &jump, &m_xword);
604 1.1.2.2 yamt break;
605 1.1.2.2 yamt case 2:
606 1.1.2.2 yamt status = emit_xcall(compiler, pc, BPFJIT_A, 0, &jump, &m_xhalf);
607 1.1.2.2 yamt break;
608 1.1.2.2 yamt case 1:
609 1.1.2.2 yamt status = emit_xcall(compiler, pc, BPFJIT_A, 0, &jump, &m_xbyte);
610 1.1.2.2 yamt break;
611 1.1.2.2 yamt }
612 1.1.2.2 yamt
613 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
614 1.1.2.2 yamt return status;
615 1.1.2.2 yamt
616 1.1.2.2 yamt ret0[(*ret0_size)++] = jump;
617 1.1.2.2 yamt
618 1.1.2.2 yamt label = sljit_emit_label(compiler);
619 1.1.2.2 yamt if (label == NULL)
620 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
621 1.1.2.2 yamt sljit_set_label(over_mchain_jump, label);
622 1.1.2.2 yamt #endif
623 1.1.2.2 yamt
624 1.1.2.2 yamt return status;
625 1.1.2.2 yamt }
626 1.1.2.2 yamt
627 1.1.2.2 yamt /*
628 1.1.2.2 yamt * Generate code for BPF_LDX+BPF_B+BPF_MSH X <- 4*(P[k:1]&0xf).
629 1.1.2.2 yamt */
630 1.1.2.2 yamt static int
631 1.1.2.2 yamt emit_msh(struct sljit_compiler* compiler,
632 1.1.2.2 yamt struct bpf_insn *pc, struct sljit_jump *to_mchain_jump,
633 1.1.2.2 yamt struct sljit_jump **ret0, size_t *ret0_size)
634 1.1.2.2 yamt {
635 1.1.2.2 yamt int status;
636 1.1.2.2 yamt #ifdef _KERNEL
637 1.1.2.2 yamt struct sljit_label *label;
638 1.1.2.2 yamt struct sljit_jump *jump, *over_mchain_jump;
639 1.1.2.2 yamt const bool check_zero_buflen = (to_mchain_jump != NULL);
640 1.1.2.2 yamt #endif
641 1.1.2.2 yamt const uint32_t k = pc->k;
642 1.1.2.2 yamt
643 1.1.2.2 yamt #ifdef _KERNEL
644 1.1.2.2 yamt if (to_mchain_jump == NULL) {
645 1.1.2.2 yamt to_mchain_jump = sljit_emit_cmp(compiler,
646 1.1.2.2 yamt SLJIT_C_EQUAL,
647 1.1.2.2 yamt BPFJIT_BUFLEN, 0,
648 1.1.2.2 yamt SLJIT_IMM, 0);
649 1.1.2.2 yamt if (to_mchain_jump == NULL)
650 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
651 1.1.2.2 yamt }
652 1.1.2.2 yamt #endif
653 1.1.2.2 yamt
654 1.1.2.2 yamt /* tmp1 = buf[k] */
655 1.1.2.2 yamt status = sljit_emit_op1(compiler,
656 1.1.2.2 yamt SLJIT_MOV_UB,
657 1.1.2.2 yamt BPFJIT_TMP1, 0,
658 1.1.2.2 yamt SLJIT_MEM1(BPFJIT_BUF), k);
659 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
660 1.1.2.2 yamt return status;
661 1.1.2.2 yamt
662 1.1.2.2 yamt /* tmp1 &= 0xf */
663 1.1.2.2 yamt status = sljit_emit_op2(compiler,
664 1.1.2.2 yamt SLJIT_AND,
665 1.1.2.2 yamt BPFJIT_TMP1, 0,
666 1.1.2.2 yamt BPFJIT_TMP1, 0,
667 1.1.2.2 yamt SLJIT_IMM, 0xf);
668 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
669 1.1.2.2 yamt return status;
670 1.1.2.2 yamt
671 1.1.2.2 yamt /* tmp1 = tmp1 << 2 */
672 1.1.2.2 yamt status = sljit_emit_op2(compiler,
673 1.1.2.2 yamt SLJIT_SHL,
674 1.1.2.2 yamt BPFJIT_X, 0,
675 1.1.2.2 yamt BPFJIT_TMP1, 0,
676 1.1.2.2 yamt SLJIT_IMM, 2);
677 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
678 1.1.2.2 yamt return status;
679 1.1.2.2 yamt
680 1.1.2.2 yamt #ifdef _KERNEL
681 1.1.2.2 yamt over_mchain_jump = sljit_emit_jump(compiler, SLJIT_JUMP);
682 1.1.2.2 yamt if (over_mchain_jump == NULL)
683 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
684 1.1.2.2 yamt
685 1.1.2.2 yamt /* entry point to mchain handler */
686 1.1.2.2 yamt label = sljit_emit_label(compiler);
687 1.1.2.2 yamt if (label == NULL)
688 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
689 1.1.2.2 yamt sljit_set_label(to_mchain_jump, label);
690 1.1.2.2 yamt
691 1.1.2.2 yamt if (check_zero_buflen) {
692 1.1.2.2 yamt /* if (buflen != 0) return 0; */
693 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
694 1.1.2.2 yamt SLJIT_C_NOT_EQUAL,
695 1.1.2.2 yamt BPFJIT_BUFLEN, 0,
696 1.1.2.2 yamt SLJIT_IMM, 0);
697 1.1.2.2 yamt if (jump == NULL)
698 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
699 1.1.2.2 yamt ret0[(*ret0_size)++] = jump;
700 1.1.2.2 yamt }
701 1.1.2.2 yamt
702 1.1.2.2 yamt status = emit_xcall(compiler, pc, BPFJIT_TMP1, 0, &jump, &m_xbyte);
703 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
704 1.1.2.2 yamt return status;
705 1.1.2.2 yamt ret0[(*ret0_size)++] = jump;
706 1.1.2.2 yamt
707 1.1.2.2 yamt /* tmp1 &= 0xf */
708 1.1.2.2 yamt status = sljit_emit_op2(compiler,
709 1.1.2.2 yamt SLJIT_AND,
710 1.1.2.2 yamt BPFJIT_TMP1, 0,
711 1.1.2.2 yamt BPFJIT_TMP1, 0,
712 1.1.2.2 yamt SLJIT_IMM, 0xf);
713 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
714 1.1.2.2 yamt return status;
715 1.1.2.2 yamt
716 1.1.2.2 yamt /* tmp1 = tmp1 << 2 */
717 1.1.2.2 yamt status = sljit_emit_op2(compiler,
718 1.1.2.2 yamt SLJIT_SHL,
719 1.1.2.2 yamt BPFJIT_X, 0,
720 1.1.2.2 yamt BPFJIT_TMP1, 0,
721 1.1.2.2 yamt SLJIT_IMM, 2);
722 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
723 1.1.2.2 yamt return status;
724 1.1.2.2 yamt
725 1.1.2.2 yamt
726 1.1.2.2 yamt label = sljit_emit_label(compiler);
727 1.1.2.2 yamt if (label == NULL)
728 1.1.2.2 yamt return SLJIT_ERR_ALLOC_FAILED;
729 1.1.2.2 yamt sljit_set_label(over_mchain_jump, label);
730 1.1.2.2 yamt #endif
731 1.1.2.2 yamt
732 1.1.2.2 yamt return status;
733 1.1.2.2 yamt }
734 1.1.2.2 yamt
735 1.1.2.2 yamt static int
736 1.1.2.2 yamt emit_pow2_division(struct sljit_compiler* compiler, uint32_t k)
737 1.1.2.2 yamt {
738 1.1.2.2 yamt int shift = 0;
739 1.1.2.2 yamt int status = SLJIT_SUCCESS;
740 1.1.2.2 yamt
741 1.1.2.2 yamt while (k > 1) {
742 1.1.2.2 yamt k >>= 1;
743 1.1.2.2 yamt shift++;
744 1.1.2.2 yamt }
745 1.1.2.2 yamt
746 1.1.2.2 yamt BPFJIT_ASSERT(k == 1 && shift < 32);
747 1.1.2.2 yamt
748 1.1.2.2 yamt if (shift != 0) {
749 1.1.2.2 yamt status = sljit_emit_op2(compiler,
750 1.1.2.2 yamt SLJIT_LSHR|SLJIT_INT_OP,
751 1.1.2.2 yamt BPFJIT_A, 0,
752 1.1.2.2 yamt BPFJIT_A, 0,
753 1.1.2.2 yamt SLJIT_IMM, shift);
754 1.1.2.2 yamt }
755 1.1.2.2 yamt
756 1.1.2.2 yamt return status;
757 1.1.2.2 yamt }
758 1.1.2.2 yamt
759 1.1.2.2 yamt #if !defined(BPFJIT_USE_UDIV)
760 1.1.2.2 yamt static sljit_uw
761 1.1.2.2 yamt divide(sljit_uw x, sljit_uw y)
762 1.1.2.2 yamt {
763 1.1.2.2 yamt
764 1.1.2.2 yamt return (uint32_t)x / (uint32_t)y;
765 1.1.2.2 yamt }
766 1.1.2.2 yamt #endif
767 1.1.2.2 yamt
768 1.1.2.2 yamt /*
769 1.1.2.2 yamt * Generate A = A / div.
770 1.1.2.2 yamt * divt,divw are either SLJIT_IMM,pc->k or BPFJIT_X,0.
771 1.1.2.2 yamt */
772 1.1.2.2 yamt static int
773 1.1.2.2 yamt emit_division(struct sljit_compiler* compiler, int divt, sljit_w divw)
774 1.1.2.2 yamt {
775 1.1.2.2 yamt int status;
776 1.1.2.2 yamt
777 1.1.2.2 yamt #if BPFJIT_X == SLJIT_TEMPORARY_REG1 || \
778 1.1.2.2 yamt BPFJIT_X == SLJIT_RETURN_REG || \
779 1.1.2.2 yamt BPFJIT_X == SLJIT_TEMPORARY_REG2 || \
780 1.1.2.2 yamt BPFJIT_A == SLJIT_TEMPORARY_REG2
781 1.1.2.2 yamt #error "Not supported assignment of registers."
782 1.1.2.2 yamt #endif
783 1.1.2.2 yamt
784 1.1.2.2 yamt #if BPFJIT_A != SLJIT_TEMPORARY_REG1
785 1.1.2.2 yamt status = sljit_emit_op1(compiler,
786 1.1.2.2 yamt SLJIT_MOV,
787 1.1.2.2 yamt SLJIT_TEMPORARY_REG1, 0,
788 1.1.2.2 yamt BPFJIT_A, 0);
789 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
790 1.1.2.2 yamt return status;
791 1.1.2.2 yamt #endif
792 1.1.2.2 yamt
793 1.1.2.2 yamt status = sljit_emit_op1(compiler,
794 1.1.2.2 yamt SLJIT_MOV,
795 1.1.2.2 yamt SLJIT_TEMPORARY_REG2, 0,
796 1.1.2.2 yamt divt, divw);
797 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
798 1.1.2.2 yamt return status;
799 1.1.2.2 yamt
800 1.1.2.2 yamt #if defined(BPFJIT_USE_UDIV)
801 1.1.2.2 yamt status = sljit_emit_op0(compiler, SLJIT_UDIV|SLJIT_INT_OP);
802 1.1.2.2 yamt
803 1.1.2.2 yamt #if BPFJIT_A != SLJIT_TEMPORARY_REG1
804 1.1.2.2 yamt status = sljit_emit_op1(compiler,
805 1.1.2.2 yamt SLJIT_MOV,
806 1.1.2.2 yamt BPFJIT_A, 0,
807 1.1.2.2 yamt SLJIT_TEMPORARY_REG1, 0);
808 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
809 1.1.2.2 yamt return status;
810 1.1.2.2 yamt #endif
811 1.1.2.2 yamt #else
812 1.1.2.2 yamt status = sljit_emit_ijump(compiler,
813 1.1.2.2 yamt SLJIT_CALL2,
814 1.1.2.2 yamt SLJIT_IMM, SLJIT_FUNC_OFFSET(divide));
815 1.1.2.2 yamt
816 1.1.2.2 yamt #if BPFJIT_A != SLJIT_RETURN_REG
817 1.1.2.2 yamt status = sljit_emit_op1(compiler,
818 1.1.2.2 yamt SLJIT_MOV,
819 1.1.2.2 yamt BPFJIT_A, 0,
820 1.1.2.2 yamt SLJIT_RETURN_REG, 0);
821 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
822 1.1.2.2 yamt return status;
823 1.1.2.2 yamt #endif
824 1.1.2.2 yamt #endif
825 1.1.2.2 yamt
826 1.1.2.2 yamt return status;
827 1.1.2.2 yamt }
828 1.1.2.2 yamt
829 1.1.2.2 yamt /*
830 1.1.2.2 yamt * Count BPF_RET instructions.
831 1.1.2.2 yamt */
832 1.1.2.2 yamt static size_t
833 1.1.2.2 yamt count_returns(struct bpf_insn *insns, size_t insn_count)
834 1.1.2.2 yamt {
835 1.1.2.2 yamt size_t i;
836 1.1.2.2 yamt size_t rv;
837 1.1.2.2 yamt
838 1.1.2.2 yamt rv = 0;
839 1.1.2.2 yamt for (i = 0; i < insn_count; i++) {
840 1.1.2.2 yamt if (BPF_CLASS(insns[i].code) == BPF_RET)
841 1.1.2.2 yamt rv++;
842 1.1.2.2 yamt }
843 1.1.2.2 yamt
844 1.1.2.2 yamt return rv;
845 1.1.2.2 yamt }
846 1.1.2.2 yamt
847 1.1.2.2 yamt /*
848 1.1.2.2 yamt * Return true if pc is a "read from packet" instruction.
849 1.1.2.2 yamt * If length is not NULL and return value is true, *length will
850 1.1.2.2 yamt * be set to a safe length required to read a packet.
851 1.1.2.2 yamt */
852 1.1.2.2 yamt static bool
853 1.1.2.2 yamt read_pkt_insn(struct bpf_insn *pc, uint32_t *length)
854 1.1.2.2 yamt {
855 1.1.2.2 yamt bool rv;
856 1.1.2.2 yamt uint32_t width;
857 1.1.2.2 yamt
858 1.1.2.2 yamt switch (BPF_CLASS(pc->code)) {
859 1.1.2.2 yamt default:
860 1.1.2.2 yamt rv = false;
861 1.1.2.2 yamt break;
862 1.1.2.2 yamt
863 1.1.2.2 yamt case BPF_LD:
864 1.1.2.2 yamt rv = BPF_MODE(pc->code) == BPF_ABS ||
865 1.1.2.2 yamt BPF_MODE(pc->code) == BPF_IND;
866 1.1.2.2 yamt if (rv)
867 1.1.2.2 yamt width = read_width(pc);
868 1.1.2.2 yamt break;
869 1.1.2.2 yamt
870 1.1.2.2 yamt case BPF_LDX:
871 1.1.2.2 yamt rv = pc->code == (BPF_LDX|BPF_B|BPF_MSH);
872 1.1.2.2 yamt width = 1;
873 1.1.2.2 yamt break;
874 1.1.2.2 yamt }
875 1.1.2.2 yamt
876 1.1.2.2 yamt if (rv && length != NULL) {
877 1.1.2.2 yamt *length = (pc->k > UINT32_MAX - width) ?
878 1.1.2.2 yamt UINT32_MAX : pc->k + width;
879 1.1.2.2 yamt }
880 1.1.2.2 yamt
881 1.1.2.2 yamt return rv;
882 1.1.2.2 yamt }
883 1.1.2.2 yamt
884 1.1.2.2 yamt /*
885 1.1.2.2 yamt * Set bj_check_length for all "read from packet" instructions
886 1.1.2.2 yamt * in a linear block of instructions [from, to).
887 1.1.2.2 yamt */
888 1.1.2.2 yamt static void
889 1.1.2.2 yamt set_check_length(struct bpf_insn *insns, struct bpfjit_insn_data *insn_dat,
890 1.1.2.2 yamt size_t from, size_t to, uint32_t length)
891 1.1.2.2 yamt {
892 1.1.2.2 yamt
893 1.1.2.2 yamt for (; from < to; from++) {
894 1.1.2.2 yamt if (read_pkt_insn(&insns[from], NULL)) {
895 1.1.2.2 yamt insn_dat[from].bj_aux.bj_rdata.bj_check_length = length;
896 1.1.2.2 yamt length = 0;
897 1.1.2.2 yamt }
898 1.1.2.2 yamt }
899 1.1.2.2 yamt }
900 1.1.2.2 yamt
901 1.1.2.2 yamt /*
902 1.1.2.2 yamt * The function divides instructions into blocks. Destination of a jump
903 1.1.2.2 yamt * instruction starts a new block. BPF_RET and BPF_JMP instructions
904 1.1.2.2 yamt * terminate a block. Blocks are linear, that is, there are no jumps out
905 1.1.2.2 yamt * from the middle of a block and there are no jumps in to the middle of
906 1.1.2.2 yamt * a block.
907 1.1.2.2 yamt * If a block has one or more "read from packet" instructions,
908 1.1.2.2 yamt * bj_check_length will be set to one value for the whole block and that
909 1.1.2.2 yamt * value will be equal to the greatest value of safe lengths of "read from
910 1.1.2.2 yamt * packet" instructions inside the block.
911 1.1.2.2 yamt */
912 1.1.2.2 yamt static int
913 1.1.2.2 yamt optimize(struct bpf_insn *insns,
914 1.1.2.2 yamt struct bpfjit_insn_data *insn_dat, size_t insn_count)
915 1.1.2.2 yamt {
916 1.1.2.2 yamt size_t i;
917 1.1.2.2 yamt size_t first_read;
918 1.1.2.2 yamt bool unreachable;
919 1.1.2.2 yamt uint32_t jt, jf;
920 1.1.2.2 yamt uint32_t length, safe_length;
921 1.1.2.2 yamt struct bpfjit_jump *jmp, *jtf;
922 1.1.2.2 yamt
923 1.1.2.2 yamt for (i = 0; i < insn_count; i++)
924 1.1.2.2 yamt SLIST_INIT(&insn_dat[i].bj_jumps);
925 1.1.2.2 yamt
926 1.1.2.2 yamt safe_length = 0;
927 1.1.2.2 yamt unreachable = false;
928 1.1.2.2 yamt first_read = SIZE_MAX;
929 1.1.2.2 yamt
930 1.1.2.2 yamt for (i = 0; i < insn_count; i++) {
931 1.1.2.2 yamt
932 1.1.2.2 yamt if (!SLIST_EMPTY(&insn_dat[i].bj_jumps)) {
933 1.1.2.2 yamt unreachable = false;
934 1.1.2.2 yamt
935 1.1.2.2 yamt set_check_length(insns, insn_dat,
936 1.1.2.2 yamt first_read, i, safe_length);
937 1.1.2.2 yamt first_read = SIZE_MAX;
938 1.1.2.2 yamt
939 1.1.2.2 yamt safe_length = UINT32_MAX;
940 1.1.2.2 yamt SLIST_FOREACH(jmp, &insn_dat[i].bj_jumps, bj_entries) {
941 1.1.2.2 yamt if (jmp->bj_safe_length < safe_length)
942 1.1.2.2 yamt safe_length = jmp->bj_safe_length;
943 1.1.2.2 yamt }
944 1.1.2.2 yamt }
945 1.1.2.2 yamt
946 1.1.2.2 yamt insn_dat[i].bj_unreachable = unreachable;
947 1.1.2.2 yamt if (unreachable)
948 1.1.2.2 yamt continue;
949 1.1.2.2 yamt
950 1.1.2.2 yamt if (read_pkt_insn(&insns[i], &length)) {
951 1.1.2.2 yamt if (first_read == SIZE_MAX)
952 1.1.2.2 yamt first_read = i;
953 1.1.2.2 yamt if (length > safe_length)
954 1.1.2.2 yamt safe_length = length;
955 1.1.2.2 yamt }
956 1.1.2.2 yamt
957 1.1.2.2 yamt switch (BPF_CLASS(insns[i].code)) {
958 1.1.2.2 yamt case BPF_RET:
959 1.1.2.2 yamt unreachable = true;
960 1.1.2.2 yamt continue;
961 1.1.2.2 yamt
962 1.1.2.2 yamt case BPF_JMP:
963 1.1.2.2 yamt if (insns[i].code == (BPF_JMP|BPF_JA)) {
964 1.1.2.2 yamt jt = jf = insns[i].k;
965 1.1.2.2 yamt } else {
966 1.1.2.2 yamt jt = insns[i].jt;
967 1.1.2.2 yamt jf = insns[i].jf;
968 1.1.2.2 yamt }
969 1.1.2.2 yamt
970 1.1.2.2 yamt if (jt >= insn_count - (i + 1) ||
971 1.1.2.2 yamt jf >= insn_count - (i + 1)) {
972 1.1.2.2 yamt return -1;
973 1.1.2.2 yamt }
974 1.1.2.2 yamt
975 1.1.2.2 yamt if (jt > 0 && jf > 0)
976 1.1.2.2 yamt unreachable = true;
977 1.1.2.2 yamt
978 1.1.2.2 yamt jtf = insn_dat[i].bj_aux.bj_jdata.bj_jtf;
979 1.1.2.2 yamt
980 1.1.2.2 yamt jtf[0].bj_jump = NULL;
981 1.1.2.2 yamt jtf[0].bj_safe_length = safe_length;
982 1.1.2.2 yamt SLIST_INSERT_HEAD(&insn_dat[i + 1 + jt].bj_jumps,
983 1.1.2.2 yamt &jtf[0], bj_entries);
984 1.1.2.2 yamt
985 1.1.2.2 yamt if (jf != jt) {
986 1.1.2.2 yamt jtf[1].bj_jump = NULL;
987 1.1.2.2 yamt jtf[1].bj_safe_length = safe_length;
988 1.1.2.2 yamt SLIST_INSERT_HEAD(&insn_dat[i + 1 + jf].bj_jumps,
989 1.1.2.2 yamt &jtf[1], bj_entries);
990 1.1.2.2 yamt }
991 1.1.2.2 yamt
992 1.1.2.2 yamt continue;
993 1.1.2.2 yamt }
994 1.1.2.2 yamt }
995 1.1.2.2 yamt
996 1.1.2.2 yamt set_check_length(insns, insn_dat, first_read, insn_count, safe_length);
997 1.1.2.2 yamt
998 1.1.2.2 yamt return 0;
999 1.1.2.2 yamt }
1000 1.1.2.2 yamt
1001 1.1.2.2 yamt /*
1002 1.1.2.2 yamt * Count out-of-bounds and division by zero jumps.
1003 1.1.2.2 yamt *
1004 1.1.2.2 yamt * insn_dat should be initialized by optimize().
1005 1.1.2.2 yamt */
1006 1.1.2.2 yamt static size_t
1007 1.1.2.2 yamt get_ret0_size(struct bpf_insn *insns, struct bpfjit_insn_data *insn_dat,
1008 1.1.2.2 yamt size_t insn_count)
1009 1.1.2.2 yamt {
1010 1.1.2.2 yamt size_t rv = 0;
1011 1.1.2.2 yamt size_t i;
1012 1.1.2.2 yamt
1013 1.1.2.2 yamt for (i = 0; i < insn_count; i++) {
1014 1.1.2.2 yamt
1015 1.1.2.2 yamt if (read_pkt_insn(&insns[i], NULL)) {
1016 1.1.2.2 yamt if (insn_dat[i].bj_aux.bj_rdata.bj_check_length > 0)
1017 1.1.2.2 yamt rv++;
1018 1.1.2.2 yamt #ifdef _KERNEL
1019 1.1.2.2 yamt rv++;
1020 1.1.2.2 yamt #endif
1021 1.1.2.2 yamt }
1022 1.1.2.2 yamt
1023 1.1.2.2 yamt if (insns[i].code == (BPF_LD|BPF_IND|BPF_B) ||
1024 1.1.2.2 yamt insns[i].code == (BPF_LD|BPF_IND|BPF_H) ||
1025 1.1.2.2 yamt insns[i].code == (BPF_LD|BPF_IND|BPF_W)) {
1026 1.1.2.2 yamt rv++;
1027 1.1.2.2 yamt }
1028 1.1.2.2 yamt
1029 1.1.2.2 yamt if (insns[i].code == (BPF_ALU|BPF_DIV|BPF_X))
1030 1.1.2.2 yamt rv++;
1031 1.1.2.2 yamt
1032 1.1.2.2 yamt if (insns[i].code == (BPF_ALU|BPF_DIV|BPF_K) &&
1033 1.1.2.2 yamt insns[i].k == 0) {
1034 1.1.2.2 yamt rv++;
1035 1.1.2.2 yamt }
1036 1.1.2.2 yamt }
1037 1.1.2.2 yamt
1038 1.1.2.2 yamt return rv;
1039 1.1.2.2 yamt }
1040 1.1.2.2 yamt
1041 1.1.2.2 yamt /*
1042 1.1.2.2 yamt * Convert BPF_ALU operations except BPF_NEG and BPF_DIV to sljit operation.
1043 1.1.2.2 yamt */
1044 1.1.2.2 yamt static int
1045 1.1.2.2 yamt bpf_alu_to_sljit_op(struct bpf_insn *pc)
1046 1.1.2.2 yamt {
1047 1.1.2.2 yamt
1048 1.1.2.2 yamt /*
1049 1.1.2.2 yamt * Note: all supported 64bit arches have 32bit multiply
1050 1.1.2.2 yamt * instruction so SLJIT_INT_OP doesn't have any overhead.
1051 1.1.2.2 yamt */
1052 1.1.2.2 yamt switch (BPF_OP(pc->code)) {
1053 1.1.2.2 yamt case BPF_ADD: return SLJIT_ADD;
1054 1.1.2.2 yamt case BPF_SUB: return SLJIT_SUB;
1055 1.1.2.2 yamt case BPF_MUL: return SLJIT_MUL|SLJIT_INT_OP;
1056 1.1.2.2 yamt case BPF_OR: return SLJIT_OR;
1057 1.1.2.2 yamt case BPF_AND: return SLJIT_AND;
1058 1.1.2.2 yamt case BPF_LSH: return SLJIT_SHL;
1059 1.1.2.2 yamt case BPF_RSH: return SLJIT_LSHR|SLJIT_INT_OP;
1060 1.1.2.2 yamt default:
1061 1.1.2.2 yamt BPFJIT_ASSERT(false);
1062 1.1.2.2 yamt return 0;
1063 1.1.2.2 yamt }
1064 1.1.2.2 yamt }
1065 1.1.2.2 yamt
1066 1.1.2.2 yamt /*
1067 1.1.2.2 yamt * Convert BPF_JMP operations except BPF_JA to sljit condition.
1068 1.1.2.2 yamt */
1069 1.1.2.2 yamt static int
1070 1.1.2.2 yamt bpf_jmp_to_sljit_cond(struct bpf_insn *pc, bool negate)
1071 1.1.2.2 yamt {
1072 1.1.2.2 yamt /*
1073 1.1.2.2 yamt * Note: all supported 64bit arches have 32bit comparison
1074 1.1.2.2 yamt * instructions so SLJIT_INT_OP doesn't have any overhead.
1075 1.1.2.2 yamt */
1076 1.1.2.2 yamt int rv = SLJIT_INT_OP;
1077 1.1.2.2 yamt
1078 1.1.2.2 yamt switch (BPF_OP(pc->code)) {
1079 1.1.2.2 yamt case BPF_JGT:
1080 1.1.2.2 yamt rv |= negate ? SLJIT_C_LESS_EQUAL : SLJIT_C_GREATER;
1081 1.1.2.2 yamt break;
1082 1.1.2.2 yamt case BPF_JGE:
1083 1.1.2.2 yamt rv |= negate ? SLJIT_C_LESS : SLJIT_C_GREATER_EQUAL;
1084 1.1.2.2 yamt break;
1085 1.1.2.2 yamt case BPF_JEQ:
1086 1.1.2.2 yamt rv |= negate ? SLJIT_C_NOT_EQUAL : SLJIT_C_EQUAL;
1087 1.1.2.2 yamt break;
1088 1.1.2.2 yamt case BPF_JSET:
1089 1.1.2.2 yamt rv |= negate ? SLJIT_C_EQUAL : SLJIT_C_NOT_EQUAL;
1090 1.1.2.2 yamt break;
1091 1.1.2.2 yamt default:
1092 1.1.2.2 yamt BPFJIT_ASSERT(false);
1093 1.1.2.2 yamt }
1094 1.1.2.2 yamt
1095 1.1.2.2 yamt return rv;
1096 1.1.2.2 yamt }
1097 1.1.2.2 yamt
1098 1.1.2.2 yamt static unsigned int
1099 1.1.2.2 yamt bpfjit_optimization_hints(struct bpf_insn *insns, size_t insn_count)
1100 1.1.2.2 yamt {
1101 1.1.2.2 yamt unsigned int rv = BPFJIT_INIT_A;
1102 1.1.2.2 yamt struct bpf_insn *pc;
1103 1.1.2.3 yamt unsigned int minm, maxm;
1104 1.1.2.2 yamt
1105 1.1.2.2 yamt BPFJIT_ASSERT(BPF_MEMWORDS - 1 <= 0xff);
1106 1.1.2.2 yamt
1107 1.1.2.2 yamt maxm = 0;
1108 1.1.2.2 yamt minm = BPF_MEMWORDS - 1;
1109 1.1.2.2 yamt
1110 1.1.2.2 yamt for (pc = insns; pc != insns + insn_count; pc++) {
1111 1.1.2.2 yamt switch (BPF_CLASS(pc->code)) {
1112 1.1.2.2 yamt case BPF_LD:
1113 1.1.2.2 yamt if (BPF_MODE(pc->code) == BPF_IND)
1114 1.1.2.2 yamt rv |= BPFJIT_INIT_X;
1115 1.1.2.2 yamt if (BPF_MODE(pc->code) == BPF_MEM &&
1116 1.1.2.2 yamt (uint32_t)pc->k < BPF_MEMWORDS) {
1117 1.1.2.2 yamt if (pc->k > maxm)
1118 1.1.2.2 yamt maxm = pc->k;
1119 1.1.2.2 yamt if (pc->k < minm)
1120 1.1.2.2 yamt minm = pc->k;
1121 1.1.2.2 yamt }
1122 1.1.2.2 yamt continue;
1123 1.1.2.2 yamt case BPF_LDX:
1124 1.1.2.2 yamt rv |= BPFJIT_INIT_X;
1125 1.1.2.2 yamt if (BPF_MODE(pc->code) == BPF_MEM &&
1126 1.1.2.2 yamt (uint32_t)pc->k < BPF_MEMWORDS) {
1127 1.1.2.2 yamt if (pc->k > maxm)
1128 1.1.2.2 yamt maxm = pc->k;
1129 1.1.2.2 yamt if (pc->k < minm)
1130 1.1.2.2 yamt minm = pc->k;
1131 1.1.2.2 yamt }
1132 1.1.2.2 yamt continue;
1133 1.1.2.2 yamt case BPF_ST:
1134 1.1.2.2 yamt if ((uint32_t)pc->k < BPF_MEMWORDS) {
1135 1.1.2.2 yamt if (pc->k > maxm)
1136 1.1.2.2 yamt maxm = pc->k;
1137 1.1.2.2 yamt if (pc->k < minm)
1138 1.1.2.2 yamt minm = pc->k;
1139 1.1.2.2 yamt }
1140 1.1.2.2 yamt continue;
1141 1.1.2.2 yamt case BPF_STX:
1142 1.1.2.2 yamt rv |= BPFJIT_INIT_X;
1143 1.1.2.2 yamt if ((uint32_t)pc->k < BPF_MEMWORDS) {
1144 1.1.2.2 yamt if (pc->k > maxm)
1145 1.1.2.2 yamt maxm = pc->k;
1146 1.1.2.2 yamt if (pc->k < minm)
1147 1.1.2.2 yamt minm = pc->k;
1148 1.1.2.2 yamt }
1149 1.1.2.2 yamt continue;
1150 1.1.2.2 yamt case BPF_ALU:
1151 1.1.2.2 yamt if (pc->code == (BPF_ALU|BPF_NEG))
1152 1.1.2.2 yamt continue;
1153 1.1.2.2 yamt if (BPF_SRC(pc->code) == BPF_X)
1154 1.1.2.2 yamt rv |= BPFJIT_INIT_X;
1155 1.1.2.2 yamt continue;
1156 1.1.2.2 yamt case BPF_JMP:
1157 1.1.2.2 yamt if (pc->code == (BPF_JMP|BPF_JA))
1158 1.1.2.2 yamt continue;
1159 1.1.2.2 yamt if (BPF_SRC(pc->code) == BPF_X)
1160 1.1.2.2 yamt rv |= BPFJIT_INIT_X;
1161 1.1.2.2 yamt continue;
1162 1.1.2.2 yamt case BPF_RET:
1163 1.1.2.2 yamt continue;
1164 1.1.2.2 yamt case BPF_MISC:
1165 1.1.2.2 yamt rv |= BPFJIT_INIT_X;
1166 1.1.2.2 yamt continue;
1167 1.1.2.2 yamt default:
1168 1.1.2.2 yamt BPFJIT_ASSERT(false);
1169 1.1.2.2 yamt }
1170 1.1.2.2 yamt }
1171 1.1.2.2 yamt
1172 1.1.2.2 yamt return rv | (maxm << 8) | minm;
1173 1.1.2.2 yamt }
1174 1.1.2.2 yamt
1175 1.1.2.2 yamt /*
1176 1.1.2.2 yamt * Convert BPF_K and BPF_X to sljit register.
1177 1.1.2.2 yamt */
1178 1.1.2.2 yamt static int
1179 1.1.2.2 yamt kx_to_reg(struct bpf_insn *pc)
1180 1.1.2.2 yamt {
1181 1.1.2.2 yamt
1182 1.1.2.2 yamt switch (BPF_SRC(pc->code)) {
1183 1.1.2.2 yamt case BPF_K: return SLJIT_IMM;
1184 1.1.2.2 yamt case BPF_X: return BPFJIT_X;
1185 1.1.2.2 yamt default:
1186 1.1.2.2 yamt BPFJIT_ASSERT(false);
1187 1.1.2.2 yamt return 0;
1188 1.1.2.2 yamt }
1189 1.1.2.2 yamt }
1190 1.1.2.2 yamt
1191 1.1.2.2 yamt static sljit_w
1192 1.1.2.2 yamt kx_to_reg_arg(struct bpf_insn *pc)
1193 1.1.2.2 yamt {
1194 1.1.2.2 yamt
1195 1.1.2.2 yamt switch (BPF_SRC(pc->code)) {
1196 1.1.2.2 yamt case BPF_K: return (uint32_t)pc->k; /* SLJIT_IMM, pc->k, */
1197 1.1.2.2 yamt case BPF_X: return 0; /* BPFJIT_X, 0, */
1198 1.1.2.2 yamt default:
1199 1.1.2.2 yamt BPFJIT_ASSERT(false);
1200 1.1.2.2 yamt return 0;
1201 1.1.2.2 yamt }
1202 1.1.2.2 yamt }
1203 1.1.2.2 yamt
1204 1.1.2.2 yamt bpfjit_function_t
1205 1.1.2.2 yamt bpfjit_generate_code(struct bpf_insn *insns, size_t insn_count)
1206 1.1.2.2 yamt {
1207 1.1.2.2 yamt void *rv;
1208 1.1.2.2 yamt size_t i;
1209 1.1.2.2 yamt int status;
1210 1.1.2.2 yamt int branching, negate;
1211 1.1.2.2 yamt unsigned int rval, mode, src;
1212 1.1.2.2 yamt int ntmp;
1213 1.1.2.3 yamt unsigned int locals_size;
1214 1.1.2.3 yamt unsigned int minm, maxm; /* min/max k for M[k] */
1215 1.1.2.3 yamt size_t mem_locals_start; /* start of M[] array */
1216 1.1.2.2 yamt unsigned int opts;
1217 1.1.2.2 yamt struct bpf_insn *pc;
1218 1.1.2.2 yamt struct sljit_compiler* compiler;
1219 1.1.2.2 yamt
1220 1.1.2.2 yamt /* a list of jumps to a normal return from a generated function */
1221 1.1.2.2 yamt struct sljit_jump **returns;
1222 1.1.2.2 yamt size_t returns_size, returns_maxsize;
1223 1.1.2.2 yamt
1224 1.1.2.2 yamt /* a list of jumps to out-of-bound return from a generated function */
1225 1.1.2.2 yamt struct sljit_jump **ret0;
1226 1.1.2.2 yamt size_t ret0_size, ret0_maxsize;
1227 1.1.2.2 yamt
1228 1.1.2.2 yamt struct bpfjit_insn_data *insn_dat;
1229 1.1.2.2 yamt
1230 1.1.2.2 yamt /* for local use */
1231 1.1.2.2 yamt struct sljit_label *label;
1232 1.1.2.2 yamt struct sljit_jump *jump;
1233 1.1.2.2 yamt struct bpfjit_jump *bjump, *jtf;
1234 1.1.2.2 yamt
1235 1.1.2.2 yamt struct sljit_jump *to_mchain_jump;
1236 1.1.2.2 yamt
1237 1.1.2.2 yamt uint32_t jt, jf;
1238 1.1.2.2 yamt
1239 1.1.2.2 yamt rv = NULL;
1240 1.1.2.2 yamt compiler = NULL;
1241 1.1.2.2 yamt insn_dat = NULL;
1242 1.1.2.2 yamt returns = NULL;
1243 1.1.2.2 yamt ret0 = NULL;
1244 1.1.2.2 yamt
1245 1.1.2.2 yamt opts = bpfjit_optimization_hints(insns, insn_count);
1246 1.1.2.2 yamt minm = opts & 0xff;
1247 1.1.2.2 yamt maxm = (opts >> 8) & 0xff;
1248 1.1.2.2 yamt mem_locals_start = mem_local_offset(0, 0);
1249 1.1.2.2 yamt locals_size = (minm <= maxm) ?
1250 1.1.2.2 yamt mem_local_offset(maxm + 1, minm) : mem_locals_start;
1251 1.1.2.2 yamt
1252 1.1.2.2 yamt ntmp = 4;
1253 1.1.2.2 yamt #ifdef _KERNEL
1254 1.1.2.2 yamt ntmp += 1; /* for BPFJIT_KERN_TMP */
1255 1.1.2.2 yamt #endif
1256 1.1.2.2 yamt
1257 1.1.2.2 yamt returns_maxsize = count_returns(insns, insn_count);
1258 1.1.2.2 yamt if (returns_maxsize == 0)
1259 1.1.2.2 yamt goto fail;
1260 1.1.2.2 yamt
1261 1.1.2.2 yamt insn_dat = BPFJIT_MALLOC(insn_count * sizeof(insn_dat[0]));
1262 1.1.2.2 yamt if (insn_dat == NULL)
1263 1.1.2.2 yamt goto fail;
1264 1.1.2.2 yamt
1265 1.1.2.2 yamt if (optimize(insns, insn_dat, insn_count) < 0)
1266 1.1.2.2 yamt goto fail;
1267 1.1.2.2 yamt
1268 1.1.2.2 yamt ret0_size = 0;
1269 1.1.2.2 yamt ret0_maxsize = get_ret0_size(insns, insn_dat, insn_count);
1270 1.1.2.2 yamt if (ret0_maxsize > 0) {
1271 1.1.2.2 yamt ret0 = BPFJIT_MALLOC(ret0_maxsize * sizeof(ret0[0]));
1272 1.1.2.2 yamt if (ret0 == NULL)
1273 1.1.2.2 yamt goto fail;
1274 1.1.2.2 yamt }
1275 1.1.2.2 yamt
1276 1.1.2.2 yamt returns_size = 0;
1277 1.1.2.2 yamt returns = BPFJIT_MALLOC(returns_maxsize * sizeof(returns[0]));
1278 1.1.2.2 yamt if (returns == NULL)
1279 1.1.2.2 yamt goto fail;
1280 1.1.2.2 yamt
1281 1.1.2.2 yamt compiler = sljit_create_compiler();
1282 1.1.2.2 yamt if (compiler == NULL)
1283 1.1.2.2 yamt goto fail;
1284 1.1.2.2 yamt
1285 1.1.2.2 yamt #if !defined(_KERNEL) && defined(SLJIT_VERBOSE) && SLJIT_VERBOSE
1286 1.1.2.2 yamt sljit_compiler_verbose(compiler, stderr);
1287 1.1.2.2 yamt #endif
1288 1.1.2.2 yamt
1289 1.1.2.2 yamt status = sljit_emit_enter(compiler, 3, ntmp, 3, locals_size);
1290 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1291 1.1.2.2 yamt goto fail;
1292 1.1.2.2 yamt
1293 1.1.2.2 yamt for (i = mem_locals_start; i < locals_size; i+= sizeof(uint32_t)) {
1294 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1295 1.1.2.2 yamt SLJIT_MOV_UI,
1296 1.1.2.2 yamt SLJIT_MEM1(SLJIT_LOCALS_REG), i,
1297 1.1.2.2 yamt SLJIT_IMM, 0);
1298 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1299 1.1.2.2 yamt goto fail;
1300 1.1.2.2 yamt }
1301 1.1.2.2 yamt
1302 1.1.2.2 yamt if (opts & BPFJIT_INIT_A) {
1303 1.1.2.2 yamt /* A = 0; */
1304 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1305 1.1.2.2 yamt SLJIT_MOV,
1306 1.1.2.2 yamt BPFJIT_A, 0,
1307 1.1.2.2 yamt SLJIT_IMM, 0);
1308 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1309 1.1.2.2 yamt goto fail;
1310 1.1.2.2 yamt }
1311 1.1.2.2 yamt
1312 1.1.2.2 yamt if (opts & BPFJIT_INIT_X) {
1313 1.1.2.2 yamt /* X = 0; */
1314 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1315 1.1.2.2 yamt SLJIT_MOV,
1316 1.1.2.2 yamt BPFJIT_X, 0,
1317 1.1.2.2 yamt SLJIT_IMM, 0);
1318 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1319 1.1.2.2 yamt goto fail;
1320 1.1.2.2 yamt }
1321 1.1.2.2 yamt
1322 1.1.2.2 yamt for (i = 0; i < insn_count; i++) {
1323 1.1.2.2 yamt if (insn_dat[i].bj_unreachable)
1324 1.1.2.2 yamt continue;
1325 1.1.2.2 yamt
1326 1.1.2.2 yamt to_mchain_jump = NULL;
1327 1.1.2.2 yamt
1328 1.1.2.2 yamt /*
1329 1.1.2.2 yamt * Resolve jumps to the current insn.
1330 1.1.2.2 yamt */
1331 1.1.2.2 yamt label = NULL;
1332 1.1.2.2 yamt SLIST_FOREACH(bjump, &insn_dat[i].bj_jumps, bj_entries) {
1333 1.1.2.2 yamt if (bjump->bj_jump != NULL) {
1334 1.1.2.2 yamt if (label == NULL)
1335 1.1.2.2 yamt label = sljit_emit_label(compiler);
1336 1.1.2.2 yamt if (label == NULL)
1337 1.1.2.2 yamt goto fail;
1338 1.1.2.2 yamt sljit_set_label(bjump->bj_jump, label);
1339 1.1.2.2 yamt }
1340 1.1.2.2 yamt }
1341 1.1.2.2 yamt
1342 1.1.2.2 yamt if (read_pkt_insn(&insns[i], NULL) &&
1343 1.1.2.2 yamt insn_dat[i].bj_aux.bj_rdata.bj_check_length > 0) {
1344 1.1.2.2 yamt /* if (buflen < bj_check_length) return 0; */
1345 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
1346 1.1.2.2 yamt SLJIT_C_LESS,
1347 1.1.2.2 yamt BPFJIT_BUFLEN, 0,
1348 1.1.2.2 yamt SLJIT_IMM,
1349 1.1.2.2 yamt insn_dat[i].bj_aux.bj_rdata.bj_check_length);
1350 1.1.2.2 yamt if (jump == NULL)
1351 1.1.2.2 yamt goto fail;
1352 1.1.2.2 yamt #ifdef _KERNEL
1353 1.1.2.2 yamt to_mchain_jump = jump;
1354 1.1.2.2 yamt #else
1355 1.1.2.2 yamt ret0[ret0_size++] = jump;
1356 1.1.2.2 yamt #endif
1357 1.1.2.2 yamt }
1358 1.1.2.2 yamt
1359 1.1.2.2 yamt pc = &insns[i];
1360 1.1.2.2 yamt switch (BPF_CLASS(pc->code)) {
1361 1.1.2.2 yamt
1362 1.1.2.2 yamt default:
1363 1.1.2.2 yamt goto fail;
1364 1.1.2.2 yamt
1365 1.1.2.2 yamt case BPF_LD:
1366 1.1.2.2 yamt /* BPF_LD+BPF_IMM A <- k */
1367 1.1.2.2 yamt if (pc->code == (BPF_LD|BPF_IMM)) {
1368 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1369 1.1.2.2 yamt SLJIT_MOV,
1370 1.1.2.2 yamt BPFJIT_A, 0,
1371 1.1.2.2 yamt SLJIT_IMM, (uint32_t)pc->k);
1372 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1373 1.1.2.2 yamt goto fail;
1374 1.1.2.2 yamt
1375 1.1.2.2 yamt continue;
1376 1.1.2.2 yamt }
1377 1.1.2.2 yamt
1378 1.1.2.2 yamt /* BPF_LD+BPF_MEM A <- M[k] */
1379 1.1.2.2 yamt if (pc->code == (BPF_LD|BPF_MEM)) {
1380 1.1.2.2 yamt if (pc->k < minm || pc->k > maxm)
1381 1.1.2.2 yamt goto fail;
1382 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1383 1.1.2.2 yamt SLJIT_MOV_UI,
1384 1.1.2.2 yamt BPFJIT_A, 0,
1385 1.1.2.2 yamt SLJIT_MEM1(SLJIT_LOCALS_REG),
1386 1.1.2.2 yamt mem_local_offset(pc->k, minm));
1387 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1388 1.1.2.2 yamt goto fail;
1389 1.1.2.2 yamt
1390 1.1.2.2 yamt continue;
1391 1.1.2.2 yamt }
1392 1.1.2.2 yamt
1393 1.1.2.2 yamt /* BPF_LD+BPF_W+BPF_LEN A <- len */
1394 1.1.2.2 yamt if (pc->code == (BPF_LD|BPF_W|BPF_LEN)) {
1395 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1396 1.1.2.2 yamt SLJIT_MOV,
1397 1.1.2.2 yamt BPFJIT_A, 0,
1398 1.1.2.2 yamt BPFJIT_WIRELEN, 0);
1399 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1400 1.1.2.2 yamt goto fail;
1401 1.1.2.2 yamt
1402 1.1.2.2 yamt continue;
1403 1.1.2.2 yamt }
1404 1.1.2.2 yamt
1405 1.1.2.2 yamt mode = BPF_MODE(pc->code);
1406 1.1.2.2 yamt if (mode != BPF_ABS && mode != BPF_IND)
1407 1.1.2.2 yamt goto fail;
1408 1.1.2.2 yamt
1409 1.1.2.2 yamt status = emit_pkt_read(compiler, pc,
1410 1.1.2.2 yamt to_mchain_jump, ret0, &ret0_size);
1411 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1412 1.1.2.2 yamt goto fail;
1413 1.1.2.2 yamt
1414 1.1.2.2 yamt continue;
1415 1.1.2.2 yamt
1416 1.1.2.2 yamt case BPF_LDX:
1417 1.1.2.2 yamt mode = BPF_MODE(pc->code);
1418 1.1.2.2 yamt
1419 1.1.2.2 yamt /* BPF_LDX+BPF_W+BPF_IMM X <- k */
1420 1.1.2.2 yamt if (mode == BPF_IMM) {
1421 1.1.2.2 yamt if (BPF_SIZE(pc->code) != BPF_W)
1422 1.1.2.2 yamt goto fail;
1423 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1424 1.1.2.2 yamt SLJIT_MOV,
1425 1.1.2.2 yamt BPFJIT_X, 0,
1426 1.1.2.2 yamt SLJIT_IMM, (uint32_t)pc->k);
1427 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1428 1.1.2.2 yamt goto fail;
1429 1.1.2.2 yamt
1430 1.1.2.2 yamt continue;
1431 1.1.2.2 yamt }
1432 1.1.2.2 yamt
1433 1.1.2.2 yamt /* BPF_LDX+BPF_W+BPF_LEN X <- len */
1434 1.1.2.2 yamt if (mode == BPF_LEN) {
1435 1.1.2.2 yamt if (BPF_SIZE(pc->code) != BPF_W)
1436 1.1.2.2 yamt goto fail;
1437 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1438 1.1.2.2 yamt SLJIT_MOV,
1439 1.1.2.2 yamt BPFJIT_X, 0,
1440 1.1.2.2 yamt BPFJIT_WIRELEN, 0);
1441 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1442 1.1.2.2 yamt goto fail;
1443 1.1.2.2 yamt
1444 1.1.2.2 yamt continue;
1445 1.1.2.2 yamt }
1446 1.1.2.2 yamt
1447 1.1.2.2 yamt /* BPF_LDX+BPF_W+BPF_MEM X <- M[k] */
1448 1.1.2.2 yamt if (mode == BPF_MEM) {
1449 1.1.2.2 yamt if (BPF_SIZE(pc->code) != BPF_W)
1450 1.1.2.2 yamt goto fail;
1451 1.1.2.2 yamt if (pc->k < minm || pc->k > maxm)
1452 1.1.2.2 yamt goto fail;
1453 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1454 1.1.2.2 yamt SLJIT_MOV_UI,
1455 1.1.2.2 yamt BPFJIT_X, 0,
1456 1.1.2.2 yamt SLJIT_MEM1(SLJIT_LOCALS_REG),
1457 1.1.2.2 yamt mem_local_offset(pc->k, minm));
1458 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1459 1.1.2.2 yamt goto fail;
1460 1.1.2.2 yamt
1461 1.1.2.2 yamt continue;
1462 1.1.2.2 yamt }
1463 1.1.2.2 yamt
1464 1.1.2.2 yamt /* BPF_LDX+BPF_B+BPF_MSH X <- 4*(P[k:1]&0xf) */
1465 1.1.2.2 yamt if (mode != BPF_MSH || BPF_SIZE(pc->code) != BPF_B)
1466 1.1.2.2 yamt goto fail;
1467 1.1.2.2 yamt
1468 1.1.2.2 yamt status = emit_msh(compiler, pc,
1469 1.1.2.2 yamt to_mchain_jump, ret0, &ret0_size);
1470 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1471 1.1.2.2 yamt goto fail;
1472 1.1.2.2 yamt
1473 1.1.2.2 yamt continue;
1474 1.1.2.2 yamt
1475 1.1.2.2 yamt case BPF_ST:
1476 1.1.2.2 yamt if (pc->code != BPF_ST || pc->k < minm || pc->k > maxm)
1477 1.1.2.2 yamt goto fail;
1478 1.1.2.2 yamt
1479 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1480 1.1.2.2 yamt SLJIT_MOV_UI,
1481 1.1.2.2 yamt SLJIT_MEM1(SLJIT_LOCALS_REG),
1482 1.1.2.2 yamt mem_local_offset(pc->k, minm),
1483 1.1.2.2 yamt BPFJIT_A, 0);
1484 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1485 1.1.2.2 yamt goto fail;
1486 1.1.2.2 yamt
1487 1.1.2.2 yamt continue;
1488 1.1.2.2 yamt
1489 1.1.2.2 yamt case BPF_STX:
1490 1.1.2.2 yamt if (pc->code != BPF_STX || pc->k < minm || pc->k > maxm)
1491 1.1.2.2 yamt goto fail;
1492 1.1.2.2 yamt
1493 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1494 1.1.2.2 yamt SLJIT_MOV_UI,
1495 1.1.2.2 yamt SLJIT_MEM1(SLJIT_LOCALS_REG),
1496 1.1.2.2 yamt mem_local_offset(pc->k, minm),
1497 1.1.2.2 yamt BPFJIT_X, 0);
1498 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1499 1.1.2.2 yamt goto fail;
1500 1.1.2.2 yamt
1501 1.1.2.2 yamt continue;
1502 1.1.2.2 yamt
1503 1.1.2.2 yamt case BPF_ALU:
1504 1.1.2.2 yamt
1505 1.1.2.2 yamt if (pc->code == (BPF_ALU|BPF_NEG)) {
1506 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1507 1.1.2.2 yamt SLJIT_NEG,
1508 1.1.2.2 yamt BPFJIT_A, 0,
1509 1.1.2.2 yamt BPFJIT_A, 0);
1510 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1511 1.1.2.2 yamt goto fail;
1512 1.1.2.2 yamt
1513 1.1.2.2 yamt continue;
1514 1.1.2.2 yamt }
1515 1.1.2.2 yamt
1516 1.1.2.2 yamt if (BPF_OP(pc->code) != BPF_DIV) {
1517 1.1.2.2 yamt status = sljit_emit_op2(compiler,
1518 1.1.2.2 yamt bpf_alu_to_sljit_op(pc),
1519 1.1.2.2 yamt BPFJIT_A, 0,
1520 1.1.2.2 yamt BPFJIT_A, 0,
1521 1.1.2.2 yamt kx_to_reg(pc), kx_to_reg_arg(pc));
1522 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1523 1.1.2.2 yamt goto fail;
1524 1.1.2.2 yamt
1525 1.1.2.2 yamt continue;
1526 1.1.2.2 yamt }
1527 1.1.2.2 yamt
1528 1.1.2.2 yamt /* BPF_DIV */
1529 1.1.2.2 yamt
1530 1.1.2.2 yamt src = BPF_SRC(pc->code);
1531 1.1.2.2 yamt if (src != BPF_X && src != BPF_K)
1532 1.1.2.2 yamt goto fail;
1533 1.1.2.2 yamt
1534 1.1.2.2 yamt /* division by zero? */
1535 1.1.2.2 yamt if (src == BPF_X) {
1536 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
1537 1.1.2.2 yamt SLJIT_C_EQUAL|SLJIT_INT_OP,
1538 1.1.2.2 yamt BPFJIT_X, 0,
1539 1.1.2.2 yamt SLJIT_IMM, 0);
1540 1.1.2.2 yamt if (jump == NULL)
1541 1.1.2.2 yamt goto fail;
1542 1.1.2.2 yamt ret0[ret0_size++] = jump;
1543 1.1.2.2 yamt } else if (pc->k == 0) {
1544 1.1.2.2 yamt jump = sljit_emit_jump(compiler, SLJIT_JUMP);
1545 1.1.2.2 yamt if (jump == NULL)
1546 1.1.2.2 yamt goto fail;
1547 1.1.2.2 yamt ret0[ret0_size++] = jump;
1548 1.1.2.2 yamt }
1549 1.1.2.2 yamt
1550 1.1.2.2 yamt if (src == BPF_X) {
1551 1.1.2.2 yamt status = emit_division(compiler, BPFJIT_X, 0);
1552 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1553 1.1.2.2 yamt goto fail;
1554 1.1.2.2 yamt } else if (pc->k != 0) {
1555 1.1.2.2 yamt if (pc->k & (pc->k - 1)) {
1556 1.1.2.2 yamt status = emit_division(compiler,
1557 1.1.2.2 yamt SLJIT_IMM, (uint32_t)pc->k);
1558 1.1.2.2 yamt } else {
1559 1.1.2.2 yamt status = emit_pow2_division(compiler,
1560 1.1.2.2 yamt (uint32_t)pc->k);
1561 1.1.2.2 yamt }
1562 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1563 1.1.2.2 yamt goto fail;
1564 1.1.2.2 yamt }
1565 1.1.2.2 yamt
1566 1.1.2.2 yamt continue;
1567 1.1.2.2 yamt
1568 1.1.2.2 yamt case BPF_JMP:
1569 1.1.2.2 yamt
1570 1.1.2.2 yamt if (pc->code == (BPF_JMP|BPF_JA)) {
1571 1.1.2.2 yamt jt = jf = pc->k;
1572 1.1.2.2 yamt } else {
1573 1.1.2.2 yamt jt = pc->jt;
1574 1.1.2.2 yamt jf = pc->jf;
1575 1.1.2.2 yamt }
1576 1.1.2.2 yamt
1577 1.1.2.2 yamt negate = (jt == 0) ? 1 : 0;
1578 1.1.2.2 yamt branching = (jt == jf) ? 0 : 1;
1579 1.1.2.2 yamt jtf = insn_dat[i].bj_aux.bj_jdata.bj_jtf;
1580 1.1.2.2 yamt
1581 1.1.2.2 yamt if (branching) {
1582 1.1.2.2 yamt if (BPF_OP(pc->code) != BPF_JSET) {
1583 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
1584 1.1.2.2 yamt bpf_jmp_to_sljit_cond(pc, negate),
1585 1.1.2.2 yamt BPFJIT_A, 0,
1586 1.1.2.2 yamt kx_to_reg(pc), kx_to_reg_arg(pc));
1587 1.1.2.2 yamt } else {
1588 1.1.2.2 yamt status = sljit_emit_op2(compiler,
1589 1.1.2.2 yamt SLJIT_AND,
1590 1.1.2.2 yamt BPFJIT_TMP1, 0,
1591 1.1.2.2 yamt BPFJIT_A, 0,
1592 1.1.2.2 yamt kx_to_reg(pc), kx_to_reg_arg(pc));
1593 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1594 1.1.2.2 yamt goto fail;
1595 1.1.2.2 yamt
1596 1.1.2.2 yamt jump = sljit_emit_cmp(compiler,
1597 1.1.2.2 yamt bpf_jmp_to_sljit_cond(pc, negate),
1598 1.1.2.2 yamt BPFJIT_TMP1, 0,
1599 1.1.2.2 yamt SLJIT_IMM, 0);
1600 1.1.2.2 yamt }
1601 1.1.2.2 yamt
1602 1.1.2.2 yamt if (jump == NULL)
1603 1.1.2.2 yamt goto fail;
1604 1.1.2.2 yamt
1605 1.1.2.2 yamt BPFJIT_ASSERT(jtf[negate].bj_jump == NULL);
1606 1.1.2.2 yamt jtf[negate].bj_jump = jump;
1607 1.1.2.2 yamt }
1608 1.1.2.2 yamt
1609 1.1.2.2 yamt if (!branching || (jt != 0 && jf != 0)) {
1610 1.1.2.2 yamt jump = sljit_emit_jump(compiler, SLJIT_JUMP);
1611 1.1.2.2 yamt if (jump == NULL)
1612 1.1.2.2 yamt goto fail;
1613 1.1.2.2 yamt
1614 1.1.2.2 yamt BPFJIT_ASSERT(jtf[branching].bj_jump == NULL);
1615 1.1.2.2 yamt jtf[branching].bj_jump = jump;
1616 1.1.2.2 yamt }
1617 1.1.2.2 yamt
1618 1.1.2.2 yamt continue;
1619 1.1.2.2 yamt
1620 1.1.2.2 yamt case BPF_RET:
1621 1.1.2.2 yamt
1622 1.1.2.2 yamt rval = BPF_RVAL(pc->code);
1623 1.1.2.2 yamt if (rval == BPF_X)
1624 1.1.2.2 yamt goto fail;
1625 1.1.2.2 yamt
1626 1.1.2.2 yamt /* BPF_RET+BPF_K accept k bytes */
1627 1.1.2.2 yamt if (rval == BPF_K) {
1628 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1629 1.1.2.2 yamt SLJIT_MOV,
1630 1.1.2.2 yamt BPFJIT_A, 0,
1631 1.1.2.2 yamt SLJIT_IMM, (uint32_t)pc->k);
1632 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1633 1.1.2.2 yamt goto fail;
1634 1.1.2.2 yamt }
1635 1.1.2.2 yamt
1636 1.1.2.2 yamt /* BPF_RET+BPF_A accept A bytes */
1637 1.1.2.2 yamt if (rval == BPF_A) {
1638 1.1.2.2 yamt #if BPFJIT_A != SLJIT_RETURN_REG
1639 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1640 1.1.2.2 yamt SLJIT_MOV,
1641 1.1.2.2 yamt SLJIT_RETURN_REG, 0,
1642 1.1.2.2 yamt BPFJIT_A, 0);
1643 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1644 1.1.2.2 yamt goto fail;
1645 1.1.2.2 yamt #endif
1646 1.1.2.2 yamt }
1647 1.1.2.2 yamt
1648 1.1.2.2 yamt /*
1649 1.1.2.2 yamt * Save a jump to a normal return. If the program
1650 1.1.2.2 yamt * ends with BPF_RET, no jump is needed because
1651 1.1.2.2 yamt * the normal return is generated right after the
1652 1.1.2.2 yamt * last instruction.
1653 1.1.2.2 yamt */
1654 1.1.2.2 yamt if (i != insn_count - 1) {
1655 1.1.2.2 yamt jump = sljit_emit_jump(compiler, SLJIT_JUMP);
1656 1.1.2.2 yamt if (jump == NULL)
1657 1.1.2.2 yamt goto fail;
1658 1.1.2.2 yamt returns[returns_size++] = jump;
1659 1.1.2.2 yamt }
1660 1.1.2.2 yamt
1661 1.1.2.2 yamt continue;
1662 1.1.2.2 yamt
1663 1.1.2.2 yamt case BPF_MISC:
1664 1.1.2.2 yamt
1665 1.1.2.2 yamt if (pc->code == (BPF_MISC|BPF_TAX)) {
1666 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1667 1.1.2.2 yamt SLJIT_MOV_UI,
1668 1.1.2.2 yamt BPFJIT_X, 0,
1669 1.1.2.2 yamt BPFJIT_A, 0);
1670 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1671 1.1.2.2 yamt goto fail;
1672 1.1.2.2 yamt
1673 1.1.2.2 yamt continue;
1674 1.1.2.2 yamt }
1675 1.1.2.2 yamt
1676 1.1.2.2 yamt if (pc->code == (BPF_MISC|BPF_TXA)) {
1677 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1678 1.1.2.2 yamt SLJIT_MOV,
1679 1.1.2.2 yamt BPFJIT_A, 0,
1680 1.1.2.2 yamt BPFJIT_X, 0);
1681 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1682 1.1.2.2 yamt goto fail;
1683 1.1.2.2 yamt
1684 1.1.2.2 yamt continue;
1685 1.1.2.2 yamt }
1686 1.1.2.2 yamt
1687 1.1.2.2 yamt goto fail;
1688 1.1.2.2 yamt } /* switch */
1689 1.1.2.2 yamt } /* main loop */
1690 1.1.2.2 yamt
1691 1.1.2.2 yamt BPFJIT_ASSERT(ret0_size == ret0_maxsize);
1692 1.1.2.2 yamt BPFJIT_ASSERT(returns_size <= returns_maxsize);
1693 1.1.2.2 yamt
1694 1.1.2.2 yamt if (returns_size > 0) {
1695 1.1.2.2 yamt label = sljit_emit_label(compiler);
1696 1.1.2.2 yamt if (label == NULL)
1697 1.1.2.2 yamt goto fail;
1698 1.1.2.2 yamt for (i = 0; i < returns_size; i++)
1699 1.1.2.2 yamt sljit_set_label(returns[i], label);
1700 1.1.2.2 yamt }
1701 1.1.2.2 yamt
1702 1.1.2.2 yamt status = sljit_emit_return(compiler,
1703 1.1.2.2 yamt SLJIT_MOV_UI,
1704 1.1.2.2 yamt BPFJIT_A, 0);
1705 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1706 1.1.2.2 yamt goto fail;
1707 1.1.2.2 yamt
1708 1.1.2.2 yamt if (ret0_size > 0) {
1709 1.1.2.2 yamt label = sljit_emit_label(compiler);
1710 1.1.2.2 yamt if (label == NULL)
1711 1.1.2.2 yamt goto fail;
1712 1.1.2.2 yamt
1713 1.1.2.2 yamt for (i = 0; i < ret0_size; i++)
1714 1.1.2.2 yamt sljit_set_label(ret0[i], label);
1715 1.1.2.2 yamt
1716 1.1.2.2 yamt status = sljit_emit_op1(compiler,
1717 1.1.2.2 yamt SLJIT_MOV,
1718 1.1.2.2 yamt SLJIT_RETURN_REG, 0,
1719 1.1.2.2 yamt SLJIT_IMM, 0);
1720 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1721 1.1.2.2 yamt goto fail;
1722 1.1.2.2 yamt
1723 1.1.2.2 yamt status = sljit_emit_return(compiler,
1724 1.1.2.2 yamt SLJIT_MOV_UI,
1725 1.1.2.2 yamt SLJIT_RETURN_REG, 0);
1726 1.1.2.2 yamt if (status != SLJIT_SUCCESS)
1727 1.1.2.2 yamt goto fail;
1728 1.1.2.2 yamt }
1729 1.1.2.2 yamt
1730 1.1.2.2 yamt rv = sljit_generate_code(compiler);
1731 1.1.2.2 yamt
1732 1.1.2.2 yamt fail:
1733 1.1.2.2 yamt if (compiler != NULL)
1734 1.1.2.2 yamt sljit_free_compiler(compiler);
1735 1.1.2.2 yamt
1736 1.1.2.2 yamt if (insn_dat != NULL)
1737 1.1.2.2 yamt BPFJIT_FREE(insn_dat);
1738 1.1.2.2 yamt
1739 1.1.2.2 yamt if (returns != NULL)
1740 1.1.2.2 yamt BPFJIT_FREE(returns);
1741 1.1.2.2 yamt
1742 1.1.2.2 yamt if (ret0 != NULL)
1743 1.1.2.2 yamt BPFJIT_FREE(ret0);
1744 1.1.2.2 yamt
1745 1.1.2.2 yamt return (bpfjit_function_t)rv;
1746 1.1.2.2 yamt }
1747 1.1.2.2 yamt
1748 1.1.2.2 yamt void
1749 1.1.2.2 yamt bpfjit_free_code(bpfjit_function_t code)
1750 1.1.2.2 yamt {
1751 1.1.2.2 yamt
1752 1.1.2.2 yamt sljit_free_code((void *)code);
1753 1.1.2.2 yamt }
1754