summaryrefslogtreecommitdiffstats
path: root/sys
diff options
context:
space:
mode:
authorjkim <jkim@FreeBSD.org>2009-11-20 21:12:40 +0000
committerjkim <jkim@FreeBSD.org>2009-11-20 21:12:40 +0000
commit2d93b6e4248fe243a13bad6aae36aed5e0d576f6 (patch)
tree30a8fbd98d626d2b4c5f0839260a299294418951 /sys
parente66ae1c3f9bd142ce98d9a41f908d7e62fca76af (diff)
downloadFreeBSD-src-2d93b6e4248fe243a13bad6aae36aed5e0d576f6.zip
FreeBSD-src-2d93b6e4248fe243a13bad6aae36aed5e0d576f6.tar.gz
General style cleanup, no functional change.
Diffstat (limited to 'sys')
-rw-r--r--sys/amd64/amd64/bpf_jit_machdep.c61
-rw-r--r--sys/i386/i386/bpf_jit_machdep.c61
-rw-r--r--sys/net/bpf_jitter.c49
3 files changed, 72 insertions, 99 deletions
diff --git a/sys/amd64/amd64/bpf_jit_machdep.c b/sys/amd64/amd64/bpf_jit_machdep.c
index bbaebed..ff5500f 100644
--- a/sys/amd64/amd64/bpf_jit_machdep.c
+++ b/sys/amd64/amd64/bpf_jit_machdep.c
@@ -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);
diff --git a/sys/i386/i386/bpf_jit_machdep.c b/sys/i386/i386/bpf_jit_machdep.c
index 8e51f7b..2a4ed46 100644
--- a/sys/i386/i386/bpf_jit_machdep.c
+++ b/sys/i386/i386/bpf_jit_machdep.c
@@ -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);
diff --git a/sys/net/bpf_jitter.c b/sys/net/bpf_jitter.c
index a72d2d4..d024047 100644
--- a/sys/net/bpf_jitter.c
+++ b/sys/net/bpf_jitter.c
@@ -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,
OpenPOWER on IntegriCloud