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