vm_init.c revision 92748
1254721Semaste/*
2254721Semaste * Copyright (c) 1991, 1993
3353358Sdim *	The Regents of the University of California.  All rights reserved.
4353358Sdim *
5353358Sdim * This code is derived from software contributed to Berkeley by
6254721Semaste * The Mach Operating System project at Carnegie-Mellon University.
7254721Semaste *
8254721Semaste * Redistribution and use in source and binary forms, with or without
9254721Semaste * modification, are permitted provided that the following conditions
10254721Semaste * are met:
11254721Semaste * 1. Redistributions of source code must retain the above copyright
12254721Semaste *    notice, this list of conditions and the following disclaimer.
13254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
14254721Semaste *    notice, this list of conditions and the following disclaimer in the
15254721Semaste *    documentation and/or other materials provided with the distribution.
16314564Sdim * 3. All advertising materials mentioning features or use of this software
17254721Semaste *    must display the following acknowledgement:
18314564Sdim *	This product includes software developed by the University of
19254721Semaste *	California, Berkeley and its contributors.
20314564Sdim * 4. Neither the name of the University nor the names of its contributors
21254721Semaste *    may be used to endorse or promote products derived from this software
22314564Sdim *    without specific prior written permission.
23254721Semaste *
24314564Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26314564Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28353358Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29353358Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30314564Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31314564Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32314564Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33314564Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34314564Sdim * SUCH DAMAGE.
35314564Sdim *
36314564Sdim *	from: @(#)vm_init.c	8.1 (Berkeley) 6/11/93
37314564Sdim *
38314564Sdim *
39314564Sdim * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40314564Sdim * All rights reserved.
41314564Sdim *
42314564Sdim * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43314564Sdim *
44314564Sdim * Permission to use, copy, modify and distribute this software and
45314564Sdim * its documentation is hereby granted, provided that both the copyright
46314564Sdim * notice and this permission notice appear in all copies of the
47254721Semaste * software, derivative works or modified versions, and any portions
48314564Sdim * thereof, and that both notices appear in supporting documentation.
49254721Semaste *
50314564Sdim * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51254721Semaste * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52314564Sdim * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53254721Semaste *
54314564Sdim * Carnegie Mellon requests users of this software to return to
55254721Semaste *
56353358Sdim *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57254721Semaste *  School of Computer Science
58314564Sdim *  Carnegie Mellon University
59314564Sdim *  Pittsburgh PA 15213-3890
60280031Sdim *
61314564Sdim * any improvements or extensions that they make and grant Carnegie the
62296417Sdim * rights to redistribute these changes.
63314564Sdim *
64296417Sdim * $FreeBSD: head/sys/vm/vm_init.c 92748 2002-03-20 04:02:59Z jeff $
65314564Sdim */
66296417Sdim
67314564Sdim/*
68314564Sdim *	Initialize the Virtual Memory subsystem.
69353358Sdim */
70353358Sdim
71314564Sdim#include <sys/param.h>
72314564Sdim#include <sys/kernel.h>
73314564Sdim#include <sys/lock.h>
74314564Sdim#include <sys/mutex.h>
75314564Sdim#include <sys/proc.h>
76314564Sdim#include <sys/systm.h>
77314564Sdim#include <sys/bio.h>
78314564Sdim#include <sys/buf.h>
79314564Sdim
80314564Sdim#include <vm/vm.h>
81314564Sdim#include <vm/vm_param.h>
82314564Sdim#include <vm/vm_kern.h>
83314564Sdim#include <vm/vm_object.h>
84314564Sdim#include <vm/vm_page.h>
85314564Sdim#include <vm/vm_map.h>
86314564Sdim#include <vm/vm_pager.h>
87314564Sdim#include <vm/vm_extern.h>
88314564Sdim
89314564Sdim/*
90314564Sdim * System initialization
91314564Sdim */
92280031Sdimstatic void vm_mem_init(void *);
93314564SdimSYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL)
94314564Sdim
95314564Sdim/*
96314564Sdim *	vm_init initializes the virtual memory system.
97314564Sdim *	This is done only by the first cpu up.
98314564Sdim *
99314564Sdim *	The start and end address of physical memory is passed in.
100314564Sdim */
101314564Sdim/* ARGSUSED*/
102280031Sdimstatic void
103254721Semastevm_mem_init(dummy)
104314564Sdim	void *dummy;
105254721Semaste{
106314564Sdim	/*
107254721Semaste	 * Initializes resident memory structures. From here on, all physical
108314564Sdim	 * memory is accounted for, and we use only virtual addresses.
109254721Semaste	 */
110314564Sdim	vm_set_page_size();
111254721Semaste	virtual_avail = vm_page_startup(avail_start, avail_end, virtual_avail);
112353358Sdim
113353358Sdim	/*
114314564Sdim	 * Initialize other VM packages
115254721Semaste	 */
116314564Sdim	vm_object_init();
117254721Semaste	vm_map_startup();
118314564Sdim	kmem_init(virtual_avail, virtual_end);
119262528Semaste	pmap_init(avail_start, avail_end);
120314564Sdim	vm_pager_init();
121254721Semaste}
122314564Sdim
123254721Semastevoid
124314564Sdimvm_ksubmap_init(struct kva_md_info *kmi)
125254721Semaste{
126314564Sdim	vm_offset_t firstaddr;
127254721Semaste	caddr_t v;
128314564Sdim	vm_size_t size = 0;
129254721Semaste	int physmem_est;
130314564Sdim	vm_offset_t minaddr;
131254721Semaste	vm_offset_t maxaddr;
132314564Sdim
133276479Sdim	/*
134314564Sdim	 * Allocate space for system data structures.
135254721Semaste	 * The first available kernel virtual address is in "v".
136314564Sdim	 * As pages of kernel virtual memory are allocated, "v" is incremented.
137254721Semaste	 * As pages of memory are allocated and cleared,
138314564Sdim	 * "firstaddr" is incremented.
139254721Semaste	 * An index into the kernel page table corresponding to the
140314564Sdim	 * virtual memory address maintained in "v" is kept in "mapaddr".
141254721Semaste	 */
142314564Sdim
143254721Semaste	/*
144314564Sdim	 * Make two passes.  The first pass calculates how much memory is
145254721Semaste	 * needed and allocates it.  The second pass assigns virtual
146314564Sdim	 * addresses to the various data structures.
147314564Sdim	 */
148314564Sdim	firstaddr = 0;
149314564Sdimagain:
150314564Sdim	v = (caddr_t)firstaddr;
151314564Sdim
152314564Sdim	v = kern_timeout_callwheel_alloc(v);
153314564Sdim
154314564Sdim	/*
155314564Sdim	 * Discount the physical memory larger than the size of kernel_map
156314564Sdim	 * to avoid eating up all of KVA space.
157314564Sdim	 */
158314564Sdim	if (kernel_map->first_free == NULL) {
159314564Sdim		printf("Warning: no free entries in kernel_map.\n");
160314564Sdim		physmem_est = physmem;
161314564Sdim	} else {
162314564Sdim		physmem_est = min(physmem, btoc(kernel_map->max_offset -
163314564Sdim		    kernel_map->min_offset));
164314564Sdim	}
165314564Sdim
166314564Sdim	v = kern_vfs_bio_buffer_alloc(v, physmem_est);
167314564Sdim
168314564Sdim	/*
169314564Sdim	 * End of first pass, size has been calculated so allocate memory
170314564Sdim	 */
171314564Sdim	if (firstaddr == 0) {
172314564Sdim		size = (vm_size_t)((char *)v - firstaddr);
173314564Sdim		firstaddr = kmem_alloc(kernel_map, round_page(size));
174314564Sdim		if (firstaddr == 0)
175314564Sdim			panic("startup: no room for tables");
176314564Sdim		goto again;
177314564Sdim	}
178314564Sdim
179314564Sdim	/*
180314564Sdim	 * End of second pass, addresses have been assigned
181314564Sdim	 */
182314564Sdim	if ((vm_size_t)((char *)v - firstaddr) != size)
183314564Sdim		panic("startup: table size inconsistency");
184314564Sdim
185314564Sdim	clean_map = kmem_suballoc(kernel_map, &kmi->clean_sva, &kmi->clean_eva,
186314564Sdim			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
187314564Sdim	buffer_map = kmem_suballoc(clean_map, &kmi->buffer_sva,
188314564Sdim			&kmi->buffer_eva, (nbuf*BKVASIZE));
189314564Sdim	buffer_map->system_map = 1;
190314564Sdim	pager_map = kmem_suballoc(clean_map, &kmi->pager_sva, &kmi->pager_eva,
191314564Sdim				(nswbuf*MAXPHYS) + pager_map_size);
192314564Sdim	pager_map->system_map = 1;
193314564Sdim	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
194314564Sdim				(16*(ARG_MAX+(PAGE_SIZE*3))));
195314564Sdim
196314564Sdim	/*
197314564Sdim	 * XXX: Mbuf system machine-specific initializations should
198314564Sdim	 *      go here, if anywhere.
199314564Sdim	 */
200314564Sdim
201314564Sdim	/*
202314564Sdim	 * Initialize the callouts we just allocated.
203314564Sdim	 */
204314564Sdim	kern_timeout_callwheel_init();
205314564Sdim}
206314564Sdim
207254721Semaste