Skip to content

Commit 758f204

Browse files
rnavborkmann
authored andcommitted
powerpc/bpf: use unsigned division instruction for 64-bit operations
BPF_ALU64 div/mod operations are currently using signed division, unlike BPF_ALU32 operations. Fix the same. DIV64 and MOD64 overflow tests pass with this fix. Fixes: 156d0e2 ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF") Cc: [email protected] # v4.8+ Signed-off-by: Naveen N. Rao <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 3e06826 commit 758f204

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

arch/powerpc/include/asm/ppc-opcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@
338338
#define PPC_INST_MADDLD 0x10000033
339339
#define PPC_INST_DIVWU 0x7c000396
340340
#define PPC_INST_DIVD 0x7c0003d2
341+
#define PPC_INST_DIVDU 0x7c000392
341342
#define PPC_INST_RLWINM 0x54000000
342343
#define PPC_INST_RLWINM_DOT 0x54000001
343344
#define PPC_INST_RLWIMI 0x50000000

arch/powerpc/net/bpf_jit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
___PPC_RA(a) | IMM_L(i))
117117
#define PPC_DIVWU(d, a, b) EMIT(PPC_INST_DIVWU | ___PPC_RT(d) | \
118118
___PPC_RA(a) | ___PPC_RB(b))
119-
#define PPC_DIVD(d, a, b) EMIT(PPC_INST_DIVD | ___PPC_RT(d) | \
119+
#define PPC_DIVDU(d, a, b) EMIT(PPC_INST_DIVDU | ___PPC_RT(d) | \
120120
___PPC_RA(a) | ___PPC_RB(b))
121121
#define PPC_AND(d, a, b) EMIT(PPC_INST_AND | ___PPC_RA(d) | \
122122
___PPC_RS(a) | ___PPC_RB(b))

arch/powerpc/net/bpf_jit_comp64.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,12 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
399399
case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
400400
case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
401401
if (BPF_OP(code) == BPF_MOD) {
402-
PPC_DIVD(b2p[TMP_REG_1], dst_reg, src_reg);
402+
PPC_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg);
403403
PPC_MULD(b2p[TMP_REG_1], src_reg,
404404
b2p[TMP_REG_1]);
405405
PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
406406
} else
407-
PPC_DIVD(dst_reg, dst_reg, src_reg);
407+
PPC_DIVDU(dst_reg, dst_reg, src_reg);
408408
break;
409409
case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
410410
case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
@@ -432,15 +432,15 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
432432
break;
433433
case BPF_ALU64:
434434
if (BPF_OP(code) == BPF_MOD) {
435-
PPC_DIVD(b2p[TMP_REG_2], dst_reg,
435+
PPC_DIVDU(b2p[TMP_REG_2], dst_reg,
436436
b2p[TMP_REG_1]);
437437
PPC_MULD(b2p[TMP_REG_1],
438438
b2p[TMP_REG_1],
439439
b2p[TMP_REG_2]);
440440
PPC_SUB(dst_reg, dst_reg,
441441
b2p[TMP_REG_1]);
442442
} else
443-
PPC_DIVD(dst_reg, dst_reg,
443+
PPC_DIVDU(dst_reg, dst_reg,
444444
b2p[TMP_REG_1]);
445445
break;
446446
}

0 commit comments

Comments
 (0)