vm_kern.c revision 99754
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 99754 2002-07-11 02:39:24Z alc $
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.
9998686Salc *
10098686Salc * MPSAFE
1011541Srgrimes */
1028876Srgrimesvm_offset_t
1035455Sdgkmem_alloc_pageable(map, size)
1045455Sdg	vm_map_t map;
10570480Salfred	vm_size_t size;
1061541Srgrimes{
1075455Sdg	vm_offset_t addr;
10870480Salfred	int result;
1091541Srgrimes
1101541Srgrimes	size = round_page(size);
1111541Srgrimes	addr = vm_map_min(map);
11298686Salc	result = vm_map_find(map, NULL, 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.
12498686Salc *
12598686Salc * MPSAFE
12647841Sdt */
12747841Sdtvm_offset_t
12847841Sdtkmem_alloc_nofault(map, size)
12947841Sdt	vm_map_t map;
13070480Salfred	vm_size_t size;
13147841Sdt{
13247841Sdt	vm_offset_t addr;
13370480Salfred	int result;
13447841Sdt
13547841Sdt	size = round_page(size);
13647841Sdt	addr = vm_map_min(map);
13798686Salc	result = vm_map_find(map, NULL, 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.
22898686Salc *
22998686Salc * MPSAFE
2301541Srgrimes */
2318876Srgrimesvoid
2325455Sdgkmem_free(map, addr, size)
2335455Sdg	vm_map_t map;
23470480Salfred	vm_offset_t addr;
2355455Sdg	vm_size_t size;
2361541Srgrimes{
23771571Sjhb
2381541Srgrimes	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
2391541Srgrimes}
2401541Srgrimes
2411541Srgrimes/*
2421541Srgrimes *	kmem_suballoc:
2431541Srgrimes *
2441541Srgrimes *	Allocates a map to manage a subrange
2451541Srgrimes *	of the kernel virtual address space.
2461541Srgrimes *
2471541Srgrimes *	Arguments are as follows:
2481541Srgrimes *
2491541Srgrimes *	parent		Map to take range from
25070480Salfred *	min, max	Returned endpoints of map
2511541Srgrimes *	size		Size of range to find
2521541Srgrimes */
2538876Srgrimesvm_map_t
25432702Sdysonkmem_suballoc(parent, min, max, size)
25570478Salfred	vm_map_t parent;
2565455Sdg	vm_offset_t *min, *max;
25770478Salfred	vm_size_t size;
2581541Srgrimes{
25970478Salfred	int ret;
2605455Sdg	vm_map_t result;
2611541Srgrimes
26279224Sdillon	GIANT_REQUIRED;
26376827Salfred
2641541Srgrimes	size = round_page(size);
2651541Srgrimes
2661541Srgrimes	*min = (vm_offset_t) vm_map_min(parent);
2671541Srgrimes	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
26813490Sdyson	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
2691541Srgrimes	if (ret != KERN_SUCCESS) {
2701541Srgrimes		printf("kmem_suballoc: bad status return of %d.\n", ret);
2711541Srgrimes		panic("kmem_suballoc");
2721541Srgrimes	}
2731541Srgrimes	*max = *min + size;
27432702Sdyson	result = vm_map_create(vm_map_pmap(parent), *min, *max);
2751541Srgrimes	if (result == NULL)
2761541Srgrimes		panic("kmem_suballoc: cannot create submap");
27770478Salfred	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
2781541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2795455Sdg	return (result);
2801541Srgrimes}
2811541Srgrimes
2821541Srgrimes/*
28342957Sdillon *	kmem_malloc:
2841541Srgrimes *
28542957Sdillon * 	Allocate wired-down memory in the kernel's address map for the higher
28642957Sdillon * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
28742957Sdillon * 	kmem_alloc() because we may need to allocate memory at interrupt
28842957Sdillon * 	level where we cannot block (canwait == FALSE).
2891541Srgrimes *
29042957Sdillon * 	This routine has its own private kernel submap (kmem_map) and object
29142957Sdillon * 	(kmem_object).  This, combined with the fact that only malloc uses
29242957Sdillon * 	this routine, ensures that we will never block in map or object waits.
2931541Srgrimes *
29442957Sdillon * 	Note that this still only works in a uni-processor environment and
29542957Sdillon * 	when called at splhigh().
29642957Sdillon *
29742957Sdillon * 	We don't worry about expanding the map (adding entries) since entries
29842957Sdillon * 	for wired maps are statically allocated.
29942957Sdillon *
30042957Sdillon *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
30142957Sdillon *	I have not verified that it actually does not block.
30278592Sbmilekic *
30378592Sbmilekic *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
30478592Sbmilekic *	which we never free.
3051541Srgrimes */
3061541Srgrimesvm_offset_t
30742957Sdillonkmem_malloc(map, size, flags)
30870480Salfred	vm_map_t map;
30970480Salfred	vm_size_t size;
31042957Sdillon	int flags;
3111541Srgrimes{
31270480Salfred	vm_offset_t offset, i;
3135455Sdg	vm_map_entry_t entry;
3145455Sdg	vm_offset_t addr;
3155455Sdg	vm_page_t m;
31698455Sjeff	int pflags;
3171541Srgrimes
31879224Sdillon	GIANT_REQUIRED;
31979224Sdillon
3201541Srgrimes	size = round_page(size);
3211541Srgrimes	addr = vm_map_min(map);
3221541Srgrimes
3231541Srgrimes	/*
3245455Sdg	 * Locate sufficient space in the map.  This will give us the final
3255455Sdg	 * virtual address for the new memory, and thus will tell us the
3265455Sdg	 * offset within the kernel map.
3271541Srgrimes	 */
3281541Srgrimes	vm_map_lock(map);
32933758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
3301541Srgrimes		vm_map_unlock(map);
33178592Sbmilekic		if (map != kmem_map) {
33287157Sluigi			static int last_report; /* when we did it (in ticks) */
33387157Sluigi			if (ticks < last_report ||
33487157Sluigi			    (ticks - last_report) >= hz) {
33587157Sluigi				last_report = ticks;
33687157Sluigi				printf("Out of mbuf address space!\n");
33787157Sluigi				printf("Consider increasing NMBCLUSTERS\n");
33887157Sluigi			}
33976827Salfred			goto bad;
3407066Sdg		}
34142957Sdillon		if ((flags & M_NOWAIT) == 0)
34248409Speter			panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
34348409Speter				(long)size, (long)map->size);
34476827Salfred		goto bad;
3451541Srgrimes	}
34615367Sdyson	offset = addr - VM_MIN_KERNEL_ADDRESS;
3471541Srgrimes	vm_object_reference(kmem_object);
34813490Sdyson	vm_map_insert(map, kmem_object, offset, addr, addr + size,
34913490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
3501541Srgrimes
35198455Sjeff	/*
35298455Sjeff	 * Note: if M_NOWAIT specified alone, allocate from
35398455Sjeff	 * interrupt-safe queues only (just the free list).  If
35498455Sjeff	 * M_USE_RESERVE is also specified, we can also
35598455Sjeff	 * allocate from the cache.  Neither of the latter two
35698455Sjeff	 * flags may be specified from an interrupt since interrupts
35798455Sjeff	 * are not allowed to mess with the cache queue.
35898455Sjeff	 */
35998455Sjeff
36098455Sjeff	if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
36198455Sjeff		pflags = VM_ALLOC_INTERRUPT;
36298455Sjeff	else
36398455Sjeff		pflags = VM_ALLOC_SYSTEM;
36498455Sjeff
36598455Sjeff	if (flags & M_ZERO)
36698455Sjeff		pflags |= VM_ALLOC_ZERO;
36798455Sjeff
36898455Sjeff
3691541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
37015809Sdysonretry:
37198450Sjeff		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
37298450Sjeff
3731541Srgrimes		/*
3745455Sdg		 * Ran out of space, free everything up and return. Don't need
3755455Sdg		 * to lock page queues here as we know that the pages we got
3765455Sdg		 * aren't on any queues.
3771541Srgrimes		 */
3781541Srgrimes		if (m == NULL) {
37942957Sdillon			if ((flags & M_NOWAIT) == 0) {
38044793Salc				vm_map_unlock(map);
38115809Sdyson				VM_WAIT;
38244793Salc				vm_map_lock(map);
38315809Sdyson				goto retry;
38415809Sdyson			}
38591946Stegge			/*
38691946Stegge			 * Free the pages before removing the map entry.
38791946Stegge			 * They are already marked busy.  Calling
38891946Stegge			 * vm_map_delete before the pages has been freed or
38991946Stegge			 * unbusied will cause a deadlock.
39091946Stegge			 */
39191946Stegge			while (i != 0) {
39291946Stegge				i -= PAGE_SIZE;
39391946Stegge				m = vm_page_lookup(kmem_object,
39491946Stegge						   OFF_TO_IDX(offset + i));
39591946Stegge				vm_page_free(m);
39691946Stegge			}
3971541Srgrimes			vm_map_delete(map, addr, addr + size);
3981541Srgrimes			vm_map_unlock(map);
39976827Salfred			goto bad;
4001541Srgrimes		}
40198455Sjeff		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
40298455Sjeff			vm_page_zero_fill(m);
40338799Sdfr		vm_page_flag_clear(m, PG_ZERO);
4046585Sdg		m->valid = VM_PAGE_BITS_ALL;
4051541Srgrimes	}
4061541Srgrimes
4071541Srgrimes	/*
4085455Sdg	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
4095455Sdg	 * be able to extend the previous entry so there will be a new entry
4105455Sdg	 * exactly corresponding to this address range and it will have
4115455Sdg	 * wired_count == 0.
4121541Srgrimes	 */
4131541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
4141541Srgrimes	    entry->start != addr || entry->end != addr + size ||
41544793Salc	    entry->wired_count != 0)
4161541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
41744793Salc	entry->wired_count = 1;
4181541Srgrimes
41920993Sdyson	vm_map_simplify_entry(map, entry);
42020993Sdyson
4211541Srgrimes	/*
4225455Sdg	 * Loop thru pages, entering them in the pmap. (We cannot add them to
4235455Sdg	 * the wired count without wrapping the vm_page_queue_lock in
4245455Sdg	 * splimp...)
4251541Srgrimes	 */
4261541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
42712767Sdyson		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
42813490Sdyson		vm_page_wire(m);
42938799Sdfr		vm_page_wakeup(m);
43042957Sdillon		/*
43142957Sdillon		 * Because this is kernel_pmap, this call will not block.
43242957Sdillon		 */
43360755Speter		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
43438799Sdfr		vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE | PG_REFERENCED);
4351541Srgrimes	}
4361541Srgrimes	vm_map_unlock(map);
4371541Srgrimes
4385455Sdg	return (addr);
43976827Salfred
44076827Salfredbad:
44176827Salfred	return (0);
4421541Srgrimes}
4431541Srgrimes
4441541Srgrimes/*
44542957Sdillon *	kmem_alloc_wait:
4461541Srgrimes *
4471541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
4481541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
4491541Srgrimes *
45042957Sdillon *	This routine may block.
4511541Srgrimes */
4528876Srgrimesvm_offset_t
4535455Sdgkmem_alloc_wait(map, size)
4545455Sdg	vm_map_t map;
4555455Sdg	vm_size_t size;
4561541Srgrimes{
4575455Sdg	vm_offset_t addr;
4581541Srgrimes
45979224Sdillon	GIANT_REQUIRED;
46076827Salfred
4611541Srgrimes	size = round_page(size);
4621541Srgrimes
4631541Srgrimes	for (;;) {
4641541Srgrimes		/*
4655455Sdg		 * To make this work for more than one map, use the map's lock
4665455Sdg		 * to lock out sleepers/wakers.
4671541Srgrimes		 */
4681541Srgrimes		vm_map_lock(map);
46933758Sdyson		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
4701541Srgrimes			break;
4711541Srgrimes		/* no space now; see if we can ever get space */
4721541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4731541Srgrimes			vm_map_unlock(map);
4741541Srgrimes			return (0);
4751541Srgrimes		}
47699754Salc		map->needs_wakeup = TRUE;
47799754Salc		vm_map_unlock_and_wait(map, FALSE);
4781541Srgrimes	}
47999754Salc	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
4801541Srgrimes	vm_map_unlock(map);
4811541Srgrimes	return (addr);
4821541Srgrimes}
4831541Srgrimes
4841541Srgrimes/*
48542957Sdillon *	kmem_free_wakeup:
4861541Srgrimes *
4879507Sdg *	Returns memory to a submap of the kernel, and wakes up any processes
4881541Srgrimes *	waiting for memory in that map.
4891541Srgrimes */
4908876Srgrimesvoid
4915455Sdgkmem_free_wakeup(map, addr, size)
4925455Sdg	vm_map_t map;
4935455Sdg	vm_offset_t addr;
4945455Sdg	vm_size_t size;
4951541Srgrimes{
49679224Sdillon	GIANT_REQUIRED;
49776827Salfred
4981541Srgrimes	vm_map_lock(map);
4991541Srgrimes	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
50099754Salc	if (map->needs_wakeup) {
50199754Salc		map->needs_wakeup = FALSE;
50299754Salc		vm_map_wakeup(map);
50399754Salc	}
5041541Srgrimes	vm_map_unlock(map);
5051541Srgrimes}
5061541Srgrimes
5071541Srgrimes/*
50842957Sdillon * 	kmem_init:
50942957Sdillon *
51042957Sdillon *	Create the kernel map; insert a mapping covering kernel text,
51142957Sdillon *	data, bss, and all space allocated thus far (`boostrap' data).  The
51242957Sdillon *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
51342957Sdillon *	`start' as allocated, and the range between `start' and `end' as free.
5141541Srgrimes */
5158876Srgrimesvoid
5165455Sdgkmem_init(start, end)
5171541Srgrimes	vm_offset_t start, end;
5181541Srgrimes{
51970480Salfred	vm_map_t m;
5201541Srgrimes
52132702Sdyson	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
5221541Srgrimes	vm_map_lock(m);
5231541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
5241541Srgrimes	kernel_map = m;
52527899Sdyson	kernel_map->system_map = 1;
5265455Sdg	(void) vm_map_insert(m, NULL, (vm_offset_t) 0,
52713490Sdyson	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 0);
5281541Srgrimes	/* ... and ending with the completion of the above `insert' */
5291541Srgrimes	vm_map_unlock(m);
5301541Srgrimes}
531