Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


00b219f7c39e54d5216de5e180c1bffd461e290c
[palacios.git] / misc / decoder_test / test.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7
8 #include "test.h"
9 #include "vmm_xed.h"
10 #include "vmm_decoder.h"
11 #include "vm_guest.h"
12
13
14 /* Disgusting mask hack...
15    I can't think right now, so we'll do it this way...
16 */
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;
21
22
23 #define MASK(val, length) ({                    \
24       ullong_t mask = 0x0LL;                    \
25       switch (length) {                         \
26       case 1:                                   \
27         mask = mask_1;                          \
28       case 2:                                   \
29         mask = mask_2;                          \
30       case 4:                                   \
31         mask = mask_4;                          \
32       case 8:                                   \
33         mask = mask_8;                          \
34       }                                         \
35       val & mask;})                             \
36
37 static void init_guest_info(struct guest_info * info) {
38   memset(info, 0, sizeof(struct guest_info));
39   info->cpu_mode = PROTECTED;
40
41   info->vm_regs.rax = 0x01010101;
42   info->vm_regs.rbx = 0x02020202;
43   info->vm_regs.rcx = 0x03030303;
44   info->vm_regs.rdx = 0x04040404;
45
46   info->vm_regs.rdi = 0x05050505;
47   info->vm_regs.rsi = 0x06060606;
48   info->vm_regs.rsp = 0x07070707;
49   info->vm_regs.rbp = 0x08080808;
50
51   info->vm_regs.rdi = 0x05050505;
52   info->vm_regs.rsi = 0x06060606;
53   info->vm_regs.rsp = 0x07070707;
54   info->vm_regs.rbp = 0x08080808;
55
56
57   info->segments.ds.base = 0xf0f0f0f0;
58   info->segments.es.base = 0xe0e0e0e0;
59
60
61
62
63 }
64 static const char * mem = "MEMORY";
65 static const char * reg = "REGISTER";
66 static const char * invalid = "INVALID";
67
68 static const char * get_op_type_str(operand_type_t type) {
69   if (type == MEM_OPERAND) {
70     return mem;
71   } else if (type == REG_OPERAND) {
72     return reg;
73   } else {
74     return invalid;
75   }
76 }
77
78 static int print_op(struct x86_operand *op) {
79   printf("\ttype=%s\n", get_op_type_str(op->type));
80
81   switch (op->type) {
82   case REG_OPERAND:
83     printf("\tsize=%d\n", op->size);
84     printf("\taddr=0x%x (val=%x)\n", op->operand, MASK(*(uint_t*)(op->operand), op->size));
85     return 0;
86   case MEM_OPERAND:
87     printf("\tsize=%d\n", op->size);
88     printf("\taddr=0x%x\n", op->operand);
89     return 0;
90
91   default:
92     return -1;
93   }
94 }
95
96 int main(int argc, char ** argv) {
97   char * filename;
98   int fd;
99   struct stat file_state;
100   int ret;
101   char * file_buf;
102   int buf_offset = 0;
103   int file_size = 0;
104   char * instr_ptr = 0;
105   
106   struct guest_info * info = (struct guest_info *)malloc(sizeof(struct guest_info ));;
107
108   init_decoder();
109   init_guest_info(info);
110
111   if (argc == 1) {
112     printf("Error: Must give a binary file\n");
113     exit(-1);
114   }
115
116   filename = argv[1];
117   
118   ret = stat(filename, &file_state); 
119
120   if (ret == -1) {
121     printf("Could not stat file\n");
122     return -1;
123   }
124   file_size = file_state.st_size;
125
126   file_buf = malloc(file_size);
127
128   fd = open(filename, NULL);
129
130   if (fd == -1) {
131     printf("Could not open file\n");
132     return -1;
133   } else {
134     int total_read = 0;
135     int num_read = 0;
136     
137     while (total_read < file_size) {
138       num_read = read(fd, file_buf + total_read, file_size - total_read);
139       
140       if (num_read == 0) {
141         printf("end of file\n");
142         break;
143       }
144       if (num_read == -1) {
145         printf("Read error\n");
146         exit(-1);
147       }
148
149       total_read += num_read;
150     }
151
152   }
153
154   
155   instr_ptr = file_buf;
156  
157
158   PrintV3CtrlRegs(info);
159   PrintV3GPRs(info);
160   PrintV3Segments(info);
161
162
163   while (buf_offset < file_size) {
164     struct x86_instr instr;
165
166     if (v3_decode(info, (addr_t)instr_ptr + buf_offset, &instr) == -1) {
167       printf("Unhandled instruction\n");
168       buf_offset += instr.instr_length;
169       continue;
170     }
171     printf("instr_length = %d, noperands=%d\n", instr.instr_length, instr.num_operands);
172
173     printf("Source:\n");
174     print_op(&(instr.src_operand));
175
176     printf("Dest:\n");
177     print_op(&(instr.dst_operand));
178
179
180     printf("\n\n");
181
182     buf_offset += instr.instr_length;
183   }
184
185   return 0;
186 }