Home | History | Annotate | Line # | Download | only in uvm
uvm_km.c revision 1.35
      1 /*	$NetBSD: uvm_km.c,v 1.35 2000/05/08 23:10:20 thorpej 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 <vm/vm.h>
    150 #include <vm/vm_page.h>
    151 #include <vm/vm_kern.h>
    152 
    153 #include <uvm/uvm.h>
    154 
    155 /*
    156  * global data structures
    157  */
    158 
    159 vm_map_t kernel_map = NULL;
    160 
    161 struct vmi_list vmi_list;
    162 simple_lock_data_t vmi_list_slock;
    163 
    164 /*
    165  * local data structues
    166  */
    167 
    168 static struct vm_map		kernel_map_store;
    169 static struct uvm_object	kmem_object_store;
    170 static struct uvm_object	mb_object_store;
    171 
    172 /*
    173  * All pager operations here are NULL, but the object must have
    174  * a pager ops vector associated with it; various places assume
    175  * it to be so.
    176  */
    177 static struct uvm_pagerops	km_pager;
    178 
    179 /*
    180  * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
    181  * KVM already allocated for text, data, bss, and static data structures).
    182  *
    183  * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
    184  *    we assume that [min -> start] has already been allocated and that
    185  *    "end" is the end.
    186  */
    187 
    188 void
    189 uvm_km_init(start, end)
    190 	vaddr_t start, end;
    191 {
    192 	vaddr_t base = VM_MIN_KERNEL_ADDRESS;
    193 
    194 	/*
    195 	 * first, initialize the interrupt-safe map list.
    196 	 */
    197 	LIST_INIT(&vmi_list);
    198 	simple_lock_init(&vmi_list_slock);
    199 
    200 	/*
    201 	 * next, init kernel memory objects.
    202 	 */
    203 
    204 	/* kernel_object: for pageable anonymous kernel memory */
    205 	uao_init();
    206 	uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
    207 				 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
    208 
    209 	/*
    210 	 * kmem_object: for use by the kernel malloc().  Memory is always
    211 	 * wired, and this object (and the kmem_map) can be accessed at
    212 	 * interrupt time.
    213 	 */
    214 	simple_lock_init(&kmem_object_store.vmobjlock);
    215 	kmem_object_store.pgops = &km_pager;
    216 	TAILQ_INIT(&kmem_object_store.memq);
    217 	kmem_object_store.uo_npages = 0;
    218 	/* we are special.  we never die */
    219 	kmem_object_store.uo_refs = UVM_OBJ_KERN_INTRSAFE;
    220 	uvmexp.kmem_object = &kmem_object_store;
    221 
    222 	/*
    223 	 * mb_object: for mbuf cluster pages on platforms which use the
    224 	 * mb_map.  Memory is always wired, and this object (and the mb_map)
    225 	 * can be accessed at interrupt time.
    226 	 */
    227 	simple_lock_init(&mb_object_store.vmobjlock);
    228 	mb_object_store.pgops = &km_pager;
    229 	TAILQ_INIT(&mb_object_store.memq);
    230 	mb_object_store.uo_npages = 0;
    231 	/* we are special.  we never die */
    232 	mb_object_store.uo_refs = UVM_OBJ_KERN_INTRSAFE;
    233 	uvmexp.mb_object = &mb_object_store;
    234 
    235 	/*
    236 	 * init the map and reserve allready allocated kernel space
    237 	 * before installing.
    238 	 */
    239 
    240 	uvm_map_setup(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
    241 	kernel_map_store.pmap = pmap_kernel();
    242 	if (uvm_map(&kernel_map_store, &base, start - base, NULL,
    243 	    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    244 	    UVM_INH_NONE, UVM_ADV_RANDOM,UVM_FLAG_FIXED)) != KERN_SUCCESS)
    245 		panic("uvm_km_init: could not reserve space for kernel");
    246 
    247 	/*
    248 	 * install!
    249 	 */
    250 
    251 	kernel_map = &kernel_map_store;
    252 }
    253 
    254 /*
    255  * uvm_km_suballoc: allocate a submap in the kernel map.   once a submap
    256  * is allocated all references to that area of VM must go through it.  this
    257  * allows the locking of VAs in kernel_map to be broken up into regions.
    258  *
    259  * => if `fixed' is true, *min specifies where the region described
    260  *      by the submap must start
    261  * => if submap is non NULL we use that as the submap, otherwise we
    262  *	alloc a new map
    263  */
    264 struct vm_map *
    265 uvm_km_suballoc(map, min, max, size, flags, fixed, submap)
    266 	struct vm_map *map;
    267 	vaddr_t *min, *max;		/* OUT, OUT */
    268 	vsize_t size;
    269 	int flags;
    270 	boolean_t fixed;
    271 	struct vm_map *submap;
    272 {
    273 	int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
    274 
    275 	size = round_page(size);	/* round up to pagesize */
    276 
    277 	/*
    278 	 * first allocate a blank spot in the parent map
    279 	 */
    280 
    281 	if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET,
    282 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    283 	    UVM_ADV_RANDOM, mapflags)) != KERN_SUCCESS) {
    284 	       panic("uvm_km_suballoc: unable to allocate space in parent map");
    285 	}
    286 
    287 	/*
    288 	 * set VM bounds (min is filled in by uvm_map)
    289 	 */
    290 
    291 	*max = *min + size;
    292 
    293 	/*
    294 	 * add references to pmap and create or init the submap
    295 	 */
    296 
    297 	pmap_reference(vm_map_pmap(map));
    298 	if (submap == NULL) {
    299 		submap = uvm_map_create(vm_map_pmap(map), *min, *max, flags);
    300 		if (submap == NULL)
    301 			panic("uvm_km_suballoc: unable to create submap");
    302 	} else {
    303 		uvm_map_setup(submap, *min, *max, flags);
    304 		submap->pmap = vm_map_pmap(map);
    305 	}
    306 
    307 	/*
    308 	 * now let uvm_map_submap plug in it...
    309 	 */
    310 
    311 	if (uvm_map_submap(map, *min, *max, submap) != KERN_SUCCESS)
    312 		panic("uvm_km_suballoc: submap allocation failed");
    313 
    314 	return(submap);
    315 }
    316 
    317 /*
    318  * uvm_km_pgremove: remove pages from a kernel uvm_object.
    319  *
    320  * => when you unmap a part of anonymous kernel memory you want to toss
    321  *    the pages right away.    (this gets called from uvm_unmap_...).
    322  */
    323 
    324 #define UKM_HASH_PENALTY 4      /* a guess */
    325 
    326 void
    327 uvm_km_pgremove(uobj, start, end)
    328 	struct uvm_object *uobj;
    329 	vaddr_t start, end;
    330 {
    331 	boolean_t by_list;
    332 	struct vm_page *pp, *ppnext;
    333 	vaddr_t curoff;
    334 	UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
    335 
    336 	simple_lock(&uobj->vmobjlock);		/* lock object */
    337 
    338 #ifdef DIAGNOSTIC
    339 	if (__predict_false(uobj->pgops != &aobj_pager))
    340 		panic("uvm_km_pgremove: object %p not an aobj", uobj);
    341 #endif
    342 
    343 	/* choose cheapest traversal */
    344 	by_list = (uobj->uo_npages <=
    345 	     ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
    346 
    347 	if (by_list)
    348 		goto loop_by_list;
    349 
    350 	/* by hash */
    351 
    352 	for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
    353 		pp = uvm_pagelookup(uobj, curoff);
    354 		if (pp == NULL)
    355 			continue;
    356 
    357 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    358 		    pp->flags & PG_BUSY, 0, 0);
    359 
    360 		/* now do the actual work */
    361 		if (pp->flags & PG_BUSY) {
    362 			/* owner must check for this when done */
    363 			pp->flags |= PG_RELEASED;
    364 		} else {
    365 			/* free the swap slot... */
    366 			uao_dropswap(uobj, curoff >> PAGE_SHIFT);
    367 
    368 			/*
    369 			 * ...and free the page; note it may be on the
    370 			 * active or inactive queues.
    371 			 */
    372 			uvm_lock_pageq();
    373 			uvm_pagefree(pp);
    374 			uvm_unlock_pageq();
    375 		}
    376 		/* done */
    377 	}
    378 	simple_unlock(&uobj->vmobjlock);
    379 	return;
    380 
    381 loop_by_list:
    382 
    383 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
    384 		ppnext = pp->listq.tqe_next;
    385 		if (pp->offset < start || pp->offset >= end) {
    386 			continue;
    387 		}
    388 
    389 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    390 		    pp->flags & PG_BUSY, 0, 0);
    391 
    392 		/* now do the actual work */
    393 		if (pp->flags & PG_BUSY) {
    394 			/* owner must check for this when done */
    395 			pp->flags |= PG_RELEASED;
    396 		} else {
    397 			/* free the swap slot... */
    398 			uao_dropswap(uobj, pp->offset >> PAGE_SHIFT);
    399 
    400 			/*
    401 			 * ...and free the page; note it may be on the
    402 			 * active or inactive queues.
    403 			 */
    404 			uvm_lock_pageq();
    405 			uvm_pagefree(pp);
    406 			uvm_unlock_pageq();
    407 		}
    408 		/* done */
    409 	}
    410 	simple_unlock(&uobj->vmobjlock);
    411 	return;
    412 }
    413 
    414 
    415 /*
    416  * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for "intrsafe"
    417  *    objects
    418  *
    419  * => when you unmap a part of anonymous kernel memory you want to toss
    420  *    the pages right away.    (this gets called from uvm_unmap_...).
    421  * => none of the pages will ever be busy, and none of them will ever
    422  *    be on the active or inactive queues (because these objects are
    423  *    never allowed to "page").
    424  */
    425 
    426 void
    427 uvm_km_pgremove_intrsafe(uobj, start, end)
    428 	struct uvm_object *uobj;
    429 	vaddr_t start, end;
    430 {
    431 	boolean_t by_list;
    432 	struct vm_page *pp, *ppnext;
    433 	vaddr_t curoff;
    434 	UVMHIST_FUNC("uvm_km_pgremove_intrsafe"); UVMHIST_CALLED(maphist);
    435 
    436 	simple_lock(&uobj->vmobjlock);		/* lock object */
    437 
    438 #ifdef DIAGNOSTIC
    439 	if (__predict_false(UVM_OBJ_IS_INTRSAFE_OBJECT(uobj) == 0))
    440 		panic("uvm_km_pgremove_intrsafe: object %p not intrsafe", uobj);
    441 #endif
    442 
    443 	/* choose cheapest traversal */
    444 	by_list = (uobj->uo_npages <=
    445 	     ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
    446 
    447 	if (by_list)
    448 		goto loop_by_list;
    449 
    450 	/* by hash */
    451 
    452 	for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
    453 		pp = uvm_pagelookup(uobj, curoff);
    454 		if (pp == NULL)
    455 			continue;
    456 
    457 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    458 		    pp->flags & PG_BUSY, 0, 0);
    459 #ifdef DIAGNOSTIC
    460 		if (__predict_false(pp->flags & PG_BUSY))
    461 			panic("uvm_km_pgremove_intrsafe: busy page");
    462 		if (__predict_false(pp->pqflags & PQ_ACTIVE))
    463 			panic("uvm_km_pgremove_intrsafe: active page");
    464 		if (__predict_false(pp->pqflags & PQ_INACTIVE))
    465 			panic("uvm_km_pgremove_intrsafe: inactive page");
    466 #endif
    467 
    468 		/* free the page */
    469 		uvm_pagefree(pp);
    470 	}
    471 	simple_unlock(&uobj->vmobjlock);
    472 	return;
    473 
    474 loop_by_list:
    475 
    476 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
    477 		ppnext = pp->listq.tqe_next;
    478 		if (pp->offset < start || pp->offset >= end) {
    479 			continue;
    480 		}
    481 
    482 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    483 		    pp->flags & PG_BUSY, 0, 0);
    484 
    485 #ifdef DIAGNOSTIC
    486 		if (__predict_false(pp->flags & PG_BUSY))
    487 			panic("uvm_km_pgremove_intrsafe: busy page");
    488 		if (__predict_false(pp->pqflags & PQ_ACTIVE))
    489 			panic("uvm_km_pgremove_intrsafe: active page");
    490 		if (__predict_false(pp->pqflags & PQ_INACTIVE))
    491 			panic("uvm_km_pgremove_intrsafe: inactive page");
    492 #endif
    493 
    494 		/* free the page */
    495 		uvm_pagefree(pp);
    496 	}
    497 	simple_unlock(&uobj->vmobjlock);
    498 	return;
    499 }
    500 
    501 
    502 /*
    503  * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
    504  *
    505  * => we map wired memory into the specified map using the obj passed in
    506  * => NOTE: we can return NULL even if we can wait if there is not enough
    507  *	free VM space in the map... caller should be prepared to handle
    508  *	this case.
    509  * => we return KVA of memory allocated
    510  * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
    511  *	lock the map
    512  */
    513 
    514 vaddr_t
    515 uvm_km_kmemalloc(map, obj, size, flags)
    516 	vm_map_t map;
    517 	struct uvm_object *obj;
    518 	vsize_t size;
    519 	int flags;
    520 {
    521 	vaddr_t kva, loopva;
    522 	vaddr_t offset;
    523 	struct vm_page *pg;
    524 	UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
    525 
    526 
    527 	UVMHIST_LOG(maphist,"  (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
    528 	map, obj, size, flags);
    529 #ifdef DIAGNOSTIC
    530 	/* sanity check */
    531 	if (__predict_false(vm_map_pmap(map) != pmap_kernel()))
    532 		panic("uvm_km_kmemalloc: invalid map");
    533 #endif
    534 
    535 	/*
    536 	 * setup for call
    537 	 */
    538 
    539 	size = round_page(size);
    540 	kva = vm_map_min(map);	/* hint */
    541 
    542 	/*
    543 	 * allocate some virtual space
    544 	 */
    545 
    546 	if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
    547 	      UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    548 			  UVM_ADV_RANDOM, (flags & UVM_KMF_TRYLOCK)))
    549 			!= KERN_SUCCESS)) {
    550 		UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
    551 		return(0);
    552 	}
    553 
    554 	/*
    555 	 * if all we wanted was VA, return now
    556 	 */
    557 
    558 	if (flags & UVM_KMF_VALLOC) {
    559 		UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
    560 		return(kva);
    561 	}
    562 	/*
    563 	 * recover object offset from virtual address
    564 	 */
    565 
    566 	offset = kva - vm_map_min(kernel_map);
    567 	UVMHIST_LOG(maphist, "  kva=0x%x, offset=0x%x", kva, offset,0,0);
    568 
    569 	/*
    570 	 * now allocate and map in the memory... note that we are the only ones
    571 	 * whom should ever get a handle on this area of VM.
    572 	 */
    573 
    574 	loopva = kva;
    575 	while (size) {
    576 		simple_lock(&obj->vmobjlock);
    577 		pg = uvm_pagealloc(obj, offset, NULL, 0);
    578 		if (pg) {
    579 			pg->flags &= ~PG_BUSY;	/* new page */
    580 			UVM_PAGE_OWN(pg, NULL);
    581 		}
    582 		simple_unlock(&obj->vmobjlock);
    583 
    584 		/*
    585 		 * out of memory?
    586 		 */
    587 
    588 		if (__predict_false(pg == NULL)) {
    589 			if (flags & UVM_KMF_NOWAIT) {
    590 				/* free everything! */
    591 				uvm_unmap(map, kva, kva + size);
    592 				return(0);
    593 			} else {
    594 				uvm_wait("km_getwait2");	/* sleep here */
    595 				continue;
    596 			}
    597 		}
    598 
    599 		/*
    600 		 * map it in: note that we call pmap_enter with the map and
    601 		 * object unlocked in case we are kmem_map/kmem_object
    602 		 * (because if pmap_enter wants to allocate out of kmem_object
    603 		 * it will need to lock it itself!)
    604 		 */
    605 		if (UVM_OBJ_IS_INTRSAFE_OBJECT(obj)) {
    606 			pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
    607 			    VM_PROT_ALL);
    608 		} else {
    609 			pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    610 			    UVM_PROT_ALL,
    611 			    PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    612 		}
    613 		loopva += PAGE_SIZE;
    614 		offset += PAGE_SIZE;
    615 		size -= PAGE_SIZE;
    616 	}
    617 
    618 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    619 	return(kva);
    620 }
    621 
    622 /*
    623  * uvm_km_free: free an area of kernel memory
    624  */
    625 
    626 void
    627 uvm_km_free(map, addr, size)
    628 	vm_map_t map;
    629 	vaddr_t addr;
    630 	vsize_t size;
    631 {
    632 
    633 	uvm_unmap(map, trunc_page(addr), round_page(addr+size));
    634 }
    635 
    636 /*
    637  * uvm_km_free_wakeup: free an area of kernel memory and wake up
    638  * anyone waiting for vm space.
    639  *
    640  * => XXX: "wanted" bit + unlock&wait on other end?
    641  */
    642 
    643 void
    644 uvm_km_free_wakeup(map, addr, size)
    645 	vm_map_t map;
    646 	vaddr_t addr;
    647 	vsize_t size;
    648 {
    649 	vm_map_entry_t dead_entries;
    650 
    651 	vm_map_lock(map);
    652 	(void)uvm_unmap_remove(map, trunc_page(addr), round_page(addr+size),
    653 			 &dead_entries);
    654 	wakeup(map);
    655 	vm_map_unlock(map);
    656 
    657 	if (dead_entries != NULL)
    658 		uvm_unmap_detach(dead_entries, 0);
    659 }
    660 
    661 /*
    662  * uvm_km_alloc1: allocate wired down memory in the kernel map.
    663  *
    664  * => we can sleep if needed
    665  */
    666 
    667 vaddr_t
    668 uvm_km_alloc1(map, size, zeroit)
    669 	vm_map_t map;
    670 	vsize_t size;
    671 	boolean_t zeroit;
    672 {
    673 	vaddr_t kva, loopva, offset;
    674 	struct vm_page *pg;
    675 	UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
    676 
    677 	UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
    678 
    679 #ifdef DIAGNOSTIC
    680 	if (vm_map_pmap(map) != pmap_kernel())
    681 		panic("uvm_km_alloc1");
    682 #endif
    683 
    684 	size = round_page(size);
    685 	kva = vm_map_min(map);		/* hint */
    686 
    687 	/*
    688 	 * allocate some virtual space
    689 	 */
    690 
    691 	if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
    692 	      UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    693 					      UVM_INH_NONE, UVM_ADV_RANDOM,
    694 					      0)) != KERN_SUCCESS)) {
    695 		UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
    696 		return(0);
    697 	}
    698 
    699 	/*
    700 	 * recover object offset from virtual address
    701 	 */
    702 
    703 	offset = kva - vm_map_min(kernel_map);
    704 	UVMHIST_LOG(maphist,"  kva=0x%x, offset=0x%x", kva, offset,0,0);
    705 
    706 	/*
    707 	 * now allocate the memory.  we must be careful about released pages.
    708 	 */
    709 
    710 	loopva = kva;
    711 	while (size) {
    712 		simple_lock(&uvm.kernel_object->vmobjlock);
    713 		pg = uvm_pagelookup(uvm.kernel_object, offset);
    714 
    715 		/*
    716 		 * if we found a page in an unallocated region, it must be
    717 		 * released
    718 		 */
    719 		if (pg) {
    720 			if ((pg->flags & PG_RELEASED) == 0)
    721 				panic("uvm_km_alloc1: non-released page");
    722 			pg->flags |= PG_WANTED;
    723 			UVM_UNLOCK_AND_WAIT(pg, &uvm.kernel_object->vmobjlock,
    724 			    FALSE, "km_alloc", 0);
    725 			continue;   /* retry */
    726 		}
    727 
    728 		/* allocate ram */
    729 		pg = uvm_pagealloc(uvm.kernel_object, offset, NULL, 0);
    730 		if (pg) {
    731 			pg->flags &= ~PG_BUSY;	/* new page */
    732 			UVM_PAGE_OWN(pg, NULL);
    733 		}
    734 		simple_unlock(&uvm.kernel_object->vmobjlock);
    735 		if (__predict_false(pg == NULL)) {
    736 			uvm_wait("km_alloc1w");	/* wait for memory */
    737 			continue;
    738 		}
    739 
    740 		/*
    741 		 * map it in; note we're never called with an intrsafe
    742 		 * object, so we always use regular old pmap_enter().
    743 		 */
    744 		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    745 		    UVM_PROT_ALL, PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    746 
    747 		loopva += PAGE_SIZE;
    748 		offset += PAGE_SIZE;
    749 		size -= PAGE_SIZE;
    750 	}
    751 
    752 	/*
    753 	 * zero on request (note that "size" is now zero due to the above loop
    754 	 * so we need to subtract kva from loopva to reconstruct the size).
    755 	 */
    756 
    757 	if (zeroit)
    758 		memset((caddr_t)kva, 0, loopva - kva);
    759 
    760 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    761 	return(kva);
    762 }
    763 
    764 /*
    765  * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
    766  *
    767  * => memory is not allocated until fault time
    768  */
    769 
    770 vaddr_t
    771 uvm_km_valloc(map, size)
    772 	vm_map_t map;
    773 	vsize_t size;
    774 {
    775 	vaddr_t kva;
    776 	UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
    777 
    778 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
    779 
    780 #ifdef DIAGNOSTIC
    781 	if (__predict_false(vm_map_pmap(map) != pmap_kernel()))
    782 		panic("uvm_km_valloc");
    783 #endif
    784 
    785 	size = round_page(size);
    786 	kva = vm_map_min(map);		/* hint */
    787 
    788 	/*
    789 	 * allocate some virtual space.  will be demand filled by kernel_object.
    790 	 */
    791 
    792 	if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
    793 	    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    794 					    UVM_INH_NONE, UVM_ADV_RANDOM,
    795 					    0)) != KERN_SUCCESS)) {
    796 		UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
    797 		return(0);
    798 	}
    799 
    800 	UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
    801 	return(kva);
    802 }
    803 
    804 /*
    805  * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
    806  *
    807  * => memory is not allocated until fault time
    808  * => if no room in map, wait for space to free, unless requested size
    809  *    is larger than map (in which case we return 0)
    810  */
    811 
    812 vaddr_t
    813 uvm_km_valloc_wait(map, size)
    814 	vm_map_t map;
    815 	vsize_t size;
    816 {
    817 	vaddr_t kva;
    818 	UVMHIST_FUNC("uvm_km_valloc_wait"); UVMHIST_CALLED(maphist);
    819 
    820 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
    821 
    822 #ifdef DIAGNOSTIC
    823 	if (__predict_false(vm_map_pmap(map) != pmap_kernel()))
    824 		panic("uvm_km_valloc_wait");
    825 #endif
    826 
    827 	size = round_page(size);
    828 	if (size > vm_map_max(map) - vm_map_min(map))
    829 		return(0);
    830 
    831 	while (1) {
    832 		kva = vm_map_min(map);		/* hint */
    833 
    834 		/*
    835 		 * allocate some virtual space.   will be demand filled
    836 		 * by kernel_object.
    837 		 */
    838 
    839 		if (__predict_true(uvm_map(map, &kva, size, uvm.kernel_object,
    840 		    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL,
    841 		    UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, 0))
    842 		    == KERN_SUCCESS)) {
    843 			UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    844 			return(kva);
    845 		}
    846 
    847 		/*
    848 		 * failed.  sleep for a while (on map)
    849 		 */
    850 
    851 		UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
    852 		tsleep((caddr_t)map, PVM, "vallocwait", 0);
    853 	}
    854 	/*NOTREACHED*/
    855 }
    856 
    857 /* Sanity; must specify both or none. */
    858 #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
    859     (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
    860 #error Must specify MAP and UNMAP together.
    861 #endif
    862 
    863 /*
    864  * uvm_km_alloc_poolpage: allocate a page for the pool allocator
    865  *
    866  * => if the pmap specifies an alternate mapping method, we use it.
    867  */
    868 
    869 /* ARGSUSED */
    870 vaddr_t
    871 uvm_km_alloc_poolpage1(map, obj, waitok)
    872 	vm_map_t map;
    873 	struct uvm_object *obj;
    874 	boolean_t waitok;
    875 {
    876 #if defined(PMAP_MAP_POOLPAGE)
    877 	struct vm_page *pg;
    878 	vaddr_t va;
    879 
    880  again:
    881 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    882 	if (__predict_false(pg == NULL)) {
    883 		if (waitok) {
    884 			uvm_wait("plpg");
    885 			goto again;
    886 		} else
    887 			return (0);
    888 	}
    889 	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
    890 	if (__predict_false(va == 0))
    891 		uvm_pagefree(pg);
    892 	return (va);
    893 #else
    894 	vaddr_t va;
    895 	int s;
    896 
    897 	/*
    898 	 * NOTE: We may be called with a map that doens't require splimp
    899 	 * protection (e.g. kernel_map).  However, it does not hurt to
    900 	 * go to splimp in this case (since unprocted maps will never be
    901 	 * accessed in interrupt context).
    902 	 *
    903 	 * XXX We may want to consider changing the interface to this
    904 	 * XXX function.
    905 	 */
    906 
    907 	s = splimp();
    908 	va = uvm_km_kmemalloc(map, obj, PAGE_SIZE, waitok ? 0 : UVM_KMF_NOWAIT);
    909 	splx(s);
    910 	return (va);
    911 #endif /* PMAP_MAP_POOLPAGE */
    912 }
    913 
    914 /*
    915  * uvm_km_free_poolpage: free a previously allocated pool page
    916  *
    917  * => if the pmap specifies an alternate unmapping method, we use it.
    918  */
    919 
    920 /* ARGSUSED */
    921 void
    922 uvm_km_free_poolpage1(map, addr)
    923 	vm_map_t map;
    924 	vaddr_t addr;
    925 {
    926 #if defined(PMAP_UNMAP_POOLPAGE)
    927 	paddr_t pa;
    928 
    929 	pa = PMAP_UNMAP_POOLPAGE(addr);
    930 	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
    931 #else
    932 	int s;
    933 
    934 	/*
    935 	 * NOTE: We may be called with a map that doens't require splimp
    936 	 * protection (e.g. kernel_map).  However, it does not hurt to
    937 	 * go to splimp in this case (since unprocted maps will never be
    938 	 * accessed in interrupt context).
    939 	 *
    940 	 * XXX We may want to consider changing the interface to this
    941 	 * XXX function.
    942 	 */
    943 
    944 	s = splimp();
    945 	uvm_km_free(map, addr, PAGE_SIZE);
    946 	splx(s);
    947 #endif /* PMAP_UNMAP_POOLPAGE */
    948 }
    949