2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmx_handler.h>
21 #include <palacios/vmm_types.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmcs.h>
24 #include <palacios/vmx_lowlevel.h>
25 #include <palacios/vmx_io.h>
26 #include <palacios/vmm_cpuid.h>
27 #include <palacios/vmm_debug.h>
29 #include <palacios/vmx.h>
30 #include <palacios/vmm_ctrl_regs.h>
31 #include <palacios/vmm_lowlevel.h>
32 #include <palacios/vmx_ctrl_regs.h>
33 #include <palacios/vmx_assist.h>
34 #include <palacios/vmm_halt.h>
35 #include <palacios/vmm_mwait.h>
36 #include <palacios/vmx_ept.h>
39 #ifndef V3_CONFIG_DEBUG_VMX
41 #define PrintDebug(fmt, args...)
44 #ifdef V3_CONFIG_TELEMETRY
45 #include <palacios/vmm_telemetry.h>
48 /* At this point the GPRs are already copied into the guest_info state */
49 int v3_handle_vmx_exit(struct guest_info * info, struct vmx_exit_info * exit_info) {
50 struct vmx_basic_exit_info * basic_info = (struct vmx_basic_exit_info *)&(exit_info->exit_reason);
53 PrintError(info->vm_info, info, "Handling VMX_EXIT: %s (%u), %lu (0x%lx)\n",
54 v3_vmx_exit_code_to_str(exit_info->exit_reason),
55 exit_info->exit_reason,
56 exit_info->exit_qual, exit_info->exit_qual);
62 if (basic_info->entry_error == 1) {
63 switch (basic_info->reason) {
64 case VMX_EXIT_INVALID_GUEST_STATE:
65 PrintError(info->vm_info, info, "VM Entry failed due to invalid guest state\n");
66 PrintError(info->vm_info, info, "Printing VMCS: (NOTE: This VMCS may not belong to the correct guest)\n");
69 case VMX_EXIT_INVALID_MSR_LOAD:
70 PrintError(info->vm_info, info, "VM Entry failed due to error loading MSRs\n");
73 PrintError(info->vm_info, info, "Entry failed for unknown reason (%d)\n", basic_info->reason);
82 #ifdef V3_CONFIG_TELEMETRY
83 if (info->vm_info->enable_telemetry) {
84 v3_telemetry_start_exit(info);
88 switch (basic_info->reason) {
89 case VMX_EXIT_INFO_EXCEPTION_OR_NMI: {
90 pf_error_t error_code = *(pf_error_t *)&(exit_info->int_err);
93 // JRL: Change "0x0e" to a macro value
94 if ((uint8_t)exit_info->int_info == 14) {
95 #ifdef V3_CONFIG_DEBUG_SHADOW_PAGING
96 PrintDebug(info->vm_info, info, "Page Fault at %p error_code=%x\n", (void *)exit_info->exit_qual, *(uint32_t *)&error_code);
99 if (info->shdw_pg_mode == SHADOW_PAGING) {
100 if (v3_handle_shadow_pagefault(info, (addr_t)exit_info->exit_qual, error_code) == -1) {
101 PrintError(info->vm_info, info, "Error handling shadow page fault\n");
106 PrintError(info->vm_info, info, "Page fault in unimplemented paging mode\n");
109 } else if ((uint8_t)exit_info->int_info == 2) {
110 // NMI. Don't do anything
112 PrintError(info->vm_info, info, "Unknown exception: 0x%x\n", (uint8_t)exit_info->int_info);
119 case VMX_EXIT_EPT_VIOLATION: {
120 struct ept_exit_qual * ept_qual = (struct ept_exit_qual *)&(exit_info->exit_qual);
122 if (v3_handle_nested_pagefault(info, exit_info->ept_fault_addr, ept_qual,NULL,NULL) == -1) {
123 PrintError(info->vm_info, info, "Error handling EPT fault\n");
129 case VMX_EXIT_INVLPG:
130 if (info->shdw_pg_mode == SHADOW_PAGING) {
131 if (v3_handle_shadow_invlpg(info) == -1) {
132 PrintError(info->vm_info, info, "Error handling INVLPG\n");
140 #ifdef V3_CONFIG_DEBUG_TIME
141 PrintDebug(info->vm_info, info, "RDTSC\n");
143 if (v3_handle_rdtsc(info) == -1) {
144 PrintError(info->vm_info, info, "Error Handling RDTSC instruction\n");
151 if (v3_handle_cpuid(info) == -1) {
152 PrintError(info->vm_info, info, "Error Handling CPUID instruction\n");
158 if (v3_handle_msr_read(info) == -1) {
159 PrintError(info->vm_info, info, "Error handling MSR Read\n");
165 if (v3_handle_msr_write(info) == -1) {
166 PrintError(info->vm_info, info, "Error handling MSR Write\n");
171 case VMX_EXIT_VMCALL:
176 // VMCALL is a 3 byte op
177 // We do this early because some hypercalls can change the rip...
180 if (v3_handle_hypercall(info) == -1) {
184 case VMX_EXIT_IO_INSTR: {
185 struct vmx_exit_io_qual * io_qual = (struct vmx_exit_io_qual *)&(exit_info->exit_qual);
187 if (io_qual->dir == 0) {
188 if (io_qual->string) {
189 if (v3_handle_vmx_io_outs(info, exit_info) == -1) {
190 PrintError(info->vm_info, info, "Error in outs IO handler\n");
194 if (v3_handle_vmx_io_out(info, exit_info) == -1) {
195 PrintError(info->vm_info, info, "Error in out IO handler\n");
200 if (io_qual->string) {
201 if(v3_handle_vmx_io_ins(info, exit_info) == -1) {
202 PrintError(info->vm_info, info, "Error in ins IO handler\n");
206 if (v3_handle_vmx_io_in(info, exit_info) == -1) {
207 PrintError(info->vm_info, info, "Error in in IO handler\n");
214 case VMX_EXIT_CR_REG_ACCESSES: {
215 struct vmx_exit_cr_qual * cr_qual = (struct vmx_exit_cr_qual *)&(exit_info->exit_qual);
217 // PrintDebug(info->vm_info, info, "Control register: %d\n", cr_qual->access_type);
218 switch(cr_qual->cr_id) {
220 //PrintDebug(info->vm_info, info, "Handling CR0 Access\n");
221 if (v3_vmx_handle_cr0_access(info, cr_qual, exit_info) == -1) {
222 PrintError(info->vm_info, info, "Error in CR0 access handler\n");
227 //PrintDebug(info->vm_info, info, "Handling CR3 Access\n");
228 if (v3_vmx_handle_cr3_access(info, cr_qual) == -1) {
229 PrintError(info->vm_info, info, "Error in CR3 access handler\n");
234 //PrintDebug(info->vm_info, info, "Handling CR4 Access\n");
235 if (v3_vmx_handle_cr4_access(info, cr_qual) == -1) {
236 PrintError(info->vm_info, info, "Error in CR4 access handler\n");
241 if (v3_vmx_handle_cr8_access(info, cr_qual) == -1) {
242 PrintError(info->vm_info, info, "Error in CR8 access handler\n");
247 PrintError(info->vm_info, info, "Unhandled CR access: %d\n", cr_qual->cr_id);
251 // TODO: move RIP increment into all of the above individual CR
252 // handlers, not just v3_vmx_handle_cr4_access()
253 if (cr_qual->cr_id != 4)
254 info->rip += exit_info->instr_len;
259 PrintDebug(info->vm_info, info, "Guest halted\n");
261 if (v3_handle_halt(info) == -1) {
262 PrintError(info->vm_info, info, "Error handling halt instruction\n");
268 case VMX_EXIT_MONITOR:
269 PrintDebug(info->vm_info, info, "Guest Executing monitor\n");
271 if (v3_handle_monitor(info) == -1) {
272 PrintError(info->vm_info, info, "Error handling monitor instruction\n");
279 PrintDebug(info->vm_info, info, "Guest Executing mwait\n");
281 if (v3_handle_mwait(info) == -1) {
282 PrintError(info->vm_info, info, "Error handling mwait instruction\n");
294 case VMX_EXIT_EXTERNAL_INTR:
295 // Interrupts are handled outside switch
297 case VMX_EXIT_INTR_WINDOW:
298 // This is handled in the atomic part of the vmx code,
299 // not in the generic (interruptable) vmx handler
301 case VMX_EXIT_EXPIRED_PREEMPT_TIMER:
302 V3_Print(info->vm_info, info, "VMX Preempt Timer Expired.\n");
303 // This just forces an exit and is handled outside the switch
307 PrintError(info->vm_info, info, "Unhandled VMX_EXIT: %s (%u), %lu (0x%lx)\n",
308 v3_vmx_exit_code_to_str(basic_info->reason),
310 exit_info->exit_qual, exit_info->exit_qual);
315 #ifdef V3_CONFIG_TELEMETRY
316 if (info->vm_info->enable_telemetry) {
317 v3_telemetry_end_exit(info, exit_info->exit_reason);
325 static const char VMX_EXIT_INFO_EXCEPTION_OR_NMI_STR[] = "VMX_EXIT_INFO_EXCEPTION_OR_NMI";
326 static const char VMX_EXIT_EXTERNAL_INTR_STR[] = "VMX_EXIT_EXTERNAL_INTR";
327 static const char VMX_EXIT_TRIPLE_FAULT_STR[] = "VMX_EXIT_TRIPLE_FAULT";
328 static const char VMX_EXIT_INIT_SIGNAL_STR[] = "VMX_EXIT_INIT_SIGNAL";
329 static const char VMX_EXIT_STARTUP_IPI_STR[] = "VMX_EXIT_STARTUP_IPI";
330 static const char VMX_EXIT_IO_SMI_STR[] = "VMX_EXIT_IO_SMI";
331 static const char VMX_EXIT_OTHER_SMI_STR[] = "VMX_EXIT_OTHER_SMI";
332 static const char VMX_EXIT_INTR_WINDOW_STR[] = "VMX_EXIT_INTR_WINDOW";
333 static const char VMX_EXIT_NMI_WINDOW_STR[] = "VMX_EXIT_NMI_WINDOW";
334 static const char VMX_EXIT_TASK_SWITCH_STR[] = "VMX_EXIT_TASK_SWITCH";
335 static const char VMX_EXIT_CPUID_STR[] = "VMX_EXIT_CPUID";
336 static const char VMX_EXIT_HLT_STR[] = "VMX_EXIT_HLT";
337 static const char VMX_EXIT_INVD_STR[] = "VMX_EXIT_INVD";
338 static const char VMX_EXIT_INVLPG_STR[] = "VMX_EXIT_INVLPG";
339 static const char VMX_EXIT_RDPMC_STR[] = "VMX_EXIT_RDPMC";
340 static const char VMX_EXIT_RDTSC_STR[] = "VMX_EXIT_RDTSC";
341 static const char VMX_EXIT_RSM_STR[] = "VMX_EXIT_RSM";
342 static const char VMX_EXIT_VMCALL_STR[] = "VMX_EXIT_VMCALL";
343 static const char VMX_EXIT_VMCLEAR_STR[] = "VMX_EXIT_VMCLEAR";
344 static const char VMX_EXIT_VMLAUNCH_STR[] = "VMX_EXIT_VMLAUNCH";
345 static const char VMX_EXIT_VMPTRLD_STR[] = "VMX_EXIT_VMPTRLD";
346 static const char VMX_EXIT_VMPTRST_STR[] = "VMX_EXIT_VMPTRST";
347 static const char VMX_EXIT_VMREAD_STR[] = "VMX_EXIT_VMREAD";
348 static const char VMX_EXIT_VMRESUME_STR[] = "VMX_EXIT_VMRESUME";
349 static const char VMX_EXIT_VMWRITE_STR[] = "VMX_EXIT_VMWRITE";
350 static const char VMX_EXIT_VMXOFF_STR[] = "VMX_EXIT_VMXOFF";
351 static const char VMX_EXIT_VMXON_STR[] = "VMX_EXIT_VMXON";
352 static const char VMX_EXIT_CR_REG_ACCESSES_STR[] = "VMX_EXIT_CR_REG_ACCESSES";
353 static const char VMX_EXIT_MOV_DR_STR[] = "VMX_EXIT_MOV_DR";
354 static const char VMX_EXIT_IO_INSTR_STR[] = "VMX_EXIT_IO_INSTR";
355 static const char VMX_EXIT_RDMSR_STR[] = "VMX_EXIT_RDMSR";
356 static const char VMX_EXIT_WRMSR_STR[] = "VMX_EXIT_WRMSR";
357 static const char VMX_EXIT_INVALID_GUEST_STATE_STR[] = "VMX_EXIT_INVALID_GUEST_STATE";
358 static const char VMX_EXIT_INVALID_MSR_LOAD_STR[] = "VMX_EXIT_INVALID_MSR_LOAD";
359 static const char VMX_EXIT_MWAIT_STR[] = "VMX_EXIT_MWAIT";
360 static const char VMX_EXIT_MONITOR_STR[] = "VMX_EXIT_MONITOR";
361 static const char VMX_EXIT_PAUSE_STR[] = "VMX_EXIT_PAUSE";
362 static const char VMX_EXIT_INVALID_MACHINE_CHECK_STR[] = "VMX_EXIT_INVALIDE_MACHINE_CHECK";
363 static const char VMX_EXIT_TPR_BELOW_THRESHOLD_STR[] = "VMX_EXIT_TPR_BELOW_THRESHOLD";
364 static const char VMX_EXIT_APIC_STR[] = "VMX_EXIT_APIC";
365 static const char VMX_EXIT_GDTR_IDTR_STR[] = "VMX_EXIT_GDTR_IDTR";
366 static const char VMX_EXIT_LDTR_TR_STR[] = "VMX_EXIT_LDTR_TR";
367 static const char VMX_EXIT_EPT_VIOLATION_STR[] = "VMX_EXIT_EPT_VIOLATION";
368 static const char VMX_EXIT_EPT_CONFIG_STR[] = "VMX_EXIT_EPT_CONFIG";
369 static const char VMX_EXIT_INVEPT_STR[] = "VMX_EXIT_INVEPT";
370 static const char VMX_EXIT_RDTSCP_STR[] = "VMX_EXIT_RDTSCP";
371 static const char VMX_EXIT_EXPIRED_PREEMPT_TIMER_STR[] = "VMX_EXIT_EXPIRED_PREEMPT_TIMER";
372 static const char VMX_EXIT_INVVPID_STR[] = "VMX_EXIT_INVVPID";
373 static const char VMX_EXIT_WBINVD_STR[] = "VMX_EXIT_WBINVD";
374 static const char VMX_EXIT_XSETBV_STR[] = "VMX_EXIT_XSETBV";
376 const char * v3_vmx_exit_code_to_str(vmx_exit_t exit)
379 case VMX_EXIT_INFO_EXCEPTION_OR_NMI:
380 return VMX_EXIT_INFO_EXCEPTION_OR_NMI_STR;
381 case VMX_EXIT_EXTERNAL_INTR:
382 return VMX_EXIT_EXTERNAL_INTR_STR;
383 case VMX_EXIT_TRIPLE_FAULT:
384 return VMX_EXIT_TRIPLE_FAULT_STR;
385 case VMX_EXIT_INIT_SIGNAL:
386 return VMX_EXIT_INIT_SIGNAL_STR;
387 case VMX_EXIT_STARTUP_IPI:
388 return VMX_EXIT_STARTUP_IPI_STR;
389 case VMX_EXIT_IO_SMI:
390 return VMX_EXIT_IO_SMI_STR;
391 case VMX_EXIT_OTHER_SMI:
392 return VMX_EXIT_OTHER_SMI_STR;
393 case VMX_EXIT_INTR_WINDOW:
394 return VMX_EXIT_INTR_WINDOW_STR;
395 case VMX_EXIT_NMI_WINDOW:
396 return VMX_EXIT_NMI_WINDOW_STR;
397 case VMX_EXIT_TASK_SWITCH:
398 return VMX_EXIT_TASK_SWITCH_STR;
400 return VMX_EXIT_CPUID_STR;
402 return VMX_EXIT_HLT_STR;
404 return VMX_EXIT_INVD_STR;
405 case VMX_EXIT_INVLPG:
406 return VMX_EXIT_INVLPG_STR;
408 return VMX_EXIT_RDPMC_STR;
410 return VMX_EXIT_RDTSC_STR;
412 return VMX_EXIT_RSM_STR;
413 case VMX_EXIT_VMCALL:
414 return VMX_EXIT_VMCALL_STR;
415 case VMX_EXIT_VMCLEAR:
416 return VMX_EXIT_VMCLEAR_STR;
417 case VMX_EXIT_VMLAUNCH:
418 return VMX_EXIT_VMLAUNCH_STR;
419 case VMX_EXIT_VMPTRLD:
420 return VMX_EXIT_VMPTRLD_STR;
421 case VMX_EXIT_VMPTRST:
422 return VMX_EXIT_VMPTRST_STR;
423 case VMX_EXIT_VMREAD:
424 return VMX_EXIT_VMREAD_STR;
425 case VMX_EXIT_VMRESUME:
426 return VMX_EXIT_VMRESUME_STR;
427 case VMX_EXIT_VMWRITE:
428 return VMX_EXIT_VMWRITE_STR;
429 case VMX_EXIT_VMXOFF:
430 return VMX_EXIT_VMXOFF_STR;
432 return VMX_EXIT_VMXON_STR;
433 case VMX_EXIT_CR_REG_ACCESSES:
434 return VMX_EXIT_CR_REG_ACCESSES_STR;
435 case VMX_EXIT_MOV_DR:
436 return VMX_EXIT_MOV_DR_STR;
437 case VMX_EXIT_IO_INSTR:
438 return VMX_EXIT_IO_INSTR_STR;
440 return VMX_EXIT_RDMSR_STR;
442 return VMX_EXIT_WRMSR_STR;
443 case VMX_EXIT_INVALID_GUEST_STATE:
444 return VMX_EXIT_INVALID_GUEST_STATE_STR;
445 case VMX_EXIT_INVALID_MSR_LOAD:
446 return VMX_EXIT_INVALID_MSR_LOAD_STR;
448 return VMX_EXIT_MWAIT_STR;
449 case VMX_EXIT_MONITOR:
450 return VMX_EXIT_MONITOR_STR;
452 return VMX_EXIT_PAUSE_STR;
453 case VMX_EXIT_INVALID_MACHINE_CHECK:
454 return VMX_EXIT_INVALID_MACHINE_CHECK_STR;
455 case VMX_EXIT_TPR_BELOW_THRESHOLD:
456 return VMX_EXIT_TPR_BELOW_THRESHOLD_STR;
458 return VMX_EXIT_APIC_STR;
459 case VMX_EXIT_GDTR_IDTR:
460 return VMX_EXIT_GDTR_IDTR_STR;
461 case VMX_EXIT_LDTR_TR:
462 return VMX_EXIT_LDTR_TR_STR;
463 case VMX_EXIT_EPT_VIOLATION:
464 return VMX_EXIT_EPT_VIOLATION_STR;
465 case VMX_EXIT_EPT_CONFIG:
466 return VMX_EXIT_EPT_CONFIG_STR;
467 case VMX_EXIT_INVEPT:
468 return VMX_EXIT_INVEPT_STR;
469 case VMX_EXIT_RDTSCP:
470 return VMX_EXIT_RDTSCP_STR;
471 case VMX_EXIT_EXPIRED_PREEMPT_TIMER:
472 return VMX_EXIT_EXPIRED_PREEMPT_TIMER_STR;
473 case VMX_EXIT_INVVPID:
474 return VMX_EXIT_INVVPID_STR;
475 case VMX_EXIT_WBINVD:
476 return VMX_EXIT_WBINVD_STR;
477 case VMX_EXIT_XSETBV:
478 return VMX_EXIT_XSETBV_STR;