vm_kern.c revision 168395
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 168395 2007-04-05 20:52:51Z pjd $");
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);
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;
24432702Sdyson	result = vm_map_create(vm_map_pmap(parent), *min, *max);
2451541Srgrimes	if (result == NULL)
2461541Srgrimes		panic("kmem_suballoc: cannot create submap");
24770478Salfred	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
2481541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2495455Sdg	return (result);
2501541Srgrimes}
2511541Srgrimes
2521541Srgrimes/*
25342957Sdillon *	kmem_malloc:
2541541Srgrimes *
25542957Sdillon * 	Allocate wired-down memory in the kernel's address map for the higher
25642957Sdillon * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
25742957Sdillon * 	kmem_alloc() because we may need to allocate memory at interrupt
25842957Sdillon * 	level where we cannot block (canwait == FALSE).
2591541Srgrimes *
26042957Sdillon * 	This routine has its own private kernel submap (kmem_map) and object
26142957Sdillon * 	(kmem_object).  This, combined with the fact that only malloc uses
26242957Sdillon * 	this routine, ensures that we will never block in map or object waits.
2631541Srgrimes *
26442957Sdillon * 	Note that this still only works in a uni-processor environment and
26542957Sdillon * 	when called at splhigh().
26642957Sdillon *
26742957Sdillon * 	We don't worry about expanding the map (adding entries) since entries
26842957Sdillon * 	for wired maps are statically allocated.
26942957Sdillon *
27042957Sdillon *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
27142957Sdillon *	I have not verified that it actually does not block.
27278592Sbmilekic *
27378592Sbmilekic *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
27478592Sbmilekic *	which we never free.
2751541Srgrimes */
2761541Srgrimesvm_offset_t
27742957Sdillonkmem_malloc(map, size, flags)
27870480Salfred	vm_map_t map;
27970480Salfred	vm_size_t size;
28042957Sdillon	int flags;
2811541Srgrimes{
28270480Salfred	vm_offset_t offset, i;
2835455Sdg	vm_map_entry_t entry;
2845455Sdg	vm_offset_t addr;
2855455Sdg	vm_page_t m;
28698455Sjeff	int pflags;
2871541Srgrimes
2881541Srgrimes	size = round_page(size);
2891541Srgrimes	addr = vm_map_min(map);
2901541Srgrimes
2911541Srgrimes	/*
2925455Sdg	 * Locate sufficient space in the map.  This will give us the final
2935455Sdg	 * virtual address for the new memory, and thus will tell us the
2945455Sdg	 * offset within the kernel map.
2951541Srgrimes	 */
2961541Srgrimes	vm_map_lock(map);
29733758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
2981541Srgrimes		vm_map_unlock(map);
299168395Spjd		if ((flags & M_NOWAIT) == 0) {
300168395Spjd			EVENTHANDLER_INVOKE(vm_lowmem, 0);
301168395Spjd			uma_reclaim();
302168395Spjd			vm_map_lock(map);
303168395Spjd			if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
304168395Spjd				vm_map_unlock(map);
305168395Spjd				panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
306168395Spjd					(long)size, (long)map->size);
307168395Spjd			}
308168395Spjd		} else {
309168395Spjd			return (0);
310168395Spjd		}
3111541Srgrimes	}
31215367Sdyson	offset = addr - VM_MIN_KERNEL_ADDRESS;
3131541Srgrimes	vm_object_reference(kmem_object);
31413490Sdyson	vm_map_insert(map, kmem_object, offset, addr, addr + size,
31513490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
3161541Srgrimes
31798455Sjeff	/*
31898455Sjeff	 * Note: if M_NOWAIT specified alone, allocate from
31998455Sjeff	 * interrupt-safe queues only (just the free list).  If
32098455Sjeff	 * M_USE_RESERVE is also specified, we can also
32198455Sjeff	 * allocate from the cache.  Neither of the latter two
32298455Sjeff	 * flags may be specified from an interrupt since interrupts
32398455Sjeff	 * are not allowed to mess with the cache queue.
32498455Sjeff	 */
32598455Sjeff
32698455Sjeff	if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
327108351Salc		pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
32898455Sjeff	else
329108351Salc		pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
33098455Sjeff
33198455Sjeff	if (flags & M_ZERO)
33298455Sjeff		pflags |= VM_ALLOC_ZERO;
33398455Sjeff
334113489Salc	VM_OBJECT_LOCK(kmem_object);
3351541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
33615809Sdysonretry:
33798450Sjeff		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
33898450Sjeff
3391541Srgrimes		/*
3405455Sdg		 * Ran out of space, free everything up and return. Don't need
3415455Sdg		 * to lock page queues here as we know that the pages we got
3425455Sdg		 * aren't on any queues.
3431541Srgrimes		 */
3441541Srgrimes		if (m == NULL) {
34542957Sdillon			if ((flags & M_NOWAIT) == 0) {
346113489Salc				VM_OBJECT_UNLOCK(kmem_object);
34744793Salc				vm_map_unlock(map);
34815809Sdyson				VM_WAIT;
34944793Salc				vm_map_lock(map);
350113489Salc				VM_OBJECT_LOCK(kmem_object);
35115809Sdyson				goto retry;
35215809Sdyson			}
35391946Stegge			/*
35491946Stegge			 * Free the pages before removing the map entry.
35591946Stegge			 * They are already marked busy.  Calling
35691946Stegge			 * vm_map_delete before the pages has been freed or
35791946Stegge			 * unbusied will cause a deadlock.
35891946Stegge			 */
35991946Stegge			while (i != 0) {
36091946Stegge				i -= PAGE_SIZE;
36191946Stegge				m = vm_page_lookup(kmem_object,
36291946Stegge						   OFF_TO_IDX(offset + i));
363100796Salc				vm_page_lock_queues();
364108351Salc				vm_page_unwire(m, 0);
36591946Stegge				vm_page_free(m);
366100796Salc				vm_page_unlock_queues();
36791946Stegge			}
368113489Salc			VM_OBJECT_UNLOCK(kmem_object);
3691541Srgrimes			vm_map_delete(map, addr, addr + size);
3701541Srgrimes			vm_map_unlock(map);
371113418Salc			return (0);
3721541Srgrimes		}
37398455Sjeff		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
374102382Salc			pmap_zero_page(m);
375120761Salc		m->valid = VM_PAGE_BITS_ALL;
376166964Salc		KASSERT((m->flags & PG_UNMANAGED) != 0,
377166964Salc		    ("kmem_malloc: page %p is managed", m));
3781541Srgrimes	}
379113489Salc	VM_OBJECT_UNLOCK(kmem_object);
3801541Srgrimes
3811541Srgrimes	/*
3825455Sdg	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
3835455Sdg	 * be able to extend the previous entry so there will be a new entry
3845455Sdg	 * exactly corresponding to this address range and it will have
3855455Sdg	 * wired_count == 0.
3861541Srgrimes	 */
3871541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
3881541Srgrimes	    entry->start != addr || entry->end != addr + size ||
38944793Salc	    entry->wired_count != 0)
3901541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
39144793Salc	entry->wired_count = 1;
3921541Srgrimes
393124048Salc	/*
394124048Salc	 * At this point, the kmem_object must be unlocked because
395124048Salc	 * vm_map_simplify_entry() calls vm_object_deallocate(), which
396124048Salc	 * locks the kmem_object.
397124048Salc	 */
39820993Sdyson	vm_map_simplify_entry(map, entry);
39920993Sdyson
4001541Srgrimes	/*
401164234Salc	 * Loop thru pages, entering them in the pmap.
4021541Srgrimes	 */
403124048Salc	VM_OBJECT_LOCK(kmem_object);
4041541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
40512767Sdyson		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
40642957Sdillon		/*
40742957Sdillon		 * Because this is kernel_pmap, this call will not block.
40842957Sdillon		 */
40960755Speter		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
410108351Salc		vm_page_wakeup(m);
4111541Srgrimes	}
412124048Salc	VM_OBJECT_UNLOCK(kmem_object);
4131541Srgrimes	vm_map_unlock(map);
4141541Srgrimes
4155455Sdg	return (addr);
4161541Srgrimes}
4171541Srgrimes
4181541Srgrimes/*
41942957Sdillon *	kmem_alloc_wait:
4201541Srgrimes *
4211541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
4221541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
4231541Srgrimes *
42442957Sdillon *	This routine may block.
4251541Srgrimes */
4268876Srgrimesvm_offset_t
4275455Sdgkmem_alloc_wait(map, size)
4285455Sdg	vm_map_t map;
4295455Sdg	vm_size_t size;
4301541Srgrimes{
4315455Sdg	vm_offset_t addr;
4321541Srgrimes
4331541Srgrimes	size = round_page(size);
4341541Srgrimes
4351541Srgrimes	for (;;) {
4361541Srgrimes		/*
4375455Sdg		 * To make this work for more than one map, use the map's lock
4385455Sdg		 * to lock out sleepers/wakers.
4391541Srgrimes		 */
4401541Srgrimes		vm_map_lock(map);
44133758Sdyson		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
4421541Srgrimes			break;
4431541Srgrimes		/* no space now; see if we can ever get space */
4441541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4451541Srgrimes			vm_map_unlock(map);
4461541Srgrimes			return (0);
4471541Srgrimes		}
44899754Salc		map->needs_wakeup = TRUE;
44999754Salc		vm_map_unlock_and_wait(map, FALSE);
4501541Srgrimes	}
45199754Salc	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
4521541Srgrimes	vm_map_unlock(map);
4531541Srgrimes	return (addr);
4541541Srgrimes}
4551541Srgrimes
4561541Srgrimes/*
45742957Sdillon *	kmem_free_wakeup:
4581541Srgrimes *
4599507Sdg *	Returns memory to a submap of the kernel, and wakes up any processes
4601541Srgrimes *	waiting for memory in that map.
4611541Srgrimes */
4628876Srgrimesvoid
4635455Sdgkmem_free_wakeup(map, addr, size)
4645455Sdg	vm_map_t map;
4655455Sdg	vm_offset_t addr;
4665455Sdg	vm_size_t size;
4671541Srgrimes{
46876827Salfred
4691541Srgrimes	vm_map_lock(map);
4701541Srgrimes	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
47199754Salc	if (map->needs_wakeup) {
47299754Salc		map->needs_wakeup = FALSE;
47399754Salc		vm_map_wakeup(map);
47499754Salc	}
4751541Srgrimes	vm_map_unlock(map);
4761541Srgrimes}
4771541Srgrimes
4781541Srgrimes/*
47942957Sdillon * 	kmem_init:
48042957Sdillon *
48142957Sdillon *	Create the kernel map; insert a mapping covering kernel text,
48242957Sdillon *	data, bss, and all space allocated thus far (`boostrap' data).  The
48342957Sdillon *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
48442957Sdillon *	`start' as allocated, and the range between `start' and `end' as free.
4851541Srgrimes */
4868876Srgrimesvoid
4875455Sdgkmem_init(start, end)
4881541Srgrimes	vm_offset_t start, end;
4891541Srgrimes{
49070480Salfred	vm_map_t m;
4911541Srgrimes
49232702Sdyson	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
493108426Salc	m->system_map = 1;
4941541Srgrimes	vm_map_lock(m);
4951541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
4961541Srgrimes	kernel_map = m;
497108426Salc	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
498165854Salc	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL,
499165854Salc	    MAP_NOFAULT);
5001541Srgrimes	/* ... and ending with the completion of the above `insert' */
5011541Srgrimes	vm_map_unlock(m);
5021541Srgrimes}
503