Home | History | Annotate | Line # | Download | only in uvm
uvm_km.c revision 1.8
      1 /*	$NetBSD: uvm_km.c,v 1.8 1998/03/09 00:58:57 mrg 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 *, vm_offset_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 	vm_offset_t offset;
    205 	struct vm_page **pps;
    206 	int *npagesp;
    207 	int centeridx, advice, flags;
    208 	vm_prot_t access_type;
    209 {
    210 	vm_offset_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 					/* XXX: prevents pageout attempts */
    258 					ptmp->wire_count = 1;
    259 					uvm_pagezero(ptmp);
    260 				}
    261 			}
    262 
    263 			/*
    264 			 * to be useful must get a non-busy, non-released page
    265 			 */
    266 			if (ptmp == NULL ||
    267 			    (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
    268 				if (lcv == centeridx ||
    269 				    (flags & PGO_ALLPAGES) != 0)
    270 					/* need to do a wait or I/O! */
    271 					done = FALSE;
    272 				continue;
    273 			}
    274 
    275 			/*
    276 			 * useful page: busy/lock it and plug it in our
    277 			 * result array
    278 			 */
    279 
    280 			/* caller must un-busy this page */
    281 			ptmp->flags |= PG_BUSY;
    282 			UVM_PAGE_OWN(ptmp, "uvm_km_get1");
    283 			pps[lcv] = ptmp;
    284 			gotpages++;
    285 
    286 		}	/* "for" lcv loop */
    287 
    288 		/*
    289 		 * step 1b: now we've either done everything needed or we
    290 		 * to unlock and do some waiting or I/O.
    291 		 */
    292 
    293 		UVMHIST_LOG(maphist, "<- done (done=%d)", done, 0,0,0);
    294 
    295 		*npagesp = gotpages;
    296 		if (done)
    297 			return(VM_PAGER_OK);		/* bingo! */
    298 		else
    299 			return(VM_PAGER_UNLOCK);	/* EEK!   Need to
    300 							 * unlock and I/O */
    301 	}
    302 
    303 	/*
    304 	 * step 2: get non-resident or busy pages.
    305 	 * object is locked.   data structures are unlocked.
    306 	 */
    307 
    308 	for (lcv = 0, current_offset = offset ;
    309 	    lcv < maxpages ; lcv++, current_offset += PAGE_SIZE) {
    310 
    311 		/* skip over pages we've already gotten or don't want */
    312 		/* skip over pages we don't _have_ to get */
    313 		if (pps[lcv] != NULL ||
    314 		    (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
    315 			continue;
    316 
    317 		/*
    318 		 * we have yet to locate the current page (pps[lcv]).   we
    319 		 * first look for a page that is already at the current offset.
    320 		 * if we find a page, we check to see if it is busy or
    321 		 * released.  if that is the case, then we sleep on the page
    322 		 * until it is no longer busy or released and repeat the
    323 		 * lookup.    if the page we found is neither busy nor
    324 		 * released, then we busy it (so we own it) and plug it into
    325 		 * pps[lcv].   this 'break's the following while loop and
    326 		 * indicates we are ready to move on to the next page in the
    327 		 * "lcv" loop above.
    328 		 *
    329 		 * if we exit the while loop with pps[lcv] still set to NULL,
    330 		 * then it means that we allocated a new busy/fake/clean page
    331 		 * ptmp in the object and we need to do I/O to fill in the
    332 		 * data.
    333 		 */
    334 
    335 		while (pps[lcv] == NULL) {	/* top of "pps" while loop */
    336 
    337 			/* look for a current page */
    338 			ptmp = uvm_pagelookup(uobj, current_offset);
    339 
    340 			/* nope?   allocate one now (if we can) */
    341 			if (ptmp == NULL) {
    342 
    343 				ptmp = uvm_pagealloc(uobj, current_offset,
    344 				    NULL);	/* alloc */
    345 
    346 				/* out of RAM? */
    347 				if (ptmp == NULL) {
    348 					simple_unlock(&uobj->vmobjlock);
    349 					uvm_wait("kmgetwait1");
    350 					simple_lock(&uobj->vmobjlock);
    351 					/* goto top of pps while loop */
    352 					continue;
    353 				}
    354 
    355 				/*
    356 				 * got new page ready for I/O.  break pps
    357 				 * while loop.  pps[lcv] is still NULL.
    358 				 */
    359 				break;
    360 			}
    361 
    362 			/* page is there, see if we need to wait on it */
    363 			if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
    364 				ptmp->flags |= PG_WANTED;
    365 				UVM_UNLOCK_AND_WAIT(ptmp,&uobj->vmobjlock, 0,
    366 				    "uvn_get",0);
    367 				simple_lock(&uobj->vmobjlock);
    368 				continue;	/* goto top of pps while loop */
    369 			}
    370 
    371 			/*
    372 			 * if we get here then the page has become resident
    373 			 * and unbusy between steps 1 and 2.  we busy it now
    374 			 * (so we own it) and set pps[lcv] (so that we exit
    375 			 * the while loop).  caller must un-busy.
    376 			 */
    377 			ptmp->flags |= PG_BUSY;
    378 			UVM_PAGE_OWN(ptmp, "uvm_km_get2");
    379 			pps[lcv] = ptmp;
    380 		}
    381 
    382 		/*
    383 		 * if we own the a valid page at the correct offset, pps[lcv]
    384 		 * will point to it.   nothing more to do except go to the
    385 		 * next page.
    386 		 */
    387 
    388 		if (pps[lcv])
    389 			continue;			/* next lcv */
    390 
    391 		/*
    392 		 * we have a "fake/busy/clean" page that we just allocated.
    393 		 * do the needed "i/o" (in this case that means zero it).
    394 		 */
    395 
    396 		uvm_pagezero(ptmp);
    397 		ptmp->flags &= ~(PG_FAKE);
    398 		ptmp->wire_count = 1;	/* XXX: prevents pageout attempts */
    399 		pps[lcv] = ptmp;
    400 
    401 	}	/* lcv loop */
    402 
    403 	/*
    404 	 * finally, unlock object and return.
    405 	 */
    406 
    407 	simple_unlock(&uobj->vmobjlock);
    408 	UVMHIST_LOG(maphist, "<- done (OK)",0,0,0,0);
    409 	return(VM_PAGER_OK);
    410 }
    411 
    412 /*
    413  * uvm_km_init: init kernel maps and objects to reflect reality (i.e.
    414  * KVM already allocated for text, data, bss, and static data structures).
    415  *
    416  * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
    417  *    we assume that [min -> start] has already been allocated and that
    418  *    "end" is the end.
    419  */
    420 
    421 void
    422 uvm_km_init(start, end)
    423 	vm_offset_t start, end;
    424 {
    425 	vm_offset_t base = VM_MIN_KERNEL_ADDRESS;
    426 
    427 	/*
    428 	 * first, init kernel memory objects.
    429 	 */
    430 
    431 	/* kernel_object: for pageable anonymous kernel memory */
    432 	uvm.kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
    433 				 VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
    434 
    435 	/* kmem_object: for malloc'd memory (wired, protected by splimp) */
    436 	simple_lock_init(&kmem_object_store.vmobjlock);
    437 	kmem_object_store.pgops = &km_pager;
    438 	TAILQ_INIT(&kmem_object_store.memq);
    439 	kmem_object_store.uo_npages = 0;
    440 	/* we are special.  we never die */
    441 	kmem_object_store.uo_refs = UVM_OBJ_KERN;
    442 	uvmexp.kmem_object = &kmem_object_store;
    443 
    444 	/* mb_object: for mbuf memory (always wired, protected by splimp) */
    445 	simple_lock_init(&mb_object_store.vmobjlock);
    446 	mb_object_store.pgops = &km_pager;
    447 	TAILQ_INIT(&mb_object_store.memq);
    448 	mb_object_store.uo_npages = 0;
    449 	/* we are special.  we never die */
    450 	mb_object_store.uo_refs = UVM_OBJ_KERN;
    451 	uvmexp.mb_object = &mb_object_store;
    452 
    453 	/*
    454 	 * init the map and reserve allready allocated kernel space
    455 	 * before installing.
    456 	 */
    457 
    458 	uvm_map_setup(&kernel_map_store, base, end, FALSE);
    459 	kernel_map_store.pmap = pmap_kernel();
    460 	if (uvm_map(&kernel_map_store, &base, start - base, NULL,
    461 	    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL,
    462 	    UVM_INH_NONE, UVM_ADV_RANDOM,UVM_FLAG_FIXED)) != KERN_SUCCESS)
    463 		panic("uvm_km_init: could not reserve space for kernel");
    464 
    465 	/*
    466 	 * install!
    467 	 */
    468 
    469 	kernel_map = &kernel_map_store;
    470 }
    471 
    472 /*
    473  * uvm_km_suballoc: allocate a submap in the kernel map.   once a submap
    474  * is allocated all references to that area of VM must go through it.  this
    475  * allows the locking of VAs in kernel_map to be broken up into regions.
    476  *
    477  * => if `fixed' is true, *min specifies where the region described
    478  *      by the submap must start
    479  * => if submap is non NULL we use that as the submap, otherwise we
    480  *	alloc a new map
    481  */
    482 struct vm_map *
    483 uvm_km_suballoc(map, min, max, size, pageable, fixed, submap)
    484 	struct vm_map *map;
    485 	vm_offset_t *min, *max;		/* OUT, OUT */
    486 	vm_size_t size;
    487 	boolean_t pageable;
    488 	boolean_t fixed;
    489 	struct vm_map *submap;
    490 {
    491 	int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
    492 
    493 	size = round_page(size);	/* round up to pagesize */
    494 
    495 	/*
    496 	 * first allocate a blank spot in the parent map
    497 	 */
    498 
    499 	if (uvm_map(map, min, size, NULL, UVM_UNKNOWN_OFFSET,
    500 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    501 	    UVM_ADV_RANDOM, mapflags)) != KERN_SUCCESS) {
    502 	       panic("uvm_km_suballoc: unable to allocate space in parent map");
    503 	}
    504 
    505 	/*
    506 	 * set VM bounds (min is filled in by uvm_map)
    507 	 */
    508 
    509 	*max = *min + size;
    510 
    511 	/*
    512 	 * add references to pmap and create or init the submap
    513 	 */
    514 
    515 	pmap_reference(vm_map_pmap(map));
    516 	if (submap == NULL) {
    517 		submap = uvm_map_create(vm_map_pmap(map), *min, *max, pageable);
    518 		if (submap == NULL)
    519 			panic("uvm_km_suballoc: unable to create submap");
    520 	} else {
    521 		uvm_map_setup(submap, *min, *max, pageable);
    522 		submap->pmap = vm_map_pmap(map);
    523 	}
    524 
    525 	/*
    526 	 * now let uvm_map_submap plug in it...
    527 	 */
    528 
    529 	if (uvm_map_submap(map, *min, *max, submap) != KERN_SUCCESS)
    530 		panic("uvm_km_suballoc: submap allocation failed");
    531 
    532 	return(submap);
    533 }
    534 
    535 /*
    536  * uvm_km_pgremove: remove pages from a kernel uvm_object.
    537  *
    538  * => when you unmap a part of anonymous kernel memory you want to toss
    539  *    the pages right away.    (this gets called from uvm_unmap_...).
    540  */
    541 
    542 #define UKM_HASH_PENALTY 4      /* a guess */
    543 
    544 void
    545 uvm_km_pgremove(uobj, start, end)
    546 	struct uvm_object *uobj;
    547 	vm_offset_t start, end;
    548 {
    549 	boolean_t by_list, is_aobj;
    550 	struct vm_page *pp, *ppnext;
    551 	vm_offset_t curoff;
    552 	UVMHIST_FUNC("uvm_km_pgremove"); UVMHIST_CALLED(maphist);
    553 
    554 	simple_lock(&uobj->vmobjlock);		/* lock object */
    555 
    556 	/* is uobj an aobj? */
    557 	is_aobj = uobj->pgops == &aobj_pager;
    558 
    559 	/* choose cheapest traversal */
    560 	by_list = (uobj->uo_npages <=
    561 	     ((end - start) / PAGE_SIZE) * UKM_HASH_PENALTY);
    562 
    563 	if (by_list)
    564 		goto loop_by_list;
    565 
    566 	/* by hash */
    567 
    568 	for (curoff = start ; curoff < end ; curoff += PAGE_SIZE) {
    569 		pp = uvm_pagelookup(uobj, curoff);
    570 		if (pp == NULL)
    571 			continue;
    572 
    573 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    574 		    pp->flags & PG_BUSY, 0, 0);
    575 		/* now do the actual work */
    576 		if (pp->flags & PG_BUSY)
    577 			/* owner must check for this when done */
    578 			pp->flags |= PG_RELEASED;
    579 		else {
    580 			pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
    581 
    582 			/*
    583 			 * if this kernel object is an aobj, free the swap slot.
    584 			 */
    585 			if (is_aobj) {
    586 				int slot = uao_set_swslot(uobj,
    587 				    curoff / PAGE_SIZE, 0);
    588 
    589 				if (slot)
    590 					uvm_swap_free(slot, 1);
    591 			}
    592 
    593 			uvm_lock_pageq();
    594 			uvm_pagefree(pp);
    595 			uvm_unlock_pageq();
    596 		}
    597 		/* done */
    598 
    599 	}
    600 	simple_unlock(&uobj->vmobjlock);
    601 	return;
    602 
    603 loop_by_list:
    604 
    605 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = ppnext) {
    606 
    607 		ppnext = pp->listq.tqe_next;
    608 		if (pp->offset < start || pp->offset >= end) {
    609 			continue;
    610 		}
    611 
    612 		UVMHIST_LOG(maphist,"  page 0x%x, busy=%d", pp,
    613 		    pp->flags & PG_BUSY, 0, 0);
    614 		/* now do the actual work */
    615 		if (pp->flags & PG_BUSY)
    616 			/* owner must check for this when done */
    617 			pp->flags |= PG_RELEASED;
    618 		else {
    619 			pmap_page_protect(PMAP_PGARG(pp), VM_PROT_NONE);
    620 
    621 			/*
    622 			 * if this kernel object is an aobj, free the swap slot.
    623 			 */
    624 			if (is_aobj) {
    625 				int slot = uao_set_swslot(uobj,
    626 				    pp->offset / PAGE_SIZE, 0);
    627 
    628 				if (slot)
    629 					uvm_swap_free(slot, 1);
    630 			}
    631 
    632 			uvm_lock_pageq();
    633 			uvm_pagefree(pp);
    634 			uvm_unlock_pageq();
    635 		}
    636 		/* done */
    637 
    638 	}
    639 	simple_unlock(&uobj->vmobjlock);
    640 	return;
    641 }
    642 
    643 
    644 /*
    645  * uvm_km_kmemalloc: lower level kernel memory allocator for malloc()
    646  *
    647  * => we map wired memory into the specified map using the obj passed in
    648  * => NOTE: we can return NULL even if we can wait if there is not enough
    649  *	free VM space in the map... caller should be prepared to handle
    650  *	this case.
    651  * => we return KVA of memory allocated
    652  * => flags: NOWAIT, VALLOC - just allocate VA, TRYLOCK - fail if we can't
    653  *	lock the map
    654  */
    655 
    656 vm_offset_t
    657 uvm_km_kmemalloc(map, obj, size, flags)
    658 	vm_map_t map;
    659 	struct uvm_object *obj;
    660 	vm_size_t size;
    661 	int flags;
    662 {
    663 	vm_offset_t kva, loopva;
    664 	vm_offset_t offset;
    665 	struct vm_page *pg;
    666 	UVMHIST_FUNC("uvm_km_kmemalloc"); UVMHIST_CALLED(maphist);
    667 
    668 
    669 	UVMHIST_LOG(maphist,"  (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
    670 	map, obj, size, flags);
    671 #ifdef DIAGNOSTIC
    672 	/* sanity check */
    673 	if (vm_map_pmap(map) != pmap_kernel())
    674 		panic("uvm_km_kmemalloc: invalid map");
    675 #endif
    676 
    677 	/*
    678 	 * setup for call
    679 	 */
    680 
    681 	size = round_page(size);
    682 	kva = vm_map_min(map);	/* hint */
    683 
    684 	/*
    685 	 * allocate some virtual space
    686 	 */
    687 
    688 	if (uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
    689 	      UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    690 			  UVM_ADV_RANDOM, (flags & UVM_KMF_TRYLOCK)))
    691 			!= KERN_SUCCESS) {
    692 		UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
    693 		return(0);
    694 	}
    695 
    696 	/*
    697 	 * if all we wanted was VA, return now
    698 	 */
    699 
    700 	if (flags & UVM_KMF_VALLOC) {
    701 		UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
    702 		return(kva);
    703 	}
    704 	/*
    705 	 * recover object offset from virtual address
    706 	 */
    707 
    708 	offset = kva - vm_map_min(kernel_map);
    709 	UVMHIST_LOG(maphist, "  kva=0x%x, offset=0x%x", kva, offset,0,0);
    710 
    711 	/*
    712 	 * now allocate and map in the memory... note that we are the only ones
    713 	 * whom should ever get a handle on this area of VM.
    714 	 */
    715 
    716 	loopva = kva;
    717 	while (size) {
    718 		simple_lock(&obj->vmobjlock);
    719 		pg = uvm_pagealloc(obj, offset, NULL);
    720 		if (pg) {
    721 			pg->flags &= ~PG_BUSY;	/* new page */
    722 			UVM_PAGE_OWN(pg, NULL);
    723 
    724 			pg->wire_count = 1;
    725 			uvmexp.wired++;
    726 		}
    727 		simple_unlock(&obj->vmobjlock);
    728 
    729 		/*
    730 		 * out of memory?
    731 		 */
    732 
    733 		if (pg == NULL) {
    734 			if (flags & UVM_KMF_NOWAIT) {
    735 				/* free everything! */
    736 				uvm_unmap(map, kva, kva + size, 0);
    737 				return(0);
    738 			} else {
    739 				uvm_wait("km_getwait2");	/* sleep here */
    740 				continue;
    741 			}
    742 		}
    743 
    744 		/*
    745 		 * map it in: note that we call pmap_enter with the map and
    746 		 * object unlocked in case we are kmem_map/kmem_object
    747 		 * (because if pmap_enter wants to allocate out of kmem_object
    748 		 * it will need to lock it itself!)
    749 		 */
    750 #if defined(PMAP_NEW)
    751 		pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg), VM_PROT_ALL);
    752 #else
    753 		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    754 		    UVM_PROT_ALL, TRUE);
    755 #endif
    756 		loopva += PAGE_SIZE;
    757 		offset += PAGE_SIZE;
    758 		size -= PAGE_SIZE;
    759 	}
    760 
    761 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    762 	return(kva);
    763 }
    764 
    765 /*
    766  * uvm_km_free: free an area of kernel memory
    767  */
    768 
    769 void
    770 uvm_km_free(map, addr, size)
    771 	vm_map_t map;
    772 	vm_offset_t addr;
    773 	vm_size_t size;
    774 {
    775 
    776 	uvm_unmap(map, trunc_page(addr), round_page(addr+size), 1);
    777 }
    778 
    779 /*
    780  * uvm_km_free_wakeup: free an area of kernel memory and wake up
    781  * anyone waiting for vm space.
    782  *
    783  * => XXX: "wanted" bit + unlock&wait on other end?
    784  */
    785 
    786 void
    787 uvm_km_free_wakeup(map, addr, size)
    788 	vm_map_t map;
    789 	vm_offset_t addr;
    790 	vm_size_t size;
    791 {
    792 	vm_map_entry_t dead_entries;
    793 
    794 	vm_map_lock(map);
    795 	(void)uvm_unmap_remove(map, trunc_page(addr), round_page(addr+size), 1,
    796 			 &dead_entries);
    797 	thread_wakeup(map);
    798 	vm_map_unlock(map);
    799 
    800 	if (dead_entries != NULL)
    801 		uvm_unmap_detach(dead_entries, 0);
    802 }
    803 
    804 /*
    805  * uvm_km_alloc1: allocate wired down memory in the kernel map.
    806  *
    807  * => we can sleep if needed
    808  */
    809 
    810 vm_offset_t
    811 uvm_km_alloc1(map, size, zeroit)
    812 	vm_map_t map;
    813 	vm_size_t size;
    814 	boolean_t zeroit;
    815 {
    816 	vm_offset_t kva, loopva, offset;
    817 	struct vm_page *pg;
    818 	UVMHIST_FUNC("uvm_km_alloc1"); UVMHIST_CALLED(maphist);
    819 
    820 	UVMHIST_LOG(maphist,"(map=0x%x, size=0x%x)", map, size,0,0);
    821 
    822 #ifdef DIAGNOSTIC
    823 	if (vm_map_pmap(map) != pmap_kernel())
    824 		panic("uvm_km_alloc1");
    825 #endif
    826 
    827 	size = round_page(size);
    828 	kva = vm_map_min(map);		/* hint */
    829 
    830 	/*
    831 	 * allocate some virtual space
    832 	 */
    833 
    834 	if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
    835 	      UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    836 			  UVM_ADV_RANDOM, 0)) != KERN_SUCCESS) {
    837 		UVMHIST_LOG(maphist,"<- done (no VM)",0,0,0,0);
    838 		return(0);
    839 	}
    840 
    841 	/*
    842 	 * recover object offset from virtual address
    843 	 */
    844 
    845 	offset = kva - vm_map_min(kernel_map);
    846 	UVMHIST_LOG(maphist,"  kva=0x%x, offset=0x%x", kva, offset,0,0);
    847 
    848 	/*
    849 	 * now allocate the memory.  we must be careful about released pages.
    850 	 */
    851 
    852 	loopva = kva;
    853 	while (size) {
    854 		simple_lock(&uvm.kernel_object->vmobjlock);
    855 		pg = uvm_pagelookup(uvm.kernel_object, offset);
    856 
    857 		/*
    858 		 * if we found a page in an unallocated region, it must be
    859 		 * released
    860 		 */
    861 		if (pg) {
    862 			if ((pg->flags & PG_RELEASED) == 0)
    863 				panic("uvm_km_alloc1: non-released page");
    864 			pg->flags |= PG_WANTED;
    865 			UVM_UNLOCK_AND_WAIT(pg, &uvm.kernel_object->vmobjlock,
    866 			    0, "km_alloc", 0);
    867 			continue;   /* retry */
    868 		}
    869 
    870 		/* allocate ram */
    871 		pg = uvm_pagealloc(uvm.kernel_object, offset, NULL);
    872 		if (pg) {
    873 			pg->flags &= ~PG_BUSY;	/* new page */
    874 			UVM_PAGE_OWN(pg, NULL);
    875 		}
    876 		simple_unlock(&uvm.kernel_object->vmobjlock);
    877 		if (pg == NULL) {
    878 			uvm_wait("km_alloc1w");	/* wait for memory */
    879 			continue;
    880 		}
    881 
    882 		/* map it in */
    883 #if defined(PMAP_NEW)
    884 		pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg), UVM_PROT_ALL);
    885 #else
    886 		pmap_enter(map->pmap, loopva, VM_PAGE_TO_PHYS(pg),
    887 		    UVM_PROT_ALL, TRUE);
    888 #endif
    889 		loopva += PAGE_SIZE;
    890 		offset += PAGE_SIZE;
    891 		size -= PAGE_SIZE;
    892 	}
    893 
    894 	/*
    895 	 * zero on request (note that "size" is now zero due to the above loop
    896 	 * so we need to subtract kva from loopva to reconstruct the size).
    897 	 */
    898 
    899 	if (zeroit)
    900 		bzero((caddr_t)kva, loopva - kva);
    901 
    902 	UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    903 	return(kva);
    904 }
    905 
    906 /*
    907  * uvm_km_valloc: allocate zero-fill memory in the kernel's address space
    908  *
    909  * => memory is not allocated until fault time
    910  */
    911 
    912 vm_offset_t
    913 uvm_km_valloc(map, size)
    914 	vm_map_t map;
    915 	vm_size_t size;
    916 {
    917 	vm_offset_t kva;
    918 	UVMHIST_FUNC("uvm_km_valloc"); UVMHIST_CALLED(maphist);
    919 
    920 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
    921 
    922 #ifdef DIAGNOSTIC
    923 	if (vm_map_pmap(map) != pmap_kernel())
    924 		panic("uvm_km_valloc");
    925 #endif
    926 
    927 	size = round_page(size);
    928 	kva = vm_map_min(map);		/* hint */
    929 
    930 	/*
    931 	 * allocate some virtual space.  will be demand filled by kernel_object.
    932 	 */
    933 
    934 	if (uvm_map(map, &kva, size, uvm.kernel_object, UVM_UNKNOWN_OFFSET,
    935 	    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    936 	    UVM_ADV_RANDOM, 0)) != KERN_SUCCESS) {
    937 		UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0);
    938 		return(0);
    939 	}
    940 
    941 	UVMHIST_LOG(maphist, "<- done (kva=0x%x)", kva,0,0,0);
    942 	return(kva);
    943 }
    944 
    945 /*
    946  * uvm_km_valloc_wait: allocate zero-fill memory in the kernel's address space
    947  *
    948  * => memory is not allocated until fault time
    949  * => if no room in map, wait for space to free, unless requested size
    950  *    is larger than map (in which case we return 0)
    951  */
    952 
    953 vm_offset_t
    954 uvm_km_valloc_wait(map, size)
    955 	vm_map_t map;
    956 	vm_size_t size;
    957 {
    958 	vm_offset_t kva;
    959 	UVMHIST_FUNC("uvm_km_valloc_wait"); UVMHIST_CALLED(maphist);
    960 
    961 	UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x)", map, size, 0,0);
    962 
    963 #ifdef DIAGNOSTIC
    964 	if (vm_map_pmap(map) != pmap_kernel())
    965 		panic("uvm_km_valloc_wait");
    966 #endif
    967 
    968 	size = round_page(size);
    969 	if (size > vm_map_max(map) - vm_map_min(map))
    970 		return(0);
    971 
    972 	while (1) {
    973 		kva = vm_map_min(map);		/* hint */
    974 
    975 		/*
    976 		 * allocate some virtual space.   will be demand filled
    977 		 * by kernel_object.
    978 		 */
    979 
    980 		if (uvm_map(map, &kva, size, uvm.kernel_object,
    981 		    UVM_UNKNOWN_OFFSET, UVM_MAPFLAG(UVM_PROT_ALL,
    982 		    UVM_PROT_ALL, UVM_INH_NONE, UVM_ADV_RANDOM, 0))
    983 		    == KERN_SUCCESS) {
    984 			UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
    985 			return(kva);
    986 		}
    987 
    988 		/*
    989 		 * failed.  sleep for a while (on map)
    990 		 */
    991 
    992 		UVMHIST_LOG(maphist,"<<<sleeping>>>",0,0,0,0);
    993 		tsleep((caddr_t)map, PVM, "vallocwait", 0);
    994 	}
    995 	/*NOTREACHED*/
    996 }
    997