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