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> 95*e2068d0bSJeff 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) 200*e2068d0bSJeff 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 } 209*e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 2107a469c8eSJeff Roberson ("kmem_alloc_attr_domain: Domain mismatch %d != %d", 211*e2068d0bSJeff 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; 21539ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 21639ffa8c1SKonstantin Belousov VM_PROT_ALL | 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) 284*e2068d0bSJeff 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 } 292*e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 2937a469c8eSJeff Roberson ("kmem_alloc_contig_domain: Domain mismatch %d != %d", 294*e2068d0bSJeff 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; 30139ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL, 30239ffa8c1SKonstantin Belousov VM_PROT_ALL | 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 3757a469c8eSJeff Roberson kmem_malloc_domain(int domain, vm_size_t size, int flags) 376df8bae1dSRodney W. Grimes { 3777a469c8eSJeff Roberson vmem_t *vmem; 378df8bae1dSRodney W. Grimes vm_offset_t addr; 3795df87b21SJeff Roberson int rv; 380df8bae1dSRodney W. Grimes 3817a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 382df8bae1dSRodney W. Grimes size = round_page(size); 3835df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 3845df87b21SJeff Roberson return (0); 385df8bae1dSRodney W. Grimes 3867a469c8eSJeff Roberson rv = kmem_back_domain(domain, kernel_object, addr, size, flags); 3875df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 3885df87b21SJeff Roberson vmem_free(vmem, addr, size); 389f31c239dSAlan Cox return (0); 390df8bae1dSRodney W. Grimes } 3915df87b21SJeff Roberson return (addr); 392e3813573SMatthew D Fleming } 393e3813573SMatthew D Fleming 3947a469c8eSJeff Roberson vm_offset_t 3957a469c8eSJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags) 3967a469c8eSJeff Roberson { 3977a469c8eSJeff Roberson struct vm_domainset_iter di; 3987a469c8eSJeff Roberson vm_offset_t addr; 3997a469c8eSJeff Roberson int domain; 4007a469c8eSJeff Roberson 4017a469c8eSJeff Roberson KASSERT(vmem == kernel_arena, 4027a469c8eSJeff Roberson ("kmem_malloc: Only kernel_arena is supported.")); 4037a469c8eSJeff Roberson 4047a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 4057a469c8eSJeff Roberson do { 4067a469c8eSJeff Roberson addr = kmem_malloc_domain(domain, size, flags); 4077a469c8eSJeff Roberson if (addr != 0) 4087a469c8eSJeff Roberson break; 4097a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 4107a469c8eSJeff Roberson 4117a469c8eSJeff Roberson return (addr); 4127a469c8eSJeff Roberson } 4137a469c8eSJeff Roberson 414e3813573SMatthew D Fleming /* 415e3813573SMatthew D Fleming * kmem_back: 416e3813573SMatthew D Fleming * 417e3813573SMatthew D Fleming * Allocate physical pages for the specified virtual address range. 418e3813573SMatthew D Fleming */ 419e3813573SMatthew D Fleming int 4207a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr, 4217a469c8eSJeff Roberson vm_size_t size, int flags) 422e3813573SMatthew D Fleming { 423e3813573SMatthew D Fleming vm_offset_t offset, i; 42433fff5d5SMark Johnston vm_page_t m, mpred; 425e3813573SMatthew D Fleming int pflags; 426e3813573SMatthew D Fleming 4272e47807cSJeff Roberson KASSERT(object == kernel_object, 4287a469c8eSJeff Roberson ("kmem_back_domain: only supports kernel object.")); 4295df87b21SJeff Roberson 4300891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 4315df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 4328d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 4338d6fbbb8SJeff Roberson if (flags & M_WAITOK) 4348d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITFAIL; 435df8bae1dSRodney W. Grimes 43633fff5d5SMark Johnston i = 0; 43733fff5d5SMark Johnston VM_OBJECT_WLOCK(object); 4388d6fbbb8SJeff Roberson retry: 43933fff5d5SMark Johnston mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i)); 44033fff5d5SMark Johnston for (; i < size; i += PAGE_SIZE, mpred = m) { 4417a469c8eSJeff Roberson m = vm_page_alloc_domain_after(object, atop(offset + i), 4427a469c8eSJeff Roberson domain, pflags, mpred); 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes /* 4450d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 4460d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 4470d94caffSDavid Greenman * aren't on any queues. 448df8bae1dSRodney W. Grimes */ 449df8bae1dSRodney W. Grimes if (m == NULL) { 4508d6fbbb8SJeff Roberson if ((flags & M_NOWAIT) == 0) 451b18bfc3dSJohn Dyson goto retry; 4528d6fbbb8SJeff Roberson VM_OBJECT_WUNLOCK(object); 4539e829b22SAlan Cox kmem_unback(object, addr, i); 454e3813573SMatthew D Fleming return (KERN_NO_SPACE); 455df8bae1dSRodney W. Grimes } 456*e2068d0bSJeff Roberson KASSERT(vm_phys_domain(m) == domain, 4577a469c8eSJeff Roberson ("kmem_back_domain: Domain mismatch %d != %d", 458*e2068d0bSJeff Roberson vm_phys_domain(m), domain)); 4591e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 460fff6062aSAlan Cox pmap_zero_page(m); 461d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) != 0, 4629f5c801bSAlan Cox ("kmem_malloc: page %p is managed", m)); 4635df87b21SJeff Roberson m->valid = VM_PAGE_BITS_ALL; 46439ffa8c1SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 46539ffa8c1SKonstantin Belousov VM_PROT_ALL | PMAP_ENTER_WIRED, 0); 466df8bae1dSRodney W. Grimes } 4675df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 468df8bae1dSRodney W. Grimes 469e3813573SMatthew D Fleming return (KERN_SUCCESS); 470df8bae1dSRodney W. Grimes } 471df8bae1dSRodney W. Grimes 4727a469c8eSJeff Roberson int 4737a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 4747a469c8eSJeff Roberson { 4757a469c8eSJeff Roberson struct vm_domainset_iter di; 4767a469c8eSJeff Roberson int domain; 4777a469c8eSJeff Roberson int ret; 4787a469c8eSJeff Roberson 4797a469c8eSJeff Roberson KASSERT(object == kernel_object, 4807a469c8eSJeff Roberson ("kmem_back: only supports kernel object.")); 4817a469c8eSJeff Roberson 4827a469c8eSJeff Roberson vm_domainset_iter_malloc_init(&di, kernel_object, &domain, &flags); 4837a469c8eSJeff Roberson do { 4847a469c8eSJeff Roberson ret = kmem_back_domain(domain, object, addr, size, flags); 4857a469c8eSJeff Roberson if (ret == KERN_SUCCESS) 4867a469c8eSJeff Roberson break; 4877a469c8eSJeff Roberson } while (vm_domainset_iter_malloc(&di, &domain, &flags) == 0); 4887a469c8eSJeff Roberson 4897a469c8eSJeff Roberson return (ret); 4907a469c8eSJeff Roberson } 4917a469c8eSJeff Roberson 4929e829b22SAlan Cox /* 4939e829b22SAlan Cox * kmem_unback: 4949e829b22SAlan Cox * 4959e829b22SAlan Cox * Unmap and free the physical pages underlying the specified virtual 4969e829b22SAlan Cox * address range. 4979e829b22SAlan Cox * 4989e829b22SAlan Cox * A physical page must exist within the specified object at each index 4999e829b22SAlan Cox * that is being unmapped. 5009e829b22SAlan Cox */ 5017a469c8eSJeff Roberson static int 5027a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5035df87b21SJeff Roberson { 5047e05ffa6SMark Johnston vm_page_t m, next; 5057e05ffa6SMark Johnston vm_offset_t end, offset; 5067a469c8eSJeff Roberson int domain; 5075df87b21SJeff Roberson 5082e47807cSJeff Roberson KASSERT(object == kernel_object, 5092e47807cSJeff Roberson ("kmem_unback: only supports kernel object.")); 5105df87b21SJeff Roberson 5117a469c8eSJeff Roberson if (size == 0) 5127a469c8eSJeff Roberson return (0); 513fa2f411cSAlan Cox pmap_remove(kernel_pmap, addr, addr + size); 5145df87b21SJeff Roberson offset = addr - VM_MIN_KERNEL_ADDRESS; 5157e05ffa6SMark Johnston end = offset + size; 5165df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 5177a469c8eSJeff Roberson m = vm_page_lookup(object, atop(offset)); 518*e2068d0bSJeff Roberson domain = vm_phys_domain(m); 5197a469c8eSJeff Roberson for (; offset < end; offset += PAGE_SIZE, m = next) { 5207e05ffa6SMark Johnston next = vm_page_next(m); 52127e9ed8aSAlan Cox vm_page_unwire(m, PQ_NONE); 5225df87b21SJeff Roberson vm_page_free(m); 5235df87b21SJeff Roberson } 5245df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 5257a469c8eSJeff Roberson 5267a469c8eSJeff Roberson return (domain); 5277a469c8eSJeff Roberson } 5287a469c8eSJeff Roberson 5297a469c8eSJeff Roberson void 5307a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5317a469c8eSJeff Roberson { 5327a469c8eSJeff Roberson 5337a469c8eSJeff Roberson _kmem_unback(object, addr, size); 5345df87b21SJeff Roberson } 5355df87b21SJeff Roberson 536df8bae1dSRodney W. Grimes /* 5375df87b21SJeff Roberson * kmem_free: 5385df87b21SJeff Roberson * 5395df87b21SJeff Roberson * Free memory allocated with kmem_malloc. The size must match the 5405df87b21SJeff Roberson * original allocation. 5415df87b21SJeff Roberson */ 5425df87b21SJeff Roberson void 5435df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size) 5445df87b21SJeff Roberson { 5457a469c8eSJeff Roberson int domain; 5465df87b21SJeff Roberson 5472e47807cSJeff Roberson KASSERT(vmem == kernel_arena, 5482e47807cSJeff Roberson ("kmem_free: Only kernel_arena is supported.")); 5495df87b21SJeff Roberson size = round_page(size); 5507a469c8eSJeff Roberson domain = _kmem_unback(kernel_object, addr, size); 5517a469c8eSJeff Roberson vmem_free(vm_dom[domain].vmd_kernel_arena, addr, size); 5525df87b21SJeff Roberson } 5535df87b21SJeff Roberson 5545df87b21SJeff Roberson /* 5555df87b21SJeff Roberson * kmap_alloc_wait: 556df8bae1dSRodney W. Grimes * 557df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 558df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 559df8bae1dSRodney W. Grimes * 5601c7c3c6aSMatthew Dillon * This routine may block. 561df8bae1dSRodney W. Grimes */ 5620d94caffSDavid Greenman vm_offset_t 5636e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size) 564df8bae1dSRodney W. Grimes { 565df8bae1dSRodney W. Grimes vm_offset_t addr; 56623955314SAlfred Perlstein 567df8bae1dSRodney W. Grimes size = round_page(size); 5683364c323SKonstantin Belousov if (!swap_reserve(size)) 5693364c323SKonstantin Belousov return (0); 570df8bae1dSRodney W. Grimes 571df8bae1dSRodney W. Grimes for (;;) { 572df8bae1dSRodney W. Grimes /* 5730d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 5740d94caffSDavid Greenman * to lock out sleepers/wakers. 575df8bae1dSRodney W. Grimes */ 576df8bae1dSRodney W. Grimes vm_map_lock(map); 577e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 578df8bae1dSRodney W. Grimes break; 579df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 580df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 581df8bae1dSRodney W. Grimes vm_map_unlock(map); 5823364c323SKonstantin Belousov swap_release(size); 583df8bae1dSRodney W. Grimes return (0); 584df8bae1dSRodney W. Grimes } 5859688f931SAlan Cox map->needs_wakeup = TRUE; 5868ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 587df8bae1dSRodney W. Grimes } 5883364c323SKonstantin Belousov vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, 5893364c323SKonstantin Belousov VM_PROT_ALL, MAP_ACC_CHARGED); 590df8bae1dSRodney W. Grimes vm_map_unlock(map); 591df8bae1dSRodney W. Grimes return (addr); 592df8bae1dSRodney W. Grimes } 593df8bae1dSRodney W. Grimes 594df8bae1dSRodney W. Grimes /* 5955df87b21SJeff Roberson * kmap_free_wakeup: 596df8bae1dSRodney W. Grimes * 59724a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 598df8bae1dSRodney W. Grimes * waiting for memory in that map. 599df8bae1dSRodney W. Grimes */ 6000d94caffSDavid Greenman void 6016e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 602df8bae1dSRodney W. Grimes { 60323955314SAlfred Perlstein 604df8bae1dSRodney W. Grimes vm_map_lock(map); 605655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 6069688f931SAlan Cox if (map->needs_wakeup) { 6079688f931SAlan Cox map->needs_wakeup = FALSE; 6089688f931SAlan Cox vm_map_wakeup(map); 6099688f931SAlan Cox } 610df8bae1dSRodney W. Grimes vm_map_unlock(map); 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes 6135df87b21SJeff Roberson void 61489cb2a19SMatthew D Fleming kmem_init_zero_region(void) 61589cb2a19SMatthew D Fleming { 616cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 61789cb2a19SMatthew D Fleming vm_page_t m; 61889cb2a19SMatthew D Fleming 619cfb00e5aSMatthew D Fleming /* 620cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 621cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 622cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 623cfb00e5aSMatthew D Fleming */ 6245df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 625703dec68SAlan Cox m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | 62689cb2a19SMatthew D Fleming VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO); 62789cb2a19SMatthew D Fleming if ((m->flags & PG_ZERO) == 0) 62889cb2a19SMatthew D Fleming pmap_zero_page(m); 62989cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 63089cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 6315df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 63289cb2a19SMatthew D Fleming 63389cb2a19SMatthew D Fleming zero_region = (const void *)addr; 63489cb2a19SMatthew D Fleming } 63589cb2a19SMatthew D Fleming 636df8bae1dSRodney W. Grimes /* 6371c7c3c6aSMatthew Dillon * kmem_init: 6381c7c3c6aSMatthew Dillon * 6391c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 6401c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 6411c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 6421c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 643df8bae1dSRodney W. Grimes */ 6440d94caffSDavid Greenman void 6456e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end) 646df8bae1dSRodney W. Grimes { 647030f2369SAlfred Perlstein vm_map_t m; 648df8bae1dSRodney W. Grimes 6492d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 650c9267356SAlan Cox m->system_map = 1; 651df8bae1dSRodney W. Grimes vm_map_lock(m); 652df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 653df8bae1dSRodney W. Grimes kernel_map = m; 654c9267356SAlan Cox (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 6555cfa90e9SAlan Cox #ifdef __amd64__ 6565cfa90e9SAlan Cox KERNBASE, 6575cfa90e9SAlan Cox #else 6585cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 6595cfa90e9SAlan Cox #endif 6605cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 661df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 662df8bae1dSRodney W. Grimes vm_map_unlock(m); 663df8bae1dSRodney W. Grimes } 66486f08737SRobert Watson 6659309e63cSRobert Watson #ifdef DIAGNOSTIC 66686f08737SRobert Watson /* 66786f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 66886f08737SRobert Watson * purposes. 66986f08737SRobert Watson */ 67086f08737SRobert Watson static int 67186f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 67286f08737SRobert Watson { 67386f08737SRobert Watson int error, i; 67486f08737SRobert Watson 67586f08737SRobert Watson i = 0; 67686f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 67786f08737SRobert Watson if (error) 67886f08737SRobert Watson return (error); 6799b43bc27SAndriy Gapon if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 6809b43bc27SAndriy Gapon return (EINVAL); 6819b43bc27SAndriy Gapon if (i != 0) 6829b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, i); 68386f08737SRobert Watson return (0); 68486f08737SRobert Watson } 68586f08737SRobert Watson 68686f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 6879b43bc27SAndriy Gapon debug_vm_lowmem, "I", "set to trigger vm_lowmem event with given flags"); 6889309e63cSRobert Watson #endif 689