OLD | NEW |
| (Empty) |
1 /* | |
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 | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
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. | |
10 * | |
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 | |
13 * of the glue code. | |
14 */ | |
15 | |
16 #include <assert.h> | |
17 #include <stddef.h> | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 #include <string.h> | |
21 | |
22 #include "native_client/src/include/elf32.h" | |
23 #include "native_client/src/shared/utils/types.h" | |
24 #include "native_client/src/trusted/validator_ragel/unreviewed/decoder_internal.
h" | |
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) | |
32 #define SET_VEX_PREFIX2(P) vex_prefix2 = (P) | |
33 #define CLEAR_SPURIOUS_REX_B() \ | |
34 instruction.prefix.rex_b_spurious = FALSE | |
35 #define SET_SPURIOUS_REX_B() \ | |
36 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE | |
37 #define CLEAR_SPURIOUS_REX_X() \ | |
38 instruction.prefix.rex_x_spurious = FALSE | |
39 #define SET_SPURIOUS_REX_X() \ | |
40 if (GET_REX_PREFIX() & REX_X) instruction.prefix.rex_x_spurious = TRUE | |
41 #define CLEAR_SPURIOUS_REX_R() \ | |
42 instruction.prefix.rex_r_spurious = FALSE | |
43 #define SET_SPURIOUS_REX_R() \ | |
44 if (GET_REX_PREFIX() & REX_R) instruction.prefix.rex_r_spurious = TRUE | |
45 #define CLEAR_SPURIOUS_REX_W() \ | |
46 instruction.prefix.rex_w_spurious = FALSE | |
47 #define SET_SPURIOUS_REX_W() \ | |
48 if (GET_REX_PREFIX() & REX_W) instruction.prefix.rex_w_spurious = TRUE | |
49 | |
50 %%{ | |
51 machine x86_64_decoder; | |
52 alphtype unsigned char; | |
53 variable p current_position; | |
54 variable pe end_of_data; | |
55 variable eof end_of_data; | |
56 variable cs current_state; | |
57 | |
58 include byte_machine "byte_machines.rl"; | |
59 | |
60 include prefixes_parsing_decoder | |
61 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
62 include rex_actions | |
63 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
64 include rex_parsing | |
65 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
66 include vex_actions_amd64 | |
67 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
68 include vex_parsing_amd64 | |
69 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
70 include att_suffix_actions | |
71 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
72 include set_spurious_prefixes | |
73 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
74 include displacement_fields_actions | |
75 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
76 include displacement_fields_parsing | |
77 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
78 include modrm_actions_amd64 | |
79 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
80 include modrm_parsing | |
81 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
82 include operand_actions_amd64 | |
83 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
84 include immediate_fields_actions | |
85 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
86 include immediate_fields_parsing_amd64 | |
87 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
88 include relative_fields_decoder_actions | |
89 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
90 include relative_fields_parsing | |
91 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
92 include cpuid_actions | |
93 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
94 | |
95 include decode_x86_64 "decoder_x86_64_instruction.rl"; | |
96 | |
97 include decoder | |
98 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
99 | |
100 main := decoder; | |
101 }%% | |
102 | |
103 %% write data; | |
104 | |
105 int DecodeChunkAMD64(const uint8_t *data, size_t size, | |
106 ProcessInstructionFunc process_instruction, | |
107 ProcessDecodingErrorFunc process_error, | |
108 void *userdata) { | |
109 const uint8_t *current_position = data; | |
110 const uint8_t *end_of_data = data + size; | |
111 const uint8_t *instruction_begin = current_position; | |
112 uint8_t vex_prefix2 = 0xe0; | |
113 uint8_t vex_prefix3 = 0x00; | |
114 enum ImmediateMode imm_operand = IMMNONE; | |
115 enum ImmediateMode imm2_operand = IMMNONE; | |
116 struct Instruction instruction; | |
117 int result = TRUE; | |
118 | |
119 int current_state; | |
120 | |
121 SET_DISP_TYPE(DISPNONE); | |
122 SET_IMM_TYPE(IMMNONE); | |
123 SET_IMM2_TYPE(IMMNONE); | |
124 SET_REX_PREFIX(FALSE); | |
125 SET_DATA16_PREFIX(FALSE); | |
126 SET_LOCK_PREFIX(FALSE); | |
127 SET_REPNZ_PREFIX(FALSE); | |
128 SET_REPZ_PREFIX(FALSE); | |
129 SET_BRANCH_NOT_TAKEN(FALSE); | |
130 SET_BRANCH_TAKEN(FALSE); | |
131 SET_ATT_INSTRUCTION_SUFFIX(NULL); | |
132 instruction.prefix.rex_b_spurious = FALSE; | |
133 instruction.prefix.rex_x_spurious = FALSE; | |
134 instruction.prefix.rex_r_spurious = FALSE; | |
135 instruction.prefix.rex_w_spurious = FALSE; | |
136 | |
137 %% write init; | |
138 %% write exec; | |
139 | |
140 error_detected: | |
141 return result; | |
142 } | |
OLD | NEW |