uvm_km.c revision 1.22
1/*	$NetBSD: uvm_km.c,v 1.22 1999/03/26 21:58:39 mycroft 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#include "opt_uvmhist.h"
70#include "opt_pmap_new.h"
71
72/*
73 * uvm_km.c: handle kernel memory allocation and management
74 */
75
76/*
77 * overview of kernel memory management:
78 *
79 * the kernel virtual address space is mapped by "kernel_map."   kernel_map
80 * starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
81 * note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
82 *
83 * the kernel_map has several "submaps."   submaps can only appear in
84 * the kernel_map (user processes can't use them).   submaps "take over"
85 * the management of a sub-range of the kernel's address space.  submaps
86 * are typically allocated at boot time and are never released.   kernel
87 * virtual address space that is mapped by a submap is locked by the
88 * submap's lock -- not the kernel_map's lock.
89 *
90 * thus, the useful feature of submaps is that they allow us to break
91 * up the locking and protection of the kernel address space into smaller
92 * chunks.
93 *
94 * the vm system has several standard kernel submaps, including:
95 *   kmem_map => contains only wired kernel memory for the kernel
96 *		malloc.   *** access to kmem_map must be protected
97 *		by splimp() because we are allowed to call malloc()
98 *		at interrupt time ***
99 *   mb_map => memory for large mbufs,  *** protected by splimp ***
100 *   pager_map => used to map "buf" structures into kernel space
101 *   exec_map => used during exec to handle exec args
102 *   etc...
103 *
104 * the kernel allocates its private memory out of special uvm_objects whose
105 * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
106 * are "special" and never die).   all kernel objects should be thought of
107 * as large, fixed-sized, sparsely populated uvm_objects.   each kernel
108 * object is equal to the size of kernel virtual address space (i.e. the
109 * value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
110 *
111 * most kernel private memory lives in kernel_object.   the only exception
112 * to this is for memory that belongs to submaps that must be protected
113 * by splimp().    each of these submaps has their own private kernel
114 * object (e.g. kmem_object, mb_object).
115 *
116 * note that just because a kernel object spans the entire kernel virutal
117 * address space doesn't mean that it has to be mapped into the entire space.
118 * large chunks of a kernel object's space go unused either because
119 * that area of kernel VM is unmapped, or there is some other type of
120 * object mapped into that range (e.g. a vnode).    for submap's kernel
121 * objects, the only part of the object that can ever be populated is the
122 * offsets that are managed by the submap.
123 *
124 * note that the "offset" in a kernel object is always the kernel virtual
125 * address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
126 * example:
127 *   suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
128 *   uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
129 *   kernel map].    if uvm_km_alloc returns virtual address 0xf8235000,
130 *   then that means that the page at offset 0x235000 in kernel_object is
131 *   mapped at 0xf8235000.
132 *
133 * note that the offsets in kmem_object and mb_object also follow this
134 * rule.   this means that the offsets for kmem_object must fall in the
135 * range of [vm_map_min(kmem_object) - vm_map_min(kernel_map)] to
136 * [vm_map_max(kmem_object) - vm_map_min(kernel_map)], so the offsets
137 * in those objects will typically not start at zero.
138 *
139 * kernel object have one other special property: when the kernel virtual
140 * memory mapping them is unmapped, the backing memory in the object is
141 * freed right away.   this is done with the uvm_km_pgremove() function.
142 * this has to be done because there is no backing store for kernel pages
143 * and no need to save them after they are no longer referenced.
144 */
145
146#include <sys/param.h>
147#include <sys/systm.h>
148#include <sys/proc.h>
149
150#include <vm/vm.h>
151#include <vm/vm_page.h>
152#include <vm/vm_kern.h>
153
154#include <uvm/uvm.h>
155
156/*
157 * global data structures
158 */
159
160vm_map_t kernel_map = NULL;
161
162/*
163 * local functions
164 */
165
166static int uvm_km_get __P((struct uvm_object *, vaddr_t,
167													 vm_page_t *, int *, int, vm_prot_t, int, int));
168/*
169 * local data structues
170 */
171
172static struct vm_map		kernel_map_store;
173static struct uvm_object	kmem_object_store;
174static struct uvm_object	mb_object_store;
175
176static struct uvm_pagerops km_pager = {
177	NULL,	/* init */
178	NULL, /* reference */
179	NULL, /* detach */
180	NULL, /* fault */
181	NULL, /* flush */
182	uvm_km_get, /* get */
183	/* ... rest are NULL */
184};
185
186/*
187 * uvm_km_get: pager get function for kernel objects
188 *
189 * => currently we do not support pageout to the swap area, so this
190 *    pager is very simple.    eventually we may want an anonymous
191 *    object pager which will do paging.
192 * => XXXCDC: this pager should be phased out in favor of the aobj pager
193 */
194
195
196static int
197uvm_km_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
198	struct uvm_object *uobj;
199	vaddr_t offset;
200	struct vm_page **pps;
201	int *npagesp;
202	int centeridx, advice, flags;
203	vm_prot_t access_type;
204{
205	vaddr_t current_offset;
206	vm_page_t ptmp;
207	int lcv, gotpages, maxpages;
208	boolean_t done;
209	UVMHIST_FUNC("uvm_km_get"); UVMHIST_CALLED(maphist);
210
211	UVMHIST_LOG(maphist, "flags=%d", flags,0,0,0);
212
213	/*
214	 * get number of pages
215	 */
216
217	maxpages = *npagesp;
218
219	/*
220	 * step 1: handled the case where fault data structures are locked.
221	 */
222
223	if (flags & PGO_LOCKED) {
224
225		/*
226		 * step 1a: get pages that are already resident.   only do
227		 * this if the data structures are locked (i.e. the first time
228		 * through).
229		 */
230
231		done = TRUE;	/* be optimistic */
232		gotpages = 0;	/* # of pages we got so far */
233
234		for (lcv = 0, current_offset = offset ;
235		    lcv < maxpages ; lcv++, current_offset += PAGE_SIZE) {
236
237			/* do we care about this page?  if not, skip it */
238			if (pps[lcv] == PGO_DONTCARE)
239				continue;
240
241			/* lookup page */
242			ptmp = uvm_pagelookup(uobj, current_offset);
243
244			/* null?  attempt to allocate the page */
245			if (ptmp == NULL) {
246				ptmp = uvm_pagealloc(uobj, current_offset,
247				    NULL);
248				if (ptmp) {
249					/* new page */
250					ptmp->flags &= ~(PG_BUSY|PG_FAKE);
251					UVM_PAGE_OWN(ptmp, NULL);
252					uvm_pagezero(ptmp);
253				}
254			}
255
256			/*
257			 * to be useful must get a non-busy, non-released page
258			 */
259			if (ptmp == NULL ||
260			    (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
261				if (lcv == centeridx ||
262				    (flags & PGO_ALLPAGES) != 0)
263					/* need to do a wait or I/O! */
264					done = FALSE;
265				continue;
266			}
267
268			/*
269			 * useful page: busy/lock it and plug it in our
270			 * result array
271			 */
272
273			/* caller must un-busy this page */
274			ptmp->flags |= PG_BUSY;
275			UVM_PAGE_OWN(ptmp, "uvm_km_get1");
276			pps[lcv] = ptmp;
277			gotpages++;
278
279		}	/* "for" lcv loop */
280
281		/*
282		 * step 1b: now we've either done everything needed or we
283		 * to unlock and do some waiting or I/O.
284		 */
285
286		UVMHIST_LOG(maphist, "<- done (done=%d)", done, 0,0,0);
287
288		*npagesp = gotpages;
289		if (done)
290			return(VM_PAGER_OK);		/* bingo! */
291		else
292			return(VM_PAGER_UNLOCK);	/* EEK!   Need to
293							 * unlock and I/O */
294	}
295
296	/*
297	 * step 2: get non-resident or busy pages.
298	 * object is locked.   data structures are unlocked.
299	 */
300
301	for (lcv = 0, current_offset = offset ;
302	    lcv < maxpages ; lcv++, current_offset += PAGE_SIZE) {
303
304		/* skip over pages we've already gotten or don't want */
305		/* skip over pages we don't _have_ to get */
306		if (pps[lcv] != NULL ||
307		    (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
308			continue;
309
310		/*
311		 * we have yet to locate the current page (pps[lcv]).   we
312		 * first look for a page that is already at the current offset.
313		 * if we find a page, we check to see if it is busy or
314		 * released.  if that is the case, then we sleep on the page
315		 * until it is no longer busy or released and repeat the
316		 * lookup.    if the page we found is neither busy nor
317		 * released, then we busy it (so we own it) and plug it into
318		 * pps[lcv].   this 'break's the following while loop and
319		 * indicates we are ready to move on to the next page in the
320		 * "lcv" loop above.
321		 *
322		 * if we exit the while loop with pps[lcv] still set to NULL,
323		 * then it means that we allocated a new busy/fake/clean page
324		 * ptmp in the object and we need to do I/O to fill in the
325		 * data.
326		 */
327
328		while (pps[lcv] == NULL) {	/* top of "pps" while loop */
329
330			/* look for a current page */
331			ptmp = uvm_pagelookup(uobj, current_offset);
332
333			/* nope?   allocate one now (if we can) */
334			if (ptmp == NULL) {
335
336				ptmp = uvm_pagealloc(uobj, current_offset,
337				    NULL);	/* alloc */
338
339				/* out of RAM? */
340				if (ptmp == NULL) {
341					simple_unlock(&uobj->vmobjlock);
342					uvm_wait("kmgetwait1");
343					simple_lock(&uobj->vmobjlock);
344					/* goto top of pps while loop */
345					continue;
346				}
347
348				/*
349				 * got new page ready for I/O.  break pps
350				 * while loop.  pps[lcv] is still NULL.
351				 */
352				break;
353			}
354
355			/* page is there, see if we need to wait on it */
356			if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
357				ptmp->flags |= PG_WANTED;
358				UVM_UNLOCK_AND_WAIT(ptmp,&uobj->vmobjlock, 0,
359				    "uvn_get",0);
360				simple_lock(&uobj->vmobjlock);
361				continue;	/* goto top of pps while loop */
362			}
363
364			/*
365			 * if we get here then the page has become resident
366			 * and unbusy between steps 1 and 2.  we busy it now
367			 * (so we own it) and set pps[lcv] (so that we exit
368			 * the while loop).  caller must un-busy.
369			 */
370			ptmp->flags |= PG_BUSY;
371			UVM_PAGE_OWN(ptmp, "uvm_km_get2");
372			pps[lcv] = ptmp;
373		}
374
375		/*
376		 * if we own the a valid page at the correct offset, pps[lcv]
377		 * will point to it.   nothing more to do except go to the
378		 * next page.
379		 */
380
381		if (pps[lcv])
382			continue;			/* next lcv */
383
384		/*
385		 * we have a "fake/busy/clean" page that we just allocated.
386		 * do the needed "i/o" (in this case that means zero it).
387		 */
388
389		uvm_pagezero(ptmp);
390		ptmp->flags &= ~(PG_FAKE);
391		pps[lcv] = ptmp;
392
393	}	/* lcv loop */
394
395	/*
396	 * finally, unlock object and return.
397	 */
398
399	simple_unlock(&uobj->vmobjlock);
400	UVMHIST_LOG(maphist, "<- done (OK)",0,0,0,0);
401	return(VM_PAGER_OK);
402}
403
404/*
405 * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
406 * KVM already allocated for text, data, bss, and static data structures).
407 *
408 * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
409 *    we assume that [min -> start] has already been allocated and that
410 *    "end" is the end.
411 */
412
413void
414uvm_km_init(start, end)
415	vaddr_t start, end;
416{
417	vaddr_t base = VM_MIN_KERNEL_ADDRESS;
418
419	/*
420	 * first, init kernel memory objects.
421	 */
422
423	/* kernel_object: for pageable anonymous kernel memory */
424	uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
425				 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
426
427	/* kmem_object: for malloc'd memory (wired, protected by splimp) */
428	simple_lock_init(&kmem_object_store.vmobjlock);
429	kmem_object_store.pgops = &km_pager;
430	TAILQ_INIT(&kmem_object_store.memq);
431	kmem_object_store.uo_npages = 0;
432	/* we are special.  we never die */
433	kmem_object_store.uo_refs = UVM_OBJ_KERN;
434	uvmexp.kmem_object = &kmem_object_store;
435
436	/* mb_object: for mbuf memory (always wired, protected by splimp) */
437	simple_lock_init(&mb_object_store.vmobjlock);
438	mb_object_store.pgops = &km_pager;
439	TAILQ_INIT(&mb_object_store.memq);
440	mb_object_store.uo_npages = 0;
441	/* we are special.  we never die */
442	mb_object_store.uo_refs = UVM_OBJ_KERN;
443	uvmexp.mb_object = &mb_object_store;
444
445	/*
446	 * init the map and reserve allready allocated kernel space
447	 * before installing.
448	 */
449
450	uvm_map_setup(&kernel_map_store, base, end, FALSE);
451	kernel_map_store.pmap = pmap_kernel();
452	if (uvm_map(&kernel_map_store, &base, start - base, NULL,
453	    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
454	    UVM_INH_NONE, UVM_ADV_RANDOM,UVM_FLAG_FIXED)) != KERN_SUCCESS)
455		panic("uvm_km_init: could not reserve space for kernel");
456
457	/*
458	 * install!
459	 */
460
461	kernel_map = &kernel_map_store;
462}
463
464/*
465 * uvm_km_suballoc: allocate a submap in the kernel map.   once a submap
466 * is allocated all references to that area of VM must go through it.  this
467 * allows the locking of VAs in kernel_map to be broken up into regions.
468 *
469 * => if `fixed' is true, *min specifies where the region described
470 *      by the submap must start
471 * => if submap is non NULL we use that as the submap, otherwise we
472 *	alloc a new map
473 */
474struct vm_map *
475uvm_km_suballoc(map, min, max, size, pageable, fixed, submap)
476	struct vm_map *map;
477	vaddr_t *min, *max;		/* OUT, OUT */
478	vsize_t size;
479	boolean_t pageable;
480	boolean_t fixed;
481	struct vm_map *submap;
482{
483	int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
484
485	size = round_page(size);	/* round up to pagesize */
486
487	/*
488	 * first allocate a blank spot in the parent map
489	 */
490
491	if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET,
492	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
493	    UVM_ADV_RANDOM, mapflags)) != KERN_SUCCESS) {
494	       panic("uvm_km_suballoc: unable to allocate space in parent map");
495	}
496
497	/*
498	 * set VM bounds (min is filled in by uvm_map)
499	 */
500
501	*max = *min + size;
502
503	/*
504	 * add references to pmap and create or init the submap
505	 */
506
507	pmap_reference(vm_map_pmap(map));
508	if (submap == NULL) {
509		submap = uvm_map_create(vm_map_pmap(map), *min, *max, pageable);
510		if (submap == NULL)
511			panic("uvm_km_suballoc: unable to create submap");
512	} else {
513		uvm_map_setup(submap, *min, *max, pageable);
514		submap->pmap = vm_map_pmap(map);
515	}
516
517	/*
518	 * now let uvm_map_submap plug in it...
519	 */
520
521	if (uvm_map_submap(map, *min, *max, submap) != KERN_SUCCESS)
522		panic("uvm_km_suballoc: submap allocation failed");
523
524	return(submap);
525}
526
527/*
528 * uvm_km_pgremove: remove pages from a kernel uvm_object.
529 *
530 * => when you unmap a part of anonymous kernel memory you want to toss
531 *    the pages right away.    (this gets called from uvm_unmap_...).
532 */
533
534#define UKM_HASH_PENALTY 4      /* a guess */
535
536void
537uvm_km_pgremove(uobj, start, end)
538	struct uvm_object *uobj;
539	vaddr_t start, end;
540{
541	boolean_t by_list, is_aobj;
542	struct vm_page *pp, *ppnext;
543	vaddr_t curoff;
544	UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
545
546	simple_lock(&uobj->vmobjlock);		/* lock object */
547
548	/* is uobj an aobj? */
549	is_aobj = uobj->pgops == &aobj_pager;
550
551	/* choose cheapest traversal */
552	by_list = (uobj->uo_npages <=
553	     ((end - start) >> PAGE_SHIFT) * UKM_HASH_PENALTY);
554
555	if (by_list)
556		goto loop_by_list;
557
558	/* by hash */
559
560	for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
561		pp = uvm_pagelookup(uobj, curoff);
562		if (pp == NULL)
563			continue;
564
565		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
566		    pp->flags & PG_BUSY, 0, 0);
567		/* now do the actual work */
568		if (pp->flags & PG_BUSY)
569			/* owner must check for this when done */
570			pp->flags |= PG_RELEASED;
571		else {
572			pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
573
574			/*
575			 * if this kernel object is an aobj, free the swap slot.
576			 */
577			if (is_aobj) {
578				uao_dropswap(uobj, curoff >> PAGE_SHIFT);
579			}
580
581			uvm_lock_pageq();
582			uvm_pagefree(pp);
583			uvm_unlock_pageq();
584		}
585		/* done */
586
587	}
588	simple_unlock(&uobj->vmobjlock);
589	return;
590
591loop_by_list:
592
593	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
594
595		ppnext = pp->listq.tqe_next;
596		if (pp->offset < start || pp->offset >= end) {
597			continue;
598		}
599
600		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
601		    pp->flags & PG_BUSY, 0, 0);
602		/* now do the actual work */
603		if (pp->flags & PG_BUSY)
604			/* owner must check for this when done */
605			pp->flags |= PG_RELEASED;
606		else {
607			pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
608
609			/*
610			 * if this kernel object is an aobj, free the swap slot.
611			 */
612			if (is_aobj) {
613				uao_dropswap(uobj, pp->offset >> PAGE_SHIFT);
614			}
615
616			uvm_lock_pageq();
617			uvm_pagefree(pp);
618			uvm_unlock_pageq();
619		}
620		/* done */
621
622	}
623	simple_unlock(&uobj->vmobjlock);
624	return;
625}
626
627
628/*
629 * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
630 *
631 * => we map wired memory into the specified map using the obj passed in
632 * => NOTE: we can return NULL even if we can wait if there is not enough
633 *	free VM space in the map... caller should be prepared to handle
634 *	this case.
635 * => we return KVA of memory allocated
636 * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
637 *	lock the map
638 */
639
640vaddr_t
641uvm_km_kmemalloc(map, obj, size, flags)
642	vm_map_t map;
643	struct uvm_object *obj;
644	vsize_t size;
645	int flags;
646{
647	vaddr_t kva, loopva;
648	vaddr_t offset;
649	struct vm_page *pg;
650	UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
651
652
653	UVMHIST_LOG(maphist,"  (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
654	map, obj, size, flags);
655#ifdef DIAGNOSTIC
656	/* sanity check */
657	if (vm_map_pmap(map) != pmap_kernel())
658		panic("uvm_km_kmemalloc: invalid map");
659#endif
660
661	/*
662	 * setup for call
663	 */
664
665	size = round_page(size);
666	kva = vm_map_min(map);	/* hint */
667
668	/*
669	 * allocate some virtual space
670	 */
671
672	if (uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
673	      UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
674			  UVM_ADV_RANDOM, (flags & UVM_KMF_TRYLOCK)))
675			!= KERN_SUCCESS) {
676		UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
677		return(0);
678	}
679
680	/*
681	 * if all we wanted was VA, return now
682	 */
683
684	if (flags & UVM_KMF_VALLOC) {
685		UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
686		return(kva);
687	}
688	/*
689	 * recover object offset from virtual address
690	 */
691
692	offset = kva - vm_map_min(kernel_map);
693	UVMHIST_LOG(maphist, "  kva=0x%x, offset=0x%x", kva, offset,0,0);
694
695	/*
696	 * now allocate and map in the memory... note that we are the only ones
697	 * whom should ever get a handle on this area of VM.
698	 */
699
700	loopva = kva;
701	while (size) {
702		simple_lock(&obj->vmobjlock);
703		pg = uvm_pagealloc(obj, offset, NULL);
704		if (pg) {
705			pg->flags &= ~PG_BUSY;	/* new page */
706			UVM_PAGE_OWN(pg, NULL);
707		}
708		simple_unlock(&obj->vmobjlock);
709
710		/*
711		 * out of memory?
712		 */
713
714		if (pg == NULL) {
715			if (flags & UVM_KMF_NOWAIT) {
716				/* free everything! */
717				uvm_unmap(map, kva, kva + size);
718				return(0);
719			} else {
720				uvm_wait("km_getwait2");	/* sleep here */
721				continue;
722			}
723		}
724
725		/*
726		 * map it in: note that we call pmap_enter with the map and
727		 * object unlocked in case we are kmem_map/kmem_object
728		 * (because if pmap_enter wants to allocate out of kmem_object
729		 * it will need to lock it itself!)
730		 */
731#if defined(PMAP_NEW)
732		pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg), VM_PROT_ALL);
733#else
734		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
735		    UVM_PROT_ALL, TRUE, 0);
736#endif
737		loopva += PAGE_SIZE;
738		offset += PAGE_SIZE;
739		size -= PAGE_SIZE;
740	}
741
742	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
743	return(kva);
744}
745
746/*
747 * uvm_km_free: free an area of kernel memory
748 */
749
750void
751uvm_km_free(map, addr, size)
752	vm_map_t map;
753	vaddr_t addr;
754	vsize_t size;
755{
756
757	uvm_unmap(map, trunc_page(addr), round_page(addr+size));
758}
759
760/*
761 * uvm_km_free_wakeup: free an area of kernel memory and wake up
762 * anyone waiting for vm space.
763 *
764 * => XXX: "wanted" bit + unlock&wait on other end?
765 */
766
767void
768uvm_km_free_wakeup(map, addr, size)
769	vm_map_t map;
770	vaddr_t addr;
771	vsize_t size;
772{
773	vm_map_entry_t dead_entries;
774
775	vm_map_lock(map);
776	(void)uvm_unmap_remove(map, trunc_page(addr), round_page(addr+size),
777			 &dead_entries);
778	thread_wakeup(map);
779	vm_map_unlock(map);
780
781	if (dead_entries != NULL)
782		uvm_unmap_detach(dead_entries, 0);
783}
784
785/*
786 * uvm_km_alloc1: allocate wired down memory in the kernel map.
787 *
788 * => we can sleep if needed
789 */
790
791vaddr_t
792uvm_km_alloc1(map, size, zeroit)
793	vm_map_t map;
794	vsize_t size;
795	boolean_t zeroit;
796{
797	vaddr_t kva, loopva, offset;
798	struct vm_page *pg;
799	UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
800
801	UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
802
803#ifdef DIAGNOSTIC
804	if (vm_map_pmap(map) != pmap_kernel())
805		panic("uvm_km_alloc1");
806#endif
807
808	size = round_page(size);
809	kva = vm_map_min(map);		/* hint */
810
811	/*
812	 * allocate some virtual space
813	 */
814
815	if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
816	      UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
817			  UVM_ADV_RANDOM, 0)) != KERN_SUCCESS) {
818		UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
819		return(0);
820	}
821
822	/*
823	 * recover object offset from virtual address
824	 */
825
826	offset = kva - vm_map_min(kernel_map);
827	UVMHIST_LOG(maphist,"  kva=0x%x, offset=0x%x", kva, offset,0,0);
828
829	/*
830	 * now allocate the memory.  we must be careful about released pages.
831	 */
832
833	loopva = kva;
834	while (size) {
835		simple_lock(&uvm.kernel_object->vmobjlock);
836		pg = uvm_pagelookup(uvm.kernel_object, offset);
837
838		/*
839		 * if we found a page in an unallocated region, it must be
840		 * released
841		 */
842		if (pg) {
843			if ((pg->flags & PG_RELEASED) == 0)
844				panic("uvm_km_alloc1: non-released page");
845			pg->flags |= PG_WANTED;
846			UVM_UNLOCK_AND_WAIT(pg, &uvm.kernel_object->vmobjlock,
847			    0, "km_alloc", 0);
848			continue;   /* retry */
849		}
850
851		/* allocate ram */
852		pg = uvm_pagealloc(uvm.kernel_object, offset, NULL);
853		if (pg) {
854			pg->flags &= ~PG_BUSY;	/* new page */
855			UVM_PAGE_OWN(pg, NULL);
856		}
857		simple_unlock(&uvm.kernel_object->vmobjlock);
858		if (pg == NULL) {
859			uvm_wait("km_alloc1w");	/* wait for memory */
860			continue;
861		}
862
863		/* map it in */
864#if defined(PMAP_NEW)
865		pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg), UVM_PROT_ALL);
866#else
867		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
868		    UVM_PROT_ALL, TRUE, 0);
869#endif
870		loopva += PAGE_SIZE;
871		offset += PAGE_SIZE;
872		size -= PAGE_SIZE;
873	}
874
875	/*
876	 * zero on request (note that "size" is now zero due to the above loop
877	 * so we need to subtract kva from loopva to reconstruct the size).
878	 */
879
880	if (zeroit)
881		memset((caddr_t)kva, 0, loopva - kva);
882
883	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
884	return(kva);
885}
886
887/*
888 * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
889 *
890 * => memory is not allocated until fault time
891 */
892
893vaddr_t
894uvm_km_valloc(map, size)
895	vm_map_t map;
896	vsize_t size;
897{
898	vaddr_t kva;
899	UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
900
901	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
902
903#ifdef DIAGNOSTIC
904	if (vm_map_pmap(map) != pmap_kernel())
905		panic("uvm_km_valloc");
906#endif
907
908	size = round_page(size);
909	kva = vm_map_min(map);		/* hint */
910
911	/*
912	 * allocate some virtual space.  will be demand filled by kernel_object.
913	 */
914
915	if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
916	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
917	    UVM_ADV_RANDOM, 0)) != KERN_SUCCESS) {
918		UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
919		return(0);
920	}
921
922	UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
923	return(kva);
924}
925
926/*
927 * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
928 *
929 * => memory is not allocated until fault time
930 * => if no room in map, wait for space to free, unless requested size
931 *    is larger than map (in which case we return 0)
932 */
933
934vaddr_t
935uvm_km_valloc_wait(map, size)
936	vm_map_t map;
937	vsize_t size;
938{
939	vaddr_t kva;
940	UVMHIST_FUNC("uvm_km_valloc_wait"); UVMHIST_CALLED(maphist);
941
942	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
943
944#ifdef DIAGNOSTIC
945	if (vm_map_pmap(map) != pmap_kernel())
946		panic("uvm_km_valloc_wait");
947#endif
948
949	size = round_page(size);
950	if (size > vm_map_max(map) - vm_map_min(map))
951		return(0);
952
953	while (1) {
954		kva = vm_map_min(map);		/* hint */
955
956		/*
957		 * allocate some virtual space.   will be demand filled
958		 * by kernel_object.
959		 */
960
961		if (uvm_map(map, &kva, size, uvm.kernel_object,
962		    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL,
963		    UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, 0))
964		    == KERN_SUCCESS) {
965			UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
966			return(kva);
967		}
968
969		/*
970		 * failed.  sleep for a while (on map)
971		 */
972
973		UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
974		tsleep((caddr_t)map, PVM, "vallocwait", 0);
975	}
976	/*NOTREACHED*/
977}
978
979/* Sanity; must specify both or none. */
980#if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
981    (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
982#error Must specify MAP and UNMAP together.
983#endif
984
985/*
986 * uvm_km_alloc_poolpage: allocate a page for the pool allocator
987 *
988 * => if the pmap specifies an alternate mapping method, we use it.
989 */
990
991/* ARGSUSED */
992vaddr_t
993uvm_km_alloc_poolpage1(map, obj, waitok)
994	vm_map_t map;
995	struct uvm_object *obj;
996	boolean_t waitok;
997{
998#if defined(PMAP_MAP_POOLPAGE)
999	struct vm_page *pg;
1000	vaddr_t va;
1001
1002 again:
1003	pg = uvm_pagealloc(NULL, 0, NULL);
1004	if (pg == NULL) {
1005		if (waitok) {
1006			uvm_wait("plpg");
1007			goto again;
1008		} else
1009			return (0);
1010	}
1011	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
1012	if (va == 0)
1013		uvm_pagefree(pg);
1014	return (va);
1015#else
1016	vaddr_t va;
1017	int s;
1018
1019	/*
1020	 * NOTE: We may be called with a map that doens't require splimp
1021	 * protection (e.g. kernel_map).  However, it does not hurt to
1022	 * go to splimp in this case (since unprocted maps will never be
1023	 * accessed in interrupt context).
1024	 *
1025	 * XXX We may want to consider changing the interface to this
1026	 * XXX function.
1027	 */
1028
1029	s = splimp();
1030	va = uvm_km_kmemalloc(map, obj, PAGE_SIZE, waitok ? 0 : UVM_KMF_NOWAIT);
1031	splx(s);
1032	return (va);
1033#endif /* PMAP_MAP_POOLPAGE */
1034}
1035
1036/*
1037 * uvm_km_free_poolpage: free a previously allocated pool page
1038 *
1039 * => if the pmap specifies an alternate unmapping method, we use it.
1040 */
1041
1042/* ARGSUSED */
1043void
1044uvm_km_free_poolpage1(map, addr)
1045	vm_map_t map;
1046	vaddr_t addr;
1047{
1048#if defined(PMAP_UNMAP_POOLPAGE)
1049	paddr_t pa;
1050
1051	pa = PMAP_UNMAP_POOLPAGE(addr);
1052	uvm_pagefree(PHYS_TO_VM_PAGE(pa));
1053#else
1054	int s;
1055
1056	/*
1057	 * NOTE: We may be called with a map that doens't require splimp
1058	 * protection (e.g. kernel_map).  However, it does not hurt to
1059	 * go to splimp in this case (since unprocted maps will never be
1060	 * accessed in interrupt context).
1061	 *
1062	 * XXX We may want to consider changing the interface to this
1063	 * XXX function.
1064	 */
1065
1066	s = splimp();
1067	uvm_km_free(map, addr, PAGE_SIZE);
1068	splx(s);
1069#endif /* PMAP_UNMAP_POOLPAGE */
1070}
1071