vm_kern.c revision 166964
1149514Swollman/*- 264499Swollman * Copyright (c) 1991, 1993 32742Swollman * The Regents of the University of California. All rights reserved. 42742Swollman * 52742Swollman * This code is derived from software contributed to Berkeley by 62742Swollman * The Mach Operating System project at Carnegie-Mellon University. 7149514Swollman * 82742Swollman * Redistribution and use in source and binary forms, with or without 958787Sru * modification, are permitted provided that the following conditions 1058787Sru * are met: 112742Swollman * 1. Redistributions of source code must retain the above copyright 1286222Swollman * notice, this list of conditions and the following disclaimer. 1320094Swollman * 2. Redistributions in binary form must reproduce the above copyright 1420094Swollman * notice, this list of conditions and the following disclaimer in the 1520094Swollman * documentation and/or other materials provided with the distribution. 1620094Swollman * 4. Neither the name of the University nor the names of its contributors 1720094Swollman * may be used to endorse or promote products derived from this software 1820094Swollman * without specific prior written permission. 1920094Swollman * 2020094Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2119878Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2219878Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2319878Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2419878Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2519878Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2619878Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2719878Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2819878Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958787Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058787Sru * SUCH DAMAGE. 3158787Sru * 3258787Sru * from: @(#)vm_kern.c 8.3 (Berkeley) 1/12/94 3358787Sru * 3458787Sru * 3558787Sru * Copyright (c) 1987, 1990 Carnegie-Mellon University. 3658787Sru * All rights reserved. 3758787Sru * 3858787Sru * Authors: Avadis Tevanian, Jr., Michael Wayne Young 3958787Sru * 4058787Sru * Permission to use, copy, modify and distribute this software and 4158787Sru * its documentation is hereby granted, provided that both the copyright 4258787Sru * notice and this permission notice appear in all copies of the 4358787Sru * software, derivative works or modified versions, and any portions 4458787Sru * thereof, and that both notices appear in supporting documentation. 4558787Sru * 4658787Sru * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 472742Swollman * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 482742Swollman * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 492742Swollman * 502742Swollman * Carnegie Mellon requests users of this software to return to 512742Swollman * 522742Swollman * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 532742Swollman * School of Computer Science 5419878Swollman * Carnegie Mellon University 552742Swollman * Pittsburgh PA 15213-3890 562742Swollman * 572742Swollman * any improvements or extensions that they make and grant Carnegie the 5819878Swollman * rights to redistribute these changes. 592742Swollman */ 602742Swollman 61149514Swollman/* 6221217Swollman * Kernel memory management. 639908Swollman */ 649908Swollman 652742Swollman#include <sys/cdefs.h> 6619878Swollman__FBSDID("$FreeBSD: head/sys/vm/vm_kern.c 166964 2007-02-25 06:14:58Z alc $"); 6719878Swollman 6819878Swollman#include <sys/param.h> 6919878Swollman#include <sys/systm.h> 7019878Swollman#include <sys/kernel.h> /* for ticks and hz */ 7119878Swollman#include <sys/lock.h> 7219878Swollman#include <sys/mutex.h> 7319878Swollman#include <sys/proc.h> 7419878Swollman#include <sys/malloc.h> 7519878Swollman 7619878Swollman#include <vm/vm.h> 7719878Swollman#include <vm/vm_param.h> 7819878Swollman#include <vm/pmap.h> 7919878Swollman#include <vm/vm_map.h> 8019878Swollman#include <vm/vm_object.h> 8119878Swollman#include <vm/vm_page.h> 8293799Swollman#include <vm/vm_pageout.h> 8358787Sru#include <vm/vm_extern.h> 8458787Sru 8519878Swollmanvm_map_t kernel_map=0; 8619878Swollmanvm_map_t kmem_map=0; 8719878Swollmanvm_map_t exec_map=0; 889908Swollmanvm_map_t pipe_map; 89149514Swollmanvm_map_t buffer_map=0; 909908Swollman 919908Swollman/* 929908Swollman * kmem_alloc_nofault: 9321217Swollman * 949908Swollman * Allocate a virtual address range with no underlying object and 9558787Sru * no initial mapping to physical memory. Any mapping from this 9619878Swollman * range to physical memory must be explicitly created prior to 9719878Swollman * its use, typically with pmap_qenter(). Any attempt to create 989908Swollman * a mapping on demand through vm_fault() will result in a panic. 99149514Swollman */ 1009908Swollmanvm_offset_t 1019908Swollmankmem_alloc_nofault(map, size) 1029908Swollman vm_map_t map; 1039908Swollman vm_size_t size; 10458787Sru{ 10558787Sru vm_offset_t addr; 10658787Sru int result; 10764499Swollman 10864499Swollman size = round_page(size); 10964499Swollman addr = vm_map_min(map); 11058787Sru result = vm_map_find(map, NULL, 0, 11158787Sru &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT); 11267578Swollman if (result != KERN_SUCCESS) { 11358787Sru return (0); 11458787Sru } 11558787Sru return (addr); 116149514Swollman} 11764499Swollman 11864499Swollman/* 11964499Swollman * Allocate wired-down memory in the kernel's address map 12064499Swollman * or a submap. 12186222Swollman */ 12286222Swollmanvm_offset_t 12386222Swollmankmem_alloc(map, size) 12486222Swollman vm_map_t map; 12586222Swollman vm_size_t size; 12686222Swollman{ 12786222Swollman vm_offset_t addr; 12886222Swollman vm_offset_t offset; 12986222Swollman vm_offset_t i; 13086222Swollman 13186222Swollman size = round_page(size); 13286222Swollman 13386222Swollman /* 13486222Swollman * Use the kernel object for wired-down kernel pages. Assume that no 13586222Swollman * region of the kernel object is referenced more than once. 13686222Swollman */ 13786222Swollman 13886222Swollman /* 13986222Swollman * Locate sufficient space in the map. This will give us the final 14086222Swollman * virtual address for the new memory, and thus will tell us the 14186222Swollman * offset within the kernel map. 14286222Swollman */ 14386222Swollman vm_map_lock(map); 144136638Swollman if (vm_map_findspace(map, vm_map_min(map), size, &addr)) { 145136638Swollman vm_map_unlock(map); 146136638Swollman return (0); 147136638Swollman } 148136638Swollman offset = addr - VM_MIN_KERNEL_ADDRESS; 149136638Swollman vm_object_reference(kernel_object); 150136638Swollman vm_map_insert(map, kernel_object, offset, addr, addr + size, 15193799Swollman VM_PROT_ALL, VM_PROT_ALL, 0); 15293799Swollman vm_map_unlock(map); 15393799Swollman 15493799Swollman /* 15593799Swollman * Guarantee that there are pages already in this object before 15693799Swollman * calling vm_map_wire. This is to prevent the following 15793799Swollman * scenario: 158136638Swollman * 159136638Swollman * 1) Threads have swapped out, so that there is a pager for the 160136638Swollman * kernel_object. 2) The kmsg zone is empty, and so we are 161136638Swollman * kmem_allocing a new page for it. 3) vm_map_wire calls vm_fault; 162136638Swollman * there is no page, but there is a pager, so we call 163136638Swollman * pager_data_request. But the kmsg zone is empty, so we must 164136638Swollman * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when 165136638Swollman * we get the data back from the pager, it will be (very stale) 166136638Swollman * non-zero data. kmem_alloc is defined to return zero-filled memory. 167136638Swollman * 168136638Swollman * We're intentionally not activating the pages we allocate to prevent a 169136638Swollman * race with page-out. vm_map_wire will wire the pages. 170136638Swollman */ 171136638Swollman VM_OBJECT_LOCK(kernel_object); 172136638Swollman for (i = 0; i < size; i += PAGE_SIZE) { 173136638Swollman vm_page_t mem; 174136638Swollman 175136638Swollman mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i), 176136638Swollman VM_ALLOC_NOBUSY | VM_ALLOC_ZERO | VM_ALLOC_RETRY); 177136638Swollman mem->valid = VM_PAGE_BITS_ALL; 178136638Swollman KASSERT((mem->flags & PG_UNMANAGED) != 0, 179136638Swollman ("kmem_alloc: page %p is managed", mem)); 180136638Swollman } 181136638Swollman VM_OBJECT_UNLOCK(kernel_object); 182136638Swollman 183136638Swollman /* 184136638Swollman * And finally, mark the data as non-pageable. 185136638Swollman */ 186136638Swollman (void) vm_map_wire(map, addr, addr + size, 187136638Swollman VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); 188136638Swollman 189136638Swollman return (addr); 190136638Swollman} 191136638Swollman 192136638Swollman/* 193136638Swollman * kmem_free: 194136638Swollman * 195136638Swollman * Release a region of kernel virtual memory allocated 196136638Swollman * with kmem_alloc, and return the physical pages 197136638Swollman * associated with that region. 198136638Swollman * 199136638Swollman * This routine may not block on kernel maps. 200136638Swollman */ 201136638Swollmanvoid 202136638Swollmankmem_free(map, addr, size) 20393799Swollman vm_map_t map; 20493799Swollman vm_offset_t addr; 20593799Swollman vm_size_t size; 206136638Swollman{ 20793799Swollman 20893799Swollman (void) vm_map_remove(map, trunc_page(addr), round_page(addr + size)); 2092742Swollman} 21020094Swollman 211136638Swollman/* 212136638Swollman * kmem_suballoc: 21393799Swollman * 21419878Swollman * Allocates a map to manage a subrange 21558787Sru * of the kernel virtual address space. 21693799Swollman * 21793799Swollman * Arguments are as follows: 21864499Swollman * 21920094Swollman * parent Map to take range from 22020094Swollman * min, max Returned endpoints of map 221136638Swollman * size Size of range to find 222136638Swollman */ 22320094Swollmanvm_map_t 22493799Swollmankmem_suballoc(parent, min, max, size) 22593799Swollman vm_map_t parent; 22693799Swollman vm_offset_t *min, *max; 22793799Swollman vm_size_t size; 22893799Swollman{ 22993799Swollman int ret; 23093799Swollman vm_map_t result; 23193799Swollman 23293799Swollman size = round_page(size); 233136638Swollman 23493799Swollman *min = (vm_offset_t) vm_map_min(parent); 23520094Swollman ret = vm_map_find(parent, NULL, (vm_offset_t) 0, 23658787Sru min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0); 23793799Swollman if (ret != KERN_SUCCESS) { 23893799Swollman printf("kmem_suballoc: bad status return of %d.\n", ret); 23993799Swollman panic("kmem_suballoc"); 24093799Swollman } 24120094Swollman *max = *min + size; 24220094Swollman result = vm_map_create(vm_map_pmap(parent), *min, *max); 243136638Swollman if (result == NULL) 244136638Swollman panic("kmem_suballoc: cannot create submap"); 245136638Swollman if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS) 246136638Swollman panic("kmem_suballoc: unable to change range to submap"); 247136638Swollman return (result); 248136638Swollman} 249136638Swollman 250136638Swollman/* 251136638Swollman * kmem_malloc: 252136638Swollman * 253136638Swollman * Allocate wired-down memory in the kernel's address map for the higher 254136638Swollman * level kernel memory allocator (kern/kern_malloc.c). We cannot use 255136638Swollman * kmem_alloc() because we may need to allocate memory at interrupt 256136638Swollman * level where we cannot block (canwait == FALSE). 257136638Swollman * 258136638Swollman * This routine has its own private kernel submap (kmem_map) and object 259136638Swollman * (kmem_object). This, combined with the fact that only malloc uses 260136638Swollman * this routine, ensures that we will never block in map or object waits. 261136638Swollman * 262136638Swollman * Note that this still only works in a uni-processor environment and 263136638Swollman * when called at splhigh(). 264136638Swollman * 265136638Swollman * We don't worry about expanding the map (adding entries) since entries 266136638Swollman * for wired maps are statically allocated. 267136638Swollman * 268136638Swollman * NOTE: This routine is not supposed to block if M_NOWAIT is set, but 269136638Swollman * I have not verified that it actually does not block. 270136638Swollman * 271136638Swollman * `map' is ONLY allowed to be kmem_map or one of the mbuf submaps to 272136638Swollman * which we never free. 273136638Swollman */ 274136638Swollmanvm_offset_t 275136638Swollmankmem_malloc(map, size, flags) 276136638Swollman vm_map_t map; 277136638Swollman vm_size_t size; 278136638Swollman int flags; 279136638Swollman{ 280136638Swollman vm_offset_t offset, i; 281136638Swollman vm_map_entry_t entry; 28220094Swollman vm_offset_t addr; 283136638Swollman vm_page_t m; 28493799Swollman int pflags; 28520094Swollman 28620094Swollman size = round_page(size); 28793799Swollman addr = vm_map_min(map); 28893799Swollman 28993799Swollman /* 29020094Swollman * Locate sufficient space in the map. This will give us the final 29193799Swollman * virtual address for the new memory, and thus will tell us the 29293799Swollman * offset within the kernel map. 29393799Swollman */ 29420094Swollman vm_map_lock(map); 29520094Swollman if (vm_map_findspace(map, vm_map_min(map), size, &addr)) { 296149514Swollman vm_map_unlock(map); 297136638Swollman if ((flags & M_NOWAIT) == 0) 29893799Swollman panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated", 29920094Swollman (long)size, (long)map->size); 30058787Sru return (0); 30193799Swollman } 30293799Swollman offset = addr - VM_MIN_KERNEL_ADDRESS; 30393799Swollman vm_object_reference(kmem_object); 30493799Swollman vm_map_insert(map, kmem_object, offset, addr, addr + size, 305136638Swollman VM_PROT_ALL, VM_PROT_ALL, 0); 306136638Swollman 30720094Swollman /* 30820094Swollman * Note: if M_NOWAIT specified alone, allocate from 30920094Swollman * interrupt-safe queues only (just the free list). If 310136638Swollman * M_USE_RESERVE is also specified, we can also 31193799Swollman * allocate from the cache. Neither of the latter two 31220094Swollman * flags may be specified from an interrupt since interrupts 31320094Swollman * are not allowed to mess with the cache queue. 31493799Swollman */ 31593799Swollman 31693799Swollman if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT) 31720094Swollman pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED; 31820094Swollman else 31920094Swollman pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED; 32093799Swollman 32193799Swollman if (flags & M_ZERO) 322136638Swollman pflags |= VM_ALLOC_ZERO; 323136638Swollman 32420094Swollman VM_OBJECT_LOCK(kmem_object); 325136638Swollman for (i = 0; i < size; i += PAGE_SIZE) { 326136638Swollmanretry: 327136638Swollman m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags); 328136638Swollman 329136638Swollman /* 330136638Swollman * Ran out of space, free everything up and return. Don't need 331136638Swollman * to lock page queues here as we know that the pages we got 332136638Swollman * aren't on any queues. 333136638Swollman */ 334136638Swollman if (m == NULL) { 335136638Swollman if ((flags & M_NOWAIT) == 0) { 336136638Swollman VM_OBJECT_UNLOCK(kmem_object); 337136638Swollman vm_map_unlock(map); 338136638Swollman VM_WAIT; 339136638Swollman vm_map_lock(map); 340136638Swollman VM_OBJECT_LOCK(kmem_object); 341136638Swollman goto retry; 342136638Swollman } 343136638Swollman /* 344136638Swollman * Free the pages before removing the map entry. 345136638Swollman * They are already marked busy. Calling 346136638Swollman * vm_map_delete before the pages has been freed or 3478029Swollman * unbusied will cause a deadlock. 34814343Swollman */ 34914343Swollman while (i != 0) { 35014343Swollman i -= PAGE_SIZE; 35119878Swollman m = vm_page_lookup(kmem_object, 35214343Swollman OFF_TO_IDX(offset + i)); 35314343Swollman vm_page_lock_queues(); 3542742Swollman vm_page_unwire(m, 0); 3552742Swollman vm_page_free(m); 3562742Swollman vm_page_unlock_queues(); 35786222Swollman } 35819878Swollman VM_OBJECT_UNLOCK(kmem_object); 35919878Swollman vm_map_delete(map, addr, addr + size); 3602742Swollman vm_map_unlock(map); 3612742Swollman return (0); 3622742Swollman } 363149514Swollman if (flags & M_ZERO && (m->flags & PG_ZERO) == 0) 3642742Swollman pmap_zero_page(m); 3652742Swollman m->valid = VM_PAGE_BITS_ALL; 3662742Swollman KASSERT((m->flags & PG_UNMANAGED) != 0, 3672742Swollman ("kmem_malloc: page %p is managed", m)); 3682742Swollman } 3692742Swollman VM_OBJECT_UNLOCK(kmem_object); 37020094Swollman 37120094Swollman /* 37220094Swollman * Mark map entry as non-pageable. Assert: vm_map_insert() will never 37320094Swollman * be able to extend the previous entry so there will be a new entry 37420094Swollman * exactly corresponding to this address range and it will have 37520094Swollman * wired_count == 0. 37620094Swollman */ 37720094Swollman if (!vm_map_lookup_entry(map, addr, &entry) || 37820094Swollman entry->start != addr || entry->end != addr + size || 37920094Swollman entry->wired_count != 0) 38020094Swollman panic("kmem_malloc: entry not found or misaligned"); 38120094Swollman entry->wired_count = 1; 38220094Swollman 38320094Swollman /* 38420094Swollman * At this point, the kmem_object must be unlocked because 38520094Swollman * vm_map_simplify_entry() calls vm_object_deallocate(), which 38620094Swollman * locks the kmem_object. 38720094Swollman */ 38820094Swollman vm_map_simplify_entry(map, entry); 38920094Swollman 39020094Swollman /* 39120094Swollman * Loop thru pages, entering them in the pmap. 39220094Swollman */ 39320094Swollman VM_OBJECT_LOCK(kmem_object); 39420094Swollman for (i = 0; i < size; i += PAGE_SIZE) { 39520094Swollman m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i)); 39643014Swollman /* 39743014Swollman * Because this is kernel_pmap, this call will not block. 39843014Swollman */ 39943014Swollman pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1); 40075267Swollman vm_page_wakeup(m); 40175267Swollman } 40275267Swollman VM_OBJECT_UNLOCK(kmem_object); 40375267Swollman vm_map_unlock(map); 40475267Swollman 40575267Swollman return (addr); 406105196Swollman} 407105196Swollman 408105196Swollman/* 409105196Swollman * kmem_alloc_wait: 410105196Swollman * 411105196Swollman * Allocates pageable memory from a sub-map of the kernel. If the submap 412105196Swollman * has no room, the caller sleeps waiting for more memory in the submap. 413105196Swollman * 414105196Swollman * This routine may block. 415105196Swollman */ 416105196Swollmanvm_offset_t 417105196Swollmankmem_alloc_wait(map, size) 418105196Swollman vm_map_t map; 419105196Swollman vm_size_t size; 420105196Swollman{ 421105196Swollman vm_offset_t addr; 422105196Swollman 423136638Swollman size = round_page(size); 424136638Swollman 425136638Swollman for (;;) { 426136638Swollman /* 427136638Swollman * To make this work for more than one map, use the map's lock 428105196Swollman * to lock out sleepers/wakers. 429105196Swollman */ 43043014Swollman vm_map_lock(map); 43143014Swollman if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0) 432105196Swollman break; 43367578Swollman /* no space now; see if we can ever get space */ 43467578Swollman if (vm_map_max(map) - vm_map_min(map) < size) { 43567578Swollman vm_map_unlock(map); 43667578Swollman return (0); 43743014Swollman } 4382742Swollman map->needs_wakeup = TRUE; 43943543Swollman vm_map_unlock_and_wait(map, FALSE); 44043543Swollman } 44158787Sru vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0); 44258787Sru vm_map_unlock(map); 44358787Sru return (addr); 44443543Swollman} 44543014Swollman 44643543Swollman/* 44743543Swollman * kmem_free_wakeup: 44858787Sru * 44958787Sru * Returns memory to a submap of the kernel, and wakes up any processes 45058787Sru * waiting for memory in that map. 45143543Swollman */ 45258787Sruvoid 45343543Swollmankmem_free_wakeup(map, addr, size) 45443014Swollman vm_map_t map; 45543543Swollman vm_offset_t addr; 45643543Swollman vm_size_t size; 45743543Swollman{ 45843543Swollman 45943543Swollman vm_map_lock(map); 46058787Sru (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size)); 46143543Swollman if (map->needs_wakeup) { 46243543Swollman map->needs_wakeup = FALSE; 46358787Sru vm_map_wakeup(map); 46443543Swollman } 46558787Sru vm_map_unlock(map); 46658787Sru} 46743543Swollman 46858787Sru/* 46943543Swollman * kmem_init: 47058787Sru * 47158787Sru * Create the kernel map; insert a mapping covering kernel text, 47243543Swollman * data, bss, and all space allocated thus far (`boostrap' data). The 47343543Swollman * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and 47443543Swollman * `start' as allocated, and the range between `start' and `end' as free. 47558787Sru */ 47643543Swollmanvoid 47743543Swollmankmem_init(start, end) 47858787Sru vm_offset_t start, end; 47943543Swollman{ 48058787Sru vm_map_t m; 48158787Sru 48243543Swollman m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end); 48358787Sru m->system_map = 1; 48458787Sru vm_map_lock(m); 48543543Swollman /* N.B.: cannot use kgdb to debug, starting with this assignment ... */ 48643543Swollman kernel_map = m; 48758787Sru (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0, 48858787Sru VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 48943543Swollman MAP_NOFAULT); 49043543Swollman /* ... and ending with the completion of the above `insert' */ 49158787Sru vm_map_unlock(m); 49258787Sru} 49343543Swollman