xref: /freebsd/sys/vm/vm_kern.c (revision d1780e8dacb7ae7fb6a6a238301b4ae1d9d3e244)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Kernel memory management.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
7060363fb9SLuigi Rizzo #include <sys/kernel.h>		/* for ticks and hz */
710f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73f23b4c91SGarrett Wollman #include <sys/proc.h>
74a1f6d91cSDavid Greenman #include <sys/malloc.h>
7589f6b863SAttilio Rao #include <sys/rwlock.h>
7686f08737SRobert Watson #include <sys/sysctl.h>
775df87b21SJeff Roberson #include <sys/vmem.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
815df87b21SJeff Roberson #include <vm/vm_kern.h>
82efeaf95aSDavid Greenman #include <vm/pmap.h>
83efeaf95aSDavid Greenman #include <vm/vm_map.h>
84efeaf95aSDavid Greenman #include <vm/vm_object.h>
85df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
879b4288a3SBruce Evans #include <vm/vm_extern.h>
880f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h>
89df8bae1dSRodney W. Grimes 
90e7788a47SKonstantin Belousov vm_map_t kernel_map;
91e7788a47SKonstantin Belousov vm_map_t exec_map;
92cebde069SMike Silbersack vm_map_t pipe_map;
93f23b4c91SGarrett Wollman 
9489cb2a19SMatthew D Fleming const void *zero_region;
9589cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
9689cb2a19SMatthew D Fleming 
97645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */
98645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
99645743eaSJohn Baldwin 
100ec492b13SMark Johnston u_int exec_map_entry_size;
101ec492b13SMark Johnston u_int exec_map_entries;
102ec492b13SMark Johnston 
10394bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
104f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
10594bfd5b1SMarius Strobl 
10694bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
107fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__)
10894bfd5b1SMarius Strobl     &vm_max_kernel_address, 0,
10994bfd5b1SMarius Strobl #else
110f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
11194bfd5b1SMarius Strobl #endif
11294bfd5b1SMarius Strobl     "Max kernel address");
11394bfd5b1SMarius Strobl 
114df8bae1dSRodney W. Grimes /*
1155df87b21SJeff Roberson  *	kva_alloc:
116a839bdc8SDmitrij Tejblum  *
117b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
118b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
119b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
120b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
121b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
122a839bdc8SDmitrij Tejblum  */
123a839bdc8SDmitrij Tejblum vm_offset_t
1245df87b21SJeff Roberson kva_alloc(size)
125030f2369SAlfred Perlstein 	vm_size_t size;
126a839bdc8SDmitrij Tejblum {
127a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
128a839bdc8SDmitrij Tejblum 
129a839bdc8SDmitrij Tejblum 	size = round_page(size);
1305df87b21SJeff Roberson 	if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
131a839bdc8SDmitrij Tejblum 		return (0);
1325df87b21SJeff Roberson 
133a839bdc8SDmitrij Tejblum 	return (addr);
134a839bdc8SDmitrij Tejblum }
135a839bdc8SDmitrij Tejblum 
136a839bdc8SDmitrij Tejblum /*
1375df87b21SJeff Roberson  *	kva_free:
138ca596a25SJuli Mallett  *
1395df87b21SJeff Roberson  *	Release a region of kernel virtual memory allocated
1405df87b21SJeff Roberson  *	with kva_alloc, and return the physical pages
1415df87b21SJeff Roberson  *	associated with that region.
1425df87b21SJeff Roberson  *
1435df87b21SJeff Roberson  *	This routine may not block on kernel maps.
144ca596a25SJuli Mallett  */
1455df87b21SJeff Roberson void
1465df87b21SJeff Roberson kva_free(addr, size)
147ca596a25SJuli Mallett 	vm_offset_t addr;
148030f2369SAlfred Perlstein 	vm_size_t size;
149df8bae1dSRodney W. Grimes {
150df8bae1dSRodney W. Grimes 
151df8bae1dSRodney W. Grimes 	size = round_page(size);
1525df87b21SJeff Roberson 	vmem_free(kernel_arena, addr, size);
153df8bae1dSRodney W. Grimes }
154df8bae1dSRodney W. Grimes 
155df8bae1dSRodney W. Grimes /*
1560ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physical pages
1570ff0fc84SAlan Cox  *	within the specified address range to the kernel object.  Creates a
1580ff0fc84SAlan Cox  *	wired mapping from this region to these pages, and returns the
1590ff0fc84SAlan Cox  *	region's starting virtual address.  The allocated pages are not
1600ff0fc84SAlan Cox  *	necessarily physically contiguous.  If M_ZERO is specified through the
1610ff0fc84SAlan Cox  *	given flags, then the pages are zeroed before they are mapped.
1620ff0fc84SAlan Cox  */
1630ff0fc84SAlan Cox vm_offset_t
1645df87b21SJeff Roberson kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low,
1650ff0fc84SAlan Cox     vm_paddr_t high, vm_memattr_t memattr)
1660ff0fc84SAlan Cox {
1675df87b21SJeff Roberson 	vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
168*d1780e8dSKonstantin Belousov 	vm_offset_t addr, i, offset;
1690ff0fc84SAlan Cox 	vm_page_t m;
1700ff0fc84SAlan Cox 	int pflags, tries;
1710ff0fc84SAlan Cox 
1720ff0fc84SAlan Cox 	size = round_page(size);
1735df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
1740ff0fc84SAlan Cox 		return (0);
1750ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
1765df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
17789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1785df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
1790ff0fc84SAlan Cox 		tries = 0;
1800ff0fc84SAlan Cox retry:
181*d1780e8dSKonstantin Belousov 		m = vm_page_alloc_contig(object, atop(offset + i),
1825df87b21SJeff Roberson 		    pflags, 1, low, high, PAGE_SIZE, 0, memattr);
1830ff0fc84SAlan Cox 		if (m == NULL) {
18489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1850ff0fc84SAlan Cox 			if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
186c869e672SAlan Cox 				if (!vm_page_reclaim_contig(pflags, 1,
187c869e672SAlan Cox 				    low, high, PAGE_SIZE, 0) &&
188c869e672SAlan Cox 				    (flags & M_WAITOK) != 0)
189c869e672SAlan Cox 					VM_WAIT;
19089f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
1910ff0fc84SAlan Cox 				tries++;
1920ff0fc84SAlan Cox 				goto retry;
1930ff0fc84SAlan Cox 			}
1949e829b22SAlan Cox 			kmem_unback(object, addr, i);
1955df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
1960ff0fc84SAlan Cox 			return (0);
1970ff0fc84SAlan Cox 		}
1980ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
1990ff0fc84SAlan Cox 			pmap_zero_page(m);
2000ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
20139ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
20239ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
2030ff0fc84SAlan Cox 	}
20489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2050ff0fc84SAlan Cox 	return (addr);
2060ff0fc84SAlan Cox }
2070ff0fc84SAlan Cox 
2080ff0fc84SAlan Cox /*
2090ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2100ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2110ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2120ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2130ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2140ff0fc84SAlan Cox  *	mapped.
2150ff0fc84SAlan Cox  */
2160ff0fc84SAlan Cox vm_offset_t
2175df87b21SJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low,
2180ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2190ff0fc84SAlan Cox     vm_memattr_t memattr)
2200ff0fc84SAlan Cox {
2215df87b21SJeff Roberson 	vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
222*d1780e8dSKonstantin Belousov 	vm_offset_t addr, offset, tmp;
2230ff0fc84SAlan Cox 	vm_page_t end_m, m;
224c869e672SAlan Cox 	u_long npages;
2250ff0fc84SAlan Cox 	int pflags, tries;
2260ff0fc84SAlan Cox 
2270ff0fc84SAlan Cox 	size = round_page(size);
2285df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
2290ff0fc84SAlan Cox 		return (0);
2300ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
2315df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
232c869e672SAlan Cox 	npages = atop(size);
23389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2340ff0fc84SAlan Cox 	tries = 0;
2350ff0fc84SAlan Cox retry:
236*d1780e8dSKonstantin Belousov 	m = vm_page_alloc_contig(object, atop(offset), pflags,
237c869e672SAlan Cox 	    npages, low, high, alignment, boundary, memattr);
2380ff0fc84SAlan Cox 	if (m == NULL) {
23989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2400ff0fc84SAlan Cox 		if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
241c869e672SAlan Cox 			if (!vm_page_reclaim_contig(pflags, npages, low, high,
242c869e672SAlan Cox 			    alignment, boundary) && (flags & M_WAITOK) != 0)
243c869e672SAlan Cox 				VM_WAIT;
24489f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2450ff0fc84SAlan Cox 			tries++;
2460ff0fc84SAlan Cox 			goto retry;
2470ff0fc84SAlan Cox 		}
2485df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
2490ff0fc84SAlan Cox 		return (0);
2500ff0fc84SAlan Cox 	}
251c869e672SAlan Cox 	end_m = m + npages;
2525df87b21SJeff Roberson 	tmp = addr;
2530ff0fc84SAlan Cox 	for (; m < end_m; m++) {
2540ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2550ff0fc84SAlan Cox 			pmap_zero_page(m);
2560ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
25739ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL,
25839ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
2595df87b21SJeff Roberson 		tmp += PAGE_SIZE;
2600ff0fc84SAlan Cox 	}
26189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2620ff0fc84SAlan Cox 	return (addr);
2630ff0fc84SAlan Cox }
2640ff0fc84SAlan Cox 
2650ff0fc84SAlan Cox /*
266df8bae1dSRodney W. Grimes  *	kmem_suballoc:
267df8bae1dSRodney W. Grimes  *
268df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
269df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
270df8bae1dSRodney W. Grimes  *
271df8bae1dSRodney W. Grimes  *	Arguments are as follows:
272df8bae1dSRodney W. Grimes  *
273df8bae1dSRodney W. Grimes  *	parent		Map to take range from
274df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
275030f2369SAlfred Perlstein  *	size		Size of range to find
2763202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
277df8bae1dSRodney W. Grimes  */
2780d94caffSDavid Greenman vm_map_t
2793202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
2803202ed75SAlan Cox     vm_size_t size, boolean_t superpage_align)
281df8bae1dSRodney W. Grimes {
2826e4f51d1SAlfred Perlstein 	int ret;
283df8bae1dSRodney W. Grimes 	vm_map_t result;
28423955314SAlfred Perlstein 
285df8bae1dSRodney W. Grimes 	size = round_page(size);
286df8bae1dSRodney W. Grimes 
2872bc24aa9SAlan Cox 	*min = vm_map_min(parent);
288edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
2895aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
2903364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
29124dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
29224dedba9SAlan Cox 		panic("kmem_suballoc: bad status return of %d", ret);
293df8bae1dSRodney W. Grimes 	*max = *min + size;
2942d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
295df8bae1dSRodney W. Grimes 	if (result == NULL)
296df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
2976e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
298df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
299df8bae1dSRodney W. Grimes 	return (result);
300df8bae1dSRodney W. Grimes }
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes /*
3031c7c3c6aSMatthew Dillon  *	kmem_malloc:
3041c7c3c6aSMatthew Dillon  *
3055df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
306df8bae1dSRodney W. Grimes  */
307df8bae1dSRodney W. Grimes vm_offset_t
3085df87b21SJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags)
309df8bae1dSRodney W. Grimes {
310df8bae1dSRodney W. Grimes 	vm_offset_t addr;
3115df87b21SJeff Roberson 	int rv;
312df8bae1dSRodney W. Grimes 
313df8bae1dSRodney W. Grimes 	size = round_page(size);
3145df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
3155df87b21SJeff Roberson 		return (0);
316df8bae1dSRodney W. Grimes 
3175df87b21SJeff Roberson 	rv = kmem_back((vmem == kmem_arena) ? kmem_object : kernel_object,
3185df87b21SJeff Roberson 	    addr, size, flags);
3195df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
3205df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
321f31c239dSAlan Cox 		return (0);
322df8bae1dSRodney W. Grimes 	}
3235df87b21SJeff Roberson 	return (addr);
324e3813573SMatthew D Fleming }
325e3813573SMatthew D Fleming 
326e3813573SMatthew D Fleming /*
327e3813573SMatthew D Fleming  *	kmem_back:
328e3813573SMatthew D Fleming  *
329e3813573SMatthew D Fleming  *	Allocate physical pages for the specified virtual address range.
330e3813573SMatthew D Fleming  */
331e3813573SMatthew D Fleming int
3325df87b21SJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
333e3813573SMatthew D Fleming {
334e3813573SMatthew D Fleming 	vm_offset_t offset, i;
335e3813573SMatthew D Fleming 	vm_page_t m;
336e3813573SMatthew D Fleming 	int pflags;
337e3813573SMatthew D Fleming 
3385df87b21SJeff Roberson 	KASSERT(object == kmem_object || object == kernel_object,
3395df87b21SJeff Roberson 	    ("kmem_back: only supports kernel objects."));
3405df87b21SJeff Roberson 
3410891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
3425df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
343df8bae1dSRodney W. Grimes 
3445df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
3451e081f88SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
3461e081f88SJeff Roberson retry:
347*d1780e8dSKonstantin Belousov 		m = vm_page_alloc(object, atop(offset + i), pflags);
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes 		/*
3500d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
3510d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
3520d94caffSDavid Greenman 		 * aren't on any queues.
353df8bae1dSRodney W. Grimes 		 */
354df8bae1dSRodney W. Grimes 		if (m == NULL) {
3555df87b21SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
3569e829b22SAlan Cox 			if ((flags & M_NOWAIT) == 0) {
357b18bfc3dSJohn Dyson 				VM_WAIT;
3585df87b21SJeff Roberson 				VM_OBJECT_WLOCK(object);
359b18bfc3dSJohn Dyson 				goto retry;
360b18bfc3dSJohn Dyson 			}
3619e829b22SAlan Cox 			kmem_unback(object, addr, i);
362e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
363df8bae1dSRodney W. Grimes 		}
3641e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
365fff6062aSAlan Cox 			pmap_zero_page(m);
366d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
3679f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
3685df87b21SJeff Roberson 		m->valid = VM_PAGE_BITS_ALL;
36939ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
37039ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
371df8bae1dSRodney W. Grimes 	}
3725df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
373df8bae1dSRodney W. Grimes 
374e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
375df8bae1dSRodney W. Grimes }
376df8bae1dSRodney W. Grimes 
3779e829b22SAlan Cox /*
3789e829b22SAlan Cox  *	kmem_unback:
3799e829b22SAlan Cox  *
3809e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
3819e829b22SAlan Cox  *	address range.
3829e829b22SAlan Cox  *
3839e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
3849e829b22SAlan Cox  *	that is being unmapped.
3859e829b22SAlan Cox  */
3865df87b21SJeff Roberson void
3875df87b21SJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
3885df87b21SJeff Roberson {
3895df87b21SJeff Roberson 	vm_page_t m;
3905cd7a4f7SAlan Cox 	vm_offset_t i, offset;
3915df87b21SJeff Roberson 
3925df87b21SJeff Roberson 	KASSERT(object == kmem_object || object == kernel_object,
3935df87b21SJeff Roberson 	    ("kmem_unback: only supports kernel objects."));
3945df87b21SJeff Roberson 
395fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
3965df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
3975df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
3985df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
399*d1780e8dSKonstantin Belousov 		m = vm_page_lookup(object, atop(offset + i));
40027e9ed8aSAlan Cox 		vm_page_unwire(m, PQ_NONE);
4015df87b21SJeff Roberson 		vm_page_free(m);
4025df87b21SJeff Roberson 	}
4035df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
4045df87b21SJeff Roberson }
4055df87b21SJeff Roberson 
406df8bae1dSRodney W. Grimes /*
4075df87b21SJeff Roberson  *	kmem_free:
4085df87b21SJeff Roberson  *
4095df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
4105df87b21SJeff Roberson  *	original allocation.
4115df87b21SJeff Roberson  */
4125df87b21SJeff Roberson void
4135df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size)
4145df87b21SJeff Roberson {
4155df87b21SJeff Roberson 
4165df87b21SJeff Roberson 	size = round_page(size);
4175df87b21SJeff Roberson 	kmem_unback((vmem == kmem_arena) ? kmem_object : kernel_object,
4185df87b21SJeff Roberson 	    addr, size);
4195df87b21SJeff Roberson 	vmem_free(vmem, addr, size);
4205df87b21SJeff Roberson }
4215df87b21SJeff Roberson 
4225df87b21SJeff Roberson /*
4235df87b21SJeff Roberson  *	kmap_alloc_wait:
424df8bae1dSRodney W. Grimes  *
425df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
426df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
427df8bae1dSRodney W. Grimes  *
4281c7c3c6aSMatthew Dillon  *	This routine may block.
429df8bae1dSRodney W. Grimes  */
4300d94caffSDavid Greenman vm_offset_t
4315df87b21SJeff Roberson kmap_alloc_wait(map, size)
432df8bae1dSRodney W. Grimes 	vm_map_t map;
433df8bae1dSRodney W. Grimes 	vm_size_t size;
434df8bae1dSRodney W. Grimes {
435df8bae1dSRodney W. Grimes 	vm_offset_t addr;
43623955314SAlfred Perlstein 
437df8bae1dSRodney W. Grimes 	size = round_page(size);
4383364c323SKonstantin Belousov 	if (!swap_reserve(size))
4393364c323SKonstantin Belousov 		return (0);
440df8bae1dSRodney W. Grimes 
441df8bae1dSRodney W. Grimes 	for (;;) {
442df8bae1dSRodney W. Grimes 		/*
4430d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
4440d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
445df8bae1dSRodney W. Grimes 		 */
446df8bae1dSRodney W. Grimes 		vm_map_lock(map);
447e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
448df8bae1dSRodney W. Grimes 			break;
449df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
450df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
451df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
4523364c323SKonstantin Belousov 			swap_release(size);
453df8bae1dSRodney W. Grimes 			return (0);
454df8bae1dSRodney W. Grimes 		}
4559688f931SAlan Cox 		map->needs_wakeup = TRUE;
4568ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
457df8bae1dSRodney W. Grimes 	}
4583364c323SKonstantin Belousov 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
4593364c323SKonstantin Belousov 	    VM_PROT_ALL, MAP_ACC_CHARGED);
460df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
461df8bae1dSRodney W. Grimes 	return (addr);
462df8bae1dSRodney W. Grimes }
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes /*
4655df87b21SJeff Roberson  *	kmap_free_wakeup:
466df8bae1dSRodney W. Grimes  *
46724a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
468df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
469df8bae1dSRodney W. Grimes  */
4700d94caffSDavid Greenman void
4715df87b21SJeff Roberson kmap_free_wakeup(map, addr, size)
472df8bae1dSRodney W. Grimes 	vm_map_t map;
473df8bae1dSRodney W. Grimes 	vm_offset_t addr;
474df8bae1dSRodney W. Grimes 	vm_size_t size;
475df8bae1dSRodney W. Grimes {
47623955314SAlfred Perlstein 
477df8bae1dSRodney W. Grimes 	vm_map_lock(map);
478655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
4799688f931SAlan Cox 	if (map->needs_wakeup) {
4809688f931SAlan Cox 		map->needs_wakeup = FALSE;
4819688f931SAlan Cox 		vm_map_wakeup(map);
4829688f931SAlan Cox 	}
483df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
484df8bae1dSRodney W. Grimes }
485df8bae1dSRodney W. Grimes 
4865df87b21SJeff Roberson void
48789cb2a19SMatthew D Fleming kmem_init_zero_region(void)
48889cb2a19SMatthew D Fleming {
489cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
49089cb2a19SMatthew D Fleming 	vm_page_t m;
49189cb2a19SMatthew D Fleming 
492cfb00e5aSMatthew D Fleming 	/*
493cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
494cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
495cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
496cfb00e5aSMatthew D Fleming 	 */
4975df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
498703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
49989cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
50089cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
50189cb2a19SMatthew D Fleming 		pmap_zero_page(m);
50289cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
50389cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
5045df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
50589cb2a19SMatthew D Fleming 
50689cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
50789cb2a19SMatthew D Fleming }
50889cb2a19SMatthew D Fleming 
509df8bae1dSRodney W. Grimes /*
5101c7c3c6aSMatthew Dillon  * 	kmem_init:
5111c7c3c6aSMatthew Dillon  *
5121c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
5131c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
5141c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
5151c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
516df8bae1dSRodney W. Grimes  */
5170d94caffSDavid Greenman void
5180d94caffSDavid Greenman kmem_init(start, end)
519df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
520df8bae1dSRodney W. Grimes {
521030f2369SAlfred Perlstein 	vm_map_t m;
522df8bae1dSRodney W. Grimes 
5232d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
524c9267356SAlan Cox 	m->system_map = 1;
525df8bae1dSRodney W. Grimes 	vm_map_lock(m);
526df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
527df8bae1dSRodney W. Grimes 	kernel_map = m;
528c9267356SAlan Cox 	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
5295cfa90e9SAlan Cox #ifdef __amd64__
5305cfa90e9SAlan Cox 	    KERNBASE,
5315cfa90e9SAlan Cox #else
5325cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
5335cfa90e9SAlan Cox #endif
5345cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
535df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
536df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
537df8bae1dSRodney W. Grimes }
53886f08737SRobert Watson 
5399309e63cSRobert Watson #ifdef DIAGNOSTIC
54086f08737SRobert Watson /*
54186f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
54286f08737SRobert Watson  * purposes.
54386f08737SRobert Watson  */
54486f08737SRobert Watson static int
54586f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
54686f08737SRobert Watson {
54786f08737SRobert Watson 	int error, i;
54886f08737SRobert Watson 
54986f08737SRobert Watson 	i = 0;
55086f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
55186f08737SRobert Watson 	if (error)
55286f08737SRobert Watson 		return (error);
5539b43bc27SAndriy Gapon 	if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
5549b43bc27SAndriy Gapon 		return (EINVAL);
5559b43bc27SAndriy Gapon 	if (i != 0)
5569b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, i);
55786f08737SRobert Watson 	return (0);
55886f08737SRobert Watson }
55986f08737SRobert Watson 
56086f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
5619b43bc27SAndriy Gapon     debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");
5629309e63cSRobert Watson #endif
563