1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #ifndef __VMM_EMULATE_H
5 #define __VMM_EMULATE_H
9 #include <palacios/vm_guest.h>
10 #include <palacios/vmm.h>
13 typedef enum {INVALID_OPERAND, REG_OPERAND, MEM_OPERAND, IMM_OPERAND} operand_type_t;
22 uint_t lock : 1; // 0xF0
23 uint_t repne : 1; // 0xF2
24 uint_t repnz : 1; // 0xF2
25 uint_t rep : 1; // 0xF3
26 uint_t repe : 1; // 0xF3
27 uint_t repz : 1; // 0xF3
28 uint_t cs_override : 1; // 0x2E
29 uint_t ss_override : 1; // 0x36
30 uint_t ds_override : 1; // 0x3E
31 uint_t es_override : 1; // 0x26
32 uint_t fs_override : 1; // 0x64
33 uint_t gs_override : 1; // 0x65
34 uint_t br_not_taken : 1; // 0x2E
35 uint_t br_takend : 1; // 0x3E
36 uint_t op_size : 1; // 0x66
37 uint_t addr_size : 1; // 0x67
42 struct x86_prefixes prefixes;
44 addr_t opcode; // a pointer to the V3_OPCODE_[*] arrays defined below
46 struct x86_operand dst_operand;
47 struct x86_operand src_operand;
48 struct x86_operand third_operand;
53 struct basic_instr_info {
62 /************************/
63 /* EXTERNAL DECODER API */
64 /************************/
66 This is an External API definition that must be implemented by a decoder
71 * Initializes a decoder
76 * Decodes an instruction
77 * All addresses in arguments are in the host address space
78 * instr_ptr is the host address of the instruction
79 * IMPORTANT: make sure the instr_ptr is in contiguous host memory
80 * ie. Copy it to a buffer before the call
82 int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * instr);
85 * Encodes an instruction
86 * All addresses in arguments are in the host address space
87 * The instruction is encoded from the struct, and copied into a 15 byte host buffer
88 * referenced by instr_buf
89 * any unused bytes at the end of instr_buf will be filled with nops
90 * IMPORTANT: instr_buf must be allocated and 15 bytes long
92 int v3_encode(struct guest_info * info, struct x86_instr * instr, char * instr_buf);
96 * Gets the operand size for a memory operation
99 int v3_basic_mem_decode(struct guest_info * info, addr_t instr_ptr, struct basic_instr_info * instr_info);
103 /* Removes a rep prefix in place */
104 void strip_rep_prefix(uchar_t * instr, int length);
109 * JRL: Some of this was taken from the Xen sources...
112 #define PACKED __attribute__((packed))
114 #define MODRM_MOD(x) ((x >> 6) & 0x3)
115 #define MODRM_REG(x) ((x >> 3) & 0x7)
116 #define MODRM_RM(x) (x & 0x7)
119 uint_t rm : 3 PACKED;
120 uint_t reg : 3 PACKED;
121 uint_t mod : 2 PACKED;
125 #define SIB_BASE(x) ((x >> 6) & 0x3)
126 #define SIB_INDEX(x) ((x >> 3) & 0x7)
127 #define SIB_SCALE(x) (x & 0x7)
130 uint_t base : 3 PACKED;
131 uint_t index : 3 PACKED;
132 uint_t scale : 2 PACKED;
137 #define MAKE_INSTR(nm, ...) static const uchar_t V3_OPCODE_##nm[] = { __VA_ARGS__ }
140 * Here's how it works:
141 * First byte: Length.
142 * Following bytes: Opcode bytes.
143 * Special case: Last byte, if zero, doesn't need to match.
145 MAKE_INSTR(INVD, 2, 0x0f, 0x08);
146 MAKE_INSTR(CPUID, 2, 0x0f, 0xa2);
147 MAKE_INSTR(RDMSR, 2, 0x0f, 0x32);
148 MAKE_INSTR(WRMSR, 2, 0x0f, 0x30);
149 MAKE_INSTR(RDTSC, 2, 0x0f, 0x31);
150 MAKE_INSTR(RDTSCP, 3, 0x0f, 0x01, 0xf9);
151 MAKE_INSTR(CLI, 1, 0xfa);
152 MAKE_INSTR(STI, 1, 0xfb);
153 MAKE_INSTR(RDPMC, 2, 0x0f, 0x33);
154 MAKE_INSTR(CLGI, 3, 0x0f, 0x01, 0xdd);
155 MAKE_INSTR(STGI, 3, 0x0f, 0x01, 0xdc);
156 MAKE_INSTR(VMRUN, 3, 0x0f, 0x01, 0xd8);
157 MAKE_INSTR(VMLOAD, 3, 0x0f, 0x01, 0xda);
158 MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, 0xdb);
159 MAKE_INSTR(VMCALL, 3, 0x0f, 0x01, 0xd9);
160 MAKE_INSTR(PAUSE, 2, 0xf3, 0x90);
161 MAKE_INSTR(SKINIT, 3, 0x0f, 0x01, 0xde);
162 MAKE_INSTR(MOV2CR, 3, 0x0f, 0x22, 0x00);
163 MAKE_INSTR(MOVCR2, 3, 0x0f, 0x20, 0x00);
164 MAKE_INSTR(MOV2DR, 3, 0x0f, 0x23, 0x00);
165 MAKE_INSTR(MOVDR2, 3, 0x0f, 0x21, 0x00);
166 MAKE_INSTR(PUSHF, 1, 0x9c);
167 MAKE_INSTR(POPF, 1, 0x9d);
168 MAKE_INSTR(RSM, 2, 0x0f, 0xaa);
169 MAKE_INSTR(INVLPG, 3, 0x0f, 0x01, 0x00);
170 MAKE_INSTR(INVLPGA,3, 0x0f, 0x01, 0xdf);
171 MAKE_INSTR(HLT, 1, 0xf4);
172 MAKE_INSTR(CLTS, 2, 0x0f, 0x06);
173 MAKE_INSTR(LMSW, 3, 0x0f, 0x01, 0x00);
174 MAKE_INSTR(SMSW, 3, 0x0f, 0x01, 0x00);
177 #define PREFIX_LOCK 0xF0
178 #define PREFIX_REPNE 0xF2
179 #define PREFIX_REPNZ 0xF2
180 #define PREFIX_REP 0xF3
181 #define PREFIX_REPE 0xF3
182 #define PREFIX_REPZ 0xF3
183 #define PREFIX_CS_OVERRIDE 0x2E
184 #define PREFIX_SS_OVERRIDE 0x36
185 #define PREFIX_DS_OVERRIDE 0x3E
186 #define PREFIX_ES_OVERRIDE 0x26
187 #define PREFIX_FS_OVERRIDE 0x64
188 #define PREFIX_GS_OVERRIDE 0x65
189 #define PREFIX_BR_NOT_TAKEN 0x2E
190 #define PREFIX_BR_TAKEN 0x3E
191 #define PREFIX_OP_SIZE 0x66
192 #define PREFIX_ADDR_SIZE 0x67
194 int opcode_cmp(const uchar_t * op1, const uchar_t * op2);
197 static inline int is_prefix_byte(char byte) {
200 case 0xF2: // REPNE/REPNZ
201 case 0xF3: // REP or REPE/REPZ
202 case 0x2E: // CS override or Branch hint not taken (with Jcc instrs)
203 case 0x36: // SS override
204 case 0x3E: // DS override or Branch hint taken (with Jcc instrs)
205 case 0x26: // ES override
206 case 0x64: // FS override
207 case 0x65: // GS override
208 //case 0x2E: // branch not taken hint
209 // case 0x3E: // branch taken hint
210 case 0x66: // operand size override
211 case 0x67: // address size override
221 static inline v3_reg_t get_gpr_mask(struct guest_info * info) {
222 switch (info->cpu_mode) {
235 static inline addr_t get_addr_linear(struct guest_info * info, addr_t addr, struct v3_segment * seg) {
236 switch (info->cpu_mode) {
238 // It appears that the segment values are computed and cached in the vmcb structure
239 // We Need to check this for Intel
240 /* return addr + (seg->selector << 4);
244 return addr + seg->base;
253 typedef enum {INVALID_ADDR_TYPE, REG, DISP0, DISP8, DISP16, DISP32} modrm_mode_t;
254 typedef enum {INVALID_REG_SIZE, REG64, REG32, REG16, REG8} reg_size_t;
263 static inline addr_t decode_register(struct v3_gprs * gprs, char reg_code, reg_size_t reg_size) {
268 reg_addr = (addr_t)&(gprs->rax);
271 reg_addr = (addr_t)&(gprs->rcx);
274 reg_addr = (addr_t)&(gprs->rdx);
277 reg_addr = (addr_t)&(gprs->rbx);
280 if (reg_size == REG8) {
281 reg_addr = (addr_t)&(gprs->rax) + 1;
283 reg_addr = (addr_t)&(gprs->rsp);
287 if (reg_size == REG8) {
288 reg_addr = (addr_t)&(gprs->rcx) + 1;
290 reg_addr = (addr_t)&(gprs->rbp);
294 if (reg_size == REG8) {
295 reg_addr = (addr_t)&(gprs->rdx) + 1;
297 reg_addr = (addr_t)&(gprs->rsi);
301 if (reg_size == REG8) {
302 reg_addr = (addr_t)&(gprs->rbx) + 1;
304 reg_addr = (addr_t)&(gprs->rdi);
317 static inline operand_type_t decode_operands16(struct v3_gprs * gprs, // input/output
318 char * modrm_instr, // input
319 int * offset, // output
320 addr_t * first_operand, // output
321 addr_t * second_operand, // output
322 reg_size_t reg_size) { // input
324 struct modrm_byte * modrm = (struct modrm_byte *)modrm_instr;
325 addr_t base_addr = 0;
326 modrm_mode_t mod_mode = 0;
327 operand_type_t addr_type = INVALID_OPERAND;
328 char * instr_cursor = modrm_instr;
330 // PrintDebug("ModRM mod=%d\n", modrm->mod);
334 if (modrm->mod == 3) {
336 addr_type = REG_OPERAND;
337 //PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
339 *first_operand = decode_register(gprs, modrm->rm, reg_size);
343 addr_type = MEM_OPERAND;
345 if (modrm->mod == 0) {
347 } else if (modrm->mod == 1) {
349 } else if (modrm->mod == 2) {
355 base_addr = gprs->rbx + gprs->rsi;
358 base_addr = gprs->rbx + gprs->rdi;
361 base_addr = gprs->rbp + gprs->rsi;
364 base_addr = gprs->rbp + gprs->rdi;
367 base_addr = gprs->rsi;
370 base_addr = gprs->rdi;
373 if (modrm->mod == 0) {
377 base_addr = gprs->rbp;
381 base_addr = gprs->rbx;
387 if (mod_mode == DISP8) {
388 base_addr += (uchar_t)*(instr_cursor);
390 } else if (mod_mode == DISP16) {
391 base_addr += (ushort_t)*(instr_cursor);
395 *first_operand = base_addr;
398 *offset += (instr_cursor - modrm_instr);
399 *second_operand = decode_register(gprs, modrm->reg, reg_size);
406 static inline operand_type_t decode_operands32(struct v3_gprs * gprs, // input/output
407 char * modrm_instr, // input
408 int * offset, // output
409 addr_t * first_operand, // output
410 addr_t * second_operand, // output
411 reg_size_t reg_size) { // input
413 char * instr_cursor = modrm_instr;
414 struct modrm_byte * modrm = (struct modrm_byte *)modrm_instr;
415 addr_t base_addr = 0;
416 modrm_mode_t mod_mode = 0;
417 uint_t has_sib_byte = 0;
418 operand_type_t addr_type = INVALID_OPERAND;
424 if (modrm->mod == 3) {
426 addr_type = REG_OPERAND;
428 // PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
430 *first_operand = decode_register(gprs, modrm->rm, reg_size);
434 addr_type = MEM_OPERAND;
436 if (modrm->mod == 0) {
438 } else if (modrm->mod == 1) {
440 } else if (modrm->mod == 2) {
446 base_addr = gprs->rax;
449 base_addr = gprs->rcx;
452 base_addr = gprs->rdx;
455 base_addr = gprs->rbx;
461 if (modrm->mod == 0) {
465 base_addr = gprs->rbp;
469 base_addr = gprs->rsi;
472 base_addr = gprs->rdi;
478 struct sib_byte * sib = (struct sib_byte *)(instr_cursor);
484 if (sib->scale == 1) {
486 } else if (sib->scale == 2) {
488 } else if (sib->scale == 3) {
493 switch (sib->index) {
495 base_addr = gprs->rax;
498 base_addr = gprs->rcx;
501 base_addr = gprs->rdx;
504 base_addr = gprs->rbx;
510 base_addr = gprs->rbp;
513 base_addr = gprs->rsi;
516 base_addr = gprs->rdi;
525 base_addr += gprs->rax;
528 base_addr += gprs->rcx;
531 base_addr += gprs->rdx;
534 base_addr += gprs->rbx;
537 base_addr += gprs->rsp;
540 if (modrm->mod != 0) {
541 base_addr += gprs->rbp;
545 base_addr += gprs->rsi;
548 base_addr += gprs->rdi;
555 if (mod_mode == DISP8) {
556 base_addr += (uchar_t)*(instr_cursor);
558 } else if (mod_mode == DISP32) {
559 base_addr += (uint_t)*(instr_cursor);
564 *first_operand = base_addr;
567 *offset += (instr_cursor - modrm_instr);
569 *second_operand = decode_register(gprs, modrm->reg, reg_size);