xref: /netbsd-src/external/gpl3/binutils/dist/include/opcode/bpf.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1*cb63e24eSchristos /* bpf.h - BPF opcode list for binutils.
2*cb63e24eSchristos    Copyright (C) 2023-2024 Free Software Foundation, Inc.
3*cb63e24eSchristos 
4*cb63e24eSchristos    Contributed by Oracle Inc.
5*cb63e24eSchristos 
6*cb63e24eSchristos    This file is part of the GNU binutils.
7*cb63e24eSchristos 
8*cb63e24eSchristos    This is free software; you can redistribute them and/or modify them
9*cb63e24eSchristos    under the terms of the GNU General Public License as published by
10*cb63e24eSchristos    the Free Software Foundation; either version 3, or (at your option)
11*cb63e24eSchristos    any later version.
12*cb63e24eSchristos 
13*cb63e24eSchristos    This program is distributed in the hope that it will be useful, but
14*cb63e24eSchristos    WITHOUT ANY WARRANTY; without even the implied warranty of
15*cb63e24eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*cb63e24eSchristos    General Public License for more details.
17*cb63e24eSchristos 
18*cb63e24eSchristos    You should have received a copy of the GNU General Public License
19*cb63e24eSchristos    along with this program; see the file COPYING3. If not,
20*cb63e24eSchristos    see <http://www.gnu.org/licenses/>.  */
21*cb63e24eSchristos 
22*cb63e24eSchristos #ifndef _BPF_H_
23*cb63e24eSchristos #define _BPF_H_
24*cb63e24eSchristos 
25*cb63e24eSchristos #include <stdint.h>
26*cb63e24eSchristos 
27*cb63e24eSchristos /* The BPF ISA has little-endian and big-endian variants.  */
28*cb63e24eSchristos 
29*cb63e24eSchristos enum bpf_endian
30*cb63e24eSchristos {
31*cb63e24eSchristos   BPF_ENDIAN_LITTLE,
32*cb63e24eSchristos   BPF_ENDIAN_BIG
33*cb63e24eSchristos };
34*cb63e24eSchristos 
35*cb63e24eSchristos /* Most BPF instructions are conformed by a single 64-bit instruction
36*cb63e24eSchristos    word.  The lddw instruction is conformed by two consecutive 64-bit
37*cb63e24eSchristos    instruction words.  */
38*cb63e24eSchristos 
39*cb63e24eSchristos typedef uint64_t bpf_insn_word;
40*cb63e24eSchristos 
41*cb63e24eSchristos /* There are several versions of the BPF ISA.  */
42*cb63e24eSchristos 
43*cb63e24eSchristos #define BPF_V1 0x1
44*cb63e24eSchristos #define BPF_V2 0x2
45*cb63e24eSchristos #define BPF_V3 0x3
46*cb63e24eSchristos #define BPF_V4 0x4
47*cb63e24eSchristos #define BPF_XBPF 0xf
48*cb63e24eSchristos 
49*cb63e24eSchristos /* Masks for the several instruction fields in a BPF instruction.
50*cb63e24eSchristos    These assume big-endian BPF instructions.  */
51*cb63e24eSchristos 
52*cb63e24eSchristos #define BPF_CODE     0xff00000000000000UL
53*cb63e24eSchristos #define BPF_REGS     0x00ff000000000000UL
54*cb63e24eSchristos #define BPF_DST      0x00f0000000000000UL
55*cb63e24eSchristos #define BPF_SRC      0x000f000000000000UL
56*cb63e24eSchristos #define BPF_OFFSET16 0x0000ffff00000000UL
57*cb63e24eSchristos #define BPF_IMM32    0x00000000ffffffffUL
58*cb63e24eSchristos 
59*cb63e24eSchristos /* The BPF opcode instruction field is eight bits long and its
60*cb63e24eSchristos    interpretation depends on the instruction class.
61*cb63e24eSchristos 
62*cb63e24eSchristos    For arithmetic and jump instructions the 8-bit opcode field is
63*cb63e24eSchristos    subdivided in:
64*cb63e24eSchristos 
65*cb63e24eSchristos      op-code:4 op-src:1 op-class:3
66*cb63e24eSchristos 
67*cb63e24eSchristos    For load/store instructions, the 8-bit opcode field is subdivided
68*cb63e24eSchristos    in:
69*cb63e24eSchristos 
70*cb63e24eSchristos      op-mode:3 op-size:2 op-class:3
71*cb63e24eSchristos 
72*cb63e24eSchristos    All the constants defined below are to be applied on the first
73*cb63e24eSchristos    64-bit word of a BPF instruction.  Please define them assuming
74*cb63e24eSchristos    big-endian instructions; the matching and writing routines using
75*cb63e24eSchristos    the instruction table know how to handle the endianness groups.  */
76*cb63e24eSchristos 
77*cb63e24eSchristos #define BPF_SRC_X ((uint64_t)0x08 << 56)
78*cb63e24eSchristos #define BPF_SRC_K ((uint64_t)0x00 << 56)
79*cb63e24eSchristos 
80*cb63e24eSchristos #define BPF_CODE_ADD ((uint64_t)0x00 << 56)
81*cb63e24eSchristos #define BPF_CODE_SUB ((uint64_t)0x10 << 56)
82*cb63e24eSchristos #define BPF_CODE_MUL ((uint64_t)0x20 << 56)
83*cb63e24eSchristos #define BPF_CODE_DIV ((uint64_t)0x30 << 56)
84*cb63e24eSchristos #define BPF_CODE_OR  ((uint64_t)0x40 << 56)
85*cb63e24eSchristos #define BPF_CODE_AND ((uint64_t)0x50 << 56)
86*cb63e24eSchristos #define BPF_CODE_LSH ((uint64_t)0x60 << 56)
87*cb63e24eSchristos #define BPF_CODE_RSH ((uint64_t)0x70 << 56)
88*cb63e24eSchristos #define BPF_CODE_NEG ((uint64_t)0x80 << 56)
89*cb63e24eSchristos #define BPF_CODE_MOD ((uint64_t)0x90 << 56)
90*cb63e24eSchristos #define BPF_CODE_XOR ((uint64_t)0xa0 << 56)
91*cb63e24eSchristos #define BPF_CODE_MOV ((uint64_t)0xb0 << 56)
92*cb63e24eSchristos #define BPF_CODE_ARSH ((uint64_t)0xc0 << 56)
93*cb63e24eSchristos #define BPF_CODE_END ((uint64_t)0xd0 << 56)
94*cb63e24eSchristos 
95*cb63e24eSchristos #define BPF_CODE_JA   ((uint64_t)0x00 << 56)
96*cb63e24eSchristos #define BPF_CODE_JEQ  ((uint64_t)0x10 << 56)
97*cb63e24eSchristos #define BPF_CODE_JGT  ((uint64_t)0x20 << 56)
98*cb63e24eSchristos #define BPF_CODE_JGE  ((uint64_t)0x30 << 56)
99*cb63e24eSchristos #define BPF_CODE_JSET ((uint64_t)0x40 << 56)
100*cb63e24eSchristos #define BPF_CODE_JNE  ((uint64_t)0x50 << 56)
101*cb63e24eSchristos #define BPF_CODE_JSGT ((uint64_t)0x60 << 56)
102*cb63e24eSchristos #define BPF_CODE_JSGE ((uint64_t)0x70 << 56)
103*cb63e24eSchristos #define BPF_CODE_CALL ((uint64_t)0x80 << 56)
104*cb63e24eSchristos #define BPF_CODE_EXIT ((uint64_t)0x90 << 56)
105*cb63e24eSchristos #define BPF_CODE_JLT  ((uint64_t)0xa0 << 56)
106*cb63e24eSchristos #define BPF_CODE_JLE  ((uint64_t)0xb0 << 56)
107*cb63e24eSchristos #define BPF_CODE_JSLT ((uint64_t)0xc0 << 56)
108*cb63e24eSchristos #define BPF_CODE_JSLE ((uint64_t)0xd0 << 56)
109*cb63e24eSchristos 
110*cb63e24eSchristos #define BPF_MODE_IMM  ((uint64_t)0x00 << 56)
111*cb63e24eSchristos #define BPF_MODE_ABS  ((uint64_t)0x20 << 56)
112*cb63e24eSchristos #define BPF_MODE_IND  ((uint64_t)0x40 << 56)
113*cb63e24eSchristos #define BPF_MODE_MEM  ((uint64_t)0x60 << 56)
114*cb63e24eSchristos #define BPF_MODE_ATOMIC ((uint64_t)0xc0 << 56)
115*cb63e24eSchristos #define BPF_MODE_SMEM ((uint64_t)0x80 << 56)
116*cb63e24eSchristos 
117*cb63e24eSchristos #define BPF_SIZE_W  ((uint64_t)0x00 << 56)
118*cb63e24eSchristos #define BPF_SIZE_H  ((uint64_t)0x08 << 56)
119*cb63e24eSchristos #define BPF_SIZE_B  ((uint64_t)0x10 << 56)
120*cb63e24eSchristos #define BPF_SIZE_DW ((uint64_t)0x18 << 56)
121*cb63e24eSchristos 
122*cb63e24eSchristos #define BPF_CLASS_LD    ((uint64_t)0x00 << 56)
123*cb63e24eSchristos #define BPF_CLASS_LDX   ((uint64_t)0x01 << 56)
124*cb63e24eSchristos #define BPF_CLASS_ST    ((uint64_t)0x02 << 56)
125*cb63e24eSchristos #define BPF_CLASS_STX   ((uint64_t)0x03 << 56)
126*cb63e24eSchristos #define BPF_CLASS_ALU   ((uint64_t)0x04 << 56)
127*cb63e24eSchristos #define BPF_CLASS_JMP   ((uint64_t)0x05 << 56)
128*cb63e24eSchristos #define BPF_CLASS_JMP32 ((uint64_t)0x06 << 56)
129*cb63e24eSchristos #define BPF_CLASS_ALU64 ((uint64_t)0x07 << 56)
130*cb63e24eSchristos 
131*cb63e24eSchristos /* Certain instructions (ab)use other instruction fields as opcodes,
132*cb63e24eSchristos    even if these are multi-byte or infra-byte.  Bleh.  */
133*cb63e24eSchristos 
134*cb63e24eSchristos #define BPF_OFFSET16_SDIVMOD ((uint64_t)0x1 << 32)
135*cb63e24eSchristos #define BPF_OFFSET16_MOVS8 ((uint64_t)8 << 32)
136*cb63e24eSchristos #define BPF_OFFSET16_MOVS16 ((uint64_t)16 << 32)
137*cb63e24eSchristos #define BPF_OFFSET16_MOVS32 ((uint64_t)32 << 32)
138*cb63e24eSchristos 
139*cb63e24eSchristos #define BPF_IMM32_END16 ((uint64_t)0x00000010)
140*cb63e24eSchristos #define BPF_IMM32_END32 ((uint64_t)0x00000020)
141*cb63e24eSchristos #define BPF_IMM32_END64 ((uint64_t)0x00000040)
142*cb63e24eSchristos 
143*cb63e24eSchristos #define BPF_IMM32_BSWAP16 ((uint64_t)0x00000010)
144*cb63e24eSchristos #define BPF_IMM32_BSWAP32 ((uint64_t)0x00000020)
145*cb63e24eSchristos #define BPF_IMM32_BSWAP64 ((uint64_t)0x00000040)
146*cb63e24eSchristos 
147*cb63e24eSchristos #define BPF_IMM32_AADD ((uint64_t)0x00000000)
148*cb63e24eSchristos #define BPF_IMM32_AOR  ((uint64_t)0x00000040)
149*cb63e24eSchristos #define BPF_IMM32_AAND ((uint64_t)0x00000050)
150*cb63e24eSchristos #define BPF_IMM32_AXOR ((uint64_t)0x000000a0)
151*cb63e24eSchristos #define BPF_IMM32_AFADD ((uint64_t)0x00000001)
152*cb63e24eSchristos #define BPF_IMM32_AFOR  ((uint64_t)0x00000041)
153*cb63e24eSchristos #define BPF_IMM32_AFAND ((uint64_t)0x00000051)
154*cb63e24eSchristos #define BPF_IMM32_AFXOR ((uint64_t)0x000000a1)
155*cb63e24eSchristos #define BPF_IMM32_AXCHG ((uint64_t)0x000000e1)
156*cb63e24eSchristos #define BPF_IMM32_ACMP  ((uint64_t)0x000000f1)
157*cb63e24eSchristos 
158*cb63e24eSchristos /* Unique identifiers for BPF instructions.  */
159*cb63e24eSchristos 
160*cb63e24eSchristos enum bpf_insn_id
161*cb63e24eSchristos {
162*cb63e24eSchristos   BPF_NOINSN = 0,
163*cb63e24eSchristos   /* 64-bit load instruction.  */
164*cb63e24eSchristos   BPF_INSN_LDDW,
165*cb63e24eSchristos   /* ALU instructions.  */
166*cb63e24eSchristos   BPF_INSN_ADDR, BPF_INSN_ADDI, BPF_INSN_SUBR, BPF_INSN_SUBI,
167*cb63e24eSchristos   BPF_INSN_MULR, BPF_INSN_MULI, BPF_INSN_SDIVR, BPF_INSN_SDIVI,
168*cb63e24eSchristos   BPF_INSN_SMODR, BPF_INSN_SMODI, BPF_INSN_DIVR, BPF_INSN_DIVI,
169*cb63e24eSchristos   BPF_INSN_MODR, BPF_INSN_MODI, BPF_INSN_ORR, BPF_INSN_ORI,
170*cb63e24eSchristos   BPF_INSN_ANDR, BPF_INSN_ANDI, BPF_INSN_XORR, BPF_INSN_XORI,
171*cb63e24eSchristos   BPF_INSN_NEGR, BPF_INSN_LSHR, BPF_INSN_LSHI,
172*cb63e24eSchristos   BPF_INSN_RSHR, BPF_INSN_RSHI, BPF_INSN_ARSHR, BPF_INSN_ARSHI,
173*cb63e24eSchristos   BPF_INSN_MOVS8R, BPF_INSN_MOVS16R, BPF_INSN_MOVS32R,
174*cb63e24eSchristos   BPF_INSN_MOVR, BPF_INSN_MOVI,
175*cb63e24eSchristos   /* ALU32 instructions.  */
176*cb63e24eSchristos   BPF_INSN_ADD32R, BPF_INSN_ADD32I, BPF_INSN_SUB32R, BPF_INSN_SUB32I,
177*cb63e24eSchristos   BPF_INSN_MUL32R, BPF_INSN_MUL32I, BPF_INSN_SDIV32R, BPF_INSN_SDIV32I,
178*cb63e24eSchristos   BPF_INSN_SMOD32R, BPF_INSN_SMOD32I, BPF_INSN_DIV32R, BPF_INSN_DIV32I,
179*cb63e24eSchristos   BPF_INSN_MOD32R, BPF_INSN_MOD32I, BPF_INSN_OR32R, BPF_INSN_OR32I,
180*cb63e24eSchristos   BPF_INSN_AND32R, BPF_INSN_AND32I, BPF_INSN_XOR32R, BPF_INSN_XOR32I,
181*cb63e24eSchristos   BPF_INSN_NEG32R, BPF_INSN_LSH32R, BPF_INSN_LSH32I,
182*cb63e24eSchristos   BPF_INSN_RSH32R, BPF_INSN_RSH32I, BPF_INSN_ARSH32R, BPF_INSN_ARSH32I,
183*cb63e24eSchristos   BPF_INSN_MOVS328R, BPF_INSN_MOVS3216R, BPF_INSN_MOVS3232R,
184*cb63e24eSchristos   BPF_INSN_MOV32R, BPF_INSN_MOV32I,
185*cb63e24eSchristos   /* Byte swap instructions.  */
186*cb63e24eSchristos   BPF_INSN_BSWAP16, BPF_INSN_BSWAP32, BPF_INSN_BSWAP64,
187*cb63e24eSchristos   /* Endianness conversion instructions.  */
188*cb63e24eSchristos   BPF_INSN_ENDLE16, BPF_INSN_ENDLE32, BPF_INSN_ENDLE64,
189*cb63e24eSchristos   BPF_INSN_ENDBE16, BPF_INSN_ENDBE32, BPF_INSN_ENDBE64,
190*cb63e24eSchristos   /* Absolute load instructions.  */
191*cb63e24eSchristos   BPF_INSN_LDABSB, BPF_INSN_LDABSH, BPF_INSN_LDABSW, BPF_INSN_LDABSDW,
192*cb63e24eSchristos   /* Indirect load instructions.  */
193*cb63e24eSchristos   BPF_INSN_LDINDB, BPF_INSN_LDINDH, BPF_INSN_LDINDW, BPF_INSN_LDINDDW,
194*cb63e24eSchristos   /* Generic load instructions (to register.)  */
195*cb63e24eSchristos   BPF_INSN_LDXB, BPF_INSN_LDXH, BPF_INSN_LDXW, BPF_INSN_LDXDW,
196*cb63e24eSchristos   /* Generic signed load instructions.  */
197*cb63e24eSchristos   BPF_INSN_LDXSB, BPF_INSN_LDXSH, BPF_INSN_LDXSW, BPF_INSN_LDXSDW,
198*cb63e24eSchristos   /* Generic store instructions (from register.)  */
199*cb63e24eSchristos   BPF_INSN_STXBR, BPF_INSN_STXHR, BPF_INSN_STXWR, BPF_INSN_STXDWR,
200*cb63e24eSchristos   BPF_INSN_STXBI, BPF_INSN_STXHI, BPF_INSN_STXWI, BPF_INSN_STXDWI,
201*cb63e24eSchristos   /* Compare-and-jump instructions (reg OP reg.)  */
202*cb63e24eSchristos   BPF_INSN_JAR, BPF_INSN_JEQR, BPF_INSN_JGTR, BPF_INSN_JSGTR,
203*cb63e24eSchristos   BPF_INSN_JGER, BPF_INSN_JSGER, BPF_INSN_JLTR, BPF_INSN_JSLTR,
204*cb63e24eSchristos   BPF_INSN_JSLER, BPF_INSN_JLER, BPF_INSN_JSETR, BPF_INSN_JNER,
205*cb63e24eSchristos   BPF_INSN_CALLR, BPF_INSN_CALL, BPF_INSN_EXIT,
206*cb63e24eSchristos   /* Compare-and-jump instructions (reg OP imm.)  */
207*cb63e24eSchristos   BPF_INSN_JEQI, BPF_INSN_JGTI, BPF_INSN_JSGTI,
208*cb63e24eSchristos   BPF_INSN_JGEI, BPF_INSN_JSGEI, BPF_INSN_JLTI, BPF_INSN_JSLTI,
209*cb63e24eSchristos   BPF_INSN_JSLEI, BPF_INSN_JLEI, BPF_INSN_JSETI, BPF_INSN_JNEI,
210*cb63e24eSchristos   /* jump-always with 32-bit offset.  */
211*cb63e24eSchristos   BPF_INSN_JAL,
212*cb63e24eSchristos   /* 32-bit compare-and-jump instructions (reg OP reg.)  */
213*cb63e24eSchristos   BPF_INSN_JEQ32R, BPF_INSN_JGT32R, BPF_INSN_JSGT32R,
214*cb63e24eSchristos   BPF_INSN_JGE32R, BPF_INSN_JSGE32R, BPF_INSN_JLT32R, BPF_INSN_JSLT32R,
215*cb63e24eSchristos   BPF_INSN_JSLE32R, BPF_INSN_JLE32R, BPF_INSN_JSET32R, BPF_INSN_JNE32R,
216*cb63e24eSchristos   /* 32-bit compare-and-jump instructions (reg OP imm.)  */
217*cb63e24eSchristos   BPF_INSN_JEQ32I, BPF_INSN_JGT32I, BPF_INSN_JSGT32I,
218*cb63e24eSchristos   BPF_INSN_JGE32I, BPF_INSN_JSGE32I, BPF_INSN_JLT32I, BPF_INSN_JSLT32I,
219*cb63e24eSchristos   BPF_INSN_JSLE32I, BPF_INSN_JLE32I, BPF_INSN_JSET32I, BPF_INSN_JNE32I,
220*cb63e24eSchristos   /* Atomic instructions.  */
221*cb63e24eSchristos   BPF_INSN_AADD, BPF_INSN_AOR, BPF_INSN_AAND, BPF_INSN_AXOR,
222*cb63e24eSchristos   /* Atomic instructions with fetching.  */
223*cb63e24eSchristos   BPF_INSN_AFADD, BPF_INSN_AFOR, BPF_INSN_AFAND, BPF_INSN_AFXOR,
224*cb63e24eSchristos   /* Atomic instructions (32-bit.)  */
225*cb63e24eSchristos   BPF_INSN_AADD32, BPF_INSN_AOR32, BPF_INSN_AAND32, BPF_INSN_AXOR32,
226*cb63e24eSchristos   /* Atomic instructions with fetching (32-bit.)  */
227*cb63e24eSchristos   BPF_INSN_AFADD32, BPF_INSN_AFOR32, BPF_INSN_AFAND32, BPF_INSN_AFXOR32,
228*cb63e24eSchristos   /* Atomic compare-and-swap, atomic exchange.  */
229*cb63e24eSchristos   BPF_INSN_ACMP, BPF_INSN_AXCHG,
230*cb63e24eSchristos   /* Atomic compare-and-swap, atomic exchange (32-bit).  */
231*cb63e24eSchristos   BPF_INSN_ACMP32, BPF_INSN_AXCHG32,
232*cb63e24eSchristos   /* GNU simulator specific instruction.  */
233*cb63e24eSchristos   BPF_INSN_BRKPT,
234*cb63e24eSchristos };
235*cb63e24eSchristos 
236*cb63e24eSchristos /* Entry for a BPF instruction in the opcodes table.  */
237*cb63e24eSchristos 
238*cb63e24eSchristos struct bpf_opcode
239*cb63e24eSchristos {
240*cb63e24eSchristos   /* Unique numerical code for the instruction.  */
241*cb63e24eSchristos   enum bpf_insn_id id;
242*cb63e24eSchristos 
243*cb63e24eSchristos   /* The instruction template defines both the syntax of the
244*cb63e24eSchristos      instruction and the set of the different operands that appear in
245*cb63e24eSchristos      the instruction.
246*cb63e24eSchristos 
247*cb63e24eSchristos      Tags:
248*cb63e24eSchristos      %% - literal %.
249*cb63e24eSchristos      %dr - destination 64-bit register.
250*cb63e24eSchristos      %dw - destination 32-bit register.
251*cb63e24eSchristos      %sr - source 64-bit register.
252*cb63e24eSchristos      %sw - source 32-bit register.
253*cb63e24eSchristos      %d32 - 32-bit signed displacement (in 64-bit words minus one.)
254*cb63e24eSchristos      %d16 - 16-bit signed displacement (in 64-bit words minus one.)
255*cb63e24eSchristos      %o16 - 16-bit signed offset (in bytes.)
256*cb63e24eSchristos      %i32 - 32-bit signed immediate.
257*cb63e24eSchristos      %I32 - Like %i32.
258*cb63e24eSchristos      %i64 - 64-bit signed immediate.
259*cb63e24eSchristos      %w - expect zero or more white spaces and print a single space.
260*cb63e24eSchristos      %W - expect one or more white spaces and print a single space.
261*cb63e24eSchristos 
262*cb63e24eSchristos      When parsing and printing %o16 and %I32 (but not %i32) an
263*cb63e24eSchristos      explicit sign is always expected and included.  Therefore, to
264*cb63e24eSchristos      denote something like `[%r3 + 10]', please use a template like `[
265*cb63e24eSchristos      %sr %o16]' instead of `[ %sr + %o16 ]'.
266*cb63e24eSchristos 
267*cb63e24eSchristos      If %dr, %dw, %sr or %sw are found multiple times in a template,
268*cb63e24eSchristos      they refer to the same register, i.e. `%rd = le64 %rd' denotes
269*cb63e24eSchristos      `r2 = le64 r2', but not `r2 = le64 r1'.
270*cb63e24eSchristos 
271*cb63e24eSchristos      If %i64 appears in a template then the instruction is 128-bits
272*cb63e24eSchristos      long and composed by two consecutive 64-bit instruction words.
273*cb63e24eSchristos 
274*cb63e24eSchristos      A white space character means to expect zero or more white
275*cb63e24eSchristos      spaces, and to print no space.
276*cb63e24eSchristos 
277*cb63e24eSchristos      There are two templates defined per instruction, corresponding to
278*cb63e24eSchristos      two used different dialects: a "normal" assembly-like syntax and
279*cb63e24eSchristos      a "pseudo-c" syntax.  Some toolchains support just one of these
280*cb63e24eSchristos      dialects.  The GNU Toolchain supports both.  */
281*cb63e24eSchristos   const char *normal;
282*cb63e24eSchristos   const char *pseudoc;
283*cb63e24eSchristos 
284*cb63e24eSchristos   /* The version that introduced this instruction.  Instructions are
285*cb63e24eSchristos      generally not removed once they get introduced.  */
286*cb63e24eSchristos   uint8_t version;
287*cb63e24eSchristos 
288*cb63e24eSchristos   /* Maks marking the opcode fields in the instruction, and the
289*cb63e24eSchristos      opcodes characterizing it.
290*cb63e24eSchristos 
291*cb63e24eSchristos      In multi-word instructions these apply to the first word in the
292*cb63e24eSchristos      instruction.  Note that these values assumes big-endian
293*cb63e24eSchristos      instructions; code using these field must be aware of the
294*cb63e24eSchristos      endianness groups to which BPF instructions must conform to and
295*cb63e24eSchristos      DTRT.  */
296*cb63e24eSchristos   bpf_insn_word mask;
297*cb63e24eSchristos   bpf_insn_word opcode;
298*cb63e24eSchristos };
299*cb63e24eSchristos 
300*cb63e24eSchristos /* Try to match a BPF instruction given its first instruction word.
301*cb63e24eSchristos    If no matching instruction is found, return NULL.  */
302*cb63e24eSchristos 
303*cb63e24eSchristos const struct bpf_opcode *bpf_match_insn (bpf_insn_word word,
304*cb63e24eSchristos                                          enum bpf_endian endian,
305*cb63e24eSchristos                                          int version);
306*cb63e24eSchristos 
307*cb63e24eSchristos /* Operand extractors.
308*cb63e24eSchristos 
309*cb63e24eSchristos    These all get big-endian instruction words.  Note how the extractor
310*cb63e24eSchristos    for 64-bit signed immediates requires two instruction words.  */
311*cb63e24eSchristos 
312*cb63e24eSchristos uint8_t bpf_extract_src (bpf_insn_word word, enum bpf_endian endian);
313*cb63e24eSchristos uint8_t bpf_extract_dst (bpf_insn_word word, enum bpf_endian endian);
314*cb63e24eSchristos int16_t bpf_extract_offset16 (bpf_insn_word word, enum bpf_endian endian);
315*cb63e24eSchristos int32_t bpf_extract_imm32 (bpf_insn_word word, enum bpf_endian endian);
316*cb63e24eSchristos int64_t bpf_extract_imm64 (bpf_insn_word word1, bpf_insn_word word2,
317*cb63e24eSchristos                            enum bpf_endian endian);
318*cb63e24eSchristos 
319*cb63e24eSchristos /* Get the opcode occupying the INDEX position in the opcodes table.
320*cb63e24eSchristos    The INDEX is zero based.  If the provided index overflows the
321*cb63e24eSchristos    opcodes table then NULL is returned.  */
322*cb63e24eSchristos 
323*cb63e24eSchristos const struct bpf_opcode *bpf_get_opcode (unsigned int index);
324*cb63e24eSchristos 
325*cb63e24eSchristos #endif /* !_BPF_H_ */
326