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 extern struct vmm_os_hooks * os_hooks;
56 static struct VM theVM;
58 static uint_t GetLinearIP(struct VM *vm)
60 if (vm->state==VM_VMXASSIST_V8086_BIOS || vm->state==VM_VMXASSIST_V8086) {
61 return vm->vmcs.guestStateArea.cs.baseAddr + vm->vmcs.guestStateArea.rip;
63 return vm->vmcs.guestStateArea.rip;
68 static void VMXPanic()
75 #define INSTR_OFFSET_START 17
76 #define NOP_SEQ_LEN 10
77 #define INSTR_OFFSET_END (INSTR_OFFSET_START+NOP_SEQ_LEN-1)
78 #define TEMPLATE_CODE_LEN 35
83 // simply execute the instruction that is faulting and return
84 static int ExecFaultingInstructionInVMM(struct VM *vm)
86 uint_t address = GetLinearIP(vm);
87 myregs = (uint_t)&(vm->registers);
90 PrintTrace("About the execute faulting instruction!\n");
91 PrintTrace("Instruction is:\n");
92 PrintTraceMemDump((void*)(address),vm->vmcs.exitInfoFields.instrLength);
95 PrintTrace("The template code is:\n");
96 PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN);
98 // clone the template code
99 //memcpy(&&template_code,code,MAX_CODE);
101 // clean up the nop field
102 memset(&&template_code+INSTR_OFFSET_START,*((uchar_t *)(&&template_code+0)),NOP_SEQ_LEN);
103 // overwrite the nops with the faulting instruction
104 memcpy(&&template_code+INSTR_OFFSET_START, (void*)(address),vm->vmcs.exitInfoFields.instrLength);
106 PrintTrace("Finished modifying the template code, which now is:\n");
107 PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN);
109 PrintTrace("Now entering modified template code\n");
113 // Template code stores current registers,
114 // restores registers, has a landing pad of noops
115 // that will be modified, restores current regs, and then returns
117 // Note that this currently ignores cr0, cr3, cr4, dr7, rsp, rip, and rflags
118 // it also blythly assumes it can exec the instruction in protected mode
120 __asm__ __volatile__ ("nop\n" // for cloning purposes (1 byte)
121 "pusha\n" // push our current regs onto the current stack (1 byte)
122 "movl %0, %%eax\n" // Get oldesp location (5 bytes)
123 "movl %%esp, (%%eax)\n" // store the current stack pointer in oldesp (2 bytes)
124 "movl %1, %%eax\n" // Get regs location (5 bytes)
125 "movl (%%eax), %%esp\n" // point esp at regs (2 bytes)
126 "popa\n" // now we have the VM registers restored (1 byte)
127 "nop\n" // now we execute the actual instruction (1 byte x 10)
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 "nop\n" // now we execute the actual instruction
135 "nop\n" // now we execute the actual instruction
136 "nop\n" // now we execute the actual instruction
137 // need to copy back to the VM registers!
138 "movl %0, %%eax\n" // recapture oldesp location (5 bytes)
139 "movl (%%eax), %%esp\n" // now we'll get our esp back from oldesp (2 bytes)
140 "popa\n" // and restore our GP regs and we're done (1 byte)
145 PrintTrace("Survived executing the faulting instruction and returning.\n");
147 vm->vmcs.guestStateArea.rip += vm->vmcs.exitInfoFields.instrLength;
154 int is_vmx_capable() {
156 union VMX_MSR featureMSR;
159 if (ret & CPUID_1_ECX_VTXFLAG) {
160 Get_MSR(IA32_FEATURE_CONTROL_MSR, &featureMSR.regs.high, &featureMSR.regs.low);
162 PrintTrace("MSRREGlow: 0x%.8x\n", featureMSR.regs.low);
164 if ((featureMSR.regs.low & FEATURE_CONTROL_VALID) != FEATURE_CONTROL_VALID) {
165 PrintDebug("VMX is locked -- enable in the BIOS\n");
169 PrintDebug("VMX not supported on this cpu\n");
178 VmxOnRegion * Init_VMX() {
180 VmxOnRegion * region = NULL;
183 region = CreateVmxOnRegion();
186 ret = Enable_VMX((ullong_t)((uint_t)region));
188 PrintDebug("VMX Enabled\n");
190 PrintDebug("VMX failure (ret = %d)\n", ret);
193 theVM.vmxonregion = region;
198 extern uint_t VMCS_CLEAR();
199 extern uint_t VMCS_LOAD();
200 extern uint_t VMCS_STORE();
201 extern uint_t VMCS_LAUNCH();
202 extern uint_t VMCS_RESUME();
203 extern uint_t Init_VMCS_HostState();
204 extern uint_t Init_VMCS_GuestState();
206 void SetCtrlBitsCorrectly(int msrno, int vmcsno)
211 PrintTrace("SetCtrlBitsCorrectly(%x,%x)\n", msrno, vmcsno);
212 Get_MSR(msrno, &msr.regs.high, &msr.regs.low);
213 PrintTrace("MSR %x = %x : %x \n", msrno, msr.regs.high, msr.regs.low);
214 reserved = msr.regs.low;
215 reserved &= msr.regs.high;
216 VMCS_WRITE(vmcsno, &reserved);
220 void SetCRBitsCorrectly(int msr0no, int msr1no, int vmcsno)
223 union VMX_MSR msr0, msr1;
225 PrintTrace("SetCRBitsCorrectly(%x,%x,%x)\n",msr0no,msr1no,vmcsno);
226 Get_MSR(msr0no, &msr0.regs.high, &msr0.regs.low);
227 Get_MSR(msr1no, &msr1.regs.high, &msr1.regs.low);
228 PrintTrace("MSR %x = %x, %x = %x \n", msr0no, msr0.regs.low, msr1no, msr1.regs.low);
229 reserved = msr0.regs.low;
230 reserved &= msr1.regs.low;
231 VMCS_WRITE(vmcsno, &reserved);
235 extern int Get_CR2();
236 extern int vmRunning;
239 static int PanicUnhandledVMExit(struct VM *vm)
241 PrintInfo("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason);
242 PrintTrace("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason);
243 PrintTrace_VMCS_ALL();
244 PrintTrace_VMX_Regs(&(vm->registers));
250 static int HandleVMPrintsAndPanics(struct VM *vm, uint_t port, uint_t data)
252 if (port==VMXASSIST_INFO_PORT &&
253 (vm->state == VM_VMXASSIST_STARTUP ||
254 vm->state == VM_VMXASSIST_V8086_BIOS ||
255 vm->state == VM_VMXASSIST_V8086)) {
256 // Communication channel from VMXAssist
257 PrintTrace("VMXASSIST Output Port\n");
258 PrintDebug("%c",data&0xff);
262 if ((port==ROMBIOS_PANIC_PORT ||
263 port==ROMBIOS_PANIC_PORT2 ||
264 port==ROMBIOS_DEBUG_PORT ||
265 port==ROMBIOS_INFO_PORT) &&
266 (vm->state==VM_VMXASSIST_V8086_BIOS)) {
267 // rombios is communicating
268 PrintTrace("ROMBIOS Output Port\n");
269 // PrintDebug("%c",data&0xff);
273 if (port==BOOT_STATE_CARD_PORT && vm->state==VM_VMXASSIST_V8086_BIOS) {
274 // rombios is sending something to the display card
275 PrintTrace("Hex Display: 0x%x\n",data&0xff);
281 static int HandleInOutExit(struct VM *vm)
285 struct VMCSExitInfoFields *exitinfo = &(vm->vmcs.exitInfoFields);
286 struct VMExitIOQual * qual = (struct VMExitIOQual *)&(vm->vmcs.exitInfoFields.qualification);
287 struct VMXRegs *regs = &(vm->registers);
289 address=GetLinearIP(vm);
291 PrintTrace("Handling Input/Output Instruction Exit\n");
293 PrintTrace_VMX_Regs(regs);
295 PrintTrace("Qualifications=0x%x\n", exitinfo->qualification);
296 PrintTrace("Reason=0x%x\n", exitinfo->reason);
297 PrintTrace("IO Port: 0x%x (%d)\n", qual->port, qual->port);
298 PrintTrace("Instruction Info=%x\n", exitinfo->instrInfo);
299 PrintTrace("%x : %s %s %s instruction of length %d for %d bytes from/to port 0x%x\n",
301 qual->dir == 0 ? "output" : "input",
302 qual->string ==0 ? "nonstring" : "STRING",
303 qual->REP == 0 ? "with no rep" : "WITH REP",
304 exitinfo->instrLength,
305 qual->accessSize==0 ? 1 : qual->accessSize==1 ? 2 : 4,
308 if ((qual->port == PIC_MASTER_CMD_ISR_PORT) ||
309 (qual->port == PIC_MASTER_IMR_PORT) ||
310 (qual->port == PIC_SLAVE_CMD_ISR_PORT) ||
311 (qual->port == PIC_SLAVE_IMR_PORT)) {
312 PrintTrace( "PIC Access\n");
316 if ((qual->dir == 1) && (qual->REP == 0) && (qual->string == 0)) {
317 char byte = In_Byte(qual->port);
319 vm->vmcs.guestStateArea.rip += exitinfo->instrLength;
320 regs->eax = (regs->eax & 0xffffff00) | byte;
321 PrintTrace("Returning 0x%x in eax\n", (regs->eax));
324 if (qual->dir==0 && qual->REP==0 && qual->string==0) {
325 // See if we need to handle the outb as a signal or
327 if (HandleVMPrintsAndPanics(vm,qual->port,regs->eax)) {
329 // If not, just go ahead and do the outb
330 Out_Byte(qual->port,regs->eax);
331 PrintTrace("Wrote 0x%x to port\n",(regs->eax));
333 vm->vmcs.guestStateArea.rip += exitinfo->instrLength;
340 static int HandleExternalIRQExit(struct VM *vm)
342 struct VMCSExitInfoFields * exitinfo = &(vm->vmcs.exitInfoFields);
343 struct VMExitIntInfo * intInfo = (struct VMExitIntInfo *)&(vm->vmcs.exitInfoFields.intInfo);
345 PrintTrace("External Interrupt captured\n");
346 PrintTrace("IntInfo: %x\n", exitinfo->intInfo);
349 if (!intInfo->valid) {
350 // interrupts are off, but this interrupt is not acknoledged (still pending)
351 // so we turn on interrupts to deliver appropriately in the
353 PrintTrace("External Interrupt is invald. Turning Interrupts back on\n");
358 // At this point, interrupts are off and the interrupt has been
359 // acknowledged. We will now handle the interrupt ourselves
360 // and turn interrupts back on in the host
362 PrintTrace("type: %d\n", intInfo->type);
363 PrintTrace("number: %d\n", intInfo->nr);
365 PrintTrace("Interrupt %d occuring now and handled by HandleExternalIRQExit\n",intInfo->nr);
367 switch (intInfo->type) {
368 case 0: { // ext. IRQ
369 // In the following, we construct an "int x" instruction
370 // where x is the specific interrupt number that is raised
371 // then we execute that instruciton
372 // because we are in host context, that means it is delivered as normal
373 // through the host IDT
375 ((char*)(&&ext_int_seq_start))[1] = intInfo->nr;
377 PrintTrace("Interrupt instruction setup done %x\n", *((ushort_t *)(&&ext_int_seq_start)));
385 PrintTrace("Type: NMI\n");
387 case 3: // hw exception
388 PrintTrace("Type: HW Exception\n");
390 case 4: // sw exception
391 PrintTrace("Type: SW Exception\n");
394 PrintTrace("Invalid Interrupt Type\n");
398 if (intInfo->valid && intInfo->errorCode) {
399 PrintTrace("IntError: %x\n", exitinfo->intErrorCode);
409 void DecodeCurrentInstruction(struct VM *vm, struct Instruction *inst)
411 // this is a gruesome hack
412 uint_t address = GetLinearIP(vm);
413 uint_t length = vm->vmcs.exitInfoFields.instrLength;
414 unsigned char *t = (unsigned char *) address;
418 PrintTrace("DecodeCurrentInstruction: instruction is\n");
419 PrintTraceMemDump(t,length);
421 if (length==3 && t[0]==0x0f && t[1]==0x22 && t[2]==0xc0) {
422 // mov from eax to cr0
423 // usually used to signal
424 inst->type=VM_MOV_TO_CR0;
425 inst->address=address;
427 inst->input1=vm->registers.eax;
428 inst->input2=vm->vmcs.guestStateArea.cr0;
429 inst->output=vm->registers.eax;
430 PrintTrace("MOV FROM EAX TO CR0\n");
432 inst->type=VM_UNKNOWN_INST;
437 static void V8086ModeSegmentRegisterFixup(struct VM *vm)
439 vm->vmcs.guestStateArea.cs.baseAddr=vm->vmcs.guestStateArea.cs.selector<<4;
440 vm->vmcs.guestStateArea.es.baseAddr=vm->vmcs.guestStateArea.es.selector<<4;
441 vm->vmcs.guestStateArea.ss.baseAddr=vm->vmcs.guestStateArea.ss.selector<<4;
442 vm->vmcs.guestStateArea.ds.baseAddr=vm->vmcs.guestStateArea.ds.selector<<4;
443 vm->vmcs.guestStateArea.fs.baseAddr=vm->vmcs.guestStateArea.fs.selector<<4;
444 vm->vmcs.guestStateArea.gs.baseAddr=vm->vmcs.guestStateArea.gs.selector<<4;
447 static void SetupV8086ModeForBoot(struct VM *vm)
449 vm->state = VM_VMXASSIST_V8086_BIOS;
451 // Put guest into V8086 mode on return
452 vm->vmcs.guestStateArea.rflags |= EFLAGS_VM | EFLAGS_IOPL_HI | EFLAGS_IOPL_LO ;
454 // We will start at f000:fff0 on return
456 // We want this to look as much as possible as a processor
458 vm->vmcs.guestStateArea.rip = 0xfff0; // note, 16 bit rip
459 vm->vmcs.guestStateArea.cs.selector = 0xf000;
460 vm->vmcs.guestStateArea.cs.limit=0xffff;
461 vm->vmcs.guestStateArea.cs.access.as_dword = 0xf3;
463 vm->vmcs.guestStateArea.ss.selector = 0x0000;
464 vm->vmcs.guestStateArea.ss.limit=0xffff;
465 vm->vmcs.guestStateArea.ss.access.as_dword = 0xf3;
467 vm->vmcs.guestStateArea.ds.selector = 0x0000;
468 vm->vmcs.guestStateArea.ds.limit=0xffff;
469 vm->vmcs.guestStateArea.ds.access.as_dword = 0xf3;
471 vm->vmcs.guestStateArea.es.selector = 0x0000;
472 vm->vmcs.guestStateArea.es.limit=0xffff;
473 vm->vmcs.guestStateArea.es.access.as_dword = 0xf3;
475 vm->vmcs.guestStateArea.fs.selector = 0x0000;
476 vm->vmcs.guestStateArea.fs.limit=0xffff;
477 vm->vmcs.guestStateArea.fs.access.as_dword = 0xf3;
479 vm->vmcs.guestStateArea.gs.selector = 0x0000;
480 vm->vmcs.guestStateArea.gs.limit=0xffff;
481 vm->vmcs.guestStateArea.gs.access.as_dword = 0xf3;
483 V8086ModeSegmentRegisterFixup(vm);
485 PrintTrace_VMCSData(&(vm->vmcs));
491 static int HandleExceptionOrNMI(struct VM *vm)
493 struct Instruction inst;
501 uint_t selectorindex=0;
503 PrintTrace("Exception or NMI occurred\n");
505 num=vm->vmcs.exitInfoFields.intInfo & 0xff;
506 type=(vm->vmcs.exitInfoFields.intInfo & 0x700)>>8;
507 errorvalid=(vm->vmcs.exitInfoFields.intInfo & 0x800)>>11;
509 error=vm->vmcs.exitInfoFields.intErrorCode;
513 selectorindex=(error>>3)&0xffff;
516 PrintTrace("Exception %d now - handled by HandleExceptionOrNMI\n",num);
518 PrintTrace("Exception Number %u : %s\n", num, exception_names[num]);
519 PrintTrace("Exception Type %u : %s\n", type, exception_type_names[type]);
522 PrintTrace("External\n");
524 PrintTrace("%s - Selector Index is %u\n", idt ? "IDT" : ti ? "LDT" : "GDT", selectorindex);
528 DecodeCurrentInstruction(vm,&inst);
530 if (inst.type==VM_MOV_TO_CR0) {
531 PrintTrace("MOV TO CR0, oldvalue=0x%x, newvalue=0x%x\n",inst.input2, inst.input1);
532 if ((inst.input2 & CR0_PE) && !(inst.input1 & CR0_PE) && vm->state==VM_VMXASSIST_STARTUP) {
533 // This is VMXAssist signalling for us to turn on V8086 mode and
534 // jump into the bios
535 PrintTrace("VMXAssist is signaling us for switch to V8086 mode and jump to 0xf000:fff0\n");
536 SetupV8086ModeForBoot(vm);
539 PrintTrace("Instruction is a write to CR0, but we don't understand it so we'll just exec it\n");
544 PrintTrace("Trying to execute the faulting instruction in VMM context now\n");
545 ExecFaultingInstructionInVMM(vm);
549 //PanicUnhandledVMExit(vmcs,regs);
555 static struct VM *FindVM()
561 int Do_VMM(struct VMXRegs regs)
564 ullong_t vmcs_ptr = 0;
565 uint_t vmcs_ptr_low = 0;
567 uint_t vmx_abort = 0;
571 PrintTrace("Vm Exit\n");
572 ret = VMCS_STORE(&vmcs_ptr);
573 vmcs_ptr &= 0xffffffff;
574 vmcs_ptr_low += vmcs_ptr;
579 PrintTrace("ret=%d\n", ret);
580 PrintTrace("Revision: %x\n", *(uint_t *)(vmcs_ptr_low));
581 vmx_abort = *(uint_t*)(((char *)vmcs_ptr_low)+4);
583 struct VM *vm = FindVM();
585 if (vmx_abort != 0) {
586 PrintTrace("VM ABORTED w/ code: %x\n", vmx_abort);
590 vm->registers = regs;
592 if (CopyOutVMCSData(&(vm->vmcs)) != 0) {
593 PrintTrace("Could not copy out VMCS\n");
598 PrintTrace("Guest esp: 0x%x (%u)\n", vm->vmcs.guestStateArea.rsp, vm->vmcs.guestStateArea.rsp);
600 PrintTrace("VM Exit for reason: %d (%x)\n",
601 vm->vmcs.exitInfoFields.reason & 0x00000fff,
602 vm->vmcs.exitInfoFields.reason);
604 if (vm->vmcs.exitInfoFields.reason & (0x1<<29) ) {
605 PrintTrace("VM Exit is from VMX root operation. Panicking\n");
609 if (vm->vmcs.exitInfoFields.reason & (0x1<<31) ) {
610 PrintTrace("VM Exit is due to a VM entry failure. Shouldn't happen here. Panicking\n");
611 PrintTrace_VMCSData(&(vm->vmcs));
615 switch (vm->vmcs.exitInfoFields.reason) {
616 case VM_EXIT_REASON_INFO_EXCEPTION_OR_NMI:
617 ret = HandleExceptionOrNMI(vm);
619 case VM_EXIT_REASON_EXTERNAL_INTR:
620 ret = HandleExternalIRQExit(vm);
622 case VM_EXIT_REASON_TRIPLE_FAULT:
623 ret = PanicUnhandledVMExit(vm);
625 case VM_EXIT_REASON_INIT_SIGNAL:
626 ret = PanicUnhandledVMExit(vm);
628 case VM_EXIT_REASON_STARTUP_IPI:
629 ret = PanicUnhandledVMExit(vm);
631 case VM_EXIT_REASON_IO_SMI:
632 ret = PanicUnhandledVMExit(vm);
634 case VM_EXIT_REASON_OTHER_SMI:
635 ret = PanicUnhandledVMExit(vm);
637 case VM_EXIT_REASON_INTR_WINDOW:
638 ret = PanicUnhandledVMExit(vm);
640 case VM_EXIT_REASON_NMI_WINDOW:
641 ret = PanicUnhandledVMExit(vm);
643 case VM_EXIT_REASON_TASK_SWITCH:
644 ret = PanicUnhandledVMExit(vm);
646 case VM_EXIT_REASON_CPUID:
647 ret = PanicUnhandledVMExit(vm);
649 case VM_EXIT_REASON_INVD:
650 ret = PanicUnhandledVMExit(vm);
652 case VM_EXIT_REASON_INVLPG:
653 ret = PanicUnhandledVMExit(vm);
655 case VM_EXIT_REASON_RDPMC:
656 ret = PanicUnhandledVMExit(vm);
658 case VM_EXIT_REASON_RDTSC:
659 ret = PanicUnhandledVMExit(vm);
661 case VM_EXIT_REASON_RSM:
662 ret = PanicUnhandledVMExit(vm);
664 case VM_EXIT_REASON_VMCALL:
665 ret = PanicUnhandledVMExit(vm);
667 case VM_EXIT_REASON_VMCLEAR:
668 ret = PanicUnhandledVMExit(vm);
670 case VM_EXIT_REASON_VMLAUNCH:
671 ret = PanicUnhandledVMExit(vm);
673 case VM_EXIT_REASON_VMPTRLD:
674 ret = PanicUnhandledVMExit(vm);
676 case VM_EXIT_REASON_VMPTRST:
677 ret = PanicUnhandledVMExit(vm);
679 case VM_EXIT_REASON_VMREAD:
680 ret = PanicUnhandledVMExit(vm);
682 case VM_EXIT_REASON_VMRESUME:
683 ret = PanicUnhandledVMExit(vm);
685 case VM_EXIT_REASON_VMWRITE:
686 ret = PanicUnhandledVMExit(vm);
688 case VM_EXIT_REASON_VMXOFF:
689 ret = PanicUnhandledVMExit(vm);
691 case VM_EXIT_REASON_VMXON:
692 ret = PanicUnhandledVMExit(vm);
694 case VM_EXIT_REASON_CR_REG_ACCESSES:
695 ret = PanicUnhandledVMExit(vm);
697 case VM_EXIT_REASON_MOV_DR:
698 ret = PanicUnhandledVMExit(vm);
700 case VM_EXIT_REASON_IO_INSTR:
701 ret = HandleInOutExit(vm);
703 case VM_EXIT_REASON_RDMSR:
704 ret = PanicUnhandledVMExit(vm);
706 case VM_EXIT_REASON_WRMSR:
707 ret = PanicUnhandledVMExit(vm);
709 case VM_EXIT_REASON_ENTRY_FAIL_INVALID_GUEST_STATE:
710 ret = PanicUnhandledVMExit(vm);
712 case VM_EXIT_REASON_ENTRY_FAIL_MSR_LOAD:
713 ret = PanicUnhandledVMExit(vm);
715 case VM_EXIT_REASON_MWAIT:
716 ret = PanicUnhandledVMExit(vm);
718 case VM_EXIT_REASON_MONITOR:
719 ret = PanicUnhandledVMExit(vm);
721 case VM_EXIT_REASON_PAUSE:
722 ret = PanicUnhandledVMExit(vm);
724 case VM_EXIT_REASON_ENTRY_FAILURE_MACHINE_CHECK:
725 ret = PanicUnhandledVMExit(vm);
727 case VM_EXIT_REASON_TPR_BELOW_THRESHOLD:
728 ret = PanicUnhandledVMExit(vm);
731 ret = PanicUnhandledVMExit(vm);
736 regs = vm->registers;
737 CopyInVMCSData(&(vm->vmcs));
741 VMCS_CLEAR(vmcs_ptr);
745 PrintTrace("Returning from Do_VMM: %d\n", ret);
751 static void ConfigureExits(struct VM *vm)
753 CopyOutVMCSExecCtrlFields(&(vm->vmcs.execCtrlFields));
755 vm->vmcs.execCtrlFields.pinCtrls |= 0
756 // EXTERNAL_INTERRUPT_EXITING
758 vm->vmcs.execCtrlFields.procCtrls |= 0
759 // INTERRUPT_WINDOWS_EXIT
767 |UNCONDITION_IO_EXITING
771 CopyInVMCSExecCtrlFields(&(vm->vmcs.execCtrlFields));
773 CopyOutVMCSExitCtrlFields(&(vm->vmcs.exitCtrlFields));
775 vm->vmcs.exitCtrlFields.exitCtrls |= ACK_IRQ_ON_EXIT;
777 CopyInVMCSExitCtrlFields(&(vm->vmcs.exitCtrlFields));
780 /* VMCS_READ(VM_EXIT_CTRLS, &flags); */
781 /* flags |= ACK_IRQ_ON_EXIT; */
782 /* VMCS_WRITE(VM_EXIT_CTRLS, &flags); */
787 extern int SAFE_VM_LAUNCH();
789 int MyLaunch(struct VM *vm)
791 ullong_t vmcs = (ullong_t)((uint_t) (vm->vmcsregion));
792 uint_t entry_eip = vm->descriptor.entry_ip;
793 uint_t exit_eip = vm->descriptor.exit_eip;
794 uint_t guest_esp = vm->descriptor.guest_esp;
795 uint_t f = 0xffffffff;
800 PrintTrace("Guest ESP: 0x%x (%u)\n", guest_esp, guest_esp);
802 exit_eip=(uint_t)RunVMM;
804 PrintTrace("Clear\n");
806 PrintTrace("Load\n");
810 PrintTrace("VMCS_LINK_PTR\n");
811 VMCS_WRITE(VMCS_LINK_PTR, &f);
812 PrintTrace("VMCS_LINK_PTR_HIGH\n");
813 VMCS_WRITE(VMCS_LINK_PTR_HIGH, &f);
816 SetCtrlBitsCorrectly(IA32_VMX_PINBASED_CTLS_MSR, PIN_VM_EXEC_CTRLS);
817 SetCtrlBitsCorrectly(IA32_VMX_PROCBASED_CTLS_MSR, PROC_VM_EXEC_CTRLS);
818 SetCtrlBitsCorrectly(IA32_VMX_EXIT_CTLS_MSR, VM_EXIT_CTRLS);
819 SetCtrlBitsCorrectly(IA32_VMX_ENTRY_CTLS_MSR, VM_ENTRY_CTRLS);
823 //SetCtrlBitsCorrectly(IA32_something,GUEST_IA32_DEBUGCTL);
824 //SetCtrlBitsCorrectly(IA32_something,GUEST_IA32_DEBUGCTL_HIGH);
828 PrintTrace("Setting up host state\n");
829 SetCRBitsCorrectly(IA32_VMX_CR0_FIXED0_MSR, IA32_VMX_CR0_FIXED1_MSR, HOST_CR0);
830 SetCRBitsCorrectly(IA32_VMX_CR4_FIXED0_MSR, IA32_VMX_CR4_FIXED1_MSR, HOST_CR4);
831 ret = Init_VMCS_HostState();
833 if (ret != VMX_SUCCESS) {
834 if (ret == VMX_FAIL_VALID) {
835 PrintTrace("Init Host state: VMCS FAILED WITH ERROR\n");
837 PrintTrace("Init Host state: Invalid VMCS\n");
842 // PrintTrace("HOST_RIP: %x (%u)\n", exit_eip, exit_eip);
843 VMCS_WRITE(HOST_RIP, &exit_eip);
846 PrintTrace("Setting up guest state\n");
847 PrintTrace("GUEST_RIP: %x (%u)\n", entry_eip, entry_eip);
848 VMCS_WRITE(GUEST_RIP,&entry_eip);
850 SetCRBitsCorrectly(IA32_VMX_CR0_FIXED0_MSR, IA32_VMX_CR0_FIXED1_MSR, GUEST_CR0);
851 SetCRBitsCorrectly(IA32_VMX_CR4_FIXED0_MSR, IA32_VMX_CR4_FIXED1_MSR, GUEST_CR4);
852 ret = Init_VMCS_GuestState();
854 PrintTrace("InitGuestState returned\n");
855 if (ret != VMX_SUCCESS) {
856 if (ret == VMX_FAIL_VALID) {
857 PrintTrace("Init Guest state: VMCS FAILED WITH ERROR\n");
859 PrintTrace("Init Guest state: Invalid VMCS\n");
863 PrintTrace("GUEST_RSP: %x (%u)\n", guest_esp, (uint_t)guest_esp);
864 VMCS_WRITE(GUEST_RSP,&guest_esp);
868 if (VMCS_WRITE(EXCEPTION_BITMAP,&tmpReg ) != VMX_SUCCESS) {
869 PrintInfo("Bitmap error\n");
874 PrintTrace("VMCS_LAUNCH\n");
876 vm->state=VM_VMXASSIST_STARTUP;
878 vmm_ret = SAFE_VM_LAUNCH();
880 PrintTrace("VMM error %d\n", vmm_ret);
888 int VMLaunch(struct VMDescriptor *vm)
890 VMCS * vmcs = CreateVMCS();
893 ullong_t vmcs_ptr = (ullong_t)((uint_t)vmcs);
894 uint_t top = (vmcs_ptr>>32)&0xffffffff;
895 uint_t bottom = (vmcs_ptr)&0xffffffff;
897 theVM.vmcsregion = vmcs;
898 theVM.descriptor = *vm;
900 PrintTrace("vmcs_ptr_top=%x vmcs_ptr_bottom=%x, eip=%x\n", top, bottom, vm->entry_ip);
901 rc=MyLaunch(&theVM); // vmcs_ptr, vm->entry_ip, vm->exit_eip, vm->guest_esp);
902 PrintTrace("Returned from MyLaunch();\n");
907 VmxOnRegion * CreateVmxOnRegion() {
908 union VMX_MSR basicMSR;
909 VmxOnRegion * region = (VmxOnRegion *)(os_hooks)->allocate_pages(1);
911 Get_MSR(IA32_VMX_BASIC_MSR, &basicMSR.regs.high, &basicMSR.regs.low);
912 // memcpy(region, &basicMSR.vmxBasic.revision, sizeof(uint_t));
914 *(ulong_t*)region = basicMSR.vmxBasic.revision;
916 PrintInfo("VMX revision: 0x%lu\n", *(ulong_t *)region);
921 VMCS * CreateVMCS() {
922 union VMX_MSR basicMSR;
923 VMCS * vmcs = (VMCS *)(os_hooks)->allocate_pages(1);
925 Get_MSR(IA32_VMX_BASIC_MSR, &basicMSR.regs.high, &basicMSR.regs.low);
926 *(ulong_t *)vmcs = basicMSR.vmxBasic.revision;
927 *(ulong_t *)((char*)vmcs + 4) = 0;
929 PrintTrace("VMCS Region size: %u\n", basicMSR.vmxBasic.regionSize);
930 PrintTrace("VMCS Abort: %x\n",*(uint_t *)(((char*)vmcs)+4));