xref: /freebsd/sys/vm/vm_kern.c (revision 645743ea99829e6d578cca5a580cf0a4102fb7dd)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Kernel memory management.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
7060363fb9SLuigi Rizzo #include <sys/kernel.h>		/* for ticks and hz */
710f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73f23b4c91SGarrett Wollman #include <sys/proc.h>
74a1f6d91cSDavid Greenman #include <sys/malloc.h>
7589f6b863SAttilio Rao #include <sys/rwlock.h>
7686f08737SRobert Watson #include <sys/sysctl.h>
775df87b21SJeff Roberson #include <sys/vmem.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
815df87b21SJeff Roberson #include <vm/vm_kern.h>
82efeaf95aSDavid Greenman #include <vm/pmap.h>
83efeaf95aSDavid Greenman #include <vm/vm_map.h>
84efeaf95aSDavid Greenman #include <vm/vm_object.h>
85df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
879b4288a3SBruce Evans #include <vm/vm_extern.h>
880f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h>
89df8bae1dSRodney W. Grimes 
90e7788a47SKonstantin Belousov vm_map_t kernel_map;
91e7788a47SKonstantin Belousov vm_map_t exec_map;
92cebde069SMike Silbersack vm_map_t pipe_map;
93f23b4c91SGarrett Wollman 
9489cb2a19SMatthew D Fleming const void *zero_region;
9589cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
9689cb2a19SMatthew D Fleming 
97*645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */
98*645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
99*645743eaSJohn Baldwin 
10094bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
101f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
10294bfd5b1SMarius Strobl 
10394bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
104fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__)
10594bfd5b1SMarius Strobl     &vm_max_kernel_address, 0,
10694bfd5b1SMarius Strobl #else
107f0188618SHans Petter Selasky     SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
10894bfd5b1SMarius Strobl #endif
10994bfd5b1SMarius Strobl     "Max kernel address");
11094bfd5b1SMarius Strobl 
111df8bae1dSRodney W. Grimes /*
1125df87b21SJeff Roberson  *	kva_alloc:
113a839bdc8SDmitrij Tejblum  *
114b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
115b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
116b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
117b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
118b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
119a839bdc8SDmitrij Tejblum  */
120a839bdc8SDmitrij Tejblum vm_offset_t
1215df87b21SJeff Roberson kva_alloc(size)
122030f2369SAlfred Perlstein 	vm_size_t size;
123a839bdc8SDmitrij Tejblum {
124a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
125a839bdc8SDmitrij Tejblum 
126a839bdc8SDmitrij Tejblum 	size = round_page(size);
1275df87b21SJeff Roberson 	if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
128a839bdc8SDmitrij Tejblum 		return (0);
1295df87b21SJeff Roberson 
130a839bdc8SDmitrij Tejblum 	return (addr);
131a839bdc8SDmitrij Tejblum }
132a839bdc8SDmitrij Tejblum 
133a839bdc8SDmitrij Tejblum /*
1345df87b21SJeff Roberson  *	kva_free:
135ca596a25SJuli Mallett  *
1365df87b21SJeff Roberson  *	Release a region of kernel virtual memory allocated
1375df87b21SJeff Roberson  *	with kva_alloc, and return the physical pages
1385df87b21SJeff Roberson  *	associated with that region.
1395df87b21SJeff Roberson  *
1405df87b21SJeff Roberson  *	This routine may not block on kernel maps.
141ca596a25SJuli Mallett  */
1425df87b21SJeff Roberson void
1435df87b21SJeff Roberson kva_free(addr, size)
144ca596a25SJuli Mallett 	vm_offset_t addr;
145030f2369SAlfred Perlstein 	vm_size_t size;
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes 	size = round_page(size);
1495df87b21SJeff Roberson 	vmem_free(kernel_arena, addr, size);
150df8bae1dSRodney W. Grimes }
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes /*
1530ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physical pages
1540ff0fc84SAlan Cox  *	within the specified address range to the kernel object.  Creates a
1550ff0fc84SAlan Cox  *	wired mapping from this region to these pages, and returns the
1560ff0fc84SAlan Cox  *	region's starting virtual address.  The allocated pages are not
1570ff0fc84SAlan Cox  *	necessarily physically contiguous.  If M_ZERO is specified through the
1580ff0fc84SAlan Cox  *	given flags, then the pages are zeroed before they are mapped.
1590ff0fc84SAlan Cox  */
1600ff0fc84SAlan Cox vm_offset_t
1615df87b21SJeff Roberson kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low,
1620ff0fc84SAlan Cox     vm_paddr_t high, vm_memattr_t memattr)
1630ff0fc84SAlan Cox {
1645df87b21SJeff Roberson 	vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
1659e829b22SAlan Cox 	vm_offset_t addr, i;
1665df87b21SJeff Roberson 	vm_ooffset_t offset;
1670ff0fc84SAlan Cox 	vm_page_t m;
1680ff0fc84SAlan Cox 	int pflags, tries;
1690ff0fc84SAlan Cox 
1700ff0fc84SAlan Cox 	size = round_page(size);
1715df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
1720ff0fc84SAlan Cox 		return (0);
1730ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
1745df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
17589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1765df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
1770ff0fc84SAlan Cox 		tries = 0;
1780ff0fc84SAlan Cox retry:
1795df87b21SJeff Roberson 		m = vm_page_alloc_contig(object, OFF_TO_IDX(offset + i),
1805df87b21SJeff Roberson 		    pflags, 1, low, high, PAGE_SIZE, 0, memattr);
1810ff0fc84SAlan Cox 		if (m == NULL) {
18289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1830ff0fc84SAlan Cox 			if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
18485eeca35SAlan Cox 				vm_pageout_grow_cache(tries, low, high);
18589f6b863SAttilio Rao 				VM_OBJECT_WLOCK(object);
1860ff0fc84SAlan Cox 				tries++;
1870ff0fc84SAlan Cox 				goto retry;
1880ff0fc84SAlan Cox 			}
1899e829b22SAlan Cox 			kmem_unback(object, addr, i);
1905df87b21SJeff Roberson 			vmem_free(vmem, addr, size);
1910ff0fc84SAlan Cox 			return (0);
1920ff0fc84SAlan Cox 		}
1930ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
1940ff0fc84SAlan Cox 			pmap_zero_page(m);
1950ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
19639ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
19739ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
1980ff0fc84SAlan Cox 	}
19989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2000ff0fc84SAlan Cox 	return (addr);
2010ff0fc84SAlan Cox }
2020ff0fc84SAlan Cox 
2030ff0fc84SAlan Cox /*
2040ff0fc84SAlan Cox  *	Allocates a region from the kernel address map and physically
2050ff0fc84SAlan Cox  *	contiguous pages within the specified address range to the kernel
2060ff0fc84SAlan Cox  *	object.  Creates a wired mapping from this region to these pages, and
2070ff0fc84SAlan Cox  *	returns the region's starting virtual address.  If M_ZERO is specified
2080ff0fc84SAlan Cox  *	through the given flags, then the pages are zeroed before they are
2090ff0fc84SAlan Cox  *	mapped.
2100ff0fc84SAlan Cox  */
2110ff0fc84SAlan Cox vm_offset_t
2125df87b21SJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low,
2130ff0fc84SAlan Cox     vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
2140ff0fc84SAlan Cox     vm_memattr_t memattr)
2150ff0fc84SAlan Cox {
2165df87b21SJeff Roberson 	vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
2175df87b21SJeff Roberson 	vm_offset_t addr, tmp;
2180ff0fc84SAlan Cox 	vm_ooffset_t offset;
2190ff0fc84SAlan Cox 	vm_page_t end_m, m;
2200ff0fc84SAlan Cox 	int pflags, tries;
2210ff0fc84SAlan Cox 
2220ff0fc84SAlan Cox 	size = round_page(size);
2235df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
2240ff0fc84SAlan Cox 		return (0);
2250ff0fc84SAlan Cox 	offset = addr - VM_MIN_KERNEL_ADDRESS;
2265df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
22789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
2280ff0fc84SAlan Cox 	tries = 0;
2290ff0fc84SAlan Cox retry:
2300ff0fc84SAlan Cox 	m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags,
2310ff0fc84SAlan Cox 	    atop(size), low, high, alignment, boundary, memattr);
2320ff0fc84SAlan Cox 	if (m == NULL) {
23389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2340ff0fc84SAlan Cox 		if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
23585eeca35SAlan Cox 			vm_pageout_grow_cache(tries, low, high);
23689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2370ff0fc84SAlan Cox 			tries++;
2380ff0fc84SAlan Cox 			goto retry;
2390ff0fc84SAlan Cox 		}
2405df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
2410ff0fc84SAlan Cox 		return (0);
2420ff0fc84SAlan Cox 	}
2430ff0fc84SAlan Cox 	end_m = m + atop(size);
2445df87b21SJeff Roberson 	tmp = addr;
2450ff0fc84SAlan Cox 	for (; m < end_m; m++) {
2460ff0fc84SAlan Cox 		if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
2470ff0fc84SAlan Cox 			pmap_zero_page(m);
2480ff0fc84SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
24939ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL,
25039ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
2515df87b21SJeff Roberson 		tmp += PAGE_SIZE;
2520ff0fc84SAlan Cox 	}
25389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
2540ff0fc84SAlan Cox 	return (addr);
2550ff0fc84SAlan Cox }
2560ff0fc84SAlan Cox 
2570ff0fc84SAlan Cox /*
258df8bae1dSRodney W. Grimes  *	kmem_suballoc:
259df8bae1dSRodney W. Grimes  *
260df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
261df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
262df8bae1dSRodney W. Grimes  *
263df8bae1dSRodney W. Grimes  *	Arguments are as follows:
264df8bae1dSRodney W. Grimes  *
265df8bae1dSRodney W. Grimes  *	parent		Map to take range from
266df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
267030f2369SAlfred Perlstein  *	size		Size of range to find
2683202ed75SAlan Cox  *	superpage_align	Request that min is superpage aligned
269df8bae1dSRodney W. Grimes  */
2700d94caffSDavid Greenman vm_map_t
2713202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
2723202ed75SAlan Cox     vm_size_t size, boolean_t superpage_align)
273df8bae1dSRodney W. Grimes {
2746e4f51d1SAlfred Perlstein 	int ret;
275df8bae1dSRodney W. Grimes 	vm_map_t result;
27623955314SAlfred Perlstein 
277df8bae1dSRodney W. Grimes 	size = round_page(size);
278df8bae1dSRodney W. Grimes 
2792bc24aa9SAlan Cox 	*min = vm_map_min(parent);
280edb572a3SJohn Baldwin 	ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
2815aa60b6fSJohn Baldwin 	    VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
2823364c323SKonstantin Belousov 	    MAP_ACC_NO_CHARGE);
28324dedba9SAlan Cox 	if (ret != KERN_SUCCESS)
28424dedba9SAlan Cox 		panic("kmem_suballoc: bad status return of %d", ret);
285df8bae1dSRodney W. Grimes 	*max = *min + size;
2862d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
287df8bae1dSRodney W. Grimes 	if (result == NULL)
288df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
2896e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
290df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
291df8bae1dSRodney W. Grimes 	return (result);
292df8bae1dSRodney W. Grimes }
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes /*
2951c7c3c6aSMatthew Dillon  *	kmem_malloc:
2961c7c3c6aSMatthew Dillon  *
2975df87b21SJeff Roberson  *	Allocate wired-down pages in the kernel's address space.
298df8bae1dSRodney W. Grimes  */
299df8bae1dSRodney W. Grimes vm_offset_t
3005df87b21SJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags)
301df8bae1dSRodney W. Grimes {
302df8bae1dSRodney W. Grimes 	vm_offset_t addr;
3035df87b21SJeff Roberson 	int rv;
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	size = round_page(size);
3065df87b21SJeff Roberson 	if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
3075df87b21SJeff Roberson 		return (0);
308df8bae1dSRodney W. Grimes 
3095df87b21SJeff Roberson 	rv = kmem_back((vmem == kmem_arena) ? kmem_object : kernel_object,
3105df87b21SJeff Roberson 	    addr, size, flags);
3115df87b21SJeff Roberson 	if (rv != KERN_SUCCESS) {
3125df87b21SJeff Roberson 		vmem_free(vmem, addr, size);
313f31c239dSAlan Cox 		return (0);
314df8bae1dSRodney W. Grimes 	}
3155df87b21SJeff Roberson 	return (addr);
316e3813573SMatthew D Fleming }
317e3813573SMatthew D Fleming 
318e3813573SMatthew D Fleming /*
319e3813573SMatthew D Fleming  *	kmem_back:
320e3813573SMatthew D Fleming  *
321e3813573SMatthew D Fleming  *	Allocate physical pages for the specified virtual address range.
322e3813573SMatthew D Fleming  */
323e3813573SMatthew D Fleming int
3245df87b21SJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
325e3813573SMatthew D Fleming {
326e3813573SMatthew D Fleming 	vm_offset_t offset, i;
327e3813573SMatthew D Fleming 	vm_page_t m;
328e3813573SMatthew D Fleming 	int pflags;
329e3813573SMatthew D Fleming 
3305df87b21SJeff Roberson 	KASSERT(object == kmem_object || object == kernel_object,
3315df87b21SJeff Roberson 	    ("kmem_back: only supports kernel objects."));
3325df87b21SJeff Roberson 
3330891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
3345df87b21SJeff Roberson 	pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
335df8bae1dSRodney W. Grimes 
3365df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
3371e081f88SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
3381e081f88SJeff Roberson retry:
3395df87b21SJeff Roberson 		m = vm_page_alloc(object, OFF_TO_IDX(offset + i), pflags);
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes 		/*
3420d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
3430d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
3440d94caffSDavid Greenman 		 * aren't on any queues.
345df8bae1dSRodney W. Grimes 		 */
346df8bae1dSRodney W. Grimes 		if (m == NULL) {
3475df87b21SJeff Roberson 			VM_OBJECT_WUNLOCK(object);
3489e829b22SAlan Cox 			if ((flags & M_NOWAIT) == 0) {
349b18bfc3dSJohn Dyson 				VM_WAIT;
3505df87b21SJeff Roberson 				VM_OBJECT_WLOCK(object);
351b18bfc3dSJohn Dyson 				goto retry;
352b18bfc3dSJohn Dyson 			}
3539e829b22SAlan Cox 			kmem_unback(object, addr, i);
354e3813573SMatthew D Fleming 			return (KERN_NO_SPACE);
355df8bae1dSRodney W. Grimes 		}
3561e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
357fff6062aSAlan Cox 			pmap_zero_page(m);
358d98d0ce2SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) != 0,
3599f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
3605df87b21SJeff Roberson 		m->valid = VM_PAGE_BITS_ALL;
36139ffa8c1SKonstantin Belousov 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
36239ffa8c1SKonstantin Belousov 		    VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
363df8bae1dSRodney W. Grimes 	}
3645df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
365df8bae1dSRodney W. Grimes 
366e3813573SMatthew D Fleming 	return (KERN_SUCCESS);
367df8bae1dSRodney W. Grimes }
368df8bae1dSRodney W. Grimes 
3699e829b22SAlan Cox /*
3709e829b22SAlan Cox  *	kmem_unback:
3719e829b22SAlan Cox  *
3729e829b22SAlan Cox  *	Unmap and free the physical pages underlying the specified virtual
3739e829b22SAlan Cox  *	address range.
3749e829b22SAlan Cox  *
3759e829b22SAlan Cox  *	A physical page must exist within the specified object at each index
3769e829b22SAlan Cox  *	that is being unmapped.
3779e829b22SAlan Cox  */
3785df87b21SJeff Roberson void
3795df87b21SJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
3805df87b21SJeff Roberson {
3815df87b21SJeff Roberson 	vm_page_t m;
3825cd7a4f7SAlan Cox 	vm_offset_t i, offset;
3835df87b21SJeff Roberson 
3845df87b21SJeff Roberson 	KASSERT(object == kmem_object || object == kernel_object,
3855df87b21SJeff Roberson 	    ("kmem_unback: only supports kernel objects."));
3865df87b21SJeff Roberson 
387fa2f411cSAlan Cox 	pmap_remove(kernel_pmap, addr, addr + size);
3885df87b21SJeff Roberson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
3895df87b21SJeff Roberson 	VM_OBJECT_WLOCK(object);
3905df87b21SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
3915df87b21SJeff Roberson 		m = vm_page_lookup(object, OFF_TO_IDX(offset + i));
39227e9ed8aSAlan Cox 		vm_page_unwire(m, PQ_NONE);
3935df87b21SJeff Roberson 		vm_page_free(m);
3945df87b21SJeff Roberson 	}
3955df87b21SJeff Roberson 	VM_OBJECT_WUNLOCK(object);
3965df87b21SJeff Roberson }
3975df87b21SJeff Roberson 
398df8bae1dSRodney W. Grimes /*
3995df87b21SJeff Roberson  *	kmem_free:
4005df87b21SJeff Roberson  *
4015df87b21SJeff Roberson  *	Free memory allocated with kmem_malloc.  The size must match the
4025df87b21SJeff Roberson  *	original allocation.
4035df87b21SJeff Roberson  */
4045df87b21SJeff Roberson void
4055df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size)
4065df87b21SJeff Roberson {
4075df87b21SJeff Roberson 
4085df87b21SJeff Roberson 	size = round_page(size);
4095df87b21SJeff Roberson 	kmem_unback((vmem == kmem_arena) ? kmem_object : kernel_object,
4105df87b21SJeff Roberson 	    addr, size);
4115df87b21SJeff Roberson 	vmem_free(vmem, addr, size);
4125df87b21SJeff Roberson }
4135df87b21SJeff Roberson 
4145df87b21SJeff Roberson /*
4155df87b21SJeff Roberson  *	kmap_alloc_wait:
416df8bae1dSRodney W. Grimes  *
417df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
418df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
419df8bae1dSRodney W. Grimes  *
4201c7c3c6aSMatthew Dillon  *	This routine may block.
421df8bae1dSRodney W. Grimes  */
4220d94caffSDavid Greenman vm_offset_t
4235df87b21SJeff Roberson kmap_alloc_wait(map, size)
424df8bae1dSRodney W. Grimes 	vm_map_t map;
425df8bae1dSRodney W. Grimes 	vm_size_t size;
426df8bae1dSRodney W. Grimes {
427df8bae1dSRodney W. Grimes 	vm_offset_t addr;
42823955314SAlfred Perlstein 
429df8bae1dSRodney W. Grimes 	size = round_page(size);
4303364c323SKonstantin Belousov 	if (!swap_reserve(size))
4313364c323SKonstantin Belousov 		return (0);
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes 	for (;;) {
434df8bae1dSRodney W. Grimes 		/*
4350d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
4360d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
437df8bae1dSRodney W. Grimes 		 */
438df8bae1dSRodney W. Grimes 		vm_map_lock(map);
439e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
440df8bae1dSRodney W. Grimes 			break;
441df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
442df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
443df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
4443364c323SKonstantin Belousov 			swap_release(size);
445df8bae1dSRodney W. Grimes 			return (0);
446df8bae1dSRodney W. Grimes 		}
4479688f931SAlan Cox 		map->needs_wakeup = TRUE;
4488ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
449df8bae1dSRodney W. Grimes 	}
4503364c323SKonstantin Belousov 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
4513364c323SKonstantin Belousov 	    VM_PROT_ALL, MAP_ACC_CHARGED);
452df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
453df8bae1dSRodney W. Grimes 	return (addr);
454df8bae1dSRodney W. Grimes }
455df8bae1dSRodney W. Grimes 
456df8bae1dSRodney W. Grimes /*
4575df87b21SJeff Roberson  *	kmap_free_wakeup:
458df8bae1dSRodney W. Grimes  *
45924a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
460df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
461df8bae1dSRodney W. Grimes  */
4620d94caffSDavid Greenman void
4635df87b21SJeff Roberson kmap_free_wakeup(map, addr, size)
464df8bae1dSRodney W. Grimes 	vm_map_t map;
465df8bae1dSRodney W. Grimes 	vm_offset_t addr;
466df8bae1dSRodney W. Grimes 	vm_size_t size;
467df8bae1dSRodney W. Grimes {
46823955314SAlfred Perlstein 
469df8bae1dSRodney W. Grimes 	vm_map_lock(map);
470655c3490SKonstantin Belousov 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
4719688f931SAlan Cox 	if (map->needs_wakeup) {
4729688f931SAlan Cox 		map->needs_wakeup = FALSE;
4739688f931SAlan Cox 		vm_map_wakeup(map);
4749688f931SAlan Cox 	}
475df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
476df8bae1dSRodney W. Grimes }
477df8bae1dSRodney W. Grimes 
4785df87b21SJeff Roberson void
47989cb2a19SMatthew D Fleming kmem_init_zero_region(void)
48089cb2a19SMatthew D Fleming {
481cfb00e5aSMatthew D Fleming 	vm_offset_t addr, i;
48289cb2a19SMatthew D Fleming 	vm_page_t m;
48389cb2a19SMatthew D Fleming 
484cfb00e5aSMatthew D Fleming 	/*
485cfb00e5aSMatthew D Fleming 	 * Map a single physical page of zeros to a larger virtual range.
486cfb00e5aSMatthew D Fleming 	 * This requires less looping in places that want large amounts of
487cfb00e5aSMatthew D Fleming 	 * zeros, while not using much more physical resources.
488cfb00e5aSMatthew D Fleming 	 */
4895df87b21SJeff Roberson 	addr = kva_alloc(ZERO_REGION_SIZE);
490703dec68SAlan Cox 	m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
49189cb2a19SMatthew D Fleming 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
49289cb2a19SMatthew D Fleming 	if ((m->flags & PG_ZERO) == 0)
49389cb2a19SMatthew D Fleming 		pmap_zero_page(m);
49489cb2a19SMatthew D Fleming 	for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
49589cb2a19SMatthew D Fleming 		pmap_qenter(addr + i, &m, 1);
4965df87b21SJeff Roberson 	pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
49789cb2a19SMatthew D Fleming 
49889cb2a19SMatthew D Fleming 	zero_region = (const void *)addr;
49989cb2a19SMatthew D Fleming }
50089cb2a19SMatthew D Fleming 
501df8bae1dSRodney W. Grimes /*
5021c7c3c6aSMatthew Dillon  * 	kmem_init:
5031c7c3c6aSMatthew Dillon  *
5041c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
5051c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
5061c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
5071c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
508df8bae1dSRodney W. Grimes  */
5090d94caffSDavid Greenman void
5100d94caffSDavid Greenman kmem_init(start, end)
511df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
512df8bae1dSRodney W. Grimes {
513030f2369SAlfred Perlstein 	vm_map_t m;
514df8bae1dSRodney W. Grimes 
5152d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
516c9267356SAlan Cox 	m->system_map = 1;
517df8bae1dSRodney W. Grimes 	vm_map_lock(m);
518df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
519df8bae1dSRodney W. Grimes 	kernel_map = m;
520c9267356SAlan Cox 	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
5215cfa90e9SAlan Cox #ifdef __amd64__
5225cfa90e9SAlan Cox 	    KERNBASE,
5235cfa90e9SAlan Cox #else
5245cfa90e9SAlan Cox 	    VM_MIN_KERNEL_ADDRESS,
5255cfa90e9SAlan Cox #endif
5265cfa90e9SAlan Cox 	    start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
527df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
528df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
529df8bae1dSRodney W. Grimes }
53086f08737SRobert Watson 
5319309e63cSRobert Watson #ifdef DIAGNOSTIC
53286f08737SRobert Watson /*
53386f08737SRobert Watson  * Allow userspace to directly trigger the VM drain routine for testing
53486f08737SRobert Watson  * purposes.
53586f08737SRobert Watson  */
53686f08737SRobert Watson static int
53786f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
53886f08737SRobert Watson {
53986f08737SRobert Watson 	int error, i;
54086f08737SRobert Watson 
54186f08737SRobert Watson 	i = 0;
54286f08737SRobert Watson 	error = sysctl_handle_int(oidp, &i, 0, req);
54386f08737SRobert Watson 	if (error)
54486f08737SRobert Watson 		return (error);
54586f08737SRobert Watson 	if (i)
54686f08737SRobert Watson 		EVENTHANDLER_INVOKE(vm_lowmem, 0);
54786f08737SRobert Watson 	return (0);
54886f08737SRobert Watson }
54986f08737SRobert Watson 
55086f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
55186f08737SRobert Watson     debug_vm_lowmem, "I", "set to trigger vm_lowmem event");
5529309e63cSRobert Watson #endif
553