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);
126 V3_Print("CS DB FLag=%x\n", core->segments.cs.db);
133 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
134 struct x86_instr * instr, op_form_t form) {
135 // get operational mode of the guest for operand width
136 uint8_t operand_width = get_operand_width(core, instr, form);
137 uint8_t addr_width = get_addr_width(core, instr);
139 uint8_t * instr_start = instr_ptr;
142 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
159 uint8_t reg_code = 0;
161 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
164 PrintError("Error decoding operand\n");
170 instr->src_operand.type = IMM_OPERAND;
171 instr->src_operand.size = operand_width;
174 if (operand_width == 1) {
175 instr->src_operand.operand = *(uint8_t *)instr_ptr;
176 } else if (operand_width == 2) {
177 instr->src_operand.operand = *(uint16_t *)instr_ptr;
178 } else if (operand_width == 4) {
179 instr->src_operand.operand = *(uint32_t *)instr_ptr;
180 } else if (operand_width == 8) {
181 instr->src_operand.operand = *(sint32_t *)instr_ptr; // This is a special case for sign extended 64bit ops
183 PrintError("Illegal operand width (%d)\n", operand_width);
187 instr->src_operand.read = 1;
188 instr->dst_operand.write = 1;
190 instr_ptr += operand_width;
192 instr->num_operands = 2;
210 uint8_t reg_code = 0;
212 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
215 PrintError("Error decoding operand\n");
221 instr->src_operand.type = REG_OPERAND;
222 instr->src_operand.size = operand_width;
224 instr->src_operand.read = 1;
225 instr->dst_operand.write = 1;
228 decode_gpr(core, reg_code, &(instr->src_operand));
230 instr->num_operands = 2;
247 uint8_t reg_code = 0;
249 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
252 PrintError("Error decoding operand\n");
258 instr->dst_operand.size = operand_width;
259 instr->dst_operand.type = REG_OPERAND;
260 decode_gpr(core, reg_code, &(instr->dst_operand));
262 instr->src_operand.read = 1;
263 instr->dst_operand.write = 1;
265 instr->num_operands = 2;
271 uint8_t reg_code = 0;
273 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
274 instr->src_operand.size = 1;
277 PrintError("Error decoding operand\n");
283 instr->dst_operand.size = operand_width;
284 instr->dst_operand.type = REG_OPERAND;
285 decode_gpr(core, reg_code, &(instr->dst_operand));
287 instr->src_operand.read = 1;
288 instr->dst_operand.write = 1;
290 instr->num_operands = 2;
296 uint8_t reg_code = 0;
298 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
299 instr->src_operand.size = 2;
302 PrintError("Error decoding operand\n");
308 instr->dst_operand.size = operand_width;
309 instr->dst_operand.type = REG_OPERAND;
310 decode_gpr(core, reg_code, &(instr->dst_operand));
312 instr->src_operand.read = 1;
313 instr->dst_operand.write = 1;
315 instr->num_operands = 2;
325 uint8_t reg_code = 0;
327 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
330 PrintError("Error decoding operand\n");
336 instr->src_operand.type = IMM_OPERAND;
337 instr->src_operand.size = operand_width;
338 instr->src_operand.operand = (addr_t)MASK((sint64_t)*(sint8_t *)instr_ptr, operand_width); // sign extend.
340 instr->src_operand.read = 1;
341 instr->dst_operand.write = 1;
345 instr->num_operands = 2;
351 instr->is_str_op = 1;
353 if (instr->prefixes.rep == 1) {
354 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
356 instr->str_op_length = 1;
360 // Destination: ES:(E)DI
362 instr->src_operand.type = MEM_OPERAND;
363 instr->src_operand.size = operand_width;
364 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
367 instr->dst_operand.type = MEM_OPERAND;
368 instr->dst_operand.size = operand_width;
369 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
372 instr->src_operand.read = 1;
373 instr->dst_operand.write = 1;
375 instr->num_operands = 2;
380 uint8_t reg_code = 0;
382 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
386 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
392 instr->dst_operand.type = REG_OPERAND;
393 instr->dst_operand.size = operand_width;
394 decode_cr(core, reg_code, &(instr->dst_operand));
396 instr->src_operand.read = 1;
397 instr->dst_operand.write = 1;
399 instr->num_operands = 2;
403 uint8_t reg_code = 0;
405 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
409 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
415 instr->src_operand.type = REG_OPERAND;
416 instr->src_operand.size = operand_width;
417 decode_cr(core, reg_code, &(instr->src_operand));
419 instr->src_operand.read = 1;
420 instr->dst_operand.write = 1;
422 instr->num_operands = 2;
427 instr->is_str_op = 1;
429 if (instr->prefixes.rep == 1) {
430 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
432 instr->str_op_length = 1;
435 instr->src_operand.size = operand_width;
436 instr->src_operand.type = REG_OPERAND;
437 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
439 instr->dst_operand.type = MEM_OPERAND;
440 instr->dst_operand.size = operand_width;
441 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
443 instr->src_operand.read = 1;
444 instr->dst_operand.write = 1;
446 instr->num_operands = 2;
451 uint8_t reg_code = 0;
453 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
456 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
462 instr->num_operands = 1;
467 uint8_t reg_code = 0;
469 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
472 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
478 instr->dst_operand.read = 1;
480 instr->num_operands = 1;
488 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
492 return (instr_ptr - instr_start);
496 static v3_op_type_t op_form_to_type(op_form_t form) {
661 return V3_INVALID_OP;