160727d8bSWarner Losh /*- 2*796df753SPedro 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 70df8bae1dSRodney W. Grimes #include <sys/param.h> 71df8bae1dSRodney W. Grimes #include <sys/systm.h> 7260363fb9SLuigi Rizzo #include <sys/kernel.h> /* for ticks and hz */ 730f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h> 74fb919e4dSMark Murray #include <sys/lock.h> 75f23b4c91SGarrett Wollman #include <sys/proc.h> 76a1f6d91cSDavid Greenman #include <sys/malloc.h> 7789f6b863SAttilio Rao #include <sys/rwlock.h> 7886f08737SRobert Watson #include <sys/sysctl.h> 795df87b21SJeff Roberson #include <sys/vmem.h> 80df8bae1dSRodney W. Grimes 81df8bae1dSRodney W. Grimes #include <vm/vm.h> 82efeaf95aSDavid Greenman #include <vm/vm_param.h> 835df87b21SJeff Roberson #include <vm/vm_kern.h> 84efeaf95aSDavid Greenman #include <vm/pmap.h> 85efeaf95aSDavid Greenman #include <vm/vm_map.h> 86efeaf95aSDavid Greenman #include <vm/vm_object.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 88df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 8933fff5d5SMark Johnston #include <vm/vm_radix.h> 909b4288a3SBruce Evans #include <vm/vm_extern.h> 910f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h> 92df8bae1dSRodney W. Grimes 93e7788a47SKonstantin Belousov vm_map_t kernel_map; 94e7788a47SKonstantin Belousov vm_map_t exec_map; 95cebde069SMike Silbersack vm_map_t pipe_map; 96f23b4c91SGarrett Wollman 9789cb2a19SMatthew D Fleming const void *zero_region; 9889cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0); 9989cb2a19SMatthew D Fleming 100645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */ 101645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS; 102645743eaSJohn Baldwin 103ec492b13SMark Johnston u_int exec_map_entry_size; 104ec492b13SMark Johnston u_int exec_map_entries; 105ec492b13SMark Johnston 10694bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD, 107f0188618SHans Petter Selasky SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address"); 10894bfd5b1SMarius Strobl 10994bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD, 110fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__) 11194bfd5b1SMarius Strobl &vm_max_kernel_address, 0, 11294bfd5b1SMarius Strobl #else 113f0188618SHans Petter Selasky SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS, 11494bfd5b1SMarius Strobl #endif 11594bfd5b1SMarius Strobl "Max kernel address"); 11694bfd5b1SMarius Strobl 117df8bae1dSRodney W. Grimes /* 1185df87b21SJeff Roberson * kva_alloc: 119a839bdc8SDmitrij Tejblum * 120b77c2bcdSAlan Cox * Allocate a virtual address range with no underlying object and 121b77c2bcdSAlan Cox * no initial mapping to physical memory. Any mapping from this 122b77c2bcdSAlan Cox * range to physical memory must be explicitly created prior to 123b77c2bcdSAlan Cox * its use, typically with pmap_qenter(). Any attempt to create 124b77c2bcdSAlan Cox * a mapping on demand through vm_fault() will result in a panic. 125a839bdc8SDmitrij Tejblum */ 126a839bdc8SDmitrij Tejblum vm_offset_t 1276e309d75SEd Maste kva_alloc(vm_size_t size) 128a839bdc8SDmitrij Tejblum { 129a839bdc8SDmitrij Tejblum vm_offset_t addr; 130a839bdc8SDmitrij Tejblum 131a839bdc8SDmitrij Tejblum size = round_page(size); 1325df87b21SJeff Roberson if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr)) 133a839bdc8SDmitrij Tejblum return (0); 1345df87b21SJeff Roberson 135a839bdc8SDmitrij Tejblum return (addr); 136a839bdc8SDmitrij Tejblum } 137a839bdc8SDmitrij Tejblum 138a839bdc8SDmitrij Tejblum /* 1395df87b21SJeff Roberson * kva_free: 140ca596a25SJuli Mallett * 1415df87b21SJeff Roberson * Release a region of kernel virtual memory allocated 1425df87b21SJeff Roberson * with kva_alloc, and return the physical pages 1435df87b21SJeff Roberson * associated with that region. 1445df87b21SJeff Roberson * 1455df87b21SJeff Roberson * This routine may not block on kernel maps. 146ca596a25SJuli Mallett */ 1475df87b21SJeff Roberson void 1486e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size) 149df8bae1dSRodney W. Grimes { 150df8bae1dSRodney W. Grimes 151df8bae1dSRodney W. Grimes size = round_page(size); 1525df87b21SJeff Roberson vmem_free(kernel_arena, addr, size); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes 155df8bae1dSRodney W. Grimes /* 1560ff0fc84SAlan Cox * Allocates a region from the kernel address map and physical pages 1570ff0fc84SAlan Cox * within the specified address range to the kernel object. Creates a 1580ff0fc84SAlan Cox * wired mapping from this region to these pages, and returns the 1590ff0fc84SAlan Cox * region's starting virtual address. The allocated pages are not 1600ff0fc84SAlan Cox * necessarily physically contiguous. If M_ZERO is specified through the 1610ff0fc84SAlan Cox * given flags, then the pages are zeroed before they are mapped. 1620ff0fc84SAlan Cox */ 1630ff0fc84SAlan Cox vm_offset_t 1645df87b21SJeff Roberson kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low, 1650ff0fc84SAlan Cox vm_paddr_t high, vm_memattr_t memattr) 1660ff0fc84SAlan Cox { 1672e47807cSJeff Roberson vm_object_t object = kernel_object; 168d1780e8dSKonstantin Belousov vm_offset_t addr, i, offset; 1690ff0fc84SAlan Cox vm_page_t m; 1700ff0fc84SAlan Cox int pflags, tries; 1710ff0fc84SAlan Cox 1722e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 1732e47807cSJeff Roberson ("kmem_alloc_attr: Only kernel_arena is supported.")); 1740ff0fc84SAlan Cox size = round_page(size); 1755df87b21SJeff Roberson if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr)) 1760ff0fc84SAlan Cox return (0); 1770ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 1785df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 1798d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 1808d6fbbb8SJeff Roberson pflags |= VM_ALLOC_NOWAIT; 18189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1825df87b21SJeff Roberson for (i = 0; i < size; i += PAGE_SIZE) { 1830ff0fc84SAlan Cox tries = 0; 1840ff0fc84SAlan Cox retry: 185d1780e8dSKonstantin Belousov m = vm_page_alloc_contig(object, atop(offset + i), 1865df87b21SJeff Roberson pflags, 1, low, high, PAGE_SIZE, 0, memattr); 1870ff0fc84SAlan Cox if (m == NULL) { 18889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1890ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 190c869e672SAlan Cox if (!vm_page_reclaim_contig(pflags, 1, 191c869e672SAlan Cox low, high, PAGE_SIZE, 0) && 192c869e672SAlan Cox (flags & M_WAITOK) != 0) 193c869e672SAlan Cox VM_WAIT; 19489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1950ff0fc84SAlan Cox tries++; 1960ff0fc84SAlan Cox goto retry; 1970ff0fc84SAlan Cox } 1989e829b22SAlan Cox kmem_unback(object, addr, i); 1995df87b21SJeff Roberson vmem_free(vmem, addr, size); 2000ff0fc84SAlan Cox return (0); 2010ff0fc84SAlan Cox } 2020ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2030ff0fc84SAlan Cox pmap_zero_page(m); 2040ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 20539ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 20639ffa8c1SKonstantin Belousov VM_PROT_ALL | PMAP_ENTER_WIRED, 0); 2070ff0fc84SAlan Cox } 20889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2090ff0fc84SAlan Cox return (addr); 2100ff0fc84SAlan Cox } 2110ff0fc84SAlan Cox 2120ff0fc84SAlan Cox /* 2130ff0fc84SAlan Cox * Allocates a region from the kernel address map and physically 2140ff0fc84SAlan Cox * contiguous pages within the specified address range to the kernel 2150ff0fc84SAlan Cox * object. Creates a wired mapping from this region to these pages, and 2160ff0fc84SAlan Cox * returns the region's starting virtual address. If M_ZERO is specified 2170ff0fc84SAlan Cox * through the given flags, then the pages are zeroed before they are 2180ff0fc84SAlan Cox * mapped. 2190ff0fc84SAlan Cox */ 2200ff0fc84SAlan Cox vm_offset_t 2215df87b21SJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low, 2220ff0fc84SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 2230ff0fc84SAlan Cox vm_memattr_t memattr) 2240ff0fc84SAlan Cox { 2252e47807cSJeff Roberson vm_object_t object = kernel_object; 226d1780e8dSKonstantin Belousov vm_offset_t addr, offset, tmp; 2270ff0fc84SAlan Cox vm_page_t end_m, m; 228c869e672SAlan Cox u_long npages; 2290ff0fc84SAlan Cox int pflags, tries; 2300ff0fc84SAlan Cox 2312e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 2322e47807cSJeff Roberson ("kmem_alloc_contig: Only kernel_arena is supported.")); 2330ff0fc84SAlan Cox size = round_page(size); 2345df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 2350ff0fc84SAlan Cox return (0); 2360ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 2375df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 2388d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 2398d6fbbb8SJeff Roberson pflags |= VM_ALLOC_NOWAIT; 240c869e672SAlan Cox npages = atop(size); 24189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2420ff0fc84SAlan Cox tries = 0; 2430ff0fc84SAlan Cox retry: 244d1780e8dSKonstantin Belousov m = vm_page_alloc_contig(object, atop(offset), pflags, 245c869e672SAlan Cox npages, low, high, alignment, boundary, memattr); 2460ff0fc84SAlan Cox if (m == NULL) { 24789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2480ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 249c869e672SAlan Cox if (!vm_page_reclaim_contig(pflags, npages, low, high, 250c869e672SAlan Cox alignment, boundary) && (flags & M_WAITOK) != 0) 251c869e672SAlan Cox VM_WAIT; 25289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2530ff0fc84SAlan Cox tries++; 2540ff0fc84SAlan Cox goto retry; 2550ff0fc84SAlan Cox } 2565df87b21SJeff Roberson vmem_free(vmem, addr, size); 2570ff0fc84SAlan Cox return (0); 2580ff0fc84SAlan Cox } 259c869e672SAlan Cox end_m = m + npages; 2605df87b21SJeff Roberson tmp = addr; 2610ff0fc84SAlan Cox for (; m < end_m; m++) { 2620ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2630ff0fc84SAlan Cox pmap_zero_page(m); 2640ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 26539ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL, 26639ffa8c1SKonstantin Belousov VM_PROT_ALL | PMAP_ENTER_WIRED, 0); 2675df87b21SJeff Roberson tmp += PAGE_SIZE; 2680ff0fc84SAlan Cox } 26989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2700ff0fc84SAlan Cox return (addr); 2710ff0fc84SAlan Cox } 2720ff0fc84SAlan Cox 2730ff0fc84SAlan Cox /* 274df8bae1dSRodney W. Grimes * kmem_suballoc: 275df8bae1dSRodney W. Grimes * 276df8bae1dSRodney W. Grimes * Allocates a map to manage a subrange 277df8bae1dSRodney W. Grimes * of the kernel virtual address space. 278df8bae1dSRodney W. Grimes * 279df8bae1dSRodney W. Grimes * Arguments are as follows: 280df8bae1dSRodney W. Grimes * 281df8bae1dSRodney W. Grimes * parent Map to take range from 282df8bae1dSRodney W. Grimes * min, max Returned endpoints of map 283030f2369SAlfred Perlstein * size Size of range to find 2843202ed75SAlan Cox * superpage_align Request that min is superpage aligned 285df8bae1dSRodney W. Grimes */ 2860d94caffSDavid Greenman vm_map_t 2873202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max, 2883202ed75SAlan Cox vm_size_t size, boolean_t superpage_align) 289df8bae1dSRodney W. Grimes { 2906e4f51d1SAlfred Perlstein int ret; 291df8bae1dSRodney W. Grimes vm_map_t result; 29223955314SAlfred Perlstein 293df8bae1dSRodney W. Grimes size = round_page(size); 294df8bae1dSRodney W. Grimes 2952bc24aa9SAlan Cox *min = vm_map_min(parent); 296edb572a3SJohn Baldwin ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ? 2975aa60b6fSJohn Baldwin VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 2983364c323SKonstantin Belousov MAP_ACC_NO_CHARGE); 29924dedba9SAlan Cox if (ret != KERN_SUCCESS) 30024dedba9SAlan Cox panic("kmem_suballoc: bad status return of %d", ret); 301df8bae1dSRodney W. Grimes *max = *min + size; 3022d8acc0fSJohn Dyson result = vm_map_create(vm_map_pmap(parent), *min, *max); 303df8bae1dSRodney W. Grimes if (result == NULL) 304df8bae1dSRodney W. Grimes panic("kmem_suballoc: cannot create submap"); 3056e4f51d1SAlfred Perlstein if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS) 306df8bae1dSRodney W. Grimes panic("kmem_suballoc: unable to change range to submap"); 307df8bae1dSRodney W. Grimes return (result); 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes 310df8bae1dSRodney W. Grimes /* 3111c7c3c6aSMatthew Dillon * kmem_malloc: 3121c7c3c6aSMatthew Dillon * 3135df87b21SJeff Roberson * Allocate wired-down pages in the kernel's address space. 314df8bae1dSRodney W. Grimes */ 315df8bae1dSRodney W. Grimes vm_offset_t 3165df87b21SJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags) 317df8bae1dSRodney W. Grimes { 318df8bae1dSRodney W. Grimes vm_offset_t addr; 3195df87b21SJeff Roberson int rv; 320df8bae1dSRodney W. Grimes 3212e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 3222e47807cSJeff Roberson ("kmem_malloc: Only kernel_arena is supported.")); 323df8bae1dSRodney W. Grimes size = round_page(size); 3245df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 3255df87b21SJeff Roberson return (0); 326df8bae1dSRodney W. Grimes 3272e47807cSJeff Roberson rv = kmem_back(kernel_object, addr, size, flags); 3285df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 3295df87b21SJeff Roberson vmem_free(vmem, addr, size); 330f31c239dSAlan Cox return (0); 331df8bae1dSRodney W. Grimes } 3325df87b21SJeff Roberson return (addr); 333e3813573SMatthew D Fleming } 334e3813573SMatthew D Fleming 335e3813573SMatthew D Fleming /* 336e3813573SMatthew D Fleming * kmem_back: 337e3813573SMatthew D Fleming * 338e3813573SMatthew D Fleming * Allocate physical pages for the specified virtual address range. 339e3813573SMatthew D Fleming */ 340e3813573SMatthew D Fleming int 3415df87b21SJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 342e3813573SMatthew D Fleming { 343e3813573SMatthew D Fleming vm_offset_t offset, i; 34433fff5d5SMark Johnston vm_page_t m, mpred; 345e3813573SMatthew D Fleming int pflags; 346e3813573SMatthew D Fleming 3472e47807cSJeff Roberson KASSERT(object == kernel_object, 3482e47807cSJeff Roberson ("kmem_back: only supports kernel object.")); 3495df87b21SJeff Roberson 3500891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 3515df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 3528d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 3538d6fbbb8SJeff Roberson if (flags & M_WAITOK) 3548d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITFAIL; 355df8bae1dSRodney W. Grimes 35633fff5d5SMark Johnston i = 0; 35733fff5d5SMark Johnston VM_OBJECT_WLOCK(object); 3588d6fbbb8SJeff Roberson retry: 35933fff5d5SMark Johnston mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i)); 36033fff5d5SMark Johnston for (; i < size; i += PAGE_SIZE, mpred = m) { 36133fff5d5SMark Johnston m = vm_page_alloc_after(object, atop(offset + i), pflags, 36233fff5d5SMark Johnston mpred); 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes /* 3650d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 3660d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 3670d94caffSDavid Greenman * aren't on any queues. 368df8bae1dSRodney W. Grimes */ 369df8bae1dSRodney W. Grimes if (m == NULL) { 3708d6fbbb8SJeff Roberson if ((flags & M_NOWAIT) == 0) 371b18bfc3dSJohn Dyson goto retry; 3728d6fbbb8SJeff Roberson VM_OBJECT_WUNLOCK(object); 3739e829b22SAlan Cox kmem_unback(object, addr, i); 374e3813573SMatthew D Fleming return (KERN_NO_SPACE); 375df8bae1dSRodney W. Grimes } 3761e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 377fff6062aSAlan Cox pmap_zero_page(m); 378d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) != 0, 3799f5c801bSAlan Cox ("kmem_malloc: page %p is managed", m)); 3805df87b21SJeff Roberson m->valid = VM_PAGE_BITS_ALL; 38139ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 38239ffa8c1SKonstantin Belousov VM_PROT_ALL | PMAP_ENTER_WIRED, 0); 383df8bae1dSRodney W. Grimes } 3845df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 385df8bae1dSRodney W. Grimes 386e3813573SMatthew D Fleming return (KERN_SUCCESS); 387df8bae1dSRodney W. Grimes } 388df8bae1dSRodney W. Grimes 3899e829b22SAlan Cox /* 3909e829b22SAlan Cox * kmem_unback: 3919e829b22SAlan Cox * 3929e829b22SAlan Cox * Unmap and free the physical pages underlying the specified virtual 3939e829b22SAlan Cox * address range. 3949e829b22SAlan Cox * 3959e829b22SAlan Cox * A physical page must exist within the specified object at each index 3969e829b22SAlan Cox * that is being unmapped. 3979e829b22SAlan Cox */ 3985df87b21SJeff Roberson void 3995df87b21SJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 4005df87b21SJeff Roberson { 4017e05ffa6SMark Johnston vm_page_t m, next; 4027e05ffa6SMark Johnston vm_offset_t end, offset; 4035df87b21SJeff Roberson 4042e47807cSJeff Roberson KASSERT(object == kernel_object, 4052e47807cSJeff Roberson ("kmem_unback: only supports kernel object.")); 4065df87b21SJeff Roberson 407fa2f411cSAlan Cox pmap_remove(kernel_pmap, addr, addr + size); 4085df87b21SJeff Roberson offset = addr - VM_MIN_KERNEL_ADDRESS; 4097e05ffa6SMark Johnston end = offset + size; 4105df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 4117e05ffa6SMark Johnston for (m = vm_page_lookup(object, atop(offset)); offset < end; 4127e05ffa6SMark Johnston offset += PAGE_SIZE, m = next) { 4137e05ffa6SMark Johnston next = vm_page_next(m); 41427e9ed8aSAlan Cox vm_page_unwire(m, PQ_NONE); 4155df87b21SJeff Roberson vm_page_free(m); 4165df87b21SJeff Roberson } 4175df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 4185df87b21SJeff Roberson } 4195df87b21SJeff Roberson 420df8bae1dSRodney W. Grimes /* 4215df87b21SJeff Roberson * kmem_free: 4225df87b21SJeff Roberson * 4235df87b21SJeff Roberson * Free memory allocated with kmem_malloc. The size must match the 4245df87b21SJeff Roberson * original allocation. 4255df87b21SJeff Roberson */ 4265df87b21SJeff Roberson void 4275df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size) 4285df87b21SJeff Roberson { 4295df87b21SJeff Roberson 4302e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 4312e47807cSJeff Roberson ("kmem_free: Only kernel_arena is supported.")); 4325df87b21SJeff Roberson size = round_page(size); 4332e47807cSJeff Roberson kmem_unback(kernel_object, addr, size); 4345df87b21SJeff Roberson vmem_free(vmem, addr, size); 4355df87b21SJeff Roberson } 4365df87b21SJeff Roberson 4375df87b21SJeff Roberson /* 4385df87b21SJeff Roberson * kmap_alloc_wait: 439df8bae1dSRodney W. Grimes * 440df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 441df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 442df8bae1dSRodney W. Grimes * 4431c7c3c6aSMatthew Dillon * This routine may block. 444df8bae1dSRodney W. Grimes */ 4450d94caffSDavid Greenman vm_offset_t 4466e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size) 447df8bae1dSRodney W. Grimes { 448df8bae1dSRodney W. Grimes vm_offset_t addr; 44923955314SAlfred Perlstein 450df8bae1dSRodney W. Grimes size = round_page(size); 4513364c323SKonstantin Belousov if (!swap_reserve(size)) 4523364c323SKonstantin Belousov return (0); 453df8bae1dSRodney W. Grimes 454df8bae1dSRodney W. Grimes for (;;) { 455df8bae1dSRodney W. Grimes /* 4560d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 4570d94caffSDavid Greenman * to lock out sleepers/wakers. 458df8bae1dSRodney W. Grimes */ 459df8bae1dSRodney W. Grimes vm_map_lock(map); 460e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 461df8bae1dSRodney W. Grimes break; 462df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 463df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 464df8bae1dSRodney W. Grimes vm_map_unlock(map); 4653364c323SKonstantin Belousov swap_release(size); 466df8bae1dSRodney W. Grimes return (0); 467df8bae1dSRodney W. Grimes } 4689688f931SAlan Cox map->needs_wakeup = TRUE; 4698ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 470df8bae1dSRodney W. Grimes } 4713364c323SKonstantin Belousov vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, 4723364c323SKonstantin Belousov VM_PROT_ALL, MAP_ACC_CHARGED); 473df8bae1dSRodney W. Grimes vm_map_unlock(map); 474df8bae1dSRodney W. Grimes return (addr); 475df8bae1dSRodney W. Grimes } 476df8bae1dSRodney W. Grimes 477df8bae1dSRodney W. Grimes /* 4785df87b21SJeff Roberson * kmap_free_wakeup: 479df8bae1dSRodney W. Grimes * 48024a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 481df8bae1dSRodney W. Grimes * waiting for memory in that map. 482df8bae1dSRodney W. Grimes */ 4830d94caffSDavid Greenman void 4846e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 485df8bae1dSRodney W. Grimes { 48623955314SAlfred Perlstein 487df8bae1dSRodney W. Grimes vm_map_lock(map); 488655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 4899688f931SAlan Cox if (map->needs_wakeup) { 4909688f931SAlan Cox map->needs_wakeup = FALSE; 4919688f931SAlan Cox vm_map_wakeup(map); 4929688f931SAlan Cox } 493df8bae1dSRodney W. Grimes vm_map_unlock(map); 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes 4965df87b21SJeff Roberson void 49789cb2a19SMatthew D Fleming kmem_init_zero_region(void) 49889cb2a19SMatthew D Fleming { 499cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 50089cb2a19SMatthew D Fleming vm_page_t m; 50189cb2a19SMatthew D Fleming 502cfb00e5aSMatthew D Fleming /* 503cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 504cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 505cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 506cfb00e5aSMatthew D Fleming */ 5075df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 508703dec68SAlan Cox m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | 50989cb2a19SMatthew D Fleming VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO); 51089cb2a19SMatthew D Fleming if ((m->flags & PG_ZERO) == 0) 51189cb2a19SMatthew D Fleming pmap_zero_page(m); 51289cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 51389cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 5145df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 51589cb2a19SMatthew D Fleming 51689cb2a19SMatthew D Fleming zero_region = (const void *)addr; 51789cb2a19SMatthew D Fleming } 51889cb2a19SMatthew D Fleming 519df8bae1dSRodney W. Grimes /* 5201c7c3c6aSMatthew Dillon * kmem_init: 5211c7c3c6aSMatthew Dillon * 5221c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 5231c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 5241c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 5251c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 526df8bae1dSRodney W. Grimes */ 5270d94caffSDavid Greenman void 5286e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end) 529df8bae1dSRodney W. Grimes { 530030f2369SAlfred Perlstein vm_map_t m; 531df8bae1dSRodney W. Grimes 5322d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 533c9267356SAlan Cox m->system_map = 1; 534df8bae1dSRodney W. Grimes vm_map_lock(m); 535df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 536df8bae1dSRodney W. Grimes kernel_map = m; 537c9267356SAlan Cox (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 5385cfa90e9SAlan Cox #ifdef __amd64__ 5395cfa90e9SAlan Cox KERNBASE, 5405cfa90e9SAlan Cox #else 5415cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 5425cfa90e9SAlan Cox #endif 5435cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 544df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 545df8bae1dSRodney W. Grimes vm_map_unlock(m); 546df8bae1dSRodney W. Grimes } 54786f08737SRobert Watson 5489309e63cSRobert Watson #ifdef DIAGNOSTIC 54986f08737SRobert Watson /* 55086f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 55186f08737SRobert Watson * purposes. 55286f08737SRobert Watson */ 55386f08737SRobert Watson static int 55486f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 55586f08737SRobert Watson { 55686f08737SRobert Watson int error, i; 55786f08737SRobert Watson 55886f08737SRobert Watson i = 0; 55986f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 56086f08737SRobert Watson if (error) 56186f08737SRobert Watson return (error); 5629b43bc27SAndriy Gapon if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 5639b43bc27SAndriy Gapon return (EINVAL); 5649b43bc27SAndriy Gapon if (i != 0) 5659b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, i); 56686f08737SRobert Watson return (0); 56786f08737SRobert Watson } 56886f08737SRobert Watson 56986f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 5709b43bc27SAndriy Gapon debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags"); 5719309e63cSRobert Watson #endif 572