vm_kern.c revision 194766
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 194766 2009-06-23 20:45:22Z kib $");
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>
76188964Srwatson#include <sys/sysctl.h>
771541Srgrimes
781541Srgrimes#include <vm/vm.h>
7912662Sdg#include <vm/vm_param.h>
8012662Sdg#include <vm/pmap.h>
8112662Sdg#include <vm/vm_map.h>
8212662Sdg#include <vm/vm_object.h>
831541Srgrimes#include <vm/vm_page.h>
841541Srgrimes#include <vm/vm_pageout.h>
8512726Sbde#include <vm/vm_extern.h>
86168395Spjd#include <vm/uma.h>
871541Srgrimes
8819830Sdysonvm_map_t kernel_map=0;
8919830Sdysonvm_map_t kmem_map=0;
9019830Sdysonvm_map_t exec_map=0;
91118764Ssilbyvm_map_t pipe_map;
9219830Sdysonvm_map_t buffer_map=0;
932112Swollman
941541Srgrimes/*
9547841Sdt *	kmem_alloc_nofault:
9647841Sdt *
97118317Salc *	Allocate a virtual address range with no underlying object and
98118317Salc *	no initial mapping to physical memory.  Any mapping from this
99118317Salc *	range to physical memory must be explicitly created prior to
100118317Salc *	its use, typically with pmap_qenter().  Any attempt to create
101118317Salc *	a mapping on demand through vm_fault() will result in a panic.
10247841Sdt */
10347841Sdtvm_offset_t
10447841Sdtkmem_alloc_nofault(map, size)
10547841Sdt	vm_map_t map;
10670480Salfred	vm_size_t size;
10747841Sdt{
10847841Sdt	vm_offset_t addr;
10970480Salfred	int result;
11047841Sdt
11147841Sdt	size = round_page(size);
11247841Sdt	addr = vm_map_min(map);
113178933Salc	result = vm_map_find(map, NULL, 0, &addr, size, VMFS_ANY_SPACE,
114178933Salc	    VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
11547841Sdt	if (result != KERN_SUCCESS) {
11647841Sdt		return (0);
11747841Sdt	}
11847841Sdt	return (addr);
11947841Sdt}
12047841Sdt
12147841Sdt/*
1221541Srgrimes *	Allocate wired-down memory in the kernel's address map
1231541Srgrimes *	or a submap.
1241541Srgrimes */
1258876Srgrimesvm_offset_t
1265455Sdgkmem_alloc(map, size)
12770480Salfred	vm_map_t map;
12870480Salfred	vm_size_t size;
1291541Srgrimes{
1305455Sdg	vm_offset_t addr;
13170480Salfred	vm_offset_t offset;
1325455Sdg	vm_offset_t i;
1331541Srgrimes
1341541Srgrimes	size = round_page(size);
1351541Srgrimes
1361541Srgrimes	/*
1375455Sdg	 * Use the kernel object for wired-down kernel pages. Assume that no
1385455Sdg	 * region of the kernel object is referenced more than once.
1391541Srgrimes	 */
1401541Srgrimes
1411541Srgrimes	/*
1425455Sdg	 * Locate sufficient space in the map.  This will give us the final
1435455Sdg	 * virtual address for the new memory, and thus will tell us the
1445455Sdg	 * offset within the kernel map.
1451541Srgrimes	 */
1461541Srgrimes	vm_map_lock(map);
14733758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
1481541Srgrimes		vm_map_unlock(map);
1491541Srgrimes		return (0);
1501541Srgrimes	}
1511541Srgrimes	offset = addr - VM_MIN_KERNEL_ADDRESS;
1521541Srgrimes	vm_object_reference(kernel_object);
15313490Sdyson	vm_map_insert(map, kernel_object, offset, addr, addr + size,
15413490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
1551541Srgrimes	vm_map_unlock(map);
1561541Srgrimes
1571541Srgrimes	/*
1585455Sdg	 * Guarantee that there are pages already in this object before
159122383Smini	 * calling vm_map_wire.  This is to prevent the following
1605455Sdg	 * scenario:
1618876Srgrimes	 *
1625455Sdg	 * 1) Threads have swapped out, so that there is a pager for the
1635455Sdg	 * kernel_object. 2) The kmsg zone is empty, and so we are
164122383Smini	 * kmem_allocing a new page for it. 3) vm_map_wire calls vm_fault;
1655455Sdg	 * there is no page, but there is a pager, so we call
1665455Sdg	 * pager_data_request.  But the kmsg zone is empty, so we must
1675455Sdg	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
1685455Sdg	 * we get the data back from the pager, it will be (very stale)
1695455Sdg	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
1708876Srgrimes	 *
1715455Sdg	 * We're intentionally not activating the pages we allocate to prevent a
172122383Smini	 * race with page-out.  vm_map_wire will wire the pages.
1731541Srgrimes	 */
174120761Salc	VM_OBJECT_LOCK(kernel_object);
1755455Sdg	for (i = 0; i < size; i += PAGE_SIZE) {
1765455Sdg		vm_page_t mem;
1771541Srgrimes
17833109Sdyson		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
179136923Salc		    VM_ALLOC_NOBUSY | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
180120761Salc		mem->valid = VM_PAGE_BITS_ALL;
181166964Salc		KASSERT((mem->flags & PG_UNMANAGED) != 0,
182166964Salc		    ("kmem_alloc: page %p is managed", mem));
1831541Srgrimes	}
184120761Salc	VM_OBJECT_UNLOCK(kernel_object);
1855455Sdg
1861541Srgrimes	/*
1875455Sdg	 * And finally, mark the data as non-pageable.
1881541Srgrimes	 */
189118771Sbms	(void) vm_map_wire(map, addr, addr + size,
190118771Sbms	    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
1911541Srgrimes
1925455Sdg	return (addr);
1931541Srgrimes}
1941541Srgrimes
1951541Srgrimes/*
1961541Srgrimes *	kmem_free:
1971541Srgrimes *
1981541Srgrimes *	Release a region of kernel virtual memory allocated
1991541Srgrimes *	with kmem_alloc, and return the physical pages
2001541Srgrimes *	associated with that region.
20142957Sdillon *
20242957Sdillon *	This routine may not block on kernel maps.
2031541Srgrimes */
2048876Srgrimesvoid
2055455Sdgkmem_free(map, addr, size)
2065455Sdg	vm_map_t map;
20770480Salfred	vm_offset_t addr;
2085455Sdg	vm_size_t size;
2091541Srgrimes{
21071571Sjhb
2111541Srgrimes	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
2121541Srgrimes}
2131541Srgrimes
2141541Srgrimes/*
2151541Srgrimes *	kmem_suballoc:
2161541Srgrimes *
2171541Srgrimes *	Allocates a map to manage a subrange
2181541Srgrimes *	of the kernel virtual address space.
2191541Srgrimes *
2201541Srgrimes *	Arguments are as follows:
2211541Srgrimes *
2221541Srgrimes *	parent		Map to take range from
22370480Salfred *	min, max	Returned endpoints of map
2241541Srgrimes *	size		Size of range to find
225178933Salc *	superpage_align	Request that min is superpage aligned
2261541Srgrimes */
2278876Srgrimesvm_map_t
228178933Salckmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
229178933Salc    vm_size_t size, boolean_t superpage_align)
2301541Srgrimes{
23170478Salfred	int ret;
2325455Sdg	vm_map_t result;
2331541Srgrimes
2341541Srgrimes	size = round_page(size);
2351541Srgrimes
236178637Salc	*min = vm_map_min(parent);
237178933Salc	ret = vm_map_find(parent, NULL, 0, min, size, superpage_align ?
238194766Skib	    VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
239194766Skib	    MAP_ACC_NO_CHARGE);
240177762Salc	if (ret != KERN_SUCCESS)
241177762Salc		panic("kmem_suballoc: bad status return of %d", ret);
2421541Srgrimes	*max = *min + size;
24332702Sdyson	result = vm_map_create(vm_map_pmap(parent), *min, *max);
2441541Srgrimes	if (result == NULL)
2451541Srgrimes		panic("kmem_suballoc: cannot create submap");
24670478Salfred	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
2471541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2485455Sdg	return (result);
2491541Srgrimes}
2501541Srgrimes
2511541Srgrimes/*
25242957Sdillon *	kmem_malloc:
2531541Srgrimes *
25442957Sdillon * 	Allocate wired-down memory in the kernel's address map for the higher
25542957Sdillon * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
25642957Sdillon * 	kmem_alloc() because we may need to allocate memory at interrupt
25742957Sdillon * 	level where we cannot block (canwait == FALSE).
2581541Srgrimes *
25942957Sdillon * 	This routine has its own private kernel submap (kmem_map) and object
26042957Sdillon * 	(kmem_object).  This, combined with the fact that only malloc uses
26142957Sdillon * 	this routine, ensures that we will never block in map or object waits.
2621541Srgrimes *
26342957Sdillon * 	We don't worry about expanding the map (adding entries) since entries
26442957Sdillon * 	for wired maps are statically allocated.
26542957Sdillon *
26678592Sbmilekic *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
26778592Sbmilekic *	which we never free.
2681541Srgrimes */
2691541Srgrimesvm_offset_t
27042957Sdillonkmem_malloc(map, size, flags)
27170480Salfred	vm_map_t map;
27270480Salfred	vm_size_t size;
27342957Sdillon	int flags;
2741541Srgrimes{
27570480Salfred	vm_offset_t offset, i;
276189015Skib	vm_map_entry_t entry;
2775455Sdg	vm_offset_t addr;
2785455Sdg	vm_page_t m;
27998455Sjeff	int pflags;
2801541Srgrimes
2811541Srgrimes	size = round_page(size);
2821541Srgrimes	addr = vm_map_min(map);
2831541Srgrimes
2841541Srgrimes	/*
2855455Sdg	 * Locate sufficient space in the map.  This will give us the final
2865455Sdg	 * virtual address for the new memory, and thus will tell us the
2875455Sdg	 * offset within the kernel map.
2881541Srgrimes	 */
2891541Srgrimes	vm_map_lock(map);
29033758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
2911541Srgrimes		vm_map_unlock(map);
292175210Spjd                if ((flags & M_NOWAIT) == 0) {
293175210Spjd			for (i = 0; i < 8; i++) {
294175210Spjd				EVENTHANDLER_INVOKE(vm_lowmem, 0);
295175210Spjd				uma_reclaim();
296175210Spjd				vm_map_lock(map);
297175210Spjd				if (vm_map_findspace(map, vm_map_min(map),
298175210Spjd				    size, &addr) == 0) {
299175210Spjd					break;
300175210Spjd				}
301168395Spjd				vm_map_unlock(map);
302175210Spjd				tsleep(&i, 0, "nokva", (hz / 4) * (i + 1));
303175210Spjd			}
304175210Spjd			if (i == 8) {
305168395Spjd				panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
306175210Spjd				    (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	if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
318108351Salc		pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
31998455Sjeff	else
320108351Salc		pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
32198455Sjeff
32298455Sjeff	if (flags & M_ZERO)
32398455Sjeff		pflags |= VM_ALLOC_ZERO;
32498455Sjeff
325113489Salc	VM_OBJECT_LOCK(kmem_object);
3261541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
32715809Sdysonretry:
32898450Sjeff		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
32998450Sjeff
3301541Srgrimes		/*
3315455Sdg		 * Ran out of space, free everything up and return. Don't need
3325455Sdg		 * to lock page queues here as we know that the pages we got
3335455Sdg		 * aren't on any queues.
3341541Srgrimes		 */
3351541Srgrimes		if (m == NULL) {
33642957Sdillon			if ((flags & M_NOWAIT) == 0) {
337113489Salc				VM_OBJECT_UNLOCK(kmem_object);
33844793Salc				vm_map_unlock(map);
33915809Sdyson				VM_WAIT;
34044793Salc				vm_map_lock(map);
341113489Salc				VM_OBJECT_LOCK(kmem_object);
34215809Sdyson				goto retry;
34315809Sdyson			}
34491946Stegge			/*
34591946Stegge			 * Free the pages before removing the map entry.
34691946Stegge			 * They are already marked busy.  Calling
34791946Stegge			 * vm_map_delete before the pages has been freed or
34891946Stegge			 * unbusied will cause a deadlock.
34991946Stegge			 */
35091946Stegge			while (i != 0) {
35191946Stegge				i -= PAGE_SIZE;
35291946Stegge				m = vm_page_lookup(kmem_object,
35391946Stegge						   OFF_TO_IDX(offset + i));
354100796Salc				vm_page_lock_queues();
355108351Salc				vm_page_unwire(m, 0);
35691946Stegge				vm_page_free(m);
357100796Salc				vm_page_unlock_queues();
35891946Stegge			}
359113489Salc			VM_OBJECT_UNLOCK(kmem_object);
360189015Skib			vm_map_delete(map, addr, addr + size);
3611541Srgrimes			vm_map_unlock(map);
362113418Salc			return (0);
3631541Srgrimes		}
36498455Sjeff		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
365102382Salc			pmap_zero_page(m);
366120761Salc		m->valid = VM_PAGE_BITS_ALL;
367166964Salc		KASSERT((m->flags & PG_UNMANAGED) != 0,
368166964Salc		    ("kmem_malloc: page %p is managed", m));
3691541Srgrimes	}
370113489Salc	VM_OBJECT_UNLOCK(kmem_object);
3711541Srgrimes
3721541Srgrimes	/*
3735455Sdg	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
3745455Sdg	 * be able to extend the previous entry so there will be a new entry
3755455Sdg	 * exactly corresponding to this address range and it will have
3765455Sdg	 * wired_count == 0.
3771541Srgrimes	 */
3781541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
3791541Srgrimes	    entry->start != addr || entry->end != addr + size ||
38044793Salc	    entry->wired_count != 0)
3811541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
38244793Salc	entry->wired_count = 1;
3831541Srgrimes
384124048Salc	/*
385124048Salc	 * At this point, the kmem_object must be unlocked because
386124048Salc	 * vm_map_simplify_entry() calls vm_object_deallocate(), which
387124048Salc	 * locks the kmem_object.
388124048Salc	 */
38920993Sdyson	vm_map_simplify_entry(map, entry);
39020993Sdyson
3911541Srgrimes	/*
392164234Salc	 * Loop thru pages, entering them in the pmap.
3931541Srgrimes	 */
394124048Salc	VM_OBJECT_LOCK(kmem_object);
3951541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
39612767Sdyson		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
39742957Sdillon		/*
39842957Sdillon		 * Because this is kernel_pmap, this call will not block.
39942957Sdillon		 */
400175067Salc		pmap_enter(kernel_pmap, addr + i, VM_PROT_ALL, m, VM_PROT_ALL,
401175067Salc		    TRUE);
402108351Salc		vm_page_wakeup(m);
4031541Srgrimes	}
404124048Salc	VM_OBJECT_UNLOCK(kmem_object);
4051541Srgrimes	vm_map_unlock(map);
4061541Srgrimes
4075455Sdg	return (addr);
4081541Srgrimes}
4091541Srgrimes
4101541Srgrimes/*
41142957Sdillon *	kmem_alloc_wait:
4121541Srgrimes *
4131541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
4141541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
4151541Srgrimes *
41642957Sdillon *	This routine may block.
4171541Srgrimes */
4188876Srgrimesvm_offset_t
4195455Sdgkmem_alloc_wait(map, size)
4205455Sdg	vm_map_t map;
4215455Sdg	vm_size_t size;
4221541Srgrimes{
4235455Sdg	vm_offset_t addr;
4241541Srgrimes
4251541Srgrimes	size = round_page(size);
426194766Skib	if (!swap_reserve(size))
427194766Skib		return (0);
4281541Srgrimes
4291541Srgrimes	for (;;) {
4301541Srgrimes		/*
4315455Sdg		 * To make this work for more than one map, use the map's lock
4325455Sdg		 * to lock out sleepers/wakers.
4331541Srgrimes		 */
4341541Srgrimes		vm_map_lock(map);
43533758Sdyson		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
4361541Srgrimes			break;
4371541Srgrimes		/* no space now; see if we can ever get space */
4381541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4391541Srgrimes			vm_map_unlock(map);
440194766Skib			swap_release(size);
4411541Srgrimes			return (0);
4421541Srgrimes		}
44399754Salc		map->needs_wakeup = TRUE;
444173429Spjd		vm_map_unlock_and_wait(map, 0);
4451541Srgrimes	}
446194766Skib	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
447194766Skib	    VM_PROT_ALL, MAP_ACC_CHARGED);
4481541Srgrimes	vm_map_unlock(map);
4491541Srgrimes	return (addr);
4501541Srgrimes}
4511541Srgrimes
4521541Srgrimes/*
45342957Sdillon *	kmem_free_wakeup:
4541541Srgrimes *
4559507Sdg *	Returns memory to a submap of the kernel, and wakes up any processes
4561541Srgrimes *	waiting for memory in that map.
4571541Srgrimes */
4588876Srgrimesvoid
4595455Sdgkmem_free_wakeup(map, addr, size)
4605455Sdg	vm_map_t map;
4615455Sdg	vm_offset_t addr;
4625455Sdg	vm_size_t size;
4631541Srgrimes{
46476827Salfred
4651541Srgrimes	vm_map_lock(map);
466189015Skib	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
46799754Salc	if (map->needs_wakeup) {
46899754Salc		map->needs_wakeup = FALSE;
46999754Salc		vm_map_wakeup(map);
47099754Salc	}
4711541Srgrimes	vm_map_unlock(map);
4721541Srgrimes}
4731541Srgrimes
4741541Srgrimes/*
47542957Sdillon * 	kmem_init:
47642957Sdillon *
47742957Sdillon *	Create the kernel map; insert a mapping covering kernel text,
47842957Sdillon *	data, bss, and all space allocated thus far (`boostrap' data).  The
47942957Sdillon *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
48042957Sdillon *	`start' as allocated, and the range between `start' and `end' as free.
4811541Srgrimes */
4828876Srgrimesvoid
4835455Sdgkmem_init(start, end)
4841541Srgrimes	vm_offset_t start, end;
4851541Srgrimes{
48670480Salfred	vm_map_t m;
4871541Srgrimes
48832702Sdyson	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
489108426Salc	m->system_map = 1;
4901541Srgrimes	vm_map_lock(m);
4911541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
4921541Srgrimes	kernel_map = m;
493108426Salc	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
494179923Salc#ifdef __amd64__
495179923Salc	    KERNBASE,
496179923Salc#else
497179923Salc	    VM_MIN_KERNEL_ADDRESS,
498179923Salc#endif
499179923Salc	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
5001541Srgrimes	/* ... and ending with the completion of the above `insert' */
5011541Srgrimes	vm_map_unlock(m);
5021541Srgrimes}
503188964Srwatson
504188967Srwatson#ifdef DIAGNOSTIC
505188964Srwatson/*
506188964Srwatson * Allow userspace to directly trigger the VM drain routine for testing
507188964Srwatson * purposes.
508188964Srwatson */
509188964Srwatsonstatic int
510188964Srwatsondebug_vm_lowmem(SYSCTL_HANDLER_ARGS)
511188964Srwatson{
512188964Srwatson	int error, i;
513188964Srwatson
514188964Srwatson	i = 0;
515188964Srwatson	error = sysctl_handle_int(oidp, &i, 0, req);
516188964Srwatson	if (error)
517188964Srwatson		return (error);
518188964Srwatson	if (i)
519188964Srwatson		EVENTHANDLER_INVOKE(vm_lowmem, 0);
520188964Srwatson	return (0);
521188964Srwatson}
522188964Srwatson
523188964SrwatsonSYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
524188964Srwatson    debug_vm_lowmem, "I", "set to trigger vm_lowmem event");
525188967Srwatson#endif
526