1 #include <palacios/svm.h>
2 #include <palacios/vmm.h>
4 #include <palacios/vmcb.h>
5 #include <palacios/vmm_mem.h>
6 #include <palacios/vmm_paging.h>
7 #include <palacios/svm_handler.h>
9 #include <palacios/vmm_debug.h>
10 #include <palacios/vm_guest_mem.h>
12 #include <palacios/vmm_decoder.h>
17 extern uint_t cpuid_ecx(uint_t op);
18 extern uint_t cpuid_edx(uint_t op);
19 extern void Get_MSR(uint_t MSR, uint_t * high_byte, uint_t * low_byte);
20 extern void Set_MSR(uint_t MSR, uint_t high_byte, uint_t low_byte);
21 extern uint_t launch_svm(vmcb_t * vmcb_addr);
22 extern void safe_svm_launch(vmcb_t * vmcb_addr, struct v3_gprs * gprs);
27 extern uint_t Get_CR3();
30 extern void DisableInts();
31 extern void EnableInts();
39 static vmcb_t * Allocate_VMCB() {
40 vmcb_t * vmcb_page = (vmcb_t *)V3_AllocPages(1);
43 memset(vmcb_page, 0, 4096);
52 static void Init_VMCB_BIOS(vmcb_t * vmcb, struct guest_info *vm_info) {
53 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
54 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
58 guest_state->rsp = vm_info->vm_regs.rsp;
59 // guest_state->rip = vm_info->rip;
60 guest_state->rip = 0xfff0;
64 //ctrl_area->instrs.instrs.CR0 = 1;
65 ctrl_area->cr_reads.cr0 = 1;
66 ctrl_area->cr_writes.cr0 = 1;
68 guest_state->efer |= EFER_MSR_svm_enable;
69 guest_state->rflags = 0x00000002; // The reserved bit is always 1
70 ctrl_area->svm_instrs.VMRUN = 1;
71 ctrl_area->svm_instrs.VMMCALL = 1;
72 ctrl_area->svm_instrs.VMLOAD = 1;
73 ctrl_area->svm_instrs.VMSAVE = 1;
74 ctrl_area->svm_instrs.STGI = 1;
75 ctrl_area->svm_instrs.CLGI = 1;
76 ctrl_area->svm_instrs.SKINIT = 1;
77 ctrl_area->svm_instrs.RDTSCP = 1;
78 ctrl_area->svm_instrs.ICEBP = 1;
79 ctrl_area->svm_instrs.WBINVD = 1;
80 ctrl_area->svm_instrs.MONITOR = 1;
81 ctrl_area->svm_instrs.MWAIT_always = 1;
82 ctrl_area->svm_instrs.MWAIT_if_armed = 1;
85 ctrl_area->instrs.HLT = 1;
86 // guest_state->cr0 = 0x00000001; // PE
87 ctrl_area->guest_ASID = 1;
91 ctrl_area->exceptions.de = 1;
92 ctrl_area->exceptions.df = 1;
94 ctrl_area->exceptions.ts = 1;
95 ctrl_area->exceptions.ss = 1;
96 ctrl_area->exceptions.ac = 1;
97 ctrl_area->exceptions.mc = 1;
98 ctrl_area->exceptions.gp = 1;
99 ctrl_area->exceptions.ud = 1;
100 ctrl_area->exceptions.np = 1;
101 ctrl_area->exceptions.of = 1;
103 ctrl_area->exceptions.nmi = 1;
105 // Debug of boot on physical machines - 7/14/08
106 ctrl_area->instrs.NMI=1;
107 ctrl_area->instrs.SMI=1;
108 ctrl_area->instrs.INIT=1;
109 ctrl_area->instrs.PAUSE=1;
110 ctrl_area->instrs.shutdown_evts=1;
114 vm_info->vm_regs.rdx = 0x00000f00;
116 guest_state->cr0 = 0x60000010;
118 guest_state->cs.selector = 0xf000;
119 guest_state->cs.limit=0xffff;
120 guest_state->cs.base = 0x0000000f0000LL;
121 guest_state->cs.attrib.raw = 0xf3;
124 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
125 for ( i = 0; segregs[i] != NULL; i++) {
126 struct vmcb_selector * seg = segregs[i];
128 seg->selector = 0x0000;
129 // seg->base = seg->selector << 4;
130 seg->base = 0x00000000;
131 seg->attrib.raw = 0xf3;
135 guest_state->gdtr.limit = 0x0000ffff;
136 guest_state->gdtr.base = 0x0000000000000000LL;
137 guest_state->idtr.limit = 0x0000ffff;
138 guest_state->idtr.base = 0x0000000000000000LL;
140 guest_state->ldtr.selector = 0x0000;
141 guest_state->ldtr.limit = 0x0000ffff;
142 guest_state->ldtr.base = 0x0000000000000000LL;
143 guest_state->tr.selector = 0x0000;
144 guest_state->tr.limit = 0x0000ffff;
145 guest_state->tr.base = 0x0000000000000000LL;
148 guest_state->dr6 = 0x00000000ffff0ff0LL;
149 guest_state->dr7 = 0x0000000000000400LL;
151 if (vm_info->io_map.num_ports > 0) {
152 struct vmm_io_hook * iter;
153 addr_t io_port_bitmap;
155 io_port_bitmap = (addr_t)V3_AllocPages(3);
156 memset((uchar_t*)io_port_bitmap, 0, PAGE_SIZE * 3);
158 ctrl_area->IOPM_BASE_PA = io_port_bitmap;
160 //PrintDebug("Setting up IO Map at 0x%x\n", io_port_bitmap);
162 FOREACH_IO_HOOK(vm_info->io_map, iter) {
163 ushort_t port = iter->port;
164 uchar_t * bitmap = (uchar_t *)io_port_bitmap;
166 bitmap += (port / 8);
167 PrintDebug("Setting Bit for port 0x%x\n", port);
168 *bitmap |= 1 << (port % 8);
172 //PrintDebugMemDump((uchar_t*)io_port_bitmap, PAGE_SIZE *2);
174 ctrl_area->instrs.IOIO_PROT = 1;
179 PrintDebug("Exiting on interrupts\n");
180 ctrl_area->guest_ctrl.V_INTR_MASKING = 1;
181 ctrl_area->instrs.INTR = 1;
184 if (vm_info->shdw_pg_mode == SHADOW_PAGING) {
185 PrintDebug("Creating initial shadow page table\n");
186 vm_info->direct_map_pt = (addr_t)create_passthrough_pde32_pts(vm_info);
187 vm_info->shdw_pg_state.shadow_cr3 |= (vm_info->direct_map_pt & ~0xfff);
188 vm_info->shdw_pg_state.guest_cr0 = 0x0000000000000010LL;
189 PrintDebug("Created\n");
191 guest_state->cr3 = vm_info->shdw_pg_state.shadow_cr3;
193 //PrintDebugPageTables((pde32_t*)(vm_info->shdw_pg_state.shadow_cr3.e_reg.low));
195 ctrl_area->cr_reads.cr3 = 1;
196 ctrl_area->cr_writes.cr3 = 1;
199 ctrl_area->instrs.INVLPG = 1;
200 ctrl_area->instrs.INVLPGA = 1;
202 ctrl_area->exceptions.pf = 1;
204 /* JRL: This is a performance killer, and a simplistic solution */
205 /* We need to fix this */
206 ctrl_area->TLB_CONTROL = 1;
210 guest_state->g_pat = 0x7040600070406ULL;
212 guest_state->cr0 |= 0x80000000;
214 } else if (vm_info->shdw_pg_mode == NESTED_PAGING) {
215 // Flush the TLB on entries/exits
216 ctrl_area->TLB_CONTROL = 1;
218 // Enable Nested Paging
219 ctrl_area->NP_ENABLE = 1;
221 PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
223 // Set the Nested Page Table pointer
224 vm_info->direct_map_pt = ((addr_t)create_passthrough_pde32_pts(vm_info) & ~0xfff);
225 ctrl_area->N_CR3 = vm_info->direct_map_pt;
227 // ctrl_area->N_CR3 = Get_CR3();
228 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
230 guest_state->g_pat = 0x7040600070406ULL;
238 static int init_svm_guest(struct guest_info *info) {
240 PrintDebug("Allocating VMCB\n");
241 info->vmm_data = (void*)Allocate_VMCB();
244 //PrintDebug("Generating Guest nested page tables\n");
245 // info->page_tables = NULL;
246 //info->page_tables = generate_guest_page_tables_64(&(info->mem_layout), &(info->mem_list));
247 //info->page_tables = generate_guest_page_tables(&(info->mem_layout), &(info->mem_list));
248 // PrintDebugPageTables(info->page_tables);
251 PrintDebug("Initializing VMCB (addr=%x)\n", info->vmm_data);
252 Init_VMCB_BIOS((vmcb_t*)(info->vmm_data), info);
255 info->run_state = VM_STOPPED;
259 info->vm_regs.rdi = 0;
260 info->vm_regs.rsi = 0;
261 info->vm_regs.rbp = 0;
262 info->vm_regs.rsp = 0;
263 info->vm_regs.rbx = 0;
264 info->vm_regs.rdx = 0;
265 info->vm_regs.rcx = 0;
266 info->vm_regs.rax = 0;
272 // can we start a kernel thread here...
273 static int start_svm_guest(struct guest_info *info) {
274 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
275 vmcb_ctrl_t * guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
276 uint_t num_exits = 0;
280 PrintDebug("Launching SVM VM (vmcb=%x)\n", info->vmm_data);
281 //PrintDebugVMCB((vmcb_t*)(info->vmm_data));
283 info->run_state = VM_RUNNING;
292 // PrintDebug("SVM Entry to rip=%x...\n", info->rip);
294 rdtscll(info->time_state.cached_host_tsc);
295 guest_ctrl->TSC_OFFSET = info->time_state.guest_tsc - info->time_state.cached_host_tsc;
297 safe_svm_launch((vmcb_t*)(info->vmm_data), &(info->vm_regs));
300 //PrintDebug("SVM Returned\n");
303 v3_update_time(info, tmp_tsc - info->time_state.cached_host_tsc);
308 if ((num_exits % 25) == 0) {
309 PrintDebug("SVM Exit number %d\n", num_exits);
313 if (handle_svm_exit(info) != 0) {
316 addr_t linear_addr = 0;
318 info->run_state = VM_ERROR;
320 PrintDebug("SVM ERROR!!\n");
322 PrintDebug("RIP: %x\n", guest_state->rip);
325 linear_addr = get_addr_linear(info, guest_state->rip, &(info->segments.cs));
328 PrintDebug("RIP Linear: %x\n", linear_addr);
329 PrintV3Segments(info);
330 PrintV3CtrlRegs(info);
333 if (info->mem_mode == PHYSICAL_MEM) {
334 guest_pa_to_host_pa(info, linear_addr, &host_addr);
335 } else if (info->mem_mode == VIRTUAL_MEM) {
336 guest_va_to_host_pa(info, linear_addr, &host_addr);
340 PrintDebug("Host Address of rip = 0x%x\n", host_addr);
342 PrintDebug("Instr (15 bytes) at %x:\n", host_addr);
343 PrintTraceMemDump((char*)host_addr, 15);
354 /* Checks machine SVM capability */
355 /* Implemented from: AMD Arch Manual 3, sect 15.4 */
356 int is_svm_capable() {
362 uint_t vm_cr_low = 0, vm_cr_high = 0;
365 ret = cpuid_ecx(CPUID_FEATURE_IDS);
367 PrintDebug("CPUID_FEATURE_IDS_ecx=0x%x\n",ret);
369 if ((ret & CPUID_FEATURE_IDS_ecx_svm_avail) == 0) {
370 PrintDebug("SVM Not Available\n");
373 Get_MSR(SVM_VM_CR_MSR, &vm_cr_high, &vm_cr_low);
375 PrintDebug("SVM_VM_CR_MSR = 0x%x 0x%x\n",vm_cr_high,vm_cr_low);
377 if ((vm_cr_low & SVM_VM_CR_MSR_svmdis) == 1) {
378 PrintDebug("SVM is available but is disabled.\n");
380 ret = cpuid_edx(CPUID_SVM_REV_AND_FEATURE_IDS);
382 PrintDebug("CPUID_FEATURE_IDS_edx=0x%x\n",ret);
384 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_svml) == 0) {
385 PrintDebug("SVM BIOS Disabled, not unlockable\n");
387 PrintDebug("SVM is locked with a key\n");
392 PrintDebug("SVM is available and enabled.\n");
394 ret = cpuid_edx(CPUID_SVM_REV_AND_FEATURE_IDS);
396 PrintDebug("CPUID_FEATURE_IDS_edx=0x%x\n",ret);
398 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 0) {
399 PrintDebug("SVM Nested Paging not supported\n");
401 PrintDebug("SVM Nested Paging supported\n");
411 uint_t ret = cpuid_ecx(CPUID_FEATURE_IDS);
412 uint_t vm_cr_low = 0, vm_cr_high = 0;
415 if ((ret & CPUID_FEATURE_IDS_ecx_svm_avail) == 0) {
416 PrintDebug("SVM Not Available\n");
420 Get_MSR(SVM_VM_CR_MSR, &vm_cr_high, &vm_cr_low);
422 PrintDebug("SVM_VM_CR_MSR = 0x%x 0x%x\n",vm_cr_high,vm_cr_low);
425 // this part is clearly wrong, since the np bit is in
427 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 1) {
428 PrintDebug("Nested Paging not supported\n");
430 PrintDebug("Nested Paging supported\n");
433 if ((vm_cr_low & SVM_VM_CR_MSR_svmdis) == 0) {
434 PrintDebug("SVM is disabled.\n");
438 ret = cpuid_edx(CPUID_SVM_REV_AND_FEATURE_IDS);
440 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_svml) == 0) {
441 PrintDebug("SVM BIOS Disabled, not unlockable\n");
443 PrintDebug("SVM is locked with a key\n");
452 int has_svm_nested_paging() {
455 ret = cpuid_edx(CPUID_SVM_REV_AND_FEATURE_IDS);
457 //PrintDebug("CPUID_FEATURE_IDS_edx=0x%x\n",ret);
459 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 0) {
460 PrintDebug("SVM Nested Paging not supported\n");
463 PrintDebug("SVM Nested Paging supported\n");
471 void Init_SVM(struct vmm_ctrl_ops * vmm_ops) {
476 // Enable SVM on the CPU
477 Get_MSR(EFER_MSR, &(msr.e_reg.high), &(msr.e_reg.low));
478 msr.e_reg.low |= EFER_MSR_svm_enable;
479 Set_MSR(EFER_MSR, 0, msr.e_reg.low);
481 PrintDebug("SVM Enabled\n");
484 // Setup the host state save area
485 host_state = V3_AllocPages(4);
488 msr.e_reg.low = (uint_t)host_state;
491 PrintDebug("Host State being saved at %x\n", (uint_t)host_state);
492 Set_MSR(SVM_VM_HSAVE_PA_MSR, msr.e_reg.high, msr.e_reg.low);
496 // Setup the SVM specific vmm operations
497 vmm_ops->init_guest = &init_svm_guest;
498 vmm_ops->start_guest = &start_svm_guest;
499 vmm_ops->has_nested_paging = &has_svm_nested_paging;
555 /*static void Init_VMCB(vmcb_t * vmcb, struct guest_info vm_info) {
556 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
557 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
561 guest_state->rsp = vm_info.vm_regs.rsp;
562 guest_state->rip = vm_info.rip;
565 //ctrl_area->instrs.instrs.CR0 = 1;
566 ctrl_area->cr_reads.cr0 = 1;
567 ctrl_area->cr_writes.cr0 = 1;
569 guest_state->efer |= EFER_MSR_svm_enable;
570 guest_state->rflags = 0x00000002; // The reserved bit is always 1
571 ctrl_area->svm_instrs.VMRUN = 1;
572 // guest_state->cr0 = 0x00000001; // PE
573 ctrl_area->guest_ASID = 1;
576 ctrl_area->exceptions.de = 1;
577 ctrl_area->exceptions.df = 1;
578 ctrl_area->exceptions.pf = 1;
579 ctrl_area->exceptions.ts = 1;
580 ctrl_area->exceptions.ss = 1;
581 ctrl_area->exceptions.ac = 1;
582 ctrl_area->exceptions.mc = 1;
583 ctrl_area->exceptions.gp = 1;
584 ctrl_area->exceptions.ud = 1;
585 ctrl_area->exceptions.np = 1;
586 ctrl_area->exceptions.of = 1;
587 ctrl_area->exceptions.nmi = 1;
589 guest_state->cs.selector = 0x0000;
590 guest_state->cs.limit=~0u;
591 guest_state->cs.base = guest_state->cs.selector<<4;
592 guest_state->cs.attrib.raw = 0xf3;
595 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
596 for ( i = 0; segregs[i] != NULL; i++) {
597 struct vmcb_selector * seg = segregs[i];
599 seg->selector = 0x0000;
600 seg->base = seg->selector << 4;
601 seg->attrib.raw = 0xf3;
605 if (vm_info.io_map.num_ports > 0) {
606 struct vmm_io_hook * iter;
607 addr_t io_port_bitmap;
609 io_port_bitmap = (addr_t)V3_AllocPages(3);
610 memset((uchar_t*)io_port_bitmap, 0, PAGE_SIZE * 3);
612 ctrl_area->IOPM_BASE_PA = io_port_bitmap;
614 //PrintDebug("Setting up IO Map at 0x%x\n", io_port_bitmap);
616 FOREACH_IO_HOOK(vm_info.io_map, iter) {
617 ushort_t port = iter->port;
618 uchar_t * bitmap = (uchar_t *)io_port_bitmap;
620 bitmap += (port / 8);
621 PrintDebug("Setting Bit in block %x\n", bitmap);
622 *bitmap |= 1 << (port % 8);
626 //PrintDebugMemDump((uchar_t*)io_port_bitmap, PAGE_SIZE *2);
628 ctrl_area->instrs.IOIO_PROT = 1;
631 ctrl_area->instrs.INTR = 1;
635 if (vm_info.page_mode == SHADOW_PAGING) {
636 PrintDebug("Creating initial shadow page table\n");
637 vm_info.shdw_pg_state.shadow_cr3 |= ((addr_t)create_passthrough_pde32_pts(&vm_info) & ~0xfff);
638 PrintDebug("Created\n");
640 guest_state->cr3 = vm_info.shdw_pg_state.shadow_cr3;
642 ctrl_area->cr_reads.cr3 = 1;
643 ctrl_area->cr_writes.cr3 = 1;
646 ctrl_area->instrs.INVLPG = 1;
647 ctrl_area->instrs.INVLPGA = 1;
649 guest_state->g_pat = 0x7040600070406ULL;
651 guest_state->cr0 |= 0x80000000;
652 } else if (vm_info.page_mode == NESTED_PAGING) {
653 // Flush the TLB on entries/exits
654 //ctrl_area->TLB_CONTROL = 1;
656 // Enable Nested Paging
657 //ctrl_area->NP_ENABLE = 1;
659 //PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
661 // Set the Nested Page Table pointer
662 // ctrl_area->N_CR3 = ((addr_t)vm_info.page_tables);
663 // ctrl_area->N_CR3 = (addr_t)(vm_info.page_tables);
665 // ctrl_area->N_CR3 = Get_CR3();
666 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
668 // guest_state->g_pat = 0x7040600070406ULL;
683 void Init_VMCB_pe(vmcb_t *vmcb, struct guest_info vm_info) {
684 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
685 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
689 guest_state->rsp = vm_info.vm_regs.rsp;
690 guest_state->rip = vm_info.rip;
693 /* I pretty much just gutted this from TVMM */
694 /* Note: That means its probably wrong */
696 // set the segment registers to mirror ours
697 guest_state->cs.selector = 1<<3;
698 guest_state->cs.attrib.fields.type = 0xa; // Code segment+read
699 guest_state->cs.attrib.fields.S = 1;
700 guest_state->cs.attrib.fields.P = 1;
701 guest_state->cs.attrib.fields.db = 1;
702 guest_state->cs.attrib.fields.G = 1;
703 guest_state->cs.limit = 0xfffff;
704 guest_state->cs.base = 0;
706 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
707 for ( i = 0; segregs[i] != NULL; i++) {
708 struct vmcb_selector * seg = segregs[i];
710 seg->selector = 2<<3;
711 seg->attrib.fields.type = 0x2; // Data Segment+read/write
712 seg->attrib.fields.S = 1;
713 seg->attrib.fields.P = 1;
714 seg->attrib.fields.db = 1;
715 seg->attrib.fields.G = 1;
716 seg->limit = 0xfffff;
722 /* JRL THIS HAS TO GO */
724 // guest_state->tr.selector = GetTR_Selector();
725 guest_state->tr.attrib.fields.type = 0x9;
726 guest_state->tr.attrib.fields.P = 1;
727 // guest_state->tr.limit = GetTR_Limit();
728 //guest_state->tr.base = GetTR_Base();// - 0x2000;
736 guest_state->efer |= EFER_MSR_svm_enable;
737 guest_state->rflags = 0x00000002; // The reserved bit is always 1
738 ctrl_area->svm_instrs.VMRUN = 1;
739 guest_state->cr0 = 0x00000001; // PE
740 ctrl_area->guest_ASID = 1;
743 // guest_state->cpl = 0;
749 ctrl_area->cr_writes.cr4 = 1;
751 ctrl_area->exceptions.de = 1;
752 ctrl_area->exceptions.df = 1;
753 ctrl_area->exceptions.pf = 1;
754 ctrl_area->exceptions.ts = 1;
755 ctrl_area->exceptions.ss = 1;
756 ctrl_area->exceptions.ac = 1;
757 ctrl_area->exceptions.mc = 1;
758 ctrl_area->exceptions.gp = 1;
759 ctrl_area->exceptions.ud = 1;
760 ctrl_area->exceptions.np = 1;
761 ctrl_area->exceptions.of = 1;
762 ctrl_area->exceptions.nmi = 1;
766 ctrl_area->instrs.IOIO_PROT = 1;
767 ctrl_area->IOPM_BASE_PA = (uint_t)V3_AllocPages(3);
771 tmp_reg.r_reg = ctrl_area->IOPM_BASE_PA;
772 memset((void*)(tmp_reg.e_reg.low), 0xffffffff, PAGE_SIZE * 2);
775 ctrl_area->instrs.INTR = 1;
782 memset(gdt_buf, 0, 6);
783 memset(idt_buf, 0, 6);
786 uint_t gdt_base, idt_base;
787 ushort_t gdt_limit, idt_limit;
790 gdt_base = *(ulong_t*)((uchar_t*)gdt_buf + 2) & 0xffffffff;
791 gdt_limit = *(ushort_t*)(gdt_buf) & 0xffff;
792 PrintDebug("GDT: base: %x, limit: %x\n", gdt_base, gdt_limit);
795 idt_base = *(ulong_t*)(idt_buf + 2) & 0xffffffff;
796 idt_limit = *(ushort_t*)(idt_buf) & 0xffff;
797 PrintDebug("IDT: base: %x, limit: %x\n",idt_base, idt_limit);
800 // gdt_base -= 0x2000;
801 //idt_base -= 0x2000;
803 guest_state->gdtr.base = gdt_base;
804 guest_state->gdtr.limit = gdt_limit;
805 guest_state->idtr.base = idt_base;
806 guest_state->idtr.limit = idt_limit;
812 // also determine if CPU supports nested paging
814 if (vm_info.page_tables) {
816 // Flush the TLB on entries/exits
817 ctrl_area->TLB_CONTROL = 1;
819 // Enable Nested Paging
820 ctrl_area->NP_ENABLE = 1;
822 PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
824 // Set the Nested Page Table pointer
825 ctrl_area->N_CR3 |= ((addr_t)vm_info.page_tables & 0xfffff000);
828 // ctrl_area->N_CR3 = Get_CR3();
829 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
831 guest_state->g_pat = 0x7040600070406ULL;
833 PrintDebug("Set Nested CR3: lo: 0x%x hi: 0x%x\n", (uint_t)*(&(ctrl_area->N_CR3)), (uint_t)*((unsigned char *)&(ctrl_area->N_CR3) + 4));
834 PrintDebug("Set Guest CR3: lo: 0x%x hi: 0x%x\n", (uint_t)*(&(guest_state->cr3)), (uint_t)*((unsigned char *)&(guest_state->cr3) + 4));
836 // guest_state->cr0 |= 0x80000000;