General style cleanup, no functional change.

This commit is contained in:
Jung-uk Kim 2009-11-20 21:12:40 +00:00
parent 4e1fc19cea
commit c12b965f99
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=199615
3 changed files with 72 additions and 99 deletions

View File

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <net/if.h>
#else
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#endif
@ -104,38 +105,38 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
u_int i, pass;
/*
* NOTE: do not modify the name of this variable, as it's used by
* NOTE: Do not modify the name of this variable, as it's used by
* the macros to emit code.
*/
emit_func emitm;
/* Allocate the reference table for the jumps */
/* Allocate the reference table for the jumps. */
#ifdef _KERNEL
stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT);
stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
M_NOWAIT | M_ZERO);
#else
stream.refs = malloc((nins + 1) * sizeof(u_int));
#endif
if (stream.refs == NULL)
return (NULL);
/* Reset the reference table */
for (i = 0; i < nins + 1; i++)
stream.refs[i] = 0;
#ifndef _KERNEL
memset(stream.refs, 0, (nins + 1) * sizeof(u_int));
#endif
stream.cur_ip = 0;
stream.bpf_pc = 0;
stream.ibuf = NULL;
/*
* the first pass will emit the lengths of the instructions
* to create the reference table
* The first pass will emit the lengths of the instructions
* to create the reference table.
*/
emitm = emit_length;
pass = 0;
for (;;) {
for (pass = 0; pass < 2; pass++) {
ins = prog;
/* create the procedure header */
/* Create the procedure header. */
PUSH(RBP);
MOVrq(RSP, RBP);
SUBib(BPF_MEMWORDS * sizeof(uint32_t), RSP);
@ -470,25 +471,16 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
ins++;
}
pass++;
if (pass >= 2) {
#ifndef _KERNEL
if (mprotect(stream.ibuf, stream.cur_ip,
PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, stream.cur_ip);
stream.ibuf = NULL;
}
#endif
*size = stream.cur_ip;
break;
}
if (pass > 0)
continue;
*size = stream.cur_ip;
#ifdef _KERNEL
stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL)
break;
#else
stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE,
stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
MAP_ANON, -1, 0);
if (stream.ibuf == MAP_FAILED) {
stream.ibuf = NULL;
@ -497,28 +489,33 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
#endif
/*
* modify the reference table to contain the offsets and
* not the lengths of the instructions
* Modify the reference table to contain the offsets and
* not the lengths of the instructions.
*/
for (i = 1; i < nins + 1; i++)
stream.refs[i] += stream.refs[i - 1];
/* Reset the counters */
/* Reset the counters. */
stream.cur_ip = 0;
stream.bpf_pc = 0;
/* the second pass creates the actual code */
/* The second pass creates the actual code. */
emitm = emit_code;
}
/*
* the reference table is needed only during compilation,
* now we can free it
* The reference table is needed only during compilation,
* now we can free it.
*/
#ifdef _KERNEL
free(stream.refs, M_BPFJIT);
#else
free(stream.refs);
if (stream.ibuf != NULL &&
mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, *size);
stream.ibuf = NULL;
}
#endif
return ((bpf_filter_func)stream.ibuf);

View File

@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
#include <net/if.h>
#else
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#endif
@ -104,38 +105,38 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
u_int i, pass;
/*
* NOTE: do not modify the name of this variable, as it's used by
* NOTE: Do not modify the name of this variable, as it's used by
* the macros to emit code.
*/
emit_func emitm;
/* Allocate the reference table for the jumps */
/* Allocate the reference table for the jumps. */
#ifdef _KERNEL
stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, M_NOWAIT);
stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT,
M_NOWAIT | M_ZERO);
#else
stream.refs = malloc((nins + 1) * sizeof(u_int));
#endif
if (stream.refs == NULL)
return (NULL);
/* Reset the reference table */
for (i = 0; i < nins + 1; i++)
stream.refs[i] = 0;
#ifndef _KERNEL
memset(stream.refs, 0, (nins + 1) * sizeof(u_int));
#endif
stream.cur_ip = 0;
stream.bpf_pc = 0;
stream.ibuf = NULL;
/*
* the first pass will emit the lengths of the instructions
* to create the reference table
* The first pass will emit the lengths of the instructions
* to create the reference table.
*/
emitm = emit_length;
pass = 0;
for (;;) {
for (pass = 0; pass < 2; pass++) {
ins = prog;
/* create the procedure header */
/* Create the procedure header. */
PUSH(EBP);
MOVrd(ESP, EBP);
SUBib(BPF_MEMWORDS * sizeof(uint32_t), ESP);
@ -503,25 +504,16 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
ins++;
}
pass++;
if (pass >= 2) {
#ifndef _KERNEL
if (mprotect(stream.ibuf, stream.cur_ip,
PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, stream.cur_ip);
stream.ibuf = NULL;
}
#endif
*size = stream.cur_ip;
break;
}
if (pass > 0)
continue;
*size = stream.cur_ip;
#ifdef _KERNEL
stream.ibuf = malloc(stream.cur_ip, M_BPFJIT, M_NOWAIT);
stream.ibuf = malloc(*size, M_BPFJIT, M_NOWAIT);
if (stream.ibuf == NULL)
break;
#else
stream.ibuf = mmap(NULL, stream.cur_ip, PROT_READ | PROT_WRITE,
stream.ibuf = mmap(NULL, *size, PROT_READ | PROT_WRITE,
MAP_ANON, -1, 0);
if (stream.ibuf == MAP_FAILED) {
stream.ibuf = NULL;
@ -530,28 +522,33 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size)
#endif
/*
* modify the reference table to contain the offsets and
* not the lengths of the instructions
* Modify the reference table to contain the offsets and
* not the lengths of the instructions.
*/
for (i = 1; i < nins + 1; i++)
stream.refs[i] += stream.refs[i - 1];
/* Reset the counters */
/* Reset the counters. */
stream.cur_ip = 0;
stream.bpf_pc = 0;
/* the second pass creates the actual code */
/* The second pass creates the actual code. */
emitm = emit_code;
}
/*
* the reference table is needed only during compilation,
* now we can free it
* The reference table is needed only during compilation,
* now we can free it.
*/
#ifdef _KERNEL
free(stream.refs, M_BPFJIT);
#else
free(stream.refs);
if (stream.ibuf != NULL &&
mprotect(stream.ibuf, *size, PROT_READ | PROT_EXEC) != 0) {
munmap(stream.ibuf, *size);
stream.ibuf = NULL;
}
#endif
return ((bpf_filter_func)stream.ibuf);

View File

@ -42,7 +42,6 @@ __FBSDID("$FreeBSD$");
#include <sys/sysctl.h>
#else
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/types.h>
@ -62,27 +61,36 @@ SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW, 0, "BPF JIT compiler");
int bpf_jitter_enable = 1;
SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,
&bpf_jitter_enable, 0, "enable BPF JIT compiler");
#endif
bpf_jit_filter *
bpf_jitter(struct bpf_insn *fp, int nins)
{
bpf_jit_filter *filter;
/* Allocate the filter structure */
/* Allocate the filter structure. */
#ifdef _KERNEL
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter),
M_BPFJIT, M_NOWAIT);
#else
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));
#endif
if (filter == NULL)
return (NULL);
/* No filter means accept all */
/* No filter means accept all. */
if (fp == NULL || nins == 0) {
filter->func = bpf_jit_accept_all;
return (filter);
}
/* Create the binary */
/* Create the binary. */
if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {
#ifdef _KERNEL
free(filter, M_BPFJIT);
#else
free(filter);
#endif
return (NULL);
}
@ -93,45 +101,16 @@ void
bpf_destroy_jit_filter(bpf_jit_filter *filter)
{
#ifdef _KERNEL
if (filter->func != bpf_jit_accept_all)
free(filter->func, M_BPFJIT);
free(filter, M_BPFJIT);
}
#else
bpf_jit_filter *
bpf_jitter(struct bpf_insn *fp, int nins)
{
bpf_jit_filter *filter;
/* Allocate the filter structure */
filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));
if (filter == NULL)
return (NULL);
/* No filter means accept all */
if (fp == NULL || nins == 0) {
filter->func = bpf_jit_accept_all;
return (filter);
}
/* Create the binary */
if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {
free(filter);
return (NULL);
}
return (filter);
}
void
bpf_destroy_jit_filter(bpf_jit_filter *filter)
{
if (filter->func != bpf_jit_accept_all)
munmap(filter->func, filter->size);
free(filter);
}
#endif
}
static u_int
bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen,