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 375*067fd858SAlan Cox kmem_malloc_domain(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 382*067fd858SAlan Cox if (__predict_true((flags & M_EXEC) == 0)) 3830766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 3840766f278SJonathan T. Looney else 3850766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_rwx_arena; 3860766f278SJonathan T. Looney #else 3870766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 3880766f278SJonathan T. Looney #endif 389df8bae1dSRodney W. Grimes size = round_page(size); 3900766f278SJonathan T. Looney if (vmem_alloc(arena, size, flags | M_BESTFIT, &addr)) 3915df87b21SJeff Roberson return (0); 392df8bae1dSRodney W. Grimes 3937a469c8eSJeff Roberson rv = kmem_back_domain(domain, kernel_object, addr, size, flags); 3945df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 3950766f278SJonathan T. Looney vmem_free(arena, addr, size); 396f31c239dSAlan Cox return (0); 397df8bae1dSRodney W. Grimes } 3985df87b21SJeff Roberson return (addr); 399e3813573SMatthew D Fleming } 400e3813573SMatthew D Fleming 4017a469c8eSJeff Roberson vm_offset_t 402*067fd858SAlan Cox kmem_malloc(struct vmem *vmem __unused, vm_size_t size, int flags) 4037a469c8eSJeff Roberson { 4047a469c8eSJeff Roberson struct vm_domainset_iter di; 4057a469c8eSJeff Roberson vm_offset_t addr; 4067a469c8eSJeff Roberson int domain; 4077a469c8eSJeff Roberson 4087a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 4097a469c8eSJeff Roberson do { 410*067fd858SAlan Cox addr = kmem_malloc_domain(domain, size, flags); 4117a469c8eSJeff Roberson if (addr != 0) 4127a469c8eSJeff Roberson break; 4137a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 4147a469c8eSJeff Roberson 4157a469c8eSJeff Roberson return (addr); 4167a469c8eSJeff Roberson } 4177a469c8eSJeff Roberson 418e3813573SMatthew D Fleming /* 419e3813573SMatthew D Fleming * kmem_back: 420e3813573SMatthew D Fleming * 421e3813573SMatthew D Fleming * Allocate physical pages for the specified virtual address range. 422e3813573SMatthew D Fleming */ 423e3813573SMatthew D Fleming int 4247a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr, 4257a469c8eSJeff Roberson vm_size_t size, int flags) 426e3813573SMatthew D Fleming { 427e3813573SMatthew D Fleming vm_offset_t offset, i; 42833fff5d5SMark Johnston vm_page_t m, mpred; 4290766f278SJonathan T. Looney vm_prot_t prot; 430e3813573SMatthew D Fleming int pflags; 431e3813573SMatthew D Fleming 4322e47807cSJeff Roberson KASSERT(object == kernel_object, 4337a469c8eSJeff Roberson ("kmem_back_domain: only supports kernel object.")); 4345df87b21SJeff Roberson 4350891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 4365df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 4378d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 4388d6fbbb8SJeff Roberson if (flags & M_WAITOK) 4398d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITFAIL; 4400766f278SJonathan T. Looney prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW; 441df8bae1dSRodney W. Grimes 44233fff5d5SMark Johnston i = 0; 44333fff5d5SMark Johnston VM_OBJECT_WLOCK(object); 4448d6fbbb8SJeff Roberson retry: 44533fff5d5SMark Johnston mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i)); 44633fff5d5SMark Johnston for (; i < size; i += PAGE_SIZE, mpred = m) { 4477a469c8eSJeff Roberson m = vm_page_alloc_domain_after(object, atop(offset + i), 4487a469c8eSJeff Roberson domain, pflags, mpred); 449df8bae1dSRodney W. Grimes 450df8bae1dSRodney W. Grimes /* 4510d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 4520d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 4530d94caffSDavid Greenman * aren't on any queues. 454df8bae1dSRodney W. Grimes */ 455df8bae1dSRodney W. Grimes if (m == NULL) { 4568d6fbbb8SJeff Roberson if ((flags & M_NOWAIT) == 0) 457b18bfc3dSJohn Dyson goto retry; 4588d6fbbb8SJeff Roberson VM_OBJECT_WUNLOCK(object); 4599e829b22SAlan Cox kmem_unback(object, addr, i); 460e3813573SMatthew D Fleming return (KERN_NO_SPACE); 461df8bae1dSRodney W. Grimes } 462e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 4637a469c8eSJeff Roberson ("kmem_back_domain: Domain mismatch %d != %d", 464e2068d0bSJeff Roberson vm_phys_domain(m), domain)); 4651e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 466fff6062aSAlan Cox pmap_zero_page(m); 467d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) != 0, 4689f5c801bSAlan Cox ("kmem_malloc: page %p is managed", m)); 4695df87b21SJeff Roberson m->valid = VM_PAGE_BITS_ALL; 4700766f278SJonathan T. Looney pmap_enter(kernel_pmap, addr + i, m, prot, 4710766f278SJonathan T. Looney prot | PMAP_ENTER_WIRED, 0); 472df8bae1dSRodney W. Grimes } 4735df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 474df8bae1dSRodney W. Grimes 475e3813573SMatthew D Fleming return (KERN_SUCCESS); 476df8bae1dSRodney W. Grimes } 477df8bae1dSRodney W. Grimes 4787a469c8eSJeff Roberson int 4797a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 4807a469c8eSJeff Roberson { 4817a469c8eSJeff Roberson struct vm_domainset_iter di; 4827a469c8eSJeff Roberson int domain; 4837a469c8eSJeff Roberson int ret; 4847a469c8eSJeff Roberson 4857a469c8eSJeff Roberson KASSERT(object == kernel_object, 4867a469c8eSJeff Roberson ("kmem_back: only supports kernel object.")); 4877a469c8eSJeff Roberson 4887a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 4897a469c8eSJeff Roberson do { 4907a469c8eSJeff Roberson ret = kmem_back_domain(domain, object, addr, size, flags); 4917a469c8eSJeff Roberson if (ret == KERN_SUCCESS) 4927a469c8eSJeff Roberson break; 4937a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 4947a469c8eSJeff Roberson 4957a469c8eSJeff Roberson return (ret); 4967a469c8eSJeff Roberson } 4977a469c8eSJeff Roberson 4989e829b22SAlan Cox /* 4999e829b22SAlan Cox * kmem_unback: 5009e829b22SAlan Cox * 5019e829b22SAlan Cox * Unmap and free the physical pages underlying the specified virtual 5029e829b22SAlan Cox * address range. 5039e829b22SAlan Cox * 5049e829b22SAlan Cox * A physical page must exist within the specified object at each index 5059e829b22SAlan Cox * that is being unmapped. 5069e829b22SAlan Cox */ 5077a469c8eSJeff Roberson static int 5087a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5095df87b21SJeff Roberson { 5107e05ffa6SMark Johnston vm_page_t m, next; 5117e05ffa6SMark Johnston vm_offset_t end, offset; 5127a469c8eSJeff Roberson int domain; 5135df87b21SJeff Roberson 5142e47807cSJeff Roberson KASSERT(object == kernel_object, 5152e47807cSJeff Roberson ("kmem_unback: only supports kernel object.")); 5165df87b21SJeff Roberson 5177a469c8eSJeff Roberson if (size == 0) 5187a469c8eSJeff Roberson return (0); 519fa2f411cSAlan Cox pmap_remove(kernel_pmap, addr, addr + size); 5205df87b21SJeff Roberson offset = addr - VM_MIN_KERNEL_ADDRESS; 5217e05ffa6SMark Johnston end = offset + size; 5225df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 5237a469c8eSJeff Roberson m = vm_page_lookup(object, atop(offset)); 524e2068d0bSJeff Roberson domain = vm_phys_domain(m); 5257a469c8eSJeff Roberson for (; offset < end; offset += PAGE_SIZE, m = next) { 5267e05ffa6SMark Johnston next = vm_page_next(m); 52727e9ed8aSAlan Cox vm_page_unwire(m, PQ_NONE); 5285df87b21SJeff Roberson vm_page_free(m); 5295df87b21SJeff Roberson } 5305df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 5317a469c8eSJeff Roberson 5327a469c8eSJeff Roberson return (domain); 5337a469c8eSJeff Roberson } 5347a469c8eSJeff Roberson 5357a469c8eSJeff Roberson void 5367a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5377a469c8eSJeff Roberson { 5387a469c8eSJeff Roberson 5397a469c8eSJeff Roberson _kmem_unback(object, addr, size); 5405df87b21SJeff Roberson } 5415df87b21SJeff Roberson 542df8bae1dSRodney W. Grimes /* 5435df87b21SJeff Roberson * kmem_free: 5445df87b21SJeff Roberson * 5455df87b21SJeff Roberson * Free memory allocated with kmem_malloc. The size must match the 5465df87b21SJeff Roberson * original allocation. 5475df87b21SJeff Roberson */ 5485df87b21SJeff Roberson void 5495df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size) 5505df87b21SJeff Roberson { 5510766f278SJonathan T. Looney struct vmem *arena; 5527a469c8eSJeff Roberson int domain; 5535df87b21SJeff Roberson 5540766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0 5550766f278SJonathan T. Looney KASSERT(vmem == kernel_arena || vmem == kernel_rwx_arena, 5560766f278SJonathan T. Looney ("kmem_free: Only kernel_arena or kernel_rwx_arena are supported.")); 5570766f278SJonathan T. Looney #else 5582e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 5592e47807cSJeff Roberson ("kmem_free: Only kernel_arena is supported.")); 5600766f278SJonathan T. Looney #endif 5610766f278SJonathan T. Looney 5625df87b21SJeff Roberson size = round_page(size); 5637a469c8eSJeff Roberson domain = _kmem_unback(kernel_object, addr, size); 5640766f278SJonathan T. Looney #if VM_NRESERVLEVEL > 0 5650766f278SJonathan T. Looney if (__predict_true(vmem == kernel_arena)) 5660766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 5670766f278SJonathan T. Looney else 5680766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_rwx_arena; 5690766f278SJonathan T. Looney #else 5700766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 5710766f278SJonathan T. Looney #endif 5720766f278SJonathan T. Looney vmem_free(arena, addr, size); 5735df87b21SJeff Roberson } 5745df87b21SJeff Roberson 5755df87b21SJeff Roberson /* 5765df87b21SJeff Roberson * kmap_alloc_wait: 577df8bae1dSRodney W. Grimes * 578df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 579df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 580df8bae1dSRodney W. Grimes * 5811c7c3c6aSMatthew Dillon * This routine may block. 582df8bae1dSRodney W. Grimes */ 5830d94caffSDavid Greenman vm_offset_t 5846e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size) 585df8bae1dSRodney W. Grimes { 586df8bae1dSRodney W. Grimes vm_offset_t addr; 58723955314SAlfred Perlstein 588df8bae1dSRodney W. Grimes size = round_page(size); 5893364c323SKonstantin Belousov if (!swap_reserve(size)) 5903364c323SKonstantin Belousov return (0); 591df8bae1dSRodney W. Grimes 592df8bae1dSRodney W. Grimes for (;;) { 593df8bae1dSRodney W. Grimes /* 5940d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 5950d94caffSDavid Greenman * to lock out sleepers/wakers. 596df8bae1dSRodney W. Grimes */ 597df8bae1dSRodney W. Grimes vm_map_lock(map); 598e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 599df8bae1dSRodney W. Grimes break; 600df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 601df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 602df8bae1dSRodney W. Grimes vm_map_unlock(map); 6033364c323SKonstantin Belousov swap_release(size); 604df8bae1dSRodney W. Grimes return (0); 605df8bae1dSRodney W. Grimes } 6069688f931SAlan Cox map->needs_wakeup = TRUE; 6078ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 608df8bae1dSRodney W. Grimes } 6093364c323SKonstantin Belousov vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, 6103364c323SKonstantin Belousov VM_PROT_ALL, MAP_ACC_CHARGED); 611df8bae1dSRodney W. Grimes vm_map_unlock(map); 612df8bae1dSRodney W. Grimes return (addr); 613df8bae1dSRodney W. Grimes } 614df8bae1dSRodney W. Grimes 615df8bae1dSRodney W. Grimes /* 6165df87b21SJeff Roberson * kmap_free_wakeup: 617df8bae1dSRodney W. Grimes * 61824a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 619df8bae1dSRodney W. Grimes * waiting for memory in that map. 620df8bae1dSRodney W. Grimes */ 6210d94caffSDavid Greenman void 6226e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 623df8bae1dSRodney W. Grimes { 62423955314SAlfred Perlstein 625df8bae1dSRodney W. Grimes vm_map_lock(map); 626655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 6279688f931SAlan Cox if (map->needs_wakeup) { 6289688f931SAlan Cox map->needs_wakeup = FALSE; 6299688f931SAlan Cox vm_map_wakeup(map); 6309688f931SAlan Cox } 631df8bae1dSRodney W. Grimes vm_map_unlock(map); 632df8bae1dSRodney W. Grimes } 633df8bae1dSRodney W. Grimes 6345df87b21SJeff Roberson void 63589cb2a19SMatthew D Fleming kmem_init_zero_region(void) 63689cb2a19SMatthew D Fleming { 637cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 63889cb2a19SMatthew D Fleming vm_page_t m; 63989cb2a19SMatthew D Fleming 640cfb00e5aSMatthew D Fleming /* 641cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 642cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 643cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 644cfb00e5aSMatthew D Fleming */ 6455df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 646703dec68SAlan Cox m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | 64789cb2a19SMatthew D Fleming VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO); 64889cb2a19SMatthew D Fleming if ((m->flags & PG_ZERO) == 0) 64989cb2a19SMatthew D Fleming pmap_zero_page(m); 65089cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 65189cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 6525df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 65389cb2a19SMatthew D Fleming 65489cb2a19SMatthew D Fleming zero_region = (const void *)addr; 65589cb2a19SMatthew D Fleming } 65689cb2a19SMatthew D Fleming 657df8bae1dSRodney W. Grimes /* 6581c7c3c6aSMatthew Dillon * kmem_init: 6591c7c3c6aSMatthew Dillon * 6601c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 6611c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 6621c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 6631c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 664df8bae1dSRodney W. Grimes */ 6650d94caffSDavid Greenman void 6666e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end) 667df8bae1dSRodney W. Grimes { 668030f2369SAlfred Perlstein vm_map_t m; 669df8bae1dSRodney W. Grimes 6702d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 671c9267356SAlan Cox m->system_map = 1; 672df8bae1dSRodney W. Grimes vm_map_lock(m); 673df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 674df8bae1dSRodney W. Grimes kernel_map = m; 675c9267356SAlan Cox (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 6765cfa90e9SAlan Cox #ifdef __amd64__ 6775cfa90e9SAlan Cox KERNBASE, 6785cfa90e9SAlan Cox #else 6795cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 6805cfa90e9SAlan Cox #endif 6815cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 682df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 683df8bae1dSRodney W. Grimes vm_map_unlock(m); 684df8bae1dSRodney W. Grimes } 68586f08737SRobert Watson 686483f692eSMark Johnston /* 687483f692eSMark Johnston * kmem_bootstrap_free: 688483f692eSMark Johnston * 689483f692eSMark Johnston * Free pages backing preloaded data (e.g., kernel modules) to the 690483f692eSMark Johnston * system. Currently only supported on platforms that create a 691483f692eSMark Johnston * vm_phys segment for preloaded data. 692483f692eSMark Johnston */ 693483f692eSMark Johnston void 694483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size) 695483f692eSMark Johnston { 696483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__) 697483f692eSMark Johnston struct vm_domain *vmd; 6986c85795aSMark Johnston vm_offset_t end, va; 699483f692eSMark Johnston vm_paddr_t pa; 700483f692eSMark Johnston vm_page_t m; 701483f692eSMark Johnston 702483f692eSMark Johnston end = trunc_page(start + size); 703483f692eSMark Johnston start = round_page(start); 704483f692eSMark Johnston 7056c85795aSMark Johnston for (va = start; va < end; va += PAGE_SIZE) { 7066c85795aSMark Johnston pa = pmap_kextract(va); 707483f692eSMark Johnston m = PHYS_TO_VM_PAGE(pa); 708483f692eSMark Johnston 709483f692eSMark Johnston vmd = vm_pagequeue_domain(m); 710483f692eSMark Johnston vm_domain_free_lock(vmd); 711483f692eSMark Johnston vm_phys_free_pages(m, 0); 712c16bd872SMark Johnston vmd->vmd_page_count++; 713483f692eSMark Johnston vm_domain_free_unlock(vmd); 714c16bd872SMark Johnston 715c16bd872SMark Johnston vm_domain_freecnt_inc(vmd, 1); 716c16bd872SMark Johnston vm_cnt.v_page_count++; 717483f692eSMark Johnston } 7186c85795aSMark Johnston pmap_remove(kernel_pmap, start, end); 7196c85795aSMark Johnston (void)vmem_add(kernel_arena, start, end - start, M_WAITOK); 720483f692eSMark Johnston #endif 721483f692eSMark Johnston } 722483f692eSMark Johnston 7239309e63cSRobert Watson #ifdef DIAGNOSTIC 72486f08737SRobert Watson /* 72586f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 72686f08737SRobert Watson * purposes. 72786f08737SRobert Watson */ 72886f08737SRobert Watson static int 72986f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 73086f08737SRobert Watson { 73186f08737SRobert Watson int error, i; 73286f08737SRobert Watson 73386f08737SRobert Watson i = 0; 73486f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 73586f08737SRobert Watson if (error) 73686f08737SRobert Watson return (error); 7379b43bc27SAndriy Gapon if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 7389b43bc27SAndriy Gapon return (EINVAL); 7399b43bc27SAndriy Gapon if (i != 0) 7409b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, i); 74186f08737SRobert Watson return (0); 74286f08737SRobert Watson } 74386f08737SRobert Watson 74486f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 7459b43bc27SAndriy Gapon debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags"); 7469309e63cSRobert Watson #endif 747