OLD | NEW |
| (Empty) |
1 /* | |
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 | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
8 * This file contains common parts of x86-32 and x86-64 internals (inline | |
9 * functions and defines). | |
10 */ | |
11 | |
12 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_VALIDATOR_INTERNAL_H_ | |
13 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_VALIDATOR_INTERNAL_H_ | |
14 | |
15 #include "native_client/src/shared/platform/nacl_check.h" | |
16 #include "native_client/src/shared/utils/types.h" | |
17 #include "native_client/src/trusted/validator_ragel/unreviewed/decoding.h" | |
18 #include "native_client/src/trusted/validator_ragel/validator.h" | |
19 | |
20 /* Maximum set of R-DFA allowable CPUID features. */ | |
21 extern const NaClCPUFeaturesX86 kValidatorCPUIDFeatures; | |
22 | |
23 /* Macroses to suppport CPUID handling. */ | |
24 #define SET_CPU_FEATURE(F) \ | |
25 if (!(F##_Allowed)) { \ | |
26 instruction_info_collected |= UNRECOGNIZED_INSTRUCTION; \ | |
27 } \ | |
28 if (!(F)) { \ | |
29 instruction_info_collected |= CPUID_UNSUPPORTED_INSTRUCTION; \ | |
30 } | |
31 #define CPUFeature_3DNOW cpu_features->data[NaClCPUFeatureX86_3DNOW] | |
32 /* | |
33 * AMD documentation claims it's always available if CPUFeature_LM is present, | |
34 * But Intel documentation does not even mention it! | |
35 * Keep it as 3DNow! instruction. | |
36 */ | |
37 #define CPUFeature_3DPRFTCH CPUFeature_3DNOW || CPUFeature_PRE | |
38 #define CPUFeature_AES cpu_features->data[NaClCPUFeatureX86_AES] | |
39 #define CPUFeature_AESAVX CPUFeature_AES && CPUFeature_AVX | |
40 #define CPUFeature_AVX cpu_features->data[NaClCPUFeatureX86_AVX] | |
41 #define CPUFeature_BMI1 cpu_features->data[NaClCPUFeatureX86_BMI1] | |
42 #define CPUFeature_CLFLUSH cpu_features->data[NaClCPUFeatureX86_CLFLUSH] | |
43 #define CPUFeature_CLMUL cpu_features->data[NaClCPUFeatureX86_CLMUL] | |
44 #define CPUFeature_CLMULAVX CPUFeature_CLMUL && CPUFeature_AVX | |
45 #define CPUFeature_CMOV cpu_features->data[NaClCPUFeatureX86_CMOV] | |
46 #define CPUFeature_CMOVx87 CPUFeature_CMOV && CPUFeature_x87 | |
47 #define CPUFeature_CX16 cpu_features->data[NaClCPUFeatureX86_CX16] | |
48 #define CPUFeature_CX8 cpu_features->data[NaClCPUFeatureX86_CX8] | |
49 #define CPUFeature_E3DNOW cpu_features->data[NaClCPUFeatureX86_E3DNOW] | |
50 #define CPUFeature_EMMX cpu_features->data[NaClCPUFeatureX86_EMMX] | |
51 #define CPUFeature_EMMXSSE CPUFeature_EMMX || CPUFeature_SSE | |
52 #define CPUFeature_F16C cpu_features->data[NaClCPUFeatureX86_F16C] | |
53 #define CPUFeature_FMA cpu_features->data[NaClCPUFeatureX86_FMA] | |
54 #define CPUFeature_FMA4 cpu_features->data[NaClCPUFeatureX86_FMA4] | |
55 #define CPUFeature_FXSR cpu_features->data[NaClCPUFeatureX86_FXSR] | |
56 #define CPUFeature_LAHF cpu_features->data[NaClCPUFeatureX86_LAHF] | |
57 #define CPUFeature_LM cpu_features->data[NaClCPUFeatureX86_LM] | |
58 #define CPUFeature_LWP cpu_features->data[NaClCPUFeatureX86_LWP] | |
59 /* | |
60 * We allow lzcnt unconditionally | |
61 * See http://code.google.com/p/nativeclient/issues/detail?id=2869 | |
62 */ | |
63 #define CPUFeature_LZCNT TRUE | |
64 #define CPUFeature_MMX cpu_features->data[NaClCPUFeatureX86_MMX] | |
65 #define CPUFeature_MON cpu_features->data[NaClCPUFeatureX86_MON] | |
66 #define CPUFeature_MOVBE cpu_features->data[NaClCPUFeatureX86_MOVBE] | |
67 #define CPUFeature_OSXSAVE cpu_features->data[NaClCPUFeatureX86_OSXSAVE] | |
68 #define CPUFeature_POPCNT cpu_features->data[NaClCPUFeatureX86_POPCNT] | |
69 #define CPUFeature_PRE cpu_features->data[NaClCPUFeatureX86_PRE] | |
70 #define CPUFeature_SSE cpu_features->data[NaClCPUFeatureX86_SSE] | |
71 #define CPUFeature_SSE2 cpu_features->data[NaClCPUFeatureX86_SSE2] | |
72 #define CPUFeature_SSE3 cpu_features->data[NaClCPUFeatureX86_SSE3] | |
73 #define CPUFeature_SSE41 cpu_features->data[NaClCPUFeatureX86_SSE41] | |
74 #define CPUFeature_SSE42 cpu_features->data[NaClCPUFeatureX86_SSE42] | |
75 #define CPUFeature_SSE4A cpu_features->data[NaClCPUFeatureX86_SSE4A] | |
76 #define CPUFeature_SSSE3 cpu_features->data[NaClCPUFeatureX86_SSSE3] | |
77 #define CPUFeature_TBM cpu_features->data[NaClCPUFeatureX86_TBM] | |
78 #define CPUFeature_TSC cpu_features->data[NaClCPUFeatureX86_TSC] | |
79 /* | |
80 * We allow tzcnt unconditionally | |
81 * See http://code.google.com/p/nativeclient/issues/detail?id=2869 | |
82 */ | |
83 #define CPUFeature_TZCNT TRUE | |
84 #define CPUFeature_x87 cpu_features->data[NaClCPUFeatureX86_x87] | |
85 #define CPUFeature_XOP cpu_features->data[NaClCPUFeatureX86_XOP] | |
86 | |
87 #define CPUFeature_3DNOW_Allowed \ | |
88 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_3DNOW] | |
89 /* | |
90 * AMD documentation claims it's always available if CPUFeature_LM is present, | |
91 * But Intel documentation does not even mention it! | |
92 * Keep it as 3DNow! instruction. | |
93 */ | |
94 #define CPUFeature_3DPRFTCH_Allowed \ | |
95 CPUFeature_3DNOW_Allowed || CPUFeature_PRE_Allowed | |
96 #define CPUFeature_AES_Allowed \ | |
97 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_AES] | |
98 #define CPUFeature_AESAVX_Allowed \ | |
99 CPUFeature_AES_Allowed && CPUFeature_AVX_Allowed | |
100 #define CPUFeature_AVX_Allowed \ | |
101 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_AVX] | |
102 #define CPUFeature_BMI1_Allowed \ | |
103 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_BMI1] | |
104 #define CPUFeature_CLFLUSH_Allowed \ | |
105 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_CLFLUSH] | |
106 #define CPUFeature_CLMUL_Allowed \ | |
107 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_CLMUL] | |
108 #define CPUFeature_CLMULAVX_Allowed \ | |
109 CPUFeature_CLMUL_Allowed && CPUFeature_AVX_Allowed | |
110 #define CPUFeature_CMOV_Allowed \ | |
111 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_CMOV] | |
112 #define CPUFeature_CMOVx87_Allowed \ | |
113 CPUFeature_CMOV_Allowed && CPUFeature_x87_Allowed | |
114 #define CPUFeature_CX16_Allowed \ | |
115 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_CX16] | |
116 #define CPUFeature_CX8_Allowed \ | |
117 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_CX8] | |
118 #define CPUFeature_E3DNOW_Allowed \ | |
119 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_E3DNOW] | |
120 #define CPUFeature_EMMX_Allowed \ | |
121 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_EMMX] | |
122 #define CPUFeature_EMMXSSE_Allowed \ | |
123 CPUFeature_EMMX_Allowed || CPUFeature_SSE_Allowed | |
124 #define CPUFeature_F16C_Allowed \ | |
125 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_F16C] | |
126 #define CPUFeature_FMA_Allowed \ | |
127 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_FMA] | |
128 #define CPUFeature_FMA4_Allowed \ | |
129 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_FMA4] | |
130 #define CPUFeature_FXSR_Allowed \ | |
131 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_FXSR] | |
132 #define CPUFeature_LAHF_Allowed \ | |
133 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_LAHF] | |
134 #define CPUFeature_LM_Allowed \ | |
135 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_LM] | |
136 #define CPUFeature_LWP_Allowed \ | |
137 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_LWP] | |
138 /* | |
139 * We allow lzcnt unconditionally | |
140 * See http://code.google.com/p/nativeclient/issues/detail?id=2869 | |
141 */ | |
142 #define CPUFeature_LZCNT_Allowed TRUE | |
143 #define CPUFeature_MMX_Allowed \ | |
144 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_MMX] | |
145 #define CPUFeature_MON_Allowed \ | |
146 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_MON] | |
147 #define CPUFeature_MOVBE_Allowed \ | |
148 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_MOVBE] | |
149 #define CPUFeature_OSXSAVE_Allowed \ | |
150 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_OSXSAVE] | |
151 #define CPUFeature_POPCNT_Allowed \ | |
152 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_POPCNT] | |
153 #define CPUFeature_PRE_Allowed \ | |
154 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_PRE] | |
155 #define CPUFeature_SSE_Allowed \ | |
156 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSE] | |
157 #define CPUFeature_SSE2_Allowed \ | |
158 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSE2] | |
159 #define CPUFeature_SSE3_Allowed \ | |
160 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSE3] | |
161 #define CPUFeature_SSE41_Allowed \ | |
162 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSE41] | |
163 #define CPUFeature_SSE42_Allowed \ | |
164 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSE42] | |
165 #define CPUFeature_SSE4A_Allowed \ | |
166 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSE4A] | |
167 #define CPUFeature_SSSE3_Allowed \ | |
168 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_SSSE3] | |
169 #define CPUFeature_TBM_Allowed \ | |
170 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_TBM] | |
171 #define CPUFeature_TSC_Allowed \ | |
172 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_TSC] | |
173 /* | |
174 * We allow tzcnt unconditionally | |
175 * See http://code.google.com/p/nativeclient/issues/detail?id=2869 | |
176 */ | |
177 #define CPUFeature_TZCNT_Allowed TRUE | |
178 #define CPUFeature_x87_Allowed \ | |
179 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_x87] | |
180 #define CPUFeature_XOP_Allowed \ | |
181 kValidatorCPUIDFeatures.data[NaClCPUFeatureX86_XOP] | |
182 | |
183 /* Remember some information about instruction for further processing. */ | |
184 #define GET_REX_PREFIX() rex_prefix | |
185 #define SET_REX_PREFIX(P) rex_prefix = (P) | |
186 #define GET_VEX_PREFIX2() vex_prefix2 | |
187 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) | |
188 #define GET_VEX_PREFIX3() vex_prefix3 | |
189 #define SET_VEX_PREFIX3(P) vex_prefix3 = (P) | |
190 #define SET_MODRM_BASE(N) base = (N) | |
191 #define SET_MODRM_INDEX(N) index = (N) | |
192 | |
193 /* Ignore this information for now. */ | |
194 #define SET_DATA16_PREFIX(S) | |
195 #define SET_REPZ_PREFIX(S) | |
196 #define SET_REPNZ_PREFIX(S) | |
197 #define SET_MODRM_SCALE(S) | |
198 #define SET_DISP_PTR(P) | |
199 #define SET_IMM_PTR(P) | |
200 #define SET_IMM2_PTR(P) | |
201 | |
202 /* | |
203 * Collect information about anyfields (offsets and immediates). | |
204 * Note: we use += below instead of |=. This means two immediate fields will | |
205 * be treated as one. It's not important for safety. | |
206 */ | |
207 #define SET_DISP_TYPE(T) SET_DISP_TYPE_##T | |
208 #define SET_DISP_TYPE_DISPNONE | |
209 #define SET_DISP_TYPE_DISP8 (instruction_info_collected += DISPLACEMENT_8BIT) | |
210 #define SET_DISP_TYPE_DISP32 (instruction_info_collected += DISPLACEMENT_32BIT) | |
211 #define SET_IMM_TYPE(T) SET_IMM_TYPE_##T | |
212 /* imm2 field is a flag, not accumulator, like with other immediates */ | |
213 #define SET_IMM_TYPE_IMM2 (instruction_info_collected |= IMMEDIATE_2BIT) | |
214 #define SET_IMM_TYPE_IMM8 (instruction_info_collected += IMMEDIATE_8BIT) | |
215 #define SET_IMM_TYPE_IMM16 (instruction_info_collected += IMMEDIATE_16BIT) | |
216 #define SET_IMM_TYPE_IMM32 (instruction_info_collected += IMMEDIATE_32BIT) | |
217 #define SET_IMM_TYPE_IMM64 (instruction_info_collected += IMMEDIATE_64BIT) | |
218 #define SET_IMM2_TYPE(T) SET_IMM2_TYPE_##T | |
219 #define SET_IMM2_TYPE_IMM8 \ | |
220 (instruction_info_collected += SECOND_IMMEDIATE_8BIT) | |
221 #define SET_IMM2_TYPE_IMM16 \ | |
222 (instruction_info_collected += SECOND_IMMEDIATE_16BIT) | |
223 | |
224 /* Mark the destination of a jump instruction and make an early validity check: | |
225 * to jump outside given code region, the target address must be aligned. | |
226 * | |
227 * Returns TRUE iff the jump passes the early validity check. | |
228 */ | |
229 static FORCEINLINE int MarkJumpTarget(size_t jump_dest, | |
230 bitmap_word *jump_dests, | |
231 size_t size) { | |
232 if ((jump_dest & kBundleMask) == 0) { | |
233 return TRUE; | |
234 } | |
235 if (jump_dest >= size) { | |
236 return FALSE; | |
237 } | |
238 BitmapSetBit(jump_dests, jump_dest); | |
239 return TRUE; | |
240 } | |
241 | |
242 /* | |
243 * Mark the given address as valid jump target address. | |
244 */ | |
245 static FORCEINLINE void MarkValidJumpTarget(size_t address, | |
246 bitmap_word *valid_targets) { | |
247 BitmapSetBit(valid_targets, address); | |
248 } | |
249 | |
250 /* | |
251 * Mark the given address as invalid jump target address (that is: unmark it). | |
252 */ | |
253 static FORCEINLINE void UnmarkValidJumpTarget(size_t address, | |
254 bitmap_word *valid_targets) { | |
255 BitmapClearBit(valid_targets, address); | |
256 } | |
257 | |
258 /* | |
259 * Mark the given addresses as invalid jump target addresses (that is: unmark | |
260 * them). | |
261 */ | |
262 static FORCEINLINE void UnmarkValidJumpTargets(size_t address, | |
263 size_t bytes, | |
264 bitmap_word *valid_targets) { | |
265 BitmapClearBits(valid_targets, address, bytes); | |
266 } | |
267 | |
268 static INLINE Bool ProcessInvalidJumpTargets( | |
269 const uint8_t *data, | |
270 size_t size, | |
271 bitmap_word *valid_targets, | |
272 bitmap_word *jump_dests, | |
273 ValidationCallbackFunc user_callback, | |
274 void *callback_data) { | |
275 size_t elements = (size + NACL_HOST_WORDSIZE - 1) / NACL_HOST_WORDSIZE; | |
276 size_t i, j; | |
277 Bool result = TRUE; | |
278 | |
279 for (i = 0; i < elements ; i++) { | |
280 bitmap_word jump_dest_mask = jump_dests[i]; | |
281 bitmap_word valid_target_mask = valid_targets[i]; | |
282 if ((jump_dest_mask & ~valid_target_mask) != 0) { | |
283 for (j = i * NACL_HOST_WORDSIZE; j < (i + 1) * NACL_HOST_WORDSIZE; j++) | |
284 if (BitmapIsBitSet(jump_dests, j) && | |
285 !BitmapIsBitSet(valid_targets, j)) { | |
286 result &= user_callback(data + j, | |
287 data + j, | |
288 BAD_JUMP_TARGET, | |
289 callback_data); | |
290 } | |
291 } | |
292 } | |
293 | |
294 return result; | |
295 } | |
296 | |
297 | |
298 /* | |
299 * Process rel8_operand. Note: rip points to the beginning of the next | |
300 * instruction here and x86 encoding guarantees rel8 field is the last one | |
301 * in a current instruction. | |
302 */ | |
303 static FORCEINLINE void Rel8Operand(const uint8_t *rip, | |
304 const uint8_t* codeblock_start, | |
305 bitmap_word *jump_dests, | |
306 size_t jumpdests_size, | |
307 uint32_t *instruction_info_collected) { | |
308 int8_t offset = (uint8_t) (rip[-1]); | |
309 size_t jump_dest = offset + (rip - codeblock_start); | |
310 | |
311 if (MarkJumpTarget(jump_dest, jump_dests, jumpdests_size)) | |
312 *instruction_info_collected |= RELATIVE_8BIT; | |
313 else | |
314 *instruction_info_collected |= RELATIVE_8BIT | DIRECT_JUMP_OUT_OF_RANGE; | |
315 } | |
316 | |
317 /* | |
318 * Process rel32_operand. Note: rip points to the beginning of the next | |
319 * instruction here and x86 encoding guarantees rel32 field is the last one | |
320 * in a current instruction. | |
321 */ | |
322 static FORCEINLINE void Rel32Operand(const uint8_t *rip, | |
323 const uint8_t* codeblock_start, | |
324 bitmap_word *jump_dests, | |
325 size_t jumpdests_size, | |
326 uint32_t *instruction_info_collected) { | |
327 int32_t offset = (rip[-4] + 256U * (rip[-3] + 256U * ( | |
328 rip[-2] + 256U * ((uint32_t) rip[-1])))); | |
329 size_t jump_dest = offset + (rip - codeblock_start); | |
330 | |
331 if (MarkJumpTarget(jump_dest, jump_dests, jumpdests_size)) | |
332 *instruction_info_collected |= RELATIVE_32BIT; | |
333 else | |
334 *instruction_info_collected |= RELATIVE_32BIT | DIRECT_JUMP_OUT_OF_RANGE; | |
335 } | |
336 | |
337 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_VALIDATOR_INTERNAL_H_ */ | |
OLD | NEW |