xref: /openbsd-src/sys/uvm/uvm_init.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: uvm_init.c,v 1.39 2015/03/14 03:38:53 jsg Exp $	*/
2 /*	$NetBSD: uvm_init.c,v 1.14 2000/06/27 17:29:23 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * from: Id: uvm_init.c,v 1.1.2.3 1998/02/06 05:15:27 chs Exp
29  */
30 
31 /*
32  * uvm_init.c: init the vm system.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/filedesc.h>
38 #include <sys/resourcevar.h>
39 #include <sys/mman.h>
40 #include <sys/malloc.h>
41 #include <sys/vnode.h>
42 #include <sys/pool.h>
43 
44 #include <uvm/uvm.h>
45 #include <uvm/uvm_addr.h>
46 
47 /*
48  * struct uvm: we store all global vars in this structure to make them
49  * easier to spot...
50  */
51 
52 struct uvm uvm;		/* decl */
53 struct uvmexp uvmexp;	/* decl */
54 
55 #if defined(VM_MIN_KERNEL_ADDRESS)
56 vaddr_t vm_min_kernel_address = VM_MIN_KERNEL_ADDRESS;
57 #else
58 vaddr_t vm_min_kernel_address;
59 #endif
60 
61 /*
62  * local prototypes
63  */
64 
65 /*
66  * uvm_init: init the VM system.   called from kern/init_main.c.
67  */
68 void
69 uvm_init(void)
70 {
71 	vaddr_t kvm_start, kvm_end;
72 
73 	/* step 0: ensure that the hardware set the page size */
74 	if (uvmexp.pagesize == 0) {
75 		panic("uvm_init: page size not set");
76 	}
77 
78 	/* step 1: set up stats. */
79 	averunnable.fscale = FSCALE;
80 
81 	/*
82 	 * step 2: init the page sub-system.  this includes allocating the
83 	 * vm_page structures, and setting up all the page queues (and
84 	 * locks).  available memory will be put in the "free" queue.
85 	 * kvm_start and kvm_end will be set to the area of kernel virtual
86 	 * memory which is available for general use.
87 	 */
88 	uvm_page_init(&kvm_start, &kvm_end);
89 
90 	/*
91 	 * step 3: init the map sub-system.  allocates the static pool of
92 	 * vm_map_entry structures that are used for "special" kernel maps
93 	 * (e.g. kernel_map, kmem_map, etc...).
94 	 */
95 	uvm_map_init();
96 
97 	/*
98 	 * step 4: setup the kernel's virtual memory data structures.  this
99 	 * includes setting up the kernel_map/kernel_object and the kmem_map/
100 	 * kmem_object.
101 	 */
102 
103 	uvm_km_init(vm_min_kernel_address, kvm_start, kvm_end);
104 
105 	/*
106 	 * step 4.5: init (tune) the fault recovery code.
107 	 */
108 	uvmfault_init();
109 
110 	/*
111 	 * step 5: init the pmap module.   the pmap module is free to allocate
112 	 * memory for its private use (e.g. pvlists).
113 	 */
114 	pmap_init();
115 
116 	/*
117 	 * step 6: init the kernel memory allocator.   after this call the
118 	 * kernel memory allocator (malloc) can be used.
119 	 */
120 	kmeminit();
121 
122 	/*
123 	 * step 6.5: init the dma allocator, which is backed by pools.
124 	 */
125 	dma_alloc_init();
126 
127 	/*
128 	 * step 7: init all pagers and the pager_map.
129 	 */
130 	uvm_pager_init();
131 
132 	/*
133 	 * step 8: init anonymous memory system
134 	 */
135 	amap_init();
136 
137 	/*
138 	 * step 9: init uvm_km_page allocator memory.
139 	 */
140 	uvm_km_page_init();
141 
142 	/*
143 	 * the VM system is now up!  now that malloc is up we can
144 	 * enable paging of kernel objects.
145 	 */
146 	uao_create(VM_KERNEL_SPACE_SIZE, UAO_FLAG_KERNSWAP);
147 
148 	/*
149 	 * reserve some unmapped space for malloc/pool use after free usage
150 	 */
151 #ifdef DEADBEEF0
152 	kvm_start = trunc_page(DEADBEEF0) - PAGE_SIZE;
153 	if (uvm_map(kernel_map, &kvm_start, 3 * PAGE_SIZE,
154 	    NULL, UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(PROT_NONE,
155 	    PROT_NONE, MAP_INHERIT_NONE, MADV_RANDOM, UVM_FLAG_FIXED)))
156 		panic("uvm_init: cannot reserve dead beef @0x%x", DEADBEEF0);
157 #endif
158 #ifdef DEADBEEF1
159 	kvm_start = trunc_page(DEADBEEF1) - PAGE_SIZE;
160 	if (uvm_map(kernel_map, &kvm_start, 3 * PAGE_SIZE,
161 	    NULL, UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(PROT_NONE,
162 	    PROT_NONE, MAP_INHERIT_NONE, MADV_RANDOM, UVM_FLAG_FIXED)))
163 		panic("uvm_init: cannot reserve dead beef @0x%x", DEADBEEF1);
164 #endif
165 	/*
166 	 * init anonymous memory systems
167 	 */
168 	uvm_anon_init();
169 
170 #ifndef SMALL_KERNEL
171 	/*
172 	 * Switch kernel and kmem_map over to a best-fit allocator,
173 	 * instead of walking the tree.
174 	 */
175 	uvm_map_set_uaddr(kernel_map, &kernel_map->uaddr_any[3],
176 	    uaddr_bestfit_create(vm_map_min(kernel_map),
177 	    vm_map_max(kernel_map)));
178 	uvm_map_set_uaddr(kmem_map, &kmem_map->uaddr_any[3],
179 	    uaddr_bestfit_create(vm_map_min(kmem_map),
180 	    vm_map_max(kmem_map)));
181 #endif /* !SMALL_KERNEL */
182 }
183