1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* eBPF instruction mini library */
8 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
10 #define BPF_ALU64_REG(OP, DST, SRC) \
11 ((struct bpf_insn) { \
12 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
18 #define BPF_ALU32_REG(OP, DST, SRC) \
19 ((struct bpf_insn) { \
20 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
26 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
28 #define BPF_ALU64_IMM(OP, DST, IMM) \
29 ((struct bpf_insn) { \
30 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
36 #define BPF_ALU32_IMM(OP, DST, IMM) \
37 ((struct bpf_insn) { \
38 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
44 /* Short form of mov, dst_reg = src_reg */
46 #define BPF_MOV64_REG(DST, SRC) \
47 ((struct bpf_insn) { \
48 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
54 #define BPF_MOV32_REG(DST, SRC) \
55 ((struct bpf_insn) { \
56 .code = BPF_ALU | BPF_MOV | BPF_X, \
62 /* Short form of mov, dst_reg = imm32 */
64 #define BPF_MOV64_IMM(DST, IMM) \
65 ((struct bpf_insn) { \
66 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
72 #define BPF_MOV32_IMM(DST, IMM) \
73 ((struct bpf_insn) { \
74 .code = BPF_ALU | BPF_MOV | BPF_K, \
80 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
81 #define BPF_LD_IMM64(DST, IMM) \
82 BPF_LD_IMM64_RAW(DST, 0, IMM)
84 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
85 ((struct bpf_insn) { \
86 .code = BPF_LD | BPF_DW | BPF_IMM, \
90 .imm = (__u32) (IMM) }), \
91 ((struct bpf_insn) { \
92 .code = 0, /* zero is reserved opcode */ \
96 .imm = ((__u64) (IMM)) >> 32 })
98 #ifndef BPF_PSEUDO_MAP_FD
99 # define BPF_PSEUDO_MAP_FD 1
102 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
103 #define BPF_LD_MAP_FD(DST, MAP_FD) \
104 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
107 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
109 #define BPF_LD_ABS(SIZE, IMM) \
110 ((struct bpf_insn) { \
111 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
117 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
119 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
120 ((struct bpf_insn) { \
121 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
127 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
129 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
130 ((struct bpf_insn) { \
131 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
137 /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
139 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
140 ((struct bpf_insn) { \
141 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
147 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
149 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
150 ((struct bpf_insn) { \
151 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
157 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
159 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
160 ((struct bpf_insn) { \
161 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
167 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
169 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
170 ((struct bpf_insn) { \
171 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
177 /* Raw code statement block */
179 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
180 ((struct bpf_insn) { \
189 #define BPF_EXIT_INSN() \
190 ((struct bpf_insn) { \
191 .code = BPF_JMP | BPF_EXIT, \