160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 17df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 18df8bae1dSRodney W. Grimes * without specific prior written permission. 19df8bae1dSRodney W. Grimes * 20df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df8bae1dSRodney W. Grimes * SUCH DAMAGE. 31df8bae1dSRodney W. Grimes * 323c4dd356SDavid Greenman * from: @(#)vm_kern.c 8.3 (Berkeley) 1/12/94 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes /* 62df8bae1dSRodney W. Grimes * Kernel memory management. 63df8bae1dSRodney W. Grimes */ 64df8bae1dSRodney W. Grimes 65874651b1SDavid E. O'Brien #include <sys/cdefs.h> 66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 67874651b1SDavid E. O'Brien 68df8bae1dSRodney W. Grimes #include <sys/param.h> 69df8bae1dSRodney W. Grimes #include <sys/systm.h> 7060363fb9SLuigi Rizzo #include <sys/kernel.h> /* for ticks and hz */ 710f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h> 72fb919e4dSMark Murray #include <sys/lock.h> 73f23b4c91SGarrett Wollman #include <sys/proc.h> 74a1f6d91cSDavid Greenman #include <sys/malloc.h> 7589f6b863SAttilio Rao #include <sys/rwlock.h> 7686f08737SRobert Watson #include <sys/sysctl.h> 775df87b21SJeff Roberson #include <sys/vmem.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 80efeaf95aSDavid Greenman #include <vm/vm_param.h> 815df87b21SJeff Roberson #include <vm/vm_kern.h> 82efeaf95aSDavid Greenman #include <vm/pmap.h> 83efeaf95aSDavid Greenman #include <vm/vm_map.h> 84efeaf95aSDavid Greenman #include <vm/vm_object.h> 85df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 86df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 879b4288a3SBruce Evans #include <vm/vm_extern.h> 880f2c2ce0SPawel Jakub Dawidek #include <vm/uma.h> 89df8bae1dSRodney W. Grimes 90e7788a47SKonstantin Belousov vm_map_t kernel_map; 91e7788a47SKonstantin Belousov vm_map_t exec_map; 92cebde069SMike Silbersack vm_map_t pipe_map; 93f23b4c91SGarrett Wollman 9489cb2a19SMatthew D Fleming const void *zero_region; 9589cb2a19SMatthew D Fleming CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0); 9689cb2a19SMatthew D Fleming 9794bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD, 9894bfd5b1SMarius Strobl NULL, VM_MIN_KERNEL_ADDRESS, "Min kernel address"); 9994bfd5b1SMarius Strobl 10094bfd5b1SMarius Strobl SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD, 101fc23011bSAlan Cox #if defined(__arm__) || defined(__sparc64__) 10294bfd5b1SMarius Strobl &vm_max_kernel_address, 0, 10394bfd5b1SMarius Strobl #else 10494bfd5b1SMarius Strobl NULL, VM_MAX_KERNEL_ADDRESS, 10594bfd5b1SMarius Strobl #endif 10694bfd5b1SMarius Strobl "Max kernel address"); 10794bfd5b1SMarius Strobl 108df8bae1dSRodney W. Grimes /* 1095df87b21SJeff Roberson * kva_alloc: 110a839bdc8SDmitrij Tejblum * 111b77c2bcdSAlan Cox * Allocate a virtual address range with no underlying object and 112b77c2bcdSAlan Cox * no initial mapping to physical memory. Any mapping from this 113b77c2bcdSAlan Cox * range to physical memory must be explicitly created prior to 114b77c2bcdSAlan Cox * its use, typically with pmap_qenter(). Any attempt to create 115b77c2bcdSAlan Cox * a mapping on demand through vm_fault() will result in a panic. 116a839bdc8SDmitrij Tejblum */ 117a839bdc8SDmitrij Tejblum vm_offset_t 1185df87b21SJeff Roberson kva_alloc(size) 119030f2369SAlfred Perlstein vm_size_t size; 120a839bdc8SDmitrij Tejblum { 121a839bdc8SDmitrij Tejblum vm_offset_t addr; 122a839bdc8SDmitrij Tejblum 123a839bdc8SDmitrij Tejblum size = round_page(size); 1245df87b21SJeff Roberson if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr)) 125a839bdc8SDmitrij Tejblum return (0); 1265df87b21SJeff Roberson 127a839bdc8SDmitrij Tejblum return (addr); 128a839bdc8SDmitrij Tejblum } 129a839bdc8SDmitrij Tejblum 130a839bdc8SDmitrij Tejblum /* 1315df87b21SJeff Roberson * kva_free: 132ca596a25SJuli Mallett * 1335df87b21SJeff Roberson * Release a region of kernel virtual memory allocated 1345df87b21SJeff Roberson * with kva_alloc, and return the physical pages 1355df87b21SJeff Roberson * associated with that region. 1365df87b21SJeff Roberson * 1375df87b21SJeff Roberson * This routine may not block on kernel maps. 138ca596a25SJuli Mallett */ 1395df87b21SJeff Roberson void 1405df87b21SJeff Roberson kva_free(addr, size) 141ca596a25SJuli Mallett vm_offset_t addr; 142030f2369SAlfred Perlstein vm_size_t size; 143df8bae1dSRodney W. Grimes { 144df8bae1dSRodney W. Grimes 145df8bae1dSRodney W. Grimes size = round_page(size); 1465df87b21SJeff Roberson vmem_free(kernel_arena, addr, size); 147df8bae1dSRodney W. Grimes } 148df8bae1dSRodney W. Grimes 149df8bae1dSRodney W. Grimes /* 1500ff0fc84SAlan Cox * Allocates a region from the kernel address map and physical pages 1510ff0fc84SAlan Cox * within the specified address range to the kernel object. Creates a 1520ff0fc84SAlan Cox * wired mapping from this region to these pages, and returns the 1530ff0fc84SAlan Cox * region's starting virtual address. The allocated pages are not 1540ff0fc84SAlan Cox * necessarily physically contiguous. If M_ZERO is specified through the 1550ff0fc84SAlan Cox * given flags, then the pages are zeroed before they are mapped. 1560ff0fc84SAlan Cox */ 1570ff0fc84SAlan Cox vm_offset_t 1585df87b21SJeff Roberson kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low, 1590ff0fc84SAlan Cox vm_paddr_t high, vm_memattr_t memattr) 1600ff0fc84SAlan Cox { 1615df87b21SJeff Roberson vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object; 1620ff0fc84SAlan Cox vm_offset_t addr; 1635df87b21SJeff Roberson vm_ooffset_t offset; 1640ff0fc84SAlan Cox vm_page_t m; 1650ff0fc84SAlan Cox int pflags, tries; 1665df87b21SJeff Roberson int i; 1670ff0fc84SAlan Cox 1680ff0fc84SAlan Cox size = round_page(size); 1695df87b21SJeff Roberson if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr)) 1700ff0fc84SAlan Cox return (0); 1710ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 1725df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 17389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1745df87b21SJeff Roberson for (i = 0; i < size; i += PAGE_SIZE) { 1750ff0fc84SAlan Cox tries = 0; 1760ff0fc84SAlan Cox retry: 1775df87b21SJeff Roberson m = vm_page_alloc_contig(object, OFF_TO_IDX(offset + i), 1785df87b21SJeff Roberson pflags, 1, low, high, PAGE_SIZE, 0, memattr); 1790ff0fc84SAlan Cox if (m == NULL) { 18089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 1810ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 18285eeca35SAlan Cox vm_pageout_grow_cache(tries, low, high); 18389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 1840ff0fc84SAlan Cox tries++; 1850ff0fc84SAlan Cox goto retry; 1860ff0fc84SAlan Cox } 1870ff0fc84SAlan Cox /* 1885df87b21SJeff Roberson * Unmap and free the pages. 1890ff0fc84SAlan Cox */ 1905df87b21SJeff Roberson if (i != 0) 1915df87b21SJeff Roberson pmap_remove(kernel_pmap, addr, addr + i); 1925df87b21SJeff Roberson while (i != 0) { 1935df87b21SJeff Roberson i -= PAGE_SIZE; 1945df87b21SJeff Roberson m = vm_page_lookup(object, 1955df87b21SJeff Roberson OFF_TO_IDX(offset + i)); 1965df87b21SJeff Roberson vm_page_unwire(m, 0); 1975df87b21SJeff Roberson vm_page_free(m); 1985df87b21SJeff Roberson } 1995df87b21SJeff Roberson vmem_free(vmem, addr, size); 2000ff0fc84SAlan Cox return (0); 2010ff0fc84SAlan Cox } 2020ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2030ff0fc84SAlan Cox pmap_zero_page(m); 2040ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 2055df87b21SJeff Roberson pmap_enter(kernel_pmap, addr + i, VM_PROT_ALL, m, VM_PROT_ALL, 2065df87b21SJeff Roberson TRUE); 2070ff0fc84SAlan Cox } 20889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2090ff0fc84SAlan Cox return (addr); 2100ff0fc84SAlan Cox } 2110ff0fc84SAlan Cox 2120ff0fc84SAlan Cox /* 2130ff0fc84SAlan Cox * Allocates a region from the kernel address map and physically 2140ff0fc84SAlan Cox * contiguous pages within the specified address range to the kernel 2150ff0fc84SAlan Cox * object. Creates a wired mapping from this region to these pages, and 2160ff0fc84SAlan Cox * returns the region's starting virtual address. If M_ZERO is specified 2170ff0fc84SAlan Cox * through the given flags, then the pages are zeroed before they are 2180ff0fc84SAlan Cox * mapped. 2190ff0fc84SAlan Cox */ 2200ff0fc84SAlan Cox vm_offset_t 2215df87b21SJeff Roberson kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low, 2220ff0fc84SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 2230ff0fc84SAlan Cox vm_memattr_t memattr) 2240ff0fc84SAlan Cox { 2255df87b21SJeff Roberson vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object; 2265df87b21SJeff Roberson vm_offset_t addr, tmp; 2270ff0fc84SAlan Cox vm_ooffset_t offset; 2280ff0fc84SAlan Cox vm_page_t end_m, m; 2290ff0fc84SAlan Cox int pflags, tries; 2300ff0fc84SAlan Cox 2310ff0fc84SAlan Cox size = round_page(size); 2325df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 2330ff0fc84SAlan Cox return (0); 2340ff0fc84SAlan Cox offset = addr - VM_MIN_KERNEL_ADDRESS; 2355df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 23689f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2370ff0fc84SAlan Cox tries = 0; 2380ff0fc84SAlan Cox retry: 2390ff0fc84SAlan Cox m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags, 2400ff0fc84SAlan Cox atop(size), low, high, alignment, boundary, memattr); 2410ff0fc84SAlan Cox if (m == NULL) { 24289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2430ff0fc84SAlan Cox if (tries < ((flags & M_NOWAIT) != 0 ? 1 : 3)) { 24485eeca35SAlan Cox vm_pageout_grow_cache(tries, low, high); 24589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 2460ff0fc84SAlan Cox tries++; 2470ff0fc84SAlan Cox goto retry; 2480ff0fc84SAlan Cox } 2495df87b21SJeff Roberson vmem_free(vmem, addr, size); 2500ff0fc84SAlan Cox return (0); 2510ff0fc84SAlan Cox } 2520ff0fc84SAlan Cox end_m = m + atop(size); 2535df87b21SJeff Roberson tmp = addr; 2540ff0fc84SAlan Cox for (; m < end_m; m++) { 2550ff0fc84SAlan Cox if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0) 2560ff0fc84SAlan Cox pmap_zero_page(m); 2570ff0fc84SAlan Cox m->valid = VM_PAGE_BITS_ALL; 2585df87b21SJeff Roberson pmap_enter(kernel_pmap, tmp, VM_PROT_ALL, m, VM_PROT_ALL, true); 2595df87b21SJeff Roberson tmp += PAGE_SIZE; 2600ff0fc84SAlan Cox } 26189f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 2620ff0fc84SAlan Cox return (addr); 2630ff0fc84SAlan Cox } 2640ff0fc84SAlan Cox 2650ff0fc84SAlan Cox /* 266df8bae1dSRodney W. Grimes * kmem_suballoc: 267df8bae1dSRodney W. Grimes * 268df8bae1dSRodney W. Grimes * Allocates a map to manage a subrange 269df8bae1dSRodney W. Grimes * of the kernel virtual address space. 270df8bae1dSRodney W. Grimes * 271df8bae1dSRodney W. Grimes * Arguments are as follows: 272df8bae1dSRodney W. Grimes * 273df8bae1dSRodney W. Grimes * parent Map to take range from 274df8bae1dSRodney W. Grimes * min, max Returned endpoints of map 275030f2369SAlfred Perlstein * size Size of range to find 2763202ed75SAlan Cox * superpage_align Request that min is superpage aligned 277df8bae1dSRodney W. Grimes */ 2780d94caffSDavid Greenman vm_map_t 2793202ed75SAlan Cox kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max, 2803202ed75SAlan Cox vm_size_t size, boolean_t superpage_align) 281df8bae1dSRodney W. Grimes { 2826e4f51d1SAlfred Perlstein int ret; 283df8bae1dSRodney W. Grimes vm_map_t result; 28423955314SAlfred Perlstein 285df8bae1dSRodney W. Grimes size = round_page(size); 286df8bae1dSRodney W. Grimes 2872bc24aa9SAlan Cox *min = vm_map_min(parent); 288*edb572a3SJohn Baldwin ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ? 2895aa60b6fSJohn Baldwin VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL, 2903364c323SKonstantin Belousov MAP_ACC_NO_CHARGE); 29124dedba9SAlan Cox if (ret != KERN_SUCCESS) 29224dedba9SAlan Cox panic("kmem_suballoc: bad status return of %d", ret); 293df8bae1dSRodney W. Grimes *max = *min + size; 2942d8acc0fSJohn Dyson result = vm_map_create(vm_map_pmap(parent), *min, *max); 295df8bae1dSRodney W. Grimes if (result == NULL) 296df8bae1dSRodney W. Grimes panic("kmem_suballoc: cannot create submap"); 2976e4f51d1SAlfred Perlstein if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS) 298df8bae1dSRodney W. Grimes panic("kmem_suballoc: unable to change range to submap"); 299df8bae1dSRodney W. Grimes return (result); 300df8bae1dSRodney W. Grimes } 301df8bae1dSRodney W. Grimes 302df8bae1dSRodney W. Grimes /* 3031c7c3c6aSMatthew Dillon * kmem_malloc: 3041c7c3c6aSMatthew Dillon * 3055df87b21SJeff Roberson * Allocate wired-down pages in the kernel's address space. 306df8bae1dSRodney W. Grimes */ 307df8bae1dSRodney W. Grimes vm_offset_t 3085df87b21SJeff Roberson kmem_malloc(struct vmem *vmem, vm_size_t size, int flags) 309df8bae1dSRodney W. Grimes { 310df8bae1dSRodney W. Grimes vm_offset_t addr; 3115df87b21SJeff Roberson int rv; 312df8bae1dSRodney W. Grimes 313df8bae1dSRodney W. Grimes size = round_page(size); 3145df87b21SJeff Roberson if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr)) 3155df87b21SJeff Roberson return (0); 316df8bae1dSRodney W. Grimes 3175df87b21SJeff Roberson rv = kmem_back((vmem == kmem_arena) ? kmem_object : kernel_object, 3185df87b21SJeff Roberson addr, size, flags); 3195df87b21SJeff Roberson if (rv != KERN_SUCCESS) { 3205df87b21SJeff Roberson vmem_free(vmem, addr, size); 321f31c239dSAlan Cox return (0); 322df8bae1dSRodney W. Grimes } 3235df87b21SJeff Roberson return (addr); 324e3813573SMatthew D Fleming } 325e3813573SMatthew D Fleming 326e3813573SMatthew D Fleming /* 327e3813573SMatthew D Fleming * kmem_back: 328e3813573SMatthew D Fleming * 329e3813573SMatthew D Fleming * Allocate physical pages for the specified virtual address range. 330e3813573SMatthew D Fleming */ 331e3813573SMatthew D Fleming int 3325df87b21SJeff Roberson kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags) 333e3813573SMatthew D Fleming { 334e3813573SMatthew D Fleming vm_offset_t offset, i; 335e3813573SMatthew D Fleming vm_page_t m; 336e3813573SMatthew D Fleming int pflags; 337e3813573SMatthew D Fleming 3385df87b21SJeff Roberson KASSERT(object == kmem_object || object == kernel_object, 3395df87b21SJeff Roberson ("kmem_back: only supports kernel objects.")); 3405df87b21SJeff Roberson 3410891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 3425df87b21SJeff Roberson pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED; 343df8bae1dSRodney W. Grimes 3445df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 3451e081f88SJeff Roberson for (i = 0; i < size; i += PAGE_SIZE) { 3461e081f88SJeff Roberson retry: 3475df87b21SJeff Roberson m = vm_page_alloc(object, OFF_TO_IDX(offset + i), pflags); 348df8bae1dSRodney W. Grimes 349df8bae1dSRodney W. Grimes /* 3500d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 3510d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 3520d94caffSDavid Greenman * aren't on any queues. 353df8bae1dSRodney W. Grimes */ 354df8bae1dSRodney W. Grimes if (m == NULL) { 3551c7c3c6aSMatthew Dillon if ((flags & M_NOWAIT) == 0) { 3565df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 357b18bfc3dSJohn Dyson VM_WAIT; 3585df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 359b18bfc3dSJohn Dyson goto retry; 360b18bfc3dSJohn Dyson } 361ff91d780STor Egge /* 3625df87b21SJeff Roberson * Unmap and free the pages. 363ff91d780STor Egge */ 3645df87b21SJeff Roberson if (i != 0) 3655df87b21SJeff Roberson pmap_remove(kernel_pmap, addr, addr + i); 366ff91d780STor Egge while (i != 0) { 367ff91d780STor Egge i -= PAGE_SIZE; 3685df87b21SJeff Roberson m = vm_page_lookup(object, 369ff91d780STor Egge OFF_TO_IDX(offset + i)); 370a623fedeSAlan Cox vm_page_unwire(m, 0); 371ff91d780STor Egge vm_page_free(m); 372ff91d780STor Egge } 3735df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 374e3813573SMatthew D Fleming return (KERN_NO_SPACE); 375df8bae1dSRodney W. Grimes } 3761e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 377fff6062aSAlan Cox pmap_zero_page(m); 378d98d0ce2SKonstantin Belousov KASSERT((m->oflags & VPO_UNMANAGED) != 0, 3799f5c801bSAlan Cox ("kmem_malloc: page %p is managed", m)); 3805df87b21SJeff Roberson m->valid = VM_PAGE_BITS_ALL; 381eb2a0517SAlan Cox pmap_enter(kernel_pmap, addr + i, VM_PROT_ALL, m, VM_PROT_ALL, 382eb2a0517SAlan Cox TRUE); 383df8bae1dSRodney W. Grimes } 3845df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 385df8bae1dSRodney W. Grimes 386e3813573SMatthew D Fleming return (KERN_SUCCESS); 387df8bae1dSRodney W. Grimes } 388df8bae1dSRodney W. Grimes 3895df87b21SJeff Roberson void 3905df87b21SJeff Roberson kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size) 3915df87b21SJeff Roberson { 3925df87b21SJeff Roberson vm_page_t m; 3935df87b21SJeff Roberson vm_offset_t offset; 3945df87b21SJeff Roberson int i; 3955df87b21SJeff Roberson 3965df87b21SJeff Roberson KASSERT(object == kmem_object || object == kernel_object, 3975df87b21SJeff Roberson ("kmem_unback: only supports kernel objects.")); 3985df87b21SJeff Roberson 3995df87b21SJeff Roberson offset = addr - VM_MIN_KERNEL_ADDRESS; 4005df87b21SJeff Roberson VM_OBJECT_WLOCK(object); 4015df87b21SJeff Roberson pmap_remove(kernel_pmap, addr, addr + size); 4025df87b21SJeff Roberson for (i = 0; i < size; i += PAGE_SIZE) { 4035df87b21SJeff Roberson m = vm_page_lookup(object, OFF_TO_IDX(offset + i)); 4045df87b21SJeff Roberson vm_page_unwire(m, 0); 4055df87b21SJeff Roberson vm_page_free(m); 4065df87b21SJeff Roberson } 4075df87b21SJeff Roberson VM_OBJECT_WUNLOCK(object); 4085df87b21SJeff Roberson } 4095df87b21SJeff Roberson 410df8bae1dSRodney W. Grimes /* 4115df87b21SJeff Roberson * kmem_free: 4125df87b21SJeff Roberson * 4135df87b21SJeff Roberson * Free memory allocated with kmem_malloc. The size must match the 4145df87b21SJeff Roberson * original allocation. 4155df87b21SJeff Roberson */ 4165df87b21SJeff Roberson void 4175df87b21SJeff Roberson kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size) 4185df87b21SJeff Roberson { 4195df87b21SJeff Roberson 4205df87b21SJeff Roberson size = round_page(size); 4215df87b21SJeff Roberson kmem_unback((vmem == kmem_arena) ? kmem_object : kernel_object, 4225df87b21SJeff Roberson addr, size); 4235df87b21SJeff Roberson vmem_free(vmem, addr, size); 4245df87b21SJeff Roberson } 4255df87b21SJeff Roberson 4265df87b21SJeff Roberson /* 4275df87b21SJeff Roberson * kmap_alloc_wait: 428df8bae1dSRodney W. Grimes * 429df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 430df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 431df8bae1dSRodney W. Grimes * 4321c7c3c6aSMatthew Dillon * This routine may block. 433df8bae1dSRodney W. Grimes */ 4340d94caffSDavid Greenman vm_offset_t 4355df87b21SJeff Roberson kmap_alloc_wait(map, size) 436df8bae1dSRodney W. Grimes vm_map_t map; 437df8bae1dSRodney W. Grimes vm_size_t size; 438df8bae1dSRodney W. Grimes { 439df8bae1dSRodney W. Grimes vm_offset_t addr; 44023955314SAlfred Perlstein 441df8bae1dSRodney W. Grimes size = round_page(size); 4423364c323SKonstantin Belousov if (!swap_reserve(size)) 4433364c323SKonstantin Belousov return (0); 444df8bae1dSRodney W. Grimes 445df8bae1dSRodney W. Grimes for (;;) { 446df8bae1dSRodney W. Grimes /* 4470d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 4480d94caffSDavid Greenman * to lock out sleepers/wakers. 449df8bae1dSRodney W. Grimes */ 450df8bae1dSRodney W. Grimes vm_map_lock(map); 451e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 452df8bae1dSRodney W. Grimes break; 453df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 454df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 455df8bae1dSRodney W. Grimes vm_map_unlock(map); 4563364c323SKonstantin Belousov swap_release(size); 457df8bae1dSRodney W. Grimes return (0); 458df8bae1dSRodney W. Grimes } 4599688f931SAlan Cox map->needs_wakeup = TRUE; 4608ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(map, 0); 461df8bae1dSRodney W. Grimes } 4623364c323SKonstantin Belousov vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, 4633364c323SKonstantin Belousov VM_PROT_ALL, MAP_ACC_CHARGED); 464df8bae1dSRodney W. Grimes vm_map_unlock(map); 465df8bae1dSRodney W. Grimes return (addr); 466df8bae1dSRodney W. Grimes } 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes /* 4695df87b21SJeff Roberson * kmap_free_wakeup: 470df8bae1dSRodney W. Grimes * 47124a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 472df8bae1dSRodney W. Grimes * waiting for memory in that map. 473df8bae1dSRodney W. Grimes */ 4740d94caffSDavid Greenman void 4755df87b21SJeff Roberson kmap_free_wakeup(map, addr, size) 476df8bae1dSRodney W. Grimes vm_map_t map; 477df8bae1dSRodney W. Grimes vm_offset_t addr; 478df8bae1dSRodney W. Grimes vm_size_t size; 479df8bae1dSRodney W. Grimes { 48023955314SAlfred Perlstein 481df8bae1dSRodney W. Grimes vm_map_lock(map); 482655c3490SKonstantin Belousov (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 4839688f931SAlan Cox if (map->needs_wakeup) { 4849688f931SAlan Cox map->needs_wakeup = FALSE; 4859688f931SAlan Cox vm_map_wakeup(map); 4869688f931SAlan Cox } 487df8bae1dSRodney W. Grimes vm_map_unlock(map); 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes 4905df87b21SJeff Roberson void 49189cb2a19SMatthew D Fleming kmem_init_zero_region(void) 49289cb2a19SMatthew D Fleming { 493cfb00e5aSMatthew D Fleming vm_offset_t addr, i; 49489cb2a19SMatthew D Fleming vm_page_t m; 49589cb2a19SMatthew D Fleming 496cfb00e5aSMatthew D Fleming /* 497cfb00e5aSMatthew D Fleming * Map a single physical page of zeros to a larger virtual range. 498cfb00e5aSMatthew D Fleming * This requires less looping in places that want large amounts of 499cfb00e5aSMatthew D Fleming * zeros, while not using much more physical resources. 500cfb00e5aSMatthew D Fleming */ 5015df87b21SJeff Roberson addr = kva_alloc(ZERO_REGION_SIZE); 502703dec68SAlan Cox m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | 50389cb2a19SMatthew D Fleming VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO); 50489cb2a19SMatthew D Fleming if ((m->flags & PG_ZERO) == 0) 50589cb2a19SMatthew D Fleming pmap_zero_page(m); 50689cb2a19SMatthew D Fleming for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE) 50789cb2a19SMatthew D Fleming pmap_qenter(addr + i, &m, 1); 5085df87b21SJeff Roberson pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ); 50989cb2a19SMatthew D Fleming 51089cb2a19SMatthew D Fleming zero_region = (const void *)addr; 51189cb2a19SMatthew D Fleming } 51289cb2a19SMatthew D Fleming 513df8bae1dSRodney W. Grimes /* 5141c7c3c6aSMatthew Dillon * kmem_init: 5151c7c3c6aSMatthew Dillon * 5161c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 5171c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 5181c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 5191c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 520df8bae1dSRodney W. Grimes */ 5210d94caffSDavid Greenman void 5220d94caffSDavid Greenman kmem_init(start, end) 523df8bae1dSRodney W. Grimes vm_offset_t start, end; 524df8bae1dSRodney W. Grimes { 525030f2369SAlfred Perlstein vm_map_t m; 526df8bae1dSRodney W. Grimes 5272d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 528c9267356SAlan Cox m->system_map = 1; 529df8bae1dSRodney W. Grimes vm_map_lock(m); 530df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 531df8bae1dSRodney W. Grimes kernel_map = m; 532c9267356SAlan Cox (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 5335cfa90e9SAlan Cox #ifdef __amd64__ 5345cfa90e9SAlan Cox KERNBASE, 5355cfa90e9SAlan Cox #else 5365cfa90e9SAlan Cox VM_MIN_KERNEL_ADDRESS, 5375cfa90e9SAlan Cox #endif 5385cfa90e9SAlan Cox start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 539df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 540df8bae1dSRodney W. Grimes vm_map_unlock(m); 541df8bae1dSRodney W. Grimes } 54286f08737SRobert Watson 5439309e63cSRobert Watson #ifdef DIAGNOSTIC 54486f08737SRobert Watson /* 54586f08737SRobert Watson * Allow userspace to directly trigger the VM drain routine for testing 54686f08737SRobert Watson * purposes. 54786f08737SRobert Watson */ 54886f08737SRobert Watson static int 54986f08737SRobert Watson debug_vm_lowmem(SYSCTL_HANDLER_ARGS) 55086f08737SRobert Watson { 55186f08737SRobert Watson int error, i; 55286f08737SRobert Watson 55386f08737SRobert Watson i = 0; 55486f08737SRobert Watson error = sysctl_handle_int(oidp, &i, 0, req); 55586f08737SRobert Watson if (error) 55686f08737SRobert Watson return (error); 55786f08737SRobert Watson if (i) 55886f08737SRobert Watson EVENTHANDLER_INVOKE(vm_lowmem, 0); 55986f08737SRobert Watson return (0); 56086f08737SRobert Watson } 56186f08737SRobert Watson 56286f08737SRobert Watson SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 56386f08737SRobert Watson debug_vm_lowmem, "I", "set to trigger vm_lowmem event"); 5649309e63cSRobert Watson #endif 565