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 /* Disgusting mask hack...
25 I can't think right now, so we'll do it this way...
27 static const ullong_t mask_1 = 0x00000000000000ffLL;
28 static const ullong_t mask_2 = 0x000000000000ffffLL;
29 static const ullong_t mask_4 = 0x00000000ffffffffLL;
30 static const ullong_t mask_8 = 0xffffffffffffffffLL;
33 #define MASK(val, length) ({ \
34 ullong_t mask = 0x0LL; \
52 static v3_op_type_t op_form_to_type(op_form_t form);
53 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form);
56 int v3_disasm(struct guest_info * info, void *instr_ptr, addr_t * rip, int mark) {
61 int v3_init_decoder(struct guest_info * core) {
66 int v3_deinit_decoder(struct guest_info * core) {
71 int v3_encode(struct guest_info * info, struct x86_instr * instr, uint8_t * instr_buf) {
76 int v3_decode(struct guest_info * core, addr_t instr_ptr, struct x86_instr * instr) {
77 op_form_t form = INVALID_INSTR;
82 V3_Print("Decoding Instruction at %p\n", (void *)instr_ptr);
84 memset(instr, 0, sizeof(struct x86_instr));
87 length = v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
90 // check for REX prefix
93 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
96 V3_Print("\t decoded as (%s)\n", op_form_to_str(form));
98 if (form == INVALID_INSTR) {
99 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
103 instr->op_type = op_form_to_type(form);
105 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
108 PrintError("Could not parse instruction operands\n");
114 instr->instr_length += length;
117 v3_print_instr(instr);
123 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
124 struct x86_instr * instr, op_form_t form) {
125 // get operational mode of the guest for operand width
126 uint8_t operand_width = get_operand_width(core, instr, form);
127 uint8_t addr_width = get_addr_width(core, instr, form);
129 uint8_t * instr_start = instr_ptr;
132 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
149 uint8_t reg_code = 0;
151 instr->dst_operand.size = operand_width;
153 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
156 PrintError("Error decoding operand\n");
162 instr->src_operand.type = IMM_OPERAND;
163 instr->src_operand.size = operand_width;
165 if (operand_width == 1) {
166 instr->src_operand.operand = *(uint8_t *)instr_ptr;
167 } else if (operand_width == 2) {
168 instr->src_operand.operand = *(uint16_t *)instr_ptr;
169 } else if (operand_width == 4) {
170 instr->src_operand.operand = *(uint32_t *)instr_ptr;
172 PrintError("Illegal operand width (%d)\n", operand_width);
176 instr_ptr += operand_width;
178 instr->num_operands = 2;
196 uint8_t reg_code = 0;
198 instr->dst_operand.size = operand_width;
200 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
203 PrintError("Error decoding operand\n");
209 instr->src_operand.type = REG_OPERAND;
210 instr->src_operand.size = operand_width;
212 decode_gpr(core, reg_code, &(instr->src_operand));
214 instr->num_operands = 2;
231 uint8_t reg_code = 0;
232 instr->src_operand.size = operand_width;
234 ret = decode_rm_operand(core, instr_ptr, 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;
258 instr->src_operand.size = operand_width;
260 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
263 PrintError("Error decoding operand\n");
269 instr->src_operand.type = IMM_OPERAND;
270 instr->src_operand.size = operand_width;
271 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
275 instr->num_operands = 2;
281 instr->is_str_op = 1;
283 if (instr->prefixes.rep == 1) {
284 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
286 instr->str_op_length = 1;
290 // Destination: ES:(E)DI
292 instr->src_operand.type = MEM_OPERAND;
293 instr->src_operand.size = operand_width;
294 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
296 instr->dst_operand.type = MEM_OPERAND;
297 instr->dst_operand.size = operand_width;
298 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
300 instr->num_operands = 2;
305 uint8_t reg_code = 0;
307 instr->src_operand.size = operand_width;
309 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand),
313 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
319 instr->dst_operand.type = REG_OPERAND;
320 instr->dst_operand.size = operand_width;
321 decode_cr(core, reg_code, &(instr->dst_operand));
323 instr->num_operands = 2;
327 uint8_t reg_code = 0;
329 instr->dst_operand.size = operand_width;
331 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand),
335 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
341 instr->src_operand.type = REG_OPERAND;
342 instr->src_operand.size = operand_width;
343 decode_cr(core, reg_code, &(instr->src_operand));
345 instr->num_operands = 2;
350 instr->is_str_op = 1;
352 if (instr->prefixes.rep == 1) {
353 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
355 instr->str_op_length = 1;
358 instr->src_operand.size = operand_width;
359 instr->src_operand.type = REG_OPERAND;
360 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
362 instr->dst_operand.type = MEM_OPERAND;
363 instr->dst_operand.size = operand_width;
364 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
366 instr->num_operands = 2;
371 uint8_t reg_code = 0;
373 // We use the dst operand here to maintain bug-for-bug compatibility with XED
375 instr->dst_operand.size = operand_width;
377 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
380 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
386 instr->num_operands = 1;
396 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
400 return (instr_ptr - instr_start);
404 static v3_op_type_t op_form_to_type(op_form_t form) {
568 return V3_INVALID_OP;