Skip to content

Commit daabb2b

Browse files
puranjaymohanAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf/tests: add tests for cpuv4 instructions
The BPF JITs now support cpuv4 instructions. Add tests for these new instructions to the test suite: 1. Sign extended Load 2. Sign extended Mov 3. Unconditional byte swap 4. Unconditional jump with 32-bit offset 5. Signed division and modulo Signed-off-by: Puranjay Mohan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 59ff6d6 commit daabb2b

File tree

2 files changed

+417
-4
lines changed

2 files changed

+417
-4
lines changed

include/linux/filter.h

+46-4
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,25 @@ struct ctl_table_header;
117117

118118
/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
119119

120-
#define BPF_ALU64_IMM(OP, DST, IMM) \
120+
#define BPF_ALU64_IMM_OFF(OP, DST, IMM, OFF) \
121121
((struct bpf_insn) { \
122122
.code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
123123
.dst_reg = DST, \
124124
.src_reg = 0, \
125-
.off = 0, \
125+
.off = OFF, \
126126
.imm = IMM })
127+
#define BPF_ALU64_IMM(OP, DST, IMM) \
128+
BPF_ALU64_IMM_OFF(OP, DST, IMM, 0)
127129

128-
#define BPF_ALU32_IMM(OP, DST, IMM) \
130+
#define BPF_ALU32_IMM_OFF(OP, DST, IMM, OFF) \
129131
((struct bpf_insn) { \
130132
.code = BPF_ALU | BPF_OP(OP) | BPF_K, \
131133
.dst_reg = DST, \
132134
.src_reg = 0, \
133-
.off = 0, \
135+
.off = OFF, \
134136
.imm = IMM })
137+
#define BPF_ALU32_IMM(OP, DST, IMM) \
138+
BPF_ALU32_IMM_OFF(OP, DST, IMM, 0)
135139

136140
/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
137141

@@ -143,6 +147,16 @@ struct ctl_table_header;
143147
.off = 0, \
144148
.imm = LEN })
145149

150+
/* Byte Swap, bswap16/32/64 */
151+
152+
#define BPF_BSWAP(DST, LEN) \
153+
((struct bpf_insn) { \
154+
.code = BPF_ALU64 | BPF_END | BPF_SRC(BPF_TO_LE), \
155+
.dst_reg = DST, \
156+
.src_reg = 0, \
157+
.off = 0, \
158+
.imm = LEN })
159+
146160
/* Short form of mov, dst_reg = src_reg */
147161

148162
#define BPF_MOV64_REG(DST, SRC) \
@@ -179,6 +193,24 @@ struct ctl_table_header;
179193
.off = 0, \
180194
.imm = IMM })
181195

196+
/* Short form of movsx, dst_reg = (s8,s16,s32)src_reg */
197+
198+
#define BPF_MOVSX64_REG(DST, SRC, OFF) \
199+
((struct bpf_insn) { \
200+
.code = BPF_ALU64 | BPF_MOV | BPF_X, \
201+
.dst_reg = DST, \
202+
.src_reg = SRC, \
203+
.off = OFF, \
204+
.imm = 0 })
205+
206+
#define BPF_MOVSX32_REG(DST, SRC, OFF) \
207+
((struct bpf_insn) { \
208+
.code = BPF_ALU | BPF_MOV | BPF_X, \
209+
.dst_reg = DST, \
210+
.src_reg = SRC, \
211+
.off = OFF, \
212+
.imm = 0 })
213+
182214
/* Special form of mov32, used for doing explicit zero extension on dst. */
183215
#define BPF_ZEXT_REG(DST) \
184216
((struct bpf_insn) { \
@@ -263,6 +295,16 @@ static inline bool insn_is_zext(const struct bpf_insn *insn)
263295
.off = OFF, \
264296
.imm = 0 })
265297

298+
/* Memory load, dst_reg = *(signed size *) (src_reg + off16) */
299+
300+
#define BPF_LDX_MEMSX(SIZE, DST, SRC, OFF) \
301+
((struct bpf_insn) { \
302+
.code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEMSX, \
303+
.dst_reg = DST, \
304+
.src_reg = SRC, \
305+
.off = OFF, \
306+
.imm = 0 })
307+
266308
/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
267309

268310
#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \

0 commit comments

Comments
 (0)