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>
23 #ifndef V3_CONFIG_DEBUG_DECODER
25 #define PrintDebug(fmt, args...)
29 #define MASK(val, length) ({ \
30 uint64_t mask = 0x0LL; \
33 mask = 0x00000000000000ffLL; \
36 mask = 0x000000000000ffffLL; \
39 mask = 0x00000000ffffffffLL; \
42 mask = 0xffffffffffffffffLL; \
48 static v3_op_type_t op_form_to_type(op_form_t form);
49 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form);
52 int v3_disasm(struct guest_info * info, void *instr_ptr, addr_t * rip, int mark) {
57 int v3_init_decoder(struct guest_info * core) {
62 int v3_deinit_decoder(struct guest_info * core) {
67 int v3_encode(struct guest_info * info, struct x86_instr * instr, uint8_t * instr_buf) {
72 int v3_decode(struct guest_info * core, addr_t instr_ptr, struct x86_instr * instr) {
73 op_form_t form = INVALID_INSTR;
78 PrintDebug(core->vm_info, core, "Decoding Instruction at %p\n", (void *)instr_ptr);
80 memset(instr, 0, sizeof(struct x86_instr));
83 length = v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
87 if (v3_get_vm_cpu_mode(core) == LONG) {
88 uint8_t prefix = *(uint8_t *)(instr_ptr + length);
90 if ((prefix & 0xf0) == 0x40) {
91 instr->prefixes.rex = 1;
93 instr->prefixes.rex_rm = (prefix & 0x01);
94 instr->prefixes.rex_sib_idx = ((prefix & 0x02) >> 1);
95 instr->prefixes.rex_reg = ((prefix & 0x04) >> 2);
96 instr->prefixes.rex_op_size = ((prefix & 0x08) >> 3);
103 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
105 PrintDebug(core->vm_info, core, "\t decoded as (%s)\n", op_form_to_str(form));
107 if (form == INVALID_INSTR) {
108 PrintError(core->vm_info, core, "Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
112 instr->op_type = op_form_to_type(form);
114 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
117 PrintError(core->vm_info, core, "Could not parse instruction operands\n");
122 instr->instr_length += length;
124 #ifdef V3_CONFIG_DEBUG_DECODER
125 V3_Print(core->vm_info, core, "Decoding Instr at %p\n", (void *)core->rip);
126 v3_print_instr(instr);
127 V3_Print(core->vm_info, core, "CS DB FLag=%x\n", core->segments.cs.db);
134 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
135 struct x86_instr * instr, op_form_t form) {
136 // get operational mode of the guest for operand width
137 uint8_t operand_width = get_operand_width(core, instr, form);
138 uint8_t addr_width = get_addr_width(core, instr);
140 uint8_t * instr_start = instr_ptr;
143 PrintDebug(core->vm_info, core, "\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
160 uint8_t reg_code = 0;
162 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
165 PrintError(core->vm_info, core, "Error decoding operand\n");
171 instr->src_operand.type = IMM_OPERAND;
172 instr->src_operand.size = operand_width;
175 if (operand_width == 1) {
176 instr->src_operand.operand = *(uint8_t *)instr_ptr;
177 } else if (operand_width == 2) {
178 instr->src_operand.operand = *(uint16_t *)instr_ptr;
179 } else if (operand_width == 4) {
180 instr->src_operand.operand = *(uint32_t *)instr_ptr;
181 } else if (operand_width == 8) {
182 instr->src_operand.operand = *(sint32_t *)instr_ptr; // This is a special case for sign extended 64bit ops
184 PrintError(core->vm_info, core, "Illegal operand width (%d)\n", operand_width);
188 instr->src_operand.read = 1;
189 instr->dst_operand.write = 1;
191 instr_ptr += operand_width;
193 instr->num_operands = 2;
211 uint8_t reg_code = 0;
213 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
216 PrintError(core->vm_info, core, "Error decoding operand\n");
222 instr->src_operand.type = REG_OPERAND;
223 instr->src_operand.size = operand_width;
225 instr->src_operand.read = 1;
226 instr->dst_operand.write = 1;
229 decode_gpr(core, reg_code, &(instr->src_operand));
231 instr->num_operands = 2;
248 uint8_t reg_code = 0;
250 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
253 PrintError(core->vm_info, core, "Error decoding operand\n");
259 instr->dst_operand.size = operand_width;
260 instr->dst_operand.type = REG_OPERAND;
261 decode_gpr(core, reg_code, &(instr->dst_operand));
263 instr->src_operand.read = 1;
264 instr->dst_operand.write = 1;
266 instr->num_operands = 2;
272 uint8_t reg_code = 0;
274 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
275 instr->src_operand.size = 1;
278 PrintError(core->vm_info, core, "Error decoding operand\n");
284 instr->dst_operand.size = operand_width;
285 instr->dst_operand.type = REG_OPERAND;
286 decode_gpr(core, reg_code, &(instr->dst_operand));
288 instr->src_operand.read = 1;
289 instr->dst_operand.write = 1;
291 instr->num_operands = 2;
297 uint8_t reg_code = 0;
299 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
300 instr->src_operand.size = 2;
303 PrintError(core->vm_info, core, "Error decoding operand\n");
309 instr->dst_operand.size = operand_width;
310 instr->dst_operand.type = REG_OPERAND;
311 decode_gpr(core, reg_code, &(instr->dst_operand));
313 instr->src_operand.read = 1;
314 instr->dst_operand.write = 1;
316 instr->num_operands = 2;
326 uint8_t reg_code = 0;
328 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
331 PrintError(core->vm_info, core, "Error decoding operand\n");
337 instr->src_operand.type = IMM_OPERAND;
338 instr->src_operand.size = operand_width;
339 instr->src_operand.operand = (addr_t)MASK((sint64_t)*(sint8_t *)instr_ptr, operand_width); // sign extend.
341 instr->src_operand.read = 1;
342 instr->dst_operand.write = 1;
346 instr->num_operands = 2;
352 instr->is_str_op = 1;
354 if (instr->prefixes.rep == 1) {
355 instr->str_op_length = MASK(core->vm_regs.rcx, addr_width);
357 instr->str_op_length = 1;
361 // Destination: ES:(E)DI
363 instr->src_operand.type = MEM_OPERAND;
364 instr->src_operand.size = operand_width;
365 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
368 instr->dst_operand.type = MEM_OPERAND;
369 instr->dst_operand.size = operand_width;
370 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
373 instr->src_operand.read = 1;
374 instr->dst_operand.write = 1;
376 instr->num_operands = 2;
381 uint8_t reg_code = 0;
383 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
387 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
393 instr->dst_operand.type = REG_OPERAND;
394 instr->dst_operand.size = operand_width;
395 decode_cr(core, reg_code, &(instr->dst_operand));
397 instr->src_operand.read = 1;
398 instr->dst_operand.write = 1;
400 instr->num_operands = 2;
404 uint8_t reg_code = 0;
406 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
410 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
416 instr->src_operand.type = REG_OPERAND;
417 instr->src_operand.size = operand_width;
418 decode_cr(core, reg_code, &(instr->src_operand));
420 instr->src_operand.read = 1;
421 instr->dst_operand.write = 1;
423 instr->num_operands = 2;
428 instr->is_str_op = 1;
430 if (instr->prefixes.rep == 1) {
431 instr->str_op_length = MASK(core->vm_regs.rcx, addr_width);
433 instr->str_op_length = 1;
436 instr->src_operand.size = operand_width;
437 instr->src_operand.type = REG_OPERAND;
438 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
440 instr->dst_operand.type = MEM_OPERAND;
441 instr->dst_operand.size = operand_width;
442 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
444 instr->src_operand.read = 1;
445 instr->dst_operand.write = 1;
447 instr->num_operands = 2;
452 instr->dst_operand.type = IMM_OPERAND;
453 instr->dst_operand.size = operand_width;
454 instr->dst_operand.operand = *(uint8_t *)instr_ptr;
455 instr_ptr += operand_width;
456 instr->num_operands = 1;
461 uint8_t reg_code = 0;
463 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
466 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
472 instr->num_operands = 1;
477 uint8_t reg_code = 0;
479 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
482 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
488 instr->dst_operand.read = 1;
490 instr->num_operands = 1;
498 PrintError(core->vm_info, core, "Invalid Instruction form: %s\n", op_form_to_str(form));
502 return (instr_ptr - instr_start);
506 static v3_op_type_t op_form_to_type(op_form_t form) {
671 return V3_INVALID_OP;