1 #include <geekos/svm_ctrl_regs.h>
2 #include <geekos/vmm_mem.h>
3 #include <geekos/vmm.h>
4 #include <geekos/vmcb.h>
5 #include <geekos/vmm_emulate.h>
6 #include <geekos/vm_guest_mem.h>
7 #include <geekos/vmm_ctrl_regs.h>
10 int handle_cr0_write(struct guest_info * info) {
11 //vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
12 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
18 if (info->cpu_mode == REAL) {
22 // The real rip address is actually a combination of the rip + CS base
23 ret = read_guest_pa_memory(info, (addr_t)guest_state->rip, 15, instr);
25 // I think we should inject a GPF into the guest
26 PrintDebug("Could not read instruction (ret=%d)\n", ret);
30 while (is_prefix_byte(instr[index])) {
31 PrintDebug("instr(%d): 0x%x\n", index, instr[index]);
34 PrintDebug("instr(%d): 0x%x\n", index, instr[index]);
35 PrintDebug("instr(%d): 0x%x\n", index+1, instr[index + 1]);
37 if ((instr[index] == cr_access_byte) &&
38 (instr[index + 1] == lmsw_byte) &&
39 (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
42 addr_t second_operand;
43 struct cr0_real *old_cr0;
44 struct cr0_real *new_cr0;
50 old_cr0 = (struct cr0_real*)&(guest_state->cr0);
52 if (decode_operands16(&(info->vm_regs), instr + index, &first_operand, &second_operand, REG16) != 0) {
53 // error... don't know what to do
59 new_cr0 = (struct cr0_real *)first_operand;
61 if ((new_cr0->pe == 1) && (old_cr0->pe == 0)) {
62 info->cpu_mode = PROTECTED;
66 if (info->page_mode == SHADOW_PAGING) {
67 struct cr0_real * virt_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
69 /* struct cr0_real is only 4 bits wide,
70 * so we can overwrite the old_cr0 without worrying about the shadow fields
75 // for now we just pass through....
79 PrintDebug("index = %d, rip = %x\n", index, (ulong_t)(info->rip));
81 PrintDebug("new_rip = %x\n", (ulong_t)(info->rip));
82 } else if ((instr[index] == cr_access_byte) &&
83 (instr[index + 1] == clts_byte)) {
86 // unsupported instruction, UD the guest
91 } else if (info->cpu_mode == PROTECTED) {
92 PrintDebug("Protected Mode write to CR0\n");
95 PrintDebug("Unknown Mode write to CR0\n");