vm_kern.c revision 1541
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * The Mach Operating System project at Carnegie-Mellon University.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 3. All advertising materials mentioning features or use of this software
171541Srgrimes *    must display the following acknowledgement:
181541Srgrimes *	This product includes software developed by the University of
191541Srgrimes *	California, Berkeley and its contributors.
201541Srgrimes * 4. Neither the name of the University nor the names of its contributors
211541Srgrimes *    may be used to endorse or promote products derived from this software
221541Srgrimes *    without specific prior written permission.
231541Srgrimes *
241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341541Srgrimes * SUCH DAMAGE.
351541Srgrimes *
361541Srgrimes *	@(#)vm_kern.c	8.3 (Berkeley) 1/12/94
371541Srgrimes *
381541Srgrimes *
391541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University.
401541Srgrimes * All rights reserved.
411541Srgrimes *
421541Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young
431541Srgrimes *
441541Srgrimes * Permission to use, copy, modify and distribute this software and
451541Srgrimes * its documentation is hereby granted, provided that both the copyright
461541Srgrimes * notice and this permission notice appear in all copies of the
471541Srgrimes * software, derivative works or modified versions, and any portions
481541Srgrimes * thereof, and that both notices appear in supporting documentation.
491541Srgrimes *
501541Srgrimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
511541Srgrimes * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
521541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
531541Srgrimes *
541541Srgrimes * Carnegie Mellon requests users of this software to return to
551541Srgrimes *
561541Srgrimes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
571541Srgrimes *  School of Computer Science
581541Srgrimes *  Carnegie Mellon University
591541Srgrimes *  Pittsburgh PA 15213-3890
601541Srgrimes *
611541Srgrimes * any improvements or extensions that they make and grant Carnegie the
621541Srgrimes * rights to redistribute these changes.
631541Srgrimes */
641541Srgrimes
651541Srgrimes/*
661541Srgrimes *	Kernel memory management.
671541Srgrimes */
681541Srgrimes
691541Srgrimes#include <sys/param.h>
701541Srgrimes#include <sys/systm.h>
711541Srgrimes
721541Srgrimes#include <vm/vm.h>
731541Srgrimes#include <vm/vm_page.h>
741541Srgrimes#include <vm/vm_pageout.h>
751541Srgrimes#include <vm/vm_kern.h>
761541Srgrimes
771541Srgrimes/*
781541Srgrimes *	kmem_alloc_pageable:
791541Srgrimes *
801541Srgrimes *	Allocate pageable memory to the kernel's address map.
811541Srgrimes *	map must be "kernel_map" below.
821541Srgrimes */
831541Srgrimes
841541Srgrimesvm_offset_t kmem_alloc_pageable(map, size)
851541Srgrimes	vm_map_t		map;
861541Srgrimes	register vm_size_t	size;
871541Srgrimes{
881541Srgrimes	vm_offset_t		addr;
891541Srgrimes	register int		result;
901541Srgrimes
911541Srgrimes#if	0
921541Srgrimes	if (map != kernel_map)
931541Srgrimes		panic("kmem_alloc_pageable: not called with kernel_map");
941541Srgrimes#endif
951541Srgrimes
961541Srgrimes	size = round_page(size);
971541Srgrimes
981541Srgrimes	addr = vm_map_min(map);
991541Srgrimes	result = vm_map_find(map, NULL, (vm_offset_t) 0,
1001541Srgrimes				&addr, size, TRUE);
1011541Srgrimes	if (result != KERN_SUCCESS) {
1021541Srgrimes		return(0);
1031541Srgrimes	}
1041541Srgrimes
1051541Srgrimes	return(addr);
1061541Srgrimes}
1071541Srgrimes
1081541Srgrimes/*
1091541Srgrimes *	Allocate wired-down memory in the kernel's address map
1101541Srgrimes *	or a submap.
1111541Srgrimes */
1121541Srgrimesvm_offset_t kmem_alloc(map, size)
1131541Srgrimes	register vm_map_t	map;
1141541Srgrimes	register vm_size_t	size;
1151541Srgrimes{
1161541Srgrimes	vm_offset_t		addr;
1171541Srgrimes	register vm_offset_t	offset;
1181541Srgrimes	extern vm_object_t	kernel_object;
1191541Srgrimes	vm_offset_t		i;
1201541Srgrimes
1211541Srgrimes	size = round_page(size);
1221541Srgrimes
1231541Srgrimes	/*
1241541Srgrimes	 *	Use the kernel object for wired-down kernel pages.
1251541Srgrimes	 *	Assume that no region of the kernel object is
1261541Srgrimes	 *	referenced more than once.
1271541Srgrimes	 */
1281541Srgrimes
1291541Srgrimes	/*
1301541Srgrimes	 * Locate sufficient space in the map.  This will give us the
1311541Srgrimes	 * final virtual address for the new memory, and thus will tell
1321541Srgrimes	 * us the offset within the kernel map.
1331541Srgrimes	 */
1341541Srgrimes	vm_map_lock(map);
1351541Srgrimes	if (vm_map_findspace(map, 0, size, &addr)) {
1361541Srgrimes		vm_map_unlock(map);
1371541Srgrimes		return (0);
1381541Srgrimes	}
1391541Srgrimes	offset = addr - VM_MIN_KERNEL_ADDRESS;
1401541Srgrimes	vm_object_reference(kernel_object);
1411541Srgrimes	vm_map_insert(map, kernel_object, offset, addr, addr + size);
1421541Srgrimes	vm_map_unlock(map);
1431541Srgrimes
1441541Srgrimes	/*
1451541Srgrimes	 *	Guarantee that there are pages already in this object
1461541Srgrimes	 *	before calling vm_map_pageable.  This is to prevent the
1471541Srgrimes	 *	following scenario:
1481541Srgrimes	 *
1491541Srgrimes	 *		1) Threads have swapped out, so that there is a
1501541Srgrimes	 *		   pager for the kernel_object.
1511541Srgrimes	 *		2) The kmsg zone is empty, and so we are kmem_allocing
1521541Srgrimes	 *		   a new page for it.
1531541Srgrimes	 *		3) vm_map_pageable calls vm_fault; there is no page,
1541541Srgrimes	 *		   but there is a pager, so we call
1551541Srgrimes	 *		   pager_data_request.  But the kmsg zone is empty,
1561541Srgrimes	 *		   so we must kmem_alloc.
1571541Srgrimes	 *		4) goto 1
1581541Srgrimes	 *		5) Even if the kmsg zone is not empty: when we get
1591541Srgrimes	 *		   the data back from the pager, it will be (very
1601541Srgrimes	 *		   stale) non-zero data.  kmem_alloc is defined to
1611541Srgrimes	 *		   return zero-filled memory.
1621541Srgrimes	 *
1631541Srgrimes	 *	We're intentionally not activating the pages we allocate
1641541Srgrimes	 *	to prevent a race with page-out.  vm_map_pageable will wire
1651541Srgrimes	 *	the pages.
1661541Srgrimes	 */
1671541Srgrimes
1681541Srgrimes	vm_object_lock(kernel_object);
1691541Srgrimes	for (i = 0 ; i < size; i+= PAGE_SIZE) {
1701541Srgrimes		vm_page_t	mem;
1711541Srgrimes
1721541Srgrimes		while ((mem = vm_page_alloc(kernel_object, offset+i)) == NULL) {
1731541Srgrimes			vm_object_unlock(kernel_object);
1741541Srgrimes			VM_WAIT;
1751541Srgrimes			vm_object_lock(kernel_object);
1761541Srgrimes		}
1771541Srgrimes		vm_page_zero_fill(mem);
1781541Srgrimes		mem->flags &= ~PG_BUSY;
1791541Srgrimes	}
1801541Srgrimes	vm_object_unlock(kernel_object);
1811541Srgrimes
1821541Srgrimes	/*
1831541Srgrimes	 *	And finally, mark the data as non-pageable.
1841541Srgrimes	 */
1851541Srgrimes
1861541Srgrimes	(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size, FALSE);
1871541Srgrimes
1881541Srgrimes	/*
1891541Srgrimes	 *	Try to coalesce the map
1901541Srgrimes	 */
1911541Srgrimes
1921541Srgrimes	vm_map_simplify(map, addr);
1931541Srgrimes
1941541Srgrimes	return(addr);
1951541Srgrimes}
1961541Srgrimes
1971541Srgrimes/*
1981541Srgrimes *	kmem_free:
1991541Srgrimes *
2001541Srgrimes *	Release a region of kernel virtual memory allocated
2011541Srgrimes *	with kmem_alloc, and return the physical pages
2021541Srgrimes *	associated with that region.
2031541Srgrimes */
2041541Srgrimesvoid kmem_free(map, addr, size)
2051541Srgrimes	vm_map_t		map;
2061541Srgrimes	register vm_offset_t	addr;
2071541Srgrimes	vm_size_t		size;
2081541Srgrimes{
2091541Srgrimes	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
2101541Srgrimes}
2111541Srgrimes
2121541Srgrimes/*
2131541Srgrimes *	kmem_suballoc:
2141541Srgrimes *
2151541Srgrimes *	Allocates a map to manage a subrange
2161541Srgrimes *	of the kernel virtual address space.
2171541Srgrimes *
2181541Srgrimes *	Arguments are as follows:
2191541Srgrimes *
2201541Srgrimes *	parent		Map to take range from
2211541Srgrimes *	size		Size of range to find
2221541Srgrimes *	min, max	Returned endpoints of map
2231541Srgrimes *	pageable	Can the region be paged
2241541Srgrimes */
2251541Srgrimesvm_map_t kmem_suballoc(parent, min, max, size, pageable)
2261541Srgrimes	register vm_map_t	parent;
2271541Srgrimes	vm_offset_t		*min, *max;
2281541Srgrimes	register vm_size_t	size;
2291541Srgrimes	boolean_t		pageable;
2301541Srgrimes{
2311541Srgrimes	register int	ret;
2321541Srgrimes	vm_map_t	result;
2331541Srgrimes
2341541Srgrimes	size = round_page(size);
2351541Srgrimes
2361541Srgrimes	*min = (vm_offset_t) vm_map_min(parent);
2371541Srgrimes	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
2381541Srgrimes				min, size, TRUE);
2391541Srgrimes	if (ret != KERN_SUCCESS) {
2401541Srgrimes		printf("kmem_suballoc: bad status return of %d.\n", ret);
2411541Srgrimes		panic("kmem_suballoc");
2421541Srgrimes	}
2431541Srgrimes	*max = *min + size;
2441541Srgrimes	pmap_reference(vm_map_pmap(parent));
2451541Srgrimes	result = vm_map_create(vm_map_pmap(parent), *min, *max, pageable);
2461541Srgrimes	if (result == NULL)
2471541Srgrimes		panic("kmem_suballoc: cannot create submap");
2481541Srgrimes	if ((ret = vm_map_submap(parent, *min, *max, result)) != KERN_SUCCESS)
2491541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2501541Srgrimes	return(result);
2511541Srgrimes}
2521541Srgrimes
2531541Srgrimes/*
2541541Srgrimes * Allocate wired-down memory in the kernel's address map for the higher
2551541Srgrimes * level kernel memory allocator (kern/kern_malloc.c).  We cannot use
2561541Srgrimes * kmem_alloc() because we may need to allocate memory at interrupt
2571541Srgrimes * level where we cannot block (canwait == FALSE).
2581541Srgrimes *
2591541Srgrimes * This routine has its own private kernel submap (kmem_map) and object
2601541Srgrimes * (kmem_object).  This, combined with the fact that only malloc uses
2611541Srgrimes * this routine, ensures that we will never block in map or object waits.
2621541Srgrimes *
2631541Srgrimes * Note that this still only works in a uni-processor environment and
2641541Srgrimes * when called at splhigh().
2651541Srgrimes *
2661541Srgrimes * We don't worry about expanding the map (adding entries) since entries
2671541Srgrimes * for wired maps are statically allocated.
2681541Srgrimes */
2691541Srgrimesvm_offset_t
2701541Srgrimeskmem_malloc(map, size, canwait)
2711541Srgrimes	register vm_map_t	map;
2721541Srgrimes	register vm_size_t	size;
2731541Srgrimes	boolean_t		canwait;
2741541Srgrimes{
2751541Srgrimes	register vm_offset_t	offset, i;
2761541Srgrimes	vm_map_entry_t		entry;
2771541Srgrimes	vm_offset_t		addr;
2781541Srgrimes	vm_page_t		m;
2791541Srgrimes	extern vm_object_t	kmem_object;
2801541Srgrimes
2811541Srgrimes	if (map != kmem_map && map != mb_map)
2821541Srgrimes		panic("kern_malloc_alloc: map != {kmem,mb}_map");
2831541Srgrimes
2841541Srgrimes	size = round_page(size);
2851541Srgrimes	addr = vm_map_min(map);
2861541Srgrimes
2871541Srgrimes	/*
2881541Srgrimes	 * Locate sufficient space in the map.  This will give us the
2891541Srgrimes	 * final virtual address for the new memory, and thus will tell
2901541Srgrimes	 * us the offset within the kernel map.
2911541Srgrimes	 */
2921541Srgrimes	vm_map_lock(map);
2931541Srgrimes	if (vm_map_findspace(map, 0, size, &addr)) {
2941541Srgrimes		vm_map_unlock(map);
2951541Srgrimes		if (canwait)		/* XXX  should wait */
2961541Srgrimes			panic("kmem_malloc: %s too small",
2971541Srgrimes			    map == kmem_map ? "kmem_map" : "mb_map");
2981541Srgrimes		return (0);
2991541Srgrimes	}
3001541Srgrimes	offset = addr - vm_map_min(kmem_map);
3011541Srgrimes	vm_object_reference(kmem_object);
3021541Srgrimes	vm_map_insert(map, kmem_object, offset, addr, addr + size);
3031541Srgrimes
3041541Srgrimes	/*
3051541Srgrimes	 * If we can wait, just mark the range as wired
3061541Srgrimes	 * (will fault pages as necessary).
3071541Srgrimes	 */
3081541Srgrimes	if (canwait) {
3091541Srgrimes		vm_map_unlock(map);
3101541Srgrimes		(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size,
3111541Srgrimes				       FALSE);
3121541Srgrimes		vm_map_simplify(map, addr);
3131541Srgrimes		return(addr);
3141541Srgrimes	}
3151541Srgrimes
3161541Srgrimes	/*
3171541Srgrimes	 * If we cannot wait then we must allocate all memory up front,
3181541Srgrimes	 * pulling it off the active queue to prevent pageout.
3191541Srgrimes	 */
3201541Srgrimes	vm_object_lock(kmem_object);
3211541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
3221541Srgrimes		m = vm_page_alloc(kmem_object, offset + i);
3231541Srgrimes
3241541Srgrimes		/*
3251541Srgrimes		 * Ran out of space, free everything up and return.
3261541Srgrimes		 * Don't need to lock page queues here as we know
3271541Srgrimes		 * that the pages we got aren't on any queues.
3281541Srgrimes		 */
3291541Srgrimes		if (m == NULL) {
3301541Srgrimes			while (i != 0) {
3311541Srgrimes				i -= PAGE_SIZE;
3321541Srgrimes				m = vm_page_lookup(kmem_object, offset + i);
3331541Srgrimes				vm_page_free(m);
3341541Srgrimes			}
3351541Srgrimes			vm_object_unlock(kmem_object);
3361541Srgrimes			vm_map_delete(map, addr, addr + size);
3371541Srgrimes			vm_map_unlock(map);
3381541Srgrimes			return(0);
3391541Srgrimes		}
3401541Srgrimes#if 0
3411541Srgrimes		vm_page_zero_fill(m);
3421541Srgrimes#endif
3431541Srgrimes		m->flags &= ~PG_BUSY;
3441541Srgrimes	}
3451541Srgrimes	vm_object_unlock(kmem_object);
3461541Srgrimes
3471541Srgrimes	/*
3481541Srgrimes	 * Mark map entry as non-pageable.
3491541Srgrimes	 * Assert: vm_map_insert() will never be able to extend the previous
3501541Srgrimes	 * entry so there will be a new entry exactly corresponding to this
3511541Srgrimes	 * address range and it will have wired_count == 0.
3521541Srgrimes	 */
3531541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
3541541Srgrimes	    entry->start != addr || entry->end != addr + size ||
3551541Srgrimes	    entry->wired_count)
3561541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
3571541Srgrimes	entry->wired_count++;
3581541Srgrimes
3591541Srgrimes	/*
3601541Srgrimes	 * Loop thru pages, entering them in the pmap.
3611541Srgrimes	 * (We cannot add them to the wired count without
3621541Srgrimes	 * wrapping the vm_page_queue_lock in splimp...)
3631541Srgrimes	 */
3641541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
3651541Srgrimes		vm_object_lock(kmem_object);
3661541Srgrimes		m = vm_page_lookup(kmem_object, offset + i);
3671541Srgrimes		vm_object_unlock(kmem_object);
3681541Srgrimes		pmap_enter(map->pmap, addr + i, VM_PAGE_TO_PHYS(m),
3691541Srgrimes			   VM_PROT_DEFAULT, TRUE);
3701541Srgrimes	}
3711541Srgrimes	vm_map_unlock(map);
3721541Srgrimes
3731541Srgrimes	vm_map_simplify(map, addr);
3741541Srgrimes	return(addr);
3751541Srgrimes}
3761541Srgrimes
3771541Srgrimes/*
3781541Srgrimes *	kmem_alloc_wait
3791541Srgrimes *
3801541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
3811541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
3821541Srgrimes *
3831541Srgrimes */
3841541Srgrimesvm_offset_t kmem_alloc_wait(map, size)
3851541Srgrimes	vm_map_t	map;
3861541Srgrimes	vm_size_t	size;
3871541Srgrimes{
3881541Srgrimes	vm_offset_t	addr;
3891541Srgrimes
3901541Srgrimes	size = round_page(size);
3911541Srgrimes
3921541Srgrimes	for (;;) {
3931541Srgrimes		/*
3941541Srgrimes		 * To make this work for more than one map,
3951541Srgrimes		 * use the map's lock to lock out sleepers/wakers.
3961541Srgrimes		 */
3971541Srgrimes		vm_map_lock(map);
3981541Srgrimes		if (vm_map_findspace(map, 0, size, &addr) == 0)
3991541Srgrimes			break;
4001541Srgrimes		/* no space now; see if we can ever get space */
4011541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4021541Srgrimes			vm_map_unlock(map);
4031541Srgrimes			return (0);
4041541Srgrimes		}
4051541Srgrimes		assert_wait((int)map, TRUE);
4061541Srgrimes		vm_map_unlock(map);
4071541Srgrimes		thread_block();
4081541Srgrimes	}
4091541Srgrimes	vm_map_insert(map, NULL, (vm_offset_t)0, addr, addr + size);
4101541Srgrimes	vm_map_unlock(map);
4111541Srgrimes	return (addr);
4121541Srgrimes}
4131541Srgrimes
4141541Srgrimes/*
4151541Srgrimes *	kmem_free_wakeup
4161541Srgrimes *
4171541Srgrimes *	Returns memory to a submap of the kernel, and wakes up any threads
4181541Srgrimes *	waiting for memory in that map.
4191541Srgrimes */
4201541Srgrimesvoid	kmem_free_wakeup(map, addr, size)
4211541Srgrimes	vm_map_t	map;
4221541Srgrimes	vm_offset_t	addr;
4231541Srgrimes	vm_size_t	size;
4241541Srgrimes{
4251541Srgrimes	vm_map_lock(map);
4261541Srgrimes	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
4271541Srgrimes	thread_wakeup((int)map);
4281541Srgrimes	vm_map_unlock(map);
4291541Srgrimes}
4301541Srgrimes
4311541Srgrimes/*
4321541Srgrimes * Create the kernel map; insert a mapping covering kernel text, data, bss,
4331541Srgrimes * and all space allocated thus far (`boostrap' data).  The new map will thus
4341541Srgrimes * map the range between VM_MIN_KERNEL_ADDRESS and `start' as allocated, and
4351541Srgrimes * the range between `start' and `end' as free.
4361541Srgrimes */
4371541Srgrimesvoid kmem_init(start, end)
4381541Srgrimes	vm_offset_t start, end;
4391541Srgrimes{
4401541Srgrimes	register vm_map_t m;
4411541Srgrimes
4421541Srgrimes	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end, FALSE);
4431541Srgrimes	vm_map_lock(m);
4441541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
4451541Srgrimes	kernel_map = m;
4461541Srgrimes	(void) vm_map_insert(m, NULL, (vm_offset_t)0,
4471541Srgrimes	    VM_MIN_KERNEL_ADDRESS, start);
4481541Srgrimes	/* ... and ending with the completion of the above `insert' */
4491541Srgrimes	vm_map_unlock(m);
4501541Srgrimes}
451