Home | History | Annotate | Line # | Download | only in uvm
uvm_km.c revision 1.76.4.4
      1 /*	$NetBSD: uvm_km.c,v 1.76.4.4 2005/02/16 23:13:58 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.76.4.4 2005/02/16 23:13:58 yamt Exp $");
    138 
    139 #include "opt_uvmhist.h"
    140 
    141 #include <sys/param.h>
    142 #include <sys/malloc.h>
    143 #include <sys/systm.h>
    144 #include <sys/proc.h>
    145 #include <sys/pool.h>
    146 
    147 #include <uvm/uvm.h>
    148 
    149 /*
    150  * global data structures
    151  */
    152 
    153 struct vm_map *kernel_map = NULL;
    154 
    155 /*
    156  * local data structues
    157  */
    158 
    159 static struct vm_map_kernel	kernel_map_store;
    160 static struct vm_map_entry	kernel_first_mapent_store;
    161 
    162 #if !defined(PMAP_MAP_POOLPAGE)
    163 
    164 /*
    165  * kva cache
    166  *
    167  * XXX maybe it's better to do this at the uvm_map layer.
    168  */
    169 
    170 #define	KM_VACACHE_SIZE	(32 * PAGE_SIZE) /* XXX tune */
    171 
    172 static void *km_vacache_alloc(struct pool *, int);
    173 static void km_vacache_free(struct pool *, void *);
    174 static void km_vacache_init(struct vm_map *, const char *, size_t);
    175 
    176 /* XXX */
    177 #define	KM_VACACHE_POOL_TO_MAP(pp) \
    178 	((struct vm_map *)((char *)(pp) - \
    179 	    offsetof(struct vm_map_kernel, vmk_vacache)))
    180 
    181 static void *
    182 km_vacache_alloc(struct pool *pp, int flags)
    183 {
    184 	vaddr_t va;
    185 	size_t size;
    186 	struct vm_map *map;
    187 	size = pp->pr_alloc->pa_pagesz;
    188 
    189 	map = KM_VACACHE_POOL_TO_MAP(pp);
    190 
    191 	va = vm_map_min(map); /* hint */
    192 	if (uvm_map(map, &va, size, NULL, UVM_UNKNOWN_OFFSET, size,
    193 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    194 	    UVM_ADV_RANDOM, UVM_FLAG_QUANTUM |
    195 	    ((flags & PR_WAITOK) ? 0 : UVM_FLAG_TRYLOCK | UVM_FLAG_NOWAIT))))
    196 		return NULL;
    197 
    198 	return (void *)va;
    199 }
    200 
    201 static void
    202 km_vacache_free(struct pool *pp, void *v)
    203 {
    204 	vaddr_t va = (vaddr_t)v;
    205 	size_t size = pp->pr_alloc->pa_pagesz;
    206 	struct vm_map *map;
    207 
    208 	map = KM_VACACHE_POOL_TO_MAP(pp);
    209 	uvm_unmap1(map, va, va + size, UVM_FLAG_QUANTUM|UVM_FLAG_VAONLY);
    210 }
    211 
    212 /*
    213  * km_vacache_init: initialize kva cache.
    214  */
    215 
    216 static void
    217 km_vacache_init(struct vm_map *map, const char *name, size_t size)
    218 {
    219 	struct vm_map_kernel *vmk;
    220 	struct pool *pp;
    221 	struct pool_allocator *pa;
    222 
    223 	KASSERT(VM_MAP_IS_KERNEL(map));
    224 	KASSERT(size < (vm_map_max(map) - vm_map_min(map)) / 2); /* sanity */
    225 
    226 	vmk = vm_map_to_kernel(map);
    227 	pp = &vmk->vmk_vacache;
    228 	pa = &vmk->vmk_vacache_allocator;
    229 	memset(pa, 0, sizeof(*pa));
    230 	pa->pa_alloc = km_vacache_alloc;
    231 	pa->pa_free = km_vacache_free;
    232 	pa->pa_pagesz = (unsigned int)size;
    233 	pool_init(pp, PAGE_SIZE, 0, 0, PR_NOTOUCH | PR_RECURSIVE, name, pa);
    234 
    235 	/* XXX for now.. */
    236 	pool_sethiwat(pp, 0);
    237 }
    238 
    239 void
    240 uvm_km_vacache_init(struct vm_map *map, const char *name, size_t size)
    241 {
    242 
    243 	map->flags |= VM_MAP_VACACHE;
    244 	if (size == 0)
    245 		size = KM_VACACHE_SIZE;
    246 	km_vacache_init(map, name, size);
    247 }
    248 
    249 #else /* !defined(PMAP_MAP_POOLPAGE) */
    250 
    251 void
    252 uvm_km_vacache_init(struct vm_map *map, const char *name, size_t size)
    253 {
    254 
    255 	/* nothing */
    256 }
    257 
    258 #endif /* !defined(PMAP_MAP_POOLPAGE) */
    259 
    260 /*
    261  * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
    262  * KVM already allocated for text, data, bss, and static data structures).
    263  *
    264  * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
    265  *    we assume that [min -> start] has already been allocated and that
    266  *    "end" is the end.
    267  */
    268 
    269 void
    270 uvm_km_init(start, end)
    271 	vaddr_t start, end;
    272 {
    273 	vaddr_t base = VM_MIN_KERNEL_ADDRESS;
    274 
    275 	/*
    276 	 * next, init kernel memory objects.
    277 	 */
    278 
    279 	/* kernel_object: for pageable anonymous kernel memory */
    280 	uao_init();
    281 	uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
    282 				 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
    283 
    284 	/*
    285 	 * init the map and reserve any space that might already
    286 	 * have been allocated kernel space before installing.
    287 	 */
    288 
    289 	uvm_map_setup_kernel(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
    290 	kernel_map_store.vmk_map.pmap = pmap_kernel();
    291 	if (start != base) {
    292 		int error;
    293 		struct uvm_map_args args;
    294 
    295 		error = uvm_map_prepare(&kernel_map_store.vmk_map,
    296 		    base, start - base,
    297 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    298 		    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    299 		    		UVM_ADV_RANDOM, UVM_FLAG_FIXED), &args);
    300 		if (!error) {
    301 			kernel_first_mapent_store.flags =
    302 			    UVM_MAP_KERNEL | UVM_MAP_FIRST;
    303 			error = uvm_map_enter(&kernel_map_store.vmk_map, &args,
    304 			    &kernel_first_mapent_store);
    305 		}
    306 
    307 		if (error)
    308 			panic(
    309 			    "uvm_km_init: could not reserve space for kernel");
    310 	}
    311 
    312 	/*
    313 	 * install!
    314 	 */
    315 
    316 	kernel_map = &kernel_map_store.vmk_map;
    317 	uvm_km_vacache_init(kernel_map, "kvakernel", 0);
    318 }
    319 
    320 /*
    321  * uvm_km_suballoc: allocate a submap in the kernel map.   once a submap
    322  * is allocated all references to that area of VM must go through it.  this
    323  * allows the locking of VAs in kernel_map to be broken up into regions.
    324  *
    325  * => if `fixed' is true, *min specifies where the region described
    326  *      by the submap must start
    327  * => if submap is non NULL we use that as the submap, otherwise we
    328  *	alloc a new map
    329  */
    330 struct vm_map *
    331 uvm_km_suballoc(map, min, max, size, flags, fixed, submap)
    332 	struct vm_map *map;
    333 	vaddr_t *min, *max;		/* IN/OUT, OUT */
    334 	vsize_t size;
    335 	int flags;
    336 	boolean_t fixed;
    337 	struct vm_map_kernel *submap;
    338 {
    339 	int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
    340 
    341 	KASSERT(vm_map_pmap(map) == pmap_kernel());
    342 
    343 	size = round_page(size);	/* round up to pagesize */
    344 
    345 	/*
    346 	 * first allocate a blank spot in the parent map
    347 	 */
    348 
    349 	if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET, 0,
    350 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    351 	    UVM_ADV_RANDOM, mapflags)) != 0) {
    352 	       panic("uvm_km_suballoc: unable to allocate space in parent map");
    353 	}
    354 
    355 	/*
    356 	 * set VM bounds (min is filled in by uvm_map)
    357 	 */
    358 
    359 	*max = *min + size;
    360 
    361 	/*
    362 	 * add references to pmap and create or init the submap
    363 	 */
    364 
    365 	pmap_reference(vm_map_pmap(map));
    366 	if (submap == NULL) {
    367 		submap = malloc(sizeof(*submap), M_VMMAP, M_WAITOK);
    368 		if (submap == NULL)
    369 			panic("uvm_km_suballoc: unable to create submap");
    370 	}
    371 	uvm_map_setup_kernel(submap, *min, *max, flags);
    372 	submap->vmk_map.pmap = vm_map_pmap(map);
    373 
    374 	/*
    375 	 * now let uvm_map_submap plug in it...
    376 	 */
    377 
    378 	if (uvm_map_submap(map, *min, *max, &submap->vmk_map) != 0)
    379 		panic("uvm_km_suballoc: submap allocation failed");
    380 
    381 	return(&submap->vmk_map);
    382 }
    383 
    384 /*
    385  * uvm_km_pgremove: remove pages from a kernel uvm_object.
    386  *
    387  * => when you unmap a part of anonymous kernel memory you want to toss
    388  *    the pages right away.    (this gets called from uvm_unmap_...).
    389  */
    390 
    391 void
    392 uvm_km_pgremove(startva, endva)
    393 	vaddr_t startva, endva;
    394 {
    395 	struct uvm_object * const uobj = uvm.kernel_object;
    396 	const voff_t start = startva - vm_map_min(kernel_map);
    397 	const voff_t end = endva - vm_map_min(kernel_map);
    398 	struct vm_page *pg;
    399 	voff_t curoff, nextoff;
    400 	int swpgonlydelta = 0;
    401 	UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
    402 
    403 	KASSERT(VM_MIN_KERNEL_ADDRESS <= startva);
    404 	KASSERT(startva < endva);
    405 	KASSERT(endva < VM_MAX_KERNEL_ADDRESS);
    406 
    407 	simple_lock(&uobj->vmobjlock);
    408 
    409 	for (curoff = start; curoff < end; curoff = nextoff) {
    410 		nextoff = curoff + PAGE_SIZE;
    411 		pg = uvm_pagelookup(uobj, curoff);
    412 		if (pg != NULL && pg->flags & PG_BUSY) {
    413 			pg->flags |= PG_WANTED;
    414 			UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    415 				    "km_pgrm", 0);
    416 			simple_lock(&uobj->vmobjlock);
    417 			nextoff = curoff;
    418 			continue;
    419 		}
    420 
    421 		/*
    422 		 * free the swap slot, then the page.
    423 		 */
    424 
    425 		if (pg == NULL &&
    426 		    uao_find_swslot(uobj, curoff >> PAGE_SHIFT) > 0) {
    427 			swpgonlydelta++;
    428 		}
    429 		uao_dropswap(uobj, curoff >> PAGE_SHIFT);
    430 		if (pg != NULL) {
    431 			uvm_lock_pageq();
    432 			uvm_pagefree(pg);
    433 			uvm_unlock_pageq();
    434 		}
    435 	}
    436 	simple_unlock(&uobj->vmobjlock);
    437 
    438 	if (swpgonlydelta > 0) {
    439 		simple_lock(&uvm.swap_data_lock);
    440 		KASSERT(uvmexp.swpgonly >= swpgonlydelta);
    441 		uvmexp.swpgonly -= swpgonlydelta;
    442 		simple_unlock(&uvm.swap_data_lock);
    443 	}
    444 }
    445 
    446 
    447 /*
    448  * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for "intrsafe"
    449  *    maps
    450  *
    451  * => when you unmap a part of anonymous kernel memory you want to toss
    452  *    the pages right away.    (this is called from uvm_unmap_...).
    453  * => none of the pages will ever be busy, and none of them will ever
    454  *    be on the active or inactive queues (because they have no object).
    455  */
    456 
    457 void
    458 uvm_km_pgremove_intrsafe(start, end)
    459 	vaddr_t start, end;
    460 {
    461 	struct vm_page *pg;
    462 	paddr_t pa;
    463 	UVMHIST_FUNC("uvm_km_pgremove_intrsafe"); UVMHIST_CALLED(maphist);
    464 
    465 	KASSERT(VM_MIN_KERNEL_ADDRESS <= start);
    466 	KASSERT(start < end);
    467 	KASSERT(end < VM_MAX_KERNEL_ADDRESS);
    468 
    469 	for (; start < end; start += PAGE_SIZE) {
    470 		if (!pmap_extract(pmap_kernel(), start, &pa)) {
    471 			continue;
    472 		}
    473 		pg = PHYS_TO_VM_PAGE(pa);
    474 		KASSERT(pg);
    475 		KASSERT(pg->uobject == NULL && pg->uanon == NULL);
    476 		uvm_pagefree(pg);
    477 	}
    478 }
    479 
    480 #if defined(DEBUG)
    481 void
    482 uvm_km_check_empty(vaddr_t start, vaddr_t end, boolean_t intrsafe)
    483 {
    484 	vaddr_t va;
    485 
    486 	KDASSERT(VM_MIN_KERNEL_ADDRESS <= start);
    487 	KDASSERT(start < end);
    488 	KDASSERT(end < VM_MAX_KERNEL_ADDRESS);
    489 
    490 	for (va = start; va < end; va += PAGE_SIZE) {
    491 		if (pmap_extract(pmap_kernel(), va, NULL)) {
    492 			panic("uvm_map_km_check_empty: has page mapped at %p",
    493 			    (const void *)va);
    494 		}
    495 		if (!intrsafe) {
    496 			const struct vm_page *pg;
    497 
    498 			simple_lock(&uvm.kernel_object->vmobjlock);
    499 			pg = uvm_pagelookup(uvm.kernel_object,
    500 			    va - vm_map_min(kernel_map));
    501 			simple_unlock(&uvm.kernel_object->vmobjlock);
    502 			if (pg) {
    503 				panic("uvm_map_km_check_empty: "
    504 				    "has page hashed at %p", (const void *)va);
    505 			}
    506 		}
    507 	}
    508 }
    509 #endif /* defined(DEBUG) */
    510 
    511 /*
    512  * uvm_km_alloc: allocate an area of kernel memory.
    513  *
    514  * => NOTE: we can return NULL even if we can wait if there is not enough
    515  *	free VM space in the map... caller should be prepared to handle
    516  *	this case.
    517  * => we return KVA of memory allocated
    518  */
    519 
    520 vaddr_t
    521 uvm_km_alloc(map, size, align, flags)
    522 	struct vm_map *map;
    523 	vsize_t size;
    524 	vsize_t align;
    525 	uvm_flag_t flags;
    526 {
    527 	vaddr_t kva, loopva;
    528 	vaddr_t offset;
    529 	vsize_t loopsize;
    530 	struct vm_page *pg;
    531 	struct uvm_object *obj;
    532 	int pgaflags;
    533 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    534 
    535 	UVMHIST_LOG(maphist,"  (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
    536 		    map, obj, size, flags);
    537 	KASSERT(vm_map_pmap(map) == pmap_kernel());
    538 
    539 	KASSERT((flags & UVM_KMF_TYPEMASK) == UVM_KMF_WIRED ||
    540 		(flags & UVM_KMF_TYPEMASK) == UVM_KMF_PAGEABLE ||
    541 		(flags & UVM_KMF_TYPEMASK) == UVM_KMF_VAONLY);
    542 
    543 	/*
    544 	 * setup for call
    545 	 */
    546 
    547 	kva = vm_map_min(map);	/* hint */
    548 	size = round_page(size);
    549 
    550 	if (flags & UVM_KMF_PAGEABLE)
    551 		obj = uvm.kernel_object;
    552 	else
    553 		obj = NULL;
    554 
    555 	/*
    556 	 * allocate some virtual space
    557 	 */
    558 
    559 	if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
    560 	    align, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    561 	    UVM_ADV_RANDOM,
    562 	    (flags & (UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT | UVM_KMF_WAITVA))
    563 	    | UVM_FLAG_QUANTUM)) != 0)) {
    564 		UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
    565 		return(0);
    566 	}
    567 
    568 	/*
    569 	 * if all we wanted was VA, return now
    570 	 */
    571 
    572 	if (flags & (UVM_KMF_VAONLY | UVM_KMF_PAGEABLE)) {
    573 		UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
    574 		return(kva);
    575 	}
    576 
    577 	/*
    578 	 * recover object offset from virtual address
    579 	 */
    580 
    581 	offset = kva - vm_map_min(kernel_map);
    582 	UVMHIST_LOG(maphist, "  kva=0x%x, offset=0x%x", kva, offset,0,0);
    583 
    584 	/*
    585 	 * now allocate and map in the memory... note that we are the only ones
    586 	 * whom should ever get a handle on this area of VM.
    587 	 */
    588 
    589 	loopva = kva;
    590 	loopsize = size;
    591 
    592 	pgaflags = UVM_PGA_USERESERVE;
    593 	if (flags & UVM_KMF_ZERO)
    594 		pgaflags |= UVM_PGA_ZERO;
    595 	while (loopsize) {
    596 		KASSERT(!pmap_extract(pmap_kernel(), loopva, NULL));
    597 
    598 		pg = uvm_pagealloc(NULL, offset, NULL, pgaflags);
    599 
    600 		/*
    601 		 * out of memory?
    602 		 */
    603 
    604 		if (__predict_false(pg == NULL)) {
    605 			if ((flags & UVM_KMF_NOWAIT) ||
    606 			    ((flags & UVM_KMF_CANFAIL) && uvm_swapisfull())) {
    607 				/* free everything! */
    608 				uvm_km_free(map, kva, size,
    609 				    flags & UVM_KMF_TYPEMASK);
    610 				return (0);
    611 			} else {
    612 				uvm_wait("km_getwait2");	/* sleep here */
    613 				continue;
    614 			}
    615 		}
    616 
    617 		pg->flags &= ~PG_BUSY;	/* new page */
    618 		UVM_PAGE_OWN(pg, NULL);
    619 
    620 		/*
    621 		 * map it in
    622 		 */
    623 
    624 		pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
    625 		    VM_PROT_READ | VM_PROT_WRITE);
    626 		loopva += PAGE_SIZE;
    627 		offset += PAGE_SIZE;
    628 		loopsize -= PAGE_SIZE;
    629 	}
    630 
    631        	pmap_update(pmap_kernel());
    632 
    633 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    634 	return(kva);
    635 }
    636 
    637 /*
    638  * uvm_km_free: free an area of kernel memory
    639  */
    640 
    641 void
    642 uvm_km_free(map, addr, size, flags)
    643 	struct vm_map *map;
    644 	vaddr_t addr;
    645 	vsize_t size;
    646 	uvm_flag_t flags;
    647 {
    648 
    649 	KASSERT((flags & UVM_KMF_TYPEMASK) == UVM_KMF_WIRED ||
    650 		(flags & UVM_KMF_TYPEMASK) == UVM_KMF_PAGEABLE ||
    651 		(flags & UVM_KMF_TYPEMASK) == UVM_KMF_VAONLY);
    652 	KASSERT((addr & PAGE_MASK) == 0);
    653 	KASSERT(vm_map_pmap(map) == pmap_kernel());
    654 
    655 	size = round_page(size);
    656 
    657 	if (flags & UVM_KMF_PAGEABLE) {
    658 		uvm_km_pgremove(addr, addr + size);
    659 		pmap_remove(pmap_kernel(), addr, addr + size);
    660 	} else if (flags & UVM_KMF_WIRED) {
    661 		uvm_km_pgremove_intrsafe(addr, addr + size);
    662 		pmap_kremove(addr, size);
    663 	}
    664 
    665 	uvm_unmap1(map, addr, addr + size, UVM_FLAG_QUANTUM|UVM_FLAG_VAONLY);
    666 }
    667 
    668 /* Sanity; must specify both or none. */
    669 #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
    670     (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
    671 #error Must specify MAP and UNMAP together.
    672 #endif
    673 
    674 /*
    675  * uvm_km_alloc_poolpage: allocate a page for the pool allocator
    676  *
    677  * => if the pmap specifies an alternate mapping method, we use it.
    678  */
    679 
    680 /* ARGSUSED */
    681 vaddr_t
    682 uvm_km_alloc_poolpage_cache(map, waitok)
    683 	struct vm_map *map;
    684 	boolean_t waitok;
    685 {
    686 #if defined(PMAP_MAP_POOLPAGE)
    687 	return uvm_km_alloc_poolpage(map, waitok);
    688 #else
    689 	struct vm_page *pg;
    690 	struct pool *pp = &vm_map_to_kernel(map)->vmk_vacache;
    691 	vaddr_t va;
    692 	int s = 0xdeadbeaf; /* XXX: gcc */
    693 	const boolean_t intrsafe = (map->flags & VM_MAP_INTRSAFE) != 0;
    694 
    695 	if ((map->flags & VM_MAP_VACACHE) == 0)
    696 		return uvm_km_alloc_poolpage(map, waitok);
    697 
    698 	if (intrsafe)
    699 		s = splvm();
    700 	va = (vaddr_t)pool_get(pp, waitok ? PR_WAITOK : PR_NOWAIT);
    701 	if (intrsafe)
    702 		splx(s);
    703 	if (va == 0)
    704 		return 0;
    705 	KASSERT(!pmap_extract(pmap_kernel(), va, NULL));
    706 again:
    707 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    708 	if (__predict_false(pg == NULL)) {
    709 		if (waitok) {
    710 			uvm_wait("plpg");
    711 			goto again;
    712 		} else {
    713 			if (intrsafe)
    714 				s = splvm();
    715 			pool_put(pp, (void *)va);
    716 			if (intrsafe)
    717 				splx(s);
    718 			return 0;
    719 		}
    720 	}
    721 	pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg),
    722 	    VM_PROT_READ|VM_PROT_WRITE);
    723 	pmap_update(pmap_kernel());
    724 
    725 	return va;
    726 #endif /* PMAP_MAP_POOLPAGE */
    727 }
    728 
    729 vaddr_t
    730 uvm_km_alloc_poolpage(map, waitok)
    731 	struct vm_map *map;
    732 	boolean_t waitok;
    733 {
    734 #if defined(PMAP_MAP_POOLPAGE)
    735 	struct vm_page *pg;
    736 	vaddr_t va;
    737 
    738  again:
    739 	pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
    740 	if (__predict_false(pg == NULL)) {
    741 		if (waitok) {
    742 			uvm_wait("plpg");
    743 			goto again;
    744 		} else
    745 			return (0);
    746 	}
    747 	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
    748 	if (__predict_false(va == 0))
    749 		uvm_pagefree(pg);
    750 	return (va);
    751 #else
    752 	vaddr_t va;
    753 	int s = 0xdeadbeaf; /* XXX: gcc */
    754 	const boolean_t intrsafe = (map->flags & VM_MAP_INTRSAFE) != 0;
    755 
    756 	if (intrsafe)
    757 		s = splvm();
    758 	va = uvm_km_alloc(map, PAGE_SIZE, 0,
    759 	    (waitok ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK) | UVM_KMF_WIRED);
    760 	if (intrsafe)
    761 		splx(s);
    762 	return (va);
    763 #endif /* PMAP_MAP_POOLPAGE */
    764 }
    765 
    766 /*
    767  * uvm_km_free_poolpage: free a previously allocated pool page
    768  *
    769  * => if the pmap specifies an alternate unmapping method, we use it.
    770  */
    771 
    772 /* ARGSUSED */
    773 void
    774 uvm_km_free_poolpage_cache(map, addr)
    775 	struct vm_map *map;
    776 	vaddr_t addr;
    777 {
    778 #if defined(PMAP_UNMAP_POOLPAGE)
    779 	uvm_km_free_poolpage(map, addr);
    780 #else
    781 	struct pool *pp;
    782 	int s = 0xdeadbeaf; /* XXX: gcc */
    783 	const boolean_t intrsafe = (map->flags & VM_MAP_INTRSAFE) != 0;
    784 
    785 	if ((map->flags & VM_MAP_VACACHE) == 0) {
    786 		uvm_km_free_poolpage(map, addr);
    787 		return;
    788 	}
    789 
    790 	KASSERT(pmap_extract(pmap_kernel(), addr, NULL));
    791 	uvm_km_pgremove_intrsafe(addr, addr + PAGE_SIZE);
    792 	pmap_kremove(addr, PAGE_SIZE);
    793 #if defined(DEBUG)
    794 	pmap_update(pmap_kernel());
    795 #endif
    796 	KASSERT(!pmap_extract(pmap_kernel(), addr, NULL));
    797 	pp = &vm_map_to_kernel(map)->vmk_vacache;
    798 	if (intrsafe)
    799 		s = splvm();
    800 	pool_put(pp, (void *)addr);
    801 	if (intrsafe)
    802 		splx(s);
    803 #endif
    804 }
    805 
    806 /* ARGSUSED */
    807 void
    808 uvm_km_free_poolpage(map, addr)
    809 	struct vm_map *map;
    810 	vaddr_t addr;
    811 {
    812 #if defined(PMAP_UNMAP_POOLPAGE)
    813 	paddr_t pa;
    814 
    815 	pa = PMAP_UNMAP_POOLPAGE(addr);
    816 	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
    817 #else
    818 	int s = 0xdeadbeaf; /* XXX: gcc */
    819 	const boolean_t intrsafe = (map->flags & VM_MAP_INTRSAFE) != 0;
    820 
    821 	if (intrsafe)
    822 		s = splvm();
    823 	uvm_km_free(map, addr, PAGE_SIZE, UVM_KMF_WIRED);
    824 	if (intrsafe)
    825 		splx(s);
    826 #endif /* PMAP_UNMAP_POOLPAGE */
    827 }
    828