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>
23 #ifndef CONFIG_DEBUG_DECODER
25 #define PrintDebug(fmt, args...)
29 #define MASK(val, length) ({ \
30 ullong_t mask = 0x0LL; \
33 mask = 0x00000000000000ffLL; \
36 mask = 0x000000000000ffffLL; \
39 mask = 0x00000000ffffffffLL; \
42 mask = 0xffffffffffffffffLL; \
48 static v3_op_type_t op_form_to_type(op_form_t form);
49 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr, struct x86_instr * instr, op_form_t form);
52 int v3_disasm(struct guest_info * info, void *instr_ptr, addr_t * rip, int mark) {
57 int v3_init_decoder(struct guest_info * core) {
62 int v3_deinit_decoder(struct guest_info * core) {
67 int v3_encode(struct guest_info * info, struct x86_instr * instr, uint8_t * instr_buf) {
72 int v3_decode(struct guest_info * core, addr_t instr_ptr, struct x86_instr * instr) {
73 op_form_t form = INVALID_INSTR;
78 V3_Print("Decoding Instruction at %p\n", (void *)instr_ptr);
80 memset(instr, 0, sizeof(struct x86_instr));
83 length = v3_get_prefixes((uint8_t *)instr_ptr, &(instr->prefixes));
87 if (v3_get_vm_cpu_mode(core) == LONG) {
88 uint8_t prefix = *(uint8_t *)(instr_ptr + length);
90 if ((prefix & 0xf0) == 0x40) {
91 instr->prefixes.rex = 1;
93 instr->prefixes.rex_rm = (prefix & 0x01);
94 instr->prefixes.rex_sib_idx = ((prefix & 0x02) >> 1);
95 instr->prefixes.rex_reg = ((prefix & 0x04) >> 2);
96 instr->prefixes.rex_op_size = ((prefix & 0x08) >> 3);
103 form = op_code_to_form((uint8_t *)(instr_ptr + length), &length);
105 V3_Print("\t decoded as (%s)\n", op_form_to_str(form));
107 if (form == INVALID_INSTR) {
108 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
112 instr->op_type = op_form_to_type(form);
114 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
117 PrintError("Could not parse instruction operands\n");
122 instr->instr_length += length;
124 v3_print_instr(instr);
130 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
131 struct x86_instr * instr, op_form_t form) {
132 // get operational mode of the guest for operand width
133 uint8_t operand_width = get_operand_width(core, instr, form);
134 uint8_t addr_width = get_addr_width(core, instr);
136 uint8_t * instr_start = instr_ptr;
139 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
156 uint8_t reg_code = 0;
158 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
161 PrintError("Error decoding operand\n");
167 instr->src_operand.type = IMM_OPERAND;
168 instr->src_operand.size = operand_width;
171 if (operand_width == 1) {
172 instr->src_operand.operand = *(uint8_t *)instr_ptr;
173 } else if (operand_width == 2) {
174 instr->src_operand.operand = *(uint16_t *)instr_ptr;
175 } else if (operand_width == 4) {
176 instr->src_operand.operand = *(uint32_t *)instr_ptr;
178 PrintError("Illegal operand width (%d)\n", operand_width);
182 instr->src_operand.read = 1;
183 instr->dst_operand.write = 1;
185 instr_ptr += operand_width;
187 instr->num_operands = 2;
205 uint8_t reg_code = 0;
207 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
210 PrintError("Error decoding operand\n");
216 instr->src_operand.type = REG_OPERAND;
217 instr->src_operand.size = operand_width;
219 instr->src_operand.read = 1;
220 instr->dst_operand.write = 1;
223 decode_gpr(core, reg_code, &(instr->src_operand));
225 instr->num_operands = 2;
242 uint8_t reg_code = 0;
244 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
247 PrintError("Error decoding operand\n");
253 instr->dst_operand.size = operand_width;
254 instr->dst_operand.type = REG_OPERAND;
255 decode_gpr(core, reg_code, &(instr->dst_operand));
257 instr->src_operand.read = 1;
258 instr->dst_operand.write = 1;
260 instr->num_operands = 2;
270 uint8_t reg_code = 0;
272 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
275 PrintError("Error decoding operand\n");
281 instr->src_operand.type = IMM_OPERAND;
282 instr->src_operand.size = operand_width;
283 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
285 instr->src_operand.read = 1;
286 instr->dst_operand.write = 1;
290 instr->num_operands = 2;
296 instr->is_str_op = 1;
298 if (instr->prefixes.rep == 1) {
299 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
301 instr->str_op_length = 1;
305 // Destination: ES:(E)DI
307 instr->src_operand.type = MEM_OPERAND;
308 instr->src_operand.size = operand_width;
309 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
312 instr->dst_operand.type = MEM_OPERAND;
313 instr->dst_operand.size = operand_width;
314 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
317 instr->src_operand.read = 1;
318 instr->dst_operand.write = 1;
320 instr->num_operands = 2;
325 uint8_t reg_code = 0;
327 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
331 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
337 instr->dst_operand.type = REG_OPERAND;
338 instr->dst_operand.size = operand_width;
339 decode_cr(core, reg_code, &(instr->dst_operand));
341 instr->src_operand.read = 1;
342 instr->dst_operand.write = 1;
344 instr->num_operands = 2;
348 uint8_t reg_code = 0;
350 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
355 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
361 instr->src_operand.type = REG_OPERAND;
362 instr->src_operand.size = operand_width;
363 decode_cr(core, reg_code, &(instr->src_operand));
365 instr->src_operand.read = 1;
366 instr->dst_operand.write = 1;
368 instr->num_operands = 2;
373 instr->is_str_op = 1;
375 if (instr->prefixes.rep == 1) {
376 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
378 instr->str_op_length = 1;
381 instr->src_operand.size = operand_width;
382 instr->src_operand.type = REG_OPERAND;
383 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
385 instr->dst_operand.type = MEM_OPERAND;
386 instr->dst_operand.size = operand_width;
387 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
389 instr->src_operand.read = 1;
390 instr->dst_operand.write = 1;
392 instr->num_operands = 2;
397 uint8_t reg_code = 0;
399 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
402 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
408 instr->num_operands = 1;
417 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
421 return (instr_ptr - instr_start);
425 static v3_op_type_t op_form_to_type(op_form_t form) {
589 return V3_INVALID_OP;