| 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 prefix_actions | |
| 61 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 62 include prefixes_parsing | |
| 63 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 64 include rex_actions | |
| 65 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 66 include rex_parsing | |
| 67 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 68 include vex_actions_amd64 | |
| 69 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 70 include vex_parsing_amd64 | |
| 71 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 72 include att_suffix_actions | |
| 73 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 74 include set_spurious_prefixes | |
| 75 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 76 include displacement_fields_actions | |
| 77 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 78 include displacement_fields_parsing | |
| 79 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 80 include modrm_actions_amd64 | |
| 81 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 82 include modrm_parsing_amd64 | |
| 83 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 84 include operand_actions_amd64 | |
| 85 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 86 include immediate_fields_actions | |
| 87 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 88 include immediate_fields_parsing_amd64 | |
| 89 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 90 include relative_fields_decoder_actions | |
| 91 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 92 include relative_fields_parsing | |
| 93 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 94 include cpuid_actions | |
| 95 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 96 | |
| 97 include decode_x86_64 "decoder_x86_64_instruction.rl"; | |
| 98 | |
| 99 include decoder | |
| 100 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | |
| 101 | |
| 102 main := decoder; | |
| 103 }%% | |
| 104 | |
| 105 %% write data; | |
| 106 | |
| 107 int DecodeChunkAMD64(const uint8_t *data, size_t size, | |
| 108 ProcessInstructionFunc process_instruction, | |
| 109 ProcessDecodingErrorFunc process_error, | |
| 110 void *userdata) { | |
| 111 const uint8_t *current_position = data; | |
| 112 const uint8_t *end_of_data = data + size; | |
| 113 const uint8_t *instruction_begin = current_position; | |
| 114 uint8_t vex_prefix2 = 0xe0; | |
| 115 uint8_t vex_prefix3 = 0x00; | |
| 116 enum ImmediateMode imm_operand = IMMNONE; | |
| 117 enum ImmediateMode imm2_operand = IMMNONE; | |
| 118 struct Instruction instruction; | |
| 119 int result = TRUE; | |
| 120 | |
| 121 int current_state; | |
| 122 | |
| 123 SET_DISP_TYPE(DISPNONE); | |
| 124 SET_IMM_TYPE(IMMNONE); | |
| 125 SET_IMM2_TYPE(IMMNONE); | |
| 126 SET_REX_PREFIX(FALSE); | |
| 127 SET_DATA16_PREFIX(FALSE); | |
| 128 SET_LOCK_PREFIX(FALSE); | |
| 129 SET_REPNZ_PREFIX(FALSE); | |
| 130 SET_REPZ_PREFIX(FALSE); | |
| 131 SET_BRANCH_NOT_TAKEN(FALSE); | |
| 132 SET_BRANCH_TAKEN(FALSE); | |
| 133 SET_ATT_INSTRUCTION_SUFFIX(NULL); | |
| 134 instruction.prefix.rex_b_spurious = FALSE; | |
| 135 instruction.prefix.rex_x_spurious = FALSE; | |
| 136 instruction.prefix.rex_r_spurious = FALSE; | |
| 137 instruction.prefix.rex_w_spurious = FALSE; | |
| 138 | |
| 139 %% write init; | |
| 140 %% write exec; | |
| 141 | |
| 142 error_detected: | |
| 143 return result; | |
| 144 } | |
| OLD | NEW |