vm_init.c revision 92029
192986Sobrien/*
292986Sobrien * Copyright (c) 1991, 1993
392986Sobrien *	The Regents of the University of California.  All rights reserved.
462856Sdcs *
562856Sdcs * This code is derived from software contributed to Berkeley by
662856Sdcs * The Mach Operating System project at Carnegie-Mellon University.
7292170Sngie *
8292170Sngie * Redistribution and use in source and binary forms, with or without
962856Sdcs * modification, are permitted provided that the following conditions
1062856Sdcs * are met:
11292144Sngie * 1. Redistributions of source code must retain the above copyright
12292144Sngie *    notice, this list of conditions and the following disclaimer.
13292144Sngie * 2. Redistributions in binary form must reproduce the above copyright
14292144Sngie *    notice, this list of conditions and the following disclaimer in the
1562856Sdcs *    documentation and/or other materials provided with the distribution.
1662856Sdcs * 3. All advertising materials mentioning features or use of this software
17292144Sngie *    must display the following acknowledgement:
1862856Sdcs *	This product includes software developed by the University of
1992889Sobrien *	California, Berkeley and its contributors.
2092889Sobrien * 4. Neither the name of the University nor the names of its contributors
2192889Sobrien *    may be used to endorse or promote products derived from this software
2292889Sobrien *    without specific prior written permission.
2392889Sobrien *
2492889Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2592889Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2692889Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2762856Sdcs * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2862856Sdcs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2962856Sdcs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3062856Sdcs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3162856Sdcs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3262856Sdcs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3362856Sdcs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3462856Sdcs * SUCH DAMAGE.
3562856Sdcs *
3662856Sdcs *	from: @(#)vm_init.c	8.1 (Berkeley) 6/11/93
3762856Sdcs *
3862856Sdcs *
3962856Sdcs * Copyright (c) 1987, 1990 Carnegie-Mellon University.
4062856Sdcs * All rights reserved.
4162856Sdcs *
4262856Sdcs * Authors: Avadis Tevanian, Jr., Michael Wayne Young
4362856Sdcs *
4462856Sdcs * Permission to use, copy, modify and distribute this software and
4562856Sdcs * its documentation is hereby granted, provided that both the copyright
4662856Sdcs * notice and this permission notice appear in all copies of the
4762856Sdcs * software, derivative works or modified versions, and any portions
4862856Sdcs * thereof, and that both notices appear in supporting documentation.
4962856Sdcs *
5062856Sdcs * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
5162856Sdcs * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
5262856Sdcs * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5362856Sdcs *
5462856Sdcs * Carnegie Mellon requests users of this software to return to
5562856Sdcs *
5662856Sdcs *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5762856Sdcs *  School of Computer Science
5862856Sdcs *  Carnegie Mellon University
5962856Sdcs *  Pittsburgh PA 15213-3890
6062856Sdcs *
6162856Sdcs * any improvements or extensions that they make and grant Carnegie the
6262856Sdcs * rights to redistribute these changes.
6362856Sdcs *
6462856Sdcs * $FreeBSD: head/sys/vm/vm_init.c 92029 2002-03-10 21:52:48Z eivind $
6562856Sdcs */
6662856Sdcs
6762856Sdcs/*
6862856Sdcs *	Initialize the Virtual Memory subsystem.
6962856Sdcs */
7062856Sdcs
7162856Sdcs#include <sys/param.h>
7262856Sdcs#include <sys/kernel.h>
7362856Sdcs#include <sys/lock.h>
7462856Sdcs#include <sys/mutex.h>
7562856Sdcs#include <sys/proc.h>
7662856Sdcs#include <sys/systm.h>
7762856Sdcs#include <sys/bio.h>
7862856Sdcs#include <sys/buf.h>
7962856Sdcs
8062856Sdcs#include <vm/vm.h>
8162856Sdcs#include <vm/vm_param.h>
8262856Sdcs#include <vm/vm_kern.h>
8362856Sdcs#include <vm/vm_object.h>
8462856Sdcs#include <vm/vm_page.h>
8562856Sdcs#include <vm/vm_map.h>
8662856Sdcs#include <vm/vm_pager.h>
8762856Sdcs#include <vm/vm_extern.h>
8862856Sdcs#include <vm/vm_zone.h>
8962856Sdcs
9062856Sdcs/*
9162856Sdcs * System initialization
9262856Sdcs */
9362856Sdcsstatic void vm_mem_init __P((void *));
9462856SdcsSYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_FIRST, vm_mem_init, NULL)
9562856Sdcs
9662856Sdcs/*
9762856Sdcs *	vm_init initializes the virtual memory system.
9862856Sdcs *	This is done only by the first cpu up.
9962856Sdcs *
10062856Sdcs *	The start and end address of physical memory is passed in.
10162856Sdcs */
10262856Sdcs/* ARGSUSED*/
10362856Sdcsstatic void
10462856Sdcsvm_mem_init(dummy)
10562856Sdcs	void *dummy;
10662856Sdcs{
10762856Sdcs	/*
10862856Sdcs	 * Initializes resident memory structures. From here on, all physical
10962856Sdcs	 * memory is accounted for, and we use only virtual addresses.
11062856Sdcs	 */
11162856Sdcs	vm_set_page_size();
11262856Sdcs	virtual_avail = vm_page_startup(avail_start, avail_end, virtual_avail);
11362856Sdcs
11462856Sdcs	/*
11562856Sdcs	 * Initialize other VM packages
11662856Sdcs	 */
11762856Sdcs	vm_zone_init();
11862856Sdcs	vm_object_init();
11962856Sdcs	vm_map_startup();
12062856Sdcs	kmem_init(virtual_avail, virtual_end);
12162856Sdcs	pmap_init(avail_start, avail_end);
12262856Sdcs	vm_pager_init();
12362856Sdcs}
12462856Sdcs
12562856Sdcsvoid
12662856Sdcsvm_ksubmap_init(struct kva_md_info *kmi)
12762856Sdcs{
12862856Sdcs	vm_offset_t firstaddr;
12962856Sdcs	caddr_t v;
13062856Sdcs	vm_size_t size = 0;
13162856Sdcs	int physmem_est;
13262856Sdcs	vm_offset_t minaddr;
13362856Sdcs	vm_offset_t maxaddr;
13462856Sdcs
13562856Sdcs	/*
13662856Sdcs	 * Allocate space for system data structures.
13762856Sdcs	 * The first available kernel virtual address is in "v".
13862856Sdcs	 * As pages of kernel virtual memory are allocated, "v" is incremented.
13962856Sdcs	 * As pages of memory are allocated and cleared,
14062856Sdcs	 * "firstaddr" is incremented.
14162856Sdcs	 * An index into the kernel page table corresponding to the
14262856Sdcs	 * virtual memory address maintained in "v" is kept in "mapaddr".
14362856Sdcs	 */
14462856Sdcs
14562856Sdcs	/*
14662856Sdcs	 * Make two passes.  The first pass calculates how much memory is
14762856Sdcs	 * needed and allocates it.  The second pass assigns virtual
14862856Sdcs	 * addresses to the various data structures.
14962856Sdcs	 */
15062856Sdcs	firstaddr = 0;
15162856Sdcsagain:
15262856Sdcs	v = (caddr_t)firstaddr;
15362856Sdcs
15462856Sdcs	v = kern_timeout_callwheel_alloc(v);
155292144Sngie
15662856Sdcs	/*
15762856Sdcs	 * Discount the physical memory larger than the size of kernel_map
15892889Sobrien	 * to avoid eating up all of KVA space.
15962856Sdcs	 */
16062856Sdcs	if (kernel_map->first_free == NULL) {
16162856Sdcs		printf("Warning: no free entries in kernel_map.\n");
16262856Sdcs		physmem_est = physmem;
16362856Sdcs	} else {
16462856Sdcs		physmem_est = min(physmem, btoc(kernel_map->max_offset -
16562856Sdcs		    kernel_map->min_offset));
16662856Sdcs	}
16762856Sdcs
16862856Sdcs	v = kern_vfs_bio_buffer_alloc(v, physmem_est);
16962856Sdcs
17062856Sdcs	/*
17162856Sdcs	 * End of first pass, size has been calculated so allocate memory
17262856Sdcs	 */
17362856Sdcs	if (firstaddr == 0) {
17462856Sdcs		size = (vm_size_t)((char *)v - firstaddr);
17562856Sdcs		firstaddr = kmem_alloc(kernel_map, round_page(size));
17662856Sdcs		if (firstaddr == 0)
17762856Sdcs			panic("startup: no room for tables");
17862856Sdcs		goto again;
17962856Sdcs	}
18062856Sdcs
18162856Sdcs	/*
18262856Sdcs	 * End of second pass, addresses have been assigned
18362856Sdcs	 */
184292144Sngie	if ((vm_size_t)((char *)v - firstaddr) != size)
185292144Sngie		panic("startup: table size inconsistency");
18662856Sdcs
18762856Sdcs	clean_map = kmem_suballoc(kernel_map, &kmi->clean_sva, &kmi->clean_eva,
18862856Sdcs			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
18992889Sobrien	buffer_map = kmem_suballoc(clean_map, &kmi->buffer_sva,
19062856Sdcs			&kmi->buffer_eva, (nbuf*BKVASIZE));
19162856Sdcs	buffer_map->system_map = 1;
19262856Sdcs	pager_map = kmem_suballoc(clean_map, &kmi->pager_sva, &kmi->pager_eva,
19362856Sdcs				(nswbuf*MAXPHYS) + pager_map_size);
19462856Sdcs	pager_map->system_map = 1;
195292144Sngie	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
196292144Sngie				(16*(ARG_MAX+(PAGE_SIZE*3))));
19762856Sdcs
19892889Sobrien	/*
19992889Sobrien	 * XXX: Mbuf system machine-specific initializations should
20062856Sdcs	 *      go here, if anywhere.
20162856Sdcs	 */
20262856Sdcs
20362856Sdcs	/*
20462856Sdcs	 * Initialize the callouts we just allocated.
20562856Sdcs	 */
20662856Sdcs	kern_timeout_callwheel_init();
20762856Sdcs}
20862856Sdcs
20962856Sdcs