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;
81 memset(instr, 0, sizeof(struct x86_instr));
84 length = v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
87 // check for REX prefix
90 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
92 if (form == INVALID_INSTR) {
93 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
97 instr->op_type = op_form_to_type(form);
99 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
102 PrintError("Could not parse instruction operands\n");
108 instr->instr_length += length;
115 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
116 struct x86_instr * instr, op_form_t form) {
117 // get operational mode of the guest for operand width
118 uint8_t operand_width = get_operand_width(core, instr, form);
119 uint8_t addr_width = get_addr_width(core, instr, form);;
121 uint8_t * instr_start = instr_ptr;
139 uint8_t reg_code = 0;
141 instr->dst_operand.size = operand_width;
143 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
146 PrintError("Error decoding operand\n");
152 instr->src_operand.type = IMM_OPERAND;
153 instr->src_operand.size = operand_width;
155 if (operand_width == 1) {
156 instr->src_operand.operand = *(uint8_t *)instr_ptr;
157 } else if (operand_width == 2) {
158 instr->src_operand.operand = *(uint16_t *)instr_ptr;
159 } else if (operand_width == 4) {
160 instr->src_operand.operand = *(uint32_t *)instr_ptr;
162 PrintError("Illegal operand width (%d)\n", operand_width);
166 instr_ptr += operand_width;
168 instr->num_operands = 2;
186 uint8_t reg_code = 0;
188 instr->dst_operand.size = operand_width;
190 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
193 PrintError("Error decoding operand\n");
199 instr->src_operand.type = REG_OPERAND;
200 instr->src_operand.size = operand_width;
202 decode_gpr(&(core->vm_regs), reg_code, &(instr->src_operand));
204 instr->num_operands = 2;
221 uint8_t reg_code = 0;
222 instr->src_operand.size = operand_width;
224 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
227 PrintError("Error decoding operand\n");
233 instr->dst_operand.size = operand_width;
234 instr->dst_operand.type = REG_OPERAND;
235 decode_gpr(&(core->vm_regs), reg_code, &(instr->dst_operand));
237 instr->num_operands = 2;
247 uint8_t reg_code = 0;
248 instr->src_operand.size = operand_width;
250 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
253 PrintError("Error decoding operand\n");
259 instr->src_operand.type = IMM_OPERAND;
260 instr->src_operand.size = operand_width;
261 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
265 instr->num_operands = 2;
271 instr->is_str_op = 1;
273 if (instr->prefixes.rep == 1) {
274 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
276 instr->str_op_length = 1;
282 instr->src_operand.type = MEM_OPERAND;
283 instr->src_operand.size = operand_width;
284 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
286 instr->src_operand.type = MEM_OPERAND;
287 instr->src_operand.size = operand_width;
288 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
290 instr->num_operands = 2;
295 PrintError("Invalid Instruction form: %d\n", form);
299 return (instr_ptr - instr_start);
303 static v3_op_type_t op_form_to_type(op_form_t form) {
467 return V3_INVALID_OP;