xref: /freebsd/sys/vm/vm_init.c (revision edb572a38c433b49f2a06fe12bed3f8156e88cfa)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_init.c	8.1 (Berkeley) 6/11/93
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 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
692b14f991SJulian Elischer #include <sys/kernel.h>
70fb919e4dSMark Murray #include <sys/lock.h>
71fb919e4dSMark Murray #include <sys/proc.h>
7289f6b863SAttilio Rao #include <sys/rwlock.h>
735df87b21SJeff Roberson #include <sys/malloc.h>
74f5fca0d8SKris Kennaway #include <sys/sysctl.h>
7505f0fdd2SPoul-Henning Kamp #include <sys/systm.h>
76cebde069SMike Silbersack #include <sys/selinfo.h>
77cebde069SMike Silbersack #include <sys/pipe.h>
78219d632cSMatthew Dillon #include <sys/bio.h>
79219d632cSMatthew Dillon #include <sys/buf.h>
805f518366SJeff Roberson #include <sys/vmem.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>
87efeaf95aSDavid Greenman #include <vm/vm_map.h>
8824a1cce3SDavid Greenman #include <vm/vm_pager.h>
89efeaf95aSDavid Greenman #include <vm/vm_extern.h>
90df8bae1dSRodney W. Grimes 
910fca57b8SThomas Moestl long physmem;
920fca57b8SThomas Moestl 
93f5fca0d8SKris Kennaway static int exec_map_entries = 16;
94f5fca0d8SKris Kennaway TUNABLE_INT("vm.exec_map_entries", &exec_map_entries);
95f5fca0d8SKris Kennaway SYSCTL_INT(_vm, OID_AUTO, exec_map_entries, CTLFLAG_RD, &exec_map_entries, 0,
96f5fca0d8SKris Kennaway     "Maximum number of simultaneous execs");
97f5fca0d8SKris Kennaway 
98df8bae1dSRodney W. Grimes /*
992b14f991SJulian Elischer  * System initialization
1002b14f991SJulian Elischer  */
10111caded3SAlfred Perlstein static void vm_mem_init(void *);
102237fdd78SRobert Watson SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL);
1032b14f991SJulian Elischer 
1042b14f991SJulian Elischer /*
1055df87b21SJeff Roberson  * Import kva into the kernel arena.
1065df87b21SJeff Roberson  */
1075df87b21SJeff Roberson static int
1085df87b21SJeff Roberson kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
1095df87b21SJeff Roberson {
1105df87b21SJeff Roberson 	vm_offset_t addr;
1115df87b21SJeff Roberson 	int result;
1125df87b21SJeff Roberson 
1135df87b21SJeff Roberson 	addr = vm_map_min(kernel_map);
114*edb572a3SJohn Baldwin 	result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
1155aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
1165df87b21SJeff Roberson 	if (result != KERN_SUCCESS)
1175df87b21SJeff Roberson                 return (ENOMEM);
1185df87b21SJeff Roberson 
1195df87b21SJeff Roberson 	*addrp = addr;
1205df87b21SJeff Roberson 
1215df87b21SJeff Roberson 	return (0);
1225df87b21SJeff Roberson }
1235df87b21SJeff Roberson 
1245df87b21SJeff Roberson /*
125df8bae1dSRodney W. Grimes  *	vm_init initializes the virtual memory system.
126df8bae1dSRodney W. Grimes  *	This is done only by the first cpu up.
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	The start and end address of physical memory is passed in.
129df8bae1dSRodney W. Grimes  */
1302b14f991SJulian Elischer /* ARGSUSED*/
1312b14f991SJulian Elischer static void
132d841aaa7SBruce Evans vm_mem_init(dummy)
133d841aaa7SBruce Evans 	void *dummy;
134df8bae1dSRodney W. Grimes {
1355df87b21SJeff Roberson 
136df8bae1dSRodney W. Grimes 	/*
1370d94caffSDavid Greenman 	 * Initializes resident memory structures. From here on, all physical
1380d94caffSDavid Greenman 	 * memory is accounted for, and we use only virtual addresses.
139df8bae1dSRodney W. Grimes 	 */
14026f9a767SRodney W. Grimes 	vm_set_page_size();
141889eb0fcSAlan Cox 	virtual_avail = vm_page_startup(virtual_avail);
142e7841165SDag-Erling Smørgrav 
143df8bae1dSRodney W. Grimes 	/*
144df8bae1dSRodney W. Grimes 	 * Initialize other VM packages
145df8bae1dSRodney W. Grimes 	 */
1465f518366SJeff Roberson 	vmem_startup();
147a316d390SJohn Dyson 	vm_object_init();
148df8bae1dSRodney W. Grimes 	vm_map_startup();
149df8bae1dSRodney W. Grimes 	kmem_init(virtual_avail, virtual_end);
1505df87b21SJeff Roberson 
1515df87b21SJeff Roberson 	/*
1525df87b21SJeff Roberson 	 * Initialize the kernel_arena.  This can grow on demand.
1535df87b21SJeff Roberson 	 */
1545df87b21SJeff Roberson 	vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
1555df87b21SJeff Roberson 	vmem_set_import(kernel_arena, kva_import, NULL, NULL,
1565df87b21SJeff Roberson #if VM_NRESERVLEVEL > 0
1575df87b21SJeff Roberson 	    1 << (VM_LEVEL_0_ORDER + PAGE_SHIFT));
1585df87b21SJeff Roberson #else
159d91722fbSJeff Roberson 	    /* On non-superpage architectures want large import sizes. */
160d91722fbSJeff Roberson 	    PAGE_SIZE * 1024);
1615df87b21SJeff Roberson #endif
1625df87b21SJeff Roberson 
1635df87b21SJeff Roberson 	kmem_init_zero_region();
164bdb93eb2SAlan Cox 	pmap_init();
165df8bae1dSRodney W. Grimes 	vm_pager_init();
166df8bae1dSRodney W. Grimes }
167219d632cSMatthew Dillon 
168219d632cSMatthew Dillon void
169219d632cSMatthew Dillon vm_ksubmap_init(struct kva_md_info *kmi)
170219d632cSMatthew Dillon {
171219d632cSMatthew Dillon 	vm_offset_t firstaddr;
172219d632cSMatthew Dillon 	caddr_t v;
173219d632cSMatthew Dillon 	vm_size_t size = 0;
174447b3772SPeter Wemm 	long physmem_est;
175219d632cSMatthew Dillon 	vm_offset_t minaddr;
176219d632cSMatthew Dillon 	vm_offset_t maxaddr;
177219d632cSMatthew Dillon 
178219d632cSMatthew Dillon 	/*
179219d632cSMatthew Dillon 	 * Allocate space for system data structures.
180219d632cSMatthew Dillon 	 * The first available kernel virtual address is in "v".
181219d632cSMatthew Dillon 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
182219d632cSMatthew Dillon 	 * As pages of memory are allocated and cleared,
183219d632cSMatthew Dillon 	 * "firstaddr" is incremented.
184219d632cSMatthew Dillon 	 */
185219d632cSMatthew Dillon 
186219d632cSMatthew Dillon 	/*
187219d632cSMatthew Dillon 	 * Make two passes.  The first pass calculates how much memory is
188219d632cSMatthew Dillon 	 * needed and allocates it.  The second pass assigns virtual
189219d632cSMatthew Dillon 	 * addresses to the various data structures.
190219d632cSMatthew Dillon 	 */
191219d632cSMatthew Dillon 	firstaddr = 0;
192219d632cSMatthew Dillon again:
193219d632cSMatthew Dillon 	v = (caddr_t)firstaddr;
194219d632cSMatthew Dillon 
195219d632cSMatthew Dillon 	/*
196219d632cSMatthew Dillon 	 * Discount the physical memory larger than the size of kernel_map
197219d632cSMatthew Dillon 	 * to avoid eating up all of KVA space.
198219d632cSMatthew Dillon 	 */
199447b3772SPeter Wemm 	physmem_est = lmin(physmem, btoc(kernel_map->max_offset -
200219d632cSMatthew Dillon 	    kernel_map->min_offset));
201219d632cSMatthew Dillon 
202219d632cSMatthew Dillon 	v = kern_vfs_bio_buffer_alloc(v, physmem_est);
203219d632cSMatthew Dillon 
204219d632cSMatthew Dillon 	/*
205219d632cSMatthew Dillon 	 * End of first pass, size has been calculated so allocate memory
206219d632cSMatthew Dillon 	 */
207219d632cSMatthew Dillon 	if (firstaddr == 0) {
208857961d9SRobert Drehmel 		size = (vm_size_t)v;
2095df87b21SJeff Roberson 		firstaddr = kmem_malloc(kernel_arena, round_page(size),
2105df87b21SJeff Roberson 		    M_ZERO | M_WAITOK);
211219d632cSMatthew Dillon 		if (firstaddr == 0)
212219d632cSMatthew Dillon 			panic("startup: no room for tables");
213219d632cSMatthew Dillon 		goto again;
214219d632cSMatthew Dillon 	}
215219d632cSMatthew Dillon 
216219d632cSMatthew Dillon 	/*
217219d632cSMatthew Dillon 	 * End of second pass, addresses have been assigned
218219d632cSMatthew Dillon 	 */
219219d632cSMatthew Dillon 	if ((vm_size_t)((char *)v - firstaddr) != size)
220219d632cSMatthew Dillon 		panic("startup: table size inconsistency");
221219d632cSMatthew Dillon 
2225df87b21SJeff Roberson 	/*
2235df87b21SJeff Roberson 	 * Allocate the clean map to hold all of the paging and I/O virtual
2245df87b21SJeff Roberson 	 * memory.
2255df87b21SJeff Roberson 	 */
2265f518366SJeff Roberson 	size = (long)nbuf * BKVASIZE + (long)nswbuf * MAXPHYS +
2275f518366SJeff Roberson 	    (long)bio_transient_maxcnt * MAXPHYS;
2285df87b21SJeff Roberson 	kmi->clean_sva = firstaddr = kva_alloc(size);
2295df87b21SJeff Roberson 	kmi->clean_eva = firstaddr + size;
2305f518366SJeff Roberson 
2315df87b21SJeff Roberson 	/*
2325df87b21SJeff Roberson 	 * Allocate the buffer arena.
2335df87b21SJeff Roberson 	 */
2345f518366SJeff Roberson 	size = (long)nbuf * BKVASIZE;
2355df87b21SJeff Roberson 	kmi->buffer_sva = firstaddr;
2365f518366SJeff Roberson 	kmi->buffer_eva = kmi->buffer_sva + size;
2375f518366SJeff Roberson 	vmem_init(buffer_arena, "buffer arena", kmi->buffer_sva, size,
2385f518366SJeff Roberson 	    PAGE_SIZE, 0, 0);
2395df87b21SJeff Roberson 	firstaddr += size;
2405f518366SJeff Roberson 
2415df87b21SJeff Roberson 	/*
2425df87b21SJeff Roberson 	 * Now swap kva.
2435df87b21SJeff Roberson 	 */
2445df87b21SJeff Roberson 	swapbkva = firstaddr;
2455f518366SJeff Roberson 	size = (long)nswbuf * MAXPHYS;
2465df87b21SJeff Roberson 	firstaddr += size;
2475f518366SJeff Roberson 
2485df87b21SJeff Roberson 	/*
2495df87b21SJeff Roberson 	 * And optionally transient bio space.
2505df87b21SJeff Roberson 	 */
2517db07e1cSKonstantin Belousov 	if (bio_transient_maxcnt != 0) {
2525f518366SJeff Roberson 		size = (long)bio_transient_maxcnt * MAXPHYS;
2535f518366SJeff Roberson 		vmem_init(transient_arena, "transient arena",
2545df87b21SJeff Roberson 		    firstaddr, size, PAGE_SIZE, 0, 0);
2555df87b21SJeff Roberson 		firstaddr += size;
2567db07e1cSKonstantin Belousov 	}
2575df87b21SJeff Roberson 	if (firstaddr != kmi->clean_eva)
2585df87b21SJeff Roberson 		panic("Clean map calculation incorrect");
2595df87b21SJeff Roberson 
2605df87b21SJeff Roberson 	/*
2615df87b21SJeff Roberson  	 * Allocate the pageable submaps.
2625df87b21SJeff Roberson 	 */
263219d632cSMatthew Dillon 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
2642af6e14dSAlan Cox 	    exec_map_entries * round_page(PATH_MAX + ARG_MAX), FALSE);
2653202ed75SAlan Cox 	pipe_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, maxpipekva,
2663202ed75SAlan Cox 	    FALSE);
267219d632cSMatthew Dillon }
268