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);
106 V3_Print("\t decoded as (%s)\n", op_form_to_str(form));
108 if (form == INVALID_INSTR) {
109 PrintError("Could not find instruction form (%x)\n", *(uint32_t *)(instr_ptr + length));
113 instr->op_type = op_form_to_type(form);
115 ret = parse_operands(core, (uint8_t *)(instr_ptr + length), instr, form);
118 PrintError("Could not parse instruction operands\n");
124 instr->instr_length += length;
127 v3_print_instr(instr);
133 static int parse_operands(struct guest_info * core, uint8_t * instr_ptr,
134 struct x86_instr * instr, op_form_t form) {
135 // get operational mode of the guest for operand width
136 uint8_t operand_width = get_operand_width(core, instr, form);
137 uint8_t addr_width = get_addr_width(core, instr);
139 uint8_t * instr_start = instr_ptr;
142 PrintDebug("\tOperand width=%d, Addr width=%d\n", operand_width, addr_width);
159 uint8_t reg_code = 0;
161 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
164 PrintError("Error decoding operand\n");
170 instr->src_operand.type = IMM_OPERAND;
171 instr->src_operand.size = operand_width;
174 if (operand_width == 1) {
175 instr->src_operand.operand = *(uint8_t *)instr_ptr;
176 } else if (operand_width == 2) {
177 instr->src_operand.operand = *(uint16_t *)instr_ptr;
178 } else if (operand_width == 4) {
179 instr->src_operand.operand = *(uint32_t *)instr_ptr;
181 PrintError("Illegal operand width (%d)\n", operand_width);
185 instr->src_operand.read = 1;
186 instr->dst_operand.write = 1;
188 instr_ptr += operand_width;
190 instr->num_operands = 2;
208 uint8_t reg_code = 0;
210 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
213 PrintError("Error decoding operand\n");
219 instr->src_operand.type = REG_OPERAND;
220 instr->src_operand.size = operand_width;
222 instr->src_operand.read = 1;
223 instr->dst_operand.write = 1;
226 decode_gpr(core, reg_code, &(instr->src_operand));
228 instr->num_operands = 2;
245 uint8_t reg_code = 0;
247 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
250 PrintError("Error decoding operand\n");
256 instr->dst_operand.size = operand_width;
257 instr->dst_operand.type = REG_OPERAND;
258 decode_gpr(core, reg_code, &(instr->dst_operand));
260 instr->src_operand.read = 1;
261 instr->dst_operand.write = 1;
263 instr->num_operands = 2;
273 uint8_t reg_code = 0;
275 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
279 PrintError("Error decoding operand\n");
285 instr->src_operand.type = IMM_OPERAND;
286 instr->src_operand.size = operand_width;
287 instr->src_operand.operand = *(sint8_t *)instr_ptr; // sign extend.
289 instr->src_operand.read = 1;
290 instr->dst_operand.write = 1;
294 instr->num_operands = 2;
300 instr->is_str_op = 1;
302 if (instr->prefixes.rep == 1) {
303 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
305 instr->str_op_length = 1;
309 // Destination: ES:(E)DI
311 instr->src_operand.type = MEM_OPERAND;
312 instr->src_operand.size = operand_width;
313 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
316 instr->dst_operand.type = MEM_OPERAND;
317 instr->dst_operand.size = operand_width;
318 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
321 instr->src_operand.read = 1;
322 instr->dst_operand.write = 1;
324 instr->num_operands = 2;
329 uint8_t reg_code = 0;
331 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
335 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
341 instr->dst_operand.type = REG_OPERAND;
342 instr->dst_operand.size = operand_width;
343 decode_cr(core, reg_code, &(instr->dst_operand));
345 instr->src_operand.read = 1;
346 instr->dst_operand.write = 1;
348 instr->num_operands = 2;
352 uint8_t reg_code = 0;
354 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
359 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
365 instr->src_operand.type = REG_OPERAND;
366 instr->src_operand.size = operand_width;
367 decode_cr(core, reg_code, &(instr->src_operand));
369 instr->src_operand.read = 1;
370 instr->dst_operand.write = 1;
372 instr->num_operands = 2;
377 instr->is_str_op = 1;
379 if (instr->prefixes.rep == 1) {
380 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
382 instr->str_op_length = 1;
385 instr->src_operand.size = operand_width;
386 instr->src_operand.type = REG_OPERAND;
387 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
389 instr->dst_operand.type = MEM_OPERAND;
390 instr->dst_operand.size = operand_width;
391 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
393 instr->src_operand.read = 1;
394 instr->dst_operand.write = 1;
396 instr->num_operands = 2;
401 uint8_t reg_code = 0;
403 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
406 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
412 instr->num_operands = 1;
422 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
426 return (instr_ptr - instr_start);
430 static v3_op_type_t op_form_to_type(op_form_t form) {
594 return V3_INVALID_OP;