|
1 | 1 | /* Capstone Disassembly Engine */
|
2 | 2 | /* BPF Backend by david942j <[email protected]>, 2019 */
|
| 3 | +/* SPDX-FileCopyrightText: 2024 Roee Toledano <[email protected]> */ |
| 4 | +/* SPDX-License-Identifier: BSD-3 */ |
3 | 5 |
|
4 | 6 | /* This file defines constants and macros used for parsing a BPF instruction */
|
5 | 7 |
|
|
9 | 11 | #define BPF_CLASS(code) ((code) & 0x7)
|
10 | 12 |
|
11 | 13 | ///< Instruction classes
|
12 |
| -#define BPF_CLASS_LD 0x00 |
13 |
| -#define BPF_CLASS_LDX 0x01 |
14 |
| -#define BPF_CLASS_ST 0x02 |
15 |
| -#define BPF_CLASS_STX 0x03 |
16 |
| -#define BPF_CLASS_ALU 0x04 |
17 |
| -#define BPF_CLASS_JMP 0x05 |
18 |
| -#define BPF_CLASS_RET 0x06 ///< cBPF only |
19 |
| -#define BPF_CLASS_MISC 0x07 ///< cBPF only |
20 |
| -#define BPF_CLASS_ALU64 0x07 ///< eBPF only |
| 14 | +#define BPF_CLASS_LD 0x00 |
| 15 | +#define BPF_CLASS_LDX 0x01 |
| 16 | +#define BPF_CLASS_ST 0x02 |
| 17 | +#define BPF_CLASS_STX 0x03 |
| 18 | +#define BPF_CLASS_ALU 0x04 |
| 19 | +#define BPF_CLASS_JMP 0x05 |
| 20 | +#define BPF_CLASS_RET 0x06 ///< cBPF only |
| 21 | +#define BPF_CLASS_JMP32 0x06 ///< eBPF only |
| 22 | +#define BPF_CLASS_MISC 0x07 ///< cBPF only |
| 23 | +#define BPF_CLASS_ALU64 0x07 ///< eBPF only |
21 | 24 |
|
22 | 25 | #define BPF_OP(code) ((code) & 0xf0)
|
23 | 26 |
|
24 | 27 | ///< Types of ALU instruction
|
25 |
| -#define BPF_ALU_ADD 0x00 |
26 |
| -#define BPF_ALU_SUB 0x10 |
27 |
| -#define BPF_ALU_MUL 0x20 |
28 |
| -#define BPF_ALU_DIV 0x30 |
29 |
| -#define BPF_ALU_OR 0x40 |
30 |
| -#define BPF_ALU_AND 0x50 |
31 |
| -#define BPF_ALU_LSH 0x60 |
32 |
| -#define BPF_ALU_RSH 0x70 |
33 |
| -#define BPF_ALU_NEG 0x80 |
34 |
| -#define BPF_ALU_MOD 0x90 |
35 |
| -#define BPF_ALU_XOR 0xa0 |
36 |
| -#define BPF_ALU_MOV 0xb0 ///< eBPF only: mov reg to reg |
37 |
| -#define BPF_ALU_ARSH 0xc0 ///< eBPF only: sign extending shift right |
38 |
| -#define BPF_ALU_END 0xd0 ///< eBPF only: endianness conversion |
| 28 | +#define BPF_ALU_ADD 0x00 |
| 29 | +#define BPF_ALU_SUB 0x10 |
| 30 | +#define BPF_ALU_MUL 0x20 |
| 31 | +#define BPF_ALU_DIV 0x30 |
| 32 | +#define BPF_ALU_OR 0x40 |
| 33 | +#define BPF_ALU_AND 0x50 |
| 34 | +#define BPF_ALU_LSH 0x60 |
| 35 | +#define BPF_ALU_RSH 0x70 |
| 36 | +#define BPF_ALU_NEG 0x80 |
| 37 | +#define BPF_ALU_MOD 0x90 |
| 38 | +#define BPF_ALU_XOR 0xa0 |
| 39 | +#define BPF_ALU_MOV 0xb0 ///< eBPF only: mov reg to reg |
| 40 | +#define BPF_ALU_ARSH 0xc0 ///< eBPF only: sign extending shift right |
| 41 | +#define BPF_ALU_END 0xd0 ///< eBPF only: endianness conversion |
39 | 42 |
|
40 | 43 | ///< Types of jmp instruction
|
41 |
| -#define BPF_JUMP_JA 0x00 ///< goto |
42 |
| -#define BPF_JUMP_JEQ 0x10 ///< '==' |
43 |
| -#define BPF_JUMP_JGT 0x20 ///< unsigned '>' |
44 |
| -#define BPF_JUMP_JGE 0x30 ///< unsigned '>=' |
45 |
| -#define BPF_JUMP_JSET 0x40 ///< '&' |
46 |
| -#define BPF_JUMP_JNE 0x50 ///< eBPF only: '!=' */ |
47 |
| -#define BPF_JUMP_JSGT 0x60 ///< eBPF only: signed '>' |
48 |
| -#define BPF_JUMP_JSGE 0x70 ///< eBPF only: signed '>=' |
49 |
| -#define BPF_JUMP_CALL 0x80 ///< eBPF only: function call |
50 |
| -#define BPF_JUMP_EXIT 0x90 ///< eBPF only: exit |
51 |
| -#define BPF_JUMP_JLT 0xa0 ///< eBPF only: unsigned '<' |
52 |
| -#define BPF_JUMP_JLE 0xb0 ///< eBPF only: unsigned '<=' |
53 |
| -#define BPF_JUMP_JSLT 0xc0 ///< eBPF only: signed '<' |
54 |
| -#define BPF_JUMP_JSLE 0xd0 ///< eBPF only: signed '<=' |
| 44 | +#define BPF_JUMP_JA 0x00 ///< goto |
| 45 | +#define BPF_JUMP_JEQ 0x10 ///< '==' |
| 46 | +#define BPF_JUMP_JGT 0x20 ///< unsigned '>' |
| 47 | +#define BPF_JUMP_JGE 0x30 ///< unsigned '>=' |
| 48 | +#define BPF_JUMP_JSET 0x40 ///< '&' |
| 49 | +#define BPF_JUMP_JNE 0x50 ///< eBPF only: '!=' */ |
| 50 | +#define BPF_JUMP_JSGT 0x60 ///< eBPF only: signed '>' |
| 51 | +#define BPF_JUMP_JSGE 0x70 ///< eBPF only: signed '>=' |
| 52 | +#define BPF_JUMP_CALL 0x80 ///< eBPF only: function call |
| 53 | +#define BPF_JUMP_EXIT 0x90 ///< eBPF only: exit |
| 54 | +#define BPF_JUMP_JLT 0xa0 ///< eBPF only: unsigned '<' |
| 55 | +#define BPF_JUMP_JLE 0xb0 ///< eBPF only: unsigned '<=' |
| 56 | +#define BPF_JUMP_JSLT 0xc0 ///< eBPF only: signed '<' |
| 57 | +#define BPF_JUMP_JSLE 0xd0 ///< eBPF only: signed '<=' |
| 58 | + |
| 59 | +///< Types of complex atomic instructions |
| 60 | +#define BPF_ATOMIC_XCHG 0xe0 ///< eBPF only: atomic exchange |
| 61 | +#define BPF_ATOMIC_CMPXCHG 0xf0 ///< eBPF only: atomic compare and exchange |
55 | 62 |
|
56 | 63 | #define BPF_SRC(code) ((code) & 0x08)
|
57 | 64 | #define BPF_RVAL(code) ((code) & 0x18) /* cBPF only: for return types */
|
58 | 65 | ///< Source operand
|
59 |
| -#define BPF_SRC_K 0x00 |
60 |
| -#define BPF_SRC_X 0x08 |
61 |
| -#define BPF_SRC_A 0x10 /* cBPF only */ |
| 66 | +#define BPF_SRC_K 0x00 |
| 67 | +#define BPF_SRC_X 0x08 |
| 68 | +#define BPF_SRC_A 0x10 /* cBPF only */ |
62 | 69 |
|
63 |
| -#define BPF_SRC_LITTLE BPF_SRC_K |
64 |
| -#define BPF_SRC_BIG BPF_SRC_X |
| 70 | +#define BPF_SRC_LITTLE BPF_SRC_K |
| 71 | +#define BPF_SRC_BIG BPF_SRC_X |
65 | 72 |
|
66 | 73 | #define BPF_SIZE(code) ((code) & 0x18)
|
67 | 74 | ///< Size modifier
|
68 |
| -#define BPF_SIZE_W 0x00 ///< word |
69 |
| -#define BPF_SIZE_H 0x08 ///< half word |
70 |
| -#define BPF_SIZE_B 0x10 ///< byte |
71 |
| -#define BPF_SIZE_DW 0x18 ///< eBPF only: double word |
| 75 | +#define BPF_SIZE_W 0x00 ///< word |
| 76 | +#define BPF_SIZE_H 0x08 ///< half word |
| 77 | +#define BPF_SIZE_B 0x10 ///< byte |
| 78 | +#define BPF_SIZE_DW 0x18 ///< eBPF only: double word |
72 | 79 |
|
73 | 80 | #define BPF_MODE(code) ((code) & 0xe0)
|
74 | 81 | ///< Mode modifier
|
75 |
| -#define BPF_MODE_IMM 0x00 ///< used for 32-bit mov in cBPF and 64-bit in eBPF |
76 |
| -#define BPF_MODE_ABS 0x20 |
77 |
| -#define BPF_MODE_IND 0x40 |
78 |
| -#define BPF_MODE_MEM 0x60 |
79 |
| -#define BPF_MODE_LEN 0x80 ///< cBPF only, reserved in eBPF |
80 |
| -#define BPF_MODE_MSH 0xa0 ///< cBPF only, reserved in eBPF |
81 |
| -#define BPF_MODE_XADD 0xc0 ///< eBPF only: exclusive add |
| 82 | +#define BPF_MODE_IMM 0x00 ///< used for 32-bit mov in cBPF and 64-bit in eBPF |
| 83 | +#define BPF_MODE_ABS \ |
| 84 | + 0x20 ///< absolute indexing of socket buffer. eBPF only, but deprecated in new versions |
| 85 | +#define BPF_MODE_IND \ |
| 86 | + 0x40 ///< indirect indexing of socket buffer. eBPF only, but deprecated in new versions |
| 87 | +#define BPF_MODE_MEM 0x60 |
| 88 | +#define BPF_MODE_LEN 0x80 ///< cBPF only, reserved in eBPF |
| 89 | +#define BPF_MODE_MSH 0xa0 ///< cBPF only, reserved in eBPF |
| 90 | +#define BPF_MODE_ATOMIC \ |
| 91 | + 0xc0 ///< eBPF only: atomic operations. Originally BPF_MODE_XADD |
| 92 | + |
| 93 | +#define BPF_MODE_FETCH \ |
| 94 | + 0x01 /* eBPF only: overwrite 'src' with what was in the modified mem address before it was modified. |
| 95 | + NOTE: in contrast to regular modifiers, this one is encoded in the 'imm' field, not opcode! |
| 96 | + Must be used for BPF_XCHG and BPF_CMPXCHG. Optional for the other atomic operations. */ |
82 | 97 |
|
83 | 98 | #define BPF_MISCOP(code) ((code) & 0x80)
|
84 | 99 | ///< Operation of misc
|
85 |
| -#define BPF_MISCOP_TAX 0x00 |
86 |
| -#define BPF_MISCOP_TXA 0x80 |
| 100 | +#define BPF_MISCOP_TAX 0x00 |
| 101 | +#define BPF_MISCOP_TXA 0x80 |
87 | 102 |
|
88 | 103 | #endif
|
0 commit comments