xref: /freebsd/sys/vm/vm_init.c (revision 219d632c15f42d97ccd44cc1f9af6ab443077a9f)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
175929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_init.c	8.1 (Berkeley) 6/11/93
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
64c3aac50fSPeter Wemm  * $FreeBSD$
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Initialize the Virtual Memory subsystem.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
722b14f991SJulian Elischer #include <sys/kernel.h>
73fb919e4dSMark Murray #include <sys/lock.h>
7487446895SJohn Baldwin #include <sys/mutex.h>
75fb919e4dSMark Murray #include <sys/proc.h>
7605f0fdd2SPoul-Henning Kamp #include <sys/systm.h>
77219d632cSMatthew Dillon #include <sys/bio.h>
78219d632cSMatthew Dillon #include <sys/buf.h>
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes #include <vm/vm.h>
81219d632cSMatthew Dillon #include <vm/vm_param.h>
82219d632cSMatthew Dillon #include <vm/vm_kern.h>
83efeaf95aSDavid Greenman #include <vm/vm_object.h>
84df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
8624a1cce3SDavid Greenman #include <vm/vm_pager.h>
87efeaf95aSDavid Greenman #include <vm/vm_extern.h>
880f68b659SJohn Baldwin #include <vm/vm_zone.h>
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes /*
912b14f991SJulian Elischer  * System initialization
922b14f991SJulian Elischer  */
932b14f991SJulian Elischer 
944590fd3aSDavid Greenman static void vm_mem_init __P((void *));
952b14f991SJulian Elischer SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL)
962b14f991SJulian Elischer 
972b14f991SJulian Elischer /*
98df8bae1dSRodney W. Grimes  *	vm_init initializes the virtual memory system.
99df8bae1dSRodney W. Grimes  *	This is done only by the first cpu up.
100df8bae1dSRodney W. Grimes  *
101df8bae1dSRodney W. Grimes  *	The start and end address of physical memory is passed in.
102df8bae1dSRodney W. Grimes  */
103df8bae1dSRodney W. Grimes 
1042b14f991SJulian Elischer /* ARGSUSED*/
1052b14f991SJulian Elischer static void
106d841aaa7SBruce Evans vm_mem_init(dummy)
107d841aaa7SBruce Evans 	void *dummy;
108df8bae1dSRodney W. Grimes {
109df8bae1dSRodney W. Grimes 	/*
1100d94caffSDavid Greenman 	 * Initializes resident memory structures. From here on, all physical
1110d94caffSDavid Greenman 	 * memory is accounted for, and we use only virtual addresses.
112df8bae1dSRodney W. Grimes 	 */
11326f9a767SRodney W. Grimes 	vm_set_page_size();
11426f9a767SRodney W. Grimes 	virtual_avail = vm_page_startup(avail_start, avail_end, virtual_avail);
115e7841165SDag-Erling Smørgrav 
116df8bae1dSRodney W. Grimes 	/*
117df8bae1dSRodney W. Grimes 	 * Initialize other VM packages
118df8bae1dSRodney W. Grimes 	 */
119e7841165SDag-Erling Smørgrav 	vm_zone_init();
120a316d390SJohn Dyson 	vm_object_init();
121df8bae1dSRodney W. Grimes 	vm_map_startup();
122df8bae1dSRodney W. Grimes 	kmem_init(virtual_avail, virtual_end);
123df8bae1dSRodney W. Grimes 	pmap_init(avail_start, avail_end);
124df8bae1dSRodney W. Grimes 	vm_pager_init();
125df8bae1dSRodney W. Grimes }
126219d632cSMatthew Dillon 
127219d632cSMatthew Dillon void
128219d632cSMatthew Dillon vm_ksubmap_init(struct kva_md_info *kmi)
129219d632cSMatthew Dillon {
130219d632cSMatthew Dillon 	vm_offset_t firstaddr;
131219d632cSMatthew Dillon 	caddr_t v;
132219d632cSMatthew Dillon 	vm_size_t size = 0;
133219d632cSMatthew Dillon 	int physmem_est;
134219d632cSMatthew Dillon 	vm_offset_t minaddr;
135219d632cSMatthew Dillon 	vm_offset_t maxaddr;
136219d632cSMatthew Dillon 
137219d632cSMatthew Dillon 	/*
138219d632cSMatthew Dillon 	 * Allocate space for system data structures.
139219d632cSMatthew Dillon 	 * The first available kernel virtual address is in "v".
140219d632cSMatthew Dillon 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
141219d632cSMatthew Dillon 	 * As pages of memory are allocated and cleared,
142219d632cSMatthew Dillon 	 * "firstaddr" is incremented.
143219d632cSMatthew Dillon 	 * An index into the kernel page table corresponding to the
144219d632cSMatthew Dillon 	 * virtual memory address maintained in "v" is kept in "mapaddr".
145219d632cSMatthew Dillon 	 */
146219d632cSMatthew Dillon 
147219d632cSMatthew Dillon 	/*
148219d632cSMatthew Dillon 	 * Make two passes.  The first pass calculates how much memory is
149219d632cSMatthew Dillon 	 * needed and allocates it.  The second pass assigns virtual
150219d632cSMatthew Dillon 	 * addresses to the various data structures.
151219d632cSMatthew Dillon 	 */
152219d632cSMatthew Dillon 	firstaddr = 0;
153219d632cSMatthew Dillon again:
154219d632cSMatthew Dillon 	v = (caddr_t)firstaddr;
155219d632cSMatthew Dillon 
156219d632cSMatthew Dillon 	v = kern_timeout_callwheel_alloc(v);
157219d632cSMatthew Dillon 
158219d632cSMatthew Dillon 	/*
159219d632cSMatthew Dillon 	 * Discount the physical memory larger than the size of kernel_map
160219d632cSMatthew Dillon 	 * to avoid eating up all of KVA space.
161219d632cSMatthew Dillon 	 */
162219d632cSMatthew Dillon 	if (kernel_map->first_free == NULL) {
163219d632cSMatthew Dillon 		printf("Warning: no free entries in kernel_map.\n");
164219d632cSMatthew Dillon 		physmem_est = physmem;
165219d632cSMatthew Dillon 	} else {
166219d632cSMatthew Dillon 		physmem_est = min(physmem, btoc(kernel_map->max_offset -
167219d632cSMatthew Dillon 		    kernel_map->min_offset));
168219d632cSMatthew Dillon 	}
169219d632cSMatthew Dillon 
170219d632cSMatthew Dillon 	v = kern_vfs_bio_buffer_alloc(v, physmem_est);
171219d632cSMatthew Dillon 
172219d632cSMatthew Dillon 	/*
173219d632cSMatthew Dillon 	 * End of first pass, size has been calculated so allocate memory
174219d632cSMatthew Dillon 	 */
175219d632cSMatthew Dillon 	if (firstaddr == 0) {
176219d632cSMatthew Dillon 		size = (vm_size_t)((char *)v - firstaddr);
177219d632cSMatthew Dillon 		firstaddr = kmem_alloc(kernel_map, round_page(size));
178219d632cSMatthew Dillon 		if (firstaddr == 0)
179219d632cSMatthew Dillon 			panic("startup: no room for tables");
180219d632cSMatthew Dillon 		goto again;
181219d632cSMatthew Dillon 	}
182219d632cSMatthew Dillon 
183219d632cSMatthew Dillon 	/*
184219d632cSMatthew Dillon 	 * End of second pass, addresses have been assigned
185219d632cSMatthew Dillon 	 */
186219d632cSMatthew Dillon 	if ((vm_size_t)((char *)v - firstaddr) != size)
187219d632cSMatthew Dillon 		panic("startup: table size inconsistency");
188219d632cSMatthew Dillon 
189219d632cSMatthew Dillon 	clean_map = kmem_suballoc(kernel_map, &kmi->clean_sva, &kmi->clean_eva,
190219d632cSMatthew Dillon 			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
191219d632cSMatthew Dillon 	buffer_map = kmem_suballoc(clean_map, &kmi->buffer_sva,
192219d632cSMatthew Dillon 			&kmi->buffer_eva, (nbuf*BKVASIZE));
193219d632cSMatthew Dillon 	buffer_map->system_map = 1;
194219d632cSMatthew Dillon 	pager_map = kmem_suballoc(clean_map, &kmi->pager_sva, &kmi->pager_eva,
195219d632cSMatthew Dillon 				(nswbuf*MAXPHYS) + pager_map_size);
196219d632cSMatthew Dillon 	pager_map->system_map = 1;
197219d632cSMatthew Dillon 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
198219d632cSMatthew Dillon 				(16*(ARG_MAX+(PAGE_SIZE*3))));
199219d632cSMatthew Dillon 
200219d632cSMatthew Dillon 	/*
201219d632cSMatthew Dillon 	 * XXX: Mbuf system machine-specific initializations should
202219d632cSMatthew Dillon 	 *      go here, if anywhere.
203219d632cSMatthew Dillon 	 */
204219d632cSMatthew Dillon 
205219d632cSMatthew Dillon 	/*
206219d632cSMatthew Dillon 	 * Initialize the callouts we just allocated.
207219d632cSMatthew Dillon 	 */
208219d632cSMatthew Dillon 	kern_timeout_callwheel_init();
209219d632cSMatthew Dillon }
210219d632cSMatthew Dillon 
211