vm_kern.c revision 212931
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 212931 2010-09-20 20:41:59Z mdf $");
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/*
122206819Sjmallett *	kmem_alloc_nofault_space:
123206819Sjmallett *
124206819Sjmallett *	Allocate a virtual address range with no underlying object and
125206819Sjmallett *	no initial mapping to physical memory within the specified
126206819Sjmallett *	address space.  Any mapping from this range to physical memory
127206819Sjmallett *	must be explicitly created prior to its use, typically with
128206819Sjmallett *	pmap_qenter().  Any attempt to create a mapping on demand
129206819Sjmallett *	through vm_fault() will result in a panic.
130206819Sjmallett */
131206819Sjmallettvm_offset_t
132206819Sjmallettkmem_alloc_nofault_space(map, size, find_space)
133206819Sjmallett	vm_map_t map;
134206819Sjmallett	vm_size_t size;
135206819Sjmallett	int find_space;
136206819Sjmallett{
137206819Sjmallett	vm_offset_t addr;
138206819Sjmallett	int result;
139206819Sjmallett
140206819Sjmallett	size = round_page(size);
141206819Sjmallett	addr = vm_map_min(map);
142206819Sjmallett	result = vm_map_find(map, NULL, 0, &addr, size, find_space,
143206819Sjmallett	    VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
144206819Sjmallett	if (result != KERN_SUCCESS) {
145206819Sjmallett		return (0);
146206819Sjmallett	}
147206819Sjmallett	return (addr);
148206819Sjmallett}
149206819Sjmallett
150206819Sjmallett/*
1511541Srgrimes *	Allocate wired-down memory in the kernel's address map
1521541Srgrimes *	or a submap.
1531541Srgrimes */
1548876Srgrimesvm_offset_t
1555455Sdgkmem_alloc(map, size)
15670480Salfred	vm_map_t map;
15770480Salfred	vm_size_t size;
1581541Srgrimes{
1595455Sdg	vm_offset_t addr;
16070480Salfred	vm_offset_t offset;
1615455Sdg	vm_offset_t i;
1621541Srgrimes
1631541Srgrimes	size = round_page(size);
1641541Srgrimes
1651541Srgrimes	/*
1665455Sdg	 * Use the kernel object for wired-down kernel pages. Assume that no
1675455Sdg	 * region of the kernel object is referenced more than once.
1681541Srgrimes	 */
1691541Srgrimes
1701541Srgrimes	/*
1715455Sdg	 * Locate sufficient space in the map.  This will give us the final
1725455Sdg	 * virtual address for the new memory, and thus will tell us the
1735455Sdg	 * offset within the kernel map.
1741541Srgrimes	 */
1751541Srgrimes	vm_map_lock(map);
17633758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
1771541Srgrimes		vm_map_unlock(map);
1781541Srgrimes		return (0);
1791541Srgrimes	}
1801541Srgrimes	offset = addr - VM_MIN_KERNEL_ADDRESS;
1811541Srgrimes	vm_object_reference(kernel_object);
18213490Sdyson	vm_map_insert(map, kernel_object, offset, addr, addr + size,
18313490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
1841541Srgrimes	vm_map_unlock(map);
1851541Srgrimes
1861541Srgrimes	/*
1875455Sdg	 * Guarantee that there are pages already in this object before
188122383Smini	 * calling vm_map_wire.  This is to prevent the following
1895455Sdg	 * scenario:
1908876Srgrimes	 *
1915455Sdg	 * 1) Threads have swapped out, so that there is a pager for the
1925455Sdg	 * kernel_object. 2) The kmsg zone is empty, and so we are
193122383Smini	 * kmem_allocing a new page for it. 3) vm_map_wire calls vm_fault;
1945455Sdg	 * there is no page, but there is a pager, so we call
1955455Sdg	 * pager_data_request.  But the kmsg zone is empty, so we must
1965455Sdg	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
1975455Sdg	 * we get the data back from the pager, it will be (very stale)
1985455Sdg	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
1998876Srgrimes	 *
2005455Sdg	 * We're intentionally not activating the pages we allocate to prevent a
201122383Smini	 * race with page-out.  vm_map_wire will wire the pages.
2021541Srgrimes	 */
203120761Salc	VM_OBJECT_LOCK(kernel_object);
2045455Sdg	for (i = 0; i < size; i += PAGE_SIZE) {
2055455Sdg		vm_page_t mem;
2061541Srgrimes
20733109Sdyson		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
208136923Salc		    VM_ALLOC_NOBUSY | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
209120761Salc		mem->valid = VM_PAGE_BITS_ALL;
210166964Salc		KASSERT((mem->flags & PG_UNMANAGED) != 0,
211166964Salc		    ("kmem_alloc: page %p is managed", mem));
2121541Srgrimes	}
213120761Salc	VM_OBJECT_UNLOCK(kernel_object);
2145455Sdg
2151541Srgrimes	/*
2165455Sdg	 * And finally, mark the data as non-pageable.
2171541Srgrimes	 */
218118771Sbms	(void) vm_map_wire(map, addr, addr + size,
219118771Sbms	    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
2201541Srgrimes
2215455Sdg	return (addr);
2221541Srgrimes}
2231541Srgrimes
2241541Srgrimes/*
2251541Srgrimes *	kmem_free:
2261541Srgrimes *
2271541Srgrimes *	Release a region of kernel virtual memory allocated
2281541Srgrimes *	with kmem_alloc, and return the physical pages
2291541Srgrimes *	associated with that region.
23042957Sdillon *
23142957Sdillon *	This routine may not block on kernel maps.
2321541Srgrimes */
2338876Srgrimesvoid
2345455Sdgkmem_free(map, addr, size)
2355455Sdg	vm_map_t map;
23670480Salfred	vm_offset_t addr;
2375455Sdg	vm_size_t size;
2381541Srgrimes{
23971571Sjhb
2401541Srgrimes	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
2411541Srgrimes}
2421541Srgrimes
2431541Srgrimes/*
2441541Srgrimes *	kmem_suballoc:
2451541Srgrimes *
2461541Srgrimes *	Allocates a map to manage a subrange
2471541Srgrimes *	of the kernel virtual address space.
2481541Srgrimes *
2491541Srgrimes *	Arguments are as follows:
2501541Srgrimes *
2511541Srgrimes *	parent		Map to take range from
25270480Salfred *	min, max	Returned endpoints of map
2531541Srgrimes *	size		Size of range to find
254178933Salc *	superpage_align	Request that min is superpage aligned
2551541Srgrimes */
2568876Srgrimesvm_map_t
257178933Salckmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
258178933Salc    vm_size_t size, boolean_t superpage_align)
2591541Srgrimes{
26070478Salfred	int ret;
2615455Sdg	vm_map_t result;
2621541Srgrimes
2631541Srgrimes	size = round_page(size);
2641541Srgrimes
265178637Salc	*min = vm_map_min(parent);
266178933Salc	ret = vm_map_find(parent, NULL, 0, min, size, superpage_align ?
267194766Skib	    VMFS_ALIGNED_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
268194766Skib	    MAP_ACC_NO_CHARGE);
269177762Salc	if (ret != KERN_SUCCESS)
270177762Salc		panic("kmem_suballoc: bad status return of %d", ret);
2711541Srgrimes	*max = *min + size;
27232702Sdyson	result = vm_map_create(vm_map_pmap(parent), *min, *max);
2731541Srgrimes	if (result == NULL)
2741541Srgrimes		panic("kmem_suballoc: cannot create submap");
27570478Salfred	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
2761541Srgrimes		panic("kmem_suballoc: unable to change range to submap");
2775455Sdg	return (result);
2781541Srgrimes}
2791541Srgrimes
2801541Srgrimes/*
28142957Sdillon *	kmem_malloc:
2821541Srgrimes *
28342957Sdillon * 	Allocate wired-down memory in the kernel's address map for the higher
28442957Sdillon * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
28542957Sdillon * 	kmem_alloc() because we may need to allocate memory at interrupt
28642957Sdillon * 	level where we cannot block (canwait == FALSE).
2871541Srgrimes *
28842957Sdillon * 	This routine has its own private kernel submap (kmem_map) and object
28942957Sdillon * 	(kmem_object).  This, combined with the fact that only malloc uses
29042957Sdillon * 	this routine, ensures that we will never block in map or object waits.
2911541Srgrimes *
29242957Sdillon * 	We don't worry about expanding the map (adding entries) since entries
29342957Sdillon * 	for wired maps are statically allocated.
29442957Sdillon *
29578592Sbmilekic *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
29678592Sbmilekic *	which we never free.
2971541Srgrimes */
2981541Srgrimesvm_offset_t
29942957Sdillonkmem_malloc(map, size, flags)
30070480Salfred	vm_map_t map;
30170480Salfred	vm_size_t size;
30242957Sdillon	int flags;
3031541Srgrimes{
3045455Sdg	vm_offset_t addr;
305211194Smdf	int i, rv;
3061541Srgrimes
3071541Srgrimes	size = round_page(size);
3081541Srgrimes	addr = vm_map_min(map);
3091541Srgrimes
3101541Srgrimes	/*
3115455Sdg	 * Locate sufficient space in the map.  This will give us the final
3125455Sdg	 * virtual address for the new memory, and thus will tell us the
3135455Sdg	 * offset within the kernel map.
3141541Srgrimes	 */
3151541Srgrimes	vm_map_lock(map);
31633758Sdyson	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
3171541Srgrimes		vm_map_unlock(map);
318175210Spjd                if ((flags & M_NOWAIT) == 0) {
319175210Spjd			for (i = 0; i < 8; i++) {
320175210Spjd				EVENTHANDLER_INVOKE(vm_lowmem, 0);
321175210Spjd				uma_reclaim();
322175210Spjd				vm_map_lock(map);
323175210Spjd				if (vm_map_findspace(map, vm_map_min(map),
324175210Spjd				    size, &addr) == 0) {
325175210Spjd					break;
326175210Spjd				}
327168395Spjd				vm_map_unlock(map);
328175210Spjd				tsleep(&i, 0, "nokva", (hz / 4) * (i + 1));
329175210Spjd			}
330175210Spjd			if (i == 8) {
331168395Spjd				panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
332175210Spjd				    (long)size, (long)map->size);
333168395Spjd			}
334168395Spjd		} else {
335168395Spjd			return (0);
336168395Spjd		}
3371541Srgrimes	}
338211194Smdf
339211194Smdf	rv = kmem_back(map, addr, size, flags);
340211194Smdf	vm_map_unlock(map);
341211194Smdf	return (rv == KERN_SUCCESS ? addr : 0);
342211194Smdf}
343211194Smdf
344211194Smdf/*
345211194Smdf *	kmem_back:
346211194Smdf *
347211194Smdf *	Allocate physical pages for the specified virtual address range.
348211194Smdf */
349211194Smdfint
350211194Smdfkmem_back(vm_map_t map, vm_offset_t addr, vm_size_t size, int flags)
351211194Smdf{
352211194Smdf	vm_offset_t offset, i;
353211194Smdf	vm_map_entry_t entry;
354211194Smdf	vm_page_t m;
355211194Smdf	int pflags;
356211194Smdf
357212931Smdf	KASSERT(vm_map_locked(map), ("kmem_back: map %p is not locked", map));
35815367Sdyson	offset = addr - VM_MIN_KERNEL_ADDRESS;
3591541Srgrimes	vm_object_reference(kmem_object);
36013490Sdyson	vm_map_insert(map, kmem_object, offset, addr, addr + size,
36113490Sdyson		VM_PROT_ALL, VM_PROT_ALL, 0);
3621541Srgrimes
36398455Sjeff	if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
364108351Salc		pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
36598455Sjeff	else
366108351Salc		pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
36798455Sjeff
36898455Sjeff	if (flags & M_ZERO)
36998455Sjeff		pflags |= VM_ALLOC_ZERO;
37098455Sjeff
371113489Salc	VM_OBJECT_LOCK(kmem_object);
3721541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
37315809Sdysonretry:
37498450Sjeff		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
37598450Sjeff
3761541Srgrimes		/*
3775455Sdg		 * Ran out of space, free everything up and return. Don't need
3785455Sdg		 * to lock page queues here as we know that the pages we got
3795455Sdg		 * aren't on any queues.
3801541Srgrimes		 */
3811541Srgrimes		if (m == NULL) {
38242957Sdillon			if ((flags & M_NOWAIT) == 0) {
383113489Salc				VM_OBJECT_UNLOCK(kmem_object);
38444793Salc				vm_map_unlock(map);
38515809Sdyson				VM_WAIT;
38644793Salc				vm_map_lock(map);
387113489Salc				VM_OBJECT_LOCK(kmem_object);
38815809Sdyson				goto retry;
38915809Sdyson			}
39091946Stegge			/*
39191946Stegge			 * Free the pages before removing the map entry.
39291946Stegge			 * They are already marked busy.  Calling
39391946Stegge			 * vm_map_delete before the pages has been freed or
39491946Stegge			 * unbusied will cause a deadlock.
39591946Stegge			 */
39691946Stegge			while (i != 0) {
39791946Stegge				i -= PAGE_SIZE;
39891946Stegge				m = vm_page_lookup(kmem_object,
39991946Stegge						   OFF_TO_IDX(offset + i));
400108351Salc				vm_page_unwire(m, 0);
40191946Stegge				vm_page_free(m);
40291946Stegge			}
403113489Salc			VM_OBJECT_UNLOCK(kmem_object);
404189015Skib			vm_map_delete(map, addr, addr + size);
405211194Smdf			return (KERN_NO_SPACE);
4061541Srgrimes		}
40798455Sjeff		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
408102382Salc			pmap_zero_page(m);
409120761Salc		m->valid = VM_PAGE_BITS_ALL;
410166964Salc		KASSERT((m->flags & PG_UNMANAGED) != 0,
411166964Salc		    ("kmem_malloc: page %p is managed", m));
4121541Srgrimes	}
413113489Salc	VM_OBJECT_UNLOCK(kmem_object);
4141541Srgrimes
4151541Srgrimes	/*
4165455Sdg	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
4175455Sdg	 * be able to extend the previous entry so there will be a new entry
4185455Sdg	 * exactly corresponding to this address range and it will have
4195455Sdg	 * wired_count == 0.
4201541Srgrimes	 */
4211541Srgrimes	if (!vm_map_lookup_entry(map, addr, &entry) ||
4221541Srgrimes	    entry->start != addr || entry->end != addr + size ||
42344793Salc	    entry->wired_count != 0)
4241541Srgrimes		panic("kmem_malloc: entry not found or misaligned");
42544793Salc	entry->wired_count = 1;
4261541Srgrimes
427124048Salc	/*
428124048Salc	 * At this point, the kmem_object must be unlocked because
429124048Salc	 * vm_map_simplify_entry() calls vm_object_deallocate(), which
430124048Salc	 * locks the kmem_object.
431124048Salc	 */
43220993Sdyson	vm_map_simplify_entry(map, entry);
43320993Sdyson
4341541Srgrimes	/*
435164234Salc	 * Loop thru pages, entering them in the pmap.
4361541Srgrimes	 */
437124048Salc	VM_OBJECT_LOCK(kmem_object);
4381541Srgrimes	for (i = 0; i < size; i += PAGE_SIZE) {
43912767Sdyson		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
44042957Sdillon		/*
44142957Sdillon		 * Because this is kernel_pmap, this call will not block.
44242957Sdillon		 */
443175067Salc		pmap_enter(kernel_pmap, addr + i, VM_PROT_ALL, m, VM_PROT_ALL,
444175067Salc		    TRUE);
445108351Salc		vm_page_wakeup(m);
4461541Srgrimes	}
447124048Salc	VM_OBJECT_UNLOCK(kmem_object);
4481541Srgrimes
449211194Smdf	return (KERN_SUCCESS);
4501541Srgrimes}
4511541Srgrimes
4521541Srgrimes/*
45342957Sdillon *	kmem_alloc_wait:
4541541Srgrimes *
4551541Srgrimes *	Allocates pageable memory from a sub-map of the kernel.  If the submap
4561541Srgrimes *	has no room, the caller sleeps waiting for more memory in the submap.
4571541Srgrimes *
45842957Sdillon *	This routine may block.
4591541Srgrimes */
4608876Srgrimesvm_offset_t
4615455Sdgkmem_alloc_wait(map, size)
4625455Sdg	vm_map_t map;
4635455Sdg	vm_size_t size;
4641541Srgrimes{
4655455Sdg	vm_offset_t addr;
4661541Srgrimes
4671541Srgrimes	size = round_page(size);
468194766Skib	if (!swap_reserve(size))
469194766Skib		return (0);
4701541Srgrimes
4711541Srgrimes	for (;;) {
4721541Srgrimes		/*
4735455Sdg		 * To make this work for more than one map, use the map's lock
4745455Sdg		 * to lock out sleepers/wakers.
4751541Srgrimes		 */
4761541Srgrimes		vm_map_lock(map);
47733758Sdyson		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
4781541Srgrimes			break;
4791541Srgrimes		/* no space now; see if we can ever get space */
4801541Srgrimes		if (vm_map_max(map) - vm_map_min(map) < size) {
4811541Srgrimes			vm_map_unlock(map);
482194766Skib			swap_release(size);
4831541Srgrimes			return (0);
4841541Srgrimes		}
48599754Salc		map->needs_wakeup = TRUE;
486173429Spjd		vm_map_unlock_and_wait(map, 0);
4871541Srgrimes	}
488194766Skib	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
489194766Skib	    VM_PROT_ALL, MAP_ACC_CHARGED);
4901541Srgrimes	vm_map_unlock(map);
4911541Srgrimes	return (addr);
4921541Srgrimes}
4931541Srgrimes
4941541Srgrimes/*
49542957Sdillon *	kmem_free_wakeup:
4961541Srgrimes *
4979507Sdg *	Returns memory to a submap of the kernel, and wakes up any processes
4981541Srgrimes *	waiting for memory in that map.
4991541Srgrimes */
5008876Srgrimesvoid
5015455Sdgkmem_free_wakeup(map, addr, size)
5025455Sdg	vm_map_t map;
5035455Sdg	vm_offset_t addr;
5045455Sdg	vm_size_t size;
5051541Srgrimes{
50676827Salfred
5071541Srgrimes	vm_map_lock(map);
508189015Skib	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
50999754Salc	if (map->needs_wakeup) {
51099754Salc		map->needs_wakeup = FALSE;
51199754Salc		vm_map_wakeup(map);
51299754Salc	}
5131541Srgrimes	vm_map_unlock(map);
5141541Srgrimes}
5151541Srgrimes
5161541Srgrimes/*
51742957Sdillon * 	kmem_init:
51842957Sdillon *
51942957Sdillon *	Create the kernel map; insert a mapping covering kernel text,
52042957Sdillon *	data, bss, and all space allocated thus far (`boostrap' data).  The
52142957Sdillon *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
52242957Sdillon *	`start' as allocated, and the range between `start' and `end' as free.
5231541Srgrimes */
5248876Srgrimesvoid
5255455Sdgkmem_init(start, end)
5261541Srgrimes	vm_offset_t start, end;
5271541Srgrimes{
52870480Salfred	vm_map_t m;
5291541Srgrimes
53032702Sdyson	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
531108426Salc	m->system_map = 1;
5321541Srgrimes	vm_map_lock(m);
5331541Srgrimes	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
5341541Srgrimes	kernel_map = m;
535108426Salc	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
536179923Salc#ifdef __amd64__
537179923Salc	    KERNBASE,
538179923Salc#else
539179923Salc	    VM_MIN_KERNEL_ADDRESS,
540179923Salc#endif
541179923Salc	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
5421541Srgrimes	/* ... and ending with the completion of the above `insert' */
5431541Srgrimes	vm_map_unlock(m);
5441541Srgrimes}
545188964Srwatson
546188967Srwatson#ifdef DIAGNOSTIC
547188964Srwatson/*
548188964Srwatson * Allow userspace to directly trigger the VM drain routine for testing
549188964Srwatson * purposes.
550188964Srwatson */
551188964Srwatsonstatic int
552188964Srwatsondebug_vm_lowmem(SYSCTL_HANDLER_ARGS)
553188964Srwatson{
554188964Srwatson	int error, i;
555188964Srwatson
556188964Srwatson	i = 0;
557188964Srwatson	error = sysctl_handle_int(oidp, &i, 0, req);
558188964Srwatson	if (error)
559188964Srwatson		return (error);
560188964Srwatson	if (i)
561188964Srwatson		EVENTHANDLER_INVOKE(vm_lowmem, 0);
562188964Srwatson	return (0);
563188964Srwatson}
564188964Srwatson
565188964SrwatsonSYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
566188964Srwatson    debug_vm_lowmem, "I", "set to trigger vm_lowmem event");
567188967Srwatson#endif
568