1/*	$OpenBSD: uvm_init.c,v 1.42 2021/03/20 10:24:21 mpi 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/percpu.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
53struct uvm uvm;		/* decl */
54struct uvmexp uvmexp;	/* decl */
55
56COUNTERS_BOOT_MEMORY(uvmexp_countersboot, exp_ncounters);
57struct cpumem *uvmexp_counters = COUNTERS_BOOT_INITIALIZER(uvmexp_countersboot);
58
59#if defined(VM_MIN_KERNEL_ADDRESS)
60vaddr_t vm_min_kernel_address = VM_MIN_KERNEL_ADDRESS;
61#else
62vaddr_t vm_min_kernel_address;
63#endif
64
65/*
66 * local prototypes
67 */
68
69/*
70 * uvm_init: init the VM system.   called from kern/init_main.c.
71 */
72
73void
74uvm_init(void)
75{
76	vaddr_t kvm_start, kvm_end;
77
78	/*
79	 * Ensure that the hardware set the page size.
80	 */
81	if (uvmexp.pagesize == 0) {
82		panic("uvm_init: page size not set");
83	}
84	averunnable.fscale = FSCALE;
85
86	/*
87	 * Init the page sub-system.  This includes allocating the vm_page
88	 * structures, and setting up all the page queues (and locks).
89	 * Available memory will be put in the "free" queue, kvm_start and
90	 * kvm_end will be set to the area of kernel virtual memory which
91	 * is available for general use.
92	 */
93	uvm_page_init(&kvm_start, &kvm_end);
94
95	/*
96	 * Init the map sub-system.
97	 *
98	 * Allocates the static pool of vm_map_entry structures that are
99	 * used for "special" kernel maps (e.g. kernel_map, kmem_map, etc...).
100	 */
101	uvm_map_init();
102
103	/*
104	 * Setup the kernel's virtual memory data structures.  This includes
105	 * setting up the kernel_map/kernel_object.
106	 */
107	uvm_km_init(vm_min_kernel_address, kvm_start, kvm_end);
108
109	/*
110	 * step 4.5: init (tune) the fault recovery code.
111	 */
112	uvmfault_init();
113
114	/*
115	 * Init the pmap module.  The pmap module is free to allocate
116	 * memory for its private use (e.g. pvlists).
117	 */
118	pmap_init();
119
120	/*
121	 * step 6: init uvm_km_page allocator memory.
122	 */
123	uvm_km_page_init();
124
125	/*
126	 * Make kernel memory allocators ready for use.
127	 * After this call the malloc memory allocator can be used.
128	 */
129	kmeminit();
130
131	/*
132	 * step 7.5: init the dma allocator, which is backed by pools.
133	 */
134	dma_alloc_init();
135
136	/*
137	 * Init all pagers and the pager_map.
138	 */
139	uvm_pager_init();
140
141	/*
142	 * step 9: init anonymous memory system
143	 */
144	amap_init();
145
146	/*
147	 * step 10: start uvm_km_page allocator thread.
148	 */
149	uvm_km_page_lateinit();
150
151	/*
152	 * the VM system is now up!  now that malloc is up we can
153	 * enable paging of kernel objects.
154	 */
155	uao_create(VM_KERNEL_SPACE_SIZE, UAO_FLAG_KERNSWAP);
156
157	/*
158	 * reserve some unmapped space for malloc/pool use after free usage
159	 */
160#ifdef DEADBEEF0
161	kvm_start = trunc_page(DEADBEEF0) - PAGE_SIZE;
162	if (uvm_map(kernel_map, &kvm_start, 3 * PAGE_SIZE,
163	    NULL, UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(PROT_NONE,
164	    PROT_NONE, MAP_INHERIT_NONE, MADV_RANDOM, UVM_FLAG_FIXED)))
165		panic("uvm_init: cannot reserve dead beef @0x%x", DEADBEEF0);
166#endif
167#ifdef DEADBEEF1
168	kvm_start = trunc_page(DEADBEEF1) - PAGE_SIZE;
169	if (uvm_map(kernel_map, &kvm_start, 3 * PAGE_SIZE,
170	    NULL, UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(PROT_NONE,
171	    PROT_NONE, MAP_INHERIT_NONE, MADV_RANDOM, UVM_FLAG_FIXED)))
172		panic("uvm_init: cannot reserve dead beef @0x%x", DEADBEEF1);
173#endif
174	/*
175	 * Init anonymous memory systems.
176	 */
177	uvm_anon_init();
178
179#ifndef SMALL_KERNEL
180	/*
181	 * Switch kernel and kmem_map over to a best-fit allocator,
182	 * instead of walking the tree.
183	 */
184	uvm_map_set_uaddr(kernel_map, &kernel_map->uaddr_any[3],
185	    uaddr_bestfit_create(vm_map_min(kernel_map),
186	    vm_map_max(kernel_map)));
187	uvm_map_set_uaddr(kmem_map, &kmem_map->uaddr_any[3],
188	    uaddr_bestfit_create(vm_map_min(kmem_map),
189	    vm_map_max(kmem_map)));
190#endif /* !SMALL_KERNEL */
191}
192
193void
194uvm_init_percpu(void)
195{
196	uvmexp_counters = counters_alloc_ncpus(uvmexp_counters, exp_ncounters);
197}
198