1 #include <palacios/vmm_mem.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmcb.h>
4 #include <palacios/vmm_emulate.h>
5 #include <palacios/vm_guest_mem.h>
6 #include <palacios/vmm_ctrl_regs.h>
9 /* Segmentation is a problem here...
11 * When we get a memory operand, presumably we use the default segment (which is?)
12 * unless an alternate segment was specfied in the prefix...
16 int handle_cr0_write(struct guest_info * info) {
20 if (info->cpu_mode == REAL) {
24 // The real rip address is actually a combination of the rip + CS base
25 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
27 // I think we should inject a GPF into the guest
28 PrintDebug("Could not read instruction (ret=%d)\n", ret);
32 while (is_prefix_byte(instr[index])) {
36 if ((instr[index] == cr_access_byte) &&
37 (instr[index + 1] == lmsw_byte) &&
38 (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
41 addr_t second_operand;
42 struct cr0_real *real_cr0;
43 struct cr0_real *new_cr0;
44 operand_type_t addr_type;
50 real_cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
52 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
55 if (addr_type == REG_OPERAND) {
56 new_cr0 = (struct cr0_real *)first_operand;
57 } else if (addr_type == MEM_OPERAND) {
60 if (guest_pa_to_host_va(info, first_operand + (info->segments.ds.base << 4), &host_addr) == -1) {
65 new_cr0 = (struct cr0_real *)host_addr;
67 // error... don't know what to do
71 if ((new_cr0->pe == 1) && (real_cr0->pe == 0)) {
72 info->cpu_mode = PROTECTED;
73 } else if ((new_cr0->pe == 0) && (real_cr0->pe == 1)) {
74 info->cpu_mode = REAL;
77 new_cr0_val = *(char*)(new_cr0) & 0x0f;
80 if (info->page_mode == SHADOW_PAGING) {
81 struct cr0_real * shadow_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
83 PrintDebug("Old CR0=%x, Old Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
84 /* struct cr0_real is only 4 bits wide,
85 * so we can overwrite the real_cr0 without worrying about the shadow fields
87 *(char*)real_cr0 &= 0xf0;
88 *(char*)real_cr0 |= new_cr0_val;
90 *(char*)shadow_cr0 &= 0xf0;
91 *(char*)shadow_cr0 |= new_cr0_val;
93 PrintDebug("New CR0=%x, New Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
95 PrintDebug("Old CR0=%x\n", *real_cr0);
96 // for now we just pass through....
97 *(char*)real_cr0 &= 0xf0;
98 *(char*)real_cr0 |= new_cr0_val;
100 PrintDebug("New CR0=%x\n", *real_cr0);
106 } else if ((instr[index] == cr_access_byte) &&
107 (instr[index + 1] == clts_byte)) {
111 } else if ((instr[index] == cr_access_byte) &&
112 (instr[index + 1] = mov_to_cr_byte)) {
113 addr_t first_operand;
114 addr_t second_operand;
115 struct cr0_32 *real_cr0;
116 struct cr0_32 *new_cr0;
117 operand_type_t addr_type;
122 real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
124 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
126 if (addr_type != REG_OPERAND) {
127 /* Mov to CR0 Can only be a 32 bit register */
132 new_cr0 = (struct cr0_32 *)first_operand;
134 if (new_cr0->pe == 1) {
135 PrintDebug("Entering Protected Mode\n");
136 info->cpu_mode = PROTECTED;
139 if (new_cr0->pg == 1) {
144 if (info->page_mode == SHADOW_PAGING) {
145 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
147 PrintDebug("Old CR0=%x, Old Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
148 *real_cr0 = *new_cr0;
151 *shadow_cr0 = *new_cr0;
153 PrintDebug("New CR0=%x, New Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
155 PrintDebug("Old CR0=%x\n", *real_cr0);
156 *real_cr0 = *new_cr0;
157 PrintDebug("New CR0=%x\n", *real_cr0);
163 PrintDebug("Unsupported Instruction\n");
164 // unsupported instruction, UD the guest
169 } else if (info->cpu_mode == PROTECTED) {
173 PrintDebug("Protected Mode write to CR0\n");
175 // The real rip address is actually a combination of the rip + CS base
176 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
178 // I think we should inject a GPF into the guest
179 PrintDebug("Could not read instruction (ret=%d)\n", ret);
183 while (is_prefix_byte(instr[index])) {
187 if ((instr[index] == cr_access_byte) &&
188 (instr[index + 1] == mov_to_cr_byte)) {
190 addr_t first_operand;
191 addr_t second_operand;
192 struct cr0_32 *real_cr0;
193 struct cr0_32 *new_cr0;
194 operand_type_t addr_type;
198 real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
200 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
202 if (addr_type != REG_OPERAND) {
206 new_cr0 = (struct cr0_32 *)first_operand;
209 if (info->page_mode == SHADOW_PAGING) {
210 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
212 if (new_cr0->pg == 1){
213 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
215 info->cpu_mode = PROTECTED_PG;
217 *shadow_cr0 = *new_cr0;
218 *real_cr0 = *new_cr0;
221 // Activate Shadow Paging
223 PrintDebug("Turning on paging in the guest\n");
225 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
228 } else if (new_cr0->pe == 0) {
229 info->cpu_mode = REAL;
231 *shadow_cr0 = *new_cr0;
232 *real_cr0 = *new_cr0;
238 *real_cr0 = *new_cr0;
245 PrintDebug("Unknown Mode write to CR0\n");
252 int handle_cr0_read(struct guest_info * info) {
255 if (info->cpu_mode == REAL) {
259 // The real rip address is actually a combination of the rip + CS base
260 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
262 // I think we should inject a GPF into the guest
263 PrintDebug("Could not read Real Mode instruction (ret=%d)\n", ret);
268 while (is_prefix_byte(instr[index])) {
272 if ((instr[index] == cr_access_byte) &&
273 (instr[index + 1] == smsw_byte) &&
274 (MODRM_REG(instr[index + 2]) == smsw_reg_byte)) {
276 addr_t first_operand;
277 addr_t second_operand;
278 struct cr0_real *cr0;
279 operand_type_t addr_type;
284 cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
287 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
289 if (addr_type == MEM_OPERAND) {
292 if (guest_pa_to_host_va(info, first_operand + (info->segments.ds.base << 4), &host_addr) == -1) {
297 first_operand = host_addr;
299 // error... don't know what to do
303 cr0_val = *(char*)cr0 & 0x0f;
305 *(char *)first_operand &= 0xf0;
306 *(char *)first_operand |= cr0_val;
308 PrintDebug("index = %d, rip = %x\n", index, (ulong_t)(info->rip));
310 PrintDebug("new_rip = %x\n", (ulong_t)(info->rip));
311 } else if ((instr[index] == cr_access_byte) &&
312 (instr[index+1] == mov_from_cr_byte)) {
314 * This can only take a 32 bit register argument in anything less than 64 bit mode.
316 addr_t first_operand;
317 addr_t second_operand;
318 operand_type_t addr_type;
320 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
324 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
326 struct cr0_32 * virt_cr0 = (struct cr0_32 *)first_operand;
328 if (addr_type != REG_OPERAND) {
329 // invalid opcode to guest
330 PrintDebug("Invalid operand type in mov from CR0\n");
334 if (info->page_mode == SHADOW_PAGING) {
335 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
337 *virt_cr0 = *real_cr0;
343 PrintDebug("Unknown read instr from CR0\n");
347 } else if (info->cpu_mode == PROTECTED) {
351 // The real rip address is actually a combination of the rip + CS base
352 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
354 // I think we should inject a GPF into the guest
355 PrintDebug("Could not read Proteced mode instruction (ret=%d)\n", ret);
359 while (is_prefix_byte(instr[index])) {
364 if ((instr[index] == cr_access_byte) &&
365 (instr[index+1] == mov_from_cr_byte)) {
366 addr_t first_operand;
367 addr_t second_operand;
368 operand_type_t addr_type;
369 struct cr0_32 * virt_cr0;
370 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
374 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
376 if (addr_type != REG_OPERAND) {
377 PrintDebug("Invalid operand type in mov from CR0\n");
381 virt_cr0 = (struct cr0_32 *)first_operand;
383 if (info->page_mode == SHADOW_PAGING) {
384 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
386 *virt_cr0 = *real_cr0;
392 PrintDebug("Unknown read instruction from CR0\n");
397 PrintDebug("Unknown mode read from CR0\n");
408 int handle_cr3_write(struct guest_info * info) {
409 if ((info->cpu_mode == PROTECTED) || (info->cpu_mode == PROTECTED_PG)) {
415 /* Isn't the RIP a Guest Virtual Address???????? */
416 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
418 PrintDebug("Could not read instruction (ret=%d)\n", ret);
422 while (is_prefix_byte(instr[index])) {
426 if ((instr[index] == cr_access_byte) &&
427 (instr[index + 1] == mov_to_cr_byte)) {
429 addr_t first_operand;
430 addr_t second_operand;
431 struct cr3_32 * new_cr3;
432 // struct cr3_32 * real_cr3;
433 operand_type_t addr_type;
437 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
439 if (addr_type != REG_OPERAND) {
440 /* Mov to CR3 can only be a 32 bit register */
444 new_cr3 = (struct cr3_32 *)first_operand;
446 if (info->page_mode == SHADOW_PAGING) {
448 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
449 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
452 *guest_cr3 = *new_cr3;
454 // Something like this
455 shadow_pt = create_new_shadow_pt32(info);
456 //shadow_pt = setup_shadow_pt32(info, CR3_TO_PDE32(*(addr_t *)new_cr3));
458 /* Copy Various flags */
459 *shadow_cr3 = *new_cr3;
461 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
463 if (info->cpu_mode == PROTECTED_PG) {
464 // If we aren't in paged mode then we have to preserve the identity mapped CR3
465 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
472 PrintDebug("Unknown Instruction\n");
476 PrintDebug("Invalid operating Mode\n");
486 int handle_cr3_read(struct guest_info * info) {
487 if ((info->cpu_mode == PROTECTED) || (info->cpu_mode == PROTECTED_PG)) {
492 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
494 PrintDebug("Could not read instruction (ret=%d)\n", ret);
498 while (is_prefix_byte(instr[index])) {
502 if ((instr[index] == cr_access_byte) &&
503 (instr[index + 1] == mov_from_cr_byte)) {
504 addr_t first_operand;
505 addr_t second_operand;
506 struct cr3_32 * virt_cr3;
507 struct cr3_32 * real_cr3 = (struct cr3_32 *)&(info->ctrl_regs.cr3);
508 operand_type_t addr_type;
512 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
514 if (addr_type != REG_OPERAND) {
515 /* Mov to CR3 can only be a 32 bit register */
519 virt_cr3 = (struct cr3_32 *)first_operand;
521 if (info->page_mode == SHADOW_PAGING) {
522 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
524 *virt_cr3 = *real_cr3;
529 PrintDebug("Unknown Instruction\n");
533 PrintDebug("Invalid operating Mode\n");