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=247422a4c9b9fe1e561c5a028d5cd0d9aa66502c;hb=5bf6d0c260240e314876a2fca8e3fd56bd6a1029;hpb=df78f581f8c54d54f2ed246cf6ef609f3bf6d4db diff --git a/palacios/src/palacios/vmx_handler.c b/palacios/src/palacios/vmx_handler.c index 247422a..3fa4f98 100644 --- a/palacios/src/palacios/vmx_handler.c +++ b/palacios/src/palacios/vmx_handler.c @@ -17,511 +17,375 @@ * redistribute, and modify it as specified in the file "V3VEE_LICENSE". */ -static int PanicUnhandledVMExit(struct VM *vm) -{ - PrintInfo("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason); - PrintTrace("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason); - PrintTrace_VMCS_ALL(); - PrintTrace_VMX_Regs(&(vm->registers)); - VMXPanic(); - return 0; -} - - - - - -static int HandleVMPrintsAndPanics(struct VM *vm, uint_t port, uint_t data) -{ - if (port==VMXASSIST_INFO_PORT && - (vm->state == VM_VMXASSIST_STARTUP || - vm->state == VM_VMXASSIST_V8086_BIOS || - vm->state == VM_VMXASSIST_V8086)) { - // Communication channel from VMXAssist - PrintTrace("VMXASSIST Output Port\n"); - PrintDebug("%c",data&0xff); - return 1; - } - - if ((port==ROMBIOS_PANIC_PORT || - port==ROMBIOS_PANIC_PORT2 || - port==ROMBIOS_DEBUG_PORT || - port==ROMBIOS_INFO_PORT) && - (vm->state==VM_VMXASSIST_V8086_BIOS)) { - // rombios is communicating - PrintTrace("ROMBIOS Output Port\n"); - // PrintDebug("%c",data&0xff); - return 1; - } - - if (port==BOOT_STATE_CARD_PORT && vm->state==VM_VMXASSIST_V8086_BIOS) { - // rombios is sending something to the display card - PrintTrace("Hex Display: 0x%x\n",data&0xff); - return 1; - } - return 0; -} - -static int HandleInOutExit(struct VM *vm) -{ - uint_t address; - - struct VMCSExitInfoFields *exitinfo = &(vm->vmcs.exitInfoFields); - struct VMExitIOQual * qual = (struct VMExitIOQual *)&(vm->vmcs.exitInfoFields.qualification); - struct VMXRegs *regs = &(vm->registers); - - address=GetLinearIP(vm); - - PrintTrace("Handling Input/Output Instruction Exit\n"); - - PrintTrace_VMX_Regs(regs); - - PrintTrace("Qualifications=0x%x\n", exitinfo->qualification); - PrintTrace("Reason=0x%x\n", exitinfo->reason); - PrintTrace("IO Port: 0x%x (%d)\n", qual->port, qual->port); - PrintTrace("Instruction Info=%x\n", exitinfo->instrInfo); - PrintTrace("%x : %s %s %s instruction of length %d for %d bytes from/to port 0x%x\n", - address, - qual->dir == 0 ? "output" : "input", - qual->string ==0 ? "nonstring" : "STRING", - qual->REP == 0 ? "with no rep" : "WITH REP", - exitinfo->instrLength, - qual->accessSize==0 ? 1 : qual->accessSize==1 ? 2 : 4, - qual->port); - - if ((qual->port == PIC_MASTER_CMD_ISR_PORT) || - (qual->port == PIC_MASTER_IMR_PORT) || - (qual->port == PIC_SLAVE_CMD_ISR_PORT) || - (qual->port == PIC_SLAVE_IMR_PORT)) { - PrintTrace( "PIC Access\n"); - } - - - if ((qual->dir == 1) && (qual->REP == 0) && (qual->string == 0)) { - char byte = In_Byte(qual->port); - - vm->vmcs.guestStateArea.rip += exitinfo->instrLength; - regs->eax = (regs->eax & 0xffffff00) | byte; - PrintTrace("Returning 0x%x in eax\n", (regs->eax)); - } - - if (qual->dir==0 && qual->REP==0 && qual->string==0) { - // See if we need to handle the outb as a signal or - // print from the VM - if (HandleVMPrintsAndPanics(vm,qual->port,regs->eax)) { - } else { - // If not, just go ahead and do the outb - Out_Byte(qual->port,regs->eax); - PrintTrace("Wrote 0x%x to port\n",(regs->eax)); +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_TELEMETRY +#include +#endif + + + + +/* 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); } - vm->vmcs.guestStateArea.rip += exitinfo->instrLength; - } - - return 0; -} - - -static int HandleExternalIRQExit(struct VM *vm) -{ - struct VMCSExitInfoFields * exitinfo = &(vm->vmcs.exitInfoFields); - struct VMExitIntInfo * intInfo = (struct VMExitIntInfo *)&(vm->vmcs.exitInfoFields.intInfo); - - PrintTrace("External Interrupt captured\n"); - PrintTrace("IntInfo: %x\n", exitinfo->intInfo); - - - if (!intInfo->valid) { - // interrupts are off, but this interrupt is not acknoledged (still pending) - // so we turn on interrupts to deliver appropriately in the - // host - PrintTrace("External Interrupt is invald. Turning Interrupts back on\n"); - asm("sti"); - return 0; - } - - // At this point, interrupts are off and the interrupt has been - // acknowledged. We will now handle the interrupt ourselves - // and turn interrupts back on in the host - - PrintTrace("type: %d\n", intInfo->type); - PrintTrace("number: %d\n", intInfo->nr); - - PrintTrace("Interrupt %d occuring now and handled by HandleExternalIRQExit\n",intInfo->nr); - - switch (intInfo->type) { - case 0: { // ext. IRQ - // In the following, we construct an "int x" instruction - // where x is the specific interrupt number that is raised - // then we execute that instruciton - // because we are in host context, that means it is delivered as normal - // through the host IDT - - ((char*)(&&ext_int_seq_start))[1] = intInfo->nr; - - PrintTrace("Interrupt instruction setup done %x\n", *((ushort_t *)(&&ext_int_seq_start))); - -ext_int_seq_start: - asm("int $0"); - } - - break; - case 2: // NMI - PrintTrace("Type: NMI\n"); - break; - case 3: // hw exception - PrintTrace("Type: HW Exception\n"); - break; - case 4: // sw exception - PrintTrace("Type: SW Exception\n"); - break; - default: - PrintTrace("Invalid Interrupt Type\n"); - return -1; - } - - if (intInfo->valid && intInfo->errorCode) { - PrintTrace("IntError: %x\n", exitinfo->intErrorCode); - } - - - return 0; - -} - - - - - - - -static int HandleExceptionOrNMI(struct VM *vm) -{ - struct Instruction inst; - uint_t num; - uint_t type; - uint_t errorvalid; - uint_t error; - uint_t ext=0; - uint_t idt=0; - uint_t ti=0; - uint_t selectorindex=0; - - PrintTrace("Exception or NMI occurred\n"); - - num=vm->vmcs.exitInfoFields.intInfo & 0xff; - type=(vm->vmcs.exitInfoFields.intInfo & 0x700)>>8; - errorvalid=(vm->vmcs.exitInfoFields.intInfo & 0x800)>>11; - if (errorvalid) { - error=vm->vmcs.exitInfoFields.intErrorCode; - ext=error&0x1; - idt=(error&0x2)>>1; - ti=(error&0x4)>>2; - selectorindex=(error>>3)&0xffff; - } - - PrintTrace("Exception %d now - handled by HandleExceptionOrNMI\n",num); - - PrintTrace("Exception Number %u : %s\n", num, exception_names[num]); - PrintTrace("Exception Type %u : %s\n", type, exception_type_names[type]); - if (errorvalid) { - if (ext) { - PrintTrace("External\n"); - } else { - PrintTrace("%s - Selector Index is %u\n", idt ? "IDT" : ti ? "LDT" : "GDT", selectorindex); +#endif + + switch (exit_info->exit_reason) { + case VMEXIT_INFO_EXCEPTION_OR_NMI: { + pf_error_t error_code = *(pf_error_t *)&(exit_info->int_err); + + + // 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; } - } - - DecodeCurrentInstruction(vm,&inst); - - if (inst.type==VM_MOV_TO_CR0) { - PrintTrace("MOV TO CR0, oldvalue=0x%x, newvalue=0x%x\n",inst.input2, inst.input1); - if ((inst.input2 & CR0_PE) && !(inst.input1 & CR0_PE) && vm->state==VM_VMXASSIST_STARTUP) { - // This is VMXAssist signalling for us to turn on V8086 mode and - // jump into the bios - PrintTrace("VMXAssist is signaling us for switch to V8086 mode and jump to 0xf000:fff0\n"); - SetupV8086ModeForBoot(vm); - goto leave; - } else { - PrintTrace("Instruction is a write to CR0, but we don't understand it so we'll just exec it\n"); - } - } - - - PrintTrace("Trying to execute the faulting instruction in VMM context now\n"); - ExecFaultingInstructionInVMM(vm); - - leave: - // - //PanicUnhandledVMExit(vmcs,regs); - //VMXPanic(); - return 0; -} - - - - -int Do_VMM(struct VMXRegs regs) -{ - - ullong_t vmcs_ptr = 0; - uint_t vmcs_ptr_low = 0; - int ret = 0; - uint_t vmx_abort = 0; - - - - PrintTrace("Vm Exit\n"); - ret = VMCS_STORE(&vmcs_ptr); - vmcs_ptr &= 0xffffffff; - vmcs_ptr_low += vmcs_ptr; - - - - - PrintTrace("ret=%d\n", ret); - PrintTrace("Revision: %x\n", *(uint_t *)(vmcs_ptr_low)); - vmx_abort = *(uint_t*)(((char *)vmcs_ptr_low)+4); - - struct VM *vm = FindVM(); - - if (vmx_abort != 0) { - PrintTrace("VM ABORTED w/ code: %x\n", vmx_abort); - return -1; - } - - vm->registers = regs; - - if (CopyOutVMCSData(&(vm->vmcs)) != 0) { - PrintTrace("Could not copy out VMCS\n"); - return -1; - } - - - PrintTrace("Guest esp: 0x%x (%u)\n", vm->vmcs.guestStateArea.rsp, vm->vmcs.guestStateArea.rsp); - - PrintTrace("VM Exit for reason: %d (%x)\n", - vm->vmcs.exitInfoFields.reason & 0x00000fff, - vm->vmcs.exitInfoFields.reason); - - if (vm->vmcs.exitInfoFields.reason & (0x1<<29) ) { - PrintTrace("VM Exit is from VMX root operation. Panicking\n"); - VMXPanic(); - } - - if (vm->vmcs.exitInfoFields.reason & (0x1<<31) ) { - PrintTrace("VM Exit is due to a VM entry failure. Shouldn't happen here. Panicking\n"); - PrintTrace_VMCSData(&(vm->vmcs)); - VMXPanic(); - } - - switch (vm->vmcs.exitInfoFields.reason) { - case VM_EXIT_REASON_INFO_EXCEPTION_OR_NMI: - ret = HandleExceptionOrNMI(vm); - break; - case VM_EXIT_REASON_EXTERNAL_INTR: - ret = HandleExternalIRQExit(vm); - break; - case VM_EXIT_REASON_TRIPLE_FAULT: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_INIT_SIGNAL: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_STARTUP_IPI: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_IO_SMI: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_OTHER_SMI: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_INTR_WINDOW: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_NMI_WINDOW: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_TASK_SWITCH: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_CPUID: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_INVD: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_INVLPG: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_RDPMC: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_RDTSC: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_RSM: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMCALL: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMCLEAR: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMLAUNCH: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMPTRLD: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMPTRST: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMREAD: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMRESUME: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMWRITE: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMXOFF: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_VMXON: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_CR_REG_ACCESSES: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_MOV_DR: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_IO_INSTR: - ret = HandleInOutExit(vm); - break; - case VM_EXIT_REASON_RDMSR: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_WRMSR: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_ENTRY_FAIL_INVALID_GUEST_STATE: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_ENTRY_FAIL_MSR_LOAD: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_MWAIT: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_MONITOR: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_PAUSE: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_ENTRY_FAILURE_MACHINE_CHECK: - ret = PanicUnhandledVMExit(vm); - break; - case VM_EXIT_REASON_TPR_BELOW_THRESHOLD: - ret = PanicUnhandledVMExit(vm); - break; - default: - ret = PanicUnhandledVMExit(vm); - break; - } - - - regs = vm->registers; - CopyInVMCSData(&(vm->vmcs)); - - /* - { - VMCS_CLEAR(vmcs_ptr); +#ifdef CONFIG_TELEMETRY + if (info->enable_telemetry) { + v3_telemetry_end_exit(info, exit_info->exit_reason); } - */ +#endif - PrintTrace("Returning from Do_VMM: %d\n", ret); - - return ret; + return 0; } - - - - - -// simply execute the instruction that is faulting and return -static int ExecFaultingInstructionInVMM(struct VM *vm) +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"; + +const char * v3_vmx_exit_code_to_str(vmx_exit_t exit) { - uint_t address = GetLinearIP(vm); - myregs = (uint_t)&(vm->registers); - - - PrintTrace("About the execute faulting instruction!\n"); - PrintTrace("Instruction is:\n"); - PrintTraceMemDump((void*)(address),vm->vmcs.exitInfoFields.instrLength); - - - PrintTrace("The template code is:\n"); - PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN); - - // clone the template code - //memcpy(&&template_code,code,MAX_CODE); - - // clean up the nop field - memset(&&template_code+INSTR_OFFSET_START,*((uchar_t *)(&&template_code+0)),NOP_SEQ_LEN); - // overwrite the nops with the faulting instruction - memcpy(&&template_code+INSTR_OFFSET_START, (void*)(address),vm->vmcs.exitInfoFields.instrLength); - - PrintTrace("Finished modifying the template code, which now is:\n"); - PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN); - - PrintTrace("Now entering modified template code\n"); - - - template_code: - // Template code stores current registers, - // restores registers, has a landing pad of noops - // that will be modified, restores current regs, and then returns - // - // Note that this currently ignores cr0, cr3, cr4, dr7, rsp, rip, and rflags - // it also blythly assumes it can exec the instruction in protected mode - // - __asm__ __volatile__ ("nop\n" // for cloning purposes (1 byte) - "pusha\n" // push our current regs onto the current stack (1 byte) - "movl %0, %%eax\n" // Get oldesp location (5 bytes) - "movl %%esp, (%%eax)\n" // store the current stack pointer in oldesp (2 bytes) - "movl %1, %%eax\n" // Get regs location (5 bytes) - "movl (%%eax), %%esp\n" // point esp at regs (2 bytes) - "popa\n" // now we have the VM registers restored (1 byte) - "nop\n" // now we execute the actual instruction (1 byte x 10) - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - "nop\n" // now we execute the actual instruction - // need to copy back to the VM registers! - "movl %0, %%eax\n" // recapture oldesp location (5 bytes) - "movl (%%eax), %%esp\n" // now we'll get our esp back from oldesp (2 bytes) - "popa\n" // and restore our GP regs and we're done (1 byte) - : "=m"(oldesp) - : "m"(myregs) - ); - - PrintTrace("Survived executing the faulting instruction and returning.\n"); - - vm->vmcs.guestStateArea.rip += vm->vmcs.exitInfoFields.instrLength; - - return 0; - + 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; }