vm_kern.c revision 98450
15455Sdg/*
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
1758705Scharnier *    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 *
361817Sdg *	from: @(#)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
435455Sdg *
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.
495455Sdg *
505455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
515455Sdg * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
521541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
535455Sdg *
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.
631817Sdg *
6450477Speter * $FreeBSD: head/sys/vm/vm_kern.c 98450 2002-06-19 20:47:18Z jeff $
651541Srgrimes */
661541Srgrimes
671541Srgrimes/*
681541Srgrimes *	Kernel memory management.
691541Srgrimes */
701541Srgrimes
711541Srgrimes#include <sys/param.h>
721541Srgrimes#include <sys/systm.h>
7387157Sluigi#include <sys/kernel.h>		/* for ticks and hz */
7476166Smarkm#include <sys/lock.h>
7576166Smarkm#include <sys/mutex.h>
762112Swollman#include <sys/proc.h>
776129Sdg#include <sys/malloc.h>
781541Srgrimes
791541Srgrimes#include <vm/vm.h>
8012662Sdg#include <vm/vm_param.h>
8112662Sdg#include <vm/pmap.h>
8212662Sdg#include <vm/vm_map.h>
8312662Sdg#include <vm/vm_object.h>
841541Srgrimes#include <vm/vm_page.h>
851541Srgrimes#include <vm/vm_pageout.h>
8612726Sbde#include <vm/vm_extern.h>
871541Srgrimes
8819830Sdysonvm_map_t kernel_map=0;
8919830Sdysonvm_map_t kmem_map=0;
9019830Sdysonvm_map_t exec_map=0;
9119830Sdysonvm_map_t clean_map=0;
9219830Sdysonvm_map_t buffer_map=0;
932112Swollman
941541Srgrimes/*
951541Srgrimes *	kmem_alloc_pageable:
961541Srgrimes *
971541Srgrimes *	Allocate pageable memory to the kernel's address map.
9812259Sdg *	"map" must be kernel_map or a submap of kernel_map.
991541Srgrimes */
1008876Srgrimesvm_offset_t
1015455Sdgkmem_alloc_pageable(map, size)
1025455Sdg	vm_map_t map;
10370480Salfred	vm_size_t size;
1041541Srgrimes{
1055455Sdg	vm_offset_t addr;
10670480Salfred	int result;
1071541Srgrimes
10879224Sdillon	GIANT_REQUIRED;
10979224Sdillon
1101541Srgrimes	size = round_page(size);
1111541Srgrimes	addr = vm_map_min(map);
1121541Srgrimes	result = vm_map_find(map, NULL, (vm_offset_t) 0,
11313490Sdyson	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
1141541Srgrimes	if (result != KERN_SUCCESS) {
1155455Sdg		return (0);
1161541Srgrimes	}
1175455Sdg	return (addr);
1181541Srgrimes}
1191541Srgrimes
1201541Srgrimes/*
12147841Sdt *	kmem_alloc_nofault:
12247841Sdt *
12347841Sdt *	Same as kmem_alloc_pageable, except that it create a nofault entry.
12447841Sdt */
12547841Sdtvm_offset_t
12647841Sdtkmem_alloc_nofault(map, size)
12747841Sdt	vm_map_t map;
12870480Salfred	vm_size_t size;
12947841Sdt{
13047841Sdt	vm_offset_t addr;
13170480Salfred	int result;
13247841Sdt
13379224Sdillon	GIANT_REQUIRED;
13476827Salfred
13547841Sdt	size = round_page(size);
13647841Sdt	addr = vm_map_min(map);
13747841Sdt	result = vm_map_find(map, NULL, (vm_offset_t) 0,
13847841Sdt	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
13947841Sdt	if (result != KERN_SUCCESS) {
14047841Sdt		return (0);
14147841Sdt	}
14247841Sdt	return (addr);
14347841Sdt}
14447841Sdt
14547841Sdt/*
1461541Srgrimes *	Allocate wired-down memory in the kernel's address map
1471541Srgrimes *	or a submap.
1481541Srgrimes */
1498876Srgrimesvm_offset_t
1505455Sdgkmem_alloc(map, size)
15170480Salfred	vm_map_t map;
15270480Salfred	vm_size_t size;
1531541Srgrimes{
1545455Sdg	vm_offset_t addr;
15570480Salfred	vm_offset_t offset;
1565455Sdg	vm_offset_t i;
1571541Srgrimes
15879224Sdillon	GIANT_REQUIRED;
15979224Sdillon
1601541Srgrimes	size = round_page(size);
1611541Srgrimes
1621541Srgrimes	/*
1635455Sdg	 * Use the kernel object for wired-down kernel pages. Assume that no
1645455Sdg	 * region of the kernel object is referenced more than once.
1651541Srgrimes	 */
1661541Srgrimes
1671541Srgrimes	/*
1685455Sdg	 * Locate sufficient space in the map.  This will give us the final
1695455Sdg	 * virtual address for the new memory, and thus will tell us the
1705455Sdg	 * offset within the kernel map.
1711541Srgrimes	 */
1721541Srgrimes	vm_map_lock(map);
17333758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
1741541Srgrimes		vm_map_unlock(map);
1751541Srgrimes		return (0);
1761541Srgrimes	}
1771541Srgrimes	offset = addr - VM_MIN_KERNEL_ADDRESS;
1781541Srgrimes	vm_object_reference(kernel_object);
17913490Sdyson	vm_map_insert(map, kernel_object, offset, addr, addr + size,
18013490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
1811541Srgrimes	vm_map_unlock(map);
1821541Srgrimes
1831541Srgrimes	/*
1845455Sdg	 * Guarantee that there are pages already in this object before
1855455Sdg	 * calling vm_map_pageable.  This is to prevent the following
1865455Sdg	 * scenario:
1878876Srgrimes	 *
1885455Sdg	 * 1) Threads have swapped out, so that there is a pager for the
1895455Sdg	 * kernel_object. 2) The kmsg zone is empty, and so we are
1905455Sdg	 * kmem_allocing a new page for it. 3) vm_map_pageable calls vm_fault;
1915455Sdg	 * there is no page, but there is a pager, so we call
1925455Sdg	 * pager_data_request.  But the kmsg zone is empty, so we must
1935455Sdg	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
1945455Sdg	 * we get the data back from the pager, it will be (very stale)
1955455Sdg	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
1968876Srgrimes	 *
1975455Sdg	 * We're intentionally not activating the pages we allocate to prevent a
1985455Sdg	 * race with page-out.  vm_map_pageable will wire the pages.
1991541Srgrimes	 */
2005455Sdg	for (i = 0; i < size; i += PAGE_SIZE) {
2015455Sdg		vm_page_t mem;
2021541Srgrimes
20333109Sdyson		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
20433109Sdyson				VM_ALLOC_ZERO | VM_ALLOC_RETRY);
20510548Sdyson		if ((mem->flags & PG_ZERO) == 0)
20610548Sdyson			vm_page_zero_fill(mem);
2076585Sdg		mem->valid = VM_PAGE_BITS_ALL;
20842957Sdillon		vm_page_flag_clear(mem, PG_ZERO);
20942957Sdillon		vm_page_wakeup(mem);
2101541Srgrimes	}
2115455Sdg
2121541Srgrimes	/*
2135455Sdg	 * And finally, mark the data as non-pageable.
2141541Srgrimes	 */
21598226Salc	(void) vm_map_wire(map, addr, addr + size, FALSE);
2161541Srgrimes
2175455Sdg	return (addr);
2181541Srgrimes}
2191541Srgrimes
2201541Srgrimes/*
2211541Srgrimes *	kmem_free:
2221541Srgrimes *
2231541Srgrimes *	Release a region of kernel virtual memory allocated
2241541Srgrimes *	with kmem_alloc, and return the physical pages
2251541Srgrimes *	associated with that region.
22642957Sdillon *
22742957Sdillon *	This routine may not block on kernel maps.
2281541Srgrimes */
2298876Srgrimesvoid
2305455Sdgkmem_free(map, addr, size)
2315455Sdg	vm_map_t map;
23270480Salfred	vm_offset_t addr;
2335455Sdg	vm_size_t size;
2341541Srgrimes{
23579224Sdillon	GIANT_REQUIRED;
23671571Sjhb
2371541Srgrimes	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
2381541Srgrimes}
2391541Srgrimes
2401541Srgrimes/*
2411541Srgrimes *	kmem_suballoc:
2421541Srgrimes *
2431541Srgrimes *	Allocates a map to manage a subrange
2441541Srgrimes *	of the kernel virtual address space.
2451541Srgrimes *
2461541Srgrimes *	Arguments are as follows:
2471541Srgrimes *
2481541Srgrimes *	parent		Map to take range from
24970480Salfred *	min, max	Returned endpoints of map
2501541Srgrimes *	size		Size of range to find
2511541Srgrimes */
2528876Srgrimesvm_map_t
25332702Sdysonkmem_suballoc(parent, min, max, size)
25470478Salfred	vm_map_t parent;
2555455Sdg	vm_offset_t *min, *max;
25670478Salfred	vm_size_t size;
2571541Srgrimes{
25870478Salfred	int ret;
2595455Sdg	vm_map_t result;
2601541Srgrimes
26179224Sdillon	GIANT_REQUIRED;
26276827Salfred
2631541Srgrimes	size = round_page(size);
2641541Srgrimes
2651541Srgrimes	*min = (vm_offset_t) vm_map_min(parent);
2661541Srgrimes	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
26713490Sdyson	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
2681541Srgrimes	if (ret != KERN_SUCCESS) {
2691541Srgrimes		printf("kmem_suballoc: bad status return of %d.\n", ret);
2701541Srgrimes		panic("kmem_suballoc");
2711541Srgrimes	}
2721541Srgrimes	*max = *min + size;
27332702Sdyson	result = vm_map_create(vm_map_pmap(parent), *min, *max);
2741541Srgrimes	if (result == NULL)
2751541Srgrimes		panic("kmem_suballoc: cannot create submap");
27670478Salfred	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
2771541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2785455Sdg	return (result);
2791541Srgrimes}
2801541Srgrimes
2811541Srgrimes/*
28242957Sdillon *	kmem_malloc:
2831541Srgrimes *
28442957Sdillon * 	Allocate wired-down memory in the kernel's address map for the higher
28542957Sdillon * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
28642957Sdillon * 	kmem_alloc() because we may need to allocate memory at interrupt
28742957Sdillon * 	level where we cannot block (canwait == FALSE).
2881541Srgrimes *
28942957Sdillon * 	This routine has its own private kernel submap (kmem_map) and object
29042957Sdillon * 	(kmem_object).  This, combined with the fact that only malloc uses
29142957Sdillon * 	this routine, ensures that we will never block in map or object waits.
2921541Srgrimes *
29342957Sdillon * 	Note that this still only works in a uni-processor environment and
29442957Sdillon * 	when called at splhigh().
29542957Sdillon *
29642957Sdillon * 	We don't worry about expanding the map (adding entries) since entries
29742957Sdillon * 	for wired maps are statically allocated.
29842957Sdillon *
29942957Sdillon *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
30042957Sdillon *	I have not verified that it actually does not block.
30178592Sbmilekic *
30278592Sbmilekic *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
30378592Sbmilekic *	which we never free.
3041541Srgrimes */
3051541Srgrimesvm_offset_t
30642957Sdillonkmem_malloc(map, size, flags)
30770480Salfred	vm_map_t map;
30870480Salfred	vm_size_t size;
30942957Sdillon	int flags;
3101541Srgrimes{
31170480Salfred	vm_offset_t offset, i;
3125455Sdg	vm_map_entry_t entry;
3135455Sdg	vm_offset_t addr;
3145455Sdg	vm_page_t m;
3151541Srgrimes
31679224Sdillon	GIANT_REQUIRED;
31779224Sdillon
3181541Srgrimes	size = round_page(size);
3191541Srgrimes	addr = vm_map_min(map);
3201541Srgrimes
3211541Srgrimes	/*
3225455Sdg	 * Locate sufficient space in the map.  This will give us the final
3235455Sdg	 * virtual address for the new memory, and thus will tell us the
3245455Sdg	 * offset within the kernel map.
3251541Srgrimes	 */
3261541Srgrimes	vm_map_lock(map);
32733758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
3281541Srgrimes		vm_map_unlock(map);
32978592Sbmilekic		if (map != kmem_map) {
33087157Sluigi			static int last_report; /* when we did it (in ticks) */
33187157Sluigi			if (ticks < last_report ||
33287157Sluigi			    (ticks - last_report) >= hz) {
33387157Sluigi				last_report = ticks;
33487157Sluigi				printf("Out of mbuf address space!\n");
33587157Sluigi				printf("Consider increasing NMBCLUSTERS\n");
33687157Sluigi			}
33776827Salfred			goto bad;
3387066Sdg		}
33942957Sdillon		if ((flags & M_NOWAIT) == 0)
34048409Speter			panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
34148409Speter				(long)size, (long)map->size);
34276827Salfred		goto bad;
3431541Srgrimes	}
34415367Sdyson	offset = addr - VM_MIN_KERNEL_ADDRESS;
3451541Srgrimes	vm_object_reference(kmem_object);
34613490Sdyson	vm_map_insert(map, kmem_object, offset, addr, addr + size,
34713490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
3481541Srgrimes
3491541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
35098450Sjeff		int pflags;
35142957Sdillon		/*
35242957Sdillon		 * Note: if M_NOWAIT specified alone, allocate from
35342957Sdillon		 * interrupt-safe queues only (just the free list).  If
35481399Sjhb		 * M_USE_RESERVE is also specified, we can also
35542957Sdillon		 * allocate from the cache.  Neither of the latter two
35642957Sdillon		 * flags may be specified from an interrupt since interrupts
35742957Sdillon		 * are not allowed to mess with the cache queue.
35842957Sdillon		 */
35915809Sdysonretry:
36098450Sjeff		if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
36198450Sjeff			pflags = VM_ALLOC_INTERRUPT;
36298450Sjeff		else
36398450Sjeff			pflags = VM_ALLOC_SYSTEM;
3641541Srgrimes
36598450Sjeff		if (flags & M_ZERO)
36698450Sjeff			pflags |= VM_ALLOC_ZERO;
36798450Sjeff
36898450Sjeff		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
36998450Sjeff
3701541Srgrimes		/*
3715455Sdg		 * Ran out of space, free everything up and return. Don't need
3725455Sdg		 * to lock page queues here as we know that the pages we got
3735455Sdg		 * aren't on any queues.
3741541Srgrimes		 */
3751541Srgrimes		if (m == NULL) {
37642957Sdillon			if ((flags & M_NOWAIT) == 0) {
37744793Salc				vm_map_unlock(map);
37815809Sdyson				VM_WAIT;
37944793Salc				vm_map_lock(map);
38015809Sdyson				goto retry;
38115809Sdyson			}
38291946Stegge			/*
38391946Stegge			 * Free the pages before removing the map entry.
38491946Stegge			 * They are already marked busy.  Calling
38591946Stegge			 * vm_map_delete before the pages has been freed or
38691946Stegge			 * unbusied will cause a deadlock.
38791946Stegge			 */
38891946Stegge			while (i != 0) {
38991946Stegge				i -= PAGE_SIZE;
39091946Stegge				m = vm_page_lookup(kmem_object,
39191946Stegge						   OFF_TO_IDX(offset + i));
39291946Stegge				vm_page_free(m);
39391946Stegge			}
3941541Srgrimes			vm_map_delete(map, addr, addr + size);
3951541Srgrimes			vm_map_unlock(map);
39676827Salfred			goto bad;
3971541Srgrimes		}
39838799Sdfr		vm_page_flag_clear(m, PG_ZERO);
3996585Sdg		m->valid = VM_PAGE_BITS_ALL;
4001541Srgrimes	}
4011541Srgrimes
4021541Srgrimes	/*
4035455Sdg	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
4045455Sdg	 * be able to extend the previous entry so there will be a new entry
4055455Sdg	 * exactly corresponding to this address range and it will have
4065455Sdg	 * wired_count == 0.
4071541Srgrimes	 */
4081541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
4091541Srgrimes	    entry->start != addr || entry->end != addr + size ||
41044793Salc	    entry->wired_count != 0)
4111541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
41244793Salc	entry->wired_count = 1;
4131541Srgrimes
41420993Sdyson	vm_map_simplify_entry(map, entry);
41520993Sdyson
4161541Srgrimes	/*
4175455Sdg	 * Loop thru pages, entering them in the pmap. (We cannot add them to
4185455Sdg	 * the wired count without wrapping the vm_page_queue_lock in
4195455Sdg	 * splimp...)
4201541Srgrimes	 */
4211541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
42212767Sdyson		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
42313490Sdyson		vm_page_wire(m);
42438799Sdfr		vm_page_wakeup(m);
42542957Sdillon		/*
42642957Sdillon		 * Because this is kernel_pmap, this call will not block.
42742957Sdillon		 */
42860755Speter		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
42938799Sdfr		vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE | PG_REFERENCED);
4301541Srgrimes	}
4311541Srgrimes	vm_map_unlock(map);
4321541Srgrimes
4335455Sdg	return (addr);
43476827Salfred
43576827Salfredbad:
43676827Salfred	return (0);
4371541Srgrimes}
4381541Srgrimes
4391541Srgrimes/*
44042957Sdillon *	kmem_alloc_wait:
4411541Srgrimes *
4421541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
4431541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
4441541Srgrimes *
44542957Sdillon *	This routine may block.
4461541Srgrimes */
4478876Srgrimesvm_offset_t
4485455Sdgkmem_alloc_wait(map, size)
4495455Sdg	vm_map_t map;
4505455Sdg	vm_size_t size;
4511541Srgrimes{
4525455Sdg	vm_offset_t addr;
4531541Srgrimes
45479224Sdillon	GIANT_REQUIRED;
45576827Salfred
4561541Srgrimes	size = round_page(size);
4571541Srgrimes
4581541Srgrimes	for (;;) {
4591541Srgrimes		/*
4605455Sdg		 * To make this work for more than one map, use the map's lock
4615455Sdg		 * to lock out sleepers/wakers.
4621541Srgrimes		 */
4631541Srgrimes		vm_map_lock(map);
46433758Sdyson		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
4651541Srgrimes			break;
4661541Srgrimes		/* no space now; see if we can ever get space */
4671541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4681541Srgrimes			vm_map_unlock(map);
4691541Srgrimes			return (0);
4701541Srgrimes		}
4711541Srgrimes		vm_map_unlock(map);
47279224Sdillon		tsleep(map, PVM, "kmaw", 0);
4731541Srgrimes	}
47413490Sdyson	vm_map_insert(map, NULL, (vm_offset_t) 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
4751541Srgrimes	vm_map_unlock(map);
4761541Srgrimes	return (addr);
4771541Srgrimes}
4781541Srgrimes
4791541Srgrimes/*
48042957Sdillon *	kmem_free_wakeup:
4811541Srgrimes *
4829507Sdg *	Returns memory to a submap of the kernel, and wakes up any processes
4831541Srgrimes *	waiting for memory in that map.
4841541Srgrimes */
4858876Srgrimesvoid
4865455Sdgkmem_free_wakeup(map, addr, size)
4875455Sdg	vm_map_t map;
4885455Sdg	vm_offset_t addr;
4895455Sdg	vm_size_t size;
4901541Srgrimes{
49179224Sdillon	GIANT_REQUIRED;
49276827Salfred
4931541Srgrimes	vm_map_lock(map);
4941541Srgrimes	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
4959507Sdg	wakeup(map);
4961541Srgrimes	vm_map_unlock(map);
4971541Srgrimes}
4981541Srgrimes
4991541Srgrimes/*
50042957Sdillon * 	kmem_init:
50142957Sdillon *
50242957Sdillon *	Create the kernel map; insert a mapping covering kernel text,
50342957Sdillon *	data, bss, and all space allocated thus far (`boostrap' data).  The
50442957Sdillon *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
50542957Sdillon *	`start' as allocated, and the range between `start' and `end' as free.
5061541Srgrimes */
5078876Srgrimesvoid
5085455Sdgkmem_init(start, end)
5091541Srgrimes	vm_offset_t start, end;
5101541Srgrimes{
51170480Salfred	vm_map_t m;
5121541Srgrimes
51332702Sdyson	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
5141541Srgrimes	vm_map_lock(m);
5151541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
5161541Srgrimes	kernel_map = m;
51727899Sdyson	kernel_map->system_map = 1;
5185455Sdg	(void) vm_map_insert(m, NULL, (vm_offset_t) 0,
51913490Sdyson	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 0);
5201541Srgrimes	/* ... and ending with the completion of the above `insert' */
5211541Srgrimes	vm_map_unlock(m);
5221541Srgrimes}
523