1 #include <geekos/svm_handler.h>
2 #include <geekos/vmm.h>
3 #include <geekos/svm_ctrl_regs.h>
5 extern struct vmm_os_hooks * os_hooks;
8 int handle_svm_exit(struct guest_info * info) {
9 vmcb_ctrl_t * guest_ctrl = 0;
10 vmcb_saved_state_t * guest_state = 0;
11 ulong_t exit_code = 0;
13 guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
14 guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
17 // Update the high level state
18 info->rip = guest_state->rip;
19 info->vm_regs.rsp = guest_state->rsp;
20 info->vm_regs.rax = guest_state->rax;
21 info->vm_regs.rsp = guest_state->rsp;
24 PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data);
25 PrintDebug("RIP: %x\n", guest_state->rip);
29 exit_code = guest_ctrl->exit_code;
31 // PrintDebugVMCB((vmcb_t*)(info->vmm_data));
32 PrintDebug("SVM Returned: Exit Code: %x\n",exit_code);
34 PrintDebug("io_info1 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info1));
35 PrintDebug("io_info1 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info1)) + 4));
37 PrintDebug("io_info2 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info2));
38 PrintDebug("io_info2 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info2)) + 4));
40 if (exit_code == VMEXIT_IOIO) {
43 } else if (exit_code == VMEXIT_CR0_WRITE) {
44 PrintDebug("CR0 Write\n");
46 if (handle_cr0_write(info) == -1) {
50 } else if (( (exit_code == VMEXIT_CR3_READ) ||
51 (exit_code == VMEXIT_CR3_WRITE) ||
52 (exit_code == VMEXIT_INVLPG) ||
53 (exit_code == VMEXIT_INVLPGA) ||
54 (exit_code == VMEXIT_EXCP14)) &&
55 (info->page_mode == SHADOW_PAGING)) {
56 handle_shadow_paging(info);
60 // Update the low level state
61 guest_state->rax = info->vm_regs.rax;
62 guest_state->rip = info->rip;
63 guest_state->rsp = info->vm_regs.rsp;
70 // This should package up an IO request and call vmm_handle_io
71 int handle_svm_io(struct guest_info * info) {
72 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
73 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
75 PrintDebug("Ctrl Area=%x\n", ctrl_area);
77 // struct svm_io_info * io_info = (struct svm_io_info *)&(ctrl_area->exit_info1);
81 // PrintDebugVMCB((vmcb_t*)(info->vmm_data));
83 guest_state->rip = ctrl_area->exit_info2;
88 // PrintDebug("Exit On Port %d\n", io_info->port);
94 int handle_shadow_paging(struct guest_info * info) {
95 vmcb_ctrl_t * guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
96 // vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
98 if (guest_ctrl->exit_code == VMEXIT_CR3_READ) {