xref: /freebsd/sys/vm/vm_kern.c (revision 30c5525b3cd28170e736202510ab64d51aa703bf)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
343c4dd356SDavid Greenman  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38df8bae1dSRodney W. Grimes  * All rights reserved.
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  *	Kernel memory management.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67874651b1SDavid E. O'Brien #include <sys/cdefs.h>
68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
69874651b1SDavid E. O'Brien 
707a469c8eSJeff Roberson #include "opt_vm.h"
717a469c8eSJeff Roberson 
72df8bae1dSRodney W. Grimes #include <sys/param.h>
73df8bae1dSRodney W. Grimes #include <sys/systm.h>
7460363fb9SLuigi Rizzo #include <sys/kernel.h>		/* for ticks and hz */
757a469c8eSJeff Roberson #include <sys/domainset.h>
760f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h>
77fb919e4dSMark Murray #include <sys/lock.h>
78f23b4c91SGarrett Wollman #include <sys/proc.h>
79a1f6d91cSDavid Greenman #include <sys/malloc.h>
8089f6b863SAttilio Rao #include <sys/rwlock.h>
8186f08737SRobert Watson #include <sys/sysctl.h>
825df87b21SJeff Roberson #include <sys/vmem.h>
837a469c8eSJeff Roberson #include <sys/vmmeter.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
877a469c8eSJeff Roberson #include <vm/vm_domainset.h>
885df87b21SJeff Roberson #include <vm/vm_kern.h>
89efeaf95aSDavid Greenman #include <vm/pmap.h>
90efeaf95aSDavid Greenman #include <vm/vm_map.h>
91efeaf95aSDavid Greenman #include <vm/vm_object.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
947a469c8eSJeff Roberson #include <vm/vm_phys.h>
95e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
9633fff5d5SMark Johnston #include <vm/vm_radix.h>
979b4288a3SBruce Evans #include <vm/vm_extern.h>
980f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h>
99df8bae1dSRodney W. Grimes 
100e7788a47SKonstantin Belousov vm_map_t kernel_map;
101e7788a47SKonstantin Belousov vm_map_t exec_map;
102cebde069SMike Silbersack vm_map_t pipe_map;
103f23b4c91SGarrett Wollman 
10489cb2a19SMatthew D Fleming const void *zero_region;
10589cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
10689cb2a19SMatthew D Fleming 
107645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */
108645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
109645743eaSJohn Baldwin 
110ec492b13SMark Johnston u_int exec_map_entry_size;
111ec492b13SMark Johnston u_int exec_map_entries;
112ec492b13SMark Johnston 
11394bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
114f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
11594bfd5b1SMarius Strobl 
11694bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
117fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__)
11894bfd5b1SMarius Strobl     &vm_max_kernel_address, 0,
11994bfd5b1SMarius Strobl #else
120f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
12194bfd5b1SMarius Strobl #endif
12294bfd5b1SMarius Strobl     "Max kernel address");
12394bfd5b1SMarius Strobl 
1241aed6d48SMark Johnston #if VM_NRESERVLEVEL > 0
12525ed23cfSMark Johnston #define	KVA_QUANTUM_SHIFT	(VM_LEVEL_0_ORDER + PAGE_SHIFT)
1261aed6d48SMark Johnston #else
1271aed6d48SMark Johnston /* On non-superpage architectures want large import sizes. */
12825ed23cfSMark Johnston #define	KVA_QUANTUM_SHIFT	(10 + PAGE_SHIFT)
1291aed6d48SMark Johnston #endif
13025ed23cfSMark Johnston #define	KVA_QUANTUM		(1 << KVA_QUANTUM_SHIFT)
1311aed6d48SMark Johnston 
132df8bae1dSRodney W. Grimes /*
1335df87b21SJeff Roberson  *	kva_alloc:
134a839bdc8SDmitrij Tejblum  *
135b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
136b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
137b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
138b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
139b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
140a839bdc8SDmitrij Tejblum  */
141a839bdc8SDmitrij Tejblum vm_offset_t
1426e309d75SEd Maste kva_alloc(vm_size_t size)
143a839bdc8SDmitrij Tejblum {
144a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
145a839bdc8SDmitrij Tejblum 
146a839bdc8SDmitrij Tejblum 	size = round_page(size);
1475df87b21SJeff Roberson 	if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
148a839bdc8SDmitrij Tejblum 		return (0);
1495df87b21SJeff Roberson 
150a839bdc8SDmitrij Tejblum 	return (addr);
151a839bdc8SDmitrij Tejblum }
152a839bdc8SDmitrij Tejblum 
153a839bdc8SDmitrij Tejblum /*
1545df87b21SJeff Roberson  *	kva_free:
155ca596a25SJuli Mallett  *
1565df87b21SJeff Roberson  *	Release a region of kernel virtual memory allocated
1575df87b21SJeff Roberson  *	with kva_alloc, and return the physical pages
1585df87b21SJeff Roberson  *	associated with that region.
1595df87b21SJeff Roberson  *
1605df87b21SJeff Roberson  *	This routine may not block on kernel maps.
161ca596a25SJuli Mallett  */
1625df87b21SJeff Roberson void
1636e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size)
164df8bae1dSRodney W. Grimes {
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes 	size = round_page(size);
1675df87b21SJeff Roberson 	vmem_free(kernel_arena, addr, size);
168df8bae1dSRodney W. Grimes }
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes /*
1710ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physical pages
1720ff0fc84SAlan Cox  *	within the specified address range to the kernel object.  Creates a
1730ff0fc84SAlan Cox  *	wired mapping from this region to these pages, and returns the
1740ff0fc84SAlan Cox  *	region's starting virtual address.  The allocated pages are not
1750ff0fc84SAlan Cox  *	necessarily physically contiguous.  If M_ZERO is specified through the
1760ff0fc84SAlan Cox  *	given flags, then the pages are zeroed before they are mapped.
1770ff0fc84SAlan Cox  */
1780ff0fc84SAlan Cox vm_offset_t
1797a469c8eSJeff Roberson kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
1800ff0fc84SAlan Cox     vm_paddr_t high, vm_memattr_t memattr)
1810ff0fc84SAlan Cox {
1827a469c8eSJeff Roberson 	vmem_t *vmem;
1832e47807cSJeff Roberson 	vm_object_t object = kernel_object;
184d1780e8dSKonstantin Belousov 	vm_offset_t addr, i, offset;
1850ff0fc84SAlan Cox 	vm_page_t m;
1860ff0fc84SAlan Cox 	int pflags, tries;
1870ff0fc84SAlan Cox 
1880ff0fc84SAlan Cox 	size = round_page(size);
1897a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
1905df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
1910ff0fc84SAlan Cox 		return (0);
1920ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
1935df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
1948d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
1958d6fbbb8SJeff Roberson 	pflags |= VM_ALLOC_NOWAIT;
19689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1975df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
1980ff0fc84SAlan Cox 		tries = 0;
1990ff0fc84SAlan Cox retry:
2007a469c8eSJeff Roberson 		m = vm_page_alloc_contig_domain(object, atop(offset + i),
2017a469c8eSJeff Roberson 		    domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
2020ff0fc84SAlan Cox 		if (m == NULL) {
20389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
2040ff0fc84SAlan Cox 			if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
2057a469c8eSJeff Roberson 				if (!vm_page_reclaim_contig_domain(domain,
2067a469c8eSJeff Roberson 				    pflags, 1, low, high, PAGE_SIZE, 0) &&
207c869e672SAlan Cox 				    (flags & M_WAITOK) != 0)
208e2068d0bSJeff Roberson 					vm_wait_domain(domain);
20989f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
2100ff0fc84SAlan Cox 				tries++;
2110ff0fc84SAlan Cox 				goto retry;
2120ff0fc84SAlan Cox 			}
2139e829b22SAlan Cox 			kmem_unback(object, addr, i);
2145df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
2150ff0fc84SAlan Cox 			return (0);
2160ff0fc84SAlan Cox 		}
217e2068d0bSJeff Roberson 		KASSERT(vm_phys_domain(m) == domain,
2187a469c8eSJeff Roberson 		    ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
219e2068d0bSJeff Roberson 		    vm_phys_domain(m), domain));
2200ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2210ff0fc84SAlan Cox 			pmap_zero_page(m);
2220ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
2230766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_RW,
2240766f278SJonathan T. Looney 		    VM_PROT_RW | PMAP_ENTER_WIRED, 0);
2250ff0fc84SAlan Cox 	}
22689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2270ff0fc84SAlan Cox 	return (addr);
2280ff0fc84SAlan Cox }
2290ff0fc84SAlan Cox 
2307a469c8eSJeff Roberson vm_offset_t
231db7c2a48SAlan Cox kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
232db7c2a48SAlan Cox     vm_memattr_t memattr)
2337a469c8eSJeff Roberson {
2347a469c8eSJeff Roberson 	struct vm_domainset_iter di;
2357a469c8eSJeff Roberson 	vm_offset_t addr;
2367a469c8eSJeff Roberson 	int domain;
2377a469c8eSJeff Roberson 
2387a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
2397a469c8eSJeff Roberson 	do {
2407a469c8eSJeff Roberson 		addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
2417a469c8eSJeff Roberson 		    memattr);
2427a469c8eSJeff Roberson 		if (addr != 0)
2437a469c8eSJeff Roberson 			break;
2447a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
2457a469c8eSJeff Roberson 
2467a469c8eSJeff Roberson 	return (addr);
2477a469c8eSJeff Roberson }
2487a469c8eSJeff Roberson 
2490ff0fc84SAlan Cox /*
2500ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2510ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2520ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2530ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2540ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2550ff0fc84SAlan Cox  *	mapped.
2560ff0fc84SAlan Cox  */
2570ff0fc84SAlan Cox vm_offset_t
2587a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
2590ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2600ff0fc84SAlan Cox     vm_memattr_t memattr)
2610ff0fc84SAlan Cox {
2627a469c8eSJeff Roberson 	vmem_t *vmem;
2632e47807cSJeff Roberson 	vm_object_t object = kernel_object;
264d1780e8dSKonstantin Belousov 	vm_offset_t addr, offset, tmp;
2650ff0fc84SAlan Cox 	vm_page_t end_m, m;
266c869e672SAlan Cox 	u_long npages;
2670ff0fc84SAlan Cox 	int pflags, tries;
2680ff0fc84SAlan Cox 
2690ff0fc84SAlan Cox 	size = round_page(size);
2707a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
2715df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
2720ff0fc84SAlan Cox 		return (0);
2730ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
2745df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
2758d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
2768d6fbbb8SJeff Roberson 	pflags |= VM_ALLOC_NOWAIT;
277c869e672SAlan Cox 	npages = atop(size);
27889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2790ff0fc84SAlan Cox 	tries = 0;
2800ff0fc84SAlan Cox retry:
2817a469c8eSJeff Roberson 	m = vm_page_alloc_contig_domain(object, atop(offset), domain, pflags,
282c869e672SAlan Cox 	    npages, low, high, alignment, boundary, memattr);
2830ff0fc84SAlan Cox 	if (m == NULL) {
28489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2850ff0fc84SAlan Cox 		if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
2867a469c8eSJeff Roberson 			if (!vm_page_reclaim_contig_domain(domain, pflags,
2877a469c8eSJeff Roberson 			    npages, low, high, alignment, boundary) &&
2887a469c8eSJeff Roberson 			    (flags & M_WAITOK) != 0)
289e2068d0bSJeff Roberson 				vm_wait_domain(domain);
29089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2910ff0fc84SAlan Cox 			tries++;
2920ff0fc84SAlan Cox 			goto retry;
2930ff0fc84SAlan Cox 		}
2945df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
2950ff0fc84SAlan Cox 		return (0);
2960ff0fc84SAlan Cox 	}
297e2068d0bSJeff Roberson 	KASSERT(vm_phys_domain(m) == domain,
2987a469c8eSJeff Roberson 	    ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
299e2068d0bSJeff Roberson 	    vm_phys_domain(m), domain));
300c869e672SAlan Cox 	end_m = m + npages;
3015df87b21SJeff Roberson 	tmp = addr;
3020ff0fc84SAlan Cox 	for (; m < end_m; m++) {
3030ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
3040ff0fc84SAlan Cox 			pmap_zero_page(m);
3050ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
3060766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
3070766f278SJonathan T. Looney 		    VM_PROT_RW | PMAP_ENTER_WIRED, 0);
3085df87b21SJeff Roberson 		tmp += PAGE_SIZE;
3090ff0fc84SAlan Cox 	}
31089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
3110ff0fc84SAlan Cox 	return (addr);
3120ff0fc84SAlan Cox }
3130ff0fc84SAlan Cox 
3147a469c8eSJeff Roberson vm_offset_t
31544d0efb2SAlan Cox kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
31644d0efb2SAlan Cox     u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
3177a469c8eSJeff Roberson {
3187a469c8eSJeff Roberson 	struct vm_domainset_iter di;
3197a469c8eSJeff Roberson 	vm_offset_t addr;
3207a469c8eSJeff Roberson 	int domain;
3217a469c8eSJeff Roberson 
3227a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
3237a469c8eSJeff Roberson 	do {
3247a469c8eSJeff Roberson 		addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
3257a469c8eSJeff Roberson 		    alignment, boundary, memattr);
3267a469c8eSJeff Roberson 		if (addr != 0)
3277a469c8eSJeff Roberson 			break;
3287a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
3297a469c8eSJeff Roberson 
3307a469c8eSJeff Roberson 	return (addr);
3317a469c8eSJeff Roberson }
3327a469c8eSJeff Roberson 
3330ff0fc84SAlan Cox /*
334df8bae1dSRodney W. Grimes  *	kmem_suballoc:
335df8bae1dSRodney W. Grimes  *
336df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
337df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
338df8bae1dSRodney W. Grimes  *
339df8bae1dSRodney W. Grimes  *	Arguments are as follows:
340df8bae1dSRodney W. Grimes  *
341df8bae1dSRodney W. Grimes  *	parent		Map to take range from
342df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
343030f2369SAlfred Perlstein  *	size		Size of range to find
3443202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
345df8bae1dSRodney W. Grimes  */
3460d94caffSDavid Greenman vm_map_t
3473202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
3483202ed75SAlan Cox     vm_size_t size, boolean_t superpage_align)
349df8bae1dSRodney W. Grimes {
3506e4f51d1SAlfred Perlstein 	int ret;
351df8bae1dSRodney W. Grimes 	vm_map_t result;
35223955314SAlfred Perlstein 
353df8bae1dSRodney W. Grimes 	size = round_page(size);
354df8bae1dSRodney W. Grimes 
3552bc24aa9SAlan Cox 	*min = vm_map_min(parent);
356edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
3575aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
3583364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
35924dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
36024dedba9SAlan Cox 		panic("kmem_suballoc: bad status return of %d", ret);
361df8bae1dSRodney W. Grimes 	*max = *min + size;
3622d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
363df8bae1dSRodney W. Grimes 	if (result == NULL)
364df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
3656e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
366df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
367df8bae1dSRodney W. Grimes 	return (result);
368df8bae1dSRodney W. Grimes }
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes /*
3711c7c3c6aSMatthew Dillon  *	kmem_malloc:
3721c7c3c6aSMatthew Dillon  *
3735df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
374df8bae1dSRodney W. Grimes  */
375df8bae1dSRodney W. Grimes vm_offset_t
376067fd858SAlan Cox kmem_malloc_domain(int domain, vm_size_t size, int flags)
377df8bae1dSRodney W. Grimes {
3780766f278SJonathan T. Looney 	vmem_t *arena;
379df8bae1dSRodney W. Grimes 	vm_offset_t addr;
3805df87b21SJeff Roberson 	int rv;
381df8bae1dSRodney W. Grimes 
3820766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0
383067fd858SAlan Cox 	if (__predict_true((flags & M_EXEC) == 0))
3840766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_arena;
3850766f278SJonathan T. Looney 	else
3860766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
3870766f278SJonathan T. Looney #else
3880766f278SJonathan T. Looney 	arena = vm_dom[domain].vmd_kernel_arena;
3890766f278SJonathan T. Looney #endif
390df8bae1dSRodney W. Grimes 	size = round_page(size);
3910766f278SJonathan T. Looney 	if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr))
3925df87b21SJeff Roberson 		return (0);
393df8bae1dSRodney W. Grimes 
3947a469c8eSJeff Roberson 	rv = kmem_back_domain(domain, kernel_object, addr, size, flags);
3955df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
3960766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
397f31c239dSAlan Cox 		return (0);
398df8bae1dSRodney W. Grimes 	}
3995df87b21SJeff Roberson 	return (addr);
400e3813573SMatthew D Fleming }
401e3813573SMatthew D Fleming 
4027a469c8eSJeff Roberson vm_offset_t
40383a90bffSAlan Cox kmem_malloc(vm_size_t size, int flags)
4047a469c8eSJeff Roberson {
4057a469c8eSJeff Roberson 	struct vm_domainset_iter di;
4067a469c8eSJeff Roberson 	vm_offset_t addr;
4077a469c8eSJeff Roberson 	int domain;
4087a469c8eSJeff Roberson 
4097a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
4107a469c8eSJeff Roberson 	do {
411067fd858SAlan Cox 		addr = kmem_malloc_domain(domain, size, flags);
4127a469c8eSJeff Roberson 		if (addr != 0)
4137a469c8eSJeff Roberson 			break;
4147a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
4157a469c8eSJeff Roberson 
4167a469c8eSJeff Roberson 	return (addr);
4177a469c8eSJeff Roberson }
4187a469c8eSJeff Roberson 
419e3813573SMatthew D Fleming /*
42025ed23cfSMark Johnston  *	kmem_back_domain:
421e3813573SMatthew D Fleming  *
42225ed23cfSMark Johnston  *	Allocate physical pages from the specified domain for the specified
42325ed23cfSMark Johnston  *	virtual address range.
424e3813573SMatthew D Fleming  */
425e3813573SMatthew D Fleming int
4267a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
4277a469c8eSJeff Roberson     vm_size_t size, int flags)
428e3813573SMatthew D Fleming {
429e3813573SMatthew D Fleming 	vm_offset_t offset, i;
43033fff5d5SMark Johnston 	vm_page_t m, mpred;
4310766f278SJonathan T. Looney 	vm_prot_t prot;
432e3813573SMatthew D Fleming 	int pflags;
433e3813573SMatthew D Fleming 
4342e47807cSJeff Roberson 	KASSERT(object == kernel_object,
4357a469c8eSJeff Roberson 	    ("kmem_back_domain: only supports kernel object."));
4365df87b21SJeff Roberson 
4370891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
4385df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
4398d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
4408d6fbbb8SJeff Roberson 	if (flags & M_WAITOK)
4418d6fbbb8SJeff Roberson 		pflags |= VM_ALLOC_WAITFAIL;
4420766f278SJonathan T. Looney 	prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
443df8bae1dSRodney W. Grimes 
44433fff5d5SMark Johnston 	i = 0;
44533fff5d5SMark Johnston 	VM_OBJECT_WLOCK(object);
4468d6fbbb8SJeff Roberson retry:
44733fff5d5SMark Johnston 	mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
44833fff5d5SMark Johnston 	for (; i < size; i += PAGE_SIZE, mpred = m) {
4497a469c8eSJeff Roberson 		m = vm_page_alloc_domain_after(object, atop(offset + i),
4507a469c8eSJeff Roberson 		    domain, pflags, mpred);
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 		/*
4530d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
4540d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
4550d94caffSDavid Greenman 		 * aren't on any queues.
456df8bae1dSRodney W. Grimes 		 */
457df8bae1dSRodney W. Grimes 		if (m == NULL) {
4588d6fbbb8SJeff Roberson 			if ((flags & M_NOWAIT) == 0)
459b18bfc3dSJohn Dyson 				goto retry;
4608d6fbbb8SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
4619e829b22SAlan Cox 			kmem_unback(object, addr, i);
462e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
463df8bae1dSRodney W. Grimes 		}
464e2068d0bSJeff Roberson 		KASSERT(vm_phys_domain(m) == domain,
4657a469c8eSJeff Roberson 		    ("kmem_back_domain: Domain mismatch %d != %d",
466e2068d0bSJeff Roberson 		    vm_phys_domain(m), domain));
4671e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
468fff6062aSAlan Cox 			pmap_zero_page(m);
469d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
4709f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
4715df87b21SJeff Roberson 		m->valid = VM_PAGE_BITS_ALL;
4720766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, addr + i, m, prot,
4730766f278SJonathan T. Looney 		    prot | PMAP_ENTER_WIRED, 0);
47449bfa624SAlan Cox #if VM_NRESERVLEVEL > 0
47549bfa624SAlan Cox 		if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
47649bfa624SAlan Cox 			m->oflags |= VPO_KMEM_EXEC;
47749bfa624SAlan Cox #endif
478df8bae1dSRodney W. Grimes 	}
4795df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
480df8bae1dSRodney W. Grimes 
481e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
482df8bae1dSRodney W. Grimes }
483df8bae1dSRodney W. Grimes 
48425ed23cfSMark Johnston /*
48525ed23cfSMark Johnston  *	kmem_back:
48625ed23cfSMark Johnston  *
48725ed23cfSMark Johnston  *	Allocate physical pages for the specified virtual address range.
48825ed23cfSMark Johnston  */
4897a469c8eSJeff Roberson int
4907a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
4917a469c8eSJeff Roberson {
49225ed23cfSMark Johnston 	vm_offset_t end, next, start;
49325ed23cfSMark Johnston 	int domain, rv;
4947a469c8eSJeff Roberson 
4957a469c8eSJeff Roberson 	KASSERT(object == kernel_object,
4967a469c8eSJeff Roberson 	    ("kmem_back: only supports kernel object."));
4977a469c8eSJeff Roberson 
49825ed23cfSMark Johnston 	for (start = addr, end = addr + size; addr < end; addr = next) {
49925ed23cfSMark Johnston 		/*
50025ed23cfSMark Johnston 		 * We must ensure that pages backing a given large virtual page
50125ed23cfSMark Johnston 		 * all come from the same physical domain.
50225ed23cfSMark Johnston 		 */
50325ed23cfSMark Johnston 		if (vm_ndomains > 1) {
50425ed23cfSMark Johnston 			domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
505*30c5525bSAndrew Gallatin 			while (VM_DOMAIN_EMPTY(domain))
506*30c5525bSAndrew Gallatin 				domain++;
50725ed23cfSMark Johnston 			next = roundup2(addr + 1, KVA_QUANTUM);
50825ed23cfSMark Johnston 			if (next > end || next < start)
50925ed23cfSMark Johnston 				next = end;
5103d14a7bbSMark Johnston 		} else {
5113d14a7bbSMark Johnston 			domain = 0;
51225ed23cfSMark Johnston 			next = end;
5133d14a7bbSMark Johnston 		}
51425ed23cfSMark Johnston 		rv = kmem_back_domain(domain, object, addr, next - addr, flags);
51525ed23cfSMark Johnston 		if (rv != KERN_SUCCESS) {
51625ed23cfSMark Johnston 			kmem_unback(object, start, addr - start);
5177a469c8eSJeff Roberson 			break;
51825ed23cfSMark Johnston 		}
51925ed23cfSMark Johnston 	}
52025ed23cfSMark Johnston 	return (rv);
5217a469c8eSJeff Roberson }
5227a469c8eSJeff Roberson 
5239e829b22SAlan Cox /*
5249e829b22SAlan Cox  *	kmem_unback:
5259e829b22SAlan Cox  *
5269e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
5279e829b22SAlan Cox  *	address range.
5289e829b22SAlan Cox  *
5299e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
5309e829b22SAlan Cox  *	that is being unmapped.
5319e829b22SAlan Cox  */
53249bfa624SAlan Cox static struct vmem *
5337a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5345df87b21SJeff Roberson {
53549bfa624SAlan Cox 	struct vmem *arena;
5367e05ffa6SMark Johnston 	vm_page_t m, next;
5377e05ffa6SMark Johnston 	vm_offset_t end, offset;
5387a469c8eSJeff Roberson 	int domain;
5395df87b21SJeff Roberson 
5402e47807cSJeff Roberson 	KASSERT(object == kernel_object,
5412e47807cSJeff Roberson 	    ("kmem_unback: only supports kernel object."));
5425df87b21SJeff Roberson 
5437a469c8eSJeff Roberson 	if (size == 0)
54449bfa624SAlan Cox 		return (NULL);
545fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
5465df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
5477e05ffa6SMark Johnston 	end = offset + size;
5485df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
5497a469c8eSJeff Roberson 	m = vm_page_lookup(object, atop(offset));
550e2068d0bSJeff Roberson 	domain = vm_phys_domain(m);
55149bfa624SAlan Cox #if VM_NRESERVLEVEL > 0
55249bfa624SAlan Cox 	if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
55349bfa624SAlan Cox 		arena = vm_dom[domain].vmd_kernel_arena;
55449bfa624SAlan Cox 	else
55549bfa624SAlan Cox 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
55649bfa624SAlan Cox #else
55749bfa624SAlan Cox 	arena = vm_dom[domain].vmd_kernel_arena;
55849bfa624SAlan Cox #endif
5597a469c8eSJeff Roberson 	for (; offset < end; offset += PAGE_SIZE, m = next) {
5607e05ffa6SMark Johnston 		next = vm_page_next(m);
56127e9ed8aSAlan Cox 		vm_page_unwire(m, PQ_NONE);
5625df87b21SJeff Roberson 		vm_page_free(m);
5635df87b21SJeff Roberson 	}
5645df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
5657a469c8eSJeff Roberson 
56649bfa624SAlan Cox 	return (arena);
5677a469c8eSJeff Roberson }
5687a469c8eSJeff Roberson 
5697a469c8eSJeff Roberson void
5707a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5717a469c8eSJeff Roberson {
5727a469c8eSJeff Roberson 
57349bfa624SAlan Cox 	(void)_kmem_unback(object, addr, size);
5745df87b21SJeff Roberson }
5755df87b21SJeff Roberson 
576df8bae1dSRodney W. Grimes /*
5775df87b21SJeff Roberson  *	kmem_free:
5785df87b21SJeff Roberson  *
5795df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
5805df87b21SJeff Roberson  *	original allocation.
5815df87b21SJeff Roberson  */
5825df87b21SJeff Roberson void
58349bfa624SAlan Cox kmem_free(vm_offset_t addr, vm_size_t size)
5845df87b21SJeff Roberson {
5850766f278SJonathan T. Looney 	struct vmem *arena;
5860766f278SJonathan T. Looney 
5875df87b21SJeff Roberson 	size = round_page(size);
58849bfa624SAlan Cox 	arena = _kmem_unback(kernel_object, addr, size);
58949bfa624SAlan Cox 	if (arena != NULL)
5900766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
5915df87b21SJeff Roberson }
5925df87b21SJeff Roberson 
5935df87b21SJeff Roberson /*
5945df87b21SJeff Roberson  *	kmap_alloc_wait:
595df8bae1dSRodney W. Grimes  *
596df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
597df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
598df8bae1dSRodney W. Grimes  *
5991c7c3c6aSMatthew Dillon  *	This routine may block.
600df8bae1dSRodney W. Grimes  */
6010d94caffSDavid Greenman vm_offset_t
6026e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size)
603df8bae1dSRodney W. Grimes {
604df8bae1dSRodney W. Grimes 	vm_offset_t addr;
60523955314SAlfred Perlstein 
606df8bae1dSRodney W. Grimes 	size = round_page(size);
6073364c323SKonstantin Belousov 	if (!swap_reserve(size))
6083364c323SKonstantin Belousov 		return (0);
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	for (;;) {
611df8bae1dSRodney W. Grimes 		/*
6120d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
6130d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
614df8bae1dSRodney W. Grimes 		 */
615df8bae1dSRodney W. Grimes 		vm_map_lock(map);
616e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
617df8bae1dSRodney W. Grimes 			break;
618df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
619df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
620df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6213364c323SKonstantin Belousov 			swap_release(size);
622df8bae1dSRodney W. Grimes 			return (0);
623df8bae1dSRodney W. Grimes 		}
6249688f931SAlan Cox 		map->needs_wakeup = TRUE;
6258ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
626df8bae1dSRodney W. Grimes 	}
6273364c323SKonstantin Belousov 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
6283364c323SKonstantin Belousov 	    VM_PROT_ALL, MAP_ACC_CHARGED);
629df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
630df8bae1dSRodney W. Grimes 	return (addr);
631df8bae1dSRodney W. Grimes }
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes /*
6345df87b21SJeff Roberson  *	kmap_free_wakeup:
635df8bae1dSRodney W. Grimes  *
63624a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
637df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
638df8bae1dSRodney W. Grimes  */
6390d94caffSDavid Greenman void
6406e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
641df8bae1dSRodney W. Grimes {
64223955314SAlfred Perlstein 
643df8bae1dSRodney W. Grimes 	vm_map_lock(map);
644655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
6459688f931SAlan Cox 	if (map->needs_wakeup) {
6469688f931SAlan Cox 		map->needs_wakeup = FALSE;
6479688f931SAlan Cox 		vm_map_wakeup(map);
6489688f931SAlan Cox 	}
649df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
650df8bae1dSRodney W. Grimes }
651df8bae1dSRodney W. Grimes 
6525df87b21SJeff Roberson void
65389cb2a19SMatthew D Fleming kmem_init_zero_region(void)
65489cb2a19SMatthew D Fleming {
655cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
65689cb2a19SMatthew D Fleming 	vm_page_t m;
65789cb2a19SMatthew D Fleming 
658cfb00e5aSMatthew D Fleming 	/*
659cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
660cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
661cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
662cfb00e5aSMatthew D Fleming 	 */
6635df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
664703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
66589cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
66689cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
66789cb2a19SMatthew D Fleming 		pmap_zero_page(m);
66889cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
66989cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
6705df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
67189cb2a19SMatthew D Fleming 
67289cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
67389cb2a19SMatthew D Fleming }
67489cb2a19SMatthew D Fleming 
675df8bae1dSRodney W. Grimes /*
676969e147aSMark Johnston  * Import KVA from the kernel map into the kernel arena.
6771aed6d48SMark Johnston  */
6781aed6d48SMark Johnston static int
6791aed6d48SMark Johnston kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
6801aed6d48SMark Johnston {
6811aed6d48SMark Johnston 	vm_offset_t addr;
6821aed6d48SMark Johnston 	int result;
6831aed6d48SMark Johnston 
6841aed6d48SMark Johnston 	KASSERT((size % KVA_QUANTUM) == 0,
6851aed6d48SMark Johnston 	    ("kva_import: Size %jd is not a multiple of %d",
6861aed6d48SMark Johnston 	    (intmax_t)size, (int)KVA_QUANTUM));
6871aed6d48SMark Johnston 	addr = vm_map_min(kernel_map);
6881aed6d48SMark Johnston 	result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
6891aed6d48SMark Johnston 	    VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
6901aed6d48SMark Johnston 	if (result != KERN_SUCCESS)
6911aed6d48SMark Johnston                 return (ENOMEM);
6921aed6d48SMark Johnston 
6931aed6d48SMark Johnston 	*addrp = addr;
6941aed6d48SMark Johnston 
6951aed6d48SMark Johnston 	return (0);
6961aed6d48SMark Johnston }
6971aed6d48SMark Johnston 
6981aed6d48SMark Johnston /*
699969e147aSMark Johnston  * Import KVA from a parent arena into a per-domain arena.  Imports must be
700969e147aSMark Johnston  * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
7011aed6d48SMark Johnston  */
7021aed6d48SMark Johnston static int
703969e147aSMark Johnston kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
7041aed6d48SMark Johnston {
7051aed6d48SMark Johnston 
7061aed6d48SMark Johnston 	KASSERT((size % KVA_QUANTUM) == 0,
707969e147aSMark Johnston 	    ("kva_import_domain: Size %jd is not a multiple of %d",
7081aed6d48SMark Johnston 	    (intmax_t)size, (int)KVA_QUANTUM));
7091aed6d48SMark Johnston 	return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
7101aed6d48SMark Johnston 	    VMEM_ADDR_MAX, flags, addrp));
7111aed6d48SMark Johnston }
7121aed6d48SMark Johnston 
7131aed6d48SMark Johnston /*
7141c7c3c6aSMatthew Dillon  * 	kmem_init:
7151c7c3c6aSMatthew Dillon  *
7161c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
7171c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
7181c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
7191c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
7201aed6d48SMark Johnston  *	Create the kernel vmem arena and its per-domain children.
721df8bae1dSRodney W. Grimes  */
7220d94caffSDavid Greenman void
7236e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end)
724df8bae1dSRodney W. Grimes {
725030f2369SAlfred Perlstein 	vm_map_t m;
7261aed6d48SMark Johnston 	int domain;
727df8bae1dSRodney W. Grimes 
7282d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
729c9267356SAlan Cox 	m->system_map = 1;
730df8bae1dSRodney W. Grimes 	vm_map_lock(m);
731df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
732df8bae1dSRodney W. Grimes 	kernel_map = m;
733c9267356SAlan Cox 	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
7345cfa90e9SAlan Cox #ifdef __amd64__
7355cfa90e9SAlan Cox 	    KERNBASE,
7365cfa90e9SAlan Cox #else
7375cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
7385cfa90e9SAlan Cox #endif
7395cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
740df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
741df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
7421aed6d48SMark Johnston 
7431aed6d48SMark Johnston 	/*
7441aed6d48SMark Johnston 	 * Initialize the kernel_arena.  This can grow on demand.
7451aed6d48SMark Johnston 	 */
7461aed6d48SMark Johnston 	vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
7471aed6d48SMark Johnston 	vmem_set_import(kernel_arena, kva_import, NULL, NULL, KVA_QUANTUM);
7481aed6d48SMark Johnston 
7491aed6d48SMark Johnston 	for (domain = 0; domain < vm_ndomains; domain++) {
750969e147aSMark Johnston 		/*
751969e147aSMark Johnston 		 * Initialize the per-domain arenas.  These are used to color
752969e147aSMark Johnston 		 * the KVA space in a way that ensures that virtual large pages
753969e147aSMark Johnston 		 * are backed by memory from the same physical domain,
754969e147aSMark Johnston 		 * maximizing the potential for superpage promotion.
755969e147aSMark Johnston 		 */
7561aed6d48SMark Johnston 		vm_dom[domain].vmd_kernel_arena = vmem_create(
7571aed6d48SMark Johnston 		    "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
7581aed6d48SMark Johnston 		vmem_set_import(vm_dom[domain].vmd_kernel_arena,
759969e147aSMark Johnston 		    kva_import_domain, NULL, kernel_arena, KVA_QUANTUM);
760969e147aSMark Johnston 
761969e147aSMark Johnston 		/*
762969e147aSMark Johnston 		 * In architectures with superpages, maintain separate arenas
763969e147aSMark Johnston 		 * for allocations with permissions that differ from the
764969e147aSMark Johnston 		 * "standard" read/write permissions used for kernel memory,
765969e147aSMark Johnston 		 * so as not to inhibit superpage promotion.
766969e147aSMark Johnston 		 */
7671aed6d48SMark Johnston #if VM_NRESERVLEVEL > 0
7681aed6d48SMark Johnston 		vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
7691aed6d48SMark Johnston 		    "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
7701aed6d48SMark Johnston 		vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
771969e147aSMark Johnston 		    kva_import_domain, (vmem_release_t *)vmem_xfree,
772969e147aSMark Johnston 		    kernel_arena, KVA_QUANTUM);
7731aed6d48SMark Johnston #endif
7741aed6d48SMark Johnston 	}
775df8bae1dSRodney W. Grimes }
77686f08737SRobert Watson 
777483f692eSMark Johnston /*
778483f692eSMark Johnston  *	kmem_bootstrap_free:
779483f692eSMark Johnston  *
780483f692eSMark Johnston  *	Free pages backing preloaded data (e.g., kernel modules) to the
781483f692eSMark Johnston  *	system.  Currently only supported on platforms that create a
782483f692eSMark Johnston  *	vm_phys segment for preloaded data.
783483f692eSMark Johnston  */
784483f692eSMark Johnston void
785483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
786483f692eSMark Johnston {
787483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__)
788483f692eSMark Johnston 	struct vm_domain *vmd;
7896c85795aSMark Johnston 	vm_offset_t end, va;
790483f692eSMark Johnston 	vm_paddr_t pa;
791483f692eSMark Johnston 	vm_page_t m;
792483f692eSMark Johnston 
793483f692eSMark Johnston 	end = trunc_page(start + size);
794483f692eSMark Johnston 	start = round_page(start);
795483f692eSMark Johnston 
7966c85795aSMark Johnston 	for (va = start; va < end; va += PAGE_SIZE) {
7976c85795aSMark Johnston 		pa = pmap_kextract(va);
798483f692eSMark Johnston 		m = PHYS_TO_VM_PAGE(pa);
799483f692eSMark Johnston 
800483f692eSMark Johnston 		vmd = vm_pagequeue_domain(m);
801483f692eSMark Johnston 		vm_domain_free_lock(vmd);
802483f692eSMark Johnston 		vm_phys_free_pages(m, 0);
803c16bd872SMark Johnston 		vmd->vmd_page_count++;
804483f692eSMark Johnston 		vm_domain_free_unlock(vmd);
805c16bd872SMark Johnston 
806c16bd872SMark Johnston 		vm_domain_freecnt_inc(vmd, 1);
807c16bd872SMark Johnston 		vm_cnt.v_page_count++;
808483f692eSMark Johnston 	}
8096c85795aSMark Johnston 	pmap_remove(kernel_pmap, start, end);
8106c85795aSMark Johnston 	(void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
811483f692eSMark Johnston #endif
812483f692eSMark Johnston }
813483f692eSMark Johnston 
8149309e63cSRobert Watson #ifdef DIAGNOSTIC
81586f08737SRobert Watson /*
81686f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
81786f08737SRobert Watson  * purposes.
81886f08737SRobert Watson  */
81986f08737SRobert Watson static int
82086f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
82186f08737SRobert Watson {
82286f08737SRobert Watson 	int error, i;
82386f08737SRobert Watson 
82486f08737SRobert Watson 	i = 0;
82586f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
82686f08737SRobert Watson 	if (error)
82786f08737SRobert Watson 		return (error);
8289b43bc27SAndriy Gapon 	if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
8299b43bc27SAndriy Gapon 		return (EINVAL);
8309b43bc27SAndriy Gapon 	if (i != 0)
8319b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, i);
83286f08737SRobert Watson 	return (0);
83386f08737SRobert Watson }
83486f08737SRobert Watson 
83586f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
8369b43bc27SAndriy Gapon     debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");
8379309e63cSRobert Watson #endif
838