1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Linux Socket Filter Data Structures
5 #ifndef __TOOLS_LINUX_FILTER_H
6 #define __TOOLS_LINUX_FILTER_H
10 /* ArgX, context and stack frame pointer register positions. Note,
11 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
12 * calls in BPF_CALL instruction.
14 #define BPF_REG_ARG1 BPF_REG_1
15 #define BPF_REG_ARG2 BPF_REG_2
16 #define BPF_REG_ARG3 BPF_REG_3
17 #define BPF_REG_ARG4 BPF_REG_4
18 #define BPF_REG_ARG5 BPF_REG_5
19 #define BPF_REG_CTX BPF_REG_6
20 #define BPF_REG_FP BPF_REG_10
22 /* Additional register mappings for converted user programs. */
23 #define BPF_REG_A BPF_REG_0
24 #define BPF_REG_X BPF_REG_7
25 #define BPF_REG_TMP BPF_REG_8
27 /* BPF program can access up to 512 bytes of stack space. */
28 #define MAX_BPF_STACK 512
30 /* Helper macros for filter block array initializers. */
32 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
34 #define BPF_ALU64_REG(OP, DST, SRC) \
35 ((struct bpf_insn) { \
36 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
42 #define BPF_ALU32_REG(OP, DST, SRC) \
43 ((struct bpf_insn) { \
44 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
50 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
52 #define BPF_ALU64_IMM(OP, DST, IMM) \
53 ((struct bpf_insn) { \
54 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
60 #define BPF_ALU32_IMM(OP, DST, IMM) \
61 ((struct bpf_insn) { \
62 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
68 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
70 #define BPF_ENDIAN(TYPE, DST, LEN) \
71 ((struct bpf_insn) { \
72 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
78 /* Short form of mov, dst_reg = src_reg */
80 #define BPF_MOV64_REG(DST, SRC) \
81 ((struct bpf_insn) { \
82 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
88 #define BPF_MOV32_REG(DST, SRC) \
89 ((struct bpf_insn) { \
90 .code = BPF_ALU | BPF_MOV | BPF_X, \
96 /* Short form of mov, dst_reg = imm32 */
98 #define BPF_MOV64_IMM(DST, IMM) \
99 ((struct bpf_insn) { \
100 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
106 #define BPF_MOV32_IMM(DST, IMM) \
107 ((struct bpf_insn) { \
108 .code = BPF_ALU | BPF_MOV | BPF_K, \
114 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
116 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
117 ((struct bpf_insn) { \
118 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
124 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
125 ((struct bpf_insn) { \
126 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
132 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
134 #define BPF_LD_ABS(SIZE, IMM) \
135 ((struct bpf_insn) { \
136 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
142 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
144 #define BPF_LD_IND(SIZE, SRC, IMM) \
145 ((struct bpf_insn) { \
146 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
152 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
154 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
155 ((struct bpf_insn) { \
156 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
162 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
164 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
165 ((struct bpf_insn) { \
166 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
175 * BPF_ADD *(uint *) (dst_reg + off16) += src_reg
176 * BPF_AND *(uint *) (dst_reg + off16) &= src_reg
177 * BPF_OR *(uint *) (dst_reg + off16) |= src_reg
178 * BPF_XOR *(uint *) (dst_reg + off16) ^= src_reg
179 * BPF_ADD | BPF_FETCH src_reg = atomic_fetch_add(dst_reg + off16, src_reg);
180 * BPF_AND | BPF_FETCH src_reg = atomic_fetch_and(dst_reg + off16, src_reg);
181 * BPF_OR | BPF_FETCH src_reg = atomic_fetch_or(dst_reg + off16, src_reg);
182 * BPF_XOR | BPF_FETCH src_reg = atomic_fetch_xor(dst_reg + off16, src_reg);
183 * BPF_XCHG src_reg = atomic_xchg(dst_reg + off16, src_reg)
184 * BPF_CMPXCHG r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg)
187 #define BPF_ATOMIC_OP(SIZE, OP, DST, SRC, OFF) \
188 ((struct bpf_insn) { \
189 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC, \
196 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) BPF_ATOMIC_OP(SIZE, BPF_ADD, DST, SRC, OFF)
198 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
200 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
201 ((struct bpf_insn) { \
202 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
208 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
210 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
211 ((struct bpf_insn) { \
212 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
218 /* Like BPF_JMP_REG, but with 32-bit wide operands for comparison. */
220 #define BPF_JMP32_REG(OP, DST, SRC, OFF) \
221 ((struct bpf_insn) { \
222 .code = BPF_JMP32 | BPF_OP(OP) | BPF_X, \
228 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
230 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
231 ((struct bpf_insn) { \
232 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
238 /* Like BPF_JMP_IMM, but with 32-bit wide operands for comparison. */
240 #define BPF_JMP32_IMM(OP, DST, IMM, OFF) \
241 ((struct bpf_insn) { \
242 .code = BPF_JMP32 | BPF_OP(OP) | BPF_K, \
248 /* Unconditional jumps, goto pc + off16 */
250 #define BPF_JMP_A(OFF) \
251 ((struct bpf_insn) { \
252 .code = BPF_JMP | BPF_JA, \
260 #define BPF_EMIT_CALL(FUNC) \
261 ((struct bpf_insn) { \
262 .code = BPF_JMP | BPF_CALL, \
266 .imm = ((FUNC) - BPF_FUNC_unspec) })
268 /* Raw code statement block */
270 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
271 ((struct bpf_insn) { \
278 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
280 #define BPF_LD_IMM64(DST, IMM) \
281 BPF_LD_IMM64_RAW(DST, 0, IMM)
283 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
284 ((struct bpf_insn) { \
285 .code = BPF_LD | BPF_DW | BPF_IMM, \
289 .imm = (__u32) (IMM) }), \
290 ((struct bpf_insn) { \
291 .code = 0, /* zero is reserved opcode */ \
295 .imm = ((__u64) (IMM)) >> 32 })
297 #define BPF_LD_IMM64_RAW_FULL(DST, SRC, OFF1, OFF2, IMM1, IMM2) \
298 ((struct bpf_insn) { \
299 .code = BPF_LD | BPF_DW | BPF_IMM, \
304 ((struct bpf_insn) { \
305 .code = 0, /* zero is reserved opcode */ \
311 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
313 #define BPF_LD_MAP_FD(DST, MAP_FD) \
314 BPF_LD_IMM64_RAW_FULL(DST, BPF_PSEUDO_MAP_FD, 0, 0, \
317 #define BPF_LD_MAP_VALUE(DST, MAP_FD, VALUE_OFF) \
318 BPF_LD_IMM64_RAW_FULL(DST, BPF_PSEUDO_MAP_VALUE, 0, 0, \
323 #define BPF_CALL_REL(TGT) \
324 ((struct bpf_insn) { \
325 .code = BPF_JMP | BPF_CALL, \
327 .src_reg = BPF_PSEUDO_CALL, \
333 #define BPF_EXIT_INSN() \
334 ((struct bpf_insn) { \
335 .code = BPF_JMP | BPF_EXIT, \
341 #endif /* __TOOLS_LINUX_FILTER_H */