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 #define MASK(val, length) ({ \
25 ullong_t mask = 0x0LL; \
28 mask = 0x00000000000000ffLL; \
31 mask = 0x000000000000ffffLL; \
34 mask = 0x00000000ffffffffLL; \
37 mask = 0xffffffffffffffffLL; \
43 static v3_op_type_t op_form_to_type(op_form_t form);
44 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form);
47 int v3_disasm(struct guest_info * info, void *instr_ptr, addr_t * rip, int mark) {
52 int v3_init_decoder(struct guest_info * core) {
57 int v3_deinit_decoder(struct guest_info * core) {
62 int v3_encode(struct guest_info * info, struct x86_instr * instr, uint8_t * instr_buf) {
67 int v3_decode(struct guest_info * core, addr_t instr_ptr, struct x86_instr * instr) {
68 op_form_t form = INVALID_INSTR;
73 V3_Print("Decoding Instruction at %p\n", (void *)instr_ptr);
75 memset(instr, 0, sizeof(struct x86_instr));
78 length = v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
81 // check for REX prefix
84 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
87 V3_Print("\t decoded as (%s)\n", op_form_to_str(form));
89 if (form == INVALID_INSTR) {
90 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
94 instr->op_type = op_form_to_type(form);
96 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
99 PrintError("Could not parse instruction operands\n");
105 instr->instr_length += length;
108 v3_print_instr(instr);
114 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
115 struct x86_instr * instr, op_form_t form) {
116 // get operational mode of the guest for operand width
117 uint8_t operand_width = get_operand_width(core, instr, form);
118 uint8_t addr_width = get_addr_width(core, instr);
120 uint8_t * instr_start = instr_ptr;
123 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
140 uint8_t reg_code = 0;
142 instr->dst_operand.size = operand_width;
144 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
147 PrintError("Error decoding operand\n");
153 instr->src_operand.type = IMM_OPERAND;
154 instr->src_operand.size = operand_width;
156 if (operand_width == 1) {
157 instr->src_operand.operand = *(uint8_t *)instr_ptr;
158 } else if (operand_width == 2) {
159 instr->src_operand.operand = *(uint16_t *)instr_ptr;
160 } else if (operand_width == 4) {
161 instr->src_operand.operand = *(uint32_t *)instr_ptr;
163 PrintError("Illegal operand width (%d)\n", operand_width);
167 instr_ptr += operand_width;
169 instr->num_operands = 2;
187 uint8_t reg_code = 0;
189 instr->dst_operand.size = operand_width;
191 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
194 PrintError("Error decoding operand\n");
200 instr->src_operand.type = REG_OPERAND;
201 instr->src_operand.size = operand_width;
203 decode_gpr(core, reg_code, &(instr->src_operand));
205 instr->num_operands = 2;
222 uint8_t reg_code = 0;
223 instr->src_operand.size = operand_width;
225 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
228 PrintError("Error decoding operand\n");
234 instr->dst_operand.size = operand_width;
235 instr->dst_operand.type = REG_OPERAND;
236 decode_gpr(core, reg_code, &(instr->dst_operand));
238 instr->num_operands = 2;
248 uint8_t reg_code = 0;
249 instr->src_operand.size = operand_width;
251 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
254 PrintError("Error decoding operand\n");
260 instr->src_operand.type = IMM_OPERAND;
261 instr->src_operand.size = operand_width;
262 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
266 instr->num_operands = 2;
272 instr->is_str_op = 1;
274 if (instr->prefixes.rep == 1) {
275 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
277 instr->str_op_length = 1;
281 // Destination: ES:(E)DI
283 instr->src_operand.type = MEM_OPERAND;
284 instr->src_operand.size = operand_width;
285 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
287 instr->dst_operand.type = MEM_OPERAND;
288 instr->dst_operand.size = operand_width;
289 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
291 instr->num_operands = 2;
296 uint8_t reg_code = 0;
298 instr->src_operand.size = operand_width;
300 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand),
304 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
310 instr->dst_operand.type = REG_OPERAND;
311 instr->dst_operand.size = operand_width;
312 decode_cr(core, reg_code, &(instr->dst_operand));
314 instr->num_operands = 2;
318 uint8_t reg_code = 0;
320 instr->dst_operand.size = operand_width;
322 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand),
326 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
332 instr->src_operand.type = REG_OPERAND;
333 instr->src_operand.size = operand_width;
334 decode_cr(core, reg_code, &(instr->src_operand));
336 instr->num_operands = 2;
341 instr->is_str_op = 1;
343 if (instr->prefixes.rep == 1) {
344 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
346 instr->str_op_length = 1;
349 instr->src_operand.size = operand_width;
350 instr->src_operand.type = REG_OPERAND;
351 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
353 instr->dst_operand.type = MEM_OPERAND;
354 instr->dst_operand.size = operand_width;
355 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
357 instr->num_operands = 2;
362 uint8_t reg_code = 0;
364 // We use the dst operand here to maintain bug-for-bug compatibility with XED
366 instr->dst_operand.size = operand_width;
368 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
371 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
377 instr->num_operands = 1;
387 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
391 return (instr_ptr - instr_start);
395 static v3_op_type_t op_form_to_type(op_form_t form) {
559 return V3_INVALID_OP;