| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2024 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "fcml_disassembler.h" |
| 21 |
|
| 22 |
#include <stddef.h> |
| 23 |
|
| 24 |
#include <fcml_errors.h> |
| 25 |
|
| 26 |
#include "fcml_coll.h" |
| 27 |
#include "fcml_decoding_tree.h" |
| 28 |
#include "fcml_def.h" |
| 29 |
#include "fcml_env_int.h" |
| 30 |
#include "fcml_hints.h" |
| 31 |
#include "fcml_mnemonic_parser.h" |
| 32 |
#include "fcml_modrm.h" |
| 33 |
#include "fcml_modrm_decoder.h" |
| 34 |
#include "fcml_stream.h" |
| 35 |
#include "fcml_utils.h" |
| 36 |
#include "fcml_dialect_int.h" |
| 37 |
#include "fcml_trace.h" |
| 38 |
#include "fcml_messages.h" |
| 39 |
#include "fcml_operand_decorators.h" |
| 40 |
#include "fcml_trace.h" |
| 41 |
|
| 42 |
/* R,X and B are stored in 1's complement form.*/ |
| 43 |
#define FCML_VEX_W(x) FCML_TP_GET_BIT(x, 7) |
| 44 |
#define FCML_VEX_R(x) !FCML_TP_GET_BIT(x, 7) |
| 45 |
#define FCML_VEX_X(x) !FCML_TP_GET_BIT(x, 6) |
| 46 |
#define FCML_VEX_B(x) !FCML_TP_GET_BIT(x, 5) |
| 47 |
#define FCML_VEX_L(x) FCML_TP_GET_BIT(x, 2) |
| 48 |
#define FCML_VEX_MMMM(x) (x & 0x1F) |
| 49 |
#define FCML_VEX_VVVV(x) (~((x & 0x78) >> 3) & 0x00F) |
| 50 |
#define FCML_VEX_PP(x) (x & 0x03) |
| 51 |
|
| 52 |
/* EVEX prefix fields. */ |
| 53 |
#define FCML_EVEX_R(x) !FCML_TP_GET_BIT(x, 7) |
| 54 |
#define FCML_EVEX_X(x) !FCML_TP_GET_BIT(x, 6) |
| 55 |
#define FCML_EVEX_B(x) !FCML_TP_GET_BIT(x, 5) |
| 56 |
#define FCML_EVEX_R_PRIM(x) !FCML_TP_GET_BIT(x, 4) |
| 57 |
#define FCML_EVEX_MM(x) (x & 0x03) |
| 58 |
#define FCML_EVEX_W(x) FCML_TP_GET_BIT(x, 7) |
| 59 |
#define FCML_EVEX_VVVV(x) FCML_VEX_VVVV(x) |
| 60 |
#define FCML_EVEX_PP(x) (x & 0x03) |
| 61 |
#define FCML_EVEX_z(x) FCML_TP_GET_BIT(x, 7) |
| 62 |
#define FCML_EVEX_L_PRIM(x) FCML_TP_GET_BIT(x, 6) |
| 63 |
#define FCML_EVEX_L(x) FCML_TP_GET_BIT(x, 5) |
| 64 |
#define FCML_EVEX_b(x) FCML_TP_GET_BIT(x, 4) |
| 65 |
#define FCML_EVEX_V_PRIM(x) !FCML_TP_GET_BIT(x, 3) |
| 66 |
#define FCML_EVEX_aaa(x) (x & 0x07) |
| 67 |
|
| 68 |
/* REX prefix fields.*/ |
| 69 |
#define FCML_REX_W(x) FCML_TP_GET_BIT(x, 3) |
| 70 |
#define FCML_REX_R(x) FCML_TP_GET_BIT(x, 2) |
| 71 |
#define FCML_REX_X(x) FCML_TP_GET_BIT(x, 1) |
| 72 |
#define FCML_REX_B(x) FCML_TP_GET_BIT(x, 0) |
| 73 |
|
| 74 |
typedef struct operand_wrapper { |
| 75 |
fcml_st_operand operand; |
| 76 |
fcml_en_access_mode access_mode; |
| 77 |
} operand_wrapper; |
| 78 |
|
| 79 |
/* Definition is located below.*/ |
| 80 |
struct inst_decoding_def; |
| 81 |
|
| 82 |
typedef struct decoding_context { |
| 83 |
fcml_st_disassembler_context *disassembler_context; |
| 84 |
fcml_st_ceh_error_container errors; |
| 85 |
fcml_st_memory_stream *stream; |
| 86 |
fcml_st_mp_mnemonic_set *mnemonics; |
| 87 |
fcml_en_pseudo_operations pseudo_op; |
| 88 |
fcml_uint64_t instruction_group; |
| 89 |
fcml_en_instruction instruction; |
| 90 |
fcml_uint16_t addr_mode; |
| 91 |
fcml_usize effective_address_size_attribute; |
| 92 |
fcml_usize effective_operand_size_attribute; |
| 93 |
/* Size of SIMD element. */ |
| 94 |
fcml_usize element_size; |
| 95 |
/* Size of SIMD vector length calculated basing on EVEX.L'L/VEX.L/XOP.L */ |
| 96 |
fcml_usize vector_length; |
| 97 |
fcml_int calculated_instruction_size; |
| 98 |
fcml_uint8_t opcodes[FCML_OPCODES_NUM]; |
| 99 |
fcml_uint8_t primary_opcode_byte; |
| 100 |
fcml_int opcodes_count; |
| 101 |
fcml_int virtual_opcodes_count; |
| 102 |
fcml_st_prefixes_details prefixes; |
| 103 |
operand_wrapper operand_wrappers[FCML_OPERANDS_COUNT]; |
| 104 |
fcml_st_modrm decoded_modrm; |
| 105 |
fcml_st_modrm_details decoded_modrm_details; |
| 106 |
/* True if ModR/M is available for instruction. */ |
| 107 |
fcml_bool is_modrm; |
| 108 |
/* True if ModR/M is used by register to register operation. */ |
| 109 |
fcml_bool is_modrm_reg_reg; |
| 110 |
fcml_hints instruction_hints; |
| 111 |
fcml_nuint8_t pseudo_opcode; |
| 112 |
fcml_nuint8_t suffix; |
| 113 |
/* Opcode fields.*/ |
| 114 |
fcml_st_condition condition; |
| 115 |
fcml_bool is_conditional; |
| 116 |
fcml_bool opcode_field_w_bit; |
| 117 |
fcml_bool opcode_field_s_bit; |
| 118 |
/* is4/is5 byte.*/ |
| 119 |
fcml_nuint8_t isX_byte; |
| 120 |
/* Instruction tuple type used to calculate disp8 */ |
| 121 |
fcml_uint8_t tuple_type; |
| 122 |
/* Currently proceeded decoding definition.*/ |
| 123 |
struct inst_decoding_def *decoding_def; |
| 124 |
} decoding_context; |
| 125 |
|
| 126 |
typedef struct disassembler { |
| 127 |
fcml_st_dialect_context_int *dialect_context; |
| 128 |
fcml_st_dt_decoding_tree *decoding_tree; |
| 129 |
} disassembler; |
| 130 |
|
| 131 |
/* Decoders responsible for operand disassembling. */ |
| 132 |
typedef fcml_ceh_error (*operand_decoder)(struct decoding_context *context, |
| 133 |
operand_wrapper *operand, fcml_operand_decorators decorators, |
| 134 |
fcml_ptr args); |
| 135 |
|
| 136 |
/* Calculates the size of the encoded operand in bytes. */ |
| 137 |
typedef fcml_int (*operand_size_calculator)(struct decoding_context *context, |
| 138 |
fcml_ptr args); |
| 139 |
|
| 140 |
/* Decoders responsible for instruction disassembling. */ |
| 141 |
typedef fcml_ceh_error (*instruction_decoder)(decoding_context *context); |
| 142 |
|
| 143 |
typedef fcml_bool (*instruction_acceptor)(decoding_context *context); |
| 144 |
|
| 145 |
typedef struct addr_mode_acceptor_chain { |
| 146 |
struct addr_mode_acceptor_chain *next; |
| 147 |
fcml_string name; |
| 148 |
instruction_acceptor acceptor; |
| 149 |
} addr_mode_acceptor_chain; |
| 150 |
|
| 151 |
typedef struct operand_decoding { |
| 152 |
/* Operand access mode. */ |
| 153 |
fcml_st_def_decoded_addr_mode *decoded_addr_mode; |
| 154 |
/* Operand decoder. */ |
| 155 |
operand_decoder decoder; |
| 156 |
/* Calculates size of the encoded operand in bytes. */ |
| 157 |
operand_size_calculator size_calculator; |
| 158 |
/* Optional hints, if operand has any. */ |
| 159 |
fcml_hints hints; |
| 160 |
/* Operand decorators. */ |
| 161 |
fcml_operand_decorators decorators; |
| 162 |
} operand_decoding; |
| 163 |
|
| 164 |
typedef struct modrm_decoding_details { |
| 165 |
/* True if vector-index memory addressing is used. */ |
| 166 |
fcml_bool is_vsib; |
| 167 |
/* Vector register size. */ |
| 168 |
fcml_usize vsib_index_size; |
| 169 |
/* True if addressing mode is restricted only to register. */ |
| 170 |
fcml_bool is_reg_restriction; |
| 171 |
/* True if addressing mode is restricted only to memory. */ |
| 172 |
fcml_bool is_mem_restriction; |
| 173 |
} modrm_decoding_details; |
| 174 |
|
| 175 |
typedef struct inst_decoding_def { |
| 176 |
/* Instruction code. */ |
| 177 |
fcml_en_instruction instruction; |
| 178 |
/* Addressing mode.*/ |
| 179 |
fcml_uint16_t addr_mode; |
| 180 |
/* Additional details for addressing mode. */ |
| 181 |
fcml_uint32_t details; |
| 182 |
/* Type of the instruction. */ |
| 183 |
fcml_uint64_t instruction_group; |
| 184 |
/* Opcodes. */ |
| 185 |
fcml_uint8_t opcodes[FCML_OPCODES_NUM]; |
| 186 |
/* Instruction mnemonic. */ |
| 187 |
fcml_st_mp_mnemonic_set *mnemonics; |
| 188 |
/* Flags that describe prefixes usage. */ |
| 189 |
fcml_uint32_t prefixes_flags; |
| 190 |
/* Flags that describe some details of opcodes. */ |
| 191 |
fcml_uint32_t opcode_flags; |
| 192 |
/* Instruction decoder. */ |
| 193 |
instruction_decoder instruction_decoder; |
| 194 |
/* Chain of address mode acceptors registered for given addressing mode. */ |
| 195 |
addr_mode_acceptor_chain *instruction_acceptors_chain; |
| 196 |
/* Function used to decode instruction operands. */ |
| 197 |
operand_decoding operand_decodings[FCML_OPERANDS_COUNT]; |
| 198 |
/* Instruction decoding order, for information purpose only. */ |
| 199 |
fcml_int order; |
| 200 |
/* Instruction level hints. */ |
| 201 |
fcml_hints hints; |
| 202 |
/* ModRM details. */ |
| 203 |
modrm_decoding_details modrm_details; |
| 204 |
/* Decorators existence. */ |
| 205 |
fcml_st_decorators_existence decorators_existence; |
| 206 |
} inst_decoding_def; |
| 207 |
|
| 208 |
/* Instruction decoder for IA instruction set, currently it is the only |
| 209 |
* one supported decoder. |
| 210 |
*/ |
| 211 |
static fcml_ceh_error instruction_decoder_IA( |
| 212 |
decoding_context *decoding_context); |
| 213 |
|
| 214 |
/********************* |
| 215 |
* Utility functions. |
| 216 |
*********************/ |
| 217 |
|
| 218 |
static void set_x64_exp(fcml_st_register *reg, fcml_bool is_rex) { |
| 219 |
if (is_rex) { |
| 220 |
reg->x64_exp = (reg->type == FCML_REG_GPR && reg->size == FCML_DS_8 |
| 221 |
&& reg->reg >= 4 && reg->reg <= 7); |
| 222 |
} else { |
| 223 |
reg->x64_exp = FCML_FALSE; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
static void set_segment_selector(fcml_st_segment_selector *seg_sel, |
| 228 |
fcml_uint8_t seg_reg, fcml_bool is_default) { |
| 229 |
seg_sel->is_default_reg = is_default; |
| 230 |
fcml_st_register *reg_seg = &(seg_sel->segment_selector); |
| 231 |
reg_seg->reg = seg_reg; |
| 232 |
reg_seg->size = FCML_DS_16; |
| 233 |
reg_seg->type = FCML_REG_SEG; |
| 234 |
reg_seg->x64_exp = FCML_FALSE; |
| 235 |
} |
| 236 |
|
| 237 |
static fcml_uint8_t override_segment_reg(decoding_context *context, |
| 238 |
fcml_uint8_t reg) { |
| 239 |
for (int i = 0; i < context->prefixes.prefixes_count; i++) { |
| 240 |
fcml_st_instruction_prefix *prefix = &(context->prefixes.prefixes[i]); |
| 241 |
if (prefix->prefix_type == FCML_PT_GROUP_2 |
| 242 |
&& !prefix->mandatory_prefix) { |
| 243 |
switch (prefix->prefix) { |
| 244 |
case 0x2E: |
| 245 |
reg = FCML_REG_CS; |
| 246 |
break; |
| 247 |
case 0x36: |
| 248 |
reg = FCML_REG_SS; |
| 249 |
break; |
| 250 |
case 0x3E: |
| 251 |
reg = FCML_REG_DS; |
| 252 |
break; |
| 253 |
case 0x26: |
| 254 |
reg = FCML_REG_ES; |
| 255 |
break; |
| 256 |
case 0x64: |
| 257 |
reg = FCML_REG_FS; |
| 258 |
break; |
| 259 |
case 0x65: |
| 260 |
reg = FCML_REG_GS; |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
return reg; |
| 266 |
} |
| 267 |
|
| 268 |
static fcml_ceh_error decode_segment_selector(decoding_context *context, |
| 269 |
fcml_st_segment_selector *selector, fcml_uint8_t encoded_segment_reg) { |
| 270 |
fcml_uint8_t seg_reg = ~FCML_SEG_ALLOW_OVERRIDE & encoded_segment_reg; |
| 271 |
if (encoded_segment_reg & FCML_SEG_ALLOW_OVERRIDE) { |
| 272 |
fcml_uint8_t tmp = seg_reg; |
| 273 |
seg_reg = override_segment_reg(context, seg_reg); |
| 274 |
selector->is_default_reg = (seg_reg == tmp); |
| 275 |
} else { |
| 276 |
selector->is_default_reg = FCML_TRUE; |
| 277 |
} |
| 278 |
selector->segment_selector.reg = seg_reg; |
| 279 |
selector->segment_selector.size = FCML_DS_16; |
| 280 |
selector->segment_selector.type = FCML_REG_SEG; |
| 281 |
selector->segment_selector.x64_exp = FCML_FALSE; |
| 282 |
return FCML_CEH_GEC_NO_ERROR; |
| 283 |
} |
| 284 |
|
| 285 |
static fcml_usize calculate_vector_length(fcml_uint8_t tuple_type, |
| 286 |
fcml_bool sae_enabled, fcml_uint8_t encoded_vector_length) { |
| 287 |
fcml_usize vector_length = 0; |
| 288 |
if (sae_enabled) { |
| 289 |
/* Vector length is assumed to be 512-bit in case of AVX-512 packed |
| 290 |
* vector instructions or 128-bit for scalar instructions. |
| 291 |
*/ |
| 292 |
if (tuple_type == FCML_TT_T1S || tuple_type == FCML_TT_T1F) { |
| 293 |
/* SIMD scalar instructions. */ |
| 294 |
vector_length = FCML_DS_128; |
| 295 |
} else { |
| 296 |
vector_length = FCML_DS_512; |
| 297 |
} |
| 298 |
} else { |
| 299 |
switch (encoded_vector_length) { |
| 300 |
case 0: |
| 301 |
vector_length = FCML_DS_128; |
| 302 |
break; |
| 303 |
case 1: |
| 304 |
vector_length = FCML_DS_256; |
| 305 |
break; |
| 306 |
case 2: |
| 307 |
vector_length = FCML_DS_512; |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
return vector_length; |
| 312 |
}; |
| 313 |
|
| 314 |
static fcml_usize decode_encoded_size_value( |
| 315 |
decoding_context *context, fcml_uint8_t encoded_size) { |
| 316 |
fcml_usize result = 0; |
| 317 |
/* Extracts size explicitly encoded in the encoded operand size. */ |
| 318 |
fcml_uint8_t size = (encoded_size & ~FCML_EOS_OPT); |
| 319 |
if (FCML_IS_EOS_DYNAMIC(size)) { |
| 320 |
switch (size) { |
| 321 |
case FCML_EOS_EOSA: |
| 322 |
result = context->effective_operand_size_attribute; |
| 323 |
break; |
| 324 |
case FCML_EOS_EASA: |
| 325 |
result = context->effective_address_size_attribute; |
| 326 |
break; |
| 327 |
case FCML_EOS_L: |
| 328 |
result = context->vector_length; |
| 329 |
break; |
| 330 |
case FCML_EOS_14_28: |
| 331 |
result = (context->effective_operand_size_attribute == FCML_DS_16) |
| 332 |
? (14 * 8) : (28 * 8); |
| 333 |
break; |
| 334 |
case FCML_EOS_94_108: |
| 335 |
result = (context->effective_operand_size_attribute == FCML_DS_16) |
| 336 |
? (94 * 8) : (108 * 8); |
| 337 |
break; |
| 338 |
case FCML_EOS_32_64: |
| 339 |
result = (context->effective_operand_size_attribute == FCML_DS_16) |
| 340 |
? (16 * 2) : (32 * 2); |
| 341 |
break; |
| 342 |
case FCML_EOS_FP: |
| 343 |
case FCML_EOS_FPI: |
| 344 |
result = context->effective_operand_size_attribute + 16; |
| 345 |
break; |
| 346 |
} |
| 347 |
} else { |
| 348 |
result = size * 8; |
| 349 |
} |
| 350 |
return result; |
| 351 |
} |
| 352 |
|
| 353 |
/*********************************** |
| 354 |
* Decoding tree related functions. |
| 355 |
***********************************/ |
| 356 |
|
| 357 |
static int calculate_decoding_order(inst_decoding_def *decoding) { |
| 358 |
|
| 359 |
fcml_uint32_t prefixes = decoding->prefixes_flags; |
| 360 |
fcml_uint32_t opcodes = decoding->opcode_flags; |
| 361 |
|
| 362 |
int order = 0; |
| 363 |
|
| 364 |
/* Mandatory prefix. */ |
| 365 |
if (FCML_DEF_PREFIX_MANDATORY_66(prefixes) |
| 366 |
| FCML_DEF_PREFIX_MANDATORY_F2(prefixes) |
| 367 |
| FCML_DEF_PREFIX_MANDATORY_F3(prefixes)) { |
| 368 |
order += 3; |
| 369 |
} |
| 370 |
|
| 371 |
/* Opcode extension. */ |
| 372 |
if (FCML_DEF_OPCODE_FLAGS_OPCODE_IS_EXT(opcodes)) { |
| 373 |
order++; |
| 374 |
} |
| 375 |
|
| 376 |
/* VEX, XOP and EVEX prefixes. */ |
| 377 |
if (FCML_DEF_PREFIX_IS_AVX_REQ(prefixes)) { |
| 378 |
order++; |
| 379 |
} |
| 380 |
|
| 381 |
/* REX/VEX/EVEX prefix. */ |
| 382 |
if (FCML_DEF_PREFIX_W_1(prefixes) |
| 383 |
|| FCML_DEF_PREFIX_W_0(prefixes) |
| 384 |
|| FCML_DEF_PREFIX_L_1(prefixes) |
| 385 |
|| FCML_DEF_PREFIX_L_0(prefixes) |
| 386 |
|| FCML_DEF_PREFIX_L_prim_1(prefixes) |
| 387 |
|| FCML_DEF_PREFIX_L_prim_0(prefixes)) { |
| 388 |
order += 2; |
| 389 |
} |
| 390 |
|
| 391 |
return order; |
| 392 |
} |
| 393 |
|
| 394 |
static instruction_decoder choose_instruction_decoder( |
| 395 |
fcml_uint8_t instruction_type) { |
| 396 |
switch (instruction_type) { |
| 397 |
case FCML_EN_IT_IA: |
| 398 |
/* Currently only IA decoder is supported. */ |
| 399 |
return &instruction_decoder_IA; |
| 400 |
} |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
/***********************/ |
| 405 |
/** Operand decoders. **/ |
| 406 |
/***********************/ |
| 407 |
|
| 408 |
/********************/ |
| 409 |
/* Immediate value. */ |
| 410 |
/********************/ |
| 411 |
|
| 412 |
static fcml_ceh_error operand_decoder_imm(decoding_context *context, |
| 413 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 414 |
fcml_ptr args) { |
| 415 |
|
| 416 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 417 |
|
| 418 |
fcml_st_def_tma_imm *imm_args = (fcml_st_def_tma_imm *) args; |
| 419 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 420 |
fcml_st_integer *immediate = &(operand->immediate); |
| 421 |
fcml_st_memory_stream *stream = context->stream; |
| 422 |
|
| 423 |
operand->type = FCML_OT_IMMEDIATE; |
| 424 |
|
| 425 |
fcml_usize size = decode_encoded_size_value( |
| 426 |
context, imm_args->encoded_size); |
| 427 |
|
| 428 |
/* Correct calculated IMM size if 64 bit IMM is not supported |
| 429 |
* by addressing mode. |
| 430 |
*/ |
| 431 |
if (size == FCML_DS_64 && !imm_args->is_64bit_imm_allowed) { |
| 432 |
size = FCML_DS_32; |
| 433 |
} |
| 434 |
|
| 435 |
fcml_usize size_ex = decode_encoded_size_value(context, |
| 436 |
imm_args->encoded_ex_size); |
| 437 |
if (size_ex == FCML_DS_UNDEF) { |
| 438 |
/* check if S opcode field is set.*/ |
| 439 |
if (size == FCML_DS_8 && context->opcode_field_s_bit) { |
| 440 |
size_ex = context->effective_operand_size_attribute; |
| 441 |
} else { |
| 442 |
size_ex = size; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
/* All immediate integers are signed.*/ |
| 447 |
immediate->is_signed = FCML_TRUE; |
| 448 |
|
| 449 |
error = fcml_fn_utils_decode_integer(stream, immediate, size); |
| 450 |
if (error) { |
| 451 |
return error; |
| 452 |
} |
| 453 |
|
| 454 |
if (size_ex != size) { |
| 455 |
error = fcml_fn_utils_conv_int_to_int(immediate, immediate, |
| 456 |
size, size_ex); |
| 457 |
if (error) { |
| 458 |
return error; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
return error; |
| 463 |
} |
| 464 |
|
| 465 |
static fcml_int operand_size_calculator_imm(decoding_context *context, |
| 466 |
fcml_ptr args) { |
| 467 |
fcml_st_def_tma_imm *imm_args = (fcml_st_def_tma_imm *) args; |
| 468 |
fcml_int size = decode_encoded_size_value(context, |
| 469 |
imm_args->encoded_size) / 8; |
| 470 |
/* 8 bytes IMM size is only supported for instructions with |
| 471 |
* following flag set. |
| 472 |
*/ |
| 473 |
if (size == 8 && !imm_args->is_64bit_imm_allowed) { |
| 474 |
size = 4; |
| 475 |
} |
| 476 |
return size; |
| 477 |
} |
| 478 |
|
| 479 |
/**********************/ |
| 480 |
/* Explicit register. */ |
| 481 |
/**********************/ |
| 482 |
|
| 483 |
static fcml_ceh_error operand_decoder_explicit_reg(decoding_context *context, |
| 484 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 485 |
fcml_ptr args) { |
| 486 |
|
| 487 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 488 |
fcml_st_def_tma_explicit_reg *reg_args = |
| 489 |
(fcml_st_def_tma_explicit_reg*)args; |
| 490 |
|
| 491 |
operand->type = FCML_OT_REGISTER; |
| 492 |
operand->reg.reg = reg_args->reg_num; |
| 493 |
operand->reg.type = (fcml_en_register) reg_args->reg_type; |
| 494 |
operand->reg.size = decode_encoded_size_value(context, |
| 495 |
reg_args->encoded_reg_size); |
| 496 |
|
| 497 |
return FCML_CEH_GEC_NO_ERROR; |
| 498 |
} |
| 499 |
|
| 500 |
/**************************************/ |
| 501 |
/* Explicit register base addressing. */ |
| 502 |
/**************************************/ |
| 503 |
|
| 504 |
static fcml_ceh_error operand_decoder_explicit_gps_reg_addressing( |
| 505 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 506 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 507 |
|
| 508 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 509 |
fcml_st_address *address = &(operand->address); |
| 510 |
fcml_st_effective_address *effective_address = |
| 511 |
&(address->effective_address); |
| 512 |
|
| 513 |
operand->type = FCML_OT_ADDRESS; |
| 514 |
address->address_form = FCML_AF_COMBINED; |
| 515 |
|
| 516 |
fcml_st_def_tma_explicit_gps_reg_addressing *reg_addr_args = |
| 517 |
(fcml_st_def_tma_explicit_gps_reg_addressing*)args; |
| 518 |
|
| 519 |
/* Size operator.*/ |
| 520 |
address->size_operator = decode_encoded_size_value( |
| 521 |
context, reg_addr_args->encoded_operand_size); |
| 522 |
|
| 523 |
/* Base register.*/ |
| 524 |
effective_address->base.size = context->effective_address_size_attribute; |
| 525 |
effective_address->base.reg = reg_addr_args->reg_num; |
| 526 |
effective_address->base.type = FCML_REG_GPR; |
| 527 |
|
| 528 |
/* Segment register.*/ |
| 529 |
return decode_segment_selector(context, |
| 530 |
&(address->segment_selector), |
| 531 |
reg_addr_args->encoded_segment_register); |
| 532 |
} |
| 533 |
|
| 534 |
/********************/ |
| 535 |
/* Opcode register. */ |
| 536 |
/********************/ |
| 537 |
|
| 538 |
static fcml_ceh_error operand_decoder_opcode_reg( |
| 539 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 540 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 541 |
|
| 542 |
fcml_st_def_tma_opcode_reg *reg_args = (fcml_st_def_tma_opcode_reg*) args; |
| 543 |
fcml_uint8_t reg_num = context->primary_opcode_byte & 0x07; |
| 544 |
|
| 545 |
if (context->prefixes.B) { |
| 546 |
reg_num |= 0x08; |
| 547 |
} |
| 548 |
|
| 549 |
/* Size operator.*/ |
| 550 |
operand_wrapper->operand.type = FCML_OT_REGISTER; |
| 551 |
fcml_st_register *reg = &(operand_wrapper->operand.reg); |
| 552 |
|
| 553 |
/* Base register.*/ |
| 554 |
reg->size = decode_encoded_size_value(context, |
| 555 |
reg_args->encoded_reg_size); |
| 556 |
reg->type = (fcml_en_register) reg_args->reg_type; |
| 557 |
reg->reg = reg_num; |
| 558 |
|
| 559 |
return FCML_CEH_GEC_NO_ERROR; |
| 560 |
} |
| 561 |
|
| 562 |
/************************************/ |
| 563 |
/* Immediate displacement relative. */ |
| 564 |
/************************************/ |
| 565 |
|
| 566 |
static fcml_ceh_error operand_decoder_immediate_dis_relative( |
| 567 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 568 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 569 |
|
| 570 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 571 |
fcml_sf_def_tma_immediate_dis_relative *rel_args = |
| 572 |
(fcml_sf_def_tma_immediate_dis_relative*)args; |
| 573 |
fcml_usize int_size = 0; |
| 574 |
fcml_int32_t offset32; |
| 575 |
fcml_int16_t offset16; |
| 576 |
|
| 577 |
// Just in case. |
| 578 |
if (FCML_IS_EOS_DYNAMIC(rel_args->encoded_size)) { |
| 579 |
FCML_TRACE_MSG("Currently dynamically calculated IMM size is \ |
| 580 |
not supported here."); |
| 581 |
return FCML_CEH_GEC_INTERNAL_ERROR; |
| 582 |
} |
| 583 |
|
| 584 |
fcml_st_entry_point *entry_point = |
| 585 |
&(context->disassembler_context->entry_point); |
| 586 |
|
| 587 |
if (rel_args->encoded_size != FCML_EOS_UNDEFINED) { |
| 588 |
int_size = rel_args->encoded_size * 8; |
| 589 |
} else { |
| 590 |
|
| 591 |
if (context->effective_operand_size_attribute == FCML_DS_16 |
| 592 |
&& entry_point->op_mode == FCML_OM_64_BIT) { |
| 593 |
fcml_fn_ceh_add_error(&(context->errors), |
| 594 |
fcml_fn_msg_get_message( |
| 595 |
FCML_MC_CEH_GEC_INVALID_ADDRESSING_MODE), |
| 596 |
FCML_CEH_MEW_WARN_INVALID_ADDRESSING_MODE, |
| 597 |
FCML_EN_CEH_EL_WARN); |
| 598 |
} |
| 599 |
|
| 600 |
int_size = FCML_DS_32; |
| 601 |
|
| 602 |
/* For 32 and 64 bit addressing, 32 bit immediate value should be |
| 603 |
* used. Only for 16 bit addressing value differs and is |
| 604 |
* equal to 16. |
| 605 |
*/ |
| 606 |
if (context->effective_operand_size_attribute == FCML_DS_16) { |
| 607 |
int_size = FCML_DS_16; |
| 608 |
} |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
fcml_st_integer integer; |
| 613 |
integer.is_signed = FCML_TRUE; |
| 614 |
|
| 615 |
/* Read displacement value from stream.*/ |
| 616 |
error = fcml_fn_utils_decode_integer(context->stream, &integer, int_size); |
| 617 |
if (error) { |
| 618 |
return error; |
| 619 |
} |
| 620 |
|
| 621 |
operand_wrapper->operand.type = FCML_OT_ADDRESS; |
| 622 |
operand_wrapper->operand.address.address_form = FCML_AF_OFFSET; |
| 623 |
|
| 624 |
fcml_st_offset *offset = &(operand_wrapper->operand.address.offset); |
| 625 |
offset->is_signed = FCML_TRUE; |
| 626 |
|
| 627 |
/* Calculate offset relative to IP.*/ |
| 628 |
fcml_ip ip = entry_point->ip; |
| 629 |
|
| 630 |
switch (context->effective_operand_size_attribute) { |
| 631 |
case FCML_DS_16: |
| 632 |
error = fcml_fn_utils_conv_int_to_int16(&integer, &offset16); |
| 633 |
if (!error) { |
| 634 |
offset->off16 = ((fcml_int32_t) ip + |
| 635 |
context->calculated_instruction_size + offset16) |
| 636 |
& 0x0000FFFF; |
| 637 |
offset->size = FCML_DS_16; |
| 638 |
} |
| 639 |
break; |
| 640 |
case FCML_DS_32: |
| 641 |
error = fcml_fn_utils_conv_int_to_int32(&integer, &offset32); |
| 642 |
if (!error) { |
| 643 |
offset->off32 = ((fcml_int32_t) ip + |
| 644 |
context->calculated_instruction_size + offset32); |
| 645 |
offset->size = FCML_DS_32; |
| 646 |
} |
| 647 |
break; |
| 648 |
case FCML_DS_64: |
| 649 |
error = fcml_fn_utils_conv_int_to_int32(&integer, &offset32); |
| 650 |
if (!error) { |
| 651 |
offset->off64 = ((fcml_int64_t)ip + |
| 652 |
context->calculated_instruction_size + offset32); |
| 653 |
offset->size = FCML_DS_64; |
| 654 |
} |
| 655 |
break; |
| 656 |
} |
| 657 |
|
| 658 |
/* Sets CS segment register.*/ |
| 659 |
set_segment_selector( |
| 660 |
&(operand_wrapper->operand.address.segment_selector), FCML_REG_CS, |
| 661 |
FCML_TRUE); |
| 662 |
|
| 663 |
return error; |
| 664 |
} |
| 665 |
|
| 666 |
static fcml_int operand_size_calculator_imm_dis_rel(decoding_context *context, |
| 667 |
fcml_ptr args) { |
| 668 |
fcml_sf_def_tma_immediate_dis_relative *rel_args = |
| 669 |
(fcml_sf_def_tma_immediate_dis_relative*)args; |
| 670 |
fcml_int size; |
| 671 |
if (rel_args->encoded_size != FCML_EOS_UNDEFINED) { |
| 672 |
size = rel_args->encoded_size; |
| 673 |
} else { |
| 674 |
size = (context->effective_operand_size_attribute == FCML_DS_16) |
| 675 |
? FCML_DS_16 / 8 : FCML_DS_32 / 8; |
| 676 |
} |
| 677 |
return size; |
| 678 |
} |
| 679 |
|
| 680 |
/***************/ |
| 681 |
/* Far pointer */ |
| 682 |
/***************/ |
| 683 |
|
| 684 |
static fcml_ceh_error operand_decoder_far_pointer(decoding_context *context, |
| 685 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 686 |
fcml_ptr args) { |
| 687 |
|
| 688 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 689 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 690 |
fcml_st_far_pointer *far_pointer = &(operand->far_pointer); |
| 691 |
fcml_bool result; |
| 692 |
|
| 693 |
operand->type = FCML_OT_FAR_POINTER; |
| 694 |
|
| 695 |
switch (context->effective_operand_size_attribute) { |
| 696 |
case FCML_DS_16: |
| 697 |
far_pointer->offset16 = (fcml_int16_t)fcml_fn_stream_read_word( |
| 698 |
context->stream, &result); |
| 699 |
break; |
| 700 |
case FCML_DS_32: |
| 701 |
far_pointer->offset32 = (fcml_int32_t)fcml_fn_stream_read_dword( |
| 702 |
context->stream, &result); |
| 703 |
break; |
| 704 |
default: |
| 705 |
error = FCML_CEH_GEC_INVALID_OPERATING_MODE; |
| 706 |
break; |
| 707 |
} |
| 708 |
|
| 709 |
far_pointer->offset_size = context->effective_operand_size_attribute; |
| 710 |
|
| 711 |
far_pointer->segment = fcml_fn_stream_read_word(context->stream, &result); |
| 712 |
if (!result) { |
| 713 |
return FCML_CEH_GEC_EOF; |
| 714 |
} |
| 715 |
|
| 716 |
if (!result) { |
| 717 |
error = FCML_CEH_GEC_EOF; |
| 718 |
} |
| 719 |
|
| 720 |
return error; |
| 721 |
} |
| 722 |
|
| 723 |
static fcml_int operand_size_calculator_far_pointer(decoding_context *context, |
| 724 |
fcml_ptr args) { |
| 725 |
return context->effective_operand_size_attribute / 8 + 2; |
| 726 |
} |
| 727 |
|
| 728 |
/****************************/ |
| 729 |
/* Explicit 8-bits integer. */ |
| 730 |
/****************************/ |
| 731 |
|
| 732 |
static fcml_ceh_error operand_decoder_explicit_ib( |
| 733 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 734 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 735 |
|
| 736 |
fcml_st_def_tma_explicit_ib *imm_args = (fcml_st_def_tma_explicit_ib*)args; |
| 737 |
|
| 738 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 739 |
fcml_st_integer *immediate = &(operand->immediate); |
| 740 |
|
| 741 |
operand->type = FCML_OT_IMMEDIATE; |
| 742 |
immediate->is_signed = FCML_TRUE; |
| 743 |
immediate->size = FCML_DS_8; |
| 744 |
immediate->int8 = imm_args->ib; |
| 745 |
|
| 746 |
return FCML_CEH_GEC_NO_ERROR; |
| 747 |
} |
| 748 |
|
| 749 |
/***************************/ |
| 750 |
/* Segment relative offset */ |
| 751 |
/***************************/ |
| 752 |
|
| 753 |
static fcml_ceh_error operand_decoder_segment_relative_offset( |
| 754 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 755 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 756 |
|
| 757 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 758 |
|
| 759 |
fcml_st_def_tma_segment_relative_offset *seg_args = |
| 760 |
(fcml_st_def_tma_segment_relative_offset*)args; |
| 761 |
|
| 762 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 763 |
fcml_st_address *address = &(operand->address); |
| 764 |
fcml_st_offset *offset = &(address->offset); |
| 765 |
|
| 766 |
fcml_bool result; |
| 767 |
|
| 768 |
error = decode_segment_selector(context, |
| 769 |
&(address->segment_selector), seg_args->encoded_segment_register); |
| 770 |
if (error) { |
| 771 |
return error; |
| 772 |
} |
| 773 |
|
| 774 |
address->size_operator = decode_encoded_size_value( |
| 775 |
context, seg_args->encoded_operand_size); |
| 776 |
address->address_form = FCML_AF_OFFSET; |
| 777 |
|
| 778 |
operand->type = FCML_OT_ADDRESS; |
| 779 |
|
| 780 |
switch (context->effective_address_size_attribute) { |
| 781 |
case FCML_DS_16: |
| 782 |
offset->off16 = fcml_fn_stream_read_word(context->stream, &result); |
| 783 |
break; |
| 784 |
case FCML_DS_32: |
| 785 |
offset->off32 = fcml_fn_stream_read_dword(context->stream, &result); |
| 786 |
break; |
| 787 |
case FCML_DS_64: |
| 788 |
offset->off64 = fcml_fn_stream_read_qword(context->stream, &result); |
| 789 |
break; |
| 790 |
default: |
| 791 |
error = FCML_CEH_GEC_INVALID_OPERATING_MODE; |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
offset->size = context->effective_address_size_attribute; |
| 796 |
|
| 797 |
if (!result) { |
| 798 |
error = FCML_CEH_GEC_EOF; |
| 799 |
} |
| 800 |
|
| 801 |
return error; |
| 802 |
} |
| 803 |
|
| 804 |
static fcml_int operand_size_calculator_seg_rel_offset( |
| 805 |
decoding_context *context, fcml_ptr args) { |
| 806 |
return context->effective_address_size_attribute / 8; |
| 807 |
} |
| 808 |
|
| 809 |
/**************/ |
| 810 |
/* ModRM - RM */ |
| 811 |
/**************/ |
| 812 |
|
| 813 |
static fcml_ceh_error operand_decoder_rm(decoding_context *context, |
| 814 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 815 |
fcml_ptr args) { |
| 816 |
|
| 817 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 818 |
|
| 819 |
fcml_st_modrm *decoded_modrm = &(context->decoded_modrm); |
| 820 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 821 |
|
| 822 |
fcml_st_def_tma_rm *rm_args = (fcml_st_def_tma_rm*) args; |
| 823 |
|
| 824 |
if ((rm_args->flags & FCML_RMF_R) && decoded_modrm->reg.is_not_null) { |
| 825 |
|
| 826 |
operand->type = FCML_OT_REGISTER; |
| 827 |
operand->reg.reg = decoded_modrm->reg.value; |
| 828 |
operand->reg.type = (fcml_en_register) rm_args->reg_type; |
| 829 |
operand->reg.size = decode_encoded_size_value( |
| 830 |
context, rm_args->encoded_register_operand_size); |
| 831 |
|
| 832 |
set_x64_exp(&(operand->reg), |
| 833 |
context->prefixes.is_rex); |
| 834 |
|
| 835 |
} else if (rm_args->flags & FCML_RMF_M) { |
| 836 |
|
| 837 |
operand->type = FCML_OT_ADDRESS; |
| 838 |
|
| 839 |
fcml_st_address *address = &(operand->address); |
| 840 |
*address = decoded_modrm->address; |
| 841 |
|
| 842 |
address->size_operator = decode_encoded_size_value( |
| 843 |
context, rm_args->encoded_memory_operand_size); |
| 844 |
|
| 845 |
/* If AVX-512 broadcast is used, use size operator from broadcast |
| 846 |
* instead of the one directly encoded in ModR/M. |
| 847 |
*/ |
| 848 |
if (FCML_IS_DECOR_BCAST(decorators) && context->prefixes.b) { |
| 849 |
/* Directly encoded bcast size has precedence over |
| 850 |
SIMD element size. */ |
| 851 |
operand->decorators.bcast.is_not_null = FCML_TRUE; |
| 852 |
operand->decorators.bcast.value = address->size_operator / |
| 853 |
context->element_size; |
| 854 |
address->size_operator = context->element_size; |
| 855 |
} |
| 856 |
|
| 857 |
if (decoded_modrm->is_rip) { |
| 858 |
/* We known the instruction size, so post processing is not needed |
| 859 |
* and RIP can be calculated now. |
| 860 |
*/ |
| 861 |
error = fcml_fn_modrm_decode_rip((fcml_uint64_t) |
| 862 |
context->disassembler_context->entry_point.ip + |
| 863 |
context->calculated_instruction_size, |
| 864 |
context->effective_address_size_attribute, |
| 865 |
&(address->offset), &(address->offset)); |
| 866 |
} |
| 867 |
|
| 868 |
/* Segment registers.*/ |
| 869 |
|
| 870 |
fcml_st_effective_address *effective_address = |
| 871 |
&(address->effective_address); |
| 872 |
fcml_st_segment_selector *segment_selector = |
| 873 |
&(address->segment_selector); |
| 874 |
|
| 875 |
segment_selector->segment_selector.type = FCML_REG_SEG; |
| 876 |
segment_selector->segment_selector.size = FCML_DS_16; |
| 877 |
|
| 878 |
if (context->decoding_def->instruction_group & FCML_AMT_BRANCH) { |
| 879 |
/* For all branches only CS segment register is used. |
| 880 |
* Segment override prefixes are ignored in this case. |
| 881 |
*/ |
| 882 |
segment_selector->segment_selector.reg = FCML_REG_CS; |
| 883 |
segment_selector->is_default_reg = FCML_TRUE; |
| 884 |
} else { |
| 885 |
if (effective_address->base.type == FCML_REG_GPR |
| 886 |
&& (effective_address->base.reg == FCML_REG_BP |
| 887 |
|| effective_address->base.reg == FCML_REG_SP)) { |
| 888 |
segment_selector->segment_selector.reg = FCML_REG_SS; |
| 889 |
segment_selector->is_default_reg = FCML_TRUE; |
| 890 |
} else { |
| 891 |
segment_selector->segment_selector.reg = |
| 892 |
override_segment_reg(context, |
| 893 |
FCML_REG_DS); |
| 894 |
segment_selector->is_default_reg = |
| 895 |
(segment_selector->segment_selector.reg == FCML_REG_DS) |
| 896 |
? FCML_TRUE : FCML_FALSE; |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
/* These hints aren't used in 32 bit addressing mode.*/ |
| 901 |
if ((context->disassembler_context->entry_point.op_mode |
| 902 |
== FCML_OM_64_BIT) |
| 903 |
&& (address->address_form == FCML_AF_OFFSET)) { |
| 904 |
operand->hints |= context->decoded_modrm.is_rip |
| 905 |
? FCML_OP_HINT_RELATIVE_ADDRESSING |
| 906 |
: FCML_OP_HINT_ABSOLUTE_ADDRESSING; |
| 907 |
} |
| 908 |
|
| 909 |
} else { |
| 910 |
error = FCML_CEH_GEC_INVALID_ADDRESSING_FORM; |
| 911 |
} |
| 912 |
|
| 913 |
return error; |
| 914 |
} |
| 915 |
|
| 916 |
/*************/ |
| 917 |
/* ModRM - R */ |
| 918 |
/*************/ |
| 919 |
|
| 920 |
static fcml_ceh_error operand_decoder_r(decoding_context *context, |
| 921 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 922 |
fcml_ptr args) { |
| 923 |
|
| 924 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 925 |
fcml_st_def_tma_r *r_args = (fcml_st_def_tma_r*) args; |
| 926 |
|
| 927 |
operand_wrapper->operand.type = FCML_OT_REGISTER; |
| 928 |
fcml_st_register *reg = &(operand_wrapper->operand.reg); |
| 929 |
|
| 930 |
reg->reg = context->decoded_modrm.reg_opcode; |
| 931 |
reg->type = r_args->reg_type; |
| 932 |
reg->size = decode_encoded_size_value(context, |
| 933 |
r_args->encoded_register_operand_size); |
| 934 |
|
| 935 |
set_x64_exp(reg, context->prefixes.is_rex); |
| 936 |
|
| 937 |
return error; |
| 938 |
} |
| 939 |
|
| 940 |
/********/ |
| 941 |
/* VVVV */ |
| 942 |
/********/ |
| 943 |
|
| 944 |
static fcml_ceh_error operand_decoder_vex_vvvv( |
| 945 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 946 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 947 |
|
| 948 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 949 |
|
| 950 |
fcml_st_def_tma_vex_vvvv_reg *v_args = (fcml_st_def_tma_vex_vvvv_reg*)args; |
| 951 |
|
| 952 |
operand_wrapper->operand.type = FCML_OT_REGISTER; |
| 953 |
fcml_st_register *reg = &(operand_wrapper->operand.reg); |
| 954 |
|
| 955 |
reg->reg = context->prefixes.vvvv | context->prefixes.V_prim << 4; |
| 956 |
reg->type = (fcml_en_register) v_args->reg_type; |
| 957 |
reg->size = decode_encoded_size_value(context, |
| 958 |
v_args->encoded_register_size); |
| 959 |
|
| 960 |
return error; |
| 961 |
} |
| 962 |
|
| 963 |
static fcml_ceh_error operand_decoder_isX( |
| 964 |
decoding_context *context, operand_wrapper *operand_wrapper, |
| 965 |
fcml_operand_decorators decorators, fcml_ptr args) { |
| 966 |
|
| 967 |
/* IS4/IS5 byte is located just after ModR/M field, so it doesn't have |
| 968 |
* to be read in any post processors. |
| 969 |
*/ |
| 970 |
fcml_st_def_tma_is *is_args = (fcml_st_def_tma_is *) args; |
| 971 |
|
| 972 |
/* Read isX byte from stream, or get cached value from context.*/ |
| 973 |
fcml_uint8_t isX; |
| 974 |
if (context->isX_byte.is_not_null) { |
| 975 |
isX = context->isX_byte.value; |
| 976 |
} else { |
| 977 |
fcml_bool result; |
| 978 |
isX = fcml_fn_stream_read(context->stream, &result); |
| 979 |
if (!result) { |
| 980 |
return FCML_CEH_GEC_EOF; |
| 981 |
} |
| 982 |
context->isX_byte.is_not_null = FCML_TRUE; |
| 983 |
context->isX_byte.value = isX; |
| 984 |
} |
| 985 |
|
| 986 |
/* IS4.*/ |
| 987 |
if ((is_args->flags & FCML_ISF_IS4) |
| 988 |
|| (is_args->flags & FCML_ISF_IS5_SRC)) { |
| 989 |
|
| 990 |
operand_wrapper->operand.type = FCML_OT_REGISTER; |
| 991 |
fcml_st_register *reg = &(operand_wrapper->operand.reg); |
| 992 |
|
| 993 |
reg->reg = ((context->disassembler_context->entry_point.op_mode |
| 994 |
== FCML_OM_32_BIT) ? (0x70 & isX) : (0xF0 & isX)) >> 4; |
| 995 |
reg->type = FCML_REG_SIMD; |
| 996 |
reg->size = context->prefixes.L ? FCML_OS_YWORD : FCML_OS_XWORD; |
| 997 |
reg->x64_exp = FCML_FALSE; |
| 998 |
|
| 999 |
} else { |
| 1000 |
/* IS5 - M2Z*/ |
| 1001 |
operand_wrapper->operand.type = FCML_OT_IMMEDIATE; |
| 1002 |
fcml_st_integer *imm = &(operand_wrapper->operand.immediate); |
| 1003 |
imm->is_signed = FCML_FALSE; |
| 1004 |
imm->int8 = isX & 0x03; |
| 1005 |
imm->size = FCML_DS_8; |
| 1006 |
} |
| 1007 |
|
| 1008 |
return FCML_CEH_GEC_NO_ERROR; |
| 1009 |
} |
| 1010 |
|
| 1011 |
static fcml_int operand_size_calculator_isX(decoding_context *context, |
| 1012 |
fcml_ptr args) { |
| 1013 |
return 1; |
| 1014 |
} |
| 1015 |
|
| 1016 |
static fcml_ceh_error operand_decoder_pseudo_op(decoding_context *context, |
| 1017 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 1018 |
fcml_ptr args) { |
| 1019 |
|
| 1020 |
fcml_bool result; |
| 1021 |
fcml_uint8_t pseudo_op = fcml_fn_stream_read(context->stream, &result); |
| 1022 |
if (!result) { |
| 1023 |
return FCML_CEH_GEC_EOF; |
| 1024 |
} |
| 1025 |
|
| 1026 |
fcml_st_operand *operand = &(operand_wrapper->operand); |
| 1027 |
fcml_st_integer *immediate = &(operand->immediate); |
| 1028 |
|
| 1029 |
operand->type = FCML_OT_IMMEDIATE; |
| 1030 |
immediate->is_signed = FCML_TRUE; |
| 1031 |
immediate->size = FCML_DS_8; |
| 1032 |
immediate->int8 = pseudo_op; |
| 1033 |
|
| 1034 |
context->pseudo_opcode.is_not_null = FCML_TRUE; |
| 1035 |
context->pseudo_opcode.value = pseudo_op; |
| 1036 |
|
| 1037 |
return FCML_CEH_GEC_NO_ERROR; |
| 1038 |
} |
| 1039 |
|
| 1040 |
static fcml_int operand_size_calculator_pseudo_op(decoding_context *context, |
| 1041 |
fcml_ptr args) { |
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/***********/ |
| 1046 |
/* Virtual */ |
| 1047 |
/***********/ |
| 1048 |
|
| 1049 |
static fcml_ceh_error operand_decoder_virtual(decoding_context *context, |
| 1050 |
operand_wrapper *operand_wrapper, fcml_operand_decorators decorators, |
| 1051 |
fcml_ptr args) { |
| 1052 |
|
| 1053 |
fcml_st_operand_decorators *op_decorators = |
| 1054 |
&(operand_wrapper->operand.decorators); |
| 1055 |
|
| 1056 |
if(context->is_modrm_reg_reg && context->prefixes.b && |
| 1057 |
(context->vector_length == FCML_DS_128 || |
| 1058 |
context->vector_length == FCML_DS_512)) { |
| 1059 |
|
| 1060 |
fcml_uint8_t ll = context->prefixes.L_prim << 1 | context->prefixes.L; |
| 1061 |
|
| 1062 |
/* Static rounding. */ |
| 1063 |
if (FCML_IS_DECOR_ER(decorators) ) { |
| 1064 |
op_decorators->er.is_not_null = FCML_TRUE; |
| 1065 |
op_decorators->er.value = ll; |
| 1066 |
operand_wrapper->operand.type = FCML_OT_VIRTUAL; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* Suppress All Exceptions. */ |
| 1070 |
if (FCML_IS_DECOR_SAE(decorators) && ll == 0) { |
| 1071 |
op_decorators->sae = FCML_TRUE; |
| 1072 |
operand_wrapper->operand.type = FCML_OT_VIRTUAL; |
| 1073 |
} |
| 1074 |
|
| 1075 |
} |
| 1076 |
|
| 1077 |
if (FCML_IS_DECOR_SAE_REQ(decorators) && !op_decorators->sae) { |
| 1078 |
return FCML_CEH_GEC_MISSING_DECORATOR; |
| 1079 |
} |
| 1080 |
|
| 1081 |
return FCML_CEH_GEC_NO_ERROR; |
| 1082 |
} |
| 1083 |
|
| 1084 |
/****************************/ |
| 1085 |
/* Operand decoders table. */ |
| 1086 |
/****************************/ |
| 1087 |
|
| 1088 |
typedef struct operand_decoder_def { |
| 1089 |
operand_decoder decoder; |
| 1090 |
operand_size_calculator size_calculator; |
| 1091 |
fcml_fp_hts_instruction_hints_calculator hints_calculator; |
| 1092 |
} operand_decoder_def; |
| 1093 |
|
| 1094 |
/* Order of elements in this array is really important and |
| 1095 |
* is related to addressing mode. |
| 1096 |
*/ |
| 1097 |
static operand_decoder_def operand_decoders[] = { |
| 1098 |
{NULL, NULL}, |
| 1099 |
{operand_decoder_imm, operand_size_calculator_imm, NULL}, |
| 1100 |
{operand_decoder_explicit_reg, NULL, NULL}, |
| 1101 |
{operand_decoder_opcode_reg, NULL, NULL}, |
| 1102 |
{operand_decoder_immediate_dis_relative, operand_size_calculator_imm_dis_rel, |
| 1103 |
fcml_fn_hts_ihc_immediate_dis_relative }, |
| 1104 |
{operand_decoder_far_pointer, operand_size_calculator_far_pointer, |
| 1105 |
fcml_fn_hts_ihc_far_pointer}, |
| 1106 |
{operand_decoder_explicit_gps_reg_addressing, NULL, NULL}, |
| 1107 |
{operand_decoder_explicit_ib, NULL, NULL}, |
| 1108 |
{operand_decoder_segment_relative_offset, |
| 1109 |
operand_size_calculator_seg_rel_offset, NULL }, |
| 1110 |
{operand_decoder_rm, NULL, fcml_fn_hts_ihc_modrm_hints}, |
| 1111 |
{operand_decoder_r, NULL, NULL}, |
| 1112 |
{operand_decoder_vex_vvvv, NULL, NULL}, |
| 1113 |
{operand_decoder_isX, operand_size_calculator_isX, NULL}, |
| 1114 |
{operand_decoder_rm, NULL, fcml_fn_hts_ihc_modrm_hints}, |
| 1115 |
{operand_decoder_pseudo_op, operand_size_calculator_pseudo_op, |
| 1116 |
fcml_fn_hts_ihc_pseudo_opcode}, |
| 1117 |
{operand_decoder_virtual, NULL, NULL} |
| 1118 |
}; |
| 1119 |
|
| 1120 |
static void free_operand_decoding(operand_decoding *operand_decoding) { |
| 1121 |
if (operand_decoding->decoded_addr_mode) { |
| 1122 |
fcml_fnp_def_free_addr_mode(operand_decoding->decoded_addr_mode); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
static void free_acceptors_chain(addr_mode_acceptor_chain *chain) { |
| 1127 |
if (!chain) { |
| 1128 |
return; |
| 1129 |
} |
| 1130 |
if (chain->next) { |
| 1131 |
free_acceptors_chain(chain->next); |
| 1132 |
} |
| 1133 |
fcml_fn_env_memory_free(chain); |
| 1134 |
} |
| 1135 |
|
| 1136 |
static void free_decoding_def(inst_decoding_def *decoding) { |
| 1137 |
if (decoding->mnemonics) { |
| 1138 |
fcml_fn_mp_free_mnemonics(decoding->mnemonics); |
| 1139 |
} |
| 1140 |
for (int i = 0; i < FCML_OPERANDS_COUNT; i++) { |
| 1141 |
free_operand_decoding(&(decoding->operand_decodings[i])); |
| 1142 |
} |
| 1143 |
free_acceptors_chain(decoding->instruction_acceptors_chain); |
| 1144 |
fcml_fn_env_memory_free(decoding); |
| 1145 |
} |
| 1146 |
|
| 1147 |
static fcml_ceh_error prepare_operand_decoding( |
| 1148 |
fcml_st_def_addr_mode_desc *addr_mode_desc, |
| 1149 |
operand_decoding *operand_decoding, |
| 1150 |
fcml_operand_desc operand_desc, |
| 1151 |
fcml_hints *instruction_hints) { |
| 1152 |
|
| 1153 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1154 |
|
| 1155 |
if (operand_desc == FCML_NA) { |
| 1156 |
return error; |
| 1157 |
} |
| 1158 |
|
| 1159 |
operand_decoding->hints = 0; |
| 1160 |
operand_decoding->decorators = FCML_DECORATORS(operand_desc); |
| 1161 |
|
| 1162 |
fcml_st_def_decoded_addr_mode *decoded_addr_mode; |
| 1163 |
error = fcml_fn_def_decode_addr_mode_args(operand_desc, &decoded_addr_mode); |
| 1164 |
if (error) { |
| 1165 |
return error; |
| 1166 |
} |
| 1167 |
|
| 1168 |
operand_decoding->decoded_addr_mode = decoded_addr_mode; |
| 1169 |
|
| 1170 |
operand_decoder_def *operand_decoder_def = |
| 1171 |
&(operand_decoders[decoded_addr_mode->addr_mode]); |
| 1172 |
|
| 1173 |
operand_decoding->decoder = operand_decoder_def->decoder; |
| 1174 |
operand_decoding->size_calculator = operand_decoder_def->size_calculator; |
| 1175 |
|
| 1176 |
if (operand_decoder_def->hints_calculator) { |
| 1177 |
fcml_st_hts_calculated_hints hints = |
| 1178 |
operand_decoder_def->hints_calculator(addr_mode_desc, |
| 1179 |
decoded_addr_mode); |
| 1180 |
operand_decoding->hints |= hints.operand_hints; |
| 1181 |
*instruction_hints |= hints.instruction_hints; |
| 1182 |
} |
| 1183 |
|
| 1184 |
return error; |
| 1185 |
} |
| 1186 |
|
| 1187 |
/********************* |
| 1188 |
* Prefixes. |
| 1189 |
*********************/ |
| 1190 |
|
| 1191 |
static fcml_st_instruction_prefix* get_prefix_if_available( |
| 1192 |
decoding_context *context, fcml_uint8_t prefix_value) { |
| 1193 |
fcml_st_instruction_prefix* prefix = NULL; |
| 1194 |
fcml_st_prefixes_details *prefixes = &(context->prefixes); |
| 1195 |
fcml_int prefix_count = prefixes->prefixes_count; |
| 1196 |
fcml_int i; |
| 1197 |
for (i = 0; i < prefix_count; i++) { |
| 1198 |
if (prefixes->prefixes[i].prefix == prefix_value) { |
| 1199 |
prefix = &(prefixes->prefixes[i]); |
| 1200 |
break; |
| 1201 |
} |
| 1202 |
} |
| 1203 |
return prefix; |
| 1204 |
} |
| 1205 |
|
| 1206 |
static fcml_bool is_prefix_available(decoding_context *context, |
| 1207 |
fcml_uint8_t prefix, fcml_bool mandatory) { |
| 1208 |
fcml_st_prefixes_details *prefixes = &(context->prefixes); |
| 1209 |
/* Handle VEX mandatory prefixes.*/ |
| 1210 |
if (mandatory && (prefixes->is_vex || prefixes->is_evex) && prefixes->pp) { |
| 1211 |
if (prefixes->pp == 0x01 && prefix == 0x66) { |
| 1212 |
return FCML_TRUE; |
| 1213 |
} |
| 1214 |
if (prefixes->pp == 0x02 && prefix == 0xF3) { |
| 1215 |
return FCML_TRUE; |
| 1216 |
} |
| 1217 |
if (prefixes->pp == 0x03 && prefix == 0xF2) { |
| 1218 |
return FCML_TRUE; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
fcml_st_instruction_prefix *found_prefix = |
| 1222 |
get_prefix_if_available(context, prefix); |
| 1223 |
return mandatory ? (found_prefix != NULL && found_prefix->mandatory_prefix) |
| 1224 |
: (found_prefix != NULL); |
| 1225 |
} |
| 1226 |
|
| 1227 |
static void clear_mandatory_flag(decoding_context *context, |
| 1228 |
fcml_uint8_t prefix_code) { |
| 1229 |
fcml_st_instruction_prefix *prefix = get_prefix_if_available( |
| 1230 |
context, prefix_code); |
| 1231 |
if (prefix) { |
| 1232 |
prefix->mandatory_prefix = FCML_FALSE; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
/******************************* |
| 1237 |
* Effective attributes sizes. * |
| 1238 |
*******************************/ |
| 1239 |
|
| 1240 |
static fcml_usize calculate_effective_asa(decoding_context *context) { |
| 1241 |
|
| 1242 |
fcml_st_entry_point *entry_point = |
| 1243 |
&(context->disassembler_context->entry_point); |
| 1244 |
fcml_usize effective_asa = entry_point->address_size_attribute; |
| 1245 |
|
| 1246 |
/* Checks if address size attribute is overridden.*/ |
| 1247 |
if (is_prefix_available(context, 0x67, FCML_FALSE)) { |
| 1248 |
switch (entry_point->op_mode) { |
| 1249 |
case FCML_OM_16_BIT: |
| 1250 |
case FCML_OM_32_BIT: |
| 1251 |
effective_asa = (effective_asa == FCML_DS_32) |
| 1252 |
? FCML_DS_16 : FCML_DS_32; |
| 1253 |
break; |
| 1254 |
case FCML_OM_64_BIT: |
| 1255 |
effective_asa = (effective_asa == FCML_DS_32) |
| 1256 |
? FCML_DS_64 : FCML_DS_32; |
| 1257 |
break; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
return effective_asa; |
| 1262 |
} |
| 1263 |
|
| 1264 |
static fcml_usize calculate_effective_osa(decoding_context *context, |
| 1265 |
fcml_uint32_t opcode_flags) { |
| 1266 |
|
| 1267 |
fcml_st_instruction_prefix *prefix; |
| 1268 |
fcml_st_entry_point *entry_point = |
| 1269 |
&(context->disassembler_context->entry_point); |
| 1270 |
fcml_st_prefixes_details *prefixes = &(context->prefixes); |
| 1271 |
fcml_usize osa = entry_point->operand_size_attribute; |
| 1272 |
|
| 1273 |
/* Gets effective address-size attribute for used mode.*/ |
| 1274 |
switch (entry_point->op_mode) { |
| 1275 |
case FCML_OM_16_BIT: |
| 1276 |
case FCML_OM_32_BIT: |
| 1277 |
/* In 16 and 32 bit mode only prefixes can change address-size |
| 1278 |
* attribute. |
| 1279 |
*/ |
| 1280 |
prefix = get_prefix_if_available(context, 0x66); |
| 1281 |
if (prefix != NULL && !prefix->mandatory_prefix) { |
| 1282 |
osa = (osa == FCML_DS_16) ? FCML_DS_32 : FCML_DS_16; |
| 1283 |
} |
| 1284 |
break; |
| 1285 |
case FCML_OM_64_BIT: |
| 1286 |
/* For some instructions EOSA can be forced to 64 bits.*/ |
| 1287 |
if (FCML_DEF_OPCODE_FLAGS_FORCE_64BITS_EOSA(opcode_flags)) { |
| 1288 |
osa = FCML_DS_64; |
| 1289 |
} else { |
| 1290 |
if (prefixes->W) { |
| 1291 |
/* Prefixes can not override REX.W.*/ |
| 1292 |
osa = FCML_DS_64; |
| 1293 |
} else { |
| 1294 |
prefix = get_prefix_if_available(context, 0x66); |
| 1295 |
if (prefix != NULL && !prefix->mandatory_prefix) { |
| 1296 |
osa = (osa == FCML_DS_16) ? FCML_DS_32 : FCML_DS_16; |
| 1297 |
} else { |
| 1298 |
/* By default EOSA 32 bits is a default value in 64 |
| 1299 |
* bit mode, but some instruction do not use this rule. |
| 1300 |
*/ |
| 1301 |
if (FCML_DEF_OPCODE_FLAGS_64BITS_EOSA_BY_DEFAULT( |
| 1302 |
opcode_flags) && (osa == FCML_DS_32)) { |
| 1303 |
osa = FCML_DS_64; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
break; |
| 1309 |
} |
| 1310 |
|
| 1311 |
return osa; |
| 1312 |
} |
| 1313 |
|
| 1314 |
/************** |
| 1315 |
* Acceptors. * |
| 1316 |
**************/ |
| 1317 |
|
| 1318 |
typedef instruction_acceptor (*inst_acceptor_factory)( |
| 1319 |
fcml_st_def_addr_mode_desc *addr_mode_desc); |
| 1320 |
|
| 1321 |
typedef struct inst_acceptor_factory_holder { |
| 1322 |
fcml_string name; |
| 1323 |
inst_acceptor_factory acceptor; |
| 1324 |
} inst_acceptor_factory_holder; |
| 1325 |
|
| 1326 |
#define FCML_IA_FACTORY(fn) { #fn, fn } |
| 1327 |
|
| 1328 |
/**************/ |
| 1329 |
/* Prefixes. */ |
| 1330 |
/**************/ |
| 1331 |
|
| 1332 |
static fcml_bool inst_acceptor_prefixes(decoding_context *context) { |
| 1333 |
|
| 1334 |
inst_decoding_def *decoding_def = |
| 1335 |
context->decoding_def; |
| 1336 |
/* Prefixes. */ |
| 1337 |
fcml_st_prefixes_details *prefixes = &(context->prefixes); |
| 1338 |
|
| 1339 |
/* LOCK prefix. */ |
| 1340 |
if (prefixes->is_lock && !FCML_DEF_PREFIX_LOCK_ALLOWED( |
| 1341 |
decoding_def->prefixes_flags)) { |
| 1342 |
FCML_TRACE("Lock prefix is not allowed."); |
| 1343 |
return FCML_FALSE; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* VEX required. */ |
| 1347 |
if ((FCML_DEF_PREFIX_VEX_REQ(decoding_def->prefixes_flags) |
| 1348 |
&& !prefixes->is_vex) |
| 1349 |
|| (!FCML_DEF_PREFIX_VEX_REQ( |
| 1350 |
decoding_def->prefixes_flags) |
| 1351 |
&& prefixes->is_vex)) { |
| 1352 |
FCML_TRACE("VEX prefix is required."); |
| 1353 |
return FCML_FALSE; |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* EVEX required. */ |
| 1357 |
fcml_bool evex_req = FCML_DEF_PREFIX_EVEX_REQ(decoding_def->prefixes_flags); |
| 1358 |
if ((evex_req && !prefixes->is_evex) || (!evex_req && prefixes->is_evex)) { |
| 1359 |
FCML_TRACE("EVEX prefix is required."); |
| 1360 |
return FCML_FALSE; |
| 1361 |
} |
| 1362 |
|
| 1363 |
/* XOP required. */ |
| 1364 |
if ((FCML_DEF_PREFIX_XOP_REQ(decoding_def->prefixes_flags) |
| 1365 |
&& !prefixes->is_xop) |
| 1366 |
|| (!FCML_DEF_PREFIX_XOP_REQ( |
| 1367 |
decoding_def->prefixes_flags) |
| 1368 |
&& prefixes->is_xop)) { |
| 1369 |
FCML_TRACE("XOP prefix is required."); |
| 1370 |
return FCML_FALSE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
/* W field. */ |
| 1374 |
if ((FCML_DEF_PREFIX_W_0(decoding_def->prefixes_flags) |
| 1375 |
&& prefixes->W) |
| 1376 |
|| (FCML_DEF_PREFIX_W_1(decoding_def->prefixes_flags) |
| 1377 |
&& !prefixes->W)) { |
| 1378 |
FCML_TRACE("Prefix field 'W' has unsupported value."); |
| 1379 |
return FCML_FALSE; |
| 1380 |
} |
| 1381 |
|
| 1382 |
/* L field. */ |
| 1383 |
fcml_bool l = FCML_TRUE; |
| 1384 |
if ((FCML_DEF_PREFIX_L_1(decoding_def->prefixes_flags) && |
| 1385 |
(!prefixes->is_avx || !prefixes->L)) |
| 1386 |
|| (FCML_DEF_PREFIX_L_0(decoding_def->prefixes_flags) |
| 1387 |
&& (!prefixes->is_avx || prefixes->L))) { |
| 1388 |
FCML_TRACE("Prefix field L has unsupported value."); |
| 1389 |
l = FCML_FALSE; |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* L' field. */ |
| 1393 |
fcml_bool l_prim = FCML_TRUE; |
| 1394 |
if ((FCML_DEF_PREFIX_L_prim_1(decoding_def->prefixes_flags) && |
| 1395 |
(!prefixes->is_avx || !prefixes->L_prim)) |
| 1396 |
|| (FCML_DEF_PREFIX_L_prim_0( |
| 1397 |
decoding_def->prefixes_flags) |
| 1398 |
&& (!prefixes->is_avx || prefixes->L_prim))) { |
| 1399 |
FCML_TRACE("Prefix field L' has unsupported value."); |
| 1400 |
l_prim = FCML_FALSE; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if ((!l || !l_prim) && !((decoding_def->decorators_existence.er || |
| 1404 |
decoding_def->decorators_existence.sae ) && prefixes->b)) { |
| 1405 |
return FCML_FALSE; |
| 1406 |
} |
| 1407 |
|
| 1408 |
/* Mandatory prefixes. */ |
| 1409 |
|
| 1410 |
fcml_bool found = FCML_FALSE; |
| 1411 |
if (FCML_DEF_PREFIX_MANDATORY_66( |
| 1412 |
decoding_def->prefixes_flags)) { |
| 1413 |
found = is_prefix_available(context, 0x66, FCML_TRUE); |
| 1414 |
} else if (FCML_DEF_PREFIX_MANDATORY_F2( |
| 1415 |
decoding_def->prefixes_flags)) { |
| 1416 |
found = is_prefix_available(context, 0xF2, FCML_TRUE); |
| 1417 |
} else if (FCML_DEF_PREFIX_MANDATORY_F3( |
| 1418 |
decoding_def->prefixes_flags)) { |
| 1419 |
found = is_prefix_available(context, 0xF3, FCML_TRUE); |
| 1420 |
} else { |
| 1421 |
/* Mandatory prefixes not used. */ |
| 1422 |
found = !((prefixes->is_vex || prefixes->is_evex) && prefixes->pp); |
| 1423 |
} |
| 1424 |
if (!found) { |
| 1425 |
/* Mandatory prefixes not found. */ |
| 1426 |
FCML_TRACE("Missing mandatory prefix."); |
| 1427 |
return FCML_FALSE; |
| 1428 |
} |
| 1429 |
|
| 1430 |
return FCML_TRUE; |
| 1431 |
} |
| 1432 |
|
| 1433 |
static instruction_acceptor inst_acceptor_factory_prefixes( |
| 1434 |
fcml_st_def_addr_mode_desc *addr_mode_desc) { |
| 1435 |
return inst_acceptor_prefixes; |
| 1436 |
} |
| 1437 |
|
| 1438 |
/**********/ |
| 1439 |
/* ModRM. */ |
| 1440 |
/**********/ |
| 1441 |
|
| 1442 |
static fcml_bool inst_acceptor_modrm(decoding_context *context) { |
| 1443 |
|
| 1444 |
inst_decoding_def *instruction_decoding_def = |
| 1445 |
context->decoding_def; |
| 1446 |
fcml_uint32_t opcode_flags = instruction_decoding_def->opcode_flags; |
| 1447 |
fcml_st_memory_stream *code = context->stream; |
| 1448 |
fcml_st_prefixes_details *prefixes = &(context->prefixes); |
| 1449 |
|
| 1450 |
modrm_decoding_details *modrm_details = |
| 1451 |
&(instruction_decoding_def->modrm_details); |
| 1452 |
|
| 1453 |
/* Check addressing mode for ModRM opcodes.*/ |
| 1454 |
if (modrm_details->is_mem_restriction) { |
| 1455 |
fcml_bool modrm_found = FCML_FALSE; |
| 1456 |
fcml_uint8_t modrm = fcml_fn_stream_peek(code, &modrm_found); |
| 1457 |
if (!modrm_found || FCML_MODRM_DEC_MOD(modrm) == 3) { |
| 1458 |
return FCML_FALSE; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
|
| 1462 |
if (modrm_details->is_reg_restriction) { |
| 1463 |
fcml_bool modrm_found = FCML_FALSE; |
| 1464 |
fcml_uint8_t modrm = fcml_fn_stream_peek(code, &modrm_found); |
| 1465 |
if (!modrm_found || FCML_MODRM_DEC_MOD(modrm) != 3) { |
| 1466 |
return FCML_FALSE; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
/* Check opcode extension. We have to access ModRM byte directly here.*/ |
| 1471 |
fcml_bool opcodes_valid = FCML_FALSE; |
| 1472 |
if (FCML_DEF_OPCODE_FLAGS_OPCODE_IS_EXT(opcode_flags)) { |
| 1473 |
fcml_bool modrm_found = FCML_FALSE; |
| 1474 |
fcml_uint8_t modrm = fcml_fn_stream_peek(code, &modrm_found); |
| 1475 |
if (modrm_found) { |
| 1476 |
fcml_uint8_t ext_reg_opcode = ((prefixes->R << 4) |
| 1477 |
| (FCML_MODRM_DEC_REG_OPCODE(modrm))); |
| 1478 |
fcml_uint8_t expected_ext_reg_opcode = |
| 1479 |
FCML_DEF_OPCODE_FLAGS_OPCODE_EXT(opcode_flags); |
| 1480 |
opcodes_valid = (ext_reg_opcode == expected_ext_reg_opcode); |
| 1481 |
} |
| 1482 |
} else { |
| 1483 |
opcodes_valid = FCML_TRUE; |
| 1484 |
} |
| 1485 |
|
| 1486 |
if (!opcodes_valid) { |
| 1487 |
return FCML_FALSE; |
| 1488 |
} |
| 1489 |
|
| 1490 |
return FCML_TRUE; |
| 1491 |
} |
| 1492 |
|
| 1493 |
static instruction_acceptor inst_acceptor_factory_modrm( |
| 1494 |
fcml_st_def_addr_mode_desc *addr_mode_desc) { |
| 1495 |
return FCML_DEF_OPCODE_FLAGS_OPCODE_IS_MODRM(addr_mode_desc->opcode_flags) |
| 1496 |
? inst_acceptor_modrm : NULL; |
| 1497 |
} |
| 1498 |
|
| 1499 |
/********************/ |
| 1500 |
/* Size attributes. */ |
| 1501 |
/********************/ |
| 1502 |
|
| 1503 |
static fcml_bool inst_acceptor_size_attributes_restrictions( |
| 1504 |
decoding_context *context) { |
| 1505 |
|
| 1506 |
inst_decoding_def *instruction_decoding_def = |
| 1507 |
context->decoding_def; |
| 1508 |
|
| 1509 |
/* Check EOSA. These fields allow us to restrict instruction decoding only |
| 1510 |
* to specific effective operand sizes. For instance we can define |
| 1511 |
* instruction that can be chosen only if EOSA is equal to 16. |
| 1512 |
*/ |
| 1513 |
if (FCML_DEF_OPCODE_FLAGS_IS_EOSA_RESTRICTION( |
| 1514 |
instruction_decoding_def->opcode_flags)) { |
| 1515 |
fcml_bool mandatory_prefix = FCML_FALSE; |
| 1516 |
/* Ignore 0x66 prefix if this instruction uses it as mandatory prefix. |
| 1517 |
* This correction is also done in the next phase for already |
| 1518 |
* chosen instruction. |
| 1519 |
*/ |
| 1520 |
fcml_st_instruction_prefix *prefix = |
| 1521 |
get_prefix_if_available(context, 0x66); |
| 1522 |
/* Set this prefix as a mandatory one if this instruction defines 66 as |
| 1523 |
* mandatory. This is set temporarily only to calculate correct |
| 1524 |
* EOSA for instruction. |
| 1525 |
*/ |
| 1526 |
if (prefix != NULL) { |
| 1527 |
mandatory_prefix = prefix->mandatory_prefix; |
| 1528 |
prefix->mandatory_prefix = FCML_DEF_PREFIX_MANDATORY_66( |
| 1529 |
instruction_decoding_def->prefixes_flags); |
| 1530 |
} |
| 1531 |
fcml_usize eosa = calculate_effective_osa(context, |
| 1532 |
instruction_decoding_def->opcode_flags); |
| 1533 |
if (prefix != NULL) { |
| 1534 |
prefix->mandatory_prefix = mandatory_prefix; |
| 1535 |
} |
| 1536 |
if (!((FCML_DEF_OPCODE_FLAGS_EOSA_16( |
| 1537 |
instruction_decoding_def->opcode_flags) |
| 1538 |
&& eosa == FCML_DS_16) |
| 1539 |
|| (FCML_DEF_OPCODE_FLAGS_EOSA_32( |
| 1540 |
instruction_decoding_def->opcode_flags) |
| 1541 |
&& eosa == FCML_DS_32) |
| 1542 |
|| (FCML_DEF_OPCODE_FLAGS_EOSA_64( |
| 1543 |
instruction_decoding_def->opcode_flags) |
| 1544 |
&& eosa == FCML_DS_64))) { |
| 1545 |
/* Wrong EOSA, ignore instruction. */ |
| 1546 |
return FCML_FALSE; |
| 1547 |
} |
| 1548 |
} |
| 1549 |
|
| 1550 |
/* Check EASA. These fields allow us to restrict instruction only |
| 1551 |
* to specific effective address sizes. For instance we can define |
| 1552 |
* instruction that can be chosen only if EASA is equal to 16. |
| 1553 |
*/ |
| 1554 |
if (FCML_DEF_OPCODE_FLAGS_IS_EASA_RESTRICTION( |
| 1555 |
instruction_decoding_def->opcode_flags)) { |
| 1556 |
fcml_usize easa = calculate_effective_asa(context); |
| 1557 |
if (!((FCML_DEF_OPCODE_FLAGS_EASA_16( |
| 1558 |
instruction_decoding_def->opcode_flags) |
| 1559 |
&& easa == FCML_DS_16) |
| 1560 |
|| (FCML_DEF_OPCODE_FLAGS_EASA_32( |
| 1561 |
instruction_decoding_def->opcode_flags) |
| 1562 |
&& easa == FCML_DS_32) |
| 1563 |
|| (FCML_DEF_OPCODE_FLAGS_EASA_64( |
| 1564 |
instruction_decoding_def->opcode_flags) |
| 1565 |
&& easa == FCML_DS_64))) { |
| 1566 |
/* Wrong EASA, ignore instruction.*/ |
| 1567 |
return FCML_FALSE; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
|
| 1571 |
return FCML_TRUE; |
| 1572 |
} |
| 1573 |
|
| 1574 |
static instruction_acceptor inst_acceptor_factory_size_attributes_restrictions( |
| 1575 |
fcml_st_def_addr_mode_desc *addr_mode_desc) { |
| 1576 |
return (FCML_DEF_OPCODE_FLAGS_IS_EOSA_RESTRICTION( |
| 1577 |
addr_mode_desc->opcode_flags) |
| 1578 |
|| FCML_DEF_OPCODE_FLAGS_IS_EASA_RESTRICTION( |
| 1579 |
addr_mode_desc->opcode_flags)) |
| 1580 |
? inst_acceptor_size_attributes_restrictions |
| 1581 |
: NULL; |
| 1582 |
} |
| 1583 |
|
| 1584 |
/********************/ |
| 1585 |
/* Addressing mode. */ |
| 1586 |
/********************/ |
| 1587 |
|
| 1588 |
static fcml_bool inst_acceptor_addr_mode(decoding_context *context) { |
| 1589 |
inst_decoding_def *inst_dec_def = context->decoding_def; |
| 1590 |
fcml_st_disassembler_context *dis_ctx = context->disassembler_context; |
| 1591 |
fcml_en_operating_mode op_mode = dis_ctx->entry_point.op_mode; |
| 1592 |
return ((op_mode == FCML_OM_16_BIT || op_mode == FCML_OM_32_BIT) |
| 1593 |
&& FCML_DEF_OPCODE_FLAGS_16_32_BIT_MODE_SUPPORTED( |
| 1594 |
inst_dec_def->opcode_flags)) |
| 1595 |
|| (op_mode == FCML_OM_64_BIT |
| 1596 |
&& FCML_DEF_OPCODE_FLAGS_64_BIT_MODE_SUPPORTED( |
| 1597 |
inst_dec_def->opcode_flags)); |
| 1598 |
} |
| 1599 |
|
| 1600 |
static instruction_acceptor inst_acceptor_factory_addr_mode( |
| 1601 |
fcml_st_def_addr_mode_desc *addr_mode_desc) { |
| 1602 |
return inst_acceptor_addr_mode; |
| 1603 |
} |
| 1604 |
|
| 1605 |
/************/ |
| 1606 |
/* AVX-512. */ |
| 1607 |
/************/ |
| 1608 |
|
| 1609 |
/* This acceptor is dedicated for AVX-512 broadcast and rounding. */ |
| 1610 |
static fcml_bool inst_acceptor_avx512(decoding_context *context) { |
| 1611 |
|
| 1612 |
inst_decoding_def *decoding_def = context->decoding_def; |
| 1613 |
|
| 1614 |
if (context->prefixes.b) { |
| 1615 |
|
| 1616 |
fcml_st_memory_stream *code = context->stream; |
| 1617 |
|
| 1618 |
/* We need to check ModR/M to know if instruction |
| 1619 |
* uses memory addressing. */ |
| 1620 |
fcml_bool modrm_found = FCML_FALSE; |
| 1621 |
fcml_uint8_t modrm = fcml_fn_stream_peek(code, &modrm_found); |
| 1622 |
fcml_bool is_reg = !modrm_found || FCML_MODRM_DEC_MOD(modrm) == 3; |
| 1623 |
|
| 1624 |
if (is_reg && !decoding_def->decorators_existence.er && |
| 1625 |
!decoding_def->decorators_existence.sae) { |
| 1626 |
/* Register-to-register addressing with prefix flag 'b' set, |
| 1627 |
* has to support rounding. */ |
| 1628 |
FCML_TRACE("Prefix flag 'b' is set but rounding is not supported."); |
| 1629 |
return FCML_FALSE; |
| 1630 |
} |
| 1631 |
|
| 1632 |
if (!is_reg && !decoding_def->decorators_existence.bcast) { |
| 1633 |
/* Memory addressing with prefix flag 'b' set |
| 1634 |
* has to support broadcasting. */ |
| 1635 |
FCML_TRACE("Prefix flag 'b' is set but broadcast is not supported."); |
| 1636 |
return FCML_FALSE; |
| 1637 |
} |
| 1638 |
} |
| 1639 |
|
| 1640 |
return FCML_TRUE; |
| 1641 |
} |
| 1642 |
|
| 1643 |
static instruction_acceptor inst_acceptor_factory_avx512( |
| 1644 |
fcml_st_def_addr_mode_desc *addr_mode_desc) { |
| 1645 |
return ((addr_mode_desc->instruction_group == FCML_AMT_AVX512_SIMD) && |
| 1646 |
FCML_DEF_OPCODE_FLAGS_OPCODE_IS_MODRM( |
| 1647 |
addr_mode_desc->opcode_flags)) ? |
| 1648 |
inst_acceptor_avx512 : NULL; |
| 1649 |
} |
| 1650 |
|
| 1651 |
static inst_acceptor_factory_holder acceptor_factories[] = { |
| 1652 |
FCML_IA_FACTORY(inst_acceptor_factory_prefixes), |
| 1653 |
FCML_IA_FACTORY(inst_acceptor_factory_addr_mode), |
| 1654 |
FCML_IA_FACTORY(inst_acceptor_factory_modrm), |
| 1655 |
FCML_IA_FACTORY(inst_acceptor_factory_size_attributes_restrictions), |
| 1656 |
FCML_IA_FACTORY(inst_acceptor_factory_avx512), |
| 1657 |
{NULL, NULL} |
| 1658 |
}; |
| 1659 |
|
| 1660 |
static fcml_ceh_error allocate_acceptors_chain( |
| 1661 |
fcml_st_def_addr_mode_desc *addr_mode_desc, |
| 1662 |
addr_mode_acceptor_chain **chain) { |
| 1663 |
|
| 1664 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1665 |
|
| 1666 |
addr_mode_acceptor_chain *current = NULL, *chain_root = NULL; |
| 1667 |
|
| 1668 |
inst_acceptor_factory_holder *factory = |
| 1669 |
&(acceptor_factories[0]); |
| 1670 |
|
| 1671 |
while (factory->acceptor) { |
| 1672 |
/* Asks every factory for acceptor dedicated to the provided |
| 1673 |
* addressing mode description. |
| 1674 |
*/ |
| 1675 |
instruction_acceptor acceptor = |
| 1676 |
factory->acceptor(addr_mode_desc); |
| 1677 |
if (acceptor) { |
| 1678 |
addr_mode_acceptor_chain *chain_element = |
| 1679 |
(addr_mode_acceptor_chain*) |
| 1680 |
fcml_fn_env_memory_alloc_clear( |
| 1681 |
sizeof(addr_mode_acceptor_chain)); |
| 1682 |
if (!chain_element) { |
| 1683 |
/* Free already allocated chain.*/ |
| 1684 |
if (chain_root) { |
| 1685 |
free_acceptors_chain(chain_root); |
| 1686 |
} |
| 1687 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 1688 |
} |
| 1689 |
chain_element->name = factory->name; |
| 1690 |
chain_element->acceptor = acceptor; |
| 1691 |
if (!chain_root) { |
| 1692 |
chain_root = chain_element; |
| 1693 |
} |
| 1694 |
if (current) { |
| 1695 |
current->next = chain_element; |
| 1696 |
} |
| 1697 |
current = chain_element; |
| 1698 |
} |
| 1699 |
factory++; |
| 1700 |
} |
| 1701 |
|
| 1702 |
*chain = chain_root; |
| 1703 |
|
| 1704 |
return error; |
| 1705 |
} |
| 1706 |
|
| 1707 |
/********************** |
| 1708 |
* Decoding callback. * |
| 1709 |
**********************/ |
| 1710 |
|
| 1711 |
static void dispose_inst_def_callback_default( |
| 1712 |
fcml_st_dialect_context_int *dialect, |
| 1713 |
fcml_ptr decoding_ptr) { |
| 1714 |
free_decoding_def((inst_decoding_def*) decoding_ptr); |
| 1715 |
} |
| 1716 |
|
| 1717 |
static void prepare_modrm_decoding_details( |
| 1718 |
fcml_st_def_decoded_addr_mode *decoded_addr_mode, |
| 1719 |
modrm_decoding_details *modrm_details) { |
| 1720 |
if (decoded_addr_mode) { |
| 1721 |
if (FCMP_DEF_IS_ADDR_MODE(decoded_addr_mode->addr_mode, |
| 1722 |
FCML_OP_VSIB_BASE)) { |
| 1723 |
fcml_st_def_tma_rm *rm_args = (fcml_st_def_tma_rm *) |
| 1724 |
decoded_addr_mode->addr_mode_args; |
| 1725 |
modrm_details->is_vsib = FCML_TRUE; |
| 1726 |
modrm_details->vsib_index_size = |
| 1727 |
fcml_fn_def_vsib_reg_to_ds(rm_args->vector_index_register); |
| 1728 |
} |
| 1729 |
if (FCMP_DEF_IS_ADDR_MODE(decoded_addr_mode->addr_mode, |
| 1730 |
FCML_OP_RM_BASE)) { |
| 1731 |
fcml_st_def_tma_rm *rm_args = |
| 1732 |
(fcml_st_def_tma_rm*)decoded_addr_mode->addr_mode_args; |
| 1733 |
modrm_details->is_reg_restriction = (rm_args->flags == FCML_RMF_R); |
| 1734 |
modrm_details->is_mem_restriction = (rm_args->flags == FCML_RMF_M); |
| 1735 |
} |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
static fcml_ceh_error prepare_inst_def_callback_default( |
| 1740 |
fcml_st_dialect_context_int *dialect, |
| 1741 |
fcml_st_dt_diss_tree_element *element, |
| 1742 |
fcml_st_def_instruction_desc *instruction_desc, |
| 1743 |
fcml_st_def_addr_mode_desc *addr_mode_desc) { |
| 1744 |
|
| 1745 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1746 |
|
| 1747 |
/* Prepare instruction decoding structure.*/ |
| 1748 |
FCML_ENV_ALLOC_CLEAR(decoding, inst_decoding_def); |
| 1749 |
if (!decoding) { |
| 1750 |
return FCML_CEH_GEC_OUT_OF_MEMORY; |
| 1751 |
} |
| 1752 |
|
| 1753 |
/* Copy opcodes.*/ |
| 1754 |
int i; |
| 1755 |
for (i = 0; i < sizeof(addr_mode_desc->opcode); i++) { |
| 1756 |
decoding->opcodes[i] = addr_mode_desc->opcode[i]; |
| 1757 |
} |
| 1758 |
|
| 1759 |
error = dialect->get_parsed_mnemonics((fcml_st_dialect*) dialect, |
| 1760 |
instruction_desc, addr_mode_desc, &(decoding->mnemonics)); |
| 1761 |
if (error) { |
| 1762 |
free_decoding_def(decoding); |
| 1763 |
return FCML_CEH_GEC_INVALID_INPUT; |
| 1764 |
} |
| 1765 |
|
| 1766 |
/* Copy flags.*/ |
| 1767 |
decoding->prefixes_flags = addr_mode_desc->allowed_prefixes; |
| 1768 |
decoding->opcode_flags = addr_mode_desc->opcode_flags; |
| 1769 |
decoding->instruction_group = addr_mode_desc->instruction_group; |
| 1770 |
decoding->instruction = instruction_desc->instruction; |
| 1771 |
decoding->addr_mode = addr_mode_desc->addr_mode; |
| 1772 |
decoding->details = addr_mode_desc->details; |
| 1773 |
|
| 1774 |
error = allocate_acceptors_chain(addr_mode_desc, |
| 1775 |
&(decoding->instruction_acceptors_chain)); |
| 1776 |
if (error) { |
| 1777 |
free_decoding_def(decoding); |
| 1778 |
return error; |
| 1779 |
} |
| 1780 |
|
| 1781 |
/* Choose function used to disassemble instruction.*/ |
| 1782 |
decoding->instruction_decoder = |
| 1783 |
choose_instruction_decoder( |
| 1784 |
instruction_desc->instruction_type); |
| 1785 |
|
| 1786 |
/* Copy instruction hints from instruction definition.*/ |
| 1787 |
decoding->hints = addr_mode_desc->instruction_hints; |
| 1788 |
|
| 1789 |
/* Prepare operand decoders.*/ |
| 1790 |
for (i = 0; i < FCML_OPERANDS_COUNT; i++) { |
| 1791 |
error = prepare_operand_decoding(addr_mode_desc, |
| 1792 |
&(decoding->operand_decodings[i]), |
| 1793 |
addr_mode_desc->operands[i], |
| 1794 |
&(decoding->hints)); |
| 1795 |
if (!error) { |
| 1796 |
prepare_modrm_decoding_details( |
| 1797 |
decoding->operand_decodings[i].decoded_addr_mode, |
| 1798 |
&(decoding->modrm_details)); |
| 1799 |
} else { |
| 1800 |
free_decoding_def(decoding); |
| 1801 |
return error; |
| 1802 |
} |
| 1803 |
} |
| 1804 |
|
| 1805 |
/* Calculates decorators existence. */ |
| 1806 |
fcml_fn_prepare_decorators_existence(&(addr_mode_desc->operands[0]), |
| 1807 |
&(decoding->decorators_existence)); |
| 1808 |
|
| 1809 |
/* Insert it in appropriate order.*/ |
| 1810 |
int order = calculate_decoding_order(decoding); |
| 1811 |
|
| 1812 |
decoding->order = order; |
| 1813 |
|
| 1814 |
fcml_st_coll_list *instruction_decoding_defs = |
| 1815 |
element->instruction_decoding_defs; |
| 1816 |
fcml_st_coll_list_element *current = instruction_decoding_defs->head; |
| 1817 |
|
| 1818 |
/* Insert prepared instruction decoding in appropriate order.*/ |
| 1819 |
fcml_st_coll_list_element *prev = NULL; |
| 1820 |
while (current) { |
| 1821 |
inst_decoding_def *decoding_def = |
| 1822 |
(inst_decoding_def*)current->item; |
| 1823 |
if (decoding_def && decoding_def->order < order) { |
| 1824 |
break; |
| 1825 |
} |
| 1826 |
prev = current; |
| 1827 |
current = current->next; |
| 1828 |
} |
| 1829 |
|
| 1830 |
if (!fcml_fn_coll_list_insert(instruction_decoding_defs, prev, decoding)) { |
| 1831 |
free_decoding_def(decoding); |
| 1832 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 1833 |
} |
| 1834 |
|
| 1835 |
return error; |
| 1836 |
} |
| 1837 |
|
| 1838 |
/******************** |
| 1839 |
* Opcode iterator. * |
| 1840 |
********************/ |
| 1841 |
|
| 1842 |
static fcml_uint8_t escape_0f[] = {0x0F}; |
| 1843 |
static fcml_uint8_t escape_0f38[] = {0x0F, 0x38}; |
| 1844 |
static fcml_uint8_t escape_0f3A[] = {0x0F, 0x3A}; |
| 1845 |
|
| 1846 |
static fcml_uint8_t *escape_opcode_table[3] = { |
| 1847 |
escape_0f, |
| 1848 |
escape_0f38, |
| 1849 |
escape_0f3A |
| 1850 |
}; |
| 1851 |
|
| 1852 |
static fcml_uint8_t escape_size_tablee_table[] = {1, 2, 2}; |
| 1853 |
|
| 1854 |
struct opcode_iterator; |
| 1855 |
|
| 1856 |
typedef struct opcode_iterator_impl { |
| 1857 |
fcml_st_memory_stream stream; |
| 1858 |
fcml_uint8_t is_virtual_opcode; |
| 1859 |
fcml_uint8_t *virtual_opcode; |
| 1860 |
fcml_int virtual_opcode_count; |
| 1861 |
fcml_int virtual_opcode_offset; |
| 1862 |
} opcode_iterator_impl; |
| 1863 |
|
| 1864 |
static fcml_int decode_escape_opcode_bytes(decoding_context *decoding_context, |
| 1865 |
fcml_uint8_t **virtual_opcode) { |
| 1866 |
|
| 1867 |
fcml_st_prefixes_details *prefixes_fields = &(decoding_context->prefixes); |
| 1868 |
fcml_int8_t size = 0; |
| 1869 |
|
| 1870 |
if (prefixes_fields->is_vex) { |
| 1871 |
if (prefixes_fields->avx_first_byte == 0xC4) { |
| 1872 |
/* 3-byte VEX. */ |
| 1873 |
int index = prefixes_fields->mmmm - 1; |
| 1874 |
*virtual_opcode = escape_opcode_table[index]; |
| 1875 |
size = escape_size_tablee_table[index]; |
| 1876 |
} else { |
| 1877 |
/* The 2-byte VEX implies a leading 0Fh opcode byte. */ |
| 1878 |
*virtual_opcode = escape_0f; |
| 1879 |
size = 1; |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
if (prefixes_fields->is_xop) { |
| 1884 |
/* For XOP opcode byte is directly encoded in mmmmm. */ |
| 1885 |
*virtual_opcode = &(prefixes_fields->mmmm); |
| 1886 |
size = 1; |
| 1887 |
} |
| 1888 |
|
| 1889 |
if (prefixes_fields->is_evex) { |
| 1890 |
int index = prefixes_fields->mmmm - 1; |
| 1891 |
*virtual_opcode = escape_opcode_table[index]; |
| 1892 |
size = escape_size_tablee_table[index]; |
| 1893 |
} |
| 1894 |
|
| 1895 |
return size; |
| 1896 |
} |
| 1897 |
|
| 1898 |
static fcml_ceh_error prepare_opcode_iterator( |
| 1899 |
decoding_context *decoding_context, |
| 1900 |
struct opcode_iterator **iterator_ptr) { |
| 1901 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 1902 |
opcode_iterator_impl *iterator = |
| 1903 |
(opcode_iterator_impl*) fcml_fn_env_memory_alloc_clear( |
| 1904 |
sizeof(opcode_iterator_impl)); |
| 1905 |
if (iterator) { |
| 1906 |
/* Prepare virtual opcodes.*/ |
| 1907 |
iterator->virtual_opcode_count = |
| 1908 |
decode_escape_opcode_bytes( |
| 1909 |
decoding_context, &(iterator->virtual_opcode)); |
| 1910 |
iterator->virtual_opcode_offset = 0; |
| 1911 |
iterator->stream = *decoding_context->stream; |
| 1912 |
iterator->is_virtual_opcode = FCML_FALSE; |
| 1913 |
/* Prepare virtual opcodes.*/ |
| 1914 |
*iterator_ptr = (struct opcode_iterator *) iterator; |
| 1915 |
} else { |
| 1916 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 1917 |
} |
| 1918 |
return error; |
| 1919 |
} |
| 1920 |
|
| 1921 |
static fcml_bool opcode_iterator_has_next( |
| 1922 |
struct opcode_iterator *iterator_ptr) { |
| 1923 |
opcode_iterator_impl *iterator = |
| 1924 |
(opcode_iterator_impl *) iterator_ptr; |
| 1925 |
if (iterator->virtual_opcode_count > 0 && iterator->virtual_opcode_offset |
| 1926 |
< iterator->virtual_opcode_count) { |
| 1927 |
/* Virtual byte is available.*/ |
| 1928 |
return FCML_TRUE; |
| 1929 |
} else if (fcml_fn_stream_size(&(iterator->stream)) > 0) { |
| 1930 |
return FCML_TRUE; |
| 1931 |
} |
| 1932 |
return FCML_FALSE; |
| 1933 |
} |
| 1934 |
|
| 1935 |
static fcml_bool opcode_iterator_is_virtual_opcode( |
| 1936 |
struct opcode_iterator *iterator_ptr) { |
| 1937 |
opcode_iterator_impl *iterator = |
| 1938 |
(opcode_iterator_impl *)iterator_ptr; |
| 1939 |
return iterator->is_virtual_opcode; |
| 1940 |
} |
| 1941 |
|
| 1942 |
static fcml_uint8_t opcode_iterator_next(struct opcode_iterator *iterator_ptr) { |
| 1943 |
opcode_iterator_impl *iterator = |
| 1944 |
(opcode_iterator_impl *)iterator_ptr; |
| 1945 |
if (iterator->virtual_opcode_count > 0 && iterator->virtual_opcode_offset |
| 1946 |
< iterator->virtual_opcode_count) { |
| 1947 |
iterator->is_virtual_opcode = FCML_TRUE; |
| 1948 |
return iterator->virtual_opcode[iterator->virtual_opcode_offset++]; |
| 1949 |
} else { |
| 1950 |
fcml_bool result; |
| 1951 |
fcml_uint8_t opcode_byte = fcml_fn_stream_read(&(iterator->stream), |
| 1952 |
&result); |
| 1953 |
if (result) { |
| 1954 |
iterator->is_virtual_opcode = FCML_FALSE; |
| 1955 |
} |
| 1956 |
return opcode_byte; |
| 1957 |
} |
| 1958 |
return FCML_FALSE; |
| 1959 |
} |
| 1960 |
|
| 1961 |
static void opcode_iterator_free(struct opcode_iterator *iterator_ptr) { |
| 1962 |
if (iterator_ptr) { |
| 1963 |
fcml_fn_env_memory_free(iterator_ptr); |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
/************************** |
| 1968 |
* Instructions decoding. * |
| 1969 |
**************************/ |
| 1970 |
|
| 1971 |
static void decode_opcode_fields(decoding_context *decoding_context, |
| 1972 |
inst_decoding_def *instruction_decoding_def, |
| 1973 |
fcml_uint8_t primary_opcode_byte) { |
| 1974 |
|
| 1975 |
/* TTTN.*/ |
| 1976 |
if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_TTTN( |
| 1977 |
instruction_decoding_def->opcode_flags)) { |
| 1978 |
fcml_uint8_t condition = primary_opcode_byte & 0x0F; |
| 1979 |
decoding_context->is_conditional = FCML_TRUE; |
| 1980 |
decoding_context->condition.condition_type = (fcml_en_condition_type) |
| 1981 |
(condition >> 1); |
| 1982 |
decoding_context->condition.is_negation = primary_opcode_byte & 0x01; |
| 1983 |
} else { |
| 1984 |
decoding_context->is_conditional = FCML_FALSE; |
| 1985 |
} |
| 1986 |
|
| 1987 |
/* W.*/ |
| 1988 |
if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_W( |
| 1989 |
instruction_decoding_def->opcode_flags)) { |
| 1990 |
decoding_context->opcode_field_w_bit = primary_opcode_byte & 0x01; |
| 1991 |
} |
| 1992 |
|
| 1993 |
/* S.*/ |
| 1994 |
if (FCML_DEF_OPCODE_FLAGS_OPCODE_FIELD_S( |
| 1995 |
instruction_decoding_def->opcode_flags)) { |
| 1996 |
decoding_context->opcode_field_s_bit = primary_opcode_byte & 0x02; |
| 1997 |
} |
| 1998 |
} |
| 1999 |
|
| 2000 |
static fcml_ceh_error decode_operand_decorators( |
| 2001 |
decoding_context *decoding_context) { |
| 2002 |
|
| 2003 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 2004 |
|
| 2005 |
for (int i = 0; i < FCML_OPERANDS_COUNT && !error; i++) { |
| 2006 |
|
| 2007 |
operand_decoding *operand_decoding = |
| 2008 |
&(decoding_context->decoding_def->operand_decodings[i]); |
| 2009 |
fcml_st_operand *operand = |
| 2010 |
&(decoding_context->operand_wrappers[i].operand); |
| 2011 |
|
| 2012 |
fcml_st_decorators_prefix_flags flags; |
| 2013 |
flags.b = decoding_context->prefixes.b; |
| 2014 |
flags.z = decoding_context->prefixes.z; |
| 2015 |
flags.aaa = decoding_context->prefixes.aaa; |
| 2016 |
flags.ll = decoding_context->prefixes.L_prim << 1 | |
| 2017 |
decoding_context->prefixes.L; |
| 2018 |
|
| 2019 |
error = fcml_fn_op_decor_decode(&flags, operand_decoding->decorators, |
| 2020 |
operand); |
| 2021 |
} |
| 2022 |
|
| 2023 |
return error; |
| 2024 |
} |
| 2025 |
|
| 2026 |
static fcml_ceh_error instruction_decoder_IA( |
| 2027 |
decoding_context *decoding_context) { |
| 2028 |
|
| 2029 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 2030 |
|
| 2031 |
inst_decoding_def *instruction_decoding_def = |
| 2032 |
decoding_context->decoding_def; |
| 2033 |
|
| 2034 |
/* Instruction hints.*/ |
| 2035 |
|
| 2036 |
decoding_context->instruction_hints = instruction_decoding_def->hints; |
| 2037 |
|
| 2038 |
/* Change prefixes to mandatory one if needed.*/ |
| 2039 |
|
| 2040 |
if (!FCML_DEF_PREFIX_MANDATORY_66( |
| 2041 |
instruction_decoding_def->prefixes_flags)) { |
| 2042 |
clear_mandatory_flag(decoding_context, 0x66); |
| 2043 |
} |
| 2044 |
|
| 2045 |
if (!FCML_DEF_PREFIX_MANDATORY_F2( |
| 2046 |
instruction_decoding_def->prefixes_flags)) { |
| 2047 |
clear_mandatory_flag(decoding_context, 0xF2); |
| 2048 |
} else { |
| 2049 |
decoding_context->prefixes.is_repne = FCML_FALSE; |
| 2050 |
} |
| 2051 |
|
| 2052 |
if (!FCML_DEF_PREFIX_MANDATORY_F3( |
| 2053 |
instruction_decoding_def->prefixes_flags)) { |
| 2054 |
clear_mandatory_flag(decoding_context, 0xF3); |
| 2055 |
} else { |
| 2056 |
decoding_context->prefixes.is_rep = FCML_FALSE; |
| 2057 |
} |
| 2058 |
|
| 2059 |
/* Branches, take into account that this condition does not |
| 2060 |
* collide with multi-instructions. |
| 2061 |
*/ |
| 2062 |
if (instruction_decoding_def->instruction != F_JCC) { |
| 2063 |
decoding_context->prefixes.is_branch = FCML_FALSE; |
| 2064 |
decoding_context->prefixes.is_nobranch = FCML_FALSE; |
| 2065 |
} |
| 2066 |
|
| 2067 |
/* Change REP/REPNE prefixes to XACQUIRE/XRELEASE if instruction |
| 2068 |
* support them. |
| 2069 |
*/ |
| 2070 |
if (FCML_DEF_PREFIX_HLE_ENABLED( |
| 2071 |
instruction_decoding_def->prefixes_flags)) { |
| 2072 |
if (decoding_context->prefixes.is_rep) { |
| 2073 |
decoding_context->prefixes.is_xrelease = FCML_TRUE; |
| 2074 |
decoding_context->prefixes.is_rep = FCML_FALSE; |
| 2075 |
} |
| 2076 |
if (decoding_context->prefixes.is_repne) { |
| 2077 |
decoding_context->prefixes.is_xacquire = FCML_TRUE; |
| 2078 |
decoding_context->prefixes.is_repne = FCML_FALSE; |
| 2079 |
} |
| 2080 |
} |
| 2081 |
|
| 2082 |
decoding_context->calculated_instruction_size = |
| 2083 |
decoding_context->prefixes.prefixes_bytes_count + |
| 2084 |
decoding_context->opcodes_count; |
| 2085 |
|
| 2086 |
/* Calculates effective operand sizes. It's not important if they will |
| 2087 |
* be used or not. |
| 2088 |
*/ |
| 2089 |
decoding_context->effective_address_size_attribute = |
| 2090 |
calculate_effective_asa(decoding_context); |
| 2091 |
decoding_context->effective_operand_size_attribute = |
| 2092 |
calculate_effective_osa(decoding_context, |
| 2093 |
instruction_decoding_def->opcode_flags); |
| 2094 |
decoding_context->element_size = |
| 2095 |
FCML_GET_SIMD_ELEMENT_SIZE(instruction_decoding_def->details); |
| 2096 |
|
| 2097 |
operand_wrapper *operand_wrappers = |
| 2098 |
&(decoding_context->operand_wrappers[0]); |
| 2099 |
modrm_decoding_details *modrm_details = |
| 2100 |
&(instruction_decoding_def->modrm_details); |
| 2101 |
fcml_st_prefixes_details *prefixes = &(decoding_context->prefixes); |
| 2102 |
|
| 2103 |
fcml_uint8_t tuple_type = |
| 2104 |
FCML_GET_SIMD_TUPLETYPE(instruction_decoding_def->details); |
| 2105 |
|
| 2106 |
/* Decode ModRM field if there is any. */ |
| 2107 |
if (FCML_DEF_OPCODE_FLAGS_OPCODE_IS_MODRM( |
| 2108 |
instruction_decoding_def->opcode_flags)) { |
| 2109 |
|
| 2110 |
decoding_context->is_modrm = FCML_TRUE; |
| 2111 |
|
| 2112 |
fcml_st_memory_stream *stream = decoding_context->stream; |
| 2113 |
|
| 2114 |
fcml_uint32_t offset = stream->offset; |
| 2115 |
|
| 2116 |
fcml_usize vector_length = calculate_vector_length( |
| 2117 |
FCML_TT_NONE, FCML_FALSE, prefixes->L | prefixes->L_prim << 1); |
| 2118 |
|
| 2119 |
fcml_st_modrm_decoder_context modrm_context; |
| 2120 |
modrm_context.op_mode = |
| 2121 |
decoding_context->disassembler_context->entry_point.op_mode; |
| 2122 |
modrm_context.effective_address_size = |
| 2123 |
decoding_context->effective_address_size_attribute; |
| 2124 |
modrm_context.input_size = |
| 2125 |
FCML_GET_SIMD_ELEMENT_SIZE(instruction_decoding_def->details); |
| 2126 |
|
| 2127 |
modrm_context.vector_length = vector_length; |
| 2128 |
modrm_context.tuple_type = tuple_type; |
| 2129 |
modrm_context.b = prefixes->b; |
| 2130 |
modrm_context.is_evex = prefixes->is_evex; |
| 2131 |
|
| 2132 |
fcml_st_modrm_source modrm_source; |
| 2133 |
modrm_source.is_vsib = modrm_details->is_vsib; |
| 2134 |
|
| 2135 |
if (modrm_details->is_vsib) { |
| 2136 |
if (modrm_details->vsib_index_size == FCML_DS_UNDEF) { |
| 2137 |
modrm_source.vsib_index_size = vector_length; |
| 2138 |
} else { |
| 2139 |
modrm_source.vsib_index_size = modrm_details->vsib_index_size; |
| 2140 |
} |
| 2141 |
} else { |
| 2142 |
modrm_source.vsib_index_size = 0; |
| 2143 |
} |
| 2144 |
|
| 2145 |
modrm_source.ext_B = prefixes->B; |
| 2146 |
modrm_source.ext_R = prefixes->R; |
| 2147 |
modrm_source.ext_R_prim = prefixes->R_prim; |
| 2148 |
modrm_source.ext_X = prefixes->X; |
| 2149 |
modrm_source.ext_V_prim = prefixes->V_prim; |
| 2150 |
modrm_source.stream = decoding_context->stream; |
| 2151 |
|
| 2152 |
fcml_uint8_t flags = 0; |
| 2153 |
if (decoding_context->disassembler_context-> |
| 2154 |
configuration.extend_disp_to_asa) { |
| 2155 |
flags |= FCML_MODRM_DEC_FLAG_EXTEND_DISPLACEMENT_TO_ASA; |
| 2156 |
} |
| 2157 |
|
| 2158 |
error = fcml_fn_modrm_decode(&modrm_context, &modrm_source, |
| 2159 |
&(decoding_context->decoded_modrm), |
| 2160 |
&(decoding_context->decoded_modrm_details), |
| 2161 |
flags); |
| 2162 |
if (error) { |
| 2163 |
return error; |
| 2164 |
} |
| 2165 |
|
| 2166 |
decoding_context->tuple_type = tuple_type; |
| 2167 |
|
| 2168 |
decoding_context->is_modrm_reg_reg = |
| 2169 |
decoding_context->decoded_modrm.reg.is_not_null; |
| 2170 |
|
| 2171 |
decoding_context->calculated_instruction_size += stream->offset - |
| 2172 |
offset; |
| 2173 |
} |
| 2174 |
|
| 2175 |
/* After decoding ModR/M we can calculate vector length in order to |
| 2176 |
* handle static rounding mode correctly. If {er} is enabled vector |
| 2177 |
* length is implied. */ |
| 2178 |
if (prefixes->is_avx) { |
| 2179 |
/* Static Rounding Mode and SAE is enabled for reg-to-reg instructions |
| 2180 |
* when EXEV.b is set. |
| 2181 |
*/ |
| 2182 |
fcml_bool sae_enabled = prefixes->b && |
| 2183 |
decoding_context->is_modrm_reg_reg && |
| 2184 |
(instruction_decoding_def->decorators_existence.er || |
| 2185 |
instruction_decoding_def->decorators_existence.sae); |
| 2186 |
|
| 2187 |
decoding_context->vector_length = |
| 2188 |
calculate_vector_length(tuple_type, sae_enabled, |
| 2189 |
prefixes->L | prefixes->L_prim << 1); |
| 2190 |
} |
| 2191 |
|
| 2192 |
/* Calculate operands size. */ |
| 2193 |
fcml_int i, operands_size = 0; |
| 2194 |
for (i = 0; i < FCML_OPERANDS_COUNT; i++) { |
| 2195 |
operand_decoding *operand_decoding = |
| 2196 |
&(instruction_decoding_def->operand_decodings[i]); |
| 2197 |
fcml_st_def_decoded_addr_mode *decoded_addr_mode = |
| 2198 |
operand_decoding->decoded_addr_mode; |
| 2199 |
if (operand_decoding->size_calculator) { |
| 2200 |
operands_size += operand_decoding->size_calculator( |
| 2201 |
decoding_context, decoded_addr_mode->addr_mode_args); |
| 2202 |
} |
| 2203 |
} |
| 2204 |
|
| 2205 |
decoding_context->calculated_instruction_size += operands_size; |
| 2206 |
decoding_context->mnemonics = instruction_decoding_def->mnemonics; |
| 2207 |
decoding_context->instruction = instruction_decoding_def->instruction; |
| 2208 |
decoding_context->instruction_group = |
| 2209 |
instruction_decoding_def->instruction_group; |
| 2210 |
decoding_context->addr_mode = instruction_decoding_def->addr_mode; |
| 2211 |
|
| 2212 |
/* Store primary opcode byte.*/ |
| 2213 |
fcml_int opcode_num = FCML_DEF_OPCODE_FLAGS_PRIMARY_OPCODE( |
| 2214 |
instruction_decoding_def->opcode_flags); |
| 2215 |
decoding_context->primary_opcode_byte = decoding_context->opcodes[ |
| 2216 |
opcode_num - decoding_context->virtual_opcodes_count]; |
| 2217 |
|
| 2218 |
/* Decode opcode fields.*/ |
| 2219 |
decode_opcode_fields(decoding_context, |
| 2220 |
instruction_decoding_def, decoding_context->primary_opcode_byte); |
| 2221 |
|
| 2222 |
/* Decode operands.*/ |
| 2223 |
for (i = 0; i < FCML_OPERANDS_COUNT; i++) { |
| 2224 |
operand_decoding *operand_decoding = |
| 2225 |
&(instruction_decoding_def->operand_decodings[i]); |
| 2226 |
fcml_st_def_decoded_addr_mode *decoded_addr_mode = |
| 2227 |
operand_decoding->decoded_addr_mode; |
| 2228 |
/* It's important not to break the loop before it |
| 2229 |
* goes through all the operands to make sure |
| 2230 |
* that all of them will be cleaned. */ |
| 2231 |
fcml_fn_env_memory_clear(operand_wrappers, |
| 2232 |
sizeof(operand_wrapper)); |
| 2233 |
if (operand_decoding->decoder) { |
| 2234 |
error = operand_decoding->decoder(decoding_context, |
| 2235 |
operand_wrappers, operand_decoding->decorators, |
| 2236 |
decoded_addr_mode->addr_mode_args); |
| 2237 |
if (error) { |
| 2238 |
break; |
| 2239 |
} |
| 2240 |
operand_wrappers->operand.hints |= operand_decoding->hints; |
| 2241 |
operand_wrappers->access_mode = decoded_addr_mode->access_mode; |
| 2242 |
operand_wrappers++; |
| 2243 |
} |
| 2244 |
} |
| 2245 |
|
| 2246 |
if (!error) { |
| 2247 |
error = decode_operand_decorators(decoding_context); |
| 2248 |
} |
| 2249 |
|
| 2250 |
/* Decode suffix.*/ |
| 2251 |
if (!error && |
| 2252 |
FCML_DEF_PREFIX_SUFFIX(instruction_decoding_def->prefixes_flags)) { |
| 2253 |
fcml_bool result; |
| 2254 |
decoding_context->suffix.value = fcml_fn_stream_read( |
| 2255 |
decoding_context->stream, &result); |
| 2256 |
if (result) { |
| 2257 |
decoding_context->suffix.is_not_null = FCML_TRUE; |
| 2258 |
} else { |
| 2259 |
error = FCML_CEH_GEC_EOF; |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
return error; |
| 2264 |
} |
| 2265 |
|
| 2266 |
static fcml_ceh_error prepare_pseudo_operation(decoding_context *context) { |
| 2267 |
|
| 2268 |
/* Seek to the beginning of the stream. */ |
| 2269 |
fcml_fn_stream_seek(context->stream, 0, FCML_EN_ST_START); |
| 2270 |
|
| 2271 |
/* Clean whole decoding context, because there might be |
| 2272 |
* remains of the previous processing. |
| 2273 |
*/ |
| 2274 |
fcml_fn_env_memory_clear(&(context->prefixes), |
| 2275 |
sizeof(fcml_st_prefixes_details)); |
| 2276 |
|
| 2277 |
/* Get first instruction byte. */ |
| 2278 |
fcml_bool result; |
| 2279 |
fcml_uint8_t po_byte = fcml_fn_stream_read(context->stream, &result); |
| 2280 |
if (!result) { |
| 2281 |
return FCML_CEH_GEC_EOF; |
| 2282 |
} |
| 2283 |
|
| 2284 |
context->pseudo_op = FP_DB; |
| 2285 |
|
| 2286 |
/* Prepare operand with immediate value. */ |
| 2287 |
fcml_st_operand *operand = &(context->operand_wrappers[0].operand); |
| 2288 |
operand->type = FCML_OT_IMMEDIATE; |
| 2289 |
operand->immediate.int8 = po_byte; |
| 2290 |
operand->immediate.size = FCML_DS_8; |
| 2291 |
|
| 2292 |
context->opcodes_count = 1; |
| 2293 |
context->calculated_instruction_size = 1; |
| 2294 |
|
| 2295 |
return FCML_CEH_GEC_NO_ERROR; |
| 2296 |
} |
| 2297 |
|
| 2298 |
static fcml_ceh_error decode_instruction(decoding_context *context) { |
| 2299 |
|
| 2300 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 2301 |
disassembler *disasm = |
| 2302 |
(disassembler *)context->disassembler_context->disassembler; |
| 2303 |
fcml_st_dt_decoding_tree *decoding_tree = disasm->decoding_tree; |
| 2304 |
|
| 2305 |
/* Prepare opcode iterator.*/ |
| 2306 |
struct opcode_iterator *iterator; |
| 2307 |
error = prepare_opcode_iterator(context, &iterator); |
| 2308 |
if (error) { |
| 2309 |
return error; |
| 2310 |
} |
| 2311 |
|
| 2312 |
struct fcml_st_dt_diss_tree_element **opcodes = |
| 2313 |
&(decoding_tree->opcode[0]); |
| 2314 |
|
| 2315 |
/* Found instruction addressing modes going here.*/ |
| 2316 |
fcml_st_dt_diss_tree_element* tree_element = NULL; |
| 2317 |
|
| 2318 |
while (opcode_iterator_has_next(iterator)) { |
| 2319 |
/* Get next instructions opcode. */ |
| 2320 |
fcml_uint8_t opcode_byte = opcode_iterator_next( |
| 2321 |
iterator); |
| 2322 |
|
| 2323 |
/* Get instruction for given opcode. */ |
| 2324 |
fcml_st_dt_diss_tree_element *current = opcodes[opcode_byte]; |
| 2325 |
|
| 2326 |
if (current) { |
| 2327 |
/* There is something for given opcode byte, so save it.*/ |
| 2328 |
tree_element = current; |
| 2329 |
/* Store all non virtual opcode bytes inside context.*/ |
| 2330 |
if (!opcode_iterator_is_virtual_opcode(iterator)) { |
| 2331 |
context->opcodes[context->opcodes_count++] = opcode_byte; |
| 2332 |
} else { |
| 2333 |
context->virtual_opcodes_count++; |
| 2334 |
} |
| 2335 |
opcodes = &(tree_element->opcodes[0]); |
| 2336 |
} else { |
| 2337 |
break; |
| 2338 |
} |
| 2339 |
} |
| 2340 |
|
| 2341 |
/* Free opcode iterator.*/ |
| 2342 |
opcode_iterator_free(iterator); |
| 2343 |
|
| 2344 |
/* Skip opcode bytes.*/ |
| 2345 |
fcml_fn_stream_seek(context->stream, context->opcodes_count, |
| 2346 |
FCML_EN_ST_CURRENT); |
| 2347 |
|
| 2348 |
fcml_bool found = FCML_FALSE; |
| 2349 |
|
| 2350 |
/* Disassemble instruction using most appropriate addressing mode from |
| 2351 |
* disassembling tree. |
| 2352 |
*/ |
| 2353 |
if (tree_element && tree_element->instruction_decoding_defs) { |
| 2354 |
|
| 2355 |
fcml_st_coll_list_element *current = |
| 2356 |
tree_element->instruction_decoding_defs->head; |
| 2357 |
int i = 0; |
| 2358 |
while (current) { |
| 2359 |
inst_decoding_def *decoding_def = |
| 2360 |
(inst_decoding_def*) current->item; |
| 2361 |
if (decoding_def) { |
| 2362 |
|
| 2363 |
FCML_TRACE("Accepting next addressing mode %d - " \ |
| 2364 |
"prefixes: 0x%08X, opcode: 0x%08X.", |
| 2365 |
i, decoding_def->prefixes_flags, |
| 2366 |
decoding_def->opcode_flags); |
| 2367 |
|
| 2368 |
/* Set current decoding definition.*/ |
| 2369 |
context->decoding_def = decoding_def; |
| 2370 |
/* Try to accept this addressing mode.*/ |
| 2371 |
fcml_bool accept = FCML_TRUE; |
| 2372 |
addr_mode_acceptor_chain *chain = |
| 2373 |
decoding_def->instruction_acceptors_chain; |
| 2374 |
while (chain) { |
| 2375 |
if (chain->acceptor && !chain->acceptor(context)) { |
| 2376 |
FCML_TRACE("Denied addressing mode %d - " \ |
| 2377 |
"prefixes: 0x%08X, opcode: 0x%08X. Acceptor: %s", |
| 2378 |
i, decoding_def->prefixes_flags, |
| 2379 |
decoding_def->opcode_flags, |
| 2380 |
chain->name); |
| 2381 |
accept = FCML_FALSE; |
| 2382 |
break; |
| 2383 |
} |
| 2384 |
chain = chain->next; |
| 2385 |
} |
| 2386 |
if (accept) { |
| 2387 |
FCML_TRACE("Accepted addressing mode %d - " \ |
| 2388 |
"prefixes: 0x%08X, opcode: 0x%08X.", |
| 2389 |
i, decoding_def->prefixes_flags, |
| 2390 |
decoding_def->opcode_flags); |
| 2391 |
fcml_stream_pointer save_point = |
| 2392 |
fcml_fn_stream_save_point(context->stream); |
| 2393 |
error = decoding_def->instruction_decoder(context); |
| 2394 |
if (!error) { |
| 2395 |
found = FCML_TRUE; |
| 2396 |
break; |
| 2397 |
} else { |
| 2398 |
FCML_TRACE("Instruction decoding failed for " |
| 2399 |
"addressing mode %d - " \ |
| 2400 |
"prefixes: 0x%08X, opcode: 0x%08X. Error: %d", |
| 2401 |
i, decoding_def->prefixes_flags, |
| 2402 |
decoding_def->opcode_flags, error); |
| 2403 |
/* We are to try the next addressing form, so |
| 2404 |
* restore the stream. */ |
| 2405 |
fcml_fn_stream_restore_point(context->stream, |
| 2406 |
save_point); |
| 2407 |
} |
| 2408 |
} |
| 2409 |
} |
| 2410 |
current = current->next; |
| 2411 |
i++; |
| 2412 |
} |
| 2413 |
|
| 2414 |
} else { |
| 2415 |
if (!context->disassembler_context->configuration |
| 2416 |
.fail_if_unknown_instruction) { |
| 2417 |
error = prepare_pseudo_operation(context); |
| 2418 |
if (!error) { |
| 2419 |
found = FCML_TRUE; |
| 2420 |
} |
| 2421 |
} else { |
| 2422 |
/* Unknown instruction.*/ |
| 2423 |
error = FCML_CEH_GEC_UNKNOWN_INSTRUCTION; |
| 2424 |
} |
| 2425 |
} |
| 2426 |
|
| 2427 |
if (!found && !error) { |
| 2428 |
error = FCML_CEH_GEC_UNKNOWN_INSTRUCTION; |
| 2429 |
} |
| 2430 |
|
| 2431 |
return error; |
| 2432 |
} |
| 2433 |
|
| 2434 |
/**************************** |
| 2435 |
* Prefixes decoding. |
| 2436 |
****************************/ |
| 2437 |
|
| 2438 |
#define FCML_IDFPF_IS_MANDATORY_CANDIDATE 0x0001 |
| 2439 |
#define FCML_IDFPF_IS_LOCK 0x0002 |
| 2440 |
#define FCML_IDFPF_IS_REP_XRELEASE 0x0004 |
| 2441 |
#define FCML_IDFPF_IS_REPNE_XACQUIRE 0x0008 |
| 2442 |
#define FCML_IDFPF_IS_VEX 0x0010 |
| 2443 |
#define FCML_IDFPF_IS_XOP 0x0020 |
| 2444 |
#define FCML_IDFPF_IS_BRANCH 0x0040 |
| 2445 |
#define FCML_IDFPF_IS_XOP_ALLOWED 0x0080 |
| 2446 |
#define FCML_IDFPF_IS_NOBRANCH 0x0100 |
| 2447 |
#define FCML_IDFPF_IS_EVEX 0x0200 |
| 2448 |
|
| 2449 |
static fcml_ceh_error decode_prefixes(decoding_context *decoding_context) { |
| 2450 |
|
| 2451 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 2452 |
|
| 2453 |
fcml_st_prefixes_details *prefixes = &(decoding_context->prefixes); |
| 2454 |
fcml_st_memory_stream *stream = decoding_context->stream; |
| 2455 |
fcml_en_operating_mode op_mode = |
| 2456 |
decoding_context->disassembler_context->entry_point.op_mode; |
| 2457 |
fcml_st_prefixes_details *prefixes_details = &(decoding_context->prefixes); |
| 2458 |
fcml_uint16_t p_flags = 0; |
| 2459 |
fcml_en_prefix_types prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2460 |
fcml_int prefix_index = 0; |
| 2461 |
fcml_int prefix_size = 0; |
| 2462 |
fcml_int xop_vex_prefix_size = 0; |
| 2463 |
fcml_bool result = FCML_FALSE; |
| 2464 |
fcml_bool is_xop_vex_allowed = FCML_TRUE; |
| 2465 |
fcml_bool is_last_prefix = FCML_FALSE; |
| 2466 |
|
| 2467 |
/* VEX like prefixes are not allowed in 16 bit mode.*/ |
| 2468 |
if (op_mode == FCML_OM_16_BIT) { |
| 2469 |
is_xop_vex_allowed = FCML_FALSE; |
| 2470 |
} |
| 2471 |
|
| 2472 |
do { |
| 2473 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2474 |
p_flags = 0; |
| 2475 |
/* Almost all prefixes are one byte length, so it's a |
| 2476 |
* reasonable default here. |
| 2477 |
*/ |
| 2478 |
prefix_size = 1; |
| 2479 |
fcml_uint8_t prefix = fcml_fn_stream_peek(stream, &result); |
| 2480 |
if (result) { |
| 2481 |
fcml_st_instruction_prefix *prefix_details = |
| 2482 |
&(prefixes->prefixes[prefix_index]); |
| 2483 |
switch (prefix) { |
| 2484 |
case 0xF0: |
| 2485 |
prefix_type = FCML_PT_GROUP_1; |
| 2486 |
is_xop_vex_allowed = FCML_FALSE; |
| 2487 |
p_flags |= FCML_IDFPF_IS_LOCK; |
| 2488 |
break; |
| 2489 |
case 0xF2: |
| 2490 |
p_flags |= (FCML_IDFPF_IS_MANDATORY_CANDIDATE | |
| 2491 |
FCML_IDFPF_IS_REPNE_XACQUIRE); |
| 2492 |
prefix_type = FCML_PT_GROUP_1; |
| 2493 |
is_xop_vex_allowed = FCML_FALSE; |
| 2494 |
break; |
| 2495 |
case 0xF3: |
| 2496 |
p_flags |= (FCML_IDFPF_IS_MANDATORY_CANDIDATE | |
| 2497 |
FCML_IDFPF_IS_REP_XRELEASE); |
| 2498 |
prefix_type = FCML_PT_GROUP_1; |
| 2499 |
is_xop_vex_allowed = FCML_FALSE; |
| 2500 |
break; |
| 2501 |
case 0x2E: |
| 2502 |
p_flags |= FCML_IDFPF_IS_NOBRANCH; |
| 2503 |
prefix_type = FCML_PT_GROUP_2; |
| 2504 |
break; |
| 2505 |
case 0x3E: |
| 2506 |
p_flags |= FCML_IDFPF_IS_BRANCH; |
| 2507 |
prefix_type = FCML_PT_GROUP_2; |
| 2508 |
break; |
| 2509 |
case 0x26: |
| 2510 |
case 0x36: |
| 2511 |
case 0x64: |
| 2512 |
case 0x65: |
| 2513 |
prefix_type = FCML_PT_GROUP_2; |
| 2514 |
break; |
| 2515 |
break; |
| 2516 |
case 0x66: |
| 2517 |
p_flags |= FCML_IDFPF_IS_MANDATORY_CANDIDATE; |
| 2518 |
prefix_type = FCML_PT_GROUP_3; |
| 2519 |
is_xop_vex_allowed = FCML_FALSE; |
| 2520 |
break; |
| 2521 |
case 0x67: |
| 2522 |
prefix_type = FCML_PT_GROUP_4; |
| 2523 |
break; |
| 2524 |
case 0xC5: |
| 2525 |
xop_vex_prefix_size = 1; |
| 2526 |
prefix_type = FCML_PT_VEX; |
| 2527 |
p_flags |= FCML_IDFPF_IS_VEX; |
| 2528 |
is_last_prefix = FCML_TRUE; |
| 2529 |
break; |
| 2530 |
case 0x8F: |
| 2531 |
xop_vex_prefix_size = 2; |
| 2532 |
prefix_type = FCML_PT_XOP; |
| 2533 |
p_flags |= FCML_IDFPF_IS_XOP; |
| 2534 |
is_last_prefix = FCML_TRUE; |
| 2535 |
break; |
| 2536 |
case 0xC4: |
| 2537 |
xop_vex_prefix_size = 2; |
| 2538 |
prefix_type = FCML_PT_VEX; |
| 2539 |
p_flags |= FCML_IDFPF_IS_VEX; |
| 2540 |
is_last_prefix = FCML_TRUE; |
| 2541 |
break; |
| 2542 |
case 0x62: |
| 2543 |
xop_vex_prefix_size = 3; |
| 2544 |
prefix_type = FCML_PT_EVEX; |
| 2545 |
p_flags |= FCML_IDFPF_IS_EVEX; |
| 2546 |
is_last_prefix = FCML_TRUE; |
| 2547 |
break; |
| 2548 |
default: |
| 2549 |
/* REX prefix is the last one, so we have to break this loop |
| 2550 |
* after finding one. |
| 2551 |
*/ |
| 2552 |
if (op_mode == FCML_OM_64_BIT && prefix >= 0x40 |
| 2553 |
&& prefix <= 0x4F) { |
| 2554 |
/* REX prefix found.*/ |
| 2555 |
prefix_type = FCML_PT_REX; |
| 2556 |
/* Decode fields.*/ |
| 2557 |
prefixes_details->is_rex = FCML_TRUE; |
| 2558 |
prefixes_details->W = FCML_REX_W(prefix); |
| 2559 |
prefixes_details->R = FCML_REX_R(prefix); |
| 2560 |
prefixes_details->X = FCML_REX_X(prefix); |
| 2561 |
prefixes_details->B = FCML_REX_B(prefix); |
| 2562 |
/* REX can not be used together with the VEX at the |
| 2563 |
* moment. |
| 2564 |
*/ |
| 2565 |
is_xop_vex_allowed = FCML_FALSE; |
| 2566 |
is_last_prefix = FCML_TRUE; |
| 2567 |
} |
| 2568 |
break; |
| 2569 |
} |
| 2570 |
|
| 2571 |
/* Handle VEX/XOP prefixes. */ |
| 2572 |
if (prefix_type == FCML_PT_VEX || prefix_type == FCML_PT_XOP |
| 2573 |
|| prefix_type == FCML_PT_EVEX) { |
| 2574 |
|
| 2575 |
if (is_xop_vex_allowed) { |
| 2576 |
|
| 2577 |
fcml_stream_pointer sp = fcml_fn_stream_save_point(stream); |
| 2578 |
|
| 2579 |
/* Skip to the second byte of VEX prefix.*/ |
| 2580 |
fcml_fn_stream_seek(stream, 1, FCML_EN_ST_CURRENT); |
| 2581 |
|
| 2582 |
if (op_mode == FCML_OM_32_BIT) { |
| 2583 |
/* Check if it is really a VEX/XOP prefix.*/ |
| 2584 |
fcml_uint8_t second_byte = fcml_fn_stream_peek(stream, |
| 2585 |
&result); |
| 2586 |
/* VEX.R/XOP.R and VEX.X/XOP.X has to be set to 11 in |
| 2587 |
* 32bit mode. |
| 2588 |
*/ |
| 2589 |
if (!result || (second_byte & 0xC0) != 0xC0) { |
| 2590 |
fcml_fn_stream_restore_point(stream, sp); |
| 2591 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2592 |
} |
| 2593 |
/* In case of XOP prefix, if the MMMMM field is less |
| 2594 |
* than 8 then these two bytes are a form of the POP |
| 2595 |
* instruction rather than an XOP prefix. |
| 2596 |
*/ |
| 2597 |
if (prefix_type == FCML_PT_XOP && |
| 2598 |
FCML_VEX_MMMM(second_byte) < 0x08) { |
| 2599 |
fcml_fn_stream_restore_point(stream, sp); |
| 2600 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2601 |
} |
| 2602 |
} |
| 2603 |
|
| 2604 |
/* Copy rest of the VEX prefixes.*/ |
| 2605 |
if (prefix_type != FCML_PT_GROUP_UNKNOWN) { |
| 2606 |
|
| 2607 |
fcml_int nbytes = fcml_fn_stream_read_bytes(stream, |
| 2608 |
&(prefix_details->avx_bytes), |
| 2609 |
xop_vex_prefix_size); |
| 2610 |
if (nbytes != xop_vex_prefix_size) { |
| 2611 |
/* Stream is incomplete, so we can not treat it |
| 2612 |
* as a VEX/XOP. |
| 2613 |
*/ |
| 2614 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2615 |
} else { |
| 2616 |
switch (prefix) { |
| 2617 |
/* Decodes VEX/XOP fields.*/ |
| 2618 |
case 0x62: { |
| 2619 |
fcml_uint8_t p0 = |
| 2620 |
prefix_details->avx_bytes[0]; |
| 2621 |
fcml_uint8_t p1 = |
| 2622 |
prefix_details->avx_bytes[1]; |
| 2623 |
fcml_uint8_t p2 = |
| 2624 |
prefix_details->avx_bytes[2]; |
| 2625 |
|
| 2626 |
// These fields are fixed for EVEX. |
| 2627 |
if ((p0 & 0x0C) || !(p1 & 0x04)) { |
| 2628 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2629 |
break; |
| 2630 |
} |
| 2631 |
|
| 2632 |
// EVEX.mm cannot be set to 0. |
| 2633 |
if (FCML_EVEX_MM(p0) == 0) { |
| 2634 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2635 |
break; |
| 2636 |
} |
| 2637 |
|
| 2638 |
// In 32-bit mode EVEX.RX has to be set to 11. |
| 2639 |
// Otherwise it's a BOUND instruction. |
| 2640 |
if (op_mode == FCML_OM_32_BIT && |
| 2641 |
(p0 & 0xC0) != 0xC0) { |
| 2642 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2643 |
break; |
| 2644 |
} |
| 2645 |
|
| 2646 |
prefixes_details->R = FCML_EVEX_R(p0); |
| 2647 |
prefixes_details->X = FCML_EVEX_X(p0); |
| 2648 |
prefixes_details->B = FCML_EVEX_B(p0); |
| 2649 |
prefixes_details->R_prim = FCML_EVEX_R_PRIM(p0); |
| 2650 |
prefixes_details->mmmm = FCML_EVEX_MM(p0); |
| 2651 |
|
| 2652 |
prefixes_details->W = FCML_EVEX_W(p1); |
| 2653 |
prefixes_details->vvvv = FCML_EVEX_VVVV(p1); |
| 2654 |
prefixes_details->pp = FCML_EVEX_PP(p1); |
| 2655 |
|
| 2656 |
prefixes_details->z = FCML_EVEX_z(p2); |
| 2657 |
prefixes_details->L_prim = FCML_EVEX_L_PRIM(p2); |
| 2658 |
prefixes_details->L = FCML_EVEX_L(p2); |
| 2659 |
prefixes_details->b = FCML_EVEX_b(p2); |
| 2660 |
prefixes_details->V_prim = FCML_EVEX_V_PRIM(p2); |
| 2661 |
prefixes_details->aaa = FCML_EVEX_aaa(p2); |
| 2662 |
|
| 2663 |
break; |
| 2664 |
} |
| 2665 |
case 0x8F: |
| 2666 |
/* XOP is encoded like 3 bytes VEX prefix.*/ |
| 2667 |
case 0xC4: |
| 2668 |
if (prefix_type == FCML_PT_XOP && |
| 2669 |
FCML_VEX_MMMM( |
| 2670 |
prefix_details->avx_bytes[0]) |
| 2671 |
< 0x08) { |
| 2672 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2673 |
break; |
| 2674 |
} |
| 2675 |
prefixes_details->R = FCML_VEX_R( |
| 2676 |
prefix_details->avx_bytes[0]); |
| 2677 |
prefixes_details->X = FCML_VEX_X( |
| 2678 |
prefix_details->avx_bytes[0]); |
| 2679 |
/* For AVX instructions, this bit is ignored in |
| 2680 |
* 32-bit mode, but AVX-512 instructions using |
| 2681 |
* opmask registers directly use it even in |
| 2682 |
* 32-bit mode to distinguish instructions. |
| 2683 |
*/ |
| 2684 |
prefixes_details->W = FCML_VEX_W( |
| 2685 |
prefix_details->avx_bytes[1]); |
| 2686 |
if (op_mode == FCML_OM_64_BIT) { |
| 2687 |
prefixes_details->B = FCML_VEX_B( |
| 2688 |
prefix_details->avx_bytes[0]); |
| 2689 |
} else { |
| 2690 |
/* Should be silently ignored in |
| 2691 |
32-bit mode. */ |
| 2692 |
prefixes_details->B = 0; |
| 2693 |
} |
| 2694 |
prefixes_details->L = FCML_VEX_L( |
| 2695 |
prefix_details->avx_bytes[1]); |
| 2696 |
prefixes_details->pp = FCML_VEX_PP( |
| 2697 |
prefix_details->avx_bytes[1]); |
| 2698 |
prefixes_details->mmmm = FCML_VEX_MMMM( |
| 2699 |
prefix_details->avx_bytes[0]); |
| 2700 |
prefixes_details->vvvv = FCML_VEX_VVVV( |
| 2701 |
prefix_details->avx_bytes[1]); |
| 2702 |
break; |
| 2703 |
case 0xC5: |
| 2704 |
prefixes_details->R = FCML_VEX_R( |
| 2705 |
prefix_details->avx_bytes[0]); |
| 2706 |
prefixes_details->L = FCML_VEX_L( |
| 2707 |
prefix_details->avx_bytes[0]); |
| 2708 |
prefixes_details->vvvv = FCML_VEX_VVVV( |
| 2709 |
prefix_details->avx_bytes[0]); |
| 2710 |
prefixes_details->pp = FCML_VEX_PP( |
| 2711 |
prefix_details->avx_bytes[0]); |
| 2712 |
break; |
| 2713 |
} |
| 2714 |
|
| 2715 |
if (op_mode == FCML_OM_32_BIT && |
| 2716 |
prefixes_details->vvvv > 7) { |
| 2717 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2718 |
} |
| 2719 |
|
| 2720 |
prefixes_details->avx_first_byte = prefix; |
| 2721 |
|
| 2722 |
prefix_size += xop_vex_prefix_size; |
| 2723 |
} |
| 2724 |
|
| 2725 |
fcml_fn_stream_restore_point(stream, sp); |
| 2726 |
} |
| 2727 |
|
| 2728 |
} else { |
| 2729 |
/* If 0xC5 and 0xC4 can not be treated as a VEX prefix, |
| 2730 |
* and 0x62 cannot be treated as EVEX prefix, |
| 2731 |
* it should be treated just as an instruction opcode. |
| 2732 |
*/ |
| 2733 |
prefix_type = FCML_PT_GROUP_UNKNOWN; |
| 2734 |
} |
| 2735 |
} |
| 2736 |
|
| 2737 |
if (prefix_type != FCML_PT_GROUP_UNKNOWN) { |
| 2738 |
prefix_details->prefix = prefix; |
| 2739 |
prefix_details->prefix_type = prefix_type; |
| 2740 |
prefix_details->mandatory_prefix = (p_flags |
| 2741 |
& FCML_IDFPF_IS_MANDATORY_CANDIDATE) |
| 2742 |
? FCML_TRUE : FCML_FALSE; |
| 2743 |
if (p_flags & FCML_IDFPF_IS_EVEX) { |
| 2744 |
prefixes_details->is_evex = FCML_TRUE; |
| 2745 |
} |
| 2746 |
if (p_flags & FCML_IDFPF_IS_VEX) { |
| 2747 |
prefixes_details->is_vex = FCML_TRUE; |
| 2748 |
} |
| 2749 |
if (p_flags & FCML_IDFPF_IS_XOP) { |
| 2750 |
prefixes_details->is_xop = FCML_TRUE; |
| 2751 |
} |
| 2752 |
if (p_flags & FCML_IDFPF_IS_LOCK) { |
| 2753 |
prefixes_details->is_lock = |
| 2754 |
(p_flags & FCML_IDFPF_IS_LOCK) |
| 2755 |
? FCML_TRUE : FCML_FALSE; |
| 2756 |
} |
| 2757 |
if (p_flags & FCML_IDFPF_IS_REP_XRELEASE) { |
| 2758 |
prefixes_details->is_rep = |
| 2759 |
(p_flags & FCML_IDFPF_IS_REP_XRELEASE) |
| 2760 |
? FCML_TRUE : FCML_FALSE; |
| 2761 |
} |
| 2762 |
if (p_flags & FCML_IDFPF_IS_REPNE_XACQUIRE) { |
| 2763 |
prefixes_details->is_repne = |
| 2764 |
(p_flags & FCML_IDFPF_IS_REPNE_XACQUIRE) |
| 2765 |
? FCML_TRUE : FCML_FALSE; |
| 2766 |
} |
| 2767 |
if (p_flags & FCML_IDFPF_IS_BRANCH) { |
| 2768 |
prefixes_details->is_branch = |
| 2769 |
(p_flags & FCML_IDFPF_IS_BRANCH) |
| 2770 |
? FCML_TRUE : FCML_FALSE; |
| 2771 |
} |
| 2772 |
if (p_flags & FCML_IDFPF_IS_NOBRANCH) { |
| 2773 |
prefixes_details->is_nobranch = |
| 2774 |
(p_flags & FCML_IDFPF_IS_NOBRANCH) |
| 2775 |
? FCML_TRUE : FCML_FALSE; |
| 2776 |
} |
| 2777 |
prefixes_details->is_avx = prefixes_details->is_vex |
| 2778 |
|| prefixes_details->is_evex |
| 2779 |
|| prefixes_details->is_xop; |
| 2780 |
prefixes_details->prefixes_bytes_count += prefix_size; |
| 2781 |
fcml_fn_stream_seek(stream, prefix_size, FCML_EN_ST_CURRENT); |
| 2782 |
prefix_index++; |
| 2783 |
} else { |
| 2784 |
is_last_prefix = FCML_FALSE; |
| 2785 |
} |
| 2786 |
|
| 2787 |
} |
| 2788 |
/* Break loop if REX or VEX prefix is already found.*/ |
| 2789 |
} while (prefix_type != FCML_PT_GROUP_UNKNOWN && !is_last_prefix); |
| 2790 |
|
| 2791 |
prefixes_details->prefixes_count = prefix_index; |
| 2792 |
|
| 2793 |
/* Check if prefixes marked as mandatory are really a mandatory ones.*/ |
| 2794 |
/* VEX and XOP prefixes don't allow any mandatory prefixes, so following*/ |
| 2795 |
/* code has nothing to do if there is VEX or XOP.*/ |
| 2796 |
if (prefix_index > 0 && prefix_type != FCML_PT_VEX && |
| 2797 |
prefix_type != FCML_PT_XOP && prefix_type != FCML_PT_EVEX) { |
| 2798 |
fcml_bool found_plain_prefix = FCML_FALSE; |
| 2799 |
fcml_int i; |
| 2800 |
for (i = prefix_index; i > 0; i--) { |
| 2801 |
if (found_plain_prefix) { |
| 2802 |
prefixes->prefixes[i - 1].mandatory_prefix = FCML_FALSE; |
| 2803 |
} else { |
| 2804 |
/* REX prefixes have to be preceded by mandatory and optional |
| 2805 |
* prefixes if there are any. |
| 2806 |
*/ |
| 2807 |
fcml_st_instruction_prefix *prefix = |
| 2808 |
&(prefixes->prefixes[i - 1]); |
| 2809 |
if (!prefix->mandatory_prefix && |
| 2810 |
prefix->prefix_type != FCML_PT_REX) { |
| 2811 |
found_plain_prefix = FCML_TRUE; |
| 2812 |
} |
| 2813 |
} |
| 2814 |
} |
| 2815 |
} |
| 2816 |
|
| 2817 |
return error; |
| 2818 |
} |
| 2819 |
|
| 2820 |
static fcml_prefixes convert_prefixes_to_generic_prefixes( |
| 2821 |
fcml_st_prefixes_details *prefixes) { |
| 2822 |
fcml_prefixes gen_prefixes = 0; |
| 2823 |
if (prefixes) { |
| 2824 |
if (prefixes->is_rep) { |
| 2825 |
gen_prefixes |= FCML_PREFIX_REP; |
| 2826 |
} |
| 2827 |
if (prefixes->is_repne) { |
| 2828 |
gen_prefixes |= FCML_PREFIX_REPNE; |
| 2829 |
} |
| 2830 |
if (prefixes->is_lock) { |
| 2831 |
gen_prefixes |= FCML_PREFIX_LOCK; |
| 2832 |
} |
| 2833 |
if (prefixes->is_xacquire) { |
| 2834 |
gen_prefixes |= FCML_PREFIX_XACQUIRE; |
| 2835 |
} |
| 2836 |
if (prefixes->is_xrelease) { |
| 2837 |
gen_prefixes |= FCML_PREFIX_XRELEASE; |
| 2838 |
} |
| 2839 |
if (prefixes->is_branch) { |
| 2840 |
gen_prefixes |= FCML_PREFIX_BRANCH_HINT; |
| 2841 |
} |
| 2842 |
if (prefixes->is_nobranch) { |
| 2843 |
gen_prefixes |= FCML_PREFIX_NOBRANCH_HINT; |
| 2844 |
} |
| 2845 |
} |
| 2846 |
return gen_prefixes; |
| 2847 |
} |
| 2848 |
|
| 2849 |
static void clean_operands_for_short_forms(fcml_st_instruction *instruction, |
| 2850 |
fcml_st_instruction_details *instruction_details) { |
| 2851 |
if (instruction_details->is_shortcut) { |
| 2852 |
/* Clean all operands, short forms do not have them.*/ |
| 2853 |
instruction->operands_count = 0; |
| 2854 |
fcml_fn_env_memory_clear(&(instruction->operands[0]), |
| 2855 |
sizeof(fcml_st_operand[FCML_OPERANDS_COUNT])); |
| 2856 |
fcml_fn_env_memory_clear(&(instruction_details->operand_details[0]), |
| 2857 |
sizeof(fcml_st_operand_details[FCML_OPERANDS_COUNT])); |
| 2858 |
} else if (instruction_details->is_pseudo_op) { |
| 2859 |
for (int i = 0; i < FCML_OPERANDS_COUNT; i++) { |
| 2860 |
/* Clean IMM pseudo opcode operand.*/ |
| 2861 |
if (instruction->operands[i].hints & FCML_OP_HINT_PSEUDO_OPCODE) { |
| 2862 |
fcml_fn_env_memory_clear(&(instruction->operands[i]), |
| 2863 |
sizeof(fcml_st_operand)); |
| 2864 |
fcml_fn_env_memory_clear( |
| 2865 |
&(instruction_details->operand_details[i]), |
| 2866 |
sizeof(fcml_st_operand_details)); |
| 2867 |
instruction->operands_count--; |
| 2868 |
/* It is always the last one.*/ |
| 2869 |
break; |
| 2870 |
} |
| 2871 |
} |
| 2872 |
} |
| 2873 |
} |
| 2874 |
|
| 2875 |
static fcml_ceh_error disassemble_core(fcml_st_disassembler_context *context, |
| 2876 |
fcml_st_disassembler_result *result) { |
| 2877 |
|
| 2878 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 2879 |
disassembler *int_disasm = (disassembler *)context->disassembler; |
| 2880 |
|
| 2881 |
error = fcml_fn_utils_prepare_entry_point(&(context->entry_point)); |
| 2882 |
if (error) { |
| 2883 |
return error; |
| 2884 |
} |
| 2885 |
|
| 2886 |
/* Prepare stream.*/ |
| 2887 |
fcml_st_memory_stream stream; |
| 2888 |
stream.base_address = context->code; |
| 2889 |
stream.size = context->code_length; |
| 2890 |
stream.offset = 0; |
| 2891 |
|
| 2892 |
/* Prepare disassemble context.*/ |
| 2893 |
decoding_context decoding_context = { 0 }; |
| 2894 |
decoding_context.disassembler_context = context; |
| 2895 |
decoding_context.effective_address_size_attribute = |
| 2896 |
context->entry_point.address_size_attribute; |
| 2897 |
decoding_context.effective_operand_size_attribute = |
| 2898 |
context->entry_point.operand_size_attribute; |
| 2899 |
|
| 2900 |
decoding_context.stream = &stream; |
| 2901 |
|
| 2902 |
error = decode_prefixes(&decoding_context); |
| 2903 |
if (error) { |
| 2904 |
return error; |
| 2905 |
} |
| 2906 |
|
| 2907 |
error = decode_instruction(&decoding_context); |
| 2908 |
|
| 2909 |
if (!error) { |
| 2910 |
|
| 2911 |
/* Copy potential errors.*/ |
| 2912 |
result->errors = decoding_context.errors; |
| 2913 |
|
| 2914 |
fcml_st_instruction *inst = &(result->instruction); |
| 2915 |
fcml_st_instruction_details *inst_details = |
| 2916 |
&(result->instruction_details); |
| 2917 |
|
| 2918 |
fcml_usize memory_data_size = 0; |
| 2919 |
|
| 2920 |
/* Prepare operands.*/ |
| 2921 |
fcml_int i; |
| 2922 |
for (i = 0; i < FCML_OPERANDS_COUNT; i++) { |
| 2923 |
operand_wrapper *operand_wrapper = |
| 2924 |
&(decoding_context.operand_wrappers[i]); |
| 2925 |
if (operand_wrapper->operand.type != FCML_OT_NONE) { |
| 2926 |
if (operand_wrapper->operand.type == FCML_OT_ADDRESS) { |
| 2927 |
memory_data_size = |
| 2928 |
operand_wrapper->operand.address.size_operator; |
| 2929 |
} |
| 2930 |
inst->operands[i] = operand_wrapper->operand; |
| 2931 |
inst_details->operand_details[i].access_mode = |
| 2932 |
operand_wrapper->access_mode; |
| 2933 |
} else { |
| 2934 |
break; |
| 2935 |
} |
| 2936 |
} |
| 2937 |
|
| 2938 |
inst->operands_count = i; |
| 2939 |
|
| 2940 |
/* ModR/M details.*/ |
| 2941 |
fcml_st_decoded_modrm_details *modrm_details = |
| 2942 |
&(inst_details->modrm_details); |
| 2943 |
|
| 2944 |
modrm_details->modrm = decoding_context.decoded_modrm_details.modrm; |
| 2945 |
modrm_details->sib = decoding_context.decoded_modrm_details.sib; |
| 2946 |
modrm_details->is_rip = decoding_context.decoded_modrm.is_rip; |
| 2947 |
modrm_details->is_modrm = decoding_context.is_modrm; |
| 2948 |
|
| 2949 |
fcml_st_modrm_displacement *disp = |
| 2950 |
&(decoding_context.decoded_modrm_details.displacement); |
| 2951 |
|
| 2952 |
modrm_details->displacement.displacement = disp->displacement; |
| 2953 |
modrm_details->displacement.N = disp->N; |
| 2954 |
|
| 2955 |
/* Prefixes.*/ |
| 2956 |
inst_details->prefixes_details = decoding_context.prefixes; |
| 2957 |
inst->prefixes = convert_prefixes_to_generic_prefixes( |
| 2958 |
&(decoding_context.prefixes)); |
| 2959 |
|
| 2960 |
/* Copy instruction hints.*/ |
| 2961 |
inst->hints = decoding_context.instruction_hints; |
| 2962 |
|
| 2963 |
/* Instruction code.*/ |
| 2964 |
inst_details->instruction_size = |
| 2965 |
decoding_context.calculated_instruction_size; |
| 2966 |
fcml_fn_env_memory_copy(&inst_details->instruction_code, |
| 2967 |
context->code, |
| 2968 |
inst_details->instruction_size > FCML_INSTRUCTION_SIZE ? |
| 2969 |
FCML_INSTRUCTION_SIZE : inst_details->instruction_size); |
| 2970 |
|
| 2971 |
/* Conditions.*/ |
| 2972 |
if (decoding_context.is_conditional) { |
| 2973 |
inst->is_conditional = FCML_TRUE; |
| 2974 |
inst->condition = decoding_context.condition; |
| 2975 |
} else { |
| 2976 |
inst->is_conditional = FCML_FALSE; |
| 2977 |
} |
| 2978 |
|
| 2979 |
/* Opcode fields.*/ |
| 2980 |
inst_details->opcode_field_s_bit = |
| 2981 |
decoding_context.opcode_field_s_bit; |
| 2982 |
inst_details->opcode_field_w_bit = |
| 2983 |
decoding_context.opcode_field_w_bit; |
| 2984 |
|
| 2985 |
/* Instruction code. */ |
| 2986 |
inst_details->instruction = decoding_context.instruction; |
| 2987 |
inst_details->addr_mode = decoding_context.addr_mode; |
| 2988 |
inst_details->instruction_group = |
| 2989 |
decoding_context.instruction_group; |
| 2990 |
|
| 2991 |
/* L'L flag for mnemonic chooser.*/ |
| 2992 |
fcml_nuint8_t l; |
| 2993 |
l.is_not_null = FCML_FALSE; |
| 2994 |
if (inst_details->prefixes_details.is_avx) { |
| 2995 |
l.is_not_null = FCML_TRUE; |
| 2996 |
l.value = inst_details->prefixes_details.L_prim << 1 | |
| 2997 |
inst_details->prefixes_details.L; |
| 2998 |
} |
| 2999 |
|
| 3000 |
fcml_st_mp_mnemonic *mnemonic = NULL; |
| 3001 |
|
| 3002 |
if (decoding_context.pseudo_op) { |
| 3003 |
|
| 3004 |
/* Gets mnemonic for pseudo operation. */ |
| 3005 |
inst->mnemonic = int_disasm->dialect_context-> |
| 3006 |
get_pseudo_op_mnemonic(decoding_context.pseudo_op); |
| 3007 |
inst_details->pseudo_op = decoding_context.pseudo_op; |
| 3008 |
|
| 3009 |
} else { |
| 3010 |
|
| 3011 |
/* Mnemonic for real assembler instruction. */ |
| 3012 |
fcml_bool shortform = decoding_context.disassembler_context-> |
| 3013 |
configuration.short_forms; |
| 3014 |
|
| 3015 |
fcml_st_mp_config mp_config; |
| 3016 |
mp_config.effective_asa = |
| 3017 |
decoding_context.effective_address_size_attribute; |
| 3018 |
mp_config.effective_osa = |
| 3019 |
decoding_context.effective_operand_size_attribute; |
| 3020 |
mp_config.is_memory = |
| 3021 |
(decoding_context.decoded_modrm.address.address_form != |
| 3022 |
FCML_AF_UNDEFINED && |
| 3023 |
!decoding_context.decoded_modrm.reg.is_not_null); |
| 3024 |
mp_config.memory_data_size = memory_data_size; |
| 3025 |
mp_config.use_shortcut = shortform; |
| 3026 |
mp_config.pseudo_opcode = decoding_context.pseudo_opcode; |
| 3027 |
mp_config.suffix = decoding_context.suffix; |
| 3028 |
mp_config.l = l; |
| 3029 |
|
| 3030 |
mnemonic = fcml_fn_mp_choose_mnemonic(decoding_context.mnemonics, |
| 3031 |
&mp_config); |
| 3032 |
if (mnemonic) { |
| 3033 |
inst_details->is_pseudo_op = mnemonic->pseudo_op.is_not_null; |
| 3034 |
inst_details->is_shortcut = mnemonic->is_shortcut && shortform; |
| 3035 |
/* Render mnemonic using provided dialect. */ |
| 3036 |
inst->mnemonic = |
| 3037 |
int_disasm->dialect_context->render_mnemonic( |
| 3038 |
mnemonic->mnemonic, decoding_context.is_conditional |
| 3039 |
? &(decoding_context.condition) : NULL, |
| 3040 |
context->configuration.conditional_group, |
| 3041 |
context->configuration. |
| 3042 |
carry_flag_conditional_suffix); |
| 3043 |
} else { |
| 3044 |
/* Mnemonic not found.*/ |
| 3045 |
FCML_TRACE_MSG("Can not choose mnemonic for disassembled \ |
| 3046 |
instruction."); |
| 3047 |
return FCML_CEH_GEC_INTERNAL_ERROR; |
| 3048 |
} |
| 3049 |
|
| 3050 |
} |
| 3051 |
|
| 3052 |
/* avx-512 */ |
| 3053 |
inst_details->tuple_type = decoding_context.tuple_type; |
| 3054 |
|
| 3055 |
/* Clean operands for short forms.*/ |
| 3056 |
clean_operands_for_short_forms(inst, inst_details); |
| 3057 |
|
| 3058 |
/* Increment IP */ |
| 3059 |
if (context->configuration.increment_ip) { |
| 3060 |
fcml_usize size = result->instruction_details.instruction_size; |
| 3061 |
context->entry_point.ip += size; |
| 3062 |
// Skip to the next instruction. |
| 3063 |
fcml_uint8_t *instruction_code = (fcml_uint8_t *)context->code; |
| 3064 |
instruction_code += size; |
| 3065 |
context->code = instruction_code; |
| 3066 |
context->code_length -= (context->code_length >= size) |
| 3067 |
? size : context->code_length; |
| 3068 |
} |
| 3069 |
|
| 3070 |
/* Execute disassembler post processor if needed.*/ |
| 3071 |
fcml_fnp_disasm_postprocessor post_processor |
| 3072 |
= int_disasm->dialect_context->disasm_postprocessor; |
| 3073 |
if (post_processor) { |
| 3074 |
post_processor(mnemonic, result); |
| 3075 |
} |
| 3076 |
|
| 3077 |
} else if (decoding_context.errors.errors) { |
| 3078 |
/* If there are any errors, return them back to the user.*/ |
| 3079 |
result->errors = decoding_context.errors; |
| 3080 |
} |
| 3081 |
|
| 3082 |
return error; |
| 3083 |
} |
| 3084 |
|
| 3085 |
/**************************** |
| 3086 |
* API. |
| 3087 |
****************************/ |
| 3088 |
|
| 3089 |
fcml_ceh_error LIB_CALL fcml_fn_disassembler_init( |
| 3090 |
const fcml_st_dialect *dialect, |
| 3091 |
fcml_st_disassembler **disasm) { |
| 3092 |
|
| 3093 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR; |
| 3094 |
|
| 3095 |
FCML_ENV_ALLOC_CLEAR(int_disasm, disassembler); |
| 3096 |
if (int_disasm) { |
| 3097 |
|
| 3098 |
int_disasm->dialect_context = (fcml_st_dialect_context_int*) dialect; |
| 3099 |
error = fcml_fn_dt_dts_tree_init( |
| 3100 |
(fcml_st_dialect_context_int*)dialect, |
| 3101 |
&(int_disasm->decoding_tree), |
| 3102 |
&prepare_inst_def_callback_default, |
| 3103 |
&dispose_inst_def_callback_default); |
| 3104 |
if (!error) { |
| 3105 |
*disasm = (fcml_st_disassembler*) int_disasm; |
| 3106 |
} |
| 3107 |
|
| 3108 |
} else { |
| 3109 |
error = FCML_CEH_GEC_OUT_OF_MEMORY; |
| 3110 |
} |
| 3111 |
|
| 3112 |
return error; |
| 3113 |
} |
| 3114 |
|
| 3115 |
void LIB_CALL fcml_fn_disassembler_result_prepare( |
| 3116 |
fcml_st_disassembler_result *result) { |
| 3117 |
if (result) { |
| 3118 |
/* Clean result container. */ |
| 3119 |
fcml_fn_env_memory_clear(result, sizeof(fcml_st_disassembler_result)); |
| 3120 |
} |
| 3121 |
} |
| 3122 |
|
| 3123 |
|
| 3124 |
fcml_ceh_error LIB_CALL fcml_fn_disassemble( |
| 3125 |
fcml_st_disassembler_context *context, |
| 3126 |
fcml_st_disassembler_result *result) { |
| 3127 |
|
| 3128 |
// Sanity check. |
| 3129 |
if (!context || !result) { |
| 3130 |
return FCML_CEH_GEC_INVALID_INPUT; |
| 3131 |
} |
| 3132 |
|
| 3133 |
if(!context->code_length) { |
| 3134 |
return FCML_CEH_GEC_EOF; |
| 3135 |
} |
| 3136 |
|
| 3137 |
/* Check if there is something already available in result, and free |
| 3138 |
* it in such cache. |
| 3139 |
*/ |
| 3140 |
fcml_fn_disassembler_result_free(result); |
| 3141 |
|
| 3142 |
fcml_ceh_error error = disassemble_core(context, result); |
| 3143 |
if (error) { |
| 3144 |
// Try to convert error code to error message if there is such need. |
| 3145 |
fcml_fn_utils_conv_gec_to_error_info( |
| 3146 |
context->configuration.enable_error_messages, |
| 3147 |
&(result->errors), error); |
| 3148 |
} |
| 3149 |
|
| 3150 |
return error; |
| 3151 |
} |
| 3152 |
|
| 3153 |
void LIB_CALL fcml_fn_disassembler_result_free( |
| 3154 |
fcml_st_disassembler_result *result) { |
| 3155 |
|
| 3156 |
if (result) { |
| 3157 |
/* Free all error messages. */ |
| 3158 |
fcml_fn_ceh_free_errors_only(&(result->errors)); |
| 3159 |
|
| 3160 |
/* Mnemonic is allocated by dialect and has to be also free'd. */ |
| 3161 |
if (result->instruction.mnemonic) { |
| 3162 |
fcml_fn_env_str_strfree(result->instruction.mnemonic); |
| 3163 |
} |
| 3164 |
|
| 3165 |
/* Clean all information inside result. */ |
| 3166 |
fcml_fn_disassembler_result_prepare(result); |
| 3167 |
} |
| 3168 |
} |
| 3169 |
|
| 3170 |
void LIB_CALL fcml_fn_disassembler_free(fcml_st_disassembler *disasm) { |
| 3171 |
disassembler *dec_disasm = (disassembler*)disasm; |
| 3172 |
if (dec_disasm) { |
| 3173 |
if (dec_disasm->decoding_tree) { |
| 3174 |
fcml_fn_dt_dts_tree_free(dec_disasm->dialect_context, |
| 3175 |
dec_disasm->decoding_tree); |
| 3176 |
} |
| 3177 |
fcml_fn_env_memory_free(dec_disasm); |
| 3178 |
} |
| 3179 |
} |