Skip to content

Commit b85062a

Browse files
Jiong WangAlexei Starovoitov
Jiong Wang
authored and
Alexei Starovoitov
committed
arm: bpf: implement jitting of JMP32
This patch implements code-gen for new JMP32 instructions on arm. For JSET, "ands" (AND with flags updated) is used, so corresponding encoding helper is added. Cc: Shubham Bansal <illusionist.neo@gmail.com> Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 654b65a commit b85062a

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

arch/arm/net/bpf_jit_32.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,31 +1083,43 @@ static inline void emit_ldx_r(const s8 dst[], const s8 src,
10831083

10841084
/* Arithmatic Operation */
10851085
static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
1086-
const u8 rn, struct jit_ctx *ctx, u8 op) {
1086+
const u8 rn, struct jit_ctx *ctx, u8 op,
1087+
bool is_jmp64) {
10871088
switch (op) {
10881089
case BPF_JSET:
1089-
emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
1090-
emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
1091-
emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
1090+
if (is_jmp64) {
1091+
emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
1092+
emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
1093+
emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
1094+
} else {
1095+
emit(ARM_ANDS_R(ARM_IP, rt, rn), ctx);
1096+
}
10921097
break;
10931098
case BPF_JEQ:
10941099
case BPF_JNE:
10951100
case BPF_JGT:
10961101
case BPF_JGE:
10971102
case BPF_JLE:
10981103
case BPF_JLT:
1099-
emit(ARM_CMP_R(rd, rm), ctx);
1100-
_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
1104+
if (is_jmp64) {
1105+
emit(ARM_CMP_R(rd, rm), ctx);
1106+
/* Only compare low halve if high halve are equal. */
1107+
_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
1108+
} else {
1109+
emit(ARM_CMP_R(rt, rn), ctx);
1110+
}
11011111
break;
11021112
case BPF_JSLE:
11031113
case BPF_JSGT:
11041114
emit(ARM_CMP_R(rn, rt), ctx);
1105-
emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
1115+
if (is_jmp64)
1116+
emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
11061117
break;
11071118
case BPF_JSLT:
11081119
case BPF_JSGE:
11091120
emit(ARM_CMP_R(rt, rn), ctx);
1110-
emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
1121+
if (is_jmp64)
1122+
emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
11111123
break;
11121124
}
11131125
}
@@ -1615,6 +1627,17 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
16151627
case BPF_JMP | BPF_JLT | BPF_X:
16161628
case BPF_JMP | BPF_JSLT | BPF_X:
16171629
case BPF_JMP | BPF_JSLE | BPF_X:
1630+
case BPF_JMP32 | BPF_JEQ | BPF_X:
1631+
case BPF_JMP32 | BPF_JGT | BPF_X:
1632+
case BPF_JMP32 | BPF_JGE | BPF_X:
1633+
case BPF_JMP32 | BPF_JNE | BPF_X:
1634+
case BPF_JMP32 | BPF_JSGT | BPF_X:
1635+
case BPF_JMP32 | BPF_JSGE | BPF_X:
1636+
case BPF_JMP32 | BPF_JSET | BPF_X:
1637+
case BPF_JMP32 | BPF_JLE | BPF_X:
1638+
case BPF_JMP32 | BPF_JLT | BPF_X:
1639+
case BPF_JMP32 | BPF_JSLT | BPF_X:
1640+
case BPF_JMP32 | BPF_JSLE | BPF_X:
16181641
/* Setup source registers */
16191642
rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
16201643
rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
@@ -1641,6 +1664,17 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
16411664
case BPF_JMP | BPF_JLE | BPF_K:
16421665
case BPF_JMP | BPF_JSLT | BPF_K:
16431666
case BPF_JMP | BPF_JSLE | BPF_K:
1667+
case BPF_JMP32 | BPF_JEQ | BPF_K:
1668+
case BPF_JMP32 | BPF_JGT | BPF_K:
1669+
case BPF_JMP32 | BPF_JGE | BPF_K:
1670+
case BPF_JMP32 | BPF_JNE | BPF_K:
1671+
case BPF_JMP32 | BPF_JSGT | BPF_K:
1672+
case BPF_JMP32 | BPF_JSGE | BPF_K:
1673+
case BPF_JMP32 | BPF_JSET | BPF_K:
1674+
case BPF_JMP32 | BPF_JLT | BPF_K:
1675+
case BPF_JMP32 | BPF_JLE | BPF_K:
1676+
case BPF_JMP32 | BPF_JSLT | BPF_K:
1677+
case BPF_JMP32 | BPF_JSLE | BPF_K:
16441678
if (off == 0)
16451679
break;
16461680
rm = tmp2[0];
@@ -1652,7 +1686,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
16521686
rd = arm_bpf_get_reg64(dst, tmp, ctx);
16531687

16541688
/* Check for the condition */
1655-
emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code));
1689+
emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code),
1690+
BPF_CLASS(code) == BPF_JMP);
16561691

16571692
/* Setup JUMP instruction */
16581693
jmp_offset = bpf2a32_offset(i+off, i, ctx);

arch/arm/net/bpf_jit_32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define ARM_INST_ADDS_I 0x02900000
6363

6464
#define ARM_INST_AND_R 0x00000000
65+
#define ARM_INST_ANDS_R 0x00100000
6566
#define ARM_INST_AND_I 0x02000000
6667

6768
#define ARM_INST_BIC_R 0x01c00000
@@ -172,6 +173,7 @@
172173
#define ARM_ADC_I(rd, rn, imm) _AL3_I(ARM_INST_ADC, rd, rn, imm)
173174

174175
#define ARM_AND_R(rd, rn, rm) _AL3_R(ARM_INST_AND, rd, rn, rm)
176+
#define ARM_ANDS_R(rd, rn, rm) _AL3_R(ARM_INST_ANDS, rd, rn, rm)
175177
#define ARM_AND_I(rd, rn, imm) _AL3_I(ARM_INST_AND, rd, rn, imm)
176178

177179
#define ARM_BIC_R(rd, rn, rm) _AL3_R(ARM_INST_BIC, rd, rn, rm)

0 commit comments

Comments
 (0)