1 #include <geekos/svm_handler.h>
2 #include <geekos/vmm.h>
3 #include <geekos/svm_ctrl_regs.h>
4 #include <geekos/svm_io.h>
6 extern struct vmm_os_hooks * os_hooks;
9 int handle_svm_exit(struct guest_info * info) {
10 vmcb_ctrl_t * guest_ctrl = 0;
11 vmcb_saved_state_t * guest_state = 0;
12 ulong_t exit_code = 0;
14 guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
15 guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
18 // Update the high level state
19 info->rip = guest_state->rip;
20 info->vm_regs.rsp = guest_state->rsp;
21 info->vm_regs.rax = guest_state->rax;
22 info->vm_regs.rsp = guest_state->rsp;
25 PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data);
26 PrintDebug("RIP: %x\n", guest_state->rip);
30 exit_code = guest_ctrl->exit_code;
32 // PrintDebugVMCB((vmcb_t*)(info->vmm_data));
33 PrintDebug("SVM Returned: Exit Code: %x\n",exit_code);
35 PrintDebug("io_info1 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info1));
36 PrintDebug("io_info1 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info1)) + 4));
38 PrintDebug("io_info2 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info2));
39 PrintDebug("io_info2 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info2)) + 4));
42 if (exit_code == VMEXIT_IOIO) {
43 struct svm_io_info * io_info = (struct svm_io_info *)&(guest_ctrl->exit_info1);
45 if (io_info->type == 0) {
47 handle_svm_io_outs(info);
49 handle_svm_io_out(info);
53 handle_svm_io_ins(info);
55 handle_svm_io_in(info);
58 } else if (exit_code == VMEXIT_CR0_WRITE) {
59 PrintDebug("CR0 Write\n");
61 if (handle_cr0_write(info) == -1) {
65 } else if (( (exit_code == VMEXIT_CR3_READ) ||
66 (exit_code == VMEXIT_CR3_WRITE) ||
67 (exit_code == VMEXIT_INVLPG) ||
68 (exit_code == VMEXIT_INVLPGA) ||
69 (exit_code == VMEXIT_EXCP14)) &&
70 (info->page_mode == SHADOW_PAGING)) {
71 handle_shadow_paging(info);
75 // Update the low level state
76 guest_state->rax = info->vm_regs.rax;
77 guest_state->rip = info->rip;
78 guest_state->rsp = info->vm_regs.rsp;
86 int handle_shadow_paging(struct guest_info * info) {
87 vmcb_ctrl_t * guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
88 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
90 if (guest_ctrl->exit_code == VMEXIT_CR3_READ) {