bpf_jit_machdep.c (199531) | bpf_jit_machdep.c (199603) |
---|---|
1/*- 2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) 3 * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 16 unchanged lines hidden (view full) --- 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. 30 */ 31 32#include <sys/cdefs.h> | 1/*- 2 * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy) 3 * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 16 unchanged lines hidden (view full) --- 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. 30 */ 31 32#include <sys/cdefs.h> |
33__FBSDID("$FreeBSD: head/sys/i386/i386/bpf_jit_machdep.c 199531 2009-11-19 15:45:24Z jkim $"); | 33__FBSDID("$FreeBSD: head/sys/i386/i386/bpf_jit_machdep.c 199603 2009-11-20 18:49:20Z jkim $"); |
34 35#ifdef _KERNEL 36#include "opt_bpf.h" 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> --- 6 unchanged lines hidden (view full) --- 48 49#include <sys/types.h> 50 51#include <net/bpf.h> 52#include <net/bpf_jitter.h> 53 54#include <i386/i386/bpf_jit_machdep.h> 55 | 34 35#ifdef _KERNEL 36#include "opt_bpf.h" 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> --- 6 unchanged lines hidden (view full) --- 48 49#include <sys/types.h> 50 51#include <net/bpf.h> 52#include <net/bpf_jitter.h> 53 54#include <i386/i386/bpf_jit_machdep.h> 55 |
56bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, size_t *, int *); | 56bpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, size_t *); |
57 58/* 59 * emit routine to update the jump table 60 */ 61static void 62emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len) 63{ 64 --- 27 unchanged lines hidden (view full) --- 92 93 return; 94} 95 96/* 97 * Function that does the real stuff 98 */ 99bpf_filter_func | 57 58/* 59 * emit routine to update the jump table 60 */ 61static void 62emit_length(bpf_bin_stream *stream, __unused u_int value, u_int len) 63{ 64 --- 27 unchanged lines hidden (view full) --- 92 93 return; 94} 95 96/* 97 * Function that does the real stuff 98 */ 99bpf_filter_func |
100bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size, int *mem) | 100bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) |
101{ 102 bpf_bin_stream stream; 103 struct bpf_insn *ins; 104 u_int i, pass; 105 106 /* 107 * NOTE: do not modify the name of this variable, as it's used by 108 * the macros to emit code. 109 */ 110 emit_func emitm; 111 112 /* Allocate the reference table for the jumps */ 113#ifdef _KERNEL | 101{ 102 bpf_bin_stream stream; 103 struct bpf_insn *ins; 104 u_int i, pass; 105 106 /* 107 * NOTE: do not modify the name of this variable, as it's used by 108 * the macros to emit code. 109 */ 110 emit_func emitm; 111 112 /* Allocate the reference table for the jumps */ 113#ifdef _KERNEL |
114 stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int), 115 M_BPFJIT, M_NOWAIT); | 114 stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT); |
116#else | 115#else |
117 stream.refs = (u_int *)malloc((nins + 1) * sizeof(u_int)); | 116 stream.refs = malloc((nins + 1) * sizeof(u_int)); |
118#endif 119 if (stream.refs == NULL) 120 return (NULL); 121 122 /* Reset the reference table */ 123 for (i = 0; i < nins + 1; i++) 124 stream.refs[i] = 0; 125 --- 8 unchanged lines hidden (view full) --- 134 135 pass = 0; 136 for (;;) { 137 ins = prog; 138 139 /* create the procedure header */ 140 PUSH(EBP); 141 MOVrd(ESP, EBP); | 117#endif 118 if (stream.refs == NULL) 119 return (NULL); 120 121 /* Reset the reference table */ 122 for (i = 0; i < nins + 1; i++) 123 stream.refs[i] = 0; 124 --- 8 unchanged lines hidden (view full) --- 133 134 pass = 0; 135 for (;;) { 136 ins = prog; 137 138 /* create the procedure header */ 139 PUSH(EBP); 140 MOVrd(ESP, EBP); |
141 SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP); |
|
142 PUSH(EDI); 143 PUSH(ESI); 144 PUSH(EBX); 145 MOVodd(8, EBP, EBX); 146 MOVodd(16, EBP, EDI); 147 148 for (i = 0; i < nins; i++) { 149 stream.bpf_pc++; --- 155 unchanged lines hidden (view full) --- 305 MOVid(ins->k, EAX); 306 break; 307 308 case BPF_LDX|BPF_IMM: 309 MOVid(ins->k, EDX); 310 break; 311 312 case BPF_LD|BPF_MEM: | 142 PUSH(EDI); 143 PUSH(ESI); 144 PUSH(EBX); 145 MOVodd(8, EBP, EBX); 146 MOVodd(16, EBP, EDI); 147 148 for (i = 0; i < nins; i++) { 149 stream.bpf_pc++; --- 155 unchanged lines hidden (view full) --- 305 MOVid(ins->k, EAX); 306 break; 307 308 case BPF_LDX|BPF_IMM: 309 MOVid(ins->k, EDX); 310 break; 311 312 case BPF_LD|BPF_MEM: |
313 MOVid((uintptr_t)mem, ECX); 314 MOVid(ins->k * 4, ESI); | 313 MOVrd(EBP, ECX); 314 MOVid(((int)ins->k - BPF_MEMWORDS) * 315 sizeof(uint32_t), ESI); |
315 MOVobd(ECX, ESI, EAX); 316 break; 317 318 case BPF_LDX|BPF_MEM: | 316 MOVobd(ECX, ESI, EAX); 317 break; 318 319 case BPF_LDX|BPF_MEM: |
319 MOVid((uintptr_t)mem, ECX); 320 MOVid(ins->k * 4, ESI); | 320 MOVrd(EBP, ECX); 321 MOVid(((int)ins->k - BPF_MEMWORDS) * 322 sizeof(uint32_t), ESI); |
321 MOVobd(ECX, ESI, EDX); 322 break; 323 324 case BPF_ST: 325 /* 326 * XXX this command and the following could 327 * be optimized if the previous instruction 328 * was already of this type 329 */ | 323 MOVobd(ECX, ESI, EDX); 324 break; 325 326 case BPF_ST: 327 /* 328 * XXX this command and the following could 329 * be optimized if the previous instruction 330 * was already of this type 331 */ |
330 MOVid((uintptr_t)mem, ECX); 331 MOVid(ins->k * 4, ESI); | 332 MOVrd(EBP, ECX); 333 MOVid(((int)ins->k - BPF_MEMWORDS) * 334 sizeof(uint32_t), ESI); |
332 MOVomd(EAX, ECX, ESI); 333 break; 334 335 case BPF_STX: | 335 MOVomd(EAX, ECX, ESI); 336 break; 337 338 case BPF_STX: |
336 MOVid((uintptr_t)mem, ECX); 337 MOVid(ins->k * 4, ESI); | 339 MOVrd(EBP, ECX); 340 MOVid(((int)ins->k - BPF_MEMWORDS) * 341 sizeof(uint32_t), ESI); |
338 MOVomd(EDX, ECX, ESI); 339 break; 340 341 case BPF_JMP|BPF_JA: 342 JMP(stream.refs[stream.bpf_pc + ins->k] - 343 stream.refs[stream.bpf_pc]); 344 break; 345 --- 162 unchanged lines hidden (view full) --- 508 stream.ibuf = NULL; 509 } 510#endif 511 *size = stream.cur_ip; 512 break; 513 } 514 515#ifdef _KERNEL | 342 MOVomd(EDX, ECX, ESI); 343 break; 344 345 case BPF_JMP|BPF_JA: 346 JMP(stream.refs[stream.bpf_pc + ins->k] - 347 stream.refs[stream.bpf_pc]); 348 break; 349 --- 162 unchanged lines hidden (view full) --- 512 stream.ibuf = NULL; 513 } 514#endif 515 *size = stream.cur_ip; 516 break; 517 } 518 519#ifdef _KERNEL |
516 stream.ibuf = (char *)contigmalloc(stream.cur_ip, M_BPFJIT, 517 M_NOWAIT, 0, ~0UL, 16, 0); | 520 stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT); |
518 if (stream.ibuf == NULL) 519 break; 520#else | 521 if (stream.ibuf == NULL) 522 break; 523#else |
521 stream.ibuf = (char *)mmap(NULL, stream.cur_ip, 522 PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); | 524 stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE, 525 MAP_ANON, -1, 0); |
523 if (stream.ibuf == MAP_FAILED) { 524 stream.ibuf = NULL; 525 break; 526 } 527#endif 528 529 /* 530 * modify the reference table to contain the offsets and --- 25 unchanged lines hidden --- | 526 if (stream.ibuf == MAP_FAILED) { 527 stream.ibuf = NULL; 528 break; 529 } 530#endif 531 532 /* 533 * modify the reference table to contain the offsets and --- 25 unchanged lines hidden --- |