2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_decoder.h>
21 #include <palacios/vmm_instr_decoder.h>
24 #define MASK(val, length) ({ \
25 ullong_t mask = 0x0LL; \
28 mask = 0x00000000000000ffLL; \
31 mask = 0x000000000000ffffLL; \
34 mask = 0x00000000ffffffffLL; \
37 mask = 0xffffffffffffffffLL; \
43 static v3_op_type_t op_form_to_type(op_form_t form);
44 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form);
47 int v3_disasm(struct guest_info * info, void *instr_ptr, addr_t * rip, int mark) {
52 int v3_init_decoder(struct guest_info * core) {
57 int v3_deinit_decoder(struct guest_info * core) {
62 int v3_encode(struct guest_info * info, struct x86_instr * instr, uint8_t * instr_buf) {
67 int v3_decode(struct guest_info * core, addr_t instr_ptr, struct x86_instr * instr) {
68 op_form_t form = INVALID_INSTR;
73 V3_Print("Decoding Instruction at %p\n", (void *)instr_ptr);
75 memset(instr, 0, sizeof(struct x86_instr));
78 length = v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
82 if (v3_get_vm_cpu_mode(core) == LONG) {
83 uint8_t prefix = *(uint8_t *)(instr_ptr + length);
85 if ((prefix & 0xf0) == 0x40) {
86 instr->prefixes.rex = 1;
88 instr->prefixes.rex_rm = (prefix & 0x01);
89 instr->prefixes.rex_sib_idx = ((prefix & 0x02) >> 1);
90 instr->prefixes.rex_reg = ((prefix & 0x04) >> 2);
91 instr->prefixes.rex_op_size = ((prefix & 0x08) >> 3);
98 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
101 V3_Print("\t decoded as (%s)\n", op_form_to_str(form));
103 if (form == INVALID_INSTR) {
104 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
108 instr->op_type = op_form_to_type(form);
110 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
113 PrintError("Could not parse instruction operands\n");
119 instr->instr_length += length;
122 v3_print_instr(instr);
128 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
129 struct x86_instr * instr, op_form_t form) {
130 // get operational mode of the guest for operand width
131 uint8_t operand_width = get_operand_width(core, instr, form);
132 uint8_t addr_width = get_addr_width(core, instr);
134 uint8_t * instr_start = instr_ptr;
137 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
154 uint8_t reg_code = 0;
156 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
159 PrintError("Error decoding operand\n");
165 instr->src_operand.type = IMM_OPERAND;
166 instr->src_operand.size = operand_width;
168 if (operand_width == 1) {
169 instr->src_operand.operand = *(uint8_t *)instr_ptr;
170 } else if (operand_width == 2) {
171 instr->src_operand.operand = *(uint16_t *)instr_ptr;
172 } else if (operand_width == 4) {
173 instr->src_operand.operand = *(uint32_t *)instr_ptr;
175 PrintError("Illegal operand width (%d)\n", operand_width);
179 instr_ptr += operand_width;
181 instr->num_operands = 2;
199 uint8_t reg_code = 0;
201 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
204 PrintError("Error decoding operand\n");
210 instr->src_operand.type = REG_OPERAND;
211 instr->src_operand.size = operand_width;
213 decode_gpr(core, reg_code, &(instr->src_operand));
215 instr->num_operands = 2;
232 uint8_t reg_code = 0;
234 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
237 PrintError("Error decoding operand\n");
243 instr->dst_operand.size = operand_width;
244 instr->dst_operand.type = REG_OPERAND;
245 decode_gpr(core, reg_code, &(instr->dst_operand));
247 instr->num_operands = 2;
257 uint8_t reg_code = 0;
259 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
262 PrintError("Error decoding operand\n");
268 instr->src_operand.type = IMM_OPERAND;
269 instr->src_operand.size = operand_width;
270 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
274 instr->num_operands = 2;
280 instr->is_str_op = 1;
282 if (instr->prefixes.rep == 1) {
283 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
285 instr->str_op_length = 1;
289 // Destination: ES:(E)DI
291 instr->src_operand.type = MEM_OPERAND;
292 instr->src_operand.size = operand_width;
293 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
295 instr->dst_operand.type = MEM_OPERAND;
296 instr->dst_operand.size = operand_width;
297 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
299 instr->num_operands = 2;
304 uint8_t reg_code = 0;
306 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
310 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
316 instr->dst_operand.type = REG_OPERAND;
317 instr->dst_operand.size = operand_width;
318 decode_cr(core, reg_code, &(instr->dst_operand));
320 instr->num_operands = 2;
324 uint8_t reg_code = 0;
326 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
330 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
336 instr->src_operand.type = REG_OPERAND;
337 instr->src_operand.size = operand_width;
338 decode_cr(core, reg_code, &(instr->src_operand));
340 instr->num_operands = 2;
345 instr->is_str_op = 1;
347 if (instr->prefixes.rep == 1) {
348 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
350 instr->str_op_length = 1;
353 instr->src_operand.size = operand_width;
354 instr->src_operand.type = REG_OPERAND;
355 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
357 instr->dst_operand.type = MEM_OPERAND;
358 instr->dst_operand.size = operand_width;
359 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
361 instr->num_operands = 2;
366 uint8_t reg_code = 0;
368 // We use the dst operand here to maintain bug-for-bug compatibility with XED
370 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
373 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
379 instr->num_operands = 1;
389 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
393 return (instr_ptr - instr_start);
397 static v3_op_type_t op_form_to_type(op_form_t form) {
561 return V3_INVALID_OP;