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