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