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