vm_kern.c revision 177762
1139825Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
171541Srgrimes *    may be used to endorse or promote products derived from this software
181541Srgrimes *    without specific prior written permission.
191541Srgrimes *
201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301541Srgrimes * SUCH DAMAGE.
311541Srgrimes *
321817Sdg *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
331541Srgrimes *
341541Srgrimes *
351541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University.
361541Srgrimes * All rights reserved.
371541Srgrimes *
381541Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young
395455Sdg *
401541Srgrimes * Permission to use, copy, modify and distribute this software and
411541Srgrimes * its documentation is hereby granted, provided that both the copyright
421541Srgrimes * notice and this permission notice appear in all copies of the
431541Srgrimes * software, derivative works or modified versions, and any portions
441541Srgrimes * thereof, and that both notices appear in supporting documentation.
455455Sdg *
465455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
475455Sdg * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
481541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
495455Sdg *
501541Srgrimes * Carnegie Mellon requests users of this software to return to
511541Srgrimes *
521541Srgrimes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
531541Srgrimes *  School of Computer Science
541541Srgrimes *  Carnegie Mellon University
551541Srgrimes *  Pittsburgh PA 15213-3890
561541Srgrimes *
571541Srgrimes * any improvements or extensions that they make and grant Carnegie the
581541Srgrimes * rights to redistribute these changes.
591541Srgrimes */
601541Srgrimes
611541Srgrimes/*
621541Srgrimes *	Kernel memory management.
631541Srgrimes */
641541Srgrimes
65116226Sobrien#include <sys/cdefs.h>
66116226Sobrien__FBSDID("$FreeBSD: head/sys/vm/vm_kern.c 177762 2008-03-30 20:08:59Z alc $");
67116226Sobrien
681541Srgrimes#include <sys/param.h>
691541Srgrimes#include <sys/systm.h>
7087157Sluigi#include <sys/kernel.h>		/* for ticks and hz */
71168395Spjd#include <sys/eventhandler.h>
7276166Smarkm#include <sys/lock.h>
7376166Smarkm#include <sys/mutex.h>
742112Swollman#include <sys/proc.h>
756129Sdg#include <sys/malloc.h>
761541Srgrimes
771541Srgrimes#include <vm/vm.h>
7812662Sdg#include <vm/vm_param.h>
7912662Sdg#include <vm/pmap.h>
8012662Sdg#include <vm/vm_map.h>
8112662Sdg#include <vm/vm_object.h>
821541Srgrimes#include <vm/vm_page.h>
831541Srgrimes#include <vm/vm_pageout.h>
8412726Sbde#include <vm/vm_extern.h>
85168395Spjd#include <vm/uma.h>
861541Srgrimes
8719830Sdysonvm_map_t kernel_map=0;
8819830Sdysonvm_map_t kmem_map=0;
8919830Sdysonvm_map_t exec_map=0;
90118764Ssilbyvm_map_t pipe_map;
9119830Sdysonvm_map_t buffer_map=0;
922112Swollman
931541Srgrimes/*
9447841Sdt *	kmem_alloc_nofault:
9547841Sdt *
96118317Salc *	Allocate a virtual address range with no underlying object and
97118317Salc *	no initial mapping to physical memory.  Any mapping from this
98118317Salc *	range to physical memory must be explicitly created prior to
99118317Salc *	its use, typically with pmap_qenter().  Any attempt to create
100118317Salc *	a mapping on demand through vm_fault() will result in a panic.
10147841Sdt */
10247841Sdtvm_offset_t
10347841Sdtkmem_alloc_nofault(map, size)
10447841Sdt	vm_map_t map;
10570480Salfred	vm_size_t size;
10647841Sdt{
10747841Sdt	vm_offset_t addr;
10870480Salfred	int result;
10947841Sdt
11047841Sdt	size = round_page(size);
11147841Sdt	addr = vm_map_min(map);
11298686Salc	result = vm_map_find(map, NULL, 0,
11347841Sdt	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
11447841Sdt	if (result != KERN_SUCCESS) {
11547841Sdt		return (0);
11647841Sdt	}
11747841Sdt	return (addr);
11847841Sdt}
11947841Sdt
12047841Sdt/*
1211541Srgrimes *	Allocate wired-down memory in the kernel's address map
1221541Srgrimes *	or a submap.
1231541Srgrimes */
1248876Srgrimesvm_offset_t
1255455Sdgkmem_alloc(map, size)
12670480Salfred	vm_map_t map;
12770480Salfred	vm_size_t size;
1281541Srgrimes{
1295455Sdg	vm_offset_t addr;
13070480Salfred	vm_offset_t offset;
1315455Sdg	vm_offset_t i;
1321541Srgrimes
1331541Srgrimes	size = round_page(size);
1341541Srgrimes
1351541Srgrimes	/*
1365455Sdg	 * Use the kernel object for wired-down kernel pages. Assume that no
1375455Sdg	 * region of the kernel object is referenced more than once.
1381541Srgrimes	 */
1391541Srgrimes
1401541Srgrimes	/*
1415455Sdg	 * Locate sufficient space in the map.  This will give us the final
1425455Sdg	 * virtual address for the new memory, and thus will tell us the
1435455Sdg	 * offset within the kernel map.
1441541Srgrimes	 */
1451541Srgrimes	vm_map_lock(map);
14633758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
1471541Srgrimes		vm_map_unlock(map);
1481541Srgrimes		return (0);
1491541Srgrimes	}
1501541Srgrimes	offset = addr - VM_MIN_KERNEL_ADDRESS;
1511541Srgrimes	vm_object_reference(kernel_object);
15213490Sdyson	vm_map_insert(map, kernel_object, offset, addr, addr + size,
15313490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
1541541Srgrimes	vm_map_unlock(map);
1551541Srgrimes
1561541Srgrimes	/*
1575455Sdg	 * Guarantee that there are pages already in this object before
158122383Smini	 * calling vm_map_wire.  This is to prevent the following
1595455Sdg	 * scenario:
1608876Srgrimes	 *
1615455Sdg	 * 1) Threads have swapped out, so that there is a pager for the
1625455Sdg	 * kernel_object. 2) The kmsg zone is empty, and so we are
163122383Smini	 * kmem_allocing a new page for it. 3) vm_map_wire calls vm_fault;
1645455Sdg	 * there is no page, but there is a pager, so we call
1655455Sdg	 * pager_data_request.  But the kmsg zone is empty, so we must
1665455Sdg	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
1675455Sdg	 * we get the data back from the pager, it will be (very stale)
1685455Sdg	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
1698876Srgrimes	 *
1705455Sdg	 * We're intentionally not activating the pages we allocate to prevent a
171122383Smini	 * race with page-out.  vm_map_wire will wire the pages.
1721541Srgrimes	 */
173120761Salc	VM_OBJECT_LOCK(kernel_object);
1745455Sdg	for (i = 0; i < size; i += PAGE_SIZE) {
1755455Sdg		vm_page_t mem;
1761541Srgrimes
17733109Sdyson		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
178136923Salc		    VM_ALLOC_NOBUSY | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
179120761Salc		mem->valid = VM_PAGE_BITS_ALL;
180166964Salc		KASSERT((mem->flags & PG_UNMANAGED) != 0,
181166964Salc		    ("kmem_alloc: page %p is managed", mem));
1821541Srgrimes	}
183120761Salc	VM_OBJECT_UNLOCK(kernel_object);
1845455Sdg
1851541Srgrimes	/*
1865455Sdg	 * And finally, mark the data as non-pageable.
1871541Srgrimes	 */
188118771Sbms	(void) vm_map_wire(map, addr, addr + size,
189118771Sbms	    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
1901541Srgrimes
1915455Sdg	return (addr);
1921541Srgrimes}
1931541Srgrimes
1941541Srgrimes/*
1951541Srgrimes *	kmem_free:
1961541Srgrimes *
1971541Srgrimes *	Release a region of kernel virtual memory allocated
1981541Srgrimes *	with kmem_alloc, and return the physical pages
1991541Srgrimes *	associated with that region.
20042957Sdillon *
20142957Sdillon *	This routine may not block on kernel maps.
2021541Srgrimes */
2038876Srgrimesvoid
2045455Sdgkmem_free(map, addr, size)
2055455Sdg	vm_map_t map;
20670480Salfred	vm_offset_t addr;
2075455Sdg	vm_size_t size;
2081541Srgrimes{
20971571Sjhb
2101541Srgrimes	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
2111541Srgrimes}
2121541Srgrimes
2131541Srgrimes/*
2141541Srgrimes *	kmem_suballoc:
2151541Srgrimes *
2161541Srgrimes *	Allocates a map to manage a subrange
2171541Srgrimes *	of the kernel virtual address space.
2181541Srgrimes *
2191541Srgrimes *	Arguments are as follows:
2201541Srgrimes *
2211541Srgrimes *	parent		Map to take range from
22270480Salfred *	min, max	Returned endpoints of map
2231541Srgrimes *	size		Size of range to find
2241541Srgrimes */
2258876Srgrimesvm_map_t
22632702Sdysonkmem_suballoc(parent, min, max, size)
22770478Salfred	vm_map_t parent;
2285455Sdg	vm_offset_t *min, *max;
22970478Salfred	vm_size_t size;
2301541Srgrimes{
23170478Salfred	int ret;
2325455Sdg	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,
23813490Sdyson	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
239177762Salc	if (ret != KERN_SUCCESS)
240177762Salc		panic("kmem_suballoc: bad status return of %d", ret);
2411541Srgrimes	*max = *min + size;
24232702Sdyson	result = vm_map_create(vm_map_pmap(parent), *min, *max);
2431541Srgrimes	if (result == NULL)
2441541Srgrimes		panic("kmem_suballoc: cannot create submap");
24570478Salfred	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
2461541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2475455Sdg	return (result);
2481541Srgrimes}
2491541Srgrimes
2501541Srgrimes/*
25142957Sdillon *	kmem_malloc:
2521541Srgrimes *
25342957Sdillon * 	Allocate wired-down memory in the kernel's address map for the higher
25442957Sdillon * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
25542957Sdillon * 	kmem_alloc() because we may need to allocate memory at interrupt
25642957Sdillon * 	level where we cannot block (canwait == FALSE).
2571541Srgrimes *
25842957Sdillon * 	This routine has its own private kernel submap (kmem_map) and object
25942957Sdillon * 	(kmem_object).  This, combined with the fact that only malloc uses
26042957Sdillon * 	this routine, ensures that we will never block in map or object waits.
2611541Srgrimes *
26242957Sdillon * 	Note that this still only works in a uni-processor environment and
26342957Sdillon * 	when called at splhigh().
26442957Sdillon *
26542957Sdillon * 	We don't worry about expanding the map (adding entries) since entries
26642957Sdillon * 	for wired maps are statically allocated.
26742957Sdillon *
26842957Sdillon *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
26942957Sdillon *	I have not verified that it actually does not block.
27078592Sbmilekic *
27178592Sbmilekic *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
27278592Sbmilekic *	which we never free.
2731541Srgrimes */
2741541Srgrimesvm_offset_t
27542957Sdillonkmem_malloc(map, size, flags)
27670480Salfred	vm_map_t map;
27770480Salfred	vm_size_t size;
27842957Sdillon	int flags;
2791541Srgrimes{
28070480Salfred	vm_offset_t offset, i;
2815455Sdg	vm_map_entry_t entry;
2825455Sdg	vm_offset_t addr;
2835455Sdg	vm_page_t m;
28498455Sjeff	int pflags;
2851541Srgrimes
2861541Srgrimes	size = round_page(size);
2871541Srgrimes	addr = vm_map_min(map);
2881541Srgrimes
2891541Srgrimes	/*
2905455Sdg	 * Locate sufficient space in the map.  This will give us the final
2915455Sdg	 * virtual address for the new memory, and thus will tell us the
2925455Sdg	 * offset within the kernel map.
2931541Srgrimes	 */
2941541Srgrimes	vm_map_lock(map);
29533758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
2961541Srgrimes		vm_map_unlock(map);
297175210Spjd                if ((flags & M_NOWAIT) == 0) {
298175210Spjd			for (i = 0; i < 8; i++) {
299175210Spjd				EVENTHANDLER_INVOKE(vm_lowmem, 0);
300175210Spjd				uma_reclaim();
301175210Spjd				vm_map_lock(map);
302175210Spjd				if (vm_map_findspace(map, vm_map_min(map),
303175210Spjd				    size, &addr) == 0) {
304175210Spjd					break;
305175210Spjd				}
306168395Spjd				vm_map_unlock(map);
307175210Spjd				tsleep(&i, 0, "nokva", (hz / 4) * (i + 1));
308175210Spjd			}
309175210Spjd			if (i == 8) {
310168395Spjd				panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
311175210Spjd				    (long)size, (long)map->size);
312168395Spjd			}
313168395Spjd		} else {
314168395Spjd			return (0);
315168395Spjd		}
3161541Srgrimes	}
31715367Sdyson	offset = addr - VM_MIN_KERNEL_ADDRESS;
3181541Srgrimes	vm_object_reference(kmem_object);
31913490Sdyson	vm_map_insert(map, kmem_object, offset, addr, addr + size,
32013490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
3211541Srgrimes
32298455Sjeff	/*
32398455Sjeff	 * Note: if M_NOWAIT specified alone, allocate from
32498455Sjeff	 * interrupt-safe queues only (just the free list).  If
32598455Sjeff	 * M_USE_RESERVE is also specified, we can also
32698455Sjeff	 * allocate from the cache.  Neither of the latter two
32798455Sjeff	 * flags may be specified from an interrupt since interrupts
32898455Sjeff	 * are not allowed to mess with the cache queue.
32998455Sjeff	 */
33098455Sjeff
33198455Sjeff	if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
332108351Salc		pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
33398455Sjeff	else
334108351Salc		pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
33598455Sjeff
33698455Sjeff	if (flags & M_ZERO)
33798455Sjeff		pflags |= VM_ALLOC_ZERO;
33898455Sjeff
339113489Salc	VM_OBJECT_LOCK(kmem_object);
3401541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
34115809Sdysonretry:
34298450Sjeff		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
34398450Sjeff
3441541Srgrimes		/*
3455455Sdg		 * Ran out of space, free everything up and return. Don't need
3465455Sdg		 * to lock page queues here as we know that the pages we got
3475455Sdg		 * aren't on any queues.
3481541Srgrimes		 */
3491541Srgrimes		if (m == NULL) {
35042957Sdillon			if ((flags & M_NOWAIT) == 0) {
351113489Salc				VM_OBJECT_UNLOCK(kmem_object);
35244793Salc				vm_map_unlock(map);
35315809Sdyson				VM_WAIT;
35444793Salc				vm_map_lock(map);
355113489Salc				VM_OBJECT_LOCK(kmem_object);
35615809Sdyson				goto retry;
35715809Sdyson			}
35891946Stegge			/*
35991946Stegge			 * Free the pages before removing the map entry.
36091946Stegge			 * They are already marked busy.  Calling
36191946Stegge			 * vm_map_delete before the pages has been freed or
36291946Stegge			 * unbusied will cause a deadlock.
36391946Stegge			 */
36491946Stegge			while (i != 0) {
36591946Stegge				i -= PAGE_SIZE;
36691946Stegge				m = vm_page_lookup(kmem_object,
36791946Stegge						   OFF_TO_IDX(offset + i));
368100796Salc				vm_page_lock_queues();
369108351Salc				vm_page_unwire(m, 0);
37091946Stegge				vm_page_free(m);
371100796Salc				vm_page_unlock_queues();
37291946Stegge			}
373113489Salc			VM_OBJECT_UNLOCK(kmem_object);
3741541Srgrimes			vm_map_delete(map, addr, addr + size);
3751541Srgrimes			vm_map_unlock(map);
376113418Salc			return (0);
3771541Srgrimes		}
37898455Sjeff		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
379102382Salc			pmap_zero_page(m);
380120761Salc		m->valid = VM_PAGE_BITS_ALL;
381166964Salc		KASSERT((m->flags & PG_UNMANAGED) != 0,
382166964Salc		    ("kmem_malloc: page %p is managed", m));
3831541Srgrimes	}
384113489Salc	VM_OBJECT_UNLOCK(kmem_object);
3851541Srgrimes
3861541Srgrimes	/*
3875455Sdg	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
3885455Sdg	 * be able to extend the previous entry so there will be a new entry
3895455Sdg	 * exactly corresponding to this address range and it will have
3905455Sdg	 * wired_count == 0.
3911541Srgrimes	 */
3921541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
3931541Srgrimes	    entry->start != addr || entry->end != addr + size ||
39444793Salc	    entry->wired_count != 0)
3951541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
39644793Salc	entry->wired_count = 1;
3971541Srgrimes
398124048Salc	/*
399124048Salc	 * At this point, the kmem_object must be unlocked because
400124048Salc	 * vm_map_simplify_entry() calls vm_object_deallocate(), which
401124048Salc	 * locks the kmem_object.
402124048Salc	 */
40320993Sdyson	vm_map_simplify_entry(map, entry);
40420993Sdyson
4051541Srgrimes	/*
406164234Salc	 * Loop thru pages, entering them in the pmap.
4071541Srgrimes	 */
408124048Salc	VM_OBJECT_LOCK(kmem_object);
4091541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
41012767Sdyson		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
41142957Sdillon		/*
41242957Sdillon		 * Because this is kernel_pmap, this call will not block.
41342957Sdillon		 */
414175067Salc		pmap_enter(kernel_pmap, addr + i, VM_PROT_ALL, m, VM_PROT_ALL,
415175067Salc		    TRUE);
416108351Salc		vm_page_wakeup(m);
4171541Srgrimes	}
418124048Salc	VM_OBJECT_UNLOCK(kmem_object);
4191541Srgrimes	vm_map_unlock(map);
4201541Srgrimes
4215455Sdg	return (addr);
4221541Srgrimes}
4231541Srgrimes
4241541Srgrimes/*
42542957Sdillon *	kmem_alloc_wait:
4261541Srgrimes *
4271541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
4281541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
4291541Srgrimes *
43042957Sdillon *	This routine may block.
4311541Srgrimes */
4328876Srgrimesvm_offset_t
4335455Sdgkmem_alloc_wait(map, size)
4345455Sdg	vm_map_t map;
4355455Sdg	vm_size_t size;
4361541Srgrimes{
4375455Sdg	vm_offset_t addr;
4381541Srgrimes
4391541Srgrimes	size = round_page(size);
4401541Srgrimes
4411541Srgrimes	for (;;) {
4421541Srgrimes		/*
4435455Sdg		 * To make this work for more than one map, use the map's lock
4445455Sdg		 * to lock out sleepers/wakers.
4451541Srgrimes		 */
4461541Srgrimes		vm_map_lock(map);
44733758Sdyson		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
4481541Srgrimes			break;
4491541Srgrimes		/* no space now; see if we can ever get space */
4501541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4511541Srgrimes			vm_map_unlock(map);
4521541Srgrimes			return (0);
4531541Srgrimes		}
45499754Salc		map->needs_wakeup = TRUE;
455173429Spjd		vm_map_unlock_and_wait(map, 0);
4561541Srgrimes	}
45799754Salc	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
4581541Srgrimes	vm_map_unlock(map);
4591541Srgrimes	return (addr);
4601541Srgrimes}
4611541Srgrimes
4621541Srgrimes/*
46342957Sdillon *	kmem_free_wakeup:
4641541Srgrimes *
4659507Sdg *	Returns memory to a submap of the kernel, and wakes up any processes
4661541Srgrimes *	waiting for memory in that map.
4671541Srgrimes */
4688876Srgrimesvoid
4695455Sdgkmem_free_wakeup(map, addr, size)
4705455Sdg	vm_map_t map;
4715455Sdg	vm_offset_t addr;
4725455Sdg	vm_size_t size;
4731541Srgrimes{
47476827Salfred
4751541Srgrimes	vm_map_lock(map);
4761541Srgrimes	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
47799754Salc	if (map->needs_wakeup) {
47899754Salc		map->needs_wakeup = FALSE;
47999754Salc		vm_map_wakeup(map);
48099754Salc	}
4811541Srgrimes	vm_map_unlock(map);
4821541Srgrimes}
4831541Srgrimes
4841541Srgrimes/*
48542957Sdillon * 	kmem_init:
48642957Sdillon *
48742957Sdillon *	Create the kernel map; insert a mapping covering kernel text,
48842957Sdillon *	data, bss, and all space allocated thus far (`boostrap' data).  The
48942957Sdillon *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
49042957Sdillon *	`start' as allocated, and the range between `start' and `end' as free.
4911541Srgrimes */
4928876Srgrimesvoid
4935455Sdgkmem_init(start, end)
4941541Srgrimes	vm_offset_t start, end;
4951541Srgrimes{
49670480Salfred	vm_map_t m;
4971541Srgrimes
49832702Sdyson	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
499108426Salc	m->system_map = 1;
5001541Srgrimes	vm_map_lock(m);
5011541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
5021541Srgrimes	kernel_map = m;
503108426Salc	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
504165854Salc	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL,
505165854Salc	    MAP_NOFAULT);
5061541Srgrimes	/* ... and ending with the completion of the above `insert' */
5071541Srgrimes	vm_map_unlock(m);
5081541Srgrimes}
509