2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (C) 2005-2016 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>
41 #include <sys/socket.h>
42 #include <sys/malloc.h>
48 #include <sys/param.h>
51 #include <sys/types.h>
54 #include <net/bpf_jitter.h>
56 #include <i386/i386/bpf_jit_machdep.h>
58 bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, size_t *);
61 * Emit routine to update the jump table.
64 emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len)
67 if (stream->refs != NULL)
68 (stream->refs)[stream->bpf_pc] += len;
69 stream->cur_ip += len;
73 * Emit routine to output the actual binary code.
76 emit_code(bpf_bin_stream *stream, u_int value, u_int len)
81 stream->ibuf[stream->cur_ip] = (u_char)value;
86 *((u_short *)(void *)(stream->ibuf + stream->cur_ip)) =
92 *((u_int *)(void *)(stream->ibuf + stream->cur_ip)) = value;
101 * Scan the filter program and find possible optimization.
104 bpf_jit_optimize(struct bpf_insn *prog, u_int nins)
109 /* Do we return immediately? */
110 if (BPF_CLASS(prog[0].code) == BPF_RET)
111 return (BPF_JIT_FRET);
113 for (flags = 0, i = 0; i < nins; i++) {
114 switch (prog[i].code) {
115 case BPF_LD|BPF_W|BPF_ABS:
116 case BPF_LD|BPF_H|BPF_ABS:
117 case BPF_LD|BPF_B|BPF_ABS:
118 case BPF_LD|BPF_W|BPF_IND:
119 case BPF_LD|BPF_H|BPF_IND:
120 case BPF_LD|BPF_B|BPF_IND:
121 case BPF_LDX|BPF_MSH|BPF_B:
122 flags |= BPF_JIT_FPKT;
125 case BPF_LDX|BPF_MEM:
128 flags |= BPF_JIT_FMEM;
131 case BPF_JMP|BPF_JGT|BPF_K:
132 case BPF_JMP|BPF_JGE|BPF_K:
133 case BPF_JMP|BPF_JEQ|BPF_K:
134 case BPF_JMP|BPF_JSET|BPF_K:
135 case BPF_JMP|BPF_JGT|BPF_X:
136 case BPF_JMP|BPF_JGE|BPF_X:
137 case BPF_JMP|BPF_JEQ|BPF_X:
138 case BPF_JMP|BPF_JSET|BPF_X:
139 flags |= BPF_JIT_FJMP;
141 case BPF_ALU|BPF_DIV|BPF_K:
142 case BPF_ALU|BPF_MOD|BPF_K:
143 flags |= BPF_JIT_FADK;
146 if (flags == BPF_JIT_FLAG_ALL)
154 * Function that does the real stuff.
157 bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
159 bpf_bin_stream stream;
160 struct bpf_insn *ins;
161 int flags, fret, fpkt, fmem, fjmp, fadk;
166 * NOTE: Do not modify the name of this variable, as it's used by
167 * the macros to emit code.
171 flags = bpf_jit_optimize(prog, nins);
172 fret = (flags & BPF_JIT_FRET) != 0;
173 fpkt = (flags & BPF_JIT_FPKT) != 0;
174 fmem = (flags & BPF_JIT_FMEM) != 0;
175 fjmp = (flags & BPF_JIT_FJMP) != 0;
176 fadk = (flags & BPF_JIT_FADK) != 0;
177 save_esp = (fpkt || fmem || fadk); /* Stack is used. */
182 memset(&stream, 0, sizeof(stream));
184 /* Allocate the reference table for the jumps. */
187 stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
190 stream.refs = calloc(nins + 1, sizeof(u_int));
192 if (stream.refs == NULL)
197 * The first pass will emit the lengths of the instructions
198 * to create the reference table.
202 for (pass = 0; pass < 2; pass++) {
205 /* Create the procedure header. */
211 SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP);
218 MOVodd(16, EBP, EDI);
221 for (i = 0; i < nins; i++) {
257 case BPF_LD|BPF_W|BPF_ABS:
263 CMPid(sizeof(int32_t), ECX);
271 MOVobd(EBX, ESI, EAX);
275 case BPF_LD|BPF_H|BPF_ABS:
282 CMPid(sizeof(int16_t), ECX);
289 MOVobw(EBX, ESI, AX);
293 case BPF_LD|BPF_B|BPF_ABS:
303 MOVobb(EBX, ESI, AL);
306 case BPF_LD|BPF_W|BPF_LEN:
308 MOVodd(12, EBP, EAX);
311 MOVodd(12, ECX, EAX);
315 case BPF_LDX|BPF_W|BPF_LEN:
317 MOVodd(12, EBP, EDX);
320 MOVodd(12, ECX, EDX);
324 case BPF_LD|BPF_W|BPF_IND:
335 CMPid(sizeof(int32_t), ECX);
343 MOVobd(EBX, ESI, EAX);
347 case BPF_LD|BPF_H|BPF_IND:
359 CMPid(sizeof(int16_t), ECX);
366 MOVobw(EBX, ESI, AX);
370 case BPF_LD|BPF_B|BPF_IND:
385 MOVobb(EBX, ESI, AL);
388 case BPF_LDX|BPF_MSH|BPF_B:
399 MOVobb(EBX, ESI, DL);
408 case BPF_LDX|BPF_IMM:
414 MOVid(((int)ins->k - BPF_MEMWORDS) *
415 sizeof(uint32_t), ESI);
416 MOVobd(ECX, ESI, EAX);
419 case BPF_LDX|BPF_MEM:
421 MOVid(((int)ins->k - BPF_MEMWORDS) *
422 sizeof(uint32_t), ESI);
423 MOVobd(ECX, ESI, EDX);
428 * XXX this command and the following could
429 * be optimized if the previous instruction
430 * was already of this type
433 MOVid(((int)ins->k - BPF_MEMWORDS) *
434 sizeof(uint32_t), ESI);
435 MOVomd(EAX, ECX, ESI);
440 MOVid(((int)ins->k - BPF_MEMWORDS) *
441 sizeof(uint32_t), ESI);
442 MOVomd(EDX, ECX, ESI);
449 case BPF_JMP|BPF_JGT|BPF_K:
450 case BPF_JMP|BPF_JGE|BPF_K:
451 case BPF_JMP|BPF_JEQ|BPF_K:
452 case BPF_JMP|BPF_JSET|BPF_K:
453 case BPF_JMP|BPF_JGT|BPF_X:
454 case BPF_JMP|BPF_JGE|BPF_X:
455 case BPF_JMP|BPF_JEQ|BPF_X:
456 case BPF_JMP|BPF_JSET|BPF_X:
457 if (ins->jt == ins->jf) {
462 case BPF_JMP|BPF_JGT|BPF_K:
467 case BPF_JMP|BPF_JGE|BPF_K:
472 case BPF_JMP|BPF_JEQ|BPF_K:
477 case BPF_JMP|BPF_JSET|BPF_K:
482 case BPF_JMP|BPF_JGT|BPF_X:
487 case BPF_JMP|BPF_JGE|BPF_X:
492 case BPF_JMP|BPF_JEQ|BPF_X:
497 case BPF_JMP|BPF_JSET|BPF_X:
504 case BPF_ALU|BPF_ADD|BPF_X:
508 case BPF_ALU|BPF_SUB|BPF_X:
512 case BPF_ALU|BPF_MUL|BPF_X:
518 case BPF_ALU|BPF_DIV|BPF_X:
519 case BPF_ALU|BPF_MOD|BPF_X:
541 if (BPF_OP(ins->code) == BPF_MOD)
546 case BPF_ALU|BPF_AND|BPF_X:
550 case BPF_ALU|BPF_OR|BPF_X:
554 case BPF_ALU|BPF_XOR|BPF_X:
558 case BPF_ALU|BPF_LSH|BPF_X:
563 case BPF_ALU|BPF_RSH|BPF_X:
568 case BPF_ALU|BPF_ADD|BPF_K:
572 case BPF_ALU|BPF_SUB|BPF_K:
576 case BPF_ALU|BPF_MUL|BPF_K:
583 case BPF_ALU|BPF_DIV|BPF_K:
584 case BPF_ALU|BPF_MOD|BPF_K:
589 if (BPF_OP(ins->code) == BPF_MOD)
594 case BPF_ALU|BPF_AND|BPF_K:
598 case BPF_ALU|BPF_OR|BPF_K:
602 case BPF_ALU|BPF_XOR|BPF_K:
606 case BPF_ALU|BPF_LSH|BPF_K:
607 SHLib((ins->k) & 0xff, EAX);
610 case BPF_ALU|BPF_RSH|BPF_K:
611 SHRib((ins->k) & 0xff, EAX);
614 case BPF_ALU|BPF_NEG:
618 case BPF_MISC|BPF_TAX:
622 case BPF_MISC|BPF_TXA:
632 *size = stream.cur_ip;
634 stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
635 if (stream.ibuf == NULL)
638 stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
640 if (stream.ibuf == MAP_FAILED) {
647 * Modify the reference table to contain the offsets and
648 * not the lengths of the instructions.
651 for (i = 1; i < nins + 1; i++)
652 stream.refs[i] += stream.refs[i - 1];
654 /* Reset the counters. */
658 /* The second pass creates the actual code. */
663 * The reference table is needed only during compilation,
664 * now we can free it.
668 free(stream.refs, M_BPFJIT);
674 if (stream.ibuf != NULL &&
675 mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
676 munmap(stream.ibuf, *size);
681 return ((bpf_filter_func)(void *)stream.ibuf);