OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * Data structures for decoding instructions. Includes definitions which are | 8 * Data structures for decoding instructions. Includes definitions which are |
9 * by both decoders (full-blown standalone one and reduced one in validator). | 9 * by both decoders (full-blown standalone one and reduced one in validator). |
10 */ | 10 */ |
11 | 11 |
12 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ | 12 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ |
13 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ | 13 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ |
14 | 14 |
15 #include "native_client/src/shared/utils/types.h" | 15 #include "native_client/src/shared/utils/types.h" |
16 #include "native_client/src/trusted/cpu_features/arch/x86/cpu_x86.h" | 16 #include "native_client/src/trusted/cpu_features/arch/x86/cpu_x86.h" |
17 | 17 |
18 EXTERN_C_BEGIN | 18 EXTERN_C_BEGIN |
19 | 19 |
| 20 /* |
| 21 * Instruction operand TYPE: GP register size (8-bit, 32-bit, MMX, XXM, etc), or |
| 22 * in-memory structure (far pointer, 256-bit SIMD operands, etc). |
| 23 */ |
20 enum OperandType { | 24 enum OperandType { |
21 /* | 25 /* |
22 * These are for general-purpose registers, memory access and immediates. | 26 * These are for general-purpose registers, memory access and immediates. |
23 * They are not used for XMM, MMX etc. | 27 * They are not used for XMM, MMX etc. |
24 */ | 28 */ |
25 OPERAND_TYPE_8_BIT, | 29 OPERAND_TYPE_8_BIT, |
26 OPERAND_TYPE_16_BIT, | 30 OPERAND_TYPE_16_BIT, |
27 OPERAND_TYPE_32_BIT, | 31 OPERAND_TYPE_32_BIT, |
28 OPERAND_TYPE_64_BIT, | 32 OPERAND_TYPE_64_BIT, |
29 | 33 |
30 /* Non-GP registers. */ | 34 /* Non-GP registers. */ |
31 OPERAND_TYPE_ST, /* Any X87 register. */ | 35 OPERAND_TYPE_ST, /* Any X87 register. */ |
32 OPERAND_TYPE_MMX, /* MMX registers: %mmX. */ | 36 OPERAND_TYPE_MMX, /* MMX registers: %mmX. */ |
33 OPERAND_TYPE_XMM, /* XMM register: %xmmX. */ | 37 OPERAND_TYPE_XMM, /* XMM register: %xmmX. */ |
34 OPERAND_TYPE_YMM, /* YMM registers: %ymmX. */ | 38 OPERAND_TYPE_YMM, /* YMM registers: %ymmX. */ |
35 OPERAND_TYPE_SEGMENT_REGISTER, /* Operand is segment register: %es … %gs. */ | 39 OPERAND_TYPE_SEGMENT_REGISTER, /* Operand is segment register: %es ... %gs. */ |
36 OPERAND_TYPE_CONTROL_REGISTER, /* Operand is control register: %crX. */ | 40 OPERAND_TYPE_CONTROL_REGISTER, /* Operand is control register: %crX. */ |
37 OPERAND_TYPE_DEBUG_REGISTER, /* Operand is debug register: %drX. */ | 41 OPERAND_TYPE_DEBUG_REGISTER, /* Operand is debug register: %drX. */ |
38 OPERAND_TYPE_TEST_REGISTER, /* Operand is test register: %trX. */ | 42 OPERAND_TYPE_TEST_REGISTER, /* Operand is test register: %trX. */ |
39 | 43 |
40 /* | 44 /* |
41 * All other operand types are not used as register arguments. These are | 45 * All other operand types are not used as register arguments. These are |
42 * immediates or memory. | 46 * immediates or memory. |
43 */ | 47 */ |
44 OPERAND_TYPES_REGISTER_MAX = OPERAND_TYPE_TEST_REGISTER + 1, | 48 OPERAND_TYPES_REGISTER_MAX = OPERAND_TYPE_TEST_REGISTER + 1, |
45 | 49 |
(...skipping 21 matching lines...) Expand all Loading... |
67 | 71 |
68 /* Miscellaneous structures in memory. */ | 72 /* Miscellaneous structures in memory. */ |
69 OPERAND_TYPE_X87_BCD, /* 10-byte packed BCD value. */ | 73 OPERAND_TYPE_X87_BCD, /* 10-byte packed BCD value. */ |
70 OPERAND_TYPE_X87_ENV, /* A 14-byte or 28-byte x87 environment. */ | 74 OPERAND_TYPE_X87_ENV, /* A 14-byte or 28-byte x87 environment. */ |
71 OPERAND_TYPE_X87_STATE, /* A 94-byte or 108-byte x87 state. */ | 75 OPERAND_TYPE_X87_STATE, /* A 94-byte or 108-byte x87 state. */ |
72 OPERAND_TYPE_X87_MMX_XMM_STATE, /* A 512-byte extended x87/MMX/XMM state. */ | 76 OPERAND_TYPE_X87_MMX_XMM_STATE, /* A 512-byte extended x87/MMX/XMM state. */ |
73 OPERAND_TYPE_SELECTOR, /* Operand is 6/10 bytes selector. */ | 77 OPERAND_TYPE_SELECTOR, /* Operand is 6/10 bytes selector. */ |
74 OPERAND_TYPE_FAR_PTR /* Operand is 6/10 bytes far pointer. */ | 78 OPERAND_TYPE_FAR_PTR /* Operand is 6/10 bytes far pointer. */ |
75 }; | 79 }; |
76 | 80 |
| 81 /* |
| 82 * Instruction operand NAME: register number (REG_RAX means any of the following |
| 83 * registers: %al/%ax/%eax/%rax/%st(0)/%mm0/%xmm0/%ymm0/%es/%cr0/%db0/%tr0), or |
| 84 * non-register operand (REG_RM means address in memory specified via "ModR/M |
| 85 * byte" (plus may be "SIB byte" or displacement), REG_DS_RBX is special operand |
| 86 * of "xlat" instruction, REG_ST is to of x87 stack and so on - see below for |
| 87 * for the full list). |
| 88 */ |
77 enum OperandName { | 89 enum OperandName { |
78 /* First 16 registers are compatible with encoding of registers in x86 ABI. */ | 90 /* First 16 registers are compatible with encoding of registers in x86 ABI. */ |
79 REG_RAX, | 91 REG_RAX, |
80 REG_RCX, | 92 REG_RCX, |
81 REG_RDX, | 93 REG_RDX, |
82 REG_RBX, | 94 REG_RBX, |
83 REG_RSP, | 95 REG_RSP, |
84 REG_RBP, | 96 REG_RBP, |
85 REG_RSI, | 97 REG_RSI, |
86 REG_RDI, | 98 REG_RDI, |
87 REG_R8, | 99 REG_R8, |
88 REG_R9, | 100 REG_R9, |
89 REG_R10, | 101 REG_R10, |
90 REG_R11, | 102 REG_R11, |
91 REG_R12, | 103 REG_R12, |
92 REG_R13, | 104 REG_R13, |
93 REG_R14, | 105 REG_R14, |
94 REG_R15, | 106 REG_R15, |
95 /* These are different kinds of operands used in special cases. */ | 107 /* These are different kinds of operands used in special cases. */ |
96 REG_RM, /* Address in memory via rm field. */ | 108 REG_RM, /* Address in memory via ModR/M (+SIB). */ |
97 REG_RIP, /* RIP - used as base in x86-64 mode. */ | 109 REG_RIP, /* RIP - used as base in x86-64 mode. */ |
98 REG_RIZ, /* EIZ/RIZ - used as "always zero index" register. */ | 110 REG_RIZ, /* EIZ/RIZ - used as "always zero index" register. */ |
99 REG_IMM, /* Fixed value in imm field. */ | 111 REG_IMM, /* Fixed value in imm field. */ |
100 REG_IMM2, /* Fixed value in second imm field. */ | 112 REG_IMM2, /* Fixed value in second imm field. */ |
101 REG_DS_RBX, /* Fox xlat: %ds(%rbx). */ | 113 REG_DS_RBX, /* Fox xlat: %ds:(%rbx). */ |
102 REG_ES_RDI, /* For string instructions: %es:(%rsi). */ | 114 REG_ES_RDI, /* For string instructions: %es:(%rsi). */ |
103 REG_DS_RSI, /* For string instructions: %ds:(%rdi). */ | 115 REG_DS_RSI, /* For string instructions: %ds:(%rdi). */ |
104 REG_PORT_DX, /* 16-bit DX: for in/out instructions. */ | 116 REG_PORT_DX, /* 16-bit DX: for in/out instructions. */ |
105 NO_REG, /* For modrm: both index and base can be absent. */ | 117 NO_REG, /* For modrm: both index and base can be absent. */ |
106 REG_ST, /* For x87 instructions: implicit %st. */ | 118 REG_ST, /* For x87 instructions: implicit %st. */ |
107 JMP_TO /* Operand is jump target address: usually %rip+offset. */ | 119 JMP_TO /* Operand is jump target address: usually %rip+offset. */ |
108 }; | 120 }; |
109 | 121 |
110 /* | 122 /* |
111 * Displacement can be of four different sizes in x86 instruction set: nothing, | 123 * Displacement can be of four different sizes in x86 instruction set: nothing, |
112 * 8-bit, 16-bit, 32-bit, and 64-bit. These are traditionally treated slightly | 124 * 8-bit, 16-bit, 32-bit, and 64-bit. These are traditionally treated slightly |
113 * differently by decoders: 8-bit are usually printed as signed offset, while | 125 * differently by decoders: 8-bit are usually printed as signed offset, while |
114 * 32-bit (in ia32 mode) and 64-bit (in amd64 mode) are printed as unsigned | 126 * 32-bit (in ia32 mode) and 64-bit (in amd64 mode) are printed as unsigned |
115 * offset. | 127 * offset. |
116 */ | 128 */ |
117 enum DisplacementMode { | 129 enum DisplacementMode { |
118 DISPNONE, | 130 DISPNONE, |
119 DISP8, | 131 DISP8, |
120 DISP16, | 132 DISP16, |
121 DISP32, | 133 DISP32, |
122 DISP64 | 134 DISP64 |
123 }; | 135 }; |
124 | 136 |
| 137 /* |
| 138 * Information about decoded instruction: name, operands, prefixes, etc. |
| 139 */ |
125 struct Instruction { | 140 struct Instruction { |
126 const char *name; | 141 const char *name; |
127 unsigned char operands_count; | 142 unsigned char operands_count; |
128 struct { | 143 struct { |
129 unsigned char rex; /* Mostly to distingush cases like %ah vs %spl. */ | 144 unsigned char rex; /* Mostly to distingush cases like %ah vs %spl. */ |
130 Bool rex_b_spurious; | 145 Bool rex_b_spurious; |
131 Bool rex_x_spurious; | 146 Bool rex_x_spurious; |
132 Bool rex_r_spurious; | 147 Bool rex_r_spurious; |
133 Bool rex_w_spurious; | 148 Bool rex_w_spurious; |
134 Bool data16; /* "Normal", non-rex prefixes. */ | 149 Bool data16; /* "Normal", non-rex prefixes. */ |
(...skipping 12 matching lines...) Expand all Loading... |
147 enum OperandName index; | 162 enum OperandName index; |
148 int scale; | 163 int scale; |
149 int64_t offset; | 164 int64_t offset; |
150 enum DisplacementMode disp_type; | 165 enum DisplacementMode disp_type; |
151 } rm; | 166 } rm; |
152 uint64_t imm[2]; | 167 uint64_t imm[2]; |
153 const char* att_instruction_suffix; | 168 const char* att_instruction_suffix; |
154 }; | 169 }; |
155 | 170 |
156 /* | 171 /* |
| 172 * Instruction processing callback: called once for each instruction in a stream |
| 173 * |
157 * "begin" points to first byte of the instruction | 174 * "begin" points to first byte of the instruction |
158 * "end" points to the last byte of the instruction | 175 * "end" points to the first byte of the next instruction |
159 * for single-byte instruction “begin” == “end” | 176 * for single-byte instruction "begin + 1" == "end" |
160 * "instruction" is detailed information about instruction | 177 * "instruction" contains detailed information about instruction |
161 */ | 178 */ |
162 typedef void (*ProcessInstructionFunc) (const uint8_t *begin, | 179 typedef void (*ProcessInstructionFunc) (const uint8_t *begin, |
163 const uint8_t *end, | 180 const uint8_t *end, |
164 struct Instruction *instruction, | 181 struct Instruction *instruction, |
165 void *userdata); | 182 void *callback_data); |
166 | 183 |
167 /* | 184 /* |
| 185 * Decoding error: called when decoder's DFA does not recognize the instruction. |
| 186 * |
168 * "ptr" points to the first byte rejected by DFA and can be used in more | 187 * "ptr" points to the first byte rejected by DFA and can be used in more |
169 * advanced decoders to try to do some kind of recovery. | 188 * advanced decoders to try to do some kind of recovery. |
170 */ | 189 */ |
171 typedef void (*ProcessDecodingErrorFunc) (const uint8_t *ptr, | 190 typedef void (*ProcessDecodingErrorFunc) (const uint8_t *ptr, |
172 void *userdata); | 191 void *callback_data); |
173 | 192 |
174 /* | 193 /* |
175 * kFullCPUIDFeatures is pre-defined constant of NaClCPUFeaturesX86 type with | 194 * kFullCPUIDFeatures is pre-defined constant of NaClCPUFeaturesX86 type with |
176 * all possible CPUID features enabled. | 195 * all possible CPUID features enabled. |
177 */ | 196 */ |
178 extern const NaClCPUFeaturesX86 kFullCPUIDFeatures; | 197 extern const NaClCPUFeaturesX86 kFullCPUIDFeatures; |
179 | 198 |
180 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 199 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
181 ProcessInstructionFunc process_instruction, | 200 ProcessInstructionFunc process_instruction, |
182 ProcessDecodingErrorFunc process_error, void *userdata); | 201 ProcessDecodingErrorFunc process_error, |
| 202 void *callback_data); |
183 | 203 |
184 int DecodeChunkIA32(const uint8_t *data, size_t size, | 204 int DecodeChunkIA32(const uint8_t *data, size_t size, |
185 ProcessInstructionFunc process_instruction, | 205 ProcessInstructionFunc process_instruction, |
186 ProcessDecodingErrorFunc process_error, void *userdata); | 206 ProcessDecodingErrorFunc process_error, |
| 207 void *callback_data); |
187 | 208 |
188 EXTERN_C_END | 209 EXTERN_C_END |
189 | 210 |
190 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ */ | 211 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ */ |
OLD | NEW |