X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmx_handler.c;h=3fa4f98bf506902376d2af60dcf412941e304384;hp=dafb2ffdb42c62bbd527bdecb39c320638a0d607;hb=5bf6d0c260240e314876a2fca8e3fd56bd6a1029;hpb=a24a1722328a575cec8dd8578902fd0f68c72c1c diff --git a/palacios/src/palacios/vmx_handler.c b/palacios/src/palacios/vmx_handler.c index dafb2ff..3fa4f98 100644 --- a/palacios/src/palacios/vmx_handler.c +++ b/palacios/src/palacios/vmx_handler.c @@ -22,41 +22,370 @@ #include #include #include +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_TELEMETRY +#include +#endif + -static int inline check_vmcs_write(vmcs_field_t field, addr_t val) -{ - int ret = 0; - ret = vmcs_write(field,val); - if (ret != VMX_SUCCESS) { - PrintError("VMWRITE error on %s!: %d\n", v3_vmcs_field_to_str(field), ret); - return 1; +/* At this point the GPRs are already copied into the guest_info state */ +int v3_handle_vmx_exit(struct guest_info * info, struct vmx_exit_info * exit_info) { + struct vmx_data * vmx_info = (struct vmx_data *)(info->vmm_data); + + /* + PrintError("Handling VMEXIT: %s (%u), %lu (0x%lx)\n", + v3_vmx_exit_code_to_str(exit_info->exit_reason), + exit_info->exit_reason, + exit_info->exit_qual, exit_info->exit_qual); + + v3_print_vmcs(); + */ +#ifdef CONFIG_TELEMETRY + if (info->enable_telemetry) { + v3_telemetry_start_exit(info); } +#endif - return 0; -} + switch (exit_info->exit_reason) { + case VMEXIT_INFO_EXCEPTION_OR_NMI: { + pf_error_t error_code = *(pf_error_t *)&(exit_info->int_err); -static int inline check_vmcs_read(vmcs_field_t field, void * val) -{ - int ret = 0; - ret = vmcs_read(field,val); - if(ret != VMX_SUCCESS) { - PrintError("VMREAD error on %s!: %d\n", v3_vmcs_field_to_str(field), ret); - return 1; + // JRL: Change "0x0e" to a macro value + if ((uint8_t)exit_info->int_info == 0x0e) { +#ifdef CONFIG_DEBUG_SHADOW_PAGING + PrintDebug("Page Fault at %p error_code=%x\n", (void *)exit_info->exit_qual, *(uint32_t *)&error_code); +#endif + + if (info->shdw_pg_mode == SHADOW_PAGING) { + if (v3_handle_shadow_pagefault(info, (addr_t)exit_info->exit_qual, error_code) == -1) { + PrintError("Error handling shadow page fault\n"); + return -1; + } + } else { + PrintError("Page fault in unimplemented paging mode\n"); + return -1; + } + } else { + PrintError("Unknown exception: 0x%x\n", (uint8_t)exit_info->int_info); + v3_print_GPRs(info); + return -1; + } + break; + } + + case VMEXIT_INVLPG: + if (info->shdw_pg_mode == SHADOW_PAGING) { + if (v3_handle_shadow_invlpg(info) == -1) { + PrintError("Error handling INVLPG\n"); + return -1; + } + } + + break; + case VMEXIT_CPUID: + if (v3_handle_cpuid(info) == -1) { + PrintError("Error Handling CPUID instruction\n"); + return -1; + } + + break; + case VMEXIT_RDMSR: + if (v3_handle_msr_read(info) == -1) { + PrintError("Error handling MSR Read\n"); + return -1; + } + + break; + case VMEXIT_WRMSR: + if (v3_handle_msr_write(info) == -1) { + PrintError("Error handling MSR Write\n"); + return -1; + } + + break; + case VMEXIT_VMCALL: + /* + * Hypercall + */ + + // VMCALL is a 3 byte op + // We do this early because some hypercalls can change the rip... + info->rip += 3; + + if (v3_handle_hypercall(info) == -1) { + return -1; + } + break; + case VMEXIT_IO_INSTR: { + struct vmx_exit_io_qual * io_qual = (struct vmx_exit_io_qual *)&(exit_info->exit_qual); + + if (io_qual->dir == 0) { + if (io_qual->string) { + if (v3_handle_vmx_io_outs(info) == -1) { + PrintError("Error in outs IO handler\n"); + return -1; + } + } else { + if (v3_handle_vmx_io_out(info) == -1) { + PrintError("Error in out IO handler\n"); + return -1; + } + } + } else { + if (io_qual->string) { + if(v3_handle_vmx_io_ins(info) == -1) { + PrintError("Error in ins IO handler\n"); + return -1; + } + } else { + if (v3_handle_vmx_io_in(info) == -1) { + PrintError("Error in in IO handler\n"); + return -1; + } + } + } + break; + } + case VMEXIT_CR_REG_ACCESSES: { + struct vmx_exit_cr_qual * cr_qual = (struct vmx_exit_cr_qual *)&(exit_info->exit_qual); + + // PrintDebug("Control register: %d\n", cr_qual->access_type); + switch(cr_qual->cr_id) { + case 0: + //PrintDebug("Handling CR0 Access\n"); + if (v3_vmx_handle_cr0_access(info, cr_qual, exit_info) == -1) { + PrintError("Error in CR0 access handler\n"); + return -1; + } + break; + case 3: + //PrintDebug("Handling CR3 Access\n"); + if (v3_vmx_handle_cr3_access(info, cr_qual) == -1) { + PrintError("Error in CR3 access handler\n"); + return -1; + } + break; + default: + PrintError("Unhandled CR access: %d\n", cr_qual->cr_id); + return -1; + } + + info->rip += exit_info->instr_len; + + break; + } + case VMEXIT_HLT: + PrintDebug("Guest halted\n"); + + if (v3_handle_halt(info) == -1) { + PrintError("Error handling halt instruction\n"); + return -1; + } + + break; + case VMEXIT_PAUSE: + // Handled as NOP + info->rip += 2; + + break; + case VMEXIT_EXTERNAL_INTR: + // Interrupts are handled outside switch + break; + case VMEXIT_INTR_WINDOW: + + vmcs_read(VMCS_PROC_CTRLS, &(vmx_info->pri_proc_ctrls.value)); + vmx_info->pri_proc_ctrls.int_wndw_exit = 0; + vmcs_write(VMCS_PROC_CTRLS, vmx_info->pri_proc_ctrls.value); + +#ifdef CONFIG_DEBUG_INTERRUPTS + PrintDebug("Interrupts available again! (RIP=%llx)\n", info->rip); +#endif + + break; + default: + PrintError("Unhandled VMEXIT: %s (%u), %lu (0x%lx)\n", + v3_vmx_exit_code_to_str(exit_info->exit_reason), + exit_info->exit_reason, + exit_info->exit_qual, exit_info->exit_qual); + return -1; + } + +#ifdef CONFIG_TELEMETRY + if (info->enable_telemetry) { + v3_telemetry_end_exit(info, exit_info->exit_reason); } +#endif return 0; } -int v3_handle_vmx_exit(struct v3_gprs * gprs) -{ - uint32_t exit_reason; - ulong_t exit_qual; +static const char VMEXIT_INFO_EXCEPTION_OR_NMI_STR[] = "VMEXIT_INFO_EXCEPTION_OR_NMI"; +static const char VMEXIT_EXTERNAL_INTR_STR[] = "VMEXIT_EXTERNAL_INTR"; +static const char VMEXIT_TRIPLE_FAULT_STR[] = "VMEXIT_TRIPLE_FAULT"; +static const char VMEXIT_INIT_SIGNAL_STR[] = "VMEXIT_INIT_SIGNAL"; +static const char VMEXIT_STARTUP_IPI_STR[] = "VMEXIT_STARTUP_IPI"; +static const char VMEXIT_IO_SMI_STR[] = "VMEXIT_IO_SMI"; +static const char VMEXIT_OTHER_SMI_STR[] = "VMEXIT_OTHER_SMI"; +static const char VMEXIT_INTR_WINDOW_STR[] = "VMEXIT_INTR_WINDOW"; +static const char VMEXIT_NMI_WINDOW_STR[] = "VMEXIT_NMI_WINDOW"; +static const char VMEXIT_TASK_SWITCH_STR[] = "VMEXIT_TASK_SWITCH"; +static const char VMEXIT_CPUID_STR[] = "VMEXIT_CPUID"; +static const char VMEXIT_HLT_STR[] = "VMEXIT_HLT"; +static const char VMEXIT_INVD_STR[] = "VMEXIT_INVD"; +static const char VMEXIT_INVLPG_STR[] = "VMEXIT_INVLPG"; +static const char VMEXIT_RDPMC_STR[] = "VMEXIT_RDPMC"; +static const char VMEXIT_RDTSC_STR[] = "VMEXIT_RDTSC"; +static const char VMEXIT_RSM_STR[] = "VMEXIT_RSM"; +static const char VMEXIT_VMCALL_STR[] = "VMEXIT_VMCALL"; +static const char VMEXIT_VMCLEAR_STR[] = "VMEXIT_VMCLEAR"; +static const char VMEXIT_VMLAUNCH_STR[] = "VMEXIT_VMLAUNCH"; +static const char VMEXIT_VMPTRLD_STR[] = "VMEXIT_VMPTRLD"; +static const char VMEXIT_VMPTRST_STR[] = "VMEXIT_VMPTRST"; +static const char VMEXIT_VMREAD_STR[] = "VMEXIT_VMREAD"; +static const char VMEXIT_VMRESUME_STR[] = "VMEXIT_VMRESUME"; +static const char VMEXIT_VMWRITE_STR[] = "VMEXIT_VMWRITE"; +static const char VMEXIT_VMXOFF_STR[] = "VMEXIT_VMXOFF"; +static const char VMEXIT_VMXON_STR[] = "VMEXIT_VMXON"; +static const char VMEXIT_CR_REG_ACCESSES_STR[] = "VMEXIT_CR_REG_ACCESSES"; +static const char VMEXIT_MOV_DR_STR[] = "VMEXIT_MOV_DR"; +static const char VMEXIT_IO_INSTR_STR[] = "VMEXIT_IO_INSTR"; +static const char VMEXIT_RDMSR_STR[] = "VMEXIT_RDMSR"; +static const char VMEXIT_WRMSR_STR[] = "VMEXIT_WRMSR"; +static const char VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE_STR[] = "VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE"; +static const char VMEXIT_ENTRY_FAIL_MSR_LOAD_STR[] = "VMEXIT_ENTRY_FAIL_MSR_LOAD"; +static const char VMEXIT_MWAIT_STR[] = "VMEXIT_MWAIT"; +static const char VMEXIT_MONITOR_STR[] = "VMEXIT_MONITOR"; +static const char VMEXIT_PAUSE_STR[] = "VMEXIT_PAUSE"; +static const char VMEXIT_ENTRY_FAILURE_MACHINE_CHECK_STR[] = "VMEXIT_ENTRY_FAILURE_MACHINE_CHECK"; +static const char VMEXIT_TPR_BELOW_THRESHOLD_STR[] = "VMEXIT_TPR_BELOW_THRESHOLD"; +static const char VMEXIT_APIC_STR[] = "VMEXIT_APIC"; +static const char VMEXIT_GDTR_IDTR_STR[] = "VMEXIT_GDTR_IDTR"; +static const char VMEXIT_LDTR_TR_STR[] = "VMEXIT_LDTR_TR"; +static const char VMEXIT_EPT_VIOLATION_STR[] = "VMEXIT_EPT_VIOLATION"; +static const char VMEXIT_EPT_CONFIG_STR[] = "VMEXIT_EPT_CONFIG"; +static const char VMEXIT_INVEPT_STR[] = "VMEXIT_INVEPT"; +static const char VMEXIT_RDTSCP_STR[] = "VMEXIT_RDTSCP"; +static const char VMEXIT_EXPIRED_PREEMPT_TIMER_STR[] = "VMEXIT_EXPIRED_PREEMPT_TIMER"; +static const char VMEXIT_INVVPID_STR[] = "VMEXIT_INVVPID"; +static const char VMEXIT_WBINVD_STR[] = "VMEXIT_WBINVD"; +static const char VMEXIT_XSETBV_STR[] = "VMEXIT_XSETBV"; - check_vmcs_read(VMCS_EXIT_REASON, &exit_reason); - check_vmcs_read(VMCS_EXIT_QUAL, &exit_qual); - PrintDebug("VMX Exit taken, id-qual: %x-%ld\n", exit_reason, exit_qual); - return -1; +const char * v3_vmx_exit_code_to_str(vmx_exit_t exit) +{ + switch(exit) { + case VMEXIT_INFO_EXCEPTION_OR_NMI: + return VMEXIT_INFO_EXCEPTION_OR_NMI_STR; + case VMEXIT_EXTERNAL_INTR: + return VMEXIT_EXTERNAL_INTR_STR; + case VMEXIT_TRIPLE_FAULT: + return VMEXIT_TRIPLE_FAULT_STR; + case VMEXIT_INIT_SIGNAL: + return VMEXIT_INIT_SIGNAL_STR; + case VMEXIT_STARTUP_IPI: + return VMEXIT_STARTUP_IPI_STR; + case VMEXIT_IO_SMI: + return VMEXIT_IO_SMI_STR; + case VMEXIT_OTHER_SMI: + return VMEXIT_OTHER_SMI_STR; + case VMEXIT_INTR_WINDOW: + return VMEXIT_INTR_WINDOW_STR; + case VMEXIT_NMI_WINDOW: + return VMEXIT_NMI_WINDOW_STR; + case VMEXIT_TASK_SWITCH: + return VMEXIT_TASK_SWITCH_STR; + case VMEXIT_CPUID: + return VMEXIT_CPUID_STR; + case VMEXIT_HLT: + return VMEXIT_HLT_STR; + case VMEXIT_INVD: + return VMEXIT_INVD_STR; + case VMEXIT_INVLPG: + return VMEXIT_INVLPG_STR; + case VMEXIT_RDPMC: + return VMEXIT_RDPMC_STR; + case VMEXIT_RDTSC: + return VMEXIT_RDTSC_STR; + case VMEXIT_RSM: + return VMEXIT_RSM_STR; + case VMEXIT_VMCALL: + return VMEXIT_VMCALL_STR; + case VMEXIT_VMCLEAR: + return VMEXIT_VMCLEAR_STR; + case VMEXIT_VMLAUNCH: + return VMEXIT_VMLAUNCH_STR; + case VMEXIT_VMPTRLD: + return VMEXIT_VMPTRLD_STR; + case VMEXIT_VMPTRST: + return VMEXIT_VMPTRST_STR; + case VMEXIT_VMREAD: + return VMEXIT_VMREAD_STR; + case VMEXIT_VMRESUME: + return VMEXIT_VMRESUME_STR; + case VMEXIT_VMWRITE: + return VMEXIT_VMWRITE_STR; + case VMEXIT_VMXOFF: + return VMEXIT_VMXOFF_STR; + case VMEXIT_VMXON: + return VMEXIT_VMXON_STR; + case VMEXIT_CR_REG_ACCESSES: + return VMEXIT_CR_REG_ACCESSES_STR; + case VMEXIT_MOV_DR: + return VMEXIT_MOV_DR_STR; + case VMEXIT_IO_INSTR: + return VMEXIT_IO_INSTR_STR; + case VMEXIT_RDMSR: + return VMEXIT_RDMSR_STR; + case VMEXIT_WRMSR: + return VMEXIT_WRMSR_STR; + case VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE: + return VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE_STR; + case VMEXIT_ENTRY_FAIL_MSR_LOAD: + return VMEXIT_ENTRY_FAIL_MSR_LOAD_STR; + case VMEXIT_MWAIT: + return VMEXIT_MWAIT_STR; + case VMEXIT_MONITOR: + return VMEXIT_MONITOR_STR; + case VMEXIT_PAUSE: + return VMEXIT_PAUSE_STR; + case VMEXIT_ENTRY_FAILURE_MACHINE_CHECK: + return VMEXIT_ENTRY_FAILURE_MACHINE_CHECK_STR; + case VMEXIT_TPR_BELOW_THRESHOLD: + return VMEXIT_TPR_BELOW_THRESHOLD_STR; + case VMEXIT_APIC: + return VMEXIT_APIC_STR; + case VMEXIT_GDTR_IDTR: + return VMEXIT_GDTR_IDTR_STR; + case VMEXIT_LDTR_TR: + return VMEXIT_LDTR_TR_STR; + case VMEXIT_EPT_VIOLATION: + return VMEXIT_EPT_VIOLATION_STR; + case VMEXIT_EPT_CONFIG: + return VMEXIT_EPT_CONFIG_STR; + case VMEXIT_INVEPT: + return VMEXIT_INVEPT_STR; + case VMEXIT_RDTSCP: + return VMEXIT_RDTSCP_STR; + case VMEXIT_EXPIRED_PREEMPT_TIMER: + return VMEXIT_EXPIRED_PREEMPT_TIMER_STR; + case VMEXIT_INVVPID: + return VMEXIT_INVVPID_STR; + case VMEXIT_WBINVD: + return VMEXIT_WBINVD_STR; + case VMEXIT_XSETBV: + return VMEXIT_XSETBV_STR; + } + return NULL; } +