Replace all stack usages with registers and remove unused macros.
This commit is contained in:
parent
510c672e13
commit
5bf34e3e87
@ -131,12 +131,10 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
ins = prog;
|
||||
|
||||
/* create the procedure header */
|
||||
PUSH(RBP);
|
||||
MOVrq(RSP, RBP);
|
||||
MOVdoq(ESI, -8, RBP);
|
||||
MOVdoq(EDX, -12, RBP);
|
||||
PUSH(RBX);
|
||||
MOVrq2(RBX, R8);
|
||||
MOVrq(RDI, RBX);
|
||||
MOVrd2(ESI, R9D);
|
||||
MOVrd(EDX, EDI);
|
||||
|
||||
for (i = 0; i < nins; i++) {
|
||||
stream.bpf_pc++;
|
||||
@ -147,24 +145,24 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
|
||||
case BPF_RET|BPF_K:
|
||||
MOVid(ins->k, EAX);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
break;
|
||||
|
||||
case BPF_RET|BPF_A:
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
break;
|
||||
|
||||
case BPF_LD|BPF_W|BPF_ABS:
|
||||
MOVid(ins->k, ECX);
|
||||
MOVrd(ECX, ESI);
|
||||
ADDib(sizeof(int), ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(5);
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(6);
|
||||
ZEROrd(EAX);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVobd(RBX, RSI, EAX);
|
||||
BSWAP(EAX);
|
||||
break;
|
||||
@ -174,10 +172,10 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
MOVid(ins->k, ECX);
|
||||
MOVrd(ECX, ESI);
|
||||
ADDib(sizeof(short), ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(3);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(4);
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVobw(RBX, RSI, AX);
|
||||
SWAP_AX();
|
||||
break;
|
||||
@ -185,19 +183,19 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
case BPF_LD|BPF_B|BPF_ABS:
|
||||
ZEROrd(EAX);
|
||||
MOVid(ins->k, ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(3);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(4);
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVobb(RBX, RCX, AL);
|
||||
break;
|
||||
|
||||
case BPF_LD|BPF_W|BPF_LEN:
|
||||
MOVoqd(-8, RBP, EAX);
|
||||
MOVrd3(R9D, EAX);
|
||||
break;
|
||||
|
||||
case BPF_LDX|BPF_W|BPF_LEN:
|
||||
MOVoqd(-8, RBP, EDX);
|
||||
MOVrd3(R9D, EDX);
|
||||
break;
|
||||
|
||||
case BPF_LD|BPF_W|BPF_IND:
|
||||
@ -205,11 +203,11 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
ADDrd(EDX, ECX);
|
||||
MOVrd(ECX, ESI);
|
||||
ADDib(sizeof(int), ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(5);
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(6);
|
||||
ZEROrd(EAX);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVobd(RBX, RSI, EAX);
|
||||
BSWAP(EAX);
|
||||
break;
|
||||
@ -220,10 +218,10 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
ADDrd(EDX, ECX);
|
||||
MOVrd(ECX, ESI);
|
||||
ADDib(sizeof(short), ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(3);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(4);
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVobw(RBX, RSI, AX);
|
||||
SWAP_AX();
|
||||
break;
|
||||
@ -232,20 +230,20 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
ZEROrd(EAX);
|
||||
MOVid(ins->k, ECX);
|
||||
ADDrd(EDX, ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(3);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(4);
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVobb(RBX, RCX, AL);
|
||||
break;
|
||||
|
||||
case BPF_LDX|BPF_MSH|BPF_B:
|
||||
MOVid(ins->k, ECX);
|
||||
CMPoqd(-12, RBP, ECX);
|
||||
JLEb(5);
|
||||
CMPrd(EDI, ECX);
|
||||
JLEb(6);
|
||||
ZEROrd(EAX);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
ZEROrd(EDX);
|
||||
MOVobb(RBX, RCX, DL);
|
||||
ANDib(0xf, DL);
|
||||
@ -377,10 +375,10 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, int *mem)
|
||||
|
||||
case BPF_ALU|BPF_DIV|BPF_X:
|
||||
CMPid(0, EDX);
|
||||
JNEb(5);
|
||||
JNEb(6);
|
||||
ZEROrd(EAX);
|
||||
POP(RBX);
|
||||
LEAVE_RET();
|
||||
MOVrq3(R8, RBX);
|
||||
RET();
|
||||
MOVrd(EDX, ECX);
|
||||
ZEROrd(EDX);
|
||||
DIVrd(ECX);
|
||||
|
@ -45,6 +45,14 @@
|
||||
#define RBP 5
|
||||
#define RSI 6
|
||||
#define RDI 7
|
||||
#define R8 0
|
||||
#define R9 1
|
||||
#define R10 2
|
||||
#define R11 3
|
||||
#define R12 4
|
||||
#define R13 5
|
||||
#define R14 6
|
||||
#define R15 7
|
||||
|
||||
#define EAX 0
|
||||
#define ECX 1
|
||||
@ -54,6 +62,14 @@
|
||||
#define EBP 5
|
||||
#define ESI 6
|
||||
#define EDI 7
|
||||
#define R8D 0
|
||||
#define R9D 1
|
||||
#define R10D 2
|
||||
#define R11D 3
|
||||
#define R12D 4
|
||||
#define R13D 5
|
||||
#define R14D 6
|
||||
#define R15D 7
|
||||
|
||||
#define AX 0
|
||||
#define CX 1
|
||||
@ -125,6 +141,20 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
|
||||
(3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* movl sr32,dr32 (dr32 = %r8-15d) */
|
||||
#define MOVrd2(sr32, dr32) do { \
|
||||
emitm(&stream, 0x8941, 2); \
|
||||
emitm(&stream, \
|
||||
(3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* movl sr32,dr32 (sr32 = %r8-15d) */
|
||||
#define MOVrd3(sr32, dr32) do { \
|
||||
emitm(&stream, 0x8944, 2); \
|
||||
emitm(&stream, \
|
||||
(3 << 6) | ((sr32 & 0x7) << 3) | (dr32 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* movq sr64,dr64 */
|
||||
#define MOVrq(sr64, dr64) do { \
|
||||
emitm(&stream, 0x8948, 2); \
|
||||
@ -132,20 +162,18 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
|
||||
(3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* movl off(sr64),dr32 */
|
||||
#define MOVoqd(off, sr64, dr32) do { \
|
||||
emitm(&stream, 0x8b, 1); \
|
||||
/* movq sr64,dr64 (dr64 = %r8-15) */
|
||||
#define MOVrq2(sr64, dr64) do { \
|
||||
emitm(&stream, 0x8949, 2); \
|
||||
emitm(&stream, \
|
||||
(1 << 6) | ((dr32 & 0x7) << 3) | (sr64 & 0x7), 1); \
|
||||
emitm(&stream, off, 1); \
|
||||
(3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* movl sr32,off(dr64) */
|
||||
#define MOVdoq(sr32, off, dr64) do { \
|
||||
emitm(&stream, 0x89, 1); \
|
||||
/* movq sr64,dr64 (sr64 = %r8-15) */
|
||||
#define MOVrq3(sr64, dr64) do { \
|
||||
emitm(&stream, 0x894c, 2); \
|
||||
emitm(&stream, \
|
||||
(1 << 6) | ((sr32 & 0x7) << 3) | (dr64 & 0x7), 1); \
|
||||
emitm(&stream, off, 1); \
|
||||
(3 << 6) | ((sr64 & 0x7) << 3) | (dr64 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* movl (sr64,or64,1),dr32 */
|
||||
@ -187,19 +215,9 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
|
||||
emitm(&stream, 0xc486, 2); \
|
||||
} while (0)
|
||||
|
||||
/* pushq r64 */
|
||||
#define PUSH(r64) do { \
|
||||
emitm(&stream, (5 << 4) | (0 << 3) | (r64 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* popq r64 */
|
||||
#define POP(r64) do { \
|
||||
emitm(&stream, (5 << 4) | (1 << 3) | (r64 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* leaveq/retq */
|
||||
#define LEAVE_RET() do { \
|
||||
emitm(&stream, 0xc3c9, 2); \
|
||||
/* ret */
|
||||
#define RET() do { \
|
||||
emitm(&stream, 0xc3, 1); \
|
||||
} while (0)
|
||||
|
||||
/* addl sr32,dr32 */
|
||||
@ -215,13 +233,6 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
|
||||
emitm(&stream, i32, 4); \
|
||||
} while (0)
|
||||
|
||||
/* addl i32,r32 */
|
||||
#define ADDid(i32, r32) do { \
|
||||
emitm(&stream, 0x81, 1); \
|
||||
emitm(&stream, (24 << 3) | r32, 1); \
|
||||
emitm(&stream, i32, 4); \
|
||||
} while (0)
|
||||
|
||||
/* addl i8,r32 */
|
||||
#define ADDib(i8, r32) do { \
|
||||
emitm(&stream, 0x83, 1); \
|
||||
@ -333,14 +344,6 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
|
||||
emitm(&stream, (27 << 3) | (r32 & 0x7), 1); \
|
||||
} while (0)
|
||||
|
||||
/* cmpl off(sr64),dr32 */
|
||||
#define CMPoqd(off, sr64, dr32) do { \
|
||||
emitm(&stream, 0x3b, 1); \
|
||||
emitm(&stream, \
|
||||
(1 << 6) | ((dr32 & 0x7) << 3) | (sr64 & 0x7), 1); \
|
||||
emitm(&stream, off, 1); \
|
||||
} while (0)
|
||||
|
||||
/* cmpl sr32,dr32 */
|
||||
#define CMPrd(sr32, dr32) do { \
|
||||
emitm(&stream, 0x39, 1); \
|
||||
@ -372,12 +375,6 @@ typedef void (*emit_func)(bpf_bin_stream *stream, u_int value, u_int n);
|
||||
emitm(&stream, off32, 4); \
|
||||
} while (0)
|
||||
|
||||
/* jle off32 */
|
||||
#define JLE(off32) do { \
|
||||
emitm(&stream, 0x8e0f, 2); \
|
||||
emitm(&stream, off32, 4); \
|
||||
} while (0)
|
||||
|
||||
/* jle off8 */
|
||||
#define JLEb(off8) do { \
|
||||
emitm(&stream, 0x7e, 1); \
|
||||
|
Loading…
x
Reference in New Issue
Block a user