1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* eBPF mini library */
10 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
12 #define BPF_ALU64_REG(OP, DST, SRC) \
13 ((struct bpf_insn) { \
14 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
20 #define BPF_ALU32_REG(OP, DST, SRC) \
21 ((struct bpf_insn) { \
22 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
28 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
30 #define BPF_ALU64_IMM(OP, DST, IMM) \
31 ((struct bpf_insn) { \
32 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
38 #define BPF_ALU32_IMM(OP, DST, IMM) \
39 ((struct bpf_insn) { \
40 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
46 /* Short form of mov, dst_reg = src_reg */
48 #define BPF_MOV64_REG(DST, SRC) \
49 ((struct bpf_insn) { \
50 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
56 #define BPF_MOV32_REG(DST, SRC) \
57 ((struct bpf_insn) { \
58 .code = BPF_ALU | BPF_MOV | BPF_X, \
64 /* Short form of mov, dst_reg = imm32 */
66 #define BPF_MOV64_IMM(DST, IMM) \
67 ((struct bpf_insn) { \
68 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
74 #define BPF_MOV32_IMM(DST, IMM) \
75 ((struct bpf_insn) { \
76 .code = BPF_ALU | BPF_MOV | BPF_K, \
82 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
83 #define BPF_LD_IMM64(DST, IMM) \
84 BPF_LD_IMM64_RAW(DST, 0, IMM)
86 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
87 ((struct bpf_insn) { \
88 .code = BPF_LD | BPF_DW | BPF_IMM, \
92 .imm = (__u32) (IMM) }), \
93 ((struct bpf_insn) { \
94 .code = 0, /* zero is reserved opcode */ \
98 .imm = ((__u64) (IMM)) >> 32 })
100 #ifndef BPF_PSEUDO_MAP_FD
101 # define BPF_PSEUDO_MAP_FD 1
104 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
105 #define BPF_LD_MAP_FD(DST, MAP_FD) \
106 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
109 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
111 #define BPF_LD_ABS(SIZE, IMM) \
112 ((struct bpf_insn) { \
113 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
119 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
121 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
122 ((struct bpf_insn) { \
123 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
129 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
131 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
132 ((struct bpf_insn) { \
133 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
139 /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
141 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
142 ((struct bpf_insn) { \
143 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
149 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
151 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
152 ((struct bpf_insn) { \
153 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
159 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
161 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
162 ((struct bpf_insn) { \
163 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
169 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
171 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
172 ((struct bpf_insn) { \
173 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
179 /* Raw code statement block */
181 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
182 ((struct bpf_insn) { \
191 #define BPF_EXIT_INSN() \
192 ((struct bpf_insn) { \
193 .code = BPF_JMP | BPF_EXIT, \