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 223db7c2a48SAlan Cox kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high, 224db7c2a48SAlan Cox 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 vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 2317a469c8eSJeff Roberson do { 2327a469c8eSJeff Roberson addr = kmem_alloc_attr_domain(domain, size, flags, low, high, 2337a469c8eSJeff Roberson memattr); 2347a469c8eSJeff Roberson if (addr != 0) 2357a469c8eSJeff Roberson break; 2367a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 2377a469c8eSJeff Roberson 2387a469c8eSJeff Roberson return (addr); 2397a469c8eSJeff Roberson } 2407a469c8eSJeff Roberson 2410ff0fc84SAlan Cox /* 2420ff0fc84SAlan Cox * Allocates a region from the kernel address map and physically 2430ff0fc84SAlan Cox * contiguous pages within the specified address range to the kernel 2440ff0fc84SAlan Cox * object. Creates a wired mapping from this region to these pages, and 2450ff0fc84SAlan Cox * returns the region's starting virtual address. If M_ZERO is specified 2460ff0fc84SAlan Cox * through the given flags, then the pages are zeroed before they are 2470ff0fc84SAlan Cox * mapped. 2480ff0fc84SAlan Cox */ 2490ff0fc84SAlan Cox vm_offset_t 2507a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low, 2510ff0fc84SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 2520ff0fc84SAlan Cox vm_memattr_t memattr) 2530ff0fc84SAlan Cox { 2547a469c8eSJeff Roberson vmem_t *vmem; 2552e47807cSJeff Roberson vm_object_t object = kernel_object; 256d1780e8dSKonstantin Belousov vm_offset_t addr, offset, tmp; 2570ff0fc84SAlan Cox vm_page_t end_m, m; 258c869e672SAlan Cox u_long npages; 2590ff0fc84SAlan Cox int pflags, tries; 2600ff0fc84SAlan Cox 2610ff0fc84SAlan Cox size = round_page(size); 2627a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 2635df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 2640ff0fc84SAlan Cox return (0); 2650ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 2665df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 2678d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 2688d6fbbb8SJeff Roberson pflags |= VM_ALLOC_NOWAIT; 269c869e672SAlan Cox npages = atop(size); 27089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2710ff0fc84SAlan Cox tries = 0; 2720ff0fc84SAlan Cox retry: 2737a469c8eSJeff Roberson m = vm_page_alloc_contig_domain(object, atop(offset), domain, pflags, 274c869e672SAlan Cox npages, low, high, alignment, boundary, memattr); 2750ff0fc84SAlan Cox if (m == NULL) { 27689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2770ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 2787a469c8eSJeff Roberson if (!vm_page_reclaim_contig_domain(domain, pflags, 2797a469c8eSJeff Roberson npages, low, high, alignment, boundary) && 2807a469c8eSJeff Roberson (flags & M_WAITOK) != 0) 281e2068d0bSJeff Roberson vm_wait_domain(domain); 28289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2830ff0fc84SAlan Cox tries++; 2840ff0fc84SAlan Cox goto retry; 2850ff0fc84SAlan Cox } 2865df87b21SJeff Roberson vmem_free(vmem, addr, size); 2870ff0fc84SAlan Cox return (0); 2880ff0fc84SAlan Cox } 289e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 2907a469c8eSJeff Roberson ("kmem_alloc_contig_domain: Domain mismatch %d != %d", 291e2068d0bSJeff Roberson vm_phys_domain(m), domain)); 292c869e672SAlan Cox end_m = m + npages; 2935df87b21SJeff Roberson tmp = addr; 2940ff0fc84SAlan Cox for (; m < end_m; m++) { 2950ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2960ff0fc84SAlan Cox pmap_zero_page(m); 2970ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 2980766f278SJonathan T. Looney pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW, 2990766f278SJonathan T. Looney VM_PROT_RW | PMAP_ENTER_WIRED, 0); 3005df87b21SJeff Roberson tmp += PAGE_SIZE; 3010ff0fc84SAlan Cox } 30289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3030ff0fc84SAlan Cox return (addr); 3040ff0fc84SAlan Cox } 3050ff0fc84SAlan Cox 3067a469c8eSJeff Roberson vm_offset_t 307*44d0efb2SAlan Cox kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high, 308*44d0efb2SAlan Cox u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr) 3097a469c8eSJeff Roberson { 3107a469c8eSJeff Roberson struct vm_domainset_iter di; 3117a469c8eSJeff Roberson vm_offset_t addr; 3127a469c8eSJeff Roberson int domain; 3137a469c8eSJeff Roberson 3147a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 3157a469c8eSJeff Roberson do { 3167a469c8eSJeff Roberson addr = kmem_alloc_contig_domain(domain, size, flags, low, high, 3177a469c8eSJeff Roberson alignment, boundary, memattr); 3187a469c8eSJeff Roberson if (addr != 0) 3197a469c8eSJeff Roberson break; 3207a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 3217a469c8eSJeff Roberson 3227a469c8eSJeff Roberson return (addr); 3237a469c8eSJeff Roberson } 3247a469c8eSJeff Roberson 3250ff0fc84SAlan Cox /* 326df8bae1dSRodney W. Grimes * kmem_suballoc: 327df8bae1dSRodney W. Grimes * 328df8bae1dSRodney W. Grimes * Allocates a map to manage a subrange 329df8bae1dSRodney W. Grimes * of the kernel virtual address space. 330df8bae1dSRodney W. Grimes * 331df8bae1dSRodney W. Grimes * Arguments are as follows: 332df8bae1dSRodney W. Grimes * 333df8bae1dSRodney W. Grimes * parent Map to take range from 334df8bae1dSRodney W. Grimes * min, max Returned endpoints of map 335030f2369SAlfred Perlstein * size Size of range to find 3363202ed75SAlan Cox * superpage_align Request that min is superpage aligned 337df8bae1dSRodney W. Grimes */ 3380d94caffSDavid Greenman vm_map_t 3393202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max, 3403202ed75SAlan Cox vm_size_t size, boolean_t superpage_align) 341df8bae1dSRodney W. Grimes { 3426e4f51d1SAlfred Perlstein int ret; 343df8bae1dSRodney W. Grimes vm_map_t result; 34423955314SAlfred Perlstein 345df8bae1dSRodney W. Grimes size = round_page(size); 346df8bae1dSRodney W. Grimes 3472bc24aa9SAlan Cox *min = vm_map_min(parent); 348edb572a3SJohn Baldwin ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ? 3495aa60b6fSJohn Baldwin VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 3503364c323SKonstantin Belousov MAP_ACC_NO_CHARGE); 35124dedba9SAlan Cox if (ret != KERN_SUCCESS) 35224dedba9SAlan Cox panic("kmem_suballoc: bad status return of %d", ret); 353df8bae1dSRodney W. Grimes *max = *min + size; 3542d8acc0fSJohn Dyson result = vm_map_create(vm_map_pmap(parent), *min, *max); 355df8bae1dSRodney W. Grimes if (result == NULL) 356df8bae1dSRodney W. Grimes panic("kmem_suballoc: cannot create submap"); 3576e4f51d1SAlfred Perlstein if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS) 358df8bae1dSRodney W. Grimes panic("kmem_suballoc: unable to change range to submap"); 359df8bae1dSRodney W. Grimes return (result); 360df8bae1dSRodney W. Grimes } 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes /* 3631c7c3c6aSMatthew Dillon * kmem_malloc: 3641c7c3c6aSMatthew Dillon * 3655df87b21SJeff Roberson * Allocate wired-down pages in the kernel's address space. 366df8bae1dSRodney W. Grimes */ 367df8bae1dSRodney W. Grimes vm_offset_t 368067fd858SAlan Cox kmem_malloc_domain(int domain, vm_size_t size, int flags) 369df8bae1dSRodney W. Grimes { 3700766f278SJonathan T. Looney vmem_t *arena; 371df8bae1dSRodney W. Grimes vm_offset_t addr; 3725df87b21SJeff Roberson int rv; 373df8bae1dSRodney W. Grimes 3740766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0 375067fd858SAlan Cox if (__predict_true((flags & M_EXEC) == 0)) 3760766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 3770766f278SJonathan T. Looney else 3780766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_rwx_arena; 3790766f278SJonathan T. Looney #else 3800766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 3810766f278SJonathan T. Looney #endif 382df8bae1dSRodney W. Grimes size = round_page(size); 3830766f278SJonathan T. Looney if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr)) 3845df87b21SJeff Roberson return (0); 385df8bae1dSRodney W. Grimes 3867a469c8eSJeff Roberson rv = kmem_back_domain(domain, kernel_object, addr, size, flags); 3875df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 3880766f278SJonathan T. Looney vmem_free(arena, addr, size); 389f31c239dSAlan Cox return (0); 390df8bae1dSRodney W. Grimes } 3915df87b21SJeff Roberson return (addr); 392e3813573SMatthew D Fleming } 393e3813573SMatthew D Fleming 3947a469c8eSJeff Roberson vm_offset_t 395067fd858SAlan Cox kmem_malloc(struct vmem *vmem __unused, vm_size_t size, int flags) 3967a469c8eSJeff Roberson { 3977a469c8eSJeff Roberson struct vm_domainset_iter di; 3987a469c8eSJeff Roberson vm_offset_t addr; 3997a469c8eSJeff Roberson int domain; 4007a469c8eSJeff Roberson 4017a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 4027a469c8eSJeff Roberson do { 403067fd858SAlan Cox addr = kmem_malloc_domain(domain, size, flags); 4047a469c8eSJeff Roberson if (addr != 0) 4057a469c8eSJeff Roberson break; 4067a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 4077a469c8eSJeff Roberson 4087a469c8eSJeff Roberson return (addr); 4097a469c8eSJeff Roberson } 4107a469c8eSJeff Roberson 411e3813573SMatthew D Fleming /* 412e3813573SMatthew D Fleming * kmem_back: 413e3813573SMatthew D Fleming * 414e3813573SMatthew D Fleming * Allocate physical pages for the specified virtual address range. 415e3813573SMatthew D Fleming */ 416e3813573SMatthew D Fleming int 4177a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr, 4187a469c8eSJeff Roberson vm_size_t size, int flags) 419e3813573SMatthew D Fleming { 420e3813573SMatthew D Fleming vm_offset_t offset, i; 42133fff5d5SMark Johnston vm_page_t m, mpred; 4220766f278SJonathan T. Looney vm_prot_t prot; 423e3813573SMatthew D Fleming int pflags; 424e3813573SMatthew D Fleming 4252e47807cSJeff Roberson KASSERT(object == kernel_object, 4267a469c8eSJeff Roberson ("kmem_back_domain: only supports kernel object.")); 4275df87b21SJeff Roberson 4280891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 4295df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 4308d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 4318d6fbbb8SJeff Roberson if (flags & M_WAITOK) 4328d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITFAIL; 4330766f278SJonathan T. Looney prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW; 434df8bae1dSRodney W. Grimes 43533fff5d5SMark Johnston i = 0; 43633fff5d5SMark Johnston VM_OBJECT_WLOCK(object); 4378d6fbbb8SJeff Roberson retry: 43833fff5d5SMark Johnston mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i)); 43933fff5d5SMark Johnston for (; i < size; i += PAGE_SIZE, mpred = m) { 4407a469c8eSJeff Roberson m = vm_page_alloc_domain_after(object, atop(offset + i), 4417a469c8eSJeff Roberson domain, pflags, mpred); 442df8bae1dSRodney W. Grimes 443df8bae1dSRodney W. Grimes /* 4440d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 4450d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 4460d94caffSDavid Greenman * aren't on any queues. 447df8bae1dSRodney W. Grimes */ 448df8bae1dSRodney W. Grimes if (m == NULL) { 4498d6fbbb8SJeff Roberson if ((flags & M_NOWAIT) == 0) 450b18bfc3dSJohn Dyson goto retry; 4518d6fbbb8SJeff Roberson VM_OBJECT_WUNLOCK(object); 4529e829b22SAlan Cox kmem_unback(object, addr, i); 453e3813573SMatthew D Fleming return (KERN_NO_SPACE); 454df8bae1dSRodney W. Grimes } 455e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 4567a469c8eSJeff Roberson ("kmem_back_domain: Domain mismatch %d != %d", 457e2068d0bSJeff Roberson vm_phys_domain(m), domain)); 4581e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 459fff6062aSAlan Cox pmap_zero_page(m); 460d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) != 0, 4619f5c801bSAlan Cox ("kmem_malloc: page %p is managed", m)); 4625df87b21SJeff Roberson m->valid = VM_PAGE_BITS_ALL; 4630766f278SJonathan T. Looney pmap_enter(kernel_pmap, addr + i, m, prot, 4640766f278SJonathan T. Looney prot | PMAP_ENTER_WIRED, 0); 465df8bae1dSRodney W. Grimes } 4665df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 467df8bae1dSRodney W. Grimes 468e3813573SMatthew D Fleming return (KERN_SUCCESS); 469df8bae1dSRodney W. Grimes } 470df8bae1dSRodney W. Grimes 4717a469c8eSJeff Roberson int 4727a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 4737a469c8eSJeff Roberson { 4747a469c8eSJeff Roberson struct vm_domainset_iter di; 4757a469c8eSJeff Roberson int domain; 4767a469c8eSJeff Roberson int ret; 4777a469c8eSJeff Roberson 4787a469c8eSJeff Roberson KASSERT(object == kernel_object, 4797a469c8eSJeff Roberson ("kmem_back: only supports kernel object.")); 4807a469c8eSJeff Roberson 4817a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 4827a469c8eSJeff Roberson do { 4837a469c8eSJeff Roberson ret = kmem_back_domain(domain, object, addr, size, flags); 4847a469c8eSJeff Roberson if (ret == KERN_SUCCESS) 4857a469c8eSJeff Roberson break; 4867a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 4877a469c8eSJeff Roberson 4887a469c8eSJeff Roberson return (ret); 4897a469c8eSJeff Roberson } 4907a469c8eSJeff Roberson 4919e829b22SAlan Cox /* 4929e829b22SAlan Cox * kmem_unback: 4939e829b22SAlan Cox * 4949e829b22SAlan Cox * Unmap and free the physical pages underlying the specified virtual 4959e829b22SAlan Cox * address range. 4969e829b22SAlan Cox * 4979e829b22SAlan Cox * A physical page must exist within the specified object at each index 4989e829b22SAlan Cox * that is being unmapped. 4999e829b22SAlan Cox */ 5007a469c8eSJeff Roberson static int 5017a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5025df87b21SJeff Roberson { 5037e05ffa6SMark Johnston vm_page_t m, next; 5047e05ffa6SMark Johnston vm_offset_t end, offset; 5057a469c8eSJeff Roberson int domain; 5065df87b21SJeff Roberson 5072e47807cSJeff Roberson KASSERT(object == kernel_object, 5082e47807cSJeff Roberson ("kmem_unback: only supports kernel object.")); 5095df87b21SJeff Roberson 5107a469c8eSJeff Roberson if (size == 0) 5117a469c8eSJeff Roberson return (0); 512fa2f411cSAlan Cox pmap_remove(kernel_pmap, addr, addr + size); 5135df87b21SJeff Roberson offset = addr - VM_MIN_KERNEL_ADDRESS; 5147e05ffa6SMark Johnston end = offset + size; 5155df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 5167a469c8eSJeff Roberson m = vm_page_lookup(object, atop(offset)); 517e2068d0bSJeff Roberson domain = vm_phys_domain(m); 5187a469c8eSJeff Roberson for (; offset < end; offset += PAGE_SIZE, m = next) { 5197e05ffa6SMark Johnston next = vm_page_next(m); 52027e9ed8aSAlan Cox vm_page_unwire(m, PQ_NONE); 5215df87b21SJeff Roberson vm_page_free(m); 5225df87b21SJeff Roberson } 5235df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 5247a469c8eSJeff Roberson 5257a469c8eSJeff Roberson return (domain); 5267a469c8eSJeff Roberson } 5277a469c8eSJeff Roberson 5287a469c8eSJeff Roberson void 5297a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5307a469c8eSJeff Roberson { 5317a469c8eSJeff Roberson 5327a469c8eSJeff Roberson _kmem_unback(object, addr, size); 5335df87b21SJeff Roberson } 5345df87b21SJeff Roberson 535df8bae1dSRodney W. Grimes /* 5365df87b21SJeff Roberson * kmem_free: 5375df87b21SJeff Roberson * 5385df87b21SJeff Roberson * Free memory allocated with kmem_malloc. The size must match the 5395df87b21SJeff Roberson * original allocation. 5405df87b21SJeff Roberson */ 5415df87b21SJeff Roberson void 5425df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size) 5435df87b21SJeff Roberson { 5440766f278SJonathan T. Looney struct vmem *arena; 5457a469c8eSJeff Roberson int domain; 5465df87b21SJeff Roberson 5470766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0 5480766f278SJonathan T. Looney KASSERT(vmem == kernel_arena || vmem == kernel_rwx_arena, 5490766f278SJonathan T. Looney ("kmem_free: Only kernel_arena or kernel_rwx_arena are supported.")); 5500766f278SJonathan T. Looney #else 5512e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 5522e47807cSJeff Roberson ("kmem_free: Only kernel_arena is supported.")); 5530766f278SJonathan T. Looney #endif 5540766f278SJonathan T. Looney 5555df87b21SJeff Roberson size = round_page(size); 5567a469c8eSJeff Roberson domain = _kmem_unback(kernel_object, addr, size); 5570766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0 5580766f278SJonathan T. Looney if (__predict_true(vmem == kernel_arena)) 5590766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 5600766f278SJonathan T. Looney else 5610766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_rwx_arena; 5620766f278SJonathan T. Looney #else 5630766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 5640766f278SJonathan T. Looney #endif 5650766f278SJonathan T. Looney vmem_free(arena, addr, size); 5665df87b21SJeff Roberson } 5675df87b21SJeff Roberson 5685df87b21SJeff Roberson /* 5695df87b21SJeff Roberson * kmap_alloc_wait: 570df8bae1dSRodney W. Grimes * 571df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 572df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 573df8bae1dSRodney W. Grimes * 5741c7c3c6aSMatthew Dillon * This routine may block. 575df8bae1dSRodney W. Grimes */ 5760d94caffSDavid Greenman vm_offset_t 5776e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size) 578df8bae1dSRodney W. Grimes { 579df8bae1dSRodney W. Grimes vm_offset_t addr; 58023955314SAlfred Perlstein 581df8bae1dSRodney W. Grimes size = round_page(size); 5823364c323SKonstantin Belousov if (!swap_reserve(size)) 5833364c323SKonstantin Belousov return (0); 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes for (;;) { 586df8bae1dSRodney W. Grimes /* 5870d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 5880d94caffSDavid Greenman * to lock out sleepers/wakers. 589df8bae1dSRodney W. Grimes */ 590df8bae1dSRodney W. Grimes vm_map_lock(map); 591e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 592df8bae1dSRodney W. Grimes break; 593df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 594df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 595df8bae1dSRodney W. Grimes vm_map_unlock(map); 5963364c323SKonstantin Belousov swap_release(size); 597df8bae1dSRodney W. Grimes return (0); 598df8bae1dSRodney W. Grimes } 5999688f931SAlan Cox map->needs_wakeup = TRUE; 6008ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 601df8bae1dSRodney W. Grimes } 6023364c323SKonstantin Belousov vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, 6033364c323SKonstantin Belousov VM_PROT_ALL, MAP_ACC_CHARGED); 604df8bae1dSRodney W. Grimes vm_map_unlock(map); 605df8bae1dSRodney W. Grimes return (addr); 606df8bae1dSRodney W. Grimes } 607df8bae1dSRodney W. Grimes 608df8bae1dSRodney W. Grimes /* 6095df87b21SJeff Roberson * kmap_free_wakeup: 610df8bae1dSRodney W. Grimes * 61124a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 612df8bae1dSRodney W. Grimes * waiting for memory in that map. 613df8bae1dSRodney W. Grimes */ 6140d94caffSDavid Greenman void 6156e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 616df8bae1dSRodney W. Grimes { 61723955314SAlfred Perlstein 618df8bae1dSRodney W. Grimes vm_map_lock(map); 619655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 6209688f931SAlan Cox if (map->needs_wakeup) { 6219688f931SAlan Cox map->needs_wakeup = FALSE; 6229688f931SAlan Cox vm_map_wakeup(map); 6239688f931SAlan Cox } 624df8bae1dSRodney W. Grimes vm_map_unlock(map); 625df8bae1dSRodney W. Grimes } 626df8bae1dSRodney W. Grimes 6275df87b21SJeff Roberson void 62889cb2a19SMatthew D Fleming kmem_init_zero_region(void) 62989cb2a19SMatthew D Fleming { 630cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 63189cb2a19SMatthew D Fleming vm_page_t m; 63289cb2a19SMatthew D Fleming 633cfb00e5aSMatthew D Fleming /* 634cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 635cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 636cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 637cfb00e5aSMatthew D Fleming */ 6385df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 639703dec68SAlan Cox m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | 64089cb2a19SMatthew D Fleming VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO); 64189cb2a19SMatthew D Fleming if ((m->flags & PG_ZERO) == 0) 64289cb2a19SMatthew D Fleming pmap_zero_page(m); 64389cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 64489cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 6455df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 64689cb2a19SMatthew D Fleming 64789cb2a19SMatthew D Fleming zero_region = (const void *)addr; 64889cb2a19SMatthew D Fleming } 64989cb2a19SMatthew D Fleming 650df8bae1dSRodney W. Grimes /* 6511c7c3c6aSMatthew Dillon * kmem_init: 6521c7c3c6aSMatthew Dillon * 6531c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 6541c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 6551c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 6561c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 657df8bae1dSRodney W. Grimes */ 6580d94caffSDavid Greenman void 6596e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end) 660df8bae1dSRodney W. Grimes { 661030f2369SAlfred Perlstein vm_map_t m; 662df8bae1dSRodney W. Grimes 6632d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 664c9267356SAlan Cox m->system_map = 1; 665df8bae1dSRodney W. Grimes vm_map_lock(m); 666df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 667df8bae1dSRodney W. Grimes kernel_map = m; 668c9267356SAlan Cox (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 6695cfa90e9SAlan Cox #ifdef __amd64__ 6705cfa90e9SAlan Cox KERNBASE, 6715cfa90e9SAlan Cox #else 6725cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 6735cfa90e9SAlan Cox #endif 6745cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 675df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 676df8bae1dSRodney W. Grimes vm_map_unlock(m); 677df8bae1dSRodney W. Grimes } 67886f08737SRobert Watson 679483f692eSMark Johnston /* 680483f692eSMark Johnston * kmem_bootstrap_free: 681483f692eSMark Johnston * 682483f692eSMark Johnston * Free pages backing preloaded data (e.g., kernel modules) to the 683483f692eSMark Johnston * system. Currently only supported on platforms that create a 684483f692eSMark Johnston * vm_phys segment for preloaded data. 685483f692eSMark Johnston */ 686483f692eSMark Johnston void 687483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size) 688483f692eSMark Johnston { 689483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__) 690483f692eSMark Johnston struct vm_domain *vmd; 6916c85795aSMark Johnston vm_offset_t end, va; 692483f692eSMark Johnston vm_paddr_t pa; 693483f692eSMark Johnston vm_page_t m; 694483f692eSMark Johnston 695483f692eSMark Johnston end = trunc_page(start + size); 696483f692eSMark Johnston start = round_page(start); 697483f692eSMark Johnston 6986c85795aSMark Johnston for (va = start; va < end; va += PAGE_SIZE) { 6996c85795aSMark Johnston pa = pmap_kextract(va); 700483f692eSMark Johnston m = PHYS_TO_VM_PAGE(pa); 701483f692eSMark Johnston 702483f692eSMark Johnston vmd = vm_pagequeue_domain(m); 703483f692eSMark Johnston vm_domain_free_lock(vmd); 704483f692eSMark Johnston vm_phys_free_pages(m, 0); 705c16bd872SMark Johnston vmd->vmd_page_count++; 706483f692eSMark Johnston vm_domain_free_unlock(vmd); 707c16bd872SMark Johnston 708c16bd872SMark Johnston vm_domain_freecnt_inc(vmd, 1); 709c16bd872SMark Johnston vm_cnt.v_page_count++; 710483f692eSMark Johnston } 7116c85795aSMark Johnston pmap_remove(kernel_pmap, start, end); 7126c85795aSMark Johnston (void)vmem_add(kernel_arena, start, end - start, M_WAITOK); 713483f692eSMark Johnston #endif 714483f692eSMark Johnston } 715483f692eSMark Johnston 7169309e63cSRobert Watson #ifdef DIAGNOSTIC 71786f08737SRobert Watson /* 71886f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 71986f08737SRobert Watson * purposes. 72086f08737SRobert Watson */ 72186f08737SRobert Watson static int 72286f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 72386f08737SRobert Watson { 72486f08737SRobert Watson int error, i; 72586f08737SRobert Watson 72686f08737SRobert Watson i = 0; 72786f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 72886f08737SRobert Watson if (error) 72986f08737SRobert Watson return (error); 7309b43bc27SAndriy Gapon if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 7319b43bc27SAndriy Gapon return (EINVAL); 7329b43bc27SAndriy Gapon if (i != 0) 7339b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, i); 73486f08737SRobert Watson return (0); 73586f08737SRobert Watson } 73686f08737SRobert Watson 73786f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 7389b43bc27SAndriy Gapon debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags"); 7399309e63cSRobert Watson #endif 740