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, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
24 #include <palacios/svm.h>
25 #include <palacios/vmm.h>
27 #include <palacios/vmcb.h>
28 #include <palacios/vmm_mem.h>
29 #include <palacios/vmm_paging.h>
30 #include <palacios/svm_handler.h>
32 #include <palacios/vmm_debug.h>
33 #include <palacios/vm_guest_mem.h>
35 #include <palacios/vmm_decoder.h>
36 #include <palacios/vmm_string.h>
37 #include <palacios/vmm_lowlevel.h>
38 #include <palacios/svm_msr.h>
40 #include <palacios/vmm_rbtree.h>
42 #include <palacios/vmm_profiler.h>
44 #include <palacios/vmm_direct_paging.h>
46 extern void v3_stgi();
47 extern void v3_clgi();
48 //extern int v3_svm_launch(vmcb_t * vmcb, struct v3_gprs * vm_regs, uint64_t * fs, uint64_t * gs);
49 extern int v3_svm_launch(vmcb_t * vmcb, struct v3_gprs * vm_regs);
54 static vmcb_t * Allocate_VMCB() {
55 vmcb_t * vmcb_page = (vmcb_t *)V3_VAddr(V3_AllocPages(1));
57 memset(vmcb_page, 0, 4096);
64 #include <palacios/vmm_ctrl_regs.h>
66 static void Init_VMCB_BIOS(vmcb_t * vmcb, struct guest_info *vm_info) {
67 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
68 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
72 guest_state->rsp = vm_info->vm_regs.rsp;
73 // guest_state->rip = vm_info->rip;
74 guest_state->rip = 0xfff0;
78 //ctrl_area->instrs.instrs.CR0 = 1;
79 ctrl_area->cr_reads.cr0 = 1;
80 ctrl_area->cr_writes.cr0 = 1;
81 //ctrl_area->cr_reads.cr4 = 1;
82 ctrl_area->cr_writes.cr4 = 1;
85 /* Set up the efer to enable 64 bit page tables */
88 struct efer_64 * efer = (struct efer_64 *)&(guest_state->efer);
89 struct cr4_32 * cr4 = (struct cr4_32 *)&(guest_state->cr4);
97 guest_state->efer |= EFER_MSR_svm_enable;
98 vm_info->guest_efer.value = 0x0LL;
100 v3_hook_msr(vm_info, EFER_MSR,
101 &v3_handle_efer_read,
102 &v3_handle_efer_write,
107 guest_state->rflags = 0x00000002; // The reserved bit is always 1
108 ctrl_area->svm_instrs.VMRUN = 1;
109 ctrl_area->svm_instrs.VMMCALL = 1;
110 ctrl_area->svm_instrs.VMLOAD = 1;
111 ctrl_area->svm_instrs.VMSAVE = 1;
112 ctrl_area->svm_instrs.STGI = 1;
113 ctrl_area->svm_instrs.CLGI = 1;
114 ctrl_area->svm_instrs.SKINIT = 1;
115 ctrl_area->svm_instrs.RDTSCP = 1;
116 ctrl_area->svm_instrs.ICEBP = 1;
117 ctrl_area->svm_instrs.WBINVD = 1;
118 ctrl_area->svm_instrs.MONITOR = 1;
119 ctrl_area->svm_instrs.MWAIT_always = 1;
120 ctrl_area->svm_instrs.MWAIT_if_armed = 1;
123 ctrl_area->instrs.HLT = 1;
124 // guest_state->cr0 = 0x00000001; // PE
125 ctrl_area->guest_ASID = 1;
129 ctrl_area->exceptions.de = 1;
130 ctrl_area->exceptions.df = 1;
132 ctrl_area->exceptions.ts = 1;
133 ctrl_area->exceptions.ss = 1;
134 ctrl_area->exceptions.ac = 1;
135 ctrl_area->exceptions.mc = 1;
136 ctrl_area->exceptions.gp = 1;
137 ctrl_area->exceptions.ud = 1;
138 ctrl_area->exceptions.np = 1;
139 ctrl_area->exceptions.of = 1;
141 ctrl_area->exceptions.nmi = 1;
145 // Debug of boot on physical machines - 7/14/08
146 ctrl_area->instrs.NMI=1;
147 ctrl_area->instrs.SMI=1;
148 ctrl_area->instrs.INIT=1;
149 ctrl_area->instrs.PAUSE=1;
150 ctrl_area->instrs.shutdown_evts=1;
152 vm_info->vm_regs.rdx = 0x00000f00;
154 guest_state->cr0 = 0x60000010;
157 guest_state->cs.selector = 0xf000;
158 guest_state->cs.limit=0xffff;
159 guest_state->cs.base = 0x0000000f0000LL;
160 guest_state->cs.attrib.raw = 0xf3;
163 /* DEBUG FOR RETURN CODE */
164 ctrl_area->exit_code = 1;
167 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
168 for ( i = 0; segregs[i] != NULL; i++) {
169 struct vmcb_selector * seg = segregs[i];
171 seg->selector = 0x0000;
172 // seg->base = seg->selector << 4;
173 seg->base = 0x00000000;
174 seg->attrib.raw = 0xf3;
178 guest_state->gdtr.limit = 0x0000ffff;
179 guest_state->gdtr.base = 0x0000000000000000LL;
180 guest_state->idtr.limit = 0x0000ffff;
181 guest_state->idtr.base = 0x0000000000000000LL;
183 guest_state->ldtr.selector = 0x0000;
184 guest_state->ldtr.limit = 0x0000ffff;
185 guest_state->ldtr.base = 0x0000000000000000LL;
186 guest_state->tr.selector = 0x0000;
187 guest_state->tr.limit = 0x0000ffff;
188 guest_state->tr.base = 0x0000000000000000LL;
191 guest_state->dr6 = 0x00000000ffff0ff0LL;
192 guest_state->dr7 = 0x0000000000000400LL;
199 if ( !RB_EMPTY_ROOT(&(vm_info->io_map)) ) {
200 struct v3_io_hook * iter;
201 struct rb_node * io_node = v3_rb_first(&(vm_info->io_map));
202 addr_t io_port_bitmap;
205 io_port_bitmap = (addr_t)V3_VAddr(V3_AllocPages(3));
206 memset((uchar_t*)io_port_bitmap, 0, PAGE_SIZE * 3);
208 ctrl_area->IOPM_BASE_PA = (addr_t)V3_PAddr((void *)io_port_bitmap);
210 //PrintDebug("Setting up IO Map at 0x%x\n", io_port_bitmap);
213 iter = rb_entry(io_node, struct v3_io_hook, tree_node);
215 ushort_t port = iter->port;
216 uchar_t * bitmap = (uchar_t *)io_port_bitmap;
217 //PrintDebug("%d: Hooking Port %d\n", i, port);
219 bitmap += (port / 8);
220 // PrintDebug("Setting Bit for port 0x%x\n", port);
221 *bitmap |= 1 << (port % 8);
224 } while ((io_node = v3_rb_next(io_node)));
227 //PrintDebugMemDump((uchar_t*)io_port_bitmap, PAGE_SIZE *2);
229 ctrl_area->instrs.IOIO_PROT = 1;
233 PrintDebug("Exiting on interrupts\n");
234 ctrl_area->guest_ctrl.V_INTR_MASKING = 1;
235 ctrl_area->instrs.INTR = 1;
238 if (vm_info->shdw_pg_mode == SHADOW_PAGING) {
239 PrintDebug("Creating initial shadow page table\n");
243 /* Testing 64 bit page tables for long paged real mode guests */
244 // vm_info->direct_map_pt = (addr_t)V3_PAddr(create_passthrough_pts_64(vm_info));
245 vm_info->direct_map_pt = (addr_t)V3_PAddr(v3_create_direct_passthrough_pts(vm_info));
248 vm_info->shdw_pg_state.guest_cr0 = 0x0000000000000010LL;
249 PrintDebug("Created\n");
252 guest_state->cr3 = vm_info->direct_map_pt;
255 //PrintDebugPageTables((pde32_t*)(vm_info->shdw_pg_state.shadow_cr3.e_reg.low));
257 ctrl_area->cr_reads.cr3 = 1;
258 ctrl_area->cr_writes.cr3 = 1;
261 ctrl_area->instrs.INVLPG = 1;
262 ctrl_area->instrs.INVLPGA = 1;
264 ctrl_area->exceptions.pf = 1;
266 /* JRL: This is a performance killer, and a simplistic solution */
267 /* We need to fix this */
268 ctrl_area->TLB_CONTROL = 1;
271 guest_state->g_pat = 0x7040600070406ULL;
273 guest_state->cr0 |= 0x80000000;
275 } else if (vm_info->shdw_pg_mode == NESTED_PAGING) {
276 // Flush the TLB on entries/exits
277 ctrl_area->TLB_CONTROL = 1;
279 // Enable Nested Paging
280 ctrl_area->NP_ENABLE = 1;
282 PrintDebug("NP_Enable at 0x%p\n", (void *)&(ctrl_area->NP_ENABLE));
284 // Set the Nested Page Table pointer
285 vm_info->direct_map_pt = ((addr_t)create_passthrough_pts_32(vm_info) & ~0xfff);
286 ctrl_area->N_CR3 = vm_info->direct_map_pt;
288 // ctrl_area->N_CR3 = Get_CR3();
289 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
291 guest_state->g_pat = 0x7040600070406ULL;
295 if (vm_info->msr_map.num_hooks > 0) {
296 PrintDebug("Hooking %d msrs\n", vm_info->msr_map.num_hooks);
297 ctrl_area->MSRPM_BASE_PA = v3_init_svm_msr_map(vm_info);
298 ctrl_area->instrs.MSR_PROT = 1;
303 /* Safety locations for fs/gs */
312 static int init_svm_guest(struct guest_info *info) {
314 PrintDebug("Allocating VMCB\n");
315 info->vmm_data = (void*)Allocate_VMCB();
318 //PrintDebug("Generating Guest nested page tables\n");
319 // info->page_tables = NULL;
320 //info->page_tables = generate_guest_page_tables_64(&(info->mem_layout), &(info->mem_list));
321 //info->page_tables = generate_guest_page_tables(&(info->mem_layout), &(info->mem_list));
322 // PrintDebugPageTables(info->page_tables);
325 PrintDebug("Initializing VMCB (addr=%p)\n", (void *)info->vmm_data);
326 Init_VMCB_BIOS((vmcb_t*)(info->vmm_data), info);
329 info->run_state = VM_STOPPED;
333 info->vm_regs.rdi = 0;
334 info->vm_regs.rsi = 0;
335 info->vm_regs.rbp = 0;
336 info->vm_regs.rsp = 0;
337 info->vm_regs.rbx = 0;
338 info->vm_regs.rdx = 0;
339 info->vm_regs.rcx = 0;
340 info->vm_regs.rax = 0;
347 // can we start a kernel thread here...
348 static int start_svm_guest(struct guest_info *info) {
349 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
350 // vmcb_ctrl_t * guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
351 uint_t num_exits = 0;
355 PrintDebug("Launching SVM VM (vmcb=%p)\n", (void *)info->vmm_data);
356 //PrintDebugVMCB((vmcb_t*)(info->vmm_data));
358 info->run_state = VM_RUNNING;
365 #define MSR_STAR 0xc0000081
366 #define MSR_LSTAR 0xc0000082
367 #define MSR_CSTAR 0xc0000083
368 #define MSR_SF_MASK 0xc0000084
369 #define MSR_GS_BASE 0xc0000101
370 #define MSR_KERNGS_BASE 0xc0000102
373 struct v3_msr host_cstar;
374 struct v3_msr host_star;
375 struct v3_msr host_lstar;
376 struct v3_msr host_syscall_mask;
377 struct v3_msr host_gs_base;
378 struct v3_msr host_kerngs_base;
385 PrintDebug("SVM Entry to CS=%p rip=%p...\n",
386 (void *)(addr_t)info->segments.cs.base,
387 (void *)(addr_t)info->rip);
391 v3_get_msr(MSR_STAR, &(host_star.hi), &(host_star.lo));
392 v3_get_msr(MSR_LSTAR, &(host_lstar.hi), &(host_lstar.lo));
393 v3_get_msr(MSR_CSTAR, &(host_cstar.hi), &(host_cstar.lo));
394 v3_get_msr(MSR_SF_MASK, &(host_syscall_mask.hi), &(host_syscall_mask.lo));
395 v3_get_msr(MSR_GS_BASE, &(host_gs_base.hi), &(host_gs_base.lo));
396 v3_get_msr(MSR_KERNGS_BASE, &(host_kerngs_base.hi), &(host_kerngs_base.lo));
399 rdtscll(info->time_state.cached_host_tsc);
400 // guest_ctrl->TSC_OFFSET = info->time_state.guest_tsc - info->time_state.cached_host_tsc;
402 //v3_svm_launch((vmcb_t*)V3_PAddr(info->vmm_data), &(info->vm_regs), &(info->fs), &(info->gs));
403 v3_svm_launch((vmcb_t*)V3_PAddr(info->vmm_data), &(info->vm_regs));
407 v3_set_msr(MSR_STAR, host_star.hi, host_star.lo);
408 v3_set_msr(MSR_LSTAR, host_lstar.hi, host_lstar.lo);
409 v3_set_msr(MSR_CSTAR, host_cstar.hi, host_cstar.lo);
410 v3_set_msr(MSR_SF_MASK, host_syscall_mask.hi, host_syscall_mask.lo);
411 v3_set_msr(MSR_GS_BASE, host_gs_base.hi, host_gs_base.lo);
412 v3_set_msr(MSR_KERNGS_BASE, host_kerngs_base.hi, host_kerngs_base.lo);
414 //PrintDebug("SVM Returned\n");
418 v3_update_time(info, tmp_tsc - info->time_state.cached_host_tsc);
421 //PrintDebug("Turning on global interrupts\n");
425 if ((num_exits % 5000) == 0) {
426 PrintDebug("SVM Exit number %d\n", num_exits);
427 if (info->enable_profiler)
428 v3_print_profile(info);
433 if (v3_handle_svm_exit(info) != 0) {
434 vmcb_ctrl_t * guest_ctrl = GET_VMCB_CTRL_AREA((vmcb_t*)(info->vmm_data));
436 addr_t linear_addr = 0;
438 info->run_state = VM_ERROR;
440 PrintDebug("SVM ERROR!!\n");
442 PrintDebug("RIP: %p\n", (void *)(addr_t)(guest_state->rip));
445 linear_addr = get_addr_linear(info, guest_state->rip, &(info->segments.cs));
448 PrintDebug("RIP Linear: %p\n", (void *)linear_addr);
449 v3_print_segments(info);
450 v3_print_ctrl_regs(info);
451 if (info->shdw_pg_mode == SHADOW_PAGING) {
452 PrintDebug("Shadow Paging Guest Registers:\n");
453 PrintDebug("\tGuest CR0=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr0));
454 PrintDebug("\tGuest CR3=%p\n", (void *)(addr_t)(info->shdw_pg_state.guest_cr3));
460 PrintDebug("SVM Exit Code: %p\n", (void *)(addr_t)guest_ctrl->exit_code);
462 PrintDebug("exit_info1 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info1));
463 PrintDebug("exit_info1 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info1)) + 4));
465 PrintDebug("exit_info2 low = 0x%.8x\n", *(uint_t*)&(guest_ctrl->exit_info2));
466 PrintDebug("exit_info2 high = 0x%.8x\n", *(uint_t *)(((uchar_t *)&(guest_ctrl->exit_info2)) + 4));
468 if (info->mem_mode == PHYSICAL_MEM) {
469 guest_pa_to_host_va(info, linear_addr, &host_addr);
470 } else if (info->mem_mode == VIRTUAL_MEM) {
471 guest_va_to_host_va(info, linear_addr, &host_addr);
475 PrintDebug("Host Address of rip = 0x%p\n", (void *)host_addr);
477 PrintDebug("Instr (15 bytes) at %p:\n", (void *)host_addr);
478 PrintTraceMemDump((uchar_t *)host_addr, 15);
491 /* Checks machine SVM capability */
492 /* Implemented from: AMD Arch Manual 3, sect 15.4 */
493 int v3_is_svm_capable() {
497 uint_t vm_cr_low = 0, vm_cr_high = 0;
498 addr_t eax = 0, ebx = 0, ecx = 0, edx = 0;
500 v3_cpuid(CPUID_FEATURE_IDS, &eax, &ebx, &ecx, &edx);
502 PrintDebug("CPUID_FEATURE_IDS_ecx=%p\n", (void *)ecx);
504 if ((ecx & CPUID_FEATURE_IDS_ecx_svm_avail) == 0) {
505 PrintDebug("SVM Not Available\n");
508 v3_get_msr(SVM_VM_CR_MSR, &vm_cr_high, &vm_cr_low);
510 PrintDebug("SVM_VM_CR_MSR = 0x%x 0x%x\n", vm_cr_high, vm_cr_low);
512 if ((vm_cr_low & SVM_VM_CR_MSR_svmdis) == 1) {
513 PrintDebug("SVM is available but is disabled.\n");
515 v3_cpuid(CPUID_SVM_REV_AND_FEATURE_IDS, &eax, &ebx, &ecx, &edx);
517 PrintDebug("CPUID_FEATURE_IDS_edx=%p\n", (void *)edx);
519 if ((edx & CPUID_SVM_REV_AND_FEATURE_IDS_edx_svml) == 0) {
520 PrintDebug("SVM BIOS Disabled, not unlockable\n");
522 PrintDebug("SVM is locked with a key\n");
527 PrintDebug("SVM is available and enabled.\n");
529 v3_cpuid(CPUID_SVM_REV_AND_FEATURE_IDS, &eax, &ebx, &ecx, &edx);
530 PrintDebug("CPUID_FEATURE_IDS_eax=%p\n", (void *)eax);
531 PrintDebug("CPUID_FEATURE_IDS_ebx=%p\n", (void *)ebx);
532 PrintDebug("CPUID_FEATURE_IDS_ecx=%p\n", (void *)ecx);
533 PrintDebug("CPUID_FEATURE_IDS_edx=%p\n", (void *)edx);
536 if ((edx & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 0) {
537 PrintDebug("SVM Nested Paging not supported\n");
539 PrintDebug("SVM Nested Paging supported\n");
548 uint_t eax = 0, ebx = 0, ecx = 0, edx = 0;
549 addr_t vm_cr_low = 0, vm_cr_high = 0;
551 v3_cpuid(CPUID_FEATURE_IDS, &eax, &ebx, &ecx, &edx);
553 if ((ecx & CPUID_FEATURE_IDS_ecx_svm_avail) == 0) {
554 PrintDebug("SVM Not Available\n");
558 v3_get_msr(SVM_VM_CR_MSR, &vm_cr_high, &vm_cr_low);
560 PrintDebug("SVM_VM_CR_MSR = 0x%x 0x%x\n", vm_cr_high, vm_cr_low);
563 // this part is clearly wrong, since the np bit is in
565 if ((edx & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 1) {
566 PrintDebug("Nested Paging not supported\n");
568 PrintDebug("Nested Paging supported\n");
571 if ((vm_cr_low & SVM_VM_CR_MSR_svmdis) == 0) {
572 PrintDebug("SVM is disabled.\n");
576 v3_cpuid(CPUID_SVM_REV_AND_FEATURE_IDS, &eax, &ebx, &ecx, &edx);
578 if ((edx & CPUID_SVM_REV_AND_FEATURE_IDS_edx_svml) == 0) {
579 PrintDebug("SVM BIOS Disabled, not unlockable\n");
581 PrintDebug("SVM is locked with a key\n");
590 static int has_svm_nested_paging() {
591 addr_t eax = 0, ebx = 0, ecx = 0, edx = 0;
593 v3_cpuid(CPUID_SVM_REV_AND_FEATURE_IDS, &eax, &ebx, &ecx, &edx);
595 //PrintDebug("CPUID_FEATURE_IDS_edx=0x%x\n", edx);
597 if ((edx & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 0) {
598 PrintDebug("SVM Nested Paging not supported\n");
601 PrintDebug("SVM Nested Paging supported\n");
609 void v3_init_SVM(struct v3_ctrl_ops * vmm_ops) {
614 // Enable SVM on the CPU
615 v3_get_msr(EFER_MSR, &(msr.e_reg.high), &(msr.e_reg.low));
616 msr.e_reg.low |= EFER_MSR_svm_enable;
617 v3_set_msr(EFER_MSR, 0, msr.e_reg.low);
619 PrintDebug("SVM Enabled\n");
622 // Setup the host state save area
623 host_state = V3_AllocPages(4);
627 // msr.e_reg.high = 0;
628 //msr.e_reg.low = (uint_t)host_state;
629 msr.r_reg = (addr_t)host_state;
631 PrintDebug("Host State being saved at %p\n", (void *)(addr_t)host_state);
632 v3_set_msr(SVM_VM_HSAVE_PA_MSR, msr.e_reg.high, msr.e_reg.low);
636 // Setup the SVM specific vmm operations
637 vmm_ops->init_guest = &init_svm_guest;
638 vmm_ops->start_guest = &start_svm_guest;
639 vmm_ops->has_nested_paging = &has_svm_nested_paging;
695 /*static void Init_VMCB(vmcb_t * vmcb, struct guest_info vm_info) {
696 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
697 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
701 guest_state->rsp = vm_info.vm_regs.rsp;
702 guest_state->rip = vm_info.rip;
705 //ctrl_area->instrs.instrs.CR0 = 1;
706 ctrl_area->cr_reads.cr0 = 1;
707 ctrl_area->cr_writes.cr0 = 1;
709 guest_state->efer |= EFER_MSR_svm_enable;
710 guest_state->rflags = 0x00000002; // The reserved bit is always 1
711 ctrl_area->svm_instrs.VMRUN = 1;
712 // guest_state->cr0 = 0x00000001; // PE
713 ctrl_area->guest_ASID = 1;
716 ctrl_area->exceptions.de = 1;
717 ctrl_area->exceptions.df = 1;
718 ctrl_area->exceptions.pf = 1;
719 ctrl_area->exceptions.ts = 1;
720 ctrl_area->exceptions.ss = 1;
721 ctrl_area->exceptions.ac = 1;
722 ctrl_area->exceptions.mc = 1;
723 ctrl_area->exceptions.gp = 1;
724 ctrl_area->exceptions.ud = 1;
725 ctrl_area->exceptions.np = 1;
726 ctrl_area->exceptions.of = 1;
727 ctrl_area->exceptions.nmi = 1;
729 guest_state->cs.selector = 0x0000;
730 guest_state->cs.limit=~0u;
731 guest_state->cs.base = guest_state->cs.selector<<4;
732 guest_state->cs.attrib.raw = 0xf3;
735 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
736 for ( i = 0; segregs[i] != NULL; i++) {
737 struct vmcb_selector * seg = segregs[i];
739 seg->selector = 0x0000;
740 seg->base = seg->selector << 4;
741 seg->attrib.raw = 0xf3;
745 if (vm_info.io_map.num_ports > 0) {
746 struct vmm_io_hook * iter;
747 addr_t io_port_bitmap;
749 io_port_bitmap = (addr_t)V3_AllocPages(3);
750 memset((uchar_t*)io_port_bitmap, 0, PAGE_SIZE * 3);
752 ctrl_area->IOPM_BASE_PA = io_port_bitmap;
754 //PrintDebug("Setting up IO Map at 0x%x\n", io_port_bitmap);
756 FOREACH_IO_HOOK(vm_info.io_map, iter) {
757 ushort_t port = iter->port;
758 uchar_t * bitmap = (uchar_t *)io_port_bitmap;
760 bitmap += (port / 8);
761 PrintDebug("Setting Bit in block %x\n", bitmap);
762 *bitmap |= 1 << (port % 8);
766 //PrintDebugMemDump((uchar_t*)io_port_bitmap, PAGE_SIZE *2);
768 ctrl_area->instrs.IOIO_PROT = 1;
771 ctrl_area->instrs.INTR = 1;
775 if (vm_info.page_mode == SHADOW_PAGING) {
776 PrintDebug("Creating initial shadow page table\n");
777 vm_info.shdw_pg_state.shadow_cr3 |= ((addr_t)create_passthrough_pts_32(&vm_info) & ~0xfff);
778 PrintDebug("Created\n");
780 guest_state->cr3 = vm_info.shdw_pg_state.shadow_cr3;
782 ctrl_area->cr_reads.cr3 = 1;
783 ctrl_area->cr_writes.cr3 = 1;
786 ctrl_area->instrs.INVLPG = 1;
787 ctrl_area->instrs.INVLPGA = 1;
789 guest_state->g_pat = 0x7040600070406ULL;
791 guest_state->cr0 |= 0x80000000;
792 } else if (vm_info.page_mode == NESTED_PAGING) {
793 // Flush the TLB on entries/exits
794 //ctrl_area->TLB_CONTROL = 1;
796 // Enable Nested Paging
797 //ctrl_area->NP_ENABLE = 1;
799 //PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
801 // Set the Nested Page Table pointer
802 // ctrl_area->N_CR3 = ((addr_t)vm_info.page_tables);
803 // ctrl_area->N_CR3 = (addr_t)(vm_info.page_tables);
805 // ctrl_area->N_CR3 = Get_CR3();
806 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
808 // guest_state->g_pat = 0x7040600070406ULL;
823 void Init_VMCB_pe(vmcb_t *vmcb, struct guest_info vm_info) {
824 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
825 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
829 guest_state->rsp = vm_info.vm_regs.rsp;
830 guest_state->rip = vm_info.rip;
833 /* I pretty much just gutted this from TVMM */
834 /* Note: That means its probably wrong */
836 // set the segment registers to mirror ours
837 guest_state->cs.selector = 1<<3;
838 guest_state->cs.attrib.fields.type = 0xa; // Code segment+read
839 guest_state->cs.attrib.fields.S = 1;
840 guest_state->cs.attrib.fields.P = 1;
841 guest_state->cs.attrib.fields.db = 1;
842 guest_state->cs.attrib.fields.G = 1;
843 guest_state->cs.limit = 0xfffff;
844 guest_state->cs.base = 0;
846 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
847 for ( i = 0; segregs[i] != NULL; i++) {
848 struct vmcb_selector * seg = segregs[i];
850 seg->selector = 2<<3;
851 seg->attrib.fields.type = 0x2; // Data Segment+read/write
852 seg->attrib.fields.S = 1;
853 seg->attrib.fields.P = 1;
854 seg->attrib.fields.db = 1;
855 seg->attrib.fields.G = 1;
856 seg->limit = 0xfffff;
862 /* JRL THIS HAS TO GO */
864 // guest_state->tr.selector = GetTR_Selector();
865 guest_state->tr.attrib.fields.type = 0x9;
866 guest_state->tr.attrib.fields.P = 1;
867 // guest_state->tr.limit = GetTR_Limit();
868 //guest_state->tr.base = GetTR_Base();// - 0x2000;
876 guest_state->efer |= EFER_MSR_svm_enable;
877 guest_state->rflags = 0x00000002; // The reserved bit is always 1
878 ctrl_area->svm_instrs.VMRUN = 1;
879 guest_state->cr0 = 0x00000001; // PE
880 ctrl_area->guest_ASID = 1;
883 // guest_state->cpl = 0;
889 ctrl_area->cr_writes.cr4 = 1;
891 ctrl_area->exceptions.de = 1;
892 ctrl_area->exceptions.df = 1;
893 ctrl_area->exceptions.pf = 1;
894 ctrl_area->exceptions.ts = 1;
895 ctrl_area->exceptions.ss = 1;
896 ctrl_area->exceptions.ac = 1;
897 ctrl_area->exceptions.mc = 1;
898 ctrl_area->exceptions.gp = 1;
899 ctrl_area->exceptions.ud = 1;
900 ctrl_area->exceptions.np = 1;
901 ctrl_area->exceptions.of = 1;
902 ctrl_area->exceptions.nmi = 1;
906 ctrl_area->instrs.IOIO_PROT = 1;
907 ctrl_area->IOPM_BASE_PA = (uint_t)V3_AllocPages(3);
911 tmp_reg.r_reg = ctrl_area->IOPM_BASE_PA;
912 memset((void*)(tmp_reg.e_reg.low), 0xffffffff, PAGE_SIZE * 2);
915 ctrl_area->instrs.INTR = 1;
922 memset(gdt_buf, 0, 6);
923 memset(idt_buf, 0, 6);
926 uint_t gdt_base, idt_base;
927 ushort_t gdt_limit, idt_limit;
930 gdt_base = *(ulong_t*)((uchar_t*)gdt_buf + 2) & 0xffffffff;
931 gdt_limit = *(ushort_t*)(gdt_buf) & 0xffff;
932 PrintDebug("GDT: base: %x, limit: %x\n", gdt_base, gdt_limit);
935 idt_base = *(ulong_t*)(idt_buf + 2) & 0xffffffff;
936 idt_limit = *(ushort_t*)(idt_buf) & 0xffff;
937 PrintDebug("IDT: base: %x, limit: %x\n",idt_base, idt_limit);
940 // gdt_base -= 0x2000;
941 //idt_base -= 0x2000;
943 guest_state->gdtr.base = gdt_base;
944 guest_state->gdtr.limit = gdt_limit;
945 guest_state->idtr.base = idt_base;
946 guest_state->idtr.limit = idt_limit;
952 // also determine if CPU supports nested paging
954 if (vm_info.page_tables) {
956 // Flush the TLB on entries/exits
957 ctrl_area->TLB_CONTROL = 1;
959 // Enable Nested Paging
960 ctrl_area->NP_ENABLE = 1;
962 PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
964 // Set the Nested Page Table pointer
965 ctrl_area->N_CR3 |= ((addr_t)vm_info.page_tables & 0xfffff000);
968 // ctrl_area->N_CR3 = Get_CR3();
969 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
971 guest_state->g_pat = 0x7040600070406ULL;
973 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));
974 PrintDebug("Set Guest CR3: lo: 0x%x hi: 0x%x\n", (uint_t)*(&(guest_state->cr3)), (uint_t)*((unsigned char *)&(guest_state->cr3) + 4));
976 // guest_state->cr0 |= 0x80000000;