1 #include <palacios/svm_handler.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vm_guest_mem.h>
4 #include <palacios/vmm_decoder.h>
5 #include <palacios/vmm_ctrl_regs.h>
6 #include <palacios/svm_io.h>
7 #include <palacios/svm_halt.h>
8 #include <palacios/vmm_intr.h>
11 int handle_svm_exit(struct guest_info * info) {
12 vmcb_ctrl_t * guest_ctrl = 0;
13 vmcb_saved_state_t * guest_state = 0;
14 ulong_t exit_code = 0;
16 guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
17 guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
20 // Update the high level state
21 info->rip = guest_state->rip;
22 info->vm_regs.rsp = guest_state->rsp;
23 info->vm_regs.rax = guest_state->rax;
25 info->cpl = guest_state->cpl;
28 info->ctrl_regs.cr0 = guest_state->cr0;
29 info->ctrl_regs.cr2 = guest_state->cr2;
30 info->ctrl_regs.cr3 = guest_state->cr3;
31 info->ctrl_regs.cr4 = guest_state->cr4;
32 info->ctrl_regs.cr8 = guest_ctrl->guest_ctrl.V_TPR;
33 info->ctrl_regs.rflags = guest_state->rflags;
35 get_vmcb_segments((vmcb_t*)(info->vmm_data), &(info->segments));
38 exit_code = guest_ctrl->exit_code;
41 // Disable printing io exits due to bochs debug messages
42 //if (!((exit_code == VMEXIT_IOIO) && ((ushort_t)(guest_ctrl->exit_info1 >> 16) == 0x402))) {
44 PrintDebug("SVM Returned: Exit Code: %x \t\t(tsc=%ul)\n",exit_code, (uint_t)info->time_state.guest_tsc);
46 // PrintDebugVMCB((vmcb_t*)(info->vmm_data));
49 // PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data);
50 //PrintDebug("RIP: %x\n", guest_state->rip);
53 //PrintDebug("SVM Returned: Exit Code: %x\n",exit_code);
55 if (exit_code == VMEXIT_IOIO) {
56 struct svm_io_info * io_info = (struct svm_io_info *)&(guest_ctrl->exit_info1);
58 if (io_info->type == 0) {
60 if (handle_svm_io_outs(info) == -1 ) {
64 if (handle_svm_io_out(info) == -1) {
70 if (handle_svm_io_ins(info) == -1) {
74 if (handle_svm_io_in(info) == -1) {
79 } else if (exit_code == VMEXIT_CR0_WRITE) {
80 PrintDebug("CR0 Write\n");
82 if (handle_cr0_write(info) == -1) {
85 } else if (exit_code == VMEXIT_CR0_READ) {
86 PrintDebug("CR0 Read\n");
88 if (handle_cr0_read(info) == -1) {
91 } else if (exit_code == VMEXIT_CR3_WRITE) {
92 PrintDebug("CR3 Write\n");
94 if (handle_cr3_write(info) == -1) {
97 } else if (exit_code == VMEXIT_CR3_READ) {
98 PrintDebug("CR3 Read\n");
100 if (handle_cr3_read(info) == -1) {
104 } else if (exit_code == VMEXIT_EXCP14) {
105 addr_t fault_addr = guest_ctrl->exit_info2;
106 pf_error_t * error_code = (pf_error_t *)&(guest_ctrl->exit_info1);
108 PrintDebug("PageFault at %x (error=%d)\n", fault_addr, *error_code);
110 if (info->shdw_pg_mode == SHADOW_PAGING) {
111 if (handle_shadow_pagefault(info, fault_addr, *error_code) == -1) {
115 PrintDebug("Page fault in un implemented paging mode\n");
119 } else if (exit_code == VMEXIT_INVLPG) {
120 if (info->shdw_pg_mode == SHADOW_PAGING) {
121 PrintDebug("Invlpg\n");
122 if (handle_shadow_invlpg(info) == -1) {
128 (exit_code == VMEXIT_INVLPGA) ||
131 } else if (exit_code == VMEXIT_INTR) {
133 // handle_svm_intr(info);
135 } else if (exit_code == VMEXIT_HLT) {
136 PrintDebug("Guest halted\n");
137 if (handle_svm_halt(info) == -1) {
146 rip_addr = get_addr_linear(info, guest_state->rip, &(info->segments.cs));
150 PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data);
151 PrintDebug("RIP: %x\n", guest_state->rip);
152 PrintDebug("RIP Linear: %x\n", rip_addr);
154 PrintDebug("SVM Returned: Exit Code: %x\n",exit_code);
156 PrintDebug("io_info1 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info1));
157 PrintDebug("io_info1 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info1)) + 4));
159 PrintDebug("io_info2 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info2));
160 PrintDebug("io_info2 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info2)) + 4));
164 if (info->mem_mode == PHYSICAL_MEM) {
165 if (guest_pa_to_host_pa(info, guest_state->rip, &host_addr) == -1) {
166 PrintDebug("Could not translate guest_state->rip to host address\n");
169 } else if (info->mem_mode == VIRTUAL_MEM) {
170 if (guest_va_to_host_pa(info, guest_state->rip, &host_addr) == -1) {
171 PrintDebug("Could not translate guest_state->rip to host address\n");
175 PrintDebug("Invalid memory mode\n");
179 PrintDebug("Host Address of rip = 0x%x\n", host_addr);
183 PrintDebug("Reading from 0x%x in guest\n", rip_addr);
185 if (info->mem_mode == PHYSICAL_MEM) {
186 read_guest_pa_memory(info, rip_addr, 15, buf);
188 read_guest_va_memory(info, rip_addr, 15, buf);
191 PrintTraceMemDump(buf, 15);
198 // Update the low level state
200 if (intr_pending(info)) {
202 switch (get_intr_type(info)) {
205 uint_t irq = get_intr_number(info);
207 guest_ctrl->EVENTINJ.vector = irq;
208 guest_ctrl->EVENTINJ.valid = 1;
209 guest_ctrl->EVENTINJ.type = SVM_INJECTION_EXTERNAL_INTR;
212 guest_ctrl->guest_ctrl.V_IRQ = 1;
213 guest_ctrl->guest_ctrl.V_INTR_VECTOR = irq;
214 guest_ctrl->guest_ctrl.V_IGN_TPR = 1;
215 guest_ctrl->guest_ctrl.V_INTR_PRIO = 0xf;
217 PrintDebug("Injecting Interrupt %d (EIP=%x)\n", guest_ctrl->guest_ctrl.V_INTR_VECTOR, info->rip);
219 injecting_intr(info, irq, EXTERNAL_IRQ);
224 guest_ctrl->EVENTINJ.type = SVM_INJECTION_NMI;
228 uint_t excp = get_intr_number(info);
230 guest_ctrl->EVENTINJ.type = SVM_INJECTION_EXCEPTION;
232 if (info->intr_state.excp_error_code) {
233 guest_ctrl->EVENTINJ.error_code = info->intr_state.excp_error_code;
234 guest_ctrl->EVENTINJ.ev = 1;
235 PrintDebug("Injecting error code %x\n", guest_ctrl->EVENTINJ.error_code);
238 guest_ctrl->EVENTINJ.vector = excp;
240 guest_ctrl->EVENTINJ.valid = 1;
241 PrintDebug("Injecting Interrupt %d (EIP=%x)\n", guest_ctrl->EVENTINJ.vector, info->rip);
242 injecting_intr(info, excp, EXCEPTION);
246 guest_ctrl->EVENTINJ.type = SVM_INJECTION_SOFT_INTR;
249 guest_ctrl->EVENTINJ.type = SVM_INJECTION_VIRTUAL_INTR;
254 PrintDebug("Attempted to issue an invalid interrupt\n");
261 guest_state->cr0 = info->ctrl_regs.cr0;
262 guest_state->cr2 = info->ctrl_regs.cr2;
263 guest_state->cr3 = info->ctrl_regs.cr3;
264 guest_state->cr4 = info->ctrl_regs.cr4;
265 guest_ctrl->guest_ctrl.V_TPR = info->ctrl_regs.cr8 & 0xff;
266 guest_state->rflags = info->ctrl_regs.rflags;
269 guest_state->cpl = info->cpl;
271 guest_state->rax = info->vm_regs.rax;
272 guest_state->rip = info->rip;
273 guest_state->rsp = info->vm_regs.rsp;
276 set_vmcb_segments((vmcb_t*)(info->vmm_data), &(info->segments));
278 if (exit_code == VMEXIT_INTR) {
279 PrintDebug("INTR ret IP = %x\n", guest_state->rip);