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.


Release 1.0
[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 * imm = "IMMEDIATE";
67 static const char * invalid = "INVALID";
68
69 static const char * get_op_type_str(operand_type_t type) {
70   if (type == MEM_OPERAND) {
71     return mem;
72   } else if (type == REG_OPERAND) {
73     return reg;
74   } else if (type == IMM_OPERAND) {
75     return imm;
76   } else {
77     return invalid;
78   }
79 }
80
81 static int print_op(struct x86_operand *op) {
82   printf("\ttype=%s\n", get_op_type_str(op->type));
83
84   switch (op->type) {
85   case REG_OPERAND:
86     printf("\tsize=%d\n", op->size);
87     printf("\taddr=0x%x (val=%x)\n", op->operand, MASK(*(uint_t*)(op->operand), op->size));
88     return 0;
89   case MEM_OPERAND:
90     printf("\tsize=%d\n", op->size);
91     printf("\taddr=0x%x\n", op->operand);
92     return 0;
93
94   case IMM_OPERAND:
95     printf("\tsize=%d\n", op->size);
96     printf("\tval=0x%x\n", op->operand);
97     return 0;
98
99   default:
100     return -1;
101   }
102 }
103
104 int main(int argc, char ** argv) {
105   char * filename;
106   int fd;
107   struct stat file_state;
108   int ret;
109   char * file_buf;
110   int buf_offset = 0;
111   int file_size = 0;
112   char * instr_ptr = 0;
113   
114   struct guest_info * info = (struct guest_info *)malloc(sizeof(struct guest_info ));;
115
116   init_decoder();
117   init_guest_info(info);
118
119   if (argc == 1) {
120     printf("Error: Must give a binary file\n");
121     exit(-1);
122   }
123
124   filename = argv[1];
125   
126   ret = stat(filename, &file_state); 
127
128   if (ret == -1) {
129     printf("Could not stat file\n");
130     return -1;
131   }
132   file_size = file_state.st_size;
133
134   file_buf = malloc(file_size);
135
136   fd = open(filename, NULL);
137
138   if (fd == -1) {
139     printf("Could not open file\n");
140     return -1;
141   } else {
142     int total_read = 0;
143     int num_read = 0;
144     
145     while (total_read < file_size) {
146       num_read = read(fd, file_buf + total_read, file_size - total_read);
147       
148       if (num_read == 0) {
149         printf("end of file\n");
150         break;
151       }
152       if (num_read == -1) {
153         printf("Read error\n");
154         exit(-1);
155       }
156
157       total_read += num_read;
158     }
159
160   }
161
162   
163   instr_ptr = file_buf;
164  
165
166   PrintV3CtrlRegs(info);
167   PrintV3GPRs(info);
168   PrintV3Segments(info);
169
170
171   while (buf_offset < file_size) {
172     struct x86_instr instr;
173
174     if (v3_decode(info, (addr_t)instr_ptr + buf_offset, &instr) == -1) {
175       printf("Unhandled instruction\n");
176       buf_offset += instr.instr_length;
177       continue;
178     }
179     printf("instr_length = %d, noperands=%d\n", instr.instr_length, instr.num_operands);
180
181     printf("Source:\n");
182     print_op(&(instr.src_operand));
183
184     printf("Dest:\n");
185     print_op(&(instr.dst_operand));
186
187
188     printf("\n\n");
189
190     buf_offset += instr.instr_length;
191   }
192
193   return 0;
194 }