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 uint64_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;
266 uint8_t reg_code = 0;
268 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
269 instr->src_operand.size = 1;
272 PrintError("Error decoding operand\n");
278 instr->dst_operand.size = operand_width;
279 instr->dst_operand.type = REG_OPERAND;
280 decode_gpr(core, reg_code, &(instr->dst_operand));
282 instr->src_operand.read = 1;
283 instr->dst_operand.write = 1;
285 instr->num_operands = 2;
291 uint8_t reg_code = 0;
293 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand), ®_code);
294 instr->src_operand.size = 2;
297 PrintError("Error decoding operand\n");
303 instr->dst_operand.size = operand_width;
304 instr->dst_operand.type = REG_OPERAND;
305 decode_gpr(core, reg_code, &(instr->dst_operand));
307 instr->src_operand.read = 1;
308 instr->dst_operand.write = 1;
310 instr->num_operands = 2;
320 uint8_t reg_code = 0;
322 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
325 PrintError("Error decoding operand\n");
331 instr->src_operand.type = IMM_OPERAND;
332 instr->src_operand.size = operand_width;
333 instr->src_operand.operand = (addr_t)MASK((sint64_t)*(sint8_t *)instr_ptr, operand_width); // sign extend.
335 instr->src_operand.read = 1;
336 instr->dst_operand.write = 1;
340 instr->num_operands = 2;
346 instr->is_str_op = 1;
348 if (instr->prefixes.rep == 1) {
349 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
351 instr->str_op_length = 1;
355 // Destination: ES:(E)DI
357 instr->src_operand.type = MEM_OPERAND;
358 instr->src_operand.size = operand_width;
359 instr->src_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rsi, addr_width), &(core->segments.ds));
362 instr->dst_operand.type = MEM_OPERAND;
363 instr->dst_operand.size = operand_width;
364 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
367 instr->src_operand.read = 1;
368 instr->dst_operand.write = 1;
370 instr->num_operands = 2;
375 uint8_t reg_code = 0;
377 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->src_operand),
381 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
387 instr->dst_operand.type = REG_OPERAND;
388 instr->dst_operand.size = operand_width;
389 decode_cr(core, reg_code, &(instr->dst_operand));
391 instr->src_operand.read = 1;
392 instr->dst_operand.write = 1;
394 instr->num_operands = 2;
398 uint8_t reg_code = 0;
400 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand),
404 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
410 instr->src_operand.type = REG_OPERAND;
411 instr->src_operand.size = operand_width;
412 decode_cr(core, reg_code, &(instr->src_operand));
414 instr->src_operand.read = 1;
415 instr->dst_operand.write = 1;
417 instr->num_operands = 2;
422 instr->is_str_op = 1;
424 if (instr->prefixes.rep == 1) {
425 instr->str_op_length = MASK(core->vm_regs.rcx, operand_width);
427 instr->str_op_length = 1;
430 instr->src_operand.size = operand_width;
431 instr->src_operand.type = REG_OPERAND;
432 instr->src_operand.operand = (addr_t)&(core->vm_regs.rax);
434 instr->dst_operand.type = MEM_OPERAND;
435 instr->dst_operand.size = operand_width;
436 instr->dst_operand.operand = get_addr_linear(core, MASK(core->vm_regs.rdi, addr_width), &(core->segments.es));
438 instr->src_operand.read = 1;
439 instr->dst_operand.write = 1;
441 instr->num_operands = 2;
446 uint8_t reg_code = 0;
448 ret = decode_rm_operand(core, instr_ptr, form, instr, &(instr->dst_operand), ®_code);
451 PrintError("Error decoding operand for (%s)\n", op_form_to_str(form));
457 instr->num_operands = 1;
466 PrintError("Invalid Instruction form: %s\n", op_form_to_str(form));
470 return (instr_ptr - instr_start);
474 static v3_op_type_t op_form_to_type(op_form_t form) {
638 return V3_INVALID_OP;