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;
273 /* Use AX for destination operand */
274 instr->dst_operand.size = operand_width;
275 instr->dst_operand.type = REG_OPERAND;
276 instr->dst_operand.operand = (addr_t)&(core->vm_regs.rax);
277 instr->dst_operand.write = 1;
279 /* Get the correct offset -- (seg + offset) */
280 struct v3_segment * src_reg = get_instr_segment(core, instr);
283 if (addr_width == 2) {
284 offset = *(uint16_t *)instr_ptr;
285 } else if (addr_width == 4) {
286 offset = *(uint32_t *)instr_ptr;
287 } else if (addr_width == 8) {
288 offset = *(uint64_t *)instr_ptr;
290 PrintError(core->vm_info, core, "illegal address width for %s (width=%d)\n",
291 op_form_to_str(form), addr_width);
295 instr->src_operand.operand = ADDR_MASK(get_addr_linear(core, offset, src_reg),
296 get_addr_width(core, instr));
298 instr->src_operand.read = 1;
299 instr->src_operand.type = MEM_OPERAND;
300 instr->src_operand.size = addr_width;
302 instr_ptr += addr_width;
303 instr->num_operands = 2;
310 /* Use AX for src operand */
311 instr->src_operand.size = operand_width;
312 instr->src_operand.type = REG_OPERAND;
313 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
314 instr->src_operand.write = 1;
316 /* Get the correct offset -- (seg + offset) */
317 struct v3_segment * dst_reg = get_instr_segment(core, instr);
320 if (addr_width == 2) {
321 offset = *(uint16_t *)instr_ptr;
322 } else if (addr_width == 4) {
323 offset = *(uint32_t *)instr_ptr;
324 } else if (addr_width == 8) {
325 offset = *(uint64_t *)instr_ptr;
327 PrintError(core->vm_info, core, "illegal address width for %s (width=%d)\n",
328 op_form_to_str(form), addr_width);
332 instr->dst_operand.operand = ADDR_MASK(get_addr_linear(core, offset, dst_reg),
333 get_addr_width(core, instr));
335 instr->dst_operand.read = 1;
336 instr->dst_operand.type = MEM_OPERAND;
337 instr->dst_operand.size = addr_width;
339 instr_ptr += addr_width;
340 instr->num_operands = 2;
346 uint8_t reg_code = 0;
348 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
349 instr->src_operand.size = 1;
352 PrintError(core->vm_info, core, "Error decoding operand\n");
358 instr->dst_operand.size = operand_width;
359 instr->dst_operand.type = REG_OPERAND;
360 decode_gpr(core, reg_code, &(instr->dst_operand));
362 instr->src_operand.read = 1;
363 instr->dst_operand.write = 1;
365 instr->num_operands = 2;
371 uint8_t reg_code = 0;
373 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
374 instr->src_operand.size = 2;
377 PrintError(core->vm_info, core, "Error decoding operand\n");
383 instr->dst_operand.size = operand_width;
384 instr->dst_operand.type = REG_OPERAND;
385 decode_gpr(core, reg_code, &(instr->dst_operand));
387 instr->src_operand.read = 1;
388 instr->dst_operand.write = 1;
390 instr->num_operands = 2;
400 uint8_t reg_code = 0;
402 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
405 PrintError(core->vm_info, core, "Error decoding operand\n");
411 instr->src_operand.type = IMM_OPERAND;
412 instr->src_operand.size = operand_width;
413 instr->src_operand.operand = (addr_t)MASK((sint64_t)*(sint8_t *)instr_ptr, operand_width); // sign extend.
415 instr->src_operand.read = 1;
416 instr->dst_operand.write = 1;
420 instr->num_operands = 2;
426 instr->is_str_op = 1;
428 if (instr->prefixes.rep == 1) {
429 instr->str_op_length = MASK(core->vm_regs.rcx, addr_width);
431 instr->str_op_length = 1;
435 // Destination: ES:(E)DI
437 instr->src_operand.type = MEM_OPERAND;
438 instr->src_operand.size = operand_width;
439 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
442 instr->dst_operand.type = MEM_OPERAND;
443 instr->dst_operand.size = operand_width;
444 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
447 instr->src_operand.read = 1;
448 instr->dst_operand.write = 1;
450 instr->num_operands = 2;
455 uint8_t reg_code = 0;
457 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
461 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
467 instr->dst_operand.type = REG_OPERAND;
468 instr->dst_operand.size = operand_width;
469 decode_cr(core, reg_code, &(instr->dst_operand));
471 instr->src_operand.read = 1;
472 instr->dst_operand.write = 1;
474 instr->num_operands = 2;
478 uint8_t reg_code = 0;
480 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
484 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
490 instr->src_operand.type = REG_OPERAND;
491 instr->src_operand.size = operand_width;
492 decode_cr(core, reg_code, &(instr->src_operand));
494 instr->src_operand.read = 1;
495 instr->dst_operand.write = 1;
497 instr->num_operands = 2;
502 instr->is_str_op = 1;
504 if (instr->prefixes.rep == 1) {
505 instr->str_op_length = MASK(core->vm_regs.rcx, addr_width);
507 instr->str_op_length = 1;
510 instr->src_operand.size = operand_width;
511 instr->src_operand.type = REG_OPERAND;
512 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
514 instr->dst_operand.type = MEM_OPERAND;
515 instr->dst_operand.size = operand_width;
516 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
518 instr->src_operand.read = 1;
519 instr->dst_operand.write = 1;
521 instr->num_operands = 2;
526 instr->dst_operand.type = IMM_OPERAND;
527 instr->dst_operand.size = operand_width;
528 instr->dst_operand.operand = *(uint8_t *)instr_ptr;
529 instr_ptr += operand_width;
530 instr->num_operands = 1;
535 uint8_t reg_code = 0;
537 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
540 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
546 instr->num_operands = 1;
551 uint8_t reg_code = 0;
553 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
556 PrintError(core->vm_info, core, "Error decoding operand for (%s)\n", op_form_to_str(form));
562 instr->dst_operand.read = 1;
564 instr->num_operands = 1;
572 PrintError(core->vm_info, core, "Invalid Instruction form: %s\n", op_form_to_str(form));
576 return (instr_ptr - instr_start);
580 static v3_op_type_t op_form_to_type(op_form_t form) {
745 return V3_INVALID_OP;