1 /* (c) 2008, Peter Dinda <pdinda@northwestern.edu> */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
3 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
6 /* Eventually we want to get rid of these */
8 #include <geekos/cpu.h>
9 #include <geekos/io_devs.h>
10 #include <geekos/io.h>
13 #include <palacios/vmx.h>
14 #include <palacios/vmcs.h>
15 #include <palacios/vmm.h>
16 #include <palacios/vmm_util.h>
17 #include <palacios/vmm_string.h>
20 extern void Get_MSR(unsigned int msr, uint_t * high, uint_t * low);
21 extern void Set_MSR(unsigned int msr, uint_t high, uint_t low);
22 extern int Enable_VMX(ullong_t regionPtr);
23 extern int cpuid_ecx(unsigned int op);
24 extern int Launch_VM(ullong_t vmcsPtr, uint_t eip);
26 #define NUMPORTS 65536
29 #define VMXASSIST_INFO_PORT 0x0e9
30 #define ROMBIOS_PANIC_PORT 0x400
31 #define ROMBIOS_PANIC_PORT2 0x401
32 #define ROMBIOS_INFO_PORT 0x402
33 #define ROMBIOS_DEBUG_PORT 0x403
36 extern struct vmm_os_hooks * os_hooks;
39 static struct VM theVM;
41 static uint_t GetLinearIP(struct VM *vm)
43 if (vm->state==VM_VMXASSIST_V8086_BIOS || vm->state==VM_VMXASSIST_V8086) {
44 return vm->vmcs.guestStateArea.cs.baseAddr + vm->vmcs.guestStateArea.rip;
46 return vm->vmcs.guestStateArea.rip;
51 static void VMXPanic()
58 #define INSTR_OFFSET_START 17
59 #define NOP_SEQ_LEN 10
60 #define INSTR_OFFSET_END (INSTR_OFFSET_START+NOP_SEQ_LEN-1)
61 #define TEMPLATE_CODE_LEN 35
66 // simply execute the instruction that is faulting and return
67 static int ExecFaultingInstructionInVMM(struct VM *vm)
69 uint_t address = GetLinearIP(vm);
70 myregs = (uint_t)&(vm->registers);
73 PrintTrace("About the execute faulting instruction!\n");
74 PrintTrace("Instruction is:\n");
75 PrintTraceMemDump((void*)(address),vm->vmcs.exitInfoFields.instrLength);
78 PrintTrace("The template code is:\n");
79 PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN);
81 // clone the template code
82 //memcpy(&&template_code,code,MAX_CODE);
84 // clean up the nop field
85 memset(&&template_code+INSTR_OFFSET_START,*((uchar_t *)(&&template_code+0)),NOP_SEQ_LEN);
86 // overwrite the nops with the faulting instruction
87 memcpy(&&template_code+INSTR_OFFSET_START, (void*)(address),vm->vmcs.exitInfoFields.instrLength);
89 PrintTrace("Finished modifying the template code, which now is:\n");
90 PrintTraceMemDump(&&template_code,TEMPLATE_CODE_LEN);
92 PrintTrace("Now entering modified template code\n");
96 // Template code stores current registers,
97 // restores registers, has a landing pad of noops
98 // that will be modified, restores current regs, and then returns
100 // Note that this currently ignores cr0, cr3, cr4, dr7, rsp, rip, and rflags
101 // it also blythly assumes it can exec the instruction in protected mode
103 __asm__ __volatile__ ("nop\n" // for cloning purposes (1 byte)
104 "pusha\n" // push our current regs onto the current stack (1 byte)
105 "movl %0, %%eax\n" // Get oldesp location (5 bytes)
106 "movl %%esp, (%%eax)\n" // store the current stack pointer in oldesp (2 bytes)
107 "movl %1, %%eax\n" // Get regs location (5 bytes)
108 "movl (%%eax), %%esp\n" // point esp at regs (2 bytes)
109 "popa\n" // now we have the VM registers restored (1 byte)
110 "nop\n" // now we execute the actual instruction (1 byte x 10)
111 "nop\n" // now we execute the actual instruction
112 "nop\n" // now we execute the actual instruction
113 "nop\n" // now we execute the actual instruction
114 "nop\n" // now we execute the actual instruction
115 "nop\n" // now we execute the actual instruction
116 "nop\n" // now we execute the actual instruction
117 "nop\n" // now we execute the actual instruction
118 "nop\n" // now we execute the actual instruction
119 "nop\n" // now we execute the actual instruction
120 // need to copy back to the VM registers!
121 "movl %0, %%eax\n" // recapture oldesp location (5 bytes)
122 "movl (%%eax), %%esp\n" // now we'll get our esp back from oldesp (2 bytes)
123 "popa\n" // and restore our GP regs and we're done (1 byte)
128 PrintTrace("Survived executing the faulting instruction and returning.\n");
130 vm->vmcs.guestStateArea.rip += vm->vmcs.exitInfoFields.instrLength;
137 int is_vmx_capable() {
139 union VMX_MSR featureMSR;
142 if (ret & CPUID_1_ECX_VTXFLAG) {
143 Get_MSR(IA32_FEATURE_CONTROL_MSR, &featureMSR.regs.high, &featureMSR.regs.low);
145 PrintTrace("MSRREGlow: 0x%.8x\n", featureMSR.regs.low);
147 if ((featureMSR.regs.low & FEATURE_CONTROL_VALID) != FEATURE_CONTROL_VALID) {
148 PrintDebug("VMX is locked -- enable in the BIOS\n");
152 PrintDebug("VMX not supported on this cpu\n");
161 VmxOnRegion * Init_VMX() {
163 VmxOnRegion * region = NULL;
166 region = CreateVmxOnRegion();
169 ret = Enable_VMX((ullong_t)((uint_t)region));
171 PrintDebug("VMX Enabled\n");
173 PrintDebug("VMX failure (ret = %d)\n", ret);
176 theVM.vmxonregion = region;
181 extern uint_t VMCS_CLEAR();
182 extern uint_t VMCS_LOAD();
183 extern uint_t VMCS_STORE();
184 extern uint_t VMCS_LAUNCH();
185 extern uint_t VMCS_RESUME();
186 extern uint_t Init_VMCS_HostState();
187 extern uint_t Init_VMCS_GuestState();
189 void SetCtrlBitsCorrectly(int msrno, int vmcsno)
194 PrintTrace("SetCtrlBitsCorrectly(%x,%x)\n", msrno, vmcsno);
195 Get_MSR(msrno, &msr.regs.high, &msr.regs.low);
196 PrintTrace("MSR %x = %x : %x \n", msrno, msr.regs.high, msr.regs.low);
197 reserved = msr.regs.low;
198 reserved &= msr.regs.high;
199 VMCS_WRITE(vmcsno, &reserved);
203 void SetCRBitsCorrectly(int msr0no, int msr1no, int vmcsno)
206 union VMX_MSR msr0, msr1;
208 PrintTrace("SetCRBitsCorrectly(%x,%x,%x)\n",msr0no,msr1no,vmcsno);
209 Get_MSR(msr0no, &msr0.regs.high, &msr0.regs.low);
210 Get_MSR(msr1no, &msr1.regs.high, &msr1.regs.low);
211 PrintTrace("MSR %x = %x, %x = %x \n", msr0no, msr0.regs.low, msr1no, msr1.regs.low);
212 reserved = msr0.regs.low;
213 reserved &= msr1.regs.low;
214 VMCS_WRITE(vmcsno, &reserved);
218 extern int Get_CR2();
219 extern int vmRunning;
222 static int PanicUnhandledVMExit(struct VM *vm)
224 PrintInfo("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason);
225 PrintTrace("Panicking due to VMExit with reason %u\n", vm->vmcs.exitInfoFields.reason);
226 PrintTrace_VMCS_ALL();
227 PrintTrace_VMX_Regs(&(vm->registers));
233 static int HandleVMPrintsAndPanics(struct VM *vm, uint_t port, uint_t data)
235 if (port==VMXASSIST_INFO_PORT &&
236 (vm->state == VM_VMXASSIST_STARTUP ||
237 vm->state == VM_VMXASSIST_V8086_BIOS ||
238 vm->state == VM_VMXASSIST_V8086)) {
239 // Communication channel from VMXAssist
240 PrintTrace("VMXASSIST Output Port\n");
241 PrintDebug("%c",data&0xff);
245 if ((port==ROMBIOS_PANIC_PORT ||
246 port==ROMBIOS_PANIC_PORT2 ||
247 port==ROMBIOS_DEBUG_PORT ||
248 port==ROMBIOS_INFO_PORT) &&
249 (vm->state==VM_VMXASSIST_V8086_BIOS)) {
250 // rombios is communicating
251 PrintTrace("ROMBIOS Output Port\n");
252 // PrintDebug("%c",data&0xff);
256 if (port==BOOT_STATE_CARD_PORT && vm->state==VM_VMXASSIST_V8086_BIOS) {
257 // rombios is sending something to the display card
258 PrintTrace("Hex Display: 0x%x\n",data&0xff);
264 static int HandleInOutExit(struct VM *vm)
268 struct VMCSExitInfoFields *exitinfo = &(vm->vmcs.exitInfoFields);
269 struct VMExitIOQual * qual = (struct VMExitIOQual *)&(vm->vmcs.exitInfoFields.qualification);
270 struct VMXRegs *regs = &(vm->registers);
272 address=GetLinearIP(vm);
274 PrintTrace("Handling Input/Output Instruction Exit\n");
276 PrintTrace_VMX_Regs(regs);
278 PrintTrace("Qualifications=0x%x\n", exitinfo->qualification);
279 PrintTrace("Reason=0x%x\n", exitinfo->reason);
280 PrintTrace("IO Port: 0x%x (%d)\n", qual->port, qual->port);
281 PrintTrace("Instruction Info=%x\n", exitinfo->instrInfo);
282 PrintTrace("%x : %s %s %s instruction of length %d for %d bytes from/to port 0x%x\n",
284 qual->dir == 0 ? "output" : "input",
285 qual->string ==0 ? "nonstring" : "STRING",
286 qual->REP == 0 ? "with no rep" : "WITH REP",
287 exitinfo->instrLength,
288 qual->accessSize==0 ? 1 : qual->accessSize==1 ? 2 : 4,
291 if ((qual->port == PIC_MASTER_CMD_ISR_PORT) ||
292 (qual->port == PIC_MASTER_IMR_PORT) ||
293 (qual->port == PIC_SLAVE_CMD_ISR_PORT) ||
294 (qual->port == PIC_SLAVE_IMR_PORT)) {
295 PrintTrace( "PIC Access\n");
299 if ((qual->dir == 1) && (qual->REP == 0) && (qual->string == 0)) {
300 char byte = In_Byte(qual->port);
302 vm->vmcs.guestStateArea.rip += exitinfo->instrLength;
303 regs->eax = (regs->eax & 0xffffff00) | byte;
304 PrintTrace("Returning 0x%x in eax\n", (regs->eax));
307 if (qual->dir==0 && qual->REP==0 && qual->string==0) {
308 // See if we need to handle the outb as a signal or
310 if (HandleVMPrintsAndPanics(vm,qual->port,regs->eax)) {
312 // If not, just go ahead and do the outb
313 Out_Byte(qual->port,regs->eax);
314 PrintTrace("Wrote 0x%x to port\n",(regs->eax));
316 vm->vmcs.guestStateArea.rip += exitinfo->instrLength;
323 static int HandleExternalIRQExit(struct VM *vm)
325 struct VMCSExitInfoFields * exitinfo = &(vm->vmcs.exitInfoFields);
326 struct VMExitIntInfo * intInfo = (struct VMExitIntInfo *)&(vm->vmcs.exitInfoFields.intInfo);
328 PrintTrace("External Interrupt captured\n");
329 PrintTrace("IntInfo: %x\n", exitinfo->intInfo);
332 if (!intInfo->valid) {
333 // interrupts are off, but this interrupt is not acknoledged (still pending)
334 // so we turn on interrupts to deliver appropriately in the
336 PrintTrace("External Interrupt is invald. Turning Interrupts back on\n");
341 // At this point, interrupts are off and the interrupt has been
342 // acknowledged. We will now handle the interrupt ourselves
343 // and turn interrupts back on in the host
345 PrintTrace("type: %d\n", intInfo->type);
346 PrintTrace("number: %d\n", intInfo->nr);
348 PrintTrace("Interrupt %d occuring now and handled by HandleExternalIRQExit\n",intInfo->nr);
350 switch (intInfo->type) {
351 case 0: { // ext. IRQ
352 // In the following, we construct an "int x" instruction
353 // where x is the specific interrupt number that is raised
354 // then we execute that instruciton
355 // because we are in host context, that means it is delivered as normal
356 // through the host IDT
358 ((char*)(&&ext_int_seq_start))[1] = intInfo->nr;
360 PrintTrace("Interrupt instruction setup done %x\n", *((ushort_t *)(&&ext_int_seq_start)));
368 PrintTrace("Type: NMI\n");
370 case 3: // hw exception
371 PrintTrace("Type: HW Exception\n");
373 case 4: // sw exception
374 PrintTrace("Type: SW Exception\n");
377 PrintTrace("Invalid Interrupt Type\n");
381 if (intInfo->valid && intInfo->errorCode) {
382 PrintTrace("IntError: %x\n", exitinfo->intErrorCode);
392 void DecodeCurrentInstruction(struct VM *vm, struct Instruction *inst)
394 // this is a gruesome hack
395 uint_t address = GetLinearIP(vm);
396 uint_t length = vm->vmcs.exitInfoFields.instrLength;
397 unsigned char *t = (unsigned char *) address;
401 PrintTrace("DecodeCurrentInstruction: instruction is\n");
402 PrintTraceMemDump(t,length);
404 if (length==3 && t[0]==0x0f && t[1]==0x22 && t[2]==0xc0) {
405 // mov from eax to cr0
406 // usually used to signal
407 inst->type=VM_MOV_TO_CR0;
408 inst->address=address;
410 inst->input1=vm->registers.eax;
411 inst->input2=vm->vmcs.guestStateArea.cr0;
412 inst->output=vm->registers.eax;
413 PrintTrace("MOV FROM EAX TO CR0\n");
415 inst->type=VM_UNKNOWN_INST;
420 static void V8086ModeSegmentRegisterFixup(struct VM *vm)
422 vm->vmcs.guestStateArea.cs.baseAddr=vm->vmcs.guestStateArea.cs.selector<<4;
423 vm->vmcs.guestStateArea.es.baseAddr=vm->vmcs.guestStateArea.es.selector<<4;
424 vm->vmcs.guestStateArea.ss.baseAddr=vm->vmcs.guestStateArea.ss.selector<<4;
425 vm->vmcs.guestStateArea.ds.baseAddr=vm->vmcs.guestStateArea.ds.selector<<4;
426 vm->vmcs.guestStateArea.fs.baseAddr=vm->vmcs.guestStateArea.fs.selector<<4;
427 vm->vmcs.guestStateArea.gs.baseAddr=vm->vmcs.guestStateArea.gs.selector<<4;
430 static void SetupV8086ModeForBoot(struct VM *vm)
432 vm->state = VM_VMXASSIST_V8086_BIOS;
434 // Put guest into V8086 mode on return
435 vm->vmcs.guestStateArea.rflags |= EFLAGS_VM | EFLAGS_IOPL_HI | EFLAGS_IOPL_LO ;
437 // We will start at f000:fff0 on return
439 // We want this to look as much as possible as a processor
441 vm->vmcs.guestStateArea.rip = 0xfff0; // note, 16 bit rip
442 vm->vmcs.guestStateArea.cs.selector = 0xf000;
443 vm->vmcs.guestStateArea.cs.limit=0xffff;
444 vm->vmcs.guestStateArea.cs.access.as_dword = 0xf3;
446 vm->vmcs.guestStateArea.ss.selector = 0x0000;
447 vm->vmcs.guestStateArea.ss.limit=0xffff;
448 vm->vmcs.guestStateArea.ss.access.as_dword = 0xf3;
450 vm->vmcs.guestStateArea.ds.selector = 0x0000;
451 vm->vmcs.guestStateArea.ds.limit=0xffff;
452 vm->vmcs.guestStateArea.ds.access.as_dword = 0xf3;
454 vm->vmcs.guestStateArea.es.selector = 0x0000;
455 vm->vmcs.guestStateArea.es.limit=0xffff;
456 vm->vmcs.guestStateArea.es.access.as_dword = 0xf3;
458 vm->vmcs.guestStateArea.fs.selector = 0x0000;
459 vm->vmcs.guestStateArea.fs.limit=0xffff;
460 vm->vmcs.guestStateArea.fs.access.as_dword = 0xf3;
462 vm->vmcs.guestStateArea.gs.selector = 0x0000;
463 vm->vmcs.guestStateArea.gs.limit=0xffff;
464 vm->vmcs.guestStateArea.gs.access.as_dword = 0xf3;
466 V8086ModeSegmentRegisterFixup(vm);
468 PrintTrace_VMCSData(&(vm->vmcs));
474 static int HandleExceptionOrNMI(struct VM *vm)
476 struct Instruction inst;
484 uint_t selectorindex=0;
486 PrintTrace("Exception or NMI occurred\n");
488 num=vm->vmcs.exitInfoFields.intInfo & 0xff;
489 type=(vm->vmcs.exitInfoFields.intInfo & 0x700)>>8;
490 errorvalid=(vm->vmcs.exitInfoFields.intInfo & 0x800)>>11;
492 error=vm->vmcs.exitInfoFields.intErrorCode;
496 selectorindex=(error>>3)&0xffff;
499 PrintTrace("Exception %d now - handled by HandleExceptionOrNMI\n",num);
501 PrintTrace("Exception Number %u : %s\n", num, exception_names[num]);
502 PrintTrace("Exception Type %u : %s\n", type, exception_type_names[type]);
505 PrintTrace("External\n");
507 PrintTrace("%s - Selector Index is %u\n", idt ? "IDT" : ti ? "LDT" : "GDT", selectorindex);
511 DecodeCurrentInstruction(vm,&inst);
513 if (inst.type==VM_MOV_TO_CR0) {
514 PrintTrace("MOV TO CR0, oldvalue=0x%x, newvalue=0x%x\n",inst.input2, inst.input1);
515 if ((inst.input2 & CR0_PE) && !(inst.input1 & CR0_PE) && vm->state==VM_VMXASSIST_STARTUP) {
516 // This is VMXAssist signalling for us to turn on V8086 mode and
517 // jump into the bios
518 PrintTrace("VMXAssist is signaling us for switch to V8086 mode and jump to 0xf000:fff0\n");
519 SetupV8086ModeForBoot(vm);
522 PrintTrace("Instruction is a write to CR0, but we don't understand it so we'll just exec it\n");
527 PrintTrace("Trying to execute the faulting instruction in VMM context now\n");
528 ExecFaultingInstructionInVMM(vm);
532 //PanicUnhandledVMExit(vmcs,regs);
538 static struct VM *FindVM()
544 int Do_VMM(struct VMXRegs regs)
547 ullong_t vmcs_ptr = 0;
548 uint_t vmcs_ptr_low = 0;
550 uint_t vmx_abort = 0;
554 PrintTrace("Vm Exit\n");
555 ret = VMCS_STORE(&vmcs_ptr);
556 vmcs_ptr &= 0xffffffff;
557 vmcs_ptr_low += vmcs_ptr;
562 PrintTrace("ret=%d\n", ret);
563 PrintTrace("Revision: %x\n", *(uint_t *)(vmcs_ptr_low));
564 vmx_abort = *(uint_t*)(((char *)vmcs_ptr_low)+4);
566 struct VM *vm = FindVM();
568 if (vmx_abort != 0) {
569 PrintTrace("VM ABORTED w/ code: %x\n", vmx_abort);
573 vm->registers = regs;
575 if (CopyOutVMCSData(&(vm->vmcs)) != 0) {
576 PrintTrace("Could not copy out VMCS\n");
581 PrintTrace("Guest esp: 0x%x (%u)\n", vm->vmcs.guestStateArea.rsp, vm->vmcs.guestStateArea.rsp);
583 PrintTrace("VM Exit for reason: %d (%x)\n",
584 vm->vmcs.exitInfoFields.reason & 0x00000fff,
585 vm->vmcs.exitInfoFields.reason);
587 if (vm->vmcs.exitInfoFields.reason & (0x1<<29) ) {
588 PrintTrace("VM Exit is from VMX root operation. Panicking\n");
592 if (vm->vmcs.exitInfoFields.reason & (0x1<<31) ) {
593 PrintTrace("VM Exit is due to a VM entry failure. Shouldn't happen here. Panicking\n");
594 PrintTrace_VMCSData(&(vm->vmcs));
598 switch (vm->vmcs.exitInfoFields.reason) {
599 case VM_EXIT_REASON_INFO_EXCEPTION_OR_NMI:
600 ret = HandleExceptionOrNMI(vm);
602 case VM_EXIT_REASON_EXTERNAL_INTR:
603 ret = HandleExternalIRQExit(vm);
605 case VM_EXIT_REASON_TRIPLE_FAULT:
606 ret = PanicUnhandledVMExit(vm);
608 case VM_EXIT_REASON_INIT_SIGNAL:
609 ret = PanicUnhandledVMExit(vm);
611 case VM_EXIT_REASON_STARTUP_IPI:
612 ret = PanicUnhandledVMExit(vm);
614 case VM_EXIT_REASON_IO_SMI:
615 ret = PanicUnhandledVMExit(vm);
617 case VM_EXIT_REASON_OTHER_SMI:
618 ret = PanicUnhandledVMExit(vm);
620 case VM_EXIT_REASON_INTR_WINDOW:
621 ret = PanicUnhandledVMExit(vm);
623 case VM_EXIT_REASON_NMI_WINDOW:
624 ret = PanicUnhandledVMExit(vm);
626 case VM_EXIT_REASON_TASK_SWITCH:
627 ret = PanicUnhandledVMExit(vm);
629 case VM_EXIT_REASON_CPUID:
630 ret = PanicUnhandledVMExit(vm);
632 case VM_EXIT_REASON_INVD:
633 ret = PanicUnhandledVMExit(vm);
635 case VM_EXIT_REASON_INVLPG:
636 ret = PanicUnhandledVMExit(vm);
638 case VM_EXIT_REASON_RDPMC:
639 ret = PanicUnhandledVMExit(vm);
641 case VM_EXIT_REASON_RDTSC:
642 ret = PanicUnhandledVMExit(vm);
644 case VM_EXIT_REASON_RSM:
645 ret = PanicUnhandledVMExit(vm);
647 case VM_EXIT_REASON_VMCALL:
648 ret = PanicUnhandledVMExit(vm);
650 case VM_EXIT_REASON_VMCLEAR:
651 ret = PanicUnhandledVMExit(vm);
653 case VM_EXIT_REASON_VMLAUNCH:
654 ret = PanicUnhandledVMExit(vm);
656 case VM_EXIT_REASON_VMPTRLD:
657 ret = PanicUnhandledVMExit(vm);
659 case VM_EXIT_REASON_VMPTRST:
660 ret = PanicUnhandledVMExit(vm);
662 case VM_EXIT_REASON_VMREAD:
663 ret = PanicUnhandledVMExit(vm);
665 case VM_EXIT_REASON_VMRESUME:
666 ret = PanicUnhandledVMExit(vm);
668 case VM_EXIT_REASON_VMWRITE:
669 ret = PanicUnhandledVMExit(vm);
671 case VM_EXIT_REASON_VMXOFF:
672 ret = PanicUnhandledVMExit(vm);
674 case VM_EXIT_REASON_VMXON:
675 ret = PanicUnhandledVMExit(vm);
677 case VM_EXIT_REASON_CR_REG_ACCESSES:
678 ret = PanicUnhandledVMExit(vm);
680 case VM_EXIT_REASON_MOV_DR:
681 ret = PanicUnhandledVMExit(vm);
683 case VM_EXIT_REASON_IO_INSTR:
684 ret = HandleInOutExit(vm);
686 case VM_EXIT_REASON_RDMSR:
687 ret = PanicUnhandledVMExit(vm);
689 case VM_EXIT_REASON_WRMSR:
690 ret = PanicUnhandledVMExit(vm);
692 case VM_EXIT_REASON_ENTRY_FAIL_INVALID_GUEST_STATE:
693 ret = PanicUnhandledVMExit(vm);
695 case VM_EXIT_REASON_ENTRY_FAIL_MSR_LOAD:
696 ret = PanicUnhandledVMExit(vm);
698 case VM_EXIT_REASON_MWAIT:
699 ret = PanicUnhandledVMExit(vm);
701 case VM_EXIT_REASON_MONITOR:
702 ret = PanicUnhandledVMExit(vm);
704 case VM_EXIT_REASON_PAUSE:
705 ret = PanicUnhandledVMExit(vm);
707 case VM_EXIT_REASON_ENTRY_FAILURE_MACHINE_CHECK:
708 ret = PanicUnhandledVMExit(vm);
710 case VM_EXIT_REASON_TPR_BELOW_THRESHOLD:
711 ret = PanicUnhandledVMExit(vm);
714 ret = PanicUnhandledVMExit(vm);
719 regs = vm->registers;
720 CopyInVMCSData(&(vm->vmcs));
724 VMCS_CLEAR(vmcs_ptr);
728 PrintTrace("Returning from Do_VMM: %d\n", ret);
734 static void ConfigureExits(struct VM *vm)
736 CopyOutVMCSExecCtrlFields(&(vm->vmcs.execCtrlFields));
738 vm->vmcs.execCtrlFields.pinCtrls |= 0
739 // EXTERNAL_INTERRUPT_EXITING
741 vm->vmcs.execCtrlFields.procCtrls |= 0
742 // INTERRUPT_WINDOWS_EXIT
750 |UNCONDITION_IO_EXITING
754 CopyInVMCSExecCtrlFields(&(vm->vmcs.execCtrlFields));
756 CopyOutVMCSExitCtrlFields(&(vm->vmcs.exitCtrlFields));
758 vm->vmcs.exitCtrlFields.exitCtrls |= ACK_IRQ_ON_EXIT;
760 CopyInVMCSExitCtrlFields(&(vm->vmcs.exitCtrlFields));
763 /* VMCS_READ(VM_EXIT_CTRLS, &flags); */
764 /* flags |= ACK_IRQ_ON_EXIT; */
765 /* VMCS_WRITE(VM_EXIT_CTRLS, &flags); */
770 extern int SAFE_VM_LAUNCH();
772 int MyLaunch(struct VM *vm)
774 ullong_t vmcs = (ullong_t)((uint_t) (vm->vmcsregion));
775 uint_t entry_eip = vm->descriptor.entry_ip;
776 uint_t exit_eip = vm->descriptor.exit_eip;
777 uint_t guest_esp = vm->descriptor.guest_esp;
778 uint_t f = 0xffffffff;
783 PrintTrace("Guest ESP: 0x%x (%u)\n", guest_esp, guest_esp);
785 exit_eip=(uint_t)RunVMM;
787 PrintTrace("Clear\n");
789 PrintTrace("Load\n");
793 PrintTrace("VMCS_LINK_PTR\n");
794 VMCS_WRITE(VMCS_LINK_PTR, &f);
795 PrintTrace("VMCS_LINK_PTR_HIGH\n");
796 VMCS_WRITE(VMCS_LINK_PTR_HIGH, &f);
799 SetCtrlBitsCorrectly(IA32_VMX_PINBASED_CTLS_MSR, PIN_VM_EXEC_CTRLS);
800 SetCtrlBitsCorrectly(IA32_VMX_PROCBASED_CTLS_MSR, PROC_VM_EXEC_CTRLS);
801 SetCtrlBitsCorrectly(IA32_VMX_EXIT_CTLS_MSR, VM_EXIT_CTRLS);
802 SetCtrlBitsCorrectly(IA32_VMX_ENTRY_CTLS_MSR, VM_ENTRY_CTRLS);
806 //SetCtrlBitsCorrectly(IA32_something,GUEST_IA32_DEBUGCTL);
807 //SetCtrlBitsCorrectly(IA32_something,GUEST_IA32_DEBUGCTL_HIGH);
811 PrintTrace("Setting up host state\n");
812 SetCRBitsCorrectly(IA32_VMX_CR0_FIXED0_MSR, IA32_VMX_CR0_FIXED1_MSR, HOST_CR0);
813 SetCRBitsCorrectly(IA32_VMX_CR4_FIXED0_MSR, IA32_VMX_CR4_FIXED1_MSR, HOST_CR4);
814 ret = Init_VMCS_HostState();
816 if (ret != VMX_SUCCESS) {
817 if (ret == VMX_FAIL_VALID) {
818 PrintTrace("Init Host state: VMCS FAILED WITH ERROR\n");
820 PrintTrace("Init Host state: Invalid VMCS\n");
825 // PrintTrace("HOST_RIP: %x (%u)\n", exit_eip, exit_eip);
826 VMCS_WRITE(HOST_RIP, &exit_eip);
829 PrintTrace("Setting up guest state\n");
830 PrintTrace("GUEST_RIP: %x (%u)\n", entry_eip, entry_eip);
831 VMCS_WRITE(GUEST_RIP,&entry_eip);
833 SetCRBitsCorrectly(IA32_VMX_CR0_FIXED0_MSR, IA32_VMX_CR0_FIXED1_MSR, GUEST_CR0);
834 SetCRBitsCorrectly(IA32_VMX_CR4_FIXED0_MSR, IA32_VMX_CR4_FIXED1_MSR, GUEST_CR4);
835 ret = Init_VMCS_GuestState();
837 PrintTrace("InitGuestState returned\n");
838 if (ret != VMX_SUCCESS) {
839 if (ret == VMX_FAIL_VALID) {
840 PrintTrace("Init Guest state: VMCS FAILED WITH ERROR\n");
842 PrintTrace("Init Guest state: Invalid VMCS\n");
846 PrintTrace("GUEST_RSP: %x (%u)\n", guest_esp, (uint_t)guest_esp);
847 VMCS_WRITE(GUEST_RSP,&guest_esp);
851 if (VMCS_WRITE(EXCEPTION_BITMAP,&tmpReg ) != VMX_SUCCESS) {
852 PrintInfo("Bitmap error\n");
857 PrintTrace("VMCS_LAUNCH\n");
859 vm->state=VM_VMXASSIST_STARTUP;
861 vmm_ret = SAFE_VM_LAUNCH();
863 PrintTrace("VMM error %d\n", vmm_ret);
871 int VMLaunch(struct VMDescriptor *vm)
873 VMCS * vmcs = CreateVMCS();
876 ullong_t vmcs_ptr = (ullong_t)((uint_t)vmcs);
877 uint_t top = (vmcs_ptr>>32)&0xffffffff;
878 uint_t bottom = (vmcs_ptr)&0xffffffff;
880 theVM.vmcsregion = vmcs;
881 theVM.descriptor = *vm;
883 PrintTrace("vmcs_ptr_top=%x vmcs_ptr_bottom=%x, eip=%x\n", top, bottom, vm->entry_ip);
884 rc=MyLaunch(&theVM); // vmcs_ptr, vm->entry_ip, vm->exit_eip, vm->guest_esp);
885 PrintTrace("Returned from MyLaunch();\n");
890 VmxOnRegion * CreateVmxOnRegion() {
891 union VMX_MSR basicMSR;
892 VmxOnRegion * region = (VmxOnRegion *)(os_hooks)->allocate_pages(1);
894 Get_MSR(IA32_VMX_BASIC_MSR, &basicMSR.regs.high, &basicMSR.regs.low);
895 // memcpy(region, &basicMSR.vmxBasic.revision, sizeof(uint_t));
897 *(ulong_t*)region = basicMSR.vmxBasic.revision;
899 PrintInfo("VMX revision: 0x%lu\n", *(ulong_t *)region);
904 VMCS * CreateVMCS() {
905 union VMX_MSR basicMSR;
906 VMCS * vmcs = (VMCS *)(os_hooks)->allocate_pages(1);
908 Get_MSR(IA32_VMX_BASIC_MSR, &basicMSR.regs.high, &basicMSR.regs.low);
909 *(ulong_t *)vmcs = basicMSR.vmxBasic.revision;
910 *(ulong_t *)((char*)vmcs + 4) = 0;
912 PrintTrace("VMCS Region size: %u\n", basicMSR.vmxBasic.regionSize);
913 PrintTrace("VMCS Abort: %x\n",*(uint_t *)(((char*)vmcs)+4));