xref: /freebsd/sys/vm/vm_kern.c (revision 303b77029b66fec6a23f7639acf91b7a8aec1464)
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,
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)
1311aed6d48SMark Johnston 
132a81c400eSJeff Roberson extern void     uma_startup2(void);
133a81c400eSJeff Roberson 
134df8bae1dSRodney W. Grimes /*
1355df87b21SJeff Roberson  *	kva_alloc:
136a839bdc8SDmitrij Tejblum  *
137b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
138b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
139b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
140b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
141b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
142a839bdc8SDmitrij Tejblum  */
143a839bdc8SDmitrij Tejblum vm_offset_t
1446e309d75SEd Maste kva_alloc(vm_size_t size)
145a839bdc8SDmitrij Tejblum {
146a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
147a839bdc8SDmitrij Tejblum 
148a839bdc8SDmitrij Tejblum 	size = round_page(size);
1495df87b21SJeff Roberson 	if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
150a839bdc8SDmitrij Tejblum 		return (0);
1515df87b21SJeff Roberson 
152a839bdc8SDmitrij Tejblum 	return (addr);
153a839bdc8SDmitrij Tejblum }
154a839bdc8SDmitrij Tejblum 
155a839bdc8SDmitrij Tejblum /*
1565df87b21SJeff Roberson  *	kva_free:
157ca596a25SJuli Mallett  *
1585df87b21SJeff Roberson  *	Release a region of kernel virtual memory allocated
1595df87b21SJeff Roberson  *	with kva_alloc, and return the physical pages
1605df87b21SJeff Roberson  *	associated with that region.
1615df87b21SJeff Roberson  *
1625df87b21SJeff Roberson  *	This routine may not block on kernel maps.
163ca596a25SJuli Mallett  */
1645df87b21SJeff Roberson void
1656e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size)
166df8bae1dSRodney W. Grimes {
167df8bae1dSRodney W. Grimes 
168df8bae1dSRodney W. Grimes 	size = round_page(size);
1695df87b21SJeff Roberson 	vmem_free(kernel_arena, addr, size);
170df8bae1dSRodney W. Grimes }
171df8bae1dSRodney W. Grimes 
172df8bae1dSRodney W. Grimes /*
1730ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physical pages
1740ff0fc84SAlan Cox  *	within the specified address range to the kernel object.  Creates a
1750ff0fc84SAlan Cox  *	wired mapping from this region to these pages, and returns the
1760ff0fc84SAlan Cox  *	region's starting virtual address.  The allocated pages are not
1770ff0fc84SAlan Cox  *	necessarily physically contiguous.  If M_ZERO is specified through the
1780ff0fc84SAlan Cox  *	given flags, then the pages are zeroed before they are mapped.
1790ff0fc84SAlan Cox  */
1809978bd99SMark Johnston static vm_offset_t
1817a469c8eSJeff Roberson kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
1820ff0fc84SAlan Cox     vm_paddr_t high, vm_memattr_t memattr)
1830ff0fc84SAlan Cox {
1847a469c8eSJeff Roberson 	vmem_t *vmem;
1852e47807cSJeff Roberson 	vm_object_t object = kernel_object;
186d1780e8dSKonstantin Belousov 	vm_offset_t addr, i, offset;
1870ff0fc84SAlan Cox 	vm_page_t m;
1880ff0fc84SAlan Cox 	int pflags, tries;
189d22ff6e6SKonstantin Belousov 	vm_prot_t prot;
1900ff0fc84SAlan Cox 
1910ff0fc84SAlan Cox 	size = round_page(size);
1927a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
1935df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
1940ff0fc84SAlan Cox 		return (0);
1950ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
19691e31c3cSJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
1978d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
1988d6fbbb8SJeff Roberson 	pflags |= VM_ALLOC_NOWAIT;
199d22ff6e6SKonstantin Belousov 	prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
20089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2015df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
2020ff0fc84SAlan Cox 		tries = 0;
2030ff0fc84SAlan Cox retry:
2047a469c8eSJeff Roberson 		m = vm_page_alloc_contig_domain(object, atop(offset + i),
2057a469c8eSJeff Roberson 		    domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
2060ff0fc84SAlan Cox 		if (m == NULL) {
20789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
2080ff0fc84SAlan Cox 			if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
2097a469c8eSJeff Roberson 				if (!vm_page_reclaim_contig_domain(domain,
2107a469c8eSJeff Roberson 				    pflags, 1, low, high, PAGE_SIZE, 0) &&
211c869e672SAlan Cox 				    (flags & M_WAITOK) != 0)
212e2068d0bSJeff Roberson 					vm_wait_domain(domain);
21389f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
2140ff0fc84SAlan Cox 				tries++;
2150ff0fc84SAlan Cox 				goto retry;
2160ff0fc84SAlan Cox 			}
2179e829b22SAlan Cox 			kmem_unback(object, addr, i);
2185df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
2190ff0fc84SAlan Cox 			return (0);
2200ff0fc84SAlan Cox 		}
221e2068d0bSJeff Roberson 		KASSERT(vm_phys_domain(m) == domain,
2227a469c8eSJeff Roberson 		    ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
223e2068d0bSJeff Roberson 		    vm_phys_domain(m), domain));
2240ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2250ff0fc84SAlan Cox 			pmap_zero_page(m);
22691e31c3cSJeff Roberson 		vm_page_valid(m);
227d22ff6e6SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, prot,
228d22ff6e6SKonstantin Belousov 		    prot | PMAP_ENTER_WIRED, 0);
2290ff0fc84SAlan Cox 	}
23089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2310ff0fc84SAlan Cox 	return (addr);
2320ff0fc84SAlan Cox }
2330ff0fc84SAlan Cox 
2347a469c8eSJeff Roberson vm_offset_t
235db7c2a48SAlan Cox kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
236db7c2a48SAlan Cox     vm_memattr_t memattr)
2377a469c8eSJeff Roberson {
2389978bd99SMark Johnston 
2399978bd99SMark Johnston 	return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low,
2409978bd99SMark Johnston 	    high, memattr));
2419978bd99SMark Johnston }
2429978bd99SMark Johnston 
2439978bd99SMark Johnston vm_offset_t
2449978bd99SMark Johnston kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags,
2459978bd99SMark Johnston     vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr)
2469978bd99SMark Johnston {
2477a469c8eSJeff Roberson 	struct vm_domainset_iter di;
2487a469c8eSJeff Roberson 	vm_offset_t addr;
2497a469c8eSJeff Roberson 	int domain;
2507a469c8eSJeff Roberson 
2519978bd99SMark Johnston 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
2527a469c8eSJeff Roberson 	do {
2537a469c8eSJeff Roberson 		addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
2547a469c8eSJeff Roberson 		    memattr);
2557a469c8eSJeff Roberson 		if (addr != 0)
2567a469c8eSJeff Roberson 			break;
2574c29d2deSMark Johnston 	} while (vm_domainset_iter_policy(&di, &domain) == 0);
2587a469c8eSJeff Roberson 
2597a469c8eSJeff Roberson 	return (addr);
2607a469c8eSJeff Roberson }
2617a469c8eSJeff Roberson 
2620ff0fc84SAlan Cox /*
2630ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2640ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2650ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2660ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2670ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2680ff0fc84SAlan Cox  *	mapped.
2690ff0fc84SAlan Cox  */
2709978bd99SMark Johnston static vm_offset_t
2717a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
2720ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2730ff0fc84SAlan Cox     vm_memattr_t memattr)
2740ff0fc84SAlan Cox {
2757a469c8eSJeff Roberson 	vmem_t *vmem;
2762e47807cSJeff Roberson 	vm_object_t object = kernel_object;
277d1780e8dSKonstantin Belousov 	vm_offset_t addr, offset, tmp;
2780ff0fc84SAlan Cox 	vm_page_t end_m, m;
279c869e672SAlan Cox 	u_long npages;
2800ff0fc84SAlan Cox 	int pflags, tries;
2810ff0fc84SAlan Cox 
2820ff0fc84SAlan Cox 	size = round_page(size);
2837a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
2845df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
2850ff0fc84SAlan Cox 		return (0);
2860ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
28791e31c3cSJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
2888d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
2898d6fbbb8SJeff Roberson 	pflags |= VM_ALLOC_NOWAIT;
290c869e672SAlan Cox 	npages = atop(size);
29189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2920ff0fc84SAlan Cox 	tries = 0;
2930ff0fc84SAlan Cox retry:
2947a469c8eSJeff Roberson 	m = vm_page_alloc_contig_domain(object, atop(offset), domain, pflags,
295c869e672SAlan Cox 	    npages, low, high, alignment, boundary, memattr);
2960ff0fc84SAlan Cox 	if (m == NULL) {
29789f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2980ff0fc84SAlan Cox 		if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
2997a469c8eSJeff Roberson 			if (!vm_page_reclaim_contig_domain(domain, pflags,
3007a469c8eSJeff Roberson 			    npages, low, high, alignment, boundary) &&
3017a469c8eSJeff Roberson 			    (flags & M_WAITOK) != 0)
302e2068d0bSJeff Roberson 				vm_wait_domain(domain);
30389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
3040ff0fc84SAlan Cox 			tries++;
3050ff0fc84SAlan Cox 			goto retry;
3060ff0fc84SAlan Cox 		}
3075df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
3080ff0fc84SAlan Cox 		return (0);
3090ff0fc84SAlan Cox 	}
310e2068d0bSJeff Roberson 	KASSERT(vm_phys_domain(m) == domain,
3117a469c8eSJeff Roberson 	    ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
312e2068d0bSJeff Roberson 	    vm_phys_domain(m), domain));
313c869e672SAlan Cox 	end_m = m + npages;
3145df87b21SJeff Roberson 	tmp = addr;
3150ff0fc84SAlan Cox 	for (; m < end_m; m++) {
3160ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
3170ff0fc84SAlan Cox 			pmap_zero_page(m);
31891e31c3cSJeff Roberson 		vm_page_valid(m);
3190766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
3200766f278SJonathan T. Looney 		    VM_PROT_RW | PMAP_ENTER_WIRED, 0);
3215df87b21SJeff Roberson 		tmp += PAGE_SIZE;
3220ff0fc84SAlan Cox 	}
32389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
3240ff0fc84SAlan Cox 	return (addr);
3250ff0fc84SAlan Cox }
3260ff0fc84SAlan Cox 
3277a469c8eSJeff Roberson vm_offset_t
32844d0efb2SAlan Cox kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high,
32944d0efb2SAlan Cox     u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
3307a469c8eSJeff Roberson {
3319978bd99SMark Johnston 
3329978bd99SMark Johnston 	return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low,
3339978bd99SMark Johnston 	    high, alignment, boundary, memattr));
3349978bd99SMark Johnston }
3359978bd99SMark Johnston 
3369978bd99SMark Johnston vm_offset_t
3379978bd99SMark Johnston kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags,
3389978bd99SMark Johnston     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
3399978bd99SMark Johnston     vm_memattr_t memattr)
3409978bd99SMark Johnston {
3417a469c8eSJeff Roberson 	struct vm_domainset_iter di;
3427a469c8eSJeff Roberson 	vm_offset_t addr;
3437a469c8eSJeff Roberson 	int domain;
3447a469c8eSJeff Roberson 
3459978bd99SMark Johnston 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
3467a469c8eSJeff Roberson 	do {
3477a469c8eSJeff Roberson 		addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
3487a469c8eSJeff Roberson 		    alignment, boundary, memattr);
3497a469c8eSJeff Roberson 		if (addr != 0)
3507a469c8eSJeff Roberson 			break;
3514c29d2deSMark Johnston 	} while (vm_domainset_iter_policy(&di, &domain) == 0);
3527a469c8eSJeff Roberson 
3537a469c8eSJeff Roberson 	return (addr);
3547a469c8eSJeff Roberson }
3557a469c8eSJeff Roberson 
3560ff0fc84SAlan Cox /*
357df8bae1dSRodney W. Grimes  *	kmem_suballoc:
358df8bae1dSRodney W. Grimes  *
359df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
360df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
361df8bae1dSRodney W. Grimes  *
362df8bae1dSRodney W. Grimes  *	Arguments are as follows:
363df8bae1dSRodney W. Grimes  *
364df8bae1dSRodney W. Grimes  *	parent		Map to take range from
365df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
366030f2369SAlfred Perlstein  *	size		Size of range to find
3673202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
368df8bae1dSRodney W. Grimes  */
3690d94caffSDavid Greenman vm_map_t
3703202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
3713202ed75SAlan Cox     vm_size_t size, boolean_t superpage_align)
372df8bae1dSRodney W. Grimes {
3736e4f51d1SAlfred Perlstein 	int ret;
374df8bae1dSRodney W. Grimes 	vm_map_t result;
37523955314SAlfred Perlstein 
376df8bae1dSRodney W. Grimes 	size = round_page(size);
377df8bae1dSRodney W. Grimes 
3782bc24aa9SAlan Cox 	*min = vm_map_min(parent);
379edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
3805aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
3813364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
38224dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
38324dedba9SAlan Cox 		panic("kmem_suballoc: bad status return of %d", ret);
384df8bae1dSRodney W. Grimes 	*max = *min + size;
3852d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
386df8bae1dSRodney W. Grimes 	if (result == NULL)
387df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
3886e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
389df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
390df8bae1dSRodney W. Grimes 	return (result);
391df8bae1dSRodney W. Grimes }
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes /*
3949978bd99SMark Johnston  *	kmem_malloc_domain:
3951c7c3c6aSMatthew Dillon  *
3965df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
397df8bae1dSRodney W. Grimes  */
3989978bd99SMark Johnston static vm_offset_t
399067fd858SAlan Cox kmem_malloc_domain(int domain, vm_size_t size, int flags)
400df8bae1dSRodney W. Grimes {
4010766f278SJonathan T. Looney 	vmem_t *arena;
402df8bae1dSRodney W. Grimes 	vm_offset_t addr;
4035df87b21SJeff Roberson 	int rv;
404df8bae1dSRodney W. Grimes 
405067fd858SAlan Cox 	if (__predict_true((flags & M_EXEC) == 0))
4060766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_arena;
4070766f278SJonathan T. Looney 	else
4080766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
409df8bae1dSRodney W. Grimes 	size = round_page(size);
4100766f278SJonathan T. Looney 	if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr))
4115df87b21SJeff Roberson 		return (0);
412df8bae1dSRodney W. Grimes 
4137a469c8eSJeff Roberson 	rv = kmem_back_domain(domain, kernel_object, addr, size, flags);
4145df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
4150766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
416f31c239dSAlan Cox 		return (0);
417df8bae1dSRodney W. Grimes 	}
4185df87b21SJeff Roberson 	return (addr);
419e3813573SMatthew D Fleming }
420e3813573SMatthew D Fleming 
4217a469c8eSJeff Roberson vm_offset_t
42283a90bffSAlan Cox kmem_malloc(vm_size_t size, int flags)
4237a469c8eSJeff Roberson {
4249978bd99SMark Johnston 
4259978bd99SMark Johnston 	return (kmem_malloc_domainset(DOMAINSET_RR(), size, flags));
4269978bd99SMark Johnston }
4279978bd99SMark Johnston 
4289978bd99SMark Johnston vm_offset_t
4299978bd99SMark Johnston kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags)
4309978bd99SMark Johnston {
4317a469c8eSJeff Roberson 	struct vm_domainset_iter di;
4327a469c8eSJeff Roberson 	vm_offset_t addr;
4337a469c8eSJeff Roberson 	int domain;
4347a469c8eSJeff Roberson 
4359978bd99SMark Johnston 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
4367a469c8eSJeff Roberson 	do {
437067fd858SAlan Cox 		addr = kmem_malloc_domain(domain, size, flags);
4387a469c8eSJeff Roberson 		if (addr != 0)
4397a469c8eSJeff Roberson 			break;
4404c29d2deSMark Johnston 	} while (vm_domainset_iter_policy(&di, &domain) == 0);
4417a469c8eSJeff Roberson 
4427a469c8eSJeff Roberson 	return (addr);
4437a469c8eSJeff Roberson }
4447a469c8eSJeff Roberson 
445e3813573SMatthew D Fleming /*
44625ed23cfSMark Johnston  *	kmem_back_domain:
447e3813573SMatthew D Fleming  *
44825ed23cfSMark Johnston  *	Allocate physical pages from the specified domain for the specified
44925ed23cfSMark Johnston  *	virtual address range.
450e3813573SMatthew D Fleming  */
451e3813573SMatthew D Fleming int
4527a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
4537a469c8eSJeff Roberson     vm_size_t size, int flags)
454e3813573SMatthew D Fleming {
455e3813573SMatthew D Fleming 	vm_offset_t offset, i;
45633fff5d5SMark Johnston 	vm_page_t m, mpred;
4570766f278SJonathan T. Looney 	vm_prot_t prot;
458e3813573SMatthew D Fleming 	int pflags;
459e3813573SMatthew D Fleming 
4602e47807cSJeff Roberson 	KASSERT(object == kernel_object,
4617a469c8eSJeff Roberson 	    ("kmem_back_domain: only supports kernel object."));
4625df87b21SJeff Roberson 
4630891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
46491e31c3cSJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED;
4658d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
4668d6fbbb8SJeff Roberson 	if (flags & M_WAITOK)
4678d6fbbb8SJeff Roberson 		pflags |= VM_ALLOC_WAITFAIL;
4680766f278SJonathan T. Looney 	prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
469df8bae1dSRodney W. Grimes 
47033fff5d5SMark Johnston 	i = 0;
47133fff5d5SMark Johnston 	VM_OBJECT_WLOCK(object);
4728d6fbbb8SJeff Roberson retry:
47333fff5d5SMark Johnston 	mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
47433fff5d5SMark Johnston 	for (; i < size; i += PAGE_SIZE, mpred = m) {
4757a469c8eSJeff Roberson 		m = vm_page_alloc_domain_after(object, atop(offset + i),
4767a469c8eSJeff Roberson 		    domain, pflags, mpred);
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes 		/*
4790d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
4800d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
4810d94caffSDavid Greenman 		 * aren't on any queues.
482df8bae1dSRodney W. Grimes 		 */
483df8bae1dSRodney W. Grimes 		if (m == NULL) {
4848d6fbbb8SJeff Roberson 			if ((flags & M_NOWAIT) == 0)
485b18bfc3dSJohn Dyson 				goto retry;
4868d6fbbb8SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
4879e829b22SAlan Cox 			kmem_unback(object, addr, i);
488e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
489df8bae1dSRodney W. Grimes 		}
490e2068d0bSJeff Roberson 		KASSERT(vm_phys_domain(m) == domain,
4917a469c8eSJeff Roberson 		    ("kmem_back_domain: Domain mismatch %d != %d",
492e2068d0bSJeff Roberson 		    vm_phys_domain(m), domain));
4931e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
494fff6062aSAlan Cox 			pmap_zero_page(m);
495d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
4969f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
49791e31c3cSJeff Roberson 		vm_page_valid(m);
4980766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, addr + i, m, prot,
4990766f278SJonathan T. Looney 		    prot | PMAP_ENTER_WIRED, 0);
50049bfa624SAlan Cox 		if (__predict_false((prot & VM_PROT_EXECUTE) != 0))
50149bfa624SAlan Cox 			m->oflags |= VPO_KMEM_EXEC;
502df8bae1dSRodney W. Grimes 	}
5035df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
504df8bae1dSRodney W. Grimes 
505e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
506df8bae1dSRodney W. Grimes }
507df8bae1dSRodney W. Grimes 
50825ed23cfSMark Johnston /*
50925ed23cfSMark Johnston  *	kmem_back:
51025ed23cfSMark Johnston  *
51125ed23cfSMark Johnston  *	Allocate physical pages for the specified virtual address range.
51225ed23cfSMark Johnston  */
5137a469c8eSJeff Roberson int
5147a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
5157a469c8eSJeff Roberson {
51625ed23cfSMark Johnston 	vm_offset_t end, next, start;
51725ed23cfSMark Johnston 	int domain, rv;
5187a469c8eSJeff Roberson 
5197a469c8eSJeff Roberson 	KASSERT(object == kernel_object,
5207a469c8eSJeff Roberson 	    ("kmem_back: only supports kernel object."));
5217a469c8eSJeff Roberson 
52225ed23cfSMark Johnston 	for (start = addr, end = addr + size; addr < end; addr = next) {
52325ed23cfSMark Johnston 		/*
52425ed23cfSMark Johnston 		 * We must ensure that pages backing a given large virtual page
52525ed23cfSMark Johnston 		 * all come from the same physical domain.
52625ed23cfSMark Johnston 		 */
52725ed23cfSMark Johnston 		if (vm_ndomains > 1) {
52825ed23cfSMark Johnston 			domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains;
52930c5525bSAndrew Gallatin 			while (VM_DOMAIN_EMPTY(domain))
53030c5525bSAndrew Gallatin 				domain++;
53125ed23cfSMark Johnston 			next = roundup2(addr + 1, KVA_QUANTUM);
53225ed23cfSMark Johnston 			if (next > end || next < start)
53325ed23cfSMark Johnston 				next = end;
5343d14a7bbSMark Johnston 		} else {
5353d14a7bbSMark Johnston 			domain = 0;
53625ed23cfSMark Johnston 			next = end;
5373d14a7bbSMark Johnston 		}
53825ed23cfSMark Johnston 		rv = kmem_back_domain(domain, object, addr, next - addr, flags);
53925ed23cfSMark Johnston 		if (rv != KERN_SUCCESS) {
54025ed23cfSMark Johnston 			kmem_unback(object, start, addr - start);
5417a469c8eSJeff Roberson 			break;
54225ed23cfSMark Johnston 		}
54325ed23cfSMark Johnston 	}
54425ed23cfSMark Johnston 	return (rv);
5457a469c8eSJeff Roberson }
5467a469c8eSJeff Roberson 
5479e829b22SAlan Cox /*
5489e829b22SAlan Cox  *	kmem_unback:
5499e829b22SAlan Cox  *
5509e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
5519e829b22SAlan Cox  *	address range.
5529e829b22SAlan Cox  *
5539e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
5549e829b22SAlan Cox  *	that is being unmapped.
5559e829b22SAlan Cox  */
55649bfa624SAlan Cox static struct vmem *
5577a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5585df87b21SJeff Roberson {
55949bfa624SAlan Cox 	struct vmem *arena;
5607e05ffa6SMark Johnston 	vm_page_t m, next;
5617e05ffa6SMark Johnston 	vm_offset_t end, offset;
5627a469c8eSJeff Roberson 	int domain;
5635df87b21SJeff Roberson 
5642e47807cSJeff Roberson 	KASSERT(object == kernel_object,
5652e47807cSJeff Roberson 	    ("kmem_unback: only supports kernel object."));
5665df87b21SJeff Roberson 
5677a469c8eSJeff Roberson 	if (size == 0)
56849bfa624SAlan Cox 		return (NULL);
569fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
5705df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
5717e05ffa6SMark Johnston 	end = offset + size;
5725df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
5737a469c8eSJeff Roberson 	m = vm_page_lookup(object, atop(offset));
574e2068d0bSJeff Roberson 	domain = vm_phys_domain(m);
57549bfa624SAlan Cox 	if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0))
57649bfa624SAlan Cox 		arena = vm_dom[domain].vmd_kernel_arena;
57749bfa624SAlan Cox 	else
57849bfa624SAlan Cox 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
5797a469c8eSJeff Roberson 	for (; offset < end; offset += PAGE_SIZE, m = next) {
5807e05ffa6SMark Johnston 		next = vm_page_next(m);
581e9ceb9ddSJeff Roberson 		vm_page_xbusy_claim(m);
58288ea538aSMark Johnston 		vm_page_unwire_noq(m);
5835df87b21SJeff Roberson 		vm_page_free(m);
5845df87b21SJeff Roberson 	}
5855df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
5867a469c8eSJeff Roberson 
58749bfa624SAlan Cox 	return (arena);
5887a469c8eSJeff Roberson }
5897a469c8eSJeff Roberson 
5907a469c8eSJeff Roberson void
5917a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5927a469c8eSJeff Roberson {
5937a469c8eSJeff Roberson 
59449bfa624SAlan Cox 	(void)_kmem_unback(object, addr, size);
5955df87b21SJeff Roberson }
5965df87b21SJeff Roberson 
597df8bae1dSRodney W. Grimes /*
5985df87b21SJeff Roberson  *	kmem_free:
5995df87b21SJeff Roberson  *
6005df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
6015df87b21SJeff Roberson  *	original allocation.
6025df87b21SJeff Roberson  */
6035df87b21SJeff Roberson void
60449bfa624SAlan Cox kmem_free(vm_offset_t addr, vm_size_t size)
6055df87b21SJeff Roberson {
6060766f278SJonathan T. Looney 	struct vmem *arena;
6070766f278SJonathan T. Looney 
6085df87b21SJeff Roberson 	size = round_page(size);
60949bfa624SAlan Cox 	arena = _kmem_unback(kernel_object, addr, size);
61049bfa624SAlan Cox 	if (arena != NULL)
6110766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
6125df87b21SJeff Roberson }
6135df87b21SJeff Roberson 
6145df87b21SJeff Roberson /*
6155df87b21SJeff Roberson  *	kmap_alloc_wait:
616df8bae1dSRodney W. Grimes  *
617df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
618df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
619df8bae1dSRodney W. Grimes  *
6201c7c3c6aSMatthew Dillon  *	This routine may block.
621df8bae1dSRodney W. Grimes  */
6220d94caffSDavid Greenman vm_offset_t
6236e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size)
624df8bae1dSRodney W. Grimes {
625df8bae1dSRodney W. Grimes 	vm_offset_t addr;
62623955314SAlfred Perlstein 
627df8bae1dSRodney W. Grimes 	size = round_page(size);
6283364c323SKonstantin Belousov 	if (!swap_reserve(size))
6293364c323SKonstantin Belousov 		return (0);
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes 	for (;;) {
632df8bae1dSRodney W. Grimes 		/*
6330d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
6340d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
635df8bae1dSRodney W. Grimes 		 */
636df8bae1dSRodney W. Grimes 		vm_map_lock(map);
6379f701172SKonstantin Belousov 		addr = vm_map_findspace(map, vm_map_min(map), size);
6389f701172SKonstantin Belousov 		if (addr + size <= vm_map_max(map))
639df8bae1dSRodney W. Grimes 			break;
640df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
641df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
642df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6433364c323SKonstantin Belousov 			swap_release(size);
644df8bae1dSRodney W. Grimes 			return (0);
645df8bae1dSRodney W. Grimes 		}
6469688f931SAlan Cox 		map->needs_wakeup = TRUE;
6478ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
648df8bae1dSRodney W. Grimes 	}
64907702f72SMark Johnston 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW,
65007702f72SMark Johnston 	    MAP_ACC_CHARGED);
651df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
652df8bae1dSRodney W. Grimes 	return (addr);
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes /*
6565df87b21SJeff Roberson  *	kmap_free_wakeup:
657df8bae1dSRodney W. Grimes  *
65824a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
659df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
660df8bae1dSRodney W. Grimes  */
6610d94caffSDavid Greenman void
6626e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
663df8bae1dSRodney W. Grimes {
66423955314SAlfred Perlstein 
665df8bae1dSRodney W. Grimes 	vm_map_lock(map);
666655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
6679688f931SAlan Cox 	if (map->needs_wakeup) {
6689688f931SAlan Cox 		map->needs_wakeup = FALSE;
6699688f931SAlan Cox 		vm_map_wakeup(map);
6709688f931SAlan Cox 	}
671df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
672df8bae1dSRodney W. Grimes }
673df8bae1dSRodney W. Grimes 
6745df87b21SJeff Roberson void
67589cb2a19SMatthew D Fleming kmem_init_zero_region(void)
67689cb2a19SMatthew D Fleming {
677cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
67889cb2a19SMatthew D Fleming 	vm_page_t m;
67989cb2a19SMatthew D Fleming 
680cfb00e5aSMatthew D Fleming 	/*
681cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
682cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
683cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
684cfb00e5aSMatthew D Fleming 	 */
6855df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
686703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
68789cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
68889cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
68989cb2a19SMatthew D Fleming 		pmap_zero_page(m);
69089cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
69189cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
6925df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
69389cb2a19SMatthew D Fleming 
69489cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
69589cb2a19SMatthew D Fleming }
69689cb2a19SMatthew D Fleming 
697df8bae1dSRodney W. Grimes /*
698969e147aSMark Johnston  * Import KVA from the kernel map into the kernel arena.
6991aed6d48SMark Johnston  */
7001aed6d48SMark Johnston static int
7011aed6d48SMark Johnston kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp)
7021aed6d48SMark Johnston {
7031aed6d48SMark Johnston 	vm_offset_t addr;
7041aed6d48SMark Johnston 	int result;
7051aed6d48SMark Johnston 
7061aed6d48SMark Johnston 	KASSERT((size % KVA_QUANTUM) == 0,
7071aed6d48SMark Johnston 	    ("kva_import: Size %jd is not a multiple of %d",
7081aed6d48SMark Johnston 	    (intmax_t)size, (int)KVA_QUANTUM));
7091aed6d48SMark Johnston 	addr = vm_map_min(kernel_map);
7101aed6d48SMark Johnston 	result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
7111aed6d48SMark Johnston 	    VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
7121aed6d48SMark Johnston 	if (result != KERN_SUCCESS)
7131aed6d48SMark Johnston                 return (ENOMEM);
7141aed6d48SMark Johnston 
7151aed6d48SMark Johnston 	*addrp = addr;
7161aed6d48SMark Johnston 
7171aed6d48SMark Johnston 	return (0);
7181aed6d48SMark Johnston }
7191aed6d48SMark Johnston 
7201aed6d48SMark Johnston /*
721969e147aSMark Johnston  * Import KVA from a parent arena into a per-domain arena.  Imports must be
722969e147aSMark Johnston  * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size.
7231aed6d48SMark Johnston  */
7241aed6d48SMark Johnston static int
725969e147aSMark Johnston kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp)
7261aed6d48SMark Johnston {
7271aed6d48SMark Johnston 
7281aed6d48SMark Johnston 	KASSERT((size % KVA_QUANTUM) == 0,
729969e147aSMark Johnston 	    ("kva_import_domain: Size %jd is not a multiple of %d",
7301aed6d48SMark Johnston 	    (intmax_t)size, (int)KVA_QUANTUM));
7311aed6d48SMark Johnston 	return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN,
7321aed6d48SMark Johnston 	    VMEM_ADDR_MAX, flags, addrp));
7331aed6d48SMark Johnston }
7341aed6d48SMark Johnston 
7351aed6d48SMark Johnston /*
7361c7c3c6aSMatthew Dillon  * 	kmem_init:
7371c7c3c6aSMatthew Dillon  *
7381c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
7391c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
7401c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
7411c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
7421aed6d48SMark Johnston  *	Create the kernel vmem arena and its per-domain children.
743df8bae1dSRodney W. Grimes  */
7440d94caffSDavid Greenman void
7456e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end)
746df8bae1dSRodney W. Grimes {
747030f2369SAlfred Perlstein 	vm_map_t m;
7481aed6d48SMark Johnston 	int domain;
749df8bae1dSRodney W. Grimes 
7502d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
751c9267356SAlan Cox 	m->system_map = 1;
752df8bae1dSRodney W. Grimes 	vm_map_lock(m);
753df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
754df8bae1dSRodney W. Grimes 	kernel_map = m;
7559d75f0dcSMark Johnston 	(void)vm_map_insert(m, NULL, 0,
7565cfa90e9SAlan Cox #ifdef __amd64__
7575cfa90e9SAlan Cox 	    KERNBASE,
7585cfa90e9SAlan Cox #else
7595cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
7605cfa90e9SAlan Cox #endif
7615cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
762df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
7639d75f0dcSMark Johnston 
7649d75f0dcSMark Johnston #ifdef __amd64__
7659d75f0dcSMark Johnston 	/*
7669d75f0dcSMark Johnston 	 * Mark KVA used for the page array as allocated.  Other platforms
7679d75f0dcSMark Johnston 	 * that handle vm_page_array allocation can simply adjust virtual_avail
7689d75f0dcSMark Johnston 	 * instead.
7699d75f0dcSMark Johnston 	 */
7709d75f0dcSMark Johnston 	(void)vm_map_insert(m, NULL, 0, (vm_offset_t)vm_page_array,
7719d75f0dcSMark Johnston 	    (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size *
7729d75f0dcSMark Johnston 	    sizeof(struct vm_page)),
7739d75f0dcSMark Johnston 	    VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
7749d75f0dcSMark Johnston #endif
775df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
7761aed6d48SMark Johnston 
7771aed6d48SMark Johnston 	/*
7781aed6d48SMark Johnston 	 * Initialize the kernel_arena.  This can grow on demand.
7791aed6d48SMark Johnston 	 */
7801aed6d48SMark Johnston 	vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0);
7811aed6d48SMark Johnston 	vmem_set_import(kernel_arena, kva_import, NULL, NULL, KVA_QUANTUM);
7821aed6d48SMark Johnston 
7831aed6d48SMark Johnston 	for (domain = 0; domain < vm_ndomains; domain++) {
784969e147aSMark Johnston 		/*
785969e147aSMark Johnston 		 * Initialize the per-domain arenas.  These are used to color
786969e147aSMark Johnston 		 * the KVA space in a way that ensures that virtual large pages
787969e147aSMark Johnston 		 * are backed by memory from the same physical domain,
788969e147aSMark Johnston 		 * maximizing the potential for superpage promotion.
789969e147aSMark Johnston 		 */
7901aed6d48SMark Johnston 		vm_dom[domain].vmd_kernel_arena = vmem_create(
7911aed6d48SMark Johnston 		    "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
7921aed6d48SMark Johnston 		vmem_set_import(vm_dom[domain].vmd_kernel_arena,
793969e147aSMark Johnston 		    kva_import_domain, NULL, kernel_arena, KVA_QUANTUM);
794969e147aSMark Johnston 
795969e147aSMark Johnston 		/*
796969e147aSMark Johnston 		 * In architectures with superpages, maintain separate arenas
797969e147aSMark Johnston 		 * for allocations with permissions that differ from the
798969e147aSMark Johnston 		 * "standard" read/write permissions used for kernel memory,
799969e147aSMark Johnston 		 * so as not to inhibit superpage promotion.
800969e147aSMark Johnston 		 */
8011aed6d48SMark Johnston #if VM_NRESERVLEVEL > 0
8021aed6d48SMark Johnston 		vm_dom[domain].vmd_kernel_rwx_arena = vmem_create(
8031aed6d48SMark Johnston 		    "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK);
8041aed6d48SMark Johnston 		vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena,
805969e147aSMark Johnston 		    kva_import_domain, (vmem_release_t *)vmem_xfree,
806969e147aSMark Johnston 		    kernel_arena, KVA_QUANTUM);
807*303b7702SMark Johnston #else
808*303b7702SMark Johnston 		vm_dom[domain].vmd_kernel_rwx_arena =
809*303b7702SMark Johnston 		    vm_dom[domain].vmd_kernel_arena;
8101aed6d48SMark Johnston #endif
8111aed6d48SMark Johnston 	}
812a81c400eSJeff Roberson 
813a81c400eSJeff Roberson 	/*
814a81c400eSJeff Roberson 	 * This must be the very first call so that the virtual address
815a81c400eSJeff Roberson 	 * space used for early allocations is properly marked used in
816a81c400eSJeff Roberson 	 * the map.
817a81c400eSJeff Roberson 	 */
818a81c400eSJeff Roberson 	uma_startup2();
819df8bae1dSRodney W. Grimes }
82086f08737SRobert Watson 
821483f692eSMark Johnston /*
822483f692eSMark Johnston  *	kmem_bootstrap_free:
823483f692eSMark Johnston  *
824483f692eSMark Johnston  *	Free pages backing preloaded data (e.g., kernel modules) to the
825483f692eSMark Johnston  *	system.  Currently only supported on platforms that create a
826483f692eSMark Johnston  *	vm_phys segment for preloaded data.
827483f692eSMark Johnston  */
828483f692eSMark Johnston void
829483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
830483f692eSMark Johnston {
831483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__)
832483f692eSMark Johnston 	struct vm_domain *vmd;
8336c85795aSMark Johnston 	vm_offset_t end, va;
834483f692eSMark Johnston 	vm_paddr_t pa;
835483f692eSMark Johnston 	vm_page_t m;
836483f692eSMark Johnston 
837483f692eSMark Johnston 	end = trunc_page(start + size);
838483f692eSMark Johnston 	start = round_page(start);
839483f692eSMark Johnston 
840f822c9e2SMark Johnston #ifdef __amd64__
841f822c9e2SMark Johnston 	/*
842f822c9e2SMark Johnston 	 * Preloaded files do not have execute permissions by default on amd64.
843f822c9e2SMark Johnston 	 * Restore the default permissions to ensure that the direct map alias
844f822c9e2SMark Johnston 	 * is updated.
845f822c9e2SMark Johnston 	 */
846f822c9e2SMark Johnston 	pmap_change_prot(start, end - start, VM_PROT_RW);
847f822c9e2SMark Johnston #endif
8486c85795aSMark Johnston 	for (va = start; va < end; va += PAGE_SIZE) {
8496c85795aSMark Johnston 		pa = pmap_kextract(va);
850483f692eSMark Johnston 		m = PHYS_TO_VM_PAGE(pa);
851483f692eSMark Johnston 
852483f692eSMark Johnston 		vmd = vm_pagequeue_domain(m);
853483f692eSMark Johnston 		vm_domain_free_lock(vmd);
854483f692eSMark Johnston 		vm_phys_free_pages(m, 0);
855483f692eSMark Johnston 		vm_domain_free_unlock(vmd);
856c16bd872SMark Johnston 
857c16bd872SMark Johnston 		vm_domain_freecnt_inc(vmd, 1);
858c16bd872SMark Johnston 		vm_cnt.v_page_count++;
859483f692eSMark Johnston 	}
8606c85795aSMark Johnston 	pmap_remove(kernel_pmap, start, end);
8616c85795aSMark Johnston 	(void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
862483f692eSMark Johnston #endif
863483f692eSMark Johnston }
864483f692eSMark Johnston 
86586f08737SRobert Watson /*
86686f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
86786f08737SRobert Watson  * purposes.
86886f08737SRobert Watson  */
86986f08737SRobert Watson static int
87086f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
87186f08737SRobert Watson {
87286f08737SRobert Watson 	int error, i;
87386f08737SRobert Watson 
87486f08737SRobert Watson 	i = 0;
87586f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
87686f08737SRobert Watson 	if (error)
87786f08737SRobert Watson 		return (error);
8789b43bc27SAndriy Gapon 	if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
8799b43bc27SAndriy Gapon 		return (EINVAL);
8809b43bc27SAndriy Gapon 	if (i != 0)
8819b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, i);
88286f08737SRobert Watson 	return (0);
88386f08737SRobert Watson }
88486f08737SRobert Watson 
885a314aba8SMateusz Guzik SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0,
8869b43bc27SAndriy Gapon     debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");
887