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/svm_pause.h>
9 #include <palacios/vmm_intr.h>
12 int handle_svm_exit(struct guest_info * info) {
13 vmcb_ctrl_t * guest_ctrl = 0;
14 vmcb_saved_state_t * guest_state = 0;
15 ulong_t exit_code = 0;
17 guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
18 guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
21 // Update the high level state
22 info->rip = guest_state->rip;
23 info->vm_regs.rsp = guest_state->rsp;
24 info->vm_regs.rax = guest_state->rax;
26 info->cpl = guest_state->cpl;
29 info->ctrl_regs.cr0 = guest_state->cr0;
30 info->ctrl_regs.cr2 = guest_state->cr2;
31 info->ctrl_regs.cr3 = guest_state->cr3;
32 info->ctrl_regs.cr4 = guest_state->cr4;
33 info->ctrl_regs.cr8 = guest_ctrl->guest_ctrl.V_TPR;
34 info->ctrl_regs.rflags = guest_state->rflags;
36 get_vmcb_segments((vmcb_t*)(info->vmm_data), &(info->segments));
39 exit_code = guest_ctrl->exit_code;
42 // Disable printing io exits due to bochs debug messages
43 //if (!((exit_code == VMEXIT_IOIO) && ((ushort_t)(guest_ctrl->exit_info1 >> 16) == 0x402))) {
46 PrintDebug("SVM Returned: Exit Code: 0x%x \t\t(tsc=%ul)\n",exit_code, (uint_t)info->time_state.guest_tsc);
48 if (exit_code < 0x4f) {
51 // Dump out the instr stream
53 //PrintDebug("RIP: %x\n", guest_state->rip);
54 PrintDebug("RIP Linear: %x\n", get_addr_linear(info, info->rip, &(info->segments.cs)));
56 // OK, now we will read the instruction
57 // The only difference between PROTECTED and PROTECTED_PG is whether we read
58 // from guest_pa or guest_va
59 if (info->mem_mode == PHYSICAL_MEM) {
60 // The real rip address is actually a combination of the rip + CS base
61 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 32, instr);
63 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 32, instr);
67 // I think we should inject a GPF into the guest
68 PrintDebug("Could not read instruction (ret=%d)\n", ret);
71 PrintDebug("Instr Stream:\n");
72 PrintTraceMemDump(instr, 32);
78 // PrintDebugVMCB((vmcb_t*)(info->vmm_data));
81 // PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data);
82 //PrintDebug("RIP: %x\n", guest_state->rip);
85 //PrintDebug("SVM Returned: Exit Code: %x\n",exit_code);
87 if (exit_code == VMEXIT_IOIO) {
88 struct svm_io_info * io_info = (struct svm_io_info *)&(guest_ctrl->exit_info1);
90 if (io_info->type == 0) {
92 if (handle_svm_io_outs(info) == -1 ) {
96 if (handle_svm_io_out(info) == -1) {
102 if (handle_svm_io_ins(info) == -1) {
106 if (handle_svm_io_in(info) == -1) {
111 } else if (exit_code == VMEXIT_CR0_WRITE) {
112 PrintDebug("CR0 Write\n");
114 if (handle_cr0_write(info) == -1) {
117 } else if (exit_code == VMEXIT_CR0_READ) {
118 PrintDebug("CR0 Read\n");
120 if (handle_cr0_read(info) == -1) {
123 } else if (exit_code == VMEXIT_CR3_WRITE) {
124 PrintDebug("CR3 Write\n");
126 if (handle_cr3_write(info) == -1) {
129 } else if (exit_code == VMEXIT_CR3_READ) {
130 PrintDebug("CR3 Read\n");
132 if (handle_cr3_read(info) == -1) {
136 } else if (exit_code == VMEXIT_EXCP14) {
137 addr_t fault_addr = guest_ctrl->exit_info2;
138 pf_error_t * error_code = (pf_error_t *)&(guest_ctrl->exit_info1);
140 PrintDebug("PageFault at %x (error=%d)\n", fault_addr, *error_code);
142 if (info->shdw_pg_mode == SHADOW_PAGING) {
143 if (handle_shadow_pagefault(info, fault_addr, *error_code) == -1) {
148 PrintDebug("Page fault in un implemented paging mode\n");
152 } else if (exit_code == VMEXIT_NPF) {
153 PrintDebug("Currently unhandled Nested Page Fault\n");
156 } else if (exit_code == VMEXIT_INVLPG) {
157 if (info->shdw_pg_mode == SHADOW_PAGING) {
158 PrintDebug("Invlpg\n");
159 if (handle_shadow_invlpg(info) == -1) {
165 (exit_code == VMEXIT_INVLPGA) ||
168 } else if (exit_code == VMEXIT_INTR) {
170 // handle_svm_intr(info);
172 } else if (exit_code == VMEXIT_SMI) {
174 // handle_svm_smi(info); // ignored for now
176 } else if (exit_code == VMEXIT_HLT) {
177 PrintDebug("Guest halted\n");
178 if (handle_svm_halt(info) == -1) {
181 } else if (exit_code == VMEXIT_PAUSE) {
182 PrintDebug("Guest paused\n");
183 if (handle_svm_pause(info) == -1) {
192 rip_addr = get_addr_linear(info, guest_state->rip, &(info->segments.cs));
196 PrintDebug("SVM Returned:(VMCB=%x)\n", info->vmm_data);
197 PrintDebug("RIP: %x\n", guest_state->rip);
198 PrintDebug("RIP Linear: %x\n", rip_addr);
200 PrintDebug("SVM Returned: Exit Code: %x\n",exit_code);
202 PrintDebug("io_info1 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info1));
203 PrintDebug("io_info1 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info1)) + 4));
205 PrintDebug("io_info2 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info2));
206 PrintDebug("io_info2 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info2)) + 4));
210 if (info->mem_mode == PHYSICAL_MEM) {
211 if (guest_pa_to_host_pa(info, guest_state->rip, &host_addr) == -1) {
212 PrintDebug("Could not translate guest_state->rip to host address\n");
215 } else if (info->mem_mode == VIRTUAL_MEM) {
216 if (guest_va_to_host_pa(info, guest_state->rip, &host_addr) == -1) {
217 PrintDebug("Could not translate guest_state->rip to host address\n");
221 PrintDebug("Invalid memory mode\n");
225 PrintDebug("Host Address of rip = 0x%x\n", host_addr);
229 PrintDebug("Reading from 0x%x in guest\n", rip_addr);
231 if (info->mem_mode == PHYSICAL_MEM) {
232 read_guest_pa_memory(info, rip_addr, 15, buf);
234 read_guest_va_memory(info, rip_addr, 15, buf);
237 PrintTraceMemDump(buf, 15);
244 // Update the low level state
246 if (intr_pending(info)) {
248 switch (get_intr_type(info)) {
251 uint_t irq = get_intr_number(info);
253 // check to see if ==-1 (non exists)
256 guest_ctrl->EVENTINJ.vector = irq;
257 guest_ctrl->EVENTINJ.valid = 1;
258 guest_ctrl->EVENTINJ.type = SVM_INJECTION_EXTERNAL_INTR;
261 guest_ctrl->guest_ctrl.V_IRQ = 1;
262 guest_ctrl->guest_ctrl.V_INTR_VECTOR = irq;
263 guest_ctrl->guest_ctrl.V_IGN_TPR = 1;
264 guest_ctrl->guest_ctrl.V_INTR_PRIO = 0xf;
266 PrintDebug("Injecting Interrupt %d (EIP=%x)\n", guest_ctrl->guest_ctrl.V_INTR_VECTOR, info->rip);
268 injecting_intr(info, irq, EXTERNAL_IRQ);
273 guest_ctrl->EVENTINJ.type = SVM_INJECTION_NMI;
277 uint_t excp = get_intr_number(info);
279 guest_ctrl->EVENTINJ.type = SVM_INJECTION_EXCEPTION;
281 if (info->intr_state.excp_error_code_valid) { //PAD
282 guest_ctrl->EVENTINJ.error_code = info->intr_state.excp_error_code;
283 guest_ctrl->EVENTINJ.ev = 1;
284 PrintDebug("Injecting error code %x\n", guest_ctrl->EVENTINJ.error_code);
287 guest_ctrl->EVENTINJ.vector = excp;
289 guest_ctrl->EVENTINJ.valid = 1;
290 PrintDebug("Injecting Interrupt %d (EIP=%x)\n", guest_ctrl->EVENTINJ.vector, info->rip);
291 injecting_intr(info, excp, EXCEPTION);
295 guest_ctrl->EVENTINJ.type = SVM_INJECTION_SOFT_INTR;
298 guest_ctrl->EVENTINJ.type = SVM_INJECTION_VIRTUAL_INTR;
303 PrintDebug("Attempted to issue an invalid interrupt\n");
310 guest_state->cr0 = info->ctrl_regs.cr0;
311 guest_state->cr2 = info->ctrl_regs.cr2;
312 guest_state->cr3 = info->ctrl_regs.cr3;
313 guest_state->cr4 = info->ctrl_regs.cr4;
314 guest_ctrl->guest_ctrl.V_TPR = info->ctrl_regs.cr8 & 0xff;
315 guest_state->rflags = info->ctrl_regs.rflags;
318 guest_state->cpl = info->cpl;
320 guest_state->rax = info->vm_regs.rax;
321 guest_state->rip = info->rip;
322 guest_state->rsp = info->vm_regs.rsp;
325 set_vmcb_segments((vmcb_t*)(info->vmm_data), &(info->segments));
327 if (exit_code == VMEXIT_INTR) {
328 //PrintDebug("INTR ret IP = %x\n", guest_state->rip);