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, Peter Dinda <pdinda@northwestern.edu>
11 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
12 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Peter Dinda <pdinda@northwestern.edu>
16 * Jack Lange <jarusl@cs.northwestern.edu>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
23 /* Eventually we want to get rid of these */
25 #include <geekos/cpu.h>
26 #include <geekos/io_devs.h>
27 #include <geekos/io.h>
30 #include <palacios/vmx.h>
31 #include <palacios/vmcs.h>
32 #include <palacios/vmm.h>
33 #include <palacios/vmm_util.h>
34 #include <palacios/vmm_string.h>
37 extern void Get_MSR(unsigned int msr, uint_t * high, uint_t * low);
38 extern void Set_MSR(unsigned int msr, uint_t high, uint_t low);
39 extern int Enable_VMX(ullong_t regionPtr);
40 extern int cpuid_ecx(unsigned int op);
41 extern int Launch_VM(ullong_t vmcsPtr, uint_t eip);
43 #define NUMPORTS 65536
46 #define VMXASSIST_INFO_PORT 0x0e9
47 #define ROMBIOS_PANIC_PORT 0x400
48 #define ROMBIOS_PANIC_PORT2 0x401
49 #define ROMBIOS_INFO_PORT 0x402
50 #define ROMBIOS_DEBUG_PORT 0x403
53 static struct VM theVM;
55 static uint_t GetLinearIP(struct VM *vm)
57 if (vm->state==VM_VMXASSIST_V8086_BIOS || vm->state==VM_VMXASSIST_V8086) {
58 return vm->vmcs.guestStateArea.cs.baseAddr + vm->vmcs.guestStateArea.rip;
60 return vm->vmcs.guestStateArea.rip;
65 static void VMXPanic()
72 #define INSTR_OFFSET_START 17
73 #define NOP_SEQ_LEN 10
74 #define INSTR_OFFSET_END (INSTR_OFFSET_START+NOP_SEQ_LEN-1)
75 #define TEMPLATE_CODE_LEN 35
80 // simply execute the instruction that is faulting and return
81 static int ExecFaultingInstructionInVMM(struct VM *vm)
83 uint_t address = GetLinearIP(vm);
84 myregs = (uint_t)&(vm->registers);
87 PrintTrace("About the execute faulting instruction!\n");
88 PrintTrace("Instruction is:\n");
89 PrintTraceMemDump((void*)(address),vm->vmcs.exitInfoFields.instrLength);
92 PrintTrace("The template code is:\n");
93 PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN);
95 // clone the template code
96 //memcpy(&&template_code,code,MAX_CODE);
98 // clean up the nop field
99 memset(&&template_code+INSTR_OFFSET_START,*((uchar_t *)(&&template_code+0)),NOP_SEQ_LEN);
100 // overwrite the nops with the faulting instruction
101 memcpy(&&template_code+INSTR_OFFSET_START, (void*)(address),vm->vmcs.exitInfoFields.instrLength);
103 PrintTrace("Finished modifying the template code, which now is:\n");
104 PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN);
106 PrintTrace("Now entering modified template code\n");
110 // Template code stores current registers,
111 // restores registers, has a landing pad of noops
112 // that will be modified, restores current regs, and then returns
114 // Note that this currently ignores cr0, cr3, cr4, dr7, rsp, rip, and rflags
115 // it also blythly assumes it can exec the instruction in protected mode
117 __asm__ __volatile__ ("nop\n" // for cloning purposes (1 byte)
118 "pusha\n" // push our current regs onto the current stack (1 byte)
119 "movl %0, %%eax\n" // Get oldesp location (5 bytes)
120 "movl %%esp, (%%eax)\n" // store the current stack pointer in oldesp (2 bytes)
121 "movl %1, %%eax\n" // Get regs location (5 bytes)
122 "movl (%%eax), %%esp\n" // point esp at regs (2 bytes)
123 "popa\n" // now we have the VM registers restored (1 byte)
124 "nop\n" // now we execute the actual instruction (1 byte x 10)
125 "nop\n" // now we execute the actual instruction
126 "nop\n" // now we execute the actual instruction
127 "nop\n" // now we execute the actual instruction
128 "nop\n" // now we execute the actual instruction
129 "nop\n" // now we execute the actual instruction
130 "nop\n" // now we execute the actual instruction
131 "nop\n" // now we execute the actual instruction
132 "nop\n" // now we execute the actual instruction
133 "nop\n" // now we execute the actual instruction
134 // need to copy back to the VM registers!
135 "movl %0, %%eax\n" // recapture oldesp location (5 bytes)
136 "movl (%%eax), %%esp\n" // now we'll get our esp back from oldesp (2 bytes)
137 "popa\n" // and restore our GP regs and we're done (1 byte)
142 PrintTrace("Survived executing the faulting instruction and returning.\n");
144 vm->vmcs.guestStateArea.rip += vm->vmcs.exitInfoFields.instrLength;
151 int is_vmx_capable() {
153 union VMX_MSR featureMSR;
156 if (ret & CPUID_1_ECX_VTXFLAG) {
157 Get_MSR(IA32_FEATURE_CONTROL_MSR, &featureMSR.regs.high, &featureMSR.regs.low);
159 PrintTrace("MSRREGlow: 0x%.8x\n", featureMSR.regs.low);
161 if ((featureMSR.regs.low & FEATURE_CONTROL_VALID) != FEATURE_CONTROL_VALID) {
162 PrintDebug("VMX is locked -- enable in the BIOS\n");
166 PrintDebug("VMX not supported on this cpu\n");
175 VmxOnRegion * Init_VMX() {
177 VmxOnRegion * region = NULL;
180 region = CreateVmxOnRegion();
183 ret = Enable_VMX((ullong_t)((uint_t)region));
185 PrintDebug("VMX Enabled\n");
187 PrintDebug("VMX failure (ret = %d)\n", ret);
190 theVM.vmxonregion = region;
195 extern uint_t VMCS_CLEAR();
196 extern uint_t VMCS_LOAD();
197 extern uint_t VMCS_STORE();
198 extern uint_t VMCS_LAUNCH();
199 extern uint_t VMCS_RESUME();
200 extern uint_t Init_VMCS_HostState();
201 extern uint_t Init_VMCS_GuestState();
203 void SetCtrlBitsCorrectly(int msrno, int vmcsno)
208 PrintTrace("SetCtrlBitsCorrectly(%x,%x)\n", msrno, vmcsno);
209 Get_MSR(msrno, &msr.regs.high, &msr.regs.low);
210 PrintTrace("MSR %x = %x : %x \n", msrno, msr.regs.high, msr.regs.low);
211 reserved = msr.regs.low;
212 reserved &= msr.regs.high;
213 VMCS_WRITE(vmcsno, &reserved);
217 void SetCRBitsCorrectly(int msr0no, int msr1no, int vmcsno)
220 union VMX_MSR msr0, msr1;
222 PrintTrace("SetCRBitsCorrectly(%x,%x,%x)\n",msr0no,msr1no,vmcsno);
223 Get_MSR(msr0no, &msr0.regs.high, &msr0.regs.low);
224 Get_MSR(msr1no, &msr1.regs.high, &msr1.regs.low);
225 PrintTrace("MSR %x = %x, %x = %x \n", msr0no, msr0.regs.low, msr1no, msr1.regs.low);
226 reserved = msr0.regs.low;
227 reserved &= msr1.regs.low;
228 VMCS_WRITE(vmcsno, &reserved);
232 extern int Get_CR2();
233 extern int vmRunning;
236 static int PanicUnhandledVMExit(struct VM *vm)
238 PrintInfo("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason);
239 PrintTrace("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason);
240 PrintTrace_VMCS_ALL();
241 PrintTrace_VMX_Regs(&(vm->registers));
247 static int HandleVMPrintsAndPanics(struct VM *vm, uint_t port, uint_t data)
249 if (port==VMXASSIST_INFO_PORT &&
250 (vm->state == VM_VMXASSIST_STARTUP ||
251 vm->state == VM_VMXASSIST_V8086_BIOS ||
252 vm->state == VM_VMXASSIST_V8086)) {
253 // Communication channel from VMXAssist
254 PrintTrace("VMXASSIST Output Port\n");
255 PrintDebug("%c",data&0xff);
259 if ((port==ROMBIOS_PANIC_PORT ||
260 port==ROMBIOS_PANIC_PORT2 ||
261 port==ROMBIOS_DEBUG_PORT ||
262 port==ROMBIOS_INFO_PORT) &&
263 (vm->state==VM_VMXASSIST_V8086_BIOS)) {
264 // rombios is communicating
265 PrintTrace("ROMBIOS Output Port\n");
266 // PrintDebug("%c",data&0xff);
270 if (port==BOOT_STATE_CARD_PORT && vm->state==VM_VMXASSIST_V8086_BIOS) {
271 // rombios is sending something to the display card
272 PrintTrace("Hex Display: 0x%x\n",data&0xff);
278 static int HandleInOutExit(struct VM *vm)
282 struct VMCSExitInfoFields *exitinfo = &(vm->vmcs.exitInfoFields);
283 struct VMExitIOQual * qual = (struct VMExitIOQual *)&(vm->vmcs.exitInfoFields.qualification);
284 struct VMXRegs *regs = &(vm->registers);
286 address=GetLinearIP(vm);
288 PrintTrace("Handling Input/Output Instruction Exit\n");
290 PrintTrace_VMX_Regs(regs);
292 PrintTrace("Qualifications=0x%x\n", exitinfo->qualification);
293 PrintTrace("Reason=0x%x\n", exitinfo->reason);
294 PrintTrace("IO Port: 0x%x (%d)\n", qual->port, qual->port);
295 PrintTrace("Instruction Info=%x\n", exitinfo->instrInfo);
296 PrintTrace("%x : %s %s %s instruction of length %d for %d bytes from/to port 0x%x\n",
298 qual->dir == 0 ? "output" : "input",
299 qual->string ==0 ? "nonstring" : "STRING",
300 qual->REP == 0 ? "with no rep" : "WITH REP",
301 exitinfo->instrLength,
302 qual->accessSize==0 ? 1 : qual->accessSize==1 ? 2 : 4,
305 if ((qual->port == PIC_MASTER_CMD_ISR_PORT) ||
306 (qual->port == PIC_MASTER_IMR_PORT) ||
307 (qual->port == PIC_SLAVE_CMD_ISR_PORT) ||
308 (qual->port == PIC_SLAVE_IMR_PORT)) {
309 PrintTrace( "PIC Access\n");
313 if ((qual->dir == 1) && (qual->REP == 0) && (qual->string == 0)) {
314 char byte = In_Byte(qual->port);
316 vm->vmcs.guestStateArea.rip += exitinfo->instrLength;
317 regs->eax = (regs->eax & 0xffffff00) | byte;
318 PrintTrace("Returning 0x%x in eax\n", (regs->eax));
321 if (qual->dir==0 && qual->REP==0 && qual->string==0) {
322 // See if we need to handle the outb as a signal or
324 if (HandleVMPrintsAndPanics(vm,qual->port,regs->eax)) {
326 // If not, just go ahead and do the outb
327 Out_Byte(qual->port,regs->eax);
328 PrintTrace("Wrote 0x%x to port\n",(regs->eax));
330 vm->vmcs.guestStateArea.rip += exitinfo->instrLength;
337 static int HandleExternalIRQExit(struct VM *vm)
339 struct VMCSExitInfoFields * exitinfo = &(vm->vmcs.exitInfoFields);
340 struct VMExitIntInfo * intInfo = (struct VMExitIntInfo *)&(vm->vmcs.exitInfoFields.intInfo);
342 PrintTrace("External Interrupt captured\n");
343 PrintTrace("IntInfo: %x\n", exitinfo->intInfo);
346 if (!intInfo->valid) {
347 // interrupts are off, but this interrupt is not acknoledged (still pending)
348 // so we turn on interrupts to deliver appropriately in the
350 PrintTrace("External Interrupt is invald. Turning Interrupts back on\n");
355 // At this point, interrupts are off and the interrupt has been
356 // acknowledged. We will now handle the interrupt ourselves
357 // and turn interrupts back on in the host
359 PrintTrace("type: %d\n", intInfo->type);
360 PrintTrace("number: %d\n", intInfo->nr);
362 PrintTrace("Interrupt %d occuring now and handled by HandleExternalIRQExit\n",intInfo->nr);
364 switch (intInfo->type) {
365 case 0: { // ext. IRQ
366 // In the following, we construct an "int x" instruction
367 // where x is the specific interrupt number that is raised
368 // then we execute that instruciton
369 // because we are in host context, that means it is delivered as normal
370 // through the host IDT
372 ((char*)(&&ext_int_seq_start))[1] = intInfo->nr;
374 PrintTrace("Interrupt instruction setup done %x\n", *((ushort_t *)(&&ext_int_seq_start)));
382 PrintTrace("Type: NMI\n");
384 case 3: // hw exception
385 PrintTrace("Type: HW Exception\n");
387 case 4: // sw exception
388 PrintTrace("Type: SW Exception\n");
391 PrintTrace("Invalid Interrupt Type\n");
395 if (intInfo->valid && intInfo->errorCode) {
396 PrintTrace("IntError: %x\n", exitinfo->intErrorCode);
406 void DecodeCurrentInstruction(struct VM *vm, struct Instruction *inst)
408 // this is a gruesome hack
409 uint_t address = GetLinearIP(vm);
410 uint_t length = vm->vmcs.exitInfoFields.instrLength;
411 unsigned char *t = (unsigned char *) address;
415 PrintTrace("DecodeCurrentInstruction: instruction is\n");
416 PrintTraceMemDump(t,length);
418 if (length==3 && t[0]==0x0f && t[1]==0x22 && t[2]==0xc0) {
419 // mov from eax to cr0
420 // usually used to signal
421 inst->type=VM_MOV_TO_CR0;
422 inst->address=address;
424 inst->input1=vm->registers.eax;
425 inst->input2=vm->vmcs.guestStateArea.cr0;
426 inst->output=vm->registers.eax;
427 PrintTrace("MOV FROM EAX TO CR0\n");
429 inst->type=VM_UNKNOWN_INST;
434 static void V8086ModeSegmentRegisterFixup(struct VM *vm)
436 vm->vmcs.guestStateArea.cs.baseAddr=vm->vmcs.guestStateArea.cs.selector<<4;
437 vm->vmcs.guestStateArea.es.baseAddr=vm->vmcs.guestStateArea.es.selector<<4;
438 vm->vmcs.guestStateArea.ss.baseAddr=vm->vmcs.guestStateArea.ss.selector<<4;
439 vm->vmcs.guestStateArea.ds.baseAddr=vm->vmcs.guestStateArea.ds.selector<<4;
440 vm->vmcs.guestStateArea.fs.baseAddr=vm->vmcs.guestStateArea.fs.selector<<4;
441 vm->vmcs.guestStateArea.gs.baseAddr=vm->vmcs.guestStateArea.gs.selector<<4;
444 static void SetupV8086ModeForBoot(struct VM *vm)
446 vm->state = VM_VMXASSIST_V8086_BIOS;
448 // Put guest into V8086 mode on return
449 vm->vmcs.guestStateArea.rflags |= EFLAGS_VM | EFLAGS_IOPL_HI | EFLAGS_IOPL_LO ;
451 // We will start at f000:fff0 on return
453 // We want this to look as much as possible as a processor
455 vm->vmcs.guestStateArea.rip = 0xfff0; // note, 16 bit rip
456 vm->vmcs.guestStateArea.cs.selector = 0xf000;
457 vm->vmcs.guestStateArea.cs.limit=0xffff;
458 vm->vmcs.guestStateArea.cs.access.as_dword = 0xf3;
460 vm->vmcs.guestStateArea.ss.selector = 0x0000;
461 vm->vmcs.guestStateArea.ss.limit=0xffff;
462 vm->vmcs.guestStateArea.ss.access.as_dword = 0xf3;
464 vm->vmcs.guestStateArea.ds.selector = 0x0000;
465 vm->vmcs.guestStateArea.ds.limit=0xffff;
466 vm->vmcs.guestStateArea.ds.access.as_dword = 0xf3;
468 vm->vmcs.guestStateArea.es.selector = 0x0000;
469 vm->vmcs.guestStateArea.es.limit=0xffff;
470 vm->vmcs.guestStateArea.es.access.as_dword = 0xf3;
472 vm->vmcs.guestStateArea.fs.selector = 0x0000;
473 vm->vmcs.guestStateArea.fs.limit=0xffff;
474 vm->vmcs.guestStateArea.fs.access.as_dword = 0xf3;
476 vm->vmcs.guestStateArea.gs.selector = 0x0000;
477 vm->vmcs.guestStateArea.gs.limit=0xffff;
478 vm->vmcs.guestStateArea.gs.access.as_dword = 0xf3;
480 V8086ModeSegmentRegisterFixup(vm);
482 PrintTrace_VMCSData(&(vm->vmcs));
488 static int HandleExceptionOrNMI(struct VM *vm)
490 struct Instruction inst;
498 uint_t selectorindex=0;
500 PrintTrace("Exception or NMI occurred\n");
502 num=vm->vmcs.exitInfoFields.intInfo & 0xff;
503 type=(vm->vmcs.exitInfoFields.intInfo & 0x700)>>8;
504 errorvalid=(vm->vmcs.exitInfoFields.intInfo & 0x800)>>11;
506 error=vm->vmcs.exitInfoFields.intErrorCode;
510 selectorindex=(error>>3)&0xffff;
513 PrintTrace("Exception %d now - handled by HandleExceptionOrNMI\n",num);
515 PrintTrace("Exception Number %u : %s\n", num, exception_names[num]);
516 PrintTrace("Exception Type %u : %s\n", type, exception_type_names[type]);
519 PrintTrace("External\n");
521 PrintTrace("%s - Selector Index is %u\n", idt ? "IDT" : ti ? "LDT" : "GDT", selectorindex);
525 DecodeCurrentInstruction(vm,&inst);
527 if (inst.type==VM_MOV_TO_CR0) {
528 PrintTrace("MOV TO CR0, oldvalue=0x%x, newvalue=0x%x\n",inst.input2, inst.input1);
529 if ((inst.input2 & CR0_PE) && !(inst.input1 & CR0_PE) && vm->state==VM_VMXASSIST_STARTUP) {
530 // This is VMXAssist signalling for us to turn on V8086 mode and
531 // jump into the bios
532 PrintTrace("VMXAssist is signaling us for switch to V8086 mode and jump to 0xf000:fff0\n");
533 SetupV8086ModeForBoot(vm);
536 PrintTrace("Instruction is a write to CR0, but we don't understand it so we'll just exec it\n");
541 PrintTrace("Trying to execute the faulting instruction in VMM context now\n");
542 ExecFaultingInstructionInVMM(vm);
546 //PanicUnhandledVMExit(vmcs,regs);
552 static struct VM *FindVM()
558 int Do_VMM(struct VMXRegs regs)
561 ullong_t vmcs_ptr = 0;
562 uint_t vmcs_ptr_low = 0;
564 uint_t vmx_abort = 0;
568 PrintTrace("Vm Exit\n");
569 ret = VMCS_STORE(&vmcs_ptr);
570 vmcs_ptr &= 0xffffffff;
571 vmcs_ptr_low += vmcs_ptr;
576 PrintTrace("ret=%d\n", ret);
577 PrintTrace("Revision: %x\n", *(uint_t *)(vmcs_ptr_low));
578 vmx_abort = *(uint_t*)(((char *)vmcs_ptr_low)+4);
580 struct VM *vm = FindVM();
582 if (vmx_abort != 0) {
583 PrintTrace("VM ABORTED w/ code: %x\n", vmx_abort);
587 vm->registers = regs;
589 if (CopyOutVMCSData(&(vm->vmcs)) != 0) {
590 PrintTrace("Could not copy out VMCS\n");
595 PrintTrace("Guest esp: 0x%x (%u)\n", vm->vmcs.guestStateArea.rsp, vm->vmcs.guestStateArea.rsp);
597 PrintTrace("VM Exit for reason: %d (%x)\n",
598 vm->vmcs.exitInfoFields.reason & 0x00000fff,
599 vm->vmcs.exitInfoFields.reason);
601 if (vm->vmcs.exitInfoFields.reason & (0x1<<29) ) {
602 PrintTrace("VM Exit is from VMX root operation. Panicking\n");
606 if (vm->vmcs.exitInfoFields.reason & (0x1<<31) ) {
607 PrintTrace("VM Exit is due to a VM entry failure. Shouldn't happen here. Panicking\n");
608 PrintTrace_VMCSData(&(vm->vmcs));
612 switch (vm->vmcs.exitInfoFields.reason) {
613 case VM_EXIT_REASON_INFO_EXCEPTION_OR_NMI:
614 ret = HandleExceptionOrNMI(vm);
616 case VM_EXIT_REASON_EXTERNAL_INTR:
617 ret = HandleExternalIRQExit(vm);
619 case VM_EXIT_REASON_TRIPLE_FAULT:
620 ret = PanicUnhandledVMExit(vm);
622 case VM_EXIT_REASON_INIT_SIGNAL:
623 ret = PanicUnhandledVMExit(vm);
625 case VM_EXIT_REASON_STARTUP_IPI:
626 ret = PanicUnhandledVMExit(vm);
628 case VM_EXIT_REASON_IO_SMI:
629 ret = PanicUnhandledVMExit(vm);
631 case VM_EXIT_REASON_OTHER_SMI:
632 ret = PanicUnhandledVMExit(vm);
634 case VM_EXIT_REASON_INTR_WINDOW:
635 ret = PanicUnhandledVMExit(vm);
637 case VM_EXIT_REASON_NMI_WINDOW:
638 ret = PanicUnhandledVMExit(vm);
640 case VM_EXIT_REASON_TASK_SWITCH:
641 ret = PanicUnhandledVMExit(vm);
643 case VM_EXIT_REASON_CPUID:
644 ret = PanicUnhandledVMExit(vm);
646 case VM_EXIT_REASON_INVD:
647 ret = PanicUnhandledVMExit(vm);
649 case VM_EXIT_REASON_INVLPG:
650 ret = PanicUnhandledVMExit(vm);
652 case VM_EXIT_REASON_RDPMC:
653 ret = PanicUnhandledVMExit(vm);
655 case VM_EXIT_REASON_RDTSC:
656 ret = PanicUnhandledVMExit(vm);
658 case VM_EXIT_REASON_RSM:
659 ret = PanicUnhandledVMExit(vm);
661 case VM_EXIT_REASON_VMCALL:
662 ret = PanicUnhandledVMExit(vm);
664 case VM_EXIT_REASON_VMCLEAR:
665 ret = PanicUnhandledVMExit(vm);
667 case VM_EXIT_REASON_VMLAUNCH:
668 ret = PanicUnhandledVMExit(vm);
670 case VM_EXIT_REASON_VMPTRLD:
671 ret = PanicUnhandledVMExit(vm);
673 case VM_EXIT_REASON_VMPTRST:
674 ret = PanicUnhandledVMExit(vm);
676 case VM_EXIT_REASON_VMREAD:
677 ret = PanicUnhandledVMExit(vm);
679 case VM_EXIT_REASON_VMRESUME:
680 ret = PanicUnhandledVMExit(vm);
682 case VM_EXIT_REASON_VMWRITE:
683 ret = PanicUnhandledVMExit(vm);
685 case VM_EXIT_REASON_VMXOFF:
686 ret = PanicUnhandledVMExit(vm);
688 case VM_EXIT_REASON_VMXON:
689 ret = PanicUnhandledVMExit(vm);
691 case VM_EXIT_REASON_CR_REG_ACCESSES:
692 ret = PanicUnhandledVMExit(vm);
694 case VM_EXIT_REASON_MOV_DR:
695 ret = PanicUnhandledVMExit(vm);
697 case VM_EXIT_REASON_IO_INSTR:
698 ret = HandleInOutExit(vm);
700 case VM_EXIT_REASON_RDMSR:
701 ret = PanicUnhandledVMExit(vm);
703 case VM_EXIT_REASON_WRMSR:
704 ret = PanicUnhandledVMExit(vm);
706 case VM_EXIT_REASON_ENTRY_FAIL_INVALID_GUEST_STATE:
707 ret = PanicUnhandledVMExit(vm);
709 case VM_EXIT_REASON_ENTRY_FAIL_MSR_LOAD:
710 ret = PanicUnhandledVMExit(vm);
712 case VM_EXIT_REASON_MWAIT:
713 ret = PanicUnhandledVMExit(vm);
715 case VM_EXIT_REASON_MONITOR:
716 ret = PanicUnhandledVMExit(vm);
718 case VM_EXIT_REASON_PAUSE:
719 ret = PanicUnhandledVMExit(vm);
721 case VM_EXIT_REASON_ENTRY_FAILURE_MACHINE_CHECK:
722 ret = PanicUnhandledVMExit(vm);
724 case VM_EXIT_REASON_TPR_BELOW_THRESHOLD:
725 ret = PanicUnhandledVMExit(vm);
728 ret = PanicUnhandledVMExit(vm);
733 regs = vm->registers;
734 CopyInVMCSData(&(vm->vmcs));
738 VMCS_CLEAR(vmcs_ptr);
742 PrintTrace("Returning from Do_VMM: %d\n", ret);
748 static void ConfigureExits(struct VM *vm)
750 CopyOutVMCSExecCtrlFields(&(vm->vmcs.execCtrlFields));
752 vm->vmcs.execCtrlFields.pinCtrls |= 0
753 // EXTERNAL_INTERRUPT_EXITING
755 vm->vmcs.execCtrlFields.procCtrls |= 0
756 // INTERRUPT_WINDOWS_EXIT
764 |UNCONDITION_IO_EXITING
768 CopyInVMCSExecCtrlFields(&(vm->vmcs.execCtrlFields));
770 CopyOutVMCSExitCtrlFields(&(vm->vmcs.exitCtrlFields));
772 vm->vmcs.exitCtrlFields.exitCtrls |= ACK_IRQ_ON_EXIT;
774 CopyInVMCSExitCtrlFields(&(vm->vmcs.exitCtrlFields));
777 /* VMCS_READ(VM_EXIT_CTRLS, &flags); */
778 /* flags |= ACK_IRQ_ON_EXIT; */
779 /* VMCS_WRITE(VM_EXIT_CTRLS, &flags); */
784 extern int SAFE_VM_LAUNCH();
786 int MyLaunch(struct VM *vm)
788 ullong_t vmcs = (ullong_t)((uint_t) (vm->vmcsregion));
789 uint_t entry_eip = vm->descriptor.entry_ip;
790 uint_t exit_eip = vm->descriptor.exit_eip;
791 uint_t guest_esp = vm->descriptor.guest_esp;
792 uint_t f = 0xffffffff;
797 PrintTrace("Guest ESP: 0x%x (%u)\n", guest_esp, guest_esp);
799 exit_eip=(uint_t)RunVMM;
801 PrintTrace("Clear\n");
803 PrintTrace("Load\n");
807 PrintTrace("VMCS_LINK_PTR\n");
808 VMCS_WRITE(VMCS_LINK_PTR, &f);
809 PrintTrace("VMCS_LINK_PTR_HIGH\n");
810 VMCS_WRITE(VMCS_LINK_PTR_HIGH, &f);
813 SetCtrlBitsCorrectly(IA32_VMX_PINBASED_CTLS_MSR, PIN_VM_EXEC_CTRLS);
814 SetCtrlBitsCorrectly(IA32_VMX_PROCBASED_CTLS_MSR, PROC_VM_EXEC_CTRLS);
815 SetCtrlBitsCorrectly(IA32_VMX_EXIT_CTLS_MSR, VM_EXIT_CTRLS);
816 SetCtrlBitsCorrectly(IA32_VMX_ENTRY_CTLS_MSR, VM_ENTRY_CTRLS);
820 //SetCtrlBitsCorrectly(IA32_something,GUEST_IA32_DEBUGCTL);
821 //SetCtrlBitsCorrectly(IA32_something,GUEST_IA32_DEBUGCTL_HIGH);
825 PrintTrace("Setting up host state\n");
826 SetCRBitsCorrectly(IA32_VMX_CR0_FIXED0_MSR, IA32_VMX_CR0_FIXED1_MSR, HOST_CR0);
827 SetCRBitsCorrectly(IA32_VMX_CR4_FIXED0_MSR, IA32_VMX_CR4_FIXED1_MSR, HOST_CR4);
828 ret = Init_VMCS_HostState();
830 if (ret != VMX_SUCCESS) {
831 if (ret == VMX_FAIL_VALID) {
832 PrintTrace("Init Host state: VMCS FAILED WITH ERROR\n");
834 PrintTrace("Init Host state: Invalid VMCS\n");
839 // PrintTrace("HOST_RIP: %x (%u)\n", exit_eip, exit_eip);
840 VMCS_WRITE(HOST_RIP, &exit_eip);
843 PrintTrace("Setting up guest state\n");
844 PrintTrace("GUEST_RIP: %x (%u)\n", entry_eip, entry_eip);
845 VMCS_WRITE(GUEST_RIP,&entry_eip);
847 SetCRBitsCorrectly(IA32_VMX_CR0_FIXED0_MSR, IA32_VMX_CR0_FIXED1_MSR, GUEST_CR0);
848 SetCRBitsCorrectly(IA32_VMX_CR4_FIXED0_MSR, IA32_VMX_CR4_FIXED1_MSR, GUEST_CR4);
849 ret = Init_VMCS_GuestState();
851 PrintTrace("InitGuestState returned\n");
852 if (ret != VMX_SUCCESS) {
853 if (ret == VMX_FAIL_VALID) {
854 PrintTrace("Init Guest state: VMCS FAILED WITH ERROR\n");
856 PrintTrace("Init Guest state: Invalid VMCS\n");
860 PrintTrace("GUEST_RSP: %x (%u)\n", guest_esp, (uint_t)guest_esp);
861 VMCS_WRITE(GUEST_RSP,&guest_esp);
865 if (VMCS_WRITE(EXCEPTION_BITMAP,&tmpReg ) != VMX_SUCCESS) {
866 PrintInfo("Bitmap error\n");
871 PrintTrace("VMCS_LAUNCH\n");
873 vm->state=VM_VMXASSIST_STARTUP;
875 vmm_ret = SAFE_VM_LAUNCH();
877 PrintTrace("VMM error %d\n", vmm_ret);
885 int VMLaunch(struct VMDescriptor *vm)
887 VMCS * vmcs = CreateVMCS();
890 ullong_t vmcs_ptr = (ullong_t)((uint_t)vmcs);
891 uint_t top = (vmcs_ptr>>32)&0xffffffff;
892 uint_t bottom = (vmcs_ptr)&0xffffffff;
894 theVM.vmcsregion = vmcs;
895 theVM.descriptor = *vm;
897 PrintTrace("vmcs_ptr_top=%x vmcs_ptr_bottom=%x, eip=%x\n", top, bottom, vm->entry_ip);
898 rc=MyLaunch(&theVM); // vmcs_ptr, vm->entry_ip, vm->exit_eip, vm->guest_esp);
899 PrintTrace("Returned from MyLaunch();\n");
904 VmxOnRegion * CreateVmxOnRegion() {
905 union VMX_MSR basicMSR;
906 VmxOnRegion * region = (VmxOnRegion *)(os_hooks)->allocate_pages(1);
908 Get_MSR(IA32_VMX_BASIC_MSR, &basicMSR.regs.high, &basicMSR.regs.low);
909 // memcpy(region, &basicMSR.vmxBasic.revision, sizeof(uint_t));
911 *(ulong_t*)region = basicMSR.vmxBasic.revision;
913 PrintInfo("VMX revision: 0x%lu\n", *(ulong_t *)region);
918 VMCS * CreateVMCS() {
919 union VMX_MSR basicMSR;
920 VMCS * vmcs = (VMCS *)(os_hooks)->allocate_pages(1);
922 Get_MSR(IA32_VMX_BASIC_MSR, &basicMSR.regs.high, &basicMSR.regs.low);
923 *(ulong_t *)vmcs = basicMSR.vmxBasic.revision;
924 *(ulong_t *)((char*)vmcs + 4) = 0;
926 PrintTrace("VMCS Region size: %u\n", basicMSR.vmxBasic.regionSize);
927 PrintTrace("VMCS Abort: %x\n",*(uint_t *)(((char*)vmcs)+4));