2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (C) 2005-2009 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/socket.h>
41 #include <sys/malloc.h>
47 #include <sys/param.h>
50 #include <sys/types.h>
53 #include <net/bpf_jitter.h>
55 #include <amd64/amd64/bpf_jit_machdep.h>
57 bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, size_t *);
60 * Emit routine to update the jump table.
63 emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len)
66 if (stream->refs != NULL)
67 (stream->refs)[stream->bpf_pc] += len;
68 stream->cur_ip += len;
72 * Emit routine to output the actual binary code.
75 emit_code(bpf_bin_stream *stream, u_int value, u_int len)
80 stream->ibuf[stream->cur_ip] = (u_char)value;
85 *((u_short *)(stream->ibuf + stream->cur_ip)) = (u_short)value;
90 *((u_int *)(stream->ibuf + stream->cur_ip)) = value;
99 * Scan the filter program and find possible optimization.
102 bpf_jit_optimize(struct bpf_insn *prog, u_int nins)
107 /* Do we return immediately? */
108 if (BPF_CLASS(prog[0].code) == BPF_RET)
109 return (BPF_JIT_FRET);
111 for (flags = 0, i = 0; i < nins; i++) {
112 switch (prog[i].code) {
113 case BPF_LD|BPF_W|BPF_ABS:
114 case BPF_LD|BPF_H|BPF_ABS:
115 case BPF_LD|BPF_B|BPF_ABS:
116 case BPF_LD|BPF_W|BPF_IND:
117 case BPF_LD|BPF_H|BPF_IND:
118 case BPF_LD|BPF_B|BPF_IND:
119 case BPF_LDX|BPF_MSH|BPF_B:
120 flags |= BPF_JIT_FPKT;
123 case BPF_LDX|BPF_MEM:
126 flags |= BPF_JIT_FMEM;
128 case BPF_LD|BPF_W|BPF_LEN:
129 case BPF_LDX|BPF_W|BPF_LEN:
130 flags |= BPF_JIT_FLEN;
133 case BPF_JMP|BPF_JGT|BPF_K:
134 case BPF_JMP|BPF_JGE|BPF_K:
135 case BPF_JMP|BPF_JEQ|BPF_K:
136 case BPF_JMP|BPF_JSET|BPF_K:
137 case BPF_JMP|BPF_JGT|BPF_X:
138 case BPF_JMP|BPF_JGE|BPF_X:
139 case BPF_JMP|BPF_JEQ|BPF_X:
140 case BPF_JMP|BPF_JSET|BPF_X:
141 flags |= BPF_JIT_FJMP;
144 if (flags == BPF_JIT_FLAG_ALL)
152 * Function that does the real stuff.
155 bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
157 bpf_bin_stream stream;
158 struct bpf_insn *ins;
159 int flags, fret, fpkt, fmem, fjmp, flen;
163 * NOTE: Do not modify the name of this variable, as it's used by
164 * the macros to emit code.
168 flags = bpf_jit_optimize(prog, nins);
169 fret = (flags & BPF_JIT_FRET) != 0;
170 fpkt = (flags & BPF_JIT_FPKT) != 0;
171 fmem = (flags & BPF_JIT_FMEM) != 0;
172 fjmp = (flags & BPF_JIT_FJMP) != 0;
173 flen = (flags & BPF_JIT_FLEN) != 0;
178 memset(&stream, 0, sizeof(stream));
180 /* Allocate the reference table for the jumps. */
183 stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
186 stream.refs = calloc(nins + 1, sizeof(u_int));
188 if (stream.refs == NULL)
193 * The first pass will emit the lengths of the instructions
194 * to create the reference table.
198 for (pass = 0; pass < 2; pass++) {
201 /* Create the procedure header. */
205 SUBib(BPF_MEMWORDS * sizeof(uint32_t), RSP);
214 for (i = 0; i < nins; i++) {
238 case BPF_LD|BPF_W|BPF_ABS:
244 CMPid(sizeof(int32_t), ECX);
255 MOVobd(RCX, RSI, EAX);
259 case BPF_LD|BPF_H|BPF_ABS:
266 CMPid(sizeof(int16_t), ECX);
274 MOVobw(RCX, RSI, AX);
278 case BPF_LD|BPF_B|BPF_ABS:
289 MOVobb(RCX, RSI, AL);
292 case BPF_LD|BPF_W|BPF_LEN:
296 case BPF_LDX|BPF_W|BPF_LEN:
300 case BPF_LD|BPF_W|BPF_IND:
311 CMPid(sizeof(int32_t), ECX);
322 MOVobd(RCX, RSI, EAX);
326 case BPF_LD|BPF_H|BPF_IND:
338 CMPid(sizeof(int16_t), ECX);
346 MOVobw(RCX, RSI, AX);
350 case BPF_LD|BPF_B|BPF_IND:
366 MOVobb(RCX, RSI, AL);
369 case BPF_LDX|BPF_MSH|BPF_B:
383 MOVobb(RCX, RSI, DL);
392 case BPF_LDX|BPF_IMM:
397 MOVid(ins->k * sizeof(uint32_t), ESI);
398 MOVobd(RSP, RSI, EAX);
401 case BPF_LDX|BPF_MEM:
402 MOVid(ins->k * sizeof(uint32_t), ESI);
403 MOVobd(RSP, RSI, EDX);
408 * XXX this command and the following could
409 * be optimized if the previous instruction
410 * was already of this type
412 MOVid(ins->k * sizeof(uint32_t), ESI);
413 MOVomd(EAX, RSP, RSI);
417 MOVid(ins->k * sizeof(uint32_t), ESI);
418 MOVomd(EDX, RSP, RSI);
425 case BPF_JMP|BPF_JGT|BPF_K:
426 if (ins->jt == ins->jf) {
434 case BPF_JMP|BPF_JGE|BPF_K:
435 if (ins->jt == ins->jf) {
443 case BPF_JMP|BPF_JEQ|BPF_K:
444 if (ins->jt == ins->jf) {
452 case BPF_JMP|BPF_JSET|BPF_K:
453 if (ins->jt == ins->jf) {
461 case BPF_JMP|BPF_JGT|BPF_X:
462 if (ins->jt == ins->jf) {
470 case BPF_JMP|BPF_JGE|BPF_X:
471 if (ins->jt == ins->jf) {
479 case BPF_JMP|BPF_JEQ|BPF_X:
480 if (ins->jt == ins->jf) {
488 case BPF_JMP|BPF_JSET|BPF_X:
489 if (ins->jt == ins->jf) {
497 case BPF_ALU|BPF_ADD|BPF_X:
501 case BPF_ALU|BPF_SUB|BPF_X:
505 case BPF_ALU|BPF_MUL|BPF_X:
511 case BPF_ALU|BPF_DIV|BPF_X:
528 case BPF_ALU|BPF_AND|BPF_X:
532 case BPF_ALU|BPF_OR|BPF_X:
536 case BPF_ALU|BPF_LSH|BPF_X:
541 case BPF_ALU|BPF_RSH|BPF_X:
546 case BPF_ALU|BPF_ADD|BPF_K:
550 case BPF_ALU|BPF_SUB|BPF_K:
554 case BPF_ALU|BPF_MUL|BPF_K:
561 case BPF_ALU|BPF_DIV|BPF_K:
569 case BPF_ALU|BPF_AND|BPF_K:
573 case BPF_ALU|BPF_OR|BPF_K:
577 case BPF_ALU|BPF_LSH|BPF_K:
578 SHLib((ins->k) & 0xff, EAX);
581 case BPF_ALU|BPF_RSH|BPF_K:
582 SHRib((ins->k) & 0xff, EAX);
585 case BPF_ALU|BPF_NEG:
589 case BPF_MISC|BPF_TAX:
593 case BPF_MISC|BPF_TXA:
603 *size = stream.cur_ip;
605 stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
606 if (stream.ibuf == NULL)
609 stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
611 if (stream.ibuf == MAP_FAILED) {
618 * Modify the reference table to contain the offsets and
619 * not the lengths of the instructions.
622 for (i = 1; i < nins + 1; i++)
623 stream.refs[i] += stream.refs[i - 1];
625 /* Reset the counters. */
629 /* The second pass creates the actual code. */
634 * The reference table is needed only during compilation,
635 * now we can free it.
639 free(stream.refs, M_BPFJIT);
645 if (stream.ibuf != NULL &&
646 mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
647 munmap(stream.ibuf, *size);
652 return ((bpf_filter_func)stream.ibuf);