1df8bae1dSRodney W. Grimes /* 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 * 3. All advertising materials mentioning features or use of this software 175929bcfaSPhilippe Charnier * must display the following acknowledgement: 18df8bae1dSRodney W. Grimes * This product includes software developed by the University of 19df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 20df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 363c4dd356SDavid Greenman * from: @(#)vm_kern.c 8.3 (Berkeley) 1/12/94 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * 39df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40df8bae1dSRodney W. Grimes * All rights reserved. 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 45df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 46df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 47df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 48df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57df8bae1dSRodney W. Grimes * School of Computer Science 58df8bae1dSRodney W. Grimes * Carnegie Mellon University 59df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 62df8bae1dSRodney W. Grimes * rights to redistribute these changes. 633c4dd356SDavid Greenman * 64c3aac50fSPeter Wemm * $FreeBSD$ 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Kernel memory management. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <sys/param.h> 72df8bae1dSRodney W. Grimes #include <sys/systm.h> 7360363fb9SLuigi Rizzo #include <sys/kernel.h> /* for ticks and hz */ 74fb919e4dSMark Murray #include <sys/lock.h> 75fb919e4dSMark Murray #include <sys/mutex.h> 76f23b4c91SGarrett Wollman #include <sys/proc.h> 77a1f6d91cSDavid Greenman #include <sys/malloc.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 80efeaf95aSDavid Greenman #include <vm/vm_param.h> 81efeaf95aSDavid Greenman #include <vm/pmap.h> 82efeaf95aSDavid Greenman #include <vm/vm_map.h> 83efeaf95aSDavid Greenman #include <vm/vm_object.h> 84df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 85df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 869b4288a3SBruce Evans #include <vm/vm_extern.h> 87df8bae1dSRodney W. Grimes 885b0a7408SJohn Dyson vm_map_t kernel_map=0; 895b0a7408SJohn Dyson vm_map_t kmem_map=0; 905b0a7408SJohn Dyson vm_map_t exec_map=0; 915b0a7408SJohn Dyson vm_map_t clean_map=0; 925b0a7408SJohn Dyson vm_map_t buffer_map=0; 93f23b4c91SGarrett Wollman 94df8bae1dSRodney W. Grimes /* 95df8bae1dSRodney W. Grimes * kmem_alloc_pageable: 96df8bae1dSRodney W. Grimes * 97df8bae1dSRodney W. Grimes * Allocate pageable memory to the kernel's address map. 98f81b8592SDavid Greenman * "map" must be kernel_map or a submap of kernel_map. 99df8bae1dSRodney W. Grimes */ 1000d94caffSDavid Greenman vm_offset_t 1010d94caffSDavid Greenman kmem_alloc_pageable(map, size) 102df8bae1dSRodney W. Grimes vm_map_t map; 103030f2369SAlfred Perlstein vm_size_t size; 104df8bae1dSRodney W. Grimes { 105df8bae1dSRodney W. Grimes vm_offset_t addr; 106030f2369SAlfred Perlstein int result; 107df8bae1dSRodney W. Grimes 108df8bae1dSRodney W. Grimes size = round_page(size); 109df8bae1dSRodney W. Grimes addr = vm_map_min(map); 110848d1419SAlan Cox result = vm_map_find(map, NULL, 0, 111bd7e5f99SJohn Dyson &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0); 112df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 113df8bae1dSRodney W. Grimes return (0); 114df8bae1dSRodney W. Grimes } 115df8bae1dSRodney W. Grimes return (addr); 116df8bae1dSRodney W. Grimes } 117df8bae1dSRodney W. Grimes 118df8bae1dSRodney W. Grimes /* 119a839bdc8SDmitrij Tejblum * kmem_alloc_nofault: 120a839bdc8SDmitrij Tejblum * 121a839bdc8SDmitrij Tejblum * Same as kmem_alloc_pageable, except that it create a nofault entry. 122a839bdc8SDmitrij Tejblum */ 123a839bdc8SDmitrij Tejblum vm_offset_t 124a839bdc8SDmitrij Tejblum kmem_alloc_nofault(map, size) 125a839bdc8SDmitrij Tejblum vm_map_t map; 126030f2369SAlfred Perlstein vm_size_t size; 127a839bdc8SDmitrij Tejblum { 128a839bdc8SDmitrij Tejblum vm_offset_t addr; 129030f2369SAlfred Perlstein int result; 130a839bdc8SDmitrij Tejblum 131a839bdc8SDmitrij Tejblum size = round_page(size); 132a839bdc8SDmitrij Tejblum addr = vm_map_min(map); 133848d1419SAlan Cox result = vm_map_find(map, NULL, 0, 134a839bdc8SDmitrij Tejblum &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 135a839bdc8SDmitrij Tejblum if (result != KERN_SUCCESS) { 136a839bdc8SDmitrij Tejblum return (0); 137a839bdc8SDmitrij Tejblum } 138a839bdc8SDmitrij Tejblum return (addr); 139a839bdc8SDmitrij Tejblum } 140a839bdc8SDmitrij Tejblum 141a839bdc8SDmitrij Tejblum /* 142df8bae1dSRodney W. Grimes * Allocate wired-down memory in the kernel's address map 143df8bae1dSRodney W. Grimes * or a submap. 144df8bae1dSRodney W. Grimes */ 1450d94caffSDavid Greenman vm_offset_t 1460d94caffSDavid Greenman kmem_alloc(map, size) 147030f2369SAlfred Perlstein vm_map_t map; 148030f2369SAlfred Perlstein vm_size_t size; 149df8bae1dSRodney W. Grimes { 150df8bae1dSRodney W. Grimes vm_offset_t addr; 151030f2369SAlfred Perlstein vm_offset_t offset; 152df8bae1dSRodney W. Grimes vm_offset_t i; 153df8bae1dSRodney W. Grimes 1540cddd8f0SMatthew Dillon GIANT_REQUIRED; 1550cddd8f0SMatthew Dillon 156df8bae1dSRodney W. Grimes size = round_page(size); 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes /* 1590d94caffSDavid Greenman * Use the kernel object for wired-down kernel pages. Assume that no 1600d94caffSDavid Greenman * region of the kernel object is referenced more than once. 161df8bae1dSRodney W. Grimes */ 162df8bae1dSRodney W. Grimes 163df8bae1dSRodney W. Grimes /* 1640d94caffSDavid Greenman * Locate sufficient space in the map. This will give us the final 1650d94caffSDavid Greenman * virtual address for the new memory, and thus will tell us the 1660d94caffSDavid Greenman * offset within the kernel map. 167df8bae1dSRodney W. Grimes */ 168df8bae1dSRodney W. Grimes vm_map_lock(map); 169e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr)) { 170df8bae1dSRodney W. Grimes vm_map_unlock(map); 171df8bae1dSRodney W. Grimes return (0); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes offset = addr - VM_MIN_KERNEL_ADDRESS; 174df8bae1dSRodney W. Grimes vm_object_reference(kernel_object); 175bd7e5f99SJohn Dyson vm_map_insert(map, kernel_object, offset, addr, addr + size, 176bd7e5f99SJohn Dyson VM_PROT_ALL, VM_PROT_ALL, 0); 177df8bae1dSRodney W. Grimes vm_map_unlock(map); 178df8bae1dSRodney W. Grimes 179df8bae1dSRodney W. Grimes /* 1800d94caffSDavid Greenman * Guarantee that there are pages already in this object before 1810d94caffSDavid Greenman * calling vm_map_pageable. This is to prevent the following 1820d94caffSDavid Greenman * scenario: 183df8bae1dSRodney W. Grimes * 1840d94caffSDavid Greenman * 1) Threads have swapped out, so that there is a pager for the 1850d94caffSDavid Greenman * kernel_object. 2) The kmsg zone is empty, and so we are 1860d94caffSDavid Greenman * kmem_allocing a new page for it. 3) vm_map_pageable calls vm_fault; 1870d94caffSDavid Greenman * there is no page, but there is a pager, so we call 1880d94caffSDavid Greenman * pager_data_request. But the kmsg zone is empty, so we must 1890d94caffSDavid Greenman * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when 1900d94caffSDavid Greenman * we get the data back from the pager, it will be (very stale) 1910d94caffSDavid Greenman * non-zero data. kmem_alloc is defined to return zero-filled memory. 192df8bae1dSRodney W. Grimes * 1930d94caffSDavid Greenman * We're intentionally not activating the pages we allocate to prevent a 1940d94caffSDavid Greenman * race with page-out. vm_map_pageable will wire the pages. 195df8bae1dSRodney W. Grimes */ 196df8bae1dSRodney W. Grimes for (i = 0; i < size; i += PAGE_SIZE) { 197df8bae1dSRodney W. Grimes vm_page_t mem; 198df8bae1dSRodney W. Grimes 19995461b45SJohn Dyson mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i), 20095461b45SJohn Dyson VM_ALLOC_ZERO | VM_ALLOC_RETRY); 201f70f05f2SJohn Dyson if ((mem->flags & PG_ZERO) == 0) 202fff6062aSAlan Cox pmap_zero_page(mem); 2037fb0c17eSDavid Greenman mem->valid = VM_PAGE_BITS_ALL; 2041c7c3c6aSMatthew Dillon vm_page_flag_clear(mem, PG_ZERO); 2051c7c3c6aSMatthew Dillon vm_page_wakeup(mem); 206df8bae1dSRodney W. Grimes } 207df8bae1dSRodney W. Grimes 208df8bae1dSRodney W. Grimes /* 209df8bae1dSRodney W. Grimes * And finally, mark the data as non-pageable. 210df8bae1dSRodney W. Grimes */ 2111d7cf06cSAlan Cox (void) vm_map_wire(map, addr, addr + size, FALSE); 212df8bae1dSRodney W. Grimes 213df8bae1dSRodney W. Grimes return (addr); 214df8bae1dSRodney W. Grimes } 215df8bae1dSRodney W. Grimes 216df8bae1dSRodney W. Grimes /* 217df8bae1dSRodney W. Grimes * kmem_free: 218df8bae1dSRodney W. Grimes * 219df8bae1dSRodney W. Grimes * Release a region of kernel virtual memory allocated 220df8bae1dSRodney W. Grimes * with kmem_alloc, and return the physical pages 221df8bae1dSRodney W. Grimes * associated with that region. 2221c7c3c6aSMatthew Dillon * 2231c7c3c6aSMatthew Dillon * This routine may not block on kernel maps. 224df8bae1dSRodney W. Grimes */ 2250d94caffSDavid Greenman void 2260d94caffSDavid Greenman kmem_free(map, addr, size) 227df8bae1dSRodney W. Grimes vm_map_t map; 228030f2369SAlfred Perlstein vm_offset_t addr; 229df8bae1dSRodney W. Grimes vm_size_t size; 230df8bae1dSRodney W. Grimes { 23123955314SAlfred Perlstein 232df8bae1dSRodney W. Grimes (void) vm_map_remove(map, trunc_page(addr), round_page(addr + size)); 233df8bae1dSRodney W. Grimes } 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes /* 236df8bae1dSRodney W. Grimes * kmem_suballoc: 237df8bae1dSRodney W. Grimes * 238df8bae1dSRodney W. Grimes * Allocates a map to manage a subrange 239df8bae1dSRodney W. Grimes * of the kernel virtual address space. 240df8bae1dSRodney W. Grimes * 241df8bae1dSRodney W. Grimes * Arguments are as follows: 242df8bae1dSRodney W. Grimes * 243df8bae1dSRodney W. Grimes * parent Map to take range from 244df8bae1dSRodney W. Grimes * min, max Returned endpoints of map 245030f2369SAlfred Perlstein * size Size of range to find 246df8bae1dSRodney W. Grimes */ 2470d94caffSDavid Greenman vm_map_t 2482d8acc0fSJohn Dyson kmem_suballoc(parent, min, max, size) 2496e4f51d1SAlfred Perlstein vm_map_t parent; 250df8bae1dSRodney W. Grimes vm_offset_t *min, *max; 2516e4f51d1SAlfred Perlstein vm_size_t size; 252df8bae1dSRodney W. Grimes { 2536e4f51d1SAlfred Perlstein int ret; 254df8bae1dSRodney W. Grimes vm_map_t result; 25523955314SAlfred Perlstein 2560cddd8f0SMatthew Dillon GIANT_REQUIRED; 257df8bae1dSRodney W. Grimes 258df8bae1dSRodney W. Grimes size = round_page(size); 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes *min = (vm_offset_t) vm_map_min(parent); 261df8bae1dSRodney W. Grimes ret = vm_map_find(parent, NULL, (vm_offset_t) 0, 262bd7e5f99SJohn Dyson min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0); 263df8bae1dSRodney W. Grimes if (ret != KERN_SUCCESS) { 264df8bae1dSRodney W. Grimes printf("kmem_suballoc: bad status return of %d.\n", ret); 265df8bae1dSRodney W. Grimes panic("kmem_suballoc"); 266df8bae1dSRodney W. Grimes } 267df8bae1dSRodney W. Grimes *max = *min + size; 2682d8acc0fSJohn Dyson result = vm_map_create(vm_map_pmap(parent), *min, *max); 269df8bae1dSRodney W. Grimes if (result == NULL) 270df8bae1dSRodney W. Grimes panic("kmem_suballoc: cannot create submap"); 2716e4f51d1SAlfred Perlstein if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS) 272df8bae1dSRodney W. Grimes panic("kmem_suballoc: unable to change range to submap"); 273df8bae1dSRodney W. Grimes return (result); 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes 276df8bae1dSRodney W. Grimes /* 2771c7c3c6aSMatthew Dillon * kmem_malloc: 2781c7c3c6aSMatthew Dillon * 279df8bae1dSRodney W. Grimes * Allocate wired-down memory in the kernel's address map for the higher 280df8bae1dSRodney W. Grimes * level kernel memory allocator (kern/kern_malloc.c). We cannot use 281df8bae1dSRodney W. Grimes * kmem_alloc() because we may need to allocate memory at interrupt 282df8bae1dSRodney W. Grimes * level where we cannot block (canwait == FALSE). 283df8bae1dSRodney W. Grimes * 284df8bae1dSRodney W. Grimes * This routine has its own private kernel submap (kmem_map) and object 285df8bae1dSRodney W. Grimes * (kmem_object). This, combined with the fact that only malloc uses 286df8bae1dSRodney W. Grimes * this routine, ensures that we will never block in map or object waits. 287df8bae1dSRodney W. Grimes * 288df8bae1dSRodney W. Grimes * Note that this still only works in a uni-processor environment and 289df8bae1dSRodney W. Grimes * when called at splhigh(). 290df8bae1dSRodney W. Grimes * 291df8bae1dSRodney W. Grimes * We don't worry about expanding the map (adding entries) since entries 292df8bae1dSRodney W. Grimes * for wired maps are statically allocated. 2931c7c3c6aSMatthew Dillon * 2941c7c3c6aSMatthew Dillon * NOTE: This routine is not supposed to block if M_NOWAIT is set, but 2951c7c3c6aSMatthew Dillon * I have not verified that it actually does not block. 29608442f8aSBosko Milekic * 29708442f8aSBosko Milekic * `map' is ONLY allowed to be kmem_map or one of the mbuf submaps to 29808442f8aSBosko Milekic * which we never free. 299df8bae1dSRodney W. Grimes */ 300df8bae1dSRodney W. Grimes vm_offset_t 3011c7c3c6aSMatthew Dillon kmem_malloc(map, size, flags) 302030f2369SAlfred Perlstein vm_map_t map; 303030f2369SAlfred Perlstein vm_size_t size; 3041c7c3c6aSMatthew Dillon int flags; 305df8bae1dSRodney W. Grimes { 306030f2369SAlfred Perlstein vm_offset_t offset, i; 307df8bae1dSRodney W. Grimes vm_map_entry_t entry; 308df8bae1dSRodney W. Grimes vm_offset_t addr; 309df8bae1dSRodney W. Grimes vm_page_t m; 3101e081f88SJeff Roberson int pflags; 311df8bae1dSRodney W. Grimes 3120cddd8f0SMatthew Dillon GIANT_REQUIRED; 31323955314SAlfred Perlstein 314df8bae1dSRodney W. Grimes size = round_page(size); 315df8bae1dSRodney W. Grimes addr = vm_map_min(map); 316df8bae1dSRodney W. Grimes 317df8bae1dSRodney W. Grimes /* 3180d94caffSDavid Greenman * Locate sufficient space in the map. This will give us the final 3190d94caffSDavid Greenman * virtual address for the new memory, and thus will tell us the 3200d94caffSDavid Greenman * offset within the kernel map. 321df8bae1dSRodney W. Grimes */ 322df8bae1dSRodney W. Grimes vm_map_lock(map); 323e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr)) { 324df8bae1dSRodney W. Grimes vm_map_unlock(map); 32508442f8aSBosko Milekic if (map != kmem_map) { 32660363fb9SLuigi Rizzo static int last_report; /* when we did it (in ticks) */ 32760363fb9SLuigi Rizzo if (ticks < last_report || 32860363fb9SLuigi Rizzo (ticks - last_report) >= hz) { 32960363fb9SLuigi Rizzo last_report = ticks; 33008442f8aSBosko Milekic printf("Out of mbuf address space!\n"); 33108442f8aSBosko Milekic printf("Consider increasing NMBCLUSTERS\n"); 33260363fb9SLuigi Rizzo } 33323955314SAlfred Perlstein goto bad; 3345eb7d0cdSDavid Greenman } 3351c7c3c6aSMatthew Dillon if ((flags & M_NOWAIT) == 0) 3363efc015bSPeter Wemm panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated", 3373efc015bSPeter Wemm (long)size, (long)map->size); 33823955314SAlfred Perlstein goto bad; 339df8bae1dSRodney W. Grimes } 3400891ef4cSJohn Dyson offset = addr - VM_MIN_KERNEL_ADDRESS; 341df8bae1dSRodney W. Grimes vm_object_reference(kmem_object); 342bd7e5f99SJohn Dyson vm_map_insert(map, kmem_object, offset, addr, addr + size, 343bd7e5f99SJohn Dyson VM_PROT_ALL, VM_PROT_ALL, 0); 344df8bae1dSRodney W. Grimes 3451c7c3c6aSMatthew Dillon /* 3461c7c3c6aSMatthew Dillon * Note: if M_NOWAIT specified alone, allocate from 3471c7c3c6aSMatthew Dillon * interrupt-safe queues only (just the free list). If 34802cd7c3cSJohn Baldwin * M_USE_RESERVE is also specified, we can also 3491c7c3c6aSMatthew Dillon * allocate from the cache. Neither of the latter two 3501c7c3c6aSMatthew Dillon * flags may be specified from an interrupt since interrupts 3511c7c3c6aSMatthew Dillon * are not allowed to mess with the cache queue. 3521c7c3c6aSMatthew Dillon */ 3531e081f88SJeff Roberson 35495f24639SJeff Roberson if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT) 35595f24639SJeff Roberson pflags = VM_ALLOC_INTERRUPT; 35695f24639SJeff Roberson else 35795f24639SJeff Roberson pflags = VM_ALLOC_SYSTEM; 35895f24639SJeff Roberson 35995f24639SJeff Roberson if (flags & M_ZERO) 36095f24639SJeff Roberson pflags |= VM_ALLOC_ZERO; 36195f24639SJeff Roberson 3621e081f88SJeff Roberson 3631e081f88SJeff Roberson for (i = 0; i < size; i += PAGE_SIZE) { 3641e081f88SJeff Roberson retry: 36595f24639SJeff Roberson m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags); 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes /* 3680d94caffSDavid Greenman * Ran out of space, free everything up and return. Don't need 3690d94caffSDavid Greenman * to lock page queues here as we know that the pages we got 3700d94caffSDavid Greenman * aren't on any queues. 371df8bae1dSRodney W. Grimes */ 372df8bae1dSRodney W. Grimes if (m == NULL) { 3731c7c3c6aSMatthew Dillon if ((flags & M_NOWAIT) == 0) { 374c7003c69SAlan Cox vm_map_unlock(map); 375b18bfc3dSJohn Dyson VM_WAIT; 376c7003c69SAlan Cox vm_map_lock(map); 377b18bfc3dSJohn Dyson goto retry; 378b18bfc3dSJohn Dyson } 379ff91d780STor Egge /* 380ff91d780STor Egge * Free the pages before removing the map entry. 381ff91d780STor Egge * They are already marked busy. Calling 382ff91d780STor Egge * vm_map_delete before the pages has been freed or 383ff91d780STor Egge * unbusied will cause a deadlock. 384ff91d780STor Egge */ 385ff91d780STor Egge while (i != 0) { 386ff91d780STor Egge i -= PAGE_SIZE; 387ff91d780STor Egge m = vm_page_lookup(kmem_object, 388ff91d780STor Egge OFF_TO_IDX(offset + i)); 38957123de6SAlan Cox vm_page_lock_queues(); 390ff91d780STor Egge vm_page_free(m); 39157123de6SAlan Cox vm_page_unlock_queues(); 392ff91d780STor Egge } 393df8bae1dSRodney W. Grimes vm_map_delete(map, addr, addr + size); 394df8bae1dSRodney W. Grimes vm_map_unlock(map); 39523955314SAlfred Perlstein goto bad; 396df8bae1dSRodney W. Grimes } 3971e081f88SJeff Roberson if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 398fff6062aSAlan Cox pmap_zero_page(m); 399e69763a3SDoug Rabson vm_page_flag_clear(m, PG_ZERO); 4007fb0c17eSDavid Greenman m->valid = VM_PAGE_BITS_ALL; 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes 403df8bae1dSRodney W. Grimes /* 4040d94caffSDavid Greenman * Mark map entry as non-pageable. Assert: vm_map_insert() will never 4050d94caffSDavid Greenman * be able to extend the previous entry so there will be a new entry 4060d94caffSDavid Greenman * exactly corresponding to this address range and it will have 4070d94caffSDavid Greenman * wired_count == 0. 408df8bae1dSRodney W. Grimes */ 409df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, addr, &entry) || 410df8bae1dSRodney W. Grimes entry->start != addr || entry->end != addr + size || 411c7003c69SAlan Cox entry->wired_count != 0) 412df8bae1dSRodney W. Grimes panic("kmem_malloc: entry not found or misaligned"); 413c7003c69SAlan Cox entry->wired_count = 1; 414df8bae1dSRodney W. Grimes 415b7b2aac2SJohn Dyson vm_map_simplify_entry(map, entry); 416b7b2aac2SJohn Dyson 417df8bae1dSRodney W. Grimes /* 4180d94caffSDavid Greenman * Loop thru pages, entering them in the pmap. (We cannot add them to 4190d94caffSDavid Greenman * the wired count without wrapping the vm_page_queue_lock in 4200d94caffSDavid Greenman * splimp...) 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes for (i = 0; i < size; i += PAGE_SIZE) { 423a316d390SJohn Dyson m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i)); 424e16cfdbeSAlan Cox vm_page_lock_queues(); 425bd7e5f99SJohn Dyson vm_page_wire(m); 426e69763a3SDoug Rabson vm_page_wakeup(m); 427e16cfdbeSAlan Cox vm_page_unlock_queues(); 4281c7c3c6aSMatthew Dillon /* 4291c7c3c6aSMatthew Dillon * Because this is kernel_pmap, this call will not block. 4301c7c3c6aSMatthew Dillon */ 4310385347cSPeter Wemm pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1); 432db44450bSAlan Cox vm_page_flag_set(m, PG_WRITEABLE | PG_REFERENCED); 433df8bae1dSRodney W. Grimes } 434df8bae1dSRodney W. Grimes vm_map_unlock(map); 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes return (addr); 43723955314SAlfred Perlstein 43823955314SAlfred Perlstein bad: 43923955314SAlfred Perlstein return (0); 440df8bae1dSRodney W. Grimes } 441df8bae1dSRodney W. Grimes 442df8bae1dSRodney W. Grimes /* 4431c7c3c6aSMatthew Dillon * kmem_alloc_wait: 444df8bae1dSRodney W. Grimes * 445df8bae1dSRodney W. Grimes * Allocates pageable memory from a sub-map of the kernel. If the submap 446df8bae1dSRodney W. Grimes * has no room, the caller sleeps waiting for more memory in the submap. 447df8bae1dSRodney W. Grimes * 4481c7c3c6aSMatthew Dillon * This routine may block. 449df8bae1dSRodney W. Grimes */ 4500d94caffSDavid Greenman vm_offset_t 4510d94caffSDavid Greenman kmem_alloc_wait(map, size) 452df8bae1dSRodney W. Grimes vm_map_t map; 453df8bae1dSRodney W. Grimes vm_size_t size; 454df8bae1dSRodney W. Grimes { 455df8bae1dSRodney W. Grimes vm_offset_t addr; 45623955314SAlfred Perlstein 457df8bae1dSRodney W. Grimes size = round_page(size); 458df8bae1dSRodney W. Grimes 459df8bae1dSRodney W. Grimes for (;;) { 460df8bae1dSRodney W. Grimes /* 4610d94caffSDavid Greenman * To make this work for more than one map, use the map's lock 4620d94caffSDavid Greenman * to lock out sleepers/wakers. 463df8bae1dSRodney W. Grimes */ 464df8bae1dSRodney W. Grimes vm_map_lock(map); 465e47ed70bSJohn Dyson if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 466df8bae1dSRodney W. Grimes break; 467df8bae1dSRodney W. Grimes /* no space now; see if we can ever get space */ 468df8bae1dSRodney W. Grimes if (vm_map_max(map) - vm_map_min(map) < size) { 469df8bae1dSRodney W. Grimes vm_map_unlock(map); 470df8bae1dSRodney W. Grimes return (0); 471df8bae1dSRodney W. Grimes } 4729688f931SAlan Cox map->needs_wakeup = TRUE; 4739688f931SAlan Cox vm_map_unlock_and_wait(map, FALSE); 474df8bae1dSRodney W. Grimes } 4759688f931SAlan Cox vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0); 476df8bae1dSRodney W. Grimes vm_map_unlock(map); 477df8bae1dSRodney W. Grimes return (addr); 478df8bae1dSRodney W. Grimes } 479df8bae1dSRodney W. Grimes 480df8bae1dSRodney W. Grimes /* 4811c7c3c6aSMatthew Dillon * kmem_free_wakeup: 482df8bae1dSRodney W. Grimes * 48324a1cce3SDavid Greenman * Returns memory to a submap of the kernel, and wakes up any processes 484df8bae1dSRodney W. Grimes * waiting for memory in that map. 485df8bae1dSRodney W. Grimes */ 4860d94caffSDavid Greenman void 4870d94caffSDavid Greenman kmem_free_wakeup(map, addr, size) 488df8bae1dSRodney W. Grimes vm_map_t map; 489df8bae1dSRodney W. Grimes vm_offset_t addr; 490df8bae1dSRodney W. Grimes vm_size_t size; 491df8bae1dSRodney W. Grimes { 49223955314SAlfred Perlstein 493df8bae1dSRodney W. Grimes vm_map_lock(map); 494df8bae1dSRodney W. Grimes (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 4959688f931SAlan Cox if (map->needs_wakeup) { 4969688f931SAlan Cox map->needs_wakeup = FALSE; 4979688f931SAlan Cox vm_map_wakeup(map); 4989688f931SAlan Cox } 499df8bae1dSRodney W. Grimes vm_map_unlock(map); 500df8bae1dSRodney W. Grimes } 501df8bae1dSRodney W. Grimes 502df8bae1dSRodney W. Grimes /* 5031c7c3c6aSMatthew Dillon * kmem_init: 5041c7c3c6aSMatthew Dillon * 5051c7c3c6aSMatthew Dillon * Create the kernel map; insert a mapping covering kernel text, 5061c7c3c6aSMatthew Dillon * data, bss, and all space allocated thus far (`boostrap' data). The 5071c7c3c6aSMatthew Dillon * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 5081c7c3c6aSMatthew Dillon * `start' as allocated, and the range between `start' and `end' as free. 509df8bae1dSRodney W. Grimes */ 5100d94caffSDavid Greenman void 5110d94caffSDavid Greenman kmem_init(start, end) 512df8bae1dSRodney W. Grimes vm_offset_t start, end; 513df8bae1dSRodney W. Grimes { 514030f2369SAlfred Perlstein vm_map_t m; 515df8bae1dSRodney W. Grimes 5162d8acc0fSJohn Dyson m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 517df8bae1dSRodney W. Grimes vm_map_lock(m); 518df8bae1dSRodney W. Grimes /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 519df8bae1dSRodney W. Grimes kernel_map = m; 5203075778bSJohn Dyson kernel_map->system_map = 1; 521df8bae1dSRodney W. Grimes (void) vm_map_insert(m, NULL, (vm_offset_t) 0, 522bd7e5f99SJohn Dyson VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 0); 523df8bae1dSRodney W. Grimes /* ... and ending with the completion of the above `insert' */ 524df8bae1dSRodney W. Grimes vm_map_unlock(m); 525df8bae1dSRodney W. Grimes } 526