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 /* | |
27 * These prefixes are only useful in AMD64 mode, but they will "cleaned up" by | |
28 * decoder's cleanup procedure in IA32 mode anyway. That's why we define them | |
29 * twice: "real" version here and "do-nothing" in decoder_x86_32.rl. | |
30 */ | |
31 #define SET_REX_PREFIX(P) instruction.prefix.rex = (P) | 26 #define SET_REX_PREFIX(P) instruction.prefix.rex = (P) |
32 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) | 27 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) |
33 #define CLEAR_SPURIOUS_REX_B() \ | 28 #define CLEAR_SPURIOUS_REX_B() \ |
34 instruction.prefix.rex_b_spurious = FALSE | 29 instruction.prefix.rex_b_spurious = FALSE |
35 #define SET_SPURIOUS_REX_B() \ | 30 #define SET_SPURIOUS_REX_B() \ |
36 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE | 31 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE |
37 #define CLEAR_SPURIOUS_REX_X() \ | 32 #define CLEAR_SPURIOUS_REX_X() \ |
38 instruction.prefix.rex_x_spurious = FALSE | 33 instruction.prefix.rex_x_spurious = FALSE |
39 #define SET_SPURIOUS_REX_X() \ | 34 #define SET_SPURIOUS_REX_X() \ |
40 if (GET_REX_PREFIX() & REX_X) instruction.prefix.rex_x_spurious = TRUE | 35 if (GET_REX_PREFIX() & REX_X) instruction.prefix.rex_x_spurious = TRUE |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 88 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
94 | 89 |
95 include decode_x86_64 "decoder_x86_64_instruction.rl"; | 90 include decode_x86_64 "decoder_x86_64_instruction.rl"; |
96 | 91 |
97 include decoder | 92 include decoder |
98 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 93 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
99 | 94 |
100 main := decoder; | 95 main := decoder; |
101 }%% | 96 }%% |
102 | 97 |
| 98 /* |
| 99 * The "write data" statement causes Ragel to emit the constant static data |
| 100 * needed by the ragel machine. |
| 101 */ |
103 %% write data; | 102 %% write data; |
104 | 103 |
105 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 104 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
106 ProcessInstructionFunc process_instruction, | 105 ProcessInstructionFunc process_instruction, |
107 ProcessDecodingErrorFunc process_error, | 106 ProcessDecodingErrorFunc process_error, |
108 void *userdata) { | 107 void *userdata) { |
109 const uint8_t *current_position = data; | 108 const uint8_t *current_position = data; |
110 const uint8_t *end_of_data = data + size; | 109 const uint8_t *end_of_data = data + size; |
111 const uint8_t *instruction_begin = current_position; | 110 const uint8_t *instruction_begin = current_position; |
112 uint8_t vex_prefix2 = 0xe0; | 111 uint8_t vex_prefix2 = 0xe0; |
113 uint8_t vex_prefix3 = 0x00; | 112 uint8_t vex_prefix3 = 0x00; |
114 enum ImmediateMode imm_operand = IMMNONE; | 113 enum ImmediateMode imm_operand = IMMNONE; |
115 enum ImmediateMode imm2_operand = IMMNONE; | 114 enum ImmediateMode imm2_operand = IMMNONE; |
116 struct Instruction instruction; | 115 struct Instruction instruction; |
117 int result = TRUE; | 116 int result = TRUE; |
118 | 117 |
119 int current_state; | 118 int current_state; |
120 | 119 |
121 SET_DISP_TYPE(DISPNONE); | 120 SET_DISP_TYPE(DISPNONE); |
122 SET_IMM_TYPE(IMMNONE); | 121 SET_IMM_TYPE(IMMNONE); |
123 SET_IMM2_TYPE(IMMNONE); | 122 SET_SECOND_IMM_TYPE(IMMNONE); |
124 SET_REX_PREFIX(FALSE); | 123 SET_REX_PREFIX(FALSE); |
125 SET_DATA16_PREFIX(FALSE); | 124 SET_DATA16_PREFIX(FALSE); |
126 SET_LOCK_PREFIX(FALSE); | 125 SET_LOCK_PREFIX(FALSE); |
127 SET_REPNZ_PREFIX(FALSE); | 126 SET_REPNZ_PREFIX(FALSE); |
128 SET_REPZ_PREFIX(FALSE); | 127 SET_REPZ_PREFIX(FALSE); |
129 SET_BRANCH_NOT_TAKEN(FALSE); | 128 SET_BRANCH_NOT_TAKEN(FALSE); |
130 SET_BRANCH_TAKEN(FALSE); | 129 SET_BRANCH_TAKEN(FALSE); |
131 SET_ATT_INSTRUCTION_SUFFIX(NULL); | 130 SET_ATT_INSTRUCTION_SUFFIX(NULL); |
132 instruction.prefix.rex_b_spurious = FALSE; | 131 instruction.prefix.rex_b_spurious = FALSE; |
133 instruction.prefix.rex_x_spurious = FALSE; | 132 instruction.prefix.rex_x_spurious = FALSE; |
134 instruction.prefix.rex_r_spurious = FALSE; | 133 instruction.prefix.rex_r_spurious = FALSE; |
135 instruction.prefix.rex_w_spurious = FALSE; | 134 instruction.prefix.rex_w_spurious = FALSE; |
136 | 135 |
| 136 /* |
| 137 * The "write init" statement causes Ragel to emit initialization code. |
| 138 * This should be executed once before the ragel machine is started. |
| 139 */ |
137 %% write init; | 140 %% write init; |
| 141 /* |
| 142 * The "write exec" statement causes Ragel to emit the ragel machine's |
| 143 * execution code. |
| 144 */ |
138 %% write exec; | 145 %% write exec; |
139 | 146 |
140 error_detected: | 147 error_detected: |
141 return result; | 148 return result; |
142 } | 149 } |
OLD | NEW |