xref: /freebsd/sys/vm/vm_kern.c (revision 5fee468e83efcfa33d016da2f5dea63ca3d8a15f)
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>
94e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
95431fb8abSMark Johnston #include <vm/vm_phys.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 
1007dd979dfSMark Johnston struct vm_map kernel_map_store;
1017dd979dfSMark Johnston struct vm_map exec_map_store;
1027dd979dfSMark Johnston struct vm_map pipe_map_store;
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,
11758aa35d4SWarner Losh #if defined(__arm__)
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
1272b601070SMark Johnston /* On non-superpage architectures we want large import sizes. */
1282b601070SMark Johnston #define	KVA_QUANTUM_SHIFT	(8 + PAGE_SHIFT)
1291aed6d48SMark Johnston #endif
13025ed23cfSMark Johnston #define	KVA_QUANTUM		(1 << KVA_QUANTUM_SHIFT)
131aea9103eSMark Johnston #define	KVA_NUMA_IMPORT_QUANTUM	(KVA_QUANTUM * 128)
1321aed6d48SMark Johnston 
133a81c400eSJeff Roberson extern void     uma_startup2(void);
134a81c400eSJeff Roberson 
135df8bae1dSRodney W. Grimes /*
1365df87b21SJeff Roberson  *	kva_alloc:
137a839bdc8SDmitrij Tejblum  *
138b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
139b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
140b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
141b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
142b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
143a839bdc8SDmitrij Tejblum  */
144a839bdc8SDmitrij Tejblum vm_offset_t
1456e309d75SEd Maste kva_alloc(vm_size_t size)
146a839bdc8SDmitrij Tejblum {
147a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
148a839bdc8SDmitrij Tejblum 
149a839bdc8SDmitrij Tejblum 	size = round_page(size);
1505df87b21SJeff Roberson 	if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
151a839bdc8SDmitrij Tejblum 		return (0);
1525df87b21SJeff Roberson 
153a839bdc8SDmitrij Tejblum 	return (addr);
154a839bdc8SDmitrij Tejblum }
155a839bdc8SDmitrij Tejblum 
156a839bdc8SDmitrij Tejblum /*
1575df87b21SJeff Roberson  *	kva_free:
158ca596a25SJuli Mallett  *
1595df87b21SJeff Roberson  *	Release a region of kernel virtual memory allocated
1605df87b21SJeff Roberson  *	with kva_alloc, and return the physical pages
1615df87b21SJeff Roberson  *	associated with that region.
1625df87b21SJeff Roberson  *
1635df87b21SJeff Roberson  *	This routine may not block on kernel maps.
164ca596a25SJuli Mallett  */
1655df87b21SJeff Roberson void
1666e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size)
167df8bae1dSRodney W. Grimes {
168df8bae1dSRodney W. Grimes 
169df8bae1dSRodney W. Grimes 	size = round_page(size);
1705df87b21SJeff Roberson 	vmem_free(kernel_arena, addr, size);
171df8bae1dSRodney W. Grimes }
172df8bae1dSRodney W. Grimes 
17333655d95SMark Johnston static vm_page_t
17433655d95SMark Johnston kmem_alloc_contig_pages(vm_object_t object, vm_pindex_t pindex, int domain,
17533655d95SMark Johnston     int pflags, u_long npages, vm_paddr_t low, vm_paddr_t high,
17633655d95SMark Johnston     u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
17733655d95SMark Johnston {
17833655d95SMark Johnston 	vm_page_t m;
17933655d95SMark Johnston 	int tries;
18033655d95SMark Johnston 	bool wait;
18133655d95SMark Johnston 
18233655d95SMark Johnston 	VM_OBJECT_ASSERT_WLOCKED(object);
18333655d95SMark Johnston 
18433655d95SMark Johnston 	wait = (pflags & VM_ALLOC_WAITOK) != 0;
18533655d95SMark Johnston 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
18633655d95SMark Johnston 	pflags |= VM_ALLOC_NOWAIT;
18733655d95SMark Johnston 	for (tries = wait ? 3 : 1;; tries--) {
18833655d95SMark Johnston 		m = vm_page_alloc_contig_domain(object, pindex, domain, pflags,
18933655d95SMark Johnston 		    npages, low, high, alignment, boundary, memattr);
19033655d95SMark Johnston 		if (m != NULL || tries == 0)
19133655d95SMark Johnston 			break;
19233655d95SMark Johnston 
19333655d95SMark Johnston 		VM_OBJECT_WUNLOCK(object);
19433655d95SMark Johnston 		if (!vm_page_reclaim_contig_domain(domain, pflags, npages,
19533655d95SMark Johnston 		    low, high, alignment, boundary) && wait)
19633655d95SMark Johnston 			vm_wait_domain(domain);
19733655d95SMark Johnston 		VM_OBJECT_WLOCK(object);
19833655d95SMark Johnston 	}
19933655d95SMark Johnston 	return (m);
20033655d95SMark Johnston }
20133655d95SMark Johnston 
202df8bae1dSRodney W. Grimes /*
2030ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physical pages
2040ff0fc84SAlan Cox  *	within the specified address range to the kernel object.  Creates a
2050ff0fc84SAlan Cox  *	wired mapping from this region to these pages, and returns the
2060ff0fc84SAlan Cox  *	region's starting virtual address.  The allocated pages are not
2070ff0fc84SAlan Cox  *	necessarily physically contiguous.  If M_ZERO is specified through the
2080ff0fc84SAlan Cox  *	given flags, then the pages are zeroed before they are mapped.
2090ff0fc84SAlan Cox  */
2109978bd99SMark Johnston static vm_offset_t
2117a469c8eSJeff Roberson kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
2120ff0fc84SAlan Cox     vm_paddr_t high, vm_memattr_t memattr)
2130ff0fc84SAlan Cox {
2147a469c8eSJeff Roberson 	vmem_t *vmem;
21533655d95SMark Johnston 	vm_object_t object;
216d1780e8dSKonstantin Belousov 	vm_offset_t addr, i, offset;
2170ff0fc84SAlan Cox 	vm_page_t m;
21833655d95SMark Johnston 	int pflags;
219d22ff6e6SKonstantin Belousov 	vm_prot_t prot;
2200ff0fc84SAlan Cox 
22133655d95SMark Johnston 	object = kernel_object;
2220ff0fc84SAlan Cox 	size = round_page(size);
2237a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
2245df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
2250ff0fc84SAlan Cox 		return (0);
2260ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
22791e31c3cSJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
228d22ff6e6SKonstantin Belousov 	prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
22989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2305df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
23133655d95SMark Johnston 		m = kmem_alloc_contig_pages(object, atop(offset + i),
2327a469c8eSJeff Roberson 		    domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
2330ff0fc84SAlan Cox 		if (m == NULL) {
23489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
2359e829b22SAlan Cox 			kmem_unback(object, addr, i);
2365df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
2370ff0fc84SAlan Cox 			return (0);
2380ff0fc84SAlan Cox 		}
239431fb8abSMark Johnston 		KASSERT(vm_page_domain(m) == domain,
2407a469c8eSJeff Roberson 		    ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
241431fb8abSMark Johnston 		    vm_page_domain(m), domain));
2420ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2430ff0fc84SAlan Cox 			pmap_zero_page(m);
24491e31c3cSJeff Roberson 		vm_page_valid(m);
245d22ff6e6SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, prot,
246d22ff6e6SKonstantin Belousov 		    prot | PMAP_ENTER_WIRED, 0);
2470ff0fc84SAlan Cox 	}
24889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2490ff0fc84SAlan Cox 	return (addr);
2500ff0fc84SAlan Cox }
2510ff0fc84SAlan Cox 
2527a469c8eSJeff Roberson vm_offset_t
253db7c2a48SAlan Cox kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
254db7c2a48SAlan Cox     vm_memattr_t memattr)
2557a469c8eSJeff Roberson {
2569978bd99SMark Johnston 
2579978bd99SMark Johnston 	return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low,
2589978bd99SMark Johnston 	    high, memattr));
2599978bd99SMark Johnston }
2609978bd99SMark Johnston 
2619978bd99SMark Johnston vm_offset_t
2629978bd99SMark Johnston kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags,
2639978bd99SMark Johnston     vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr)
2649978bd99SMark Johnston {
2657a469c8eSJeff Roberson 	struct vm_domainset_iter di;
2667a469c8eSJeff Roberson 	vm_offset_t addr;
267*5fee468eSBryan Drewery 	int domain;
2687a469c8eSJeff Roberson 
269*5fee468eSBryan Drewery 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
2707a469c8eSJeff Roberson 	do {
2717a469c8eSJeff Roberson 		addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
2727a469c8eSJeff Roberson 		    memattr);
2737a469c8eSJeff Roberson 		if (addr != 0)
2747a469c8eSJeff Roberson 			break;
2754c29d2deSMark Johnston 	} while (vm_domainset_iter_policy(&di, &domain) == 0);
2767a469c8eSJeff Roberson 
2777a469c8eSJeff Roberson 	return (addr);
2787a469c8eSJeff Roberson }
2797a469c8eSJeff Roberson 
2800ff0fc84SAlan Cox /*
2810ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2820ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2830ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2840ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2850ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2860ff0fc84SAlan Cox  *	mapped.
2870ff0fc84SAlan Cox  */
2889978bd99SMark Johnston static vm_offset_t
2897a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
2900ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2910ff0fc84SAlan Cox     vm_memattr_t memattr)
2920ff0fc84SAlan Cox {
2937a469c8eSJeff Roberson 	vmem_t *vmem;
29433655d95SMark Johnston 	vm_object_t object;
295d1780e8dSKonstantin Belousov 	vm_offset_t addr, offset, tmp;
2960ff0fc84SAlan Cox 	vm_page_t end_m, m;
297c869e672SAlan Cox 	u_long npages;
29833655d95SMark Johnston 	int pflags;
2990ff0fc84SAlan Cox 
30033655d95SMark Johnston 	object = kernel_object;
3010ff0fc84SAlan Cox 	size = round_page(size);
3027a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
3035df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
3040ff0fc84SAlan Cox 		return (0);
3050ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
30691e31c3cSJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
307c869e672SAlan Cox 	npages = atop(size);
30889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
30933655d95SMark Johnston 	m = kmem_alloc_contig_pages(object, atop(offset), domain,
31033655d95SMark Johnston 	    pflags, npages, low, high, alignment, boundary, memattr);
3110ff0fc84SAlan Cox 	if (m == NULL) {
31289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
3135df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
3140ff0fc84SAlan Cox 		return (0);
3150ff0fc84SAlan Cox 	}
316431fb8abSMark Johnston 	KASSERT(vm_page_domain(m) == domain,
3177a469c8eSJeff Roberson 	    ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
318431fb8abSMark Johnston 	    vm_page_domain(m), domain));
319c869e672SAlan Cox 	end_m = m + npages;
3205df87b21SJeff Roberson 	tmp = addr;
3210ff0fc84SAlan Cox 	for (; m < end_m; m++) {
3220ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
3230ff0fc84SAlan Cox 			pmap_zero_page(m);
32491e31c3cSJeff Roberson 		vm_page_valid(m);
3250766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
3260766f278SJonathan T. Looney 		    VM_PROT_RW | PMAP_ENTER_WIRED, 0);
3275df87b21SJeff Roberson 		tmp += PAGE_SIZE;
3280ff0fc84SAlan Cox 	}
32989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
3300ff0fc84SAlan Cox 	return (addr);
3310ff0fc84SAlan Cox }
3320ff0fc84SAlan Cox 
3337a469c8eSJeff Roberson vm_offset_t
33444d0efb2SAlan Cox kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
33544d0efb2SAlan Cox     u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
3367a469c8eSJeff Roberson {
3379978bd99SMark Johnston 
3389978bd99SMark Johnston 	return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low,
3399978bd99SMark Johnston 	    high, alignment, boundary, memattr));
3409978bd99SMark Johnston }
3419978bd99SMark Johnston 
3429978bd99SMark Johnston vm_offset_t
3439978bd99SMark Johnston kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags,
3449978bd99SMark Johnston     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
3459978bd99SMark Johnston     vm_memattr_t memattr)
3469978bd99SMark Johnston {
3477a469c8eSJeff Roberson 	struct vm_domainset_iter di;
3487a469c8eSJeff Roberson 	vm_offset_t addr;
349*5fee468eSBryan Drewery 	int domain;
3507a469c8eSJeff Roberson 
351*5fee468eSBryan Drewery 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
3527a469c8eSJeff Roberson 	do {
3537a469c8eSJeff Roberson 		addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
3547a469c8eSJeff Roberson 		    alignment, boundary, memattr);
3557a469c8eSJeff Roberson 		if (addr != 0)
3567a469c8eSJeff Roberson 			break;
3574c29d2deSMark Johnston 	} while (vm_domainset_iter_policy(&di, &domain) == 0);
3587a469c8eSJeff Roberson 
3597a469c8eSJeff Roberson 	return (addr);
3607a469c8eSJeff Roberson }
3617a469c8eSJeff Roberson 
3620ff0fc84SAlan Cox /*
3637dd979dfSMark Johnston  *	kmem_subinit:
364df8bae1dSRodney W. Grimes  *
3657dd979dfSMark Johnston  *	Initializes a map to manage a subrange
366df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
367df8bae1dSRodney W. Grimes  *
368df8bae1dSRodney W. Grimes  *	Arguments are as follows:
369df8bae1dSRodney W. Grimes  *
370df8bae1dSRodney W. Grimes  *	parent		Map to take range from
371df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
372030f2369SAlfred Perlstein  *	size		Size of range to find
3733202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
374df8bae1dSRodney W. Grimes  */
3757dd979dfSMark Johnston void
3767dd979dfSMark Johnston kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
3777dd979dfSMark Johnston     vm_size_t size, bool superpage_align)
378df8bae1dSRodney W. Grimes {
3796e4f51d1SAlfred Perlstein 	int ret;
38023955314SAlfred Perlstein 
381df8bae1dSRodney W. Grimes 	size = round_page(size);
382df8bae1dSRodney W. Grimes 
3832bc24aa9SAlan Cox 	*min = vm_map_min(parent);
384edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
3855aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
3863364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
38724dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
3887dd979dfSMark Johnston 		panic("kmem_subinit: bad status return of %d", ret);
389df8bae1dSRodney W. Grimes 	*max = *min + size;
3907dd979dfSMark Johnston 	vm_map_init(map, vm_map_pmap(parent), *min, *max);
3917dd979dfSMark Johnston 	if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS)
3927dd979dfSMark Johnston 		panic("kmem_subinit: unable to change range to submap");
393df8bae1dSRodney W. Grimes }
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes /*
3969978bd99SMark Johnston  *	kmem_malloc_domain:
3971c7c3c6aSMatthew Dillon  *
3985df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
399df8bae1dSRodney W. Grimes  */
4009978bd99SMark Johnston static vm_offset_t
401067fd858SAlan Cox kmem_malloc_domain(int domain, vm_size_t size, int flags)
402df8bae1dSRodney W. Grimes {
4030766f278SJonathan T. Looney 	vmem_t *arena;
404df8bae1dSRodney W. Grimes 	vm_offset_t addr;
4055df87b21SJeff Roberson 	int rv;
406df8bae1dSRodney W. Grimes 
407067fd858SAlan Cox 	if (__predict_true((flags & M_EXEC) == 0))
4080766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_arena;
4090766f278SJonathan T. Looney 	else
4100766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
411df8bae1dSRodney W. Grimes 	size = round_page(size);
4120766f278SJonathan T. Looney 	if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr))
4135df87b21SJeff Roberson 		return (0);
414df8bae1dSRodney W. Grimes 
4157a469c8eSJeff Roberson 	rv = kmem_back_domain(domain, kernel_object, addr, size, flags);
4165df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
4170766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
418f31c239dSAlan Cox 		return (0);
419df8bae1dSRodney W. Grimes 	}
4205df87b21SJeff Roberson 	return (addr);
421e3813573SMatthew D Fleming }
422e3813573SMatthew D Fleming 
4237a469c8eSJeff Roberson vm_offset_t
42483a90bffSAlan Cox kmem_malloc(vm_size_t size, int flags)
4257a469c8eSJeff Roberson {
4269978bd99SMark Johnston 
4279978bd99SMark Johnston 	return (kmem_malloc_domainset(DOMAINSET_RR(), size, flags));
4289978bd99SMark Johnston }
4299978bd99SMark Johnston 
4309978bd99SMark Johnston vm_offset_t
4319978bd99SMark Johnston kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags)
4329978bd99SMark Johnston {
4337a469c8eSJeff Roberson 	struct vm_domainset_iter di;
4347a469c8eSJeff Roberson 	vm_offset_t addr;
4357a469c8eSJeff Roberson 	int domain;
4367a469c8eSJeff Roberson 
4379978bd99SMark Johnston 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
4387a469c8eSJeff Roberson 	do {
439067fd858SAlan Cox 		addr = kmem_malloc_domain(domain, size, flags);
4407a469c8eSJeff Roberson 		if (addr != 0)
4417a469c8eSJeff Roberson 			break;
4424c29d2deSMark Johnston 	} while (vm_domainset_iter_policy(&di, &domain) == 0);
4437a469c8eSJeff Roberson 
4447a469c8eSJeff Roberson 	return (addr);
4457a469c8eSJeff Roberson }
4467a469c8eSJeff Roberson 
447e3813573SMatthew D Fleming /*
44825ed23cfSMark Johnston  *	kmem_back_domain:
449e3813573SMatthew D Fleming  *
45025ed23cfSMark Johnston  *	Allocate physical pages from the specified domain for the specified
45125ed23cfSMark Johnston  *	virtual address range.
452e3813573SMatthew D Fleming  */
453e3813573SMatthew D Fleming int
4547a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
4557a469c8eSJeff Roberson     vm_size_t size, int flags)
456e3813573SMatthew D Fleming {
457e3813573SMatthew D Fleming 	vm_offset_t offset, i;
45833fff5d5SMark Johnston 	vm_page_t m, mpred;
4590766f278SJonathan T. Looney 	vm_prot_t prot;
460e3813573SMatthew D Fleming 	int pflags;
461e3813573SMatthew D Fleming 
4622e47807cSJeff Roberson 	KASSERT(object == kernel_object,
4637a469c8eSJeff Roberson 	    ("kmem_back_domain: only supports kernel object."));
4645df87b21SJeff Roberson 
4650891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
46691e31c3cSJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
4678d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
4688d6fbbb8SJeff Roberson 	if (flags & M_WAITOK)
4698d6fbbb8SJeff Roberson 		pflags |= VM_ALLOC_WAITFAIL;
4700766f278SJonathan T. Looney 	prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
471df8bae1dSRodney W. Grimes 
47233fff5d5SMark Johnston 	i = 0;
47333fff5d5SMark Johnston 	VM_OBJECT_WLOCK(object);
4748d6fbbb8SJeff Roberson retry:
47533fff5d5SMark Johnston 	mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
47633fff5d5SMark Johnston 	for (; i < size; i += PAGE_SIZE, mpred = m) {
4777a469c8eSJeff Roberson 		m = vm_page_alloc_domain_after(object, atop(offset + i),
4787a469c8eSJeff Roberson 		    domain, pflags, mpred);
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 		/*
4810d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
4820d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
4830d94caffSDavid Greenman 		 * aren't on any queues.
484df8bae1dSRodney W. Grimes 		 */
485df8bae1dSRodney W. Grimes 		if (m == NULL) {
4868d6fbbb8SJeff Roberson 			if ((flags & M_NOWAIT) == 0)
487b18bfc3dSJohn Dyson 				goto retry;
4888d6fbbb8SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
4899e829b22SAlan Cox 			kmem_unback(object, addr, i);
490e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
491df8bae1dSRodney W. Grimes 		}
492431fb8abSMark Johnston 		KASSERT(vm_page_domain(m) == domain,
4937a469c8eSJeff Roberson 		    ("kmem_back_domain: Domain mismatch %d != %d",
494431fb8abSMark Johnston 		    vm_page_domain(m), domain));
4951e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
496fff6062aSAlan Cox 			pmap_zero_page(m);
497d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
4989f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
49991e31c3cSJeff Roberson 		vm_page_valid(m);
5000766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, addr + i, m, prot,
5010766f278SJonathan T. Looney 		    prot | PMAP_ENTER_WIRED, 0);
50249bfa624SAlan Cox 		if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
50349bfa624SAlan Cox 			m->oflags |= VPO_KMEM_EXEC;
504df8bae1dSRodney W. Grimes 	}
5055df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
506df8bae1dSRodney W. Grimes 
507e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
51025ed23cfSMark Johnston /*
51125ed23cfSMark Johnston  *	kmem_back:
51225ed23cfSMark Johnston  *
51325ed23cfSMark Johnston  *	Allocate physical pages for the specified virtual address range.
51425ed23cfSMark Johnston  */
5157a469c8eSJeff Roberson int
5167a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
5177a469c8eSJeff Roberson {
51825ed23cfSMark Johnston 	vm_offset_t end, next, start;
51925ed23cfSMark Johnston 	int domain, rv;
5207a469c8eSJeff Roberson 
5217a469c8eSJeff Roberson 	KASSERT(object == kernel_object,
5227a469c8eSJeff Roberson 	    ("kmem_back: only supports kernel object."));
5237a469c8eSJeff Roberson 
52425ed23cfSMark Johnston 	for (start = addr, end = addr + size; addr < end; addr = next) {
52525ed23cfSMark Johnston 		/*
52625ed23cfSMark Johnston 		 * We must ensure that pages backing a given large virtual page
52725ed23cfSMark Johnston 		 * all come from the same physical domain.
52825ed23cfSMark Johnston 		 */
52925ed23cfSMark Johnston 		if (vm_ndomains > 1) {
53025ed23cfSMark Johnston 			domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
53130c5525bSAndrew Gallatin 			while (VM_DOMAIN_EMPTY(domain))
53230c5525bSAndrew Gallatin 				domain++;
53325ed23cfSMark Johnston 			next = roundup2(addr + 1, KVA_QUANTUM);
53425ed23cfSMark Johnston 			if (next > end || next < start)
53525ed23cfSMark Johnston 				next = end;
5363d14a7bbSMark Johnston 		} else {
5373d14a7bbSMark Johnston 			domain = 0;
53825ed23cfSMark Johnston 			next = end;
5393d14a7bbSMark Johnston 		}
54025ed23cfSMark Johnston 		rv = kmem_back_domain(domain, object, addr, next - addr, flags);
54125ed23cfSMark Johnston 		if (rv != KERN_SUCCESS) {
54225ed23cfSMark Johnston 			kmem_unback(object, start, addr - start);
5437a469c8eSJeff Roberson 			break;
54425ed23cfSMark Johnston 		}
54525ed23cfSMark Johnston 	}
54625ed23cfSMark Johnston 	return (rv);
5477a469c8eSJeff Roberson }
5487a469c8eSJeff Roberson 
5499e829b22SAlan Cox /*
5509e829b22SAlan Cox  *	kmem_unback:
5519e829b22SAlan Cox  *
5529e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
5539e829b22SAlan Cox  *	address range.
5549e829b22SAlan Cox  *
5559e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
5569e829b22SAlan Cox  *	that is being unmapped.
5579e829b22SAlan Cox  */
55849bfa624SAlan Cox static struct vmem *
5597a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5605df87b21SJeff Roberson {
56149bfa624SAlan Cox 	struct vmem *arena;
5627e05ffa6SMark Johnston 	vm_page_t m, next;
5637e05ffa6SMark Johnston 	vm_offset_t end, offset;
5647a469c8eSJeff Roberson 	int domain;
5655df87b21SJeff Roberson 
5662e47807cSJeff Roberson 	KASSERT(object == kernel_object,
5672e47807cSJeff Roberson 	    ("kmem_unback: only supports kernel object."));
5685df87b21SJeff Roberson 
5697a469c8eSJeff Roberson 	if (size == 0)
57049bfa624SAlan Cox 		return (NULL);
571fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
5725df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
5737e05ffa6SMark Johnston 	end = offset + size;
5745df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
5757a469c8eSJeff Roberson 	m = vm_page_lookup(object, atop(offset));
576431fb8abSMark Johnston 	domain = vm_page_domain(m);
57749bfa624SAlan Cox 	if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
57849bfa624SAlan Cox 		arena = vm_dom[domain].vmd_kernel_arena;
57949bfa624SAlan Cox 	else
58049bfa624SAlan Cox 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
5817a469c8eSJeff Roberson 	for (; offset < end; offset += PAGE_SIZE, m = next) {
5827e05ffa6SMark Johnston 		next = vm_page_next(m);
583e9ceb9ddSJeff Roberson 		vm_page_xbusy_claim(m);
58488ea538aSMark Johnston 		vm_page_unwire_noq(m);
5855df87b21SJeff Roberson 		vm_page_free(m);
5865df87b21SJeff Roberson 	}
5875df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
5887a469c8eSJeff Roberson 
58949bfa624SAlan Cox 	return (arena);
5907a469c8eSJeff Roberson }
5917a469c8eSJeff Roberson 
5927a469c8eSJeff Roberson void
5937a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5947a469c8eSJeff Roberson {
5957a469c8eSJeff Roberson 
59649bfa624SAlan Cox 	(void)_kmem_unback(object, addr, size);
5975df87b21SJeff Roberson }
5985df87b21SJeff Roberson 
599df8bae1dSRodney W. Grimes /*
6005df87b21SJeff Roberson  *	kmem_free:
6015df87b21SJeff Roberson  *
6025df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
6035df87b21SJeff Roberson  *	original allocation.
6045df87b21SJeff Roberson  */
6055df87b21SJeff Roberson void
60649bfa624SAlan Cox kmem_free(vm_offset_t addr, vm_size_t size)
6075df87b21SJeff Roberson {
6080766f278SJonathan T. Looney 	struct vmem *arena;
6090766f278SJonathan T. Looney 
6105df87b21SJeff Roberson 	size = round_page(size);
61149bfa624SAlan Cox 	arena = _kmem_unback(kernel_object, addr, size);
61249bfa624SAlan Cox 	if (arena != NULL)
6130766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
6145df87b21SJeff Roberson }
6155df87b21SJeff Roberson 
6165df87b21SJeff Roberson /*
6175df87b21SJeff Roberson  *	kmap_alloc_wait:
618df8bae1dSRodney W. Grimes  *
619df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
620df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
621df8bae1dSRodney W. Grimes  *
6221c7c3c6aSMatthew Dillon  *	This routine may block.
623df8bae1dSRodney W. Grimes  */
6240d94caffSDavid Greenman vm_offset_t
6256e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size)
626df8bae1dSRodney W. Grimes {
627df8bae1dSRodney W. Grimes 	vm_offset_t addr;
62823955314SAlfred Perlstein 
629df8bae1dSRodney W. Grimes 	size = round_page(size);
6303364c323SKonstantin Belousov 	if (!swap_reserve(size))
6313364c323SKonstantin Belousov 		return (0);
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes 	for (;;) {
634df8bae1dSRodney W. Grimes 		/*
6350d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
6360d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
637df8bae1dSRodney W. Grimes 		 */
638df8bae1dSRodney W. Grimes 		vm_map_lock(map);
6399f701172SKonstantin Belousov 		addr = vm_map_findspace(map, vm_map_min(map), size);
6409f701172SKonstantin Belousov 		if (addr + size <= vm_map_max(map))
641df8bae1dSRodney W. Grimes 			break;
642df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
643df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
644df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6453364c323SKonstantin Belousov 			swap_release(size);
646df8bae1dSRodney W. Grimes 			return (0);
647df8bae1dSRodney W. Grimes 		}
6489688f931SAlan Cox 		map->needs_wakeup = TRUE;
6498ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
650df8bae1dSRodney W. Grimes 	}
65107702f72SMark Johnston 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW,
65207702f72SMark Johnston 	    MAP_ACC_CHARGED);
653df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
654df8bae1dSRodney W. Grimes 	return (addr);
655df8bae1dSRodney W. Grimes }
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes /*
6585df87b21SJeff Roberson  *	kmap_free_wakeup:
659df8bae1dSRodney W. Grimes  *
66024a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
661df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
662df8bae1dSRodney W. Grimes  */
6630d94caffSDavid Greenman void
6646e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
665df8bae1dSRodney W. Grimes {
66623955314SAlfred Perlstein 
667df8bae1dSRodney W. Grimes 	vm_map_lock(map);
668655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
6699688f931SAlan Cox 	if (map->needs_wakeup) {
6709688f931SAlan Cox 		map->needs_wakeup = FALSE;
6719688f931SAlan Cox 		vm_map_wakeup(map);
6729688f931SAlan Cox 	}
673df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
674df8bae1dSRodney W. Grimes }
675df8bae1dSRodney W. Grimes 
6765df87b21SJeff Roberson void
67789cb2a19SMatthew D Fleming kmem_init_zero_region(void)
67889cb2a19SMatthew D Fleming {
679cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
68089cb2a19SMatthew D Fleming 	vm_page_t m;
68189cb2a19SMatthew D Fleming 
682cfb00e5aSMatthew D Fleming 	/*
683cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
684cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
685cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
686cfb00e5aSMatthew D Fleming 	 */
6875df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
688703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
68989cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
69089cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
69189cb2a19SMatthew D Fleming 		pmap_zero_page(m);
69289cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
69389cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
6945df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
69589cb2a19SMatthew D Fleming 
69689cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
69789cb2a19SMatthew D Fleming }
69889cb2a19SMatthew D Fleming 
699df8bae1dSRodney W. Grimes /*
700969e147aSMark Johnston  * Import KVA from the kernel map into the kernel arena.
7011aed6d48SMark Johnston  */
7021aed6d48SMark Johnston static int
7031aed6d48SMark Johnston kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
7041aed6d48SMark Johnston {
7051aed6d48SMark Johnston 	vm_offset_t addr;
7061aed6d48SMark Johnston 	int result;
7071aed6d48SMark Johnston 
7081aed6d48SMark Johnston 	KASSERT((size % KVA_QUANTUM) == 0,
7091aed6d48SMark Johnston 	    ("kva_import: Size %jd is not a multiple of %d",
7101aed6d48SMark Johnston 	    (intmax_t)size, (int)KVA_QUANTUM));
7111aed6d48SMark Johnston 	addr = vm_map_min(kernel_map);
7121aed6d48SMark Johnston 	result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
7131aed6d48SMark Johnston 	    VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
7141aed6d48SMark Johnston 	if (result != KERN_SUCCESS)
7151aed6d48SMark Johnston                 return (ENOMEM);
7161aed6d48SMark Johnston 
7171aed6d48SMark Johnston 	*addrp = addr;
7181aed6d48SMark Johnston 
7191aed6d48SMark Johnston 	return (0);
7201aed6d48SMark Johnston }
7211aed6d48SMark Johnston 
7221aed6d48SMark Johnston /*
723969e147aSMark Johnston  * Import KVA from a parent arena into a per-domain arena.  Imports must be
724969e147aSMark Johnston  * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
7251aed6d48SMark Johnston  */
7261aed6d48SMark Johnston static int
727969e147aSMark Johnston kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
7281aed6d48SMark Johnston {
7291aed6d48SMark Johnston 
7301aed6d48SMark Johnston 	KASSERT((size % KVA_QUANTUM) == 0,
731969e147aSMark Johnston 	    ("kva_import_domain: Size %jd is not a multiple of %d",
7321aed6d48SMark Johnston 	    (intmax_t)size, (int)KVA_QUANTUM));
7331aed6d48SMark Johnston 	return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
7341aed6d48SMark Johnston 	    VMEM_ADDR_MAX, flags, addrp));
7351aed6d48SMark Johnston }
7361aed6d48SMark Johnston 
7371aed6d48SMark Johnston /*
7381c7c3c6aSMatthew Dillon  * 	kmem_init:
7391c7c3c6aSMatthew Dillon  *
7401c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
7411c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
7421c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
7431c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
7441aed6d48SMark Johnston  *	Create the kernel vmem arena and its per-domain children.
745df8bae1dSRodney W. Grimes  */
7460d94caffSDavid Greenman void
7476e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end)
748df8bae1dSRodney W. Grimes {
749aea9103eSMark Johnston 	vm_size_t quantum;
7501aed6d48SMark Johnston 	int domain;
751df8bae1dSRodney W. Grimes 
7527efe14cbSMark Johnston 	vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
7537efe14cbSMark Johnston 	kernel_map->system_map = 1;
7547efe14cbSMark Johnston 	vm_map_lock(kernel_map);
755df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
7567efe14cbSMark Johnston 	(void)vm_map_insert(kernel_map, NULL, 0,
7575cfa90e9SAlan Cox #ifdef __amd64__
7585cfa90e9SAlan Cox 	    KERNBASE,
7595cfa90e9SAlan Cox #else
7605cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
7615cfa90e9SAlan Cox #endif
7625cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
763df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
7649d75f0dcSMark Johnston 
7659d75f0dcSMark Johnston #ifdef __amd64__
7669d75f0dcSMark Johnston 	/*
7679d75f0dcSMark Johnston 	 * Mark KVA used for the page array as allocated.  Other platforms
7689d75f0dcSMark Johnston 	 * that handle vm_page_array allocation can simply adjust virtual_avail
7699d75f0dcSMark Johnston 	 * instead.
7709d75f0dcSMark Johnston 	 */
7717dd979dfSMark Johnston 	(void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array,
7729d75f0dcSMark Johnston 	    (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size *
7739d75f0dcSMark Johnston 	    sizeof(struct vm_page)),
7749d75f0dcSMark Johnston 	    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
7759d75f0dcSMark Johnston #endif
7767dd979dfSMark Johnston 	vm_map_unlock(kernel_map);
7771aed6d48SMark Johnston 
7781aed6d48SMark Johnston 	/*
779aea9103eSMark Johnston 	 * Use a large import quantum on NUMA systems.  This helps minimize
780aea9103eSMark Johnston 	 * interleaving of superpages, reducing internal fragmentation within
781aea9103eSMark Johnston 	 * the per-domain arenas.
782aea9103eSMark Johnston 	 */
783aea9103eSMark Johnston 	if (vm_ndomains > 1 && PMAP_HAS_DMAP)
784aea9103eSMark Johnston 		quantum = KVA_NUMA_IMPORT_QUANTUM;
785aea9103eSMark Johnston 	else
786aea9103eSMark Johnston 		quantum = KVA_QUANTUM;
787aea9103eSMark Johnston 
788aea9103eSMark Johnston 	/*
7891aed6d48SMark Johnston 	 * Initialize the kernel_arena.  This can grow on demand.
7901aed6d48SMark Johnston 	 */
7911aed6d48SMark Johnston 	vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
792aea9103eSMark Johnston 	vmem_set_import(kernel_arena, kva_import, NULL, NULL, quantum);
7931aed6d48SMark Johnston 
7941aed6d48SMark Johnston 	for (domain = 0; domain < vm_ndomains; domain++) {
795969e147aSMark Johnston 		/*
796969e147aSMark Johnston 		 * Initialize the per-domain arenas.  These are used to color
797969e147aSMark Johnston 		 * the KVA space in a way that ensures that virtual large pages
798969e147aSMark Johnston 		 * are backed by memory from the same physical domain,
799969e147aSMark Johnston 		 * maximizing the potential for superpage promotion.
800969e147aSMark Johnston 		 */
8011aed6d48SMark Johnston 		vm_dom[domain].vmd_kernel_arena = vmem_create(
8021aed6d48SMark Johnston 		    "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
8031aed6d48SMark Johnston 		vmem_set_import(vm_dom[domain].vmd_kernel_arena,
804aea9103eSMark Johnston 		    kva_import_domain, NULL, kernel_arena, quantum);
805969e147aSMark Johnston 
806969e147aSMark Johnston 		/*
807969e147aSMark Johnston 		 * In architectures with superpages, maintain separate arenas
808969e147aSMark Johnston 		 * for allocations with permissions that differ from the
809969e147aSMark Johnston 		 * "standard" read/write permissions used for kernel memory,
810969e147aSMark Johnston 		 * so as not to inhibit superpage promotion.
811aea9103eSMark Johnston 		 *
812aea9103eSMark Johnston 		 * Use the base import quantum since this arena is rarely used.
813969e147aSMark Johnston 		 */
8141aed6d48SMark Johnston #if VM_NRESERVLEVEL > 0
8151aed6d48SMark Johnston 		vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
8161aed6d48SMark Johnston 		    "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
8171aed6d48SMark Johnston 		vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
818969e147aSMark Johnston 		    kva_import_domain, (vmem_release_t *)vmem_xfree,
819969e147aSMark Johnston 		    kernel_arena, KVA_QUANTUM);
820303b7702SMark Johnston #else
821303b7702SMark Johnston 		vm_dom[domain].vmd_kernel_rwx_arena =
822303b7702SMark Johnston 		    vm_dom[domain].vmd_kernel_arena;
8231aed6d48SMark Johnston #endif
8241aed6d48SMark Johnston 	}
825a81c400eSJeff Roberson 
826a81c400eSJeff Roberson 	/*
827a81c400eSJeff Roberson 	 * This must be the very first call so that the virtual address
828a81c400eSJeff Roberson 	 * space used for early allocations is properly marked used in
829a81c400eSJeff Roberson 	 * the map.
830a81c400eSJeff Roberson 	 */
831a81c400eSJeff Roberson 	uma_startup2();
832df8bae1dSRodney W. Grimes }
83386f08737SRobert Watson 
834483f692eSMark Johnston /*
835483f692eSMark Johnston  *	kmem_bootstrap_free:
836483f692eSMark Johnston  *
837483f692eSMark Johnston  *	Free pages backing preloaded data (e.g., kernel modules) to the
838483f692eSMark Johnston  *	system.  Currently only supported on platforms that create a
839483f692eSMark Johnston  *	vm_phys segment for preloaded data.
840483f692eSMark Johnston  */
841483f692eSMark Johnston void
842483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
843483f692eSMark Johnston {
844483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__)
845483f692eSMark Johnston 	struct vm_domain *vmd;
8466c85795aSMark Johnston 	vm_offset_t end, va;
847483f692eSMark Johnston 	vm_paddr_t pa;
848483f692eSMark Johnston 	vm_page_t m;
849483f692eSMark Johnston 
850483f692eSMark Johnston 	end = trunc_page(start + size);
851483f692eSMark Johnston 	start = round_page(start);
852483f692eSMark Johnston 
853f822c9e2SMark Johnston #ifdef __amd64__
854f822c9e2SMark Johnston 	/*
855f822c9e2SMark Johnston 	 * Preloaded files do not have execute permissions by default on amd64.
856f822c9e2SMark Johnston 	 * Restore the default permissions to ensure that the direct map alias
857f822c9e2SMark Johnston 	 * is updated.
858f822c9e2SMark Johnston 	 */
859f822c9e2SMark Johnston 	pmap_change_prot(start, end - start, VM_PROT_RW);
860f822c9e2SMark Johnston #endif
8616c85795aSMark Johnston 	for (va = start; va < end; va += PAGE_SIZE) {
8626c85795aSMark Johnston 		pa = pmap_kextract(va);
863483f692eSMark Johnston 		m = PHYS_TO_VM_PAGE(pa);
864483f692eSMark Johnston 
865483f692eSMark Johnston 		vmd = vm_pagequeue_domain(m);
866483f692eSMark Johnston 		vm_domain_free_lock(vmd);
867483f692eSMark Johnston 		vm_phys_free_pages(m, 0);
868483f692eSMark Johnston 		vm_domain_free_unlock(vmd);
869c16bd872SMark Johnston 
870c16bd872SMark Johnston 		vm_domain_freecnt_inc(vmd, 1);
871c16bd872SMark Johnston 		vm_cnt.v_page_count++;
872483f692eSMark Johnston 	}
8736c85795aSMark Johnston 	pmap_remove(kernel_pmap, start, end);
8746c85795aSMark Johnston 	(void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
875483f692eSMark Johnston #endif
876483f692eSMark Johnston }
877483f692eSMark Johnston 
87886f08737SRobert Watson /*
87986f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
88086f08737SRobert Watson  * purposes.
88186f08737SRobert Watson  */
88286f08737SRobert Watson static int
88386f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
88486f08737SRobert Watson {
88586f08737SRobert Watson 	int error, i;
88686f08737SRobert Watson 
88786f08737SRobert Watson 	i = 0;
88886f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
88986f08737SRobert Watson 	if (error)
89086f08737SRobert Watson 		return (error);
8919b43bc27SAndriy Gapon 	if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
8929b43bc27SAndriy Gapon 		return (EINVAL);
8939b43bc27SAndriy Gapon 	if (i != 0)
8949b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, i);
89586f08737SRobert Watson 	return (0);
89686f08737SRobert Watson }
89786f08737SRobert Watson 
898a314aba8SMateusz Guzik SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0,
8999b43bc27SAndriy Gapon     debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");
900