Chromium Code Reviews| 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 operand_type { |
| 16 | 16 |
| 17 /* | 17 /* |
| 18 * These are for general-purpose registers, memory access and immediates. | 18 * These are for general-purpose registers, memory access and immediates. |
| 19 * They are not used for XMM, MMX etc. | 19 * They are not used for XMM, MMX etc. |
| 20 */ | 20 */ |
| 21 OPERAND_SIZE_2_BIT, /* See VPERMIL2Px instruction for description. */ | 21 OPERAND_SIZE_2_BIT, /* See VPERMIL2Px instruction for description. */ |
|
Brad Chen
2012/09/28 19:38:34
Could you make it clear in the comment that it onl
khim
2012/09/28 23:22:06
Done.
| |
| 22 OPERAND_SIZE_8_BIT, | 22 OPERAND_SIZE_8_BIT, |
| 23 OPERAND_SIZE_16_BIT, | 23 OPERAND_SIZE_16_BIT, |
| 24 OPERAND_SIZE_32_BIT, | 24 OPERAND_SIZE_32_BIT, |
| 25 OPERAND_SIZE_64_BIT, | 25 OPERAND_SIZE_64_BIT, |
|
Brad Chen
2012/09/28 19:38:34
Are there other 64-bit operand_types below? In tha
khim
2012/09/28 20:26:26
Operand can only use one items from a list. But GE
Brad Chen
2012/09/28 20:41:01
... then it would make sense to maybe use more of
khim
2012/09/28 23:22:06
Done.
| |
| 26 OPERAND_SIZE_128_BIT, | 26 OPERAND_SIZE_128_BIT, |
| 27 OPERAND_SIZE_256_BIT, | 27 OPERAND_SIZE_256_BIT, |
| 28 | 28 |
| 29 /* OPERAND_FLOAT_SIZE_*_BIT are used for in-memory operands. */ | 29 /* OPERAND_FLOAT_SIZE_*_BIT are used for in-memory operands. */ |
| 30 OPERAND_FLOAT_SIZE_16_BIT, | 30 OPERAND_FLOAT_SIZE_16_BIT, |
| 31 OPERAND_FLOAT_SIZE_32_BIT, | 31 OPERAND_FLOAT_SIZE_32_BIT, |
| 32 OPERAND_FLOAT_SIZE_64_BIT, | 32 OPERAND_FLOAT_SIZE_64_BIT, |
| 33 OPERAND_FLOAT_SIZE_80_BIT, | 33 OPERAND_FLOAT_SIZE_80_BIT, |
| 34 | 34 |
| 35 /* OPERAND_X87_SIZE_64_BIT are signed integers in memory.*/ | 35 /* OPERAND_X87_SIZE_64_BIT are signed integers in memory.*/ |
| 36 OPERAND_X87_SIZE_16_BIT, | 36 OPERAND_X87_SIZE_16_BIT, |
| 37 OPERAND_X87_SIZE_32_BIT, | 37 OPERAND_X87_SIZE_32_BIT, |
| 38 OPERAND_X87_SIZE_64_BIT, | 38 OPERAND_X87_SIZE_64_BIT, |
| 39 | 39 |
| 40 | 40 |
| 41 OPERAND_X87_BCD, /* 10-byte packed BCD value in memory. */ | 41 OPERAND_X87_BCD, /* 10-byte packed BCD value in memory. */ |
| 42 OPERAND_X87_ENV, /* A 14-byte or 28-byte x87 environment. */ | 42 OPERAND_X87_ENV, /* A 14-byte or 28-byte x87 environment. */ |
| 43 OPERAND_X87_STATE, /* A 94-byte or 108-byte x87 state. */ | 43 OPERAND_X87_STATE, /* A 94-byte or 108-byte x87 state. */ |
| 44 OPERAND_X87_MMX_MM_STATE, /* A 512-byte extended x87/MMX/XMM state. */ | 44 OPERAND_X87_MMX_MM_STATE, /* A 512-byte extended x87/MMX/XMM state. */ |
| 45 OPERAND_SELECTOR, /* Operand is 6/10 bytes selector in memory. */ | 45 OPERAND_SELECTOR, /* Operand is 6/10 bytes selector in memory. */ |
| 46 OPERAND_FAR_PTR, /* Operand is 6/10 bytes far pointer in memory. */ | 46 OPERAND_FAR_PTR, /* Operand is 6/10 bytes far pointer in memory. */ |
| 47 | 47 |
| 48 OPERAND_ST, /* Any X87 register. */ | 48 OPERAND_ST, /* Any X87 register. */ |
| 49 OPERAND_SEGMENT_REGISTER, /* Operand is segment register: %{e,c,s,d,f,g}s. */ | 49 OPERAND_SEGMENT_REGISTER, /* Operand is segment register: %{e,c,s,d,f,g}s. */ |
| 50 OPERAND_CONTROL_REGISTER, /* Operand is control register: %crX. */ | 50 OPERAND_CONTROL_REGISTER, /* Operand is control register: %crX. */ |
| 51 OPERAND_DEBUG_REGISTER, /* Operand is debug register: %drX. */ | 51 OPERAND_DEBUG_REGISTER, /* Operand is debug register: %drX. */ |
| 52 OPERAND_MMX, | 52 OPERAND_MMX, |
| 53 OPERAND_XMM, | 53 OPERAND_XMM, |
| 54 OPERAND_YMM | 54 OPERAND_YMM |
|
Brad Chen
2012/09/28 19:38:34
For my gratification, are there other operand_size
khim
2012/09/28 20:26:26
There are some for AVX2. AVX is currently supporte
| |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 enum register_name { | 57 enum register_name { |
| 58 /* First 16 registers are compatible with encoding of registers in x86 ABI. */ | 58 /* First 16 registers are compatible with encoding of registers in x86 ABI. */ |
| 59 REG_RAX, | 59 REG_RAX, |
| 60 REG_RCX, | 60 REG_RCX, |
| 61 REG_RDX, | 61 REG_RDX, |
| 62 REG_RBX, | 62 REG_RBX, |
| 63 REG_RSP, | 63 REG_RSP, |
| 64 REG_RBP, | 64 REG_RBP, |
| 65 REG_RSI, | 65 REG_RSI, |
| 66 REG_RDI, | 66 REG_RDI, |
| 67 REG_R8, | 67 REG_R8, |
| 68 REG_R9, | 68 REG_R9, |
| 69 REG_R10, | 69 REG_R10, |
| 70 REG_R11, | 70 REG_R11, |
| 71 REG_R12, | 71 REG_R12, |
| 72 REG_R13, | 72 REG_R13, |
| 73 REG_R14, | 73 REG_R14, |
| 74 REG_R15, | 74 REG_R15, |
| 75 /* These are pseudo-registers used in special cases. */ | 75 /* These are pseudo-registers used in special cases. */ |
|
Brad Chen
2012/09/28 20:41:01
Q: Why are these better here rather than as a sepa
khim
2012/09/28 23:22:06
They are here to simplify the operations with oper
| |
| 76 REG_RM, /* Address in memory via rm field. */ | 76 REG_RM, /* Address in memory via rm field. */ |
| 77 REG_RIP, /* RIP - used as base in x86-64 mode. */ | 77 REG_RIP, /* RIP - used as base in x86-64 mode. */ |
| 78 REG_RIZ, /* EIZ/RIZ - used as "always zero index" register. */ | 78 REG_RIZ, /* EIZ/RIZ - used as "always zero index" register. */ |
| 79 REG_IMM, /* Fixed value in imm field. */ | 79 REG_IMM, /* Fixed value in imm field. */ |
| 80 REG_IMM2, /* Fixed value in second imm field. */ | 80 REG_IMM2, /* Fixed value in second imm field. */ |
| 81 REG_DS_RBX, /* Fox xlat: %ds(%rbx). */ | 81 REG_DS_RBX, /* Fox xlat: %ds(%rbx). */ |
| 82 REG_ES_RDI, /* For string instructions: %es:(%rsi). */ | 82 REG_ES_RDI, /* For string instructions: %es:(%rsi). */ |
| 83 REG_DS_RSI, /* For string instructions: %ds:(%rdi). */ | 83 REG_DS_RSI, /* For string instructions: %ds:(%rdi). */ |
| 84 REG_PORT_DX, /* 16-bit DX: for in/out instructions. */ | 84 REG_PORT_DX, /* 16-bit DX: for in/out instructions. */ |
| 85 NO_REG, /* For modrm: both index and base can be absent. */ | 85 NO_REG, /* For modrm: both index and base can be absent. */ |
| 86 REG_ST, /* For x87 instructions: implicit %st. */ | 86 REG_ST, /* For x87 instructions: implicit %st. */ |
| 87 JMP_TO /* Operand is jump target address: usually %rip+offset. */ | 87 JMP_TO /* Operand is jump target address: usually %rip+offset. */ |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 /* | 90 /* |
| 91 * This enum extends NaClCPUFeatureID to cover instructions not recognized in | 91 * This enum extends NaClCPUFeatureID to cover instructions not recognized in |
|
Brad Chen
2012/09/28 20:41:01
Something missing here?
khim
2012/09/28 23:22:06
Yeah. CPUID support for decoder. Looks like we are
| |
| 92 * | 92 * |
| 93 * / | 93 * / |
| 94 enum DecoderCPUFeatures { | 94 enum DecoderCPUFeatures { |
| 95 }; | 95 }; |
| 96 */ | 96 */ |
| 97 | 97 |
| 98 enum disp_mode { | 98 enum disp_mode { |
|
Brad Chen
2012/09/28 20:41:01
Comment this enum please.
khim
2012/09/28 23:22:06
Done.
| |
| 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 struct instruction { |
|
Brad Chen
2012/09/28 20:41:01
Every non-trival type really merits a comment. Whi
khim
2012/09/28 23:22:06
Done.
| |
| 107 const char *name; | 107 const char *name; |
| 108 unsigned char operands_count; | 108 unsigned char operands_count; |
| 109 struct { | 109 struct { |
| 110 unsigned char rex; /* Mostly to distingush cases like %ah vs %spl. */ | 110 unsigned char rex; /* Mostly to distingush cases like %ah vs %spl. */ |
| 111 #ifdef _MSC_VER | 111 #ifdef _MSC_VER |
|
Brad Chen
2012/09/28 20:41:01
Yuck. I'm not liking the nearly-identical code her
khim
2012/09/28 23:22:06
Unfortunately we already have Bool in NaCl codebas
| |
| 112 Bool data16:1; /* "Normal", non-rex prefixes. */ | 112 Bool data16:1; /* "Normal", non-rex prefixes. */ |
| 113 Bool lock:1; | 113 Bool lock:1; |
| 114 Bool repnz:1; | 114 Bool repnz:1; |
| 115 Bool repz:1; | 115 Bool repz:1; |
| 116 Bool branch_not_taken:1; | 116 Bool branch_not_taken:1; |
| 117 Bool branch_taken:1; | 117 Bool branch_taken:1; |
| 118 #else | 118 #else |
| 119 _Bool data16:1; /* "Normal", non-rex prefixes. */ | 119 _Bool data16:1; /* "Normal", non-rex prefixes. */ |
| 120 _Bool lock:1; | 120 _Bool lock:1; |
| 121 _Bool repnz:1; | 121 _Bool repnz:1; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 139 }; | 139 }; |
| 140 | 140 |
| 141 typedef void (*process_instruction_func) (const uint8_t *begin, | 141 typedef void (*process_instruction_func) (const uint8_t *begin, |
| 142 const uint8_t *end, | 142 const uint8_t *end, |
| 143 struct instruction *instruction, | 143 struct instruction *instruction, |
| 144 void *userdata); | 144 void *userdata); |
| 145 | 145 |
| 146 typedef void (*process_decoding_error_func) (const uint8_t *ptr, | 146 typedef void (*process_decoding_error_func) (const uint8_t *ptr, |
| 147 void *userdata); | 147 void *userdata); |
| 148 | 148 |
| 149 /* All possible CPUID features enabled. */ | 149 /* All possible CPUID features enabled. */ |
|
Brad Chen
2012/09/28 20:41:01
This comment is too terse. Please mention full_cpu
khim
2012/09/28 23:22:06
Done.
| |
| 150 extern const NaClCPUFeaturesX86 full_cpuid_features; | 150 extern const NaClCPUFeaturesX86 full_cpuid_features; |
| 151 | 151 |
| 152 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 152 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
| 153 process_instruction_func process_instruction, | 153 process_instruction_func process_instruction, |
| 154 process_decoding_error_func process_error, void *userdata); | 154 process_decoding_error_func process_error, void *userdata); |
| 155 | 155 |
| 156 int DecodeChunkIA32(const uint8_t *data, size_t size, | 156 int DecodeChunkIA32(const uint8_t *data, size_t size, |
| 157 process_instruction_func process_instruction, | 157 process_instruction_func process_instruction, |
| 158 process_decoding_error_func process_error, void *userdata); | 158 process_decoding_error_func process_error, void *userdata); |
| 159 | 159 |
| 160 EXTERN_C_END | 160 EXTERN_C_END |
| 161 | 161 |
| 162 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ */ | 162 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_RAGEL_DECODER_H_ */ |
| OLD | NEW |