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>
35 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
51 #include <sys/param.h>
54 #include <sys/types.h>
57 #include <net/bpf_jitter.h>
59 #include <amd64/amd64/bpf_jit_machdep.h>
62 * Emit routine to update the jump table.
65 emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len)
68 if (stream->refs != NULL)
69 (stream->refs)[stream->bpf_pc] += len;
70 stream->cur_ip += len;
74 * Emit routine to output the actual binary code.
77 emit_code(bpf_bin_stream *stream, u_int value, u_int len)
82 stream->ibuf[stream->cur_ip] = (u_char)value;
87 *((u_short *)(void *)(stream->ibuf + stream->cur_ip)) =
93 *((u_int *)(void *)(stream->ibuf + stream->cur_ip)) = value;
102 * Scan the filter program and find possible optimization.
105 bpf_jit_optimize(struct bpf_insn *prog, u_int nins)
110 /* Do we return immediately? */
111 if (BPF_CLASS(prog[0].code) == BPF_RET)
112 return (BPF_JIT_FRET);
114 for (flags = 0, i = 0; i < nins; i++) {
115 switch (prog[i].code) {
116 case BPF_LD|BPF_W|BPF_ABS:
117 case BPF_LD|BPF_H|BPF_ABS:
118 case BPF_LD|BPF_B|BPF_ABS:
119 case BPF_LD|BPF_W|BPF_IND:
120 case BPF_LD|BPF_H|BPF_IND:
121 case BPF_LD|BPF_B|BPF_IND:
122 case BPF_LDX|BPF_MSH|BPF_B:
123 flags |= BPF_JIT_FPKT;
126 case BPF_LDX|BPF_MEM:
129 flags |= BPF_JIT_FMEM;
131 case BPF_LD|BPF_W|BPF_LEN:
132 case BPF_LDX|BPF_W|BPF_LEN:
133 flags |= BPF_JIT_FLEN;
136 case BPF_JMP|BPF_JGT|BPF_K:
137 case BPF_JMP|BPF_JGE|BPF_K:
138 case BPF_JMP|BPF_JEQ|BPF_K:
139 case BPF_JMP|BPF_JSET|BPF_K:
140 case BPF_JMP|BPF_JGT|BPF_X:
141 case BPF_JMP|BPF_JGE|BPF_X:
142 case BPF_JMP|BPF_JEQ|BPF_X:
143 case BPF_JMP|BPF_JSET|BPF_X:
144 flags |= BPF_JIT_FJMP;
147 if (flags == BPF_JIT_FLAG_ALL)
155 * Function that does the real stuff.
158 bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
160 bpf_bin_stream stream;
161 struct bpf_insn *ins;
162 int flags, fret, fpkt, fmem, fjmp, flen;
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 flen = (flags & BPF_JIT_FLEN) != 0;
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. */
208 SUBib(BPF_MEMWORDS * sizeof(uint32_t), RSP);
217 for (i = 0; i < nins; i++) {
241 case BPF_LD|BPF_W|BPF_ABS:
247 CMPid(sizeof(int32_t), ECX);
258 MOVobd(RCX, RSI, EAX);
262 case BPF_LD|BPF_H|BPF_ABS:
269 CMPid(sizeof(int16_t), ECX);
277 MOVobw(RCX, RSI, AX);
281 case BPF_LD|BPF_B|BPF_ABS:
292 MOVobb(RCX, RSI, AL);
295 case BPF_LD|BPF_W|BPF_LEN:
299 case BPF_LDX|BPF_W|BPF_LEN:
303 case BPF_LD|BPF_W|BPF_IND:
314 CMPid(sizeof(int32_t), ECX);
325 MOVobd(RCX, RSI, EAX);
329 case BPF_LD|BPF_H|BPF_IND:
341 CMPid(sizeof(int16_t), ECX);
349 MOVobw(RCX, RSI, AX);
353 case BPF_LD|BPF_B|BPF_IND:
369 MOVobb(RCX, RSI, AL);
372 case BPF_LDX|BPF_MSH|BPF_B:
386 MOVobb(RCX, RSI, DL);
395 case BPF_LDX|BPF_IMM:
400 MOVid(ins->k * sizeof(uint32_t), ESI);
401 MOVobd(RSP, RSI, EAX);
404 case BPF_LDX|BPF_MEM:
405 MOVid(ins->k * sizeof(uint32_t), ESI);
406 MOVobd(RSP, RSI, EDX);
411 * XXX this command and the following could
412 * be optimized if the previous instruction
413 * was already of this type
415 MOVid(ins->k * sizeof(uint32_t), ESI);
416 MOVomd(EAX, RSP, RSI);
420 MOVid(ins->k * sizeof(uint32_t), ESI);
421 MOVomd(EDX, RSP, RSI);
428 case BPF_JMP|BPF_JGT|BPF_K:
429 case BPF_JMP|BPF_JGE|BPF_K:
430 case BPF_JMP|BPF_JEQ|BPF_K:
431 case BPF_JMP|BPF_JSET|BPF_K:
432 case BPF_JMP|BPF_JGT|BPF_X:
433 case BPF_JMP|BPF_JGE|BPF_X:
434 case BPF_JMP|BPF_JEQ|BPF_X:
435 case BPF_JMP|BPF_JSET|BPF_X:
436 if (ins->jt == ins->jf) {
441 case BPF_JMP|BPF_JGT|BPF_K:
446 case BPF_JMP|BPF_JGE|BPF_K:
451 case BPF_JMP|BPF_JEQ|BPF_K:
456 case BPF_JMP|BPF_JSET|BPF_K:
461 case BPF_JMP|BPF_JGT|BPF_X:
466 case BPF_JMP|BPF_JGE|BPF_X:
471 case BPF_JMP|BPF_JEQ|BPF_X:
476 case BPF_JMP|BPF_JSET|BPF_X:
483 case BPF_ALU|BPF_ADD|BPF_X:
487 case BPF_ALU|BPF_SUB|BPF_X:
491 case BPF_ALU|BPF_MUL|BPF_X:
497 case BPF_ALU|BPF_DIV|BPF_X:
498 case BPF_ALU|BPF_MOD|BPF_X:
512 if (BPF_OP(ins->code) == BPF_MOD)
517 case BPF_ALU|BPF_AND|BPF_X:
521 case BPF_ALU|BPF_OR|BPF_X:
525 case BPF_ALU|BPF_XOR|BPF_X:
529 case BPF_ALU|BPF_LSH|BPF_X:
534 case BPF_ALU|BPF_RSH|BPF_X:
539 case BPF_ALU|BPF_ADD|BPF_K:
543 case BPF_ALU|BPF_SUB|BPF_K:
547 case BPF_ALU|BPF_MUL|BPF_K:
554 case BPF_ALU|BPF_DIV|BPF_K:
555 case BPF_ALU|BPF_MOD|BPF_K:
560 if (BPF_OP(ins->code) == BPF_MOD)
565 case BPF_ALU|BPF_AND|BPF_K:
569 case BPF_ALU|BPF_OR|BPF_K:
573 case BPF_ALU|BPF_XOR|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_EXEC | 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)(void *)stream.ibuf);