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> 742b914b85SMark Johnston #include <sys/asan.h> 757a469c8eSJeff Roberson #include <sys/domainset.h> 760f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h> 772b914b85SMark Johnston #include <sys/kernel.h> 78fb919e4dSMark Murray #include <sys/lock.h> 79a1f6d91cSDavid Greenman #include <sys/malloc.h> 80540da48dSMark Johnston #include <sys/msan.h> 812b914b85SMark Johnston #include <sys/proc.h> 8289f6b863SAttilio Rao #include <sys/rwlock.h> 8386f08737SRobert Watson #include <sys/sysctl.h> 845df87b21SJeff Roberson #include <sys/vmem.h> 857a469c8eSJeff Roberson #include <sys/vmmeter.h> 86df8bae1dSRodney W. Grimes 87df8bae1dSRodney W. Grimes #include <vm/vm.h> 88efeaf95aSDavid Greenman #include <vm/vm_param.h> 897a469c8eSJeff Roberson #include <vm/vm_domainset.h> 905df87b21SJeff Roberson #include <vm/vm_kern.h> 91efeaf95aSDavid Greenman #include <vm/pmap.h> 92efeaf95aSDavid Greenman #include <vm/vm_map.h> 93efeaf95aSDavid Greenman #include <vm/vm_object.h> 94df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 95df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 96e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 97431fb8abSMark Johnston #include <vm/vm_phys.h> 9833fff5d5SMark Johnston #include <vm/vm_radix.h> 999b4288a3SBruce Evans #include <vm/vm_extern.h> 1000f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h> 101df8bae1dSRodney W. Grimes 1027dd979dfSMark Johnston struct vm_map kernel_map_store; 1037dd979dfSMark Johnston struct vm_map exec_map_store; 1047dd979dfSMark Johnston struct vm_map pipe_map_store; 105f23b4c91SGarrett Wollman 10689cb2a19SMatthew D Fleming const void *zero_region; 10789cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0); 10889cb2a19SMatthew D Fleming 109645743eaSJohn Baldwin /* NB: Used by kernel debuggers. */ 110645743eaSJohn Baldwin const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS; 111645743eaSJohn Baldwin 112ec492b13SMark Johnston u_int exec_map_entry_size; 113ec492b13SMark Johnston u_int exec_map_entries; 114ec492b13SMark Johnston 11594bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD, 116f0188618SHans Petter Selasky SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address"); 11794bfd5b1SMarius Strobl 11894bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD, 11958aa35d4SWarner Losh #if defined(__arm__) 12094bfd5b1SMarius Strobl &vm_max_kernel_address, 0, 12194bfd5b1SMarius Strobl #else 122f0188618SHans Petter Selasky SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS, 12394bfd5b1SMarius Strobl #endif 12494bfd5b1SMarius Strobl "Max kernel address"); 12594bfd5b1SMarius Strobl 1261aed6d48SMark Johnston #if VM_NRESERVLEVEL > 0 12725ed23cfSMark Johnston #define KVA_QUANTUM_SHIFT (VM_LEVEL_0_ORDER + PAGE_SHIFT) 1281aed6d48SMark Johnston #else 1292b601070SMark Johnston /* On non-superpage architectures we want large import sizes. */ 1302b601070SMark Johnston #define KVA_QUANTUM_SHIFT (8 + PAGE_SHIFT) 1311aed6d48SMark Johnston #endif 13223e875fdSMark Johnston #define KVA_QUANTUM (1ul << KVA_QUANTUM_SHIFT) 133aea9103eSMark Johnston #define KVA_NUMA_IMPORT_QUANTUM (KVA_QUANTUM * 128) 1341aed6d48SMark Johnston 135a81c400eSJeff Roberson extern void uma_startup2(void); 136a81c400eSJeff Roberson 137df8bae1dSRodney W. Grimes /* 1385df87b21SJeff Roberson * kva_alloc: 139a839bdc8SDmitrij Tejblum * 140b77c2bcdSAlan Cox * Allocate a virtual address range with no underlying object and 141b77c2bcdSAlan Cox * no initial mapping to physical memory. Any mapping from this 142b77c2bcdSAlan Cox * range to physical memory must be explicitly created prior to 143b77c2bcdSAlan Cox * its use, typically with pmap_qenter(). Any attempt to create 144b77c2bcdSAlan Cox * a mapping on demand through vm_fault() will result in a panic. 145a839bdc8SDmitrij Tejblum */ 146a839bdc8SDmitrij Tejblum vm_offset_t 1476e309d75SEd Maste kva_alloc(vm_size_t size) 148a839bdc8SDmitrij Tejblum { 149a839bdc8SDmitrij Tejblum vm_offset_t addr; 150a839bdc8SDmitrij Tejblum 151*b9fd884aSColin Percival TSENTER(); 152a839bdc8SDmitrij Tejblum size = round_page(size); 1535df87b21SJeff Roberson if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr)) 154a839bdc8SDmitrij Tejblum return (0); 155*b9fd884aSColin Percival TSEXIT(); 1565df87b21SJeff Roberson 157a839bdc8SDmitrij Tejblum return (addr); 158a839bdc8SDmitrij Tejblum } 159a839bdc8SDmitrij Tejblum 160a839bdc8SDmitrij Tejblum /* 1615df87b21SJeff Roberson * kva_free: 162ca596a25SJuli Mallett * 1635df87b21SJeff Roberson * Release a region of kernel virtual memory allocated 1645df87b21SJeff Roberson * with kva_alloc, and return the physical pages 1655df87b21SJeff Roberson * associated with that region. 1665df87b21SJeff Roberson * 1675df87b21SJeff Roberson * This routine may not block on kernel maps. 168ca596a25SJuli Mallett */ 1695df87b21SJeff Roberson void 1706e309d75SEd Maste kva_free(vm_offset_t addr, vm_size_t size) 171df8bae1dSRodney W. Grimes { 172df8bae1dSRodney W. Grimes 173df8bae1dSRodney W. Grimes size = round_page(size); 1745df87b21SJeff Roberson vmem_free(kernel_arena, addr, size); 175df8bae1dSRodney W. Grimes } 176df8bae1dSRodney W. Grimes 177540da48dSMark Johnston /* 178540da48dSMark Johnston * Update sanitizer shadow state to reflect a new allocation. Force inlining to 179540da48dSMark Johnston * help make KMSAN origin tracking more precise. 180540da48dSMark Johnston */ 181540da48dSMark Johnston static __always_inline void 182540da48dSMark Johnston kmem_alloc_san(vm_offset_t addr, vm_size_t size, vm_size_t asize, int flags) 183540da48dSMark Johnston { 184540da48dSMark Johnston if ((flags & M_ZERO) == 0) { 185540da48dSMark Johnston kmsan_mark((void *)addr, asize, KMSAN_STATE_UNINIT); 186540da48dSMark Johnston kmsan_orig((void *)addr, asize, KMSAN_TYPE_KMEM, 187540da48dSMark Johnston KMSAN_RET_ADDR); 188540da48dSMark Johnston } else { 189540da48dSMark Johnston kmsan_mark((void *)addr, asize, KMSAN_STATE_INITED); 190540da48dSMark Johnston } 191540da48dSMark Johnston kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE); 192540da48dSMark Johnston } 193540da48dSMark Johnston 19433655d95SMark Johnston static vm_page_t 19533655d95SMark Johnston kmem_alloc_contig_pages(vm_object_t object, vm_pindex_t pindex, int domain, 19633655d95SMark Johnston int pflags, u_long npages, vm_paddr_t low, vm_paddr_t high, 19733655d95SMark Johnston u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr) 19833655d95SMark Johnston { 19933655d95SMark Johnston vm_page_t m; 20033655d95SMark Johnston int tries; 201660344caSRyan Stone bool wait, reclaim; 20233655d95SMark Johnston 20333655d95SMark Johnston VM_OBJECT_ASSERT_WLOCKED(object); 20433655d95SMark Johnston 20533655d95SMark Johnston wait = (pflags & VM_ALLOC_WAITOK) != 0; 206660344caSRyan Stone reclaim = (pflags & VM_ALLOC_NORECLAIM) == 0; 20733655d95SMark Johnston pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 20833655d95SMark Johnston pflags |= VM_ALLOC_NOWAIT; 20933655d95SMark Johnston for (tries = wait ? 3 : 1;; tries--) { 21033655d95SMark Johnston m = vm_page_alloc_contig_domain(object, pindex, domain, pflags, 21133655d95SMark Johnston npages, low, high, alignment, boundary, memattr); 212660344caSRyan Stone if (m != NULL || tries == 0 || !reclaim) 21333655d95SMark Johnston break; 21433655d95SMark Johnston 21533655d95SMark Johnston VM_OBJECT_WUNLOCK(object); 21633655d95SMark Johnston if (!vm_page_reclaim_contig_domain(domain, pflags, npages, 21733655d95SMark Johnston low, high, alignment, boundary) && wait) 21833655d95SMark Johnston vm_wait_domain(domain); 21933655d95SMark Johnston VM_OBJECT_WLOCK(object); 22033655d95SMark Johnston } 22133655d95SMark Johnston return (m); 22233655d95SMark Johnston } 22333655d95SMark Johnston 224df8bae1dSRodney W. Grimes /* 2250ff0fc84SAlan Cox * Allocates a region from the kernel address map and physical pages 2260ff0fc84SAlan Cox * within the specified address range to the kernel object. Creates a 2270ff0fc84SAlan Cox * wired mapping from this region to these pages, and returns the 2280ff0fc84SAlan Cox * region's starting virtual address. The allocated pages are not 2290ff0fc84SAlan Cox * necessarily physically contiguous. If M_ZERO is specified through the 2300ff0fc84SAlan Cox * given flags, then the pages are zeroed before they are mapped. 2310ff0fc84SAlan Cox */ 2329978bd99SMark Johnston static vm_offset_t 2337a469c8eSJeff Roberson kmem_alloc_attr_domain(int domain, vm_size_t size, int flags, vm_paddr_t low, 2340ff0fc84SAlan Cox vm_paddr_t high, vm_memattr_t memattr) 2350ff0fc84SAlan Cox { 2367a469c8eSJeff Roberson vmem_t *vmem; 23733655d95SMark Johnston vm_object_t object; 238d1780e8dSKonstantin Belousov vm_offset_t addr, i, offset; 2390ff0fc84SAlan Cox vm_page_t m; 2402b914b85SMark Johnston vm_size_t asize; 24133655d95SMark Johnston int pflags; 242d22ff6e6SKonstantin Belousov vm_prot_t prot; 2430ff0fc84SAlan Cox 24433655d95SMark Johnston object = kernel_object; 2452b914b85SMark Johnston asize = round_page(size); 2467a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 2472b914b85SMark Johnston if (vmem_alloc(vmem, asize, M_BESTFIT | flags, &addr)) 2480ff0fc84SAlan Cox return (0); 2490ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 25091e31c3cSJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED; 251d22ff6e6SKonstantin Belousov prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW; 25289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2532b914b85SMark Johnston for (i = 0; i < asize; i += PAGE_SIZE) { 25433655d95SMark Johnston m = kmem_alloc_contig_pages(object, atop(offset + i), 2557a469c8eSJeff Roberson domain, pflags, 1, low, high, PAGE_SIZE, 0, memattr); 2560ff0fc84SAlan Cox if (m == NULL) { 25789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2589e829b22SAlan Cox kmem_unback(object, addr, i); 2592b914b85SMark Johnston vmem_free(vmem, addr, asize); 2600ff0fc84SAlan Cox return (0); 2610ff0fc84SAlan Cox } 262431fb8abSMark Johnston KASSERT(vm_page_domain(m) == domain, 2637a469c8eSJeff Roberson ("kmem_alloc_attr_domain: Domain mismatch %d != %d", 264431fb8abSMark Johnston vm_page_domain(m), domain)); 2650ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2660ff0fc84SAlan Cox pmap_zero_page(m); 26791e31c3cSJeff Roberson vm_page_valid(m); 268d22ff6e6SKonstantin Belousov pmap_enter(kernel_pmap, addr + i, m, prot, 269d22ff6e6SKonstantin Belousov prot | PMAP_ENTER_WIRED, 0); 2700ff0fc84SAlan Cox } 27189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 272540da48dSMark Johnston kmem_alloc_san(addr, size, asize, flags); 2730ff0fc84SAlan Cox return (addr); 2740ff0fc84SAlan Cox } 2750ff0fc84SAlan Cox 2767a469c8eSJeff Roberson vm_offset_t 277db7c2a48SAlan Cox kmem_alloc_attr(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high, 278db7c2a48SAlan Cox vm_memattr_t memattr) 2797a469c8eSJeff Roberson { 2809978bd99SMark Johnston 2819978bd99SMark Johnston return (kmem_alloc_attr_domainset(DOMAINSET_RR(), size, flags, low, 2829978bd99SMark Johnston high, memattr)); 2839978bd99SMark Johnston } 2849978bd99SMark Johnston 2859978bd99SMark Johnston vm_offset_t 2869978bd99SMark Johnston kmem_alloc_attr_domainset(struct domainset *ds, vm_size_t size, int flags, 2879978bd99SMark Johnston vm_paddr_t low, vm_paddr_t high, vm_memattr_t memattr) 2889978bd99SMark Johnston { 2897a469c8eSJeff Roberson struct vm_domainset_iter di; 2907a469c8eSJeff Roberson vm_offset_t addr; 2915fee468eSBryan Drewery int domain; 2927a469c8eSJeff Roberson 2935fee468eSBryan Drewery vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 2947a469c8eSJeff Roberson do { 2957a469c8eSJeff Roberson addr = kmem_alloc_attr_domain(domain, size, flags, low, high, 2967a469c8eSJeff Roberson memattr); 2977a469c8eSJeff Roberson if (addr != 0) 2987a469c8eSJeff Roberson break; 2994c29d2deSMark Johnston } while (vm_domainset_iter_policy(&di, &domain) == 0); 3007a469c8eSJeff Roberson 3017a469c8eSJeff Roberson return (addr); 3027a469c8eSJeff Roberson } 3037a469c8eSJeff Roberson 3040ff0fc84SAlan Cox /* 3050ff0fc84SAlan Cox * Allocates a region from the kernel address map and physically 3060ff0fc84SAlan Cox * contiguous pages within the specified address range to the kernel 3070ff0fc84SAlan Cox * object. Creates a wired mapping from this region to these pages, and 3080ff0fc84SAlan Cox * returns the region's starting virtual address. If M_ZERO is specified 3090ff0fc84SAlan Cox * through the given flags, then the pages are zeroed before they are 3100ff0fc84SAlan Cox * mapped. 3110ff0fc84SAlan Cox */ 3129978bd99SMark Johnston static vm_offset_t 3137a469c8eSJeff Roberson kmem_alloc_contig_domain(int domain, vm_size_t size, int flags, vm_paddr_t low, 3140ff0fc84SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 3150ff0fc84SAlan Cox vm_memattr_t memattr) 3160ff0fc84SAlan Cox { 3177a469c8eSJeff Roberson vmem_t *vmem; 31833655d95SMark Johnston vm_object_t object; 319d1780e8dSKonstantin Belousov vm_offset_t addr, offset, tmp; 3200ff0fc84SAlan Cox vm_page_t end_m, m; 3212b914b85SMark Johnston vm_size_t asize; 322c869e672SAlan Cox u_long npages; 32333655d95SMark Johnston int pflags; 3240ff0fc84SAlan Cox 32533655d95SMark Johnston object = kernel_object; 3262b914b85SMark Johnston asize = round_page(size); 3277a469c8eSJeff Roberson vmem = vm_dom[domain].vmd_kernel_arena; 3282b914b85SMark Johnston if (vmem_alloc(vmem, asize, flags | M_BESTFIT, &addr)) 3290ff0fc84SAlan Cox return (0); 3300ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 33191e31c3cSJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED; 3322b914b85SMark Johnston npages = atop(asize); 33389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 33433655d95SMark Johnston m = kmem_alloc_contig_pages(object, atop(offset), domain, 33533655d95SMark Johnston pflags, npages, low, high, alignment, boundary, memattr); 3360ff0fc84SAlan Cox if (m == NULL) { 33789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3382b914b85SMark Johnston vmem_free(vmem, addr, asize); 3390ff0fc84SAlan Cox return (0); 3400ff0fc84SAlan Cox } 341431fb8abSMark Johnston KASSERT(vm_page_domain(m) == domain, 3427a469c8eSJeff Roberson ("kmem_alloc_contig_domain: Domain mismatch %d != %d", 343431fb8abSMark Johnston vm_page_domain(m), domain)); 344c869e672SAlan Cox end_m = m + npages; 3455df87b21SJeff Roberson tmp = addr; 3460ff0fc84SAlan Cox for (; m < end_m; m++) { 3470ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 3480ff0fc84SAlan Cox pmap_zero_page(m); 34991e31c3cSJeff Roberson vm_page_valid(m); 3500766f278SJonathan T. Looney pmap_enter(kernel_pmap, tmp, m, VM_PROT_RW, 3510766f278SJonathan T. Looney VM_PROT_RW | PMAP_ENTER_WIRED, 0); 3525df87b21SJeff Roberson tmp += PAGE_SIZE; 3530ff0fc84SAlan Cox } 35489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 355540da48dSMark Johnston kmem_alloc_san(addr, size, asize, flags); 3560ff0fc84SAlan Cox return (addr); 3570ff0fc84SAlan Cox } 3580ff0fc84SAlan Cox 3597a469c8eSJeff Roberson vm_offset_t 36044d0efb2SAlan Cox kmem_alloc_contig(vm_size_t size, int flags, vm_paddr_t low, vm_paddr_t high, 36144d0efb2SAlan Cox u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr) 3627a469c8eSJeff Roberson { 3639978bd99SMark Johnston 3649978bd99SMark Johnston return (kmem_alloc_contig_domainset(DOMAINSET_RR(), size, flags, low, 3659978bd99SMark Johnston high, alignment, boundary, memattr)); 3669978bd99SMark Johnston } 3679978bd99SMark Johnston 3689978bd99SMark Johnston vm_offset_t 3699978bd99SMark Johnston kmem_alloc_contig_domainset(struct domainset *ds, vm_size_t size, int flags, 3709978bd99SMark Johnston vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 3719978bd99SMark Johnston vm_memattr_t memattr) 3729978bd99SMark Johnston { 3737a469c8eSJeff Roberson struct vm_domainset_iter di; 3747a469c8eSJeff Roberson vm_offset_t addr; 3755fee468eSBryan Drewery int domain; 3767a469c8eSJeff Roberson 3775fee468eSBryan Drewery vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 3787a469c8eSJeff Roberson do { 3797a469c8eSJeff Roberson addr = kmem_alloc_contig_domain(domain, size, flags, low, high, 3807a469c8eSJeff Roberson alignment, boundary, memattr); 3817a469c8eSJeff Roberson if (addr != 0) 3827a469c8eSJeff Roberson break; 3834c29d2deSMark Johnston } while (vm_domainset_iter_policy(&di, &domain) == 0); 3847a469c8eSJeff Roberson 3857a469c8eSJeff Roberson return (addr); 3867a469c8eSJeff Roberson } 3877a469c8eSJeff Roberson 3880ff0fc84SAlan Cox /* 3897dd979dfSMark Johnston * kmem_subinit: 390df8bae1dSRodney W. Grimes * 3917dd979dfSMark Johnston * Initializes a map to manage a subrange 392df8bae1dSRodney W. Grimes * of the kernel virtual address space. 393df8bae1dSRodney W. Grimes * 394df8bae1dSRodney W. Grimes * Arguments are as follows: 395df8bae1dSRodney W. Grimes * 396df8bae1dSRodney W. Grimes * parent Map to take range from 397df8bae1dSRodney W. Grimes * min, max Returned endpoints of map 398030f2369SAlfred Perlstein * size Size of range to find 3993202ed75SAlan Cox * superpage_align Request that min is superpage aligned 400df8bae1dSRodney W. Grimes */ 4017dd979dfSMark Johnston void 4027dd979dfSMark Johnston kmem_subinit(vm_map_t map, vm_map_t parent, vm_offset_t *min, vm_offset_t *max, 4037dd979dfSMark Johnston vm_size_t size, bool superpage_align) 404df8bae1dSRodney W. Grimes { 4056e4f51d1SAlfred Perlstein int ret; 40623955314SAlfred Perlstein 407df8bae1dSRodney W. Grimes size = round_page(size); 408df8bae1dSRodney W. Grimes 4092bc24aa9SAlan Cox *min = vm_map_min(parent); 410edb572a3SJohn Baldwin ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ? 4115aa60b6fSJohn Baldwin VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 4123364c323SKonstantin Belousov MAP_ACC_NO_CHARGE); 41324dedba9SAlan Cox if (ret != KERN_SUCCESS) 4147dd979dfSMark Johnston panic("kmem_subinit: bad status return of %d", ret); 415df8bae1dSRodney W. Grimes *max = *min + size; 4167dd979dfSMark Johnston vm_map_init(map, vm_map_pmap(parent), *min, *max); 4177dd979dfSMark Johnston if (vm_map_submap(parent, *min, *max, map) != KERN_SUCCESS) 4187dd979dfSMark Johnston panic("kmem_subinit: unable to change range to submap"); 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes 421df8bae1dSRodney W. Grimes /* 4229978bd99SMark Johnston * kmem_malloc_domain: 4231c7c3c6aSMatthew Dillon * 4245df87b21SJeff Roberson * Allocate wired-down pages in the kernel's address space. 425df8bae1dSRodney W. Grimes */ 4269978bd99SMark Johnston static vm_offset_t 427067fd858SAlan Cox kmem_malloc_domain(int domain, vm_size_t size, int flags) 428df8bae1dSRodney W. Grimes { 4290766f278SJonathan T. Looney vmem_t *arena; 430df8bae1dSRodney W. Grimes vm_offset_t addr; 4312b914b85SMark Johnston vm_size_t asize; 4325df87b21SJeff Roberson int rv; 433df8bae1dSRodney W. Grimes 434067fd858SAlan Cox if (__predict_true((flags & M_EXEC) == 0)) 4350766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_arena; 4360766f278SJonathan T. Looney else 4370766f278SJonathan T. Looney arena = vm_dom[domain].vmd_kernel_rwx_arena; 4382b914b85SMark Johnston asize = round_page(size); 4392b914b85SMark Johnston if (vmem_alloc(arena, asize, flags | M_BESTFIT, &addr)) 4405df87b21SJeff Roberson return (0); 441df8bae1dSRodney W. Grimes 4422b914b85SMark Johnston rv = kmem_back_domain(domain, kernel_object, addr, asize, flags); 4435df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 4442b914b85SMark Johnston vmem_free(arena, addr, asize); 445f31c239dSAlan Cox return (0); 446df8bae1dSRodney W. Grimes } 4472b914b85SMark Johnston kasan_mark((void *)addr, size, asize, KASAN_KMEM_REDZONE); 4485df87b21SJeff Roberson return (addr); 449e3813573SMatthew D Fleming } 450e3813573SMatthew D Fleming 4517a469c8eSJeff Roberson vm_offset_t 45283a90bffSAlan Cox kmem_malloc(vm_size_t size, int flags) 4537a469c8eSJeff Roberson { 4549978bd99SMark Johnston 4559978bd99SMark Johnston return (kmem_malloc_domainset(DOMAINSET_RR(), size, flags)); 4569978bd99SMark Johnston } 4579978bd99SMark Johnston 4589978bd99SMark Johnston vm_offset_t 4599978bd99SMark Johnston kmem_malloc_domainset(struct domainset *ds, vm_size_t size, int flags) 4609978bd99SMark Johnston { 4617a469c8eSJeff Roberson struct vm_domainset_iter di; 4627a469c8eSJeff Roberson vm_offset_t addr; 4637a469c8eSJeff Roberson int domain; 4647a469c8eSJeff Roberson 4659978bd99SMark Johnston vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 4667a469c8eSJeff Roberson do { 467067fd858SAlan Cox addr = kmem_malloc_domain(domain, size, flags); 4687a469c8eSJeff Roberson if (addr != 0) 4697a469c8eSJeff Roberson break; 4704c29d2deSMark Johnston } while (vm_domainset_iter_policy(&di, &domain) == 0); 4717a469c8eSJeff Roberson 4727a469c8eSJeff Roberson return (addr); 4737a469c8eSJeff Roberson } 4747a469c8eSJeff Roberson 475e3813573SMatthew D Fleming /* 47625ed23cfSMark Johnston * kmem_back_domain: 477e3813573SMatthew D Fleming * 47825ed23cfSMark Johnston * Allocate physical pages from the specified domain for the specified 47925ed23cfSMark Johnston * virtual address range. 480e3813573SMatthew D Fleming */ 481e3813573SMatthew D Fleming int 4827a469c8eSJeff Roberson kmem_back_domain(int domain, vm_object_t object, vm_offset_t addr, 4837a469c8eSJeff Roberson vm_size_t size, int flags) 484e3813573SMatthew D Fleming { 485e3813573SMatthew D Fleming vm_offset_t offset, i; 48633fff5d5SMark Johnston vm_page_t m, mpred; 4870766f278SJonathan T. Looney vm_prot_t prot; 488e3813573SMatthew D Fleming int pflags; 489e3813573SMatthew D Fleming 4902e47807cSJeff Roberson KASSERT(object == kernel_object, 4917a469c8eSJeff Roberson ("kmem_back_domain: only supports kernel object.")); 4925df87b21SJeff Roberson 4930891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 49491e31c3cSJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_WIRED; 4958d6fbbb8SJeff Roberson pflags &= ~(VM_ALLOC_NOWAIT | VM_ALLOC_WAITOK | VM_ALLOC_WAITFAIL); 4968d6fbbb8SJeff Roberson if (flags & M_WAITOK) 4978d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITFAIL; 4980766f278SJonathan T. Looney prot = (flags & M_EXEC) != 0 ? VM_PROT_ALL : VM_PROT_RW; 499df8bae1dSRodney W. Grimes 50033fff5d5SMark Johnston i = 0; 50133fff5d5SMark Johnston VM_OBJECT_WLOCK(object); 5028d6fbbb8SJeff Roberson retry: 50333fff5d5SMark Johnston mpred = vm_radix_lookup_le(&object->rtree, atop(offset + i)); 50433fff5d5SMark Johnston for (; i < size; i += PAGE_SIZE, mpred = m) { 5057a469c8eSJeff Roberson m = vm_page_alloc_domain_after(object, atop(offset + i), 5067a469c8eSJeff Roberson domain, pflags, mpred); 507df8bae1dSRodney W. Grimes 508df8bae1dSRodney W. Grimes /* 5090d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 5100d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 5110d94caffSDavid Greenman * aren't on any queues. 512df8bae1dSRodney W. Grimes */ 513df8bae1dSRodney W. Grimes if (m == NULL) { 5148d6fbbb8SJeff Roberson if ((flags & M_NOWAIT) == 0) 515b18bfc3dSJohn Dyson goto retry; 5168d6fbbb8SJeff Roberson VM_OBJECT_WUNLOCK(object); 5179e829b22SAlan Cox kmem_unback(object, addr, i); 518e3813573SMatthew D Fleming return (KERN_NO_SPACE); 519df8bae1dSRodney W. Grimes } 520431fb8abSMark Johnston KASSERT(vm_page_domain(m) == domain, 5217a469c8eSJeff Roberson ("kmem_back_domain: Domain mismatch %d != %d", 522431fb8abSMark Johnston vm_page_domain(m), domain)); 5231e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 524fff6062aSAlan Cox pmap_zero_page(m); 525d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) != 0, 5269f5c801bSAlan Cox ("kmem_malloc: page %p is managed", m)); 52791e31c3cSJeff Roberson vm_page_valid(m); 5280766f278SJonathan T. Looney pmap_enter(kernel_pmap, addr + i, m, prot, 5290766f278SJonathan T. Looney prot | PMAP_ENTER_WIRED, 0); 53049bfa624SAlan Cox if (__predict_false((prot & VM_PROT_EXECUTE) != 0)) 53149bfa624SAlan Cox m->oflags |= VPO_KMEM_EXEC; 532df8bae1dSRodney W. Grimes } 5335df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 534540da48dSMark Johnston kmem_alloc_san(addr, size, size, flags); 535e3813573SMatthew D Fleming return (KERN_SUCCESS); 536df8bae1dSRodney W. Grimes } 537df8bae1dSRodney W. Grimes 53825ed23cfSMark Johnston /* 53925ed23cfSMark Johnston * kmem_back: 54025ed23cfSMark Johnston * 54125ed23cfSMark Johnston * Allocate physical pages for the specified virtual address range. 54225ed23cfSMark Johnston */ 5437a469c8eSJeff Roberson int 5447a469c8eSJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 5457a469c8eSJeff Roberson { 54625ed23cfSMark Johnston vm_offset_t end, next, start; 54725ed23cfSMark Johnston int domain, rv; 5487a469c8eSJeff Roberson 5497a469c8eSJeff Roberson KASSERT(object == kernel_object, 5507a469c8eSJeff Roberson ("kmem_back: only supports kernel object.")); 5517a469c8eSJeff Roberson 55225ed23cfSMark Johnston for (start = addr, end = addr + size; addr < end; addr = next) { 55325ed23cfSMark Johnston /* 55425ed23cfSMark Johnston * We must ensure that pages backing a given large virtual page 55525ed23cfSMark Johnston * all come from the same physical domain. 55625ed23cfSMark Johnston */ 55725ed23cfSMark Johnston if (vm_ndomains > 1) { 55825ed23cfSMark Johnston domain = (addr >> KVA_QUANTUM_SHIFT) % vm_ndomains; 55930c5525bSAndrew Gallatin while (VM_DOMAIN_EMPTY(domain)) 56030c5525bSAndrew Gallatin domain++; 56125ed23cfSMark Johnston next = roundup2(addr + 1, KVA_QUANTUM); 56225ed23cfSMark Johnston if (next > end || next < start) 56325ed23cfSMark Johnston next = end; 5643d14a7bbSMark Johnston } else { 5653d14a7bbSMark Johnston domain = 0; 56625ed23cfSMark Johnston next = end; 5673d14a7bbSMark Johnston } 56825ed23cfSMark Johnston rv = kmem_back_domain(domain, object, addr, next - addr, flags); 56925ed23cfSMark Johnston if (rv != KERN_SUCCESS) { 57025ed23cfSMark Johnston kmem_unback(object, start, addr - start); 5717a469c8eSJeff Roberson break; 57225ed23cfSMark Johnston } 57325ed23cfSMark Johnston } 57425ed23cfSMark Johnston return (rv); 5757a469c8eSJeff Roberson } 5767a469c8eSJeff Roberson 5779e829b22SAlan Cox /* 5789e829b22SAlan Cox * kmem_unback: 5799e829b22SAlan Cox * 5809e829b22SAlan Cox * Unmap and free the physical pages underlying the specified virtual 5819e829b22SAlan Cox * address range. 5829e829b22SAlan Cox * 5839e829b22SAlan Cox * A physical page must exist within the specified object at each index 5849e829b22SAlan Cox * that is being unmapped. 5859e829b22SAlan Cox */ 58649bfa624SAlan Cox static struct vmem * 5877a469c8eSJeff Roberson _kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 5885df87b21SJeff Roberson { 58949bfa624SAlan Cox struct vmem *arena; 5907e05ffa6SMark Johnston vm_page_t m, next; 5917e05ffa6SMark Johnston vm_offset_t end, offset; 5927a469c8eSJeff Roberson int domain; 5935df87b21SJeff Roberson 5942e47807cSJeff Roberson KASSERT(object == kernel_object, 5952e47807cSJeff Roberson ("kmem_unback: only supports kernel object.")); 5965df87b21SJeff Roberson 5977a469c8eSJeff Roberson if (size == 0) 59849bfa624SAlan Cox return (NULL); 599fa2f411cSAlan Cox pmap_remove(kernel_pmap, addr, addr + size); 6005df87b21SJeff Roberson offset = addr - VM_MIN_KERNEL_ADDRESS; 6017e05ffa6SMark Johnston end = offset + size; 6025df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 6037a469c8eSJeff Roberson m = vm_page_lookup(object, atop(offset)); 604431fb8abSMark Johnston domain = vm_page_domain(m); 60549bfa624SAlan Cox if (__predict_true((m->oflags & VPO_KMEM_EXEC) == 0)) 60649bfa624SAlan Cox arena = vm_dom[domain].vmd_kernel_arena; 60749bfa624SAlan Cox else 60849bfa624SAlan Cox arena = vm_dom[domain].vmd_kernel_rwx_arena; 6097a469c8eSJeff Roberson for (; offset < end; offset += PAGE_SIZE, m = next) { 6107e05ffa6SMark Johnston next = vm_page_next(m); 611e9ceb9ddSJeff Roberson vm_page_xbusy_claim(m); 61288ea538aSMark Johnston vm_page_unwire_noq(m); 6135df87b21SJeff Roberson vm_page_free(m); 6145df87b21SJeff Roberson } 6155df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 6167a469c8eSJeff Roberson 61749bfa624SAlan Cox return (arena); 6187a469c8eSJeff Roberson } 6197a469c8eSJeff Roberson 6207a469c8eSJeff Roberson void 6217a469c8eSJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 6227a469c8eSJeff Roberson { 6237a469c8eSJeff Roberson 62449bfa624SAlan Cox (void)_kmem_unback(object, addr, size); 6255df87b21SJeff Roberson } 6265df87b21SJeff Roberson 627df8bae1dSRodney W. Grimes /* 6285df87b21SJeff Roberson * kmem_free: 6295df87b21SJeff Roberson * 6305df87b21SJeff Roberson * Free memory allocated with kmem_malloc. The size must match the 6315df87b21SJeff Roberson * original allocation. 6325df87b21SJeff Roberson */ 6335df87b21SJeff Roberson void 63449bfa624SAlan Cox kmem_free(vm_offset_t addr, vm_size_t size) 6355df87b21SJeff Roberson { 6360766f278SJonathan T. Looney struct vmem *arena; 6370766f278SJonathan T. Looney 6385df87b21SJeff Roberson size = round_page(size); 6392b914b85SMark Johnston kasan_mark((void *)addr, size, size, 0); 64049bfa624SAlan Cox arena = _kmem_unback(kernel_object, addr, size); 64149bfa624SAlan Cox if (arena != NULL) 6420766f278SJonathan T. Looney vmem_free(arena, addr, size); 6435df87b21SJeff Roberson } 6445df87b21SJeff Roberson 6455df87b21SJeff Roberson /* 6465df87b21SJeff Roberson * kmap_alloc_wait: 647df8bae1dSRodney W. Grimes * 648df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 649df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 650df8bae1dSRodney W. Grimes * 6511c7c3c6aSMatthew Dillon * This routine may block. 652df8bae1dSRodney W. Grimes */ 6530d94caffSDavid Greenman vm_offset_t 6546e309d75SEd Maste kmap_alloc_wait(vm_map_t map, vm_size_t size) 655df8bae1dSRodney W. Grimes { 656df8bae1dSRodney W. Grimes vm_offset_t addr; 65723955314SAlfred Perlstein 658df8bae1dSRodney W. Grimes size = round_page(size); 6593364c323SKonstantin Belousov if (!swap_reserve(size)) 6603364c323SKonstantin Belousov return (0); 661df8bae1dSRodney W. Grimes 662df8bae1dSRodney W. Grimes for (;;) { 663df8bae1dSRodney W. Grimes /* 6640d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 6650d94caffSDavid Greenman * to lock out sleepers/wakers. 666df8bae1dSRodney W. Grimes */ 667df8bae1dSRodney W. Grimes vm_map_lock(map); 6689f701172SKonstantin Belousov addr = vm_map_findspace(map, vm_map_min(map), size); 6699f701172SKonstantin Belousov if (addr + size <= vm_map_max(map)) 670df8bae1dSRodney W. Grimes break; 671df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 672df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 673df8bae1dSRodney W. Grimes vm_map_unlock(map); 6743364c323SKonstantin Belousov swap_release(size); 675df8bae1dSRodney W. Grimes return (0); 676df8bae1dSRodney W. Grimes } 6779688f931SAlan Cox map->needs_wakeup = TRUE; 6788ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 679df8bae1dSRodney W. Grimes } 68007702f72SMark Johnston vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_RW, VM_PROT_RW, 68107702f72SMark Johnston MAP_ACC_CHARGED); 682df8bae1dSRodney W. Grimes vm_map_unlock(map); 683df8bae1dSRodney W. Grimes return (addr); 684df8bae1dSRodney W. Grimes } 685df8bae1dSRodney W. Grimes 686df8bae1dSRodney W. Grimes /* 6875df87b21SJeff Roberson * kmap_free_wakeup: 688df8bae1dSRodney W. Grimes * 68924a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 690df8bae1dSRodney W. Grimes * waiting for memory in that map. 691df8bae1dSRodney W. Grimes */ 6920d94caffSDavid Greenman void 6936e309d75SEd Maste kmap_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size) 694df8bae1dSRodney W. Grimes { 69523955314SAlfred Perlstein 696df8bae1dSRodney W. Grimes vm_map_lock(map); 697655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 6989688f931SAlan Cox if (map->needs_wakeup) { 6999688f931SAlan Cox map->needs_wakeup = FALSE; 7009688f931SAlan Cox vm_map_wakeup(map); 7019688f931SAlan Cox } 702df8bae1dSRodney W. Grimes vm_map_unlock(map); 703df8bae1dSRodney W. Grimes } 704df8bae1dSRodney W. Grimes 7055df87b21SJeff Roberson void 70689cb2a19SMatthew D Fleming kmem_init_zero_region(void) 70789cb2a19SMatthew D Fleming { 708cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 70989cb2a19SMatthew D Fleming vm_page_t m; 71089cb2a19SMatthew D Fleming 711cfb00e5aSMatthew D Fleming /* 712cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 713cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 714cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 715cfb00e5aSMatthew D Fleming */ 7165df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 717a4667e09SMark Johnston m = vm_page_alloc_noobj(VM_ALLOC_WIRED | VM_ALLOC_ZERO); 71889cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 71989cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 7205df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 72189cb2a19SMatthew D Fleming 72289cb2a19SMatthew D Fleming zero_region = (const void *)addr; 72389cb2a19SMatthew D Fleming } 72489cb2a19SMatthew D Fleming 725df8bae1dSRodney W. Grimes /* 726969e147aSMark Johnston * Import KVA from the kernel map into the kernel arena. 7271aed6d48SMark Johnston */ 7281aed6d48SMark Johnston static int 7291aed6d48SMark Johnston kva_import(void *unused, vmem_size_t size, int flags, vmem_addr_t *addrp) 7301aed6d48SMark Johnston { 7311aed6d48SMark Johnston vm_offset_t addr; 7321aed6d48SMark Johnston int result; 7331aed6d48SMark Johnston 7341aed6d48SMark Johnston KASSERT((size % KVA_QUANTUM) == 0, 7351aed6d48SMark Johnston ("kva_import: Size %jd is not a multiple of %d", 7361aed6d48SMark Johnston (intmax_t)size, (int)KVA_QUANTUM)); 7371aed6d48SMark Johnston addr = vm_map_min(kernel_map); 7381aed6d48SMark Johnston result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0, 7391aed6d48SMark Johnston VMFS_SUPER_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 7401aed6d48SMark Johnston if (result != KERN_SUCCESS) 7411aed6d48SMark Johnston return (ENOMEM); 7421aed6d48SMark Johnston 7431aed6d48SMark Johnston *addrp = addr; 7441aed6d48SMark Johnston 7451aed6d48SMark Johnston return (0); 7461aed6d48SMark Johnston } 7471aed6d48SMark Johnston 7481aed6d48SMark Johnston /* 749969e147aSMark Johnston * Import KVA from a parent arena into a per-domain arena. Imports must be 750969e147aSMark Johnston * KVA_QUANTUM-aligned and a multiple of KVA_QUANTUM in size. 7511aed6d48SMark Johnston */ 7521aed6d48SMark Johnston static int 753969e147aSMark Johnston kva_import_domain(void *arena, vmem_size_t size, int flags, vmem_addr_t *addrp) 7541aed6d48SMark Johnston { 7551aed6d48SMark Johnston 7561aed6d48SMark Johnston KASSERT((size % KVA_QUANTUM) == 0, 757969e147aSMark Johnston ("kva_import_domain: Size %jd is not a multiple of %d", 7581aed6d48SMark Johnston (intmax_t)size, (int)KVA_QUANTUM)); 7591aed6d48SMark Johnston return (vmem_xalloc(arena, size, KVA_QUANTUM, 0, 0, VMEM_ADDR_MIN, 7601aed6d48SMark Johnston VMEM_ADDR_MAX, flags, addrp)); 7611aed6d48SMark Johnston } 7621aed6d48SMark Johnston 7631aed6d48SMark Johnston /* 7641c7c3c6aSMatthew Dillon * kmem_init: 7651c7c3c6aSMatthew Dillon * 7661c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 7671c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 7681c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 7691c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 7701aed6d48SMark Johnston * Create the kernel vmem arena and its per-domain children. 771df8bae1dSRodney W. Grimes */ 7720d94caffSDavid Greenman void 7736e309d75SEd Maste kmem_init(vm_offset_t start, vm_offset_t end) 774df8bae1dSRodney W. Grimes { 775aea9103eSMark Johnston vm_size_t quantum; 7761aed6d48SMark Johnston int domain; 777df8bae1dSRodney W. Grimes 7787efe14cbSMark Johnston vm_map_init(kernel_map, kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 7797efe14cbSMark Johnston kernel_map->system_map = 1; 7807efe14cbSMark Johnston vm_map_lock(kernel_map); 781df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 7827efe14cbSMark Johnston (void)vm_map_insert(kernel_map, NULL, 0, 7835cfa90e9SAlan Cox #ifdef __amd64__ 7845cfa90e9SAlan Cox KERNBASE, 7855cfa90e9SAlan Cox #else 7865cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 7875cfa90e9SAlan Cox #endif 7885cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 789df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 7909d75f0dcSMark Johnston 7919d75f0dcSMark Johnston #ifdef __amd64__ 7929d75f0dcSMark Johnston /* 7939d75f0dcSMark Johnston * Mark KVA used for the page array as allocated. Other platforms 7949d75f0dcSMark Johnston * that handle vm_page_array allocation can simply adjust virtual_avail 7959d75f0dcSMark Johnston * instead. 7969d75f0dcSMark Johnston */ 7977dd979dfSMark Johnston (void)vm_map_insert(kernel_map, NULL, 0, (vm_offset_t)vm_page_array, 7989d75f0dcSMark Johnston (vm_offset_t)vm_page_array + round_2mpage(vm_page_array_size * 7999d75f0dcSMark Johnston sizeof(struct vm_page)), 8009d75f0dcSMark Johnston VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT); 8019d75f0dcSMark Johnston #endif 8027dd979dfSMark Johnston vm_map_unlock(kernel_map); 8031aed6d48SMark Johnston 8041aed6d48SMark Johnston /* 805aea9103eSMark Johnston * Use a large import quantum on NUMA systems. This helps minimize 806aea9103eSMark Johnston * interleaving of superpages, reducing internal fragmentation within 807aea9103eSMark Johnston * the per-domain arenas. 808aea9103eSMark Johnston */ 809aea9103eSMark Johnston if (vm_ndomains > 1 && PMAP_HAS_DMAP) 810aea9103eSMark Johnston quantum = KVA_NUMA_IMPORT_QUANTUM; 811aea9103eSMark Johnston else 812aea9103eSMark Johnston quantum = KVA_QUANTUM; 813aea9103eSMark Johnston 814aea9103eSMark Johnston /* 8151aed6d48SMark Johnston * Initialize the kernel_arena. This can grow on demand. 8161aed6d48SMark Johnston */ 8171aed6d48SMark Johnston vmem_init(kernel_arena, "kernel arena", 0, 0, PAGE_SIZE, 0, 0); 818aea9103eSMark Johnston vmem_set_import(kernel_arena, kva_import, NULL, NULL, quantum); 8191aed6d48SMark Johnston 8201aed6d48SMark Johnston for (domain = 0; domain < vm_ndomains; domain++) { 821969e147aSMark Johnston /* 822969e147aSMark Johnston * Initialize the per-domain arenas. These are used to color 823969e147aSMark Johnston * the KVA space in a way that ensures that virtual large pages 824969e147aSMark Johnston * are backed by memory from the same physical domain, 825969e147aSMark Johnston * maximizing the potential for superpage promotion. 826969e147aSMark Johnston */ 8271aed6d48SMark Johnston vm_dom[domain].vmd_kernel_arena = vmem_create( 8281aed6d48SMark Johnston "kernel arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK); 8291aed6d48SMark Johnston vmem_set_import(vm_dom[domain].vmd_kernel_arena, 830aea9103eSMark Johnston kva_import_domain, NULL, kernel_arena, quantum); 831969e147aSMark Johnston 832969e147aSMark Johnston /* 833969e147aSMark Johnston * In architectures with superpages, maintain separate arenas 834969e147aSMark Johnston * for allocations with permissions that differ from the 835969e147aSMark Johnston * "standard" read/write permissions used for kernel memory, 836969e147aSMark Johnston * so as not to inhibit superpage promotion. 837aea9103eSMark Johnston * 838aea9103eSMark Johnston * Use the base import quantum since this arena is rarely used. 839969e147aSMark Johnston */ 8401aed6d48SMark Johnston #if VM_NRESERVLEVEL > 0 8411aed6d48SMark Johnston vm_dom[domain].vmd_kernel_rwx_arena = vmem_create( 8421aed6d48SMark Johnston "kernel rwx arena domain", 0, 0, PAGE_SIZE, 0, M_WAITOK); 8431aed6d48SMark Johnston vmem_set_import(vm_dom[domain].vmd_kernel_rwx_arena, 844969e147aSMark Johnston kva_import_domain, (vmem_release_t *)vmem_xfree, 845969e147aSMark Johnston kernel_arena, KVA_QUANTUM); 846303b7702SMark Johnston #else 847303b7702SMark Johnston vm_dom[domain].vmd_kernel_rwx_arena = 848303b7702SMark Johnston vm_dom[domain].vmd_kernel_arena; 8491aed6d48SMark Johnston #endif 8501aed6d48SMark Johnston } 851a81c400eSJeff Roberson 852a81c400eSJeff Roberson /* 853a81c400eSJeff Roberson * This must be the very first call so that the virtual address 854a81c400eSJeff Roberson * space used for early allocations is properly marked used in 855a81c400eSJeff Roberson * the map. 856a81c400eSJeff Roberson */ 857a81c400eSJeff Roberson uma_startup2(); 858df8bae1dSRodney W. Grimes } 85986f08737SRobert Watson 860483f692eSMark Johnston /* 861483f692eSMark Johnston * kmem_bootstrap_free: 862483f692eSMark Johnston * 863483f692eSMark Johnston * Free pages backing preloaded data (e.g., kernel modules) to the 864483f692eSMark Johnston * system. Currently only supported on platforms that create a 865483f692eSMark Johnston * vm_phys segment for preloaded data. 866483f692eSMark Johnston */ 867483f692eSMark Johnston void 868483f692eSMark Johnston kmem_bootstrap_free(vm_offset_t start, vm_size_t size) 869483f692eSMark Johnston { 870483f692eSMark Johnston #if defined(__i386__) || defined(__amd64__) 871483f692eSMark Johnston struct vm_domain *vmd; 8726c85795aSMark Johnston vm_offset_t end, va; 873483f692eSMark Johnston vm_paddr_t pa; 874483f692eSMark Johnston vm_page_t m; 875483f692eSMark Johnston 876483f692eSMark Johnston end = trunc_page(start + size); 877483f692eSMark Johnston start = round_page(start); 878483f692eSMark Johnston 879f822c9e2SMark Johnston #ifdef __amd64__ 880f822c9e2SMark Johnston /* 881f822c9e2SMark Johnston * Preloaded files do not have execute permissions by default on amd64. 882f822c9e2SMark Johnston * Restore the default permissions to ensure that the direct map alias 883f822c9e2SMark Johnston * is updated. 884f822c9e2SMark Johnston */ 885f822c9e2SMark Johnston pmap_change_prot(start, end - start, VM_PROT_RW); 886f822c9e2SMark Johnston #endif 8876c85795aSMark Johnston for (va = start; va < end; va += PAGE_SIZE) { 8886c85795aSMark Johnston pa = pmap_kextract(va); 889483f692eSMark Johnston m = PHYS_TO_VM_PAGE(pa); 890483f692eSMark Johnston 891483f692eSMark Johnston vmd = vm_pagequeue_domain(m); 892483f692eSMark Johnston vm_domain_free_lock(vmd); 893483f692eSMark Johnston vm_phys_free_pages(m, 0); 894483f692eSMark Johnston vm_domain_free_unlock(vmd); 895c16bd872SMark Johnston 896c16bd872SMark Johnston vm_domain_freecnt_inc(vmd, 1); 897c16bd872SMark Johnston vm_cnt.v_page_count++; 898483f692eSMark Johnston } 8996c85795aSMark Johnston pmap_remove(kernel_pmap, start, end); 9006c85795aSMark Johnston (void)vmem_add(kernel_arena, start, end - start, M_WAITOK); 901483f692eSMark Johnston #endif 902483f692eSMark Johnston } 903483f692eSMark Johnston 90486f08737SRobert Watson /* 90586f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 90686f08737SRobert Watson * purposes. 90786f08737SRobert Watson */ 90886f08737SRobert Watson static int 90986f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 91086f08737SRobert Watson { 91186f08737SRobert Watson int error, i; 91286f08737SRobert Watson 91386f08737SRobert Watson i = 0; 91486f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 91551a7be5fSKonstantin Belousov if (error != 0) 91686f08737SRobert Watson return (error); 9179b43bc27SAndriy Gapon if ((i & ~(VM_LOW_KMEM | VM_LOW_PAGES)) != 0) 9189b43bc27SAndriy Gapon return (EINVAL); 9199b43bc27SAndriy Gapon if (i != 0) 9209b43bc27SAndriy Gapon EVENTHANDLER_INVOKE(vm_lowmem, i); 92186f08737SRobert Watson return (0); 92286f08737SRobert Watson } 92351a7be5fSKonstantin Belousov SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, 92451a7be5fSKonstantin Belousov CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_vm_lowmem, "I", 92551a7be5fSKonstantin Belousov "set to trigger vm_lowmem event with given flags"); 92689619b74SKonstantin Belousov 92789619b74SKonstantin Belousov static int 92889619b74SKonstantin Belousov debug_uma_reclaim(SYSCTL_HANDLER_ARGS) 92989619b74SKonstantin Belousov { 93089619b74SKonstantin Belousov int error, i; 93189619b74SKonstantin Belousov 93289619b74SKonstantin Belousov i = 0; 93389619b74SKonstantin Belousov error = sysctl_handle_int(oidp, &i, 0, req); 934aabe13f1SMark Johnston if (error != 0 || req->newptr == NULL) 93589619b74SKonstantin Belousov return (error); 93689619b74SKonstantin Belousov if (i != UMA_RECLAIM_TRIM && i != UMA_RECLAIM_DRAIN && 93789619b74SKonstantin Belousov i != UMA_RECLAIM_DRAIN_CPU) 93889619b74SKonstantin Belousov return (EINVAL); 93989619b74SKonstantin Belousov uma_reclaim(i); 94089619b74SKonstantin Belousov return (0); 94189619b74SKonstantin Belousov } 94289619b74SKonstantin Belousov SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim, 94389619b74SKonstantin Belousov CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, debug_uma_reclaim, "I", 94489619b74SKonstantin Belousov "set to generate request to reclaim uma caches"); 945aabe13f1SMark Johnston 946aabe13f1SMark Johnston static int 947aabe13f1SMark Johnston debug_uma_reclaim_domain(SYSCTL_HANDLER_ARGS) 948aabe13f1SMark Johnston { 949aabe13f1SMark Johnston int domain, error, request; 950aabe13f1SMark Johnston 951aabe13f1SMark Johnston request = 0; 952aabe13f1SMark Johnston error = sysctl_handle_int(oidp, &request, 0, req); 953aabe13f1SMark Johnston if (error != 0 || req->newptr == NULL) 954aabe13f1SMark Johnston return (error); 955aabe13f1SMark Johnston 956aabe13f1SMark Johnston domain = request >> 4; 957aabe13f1SMark Johnston request &= 0xf; 958aabe13f1SMark Johnston if (request != UMA_RECLAIM_TRIM && request != UMA_RECLAIM_DRAIN && 959aabe13f1SMark Johnston request != UMA_RECLAIM_DRAIN_CPU) 960aabe13f1SMark Johnston return (EINVAL); 961aabe13f1SMark Johnston if (domain < 0 || domain >= vm_ndomains) 962aabe13f1SMark Johnston return (EINVAL); 963aabe13f1SMark Johnston uma_reclaim_domain(request, domain); 964aabe13f1SMark Johnston return (0); 965aabe13f1SMark Johnston } 966aabe13f1SMark Johnston SYSCTL_PROC(_debug, OID_AUTO, uma_reclaim_domain, 967aabe13f1SMark Johnston CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, 0, 968aabe13f1SMark Johnston debug_uma_reclaim_domain, "I", 969aabe13f1SMark Johnston ""); 970