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>
28 #include <palacios/vmx.h>
29 #include <palacios/vmm_ctrl_regs.h>
30 #include <palacios/vmm_lowlevel.h>
31 #include <palacios/vmx_ctrl_regs.h>
32 #include <palacios/vmx_assist.h>
33 #include <palacios/vmm_halt.h>
34 #include <palacios/vmx_ept.h>
36 #ifndef V3_CONFIG_DEBUG_VMX
38 #define PrintDebug(fmt, args...)
41 #ifdef V3_CONFIG_TELEMETRY
42 #include <palacios/vmm_telemetry.h>
45 /* At this point the GPRs are already copied into the guest_info state */
46 int v3_handle_vmx_exit(struct guest_info * info, struct vmx_exit_info * exit_info) {
47 struct vmx_basic_exit_info * basic_info = (struct vmx_basic_exit_info *)&(exit_info->exit_reason);
50 PrintError("Handling VMX_EXIT: %s (%u), %lu (0x%lx)\n",
51 v3_vmx_exit_code_to_str(exit_info->exit_reason),
52 exit_info->exit_reason,
53 exit_info->exit_qual, exit_info->exit_qual);
59 if (basic_info->entry_error == 1) {
60 switch (basic_info->reason) {
61 case VMX_EXIT_INVALID_GUEST_STATE:
62 PrintError("VM Entry failed due to invalid guest state\n");
63 PrintError("Printing VMCS: (NOTE: This VMCS may not belong to the correct guest)\n");
66 case VMX_EXIT_INVALID_MSR_LOAD:
67 PrintError("VM Entry failed due to error loading MSRs\n");
70 PrintError("Entry failed for unknown reason (%d)\n", basic_info->reason);
79 #ifdef V3_CONFIG_TELEMETRY
80 if (info->vm_info->enable_telemetry) {
81 v3_telemetry_start_exit(info);
85 switch (basic_info->reason) {
86 case VMX_EXIT_INFO_EXCEPTION_OR_NMI: {
87 pf_error_t error_code = *(pf_error_t *)&(exit_info->int_err);
90 // JRL: Change "0x0e" to a macro value
91 if ((uint8_t)exit_info->int_info == 14) {
92 #ifdef V3_CONFIG_DEBUG_SHADOW_PAGING
93 PrintDebug("Page Fault at %p error_code=%x\n", (void *)exit_info->exit_qual, *(uint32_t *)&error_code);
96 if (info->shdw_pg_mode == SHADOW_PAGING) {
97 if (v3_handle_shadow_pagefault(info, (addr_t)exit_info->exit_qual, error_code) == -1) {
98 PrintError("Error handling shadow page fault\n");
103 PrintError("Page fault in unimplemented paging mode\n");
106 } else if ((uint8_t)exit_info->int_info == 2) {
107 // NMI. Don't do anything
108 V3_Print("NMI Exception Received\n");
110 PrintError("Unknown exception: 0x%x\n", (uint8_t)exit_info->int_info);
117 case VMX_EXIT_EPT_VIOLATION: {
118 struct ept_exit_qual * ept_qual = (struct ept_exit_qual *)&(exit_info->exit_qual);
120 if (v3_handle_ept_fault(info, exit_info->ept_fault_addr, ept_qual) == -1) {
121 PrintError("Error handling EPT fault\n");
127 case VMX_EXIT_INVLPG:
128 if (info->shdw_pg_mode == SHADOW_PAGING) {
129 if (v3_handle_shadow_invlpg(info) == -1) {
130 PrintError("Error handling INVLPG\n");
138 #ifdef V3_CONFIG_DEBUG_TIME
139 PrintDebug("RDTSC\n");
141 if (v3_handle_rdtsc(info) == -1) {
142 PrintError("Error Handling RDTSC instruction\n");
149 if (v3_handle_cpuid(info) == -1) {
150 PrintError("Error Handling CPUID instruction\n");
156 if (v3_handle_msr_read(info) == -1) {
157 PrintError("Error handling MSR Read\n");
163 if (v3_handle_msr_write(info) == -1) {
164 PrintError("Error handling MSR Write\n");
169 case VMX_EXIT_VMCALL:
174 // VMCALL is a 3 byte op
175 // We do this early because some hypercalls can change the rip...
178 if (v3_handle_hypercall(info) == -1) {
182 case VMX_EXIT_IO_INSTR: {
183 struct vmx_exit_io_qual * io_qual = (struct vmx_exit_io_qual *)&(exit_info->exit_qual);
185 if (io_qual->dir == 0) {
186 if (io_qual->string) {
187 if (v3_handle_vmx_io_outs(info, exit_info) == -1) {
188 PrintError("Error in outs IO handler\n");
192 if (v3_handle_vmx_io_out(info, exit_info) == -1) {
193 PrintError("Error in out IO handler\n");
198 if (io_qual->string) {
199 if(v3_handle_vmx_io_ins(info, exit_info) == -1) {
200 PrintError("Error in ins IO handler\n");
204 if (v3_handle_vmx_io_in(info, exit_info) == -1) {
205 PrintError("Error in in IO handler\n");
212 case VMX_EXIT_CR_REG_ACCESSES: {
213 struct vmx_exit_cr_qual * cr_qual = (struct vmx_exit_cr_qual *)&(exit_info->exit_qual);
215 // PrintDebug("Control register: %d\n", cr_qual->access_type);
216 switch(cr_qual->cr_id) {
218 //PrintDebug("Handling CR0 Access\n");
219 if (v3_vmx_handle_cr0_access(info, cr_qual, exit_info) == -1) {
220 PrintError("Error in CR0 access handler\n");
225 //PrintDebug("Handling CR3 Access\n");
226 if (v3_vmx_handle_cr3_access(info, cr_qual) == -1) {
227 PrintError("Error in CR3 access handler\n");
232 //PrintDebug("Handling CR4 Access\n");
233 if (v3_vmx_handle_cr4_access(info, cr_qual) == -1) {
234 PrintError("Error in CR4 access handler\n");
239 PrintError("Unhandled CR access: %d\n", cr_qual->cr_id);
243 // TODO: move RIP increment into all of the above individual CR
244 // handlers, not just v3_vmx_handle_cr4_access()
245 if (cr_qual->cr_id != 4)
246 info->rip += exit_info->instr_len;
251 PrintDebug("Guest halted\n");
253 if (v3_handle_halt(info) == -1) {
254 PrintError("Error handling halt instruction\n");
267 case VMX_EXIT_EXTERNAL_INTR:
268 // Interrupts are handled outside switch
270 case VMX_EXIT_INTR_WINDOW:
271 // This is handled in the atomic part of the vmx code,
272 // not in the generic (interruptable) vmx handler
274 case VMX_EXIT_EXPIRED_PREEMPT_TIMER:
275 V3_Print("VMX Preempt Timer Expired.\n");
276 // This just forces an exit and is handled outside the switch
280 PrintError("Unhandled VMX_EXIT: %s (%u), %lu (0x%lx)\n",
281 v3_vmx_exit_code_to_str(basic_info->reason),
283 exit_info->exit_qual, exit_info->exit_qual);
288 #ifdef V3_CONFIG_TELEMETRY
289 if (info->vm_info->enable_telemetry) {
290 v3_telemetry_end_exit(info, exit_info->exit_reason);
298 static const char VMX_EXIT_INFO_EXCEPTION_OR_NMI_STR[] = "VMX_EXIT_INFO_EXCEPTION_OR_NMI";
299 static const char VMX_EXIT_EXTERNAL_INTR_STR[] = "VMX_EXIT_EXTERNAL_INTR";
300 static const char VMX_EXIT_TRIPLE_FAULT_STR[] = "VMX_EXIT_TRIPLE_FAULT";
301 static const char VMX_EXIT_INIT_SIGNAL_STR[] = "VMX_EXIT_INIT_SIGNAL";
302 static const char VMX_EXIT_STARTUP_IPI_STR[] = "VMX_EXIT_STARTUP_IPI";
303 static const char VMX_EXIT_IO_SMI_STR[] = "VMX_EXIT_IO_SMI";
304 static const char VMX_EXIT_OTHER_SMI_STR[] = "VMX_EXIT_OTHER_SMI";
305 static const char VMX_EXIT_INTR_WINDOW_STR[] = "VMX_EXIT_INTR_WINDOW";
306 static const char VMX_EXIT_NMI_WINDOW_STR[] = "VMX_EXIT_NMI_WINDOW";
307 static const char VMX_EXIT_TASK_SWITCH_STR[] = "VMX_EXIT_TASK_SWITCH";
308 static const char VMX_EXIT_CPUID_STR[] = "VMX_EXIT_CPUID";
309 static const char VMX_EXIT_HLT_STR[] = "VMX_EXIT_HLT";
310 static const char VMX_EXIT_INVD_STR[] = "VMX_EXIT_INVD";
311 static const char VMX_EXIT_INVLPG_STR[] = "VMX_EXIT_INVLPG";
312 static const char VMX_EXIT_RDPMC_STR[] = "VMX_EXIT_RDPMC";
313 static const char VMX_EXIT_RDTSC_STR[] = "VMX_EXIT_RDTSC";
314 static const char VMX_EXIT_RSM_STR[] = "VMX_EXIT_RSM";
315 static const char VMX_EXIT_VMCALL_STR[] = "VMX_EXIT_VMCALL";
316 static const char VMX_EXIT_VMCLEAR_STR[] = "VMX_EXIT_VMCLEAR";
317 static const char VMX_EXIT_VMLAUNCH_STR[] = "VMX_EXIT_VMLAUNCH";
318 static const char VMX_EXIT_VMPTRLD_STR[] = "VMX_EXIT_VMPTRLD";
319 static const char VMX_EXIT_VMPTRST_STR[] = "VMX_EXIT_VMPTRST";
320 static const char VMX_EXIT_VMREAD_STR[] = "VMX_EXIT_VMREAD";
321 static const char VMX_EXIT_VMRESUME_STR[] = "VMX_EXIT_VMRESUME";
322 static const char VMX_EXIT_VMWRITE_STR[] = "VMX_EXIT_VMWRITE";
323 static const char VMX_EXIT_VMXOFF_STR[] = "VMX_EXIT_VMXOFF";
324 static const char VMX_EXIT_VMXON_STR[] = "VMX_EXIT_VMXON";
325 static const char VMX_EXIT_CR_REG_ACCESSES_STR[] = "VMX_EXIT_CR_REG_ACCESSES";
326 static const char VMX_EXIT_MOV_DR_STR[] = "VMX_EXIT_MOV_DR";
327 static const char VMX_EXIT_IO_INSTR_STR[] = "VMX_EXIT_IO_INSTR";
328 static const char VMX_EXIT_RDMSR_STR[] = "VMX_EXIT_RDMSR";
329 static const char VMX_EXIT_WRMSR_STR[] = "VMX_EXIT_WRMSR";
330 static const char VMX_EXIT_INVALID_GUEST_STATE_STR[] = "VMX_EXIT_INVALID_GUEST_STATE";
331 static const char VMX_EXIT_INVALID_MSR_LOAD_STR[] = "VMX_EXIT_INVALID_MSR_LOAD";
332 static const char VMX_EXIT_MWAIT_STR[] = "VMX_EXIT_MWAIT";
333 static const char VMX_EXIT_MONITOR_STR[] = "VMX_EXIT_MONITOR";
334 static const char VMX_EXIT_PAUSE_STR[] = "VMX_EXIT_PAUSE";
335 static const char VMX_EXIT_INVALID_MACHINE_CHECK_STR[] = "VMX_EXIT_INVALIDE_MACHINE_CHECK";
336 static const char VMX_EXIT_TPR_BELOW_THRESHOLD_STR[] = "VMX_EXIT_TPR_BELOW_THRESHOLD";
337 static const char VMX_EXIT_APIC_STR[] = "VMX_EXIT_APIC";
338 static const char VMX_EXIT_GDTR_IDTR_STR[] = "VMX_EXIT_GDTR_IDTR";
339 static const char VMX_EXIT_LDTR_TR_STR[] = "VMX_EXIT_LDTR_TR";
340 static const char VMX_EXIT_EPT_VIOLATION_STR[] = "VMX_EXIT_EPT_VIOLATION";
341 static const char VMX_EXIT_EPT_CONFIG_STR[] = "VMX_EXIT_EPT_CONFIG";
342 static const char VMX_EXIT_INVEPT_STR[] = "VMX_EXIT_INVEPT";
343 static const char VMX_EXIT_RDTSCP_STR[] = "VMX_EXIT_RDTSCP";
344 static const char VMX_EXIT_EXPIRED_PREEMPT_TIMER_STR[] = "VMX_EXIT_EXPIRED_PREEMPT_TIMER";
345 static const char VMX_EXIT_INVVPID_STR[] = "VMX_EXIT_INVVPID";
346 static const char VMX_EXIT_WBINVD_STR[] = "VMX_EXIT_WBINVD";
347 static const char VMX_EXIT_XSETBV_STR[] = "VMX_EXIT_XSETBV";
349 const char * v3_vmx_exit_code_to_str(vmx_exit_t exit)
352 case VMX_EXIT_INFO_EXCEPTION_OR_NMI:
353 return VMX_EXIT_INFO_EXCEPTION_OR_NMI_STR;
354 case VMX_EXIT_EXTERNAL_INTR:
355 return VMX_EXIT_EXTERNAL_INTR_STR;
356 case VMX_EXIT_TRIPLE_FAULT:
357 return VMX_EXIT_TRIPLE_FAULT_STR;
358 case VMX_EXIT_INIT_SIGNAL:
359 return VMX_EXIT_INIT_SIGNAL_STR;
360 case VMX_EXIT_STARTUP_IPI:
361 return VMX_EXIT_STARTUP_IPI_STR;
362 case VMX_EXIT_IO_SMI:
363 return VMX_EXIT_IO_SMI_STR;
364 case VMX_EXIT_OTHER_SMI:
365 return VMX_EXIT_OTHER_SMI_STR;
366 case VMX_EXIT_INTR_WINDOW:
367 return VMX_EXIT_INTR_WINDOW_STR;
368 case VMX_EXIT_NMI_WINDOW:
369 return VMX_EXIT_NMI_WINDOW_STR;
370 case VMX_EXIT_TASK_SWITCH:
371 return VMX_EXIT_TASK_SWITCH_STR;
373 return VMX_EXIT_CPUID_STR;
375 return VMX_EXIT_HLT_STR;
377 return VMX_EXIT_INVD_STR;
378 case VMX_EXIT_INVLPG:
379 return VMX_EXIT_INVLPG_STR;
381 return VMX_EXIT_RDPMC_STR;
383 return VMX_EXIT_RDTSC_STR;
385 return VMX_EXIT_RSM_STR;
386 case VMX_EXIT_VMCALL:
387 return VMX_EXIT_VMCALL_STR;
388 case VMX_EXIT_VMCLEAR:
389 return VMX_EXIT_VMCLEAR_STR;
390 case VMX_EXIT_VMLAUNCH:
391 return VMX_EXIT_VMLAUNCH_STR;
392 case VMX_EXIT_VMPTRLD:
393 return VMX_EXIT_VMPTRLD_STR;
394 case VMX_EXIT_VMPTRST:
395 return VMX_EXIT_VMPTRST_STR;
396 case VMX_EXIT_VMREAD:
397 return VMX_EXIT_VMREAD_STR;
398 case VMX_EXIT_VMRESUME:
399 return VMX_EXIT_VMRESUME_STR;
400 case VMX_EXIT_VMWRITE:
401 return VMX_EXIT_VMWRITE_STR;
402 case VMX_EXIT_VMXOFF:
403 return VMX_EXIT_VMXOFF_STR;
405 return VMX_EXIT_VMXON_STR;
406 case VMX_EXIT_CR_REG_ACCESSES:
407 return VMX_EXIT_CR_REG_ACCESSES_STR;
408 case VMX_EXIT_MOV_DR:
409 return VMX_EXIT_MOV_DR_STR;
410 case VMX_EXIT_IO_INSTR:
411 return VMX_EXIT_IO_INSTR_STR;
413 return VMX_EXIT_RDMSR_STR;
415 return VMX_EXIT_WRMSR_STR;
416 case VMX_EXIT_INVALID_GUEST_STATE:
417 return VMX_EXIT_INVALID_GUEST_STATE_STR;
418 case VMX_EXIT_INVALID_MSR_LOAD:
419 return VMX_EXIT_INVALID_MSR_LOAD_STR;
421 return VMX_EXIT_MWAIT_STR;
422 case VMX_EXIT_MONITOR:
423 return VMX_EXIT_MONITOR_STR;
425 return VMX_EXIT_PAUSE_STR;
426 case VMX_EXIT_INVALID_MACHINE_CHECK:
427 return VMX_EXIT_INVALID_MACHINE_CHECK_STR;
428 case VMX_EXIT_TPR_BELOW_THRESHOLD:
429 return VMX_EXIT_TPR_BELOW_THRESHOLD_STR;
431 return VMX_EXIT_APIC_STR;
432 case VMX_EXIT_GDTR_IDTR:
433 return VMX_EXIT_GDTR_IDTR_STR;
434 case VMX_EXIT_LDTR_TR:
435 return VMX_EXIT_LDTR_TR_STR;
436 case VMX_EXIT_EPT_VIOLATION:
437 return VMX_EXIT_EPT_VIOLATION_STR;
438 case VMX_EXIT_EPT_CONFIG:
439 return VMX_EXIT_EPT_CONFIG_STR;
440 case VMX_EXIT_INVEPT:
441 return VMX_EXIT_INVEPT_STR;
442 case VMX_EXIT_RDTSCP:
443 return VMX_EXIT_RDTSCP_STR;
444 case VMX_EXIT_EXPIRED_PREEMPT_TIMER:
445 return VMX_EXIT_EXPIRED_PREEMPT_TIMER_STR;
446 case VMX_EXIT_INVVPID:
447 return VMX_EXIT_INVVPID_STR;
448 case VMX_EXIT_WBINVD:
449 return VMX_EXIT_WBINVD_STR;
450 case VMX_EXIT_XSETBV:
451 return VMX_EXIT_XSETBV_STR;