xref: /f-stack/freebsd/vm/vm_init.c (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 1991, 1993
5a9643ea8Slogwang  *	The Regents of the University of California.  All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * This code is derived from software contributed to Berkeley by
8a9643ea8Slogwang  * The Mach Operating System project at Carnegie-Mellon University.
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
15a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
16a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
17a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
18*22ce4affSfengbojiang  * 3. Neither the name of the University nor the names of its contributors
19a9643ea8Slogwang  *    may be used to endorse or promote products derived from this software
20a9643ea8Slogwang  *    without specific prior written permission.
21a9643ea8Slogwang  *
22a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a9643ea8Slogwang  * SUCH DAMAGE.
33a9643ea8Slogwang  *
34a9643ea8Slogwang  *	from: @(#)vm_init.c	8.1 (Berkeley) 6/11/93
35a9643ea8Slogwang  *
36a9643ea8Slogwang  *
37a9643ea8Slogwang  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38a9643ea8Slogwang  * All rights reserved.
39a9643ea8Slogwang  *
40a9643ea8Slogwang  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41a9643ea8Slogwang  *
42a9643ea8Slogwang  * Permission to use, copy, modify and distribute this software and
43a9643ea8Slogwang  * its documentation is hereby granted, provided that both the copyright
44a9643ea8Slogwang  * notice and this permission notice appear in all copies of the
45a9643ea8Slogwang  * software, derivative works or modified versions, and any portions
46a9643ea8Slogwang  * thereof, and that both notices appear in supporting documentation.
47a9643ea8Slogwang  *
48a9643ea8Slogwang  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49a9643ea8Slogwang  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50a9643ea8Slogwang  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51a9643ea8Slogwang  *
52a9643ea8Slogwang  * Carnegie Mellon requests users of this software to return to
53a9643ea8Slogwang  *
54a9643ea8Slogwang  *  Software Distribution Coordinator  or  [email protected]
55a9643ea8Slogwang  *  School of Computer Science
56a9643ea8Slogwang  *  Carnegie Mellon University
57a9643ea8Slogwang  *  Pittsburgh PA 15213-3890
58a9643ea8Slogwang  *
59a9643ea8Slogwang  * any improvements or extensions that they make and grant Carnegie the
60a9643ea8Slogwang  * rights to redistribute these changes.
61a9643ea8Slogwang  */
62a9643ea8Slogwang 
63a9643ea8Slogwang /*
64a9643ea8Slogwang  *	Initialize the Virtual Memory subsystem.
65a9643ea8Slogwang  */
66a9643ea8Slogwang 
67a9643ea8Slogwang #include <sys/cdefs.h>
68a9643ea8Slogwang __FBSDID("$FreeBSD$");
69a9643ea8Slogwang 
70a9643ea8Slogwang #include <sys/param.h>
71*22ce4affSfengbojiang #include <sys/domainset.h>
72a9643ea8Slogwang #include <sys/kernel.h>
73a9643ea8Slogwang #include <sys/lock.h>
74a9643ea8Slogwang #include <sys/proc.h>
75a9643ea8Slogwang #include <sys/rwlock.h>
76a9643ea8Slogwang #include <sys/malloc.h>
77a9643ea8Slogwang #include <sys/sysctl.h>
78a9643ea8Slogwang #include <sys/systm.h>
79a9643ea8Slogwang #include <sys/selinfo.h>
80a9643ea8Slogwang #include <sys/smp.h>
81a9643ea8Slogwang #include <sys/pipe.h>
82a9643ea8Slogwang #include <sys/bio.h>
83a9643ea8Slogwang #include <sys/buf.h>
84a9643ea8Slogwang #include <sys/vmem.h>
85*22ce4affSfengbojiang #include <sys/vmmeter.h>
86a9643ea8Slogwang 
87a9643ea8Slogwang #include <vm/vm.h>
88a9643ea8Slogwang #include <vm/vm_param.h>
89a9643ea8Slogwang #include <vm/vm_kern.h>
90a9643ea8Slogwang #include <vm/vm_object.h>
91a9643ea8Slogwang #include <vm/vm_page.h>
92*22ce4affSfengbojiang #include <vm/vm_phys.h>
93*22ce4affSfengbojiang #include <vm/vm_pagequeue.h>
94a9643ea8Slogwang #include <vm/vm_map.h>
95a9643ea8Slogwang #include <vm/vm_pager.h>
96a9643ea8Slogwang #include <vm/vm_extern.h>
97a9643ea8Slogwang 
98*22ce4affSfengbojiang extern void	uma_startup1(vm_offset_t);
99a9643ea8Slogwang 
100*22ce4affSfengbojiang long physmem;
101a9643ea8Slogwang 
102a9643ea8Slogwang /*
103a9643ea8Slogwang  * System initialization
104a9643ea8Slogwang  */
105a9643ea8Slogwang static void vm_mem_init(void *);
106a9643ea8Slogwang SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL);
107a9643ea8Slogwang 
108a9643ea8Slogwang /*
109a9643ea8Slogwang  *	vm_init initializes the virtual memory system.
110a9643ea8Slogwang  *	This is done only by the first cpu up.
111a9643ea8Slogwang  */
112a9643ea8Slogwang static void
vm_mem_init(void * dummy)113*22ce4affSfengbojiang vm_mem_init(void *dummy)
114a9643ea8Slogwang {
115a9643ea8Slogwang 
116a9643ea8Slogwang 	/*
117*22ce4affSfengbojiang 	 * Initialize static domainsets, used by various allocators.
118*22ce4affSfengbojiang 	 */
119*22ce4affSfengbojiang 	domainset_init();
120*22ce4affSfengbojiang 
121*22ce4affSfengbojiang 	/*
122*22ce4affSfengbojiang 	 * Initialize resident memory structures.  From here on, all physical
123a9643ea8Slogwang 	 * memory is accounted for, and we use only virtual addresses.
124a9643ea8Slogwang 	 */
125a9643ea8Slogwang 	vm_set_page_size();
126a9643ea8Slogwang 	virtual_avail = vm_page_startup(virtual_avail);
127a9643ea8Slogwang 
128a9643ea8Slogwang 	/*
129*22ce4affSfengbojiang 	 * Set an initial domain policy for thread0 so that allocations
130*22ce4affSfengbojiang 	 * can work.
131*22ce4affSfengbojiang 	 */
132*22ce4affSfengbojiang 	domainset_zero();
133*22ce4affSfengbojiang 
134*22ce4affSfengbojiang 	/* Bootstrap the kernel memory allocator. */
135*22ce4affSfengbojiang 	uma_startup1(virtual_avail);
136*22ce4affSfengbojiang 
137*22ce4affSfengbojiang 	/*
138a9643ea8Slogwang 	 * Initialize other VM packages
139a9643ea8Slogwang 	 */
140a9643ea8Slogwang 	vmem_startup();
141a9643ea8Slogwang 	vm_object_init();
142a9643ea8Slogwang 	vm_map_startup();
143a9643ea8Slogwang 	kmem_init(virtual_avail, virtual_end);
144a9643ea8Slogwang 
145a9643ea8Slogwang 	kmem_init_zero_region();
146a9643ea8Slogwang 	pmap_init();
147a9643ea8Slogwang 	vm_pager_init();
148a9643ea8Slogwang }
149a9643ea8Slogwang 
150a9643ea8Slogwang void
vm_ksubmap_init(struct kva_md_info * kmi)151a9643ea8Slogwang vm_ksubmap_init(struct kva_md_info *kmi)
152a9643ea8Slogwang {
153a9643ea8Slogwang 	vm_offset_t firstaddr;
154a9643ea8Slogwang 	caddr_t v;
155a9643ea8Slogwang 	vm_size_t size = 0;
156a9643ea8Slogwang 	long physmem_est;
157a9643ea8Slogwang 	vm_offset_t minaddr;
158a9643ea8Slogwang 	vm_offset_t maxaddr;
159a9643ea8Slogwang 
160a9643ea8Slogwang 	/*
161a9643ea8Slogwang 	 * Allocate space for system data structures.
162a9643ea8Slogwang 	 * The first available kernel virtual address is in "v".
163a9643ea8Slogwang 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
164a9643ea8Slogwang 	 * As pages of memory are allocated and cleared,
165a9643ea8Slogwang 	 * "firstaddr" is incremented.
166a9643ea8Slogwang 	 */
167a9643ea8Slogwang 
168a9643ea8Slogwang 	/*
169a9643ea8Slogwang 	 * Make two passes.  The first pass calculates how much memory is
170a9643ea8Slogwang 	 * needed and allocates it.  The second pass assigns virtual
171a9643ea8Slogwang 	 * addresses to the various data structures.
172a9643ea8Slogwang 	 */
173a9643ea8Slogwang 	firstaddr = 0;
174a9643ea8Slogwang again:
175a9643ea8Slogwang 	v = (caddr_t)firstaddr;
176a9643ea8Slogwang 
177a9643ea8Slogwang 	/*
178a9643ea8Slogwang 	 * Discount the physical memory larger than the size of kernel_map
179a9643ea8Slogwang 	 * to avoid eating up all of KVA space.
180a9643ea8Slogwang 	 */
181*22ce4affSfengbojiang 	physmem_est = lmin(physmem, btoc(vm_map_max(kernel_map) -
182*22ce4affSfengbojiang 	    vm_map_min(kernel_map)));
183a9643ea8Slogwang 
184a9643ea8Slogwang 	v = kern_vfs_bio_buffer_alloc(v, physmem_est);
185a9643ea8Slogwang 
186a9643ea8Slogwang 	/*
187a9643ea8Slogwang 	 * End of first pass, size has been calculated so allocate memory
188a9643ea8Slogwang 	 */
189a9643ea8Slogwang 	if (firstaddr == 0) {
190a9643ea8Slogwang 		size = (vm_size_t)v;
191*22ce4affSfengbojiang #ifdef VM_FREELIST_DMA32
192*22ce4affSfengbojiang 		/*
193*22ce4affSfengbojiang 		 * Try to protect 32-bit DMAable memory from the largest
194*22ce4affSfengbojiang 		 * early alloc of wired mem.
195*22ce4affSfengbojiang 		 */
196*22ce4affSfengbojiang 		firstaddr = kmem_alloc_attr(size, M_ZERO | M_NOWAIT,
197*22ce4affSfengbojiang 		    (vm_paddr_t)1 << 32, ~(vm_paddr_t)0, VM_MEMATTR_DEFAULT);
198*22ce4affSfengbojiang 		if (firstaddr == 0)
199*22ce4affSfengbojiang #endif
200*22ce4affSfengbojiang 			firstaddr = kmem_malloc(size, M_ZERO | M_WAITOK);
201a9643ea8Slogwang 		if (firstaddr == 0)
202a9643ea8Slogwang 			panic("startup: no room for tables");
203a9643ea8Slogwang 		goto again;
204a9643ea8Slogwang 	}
205a9643ea8Slogwang 
206a9643ea8Slogwang 	/*
207a9643ea8Slogwang 	 * End of second pass, addresses have been assigned
208a9643ea8Slogwang 	 */
209a9643ea8Slogwang 	if ((vm_size_t)((char *)v - firstaddr) != size)
210a9643ea8Slogwang 		panic("startup: table size inconsistency");
211a9643ea8Slogwang 
212a9643ea8Slogwang 	/*
213*22ce4affSfengbojiang 	 * Allocate the clean map to hold all of I/O virtual memory.
214a9643ea8Slogwang 	 */
215*22ce4affSfengbojiang 	size = (long)nbuf * BKVASIZE + (long)bio_transient_maxcnt * maxphys;
216a9643ea8Slogwang 	kmi->clean_sva = firstaddr = kva_alloc(size);
217a9643ea8Slogwang 	kmi->clean_eva = firstaddr + size;
218a9643ea8Slogwang 
219a9643ea8Slogwang 	/*
220a9643ea8Slogwang 	 * Allocate the buffer arena.
221a9643ea8Slogwang 	 *
222a9643ea8Slogwang 	 * Enable the quantum cache if we have more than 4 cpus.  This
223a9643ea8Slogwang 	 * avoids lock contention at the expense of some fragmentation.
224a9643ea8Slogwang 	 */
225a9643ea8Slogwang 	size = (long)nbuf * BKVASIZE;
226a9643ea8Slogwang 	kmi->buffer_sva = firstaddr;
227a9643ea8Slogwang 	kmi->buffer_eva = kmi->buffer_sva + size;
228a9643ea8Slogwang 	vmem_init(buffer_arena, "buffer arena", kmi->buffer_sva, size,
229a9643ea8Slogwang 	    PAGE_SIZE, (mp_ncpus > 4) ? BKVASIZE * 8 : 0, 0);
230a9643ea8Slogwang 	firstaddr += size;
231a9643ea8Slogwang 
232a9643ea8Slogwang 	/*
233a9643ea8Slogwang 	 * And optionally transient bio space.
234a9643ea8Slogwang 	 */
235a9643ea8Slogwang 	if (bio_transient_maxcnt != 0) {
236*22ce4affSfengbojiang 		size = (long)bio_transient_maxcnt * maxphys;
237a9643ea8Slogwang 		vmem_init(transient_arena, "transient arena",
238a9643ea8Slogwang 		    firstaddr, size, PAGE_SIZE, 0, 0);
239a9643ea8Slogwang 		firstaddr += size;
240a9643ea8Slogwang 	}
241a9643ea8Slogwang 	if (firstaddr != kmi->clean_eva)
242a9643ea8Slogwang 		panic("Clean map calculation incorrect");
243a9643ea8Slogwang 
244a9643ea8Slogwang 	/*
245*22ce4affSfengbojiang 	 * Allocate the pageable submaps.  We may cache an exec map entry per
246*22ce4affSfengbojiang 	 * CPU, so we therefore need to reserve space for at least ncpu+1
247*22ce4affSfengbojiang 	 * entries to avoid deadlock.  The exec map is also used by some image
248*22ce4affSfengbojiang 	 * activators, so we leave a fixed number of pages for their use.
249a9643ea8Slogwang 	 */
250*22ce4affSfengbojiang #ifdef __LP64__
251*22ce4affSfengbojiang 	exec_map_entries = 8 * mp_ncpus;
252*22ce4affSfengbojiang #else
253*22ce4affSfengbojiang 	exec_map_entries = 2 * mp_ncpus + 4;
254*22ce4affSfengbojiang #endif
255*22ce4affSfengbojiang 	exec_map_entry_size = round_page(PATH_MAX + ARG_MAX);
256*22ce4affSfengbojiang 	kmem_subinit(exec_map, kernel_map, &minaddr, &maxaddr,
257*22ce4affSfengbojiang 	    exec_map_entries * exec_map_entry_size + 64 * PAGE_SIZE, false);
258*22ce4affSfengbojiang 	kmem_subinit(pipe_map, kernel_map, &minaddr, &maxaddr, maxpipekva,
259*22ce4affSfengbojiang 	    false);
260a9643ea8Slogwang }
261