xref: /openbsd-src/sys/uvm/uvm_init.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: uvm_init.c,v 1.33 2014/07/11 16:35:40 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/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/resourcevar.h>
40 #include <sys/mman.h>
41 #include <sys/proc.h>
42 #include <sys/malloc.h>
43 #include <sys/vnode.h>
44 #include <sys/pool.h>
45 
46 #include <uvm/uvm.h>
47 #include <uvm/uvm_addr.h>
48 
49 /*
50  * struct uvm: we store all global vars in this structure to make them
51  * easier to spot...
52  */
53 
54 struct uvm uvm;		/* decl */
55 struct uvmexp uvmexp;	/* decl */
56 
57 /*
58  * local prototypes
59  */
60 
61 /*
62  * uvm_init: init the VM system.   called from kern/init_main.c.
63  */
64 void
65 uvm_init(void)
66 {
67 	vaddr_t kvm_start, kvm_end;
68 
69 	/* step 0: ensure that the hardware set the page size */
70 	if (uvmexp.pagesize == 0) {
71 		panic("uvm_init: page size not set");
72 	}
73 
74 	/* step 1: set up stats. */
75 	averunnable.fscale = FSCALE;
76 
77 	/*
78 	 * step 2: init the page sub-system.  this includes allocating the
79 	 * vm_page structures, and setting up all the page queues (and
80 	 * locks).  available memory will be put in the "free" queue.
81 	 * kvm_start and kvm_end will be set to the area of kernel virtual
82 	 * memory which is available for general use.
83 	 */
84 	uvm_page_init(&kvm_start, &kvm_end);
85 
86 	/*
87 	 * step 3: init the map sub-system.  allocates the static pool of
88 	 * vm_map_entry structures that are used for "special" kernel maps
89 	 * (e.g. kernel_map, kmem_map, etc...).
90 	 */
91 	uvm_map_init();
92 
93 	/*
94 	 * step 4: setup the kernel's virtual memory data structures.  this
95 	 * includes setting up the kernel_map/kernel_object and the kmem_map/
96 	 * kmem_object.
97 	 */
98 
99 	uvm_km_init(kvm_start, kvm_end);
100 
101 	/*
102 	 * step 4.5: init (tune) the fault recovery code.
103 	 */
104 	uvmfault_init();
105 
106 	/*
107 	 * step 5: init the pmap module.   the pmap module is free to allocate
108 	 * memory for its private use (e.g. pvlists).
109 	 */
110 	pmap_init();
111 
112 	/*
113 	 * step 6: init the kernel memory allocator.   after this call the
114 	 * kernel memory allocator (malloc) can be used.
115 	 */
116 	kmeminit();
117 
118 	/*
119 	 * step 6.5: init the dma allocator, which is backed by pools.
120 	 */
121 	dma_alloc_init();
122 
123 	/*
124 	 * step 7: init all pagers and the pager_map.
125 	 */
126 	uvm_pager_init();
127 
128 	/*
129 	 * step 8: init anonymous memory system
130 	 */
131 	amap_init();
132 
133 	/*
134 	 * step 9: init uvm_km_page allocator memory.
135 	 */
136 	uvm_km_page_init();
137 
138 	/*
139 	 * the VM system is now up!  now that malloc is up we can
140 	 * enable paging of kernel objects.
141 	 */
142 	uao_create(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
143 	    UAO_FLAG_KERNSWAP);
144 
145 	/*
146 	 * reserve some unmapped space for malloc/pool use after free usage
147 	 */
148 #ifdef DEADBEEF0
149 	kvm_start = trunc_page(DEADBEEF0) - PAGE_SIZE;
150 	if (uvm_map(kernel_map, &kvm_start, 3 * PAGE_SIZE,
151 	    NULL, UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(UVM_PROT_NONE,
152 	    UVM_PROT_NONE, UVM_INH_NONE, UVM_ADV_RANDOM, UVM_FLAG_FIXED)))
153 		panic("uvm_init: cannot reserve dead beef @0x%x", DEADBEEF0);
154 #endif
155 #ifdef DEADBEEF1
156 	kvm_start = trunc_page(DEADBEEF1) - PAGE_SIZE;
157 	if (uvm_map(kernel_map, &kvm_start, 3 * PAGE_SIZE,
158 	    NULL, UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(UVM_PROT_NONE,
159 	    UVM_PROT_NONE, UVM_INH_NONE, UVM_ADV_RANDOM, UVM_FLAG_FIXED)))
160 		panic("uvm_init: cannot reserve dead beef @0x%x", DEADBEEF1);
161 #endif
162 	/*
163 	 * init anonymous memory systems
164 	 */
165 	uvm_anon_init();
166 
167 #ifndef SMALL_KERNEL
168 	/*
169 	 * Switch kernel and kmem_map over to a best-fit allocator,
170 	 * instead of walking the tree.
171 	 */
172 	uvm_map_set_uaddr(kernel_map, &kernel_map->uaddr_any[3],
173 	    uaddr_bestfit_create(vm_map_min(kernel_map),
174 	    vm_map_max(kernel_map)));
175 	uvm_map_set_uaddr(kmem_map, &kmem_map->uaddr_any[3],
176 	    uaddr_bestfit_create(vm_map_min(kmem_map),
177 	    vm_map_max(kmem_map)));
178 #endif /* !SMALL_KERNEL */
179 }
180