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.


added MSR hook framework
[palacios.git] / palacios / include / palacios / vmm_decoder.h
1
2 /*
3  * This file is part of the Palacios Virtual Machine Monitor developed
4  * by the V3VEE Project with funding from the United States National 
5  * Science Foundation and the Department of Energy.  
6  *
7  * The V3VEE Project is a joint project between Northwestern University
8  * and the University of New Mexico.  You can find out more at 
9  * http://www.v3vee.org
10  *
11  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
12  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
13  * All rights reserved.
14  *
15  * Author: Jack Lange <jarusl@cs.northwestern.edu>
16  *
17  * This is free software.  You are permitted to use,
18  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
19  */
20
21 #ifndef __VMM_DECODER_H
22 #define __VMM_DECODER_H
23
24 #ifdef __V3VEE__
25
26 #include <palacios/vm_guest.h>
27 #include <palacios/vmm.h>
28
29
30 typedef enum {INVALID_OPERAND, REG_OPERAND, MEM_OPERAND, IMM_OPERAND} v3_operand_type_t;
31
32 struct x86_operand {
33   addr_t operand;
34   uint_t size;
35   v3_operand_type_t type;
36 };
37
38 struct x86_prefixes {
39   uint_t lock   : 1;  // 0xF0
40   uint_t repne  : 1;  // 0xF2
41   uint_t repnz  : 1;  // 0xF2
42   uint_t rep    : 1;  // 0xF3
43   uint_t repe   : 1;  // 0xF3
44   uint_t repz   : 1;  // 0xF3
45   uint_t cs_override : 1;  // 0x2E
46   uint_t ss_override : 1;  // 0x36
47   uint_t ds_override : 1;  // 0x3E
48   uint_t es_override : 1;  // 0x26
49   uint_t fs_override : 1;  // 0x64
50   uint_t gs_override : 1;  // 0x65
51   uint_t br_not_taken : 1;  // 0x2E
52   uint_t br_takend   : 1;  // 0x3E
53   uint_t op_size     : 1;  // 0x66
54   uint_t addr_size   : 1;  // 0x67
55 };
56
57
58 struct x86_instr {
59   struct x86_prefixes prefixes;
60   uint_t instr_length;
61   addr_t opcode;    // a pointer to the V3_OPCODE_[*] arrays defined below
62   uint_t num_operands;
63   struct x86_operand dst_operand;
64   struct x86_operand src_operand;
65   struct x86_operand third_operand;
66   void * decoder_data;
67 };
68
69
70 struct basic_instr_info {
71   uint_t instr_length;
72   uint_t op_size;
73   uint_t str_op    : 1;
74   uint_t has_rep : 1;
75 };
76
77
78
79   /************************/
80  /* EXTERNAL DECODER API */
81 /************************/
82 /* 
83    This is an External API definition that must be implemented by a decoder
84 */
85
86
87 /* 
88  * Initializes a decoder
89  */
90 int v3_init_decoder();
91
92 /* 
93  * Decodes an instruction 
94  * All addresses in arguments are in the host address space
95  * instr_ptr is the host address of the instruction 
96  * IMPORTANT: make sure the instr_ptr is in contiguous host memory
97  *   ie. Copy it to a buffer before the call
98  */
99 int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * instr);
100
101 /* 
102  * Encodes an instruction
103  * All addresses in arguments are in the host address space
104  * The instruction is encoded from the struct, and copied into a 15 byte host buffer
105  * referenced by instr_buf
106  * any unused bytes at the end of instr_buf will be filled with nops
107  * IMPORTANT: instr_buf must be allocated and 15 bytes long
108  */
109 int v3_encode(struct guest_info * info, struct x86_instr * instr, char * instr_buf);
110
111
112 /*
113  * Gets the operand size for a memory operation
114  *
115  */
116 int v3_basic_mem_decode(struct guest_info * info, addr_t instr_ptr, struct basic_instr_info * instr_info);
117
118
119
120 /* Removes a rep prefix in place */
121 void v3_strip_rep_prefix(uchar_t * instr, int length);
122
123
124
125 /* 
126  * JRL: Some of this was taken from the Xen sources... 
127  */
128
129 #define PACKED __attribute__((packed))
130
131 #define MODRM_MOD(x) ((x >> 6) & 0x3)
132 #define MODRM_REG(x) ((x >> 3) & 0x7)
133 #define MODRM_RM(x)  (x & 0x7)
134
135 struct modrm_byte {
136   uint_t rm   :   3 PACKED;
137   uint_t reg  :   3 PACKED;
138   uint_t mod  :   2 PACKED;
139 };
140
141
142 #define SIB_BASE(x) ((x >> 6) & 0x3)
143 #define SIB_INDEX(x) ((x >> 3) & 0x7)
144 #define SIB_SCALE(x) (x & 0x7)
145
146 struct sib_byte {
147   uint_t base     :   3 PACKED;
148   uint_t index    :   3 PACKED;
149   uint_t scale    :   2 PACKED;
150 };
151
152
153
154 #define MAKE_INSTR(nm, ...) static  const uchar_t V3_OPCODE_##nm[] = { __VA_ARGS__ }
155
156 /* 
157  * Here's how it works:
158  * First byte: Length. 
159  * Following bytes: Opcode bytes. 
160  * Special case: Last byte, if zero, doesn't need to match. 
161  */
162 MAKE_INSTR(INVD,   2, 0x0f, 0x08);
163 MAKE_INSTR(CPUID,  2, 0x0f, 0xa2);
164 MAKE_INSTR(RDMSR,  2, 0x0f, 0x32);
165 MAKE_INSTR(WRMSR,  2, 0x0f, 0x30);
166 MAKE_INSTR(RDTSC,  2, 0x0f, 0x31);
167 MAKE_INSTR(RDTSCP, 3, 0x0f, 0x01, 0xf9);
168 MAKE_INSTR(CLI,    1, 0xfa);
169 MAKE_INSTR(STI,    1, 0xfb);
170 MAKE_INSTR(RDPMC,  2, 0x0f, 0x33);
171 MAKE_INSTR(CLGI,   3, 0x0f, 0x01, 0xdd);
172 MAKE_INSTR(STGI,   3, 0x0f, 0x01, 0xdc);
173 MAKE_INSTR(VMRUN,  3, 0x0f, 0x01, 0xd8);
174 MAKE_INSTR(VMLOAD, 3, 0x0f, 0x01, 0xda);
175 MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, 0xdb);
176 MAKE_INSTR(VMCALL, 3, 0x0f, 0x01, 0xd9);
177 MAKE_INSTR(PAUSE,  2, 0xf3, 0x90);
178 MAKE_INSTR(SKINIT, 3, 0x0f, 0x01, 0xde);
179 MAKE_INSTR(MOV2CR, 3, 0x0f, 0x22, 0x00);
180 MAKE_INSTR(MOVCR2, 3, 0x0f, 0x20, 0x00);
181 MAKE_INSTR(MOV2DR, 3, 0x0f, 0x23, 0x00);
182 MAKE_INSTR(MOVDR2, 3, 0x0f, 0x21, 0x00);
183 MAKE_INSTR(PUSHF,  1, 0x9c);
184 MAKE_INSTR(POPF,   1, 0x9d);
185 MAKE_INSTR(RSM,    2, 0x0f, 0xaa);
186 MAKE_INSTR(INVLPG, 3, 0x0f, 0x01, 0x00);
187 MAKE_INSTR(INVLPGA,3, 0x0f, 0x01, 0xdf);
188 MAKE_INSTR(HLT,    1, 0xf4);
189 MAKE_INSTR(CLTS,   2, 0x0f, 0x06);
190 MAKE_INSTR(LMSW,   3, 0x0f, 0x01, 0x00);
191 MAKE_INSTR(SMSW,   3, 0x0f, 0x01, 0x00);
192
193
194 #define PREFIX_LOCK         0xF0
195 #define PREFIX_REPNE        0xF2
196 #define PREFIX_REPNZ        0xF2
197 #define PREFIX_REP          0xF3
198 #define PREFIX_REPE         0xF3
199 #define PREFIX_REPZ         0xF3
200 #define PREFIX_CS_OVERRIDE  0x2E
201 #define PREFIX_SS_OVERRIDE  0x36
202 #define PREFIX_DS_OVERRIDE  0x3E
203 #define PREFIX_ES_OVERRIDE  0x26
204 #define PREFIX_FS_OVERRIDE  0x64
205 #define PREFIX_GS_OVERRIDE  0x65
206 #define PREFIX_BR_NOT_TAKEN 0x2E
207 #define PREFIX_BR_TAKEN     0x3E
208 #define PREFIX_OP_SIZE      0x66
209 #define PREFIX_ADDR_SIZE    0x67
210
211 int v3_opcode_cmp(const uchar_t * op1, const uchar_t * op2);
212
213
214 static inline int is_prefix_byte(uchar_t byte) {
215   switch (byte) {
216   case 0xF0:      // lock
217   case 0xF2:      // REPNE/REPNZ
218   case 0xF3:      // REP or REPE/REPZ
219   case 0x2E:      // CS override or Branch hint not taken (with Jcc instrs)
220   case 0x36:      // SS override
221   case 0x3E:      // DS override or Branch hint taken (with Jcc instrs)
222   case 0x26:      // ES override
223   case 0x64:      // FS override
224   case 0x65:      // GS override
225     //case 0x2E:      // branch not taken hint
226     //  case 0x3E:      // branch taken hint
227   case 0x66:      // operand size override
228   case 0x67:      // address size override
229     return 1;
230     break;
231   default:
232     return 0;
233     break;
234   }
235 }
236
237
238 static inline v3_reg_t get_gpr_mask(struct guest_info * info) {
239   switch (info->cpu_mode) {
240   case REAL: 
241     return 0xffff;
242     break;
243   case PROTECTED:
244   case PROTECTED_PAE:
245     return 0xffffffff;
246   case LONG:
247   case LONG_32_COMPAT:
248   case LONG_16_COMPAT:
249   default:
250     PrintError("Unsupported Address Mode\n");
251     return -1;
252   }
253 }
254
255
256 static inline addr_t get_addr_linear(struct guest_info * info, addr_t addr, struct v3_segment * seg) {
257   switch (info->cpu_mode) {
258   case REAL:
259     // It appears that the segment values are computed and cached in the vmcb structure 
260     // We Need to check this for Intel
261     /*   return addr + (seg->selector << 4);
262          break;*/
263
264   case PROTECTED:
265   case PROTECTED_PAE:
266     return addr + seg->base;
267     break;
268
269   case LONG:
270     // In long mode the segment bases are disregarded (forced to 0), unless using 
271     // FS or GS, then the base addresses are added
272     return addr + seg->base;
273   case LONG_32_COMPAT:
274   case LONG_16_COMPAT:
275   default:
276     PrintError("Unsupported Address Mode\n");
277     return -1;
278   }
279 }
280
281
282 typedef enum {INVALID_ADDR_TYPE, REG, DISP0, DISP8, DISP16, DISP32} modrm_mode_t;
283 typedef enum {INVALID_REG_SIZE, REG64, REG32, REG16, REG8} reg_size_t;
284
285
286
287
288
289
290 struct v3_gprs;
291
292 static inline addr_t decode_register(struct v3_gprs * gprs, char reg_code, reg_size_t reg_size) {
293   addr_t reg_addr;
294
295   switch (reg_code) {
296   case 0:
297     reg_addr = (addr_t)&(gprs->rax);
298     break;
299   case 1:
300     reg_addr = (addr_t)&(gprs->rcx);
301     break;
302   case 2:
303     reg_addr = (addr_t)&(gprs->rdx);
304     break;
305   case 3:
306     reg_addr = (addr_t)&(gprs->rbx);
307     break;
308   case 4:
309     if (reg_size == REG8) {
310       reg_addr = (addr_t)&(gprs->rax) + 1;
311     } else {
312       reg_addr = (addr_t)&(gprs->rsp);
313     }
314     break;
315   case 5:
316     if (reg_size == REG8) {
317       reg_addr = (addr_t)&(gprs->rcx) + 1;
318     } else {
319       reg_addr = (addr_t)&(gprs->rbp);
320     }
321     break;
322   case 6:
323     if (reg_size == REG8) {
324       reg_addr = (addr_t)&(gprs->rdx) + 1;
325     } else {
326       reg_addr = (addr_t)&(gprs->rsi);
327     }
328     break;
329   case 7:
330     if (reg_size == REG8) {
331       reg_addr = (addr_t)&(gprs->rbx) + 1;
332     } else {
333       reg_addr = (addr_t)&(gprs->rdi);
334     }
335     break;
336   default:
337     reg_addr = 0;
338     break;
339   }
340
341   return reg_addr;
342 }
343
344
345
346 static inline v3_operand_type_t decode_operands16(struct v3_gprs * gprs, // input/output
347                                                char * modrm_instr,       // input
348                                                int * offset,             // output
349                                                addr_t * first_operand,   // output
350                                                addr_t * second_operand,  // output
351                                                reg_size_t reg_size) {    // input
352   
353   struct modrm_byte * modrm = (struct modrm_byte *)modrm_instr;
354   addr_t base_addr = 0;
355   modrm_mode_t mod_mode = 0;
356   v3_operand_type_t addr_type = INVALID_OPERAND;
357   char * instr_cursor = modrm_instr;
358
359   //  PrintDebug("ModRM mod=%d\n", modrm->mod);
360
361   instr_cursor += 1;
362
363   if (modrm->mod == 3) {
364     mod_mode = REG;
365     addr_type = REG_OPERAND;
366     //PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
367
368     *first_operand = decode_register(gprs, modrm->rm, reg_size);
369
370   } else {
371
372     addr_type = MEM_OPERAND;
373
374     if (modrm->mod == 0) {
375       mod_mode = DISP0;
376     } else if (modrm->mod == 1) {
377       mod_mode = DISP8;
378     } else if (modrm->mod == 2) {
379       mod_mode = DISP16;
380     }
381
382     switch (modrm->rm) {
383     case 0:
384       base_addr = gprs->rbx + gprs->rsi;
385       break;
386     case 1:
387       base_addr = gprs->rbx + gprs->rdi;
388       break;
389     case 2:
390       base_addr = gprs->rbp + gprs->rsi;
391       break;
392     case 3:
393       base_addr = gprs->rbp + gprs->rdi;
394       break;
395     case 4:
396       base_addr = gprs->rsi;
397       break;
398     case 5:
399       base_addr = gprs->rdi;
400       break;
401     case 6:
402       if (modrm->mod == 0) {
403         base_addr = 0;
404         mod_mode = DISP16;
405       } else {
406         base_addr = gprs->rbp;
407       }
408       break;
409     case 7:
410       base_addr = gprs->rbx;
411       break;
412     }
413
414
415
416     if (mod_mode == DISP8) {
417       base_addr += (uchar_t)*(instr_cursor);
418       instr_cursor += 1;
419     } else if (mod_mode == DISP16) {
420       base_addr += (ushort_t)*(instr_cursor);
421       instr_cursor += 2;
422     }
423     
424     *first_operand = base_addr;
425   }
426
427   *offset +=  (instr_cursor - modrm_instr);
428   *second_operand = decode_register(gprs, modrm->reg, reg_size);
429
430   return addr_type;
431 }
432
433
434
435 static inline v3_operand_type_t decode_operands32(struct v3_gprs * gprs, // input/output
436                                                uchar_t * modrm_instr,       // input
437                                                int * offset,             // output
438                                                addr_t * first_operand,   // output
439                                                addr_t * second_operand,  // output
440                                                reg_size_t reg_size) {    // input
441   
442   uchar_t * instr_cursor = modrm_instr;
443   struct modrm_byte * modrm = (struct modrm_byte *)modrm_instr;
444   addr_t base_addr = 0;
445   modrm_mode_t mod_mode = 0;
446   uint_t has_sib_byte = 0;
447   v3_operand_type_t addr_type = INVALID_OPERAND;
448
449
450
451   instr_cursor += 1;
452
453   if (modrm->mod == 3) {
454     mod_mode = REG;
455     addr_type = REG_OPERAND;
456     
457     //    PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
458
459     *first_operand = decode_register(gprs, modrm->rm, reg_size);
460
461   } else {
462
463     addr_type = MEM_OPERAND;
464
465     if (modrm->mod == 0) {
466       mod_mode = DISP0;
467     } else if (modrm->mod == 1) {
468       mod_mode = DISP8;
469     } else if (modrm->mod == 2) {
470       mod_mode = DISP32;
471     }
472     
473     switch (modrm->rm) {
474     case 0:
475       base_addr = gprs->rax;
476       break;
477     case 1:
478       base_addr = gprs->rcx;
479       break;
480     case 2:
481       base_addr = gprs->rdx;
482       break;
483     case 3:
484       base_addr = gprs->rbx;
485       break;
486     case 4:
487       has_sib_byte = 1;
488       break;
489     case 5:
490       if (modrm->mod == 0) {
491         base_addr = 0;
492         mod_mode = DISP32;
493       } else {
494         base_addr = gprs->rbp;
495       }
496       break;
497     case 6:
498       base_addr = gprs->rsi;
499       break;
500     case 7:
501       base_addr = gprs->rdi;
502       break;
503     }
504
505     if (has_sib_byte) {
506       instr_cursor += 1;
507       struct sib_byte * sib = (struct sib_byte *)(instr_cursor);
508       int scale = 1;
509
510       instr_cursor += 1;
511
512
513       if (sib->scale == 1) {
514         scale = 2;
515       } else if (sib->scale == 2) {
516         scale = 4;
517       } else if (sib->scale == 3) {
518         scale = 8;
519       }
520
521
522       switch (sib->index) {
523       case 0:
524         base_addr = gprs->rax;
525         break;
526       case 1:
527         base_addr = gprs->rcx;
528         break;
529       case 2:
530         base_addr = gprs->rdx;
531         break;
532       case 3:
533         base_addr = gprs->rbx;
534         break;
535       case 4:
536         base_addr = 0;
537         break;
538       case 5:
539         base_addr = gprs->rbp;
540         break;
541       case 6:
542         base_addr = gprs->rsi;
543         break;
544       case 7:
545         base_addr = gprs->rdi;
546         break;
547       }
548
549       base_addr *= scale;
550
551
552       switch (sib->base) {
553       case 0:
554         base_addr += gprs->rax;
555         break;
556       case 1:
557         base_addr += gprs->rcx;
558         break;
559       case 2:
560         base_addr += gprs->rdx;
561         break;
562       case 3:
563         base_addr += gprs->rbx;
564         break;
565       case 4:
566         base_addr += gprs->rsp;
567         break;
568       case 5:
569         if (modrm->mod != 0) {
570           base_addr += gprs->rbp;
571         }
572         break;
573       case 6:
574         base_addr += gprs->rsi;
575         break;
576       case 7:
577         base_addr += gprs->rdi;
578         break;
579       }
580
581     } 
582
583
584     if (mod_mode == DISP8) {
585       base_addr += (uchar_t)*(instr_cursor);
586       instr_cursor += 1;
587     } else if (mod_mode == DISP32) {
588       base_addr += (uint_t)*(instr_cursor);
589       instr_cursor += 4;
590     }
591     
592
593     *first_operand = base_addr;
594   }
595
596   *offset += (instr_cursor - modrm_instr);
597
598   *second_operand = decode_register(gprs, modrm->reg, reg_size);
599
600   return addr_type;
601 }
602
603
604
605 #endif // !__V3VEE__
606
607
608 #endif