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));
82 if (v3_get_vm_cpu_mode(core) == LONG) {
83 if ((*(uint8_t *)(instr_ptr + length) & 0xf0) == 0x40) {
84 *(uint8_t *)&(instr->prefixes.rex) = *(uint8_t *)(instr_ptr + length);
90 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
93 V3_Print("\t decoded as (%s)\n", op_form_to_str(form));
95 if (form == INVALID_INSTR) {
96 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
100 instr->op_type = op_form_to_type(form);
102 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
105 PrintError("Could not parse instruction operands\n");
111 instr->instr_length += length;
114 v3_print_instr(instr);
120 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
121 struct x86_instr * instr, op_form_t form) {
122 // get operational mode of the guest for operand width
123 uint8_t operand_width = get_operand_width(core, instr, form);
124 uint8_t addr_width = get_addr_width(core, instr);
126 uint8_t * instr_start = instr_ptr;
129 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
146 uint8_t reg_code = 0;
148 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
151 PrintError("Error decoding operand\n");
157 instr->src_operand.type = IMM_OPERAND;
158 instr->src_operand.size = operand_width;
160 if (operand_width == 1) {
161 instr->src_operand.operand = *(uint8_t *)instr_ptr;
162 } else if (operand_width == 2) {
163 instr->src_operand.operand = *(uint16_t *)instr_ptr;
164 } else if (operand_width == 4) {
165 instr->src_operand.operand = *(uint32_t *)instr_ptr;
167 PrintError("Illegal operand width (%d)\n", operand_width);
171 instr_ptr += operand_width;
173 instr->num_operands = 2;
191 uint8_t reg_code = 0;
193 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
196 PrintError("Error decoding operand\n");
202 instr->src_operand.type = REG_OPERAND;
203 instr->src_operand.size = operand_width;
205 decode_gpr(core, reg_code, &(instr->src_operand));
207 instr->num_operands = 2;
224 uint8_t reg_code = 0;
226 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
229 PrintError("Error decoding operand\n");
235 instr->dst_operand.size = operand_width;
236 instr->dst_operand.type = REG_OPERAND;
237 decode_gpr(core, reg_code, &(instr->dst_operand));
239 instr->num_operands = 2;
249 uint8_t reg_code = 0;
251 ret = decode_rm_operand(core, instr_ptr, form, 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 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
302 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
308 instr->dst_operand.type = REG_OPERAND;
309 instr->dst_operand.size = operand_width;
310 decode_cr(core, reg_code, &(instr->dst_operand));
312 instr->num_operands = 2;
316 uint8_t reg_code = 0;
318 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
322 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
328 instr->src_operand.type = REG_OPERAND;
329 instr->src_operand.size = operand_width;
330 decode_cr(core, reg_code, &(instr->src_operand));
332 instr->num_operands = 2;
337 instr->is_str_op = 1;
339 if (instr->prefixes.rep == 1) {
340 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
342 instr->str_op_length = 1;
345 instr->src_operand.size = operand_width;
346 instr->src_operand.type = REG_OPERAND;
347 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
349 instr->dst_operand.type = MEM_OPERAND;
350 instr->dst_operand.size = operand_width;
351 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
353 instr->num_operands = 2;
358 uint8_t reg_code = 0;
360 // We use the dst operand here to maintain bug-for-bug compatibility with XED
362 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
365 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
371 instr->num_operands = 1;
381 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
385 return (instr_ptr - instr_start);
389 static v3_op_type_t op_form_to_type(op_form_t form) {
553 return V3_INVALID_OP;