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));
16 if (info->cpu_mode == REAL) {
20 // The real rip address is actually a combination of the rip + CS base
21 ret = read_guest_pa_memory(info, (addr_t)guest_state->rip, 15, instr);
23 // I think we should inject a GPF into the guest
24 PrintDebug("Could not read instruction (ret=%d)\n", ret);
28 while (is_prefix_byte(instr[index])) {
32 if ((instr[index] == cr_access_byte) &&
33 (instr[index + 1] == lmsw_byte) &&
34 (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
37 addr_t second_operand;
38 struct cr0_real *old_cr0;
39 struct cr0_real *new_cr0;
45 old_cr0 = (struct cr0_real*)&(guest_state->cr0);
47 if (decode_operands16(&(info->vm_regs), instr + index, &first_operand, &second_operand, REG16) != 0) {
48 // error... don't know what to do
54 new_cr0 = (struct cr0_real *)first_operand;
56 if ((new_cr0->pe == 1) && (old_cr0->pe == 0)) {
57 info->cpu_mode = PROTECTED;
61 if (info->page_mode == SHADOW_PAGING) {
62 struct cr0_real * virt_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
64 /* struct cr0_real is only 4 bits wide,
65 * so we can overwrite the old_cr0 without worrying about the shadow fields
70 // for now we just pass through....
74 PrintDebug("index = %d, rip = %x\n", index, (ulong_t)(info->rip));
76 PrintDebug("new_rip = %x\n", (ulong_t)(info->rip));
77 } else if ((instr[index] == cr_access_byte) &&
78 (instr[index + 1] == clts_byte)) {
81 // unsupported instruction, UD the guest
86 } else if (info->cpu_mode == PROTECTED) {
87 PrintDebug("Protected Mode write to CR0\n");
90 PrintDebug("Unknown Mode write to CR0\n");