Commit 4c38e2f3 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by Alexei Starovoitov

bpf, x64: save few bytes when mul is in alu32

Add a generic emit_mov_reg() helper in order to reuse it in BPF
multiplication to load the src into rax, we can save a few bytes
in alu32 while doing so.
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent d806a0cf
...@@ -422,6 +422,24 @@ static void emit_mov_imm64(u8 **pprog, u32 dst_reg, ...@@ -422,6 +422,24 @@ static void emit_mov_imm64(u8 **pprog, u32 dst_reg,
*pprog = prog; *pprog = prog;
} }
static void emit_mov_reg(u8 **pprog, bool is64, u32 dst_reg, u32 src_reg)
{
u8 *prog = *pprog;
int cnt = 0;
if (is64) {
/* mov dst, src */
EMIT_mov(dst_reg, src_reg);
} else {
/* mov32 dst, src */
if (is_ereg(dst_reg) || is_ereg(src_reg))
EMIT1(add_2mod(0x40, dst_reg, src_reg));
EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
}
*pprog = prog;
}
static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
int oldproglen, struct jit_context *ctx) int oldproglen, struct jit_context *ctx)
{ {
...@@ -480,16 +498,11 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, ...@@ -480,16 +498,11 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg)); EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
break; break;
/* mov dst, src */
case BPF_ALU64 | BPF_MOV | BPF_X: case BPF_ALU64 | BPF_MOV | BPF_X:
EMIT_mov(dst_reg, src_reg);
break;
/* mov32 dst, src */
case BPF_ALU | BPF_MOV | BPF_X: case BPF_ALU | BPF_MOV | BPF_X:
if (is_ereg(dst_reg) || is_ereg(src_reg)) emit_mov_reg(&prog,
EMIT1(add_2mod(0x40, dst_reg, src_reg)); BPF_CLASS(insn->code) == BPF_ALU64,
EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg)); dst_reg, src_reg);
break; break;
/* neg dst */ /* neg dst */
...@@ -615,6 +628,9 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, ...@@ -615,6 +628,9 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
case BPF_ALU | BPF_MUL | BPF_X: case BPF_ALU | BPF_MUL | BPF_X:
case BPF_ALU64 | BPF_MUL | BPF_K: case BPF_ALU64 | BPF_MUL | BPF_K:
case BPF_ALU64 | BPF_MUL | BPF_X: case BPF_ALU64 | BPF_MUL | BPF_X:
{
bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
if (dst_reg != BPF_REG_0) if (dst_reg != BPF_REG_0)
EMIT1(0x50); /* push rax */ EMIT1(0x50); /* push rax */
if (dst_reg != BPF_REG_3) if (dst_reg != BPF_REG_3)
...@@ -624,14 +640,11 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, ...@@ -624,14 +640,11 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
EMIT_mov(AUX_REG, dst_reg); EMIT_mov(AUX_REG, dst_reg);
if (BPF_SRC(insn->code) == BPF_X) if (BPF_SRC(insn->code) == BPF_X)
/* mov rax, src_reg */ emit_mov_reg(&prog, is64, BPF_REG_0, src_reg);
EMIT_mov(BPF_REG_0, src_reg);
else else
/* mov rax, imm32 */ emit_mov_imm32(&prog, is64, BPF_REG_0, imm32);
emit_mov_imm32(&prog, true,
BPF_REG_0, imm32);
if (BPF_CLASS(insn->code) == BPF_ALU64) if (is64)
EMIT1(add_1mod(0x48, AUX_REG)); EMIT1(add_1mod(0x48, AUX_REG));
else if (is_ereg(AUX_REG)) else if (is_ereg(AUX_REG))
EMIT1(add_1mod(0x40, AUX_REG)); EMIT1(add_1mod(0x40, AUX_REG));
...@@ -646,7 +659,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, ...@@ -646,7 +659,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
EMIT1(0x58); /* pop rax */ EMIT1(0x58); /* pop rax */
} }
break; break;
}
/* shifts */ /* shifts */
case BPF_ALU | BPF_LSH | BPF_K: case BPF_ALU | BPF_LSH | BPF_K:
case BPF_ALU | BPF_RSH | BPF_K: case BPF_ALU | BPF_RSH | BPF_K:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment