2 * Copyright (c) 2002 - 2003 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 Jung-uk Kim <jkim@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS intERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/malloc.h>
46 #include <net/bpf_jitter.h>
48 #include <amd64/amd64/bpf_jit_machdep.h>
50 bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, int *);
53 * emit routine to update the jump table
56 emit_length(bpf_bin_stream *stream, u_int value, u_int len)
59 (stream->refs)[stream->bpf_pc] += len;
60 stream->cur_ip += len;
64 * emit routine to output the actual binary code
67 emit_code(bpf_bin_stream *stream, u_int value, u_int len)
72 stream->ibuf[stream->cur_ip] = (u_char)value;
77 *((u_short *)(stream->ibuf + stream->cur_ip)) = (u_short)value;
82 *((u_int *)(stream->ibuf + stream->cur_ip)) = value;
91 * Function that does the real stuff
94 bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
98 bpf_bin_stream stream;
101 * NOTE: do not modify the name of this variable, as it's used by
102 * the macros to emit code.
106 /* Do not compile an empty filter. */
110 /* Allocate the reference table for the jumps */
111 stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int),
113 if (stream.refs == NULL)
116 /* Reset the reference table */
117 for (i = 0; i < nins + 1; i++)
124 * the first pass will emit the lengths of the instructions
125 * to create the reference table
133 /* create the procedure header */
136 MOVoqd(RBP, -8, ESI);
137 MOVoqd(RBP, -12, EDX);
141 for (i = 0; i < nins; i++) {
159 case BPF_LD|BPF_W|BPF_ABS:
162 ADDib(ECX, sizeof(int));
163 CMPodd(ECX, RBP, -12);
168 MOVobd(EAX, RBX, RSI);
172 case BPF_LD|BPF_H|BPF_ABS:
176 ADDib(ECX, sizeof(short));
177 CMPodd(ECX, RBP, -12);
181 MOVobw(AX, RBX, RSI);
185 case BPF_LD|BPF_B|BPF_ABS:
188 CMPodd(ECX, RBP, -12);
192 MOVobb(AL, RBX, RCX);
195 case BPF_LD|BPF_W|BPF_LEN:
196 MOVodd(EAX, RBP, -8);
199 case BPF_LDX|BPF_W|BPF_LEN:
200 MOVodd(EDX, RBP, -8);
203 case BPF_LD|BPF_W|BPF_IND:
207 ADDib(ECX, sizeof(int));
208 CMPodd(ECX, RBP, -12);
213 MOVobd(EAX, RBX, RSI);
217 case BPF_LD|BPF_H|BPF_IND:
222 ADDib(ECX, sizeof(short));
223 CMPodd(ECX, RBP, -12);
227 MOVobw(AX, RBX, RSI);
231 case BPF_LD|BPF_B|BPF_IND:
235 CMPodd(ECX, RBP, -12);
239 MOVobb(AL, RBX, RCX);
242 case BPF_LDX|BPF_MSH|BPF_B:
244 CMPodd(ECX, RBP, -12);
250 MOVobb(DL, RBX, RCX);
259 case BPF_LDX|BPF_IMM:
264 MOViq(RCX, (uintptr_t)mem);
265 MOVid(ESI, ins->k * 4);
266 MOVobd(EAX, RCX, RSI);
269 case BPF_LDX|BPF_MEM:
270 MOViq(RCX, (uintptr_t)mem);
271 MOVid(ESI, ins->k * 4);
272 MOVobd(EDX, RCX, RSI);
277 * XXX this command and the following could
278 * be optimized if the previous instruction
279 * was already of this type
281 MOViq(RCX, (uintptr_t)mem);
282 MOVid(ESI, ins->k * 4);
283 MOVomd(RCX, RSI, EAX);
287 MOViq(RCX, (uintptr_t)mem);
288 MOVid(ESI, ins->k * 4);
289 MOVomd(RCX, RSI, EDX);
293 JMP(stream.refs[stream.bpf_pc + ins->k] -
294 stream.refs[stream.bpf_pc]);
297 case BPF_JMP|BPF_JGT|BPF_K:
299 /* 5 is the size of the following JMP */
300 JG(stream.refs[stream.bpf_pc + ins->jt] -
301 stream.refs[stream.bpf_pc] + 5 );
302 JMP(stream.refs[stream.bpf_pc + ins->jf] -
303 stream.refs[stream.bpf_pc]);
306 case BPF_JMP|BPF_JGE|BPF_K:
308 JGE(stream.refs[stream.bpf_pc + ins->jt] -
309 stream.refs[stream.bpf_pc] + 5);
310 JMP(stream.refs[stream.bpf_pc + ins->jf] -
311 stream.refs[stream.bpf_pc]);
314 case BPF_JMP|BPF_JEQ|BPF_K:
316 JE(stream.refs[stream.bpf_pc + ins->jt] -
317 stream.refs[stream.bpf_pc] + 5);
318 JMP(stream.refs[stream.bpf_pc + ins->jf] -
319 stream.refs[stream.bpf_pc]);
322 case BPF_JMP|BPF_JSET|BPF_K:
325 JE(stream.refs[stream.bpf_pc + ins->jf] -
326 stream.refs[stream.bpf_pc] + 5);
327 JMP(stream.refs[stream.bpf_pc + ins->jt] -
328 stream.refs[stream.bpf_pc]);
331 case BPF_JMP|BPF_JGT|BPF_X:
333 JA(stream.refs[stream.bpf_pc + ins->jt] -
334 stream.refs[stream.bpf_pc] + 5);
335 JMP(stream.refs[stream.bpf_pc + ins->jf] -
336 stream.refs[stream.bpf_pc]);
339 case BPF_JMP|BPF_JGE|BPF_X:
341 JAE(stream.refs[stream.bpf_pc + ins->jt] -
342 stream.refs[stream.bpf_pc] + 5);
343 JMP(stream.refs[stream.bpf_pc + ins->jf] -
344 stream.refs[stream.bpf_pc]);
347 case BPF_JMP|BPF_JEQ|BPF_X:
349 JE(stream.refs[stream.bpf_pc + ins->jt] -
350 stream.refs[stream.bpf_pc] + 5);
351 JMP(stream.refs[stream.bpf_pc + ins->jf] -
352 stream.refs[stream.bpf_pc]);
355 case BPF_JMP|BPF_JSET|BPF_X:
358 JE(stream.refs[stream.bpf_pc + ins->jf] -
359 stream.refs[stream.bpf_pc] + 5);
360 JMP(stream.refs[stream.bpf_pc + ins->jt] -
361 stream.refs[stream.bpf_pc]);
364 case BPF_ALU|BPF_ADD|BPF_X:
368 case BPF_ALU|BPF_SUB|BPF_X:
372 case BPF_ALU|BPF_MUL|BPF_X:
378 case BPF_ALU|BPF_DIV|BPF_X:
390 case BPF_ALU|BPF_AND|BPF_X:
394 case BPF_ALU|BPF_OR|BPF_X:
398 case BPF_ALU|BPF_LSH|BPF_X:
403 case BPF_ALU|BPF_RSH|BPF_X:
408 case BPF_ALU|BPF_ADD|BPF_K:
412 case BPF_ALU|BPF_SUB|BPF_K:
416 case BPF_ALU|BPF_MUL|BPF_K:
423 case BPF_ALU|BPF_DIV|BPF_K:
431 case BPF_ALU|BPF_AND|BPF_K:
435 case BPF_ALU|BPF_OR|BPF_K:
439 case BPF_ALU|BPF_LSH|BPF_K:
440 SHLib(EAX, (ins->k) & 255);
443 case BPF_ALU|BPF_RSH|BPF_K:
444 SHRib(EAX, (ins->k) & 255);
447 case BPF_ALU|BPF_NEG:
451 case BPF_MISC|BPF_TAX:
455 case BPF_MISC|BPF_TXA:
466 stream.ibuf = (char *)malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
467 if (stream.ibuf == NULL) {
468 free(stream.refs, M_BPFJIT);
473 * modify the reference table to contain the offsets and
474 * not the lengths of the instructions
476 for (i = 1; i < nins + 1; i++)
477 stream.refs[i] += stream.refs[i - 1];
479 /* Reset the counters */
483 /* the second pass creates the actual code */
488 * the reference table is needed only during compilation,
491 free(stream.refs, M_BPFJIT);
493 return (bpf_filter_func)stream.ibuf;