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("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("\t decoded as (%s)\n", op_form_to_str(form));
107 if (form == INVALID_INSTR) {
108 PrintError("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("Could not parse instruction operands\n");
122 instr->instr_length += length;
124 #ifdef V3_CONFIG_DEBUG_DECODER
125 v3_print_instr(instr);
132 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
133 struct x86_instr * instr, op_form_t form) {
134 // get operational mode of the guest for operand width
135 uint8_t operand_width = get_operand_width(core, instr, form);
136 uint8_t addr_width = get_addr_width(core, instr);
138 uint8_t * instr_start = instr_ptr;
141 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
158 uint8_t reg_code = 0;
160 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
163 PrintError("Error decoding operand\n");
169 instr->src_operand.type = IMM_OPERAND;
170 instr->src_operand.size = operand_width;
173 if (operand_width == 1) {
174 instr->src_operand.operand = *(uint8_t *)instr_ptr;
175 } else if (operand_width == 2) {
176 instr->src_operand.operand = *(uint16_t *)instr_ptr;
177 } else if (operand_width == 4) {
178 instr->src_operand.operand = *(uint32_t *)instr_ptr;
180 PrintError("Illegal operand width (%d)\n", operand_width);
184 instr->src_operand.read = 1;
185 instr->dst_operand.write = 1;
187 instr_ptr += operand_width;
189 instr->num_operands = 2;
207 uint8_t reg_code = 0;
209 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
212 PrintError("Error decoding operand\n");
218 instr->src_operand.type = REG_OPERAND;
219 instr->src_operand.size = operand_width;
221 instr->src_operand.read = 1;
222 instr->dst_operand.write = 1;
225 decode_gpr(core, reg_code, &(instr->src_operand));
227 instr->num_operands = 2;
244 uint8_t reg_code = 0;
246 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
249 PrintError("Error decoding operand\n");
255 instr->dst_operand.size = operand_width;
256 instr->dst_operand.type = REG_OPERAND;
257 decode_gpr(core, reg_code, &(instr->dst_operand));
259 instr->src_operand.read = 1;
260 instr->dst_operand.write = 1;
262 instr->num_operands = 2;
268 uint8_t reg_code = 0;
270 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
271 instr->src_operand.size = 1;
274 PrintError("Error decoding operand\n");
280 instr->dst_operand.size = operand_width;
281 instr->dst_operand.type = REG_OPERAND;
282 decode_gpr(core, reg_code, &(instr->dst_operand));
284 instr->src_operand.read = 1;
285 instr->dst_operand.write = 1;
287 instr->num_operands = 2;
293 uint8_t reg_code = 0;
295 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
296 instr->src_operand.size = 2;
299 PrintError("Error decoding operand\n");
305 instr->dst_operand.size = operand_width;
306 instr->dst_operand.type = REG_OPERAND;
307 decode_gpr(core, reg_code, &(instr->dst_operand));
309 instr->src_operand.read = 1;
310 instr->dst_operand.write = 1;
312 instr->num_operands = 2;
322 uint8_t reg_code = 0;
324 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
327 PrintError("Error decoding operand\n");
333 instr->src_operand.type = IMM_OPERAND;
334 instr->src_operand.size = operand_width;
335 instr->src_operand.operand = (addr_t)MASK((sint64_t)*(sint8_t *)instr_ptr, operand_width); // sign extend.
337 instr->src_operand.read = 1;
338 instr->dst_operand.write = 1;
342 instr->num_operands = 2;
348 instr->is_str_op = 1;
350 if (instr->prefixes.rep == 1) {
351 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
353 instr->str_op_length = 1;
357 // Destination: ES:(E)DI
359 instr->src_operand.type = MEM_OPERAND;
360 instr->src_operand.size = operand_width;
361 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
364 instr->dst_operand.type = MEM_OPERAND;
365 instr->dst_operand.size = operand_width;
366 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
369 instr->src_operand.read = 1;
370 instr->dst_operand.write = 1;
372 instr->num_operands = 2;
377 uint8_t reg_code = 0;
379 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
383 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
389 instr->dst_operand.type = REG_OPERAND;
390 instr->dst_operand.size = operand_width;
391 decode_cr(core, reg_code, &(instr->dst_operand));
393 instr->src_operand.read = 1;
394 instr->dst_operand.write = 1;
396 instr->num_operands = 2;
400 uint8_t reg_code = 0;
402 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
406 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
412 instr->src_operand.type = REG_OPERAND;
413 instr->src_operand.size = operand_width;
414 decode_cr(core, reg_code, &(instr->src_operand));
416 instr->src_operand.read = 1;
417 instr->dst_operand.write = 1;
419 instr->num_operands = 2;
424 instr->is_str_op = 1;
426 if (instr->prefixes.rep == 1) {
427 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
429 instr->str_op_length = 1;
432 instr->src_operand.size = operand_width;
433 instr->src_operand.type = REG_OPERAND;
434 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
436 instr->dst_operand.type = MEM_OPERAND;
437 instr->dst_operand.size = operand_width;
438 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
440 instr->src_operand.read = 1;
441 instr->dst_operand.write = 1;
443 instr->num_operands = 2;
448 uint8_t reg_code = 0;
450 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
453 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
459 instr->num_operands = 1;
464 uint8_t reg_code = 0;
466 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
469 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
475 instr->dst_operand.read = 1;
477 instr->num_operands = 1;
485 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
489 return (instr_ptr - instr_start);
493 static v3_op_type_t op_form_to_type(op_form_t form) {
656 return V3_INVALID_OP;