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