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 /* |
27 * These prefixes are only useful in AMD64 mode, but they will "cleaned up" by | 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 | 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. | 29 * twice: "real" version here and "do-nothing" in decoder_x86_32.rl. |
30 */ | 30 */ |
31 #define SET_REX_PREFIX(P) instruction.prefix.rex = (P) | 31 #define SET_REX_PREFIX(P) instruction.prefix.rex = (P) |
32 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) | 32 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) |
33 #define CLEAR_SPURIOUS_REX_B() \ | 33 #define CLEAR_SPURIOUS_REX_B() \ |
34 instruction.prefix.rex_b_spurious = FALSE | 34 instruction.prefix.rex_b_spurious = FALSE |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 93 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
94 | 94 |
95 include decode_x86_64 "decoder_x86_64_instruction.rl"; | 95 include decode_x86_64 "decoder_x86_64_instruction.rl"; |
96 | 96 |
97 include decoder | 97 include decoder |
98 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 98 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
99 | 99 |
100 main := decoder; | 100 main := decoder; |
101 }%% | 101 }%% |
102 | 102 |
| 103 /* |
| 104 * The "write data" statement causes Ragel to emit the constant static data |
| 105 * needed by the ragel machine. |
| 106 */ |
103 %% write data; | 107 %% write data; |
104 | 108 |
105 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 109 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
106 ProcessInstructionFunc process_instruction, | 110 ProcessInstructionFunc process_instruction, |
107 ProcessDecodingErrorFunc process_error, | 111 ProcessDecodingErrorFunc process_error, |
108 void *userdata) { | 112 void *userdata) { |
109 const uint8_t *current_position = data; | 113 const uint8_t *current_position = data; |
110 const uint8_t *end_of_data = data + size; | 114 const uint8_t *end_of_data = data + size; |
111 const uint8_t *instruction_begin = current_position; | 115 const uint8_t *instruction_begin = current_position; |
112 uint8_t vex_prefix2 = 0xe0; | 116 uint8_t vex_prefix2 = 0xe0; |
(...skipping 14 matching lines...) Expand all Loading... |
127 SET_REPNZ_PREFIX(FALSE); | 131 SET_REPNZ_PREFIX(FALSE); |
128 SET_REPZ_PREFIX(FALSE); | 132 SET_REPZ_PREFIX(FALSE); |
129 SET_BRANCH_NOT_TAKEN(FALSE); | 133 SET_BRANCH_NOT_TAKEN(FALSE); |
130 SET_BRANCH_TAKEN(FALSE); | 134 SET_BRANCH_TAKEN(FALSE); |
131 SET_ATT_INSTRUCTION_SUFFIX(NULL); | 135 SET_ATT_INSTRUCTION_SUFFIX(NULL); |
132 instruction.prefix.rex_b_spurious = FALSE; | 136 instruction.prefix.rex_b_spurious = FALSE; |
133 instruction.prefix.rex_x_spurious = FALSE; | 137 instruction.prefix.rex_x_spurious = FALSE; |
134 instruction.prefix.rex_r_spurious = FALSE; | 138 instruction.prefix.rex_r_spurious = FALSE; |
135 instruction.prefix.rex_w_spurious = FALSE; | 139 instruction.prefix.rex_w_spurious = FALSE; |
136 | 140 |
| 141 /* |
| 142 * The "write init" statement causes Ragel to emit initialization code. |
| 143 * This should be executed once before the ragel machine is started. |
| 144 */ |
137 %% write init; | 145 %% write init; |
| 146 /* |
| 147 * The "write exec" statement causes Ragel to emit the ragel machine's |
| 148 * execution code. |
| 149 */ |
138 %% write exec; | 150 %% write exec; |
139 | 151 |
140 error_detected: | 152 error_detected: |
141 return result; | 153 return result; |
142 } | 154 } |
OLD | NEW |