create vm map and print nCR3 on boot
[freebsd-src/fkvm-freebsd.git] / sys / kern / kern_fkvm.c
blobfe2ae7f59648b10d3fb5daf19954653f46747480
1 /*-
2 * Copyright (c) 2008 The FreeBSD Project
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <vm/vm.h>
32 #include <vm/pmap.h>
33 #include <vm/vm_extern.h>
34 #include <vm/vm_map.h>
35 #include <vm/vm_object.h>
36 #include <vm/vm_param.h>
37 #include <machine/_inttypes.h>
38 #include <machine/specialreg.h>
40 struct vmspace *sp = NULL;
41 vm_object_t obj = NULL;
43 static u_int64_t
44 fkvm_make_vm_map(void)
46 int rc;
48 sp = vmspace_alloc(0, 0xffffffffffffffff);
49 if (sp == NULL) {
50 printf("vmspace_alloc failed\n");
51 goto fail;
54 obj = vm_object_allocate(OBJT_DEFAULT, 0xffffffffffffffff >> PAGE_SHIFT);
56 vm_object_reference(obj);
57 rc = vm_map_insert(&sp->vm_map,
58 obj,
59 0, 0, 0xffffffffffffffff >> PAGE_SHIFT,
60 VM_PROT_ALL, VM_PROT_ALL,
61 0);
62 if (rc != KERN_SUCCESS) {
63 printf("vm_map_insert failed: %d\n", rc);
64 vm_object_deallocate(obj);
65 goto fail;
68 return vtophys(vmspace_pmap(sp)->pm_pml4);
70 fail:
71 if (obj != NULL) {
72 vm_object_deallocate(obj);
73 obj = NULL;
75 if (sp != NULL) {
76 vmspace_free(sp);
77 sp = NULL;
79 return 0;
83 static void
84 fkvm_load(void *unused)
86 u_int64_t ncr3;
87 u_int64_t efer;
89 printf("fkvm_load\n");
91 /* TODO: check for the presense of extensions */
92 efer = rdmsr(MSR_EFER);
93 printf("EFER = %" PRIx64 "\n", efer);
94 wrmsr(MSR_EFER, efer | EFER_SVME);
95 efer = rdmsr(MSR_EFER);
96 printf("new EFER = %" PRIx64 "\n", efer);
98 ncr3 = fkvm_make_vm_map();
99 printf("ncr3: %" PRIx64 "\n", ncr3);
101 SYSINIT(fkvm, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, fkvm_load, NULL);
103 static void
104 fkvm_unload(void *unused)
106 printf("fkvm_unload\n");
107 /* TODO */
109 SYSUNINIT(fkvm, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, fkvm_unload, NULL);