Home | History | Annotate | Line # | Download | only in uvm
uvm_km.c revision 1.38
      1 /*	$NetBSD: uvm_km.c,v 1.38 2000/07/24 20:10:53 jeffs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993, The Regents of the University of California.
      6  *
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * The Mach Operating System project at Carnegie-Mellon University.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by Charles D. Cranor,
     23  *      Washington University, the University of California, Berkeley and
     24  *      its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)vm_kern.c   8.3 (Berkeley) 1/12/94
     42  * from: Id: uvm_km.c,v 1.1.2.14 1998/02/06 05:19:27 chs Exp
     43  *
     44  *
     45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     46  * All rights reserved.
     47  *
     48  * Permission to use, copy, modify and distribute this software and
     49  * its documentation is hereby granted, provided that both the copyright
     50  * notice and this permission notice appear in all copies of the
     51  * software, derivative works or modified versions, and any portions
     52  * thereof, and that both notices appear in supporting documentation.
     53  *
     54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     57  *
     58  * Carnegie Mellon requests users of this software to return to
     59  *
     60  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     61  *  School of Computer Science
     62  *  Carnegie Mellon University
     63  *  Pittsburgh PA 15213-3890
     64  *
     65  * any improvements or extensions that they make and grant Carnegie the
     66  * rights to redistribute these changes.
     67  */
     68 
     69 #include "opt_uvmhist.h"
     70 
     71 /*
     72  * uvm_km.c: handle kernel memory allocation and management
     73  */
     74 
     75 /*
     76  * overview of kernel memory management:
     77  *
     78  * the kernel virtual address space is mapped by "kernel_map."   kernel_map
     79  * starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
     80  * note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
     81  *
     82  * the kernel_map has several "submaps."   submaps can only appear in
     83  * the kernel_map (user processes can't use them).   submaps "take over"
     84  * the management of a sub-range of the kernel's address space.  submaps
     85  * are typically allocated at boot time and are never released.   kernel
     86  * virtual address space that is mapped by a submap is locked by the
     87  * submap's lock -- not the kernel_map's lock.
     88  *
     89  * thus, the useful feature of submaps is that they allow us to break
     90  * up the locking and protection of the kernel address space into smaller
     91  * chunks.
     92  *
     93  * the vm system has several standard kernel submaps, including:
     94  *   kmem_map => contains only wired kernel memory for the kernel
     95  *		malloc.   *** access to kmem_map must be protected
     96  *		by splimp() because we are allowed to call malloc()
     97  *		at interrupt time ***
     98  *   mb_map => memory for large mbufs,  *** protected by splimp ***
     99  *   pager_map => used to map "buf" structures into kernel space
    100  *   exec_map => used during exec to handle exec args
    101  *   etc...
    102  *
    103  * the kernel allocates its private memory out of special uvm_objects whose
    104  * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
    105  * are "special" and never die).   all kernel objects should be thought of
    106  * as large, fixed-sized, sparsely populated uvm_objects.   each kernel
    107  * object is equal to the size of kernel virtual address space (i.e. the
    108  * value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
    109  *
    110  * most kernel private memory lives in kernel_object.   the only exception
    111  * to this is for memory that belongs to submaps that must be protected
    112  * by splimp().    each of these submaps has their own private kernel
    113  * object (e.g. kmem_object, mb_object).
    114  *
    115  * note that just because a kernel object spans the entire kernel virutal
    116  * address space doesn't mean that it has to be mapped into the entire space.
    117  * large chunks of a kernel object's space go unused either because
    118  * that area of kernel VM is unmapped, or there is some other type of
    119  * object mapped into that range (e.g. a vnode).    for submap's kernel
    120  * objects, the only part of the object that can ever be populated is the
    121  * offsets that are managed by the submap.
    122  *
    123  * note that the "offset" in a kernel object is always the kernel virtual
    124  * address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
    125  * example:
    126  *   suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
    127  *   uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
    128  *   kernel map].    if uvm_km_alloc returns virtual address 0xf8235000,
    129  *   then that means that the page at offset 0x235000 in kernel_object is
    130  *   mapped at 0xf8235000.
    131  *
    132  * note that the offsets in kmem_object and mb_object also follow this
    133  * rule.   this means that the offsets for kmem_object must fall in the
    134  * range of [vm_map_min(kmem_object) - vm_map_min(kernel_map)] to
    135  * [vm_map_max(kmem_object) - vm_map_min(kernel_map)], so the offsets
    136  * in those objects will typically not start at zero.
    137  *
    138  * kernel object have one other special property: when the kernel virtual
    139  * memory mapping them is unmapped, the backing memory in the object is
    140  * freed right away.   this is done with the uvm_km_pgremove() function.
    141  * this has to be done because there is no backing store for kernel pages
    142  * and no need to save them after they are no longer referenced.
    143  */
    144 
    145 #include <sys/param.h>
    146 #include <sys/systm.h>
    147 #include <sys/proc.h>
    148 
    149 #include <uvm/uvm.h>
    150 
    151 /*
    152  * global data structures
    153  */
    154 
    155 vm_map_t kernel_map = NULL;
    156 
    157 struct vmi_list vmi_list;
    158 simple_lock_data_t vmi_list_slock;
    159 
    160 /*
    161  * local data structues
    162  */
    163 
    164 static struct vm_map		kernel_map_store;
    165 static struct uvm_object	kmem_object_store;
    166 static struct uvm_object	mb_object_store;
    167 
    168 /*
    169  * All pager operations here are NULL, but the object must have
    170  * a pager ops vector associated with it; various places assume
    171  * it to be so.
    172  */
    173 static struct uvm_pagerops	km_pager;
    174 
    175 /*
    176  * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
    177  * KVM already allocated for text, data, bss, and static data structures).
    178  *
    179  * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
    180  *    we assume that [min -> start] has already been allocated and that
    181  *    "end" is the end.
    182  */
    183 
    184 void
    185 uvm_km_init(start, end)
    186 	vaddr_t start, end;
    187 {
    188 	vaddr_t base = VM_MIN_KERNEL_ADDRESS;
    189 
    190 	/*
    191 	 * first, initialize the interrupt-safe map list.
    192 	 */
    193 	LIST_INIT(&vmi_list);
    194 	simple_lock_init(&vmi_list_slock);
    195 
    196 	/*
    197 	 * next, init kernel memory objects.
    198 	 */
    199 
    200 	/* kernel_object: for pageable anonymous kernel memory */
    201 	uao_init();
    202 	uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
    203 				 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
    204 
    205 	/*
    206 	 * kmem_object: for use by the kernel malloc().  Memory is always
    207 	 * wired, and this object (and the kmem_map) can be accessed at
    208 	 * interrupt time.
    209 	 */
    210 	simple_lock_init(&kmem_object_store.vmobjlock);
    211 	kmem_object_store.pgops = &km_pager;
    212 	TAILQ_INIT(&kmem_object_store.memq);
    213 	kmem_object_store.uo_npages = 0;
    214 	/* we are special.  we never die */
    215 	kmem_object_store.uo_refs = UVM_OBJ_KERN_INTRSAFE;
    216 	uvmexp.kmem_object = &kmem_object_store;
    217 
    218 	/*
    219 	 * mb_object: for mbuf cluster pages on platforms which use the
    220 	 * mb_map.  Memory is always wired, and this object (and the mb_map)
    221 	 * can be accessed at interrupt time.
    222 	 */
    223 	simple_lock_init(&mb_object_store.vmobjlock);
    224 	mb_object_store.pgops = &km_pager;
    225 	TAILQ_INIT(&mb_object_store.memq);
    226 	mb_object_store.uo_npages = 0;
    227 	/* we are special.  we never die */
    228 	mb_object_store.uo_refs = UVM_OBJ_KERN_INTRSAFE;
    229 	uvmexp.mb_object = &mb_object_store;
    230 
    231 	/*
    232 	 * init the map and reserve allready allocated kernel space
    233 	 * before installing.
    234 	 */
    235 
    236 	uvm_map_setup(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
    237 	kernel_map_store.pmap = pmap_kernel();
    238 	if (uvm_map(&kernel_map_store, &base, start - base, NULL,
    239 	    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    240 	    UVM_INH_NONE, UVM_ADV_RANDOM,UVM_FLAG_FIXED)) != KERN_SUCCESS)
    241 		panic("uvm_km_init: could not reserve space for kernel");
    242 
    243 	/*
    244 	 * install!
    245 	 */
    246 
    247 	kernel_map = &kernel_map_store;
    248 }
    249 
    250 /*
    251  * uvm_km_suballoc: allocate a submap in the kernel map.   once a submap
    252  * is allocated all references to that area of VM must go through it.  this
    253  * allows the locking of VAs in kernel_map to be broken up into regions.
    254  *
    255  * => if `fixed' is true, *min specifies where the region described
    256  *      by the submap must start
    257  * => if submap is non NULL we use that as the submap, otherwise we
    258  *	alloc a new map
    259  */
    260 struct vm_map *
    261 uvm_km_suballoc(map, min, max, size, flags, fixed, submap)
    262 	struct vm_map *map;
    263 	vaddr_t *min, *max;		/* OUT, OUT */
    264 	vsize_t size;
    265 	int flags;
    266 	boolean_t fixed;
    267 	struct vm_map *submap;
    268 {
    269 	int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
    270 
    271 	size = round_page(size);	/* round up to pagesize */
    272 
    273 	/*
    274 	 * first allocate a blank spot in the parent map
    275 	 */
    276 
    277 	if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET,
    278 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    279 	    UVM_ADV_RANDOM, mapflags)) != KERN_SUCCESS) {
    280 	       panic("uvm_km_suballoc: unable to allocate space in parent map");
    281 	}
    282 
    283 	/*
    284 	 * set VM bounds (min is filled in by uvm_map)
    285 	 */
    286 
    287 	*max = *min + size;
    288 
    289 	/*
    290 	 * add references to pmap and create or init the submap
    291 	 */
    292 
    293 	pmap_reference(vm_map_pmap(map));
    294 	if (submap == NULL) {
    295 		submap = uvm_map_create(vm_map_pmap(map), *min, *max, flags);
    296 		if (submap == NULL)
    297 			panic("uvm_km_suballoc: unable to create submap");
    298 	} else {
    299 		uvm_map_setup(submap, *min, *max, flags);
    300 		submap->pmap = vm_map_pmap(map);
    301 	}
    302 
    303 	/*
    304 	 * now let uvm_map_submap plug in it...
    305 	 */
    306 
    307 	if (uvm_map_submap(map, *min, *max, submap) != KERN_SUCCESS)
    308 		panic("uvm_km_suballoc: submap allocation failed");
    309 
    310 	return(submap);
    311 }
    312 
    313 /*
    314  * uvm_km_pgremove: remove pages from a kernel uvm_object.
    315  *
    316  * => when you unmap a part of anonymous kernel memory you want to toss
    317  *    the pages right away.    (this gets called from uvm_unmap_...).
    318  */
    319 
    320 #define UKM_HASH_PENALTY 4      /* a guess */
    321 
    322 void
    323 uvm_km_pgremove(uobj, start, end)
    324 	struct uvm_object *uobj;
    325 	vaddr_t start, end;
    326 {
    327 	boolean_t by_list;
    328 	struct vm_page *pp, *ppnext;
    329 	vaddr_t curoff;
    330 	UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
    331 
    332 	simple_lock(&uobj->vmobjlock);		/* lock object */
    333 
    334 #ifdef DIAGNOSTIC
    335 	if (__predict_false(uobj->pgops != &aobj_pager))
    336 		panic("uvm_km_pgremove: object %p not an aobj", uobj);
    337 #endif
    338 
    339 	/* choose cheapest traversal */
    340 	by_list = (uobj->uo_npages <=
    341 	     ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
    342 
    343 	if (by_list)
    344 		goto loop_by_list;
    345 
    346 	/* by hash */
    347 
    348 	for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
    349 		pp = uvm_pagelookup(uobj, curoff);
    350 		if (pp == NULL)
    351 			continue;
    352 
    353 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    354 		    pp->flags & PG_BUSY, 0, 0);
    355 
    356 		/* now do the actual work */
    357 		if (pp->flags & PG_BUSY) {
    358 			/* owner must check for this when done */
    359 			pp->flags |= PG_RELEASED;
    360 		} else {
    361 			/* free the swap slot... */
    362 			uao_dropswap(uobj, curoff >> PAGE_SHIFT);
    363 
    364 			/*
    365 			 * ...and free the page; note it may be on the
    366 			 * active or inactive queues.
    367 			 */
    368 			uvm_lock_pageq();
    369 			uvm_pagefree(pp);
    370 			uvm_unlock_pageq();
    371 		}
    372 		/* done */
    373 	}
    374 	simple_unlock(&uobj->vmobjlock);
    375 	return;
    376 
    377 loop_by_list:
    378 
    379 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
    380 		ppnext = pp->listq.tqe_next;
    381 		if (pp->offset < start || pp->offset >= end) {
    382 			continue;
    383 		}
    384 
    385 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    386 		    pp->flags & PG_BUSY, 0, 0);
    387 
    388 		/* now do the actual work */
    389 		if (pp->flags & PG_BUSY) {
    390 			/* owner must check for this when done */
    391 			pp->flags |= PG_RELEASED;
    392 		} else {
    393 			/* free the swap slot... */
    394 			uao_dropswap(uobj, pp->offset >> PAGE_SHIFT);
    395 
    396 			/*
    397 			 * ...and free the page; note it may be on the
    398 			 * active or inactive queues.
    399 			 */
    400 			uvm_lock_pageq();
    401 			uvm_pagefree(pp);
    402 			uvm_unlock_pageq();
    403 		}
    404 		/* done */
    405 	}
    406 	simple_unlock(&uobj->vmobjlock);
    407 	return;
    408 }
    409 
    410 
    411 /*
    412  * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for "intrsafe"
    413  *    objects
    414  *
    415  * => when you unmap a part of anonymous kernel memory you want to toss
    416  *    the pages right away.    (this gets called from uvm_unmap_...).
    417  * => none of the pages will ever be busy, and none of them will ever
    418  *    be on the active or inactive queues (because these objects are
    419  *    never allowed to "page").
    420  */
    421 
    422 void
    423 uvm_km_pgremove_intrsafe(uobj, start, end)
    424 	struct uvm_object *uobj;
    425 	vaddr_t start, end;
    426 {
    427 	boolean_t by_list;
    428 	struct vm_page *pp, *ppnext;
    429 	vaddr_t curoff;
    430 	UVMHIST_FUNC("uvm_km_pgremove_intrsafe"); UVMHIST_CALLED(maphist);
    431 
    432 	simple_lock(&uobj->vmobjlock);		/* lock object */
    433 
    434 #ifdef DIAGNOSTIC
    435 	if (__predict_false(UVM_OBJ_IS_INTRSAFE_OBJECT(uobj) == 0))
    436 		panic("uvm_km_pgremove_intrsafe: object %p not intrsafe", uobj);
    437 #endif
    438 
    439 	/* choose cheapest traversal */
    440 	by_list = (uobj->uo_npages <=
    441 	     ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
    442 
    443 	if (by_list)
    444 		goto loop_by_list;
    445 
    446 	/* by hash */
    447 
    448 	for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
    449 		pp = uvm_pagelookup(uobj, curoff);
    450 		if (pp == NULL)
    451 			continue;
    452 
    453 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    454 		    pp->flags & PG_BUSY, 0, 0);
    455 #ifdef DIAGNOSTIC
    456 		if (__predict_false(pp->flags & PG_BUSY))
    457 			panic("uvm_km_pgremove_intrsafe: busy page");
    458 		if (__predict_false(pp->pqflags & PQ_ACTIVE))
    459 			panic("uvm_km_pgremove_intrsafe: active page");
    460 		if (__predict_false(pp->pqflags & PQ_INACTIVE))
    461 			panic("uvm_km_pgremove_intrsafe: inactive page");
    462 #endif
    463 
    464 		/* free the page */
    465 		uvm_pagefree(pp);
    466 	}
    467 	simple_unlock(&uobj->vmobjlock);
    468 	return;
    469 
    470 loop_by_list:
    471 
    472 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
    473 		ppnext = pp->listq.tqe_next;
    474 		if (pp->offset < start || pp->offset >= end) {
    475 			continue;
    476 		}
    477 
    478 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    479 		    pp->flags & PG_BUSY, 0, 0);
    480 
    481 #ifdef DIAGNOSTIC
    482 		if (__predict_false(pp->flags & PG_BUSY))
    483 			panic("uvm_km_pgremove_intrsafe: busy page");
    484 		if (__predict_false(pp->pqflags & PQ_ACTIVE))
    485 			panic("uvm_km_pgremove_intrsafe: active page");
    486 		if (__predict_false(pp->pqflags & PQ_INACTIVE))
    487 			panic("uvm_km_pgremove_intrsafe: inactive page");
    488 #endif
    489 
    490 		/* free the page */
    491 		uvm_pagefree(pp);
    492 	}
    493 	simple_unlock(&uobj->vmobjlock);
    494 	return;
    495 }
    496 
    497 
    498 /*
    499  * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
    500  *
    501  * => we map wired memory into the specified map using the obj passed in
    502  * => NOTE: we can return NULL even if we can wait if there is not enough
    503  *	free VM space in the map... caller should be prepared to handle
    504  *	this case.
    505  * => we return KVA of memory allocated
    506  * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
    507  *	lock the map
    508  */
    509 
    510 vaddr_t
    511 uvm_km_kmemalloc(map, obj, size, flags)
    512 	vm_map_t map;
    513 	struct uvm_object *obj;
    514 	vsize_t size;
    515 	int flags;
    516 {
    517 	vaddr_t kva, loopva;
    518 	vaddr_t offset;
    519 	struct vm_page *pg;
    520 	UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
    521 
    522 
    523 	UVMHIST_LOG(maphist,"  (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
    524 	map, obj, size, flags);
    525 #ifdef DIAGNOSTIC
    526 	/* sanity check */
    527 	if (__predict_false(vm_map_pmap(map) != pmap_kernel()))
    528 		panic("uvm_km_kmemalloc: invalid map");
    529 #endif
    530 
    531 	/*
    532 	 * setup for call
    533 	 */
    534 
    535 	size = round_page(size);
    536 	kva = vm_map_min(map);	/* hint */
    537 
    538 	/*
    539 	 * allocate some virtual space
    540 	 */
    541 
    542 	if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
    543 	      UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    544 			  UVM_ADV_RANDOM, (flags & UVM_KMF_TRYLOCK)))
    545 			!= KERN_SUCCESS)) {
    546 		UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
    547 		return(0);
    548 	}
    549 
    550 	/*
    551 	 * if all we wanted was VA, return now
    552 	 */
    553 
    554 	if (flags & UVM_KMF_VALLOC) {
    555 		UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
    556 		return(kva);
    557 	}
    558 	/*
    559 	 * recover object offset from virtual address
    560 	 */
    561 
    562 	offset = kva - vm_map_min(kernel_map);
    563 	UVMHIST_LOG(maphist, "  kva=0x%x, offset=0x%x", kva, offset,0,0);
    564 
    565 	/*
    566 	 * now allocate and map in the memory... note that we are the only ones
    567 	 * whom should ever get a handle on this area of VM.
    568 	 */
    569 
    570 	loopva = kva;
    571 	while (size) {
    572 		simple_lock(&obj->vmobjlock);
    573 		pg = uvm_pagealloc(obj, offset, NULL, 0);
    574 		if (pg) {
    575 			pg->flags &= ~PG_BUSY;	/* new page */
    576 			UVM_PAGE_OWN(pg, NULL);
    577 		}
    578 		simple_unlock(&obj->vmobjlock);
    579 
    580 		/*
    581 		 * out of memory?
    582 		 */
    583 
    584 		if (__predict_false(pg == NULL)) {
    585 			if (flags & UVM_KMF_NOWAIT) {
    586 				/* free everything! */
    587 				uvm_unmap(map, kva, kva + size);
    588 				return(0);
    589 			} else {
    590 				uvm_wait("km_getwait2");	/* sleep here */
    591 				continue;
    592 			}
    593 		}
    594 
    595 		/*
    596 		 * map it in: note that we call pmap_enter with the map and
    597 		 * object unlocked in case we are kmem_map/kmem_object
    598 		 * (because if pmap_enter wants to allocate out of kmem_object
    599 		 * it will need to lock it itself!)
    600 		 */
    601 		if (UVM_OBJ_IS_INTRSAFE_OBJECT(obj)) {
    602 			pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
    603 			    VM_PROT_ALL);
    604 		} else {
    605 			pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    606 			    UVM_PROT_ALL,
    607 			    PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    608 		}
    609 		loopva += PAGE_SIZE;
    610 		offset += PAGE_SIZE;
    611 		size -= PAGE_SIZE;
    612 	}
    613 
    614 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    615 	return(kva);
    616 }
    617 
    618 /*
    619  * uvm_km_free: free an area of kernel memory
    620  */
    621 
    622 void
    623 uvm_km_free(map, addr, size)
    624 	vm_map_t map;
    625 	vaddr_t addr;
    626 	vsize_t size;
    627 {
    628 
    629 	uvm_unmap(map, trunc_page(addr), round_page(addr+size));
    630 }
    631 
    632 /*
    633  * uvm_km_free_wakeup: free an area of kernel memory and wake up
    634  * anyone waiting for vm space.
    635  *
    636  * => XXX: "wanted" bit + unlock&wait on other end?
    637  */
    638 
    639 void
    640 uvm_km_free_wakeup(map, addr, size)
    641 	vm_map_t map;
    642 	vaddr_t addr;
    643 	vsize_t size;
    644 {
    645 	vm_map_entry_t dead_entries;
    646 
    647 	vm_map_lock(map);
    648 	(void)uvm_unmap_remove(map, trunc_page(addr), round_page(addr+size),
    649 			 &dead_entries);
    650 	wakeup(map);
    651 	vm_map_unlock(map);
    652 
    653 	if (dead_entries != NULL)
    654 		uvm_unmap_detach(dead_entries, 0);
    655 }
    656 
    657 /*
    658  * uvm_km_alloc1: allocate wired down memory in the kernel map.
    659  *
    660  * => we can sleep if needed
    661  */
    662 
    663 vaddr_t
    664 uvm_km_alloc1(map, size, zeroit)
    665 	vm_map_t map;
    666 	vsize_t size;
    667 	boolean_t zeroit;
    668 {
    669 	vaddr_t kva, loopva, offset;
    670 	struct vm_page *pg;
    671 	UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
    672 
    673 	UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
    674 
    675 #ifdef DIAGNOSTIC
    676 	if (vm_map_pmap(map) != pmap_kernel())
    677 		panic("uvm_km_alloc1");
    678 #endif
    679 
    680 	size = round_page(size);
    681 	kva = vm_map_min(map);		/* hint */
    682 
    683 	/*
    684 	 * allocate some virtual space
    685 	 */
    686 
    687 	if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
    688 	      UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    689 					      UVM_INH_NONE, UVM_ADV_RANDOM,
    690 					      0)) != KERN_SUCCESS)) {
    691 		UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
    692 		return(0);
    693 	}
    694 
    695 	/*
    696 	 * recover object offset from virtual address
    697 	 */
    698 
    699 	offset = kva - vm_map_min(kernel_map);
    700 	UVMHIST_LOG(maphist,"  kva=0x%x, offset=0x%x", kva, offset,0,0);
    701 
    702 	/*
    703 	 * now allocate the memory.  we must be careful about released pages.
    704 	 */
    705 
    706 	loopva = kva;
    707 	while (size) {
    708 		simple_lock(&uvm.kernel_object->vmobjlock);
    709 		pg = uvm_pagelookup(uvm.kernel_object, offset);
    710 
    711 		/*
    712 		 * if we found a page in an unallocated region, it must be
    713 		 * released
    714 		 */
    715 		if (pg) {
    716 			if ((pg->flags & PG_RELEASED) == 0)
    717 				panic("uvm_km_alloc1: non-released page");
    718 			pg->flags |= PG_WANTED;
    719 			UVM_UNLOCK_AND_WAIT(pg, &uvm.kernel_object->vmobjlock,
    720 			    FALSE, "km_alloc", 0);
    721 			continue;   /* retry */
    722 		}
    723 
    724 		/* allocate ram */
    725 		pg = uvm_pagealloc(uvm.kernel_object, offset, NULL, 0);
    726 		if (pg) {
    727 			pg->flags &= ~PG_BUSY;	/* new page */
    728 			UVM_PAGE_OWN(pg, NULL);
    729 		}
    730 		simple_unlock(&uvm.kernel_object->vmobjlock);
    731 		if (__predict_false(pg == NULL)) {
    732 			uvm_wait("km_alloc1w");	/* wait for memory */
    733 			continue;
    734 		}
    735 
    736 		/*
    737 		 * map it in; note we're never called with an intrsafe
    738 		 * object, so we always use regular old pmap_enter().
    739 		 */
    740 		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    741 		    UVM_PROT_ALL, PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    742 
    743 		loopva += PAGE_SIZE;
    744 		offset += PAGE_SIZE;
    745 		size -= PAGE_SIZE;
    746 	}
    747 
    748 	/*
    749 	 * zero on request (note that "size" is now zero due to the above loop
    750 	 * so we need to subtract kva from loopva to reconstruct the size).
    751 	 */
    752 
    753 	if (zeroit)
    754 		memset((caddr_t)kva, 0, loopva - kva);
    755 
    756 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    757 	return(kva);
    758 }
    759 
    760 /*
    761  * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
    762  *
    763  * => memory is not allocated until fault time
    764  */
    765 
    766 vaddr_t
    767 uvm_km_valloc(map, size)
    768 	vm_map_t map;
    769 	vsize_t size;
    770 {
    771 	vaddr_t kva;
    772 	UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
    773 
    774 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
    775 
    776 #ifdef DIAGNOSTIC
    777 	if (__predict_false(vm_map_pmap(map) != pmap_kernel()))
    778 		panic("uvm_km_valloc");
    779 #endif
    780 
    781 	size = round_page(size);
    782 	kva = vm_map_min(map);		/* hint */
    783 
    784 	/*
    785 	 * allocate some virtual space.  will be demand filled by kernel_object.
    786 	 */
    787 
    788 	if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
    789 	    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    790 					    UVM_INH_NONE, UVM_ADV_RANDOM,
    791 					    0)) != KERN_SUCCESS)) {
    792 		UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
    793 		return(0);
    794 	}
    795 
    796 	UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
    797 	return(kva);
    798 }
    799 
    800 /*
    801  * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
    802  *
    803  * => memory is not allocated until fault time
    804  * => if no room in map, wait for space to free, unless requested size
    805  *    is larger than map (in which case we return 0)
    806  */
    807 
    808 vaddr_t
    809 uvm_km_valloc_prefer_wait(map, size, prefer)
    810 	vm_map_t map;
    811 	vsize_t size;
    812 	voff_t prefer;
    813 {
    814 	vaddr_t kva;
    815 	UVMHIST_FUNC("uvm_km_valloc_prefer_wait"); UVMHIST_CALLED(maphist);
    816 
    817 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
    818 
    819 #ifdef DIAGNOSTIC
    820 	if (__predict_false(vm_map_pmap(map) != pmap_kernel()))
    821 		panic("uvm_km_valloc_wait");
    822 #endif
    823 
    824 	size = round_page(size);
    825 	if (size > vm_map_max(map) - vm_map_min(map))
    826 		return(0);
    827 
    828 	while (1) {
    829 		kva = vm_map_min(map);		/* hint */
    830 
    831 		/*
    832 		 * allocate some virtual space.   will be demand filled
    833 		 * by kernel_object.
    834 		 */
    835 
    836 		if (__predict_true(uvm_map(map, &kva, size, uvm.kernel_object,
    837 		    prefer, UVM_MAPFLAG(UVM_PROT_ALL,
    838 		    UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, 0))
    839 		    == KERN_SUCCESS)) {
    840 			UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    841 			return(kva);
    842 		}
    843 
    844 		/*
    845 		 * failed.  sleep for a while (on map)
    846 		 */
    847 
    848 		UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
    849 		tsleep((caddr_t)map, PVM, "vallocwait", 0);
    850 	}
    851 	/*NOTREACHED*/
    852 }
    853 
    854 vaddr_t
    855 uvm_km_valloc_wait(map, size)
    856 	vm_map_t map;
    857 	vsize_t size;
    858 {
    859 	return uvm_km_valloc_prefer_wait(map, size, UVM_UNKNOWN_OFFSET);
    860 }
    861 
    862 /* Sanity; must specify both or none. */
    863 #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
    864     (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
    865 #error Must specify MAP and UNMAP together.
    866 #endif
    867 
    868 /*
    869  * uvm_km_alloc_poolpage: allocate a page for the pool allocator
    870  *
    871  * => if the pmap specifies an alternate mapping method, we use it.
    872  */
    873 
    874 /* ARGSUSED */
    875 vaddr_t
    876 uvm_km_alloc_poolpage1(map, obj, waitok)
    877 	vm_map_t map;
    878 	struct uvm_object *obj;
    879 	boolean_t waitok;
    880 {
    881 #if defined(PMAP_MAP_POOLPAGE)
    882 	struct vm_page *pg;
    883 	vaddr_t va;
    884 
    885  again:
    886 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    887 	if (__predict_false(pg == NULL)) {
    888 		if (waitok) {
    889 			uvm_wait("plpg");
    890 			goto again;
    891 		} else
    892 			return (0);
    893 	}
    894 	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
    895 	if (__predict_false(va == 0))
    896 		uvm_pagefree(pg);
    897 	return (va);
    898 #else
    899 	vaddr_t va;
    900 	int s;
    901 
    902 	/*
    903 	 * NOTE: We may be called with a map that doens't require splimp
    904 	 * protection (e.g. kernel_map).  However, it does not hurt to
    905 	 * go to splimp in this case (since unprocted maps will never be
    906 	 * accessed in interrupt context).
    907 	 *
    908 	 * XXX We may want to consider changing the interface to this
    909 	 * XXX function.
    910 	 */
    911 
    912 	s = splimp();
    913 	va = uvm_km_kmemalloc(map, obj, PAGE_SIZE, waitok ? 0 : UVM_KMF_NOWAIT);
    914 	splx(s);
    915 	return (va);
    916 #endif /* PMAP_MAP_POOLPAGE */
    917 }
    918 
    919 /*
    920  * uvm_km_free_poolpage: free a previously allocated pool page
    921  *
    922  * => if the pmap specifies an alternate unmapping method, we use it.
    923  */
    924 
    925 /* ARGSUSED */
    926 void
    927 uvm_km_free_poolpage1(map, addr)
    928 	vm_map_t map;
    929 	vaddr_t addr;
    930 {
    931 #if defined(PMAP_UNMAP_POOLPAGE)
    932 	paddr_t pa;
    933 
    934 	pa = PMAP_UNMAP_POOLPAGE(addr);
    935 	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
    936 #else
    937 	int s;
    938 
    939 	/*
    940 	 * NOTE: We may be called with a map that doens't require splimp
    941 	 * protection (e.g. kernel_map).  However, it does not hurt to
    942 	 * go to splimp in this case (since unprocted maps will never be
    943 	 * accessed in interrupt context).
    944 	 *
    945 	 * XXX We may want to consider changing the interface to this
    946 	 * XXX function.
    947 	 */
    948 
    949 	s = splimp();
    950 	uvm_km_free(map, addr, PAGE_SIZE);
    951 	splx(s);
    952 #endif /* PMAP_UNMAP_POOLPAGE */
    953 }
    954