X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmx_handler.c;h=f872aeefb4cc95e7d269d82a3fff8dfa3d9d4036;hb=61597ea2c5ccace036d8a65e429e32b8f8a7ed4a;hp=247422a4c9b9fe1e561c5a028d5cd0d9aa66502c;hpb=df78f581f8c54d54f2ed246cf6ef609f3bf6d4db;p=palacios.git diff --git a/palacios/src/palacios/vmx_handler.c b/palacios/src/palacios/vmx_handler.c index 247422a..f872aee 100644 --- a/palacios/src/palacios/vmx_handler.c +++ b/palacios/src/palacios/vmx_handler.c @@ -17,511 +17,397 @@ * redistribute, and modify it as specified in the file "V3VEE_LICENSE". */ -static int PanicUnhandledVMExit(struct VM *vm) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static int inline check_vmcs_write(vmcs_field_t field, addr_t val) { - 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; -} - - - + 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); + } -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; + return ret; } -static int HandleInOutExit(struct VM *vm) +static int inline check_vmcs_read(vmcs_field_t field, void * val) { - 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)); + 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); } - vm->vmcs.guestStateArea.rip += exitinfo->instrLength; - } - return 0; -} + return ret; +} +static void inline translate_access_to_v3_seg(struct vmcs_segment_access * access, + struct v3_segment * v3_seg) { + v3_seg->type = access->type; + v3_seg->system = access->desc_type; + v3_seg->dpl = access->dpl; + v3_seg->present = access->present; + v3_seg->avail = access->avail; + v3_seg->long_mode = access->long_mode; + v3_seg->db = access->db; + v3_seg->granularity = access->granularity; +} -static int HandleExternalIRQExit(struct VM *vm) +static int load_vmcs_guest_state(struct guest_info * info) { - 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); + struct vmcs_segment_access access; + int ret = 0; + // JRL: Add error checking - 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); - } - + memset(&access, 0, sizeof(access)); - return 0; + /* CS Segment */ + check_vmcs_read(VMCS_GUEST_CS_BASE, &(info->segments.cs.base)); + check_vmcs_read(VMCS_GUEST_CS_SELECTOR, &(info->segments.cs.selector)); + check_vmcs_read(VMCS_GUEST_CS_LIMIT, &(info->segments.cs.limit)); + check_vmcs_read(VMCS_GUEST_CS_ACCESS, &(access.value)); -} + translate_access_to_v3_seg(&access, &(info->segments.cs)); + /* SS Segment */ + check_vmcs_read(VMCS_GUEST_SS_BASE, &(info->segments.ss.base)); + check_vmcs_read(VMCS_GUEST_SS_SELECTOR, &(info->segments.ss.selector)); + check_vmcs_read(VMCS_GUEST_SS_LIMIT, &(info->segments.ss.limit)); + check_vmcs_read(VMCS_GUEST_SS_ACCESS, &(access.value)); + translate_access_to_v3_seg(&access, &(info->segments.ss)); + /* DS Segment */ + check_vmcs_read(VMCS_GUEST_DS_BASE, &(info->segments.ds.base)); + check_vmcs_read(VMCS_GUEST_DS_SELECTOR, &(info->segments.ds.selector)); + check_vmcs_read(VMCS_GUEST_DS_LIMIT, &(info->segments.ds.limit)); + check_vmcs_read(VMCS_GUEST_DS_ACCESS, &(access.value)); + translate_access_to_v3_seg(&access, &(info->segments.ds)); + /* ES Segment */ + check_vmcs_read(VMCS_GUEST_ES_BASE, &(info->segments.es.base)); + check_vmcs_read(VMCS_GUEST_ES_SELECTOR, &(info->segments.es.selector)); + check_vmcs_read(VMCS_GUEST_ES_LIMIT, &(info->segments.es.limit)); + check_vmcs_read(VMCS_GUEST_ES_ACCESS, &(access.value)); + translate_access_to_v3_seg(&access, &(info->segments.es)); -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); - } - } - - 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; -} + /* FS Segment */ + check_vmcs_read(VMCS_GUEST_FS_BASE, &(info->segments.fs.base)); + check_vmcs_read(VMCS_GUEST_FS_SELECTOR, &(info->segments.fs.selector)); + check_vmcs_read(VMCS_GUEST_FS_LIMIT, &(info->segments.fs.limit)); + check_vmcs_read(VMCS_GUEST_FS_ACCESS, &(access.value)); + translate_access_to_v3_seg(&access, &(info->segments.fs)); + /* GS Segment */ + check_vmcs_read(VMCS_GUEST_GS_BASE, &(info->segments.gs.base)); + check_vmcs_read(VMCS_GUEST_GS_SELECTOR, &(info->segments.gs.selector)); + check_vmcs_read(VMCS_GUEST_GS_LIMIT, &(info->segments.gs.limit)); + check_vmcs_read(VMCS_GUEST_GS_ACCESS, &(access.value)); + translate_access_to_v3_seg(&access, &(info->segments.gs)); -int Do_VMM(struct VMXRegs regs) -{ + /* LDTR Segment */ + check_vmcs_read(VMCS_GUEST_LDTR_BASE, &(info->segments.ldtr.base)); + check_vmcs_read(VMCS_GUEST_LDTR_SELECTOR, &(info->segments.ldtr.selector)); + check_vmcs_read(VMCS_GUEST_LDTR_LIMIT, &(info->segments.ldtr.limit)); + check_vmcs_read(VMCS_GUEST_LDTR_ACCESS, &(access.value)); - ullong_t vmcs_ptr = 0; - uint_t vmcs_ptr_low = 0; - int ret = 0; - uint_t vmx_abort = 0; + translate_access_to_v3_seg(&access, &(info->segments.ldtr)); + /* TR Segment */ + check_vmcs_read(VMCS_GUEST_TR_BASE, &(info->segments.tr.base)); + check_vmcs_read(VMCS_GUEST_TR_SELECTOR, &(info->segments.tr.selector)); + check_vmcs_read(VMCS_GUEST_TR_LIMIT, &(info->segments.tr.limit)); + check_vmcs_read(VMCS_GUEST_TR_ACCESS, &(access.value)); - - PrintTrace("Vm Exit\n"); - ret = VMCS_STORE(&vmcs_ptr); - vmcs_ptr &= 0xffffffff; - vmcs_ptr_low += vmcs_ptr; + translate_access_to_v3_seg(&access, &(info->segments.tr)); + /* GDTR Segment */ + check_vmcs_read(VMCS_GUEST_GDTR_BASE, &(info->segments.gdtr.base)); + check_vmcs_read(VMCS_GUEST_GDTR_LIMIT, &(info->segments.gdtr.limit)); + + /* IDTR Segment */ + check_vmcs_read(VMCS_GUEST_IDTR_BASE, &(info->segments.idtr.base)); + check_vmcs_read(VMCS_GUEST_IDTR_LIMIT, &(info->segments.idtr.limit)); + + + /* + * Read the control state + */ + check_vmcs_read(VMCS_GUEST_RIP, &(info->rip)); + check_vmcs_read(VMCS_GUEST_RSP, &(info->vm_regs.rsp)); + check_vmcs_read(VMCS_GUEST_CR0, &(info->ctrl_regs.cr0)); + check_vmcs_read(VMCS_CR0_READ_SHDW, &(info->shdw_pg_state.guest_cr0)); + check_vmcs_read(VMCS_GUEST_CR3, &(info->ctrl_regs.cr3)); + check_vmcs_read(VMCS_GUEST_CR4, &(info->ctrl_regs.cr4)); + + return ret; +} +#if 0 +static void setup_v8086_mode_for_boot(struct guest_info * info) +{ - 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(); + ((struct vmx_data *)info->vmm_data)->state = VMXASSIST_V8086_BIOS; + struct rflags * flags = (struct rflags *)&(info->ctrl_regs.rflags); + flags->rsvd1 = 1; + flags->vm = 1; + flags->iopl = 3; - if (vmx_abort != 0) { - PrintTrace("VM ABORTED w/ code: %x\n", vmx_abort); - return -1; - } + info->rip = 0xfff0; + + /* Zero the segment registers */ + memset(&(info->segments), 0, sizeof(struct v3_segment)*6); - 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)); + info->segments.cs.selector = 0xf000; + info->segments.cs.base = 0xf000 << 4; + info->segments.cs.limit = 0xffff; + info->segments.cs.type = 3; + info->segments.cs.system = 1; + info->segments.cs.dpl = 3; + info->segments.cs.present = 1; + info->segments.cs.granularity = 0; - /* - { - VMCS_CLEAR(vmcs_ptr); + int i; + + /* Set values for selectors ds through ss */ + struct v3_segment * seg_ptr = (struct v3_segment *)&(info->segments); + for(i = 1; i < 6 ; i++) { + seg_ptr[i].selector = 0x0000; + seg_ptr[i].base = 0x00000; + seg_ptr[i].limit = 0xffff; + seg_ptr[i].type = 3; + seg_ptr[i].system = 1; + seg_ptr[i].dpl = 3; + seg_ptr[i].present = 1; + seg_ptr[i].granularity = 0; } - */ - PrintTrace("Returning from Do_VMM: %d\n", ret); - - return ret; } +#endif + +static int inline handle_cr_access(struct guest_info * info, ulong_t exit_qual) { + struct vmexit_cr_qual * cr_qual = (struct vmexit_cr_qual *)&exit_qual; + + PrintDebug("Control register: %d\n", cr_qual->access_type); + + if (cr_qual->access_type < 2) { + v3_reg_t reg = 0; + + switch(cr_qual->gpr) { + case 0: + reg = info->vm_regs.rax; + break; + case 1: + reg = info->vm_regs.rcx; + break; + case 2: + reg = info->vm_regs.rdx; + break; + case 3: + reg = info->vm_regs.rbx; + break; + case 4: + reg = info->vm_regs.rsp; + break; + case 5: + reg = info->vm_regs.rbp; + break; + case 6: + reg = info->vm_regs.rsi; + break; + case 7: + reg = info->vm_regs.rdi; + break; + case 8: + reg = info->vm_regs.r8; + break; + case 9: + reg = info->vm_regs.r9; + break; + case 10: + reg = info->vm_regs.r10; + break; + case 11: + reg = info->vm_regs.r11; + break; + case 12: + reg = info->vm_regs.r11; + break; + case 13: + reg = info->vm_regs.r13; + break; + case 14: + reg = info->vm_regs.r14; + break; + case 15: + reg = info->vm_regs.r15; + break; + } + + if (cr_qual->cr_id == 0) { + uint32_t instr_len; + + vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len); + + if ( ~reg & CR0_PE ) { + + if (v3_vmxassist_ctx_switch(info) != 0) { + PrintError("Unable to execute VMXASSIST context switch!\n"); + return -1; + } + + load_vmcs_guest_state(info); + + ((struct vmx_data *)info->vmm_data)->state = VMXASSIST_ENABLED; + + PrintDebug("Loading vmxassist at RIP: 0x%p\n", (void *)info->rip); + return 0; + } else if (v3_vmx_handle_cr0_write(info, reg) != 0) { + PrintError("Could not handle CR0 Write\n"); + return -1; + } + + load_vmcs_guest_state(info); + + PrintDebug("Leaving VMXASSIST and entering protected mode at RIP: 0x%p\n", (void *)info->rip); + + return 0; + } + } + PrintError("Unhandled CR access\n"); + return -1; +} +/* At this point the GPRs are already copied into the guest_info state */ +int v3_handle_vmx_exit(struct v3_gprs * gprs, struct guest_info * info) { + uint32_t exit_reason; + ulong_t exit_qual; + check_vmcs_read(VMCS_EXIT_REASON, &exit_reason); + check_vmcs_read(VMCS_EXIT_QUAL, &exit_qual); -// simply execute the instruction that is faulting and return -static int ExecFaultingInstructionInVMM(struct VM *vm) -{ - 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); + // PrintDebug("VMX Exit taken, id-qual: %u-%lu\n", exit_reason, exit_qual); - // clone the template code - //memcpy(&&template_code,code,MAX_CODE); + /* Update guest state */ + load_vmcs_guest_state(info); - // 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"); + switch (exit_reason) { + case VMEXIT_INFO_EXCEPTION_OR_NMI: { + uint32_t int_info; + pf_error_t error_code; + + check_vmcs_read(VMCS_EXIT_INT_INFO, &int_info); + check_vmcs_read(VMCS_EXIT_INT_ERR, &error_code); + + // JRL: Change "0x0e" to a macro value + if ((uint8_t)int_info == 0x0e) { + PrintDebug("Page Fault at %p\n", (void *)exit_qual); + + if (info->shdw_pg_mode == SHADOW_PAGING) { + if (v3_handle_shadow_pagefault(info, (addr_t)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 { + PrintDebug("Unknown exception: 0x%x\n", (uint8_t)int_info); + v3_print_GPRs(info); + return -1; + } + break; + } + + case VMEXIT_CPUID: { + int instr_len; + + v3_cpuid(info->vm_regs.rax, (addr_t *)&(info->vm_regs.rax), (addr_t *)&(info->vm_regs.rbx), + (addr_t *)&(info->vm_regs.rcx), (addr_t *)&(info->vm_regs.rdx)); + + check_vmcs_read(VMCS_EXIT_INSTR_LEN, &instr_len); + + info->rip += instr_len; + break; + } + + case VMEXIT_IO_INSTR: { + struct vmexit_io_qual * io_qual = (struct vmexit_io_qual *)&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: + if (handle_cr_access(info,exit_qual) != 0) { + PrintError("Error handling CR access\n"); + return -1; + } + + break; + + default: + PrintError("Unhandled VMEXIT: %u (0x%x), %lu (0x%lx)\n", exit_reason, exit_reason, exit_qual, exit_qual); + return -1; + } - vm->vmcs.guestStateArea.rip += vm->vmcs.exitInfoFields.instrLength; + check_vmcs_write(VMCS_GUEST_CR0, info->ctrl_regs.cr0); + check_vmcs_write(VMCS_GUEST_CR3, info->ctrl_regs.cr3); + check_vmcs_write(VMCS_GUEST_CR4, info->ctrl_regs.cr4); + check_vmcs_write(VMCS_GUEST_RIP, info->rip); + check_vmcs_write(VMCS_GUEST_RSP, info->vm_regs.rsp); - return 0; + check_vmcs_write(VMCS_CR0_READ_SHDW, info->shdw_pg_state.guest_cr0); + return 0; } -