2 #include <sys/sysctl.h>
3 #include <sys/vmm_guest_ctl.h>
6 #include <sys/vkernel.h>
8 #include <cpu/cpufunc.h>
9 #include <cpu/specialreg.h>
19 #define vmm_printf(val, err, exp) \
20 printf("vmm_guest(%d): return %d, expected %d\n", val, err, exp);
22 #define STACK_SIZE (512 * PAGE_SIZE)
27 struct vmm_guest_options options;
29 uint64_t stack_source;
41 stack = mmap(NULL, STACK_SIZE,
42 PROT_READ|PROT_WRITE|PROT_EXEC,
45 if (stack == MAP_FAILED) {
46 printf("Error on allocating stack\n");
50 posix_memalign((void **) &ptr, PAGE_SIZE, (512 + 4) * PAGE_SIZE);
51 bzero(ptr, (512 + 4) * PAGE_SIZE);
54 uint64_t *pdp = (uint64_t *)((uint64_t)ptr + PAGE_SIZE);
55 uint64_t *pdp_stack = (uint64_t *)((uint64_t)ptr + 2 * PAGE_SIZE);
56 uint64_t *pd_stack = (uint64_t *)((uint64_t)ptr + 3 * PAGE_SIZE);
57 uint64_t *pd_vec = (uint64_t *)((uint64_t)ptr + 4 * PAGE_SIZE);
59 pml4[0] = (uint64_t) pdp | VPTE_V | VPTE_RW| VPTE_U;
61 do_cpuid(0x80000001, regs);
62 amd_feature = regs[3];
64 if (amd_feature & AMDID_PAGE1GB) {
65 for (i = 0; i < VPTE_PAGE_ENTRIES; i++) {
67 pdp[i] |= VPTE_V | VPTE_RW | VPTE_U;
70 for (i = 0; i < VPTE_PAGE_ENTRIES; i++) {
71 uint64_t *pd = &pd_vec[i * VPTE_PAGE_ENTRIES];
72 pdp[i] = (uint64_t) pd;
73 pdp[i] |= VPTE_V | VPTE_RW | VPTE_U;
74 for (j = 0; j < VPTE_PAGE_ENTRIES; j++) {
75 pd[j] = (i << 30) | (j << 21);
76 pd[j] |= VPTE_V | VPTE_RW | VPTE_U | VPTE_PS;
81 void *stack_addr = NULL;
83 pml4_stack_index = (uint64_t)&stack_addr >> PML4SHIFT;
84 pml4[pml4_stack_index] = (uint64_t) pdp_stack;
85 pml4[pml4_stack_index] |= VPTE_V | VPTE_RW| VPTE_U;
87 pdp_stack_index = ((uint64_t)&stack_addr & PML4MASK) >> PDPSHIFT;
88 pdp_stack[pdp_stack_index] = (uint64_t) pd_stack;
89 pdp_stack[pdp_stack_index] |= VPTE_V | VPTE_RW| VPTE_U;
91 pd_stack_index = ((uint64_t)&stack_addr & PDPMASK) >> PDRSHIFT;
92 pd_stack[pd_stack_index] = (uint64_t) stack;
93 pd_stack[pd_stack_index] |= VPTE_V | VPTE_RW| VPTE_U | VPTE_PS;
95 options.new_stack = (uint64_t)stack + STACK_SIZE;
96 options.guest_cr3 = (register_t) pml4;
98 if(vmm_guest_ctl(VMM_GUEST_RUN, &options)) {
99 printf("Error: VMM enter failed\n");
101 printf("vmm_bootstrap: VMM bootstrap success\n");
109 printf("vmm_test: VMM bootstrap success\n");