xref: /netbsd-src/sys/uvm/uvm_init.c (revision 59e0001f2c76f540e0d2715b1f987bb546a238ec)
1 /*	$NetBSD: uvm_init.c,v 1.59 2023/09/23 18:21:12 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * from: Id: uvm_init.c,v 1.1.2.3 1998/02/06 05:15:27 chs Exp
28  */
29 
30 /*
31  * uvm_init.c: init the vm system.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: uvm_init.c,v 1.59 2023/09/23 18:21:12 ad Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/debug.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/resourcevar.h>
43 #include <sys/kmem.h>
44 #include <sys/mman.h>
45 #include <sys/vnode.h>
46 #include <sys/rndsource.h>
47 
48 #include <uvm/uvm.h>
49 #include <uvm/uvm_pdpolicy.h>
50 #include <uvm/uvm_physseg.h>
51 #include <uvm/uvm_readahead.h>
52 #include <uvm/uvm_rndsource.h>
53 
54 /*
55  * struct uvm: we store most global vars in this structure to make them
56  * easier to spot...
57  */
58 
59 struct uvm uvm;		/* decl */
60 struct uvmexp uvmexp;	/* decl */
61 struct uvm_object *uvm_kernel_object;
62 
63 #if defined(__uvmexp_pagesize)
64 const int * const uvmexp_pagesize = &uvmexp.pagesize;
65 const int * const uvmexp_pagemask = &uvmexp.pagemask;
66 const int * const uvmexp_pageshift = &uvmexp.pageshift;
67 #endif
68 
69 kmutex_t uvm_kentry_lock __cacheline_aligned;
70 
71 struct krndsource uvm_fault_rndsource;
72 
73 /*
74  * uvm_md_init: Init dependant on the MD boot context.
75  *		called from MD code.
76  */
77 
78 void
uvm_md_init(void)79 uvm_md_init(void)
80 {
81 	uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
82 	uvm_physseg_init();
83 }
84 
85 /*
86  * uvm_init: init the VM system.   called from kern/init_main.c.
87  */
88 
89 void
uvm_init(void)90 uvm_init(void)
91 {
92 	vaddr_t kvm_start, kvm_end;
93 
94 	/*
95 	 * Ensure that the hardware set the page size, zero the UVM structure.
96 	 */
97 
98 	if (uvmexp.pagesize == 0) {
99 		panic("uvm_init: page size not set");
100 	}
101 
102 	memset(&uvm, 0, sizeof(uvm));
103 	averunnable.fscale = FSCALE;
104 
105 	/*
106 	 * Init the page sub-system.  This includes allocating the vm_page
107 	 * structures, and setting up all the page queues (and locks).
108 	 * Available memory will be put in the "free" queue, kvm_start and
109 	 * kvm_end will be set to the area of kernel virtual memory which
110 	 * is available for general use.
111 	 */
112 
113 	uvm_page_init(&kvm_start, &kvm_end);
114 	uvm_pglistalloc_init();
115 
116 	/*
117 	 * Init the map sub-system.
118 	 */
119 
120 	uvm_map_init();
121 
122 	/*
123 	 * Setup the kernel's virtual memory data structures.  This includes
124 	 * setting up the kernel_map/kernel_object.
125 	 * Bootstrap all kernel memory allocators.
126 	 */
127 
128 	uao_init();
129 	uvm_km_bootstrap(kvm_start, kvm_end);
130 
131 	/*
132 	 * Setup uvm_map caches and init the amap.
133 	 */
134 
135 	uvm_map_init_caches();
136 	uvm_amap_init();
137 
138 	/*
139 	 * Init the pmap module.  The pmap module is free to allocate
140 	 * memory for its private use (e.g. pvlists).
141 	 */
142 
143 	pmap_init();
144 
145 	/*
146 	 * Make kernel memory allocators ready for use.
147 	 * After this call the pool/kmem memory allocators can be used.
148 	 */
149 
150 	uvm_km_init();
151 #ifdef __HAVE_PMAP_PV_TRACK
152 	pmap_pv_init();
153 #endif
154 
155 #ifdef DEBUG
156 	debug_init();
157 #endif
158 
159 	/*
160 	 * Init all pagers and the pager_map.
161 	 */
162 
163 	uvm_pager_init();
164 
165 	/*
166 	 * Initialize the uvm_loan() facility.
167 	 */
168 
169 	uvm_loan_init();
170 
171 	/*
172 	 * Enable paging of kernel objects.
173 	 * This second pass of initializing kernel_object requires rw_obj,
174 	 * so initialize that first.
175 	 */
176 
177 	uao_create(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS,
178 	    UAO_FLAG_KERNSWAP);
179 
180 	uvmpdpol_reinit();
181 
182 	/*
183 	 * Init anonymous memory systems.
184 	 */
185 
186 	uvm_anon_init();
187 
188 	uvm_uarea_init();
189 
190 	/*
191 	 * Init readahead mechanism.
192 	 */
193 
194 	uvm_ra_init();
195 
196 	/*
197 	 * Initialize random source for page fault events.
198 	 */
199 
200 	rnd_attach_source(&uvm_fault_rndsource, "uvmfault", RND_TYPE_VM,
201 	    RND_FLAG_COLLECT_TIME|RND_FLAG_COLLECT_VALUE|
202 	    RND_FLAG_ESTIMATE_VALUE);
203 }
204