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 static v3_op_type_t op_form_to_type(op_form_t form);
25 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form);
28 int v3_disasm(struct guest_info * info, void *instr_ptr, addr_t * rip, int mark) {
34 int v3_init_decoder(struct guest_info * core) {
39 int v3_deinit_decoder(struct guest_info * core) {
44 int v3_encode(struct guest_info * info, struct x86_instr * instr, uint8_t * instr_buf) {
48 int v3_decode(struct guest_info * core, addr_t instr_ptr, struct x86_instr * instr) {
51 memset(instr, 0, sizeof(struct x86_instr));
54 instr_ptr += v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
57 // check for REX prefix
60 form = op_code_to_form((uint8_t *)instr_ptr);
61 instr->op_type = op_form_to_type(form);
63 parse_operands(core, (uint8_t *)instr_ptr, instr, form);
73 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form) {
74 // get operational mode of the guest for operand width
75 int operand_width = get_operand_width(core, instr, form);
95 uint8_t reg_code = 0;;
96 instr->dst_operand.size = operand_width;
98 ret = decode_rm_operand(core, instr_ptr, &(instr->dst_operand), ®_code);
101 PrintError("Error decoding operand\n");
107 instr->src_operand.type = IMM_OPERAND;
108 instr->src_operand.size = operand_width;
110 if (operand_width == 1) {
111 instr->src_operand.operand = *(uint8_t *)instr_ptr;
112 } else if (operand_width == 2) {
113 instr->src_operand.operand = *(uint16_t *)instr_ptr;
114 } else if (operand_width == 4) {
115 instr->src_operand.operand = *(uint32_t *)instr_ptr;
117 PrintError("Illegal operand width (%d)\n", operand_width);
137 uint8_t reg_code = 0;
139 instr->dst_operand.size = operand_width;
141 ret = decode_rm_operand(core, instr_ptr, &(instr->dst_operand), ®_code);
144 PrintError("Error decoding operand\n");
150 instr->src_operand.type = REG_OPERAND;
151 instr->src_operand.size = operand_width;
153 decode_gpr(&(core->vm_regs), reg_code, &(instr->src_operand));
171 uint8_t reg_code = 0;
172 instr->src_operand.size = operand_width;
174 ret = decode_rm_operand(core, instr_ptr, &(instr->src_operand), ®_code);
177 PrintError("Error decoding operand\n");
183 instr->dst_operand.size = operand_width;
184 instr->dst_operand.type = REG_OPERAND;
185 decode_gpr(&(core->vm_regs), reg_code, &(instr->dst_operand));
197 uint8_t reg_code = 0;
198 instr->src_operand.size = operand_width;
200 ret = decode_rm_operand(core, instr_ptr, &(instr->src_operand), ®_code);
203 PrintError("Error decoding operand\n");
209 instr->src_operand.type = IMM_OPERAND;
210 instr->src_operand.size = operand_width;
211 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
217 PrintError("Invalid Instruction form: %d\n", form);
225 static v3_op_type_t op_form_to_type(op_form_t form) {
389 return V3_INVALID_OP;