1 #ifndef __VMM_EMULATE_H
2 #define __VMM_EMULATE_H
3 #include <geekos/vm_guest.h>
7 * This is where we do the hideous X86 instruction parsing among other things
8 * We can parse out the instruction prefixes, as well as decode the operands
10 * Before we begin I'd just like to say a few words to those that made this possible...
41 /* JRL: Some of this was taken from the Xen sources...
45 #define PACKED __attribute__((packed))
47 #define MODRM_MOD(x) ((x >> 6) & 0x3)
48 #define MODRM_REG(x) ((x >> 3) & 0x7)
49 #define MODRM_RM(x) (x & 0x07)
53 uint_t reg : 3 PACKED;
54 uint_t mod : 2 PACKED;
59 uint_t base : 3 PACKED;
60 uint_t index : 3 PACKED;
61 uint_t scale : 2 PACKED;
66 #define MAKE_INSTR(nm, ...) static const uchar_t OPCODE_##nm[] = { __VA_ARGS__ }
69 * Here's how it works:
71 * Following bytes: Opcode bytes.
72 * Special case: Last byte, if zero, doesn't need to match.
74 MAKE_INSTR(INVD, 2, 0x0f, 0x08);
75 MAKE_INSTR(CPUID, 2, 0x0f, 0xa2);
76 MAKE_INSTR(RDMSR, 2, 0x0f, 0x32);
77 MAKE_INSTR(WRMSR, 2, 0x0f, 0x30);
78 MAKE_INSTR(RDTSC, 2, 0x0f, 0x31);
79 MAKE_INSTR(RDTSCP, 3, 0x0f, 0x01, 0xf9);
80 MAKE_INSTR(CLI, 1, 0xfa);
81 MAKE_INSTR(STI, 1, 0xfb);
82 MAKE_INSTR(RDPMC, 2, 0x0f, 0x33);
83 MAKE_INSTR(CLGI, 3, 0x0f, 0x01, 0xdd);
84 MAKE_INSTR(STGI, 3, 0x0f, 0x01, 0xdc);
85 MAKE_INSTR(VMRUN, 3, 0x0f, 0x01, 0xd8);
86 MAKE_INSTR(VMLOAD, 3, 0x0f, 0x01, 0xda);
87 MAKE_INSTR(VMSAVE, 3, 0x0f, 0x01, 0xdb);
88 MAKE_INSTR(VMCALL, 3, 0x0f, 0x01, 0xd9);
89 MAKE_INSTR(PAUSE, 2, 0xf3, 0x90);
90 MAKE_INSTR(SKINIT, 3, 0x0f, 0x01, 0xde);
91 MAKE_INSTR(MOV2CR, 3, 0x0f, 0x22, 0x00);
92 MAKE_INSTR(MOVCR2, 3, 0x0f, 0x20, 0x00);
93 MAKE_INSTR(MOV2DR, 3, 0x0f, 0x23, 0x00);
94 MAKE_INSTR(MOVDR2, 3, 0x0f, 0x21, 0x00);
95 MAKE_INSTR(PUSHF, 1, 0x9c);
96 MAKE_INSTR(POPF, 1, 0x9d);
97 MAKE_INSTR(RSM, 2, 0x0f, 0xaa);
98 MAKE_INSTR(INVLPG, 3, 0x0f, 0x01, 0x00);
99 MAKE_INSTR(INVLPGA,3, 0x0f, 0x01, 0xdf);
100 MAKE_INSTR(HLT, 1, 0xf4);
101 MAKE_INSTR(CLTS, 2, 0x0f, 0x06);
102 MAKE_INSTR(LMSW, 3, 0x0f, 0x01, 0x00);
103 MAKE_INSTR(SMSW, 3, 0x0f, 0x01, 0x00);
107 static inline int is_prefix_byte(char byte) {
110 case 0xF2: // REPNE/REPNZ
111 case 0xF3: // REP or REPE/REPZ
112 case 0x2E: // CS override or Branch hint not taken (with Jcc instrs)
113 case 0x36: // SS override
114 case 0x3E: // DS override or Branch hint taken (with Jcc instrs)
115 case 0x26: // ES override
116 case 0x64: // FS override
117 case 0x65: // GS override
118 //case 0x2E: // branch not taken hint
119 // case 0x3E: // branch taken hint
120 case 0x66: // operand size override
121 case 0x67: // address size override
130 typedef enum {INVALID_ADDR_TYPE, REG, DISP0, DISP8, DISP16, DISP32} modrm_addr_type_t;
131 typedef enum {INVALID_REG_SIZE, REG64, REG32, REG16, REG8} reg_size_t;
132 typedef enum {INVALID_OPERAND_TYPE, REG_TO_REG, REG_TO_MEM, MEM_TO_REG} operand_type_t;
137 static inline int decode_operands16(struct guest_gprs * gprs,
139 addr_t * first_operand,
140 addr_t * second_operand,
141 reg_size_t reg_size) {
143 struct modrm_byte * modrm = (struct modrm_byte*)modrm_instr;
144 addr_t base_addr = 0;
145 modrm_addr_type_t mod_type = 0;
147 PrintDebug("ModRM mod=%d\n", modrm->mod);
149 if (modrm->mod == 3) {
152 PrintDebug("first operand = Register (RM=%d)\n",modrm->rm);
156 PrintDebug("EAX Operand\n");
157 *first_operand = (addr_t)&(gprs->rax);
160 *first_operand = (addr_t)&(gprs->rcx);
163 *first_operand = (addr_t)&(gprs->rdx);
166 *first_operand = (addr_t)&(gprs->rbx);
169 if (reg_size == REG8) {
170 *first_operand = (addr_t)(&(gprs->rax) + 1);
172 *first_operand = (addr_t)&(gprs->rsp);
176 if (reg_size == REG8) {
177 *first_operand = (addr_t)(&(gprs->rcx) + 1);
179 *first_operand = (addr_t)&(gprs->rbp);
183 if (reg_size == REG8) {
184 *first_operand = (addr_t)(&(gprs->rdx) + 1);
186 *first_operand = (addr_t)&(gprs->rsi);
190 if (reg_size == REG8) {
191 *first_operand = (addr_t)(&(gprs->rbx) + 1);
193 *first_operand = (addr_t)&(gprs->rdi);
199 if (modrm->mod == 0) {
201 } else if (modrm->mod == 1) {
203 } else if (modrm->mod == 2) {
209 base_addr = gprs->rbx + gprs->rsi;
211 base_addr = gprs->rbx + gprs->rdi;
213 base_addr = gprs->rbp + gprs->rsi;
215 base_addr = gprs->rbp + gprs->rdi;
217 base_addr = gprs->rsi;
219 base_addr = gprs->rdi;
221 if (modrm->mod == 0) {
225 base_addr = gprs->rbp;
228 base_addr = gprs->rbx;
231 if (mod_type == DISP8) {
232 base_addr += (uchar_t)*(modrm_instr + 1);
233 } else if (mod_type == DISP16) {
234 base_addr += (ushort_t)*(modrm_instr + 1);
238 *first_operand = base_addr;
243 switch (modrm->reg) {
245 *second_operand = (addr_t)&(gprs->rax);
248 *second_operand = (addr_t)&(gprs->rcx);
251 *second_operand = (addr_t)&(gprs->rdx);
254 *second_operand = (addr_t)&(gprs->rbx);
257 if (reg_size == REG8) {
258 *second_operand = (addr_t)&(gprs->rax) + 1;
260 *second_operand = (addr_t)&(gprs->rsp);
264 if (reg_size == REG8) {
265 *second_operand = (addr_t)&(gprs->rcx) + 1;
267 *second_operand = (addr_t)&(gprs->rbp);
271 if (reg_size == REG8) {
272 *second_operand = (addr_t)&(gprs->rdx) + 1;
274 *second_operand = (addr_t)&(gprs->rsi);
278 if (reg_size == REG8) {
279 *second_operand = (addr_t)&(gprs->rbx) + 1;
281 *second_operand = (addr_t)&(gprs->rdi);