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 70*7a469c8eSJeff Roberson #include "opt_vm.h" 71*7a469c8eSJeff 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 */ 75*7a469c8eSJeff 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> 83*7a469c8eSJeff Roberson #include <sys/vmmeter.h> 84df8bae1dSRodney W. Grimes 85df8bae1dSRodney W. Grimes #include <vm/vm.h> 86efeaf95aSDavid Greenman #include <vm/vm_param.h> 87*7a469c8eSJeff 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> 94*7a469c8eSJeff Roberson #include <vm/vm_phys.h> 9533fff5d5SMark Johnston #include <vm/vm_radix.h> 969b4288a3SBruce Evans #include <vm/vm_extern.h> 970f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h> 98df8bae1dSRodney W. Grimes 99e7788a47SKonstantin Belousov vm_map_t kernel_map; 100e7788a47SKonstantin Belousov vm_map_t exec_map; 101cebde069SMike Silbersack vm_map_t pipe_map; 102f23b4c91SGarrett Wollman 10389cb2a19SMatthew D Fleming const void *zero_region; 10489cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0); 10589cb2a19SMatthew D Fleming 106645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */ 107645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS; 108645743eaSJohn Baldwin 109ec492b13SMark Johnston u_int exec_map_entry_size; 110ec492b13SMark Johnston u_int exec_map_entries; 111ec492b13SMark Johnston 11294bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD, 113f0188618SHans Petter Selasky SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address"); 11494bfd5b1SMarius Strobl 11594bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD, 116fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__) 11794bfd5b1SMarius Strobl &vm_max_kernel_address, 0, 11894bfd5b1SMarius Strobl #else 119f0188618SHans Petter Selasky SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS, 12094bfd5b1SMarius Strobl #endif 12194bfd5b1SMarius Strobl "Max kernel address"); 12294bfd5b1SMarius Strobl 123df8bae1dSRodney W. Grimes /* 1245df87b21SJeff Roberson * kva_alloc: 125a839bdc8SDmitrij Tejblum * 126b77c2bcdSAlan Cox * Allocate a virtual address range with no underlying object and 127b77c2bcdSAlan Cox * no initial mapping to physical memory. Any mapping from this 128b77c2bcdSAlan Cox * range to physical memory must be explicitly created prior to 129b77c2bcdSAlan Cox * its use, typically with pmap_qenter(). Any attempt to create 130b77c2bcdSAlan Cox * a mapping on demand through vm_fault() will result in a panic. 131a839bdc8SDmitrij Tejblum */ 132a839bdc8SDmitrij Tejblum vm_offset_t 1336e309d75SEd Maste kva_alloc(vm_size_t size) 134a839bdc8SDmitrij Tejblum { 135a839bdc8SDmitrij Tejblum vm_offset_t addr; 136a839bdc8SDmitrij Tejblum 137a839bdc8SDmitrij Tejblum size = round_page(size); 1385df87b21SJeff Roberson if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr)) 139a839bdc8SDmitrij Tejblum return (0); 1405df87b21SJeff Roberson 141a839bdc8SDmitrij Tejblum return (addr); 142a839bdc8SDmitrij Tejblum } 143a839bdc8SDmitrij Tejblum 144a839bdc8SDmitrij Tejblum /* 1455df87b21SJeff Roberson * kva_free: 146ca596a25SJuli Mallett * 1475df87b21SJeff Roberson * Release a region of kernel virtual memory allocated 1485df87b21SJeff Roberson * with kva_alloc, and return the physical pages 1495df87b21SJeff Roberson * associated with that region. 1505df87b21SJeff Roberson * 1515df87b21SJeff Roberson * This routine may not block on kernel maps. 152ca596a25SJuli Mallett */ 1535df87b21SJeff Roberson void 1546e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size) 155df8bae1dSRodney W. Grimes { 156df8bae1dSRodney W. Grimes 157df8bae1dSRodney W. Grimes size = round_page(size); 1585df87b21SJeff Roberson vmem_free(kernel_arena, addr, size); 159df8bae1dSRodney W. Grimes } 160df8bae1dSRodney W. Grimes 161df8bae1dSRodney W. Grimes /* 1620ff0fc84SAlan Cox * Allocates a region from the kernel address map and physical pages 1630ff0fc84SAlan Cox * within the specified address range to the kernel object. Creates a 1640ff0fc84SAlan Cox * wired mapping from this region to these pages, and returns the 1650ff0fc84SAlan Cox * region's starting virtual address. The allocated pages are not 1660ff0fc84SAlan Cox * necessarily physically contiguous. If M_ZERO is specified through the 1670ff0fc84SAlan Cox * given flags, then the pages are zeroed before they are mapped. 1680ff0fc84SAlan Cox */ 1690ff0fc84SAlan Cox vm_offset_t 170*7a469c8eSJeff Roberson kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low, 1710ff0fc84SAlan Cox vm_paddr_t high, vm_memattr_t memattr) 1720ff0fc84SAlan Cox { 173*7a469c8eSJeff Roberson vmem_t *vmem; 1742e47807cSJeff Roberson vm_object_t object = kernel_object; 175d1780e8dSKonstantin Belousov vm_offset_t addr, i, offset; 1760ff0fc84SAlan Cox vm_page_t m; 1770ff0fc84SAlan Cox int pflags, tries; 1780ff0fc84SAlan Cox 1790ff0fc84SAlan Cox size = round_page(size); 180*7a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 1815df87b21SJeff Roberson if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr)) 1820ff0fc84SAlan Cox return (0); 1830ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 1845df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 1858d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 1868d6fbbb8SJeff Roberson pflags |= VM_ALLOC_NOWAIT; 18789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1885df87b21SJeff Roberson for (i = 0; i < size; i += PAGE_SIZE) { 1890ff0fc84SAlan Cox tries = 0; 1900ff0fc84SAlan Cox retry: 191*7a469c8eSJeff Roberson m = vm_page_alloc_contig_domain(object, atop(offset + i), 192*7a469c8eSJeff Roberson domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr); 1930ff0fc84SAlan Cox if (m == NULL) { 19489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1950ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 196*7a469c8eSJeff Roberson if (!vm_page_reclaim_contig_domain(domain, 197*7a469c8eSJeff Roberson pflags, 1, low, high, PAGE_SIZE, 0) && 198c869e672SAlan Cox (flags & M_WAITOK) != 0) 199c869e672SAlan Cox VM_WAIT; 20089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2010ff0fc84SAlan Cox tries++; 2020ff0fc84SAlan Cox goto retry; 2030ff0fc84SAlan Cox } 2049e829b22SAlan Cox kmem_unback(object, addr, i); 2055df87b21SJeff Roberson vmem_free(vmem, addr, size); 2060ff0fc84SAlan Cox return (0); 2070ff0fc84SAlan Cox } 208*7a469c8eSJeff Roberson KASSERT(vm_phys_domidx(m) == domain, 209*7a469c8eSJeff Roberson ("kmem_alloc_attr_domain: Domain mismatch %d != %d", 210*7a469c8eSJeff Roberson vm_phys_domidx(m), domain)); 2110ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2120ff0fc84SAlan Cox pmap_zero_page(m); 2130ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 21439ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 21539ffa8c1SKonstantin Belousov VM_PROT_ALL | PMAP_ENTER_WIRED, 0); 2160ff0fc84SAlan Cox } 21789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2180ff0fc84SAlan Cox return (addr); 2190ff0fc84SAlan Cox } 2200ff0fc84SAlan Cox 221*7a469c8eSJeff Roberson vm_offset_t 222*7a469c8eSJeff Roberson kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low, 223*7a469c8eSJeff Roberson vm_paddr_t high, vm_memattr_t memattr) 224*7a469c8eSJeff Roberson { 225*7a469c8eSJeff Roberson struct vm_domainset_iter di; 226*7a469c8eSJeff Roberson vm_offset_t addr; 227*7a469c8eSJeff Roberson int domain; 228*7a469c8eSJeff Roberson 229*7a469c8eSJeff Roberson KASSERT(vmem == kernel_arena, 230*7a469c8eSJeff Roberson ("kmem_alloc_attr: Only kernel_arena is supported.")); 231*7a469c8eSJeff Roberson 232*7a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 233*7a469c8eSJeff Roberson do { 234*7a469c8eSJeff Roberson addr = kmem_alloc_attr_domain(domain, size, flags, low, high, 235*7a469c8eSJeff Roberson memattr); 236*7a469c8eSJeff Roberson if (addr != 0) 237*7a469c8eSJeff Roberson break; 238*7a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 239*7a469c8eSJeff Roberson 240*7a469c8eSJeff Roberson return (addr); 241*7a469c8eSJeff Roberson } 242*7a469c8eSJeff Roberson 2430ff0fc84SAlan Cox /* 2440ff0fc84SAlan Cox * Allocates a region from the kernel address map and physically 2450ff0fc84SAlan Cox * contiguous pages within the specified address range to the kernel 2460ff0fc84SAlan Cox * object. Creates a wired mapping from this region to these pages, and 2470ff0fc84SAlan Cox * returns the region's starting virtual address. If M_ZERO is specified 2480ff0fc84SAlan Cox * through the given flags, then the pages are zeroed before they are 2490ff0fc84SAlan Cox * mapped. 2500ff0fc84SAlan Cox */ 2510ff0fc84SAlan Cox vm_offset_t 252*7a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low, 2530ff0fc84SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 2540ff0fc84SAlan Cox vm_memattr_t memattr) 2550ff0fc84SAlan Cox { 256*7a469c8eSJeff Roberson vmem_t *vmem; 2572e47807cSJeff Roberson vm_object_t object = kernel_object; 258d1780e8dSKonstantin Belousov vm_offset_t addr, offset, tmp; 2590ff0fc84SAlan Cox vm_page_t end_m, m; 260c869e672SAlan Cox u_long npages; 2610ff0fc84SAlan Cox int pflags, tries; 2620ff0fc84SAlan Cox 2630ff0fc84SAlan Cox size = round_page(size); 264*7a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 2655df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 2660ff0fc84SAlan Cox return (0); 2670ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 2685df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 2698d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 2708d6fbbb8SJeff Roberson pflags |= VM_ALLOC_NOWAIT; 271c869e672SAlan Cox npages = atop(size); 27289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2730ff0fc84SAlan Cox tries = 0; 2740ff0fc84SAlan Cox retry: 275*7a469c8eSJeff Roberson m = vm_page_alloc_contig_domain(object, atop(offset), domain, pflags, 276c869e672SAlan Cox npages, low, high, alignment, boundary, memattr); 2770ff0fc84SAlan Cox if (m == NULL) { 27889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2790ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 280*7a469c8eSJeff Roberson if (!vm_page_reclaim_contig_domain(domain, pflags, 281*7a469c8eSJeff Roberson npages, low, high, alignment, boundary) && 282*7a469c8eSJeff Roberson (flags & M_WAITOK) != 0) 283c869e672SAlan Cox VM_WAIT; 28489f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2850ff0fc84SAlan Cox tries++; 2860ff0fc84SAlan Cox goto retry; 2870ff0fc84SAlan Cox } 2885df87b21SJeff Roberson vmem_free(vmem, addr, size); 2890ff0fc84SAlan Cox return (0); 2900ff0fc84SAlan Cox } 291*7a469c8eSJeff Roberson KASSERT(vm_phys_domidx(m) == domain, 292*7a469c8eSJeff Roberson ("kmem_alloc_contig_domain: Domain mismatch %d != %d", 293*7a469c8eSJeff Roberson vm_phys_domidx(m), domain)); 294c869e672SAlan Cox end_m = m + npages; 2955df87b21SJeff Roberson tmp = addr; 2960ff0fc84SAlan Cox for (; m < end_m; m++) { 2970ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2980ff0fc84SAlan Cox pmap_zero_page(m); 2990ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 30039ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL, 30139ffa8c1SKonstantin Belousov VM_PROT_ALL | PMAP_ENTER_WIRED, 0); 3025df87b21SJeff Roberson tmp += PAGE_SIZE; 3030ff0fc84SAlan Cox } 30489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3050ff0fc84SAlan Cox return (addr); 3060ff0fc84SAlan Cox } 3070ff0fc84SAlan Cox 308*7a469c8eSJeff Roberson vm_offset_t 309*7a469c8eSJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low, 310*7a469c8eSJeff Roberson vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 311*7a469c8eSJeff Roberson vm_memattr_t memattr) 312*7a469c8eSJeff Roberson { 313*7a469c8eSJeff Roberson struct vm_domainset_iter di; 314*7a469c8eSJeff Roberson vm_offset_t addr; 315*7a469c8eSJeff Roberson int domain; 316*7a469c8eSJeff Roberson 317*7a469c8eSJeff Roberson KASSERT(vmem == kernel_arena, 318*7a469c8eSJeff Roberson ("kmem_alloc_contig: Only kernel_arena is supported.")); 319*7a469c8eSJeff Roberson 320*7a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 321*7a469c8eSJeff Roberson do { 322*7a469c8eSJeff Roberson addr = kmem_alloc_contig_domain(domain, size, flags, low, high, 323*7a469c8eSJeff Roberson alignment, boundary, memattr); 324*7a469c8eSJeff Roberson if (addr != 0) 325*7a469c8eSJeff Roberson break; 326*7a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 327*7a469c8eSJeff Roberson 328*7a469c8eSJeff Roberson return (addr); 329*7a469c8eSJeff Roberson } 330*7a469c8eSJeff Roberson 3310ff0fc84SAlan Cox /* 332df8bae1dSRodney W. Grimes * kmem_suballoc: 333df8bae1dSRodney W. Grimes * 334df8bae1dSRodney W. Grimes * Allocates a map to manage a subrange 335df8bae1dSRodney W. Grimes * of the kernel virtual address space. 336df8bae1dSRodney W. Grimes * 337df8bae1dSRodney W. Grimes * Arguments are as follows: 338df8bae1dSRodney W. Grimes * 339df8bae1dSRodney W. Grimes * parent Map to take range from 340df8bae1dSRodney W. Grimes * min, max Returned endpoints of map 341030f2369SAlfred Perlstein * size Size of range to find 3423202ed75SAlan Cox * superpage_align Request that min is superpage aligned 343df8bae1dSRodney W. Grimes */ 3440d94caffSDavid Greenman vm_map_t 3453202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max, 3463202ed75SAlan Cox vm_size_t size, boolean_t superpage_align) 347df8bae1dSRodney W. Grimes { 3486e4f51d1SAlfred Perlstein int ret; 349df8bae1dSRodney W. Grimes vm_map_t result; 35023955314SAlfred Perlstein 351df8bae1dSRodney W. Grimes size = round_page(size); 352df8bae1dSRodney W. Grimes 3532bc24aa9SAlan Cox *min = vm_map_min(parent); 354edb572a3SJohn Baldwin ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ? 3555aa60b6fSJohn Baldwin VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 3563364c323SKonstantin Belousov MAP_ACC_NO_CHARGE); 35724dedba9SAlan Cox if (ret != KERN_SUCCESS) 35824dedba9SAlan Cox panic("kmem_suballoc: bad status return of %d", ret); 359df8bae1dSRodney W. Grimes *max = *min + size; 3602d8acc0fSJohn Dyson result = vm_map_create(vm_map_pmap(parent), *min, *max); 361df8bae1dSRodney W. Grimes if (result == NULL) 362df8bae1dSRodney W. Grimes panic("kmem_suballoc: cannot create submap"); 3636e4f51d1SAlfred Perlstein if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS) 364df8bae1dSRodney W. Grimes panic("kmem_suballoc: unable to change range to submap"); 365df8bae1dSRodney W. Grimes return (result); 366df8bae1dSRodney W. Grimes } 367df8bae1dSRodney W. Grimes 368df8bae1dSRodney W. Grimes /* 3691c7c3c6aSMatthew Dillon * kmem_malloc: 3701c7c3c6aSMatthew Dillon * 3715df87b21SJeff Roberson * Allocate wired-down pages in the kernel's address space. 372df8bae1dSRodney W. Grimes */ 373df8bae1dSRodney W. Grimes vm_offset_t 374*7a469c8eSJeff Roberson kmem_malloc_domain(int domain, vm_size_t size, int flags) 375df8bae1dSRodney W. Grimes { 376*7a469c8eSJeff Roberson vmem_t *vmem; 377df8bae1dSRodney W. Grimes vm_offset_t addr; 3785df87b21SJeff Roberson int rv; 379df8bae1dSRodney W. Grimes 380*7a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 381df8bae1dSRodney W. Grimes size = round_page(size); 3825df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 3835df87b21SJeff Roberson return (0); 384df8bae1dSRodney W. Grimes 385*7a469c8eSJeff Roberson rv = kmem_back_domain(domain, kernel_object, addr, size, flags); 3865df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 3875df87b21SJeff Roberson vmem_free(vmem, addr, size); 388f31c239dSAlan Cox return (0); 389df8bae1dSRodney W. Grimes } 3905df87b21SJeff Roberson return (addr); 391e3813573SMatthew D Fleming } 392e3813573SMatthew D Fleming 393*7a469c8eSJeff Roberson vm_offset_t 394*7a469c8eSJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags) 395*7a469c8eSJeff Roberson { 396*7a469c8eSJeff Roberson struct vm_domainset_iter di; 397*7a469c8eSJeff Roberson vm_offset_t addr; 398*7a469c8eSJeff Roberson int domain; 399*7a469c8eSJeff Roberson 400*7a469c8eSJeff Roberson KASSERT(vmem == kernel_arena, 401*7a469c8eSJeff Roberson ("kmem_malloc: Only kernel_arena is supported.")); 402*7a469c8eSJeff Roberson 403*7a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 404*7a469c8eSJeff Roberson do { 405*7a469c8eSJeff Roberson addr = kmem_malloc_domain(domain, size, flags); 406*7a469c8eSJeff Roberson if (addr != 0) 407*7a469c8eSJeff Roberson break; 408*7a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 409*7a469c8eSJeff Roberson 410*7a469c8eSJeff Roberson return (addr); 411*7a469c8eSJeff Roberson } 412*7a469c8eSJeff Roberson 413e3813573SMatthew D Fleming /* 414e3813573SMatthew D Fleming * kmem_back: 415e3813573SMatthew D Fleming * 416e3813573SMatthew D Fleming * Allocate physical pages for the specified virtual address range. 417e3813573SMatthew D Fleming */ 418e3813573SMatthew D Fleming int 419*7a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr, 420*7a469c8eSJeff Roberson vm_size_t size, int flags) 421e3813573SMatthew D Fleming { 422e3813573SMatthew D Fleming vm_offset_t offset, i; 42333fff5d5SMark Johnston vm_page_t m, mpred; 424e3813573SMatthew D Fleming int pflags; 425e3813573SMatthew D Fleming 4262e47807cSJeff Roberson KASSERT(object == kernel_object, 427*7a469c8eSJeff Roberson ("kmem_back_domain: only supports kernel object.")); 4285df87b21SJeff Roberson 4290891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 4305df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 4318d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 4328d6fbbb8SJeff Roberson if (flags & M_WAITOK) 4338d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITFAIL; 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) { 440*7a469c8eSJeff Roberson m = vm_page_alloc_domain_after(object, atop(offset + i), 441*7a469c8eSJeff 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 } 455*7a469c8eSJeff Roberson KASSERT(vm_phys_domidx(m) == domain, 456*7a469c8eSJeff Roberson ("kmem_back_domain: Domain mismatch %d != %d", 457*7a469c8eSJeff Roberson vm_phys_domidx(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; 46339ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 46439ffa8c1SKonstantin Belousov VM_PROT_ALL | 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 471*7a469c8eSJeff Roberson int 472*7a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 473*7a469c8eSJeff Roberson { 474*7a469c8eSJeff Roberson struct vm_domainset_iter di; 475*7a469c8eSJeff Roberson int domain; 476*7a469c8eSJeff Roberson int ret; 477*7a469c8eSJeff Roberson 478*7a469c8eSJeff Roberson KASSERT(object == kernel_object, 479*7a469c8eSJeff Roberson ("kmem_back: only supports kernel object.")); 480*7a469c8eSJeff Roberson 481*7a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 482*7a469c8eSJeff Roberson do { 483*7a469c8eSJeff Roberson ret = kmem_back_domain(domain, object, addr, size, flags); 484*7a469c8eSJeff Roberson if (ret == KERN_SUCCESS) 485*7a469c8eSJeff Roberson break; 486*7a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 487*7a469c8eSJeff Roberson 488*7a469c8eSJeff Roberson return (ret); 489*7a469c8eSJeff Roberson } 490*7a469c8eSJeff 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 */ 500*7a469c8eSJeff Roberson static int 501*7a469c8eSJeff 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; 505*7a469c8eSJeff Roberson int domain; 5065df87b21SJeff Roberson 5072e47807cSJeff Roberson KASSERT(object == kernel_object, 5082e47807cSJeff Roberson ("kmem_unback: only supports kernel object.")); 5095df87b21SJeff Roberson 510*7a469c8eSJeff Roberson if (size == 0) 511*7a469c8eSJeff 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); 516*7a469c8eSJeff Roberson m = vm_page_lookup(object, atop(offset)); 517*7a469c8eSJeff Roberson domain = vm_phys_domidx(m); 518*7a469c8eSJeff 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); 524*7a469c8eSJeff Roberson 525*7a469c8eSJeff Roberson return (domain); 526*7a469c8eSJeff Roberson } 527*7a469c8eSJeff Roberson 528*7a469c8eSJeff Roberson void 529*7a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 530*7a469c8eSJeff Roberson { 531*7a469c8eSJeff Roberson 532*7a469c8eSJeff 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 { 544*7a469c8eSJeff Roberson int domain; 5455df87b21SJeff Roberson 5462e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 5472e47807cSJeff Roberson ("kmem_free: Only kernel_arena is supported.")); 5485df87b21SJeff Roberson size = round_page(size); 549*7a469c8eSJeff Roberson domain = _kmem_unback(kernel_object, addr, size); 550*7a469c8eSJeff Roberson vmem_free(vm_dom[domain].vmd_kernel_arena, addr, size); 5515df87b21SJeff Roberson } 5525df87b21SJeff Roberson 5535df87b21SJeff Roberson /* 5545df87b21SJeff Roberson * kmap_alloc_wait: 555df8bae1dSRodney W. Grimes * 556df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 557df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 558df8bae1dSRodney W. Grimes * 5591c7c3c6aSMatthew Dillon * This routine may block. 560df8bae1dSRodney W. Grimes */ 5610d94caffSDavid Greenman vm_offset_t 5626e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size) 563df8bae1dSRodney W. Grimes { 564df8bae1dSRodney W. Grimes vm_offset_t addr; 56523955314SAlfred Perlstein 566df8bae1dSRodney W. Grimes size = round_page(size); 5673364c323SKonstantin Belousov if (!swap_reserve(size)) 5683364c323SKonstantin Belousov return (0); 569df8bae1dSRodney W. Grimes 570df8bae1dSRodney W. Grimes for (;;) { 571df8bae1dSRodney W. Grimes /* 5720d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 5730d94caffSDavid Greenman * to lock out sleepers/wakers. 574df8bae1dSRodney W. Grimes */ 575df8bae1dSRodney W. Grimes vm_map_lock(map); 576e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 577df8bae1dSRodney W. Grimes break; 578df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 579df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 580df8bae1dSRodney W. Grimes vm_map_unlock(map); 5813364c323SKonstantin Belousov swap_release(size); 582df8bae1dSRodney W. Grimes return (0); 583df8bae1dSRodney W. Grimes } 5849688f931SAlan Cox map->needs_wakeup = TRUE; 5858ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 586df8bae1dSRodney W. Grimes } 5873364c323SKonstantin Belousov vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, 5883364c323SKonstantin Belousov VM_PROT_ALL, MAP_ACC_CHARGED); 589df8bae1dSRodney W. Grimes vm_map_unlock(map); 590df8bae1dSRodney W. Grimes return (addr); 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes 593df8bae1dSRodney W. Grimes /* 5945df87b21SJeff Roberson * kmap_free_wakeup: 595df8bae1dSRodney W. Grimes * 59624a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 597df8bae1dSRodney W. Grimes * waiting for memory in that map. 598df8bae1dSRodney W. Grimes */ 5990d94caffSDavid Greenman void 6006e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 601df8bae1dSRodney W. Grimes { 60223955314SAlfred Perlstein 603df8bae1dSRodney W. Grimes vm_map_lock(map); 604655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 6059688f931SAlan Cox if (map->needs_wakeup) { 6069688f931SAlan Cox map->needs_wakeup = FALSE; 6079688f931SAlan Cox vm_map_wakeup(map); 6089688f931SAlan Cox } 609df8bae1dSRodney W. Grimes vm_map_unlock(map); 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes 6125df87b21SJeff Roberson void 61389cb2a19SMatthew D Fleming kmem_init_zero_region(void) 61489cb2a19SMatthew D Fleming { 615cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 61689cb2a19SMatthew D Fleming vm_page_t m; 61789cb2a19SMatthew D Fleming 618cfb00e5aSMatthew D Fleming /* 619cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 620cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 621cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 622cfb00e5aSMatthew D Fleming */ 6235df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 624703dec68SAlan Cox m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | 62589cb2a19SMatthew D Fleming VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO); 62689cb2a19SMatthew D Fleming if ((m->flags & PG_ZERO) == 0) 62789cb2a19SMatthew D Fleming pmap_zero_page(m); 62889cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 62989cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 6305df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 63189cb2a19SMatthew D Fleming 63289cb2a19SMatthew D Fleming zero_region = (const void *)addr; 63389cb2a19SMatthew D Fleming } 63489cb2a19SMatthew D Fleming 635df8bae1dSRodney W. Grimes /* 6361c7c3c6aSMatthew Dillon * kmem_init: 6371c7c3c6aSMatthew Dillon * 6381c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 6391c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 6401c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 6411c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 642df8bae1dSRodney W. Grimes */ 6430d94caffSDavid Greenman void 6446e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end) 645df8bae1dSRodney W. Grimes { 646030f2369SAlfred Perlstein vm_map_t m; 647df8bae1dSRodney W. Grimes 6482d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 649c9267356SAlan Cox m->system_map = 1; 650df8bae1dSRodney W. Grimes vm_map_lock(m); 651df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 652df8bae1dSRodney W. Grimes kernel_map = m; 653c9267356SAlan Cox (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 6545cfa90e9SAlan Cox #ifdef __amd64__ 6555cfa90e9SAlan Cox KERNBASE, 6565cfa90e9SAlan Cox #else 6575cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 6585cfa90e9SAlan Cox #endif 6595cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 660df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 661df8bae1dSRodney W. Grimes vm_map_unlock(m); 662df8bae1dSRodney W. Grimes } 66386f08737SRobert Watson 6649309e63cSRobert Watson #ifdef DIAGNOSTIC 66586f08737SRobert Watson /* 66686f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 66786f08737SRobert Watson * purposes. 66886f08737SRobert Watson */ 66986f08737SRobert Watson static int 67086f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 67186f08737SRobert Watson { 67286f08737SRobert Watson int error, i; 67386f08737SRobert Watson 67486f08737SRobert Watson i = 0; 67586f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 67686f08737SRobert Watson if (error) 67786f08737SRobert Watson return (error); 6789b43bc27SAndriy Gapon if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 6799b43bc27SAndriy Gapon return (EINVAL); 6809b43bc27SAndriy Gapon if (i != 0) 6819b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, i); 68286f08737SRobert Watson return (0); 68386f08737SRobert Watson } 68486f08737SRobert Watson 68586f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 6869b43bc27SAndriy Gapon debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags"); 6879309e63cSRobert Watson #endif 688