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 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ | 7 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ |
8 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ | 8 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ |
9 | 9 |
10 #include "native_client/src/shared/utils/types.h" | 10 #include "native_client/src/shared/utils/types.h" |
11 #include "native_client/src/trusted/validator/x86/nacl_cpuid.h" | 11 #include "native_client/src/trusted/validator/x86/nacl_cpuid.h" |
12 | 12 |
13 EXTERN_C_BEGIN | 13 EXTERN_C_BEGIN |
14 | 14 |
15 enum operand_type { | 15 enum OperandType { |
Brad Chen
2012/10/04 17:26:04
Thanks; these enum decls look better now.
| |
16 | |
17 /* | 16 /* |
18 * These are for general-purpose registers, memory access and immediates. | 17 * These are for general-purpose registers, memory access and immediates. |
19 * They are not used for XMM, MMX etc. | 18 * They are not used for XMM, MMX etc. |
20 */ | 19 */ |
21 OPERAND_SIZE_2_BIT, /* See VPERMIL2Px instruction for description. */ | 20 /* See VPERMIL2Px instruction for description of 2-bit operand type. */ |
22 OPERAND_SIZE_8_BIT, | 21 OPERAND_TYPE_2_BIT, |
23 OPERAND_SIZE_16_BIT, | 22 /* These are regular-sized operands: GP registers or memory. */ |
24 OPERAND_SIZE_32_BIT, | 23 OPERAND_TYPE_8_BIT, |
25 OPERAND_SIZE_64_BIT, | 24 OPERAND_TYPE_16_BIT, |
26 OPERAND_SIZE_128_BIT, | 25 OPERAND_TYPE_32_BIT, |
27 OPERAND_SIZE_256_BIT, | 26 OPERAND_TYPE_64_BIT, |
27 OPERAND_TYPE_128_BIT, | |
28 OPERAND_TYPE_256_BIT, | |
29 | |
30 /* Non-GP registers. */ | |
31 OPERAND_TYPE_ST, /* Any X87 register. */ | |
32 OPERAND_TYPE_SEGMENT_REGISTER, /* Operand is segment register: %es … %gs. */ | |
33 OPERAND_TYPE_CONTROL_REGISTER, /* Operand is control register: %crX. */ | |
34 OPERAND_TYPE_DEBUG_REGISTER, /* Operand is debug register: %drX. */ | |
35 OPERAND_TYPE_MMX, | |
36 OPERAND_TYPE_XMM, | |
37 OPERAND_TYPE_YMM, | |
28 | 38 |
29 /* OPERAND_FLOAT_SIZE_*_BIT are used for in-memory operands. */ | 39 /* OPERAND_FLOAT_SIZE_*_BIT are used for in-memory operands. */ |
30 OPERAND_FLOAT_SIZE_16_BIT, | 40 OPERAND_TYPE_FLOAT_32_BIT, |
31 OPERAND_FLOAT_SIZE_32_BIT, | 41 OPERAND_TYPE_FLOAT_64_BIT, |
32 OPERAND_FLOAT_SIZE_64_BIT, | 42 OPERAND_TYPE_FLOAT_80_BIT, |
33 OPERAND_FLOAT_SIZE_80_BIT, | |
34 | 43 |
35 /* OPERAND_X87_SIZE_64_BIT are signed integers in memory.*/ | 44 /* OPERAND_X87_SIZE_*_BIT are signed integers in memory.*/ |
36 OPERAND_X87_SIZE_16_BIT, | 45 OPERAND_TYPE_X87_16_BIT, |
37 OPERAND_X87_SIZE_32_BIT, | 46 OPERAND_TYPE_X87_32_BIT, |
38 OPERAND_X87_SIZE_64_BIT, | 47 OPERAND_TYPE_X87_64_BIT, |
39 | 48 |
40 | 49 /* Miscellaneous structures in memory. */ |
41 OPERAND_X87_BCD, /* 10-byte packed BCD value in memory. */ | 50 OPERAND_TYPE_X87_BCD, /* 10-byte packed BCD value. */ |
42 OPERAND_X87_ENV, /* A 14-byte or 28-byte x87 environment. */ | 51 OPERAND_TYPE_X87_ENV, /* A 14-byte or 28-byte x87 environment. */ |
43 OPERAND_X87_STATE, /* A 94-byte or 108-byte x87 state. */ | 52 OPERAND_TYPE_X87_STATE, /* A 94-byte or 108-byte x87 state. */ |
44 OPERAND_X87_MMX_MM_STATE, /* A 512-byte extended x87/MMX/XMM state. */ | 53 OPERAND_TYPE_X87_MMX_XMM_STATE, /* A 512-byte extended x87/MMX/XMM state. */ |
45 OPERAND_SELECTOR, /* Operand is 6/10 bytes selector in memory. */ | 54 OPERAND_TYPE_SELECTOR, /* Operand is 6/10 bytes selector. */ |
46 OPERAND_FAR_PTR, /* Operand is 6/10 bytes far pointer in memory. */ | 55 OPERAND_TYPE_FAR_PTR /* Operand is 6/10 bytes far pointer. */ |
47 | |
48 OPERAND_ST, /* Any X87 register. */ | |
49 OPERAND_SEGMENT_REGISTER, /* Operand is segment register: %{e,c,s,d,f,g}s. */ | |
50 OPERAND_CONTROL_REGISTER, /* Operand is control register: %crX. */ | |
51 OPERAND_DEBUG_REGISTER, /* Operand is debug register: %drX. */ | |
52 OPERAND_MMX, | |
53 OPERAND_XMM, | |
54 OPERAND_YMM | |
55 }; | 56 }; |
56 | 57 |
57 enum register_name { | 58 enum OperandName { |
58 /* First 16 registers are compatible with encoding of registers in x86 ABI. */ | 59 /* First 16 registers are compatible with encoding of registers in x86 ABI. */ |
59 REG_RAX, | 60 REG_RAX, |
60 REG_RCX, | 61 REG_RCX, |
61 REG_RDX, | 62 REG_RDX, |
62 REG_RBX, | 63 REG_RBX, |
63 REG_RSP, | 64 REG_RSP, |
64 REG_RBP, | 65 REG_RBP, |
65 REG_RSI, | 66 REG_RSI, |
66 REG_RDI, | 67 REG_RDI, |
67 REG_R8, | 68 REG_R8, |
(...skipping 13 matching lines...) Expand all Loading... | |
81 REG_DS_RBX, /* Fox xlat: %ds(%rbx). */ | 82 REG_DS_RBX, /* Fox xlat: %ds(%rbx). */ |
82 REG_ES_RDI, /* For string instructions: %es:(%rsi). */ | 83 REG_ES_RDI, /* For string instructions: %es:(%rsi). */ |
83 REG_DS_RSI, /* For string instructions: %ds:(%rdi). */ | 84 REG_DS_RSI, /* For string instructions: %ds:(%rdi). */ |
84 REG_PORT_DX, /* 16-bit DX: for in/out instructions. */ | 85 REG_PORT_DX, /* 16-bit DX: for in/out instructions. */ |
85 NO_REG, /* For modrm: both index and base can be absent. */ | 86 NO_REG, /* For modrm: both index and base can be absent. */ |
86 REG_ST, /* For x87 instructions: implicit %st. */ | 87 REG_ST, /* For x87 instructions: implicit %st. */ |
87 JMP_TO /* Operand is jump target address: usually %rip+offset. */ | 88 JMP_TO /* Operand is jump target address: usually %rip+offset. */ |
88 }; | 89 }; |
89 | 90 |
90 /* | 91 /* |
91 * This enum extends NaClCPUFeatureID to cover instructions not recognized in | 92 * Displacement can be of four different sizes in x86 instruction set: nothing, |
92 * | 93 * 8-bit, 16-bit, 32-bit, and 64-bit. These are traditionally threated slightly |
93 * / | 94 * differently by decoders: 8-bit are usually printed as signed offset, while |
94 enum DecoderCPUFeatures { | 95 * 32-bit (in ia32 mode) and 64-bit (in amd64 mode) are printed as unsigned |
95 }; | 96 * offset. |
96 */ | 97 */ |
97 | 98 enum DisplacementMode { |
98 enum disp_mode { | |
99 DISPNONE, | 99 DISPNONE, |
100 DISP8, | 100 DISP8, |
101 DISP16, | 101 DISP16, |
102 DISP32, | 102 DISP32, |
103 DISP64, | 103 DISP64, |
104 }; | 104 }; |
105 | 105 |
106 struct instruction { | 106 /* |
107 * Structure which encodes the instruction. Used to pass the information about | |
108 * the instruction to process_instruction_func callback. | |
109 */ | |
110 struct Instruction { | |
107 const char *name; | 111 const char *name; |
108 unsigned char operands_count; | 112 unsigned char operands_count; |
109 struct { | 113 struct { |
110 unsigned char rex; /* Mostly to distingush cases like %ah vs %spl. */ | 114 unsigned char rex; /* Mostly to distingush cases like %ah vs %spl. */ |
115 /* | |
116 * Here the difference between compilers raises it's ugly head. What we | |
117 * really want to have here is C99's _Bool. Unfortunately MSVC does not | |
118 * offer it. We have Bool typedef which works fine on MSVC but sadly | |
119 * generates warnings when used with GCC. | |
120 */ | |
111 #ifdef _MSC_VER | 121 #ifdef _MSC_VER |
Brad Chen
2012/10/04 17:26:04
Can you try to find a way to get this ifdef out of
khim
2012/10/05 08:22:53
Done: this makes decoder slightly slower, but we d
| |
112 Bool data16:1; /* "Normal", non-rex prefixes. */ | 122 Bool data16:1; /* "Normal", non-rex prefixes. */ |
113 Bool lock:1; | 123 Bool lock:1; |
114 Bool repnz:1; | 124 Bool repnz:1; |
115 Bool repz:1; | 125 Bool repz:1; |
116 Bool branch_not_taken:1; | 126 Bool branch_not_taken:1; |
117 Bool branch_taken:1; | 127 Bool branch_taken:1; |
118 #else | 128 #else |
119 _Bool data16:1; /* "Normal", non-rex prefixes. */ | 129 _Bool data16:1; /* "Normal", non-rex prefixes. */ |
120 _Bool lock:1; | 130 _Bool lock:1; |
121 _Bool repnz:1; | 131 _Bool repnz:1; |
122 _Bool repz:1; | 132 _Bool repz:1; |
123 _Bool branch_not_taken:1; | 133 _Bool branch_not_taken:1; |
124 _Bool branch_taken:1; | 134 _Bool branch_taken:1; |
125 #endif | 135 #endif |
126 } prefix; | 136 } prefix; |
127 struct { | 137 struct { |
128 enum register_name name; | 138 enum OperandName name; |
129 enum operand_type type; | 139 enum OperandType type; |
130 } operands[5]; | 140 } operands[5]; |
131 struct { | 141 struct { |
132 enum register_name base; | 142 enum OperandName base; /* Can be RAX ... R15, or NO_REG. */ |
133 enum register_name index; | 143 enum OperandName index; /* Can be RAX ... R15, or RIP, RIZ, or NO_REG */ |
134 int scale; | 144 int scale; |
135 int64_t offset; | 145 int64_t offset; |
136 enum disp_mode disp_type; | 146 enum DisplacementMode disp_type; |
137 } rm; | 147 } rm; |
138 uint64_t imm[2]; | 148 uint64_t imm[2]; |
139 }; | 149 }; |
140 | 150 |
141 typedef void (*process_instruction_func) (const uint8_t *begin, | 151 typedef void (*ProcessInstructionFunc) (const uint8_t *begin, |
142 const uint8_t *end, | 152 const uint8_t *end, |
143 struct instruction *instruction, | 153 struct Instruction *instruction, |
154 void *userdata); | |
155 | |
156 typedef void (*ProcessDecodingErrorFunc) (const uint8_t *ptr, | |
144 void *userdata); | 157 void *userdata); |
145 | 158 |
146 typedef void (*process_decoding_error_func) (const uint8_t *ptr, | |
147 void *userdata); | |
148 | |
149 /* All possible CPUID features enabled. */ | 159 /* All possible CPUID features enabled. */ |
Brad Chen
2012/10/04 17:26:04
This comment is too terse. Please mention full_cpu
khim
2012/10/05 08:22:53
Done.
| |
150 extern const NaClCPUFeaturesX86 full_cpuid_features; | 160 extern const NaClCPUFeaturesX86 kFullCPUIDFeatures; |
151 | 161 |
152 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 162 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
153 process_instruction_func process_instruction, | 163 ProcessInstructionFunc process_instruction, |
154 process_decoding_error_func process_error, void *userdata); | 164 ProcessDecodingErrorFunc process_error, void *userdata); |
155 | 165 |
156 int DecodeChunkIA32(const uint8_t *data, size_t size, | 166 int DecodeChunkIA32(const uint8_t *data, size_t size, |
157 process_instruction_func process_instruction, | 167 ProcessInstructionFunc process_instruction, |
158 process_decoding_error_func process_error, void *userdata); | 168 ProcessDecodingErrorFunc process_error, void *userdata); |
159 | 169 |
160 EXTERN_C_END | 170 EXTERN_C_END |
161 | 171 |
162 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ */ | 172 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ */ |
OLD | NEW |