xref: /freebsd/sys/vm/vm_kern.c (revision 6c85795a256fac2ca714542b7241c6b28f1023da)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
343c4dd356SDavid Greenman  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38df8bae1dSRodney W. Grimes  * All rights reserved.
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  *	Kernel memory management.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67874651b1SDavid E. O'Brien #include <sys/cdefs.h>
68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
69874651b1SDavid E. O'Brien 
707a469c8eSJeff Roberson #include "opt_vm.h"
717a469c8eSJeff Roberson 
72df8bae1dSRodney W. Grimes #include <sys/param.h>
73df8bae1dSRodney W. Grimes #include <sys/systm.h>
7460363fb9SLuigi Rizzo #include <sys/kernel.h>		/* for ticks and hz */
757a469c8eSJeff Roberson #include <sys/domainset.h>
760f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h>
77fb919e4dSMark Murray #include <sys/lock.h>
78f23b4c91SGarrett Wollman #include <sys/proc.h>
79a1f6d91cSDavid Greenman #include <sys/malloc.h>
8089f6b863SAttilio Rao #include <sys/rwlock.h>
8186f08737SRobert Watson #include <sys/sysctl.h>
825df87b21SJeff Roberson #include <sys/vmem.h>
837a469c8eSJeff Roberson #include <sys/vmmeter.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
877a469c8eSJeff Roberson #include <vm/vm_domainset.h>
885df87b21SJeff Roberson #include <vm/vm_kern.h>
89efeaf95aSDavid Greenman #include <vm/pmap.h>
90efeaf95aSDavid Greenman #include <vm/vm_map.h>
91efeaf95aSDavid Greenman #include <vm/vm_object.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
947a469c8eSJeff Roberson #include <vm/vm_phys.h>
95e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
9633fff5d5SMark Johnston #include <vm/vm_radix.h>
979b4288a3SBruce Evans #include <vm/vm_extern.h>
980f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h>
99df8bae1dSRodney W. Grimes 
100e7788a47SKonstantin Belousov vm_map_t kernel_map;
101e7788a47SKonstantin Belousov vm_map_t exec_map;
102cebde069SMike Silbersack vm_map_t pipe_map;
103f23b4c91SGarrett Wollman 
10489cb2a19SMatthew D Fleming const void *zero_region;
10589cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
10689cb2a19SMatthew D Fleming 
107645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */
108645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
109645743eaSJohn Baldwin 
110ec492b13SMark Johnston u_int exec_map_entry_size;
111ec492b13SMark Johnston u_int exec_map_entries;
112ec492b13SMark Johnston 
11394bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
114f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
11594bfd5b1SMarius Strobl 
11694bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
117fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__)
11894bfd5b1SMarius Strobl     &vm_max_kernel_address, 0,
11994bfd5b1SMarius Strobl #else
120f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
12194bfd5b1SMarius Strobl #endif
12294bfd5b1SMarius Strobl     "Max kernel address");
12394bfd5b1SMarius Strobl 
124df8bae1dSRodney W. Grimes /*
1255df87b21SJeff Roberson  *	kva_alloc:
126a839bdc8SDmitrij Tejblum  *
127b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
128b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
129b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
130b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
131b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
132a839bdc8SDmitrij Tejblum  */
133a839bdc8SDmitrij Tejblum vm_offset_t
1346e309d75SEd Maste kva_alloc(vm_size_t size)
135a839bdc8SDmitrij Tejblum {
136a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
137a839bdc8SDmitrij Tejblum 
138a839bdc8SDmitrij Tejblum 	size = round_page(size);
1395df87b21SJeff Roberson 	if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
140a839bdc8SDmitrij Tejblum 		return (0);
1415df87b21SJeff Roberson 
142a839bdc8SDmitrij Tejblum 	return (addr);
143a839bdc8SDmitrij Tejblum }
144a839bdc8SDmitrij Tejblum 
145a839bdc8SDmitrij Tejblum /*
1465df87b21SJeff Roberson  *	kva_free:
147ca596a25SJuli Mallett  *
1485df87b21SJeff Roberson  *	Release a region of kernel virtual memory allocated
1495df87b21SJeff Roberson  *	with kva_alloc, and return the physical pages
1505df87b21SJeff Roberson  *	associated with that region.
1515df87b21SJeff Roberson  *
1525df87b21SJeff Roberson  *	This routine may not block on kernel maps.
153ca596a25SJuli Mallett  */
1545df87b21SJeff Roberson void
1556e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size)
156df8bae1dSRodney W. Grimes {
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	size = round_page(size);
1595df87b21SJeff Roberson 	vmem_free(kernel_arena, addr, size);
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes /*
1630ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physical pages
1640ff0fc84SAlan Cox  *	within the specified address range to the kernel object.  Creates a
1650ff0fc84SAlan Cox  *	wired mapping from this region to these pages, and returns the
1660ff0fc84SAlan Cox  *	region's starting virtual address.  The allocated pages are not
1670ff0fc84SAlan Cox  *	necessarily physically contiguous.  If M_ZERO is specified through the
1680ff0fc84SAlan Cox  *	given flags, then the pages are zeroed before they are mapped.
1690ff0fc84SAlan Cox  */
1700ff0fc84SAlan Cox vm_offset_t
1717a469c8eSJeff Roberson kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
1720ff0fc84SAlan Cox     vm_paddr_t high, vm_memattr_t memattr)
1730ff0fc84SAlan Cox {
1747a469c8eSJeff Roberson 	vmem_t *vmem;
1752e47807cSJeff Roberson 	vm_object_t object = kernel_object;
176d1780e8dSKonstantin Belousov 	vm_offset_t addr, i, offset;
1770ff0fc84SAlan Cox 	vm_page_t m;
1780ff0fc84SAlan Cox 	int pflags, tries;
1790ff0fc84SAlan Cox 
1800ff0fc84SAlan Cox 	size = round_page(size);
1817a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
1825df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
1830ff0fc84SAlan Cox 		return (0);
1840ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
1855df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
1868d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
1878d6fbbb8SJeff Roberson 	pflags |= VM_ALLOC_NOWAIT;
18889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1895df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
1900ff0fc84SAlan Cox 		tries = 0;
1910ff0fc84SAlan Cox retry:
1927a469c8eSJeff Roberson 		m = vm_page_alloc_contig_domain(object, atop(offset + i),
1937a469c8eSJeff Roberson 		    domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr);
1940ff0fc84SAlan Cox 		if (m == NULL) {
19589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1960ff0fc84SAlan Cox 			if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
1977a469c8eSJeff Roberson 				if (!vm_page_reclaim_contig_domain(domain,
1987a469c8eSJeff Roberson 				    pflags, 1, low, high, PAGE_SIZE, 0) &&
199c869e672SAlan Cox 				    (flags & M_WAITOK) != 0)
200e2068d0bSJeff Roberson 					vm_wait_domain(domain);
20189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
2020ff0fc84SAlan Cox 				tries++;
2030ff0fc84SAlan Cox 				goto retry;
2040ff0fc84SAlan Cox 			}
2059e829b22SAlan Cox 			kmem_unback(object, addr, i);
2065df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
2070ff0fc84SAlan Cox 			return (0);
2080ff0fc84SAlan Cox 		}
209e2068d0bSJeff Roberson 		KASSERT(vm_phys_domain(m) == domain,
2107a469c8eSJeff Roberson 		    ("kmem_alloc_attr_domain: Domain mismatch %d != %d",
211e2068d0bSJeff Roberson 		    vm_phys_domain(m), domain));
2120ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2130ff0fc84SAlan Cox 			pmap_zero_page(m);
2140ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
2150766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_RW,
2160766f278SJonathan T. Looney 		    VM_PROT_RW | PMAP_ENTER_WIRED, 0);
2170ff0fc84SAlan Cox 	}
21889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2190ff0fc84SAlan Cox 	return (addr);
2200ff0fc84SAlan Cox }
2210ff0fc84SAlan Cox 
2227a469c8eSJeff Roberson vm_offset_t
2237a469c8eSJeff Roberson kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low,
2247a469c8eSJeff Roberson     vm_paddr_t high, vm_memattr_t memattr)
2257a469c8eSJeff Roberson {
2267a469c8eSJeff Roberson 	struct vm_domainset_iter di;
2277a469c8eSJeff Roberson 	vm_offset_t addr;
2287a469c8eSJeff Roberson 	int domain;
2297a469c8eSJeff Roberson 
2307a469c8eSJeff Roberson 	KASSERT(vmem == kernel_arena,
2317a469c8eSJeff Roberson 	    ("kmem_alloc_attr: Only kernel_arena is supported."));
2327a469c8eSJeff Roberson 
2337a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
2347a469c8eSJeff Roberson 	do {
2357a469c8eSJeff Roberson 		addr = kmem_alloc_attr_domain(domain, size, flags, low, high,
2367a469c8eSJeff Roberson 		    memattr);
2377a469c8eSJeff Roberson 		if (addr != 0)
2387a469c8eSJeff Roberson 			break;
2397a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
2407a469c8eSJeff Roberson 
2417a469c8eSJeff Roberson 	return (addr);
2427a469c8eSJeff Roberson }
2437a469c8eSJeff Roberson 
2440ff0fc84SAlan Cox /*
2450ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2460ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2470ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2480ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2490ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2500ff0fc84SAlan Cox  *	mapped.
2510ff0fc84SAlan Cox  */
2520ff0fc84SAlan Cox vm_offset_t
2537a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low,
2540ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2550ff0fc84SAlan Cox     vm_memattr_t memattr)
2560ff0fc84SAlan Cox {
2577a469c8eSJeff Roberson 	vmem_t *vmem;
2582e47807cSJeff Roberson 	vm_object_t object = kernel_object;
259d1780e8dSKonstantin Belousov 	vm_offset_t addr, offset, tmp;
2600ff0fc84SAlan Cox 	vm_page_t end_m, m;
261c869e672SAlan Cox 	u_long npages;
2620ff0fc84SAlan Cox 	int pflags, tries;
2630ff0fc84SAlan Cox 
2640ff0fc84SAlan Cox 	size = round_page(size);
2657a469c8eSJeff Roberson 	vmem = vm_dom[domain].vmd_kernel_arena;
2665df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
2670ff0fc84SAlan Cox 		return (0);
2680ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
2695df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
2708d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
2718d6fbbb8SJeff Roberson 	pflags |= VM_ALLOC_NOWAIT;
272c869e672SAlan Cox 	npages = atop(size);
27389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2740ff0fc84SAlan Cox 	tries = 0;
2750ff0fc84SAlan Cox retry:
2767a469c8eSJeff Roberson 	m = vm_page_alloc_contig_domain(object, atop(offset), domain, pflags,
277c869e672SAlan Cox 	    npages, low, high, alignment, boundary, memattr);
2780ff0fc84SAlan Cox 	if (m == NULL) {
27989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2800ff0fc84SAlan Cox 		if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
2817a469c8eSJeff Roberson 			if (!vm_page_reclaim_contig_domain(domain, pflags,
2827a469c8eSJeff Roberson 			    npages, low, high, alignment, boundary) &&
2837a469c8eSJeff Roberson 			    (flags & M_WAITOK) != 0)
284e2068d0bSJeff Roberson 				vm_wait_domain(domain);
28589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2860ff0fc84SAlan Cox 			tries++;
2870ff0fc84SAlan Cox 			goto retry;
2880ff0fc84SAlan Cox 		}
2895df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
2900ff0fc84SAlan Cox 		return (0);
2910ff0fc84SAlan Cox 	}
292e2068d0bSJeff Roberson 	KASSERT(vm_phys_domain(m) == domain,
2937a469c8eSJeff Roberson 	    ("kmem_alloc_contig_domain: Domain mismatch %d != %d",
294e2068d0bSJeff Roberson 	    vm_phys_domain(m), domain));
295c869e672SAlan Cox 	end_m = m + npages;
2965df87b21SJeff Roberson 	tmp = addr;
2970ff0fc84SAlan Cox 	for (; m < end_m; m++) {
2980ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2990ff0fc84SAlan Cox 			pmap_zero_page(m);
3000ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
3010766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW,
3020766f278SJonathan T. Looney 		    VM_PROT_RW | PMAP_ENTER_WIRED, 0);
3035df87b21SJeff Roberson 		tmp += PAGE_SIZE;
3040ff0fc84SAlan Cox 	}
30589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
3060ff0fc84SAlan Cox 	return (addr);
3070ff0fc84SAlan Cox }
3080ff0fc84SAlan Cox 
3097a469c8eSJeff Roberson vm_offset_t
3107a469c8eSJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low,
3117a469c8eSJeff Roberson     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
3127a469c8eSJeff Roberson     vm_memattr_t memattr)
3137a469c8eSJeff Roberson {
3147a469c8eSJeff Roberson 	struct vm_domainset_iter di;
3157a469c8eSJeff Roberson 	vm_offset_t addr;
3167a469c8eSJeff Roberson 	int domain;
3177a469c8eSJeff Roberson 
3187a469c8eSJeff Roberson 	KASSERT(vmem == kernel_arena,
3197a469c8eSJeff Roberson 	    ("kmem_alloc_contig: Only kernel_arena is supported."));
3207a469c8eSJeff Roberson 
3217a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
3227a469c8eSJeff Roberson 	do {
3237a469c8eSJeff Roberson 		addr = kmem_alloc_contig_domain(domain, size, flags, low, high,
3247a469c8eSJeff Roberson 		    alignment, boundary, memattr);
3257a469c8eSJeff Roberson 		if (addr != 0)
3267a469c8eSJeff Roberson 			break;
3277a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
3287a469c8eSJeff Roberson 
3297a469c8eSJeff Roberson 	return (addr);
3307a469c8eSJeff Roberson }
3317a469c8eSJeff Roberson 
3320ff0fc84SAlan Cox /*
333df8bae1dSRodney W. Grimes  *	kmem_suballoc:
334df8bae1dSRodney W. Grimes  *
335df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
336df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
337df8bae1dSRodney W. Grimes  *
338df8bae1dSRodney W. Grimes  *	Arguments are as follows:
339df8bae1dSRodney W. Grimes  *
340df8bae1dSRodney W. Grimes  *	parent		Map to take range from
341df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
342030f2369SAlfred Perlstein  *	size		Size of range to find
3433202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
344df8bae1dSRodney W. Grimes  */
3450d94caffSDavid Greenman vm_map_t
3463202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
3473202ed75SAlan Cox     vm_size_t size, boolean_t superpage_align)
348df8bae1dSRodney W. Grimes {
3496e4f51d1SAlfred Perlstein 	int ret;
350df8bae1dSRodney W. Grimes 	vm_map_t result;
35123955314SAlfred Perlstein 
352df8bae1dSRodney W. Grimes 	size = round_page(size);
353df8bae1dSRodney W. Grimes 
3542bc24aa9SAlan Cox 	*min = vm_map_min(parent);
355edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
3565aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
3573364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
35824dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
35924dedba9SAlan Cox 		panic("kmem_suballoc: bad status return of %d", ret);
360df8bae1dSRodney W. Grimes 	*max = *min + size;
3612d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
362df8bae1dSRodney W. Grimes 	if (result == NULL)
363df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
3646e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
365df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
366df8bae1dSRodney W. Grimes 	return (result);
367df8bae1dSRodney W. Grimes }
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes /*
3701c7c3c6aSMatthew Dillon  *	kmem_malloc:
3711c7c3c6aSMatthew Dillon  *
3725df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
373df8bae1dSRodney W. Grimes  */
374df8bae1dSRodney W. Grimes vm_offset_t
3750766f278SJonathan T. Looney kmem_malloc_domain(struct vmem *vmem, int domain, vm_size_t size, int flags)
376df8bae1dSRodney W. Grimes {
3770766f278SJonathan T. Looney 	vmem_t *arena;
378df8bae1dSRodney W. Grimes 	vm_offset_t addr;
3795df87b21SJeff Roberson 	int rv;
380df8bae1dSRodney W. Grimes 
3810766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0
3820766f278SJonathan T. Looney 	KASSERT(vmem == kernel_arena || vmem == kernel_rwx_arena,
3830766f278SJonathan T. Looney 	    ("kmem_malloc_domain: Only kernel_arena or kernel_rwx_arena "
3840766f278SJonathan T. Looney 	    "are supported."));
3850766f278SJonathan T. Looney 	if (__predict_true(vmem == kernel_arena))
3860766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_arena;
3870766f278SJonathan T. Looney 	else
3880766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
3890766f278SJonathan T. Looney #else
3900766f278SJonathan T. Looney 	KASSERT(vmem == kernel_arena,
3910766f278SJonathan T. Looney 	    ("kmem_malloc_domain: Only kernel_arena is supported."));
3920766f278SJonathan T. Looney 	arena = vm_dom[domain].vmd_kernel_arena;
3930766f278SJonathan T. Looney #endif
394df8bae1dSRodney W. Grimes 	size = round_page(size);
3950766f278SJonathan T. Looney 	if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr))
3965df87b21SJeff Roberson 		return (0);
397df8bae1dSRodney W. Grimes 
3987a469c8eSJeff Roberson 	rv = kmem_back_domain(domain, kernel_object, addr, size, flags);
3995df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
4000766f278SJonathan T. Looney 		vmem_free(arena, addr, size);
401f31c239dSAlan Cox 		return (0);
402df8bae1dSRodney W. Grimes 	}
4035df87b21SJeff Roberson 	return (addr);
404e3813573SMatthew D Fleming }
405e3813573SMatthew D Fleming 
4067a469c8eSJeff Roberson vm_offset_t
4077a469c8eSJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags)
4087a469c8eSJeff Roberson {
4097a469c8eSJeff Roberson 	struct vm_domainset_iter di;
4107a469c8eSJeff Roberson 	vm_offset_t addr;
4117a469c8eSJeff Roberson 	int domain;
4127a469c8eSJeff Roberson 
4137a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
4147a469c8eSJeff Roberson 	do {
4150766f278SJonathan T. Looney 		addr = kmem_malloc_domain(vmem, domain, size, flags);
4167a469c8eSJeff Roberson 		if (addr != 0)
4177a469c8eSJeff Roberson 			break;
4187a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
4197a469c8eSJeff Roberson 
4207a469c8eSJeff Roberson 	return (addr);
4217a469c8eSJeff Roberson }
4227a469c8eSJeff Roberson 
423e3813573SMatthew D Fleming /*
424e3813573SMatthew D Fleming  *	kmem_back:
425e3813573SMatthew D Fleming  *
426e3813573SMatthew D Fleming  *	Allocate physical pages for the specified virtual address range.
427e3813573SMatthew D Fleming  */
428e3813573SMatthew D Fleming int
4297a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr,
4307a469c8eSJeff Roberson     vm_size_t size, int flags)
431e3813573SMatthew D Fleming {
432e3813573SMatthew D Fleming 	vm_offset_t offset, i;
43333fff5d5SMark Johnston 	vm_page_t m, mpred;
4340766f278SJonathan T. Looney 	vm_prot_t prot;
435e3813573SMatthew D Fleming 	int pflags;
436e3813573SMatthew D Fleming 
4372e47807cSJeff Roberson 	KASSERT(object == kernel_object,
4387a469c8eSJeff Roberson 	    ("kmem_back_domain: only supports kernel object."));
4395df87b21SJeff Roberson 
4400891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
4415df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
4428d6fbbb8SJeff Roberson 	pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL);
4438d6fbbb8SJeff Roberson 	if (flags & M_WAITOK)
4448d6fbbb8SJeff Roberson 		pflags |= VM_ALLOC_WAITFAIL;
4450766f278SJonathan T. Looney 	prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW;
446df8bae1dSRodney W. Grimes 
44733fff5d5SMark Johnston 	i = 0;
44833fff5d5SMark Johnston 	VM_OBJECT_WLOCK(object);
4498d6fbbb8SJeff Roberson retry:
45033fff5d5SMark Johnston 	mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i));
45133fff5d5SMark Johnston 	for (; i < size; i += PAGE_SIZE, mpred = m) {
4527a469c8eSJeff Roberson 		m = vm_page_alloc_domain_after(object, atop(offset + i),
4537a469c8eSJeff Roberson 		    domain, pflags, mpred);
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes 		/*
4560d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
4570d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
4580d94caffSDavid Greenman 		 * aren't on any queues.
459df8bae1dSRodney W. Grimes 		 */
460df8bae1dSRodney W. Grimes 		if (m == NULL) {
4618d6fbbb8SJeff Roberson 			if ((flags & M_NOWAIT) == 0)
462b18bfc3dSJohn Dyson 				goto retry;
4638d6fbbb8SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
4649e829b22SAlan Cox 			kmem_unback(object, addr, i);
465e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
466df8bae1dSRodney W. Grimes 		}
467e2068d0bSJeff Roberson 		KASSERT(vm_phys_domain(m) == domain,
4687a469c8eSJeff Roberson 		    ("kmem_back_domain: Domain mismatch %d != %d",
469e2068d0bSJeff Roberson 		    vm_phys_domain(m), domain));
4701e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
471fff6062aSAlan Cox 			pmap_zero_page(m);
472d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
4739f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
4745df87b21SJeff Roberson 		m->valid = VM_PAGE_BITS_ALL;
4750766f278SJonathan T. Looney 		pmap_enter(kernel_pmap, addr + i, m, prot,
4760766f278SJonathan T. Looney 		    prot | PMAP_ENTER_WIRED, 0);
477df8bae1dSRodney W. Grimes 	}
4785df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
479df8bae1dSRodney W. Grimes 
480e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
481df8bae1dSRodney W. Grimes }
482df8bae1dSRodney W. Grimes 
4837a469c8eSJeff Roberson int
4847a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
4857a469c8eSJeff Roberson {
4867a469c8eSJeff Roberson 	struct vm_domainset_iter di;
4877a469c8eSJeff Roberson 	int domain;
4887a469c8eSJeff Roberson 	int ret;
4897a469c8eSJeff Roberson 
4907a469c8eSJeff Roberson 	KASSERT(object == kernel_object,
4917a469c8eSJeff Roberson 	    ("kmem_back: only supports kernel object."));
4927a469c8eSJeff Roberson 
4937a469c8eSJeff Roberson 	vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags);
4947a469c8eSJeff Roberson 	do {
4957a469c8eSJeff Roberson 		ret = kmem_back_domain(domain, object, addr, size, flags);
4967a469c8eSJeff Roberson 		if (ret == KERN_SUCCESS)
4977a469c8eSJeff Roberson 			break;
4987a469c8eSJeff Roberson 	} while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0);
4997a469c8eSJeff Roberson 
5007a469c8eSJeff Roberson 	return (ret);
5017a469c8eSJeff Roberson }
5027a469c8eSJeff Roberson 
5039e829b22SAlan Cox /*
5049e829b22SAlan Cox  *	kmem_unback:
5059e829b22SAlan Cox  *
5069e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
5079e829b22SAlan Cox  *	address range.
5089e829b22SAlan Cox  *
5099e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
5109e829b22SAlan Cox  *	that is being unmapped.
5119e829b22SAlan Cox  */
5127a469c8eSJeff Roberson static int
5137a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5145df87b21SJeff Roberson {
5157e05ffa6SMark Johnston 	vm_page_t m, next;
5167e05ffa6SMark Johnston 	vm_offset_t end, offset;
5177a469c8eSJeff Roberson 	int domain;
5185df87b21SJeff Roberson 
5192e47807cSJeff Roberson 	KASSERT(object == kernel_object,
5202e47807cSJeff Roberson 	    ("kmem_unback: only supports kernel object."));
5215df87b21SJeff Roberson 
5227a469c8eSJeff Roberson 	if (size == 0)
5237a469c8eSJeff Roberson 		return (0);
524fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
5255df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
5267e05ffa6SMark Johnston 	end = offset + size;
5275df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
5287a469c8eSJeff Roberson 	m = vm_page_lookup(object, atop(offset));
529e2068d0bSJeff Roberson 	domain = vm_phys_domain(m);
5307a469c8eSJeff Roberson 	for (; offset < end; offset += PAGE_SIZE, m = next) {
5317e05ffa6SMark Johnston 		next = vm_page_next(m);
53227e9ed8aSAlan Cox 		vm_page_unwire(m, PQ_NONE);
5335df87b21SJeff Roberson 		vm_page_free(m);
5345df87b21SJeff Roberson 	}
5355df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
5367a469c8eSJeff Roberson 
5377a469c8eSJeff Roberson 	return (domain);
5387a469c8eSJeff Roberson }
5397a469c8eSJeff Roberson 
5407a469c8eSJeff Roberson void
5417a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
5427a469c8eSJeff Roberson {
5437a469c8eSJeff Roberson 
5447a469c8eSJeff Roberson 	_kmem_unback(object, addr, size);
5455df87b21SJeff Roberson }
5465df87b21SJeff Roberson 
547df8bae1dSRodney W. Grimes /*
5485df87b21SJeff Roberson  *	kmem_free:
5495df87b21SJeff Roberson  *
5505df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
5515df87b21SJeff Roberson  *	original allocation.
5525df87b21SJeff Roberson  */
5535df87b21SJeff Roberson void
5545df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size)
5555df87b21SJeff Roberson {
5560766f278SJonathan T. Looney 	struct vmem *arena;
5577a469c8eSJeff Roberson 	int domain;
5585df87b21SJeff Roberson 
5590766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0
5600766f278SJonathan T. Looney 	KASSERT(vmem == kernel_arena || vmem == kernel_rwx_arena,
5610766f278SJonathan T. Looney 	    ("kmem_free: Only kernel_arena or kernel_rwx_arena are supported."));
5620766f278SJonathan T. Looney #else
5632e47807cSJeff Roberson 	KASSERT(vmem == kernel_arena,
5642e47807cSJeff Roberson 	    ("kmem_free: Only kernel_arena is supported."));
5650766f278SJonathan T. Looney #endif
5660766f278SJonathan T. Looney 
5675df87b21SJeff Roberson 	size = round_page(size);
5687a469c8eSJeff Roberson 	domain = _kmem_unback(kernel_object, addr, size);
5690766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0
5700766f278SJonathan T. Looney 	if (__predict_true(vmem == kernel_arena))
5710766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_arena;
5720766f278SJonathan T. Looney 	else
5730766f278SJonathan T. Looney 		arena = vm_dom[domain].vmd_kernel_rwx_arena;
5740766f278SJonathan T. Looney #else
5750766f278SJonathan T. Looney 	arena = vm_dom[domain].vmd_kernel_arena;
5760766f278SJonathan T. Looney #endif
5770766f278SJonathan T. Looney 	vmem_free(arena, addr, size);
5785df87b21SJeff Roberson }
5795df87b21SJeff Roberson 
5805df87b21SJeff Roberson /*
5815df87b21SJeff Roberson  *	kmap_alloc_wait:
582df8bae1dSRodney W. Grimes  *
583df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
584df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
585df8bae1dSRodney W. Grimes  *
5861c7c3c6aSMatthew Dillon  *	This routine may block.
587df8bae1dSRodney W. Grimes  */
5880d94caffSDavid Greenman vm_offset_t
5896e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size)
590df8bae1dSRodney W. Grimes {
591df8bae1dSRodney W. Grimes 	vm_offset_t addr;
59223955314SAlfred Perlstein 
593df8bae1dSRodney W. Grimes 	size = round_page(size);
5943364c323SKonstantin Belousov 	if (!swap_reserve(size))
5953364c323SKonstantin Belousov 		return (0);
596df8bae1dSRodney W. Grimes 
597df8bae1dSRodney W. Grimes 	for (;;) {
598df8bae1dSRodney W. Grimes 		/*
5990d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
6000d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
601df8bae1dSRodney W. Grimes 		 */
602df8bae1dSRodney W. Grimes 		vm_map_lock(map);
603e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
604df8bae1dSRodney W. Grimes 			break;
605df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
606df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
607df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
6083364c323SKonstantin Belousov 			swap_release(size);
609df8bae1dSRodney W. Grimes 			return (0);
610df8bae1dSRodney W. Grimes 		}
6119688f931SAlan Cox 		map->needs_wakeup = TRUE;
6128ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
613df8bae1dSRodney W. Grimes 	}
6143364c323SKonstantin Belousov 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
6153364c323SKonstantin Belousov 	    VM_PROT_ALL, MAP_ACC_CHARGED);
616df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
617df8bae1dSRodney W. Grimes 	return (addr);
618df8bae1dSRodney W. Grimes }
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes /*
6215df87b21SJeff Roberson  *	kmap_free_wakeup:
622df8bae1dSRodney W. Grimes  *
62324a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
624df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
625df8bae1dSRodney W. Grimes  */
6260d94caffSDavid Greenman void
6276e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
628df8bae1dSRodney W. Grimes {
62923955314SAlfred Perlstein 
630df8bae1dSRodney W. Grimes 	vm_map_lock(map);
631655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
6329688f931SAlan Cox 	if (map->needs_wakeup) {
6339688f931SAlan Cox 		map->needs_wakeup = FALSE;
6349688f931SAlan Cox 		vm_map_wakeup(map);
6359688f931SAlan Cox 	}
636df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
6395df87b21SJeff Roberson void
64089cb2a19SMatthew D Fleming kmem_init_zero_region(void)
64189cb2a19SMatthew D Fleming {
642cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
64389cb2a19SMatthew D Fleming 	vm_page_t m;
64489cb2a19SMatthew D Fleming 
645cfb00e5aSMatthew D Fleming 	/*
646cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
647cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
648cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
649cfb00e5aSMatthew D Fleming 	 */
6505df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
651703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
65289cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
65389cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
65489cb2a19SMatthew D Fleming 		pmap_zero_page(m);
65589cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
65689cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
6575df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
65889cb2a19SMatthew D Fleming 
65989cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
66089cb2a19SMatthew D Fleming }
66189cb2a19SMatthew D Fleming 
662df8bae1dSRodney W. Grimes /*
6631c7c3c6aSMatthew Dillon  * 	kmem_init:
6641c7c3c6aSMatthew Dillon  *
6651c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
6661c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
6671c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
6681c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
669df8bae1dSRodney W. Grimes  */
6700d94caffSDavid Greenman void
6716e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end)
672df8bae1dSRodney W. Grimes {
673030f2369SAlfred Perlstein 	vm_map_t m;
674df8bae1dSRodney W. Grimes 
6752d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
676c9267356SAlan Cox 	m->system_map = 1;
677df8bae1dSRodney W. Grimes 	vm_map_lock(m);
678df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
679df8bae1dSRodney W. Grimes 	kernel_map = m;
680c9267356SAlan Cox 	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
6815cfa90e9SAlan Cox #ifdef __amd64__
6825cfa90e9SAlan Cox 	    KERNBASE,
6835cfa90e9SAlan Cox #else
6845cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
6855cfa90e9SAlan Cox #endif
6865cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
687df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
688df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
689df8bae1dSRodney W. Grimes }
69086f08737SRobert Watson 
691483f692eSMark Johnston /*
692483f692eSMark Johnston  *	kmem_bootstrap_free:
693483f692eSMark Johnston  *
694483f692eSMark Johnston  *	Free pages backing preloaded data (e.g., kernel modules) to the
695483f692eSMark Johnston  *	system.  Currently only supported on platforms that create a
696483f692eSMark Johnston  *	vm_phys segment for preloaded data.
697483f692eSMark Johnston  */
698483f692eSMark Johnston void
699483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size)
700483f692eSMark Johnston {
701483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__)
702483f692eSMark Johnston 	struct vm_domain *vmd;
703*6c85795aSMark Johnston 	vm_offset_t end, va;
704483f692eSMark Johnston 	vm_paddr_t pa;
705483f692eSMark Johnston 	vm_page_t m;
706483f692eSMark Johnston 
707483f692eSMark Johnston 	end = trunc_page(start + size);
708483f692eSMark Johnston 	start = round_page(start);
709483f692eSMark Johnston 
710*6c85795aSMark Johnston 	for (va = start; va < end; va += PAGE_SIZE) {
711*6c85795aSMark Johnston 		pa = pmap_kextract(va);
712483f692eSMark Johnston 		m = PHYS_TO_VM_PAGE(pa);
713483f692eSMark Johnston 
714483f692eSMark Johnston 		vmd = vm_pagequeue_domain(m);
715483f692eSMark Johnston 		vm_domain_free_lock(vmd);
716483f692eSMark Johnston 		vm_phys_free_pages(m, 0);
717483f692eSMark Johnston 		vm_domain_free_unlock(vmd);
718483f692eSMark Johnston 	}
719*6c85795aSMark Johnston 	pmap_remove(kernel_pmap, start, end);
720*6c85795aSMark Johnston 	(void)vmem_add(kernel_arena, start, end - start, M_WAITOK);
721483f692eSMark Johnston #endif
722483f692eSMark Johnston }
723483f692eSMark Johnston 
7249309e63cSRobert Watson #ifdef DIAGNOSTIC
72586f08737SRobert Watson /*
72686f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
72786f08737SRobert Watson  * purposes.
72886f08737SRobert Watson  */
72986f08737SRobert Watson static int
73086f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
73186f08737SRobert Watson {
73286f08737SRobert Watson 	int error, i;
73386f08737SRobert Watson 
73486f08737SRobert Watson 	i = 0;
73586f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
73686f08737SRobert Watson 	if (error)
73786f08737SRobert Watson 		return (error);
7389b43bc27SAndriy Gapon 	if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0)
7399b43bc27SAndriy Gapon 		return (EINVAL);
7409b43bc27SAndriy Gapon 	if (i != 0)
7419b43bc27SAndriy Gapon 		EVENTHANDLER_INVOKE(vm_lowmem, i);
74286f08737SRobert Watson 	return (0);
74386f08737SRobert Watson }
74486f08737SRobert Watson 
74586f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
7469b43bc27SAndriy Gapon     debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags");
7479309e63cSRobert Watson #endif
748