ALU operations in the PPC32 eBPF JIT are supposed to use src2_reg, not
dst_reg, as the second source operand in order to work correctly when
a MOV was folded into them.
However, a few operations don't correctly do this yet, so fix them.
Fixes: 19daf0aef84f ("powerpc/bpf/32: perform three operands ALU operations")
Signed-off-by: Lukas Stockner <[email protected]>
---
arch/powerpc/net/bpf_jit_comp32.c | 32 ++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)
diff --git a/arch/powerpc/net/bpf_jit_comp32.c
b/arch/powerpc/net/bpf_jit_comp32.c
index 2c3d94c94ed7..83457b21d0a8 100644
--- a/arch/powerpc/net/bpf_jit_comp32.c
+++ b/arch/powerpc/net/bpf_jit_comp32.c
@@ -495,7 +495,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
} else if (imm > 0 && is_power_of_2(imm)) {
imm = ilog2(imm);
EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, imm,
0, 31 - imm));
- EMIT(PPC_RAW_RLWIMI(dst_reg_h, dst_reg, imm, 32
- imm, 31));
+ EMIT(PPC_RAW_RLWIMI(dst_reg_h, src2_reg, imm,
32 - imm, 31));
EMIT(PPC_RAW_SLWI(dst_reg, src2_reg, imm));
} else {
bpf_set_seen_register(ctx, tmp_reg);
@@ -603,6 +603,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
EMIT(PPC_RAW_SUBFZE(dst_reg_h, src2_reg_h));
imm = -imm;
src2_reg = dst_reg;
+ src2_reg_h = dst_reg_h;
}
if (imm == 1) {
EMIT(PPC_RAW_MR(dst_reg, src2_reg));
@@ -635,6 +636,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
case BPF_ALU64 | BPF_AND | BPF_K: /* dst = dst & imm */
if (imm >= 0)
EMIT(PPC_RAW_LI(dst_reg_h, 0));
+ else if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
fallthrough;
case BPF_ALU | BPF_AND | BPF_K: /* (u32) dst = dst & imm */
if (!IMM_H(imm)) {
@@ -660,6 +663,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
/* Sign-extended */
if (imm < 0)
EMIT(PPC_RAW_LI(dst_reg_h, -1));
+ else if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
fallthrough;
case BPF_ALU | BPF_OR | BPF_K:/* dst = (u32) dst | (u32) imm */
if (IMM_L(imm)) {
@@ -668,6 +673,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
}
if (IMM_H(imm))
EMIT(PPC_RAW_ORIS(dst_reg, src2_reg,
IMM_H(imm)));
+ else if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
break;
case BPF_ALU64 | BPF_XOR | BPF_X: /* dst ^= src */
if (dst_reg == src_reg) {
@@ -687,6 +694,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
case BPF_ALU64 | BPF_XOR | BPF_K: /* dst ^= imm */
if (imm < 0)
EMIT(PPC_RAW_NOR(dst_reg_h, src2_reg_h,
src2_reg_h));
+ else if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
fallthrough;
case BPF_ALU | BPF_XOR | BPF_K: /* (u32) dst ^= (u32) imm */
if (IMM_L(imm)) {
@@ -695,6 +704,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
}
if (IMM_H(imm))
EMIT(PPC_RAW_XORIS(dst_reg, src2_reg,
IMM_H(imm)));
+ else if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
break;
case BPF_ALU | BPF_LSH | BPF_X: /* (u32) dst <<= (u32) src */
EMIT(PPC_RAW_SLW(dst_reg, src2_reg, src_reg));
@@ -720,7 +731,10 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
u32 *fimage, struct code
if (imm < 0)
return -EINVAL;
if (!imm) {
- EMIT(PPC_RAW_MR(dst_reg, src2_reg));
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
+ if (dst_reg_h != src2_reg_h)
+ EMIT(PPC_RAW_MR(dst_reg_h, src2_reg_h));
} else if (imm < 32) {
EMIT(PPC_RAW_RLWINM(dst_reg_h, src2_reg_h, imm,
0, 31 - imm));
EMIT(PPC_RAW_RLWIMI(dst_reg_h, src2_reg, imm,
32 - imm, 31));
@@ -742,7 +756,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
EMIT(PPC_RAW_SRW(dst_reg, src2_reg, src_reg));
EMIT(PPC_RAW_ADDI(tmp_reg, src_reg, 32));
EMIT(PPC_RAW_SLW(_R0, src2_reg_h, _R0));
- EMIT(PPC_RAW_SRW(tmp_reg, dst_reg_h, tmp_reg));
+ EMIT(PPC_RAW_SRW(tmp_reg, src2_reg_h, tmp_reg));
EMIT(PPC_RAW_OR(dst_reg, dst_reg, _R0));
EMIT(PPC_RAW_SRW(dst_reg_h, src2_reg_h, src_reg));
EMIT(PPC_RAW_OR(dst_reg, dst_reg, tmp_reg));
@@ -834,8 +848,11 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
u32 *fimage, struct code
break;
case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
/* special mov32 for zext */
- if (imm == 1)
+ if (imm == 1) {
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
EMIT(PPC_RAW_LI(dst_reg_h, 0));
+ }
else if (off == 8)
EMIT(PPC_RAW_EXTSB(dst_reg, src_reg));
else if (off == 16)
@@ -858,6 +875,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
case BPF_ALU64 | BPF_END | BPF_FROM_LE:
switch (imm) {
case 16:
+ /* Copy lower half if needed. */
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
/* Copy 16 bits to upper part */
EMIT(PPC_RAW_RLWIMI(dst_reg, src2_reg, 16, 0,
15));
/* Rotate 8 bits right & mask */
@@ -901,7 +921,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32
*fimage, struct code
break;
case 32:
case 64:
- /* nop */
+ /* nop, just move if needed */
+ if (dst_reg != src2_reg)
+ EMIT(PPC_RAW_MR(dst_reg, src2_reg));
break;
}
break;
--
2.56.0