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 instr->dst_operand.size = operand_width;
150 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
153 PrintError("Error decoding operand\n");
159 instr->src_operand.type = IMM_OPERAND;
160 instr->src_operand.size = operand_width;
162 if (operand_width == 1) {
163 instr->src_operand.operand = *(uint8_t *)instr_ptr;
164 } else if (operand_width == 2) {
165 instr->src_operand.operand = *(uint16_t *)instr_ptr;
166 } else if (operand_width == 4) {
167 instr->src_operand.operand = *(uint32_t *)instr_ptr;
169 PrintError("Illegal operand width (%d)\n", operand_width);
173 instr_ptr += operand_width;
175 instr->num_operands = 2;
193 uint8_t reg_code = 0;
195 instr->dst_operand.size = operand_width;
197 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
200 PrintError("Error decoding operand\n");
206 instr->src_operand.type = REG_OPERAND;
207 instr->src_operand.size = operand_width;
209 decode_gpr(core, reg_code, &(instr->src_operand));
211 instr->num_operands = 2;
228 uint8_t reg_code = 0;
229 instr->src_operand.size = operand_width;
231 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
234 PrintError("Error decoding operand\n");
240 instr->dst_operand.size = operand_width;
241 instr->dst_operand.type = REG_OPERAND;
242 decode_gpr(core, reg_code, &(instr->dst_operand));
244 instr->num_operands = 2;
254 uint8_t reg_code = 0;
255 instr->src_operand.size = operand_width;
257 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand), ®_code);
260 PrintError("Error decoding operand\n");
266 instr->src_operand.type = IMM_OPERAND;
267 instr->src_operand.size = operand_width;
268 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
272 instr->num_operands = 2;
278 instr->is_str_op = 1;
280 if (instr->prefixes.rep == 1) {
281 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
283 instr->str_op_length = 1;
287 // Destination: ES:(E)DI
289 instr->src_operand.type = MEM_OPERAND;
290 instr->src_operand.size = operand_width;
291 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
293 instr->dst_operand.type = MEM_OPERAND;
294 instr->dst_operand.size = operand_width;
295 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
297 instr->num_operands = 2;
302 uint8_t reg_code = 0;
304 instr->src_operand.size = operand_width;
306 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->src_operand),
310 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
316 instr->dst_operand.type = REG_OPERAND;
317 instr->dst_operand.size = operand_width;
318 decode_cr(core, reg_code, &(instr->dst_operand));
320 instr->num_operands = 2;
324 uint8_t reg_code = 0;
326 instr->dst_operand.size = operand_width;
328 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand),
332 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
338 instr->src_operand.type = REG_OPERAND;
339 instr->src_operand.size = operand_width;
340 decode_cr(core, reg_code, &(instr->src_operand));
342 instr->num_operands = 2;
347 instr->is_str_op = 1;
349 if (instr->prefixes.rep == 1) {
350 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
352 instr->str_op_length = 1;
355 instr->src_operand.size = operand_width;
356 instr->src_operand.type = REG_OPERAND;
357 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
359 instr->dst_operand.type = MEM_OPERAND;
360 instr->dst_operand.size = operand_width;
361 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
363 instr->num_operands = 2;
368 uint8_t reg_code = 0;
370 // We use the dst operand here to maintain bug-for-bug compatibility with XED
372 instr->dst_operand.size = operand_width;
374 ret = decode_rm_operand(core, instr_ptr, instr, &(instr->dst_operand), ®_code);
377 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
383 instr->num_operands = 1;
393 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
397 return (instr_ptr - instr_start);
401 static v3_op_type_t op_form_to_type(op_form_t form) {
565 return V3_INVALID_OP;