Home | History | Annotate | Line # | Download | only in uvm
uvm_km.c revision 1.70
      1 /*	$NetBSD: uvm_km.c,v 1.70 2005/01/01 21:00:06 yamt 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 /*
     70  * uvm_km.c: handle kernel memory allocation and management
     71  */
     72 
     73 /*
     74  * overview of kernel memory management:
     75  *
     76  * the kernel virtual address space is mapped by "kernel_map."   kernel_map
     77  * starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
     78  * note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
     79  *
     80  * the kernel_map has several "submaps."   submaps can only appear in
     81  * the kernel_map (user processes can't use them).   submaps "take over"
     82  * the management of a sub-range of the kernel's address space.  submaps
     83  * are typically allocated at boot time and are never released.   kernel
     84  * virtual address space that is mapped by a submap is locked by the
     85  * submap's lock -- not the kernel_map's lock.
     86  *
     87  * thus, the useful feature of submaps is that they allow us to break
     88  * up the locking and protection of the kernel address space into smaller
     89  * chunks.
     90  *
     91  * the vm system has several standard kernel submaps, including:
     92  *   kmem_map => contains only wired kernel memory for the kernel
     93  *		malloc.   *** access to kmem_map must be protected
     94  *		by splvm() because we are allowed to call malloc()
     95  *		at interrupt time ***
     96  *   mb_map => memory for large mbufs,  *** protected by splvm ***
     97  *   pager_map => used to map "buf" structures into kernel space
     98  *   exec_map => used during exec to handle exec args
     99  *   etc...
    100  *
    101  * the kernel allocates its private memory out of special uvm_objects whose
    102  * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
    103  * are "special" and never die).   all kernel objects should be thought of
    104  * as large, fixed-sized, sparsely populated uvm_objects.   each kernel
    105  * object is equal to the size of kernel virtual address space (i.e. the
    106  * value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
    107  *
    108  * most kernel private memory lives in kernel_object.   the only exception
    109  * to this is for memory that belongs to submaps that must be protected
    110  * by splvm().  pages in these submaps are not assigned to an object.
    111  *
    112  * note that just because a kernel object spans the entire kernel virutal
    113  * address space doesn't mean that it has to be mapped into the entire space.
    114  * large chunks of a kernel object's space go unused either because
    115  * that area of kernel VM is unmapped, or there is some other type of
    116  * object mapped into that range (e.g. a vnode).    for submap's kernel
    117  * objects, the only part of the object that can ever be populated is the
    118  * offsets that are managed by the submap.
    119  *
    120  * note that the "offset" in a kernel object is always the kernel virtual
    121  * address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
    122  * example:
    123  *   suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
    124  *   uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
    125  *   kernel map].    if uvm_km_alloc returns virtual address 0xf8235000,
    126  *   then that means that the page at offset 0x235000 in kernel_object is
    127  *   mapped at 0xf8235000.
    128  *
    129  * kernel object have one other special property: when the kernel virtual
    130  * memory mapping them is unmapped, the backing memory in the object is
    131  * freed right away.   this is done with the uvm_km_pgremove() function.
    132  * this has to be done because there is no backing store for kernel pages
    133  * and no need to save them after they are no longer referenced.
    134  */
    135 
    136 #include <sys/cdefs.h>
    137 __KERNEL_RCSID(0, "$NetBSD: uvm_km.c,v 1.70 2005/01/01 21:00:06 yamt Exp $");
    138 
    139 #include "opt_uvmhist.h"
    140 
    141 #include <sys/param.h>
    142 #include <sys/systm.h>
    143 #include <sys/proc.h>
    144 
    145 #include <uvm/uvm.h>
    146 
    147 /*
    148  * global data structures
    149  */
    150 
    151 struct vm_map *kernel_map = NULL;
    152 
    153 /*
    154  * local data structues
    155  */
    156 
    157 static struct vm_map		kernel_map_store;
    158 static struct vm_map_entry	kernel_first_mapent_store;
    159 
    160 /*
    161  * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
    162  * KVM already allocated for text, data, bss, and static data structures).
    163  *
    164  * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
    165  *    we assume that [min -> start] has already been allocated and that
    166  *    "end" is the end.
    167  */
    168 
    169 void
    170 uvm_km_init(start, end)
    171 	vaddr_t start, end;
    172 {
    173 	vaddr_t base = VM_MIN_KERNEL_ADDRESS;
    174 
    175 	/*
    176 	 * next, init kernel memory objects.
    177 	 */
    178 
    179 	/* kernel_object: for pageable anonymous kernel memory */
    180 	uao_init();
    181 	uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
    182 				 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
    183 
    184 	/*
    185 	 * init the map and reserve any space that might already
    186 	 * have been allocated kernel space before installing.
    187 	 */
    188 
    189 	uvm_map_setup(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
    190 	kernel_map_store.pmap = pmap_kernel();
    191 	if (start != base) {
    192 		int error;
    193 		struct uvm_map_args args;
    194 
    195 		error = uvm_map_prepare(&kernel_map_store, base, start - base,
    196 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    197 		    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    198 		    		UVM_ADV_RANDOM, UVM_FLAG_FIXED), &args);
    199 		if (!error) {
    200 			kernel_first_mapent_store.flags =
    201 			    UVM_MAP_KERNEL | UVM_MAP_FIRST;
    202 			error = uvm_map_enter(&kernel_map_store, &args,
    203 			    &kernel_first_mapent_store);
    204 		}
    205 
    206 		if (error)
    207 			panic(
    208 			    "uvm_km_init: could not reserve space for kernel");
    209 	}
    210 
    211 	/*
    212 	 * install!
    213 	 */
    214 
    215 	kernel_map = &kernel_map_store;
    216 }
    217 
    218 /*
    219  * uvm_km_suballoc: allocate a submap in the kernel map.   once a submap
    220  * is allocated all references to that area of VM must go through it.  this
    221  * allows the locking of VAs in kernel_map to be broken up into regions.
    222  *
    223  * => if `fixed' is true, *min specifies where the region described
    224  *      by the submap must start
    225  * => if submap is non NULL we use that as the submap, otherwise we
    226  *	alloc a new map
    227  */
    228 struct vm_map *
    229 uvm_km_suballoc(map, min, max, size, flags, fixed, submap)
    230 	struct vm_map *map;
    231 	vaddr_t *min, *max;		/* IN/OUT, OUT */
    232 	vsize_t size;
    233 	int flags;
    234 	boolean_t fixed;
    235 	struct vm_map *submap;
    236 {
    237 	int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
    238 
    239 	size = round_page(size);	/* round up to pagesize */
    240 
    241 	/*
    242 	 * first allocate a blank spot in the parent map
    243 	 */
    244 
    245 	if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET, 0,
    246 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    247 	    UVM_ADV_RANDOM, mapflags)) != 0) {
    248 	       panic("uvm_km_suballoc: unable to allocate space in parent map");
    249 	}
    250 
    251 	/*
    252 	 * set VM bounds (min is filled in by uvm_map)
    253 	 */
    254 
    255 	*max = *min + size;
    256 
    257 	/*
    258 	 * add references to pmap and create or init the submap
    259 	 */
    260 
    261 	pmap_reference(vm_map_pmap(map));
    262 	if (submap == NULL) {
    263 		submap = uvm_map_create(vm_map_pmap(map), *min, *max, flags);
    264 		if (submap == NULL)
    265 			panic("uvm_km_suballoc: unable to create submap");
    266 	} else {
    267 		uvm_map_setup(submap, *min, *max, flags);
    268 		submap->pmap = vm_map_pmap(map);
    269 	}
    270 
    271 	/*
    272 	 * now let uvm_map_submap plug in it...
    273 	 */
    274 
    275 	if (uvm_map_submap(map, *min, *max, submap) != 0)
    276 		panic("uvm_km_suballoc: submap allocation failed");
    277 
    278 	return(submap);
    279 }
    280 
    281 /*
    282  * uvm_km_pgremove: remove pages from a kernel uvm_object.
    283  *
    284  * => when you unmap a part of anonymous kernel memory you want to toss
    285  *    the pages right away.    (this gets called from uvm_unmap_...).
    286  */
    287 
    288 void
    289 uvm_km_pgremove(uobj, start, end)
    290 	struct uvm_object *uobj;
    291 	vaddr_t start, end;
    292 {
    293 	struct vm_page *pg;
    294 	voff_t curoff, nextoff;
    295 	int swpgonlydelta = 0;
    296 	UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
    297 
    298 	KASSERT(uobj->pgops == &aobj_pager);
    299 	simple_lock(&uobj->vmobjlock);
    300 
    301 	for (curoff = start; curoff < end; curoff = nextoff) {
    302 		nextoff = curoff + PAGE_SIZE;
    303 		pg = uvm_pagelookup(uobj, curoff);
    304 		if (pg != NULL && pg->flags & PG_BUSY) {
    305 			pg->flags |= PG_WANTED;
    306 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    307 				    "km_pgrm", 0);
    308 			simple_lock(&uobj->vmobjlock);
    309 			nextoff = curoff;
    310 			continue;
    311 		}
    312 
    313 		/*
    314 		 * free the swap slot, then the page.
    315 		 */
    316 
    317 		if (pg == NULL &&
    318 		    uao_find_swslot(uobj, curoff >> PAGE_SHIFT) > 0) {
    319 			swpgonlydelta++;
    320 		}
    321 		uao_dropswap(uobj, curoff >> PAGE_SHIFT);
    322 		if (pg != NULL) {
    323 			uvm_lock_pageq();
    324 			uvm_pagefree(pg);
    325 			uvm_unlock_pageq();
    326 		}
    327 	}
    328 	simple_unlock(&uobj->vmobjlock);
    329 
    330 	if (swpgonlydelta > 0) {
    331 		simple_lock(&uvm.swap_data_lock);
    332 		KASSERT(uvmexp.swpgonly >= swpgonlydelta);
    333 		uvmexp.swpgonly -= swpgonlydelta;
    334 		simple_unlock(&uvm.swap_data_lock);
    335 	}
    336 }
    337 
    338 
    339 /*
    340  * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for "intrsafe"
    341  *    maps
    342  *
    343  * => when you unmap a part of anonymous kernel memory you want to toss
    344  *    the pages right away.    (this is called from uvm_unmap_...).
    345  * => none of the pages will ever be busy, and none of them will ever
    346  *    be on the active or inactive queues (because they have no object).
    347  */
    348 
    349 void
    350 uvm_km_pgremove_intrsafe(start, end)
    351 	vaddr_t start, end;
    352 {
    353 	struct vm_page *pg;
    354 	paddr_t pa;
    355 	UVMHIST_FUNC("uvm_km_pgremove_intrsafe"); UVMHIST_CALLED(maphist);
    356 
    357 	for (; start < end; start += PAGE_SIZE) {
    358 		if (!pmap_extract(pmap_kernel(), start, &pa)) {
    359 			continue;
    360 		}
    361 		pg = PHYS_TO_VM_PAGE(pa);
    362 		KASSERT(pg);
    363 		KASSERT(pg->uobject == NULL && pg->uanon == NULL);
    364 		uvm_pagefree(pg);
    365 	}
    366 }
    367 
    368 
    369 /*
    370  * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
    371  *
    372  * => we map wired memory into the specified map using the obj passed in
    373  * => NOTE: we can return NULL even if we can wait if there is not enough
    374  *	free VM space in the map... caller should be prepared to handle
    375  *	this case.
    376  * => we return KVA of memory allocated
    377  * => align,prefer - passed on to uvm_map()
    378  * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
    379  *	lock the map
    380  */
    381 
    382 vaddr_t
    383 uvm_km_kmemalloc1(map, obj, size, align, prefer, flags)
    384 	struct vm_map *map;
    385 	struct uvm_object *obj;
    386 	vsize_t size;
    387 	vsize_t align;
    388 	voff_t prefer;
    389 	int flags;
    390 {
    391 	vaddr_t kva, loopva;
    392 	vaddr_t offset;
    393 	vsize_t loopsize;
    394 	struct vm_page *pg;
    395 	UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
    396 
    397 	UVMHIST_LOG(maphist,"  (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
    398 		    map, obj, size, flags);
    399 	KASSERT(vm_map_pmap(map) == pmap_kernel());
    400 
    401 	/*
    402 	 * setup for call
    403 	 */
    404 
    405 	size = round_page(size);
    406 	kva = vm_map_min(map);	/* hint */
    407 
    408 	/*
    409 	 * allocate some virtual space
    410 	 */
    411 
    412 	if (__predict_false(uvm_map(map, &kva, size, obj, prefer, align,
    413 		UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    414 			    UVM_ADV_RANDOM,
    415 			    (flags & (UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT))
    416 			    | UVM_FLAG_QUANTUM))
    417 			!= 0)) {
    418 		UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
    419 		return(0);
    420 	}
    421 
    422 	/*
    423 	 * if all we wanted was VA, return now
    424 	 */
    425 
    426 	if (flags & UVM_KMF_VALLOC) {
    427 		UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
    428 		return(kva);
    429 	}
    430 
    431 	/*
    432 	 * recover object offset from virtual address
    433 	 */
    434 
    435 	offset = kva - vm_map_min(kernel_map);
    436 	UVMHIST_LOG(maphist, "  kva=0x%x, offset=0x%x", kva, offset,0,0);
    437 
    438 	/*
    439 	 * now allocate and map in the memory... note that we are the only ones
    440 	 * whom should ever get a handle on this area of VM.
    441 	 */
    442 
    443 	loopva = kva;
    444 	loopsize = size;
    445 	while (loopsize) {
    446 		if (obj) {
    447 			simple_lock(&obj->vmobjlock);
    448 		}
    449 		pg = uvm_pagealloc(obj, offset, NULL, UVM_PGA_USERESERVE);
    450 		if (__predict_true(pg != NULL)) {
    451 			pg->flags &= ~PG_BUSY;	/* new page */
    452 			UVM_PAGE_OWN(pg, NULL);
    453 		}
    454 		if (obj) {
    455 			simple_unlock(&obj->vmobjlock);
    456 		}
    457 
    458 		/*
    459 		 * out of memory?
    460 		 */
    461 
    462 		if (__predict_false(pg == NULL)) {
    463 			if ((flags & UVM_KMF_NOWAIT) ||
    464 			    ((flags & UVM_KMF_CANFAIL) && uvm_swapisfull())) {
    465 				/* free everything! */
    466 				uvm_unmap(map, kva, kva + size);
    467 				return (0);
    468 			} else {
    469 				uvm_wait("km_getwait2");	/* sleep here */
    470 				continue;
    471 			}
    472 		}
    473 
    474 		/*
    475 		 * map it in
    476 		 */
    477 
    478 		if (obj == NULL) {
    479 			pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
    480 			    VM_PROT_READ | VM_PROT_WRITE);
    481 		} else {
    482 			pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    483 			    UVM_PROT_ALL,
    484 			    PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    485 		}
    486 		loopva += PAGE_SIZE;
    487 		offset += PAGE_SIZE;
    488 		loopsize -= PAGE_SIZE;
    489 	}
    490 
    491        	pmap_update(pmap_kernel());
    492 
    493 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    494 	return(kva);
    495 }
    496 
    497 /*
    498  * uvm_km_free: free an area of kernel memory
    499  */
    500 
    501 void
    502 uvm_km_free(map, addr, size)
    503 	struct vm_map *map;
    504 	vaddr_t addr;
    505 	vsize_t size;
    506 {
    507 	uvm_unmap(map, trunc_page(addr), round_page(addr+size));
    508 }
    509 
    510 /*
    511  * uvm_km_free_wakeup: free an area of kernel memory and wake up
    512  * anyone waiting for vm space.
    513  *
    514  * => XXX: "wanted" bit + unlock&wait on other end?
    515  */
    516 
    517 void
    518 uvm_km_free_wakeup(map, addr, size)
    519 	struct vm_map *map;
    520 	vaddr_t addr;
    521 	vsize_t size;
    522 {
    523 	struct vm_map_entry *dead_entries;
    524 
    525 	vm_map_lock(map);
    526 	uvm_unmap_remove(map, trunc_page(addr), round_page(addr + size),
    527 	    &dead_entries, NULL);
    528 	wakeup(map);
    529 	vm_map_unlock(map);
    530 	if (dead_entries != NULL)
    531 		uvm_unmap_detach(dead_entries, 0);
    532 }
    533 
    534 /*
    535  * uvm_km_alloc1: allocate wired down memory in the kernel map.
    536  *
    537  * => we can sleep if needed
    538  */
    539 
    540 vaddr_t
    541 uvm_km_alloc1(map, size, zeroit)
    542 	struct vm_map *map;
    543 	vsize_t size;
    544 	boolean_t zeroit;
    545 {
    546 	vaddr_t kva, loopva, offset;
    547 	struct vm_page *pg;
    548 	UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
    549 
    550 	UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
    551 	KASSERT(vm_map_pmap(map) == pmap_kernel());
    552 
    553 	size = round_page(size);
    554 	kva = vm_map_min(map);		/* hint */
    555 
    556 	/*
    557 	 * allocate some virtual space
    558 	 */
    559 
    560 	if (__predict_false(uvm_map(map, &kva, size, uvm.kernel_object,
    561 	      UVM_UNKNOWN_OFFSET, 0, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    562 					      UVM_INH_NONE, UVM_ADV_RANDOM,
    563 					      UVM_FLAG_QUANTUM)) != 0)) {
    564 		UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
    565 		return(0);
    566 	}
    567 
    568 	/*
    569 	 * recover object offset from virtual address
    570 	 */
    571 
    572 	offset = kva - vm_map_min(kernel_map);
    573 	UVMHIST_LOG(maphist,"  kva=0x%x, offset=0x%x", kva, offset,0,0);
    574 
    575 	/*
    576 	 * now allocate the memory.
    577 	 */
    578 
    579 	loopva = kva;
    580 	while (size) {
    581 		simple_lock(&uvm.kernel_object->vmobjlock);
    582 		KASSERT(uvm_pagelookup(uvm.kernel_object, offset) == NULL);
    583 		pg = uvm_pagealloc(uvm.kernel_object, offset, NULL, 0);
    584 		if (pg) {
    585 			pg->flags &= ~PG_BUSY;
    586 			UVM_PAGE_OWN(pg, NULL);
    587 		}
    588 		simple_unlock(&uvm.kernel_object->vmobjlock);
    589 		if (pg == NULL) {
    590 			uvm_wait("km_alloc1w");
    591 			continue;
    592 		}
    593 		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    594 		    UVM_PROT_ALL, PMAP_WIRED | VM_PROT_READ | VM_PROT_WRITE);
    595 		loopva += PAGE_SIZE;
    596 		offset += PAGE_SIZE;
    597 		size -= PAGE_SIZE;
    598 	}
    599 	pmap_update(map->pmap);
    600 
    601 	/*
    602 	 * zero on request (note that "size" is now zero due to the above loop
    603 	 * so we need to subtract kva from loopva to reconstruct the size).
    604 	 */
    605 
    606 	if (zeroit)
    607 		memset((caddr_t)kva, 0, loopva - kva);
    608 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    609 	return(kva);
    610 }
    611 
    612 /*
    613  * uvm_km_valloc1: allocate zero-fill memory in the kernel's address space
    614  *
    615  * => memory is not allocated until fault time
    616  * => the align, prefer and flags parameters are passed on to uvm_map().
    617  *
    618  * Note: this function is also the backend for these macros:
    619  *	uvm_km_valloc
    620  *	uvm_km_valloc_wait
    621  *	uvm_km_valloc_prefer
    622  *	uvm_km_valloc_prefer_wait
    623  *	uvm_km_valloc_align
    624  */
    625 
    626 vaddr_t
    627 uvm_km_valloc1(map, size, align, prefer, flags)
    628 	struct vm_map *map;
    629 	vsize_t size;
    630 	vsize_t align;
    631 	voff_t prefer;
    632 	uvm_flag_t flags;
    633 {
    634 	vaddr_t kva;
    635 	UVMHIST_FUNC("uvm_km_valloc1"); UVMHIST_CALLED(maphist);
    636 
    637 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, align=0x%x, prefer=0x%x)",
    638 		    map, size, align, prefer);
    639 
    640 	KASSERT(vm_map_pmap(map) == pmap_kernel());
    641 
    642 	size = round_page(size);
    643 	/*
    644 	 * Check if requested size is larger than the map, in which
    645 	 * case we can't succeed.
    646 	 */
    647 	if (size > vm_map_max(map) - vm_map_min(map))
    648 		return (0);
    649 
    650 	flags |= UVM_FLAG_QUANTUM;
    651 	for (;;) {
    652 		kva = vm_map_min(map);		/* hint */
    653 
    654 		/*
    655 		 * allocate some virtual space.   will be demand filled
    656 		 * by kernel_object.
    657 		 */
    658 
    659 		if (__predict_true(uvm_map(map, &kva, size, uvm.kernel_object,
    660 		    prefer, align, UVM_MAPFLAG(UVM_PROT_ALL,
    661 		    UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, flags))
    662 		    == 0)) {
    663 			UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    664 			return (kva);
    665 		}
    666 
    667 		/*
    668 		 * failed.  sleep for a while (on map)
    669 		 */
    670 		if ((flags & UVM_KMF_NOWAIT) != 0)
    671 			return (0);
    672 
    673 		UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
    674 		tsleep((caddr_t)map, PVM, "vallocwait", 0);
    675 	}
    676 	/*NOTREACHED*/
    677 }
    678 
    679 /* Function definitions for binary compatibility */
    680 vaddr_t
    681 uvm_km_kmemalloc(struct vm_map *map, struct uvm_object *obj,
    682 		 vsize_t sz, int flags)
    683 {
    684 	return uvm_km_kmemalloc1(map, obj, sz, 0, UVM_UNKNOWN_OFFSET, flags);
    685 }
    686 
    687 vaddr_t uvm_km_valloc(struct vm_map *map, vsize_t sz)
    688 {
    689 	return uvm_km_valloc1(map, sz, 0, UVM_UNKNOWN_OFFSET, UVM_KMF_NOWAIT);
    690 }
    691 
    692 vaddr_t uvm_km_valloc_align(struct vm_map *map, vsize_t sz, vsize_t align)
    693 {
    694 	return uvm_km_valloc1(map, sz, align, UVM_UNKNOWN_OFFSET, UVM_KMF_NOWAIT);
    695 }
    696 
    697 vaddr_t uvm_km_valloc_prefer_wait(struct vm_map *map, vsize_t sz, voff_t prefer)
    698 {
    699 	return uvm_km_valloc1(map, sz, 0, prefer, 0);
    700 }
    701 
    702 vaddr_t uvm_km_valloc_wait(struct vm_map *map, vsize_t sz)
    703 {
    704 	return uvm_km_valloc1(map, sz, 0, UVM_UNKNOWN_OFFSET, 0);
    705 }
    706 
    707 /* Sanity; must specify both or none. */
    708 #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
    709     (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
    710 #error Must specify MAP and UNMAP together.
    711 #endif
    712 
    713 /*
    714  * uvm_km_alloc_poolpage: allocate a page for the pool allocator
    715  *
    716  * => if the pmap specifies an alternate mapping method, we use it.
    717  */
    718 
    719 /* ARGSUSED */
    720 vaddr_t
    721 uvm_km_alloc_poolpage1(map, obj, waitok)
    722 	struct vm_map *map;
    723 	struct uvm_object *obj;
    724 	boolean_t waitok;
    725 {
    726 #if defined(PMAP_MAP_POOLPAGE)
    727 	struct vm_page *pg;
    728 	vaddr_t va;
    729 
    730  again:
    731 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    732 	if (__predict_false(pg == NULL)) {
    733 		if (waitok) {
    734 			uvm_wait("plpg");
    735 			goto again;
    736 		} else
    737 			return (0);
    738 	}
    739 	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
    740 	if (__predict_false(va == 0))
    741 		uvm_pagefree(pg);
    742 	return (va);
    743 #else
    744 	vaddr_t va;
    745 	int s;
    746 
    747 	/*
    748 	 * NOTE: We may be called with a map that doens't require splvm
    749 	 * protection (e.g. kernel_map).  However, it does not hurt to
    750 	 * go to splvm in this case (since unprocted maps will never be
    751 	 * accessed in interrupt context).
    752 	 *
    753 	 * XXX We may want to consider changing the interface to this
    754 	 * XXX function.
    755 	 */
    756 
    757 	s = splvm();
    758 	va = uvm_km_kmemalloc(map, obj, PAGE_SIZE,
    759 	    waitok ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK);
    760 	splx(s);
    761 	return (va);
    762 #endif /* PMAP_MAP_POOLPAGE */
    763 }
    764 
    765 /*
    766  * uvm_km_free_poolpage: free a previously allocated pool page
    767  *
    768  * => if the pmap specifies an alternate unmapping method, we use it.
    769  */
    770 
    771 /* ARGSUSED */
    772 void
    773 uvm_km_free_poolpage1(map, addr)
    774 	struct vm_map *map;
    775 	vaddr_t addr;
    776 {
    777 #if defined(PMAP_UNMAP_POOLPAGE)
    778 	paddr_t pa;
    779 
    780 	pa = PMAP_UNMAP_POOLPAGE(addr);
    781 	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
    782 #else
    783 	int s;
    784 
    785 	/*
    786 	 * NOTE: We may be called with a map that doens't require splvm
    787 	 * protection (e.g. kernel_map).  However, it does not hurt to
    788 	 * go to splvm in this case (since unprocted maps will never be
    789 	 * accessed in interrupt context).
    790 	 *
    791 	 * XXX We may want to consider changing the interface to this
    792 	 * XXX function.
    793 	 */
    794 
    795 	s = splvm();
    796 	uvm_km_free(map, addr, PAGE_SIZE);
    797 	splx(s);
    798 #endif /* PMAP_UNMAP_POOLPAGE */
    799 }
    800