1 #ifndef __VMM_EMULATE_H
2 #define __VMM_EMULATE_H
6 #include <palacios/vm_guest.h>
7 #include <palacios/vmm.h>
10 * This is where we do the hideous X86 instruction parsing among other things
11 * We can parse out the instruction prefixes, as well as decode the operands
18 typedef enum {INVALID_OPERAND, REG_OPERAND, MEM_OPERAND} operand_type_t;
26 struct x86_prefix_list {
27 uint_t lock : 1; // 0xF0
28 uint_t repne : 1; // 0xF2
29 uint_t repnz : 1; // 0xF2
30 uint_t rep : 1; // 0xF3
31 uint_t repe : 1; // 0xF3
32 uint_t repz : 1; // 0xF3
33 uint_t cs_override : 1; // 0x2E
34 uint_t ss_override : 1; // 0x36
35 uint_t ds_override : 1; // 0x3E
36 uint_t es_override : 1; // 0x26
37 uint_t fs_override : 1; // 0x64
38 uint_t gs_override : 1; // 0x65
39 uint_t br_not_taken : 1; // 0x2E
40 uint_t br_takend : 1; // 0x3E
41 uint_t op_size : 1; // 0x66
42 uint_t addr_size : 1; // 0x67
47 uint_t instr_length; // output
48 addr_t opcode; // output
49 uint_t opcode_length; // output
50 struct x86_prefix_list prefixes; // output
51 struct x86_operand src_operand; // output
52 struct x86_operand dst_operand; // output
53 struct x86_operand extra_operand;
57 /************************/
58 /* EXTERNAL DECODER API */
59 /************************/
61 This is an External API definition that must be implemented by a decoder
66 * Initializes a decoder
71 * Decodes an instruction
72 * All addresses in arguments are in the host address space
73 * instr_ptr is the host address of the instruction
74 * IMPORTANT: make sure the instr_ptr is in contiguous host memory
75 * ie. Copy it to a buffer before the call
77 int v3_decode(struct guest_info * info, addr_t instr_ptr, struct x86_instr * instr);
80 * Encodes an instruction
81 * All addresses in arguments are in the host address space
82 * The instruction is encoded from the struct, and copied into a 15 byte host buffer
83 * referenced by instr_buf
84 * any unused bytes at the end of instr_buf will be filled with nops
85 * IMPORTANT: instr_buf must be allocated and 15 bytes long
87 int v3_encode(struct guest_info * info, struct x86_instr * instr, char * instr_buf);
98 * JRL: Some of this was taken from the Xen sources...
101 #define PACKED __attribute__((packed))
103 #define MODRM_MOD(x) ((x >> 6) & 0x3)
104 #define MODRM_REG(x) ((x >> 3) & 0x7)
105 #define MODRM_RM(x) (x & 0x7)
108 uint_t rm : 3 PACKED;
109 uint_t reg : 3 PACKED;
110 uint_t mod : 2 PACKED;
114 #define SIB_BASE(x) ((x >> 6) & 0x3)
115 #define SIB_INDEX(x) ((x >> 3) & 0x7)
116 #define SIB_SCALE(x) (x & 0x7)
119 uint_t base : 3 PACKED;
120 uint_t index : 3 PACKED;
121 uint_t scale : 2 PACKED;
126 #define MAKE_INSTR(nm, ...) static const uchar_t OPCODE_##nm[] = { __VA_ARGS__ }
129 * Here's how it works:
130 * First byte: Length.
131 * Following bytes: Opcode bytes.
132 * Special case: Last byte, if zero, doesn't need to match.
134 MAKE_INSTR(INVD, 2, 0x0f, 0x08);
135 MAKE_INSTR(CPUID, 2, 0x0f, 0xa2);
136 MAKE_INSTR(RDMSR, 2, 0x0f, 0x32);
137 MAKE_INSTR(WRMSR, 2, 0x0f, 0x30);
138 MAKE_INSTR(RDTSC, 2, 0x0f, 0x31);
139 MAKE_INSTR(RDTSCP, 3, 0x0f, 0x01, 0xf9);
140 MAKE_INSTR(CLI, 1, 0xfa);
141 MAKE_INSTR(STI, 1, 0xfb);
142 MAKE_INSTR(RDPMC, 2, 0x0f, 0x33);
143 MAKE_INSTR(CLGI, 3, 0x0f, 0x01, 0xdd);
144 MAKE_INSTR(STGI, 3, 0x0f, 0x01, 0xdc);
145 MAKE_INSTR(VMRUN, 3, 0x0f, 0x01, 0xd8);
146 MAKE_INSTR(VMLOAD, 3, 0x0f, 0x01, 0xda);
147 MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, 0xdb);
148 MAKE_INSTR(VMCALL, 3, 0x0f, 0x01, 0xd9);
149 MAKE_INSTR(PAUSE, 2, 0xf3, 0x90);
150 MAKE_INSTR(SKINIT, 3, 0x0f, 0x01, 0xde);
151 MAKE_INSTR(MOV2CR, 3, 0x0f, 0x22, 0x00);
152 MAKE_INSTR(MOVCR2, 3, 0x0f, 0x20, 0x00);
153 MAKE_INSTR(MOV2DR, 3, 0x0f, 0x23, 0x00);
154 MAKE_INSTR(MOVDR2, 3, 0x0f, 0x21, 0x00);
155 MAKE_INSTR(PUSHF, 1, 0x9c);
156 MAKE_INSTR(POPF, 1, 0x9d);
157 MAKE_INSTR(RSM, 2, 0x0f, 0xaa);
158 MAKE_INSTR(INVLPG, 3, 0x0f, 0x01, 0x00);
159 MAKE_INSTR(INVLPGA,3, 0x0f, 0x01, 0xdf);
160 MAKE_INSTR(HLT, 1, 0xf4);
161 MAKE_INSTR(CLTS, 2, 0x0f, 0x06);
162 MAKE_INSTR(LMSW, 3, 0x0f, 0x01, 0x00);
163 MAKE_INSTR(SMSW, 3, 0x0f, 0x01, 0x00);
166 #define PREFIX_LOCK 0xF0
167 #define PREFIX_REPNE 0xF2
168 #define PREFIX_REPNZ 0xF2
169 #define PREFIX_REP 0xF3
170 #define PREFIX_REPE 0xF3
171 #define PREFIX_REPZ 0xF3
172 #define PREFIX_CS_OVERRIDE 0x2E
173 #define PREFIX_SS_OVERRIDE 0x36
174 #define PREFIX_DS_OVERRIDE 0x3E
175 #define PREFIX_ES_OVERRIDE 0x26
176 #define PREFIX_FS_OVERRIDE 0x64
177 #define PREFIX_GS_OVERRIDE 0x65
178 #define PREFIX_BR_NOT_TAKEN 0x2E
179 #define PREFIX_BR_TAKEN 0x3E
180 #define PREFIX_OP_SIZE 0x66
181 #define PREFIX_ADDR_SIZE 0x67
183 static inline int is_prefix_byte(char byte) {
186 case 0xF2: // REPNE/REPNZ
187 case 0xF3: // REP or REPE/REPZ
188 case 0x2E: // CS override or Branch hint not taken (with Jcc instrs)
189 case 0x36: // SS override
190 case 0x3E: // DS override or Branch hint taken (with Jcc instrs)
191 case 0x26: // ES override
192 case 0x64: // FS override
193 case 0x65: // GS override
194 //case 0x2E: // branch not taken hint
195 // case 0x3E: // branch taken hint
196 case 0x66: // operand size override
197 case 0x67: // address size override
207 static inline v3_reg_t get_gpr_mask(struct guest_info * info) {
208 switch (info->cpu_mode) {
221 static inline addr_t get_addr_linear(struct guest_info * info, addr_t addr, struct v3_segment * seg) {
222 switch (info->cpu_mode) {
224 // It appears that the segment values are computed and cached in the vmcb structure
225 // We Need to check this for Intel
226 /* return addr + (seg->selector << 4);
230 return addr + seg->base;
239 typedef enum {INVALID_ADDR_TYPE, REG, DISP0, DISP8, DISP16, DISP32} modrm_mode_t;
240 typedef enum {INVALID_REG_SIZE, REG64, REG32, REG16, REG8} reg_size_t;
249 static inline addr_t decode_register(struct v3_gprs * gprs, char reg_code, reg_size_t reg_size) {
254 reg_addr = (addr_t)&(gprs->rax);
257 reg_addr = (addr_t)&(gprs->rcx);
260 reg_addr = (addr_t)&(gprs->rdx);
263 reg_addr = (addr_t)&(gprs->rbx);
266 if (reg_size == REG8) {
267 reg_addr = (addr_t)&(gprs->rax) + 1;
269 reg_addr = (addr_t)&(gprs->rsp);
273 if (reg_size == REG8) {
274 reg_addr = (addr_t)&(gprs->rcx) + 1;
276 reg_addr = (addr_t)&(gprs->rbp);
280 if (reg_size == REG8) {
281 reg_addr = (addr_t)&(gprs->rdx) + 1;
283 reg_addr = (addr_t)&(gprs->rsi);
287 if (reg_size == REG8) {
288 reg_addr = (addr_t)&(gprs->rbx) + 1;
290 reg_addr = (addr_t)&(gprs->rdi);
303 static inline operand_type_t decode_operands16(struct v3_gprs * gprs, // input/output
304 char * modrm_instr, // input
305 int * offset, // output
306 addr_t * first_operand, // output
307 addr_t * second_operand, // output
308 reg_size_t reg_size) { // input
310 struct modrm_byte * modrm = (struct modrm_byte *)modrm_instr;
311 addr_t base_addr = 0;
312 modrm_mode_t mod_mode = 0;
313 operand_type_t addr_type = INVALID_OPERAND;
314 char * instr_cursor = modrm_instr;
316 // PrintDebug("ModRM mod=%d\n", modrm->mod);
320 if (modrm->mod == 3) {
322 addr_type = REG_OPERAND;
323 //PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
325 *first_operand = decode_register(gprs, modrm->rm, reg_size);
329 addr_type = MEM_OPERAND;
331 if (modrm->mod == 0) {
333 } else if (modrm->mod == 1) {
335 } else if (modrm->mod == 2) {
341 base_addr = gprs->rbx + gprs->rsi;
344 base_addr = gprs->rbx + gprs->rdi;
347 base_addr = gprs->rbp + gprs->rsi;
350 base_addr = gprs->rbp + gprs->rdi;
353 base_addr = gprs->rsi;
356 base_addr = gprs->rdi;
359 if (modrm->mod == 0) {
363 base_addr = gprs->rbp;
367 base_addr = gprs->rbx;
373 if (mod_mode == DISP8) {
374 base_addr += (uchar_t)*(instr_cursor);
376 } else if (mod_mode == DISP16) {
377 base_addr += (ushort_t)*(instr_cursor);
381 *first_operand = base_addr;
384 *offset += (instr_cursor - modrm_instr);
385 *second_operand = decode_register(gprs, modrm->reg, reg_size);
392 static inline operand_type_t decode_operands32(struct v3_gprs * gprs, // input/output
393 char * modrm_instr, // input
394 int * offset, // output
395 addr_t * first_operand, // output
396 addr_t * second_operand, // output
397 reg_size_t reg_size) { // input
399 char * instr_cursor = modrm_instr;
400 struct modrm_byte * modrm = (struct modrm_byte *)modrm_instr;
401 addr_t base_addr = 0;
402 modrm_mode_t mod_mode = 0;
403 uint_t has_sib_byte = 0;
404 operand_type_t addr_type = INVALID_OPERAND;
410 if (modrm->mod == 3) {
412 addr_type = REG_OPERAND;
414 // PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
416 *first_operand = decode_register(gprs, modrm->rm, reg_size);
420 addr_type = MEM_OPERAND;
422 if (modrm->mod == 0) {
424 } else if (modrm->mod == 1) {
426 } else if (modrm->mod == 2) {
432 base_addr = gprs->rax;
435 base_addr = gprs->rcx;
438 base_addr = gprs->rdx;
441 base_addr = gprs->rbx;
447 if (modrm->mod == 0) {
451 base_addr = gprs->rbp;
455 base_addr = gprs->rsi;
458 base_addr = gprs->rdi;
464 struct sib_byte * sib = (struct sib_byte *)(instr_cursor);
470 if (sib->scale == 1) {
472 } else if (sib->scale == 2) {
474 } else if (sib->scale == 3) {
479 switch (sib->index) {
481 base_addr = gprs->rax;
484 base_addr = gprs->rcx;
487 base_addr = gprs->rdx;
490 base_addr = gprs->rbx;
496 base_addr = gprs->rbp;
499 base_addr = gprs->rsi;
502 base_addr = gprs->rdi;
511 base_addr += gprs->rax;
514 base_addr += gprs->rcx;
517 base_addr += gprs->rdx;
520 base_addr += gprs->rbx;
523 base_addr += gprs->rsp;
526 if (modrm->mod != 0) {
527 base_addr += gprs->rbp;
531 base_addr += gprs->rsi;
534 base_addr += gprs->rdi;
541 if (mod_mode == DISP8) {
542 base_addr += (uchar_t)*(instr_cursor);
544 } else if (mod_mode == DISP32) {
545 base_addr += (uint_t)*(instr_cursor);
550 *first_operand = base_addr;
553 *offset += (instr_cursor - modrm_instr);
555 *second_operand = decode_register(gprs, modrm->reg, reg_size);