Chromium Code Reviews| 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 |
|
halyavin
2013/03/13 15:48:32
Change this comment.
khim
2013/03/19 14:54:46
Done.
| |
| 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 |
| 35 #define SET_SPURIOUS_REX_B() \ | 35 #define SET_SPURIOUS_REX_B() \ |
| 36 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE | 36 if (GET_REX_PREFIX() & REX_B) instruction.prefix.rex_b_spurious = TRUE |
| 37 #define CLEAR_SPURIOUS_REX_X() \ | 37 #define CLEAR_SPURIOUS_REX_X() \ |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 95 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
| 96 | 96 |
| 97 include decode_x86_64 "decoder_x86_64_instruction.rl"; | 97 include decode_x86_64 "decoder_x86_64_instruction.rl"; |
| 98 | 98 |
| 99 include decoder | 99 include decoder |
| 100 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 100 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
| 101 | 101 |
| 102 main := decoder; | 102 main := decoder; |
| 103 }%% | 103 }%% |
| 104 | 104 |
| 105 /* | |
| 106 * The "write data" statement causes Ragel to emit the constant static data | |
| 107 * needed by the ragel machine. | |
| 108 */ | |
| 105 %% write data; | 109 %% write data; |
| 106 | 110 |
| 107 int DecodeChunkAMD64(const uint8_t *data, size_t size, | 111 int DecodeChunkAMD64(const uint8_t *data, size_t size, |
| 108 ProcessInstructionFunc process_instruction, | 112 ProcessInstructionFunc process_instruction, |
| 109 ProcessDecodingErrorFunc process_error, | 113 ProcessDecodingErrorFunc process_error, |
| 110 void *userdata) { | 114 void *userdata) { |
| 111 const uint8_t *current_position = data; | 115 const uint8_t *current_position = data; |
| 112 const uint8_t *end_of_data = data + size; | 116 const uint8_t *end_of_data = data + size; |
| 113 const uint8_t *instruction_begin = current_position; | 117 const uint8_t *instruction_begin = current_position; |
| 114 uint8_t vex_prefix2 = 0xe0; | 118 uint8_t vex_prefix2 = 0xe0; |
|
halyavin
2013/03/13 15:48:32
0xe0 -> VEX_R | VEX_X | VEX_B
khim
2013/03/19 14:54:46
Done: https://codereview.chromium.org/12716018
| |
| 115 uint8_t vex_prefix3 = 0x00; | 119 uint8_t vex_prefix3 = 0x00; |
| 116 enum ImmediateMode imm_operand = IMMNONE; | 120 enum ImmediateMode imm_operand = IMMNONE; |
| 117 enum ImmediateMode imm2_operand = IMMNONE; | 121 enum ImmediateMode imm2_operand = IMMNONE; |
| 118 struct Instruction instruction; | 122 struct Instruction instruction; |
| 119 int result = TRUE; | 123 int result = TRUE; |
| 120 | 124 |
| 121 int current_state; | 125 int current_state; |
| 122 | 126 |
| 123 SET_DISP_TYPE(DISPNONE); | 127 SET_DISP_TYPE(DISPNONE); |
| 124 SET_IMM_TYPE(IMMNONE); | 128 SET_IMM_TYPE(IMMNONE); |
| 125 SET_IMM2_TYPE(IMMNONE); | 129 SET_IMM2_TYPE(IMMNONE); |
| 126 SET_REX_PREFIX(FALSE); | 130 SET_REX_PREFIX(FALSE); |
| 127 SET_DATA16_PREFIX(FALSE); | 131 SET_DATA16_PREFIX(FALSE); |
| 128 SET_LOCK_PREFIX(FALSE); | 132 SET_LOCK_PREFIX(FALSE); |
| 129 SET_REPNZ_PREFIX(FALSE); | 133 SET_REPNZ_PREFIX(FALSE); |
| 130 SET_REPZ_PREFIX(FALSE); | 134 SET_REPZ_PREFIX(FALSE); |
| 131 SET_BRANCH_NOT_TAKEN(FALSE); | 135 SET_BRANCH_NOT_TAKEN(FALSE); |
| 132 SET_BRANCH_TAKEN(FALSE); | 136 SET_BRANCH_TAKEN(FALSE); |
| 133 SET_ATT_INSTRUCTION_SUFFIX(NULL); | 137 SET_ATT_INSTRUCTION_SUFFIX(NULL); |
| 134 instruction.prefix.rex_b_spurious = FALSE; | 138 instruction.prefix.rex_b_spurious = FALSE; |
| 135 instruction.prefix.rex_x_spurious = FALSE; | 139 instruction.prefix.rex_x_spurious = FALSE; |
| 136 instruction.prefix.rex_r_spurious = FALSE; | 140 instruction.prefix.rex_r_spurious = FALSE; |
| 137 instruction.prefix.rex_w_spurious = FALSE; | 141 instruction.prefix.rex_w_spurious = FALSE; |
| 138 | 142 |
| 143 /* | |
| 144 * The "write init" statement causes Ragel to emit initialization code. | |
| 145 * This should be executed once before the ragel machine is started. | |
| 146 */ | |
| 139 %% write init; | 147 %% write init; |
| 148 /* | |
| 149 * The "write exec" statement causes Ragel to emit the ragel machine's | |
| 150 * execution code. | |
| 151 */ | |
| 140 %% write exec; | 152 %% write exec; |
| 141 | 153 |
| 142 error_detected: | 154 error_detected: |
| 143 return result; | 155 return result; |
| 144 } | 156 } |
| OLD | NEW |