10 #include "vmm_decoder.h"
14 /* Disgusting mask hack...
15 I can't think right now, so we'll do it this way...
17 static const ullong_t mask_1 = 0x00000000000000ffLL;
18 static const ullong_t mask_2 = 0x000000000000ffffLL;
19 static const ullong_t mask_4 = 0x00000000ffffffffLL;
20 static const ullong_t mask_8 = 0xffffffffffffffffLL;
23 #define MASK(val, length) ({ \
24 ullong_t mask = 0x0LL; \
37 static void init_guest_info(struct guest_info * info) {
38 memset(info, 0, sizeof(struct guest_info));
39 info->cpu_mode = PROTECTED;
41 info->vm_regs.rax = 0x01010101;
42 info->vm_regs.rbx = 0x02020202;
43 info->vm_regs.rcx = 0x03030303;
44 info->vm_regs.rdx = 0x04040404;
46 info->vm_regs.rdi = 0x05050505;
47 info->vm_regs.rsi = 0x06060606;
48 info->vm_regs.rsp = 0x07070707;
49 info->vm_regs.rbp = 0x08080808;
51 info->vm_regs.rdi = 0x05050505;
52 info->vm_regs.rsi = 0x06060606;
53 info->vm_regs.rsp = 0x07070707;
54 info->vm_regs.rbp = 0x08080808;
57 info->segments.ds.base = 0xf0f0f0f0;
58 info->segments.es.base = 0xe0e0e0e0;
64 static const char * mem = "MEMORY";
65 static const char * reg = "REGISTER";
66 static const char * imm = "IMMEDIATE";
67 static const char * invalid = "INVALID";
69 static const char * get_op_type_str(operand_type_t type) {
70 if (type == MEM_OPERAND) {
72 } else if (type == REG_OPERAND) {
74 } else if (type == IMM_OPERAND) {
81 static int print_op(struct x86_operand *op) {
82 printf("\ttype=%s\n", get_op_type_str(op->type));
86 printf("\tsize=%d\n", op->size);
87 printf("\taddr=0x%x (val=%x)\n", op->operand, MASK(*(uint_t*)(op->operand), op->size));
90 printf("\tsize=%d\n", op->size);
91 printf("\taddr=0x%x\n", op->operand);
95 printf("\tsize=%d\n", op->size);
96 printf("\tval=0x%x\n", op->operand);
104 int main(int argc, char ** argv) {
107 struct stat file_state;
112 char * instr_ptr = 0;
114 struct guest_info * info = (struct guest_info *)malloc(sizeof(struct guest_info ));;
117 init_guest_info(info);
120 printf("Error: Must give a binary file\n");
126 ret = stat(filename, &file_state);
129 printf("Could not stat file\n");
132 file_size = file_state.st_size;
134 file_buf = malloc(file_size);
136 fd = open(filename, NULL);
139 printf("Could not open file\n");
145 while (total_read < file_size) {
146 num_read = read(fd, file_buf + total_read, file_size - total_read);
149 printf("end of file\n");
152 if (num_read == -1) {
153 printf("Read error\n");
157 total_read += num_read;
163 instr_ptr = file_buf;
166 PrintV3CtrlRegs(info);
168 PrintV3Segments(info);
171 while (buf_offset < file_size) {
172 struct x86_instr instr;
174 if (v3_decode(info, (addr_t)instr_ptr + buf_offset, &instr) == -1) {
175 printf("Unhandled instruction\n");
176 buf_offset += instr.instr_length;
179 printf("instr_length = %d, noperands=%d\n", instr.instr_length, instr.num_operands);
182 print_op(&(instr.src_operand));
185 print_op(&(instr.dst_operand));
190 buf_offset += instr.instr_length;