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