OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 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 * Full-blown decoder for amd64 case. Can be used to decode instruction | 8 * Full-blown decoder for amd64 case. Can be used to decode instruction |
9 * sequence and process it, but right now is only used in tests. | 9 * sequence and process it, but right now is only used in tests. |
10 * | 10 * |
11 * The code is in [hand-written] "parse_instruction.rl" and in [auto-generated] | 11 * The code is in [hand-written] "parse_instruction.rl" and in [auto-generated] |
12 * "decoder_x86_64_instruction.rl" file. This file only includes tiny amount | 12 * "decoder_x86_64_instruction.rl" file. This file only includes tiny amount |
13 * of the glue code. | 13 * of the glue code. |
14 */ | 14 */ |
15 | 15 |
16 #include <assert.h> | 16 #include <assert.h> |
17 #include <stddef.h> | 17 #include <stddef.h> |
18 #include <stdio.h> | 18 #include <stdio.h> |
19 #include <stdlib.h> | 19 #include <stdlib.h> |
20 #include <string.h> | 20 #include <string.h> |
21 | 21 |
22 #include "native_client/src/include/elf32.h" | 22 #include "native_client/src/include/elf32.h" |
23 #include "native_client/src/shared/utils/types.h" | 23 #include "native_client/src/shared/utils/types.h" |
24 #include "native_client/src/trusted/validator_ragel/unreviewed/decoder_internal.
h" | 24 #include "native_client/src/trusted/validator_ragel/decoder_internal.h" |
25 | 25 |
26 /* | 26 #define GET_REX_PREFIX() instruction.prefix.rex |
27 * These prefixes are only useful in AMD64 mode, but they will "cleaned up" by | 27 #define SET_REX_PREFIX(PREFIX_BYTE) instruction.prefix.rex = (PREFIX_BYTE) |
28 * decoder's cleanup procedure in IA32 mode anyway. That's why we define them | 28 #define GET_VEX_PREFIX2() vex_prefix2 |
29 * twice: "real" version here and "do-nothing" in decoder_x86_32.rl. | 29 #define SET_VEX_PREFIX2(PREFIX_BYTE) vex_prefix2 = (PREFIX_BYTE) |
30 */ | |
31 #define SET_REX_PREFIX(P) instruction.prefix.rex = (P) | |
32 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) | |
33 #define CLEAR_SPURIOUS_REX_B() \ | 30 #define CLEAR_SPURIOUS_REX_B() \ |
34 instruction.prefix.rex_b_spurious = FALSE | 31 instruction.prefix.rex_b_spurious = FALSE |
35 #define SET_SPURIOUS_REX_B() \ | 32 #define SET_SPURIOUS_REX_B() \ |
36 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE | 33 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE |
37 #define CLEAR_SPURIOUS_REX_X() \ | 34 #define CLEAR_SPURIOUS_REX_X() \ |
38 instruction.prefix.rex_x_spurious = FALSE | 35 instruction.prefix.rex_x_spurious = FALSE |
39 #define SET_SPURIOUS_REX_X() \ | 36 #define SET_SPURIOUS_REX_X() \ |
40 if (GET_REX_PREFIX() & REX_X) instruction.prefix.rex_x_spurious = TRUE | 37 if (GET_REX_PREFIX() & REX_X) instruction.prefix.rex_x_spurious = TRUE |
41 #define CLEAR_SPURIOUS_REX_R() \ | 38 #define CLEAR_SPURIOUS_REX_R() \ |
42 instruction.prefix.rex_r_spurious = FALSE | 39 instruction.prefix.rex_r_spurious = FALSE |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; | 86 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; |
90 include cpuid_actions | 87 include cpuid_actions |
91 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; | 88 "native_client/src/trusted/validator_ragel/parse_instruction.rl"; |
92 | 89 |
93 include decode_x86_64 "decoder_x86_64_instruction.rl"; | 90 include decode_x86_64 "decoder_x86_64_instruction.rl"; |
94 | 91 |
95 action end_of_instruction_cleanup { | 92 action end_of_instruction_cleanup { |
96 process_instruction(instruction_begin, current_position + 1, &instruction, | 93 process_instruction(instruction_begin, current_position + 1, &instruction, |
97 userdata); | 94 userdata); |
98 instruction_begin = current_position + 1; | 95 instruction_begin = current_position + 1; |
99 SET_DISP_TYPE(DISPNONE); | 96 SET_DISPLACEMENT_FORMAT(DISPNONE); |
100 SET_IMM_TYPE(IMMNONE); | 97 SET_IMMEDIATE_FORMAT(IMMNONE); |
101 SET_IMM2_TYPE(IMMNONE); | 98 SET_SECOND_IMMEDIATE_FORMAT(IMMNONE); |
102 SET_REX_PREFIX(FALSE); | 99 SET_REX_PREFIX(FALSE); |
103 SET_DATA16_PREFIX(FALSE); | 100 SET_DATA16_PREFIX(FALSE); |
104 SET_LOCK_PREFIX(FALSE); | 101 SET_LOCK_PREFIX(FALSE); |
105 SET_REPNZ_PREFIX(FALSE); | 102 SET_REPNZ_PREFIX(FALSE); |
106 SET_REPZ_PREFIX(FALSE); | 103 SET_REPZ_PREFIX(FALSE); |
107 SET_BRANCH_NOT_TAKEN(FALSE); | 104 SET_BRANCH_NOT_TAKEN(FALSE); |
108 SET_BRANCH_TAKEN(FALSE); | 105 SET_BRANCH_TAKEN(FALSE); |
109 /* | 106 /* |
110 * Top three bits of VEX2 are inverted: see AMD/Intel manual. | 107 * Top three bits of VEX2 are inverted: see AMD/Intel manual. |
111 * Pass VEX2 prefix value that corresponds to zero bits. | 108 * Pass VEX2 prefix value that corresponds to zero bits. |
(...skipping 10 matching lines...) Expand all Loading... |
122 action report_fatal_error { | 119 action report_fatal_error { |
123 process_error(current_position, userdata); | 120 process_error(current_position, userdata); |
124 result = FALSE; | 121 result = FALSE; |
125 goto error_detected; | 122 goto error_detected; |
126 } | 123 } |
127 | 124 |
128 decoder := (one_instruction @end_of_instruction_cleanup)* | 125 decoder := (one_instruction @end_of_instruction_cleanup)* |
129 $!report_fatal_error; | 126 $!report_fatal_error; |
130 }%% | 127 }%% |
131 | 128 |
| 129 /* |
| 130 * The "write data" statement causes Ragel to emit the constant static data |
| 131 * needed by the ragel machine. |
| 132 */ |
132 %% write data; | 133 %% write data; |
133 | 134 |
134 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 135 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
135 ProcessInstructionFunc process_instruction, | 136 ProcessInstructionFunc process_instruction, |
136 ProcessDecodingErrorFunc process_error, | 137 ProcessDecodingErrorFunc process_error, |
137 void *userdata) { | 138 void *userdata) { |
138 const uint8_t *current_position = data; | 139 const uint8_t *current_position = data; |
139 const uint8_t *end_of_data = data + size; | 140 const uint8_t *end_of_data = data + size; |
140 const uint8_t *instruction_begin = current_position; | 141 const uint8_t *instruction_begin = current_position; |
141 /* | 142 /* |
142 * Top three bits of VEX2 are inverted: see AMD/Intel manual. | 143 * Top three bits of VEX2 are inverted: see AMD/Intel manual. |
143 * Start with VEX2 prefix value that corresponds to zero bits. | 144 * Start with VEX2 prefix value that corresponds to zero bits. |
144 */ | 145 */ |
145 uint8_t vex_prefix2 = VEX_R | VEX_X | VEX_B; | 146 uint8_t vex_prefix2 = VEX_R | VEX_X | VEX_B; |
146 uint8_t vex_prefix3 = 0x00; | 147 uint8_t vex_prefix3 = 0x00; |
147 enum ImmediateMode imm_operand = IMMNONE; | 148 enum ImmediateMode imm_operand = IMMNONE; |
148 enum ImmediateMode imm2_operand = IMMNONE; | 149 enum ImmediateMode imm2_operand = IMMNONE; |
149 struct Instruction instruction; | 150 struct Instruction instruction; |
150 int result = TRUE; | 151 int result = TRUE; |
151 | 152 |
152 int current_state; | 153 int current_state; |
153 | 154 |
154 memset(&instruction, 0, sizeof instruction); | 155 memset(&instruction, 0, sizeof instruction); |
155 | 156 |
| 157 /* |
| 158 * The "write init" statement causes Ragel to emit initialization code. |
| 159 * This should be executed once before the ragel machine is started. |
| 160 */ |
156 %% write init; | 161 %% write init; |
| 162 /* |
| 163 * The "write exec" statement causes Ragel to emit the ragel machine's |
| 164 * execution code. |
| 165 */ |
157 %% write exec; | 166 %% write exec; |
158 | 167 |
159 error_detected: | 168 error_detected: |
160 return result; | 169 return result; |
161 } | 170 } |
OLD | NEW |