OLD | NEW |
| (Empty) |
1 /* | |
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 | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
8 * Full-blown decoder for ia32 case. Can be used to decode instruction sequence | |
9 * 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_32_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/shared/utils/types.h" | |
23 #include "native_client/src/trusted/validator_ragel/unreviewed/decoder_internal.
h" | |
24 | |
25 /* | |
26 * These prefixes are not useful in IA32 mode, but they will "cleaned up" by | |
27 * decoder's cleanup procedure anyway. Do nothing when that happens. | |
28 */ | |
29 #define SET_REX_PREFIX(P) | |
30 #define SET_VEX_PREFIX2(P) | |
31 #define CLEAR_SPURIOUS_REX_B() | |
32 #define SET_SPURIOUS_REX_B() | |
33 #define CLEAR_SPURIOUS_REX_X() | |
34 #define SET_SPURIOUS_REX_X() | |
35 #define CLEAR_SPURIOUS_REX_R() | |
36 #define SET_SPURIOUS_REX_R() | |
37 #define CLEAR_SPURIOUS_REX_W() | |
38 #define SET_SPURIOUS_REX_W() | |
39 | |
40 %%{ | |
41 machine x86_32_decoder; | |
42 alphtype unsigned char; | |
43 variable p current_position; | |
44 variable pe end_of_data; | |
45 variable eof end_of_data; | |
46 variable cs current_state; | |
47 | |
48 include byte_machine "byte_machines.rl"; | |
49 | |
50 include prefixes_parsing_decoder | |
51 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
52 include vex_actions_ia32 | |
53 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
54 include vex_parsing_ia32 | |
55 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
56 include att_suffix_actions | |
57 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
58 include set_spurious_prefixes | |
59 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
60 include displacement_fields_actions | |
61 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
62 include displacement_fields_parsing | |
63 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
64 include modrm_actions_ia32 | |
65 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
66 include modrm_parsing | |
67 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
68 include operand_actions_ia32 | |
69 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
70 include immediate_fields_actions | |
71 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
72 include immediate_fields_parsing_ia32 | |
73 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
74 include relative_fields_decoder_actions | |
75 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
76 include relative_fields_parsing | |
77 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
78 include cpuid_actions | |
79 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
80 | |
81 include decode_x86_32 "decoder_x86_32_instruction.rl"; | |
82 | |
83 include decoder | |
84 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
85 | |
86 main := decoder; | |
87 }%% | |
88 | |
89 %% write data; | |
90 | |
91 int DecodeChunkIA32(const uint8_t *data, size_t size, | |
92 ProcessInstructionFunc process_instruction, | |
93 ProcessDecodingErrorFunc process_error, void *userdata) { | |
94 const uint8_t *current_position = data; | |
95 const uint8_t *end_of_data = data + size; | |
96 const uint8_t *instruction_begin = current_position; | |
97 uint8_t vex_prefix3 = 0x00; | |
98 enum ImmediateMode imm_operand = IMMNONE; | |
99 enum ImmediateMode imm2_operand = IMMNONE; | |
100 struct Instruction instruction; | |
101 int result = TRUE; | |
102 | |
103 int current_state; | |
104 | |
105 /* Not used in ia32_mode. */ | |
106 instruction.prefix.rex = 0; | |
107 | |
108 SET_DISP_TYPE(DISPNONE); | |
109 SET_IMM_TYPE(IMMNONE); | |
110 SET_IMM2_TYPE(IMMNONE); | |
111 SET_DATA16_PREFIX(FALSE); | |
112 SET_LOCK_PREFIX(FALSE); | |
113 SET_REPNZ_PREFIX(FALSE); | |
114 SET_REPZ_PREFIX(FALSE); | |
115 SET_BRANCH_NOT_TAKEN(FALSE); | |
116 SET_BRANCH_TAKEN(FALSE); | |
117 SET_ATT_INSTRUCTION_SUFFIX(NULL); | |
118 instruction.prefix.rex_b_spurious = FALSE; | |
119 instruction.prefix.rex_x_spurious = FALSE; | |
120 instruction.prefix.rex_r_spurious = FALSE; | |
121 instruction.prefix.rex_w_spurious = FALSE; | |
122 | |
123 %% write init; | |
124 %% write exec; | |
125 | |
126 error_detected: | |
127 return result; | |
128 } | |
OLD | NEW |