xref: /freebsd/sys/vm/vm_kern.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
16*fbbd9655SWarner 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;
1689e829b22SAlan Cox 	vm_offset_t addr, i;
1695df87b21SJeff Roberson 	vm_ooffset_t offset;
1700ff0fc84SAlan Cox 	vm_page_t m;
1710ff0fc84SAlan Cox 	int pflags, tries;
1720ff0fc84SAlan Cox 
1730ff0fc84SAlan Cox 	size = round_page(size);
1745df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
1750ff0fc84SAlan Cox 		return (0);
1760ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
1775df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
17889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1795df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
1800ff0fc84SAlan Cox 		tries = 0;
1810ff0fc84SAlan Cox retry:
1825df87b21SJeff Roberson 		m = vm_page_alloc_contig(object, OFF_TO_IDX(offset + i),
1835df87b21SJeff Roberson 		    pflags, 1, low, high, PAGE_SIZE, 0, memattr);
1840ff0fc84SAlan Cox 		if (m == NULL) {
18589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1860ff0fc84SAlan Cox 			if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
187c869e672SAlan Cox 				if (!vm_page_reclaim_contig(pflags, 1,
188c869e672SAlan Cox 				    low, high, PAGE_SIZE, 0) &&
189c869e672SAlan Cox 				    (flags & M_WAITOK) != 0)
190c869e672SAlan Cox 					VM_WAIT;
19189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
1920ff0fc84SAlan Cox 				tries++;
1930ff0fc84SAlan Cox 				goto retry;
1940ff0fc84SAlan Cox 			}
1959e829b22SAlan Cox 			kmem_unback(object, addr, i);
1965df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
1970ff0fc84SAlan Cox 			return (0);
1980ff0fc84SAlan Cox 		}
1990ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2000ff0fc84SAlan Cox 			pmap_zero_page(m);
2010ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
20239ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
20339ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
2040ff0fc84SAlan Cox 	}
20589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2060ff0fc84SAlan Cox 	return (addr);
2070ff0fc84SAlan Cox }
2080ff0fc84SAlan Cox 
2090ff0fc84SAlan Cox /*
2100ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2110ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2120ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2130ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2140ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2150ff0fc84SAlan Cox  *	mapped.
2160ff0fc84SAlan Cox  */
2170ff0fc84SAlan Cox vm_offset_t
2185df87b21SJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low,
2190ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2200ff0fc84SAlan Cox     vm_memattr_t memattr)
2210ff0fc84SAlan Cox {
2225df87b21SJeff Roberson 	vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
2235df87b21SJeff Roberson 	vm_offset_t addr, tmp;
2240ff0fc84SAlan Cox 	vm_ooffset_t offset;
2250ff0fc84SAlan Cox 	vm_page_t end_m, m;
226c869e672SAlan Cox 	u_long npages;
2270ff0fc84SAlan Cox 	int pflags, tries;
2280ff0fc84SAlan Cox 
2290ff0fc84SAlan Cox 	size = round_page(size);
2305df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
2310ff0fc84SAlan Cox 		return (0);
2320ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
2335df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
234c869e672SAlan Cox 	npages = atop(size);
23589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2360ff0fc84SAlan Cox 	tries = 0;
2370ff0fc84SAlan Cox retry:
2380ff0fc84SAlan Cox 	m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags,
239c869e672SAlan Cox 	    npages, low, high, alignment, boundary, memattr);
2400ff0fc84SAlan Cox 	if (m == NULL) {
24189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2420ff0fc84SAlan Cox 		if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
243c869e672SAlan Cox 			if (!vm_page_reclaim_contig(pflags, npages, low, high,
244c869e672SAlan Cox 			    alignment, boundary) && (flags & M_WAITOK) != 0)
245c869e672SAlan Cox 				VM_WAIT;
24689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2470ff0fc84SAlan Cox 			tries++;
2480ff0fc84SAlan Cox 			goto retry;
2490ff0fc84SAlan Cox 		}
2505df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
2510ff0fc84SAlan Cox 		return (0);
2520ff0fc84SAlan Cox 	}
253c869e672SAlan Cox 	end_m = m + npages;
2545df87b21SJeff Roberson 	tmp = addr;
2550ff0fc84SAlan Cox 	for (; m < end_m; m++) {
2560ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2570ff0fc84SAlan Cox 			pmap_zero_page(m);
2580ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
25939ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL,
26039ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
2615df87b21SJeff Roberson 		tmp += PAGE_SIZE;
2620ff0fc84SAlan Cox 	}
26389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2640ff0fc84SAlan Cox 	return (addr);
2650ff0fc84SAlan Cox }
2660ff0fc84SAlan Cox 
2670ff0fc84SAlan Cox /*
268df8bae1dSRodney W. Grimes  *	kmem_suballoc:
269df8bae1dSRodney W. Grimes  *
270df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
271df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
272df8bae1dSRodney W. Grimes  *
273df8bae1dSRodney W. Grimes  *	Arguments are as follows:
274df8bae1dSRodney W. Grimes  *
275df8bae1dSRodney W. Grimes  *	parent		Map to take range from
276df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
277030f2369SAlfred Perlstein  *	size		Size of range to find
2783202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
279df8bae1dSRodney W. Grimes  */
2800d94caffSDavid Greenman vm_map_t
2813202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
2823202ed75SAlan Cox     vm_size_t size, boolean_t superpage_align)
283df8bae1dSRodney W. Grimes {
2846e4f51d1SAlfred Perlstein 	int ret;
285df8bae1dSRodney W. Grimes 	vm_map_t result;
28623955314SAlfred Perlstein 
287df8bae1dSRodney W. Grimes 	size = round_page(size);
288df8bae1dSRodney W. Grimes 
2892bc24aa9SAlan Cox 	*min = vm_map_min(parent);
290edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
2915aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
2923364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
29324dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
29424dedba9SAlan Cox 		panic("kmem_suballoc: bad status return of %d", ret);
295df8bae1dSRodney W. Grimes 	*max = *min + size;
2962d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
297df8bae1dSRodney W. Grimes 	if (result == NULL)
298df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
2996e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
300df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
301df8bae1dSRodney W. Grimes 	return (result);
302df8bae1dSRodney W. Grimes }
303df8bae1dSRodney W. Grimes 
304df8bae1dSRodney W. Grimes /*
3051c7c3c6aSMatthew Dillon  *	kmem_malloc:
3061c7c3c6aSMatthew Dillon  *
3075df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
308df8bae1dSRodney W. Grimes  */
309df8bae1dSRodney W. Grimes vm_offset_t
3105df87b21SJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags)
311df8bae1dSRodney W. Grimes {
312df8bae1dSRodney W. Grimes 	vm_offset_t addr;
3135df87b21SJeff Roberson 	int rv;
314df8bae1dSRodney W. Grimes 
315df8bae1dSRodney W. Grimes 	size = round_page(size);
3165df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
3175df87b21SJeff Roberson 		return (0);
318df8bae1dSRodney W. Grimes 
3195df87b21SJeff Roberson 	rv = kmem_back((vmem == kmem_arena) ? kmem_object : kernel_object,
3205df87b21SJeff Roberson 	    addr, size, flags);
3215df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
3225df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
323f31c239dSAlan Cox 		return (0);
324df8bae1dSRodney W. Grimes 	}
3255df87b21SJeff Roberson 	return (addr);
326e3813573SMatthew D Fleming }
327e3813573SMatthew D Fleming 
328e3813573SMatthew D Fleming /*
329e3813573SMatthew D Fleming  *	kmem_back:
330e3813573SMatthew D Fleming  *
331e3813573SMatthew D Fleming  *	Allocate physical pages for the specified virtual address range.
332e3813573SMatthew D Fleming  */
333e3813573SMatthew D Fleming int
3345df87b21SJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
335e3813573SMatthew D Fleming {
336e3813573SMatthew D Fleming 	vm_offset_t offset, i;
337e3813573SMatthew D Fleming 	vm_page_t m;
338e3813573SMatthew D Fleming 	int pflags;
339e3813573SMatthew D Fleming 
3405df87b21SJeff Roberson 	KASSERT(object == kmem_object || object == kernel_object,
3415df87b21SJeff Roberson 	    ("kmem_back: only supports kernel objects."));
3425df87b21SJeff Roberson 
3430891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
3445df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
345df8bae1dSRodney W. Grimes 
3465df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
3471e081f88SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
3481e081f88SJeff Roberson retry:
3495df87b21SJeff Roberson 		m = vm_page_alloc(object, OFF_TO_IDX(offset + i), pflags);
350df8bae1dSRodney W. Grimes 
351df8bae1dSRodney W. Grimes 		/*
3520d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
3530d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
3540d94caffSDavid Greenman 		 * aren't on any queues.
355df8bae1dSRodney W. Grimes 		 */
356df8bae1dSRodney W. Grimes 		if (m == NULL) {
3575df87b21SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
3589e829b22SAlan Cox 			if ((flags & M_NOWAIT) == 0) {
359b18bfc3dSJohn Dyson 				VM_WAIT;
3605df87b21SJeff Roberson 				VM_OBJECT_WLOCK(object);
361b18bfc3dSJohn Dyson 				goto retry;
362b18bfc3dSJohn Dyson 			}
3639e829b22SAlan Cox 			kmem_unback(object, addr, i);
364e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
365df8bae1dSRodney W. Grimes 		}
3661e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
367fff6062aSAlan Cox 			pmap_zero_page(m);
368d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
3699f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
3705df87b21SJeff Roberson 		m->valid = VM_PAGE_BITS_ALL;
37139ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
37239ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
373df8bae1dSRodney W. Grimes 	}
3745df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
375df8bae1dSRodney W. Grimes 
376e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
377df8bae1dSRodney W. Grimes }
378df8bae1dSRodney W. Grimes 
3799e829b22SAlan Cox /*
3809e829b22SAlan Cox  *	kmem_unback:
3819e829b22SAlan Cox  *
3829e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
3839e829b22SAlan Cox  *	address range.
3849e829b22SAlan Cox  *
3859e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
3869e829b22SAlan Cox  *	that is being unmapped.
3879e829b22SAlan Cox  */
3885df87b21SJeff Roberson void
3895df87b21SJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
3905df87b21SJeff Roberson {
3915df87b21SJeff Roberson 	vm_page_t m;
3925cd7a4f7SAlan Cox 	vm_offset_t i, offset;
3935df87b21SJeff Roberson 
3945df87b21SJeff Roberson 	KASSERT(object == kmem_object || object == kernel_object,
3955df87b21SJeff Roberson 	    ("kmem_unback: only supports kernel objects."));
3965df87b21SJeff Roberson 
397fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
3985df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
3995df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
4005df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
4015df87b21SJeff Roberson 		m = vm_page_lookup(object, OFF_TO_IDX(offset + i));
40227e9ed8aSAlan Cox 		vm_page_unwire(m, PQ_NONE);
4035df87b21SJeff Roberson 		vm_page_free(m);
4045df87b21SJeff Roberson 	}
4055df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
4065df87b21SJeff Roberson }
4075df87b21SJeff Roberson 
408df8bae1dSRodney W. Grimes /*
4095df87b21SJeff Roberson  *	kmem_free:
4105df87b21SJeff Roberson  *
4115df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
4125df87b21SJeff Roberson  *	original allocation.
4135df87b21SJeff Roberson  */
4145df87b21SJeff Roberson void
4155df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size)
4165df87b21SJeff Roberson {
4175df87b21SJeff Roberson 
4185df87b21SJeff Roberson 	size = round_page(size);
4195df87b21SJeff Roberson 	kmem_unback((vmem == kmem_arena) ? kmem_object : kernel_object,
4205df87b21SJeff Roberson 	    addr, size);
4215df87b21SJeff Roberson 	vmem_free(vmem, addr, size);
4225df87b21SJeff Roberson }
4235df87b21SJeff Roberson 
4245df87b21SJeff Roberson /*
4255df87b21SJeff Roberson  *	kmap_alloc_wait:
426df8bae1dSRodney W. Grimes  *
427df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
428df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
429df8bae1dSRodney W. Grimes  *
4301c7c3c6aSMatthew Dillon  *	This routine may block.
431df8bae1dSRodney W. Grimes  */
4320d94caffSDavid Greenman vm_offset_t
4335df87b21SJeff Roberson kmap_alloc_wait(map, size)
434df8bae1dSRodney W. Grimes 	vm_map_t map;
435df8bae1dSRodney W. Grimes 	vm_size_t size;
436df8bae1dSRodney W. Grimes {
437df8bae1dSRodney W. Grimes 	vm_offset_t addr;
43823955314SAlfred Perlstein 
439df8bae1dSRodney W. Grimes 	size = round_page(size);
4403364c323SKonstantin Belousov 	if (!swap_reserve(size))
4413364c323SKonstantin Belousov 		return (0);
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	for (;;) {
444df8bae1dSRodney W. Grimes 		/*
4450d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
4460d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
447df8bae1dSRodney W. Grimes 		 */
448df8bae1dSRodney W. Grimes 		vm_map_lock(map);
449e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
450df8bae1dSRodney W. Grimes 			break;
451df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
452df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
453df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
4543364c323SKonstantin Belousov 			swap_release(size);
455df8bae1dSRodney W. Grimes 			return (0);
456df8bae1dSRodney W. Grimes 		}
4579688f931SAlan Cox 		map->needs_wakeup = TRUE;
4588ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
459df8bae1dSRodney W. Grimes 	}
4603364c323SKonstantin Belousov 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
4613364c323SKonstantin Belousov 	    VM_PROT_ALL, MAP_ACC_CHARGED);
462df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
463df8bae1dSRodney W. Grimes 	return (addr);
464df8bae1dSRodney W. Grimes }
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes /*
4675df87b21SJeff Roberson  *	kmap_free_wakeup:
468df8bae1dSRodney W. Grimes  *
46924a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
470df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
471df8bae1dSRodney W. Grimes  */
4720d94caffSDavid Greenman void
4735df87b21SJeff Roberson kmap_free_wakeup(map, addr, size)
474df8bae1dSRodney W. Grimes 	vm_map_t map;
475df8bae1dSRodney W. Grimes 	vm_offset_t addr;
476df8bae1dSRodney W. Grimes 	vm_size_t size;
477df8bae1dSRodney W. Grimes {
47823955314SAlfred Perlstein 
479df8bae1dSRodney W. Grimes 	vm_map_lock(map);
480655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
4819688f931SAlan Cox 	if (map->needs_wakeup) {
4829688f931SAlan Cox 		map->needs_wakeup = FALSE;
4839688f931SAlan Cox 		vm_map_wakeup(map);
4849688f931SAlan Cox 	}
485df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
486df8bae1dSRodney W. Grimes }
487df8bae1dSRodney W. Grimes 
4885df87b21SJeff Roberson void
48989cb2a19SMatthew D Fleming kmem_init_zero_region(void)
49089cb2a19SMatthew D Fleming {
491cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
49289cb2a19SMatthew D Fleming 	vm_page_t m;
49389cb2a19SMatthew D Fleming 
494cfb00e5aSMatthew D Fleming 	/*
495cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
496cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
497cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
498cfb00e5aSMatthew D Fleming 	 */
4995df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
500703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
50189cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
50289cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
50389cb2a19SMatthew D Fleming 		pmap_zero_page(m);
50489cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
50589cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
5065df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
50789cb2a19SMatthew D Fleming 
50889cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
50989cb2a19SMatthew D Fleming }
51089cb2a19SMatthew D Fleming 
511df8bae1dSRodney W. Grimes /*
5121c7c3c6aSMatthew Dillon  * 	kmem_init:
5131c7c3c6aSMatthew Dillon  *
5141c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
5151c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
5161c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
5171c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
518df8bae1dSRodney W. Grimes  */
5190d94caffSDavid Greenman void
5200d94caffSDavid Greenman kmem_init(start, end)
521df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
522df8bae1dSRodney W. Grimes {
523030f2369SAlfred Perlstein 	vm_map_t m;
524df8bae1dSRodney W. Grimes 
5252d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
526c9267356SAlan Cox 	m->system_map = 1;
527df8bae1dSRodney W. Grimes 	vm_map_lock(m);
528df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
529df8bae1dSRodney W. Grimes 	kernel_map = m;
530c9267356SAlan Cox 	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
5315cfa90e9SAlan Cox #ifdef __amd64__
5325cfa90e9SAlan Cox 	    KERNBASE,
5335cfa90e9SAlan Cox #else
5345cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
5355cfa90e9SAlan Cox #endif
5365cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
537df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
538df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
539df8bae1dSRodney W. Grimes }
54086f08737SRobert Watson 
5419309e63cSRobert Watson #ifdef DIAGNOSTIC
54286f08737SRobert Watson /*
54386f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
54486f08737SRobert Watson  * purposes.
54586f08737SRobert Watson  */
54686f08737SRobert Watson static int
54786f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
54886f08737SRobert Watson {
54986f08737SRobert Watson 	int error, i;
55086f08737SRobert Watson 
55186f08737SRobert Watson 	i = 0;
55286f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
55386f08737SRobert Watson 	if (error)
55486f08737SRobert Watson 		return (error);
5559b43bc27SAndriy Gapon 	if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
5569b43bc27SAndriy Gapon 		return (EINVAL);
5579b43bc27SAndriy Gapon 	if (i != 0)
5589b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, i);
55986f08737SRobert Watson 	return (0);
56086f08737SRobert Watson }
56186f08737SRobert Watson 
56286f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
5639b43bc27SAndriy Gapon     debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");
5649309e63cSRobert Watson #endif
565