9 #include <linux/netdevice.h>
10 #include <linux/filter.h>
11 #include <asm/cacheflush.h>
12 #include <asm/processor.h>
56 #define BPF_SIZE_MAX 4096
58 #define SEEN_DATAREF 1
62 #define SEEN_LITERAL 16
63 #define SEEN_LOAD_WORD 32
64 #define SEEN_LOAD_HALF 64
65 #define SEEN_LOAD_BYTE 128
66 #define SEEN_LOAD_BMSH 256
67 #define SEEN_LOAD_IWORD 512
68 #define SEEN_LOAD_IHALF 1024
69 #define SEEN_LOAD_IBYTE 2048
73 if (jit->prg + 2 <= jit->mid) \
74 *(u16 *) jit->prg = op; \
80 if (jit->prg + 4 <= jit->mid) \
81 *(u32 *) jit->prg = op; \
85 #define EMIT4_DISP(op, disp) \
87 unsigned int __disp = (disp) & 0xfff; \
91 #define EMIT4_IMM(op, imm) \
93 unsigned int __imm = (imm) & 0xffff; \
97 #define EMIT4_PCREL(op, pcrel) \
99 long __pcrel = ((pcrel) >> 1) & 0xffff; \
100 EMIT4(op | __pcrel); \
103 #define EMIT6(op1, op2) \
105 if (jit->prg + 6 <= jit->mid) { \
106 *(u32 *) jit->prg = op1; \
107 *(u16 *) (jit->prg + 4) = op2; \
112 #define EMIT6_DISP(op1, op2, disp) \
114 unsigned int __disp = (disp) & 0xfff; \
115 EMIT6(op1 | __disp, op2); \
118 #define EMIT6_IMM(op, imm) \
120 unsigned int __imm = (imm); \
121 EMIT6(op | (__imm >> 16), __imm & 0xffff); \
124 #define EMIT_CONST(val) \
127 ret = (unsigned int) (jit->lit - jit->base_ip); \
128 jit->seen |= SEEN_LITERAL; \
129 if (jit->lit + 4 <= jit->end) \
130 *(u32 *) jit->lit = val; \
135 #define EMIT_FN_CONST(bit, fn) \
138 ret = (unsigned int) (jit->lit - jit->base_ip); \
139 if (jit->seen & bit) { \
140 jit->seen |= SEEN_LITERAL; \
141 if (jit->lit + 8 <= jit->end) \
142 *(void **) jit->lit = fn; \
148 static void bpf_jit_prologue(
struct bpf_jit *jit)
153 EMIT6(0xeb8ff058, 0x0024);
159 EMIT6(0xe3e0f098, 0x0024);
162 EMIT6(0xebcdf078, 0x0024);
165 EMIT6(0xe3c0f078, 0x0024);
168 EMIT6(0xe3d0f080, 0x0024);
196 static void bpf_jit_epilogue(
struct bpf_jit *jit)
212 EMIT6(0xebcdf078, 0x0004);
215 EMIT6(0xe3c0f078, 0x0004);
218 EMIT6(0xe3d0f080, 0x0004);
231 EMIT6(0xd73ff000, 0xf000);
237 switch (filter[0].
code) {
267 unsigned int *addrs,
int i,
int last)
274 switch (filter->
code) {
286 else if (test_facility(21))
304 else if (test_facility(21))
320 else if (test_facility(34))
350 if (test_facility(21))
363 if (test_facility(21))
402 offset = addrs[i +
K] + jit->
start - jit->
prg;
414 if (filter->
jt != filter->
jf) {
418 else if (test_facility(21))
429 offset = addrs[i + filter->
jt] + jit->
start - jit->
prg;
433 if (filter->
jt != 0) {
435 offset = addrs[i + filter->
jt] + jit->
start - jit->
prg;
438 if (filter->
jf != 0) {
440 offset = addrs[i + filter->
jf] + jit->
start - jit->
prg;
441 EMIT4_PCREL(0xa7040000 | (mask ^ 0xf00000), offset);
447 if (filter->
jt != filter->
jf) {
467 if (filter->
jt != filter->
jf) {
476 if (filter->
jt != filter->
jf) {
495 load_abs:
if ((
int) K < 0)
543 else if (test_facility(21))
555 else if (test_facility(21))
694 unsigned long size, prg_len, lit_len;
704 memset(addrs, 0, fp->
len *
sizeof(*addrs));
705 memset(&jit, 0,
sizeof(cjit));
706 memset(&cjit, 0,
sizeof(cjit));
708 for (pass = 0; pass < 10; pass++) {
712 bpf_jit_prologue(&jit);
713 bpf_jit_noleaks(&jit, fp->
insns);
714 for (i = 0; i < fp->
len; i++) {
715 if (bpf_jit_insn(&jit, fp->
insns + i, addrs, i,
719 bpf_jit_epilogue(&jit);
722 if (
memcmp(&jit, &cjit,
sizeof(jit)) == 0)
724 }
else if (jit.
prg == cjit.
prg && jit.
lit == cjit.
lit) {
726 lit_len = jit.
lit - jit.
mid;
727 size =
max_t(
unsigned long, prg_len + lit_len,
743 pr_err(
"flen=%d proglen=%lu pass=%d image=%p\n",
748 print_hex_dump(
KERN_ERR,
"JIT literals:\n",