xref: /freebsd/sys/vm/vm_init.c (revision fa290859fa63d65b5da3014038ae289c1b336700)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Initialize the Virtual Memory subsystem.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <sys/param.h>
6687ab1a10SMark Johnston #include <sys/domainset.h>
672b14f991SJulian Elischer #include <sys/kernel.h>
68fb919e4dSMark Murray #include <sys/lock.h>
69fb919e4dSMark Murray #include <sys/proc.h>
7089f6b863SAttilio Rao #include <sys/rwlock.h>
715df87b21SJeff Roberson #include <sys/malloc.h>
72f5fca0d8SKris Kennaway #include <sys/sysctl.h>
7305f0fdd2SPoul-Henning Kamp #include <sys/systm.h>
74cebde069SMike Silbersack #include <sys/selinfo.h>
7521fae961SJeff Roberson #include <sys/smp.h>
76cebde069SMike Silbersack #include <sys/pipe.h>
77219d632cSMatthew Dillon #include <sys/bio.h>
78219d632cSMatthew Dillon #include <sys/buf.h>
795f518366SJeff Roberson #include <sys/vmem.h>
807a469c8eSJeff Roberson #include <sys/vmmeter.h>
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83219d632cSMatthew Dillon #include <vm/vm_param.h>
84219d632cSMatthew Dillon #include <vm/vm_kern.h>
85efeaf95aSDavid Greenman #include <vm/vm_object.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
877a469c8eSJeff Roberson #include <vm/vm_phys.h>
88e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
9024a1cce3SDavid Greenman #include <vm/vm_pager.h>
91efeaf95aSDavid Greenman #include <vm/vm_extern.h>
92df8bae1dSRodney W. Grimes 
93a81c400eSJeff Roberson extern void	uma_startup1(vm_offset_t);
947a469c8eSJeff Roberson 
950fca57b8SThomas Moestl long physmem;
960fca57b8SThomas Moestl 
97df8bae1dSRodney W. Grimes /*
982b14f991SJulian Elischer  * System initialization
992b14f991SJulian Elischer  */
10011caded3SAlfred Perlstein static void vm_mem_init(void *);
101237fdd78SRobert Watson SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL);
1022b14f991SJulian Elischer 
103*fa290859SAlan Cox #ifdef INVARIANTS
104*fa290859SAlan Cox /*
105*fa290859SAlan Cox  * Ensure that pmap_init() correctly initialized pagesizes[].
106*fa290859SAlan Cox  */
107*fa290859SAlan Cox static void
vm_check_pagesizes(void)108*fa290859SAlan Cox vm_check_pagesizes(void)
109*fa290859SAlan Cox {
110*fa290859SAlan Cox 	int i;
111*fa290859SAlan Cox 
112*fa290859SAlan Cox 	KASSERT(pagesizes[0] == PAGE_SIZE, ("pagesizes[0] != PAGE_SIZE"));
113*fa290859SAlan Cox 	for (i = 1; i < MAXPAGESIZES; i++) {
114*fa290859SAlan Cox 		KASSERT((pagesizes[i - 1] != 0 &&
115*fa290859SAlan Cox 		    pagesizes[i - 1] < pagesizes[i]) || pagesizes[i] == 0,
116*fa290859SAlan Cox 		    ("pagesizes[%d ... %d] are misconfigured", i - 1, i));
117*fa290859SAlan Cox 	}
118*fa290859SAlan Cox }
119*fa290859SAlan Cox #endif
120*fa290859SAlan Cox 
1212b14f991SJulian Elischer /*
122deab5717SMitchell Horne  *	vm_mem_init() initializes the virtual memory system.
123df8bae1dSRodney W. Grimes  *	This is done only by the first cpu up.
124df8bae1dSRodney W. Grimes  */
1252b14f991SJulian Elischer static void
vm_mem_init(void * dummy)1261aed6d48SMark Johnston vm_mem_init(void *dummy)
127df8bae1dSRodney W. Grimes {
1285df87b21SJeff Roberson 
129df8bae1dSRodney W. Grimes 	/*
13087ab1a10SMark Johnston 	 * Initialize static domainsets, used by various allocators.
13187ab1a10SMark Johnston 	 */
13287ab1a10SMark Johnston 	domainset_init();
13387ab1a10SMark Johnston 
13487ab1a10SMark Johnston 	/*
135920239efSMark Johnston 	 * Initialize resident memory structures.  From here on, all physical
1360d94caffSDavid Greenman 	 * memory is accounted for, and we use only virtual addresses.
137df8bae1dSRodney W. Grimes 	 */
13826f9a767SRodney W. Grimes 	vm_set_page_size();
139889eb0fcSAlan Cox 	virtual_avail = vm_page_startup(virtual_avail);
140e7841165SDag-Erling Smørgrav 
141920239efSMark Johnston 	/*
142920239efSMark Johnston 	 * Set an initial domain policy for thread0 so that allocations
143920239efSMark Johnston 	 * can work.
144920239efSMark Johnston 	 */
145920239efSMark Johnston 	domainset_zero();
146920239efSMark Johnston 
147a81c400eSJeff Roberson 	/* Bootstrap the kernel memory allocator. */
148a81c400eSJeff Roberson 	uma_startup1(virtual_avail);
149a81c400eSJeff Roberson 
150df8bae1dSRodney W. Grimes 	/*
151df8bae1dSRodney W. Grimes 	 * Initialize other VM packages
152df8bae1dSRodney W. Grimes 	 */
1535f518366SJeff Roberson 	vmem_startup();
154a316d390SJohn Dyson 	vm_object_init();
155df8bae1dSRodney W. Grimes 	vm_map_startup();
156df8bae1dSRodney W. Grimes 	kmem_init(virtual_avail, virtual_end);
1575df87b21SJeff Roberson 
1585df87b21SJeff Roberson 	kmem_init_zero_region();
159bdb93eb2SAlan Cox 	pmap_init();
160df8bae1dSRodney W. Grimes 	vm_pager_init();
161*fa290859SAlan Cox 
162*fa290859SAlan Cox #ifdef INVARIANTS
163*fa290859SAlan Cox 	vm_check_pagesizes();
164*fa290859SAlan Cox #endif
165df8bae1dSRodney W. Grimes }
166219d632cSMatthew Dillon 
167219d632cSMatthew Dillon void
vm_ksubmap_init(struct kva_md_info * kmi)168219d632cSMatthew Dillon vm_ksubmap_init(struct kva_md_info *kmi)
169219d632cSMatthew Dillon {
170f49fd63aSJohn Baldwin 	caddr_t firstaddr, v;
171219d632cSMatthew Dillon 	vm_size_t size = 0;
172447b3772SPeter Wemm 	long physmem_est;
173219d632cSMatthew Dillon 	vm_offset_t minaddr;
174219d632cSMatthew Dillon 	vm_offset_t maxaddr;
175219d632cSMatthew Dillon 
17645cc8519SColin Percival 	TSENTER();
177219d632cSMatthew Dillon 	/*
178219d632cSMatthew Dillon 	 * Allocate space for system data structures.
179219d632cSMatthew Dillon 	 * The first available kernel virtual address is in "v".
180219d632cSMatthew Dillon 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
181219d632cSMatthew Dillon 	 * As pages of memory are allocated and cleared,
182219d632cSMatthew Dillon 	 * "firstaddr" is incremented.
183219d632cSMatthew Dillon 	 */
184219d632cSMatthew Dillon 
185219d632cSMatthew Dillon 	/*
186219d632cSMatthew Dillon 	 * Make two passes.  The first pass calculates how much memory is
187219d632cSMatthew Dillon 	 * needed and allocates it.  The second pass assigns virtual
188219d632cSMatthew Dillon 	 * addresses to the various data structures.
189219d632cSMatthew Dillon 	 */
190f49fd63aSJohn Baldwin 	firstaddr = NULL;
191219d632cSMatthew Dillon again:
192f49fd63aSJohn Baldwin 	v = firstaddr;
193219d632cSMatthew Dillon 
194219d632cSMatthew Dillon 	/*
195219d632cSMatthew Dillon 	 * Discount the physical memory larger than the size of kernel_map
196219d632cSMatthew Dillon 	 * to avoid eating up all of KVA space.
197219d632cSMatthew Dillon 	 */
198f0165b1cSKonstantin Belousov 	physmem_est = lmin(physmem, btoc(vm_map_max(kernel_map) -
199f0165b1cSKonstantin Belousov 	    vm_map_min(kernel_map)));
200219d632cSMatthew Dillon 
201219d632cSMatthew Dillon 	v = kern_vfs_bio_buffer_alloc(v, physmem_est);
202219d632cSMatthew Dillon 
203219d632cSMatthew Dillon 	/*
204219d632cSMatthew Dillon 	 * End of first pass, size has been calculated so allocate memory
205219d632cSMatthew Dillon 	 */
206f49fd63aSJohn Baldwin 	if (firstaddr == NULL) {
207857961d9SRobert Drehmel 		size = (vm_size_t)v;
208edb2994aSAndrew Gallatin #ifdef VM_FREELIST_DMA32
209edb2994aSAndrew Gallatin 		/*
210edb2994aSAndrew Gallatin 		 * Try to protect 32-bit DMAable memory from the largest
211edb2994aSAndrew Gallatin 		 * early alloc of wired mem.
212edb2994aSAndrew Gallatin 		 */
213db7c2a48SAlan Cox 		firstaddr = kmem_alloc_attr(size, M_ZERO | M_NOWAIT,
214db7c2a48SAlan Cox 		    (vm_paddr_t)1 << 32, ~(vm_paddr_t)0, VM_MEMATTR_DEFAULT);
215f49fd63aSJohn Baldwin 		if (firstaddr == NULL)
216edb2994aSAndrew Gallatin #endif
21783a90bffSAlan Cox 			firstaddr = kmem_malloc(size, M_ZERO | M_WAITOK);
218f49fd63aSJohn Baldwin 		if (firstaddr == NULL)
219219d632cSMatthew Dillon 			panic("startup: no room for tables");
220219d632cSMatthew Dillon 		goto again;
221219d632cSMatthew Dillon 	}
222219d632cSMatthew Dillon 
223219d632cSMatthew Dillon 	/*
224219d632cSMatthew Dillon 	 * End of second pass, addresses have been assigned
225219d632cSMatthew Dillon 	 */
226f49fd63aSJohn Baldwin 	if ((vm_size_t)(v - firstaddr) != size)
227219d632cSMatthew Dillon 		panic("startup: table size inconsistency");
228219d632cSMatthew Dillon 
2295df87b21SJeff Roberson 	/*
23046b0292aSGleb Smirnoff 	 * Allocate the clean map to hold all of I/O virtual memory.
2315df87b21SJeff Roberson 	 */
232cd853791SKonstantin Belousov 	size = (long)nbuf * BKVASIZE + (long)bio_transient_maxcnt * maxphys;
233f49fd63aSJohn Baldwin 	kmi->clean_sva = kva_alloc(size);
234f49fd63aSJohn Baldwin 	kmi->clean_eva = kmi->clean_sva + size;
2355f518366SJeff Roberson 
2365df87b21SJeff Roberson 	/*
2375df87b21SJeff Roberson 	 * Allocate the buffer arena.
23821fae961SJeff Roberson 	 *
23921fae961SJeff Roberson 	 * Enable the quantum cache if we have more than 4 cpus.  This
24021fae961SJeff Roberson 	 * avoids lock contention at the expense of some fragmentation.
2415df87b21SJeff Roberson 	 */
2425f518366SJeff Roberson 	size = (long)nbuf * BKVASIZE;
243f49fd63aSJohn Baldwin 	kmi->buffer_sva = kmi->clean_sva;
2445f518366SJeff Roberson 	kmi->buffer_eva = kmi->buffer_sva + size;
2455f518366SJeff Roberson 	vmem_init(buffer_arena, "buffer arena", kmi->buffer_sva, size,
246cfbb5f8cSEric van Gyzen 	    PAGE_SIZE, (mp_ncpus > 4) ? BKVASIZE * 8 : 0, M_WAITOK);
2475f518366SJeff Roberson 
2485df87b21SJeff Roberson 	/*
2495df87b21SJeff Roberson 	 * And optionally transient bio space.
2505df87b21SJeff Roberson 	 */
2517db07e1cSKonstantin Belousov 	if (bio_transient_maxcnt != 0) {
252cd853791SKonstantin Belousov 		size = (long)bio_transient_maxcnt * maxphys;
2535f518366SJeff Roberson 		vmem_init(transient_arena, "transient arena",
254f49fd63aSJohn Baldwin 		    kmi->buffer_eva, size, PAGE_SIZE, 0, M_WAITOK);
2557db07e1cSKonstantin Belousov 	}
2565df87b21SJeff Roberson 
2575df87b21SJeff Roberson 	/*
258ec492b13SMark Johnston 	 * Allocate the pageable submaps.  We may cache an exec map entry per
259ec492b13SMark Johnston 	 * CPU, so we therefore need to reserve space for at least ncpu+1
260ec492b13SMark Johnston 	 * entries to avoid deadlock.  The exec map is also used by some image
261ec492b13SMark Johnston 	 * activators, so we leave a fixed number of pages for their use.
2625df87b21SJeff Roberson 	 */
263ec492b13SMark Johnston #ifdef __LP64__
264ec492b13SMark Johnston 	exec_map_entries = 8 * mp_ncpus;
265ec492b13SMark Johnston #else
2668d65cba2SMark Johnston 	exec_map_entries = 2 * mp_ncpus + 4;
267ec492b13SMark Johnston #endif
268ec492b13SMark Johnston 	exec_map_entry_size = round_page(PATH_MAX + ARG_MAX);
2697dd979dfSMark Johnston 	kmem_subinit(exec_map, kernel_map, &minaddr, &maxaddr,
2707dd979dfSMark Johnston 	    exec_map_entries * exec_map_entry_size + 64 * PAGE_SIZE, false);
2717dd979dfSMark Johnston 	kmem_subinit(pipe_map, kernel_map, &minaddr, &maxaddr, maxpipekva,
2727dd979dfSMark Johnston 	    false);
27345cc8519SColin Percival 	TSEXIT();
274219d632cSMatthew Dillon }
275