2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
5 * Copyright (C) 2005-2017 Jung-uk Kim <jkim@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Politecnico di Torino nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
49 #include <sys/param.h>
52 #include <sys/types.h>
55 #include <net/bpf_jitter.h>
57 #include <i386/i386/bpf_jit_machdep.h>
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 *)(void *)(stream->ibuf + stream->cur_ip)) =
91 *((u_int *)(void *)(stream->ibuf + stream->cur_ip)) = value;
100 * Scan the filter program and find possible optimization.
103 bpf_jit_optimize(struct bpf_insn *prog, u_int nins)
108 /* Do we return immediately? */
109 if (BPF_CLASS(prog[0].code) == BPF_RET)
110 return (BPF_JIT_FRET);
112 for (flags = 0, i = 0; i < nins; i++) {
113 switch (prog[i].code) {
114 case BPF_LD|BPF_W|BPF_ABS:
115 case BPF_LD|BPF_H|BPF_ABS:
116 case BPF_LD|BPF_B|BPF_ABS:
117 case BPF_LD|BPF_W|BPF_IND:
118 case BPF_LD|BPF_H|BPF_IND:
119 case BPF_LD|BPF_B|BPF_IND:
120 case BPF_LDX|BPF_MSH|BPF_B:
121 flags |= BPF_JIT_FPKT;
124 case BPF_LDX|BPF_MEM:
127 flags |= BPF_JIT_FMEM;
130 case BPF_JMP|BPF_JGT|BPF_K:
131 case BPF_JMP|BPF_JGE|BPF_K:
132 case BPF_JMP|BPF_JEQ|BPF_K:
133 case BPF_JMP|BPF_JSET|BPF_K:
134 case BPF_JMP|BPF_JGT|BPF_X:
135 case BPF_JMP|BPF_JGE|BPF_X:
136 case BPF_JMP|BPF_JEQ|BPF_X:
137 case BPF_JMP|BPF_JSET|BPF_X:
138 flags |= BPF_JIT_FJMP;
140 case BPF_ALU|BPF_DIV|BPF_K:
141 case BPF_ALU|BPF_MOD|BPF_K:
142 flags |= BPF_JIT_FADK;
145 if (flags == BPF_JIT_FLAG_ALL)
153 * Function that does the real stuff.
156 bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
158 bpf_bin_stream stream;
159 struct bpf_insn *ins;
160 int flags, fret, fpkt, fmem, fjmp, fadk;
165 * NOTE: Do not modify the name of this variable, as it's used by
166 * the macros to emit code.
170 flags = bpf_jit_optimize(prog, nins);
171 fret = (flags & BPF_JIT_FRET) != 0;
172 fpkt = (flags & BPF_JIT_FPKT) != 0;
173 fmem = (flags & BPF_JIT_FMEM) != 0;
174 fjmp = (flags & BPF_JIT_FJMP) != 0;
175 fadk = (flags & BPF_JIT_FADK) != 0;
176 save_esp = (fpkt || fmem || fadk); /* Stack is used. */
181 memset(&stream, 0, sizeof(stream));
183 /* Allocate the reference table for the jumps. */
186 stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
189 stream.refs = calloc(nins + 1, sizeof(u_int));
191 if (stream.refs == NULL)
196 * The first pass will emit the lengths of the instructions
197 * to create the reference table.
201 for (pass = 0; pass < 2; pass++) {
204 /* Create the procedure header. */
210 SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP);
217 MOVodd(16, EBP, EDI);
220 for (i = 0; i < nins; i++) {
256 case BPF_LD|BPF_W|BPF_ABS:
262 CMPid(sizeof(int32_t), ECX);
270 MOVobd(EBX, ESI, EAX);
274 case BPF_LD|BPF_H|BPF_ABS:
281 CMPid(sizeof(int16_t), ECX);
288 MOVobw(EBX, ESI, AX);
292 case BPF_LD|BPF_B|BPF_ABS:
302 MOVobb(EBX, ESI, AL);
305 case BPF_LD|BPF_W|BPF_LEN:
307 MOVodd(12, EBP, EAX);
310 MOVodd(12, ECX, EAX);
314 case BPF_LDX|BPF_W|BPF_LEN:
316 MOVodd(12, EBP, EDX);
319 MOVodd(12, ECX, EDX);
323 case BPF_LD|BPF_W|BPF_IND:
334 CMPid(sizeof(int32_t), ECX);
342 MOVobd(EBX, ESI, EAX);
346 case BPF_LD|BPF_H|BPF_IND:
358 CMPid(sizeof(int16_t), ECX);
365 MOVobw(EBX, ESI, AX);
369 case BPF_LD|BPF_B|BPF_IND:
384 MOVobb(EBX, ESI, AL);
387 case BPF_LDX|BPF_MSH|BPF_B:
398 MOVobb(EBX, ESI, DL);
407 case BPF_LDX|BPF_IMM:
413 MOVid(((int)ins->k - BPF_MEMWORDS) *
414 sizeof(uint32_t), ESI);
415 MOVobd(ECX, ESI, EAX);
418 case BPF_LDX|BPF_MEM:
420 MOVid(((int)ins->k - BPF_MEMWORDS) *
421 sizeof(uint32_t), ESI);
422 MOVobd(ECX, ESI, EDX);
427 * XXX this command and the following could
428 * be optimized if the previous instruction
429 * was already of this type
432 MOVid(((int)ins->k - BPF_MEMWORDS) *
433 sizeof(uint32_t), ESI);
434 MOVomd(EAX, ECX, ESI);
439 MOVid(((int)ins->k - BPF_MEMWORDS) *
440 sizeof(uint32_t), ESI);
441 MOVomd(EDX, ECX, ESI);
448 case BPF_JMP|BPF_JGT|BPF_K:
449 case BPF_JMP|BPF_JGE|BPF_K:
450 case BPF_JMP|BPF_JEQ|BPF_K:
451 case BPF_JMP|BPF_JSET|BPF_K:
452 case BPF_JMP|BPF_JGT|BPF_X:
453 case BPF_JMP|BPF_JGE|BPF_X:
454 case BPF_JMP|BPF_JEQ|BPF_X:
455 case BPF_JMP|BPF_JSET|BPF_X:
456 if (ins->jt == ins->jf) {
461 case BPF_JMP|BPF_JGT|BPF_K:
466 case BPF_JMP|BPF_JGE|BPF_K:
471 case BPF_JMP|BPF_JEQ|BPF_K:
476 case BPF_JMP|BPF_JSET|BPF_K:
481 case BPF_JMP|BPF_JGT|BPF_X:
486 case BPF_JMP|BPF_JGE|BPF_X:
491 case BPF_JMP|BPF_JEQ|BPF_X:
496 case BPF_JMP|BPF_JSET|BPF_X:
503 case BPF_ALU|BPF_ADD|BPF_X:
507 case BPF_ALU|BPF_SUB|BPF_X:
511 case BPF_ALU|BPF_MUL|BPF_X:
517 case BPF_ALU|BPF_DIV|BPF_X:
518 case BPF_ALU|BPF_MOD|BPF_X:
540 if (BPF_OP(ins->code) == BPF_MOD)
545 case BPF_ALU|BPF_AND|BPF_X:
549 case BPF_ALU|BPF_OR|BPF_X:
553 case BPF_ALU|BPF_XOR|BPF_X:
557 case BPF_ALU|BPF_LSH|BPF_X:
562 case BPF_ALU|BPF_RSH|BPF_X:
567 case BPF_ALU|BPF_ADD|BPF_K:
571 case BPF_ALU|BPF_SUB|BPF_K:
575 case BPF_ALU|BPF_MUL|BPF_K:
582 case BPF_ALU|BPF_DIV|BPF_K:
583 case BPF_ALU|BPF_MOD|BPF_K:
588 if (BPF_OP(ins->code) == BPF_MOD)
593 case BPF_ALU|BPF_AND|BPF_K:
597 case BPF_ALU|BPF_OR|BPF_K:
601 case BPF_ALU|BPF_XOR|BPF_K:
605 case BPF_ALU|BPF_LSH|BPF_K:
606 SHLib((ins->k) & 0xff, EAX);
609 case BPF_ALU|BPF_RSH|BPF_K:
610 SHRib((ins->k) & 0xff, EAX);
613 case BPF_ALU|BPF_NEG:
617 case BPF_MISC|BPF_TAX:
621 case BPF_MISC|BPF_TXA:
631 *size = stream.cur_ip;
633 stream.ibuf = malloc_exec(*size, M_BPFJIT, M_NOWAIT);
634 if (stream.ibuf == NULL)
637 stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
639 if (stream.ibuf == MAP_FAILED) {
646 * Modify the reference table to contain the offsets and
647 * not the lengths of the instructions.
650 for (i = 1; i < nins + 1; i++)
651 stream.refs[i] += stream.refs[i - 1];
653 /* Reset the counters. */
657 /* The second pass creates the actual code. */
662 * The reference table is needed only during compilation,
663 * now we can free it.
667 free(stream.refs, M_BPFJIT);
673 if (stream.ibuf != NULL &&
674 mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
675 munmap(stream.ibuf, *size);
680 return ((bpf_filter_func)(void *)stream.ibuf);