Home | History | Annotate | Line # | Download | only in uvm
uvm_page.c revision 1.37
      1 /*	$NetBSD: uvm_page.c,v 1.37 2000/06/09 04:43:19 soda 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_page.c   8.3 (Berkeley) 3/21/94
     42  * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 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 (at) CS.CMU.EDU
     61  *  School of Computer Science
     62  *  Carnegie Mellon University
     63  *  Pittsburgh PA 15213-3890
     64  *
     65  * any improvements or extensions that they make and grant Carnegie the
     66  * rights to redistribute these changes.
     67  */
     68 
     69 /*
     70  * uvm_page.c: page ops.
     71  */
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/malloc.h>
     76 #include <sys/sched.h>
     77 
     78 #include <vm/vm.h>
     79 #include <vm/vm_page.h>
     80 #include <vm/vm_kern.h>
     81 
     82 #define UVM_PAGE                /* pull in uvm_page.h functions */
     83 #include <uvm/uvm.h>
     84 
     85 /*
     86  * global vars... XXXCDC: move to uvm. structure.
     87  */
     88 
     89 /*
     90  * physical memory config is stored in vm_physmem.
     91  */
     92 
     93 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];	/* XXXCDC: uvm.physmem */
     94 int vm_nphysseg = 0;				/* XXXCDC: uvm.nphysseg */
     95 
     96 /*
     97  * Some supported CPUs in a given architecture don't support all
     98  * of the things necessary to do idle page zero'ing efficiently.
     99  * We therefore provide a way to disable it from machdep code here.
    100  */
    101 
    102 boolean_t vm_page_zero_enable = TRUE;
    103 
    104 /*
    105  * local variables
    106  */
    107 
    108 /*
    109  * these variables record the values returned by vm_page_bootstrap,
    110  * for debugging purposes.  The implementation of uvm_pageboot_alloc
    111  * and pmap_startup here also uses them internally.
    112  */
    113 
    114 static vaddr_t      virtual_space_start;
    115 static vaddr_t      virtual_space_end;
    116 
    117 /*
    118  * we use a hash table with only one bucket during bootup.  we will
    119  * later rehash (resize) the hash table once the allocator is ready.
    120  * we static allocate the one bootstrap bucket below...
    121  */
    122 
    123 static struct pglist uvm_bootbucket;
    124 
    125 /*
    126  * local prototypes
    127  */
    128 
    129 static void uvm_pageinsert __P((struct vm_page *));
    130 
    131 
    132 /*
    133  * inline functions
    134  */
    135 
    136 /*
    137  * uvm_pageinsert: insert a page in the object and the hash table
    138  *
    139  * => caller must lock object
    140  * => caller must lock page queues
    141  * => call should have already set pg's object and offset pointers
    142  *    and bumped the version counter
    143  */
    144 
    145 __inline static void
    146 uvm_pageinsert(pg)
    147 	struct vm_page *pg;
    148 {
    149 	struct pglist *buck;
    150 	int s;
    151 
    152 #ifdef DIAGNOSTIC
    153 	if (pg->flags & PG_TABLED)
    154 		panic("uvm_pageinsert: already inserted");
    155 #endif
    156 
    157 	buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
    158 	s = splimp();
    159 	simple_lock(&uvm.hashlock);
    160 	TAILQ_INSERT_TAIL(buck, pg, hashq);	/* put in hash */
    161 	simple_unlock(&uvm.hashlock);
    162 	splx(s);
    163 
    164 	TAILQ_INSERT_TAIL(&pg->uobject->memq, pg, listq); /* put in object */
    165 	pg->flags |= PG_TABLED;
    166 	pg->uobject->uo_npages++;
    167 
    168 }
    169 
    170 /*
    171  * uvm_page_remove: remove page from object and hash
    172  *
    173  * => caller must lock object
    174  * => caller must lock page queues
    175  */
    176 
    177 void __inline
    178 uvm_pageremove(pg)
    179 	struct vm_page *pg;
    180 {
    181 	struct pglist *buck;
    182 	int s;
    183 
    184 #ifdef DIAGNOSTIC
    185 	if ((pg->flags & (PG_FAULTING)) != 0)
    186 		panic("uvm_pageremove: page is faulting");
    187 #endif
    188 
    189 	if ((pg->flags & PG_TABLED) == 0)
    190 		return;				/* XXX: log */
    191 
    192 	buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
    193 	s = splimp();
    194 	simple_lock(&uvm.hashlock);
    195 	TAILQ_REMOVE(buck, pg, hashq);
    196 	simple_unlock(&uvm.hashlock);
    197 	splx(s);
    198 
    199 	/* object should be locked */
    200 	TAILQ_REMOVE(&pg->uobject->memq, pg, listq);
    201 
    202 	pg->flags &= ~PG_TABLED;
    203 	pg->uobject->uo_npages--;
    204 	pg->uobject = NULL;
    205 	pg->version++;
    206 
    207 }
    208 
    209 /*
    210  * uvm_page_init: init the page system.   called from uvm_init().
    211  *
    212  * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
    213  */
    214 
    215 void
    216 uvm_page_init(kvm_startp, kvm_endp)
    217 	vaddr_t *kvm_startp, *kvm_endp;
    218 {
    219 	vsize_t freepages, pagecount, n;
    220 	vm_page_t pagearray;
    221 	int lcv, i;
    222 	paddr_t paddr;
    223 
    224 
    225 	/*
    226 	 * step 1: init the page queues and page queue locks
    227 	 */
    228 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    229 		for (i = 0; i < PGFL_NQUEUES; i++)
    230 			TAILQ_INIT(&uvm.page_free[lcv].pgfl_queues[i]);
    231 	}
    232 	TAILQ_INIT(&uvm.page_active);
    233 	TAILQ_INIT(&uvm.page_inactive_swp);
    234 	TAILQ_INIT(&uvm.page_inactive_obj);
    235 	simple_lock_init(&uvm.pageqlock);
    236 	simple_lock_init(&uvm.fpageqlock);
    237 
    238 	/*
    239 	 * step 2: init the <obj,offset> => <page> hash table. for now
    240 	 * we just have one bucket (the bootstrap bucket).   later on we
    241 	 * will allocate new buckets as we dynamically resize the hash table.
    242 	 */
    243 
    244 	uvm.page_nhash = 1;			/* 1 bucket */
    245 	uvm.page_hashmask = 0;		/* mask for hash function */
    246 	uvm.page_hash = &uvm_bootbucket;	/* install bootstrap bucket */
    247 	TAILQ_INIT(uvm.page_hash);		/* init hash table */
    248 	simple_lock_init(&uvm.hashlock);	/* init hash table lock */
    249 
    250 	/*
    251 	 * step 3: allocate vm_page structures.
    252 	 */
    253 
    254 	/*
    255 	 * sanity check:
    256 	 * before calling this function the MD code is expected to register
    257 	 * some free RAM with the uvm_page_physload() function.   our job
    258 	 * now is to allocate vm_page structures for this memory.
    259 	 */
    260 
    261 	if (vm_nphysseg == 0)
    262 		panic("vm_page_bootstrap: no memory pre-allocated");
    263 
    264 	/*
    265 	 * first calculate the number of free pages...
    266 	 *
    267 	 * note that we use start/end rather than avail_start/avail_end.
    268 	 * this allows us to allocate extra vm_page structures in case we
    269 	 * want to return some memory to the pool after booting.
    270 	 */
    271 
    272 	freepages = 0;
    273 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    274 		freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
    275 
    276 	/*
    277 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
    278 	 * use.   for each page of memory we use we need a vm_page structure.
    279 	 * thus, the total number of pages we can use is the total size of
    280 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
    281 	 * structure.   we add one to freepages as a fudge factor to avoid
    282 	 * truncation errors (since we can only allocate in terms of whole
    283 	 * pages).
    284 	 */
    285 
    286 	pagecount = ((freepages + 1) << PAGE_SHIFT) /
    287 	    (PAGE_SIZE + sizeof(struct vm_page));
    288 	pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount *
    289 	    sizeof(struct vm_page));
    290 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
    291 
    292 	/*
    293 	 * step 4: init the vm_page structures and put them in the correct
    294 	 * place...
    295 	 */
    296 
    297 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    298 
    299 		n = vm_physmem[lcv].end - vm_physmem[lcv].start;
    300 		if (n > pagecount) {
    301 			printf("uvm_page_init: lost %ld page(s) in init\n",
    302 			    (long)(n - pagecount));
    303 			panic("uvm_page_init");  /* XXXCDC: shouldn't happen? */
    304 			/* n = pagecount; */
    305 		}
    306 		/* set up page array pointers */
    307 		vm_physmem[lcv].pgs = pagearray;
    308 		pagearray += n;
    309 		pagecount -= n;
    310 		vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
    311 
    312 		/* init and free vm_pages (we've already zeroed them) */
    313 		paddr = ptoa(vm_physmem[lcv].start);
    314 		for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
    315 			vm_physmem[lcv].pgs[i].phys_addr = paddr;
    316 			if (atop(paddr) >= vm_physmem[lcv].avail_start &&
    317 			    atop(paddr) <= vm_physmem[lcv].avail_end) {
    318 				uvmexp.npages++;
    319 				/* add page to free pool */
    320 				uvm_pagefree(&vm_physmem[lcv].pgs[i]);
    321 			}
    322 		}
    323 	}
    324 	/*
    325 	 * step 5: pass up the values of virtual_space_start and
    326 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
    327 	 * layers of the VM.
    328 	 */
    329 
    330 	*kvm_startp = round_page(virtual_space_start);
    331 	*kvm_endp = trunc_page(virtual_space_end);
    332 
    333 	/*
    334 	 * step 6: init pagedaemon lock
    335 	 */
    336 
    337 	simple_lock_init(&uvm.pagedaemon_lock);
    338 
    339 	/*
    340 	 * step 7: init reserve thresholds
    341 	 * XXXCDC - values may need adjusting
    342 	 */
    343 	uvmexp.reserve_pagedaemon = 1;
    344 	uvmexp.reserve_kernel = 5;
    345 
    346 	/*
    347 	 * step 8: determine if we should zero pages in the idle
    348 	 * loop.
    349 	 *
    350 	 * XXXJRT - might consider zero'ing up to the target *now*,
    351 	 *	    but that could take an awfully long time if you
    352 	 *	    have a lot of memory.
    353 	 */
    354 	uvm.page_idle_zero = vm_page_zero_enable;
    355 
    356 	/*
    357 	 * done!
    358 	 */
    359 
    360 	uvm.page_init_done = TRUE;
    361 }
    362 
    363 /*
    364  * uvm_setpagesize: set the page size
    365  *
    366  * => sets page_shift and page_mask from uvmexp.pagesize.
    367  * => XXXCDC: move global vars.
    368  */
    369 
    370 void
    371 uvm_setpagesize()
    372 {
    373 	if (uvmexp.pagesize == 0)
    374 		uvmexp.pagesize = DEFAULT_PAGE_SIZE;
    375 	uvmexp.pagemask = uvmexp.pagesize - 1;
    376 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
    377 		panic("uvm_setpagesize: page size not a power of two");
    378 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
    379 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
    380 			break;
    381 }
    382 
    383 /*
    384  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
    385  */
    386 
    387 vaddr_t
    388 uvm_pageboot_alloc(size)
    389 	vsize_t size;
    390 {
    391 #if defined(PMAP_STEAL_MEMORY)
    392 	vaddr_t addr;
    393 
    394 	/*
    395 	 * defer bootstrap allocation to MD code (it may want to allocate
    396 	 * from a direct-mapped segment).  pmap_steal_memory should round
    397 	 * off virtual_space_start/virtual_space_end.
    398 	 */
    399 
    400 	addr = pmap_steal_memory(size, &virtual_space_start,
    401 	    &virtual_space_end);
    402 
    403 	return(addr);
    404 
    405 #else /* !PMAP_STEAL_MEMORY */
    406 
    407 	static boolean_t initialized = FALSE;
    408 	vaddr_t addr, vaddr;
    409 	paddr_t paddr;
    410 
    411 	/* round to page size */
    412 	size = round_page(size);
    413 
    414 	/*
    415 	 * on first call to this function, initialize ourselves.
    416 	 */
    417 	if (initialized == FALSE) {
    418 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
    419 
    420 		/* round it the way we like it */
    421 		virtual_space_start = round_page(virtual_space_start);
    422 		virtual_space_end = trunc_page(virtual_space_end);
    423 
    424 		initialized = TRUE;
    425 	}
    426 
    427 	/*
    428 	 * allocate virtual memory for this request
    429 	 */
    430 	if (virtual_space_start == virtual_space_end ||
    431 	    (virtual_space_end - virtual_space_start) < size)
    432 		panic("uvm_pageboot_alloc: out of virtual space");
    433 
    434 	addr = virtual_space_start;
    435 
    436 #ifdef PMAP_GROWKERNEL
    437 	/*
    438 	 * If the kernel pmap can't map the requested space,
    439 	 * then allocate more resources for it.
    440 	 */
    441 	if (uvm_maxkaddr < (addr + size)) {
    442 		uvm_maxkaddr = pmap_growkernel(addr + size);
    443 		if (uvm_maxkaddr < (addr + size))
    444 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
    445 	}
    446 #endif
    447 
    448 	virtual_space_start += size;
    449 
    450 	/*
    451 	 * allocate and mapin physical pages to back new virtual pages
    452 	 */
    453 
    454 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
    455 	    vaddr += PAGE_SIZE) {
    456 
    457 		if (!uvm_page_physget(&paddr))
    458 			panic("uvm_pageboot_alloc: out of memory");
    459 
    460 		/*
    461 		 * Note this memory is no longer managed, so using
    462 		 * pmap_kenter is safe.
    463 		 */
    464 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
    465 	}
    466 	return(addr);
    467 #endif	/* PMAP_STEAL_MEMORY */
    468 }
    469 
    470 #if !defined(PMAP_STEAL_MEMORY)
    471 /*
    472  * uvm_page_physget: "steal" one page from the vm_physmem structure.
    473  *
    474  * => attempt to allocate it off the end of a segment in which the "avail"
    475  *    values match the start/end values.   if we can't do that, then we
    476  *    will advance both values (making them equal, and removing some
    477  *    vm_page structures from the non-avail area).
    478  * => return false if out of memory.
    479  */
    480 
    481 /* subroutine: try to allocate from memory chunks on the specified freelist */
    482 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int));
    483 
    484 static boolean_t
    485 uvm_page_physget_freelist(paddrp, freelist)
    486 	paddr_t *paddrp;
    487 	int freelist;
    488 {
    489 	int lcv, x;
    490 
    491 	/* pass 1: try allocating from a matching end */
    492 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    493 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    494 #else
    495 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    496 #endif
    497 	{
    498 
    499 		if (uvm.page_init_done == TRUE)
    500 			panic("vm_page_physget: called _after_ bootstrap");
    501 
    502 		if (vm_physmem[lcv].free_list != freelist)
    503 			continue;
    504 
    505 		/* try from front */
    506 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
    507 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    508 			*paddrp = ptoa(vm_physmem[lcv].avail_start);
    509 			vm_physmem[lcv].avail_start++;
    510 			vm_physmem[lcv].start++;
    511 			/* nothing left?   nuke it */
    512 			if (vm_physmem[lcv].avail_start ==
    513 			    vm_physmem[lcv].end) {
    514 				if (vm_nphysseg == 1)
    515 				    panic("vm_page_physget: out of memory!");
    516 				vm_nphysseg--;
    517 				for (x = lcv ; x < vm_nphysseg ; x++)
    518 					/* structure copy */
    519 					vm_physmem[x] = vm_physmem[x+1];
    520 			}
    521 			return (TRUE);
    522 		}
    523 
    524 		/* try from rear */
    525 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
    526 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    527 			*paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
    528 			vm_physmem[lcv].avail_end--;
    529 			vm_physmem[lcv].end--;
    530 			/* nothing left?   nuke it */
    531 			if (vm_physmem[lcv].avail_end ==
    532 			    vm_physmem[lcv].start) {
    533 				if (vm_nphysseg == 1)
    534 				    panic("vm_page_physget: out of memory!");
    535 				vm_nphysseg--;
    536 				for (x = lcv ; x < vm_nphysseg ; x++)
    537 					/* structure copy */
    538 					vm_physmem[x] = vm_physmem[x+1];
    539 			}
    540 			return (TRUE);
    541 		}
    542 	}
    543 
    544 	/* pass2: forget about matching ends, just allocate something */
    545 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    546 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    547 #else
    548 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    549 #endif
    550 	{
    551 
    552 		/* any room in this bank? */
    553 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
    554 			continue;  /* nope */
    555 
    556 		*paddrp = ptoa(vm_physmem[lcv].avail_start);
    557 		vm_physmem[lcv].avail_start++;
    558 		/* truncate! */
    559 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
    560 
    561 		/* nothing left?   nuke it */
    562 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
    563 			if (vm_nphysseg == 1)
    564 				panic("vm_page_physget: out of memory!");
    565 			vm_nphysseg--;
    566 			for (x = lcv ; x < vm_nphysseg ; x++)
    567 				/* structure copy */
    568 				vm_physmem[x] = vm_physmem[x+1];
    569 		}
    570 		return (TRUE);
    571 	}
    572 
    573 	return (FALSE);        /* whoops! */
    574 }
    575 
    576 boolean_t
    577 uvm_page_physget(paddrp)
    578 	paddr_t *paddrp;
    579 {
    580 	int i;
    581 
    582 	/* try in the order of freelist preference */
    583 	for (i = 0; i < VM_NFREELIST; i++)
    584 		if (uvm_page_physget_freelist(paddrp, i) == TRUE)
    585 			return (TRUE);
    586 	return (FALSE);
    587 }
    588 #endif /* PMAP_STEAL_MEMORY */
    589 
    590 /*
    591  * uvm_page_physload: load physical memory into VM system
    592  *
    593  * => all args are PFs
    594  * => all pages in start/end get vm_page structures
    595  * => areas marked by avail_start/avail_end get added to the free page pool
    596  * => we are limited to VM_PHYSSEG_MAX physical memory segments
    597  */
    598 
    599 void
    600 uvm_page_physload(start, end, avail_start, avail_end, free_list)
    601 	paddr_t start, end, avail_start, avail_end;
    602 	int free_list;
    603 {
    604 	int preload, lcv;
    605 	psize_t npages;
    606 	struct vm_page *pgs;
    607 	struct vm_physseg *ps;
    608 
    609 	if (uvmexp.pagesize == 0)
    610 		panic("vm_page_physload: page size not set!");
    611 
    612 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
    613 		panic("uvm_page_physload: bad free list %d\n", free_list);
    614 
    615 	if (start >= end)
    616 		panic("uvm_page_physload: start >= end");
    617 
    618 	/*
    619 	 * do we have room?
    620 	 */
    621 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
    622 		printf("vm_page_physload: unable to load physical memory "
    623 		    "segment\n");
    624 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
    625 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
    626 		return;
    627 	}
    628 
    629 	/*
    630 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
    631 	 * called yet, so malloc is not available).
    632 	 */
    633 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    634 		if (vm_physmem[lcv].pgs)
    635 			break;
    636 	}
    637 	preload = (lcv == vm_nphysseg);
    638 
    639 	/*
    640 	 * if VM is already running, attempt to malloc() vm_page structures
    641 	 */
    642 	if (!preload) {
    643 #if defined(VM_PHYSSEG_NOADD)
    644 		panic("vm_page_physload: tried to add RAM after vm_mem_init");
    645 #else
    646 		/* XXXCDC: need some sort of lockout for this case */
    647 		paddr_t paddr;
    648 		npages = end - start;  /* # of pages */
    649 		MALLOC(pgs, struct vm_page *, sizeof(struct vm_page) * npages,
    650 					 M_VMPAGE, M_NOWAIT);
    651 		if (pgs == NULL) {
    652 			printf("vm_page_physload: can not malloc vm_page "
    653 			    "structs for segment\n");
    654 			printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
    655 			return;
    656 		}
    657 		/* zero data, init phys_addr and free_list, and free pages */
    658 		memset(pgs, 0, sizeof(struct vm_page) * npages);
    659 		for (lcv = 0, paddr = ptoa(start) ;
    660 				 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
    661 			pgs[lcv].phys_addr = paddr;
    662 			pgs[lcv].free_list = free_list;
    663 			if (atop(paddr) >= avail_start &&
    664 			    atop(paddr) <= avail_end)
    665 				uvm_pagefree(&pgs[lcv]);
    666 		}
    667 		/* XXXCDC: incomplete: need to update uvmexp.free, what else? */
    668 		/* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
    669 #endif
    670 	} else {
    671 
    672 		/* gcc complains if these don't get init'd */
    673 		pgs = NULL;
    674 		npages = 0;
    675 
    676 	}
    677 
    678 	/*
    679 	 * now insert us in the proper place in vm_physmem[]
    680 	 */
    681 
    682 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
    683 
    684 	/* random: put it at the end (easy!) */
    685 	ps = &vm_physmem[vm_nphysseg];
    686 
    687 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
    688 
    689 	{
    690 		int x;
    691 		/* sort by address for binary search */
    692 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    693 			if (start < vm_physmem[lcv].start)
    694 				break;
    695 		ps = &vm_physmem[lcv];
    696 		/* move back other entries, if necessary ... */
    697 		for (x = vm_nphysseg ; x > lcv ; x--)
    698 			/* structure copy */
    699 			vm_physmem[x] = vm_physmem[x - 1];
    700 	}
    701 
    702 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    703 
    704 	{
    705 		int x;
    706 		/* sort by largest segment first */
    707 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    708 			if ((end - start) >
    709 			    (vm_physmem[lcv].end - vm_physmem[lcv].start))
    710 				break;
    711 		ps = &vm_physmem[lcv];
    712 		/* move back other entries, if necessary ... */
    713 		for (x = vm_nphysseg ; x > lcv ; x--)
    714 			/* structure copy */
    715 			vm_physmem[x] = vm_physmem[x - 1];
    716 	}
    717 
    718 #else
    719 
    720 	panic("vm_page_physload: unknown physseg strategy selected!");
    721 
    722 #endif
    723 
    724 	ps->start = start;
    725 	ps->end = end;
    726 	ps->avail_start = avail_start;
    727 	ps->avail_end = avail_end;
    728 	if (preload) {
    729 		ps->pgs = NULL;
    730 	} else {
    731 		ps->pgs = pgs;
    732 		ps->lastpg = pgs + npages - 1;
    733 	}
    734 	ps->free_list = free_list;
    735 	vm_nphysseg++;
    736 
    737 	/*
    738 	 * done!
    739 	 */
    740 
    741 	if (!preload)
    742 		uvm_page_rehash();
    743 
    744 	return;
    745 }
    746 
    747 /*
    748  * uvm_page_rehash: reallocate hash table based on number of free pages.
    749  */
    750 
    751 void
    752 uvm_page_rehash()
    753 {
    754 	int freepages, lcv, bucketcount, s, oldcount;
    755 	struct pglist *newbuckets, *oldbuckets;
    756 	struct vm_page *pg;
    757 	size_t newsize, oldsize;
    758 
    759 	/*
    760 	 * compute number of pages that can go in the free pool
    761 	 */
    762 
    763 	freepages = 0;
    764 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    765 		freepages +=
    766 		    (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
    767 
    768 	/*
    769 	 * compute number of buckets needed for this number of pages
    770 	 */
    771 
    772 	bucketcount = 1;
    773 	while (bucketcount < freepages)
    774 		bucketcount = bucketcount * 2;
    775 
    776 	/*
    777 	 * compute the size of the current table and new table.
    778 	 */
    779 
    780 	oldbuckets = uvm.page_hash;
    781 	oldcount = uvm.page_nhash;
    782 	oldsize = round_page(sizeof(struct pglist) * oldcount);
    783 	newsize = round_page(sizeof(struct pglist) * bucketcount);
    784 
    785 	/*
    786 	 * allocate the new buckets
    787 	 */
    788 
    789 	newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize);
    790 	if (newbuckets == NULL) {
    791 		printf("uvm_page_physrehash: WARNING: could not grow page "
    792 		    "hash table\n");
    793 		return;
    794 	}
    795 	for (lcv = 0 ; lcv < bucketcount ; lcv++)
    796 		TAILQ_INIT(&newbuckets[lcv]);
    797 
    798 	/*
    799 	 * now replace the old buckets with the new ones and rehash everything
    800 	 */
    801 
    802 	s = splimp();
    803 	simple_lock(&uvm.hashlock);
    804 	uvm.page_hash = newbuckets;
    805 	uvm.page_nhash = bucketcount;
    806 	uvm.page_hashmask = bucketcount - 1;  /* power of 2 */
    807 
    808 	/* ... and rehash */
    809 	for (lcv = 0 ; lcv < oldcount ; lcv++) {
    810 		while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
    811 			TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
    812 			TAILQ_INSERT_TAIL(
    813 			  &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
    814 			  pg, hashq);
    815 		}
    816 	}
    817 	simple_unlock(&uvm.hashlock);
    818 	splx(s);
    819 
    820 	/*
    821 	 * free old bucket array if is not the boot-time table
    822 	 */
    823 
    824 	if (oldbuckets != &uvm_bootbucket)
    825 		uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize);
    826 
    827 	/*
    828 	 * done
    829 	 */
    830 	return;
    831 }
    832 
    833 
    834 #if 1 /* XXXCDC: TMP TMP TMP DEBUG DEBUG DEBUG */
    835 
    836 void uvm_page_physdump __P((void)); /* SHUT UP GCC */
    837 
    838 /* call from DDB */
    839 void
    840 uvm_page_physdump()
    841 {
    842 	int lcv;
    843 
    844 	printf("rehash: physical memory config [segs=%d of %d]:\n",
    845 				 vm_nphysseg, VM_PHYSSEG_MAX);
    846 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    847 		printf("0x%llx->0x%llx [0x%llx->0x%llx]\n",
    848 		    (long long)vm_physmem[lcv].start,
    849 		    (long long)vm_physmem[lcv].end,
    850 		    (long long)vm_physmem[lcv].avail_start,
    851 		    (long long)vm_physmem[lcv].avail_end);
    852 	printf("STRATEGY = ");
    853 	switch (VM_PHYSSEG_STRAT) {
    854 	case VM_PSTRAT_RANDOM: printf("RANDOM\n"); break;
    855 	case VM_PSTRAT_BSEARCH: printf("BSEARCH\n"); break;
    856 	case VM_PSTRAT_BIGFIRST: printf("BIGFIRST\n"); break;
    857 	default: printf("<<UNKNOWN>>!!!!\n");
    858 	}
    859 	printf("number of buckets = %d\n", uvm.page_nhash);
    860 }
    861 #endif
    862 
    863 /*
    864  * uvm_pagealloc_strat: allocate vm_page from a particular free list.
    865  *
    866  * => return null if no pages free
    867  * => wake up pagedaemon if number of free pages drops below low water mark
    868  * => if obj != NULL, obj must be locked (to put in hash)
    869  * => if anon != NULL, anon must be locked (to put in anon)
    870  * => only one of obj or anon can be non-null
    871  * => caller must activate/deactivate page if it is not wired.
    872  * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
    873  * => policy decision: it is more important to pull a page off of the
    874  *	appropriate priority free list than it is to get a zero'd or
    875  *	unknown contents page.  This is because we live with the
    876  *	consequences of a bad free list decision for the entire
    877  *	lifetime of the page, e.g. if the page comes from memory that
    878  *	is slower to access.
    879  */
    880 
    881 struct vm_page *
    882 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list)
    883 	struct uvm_object *obj;
    884 	voff_t off;
    885 	int flags;
    886 	struct vm_anon *anon;
    887 	int strat, free_list;
    888 {
    889 	int lcv, try1, try2, s, zeroit = 0;
    890 	struct vm_page *pg;
    891 	struct pglist *freeq;
    892 	struct pgfreelist *pgfl;
    893 	boolean_t use_reserve;
    894 
    895 #ifdef DIAGNOSTIC
    896 	/* sanity check */
    897 	if (obj && anon)
    898 		panic("uvm_pagealloc: obj and anon != NULL");
    899 #endif
    900 
    901 	s = uvm_lock_fpageq();		/* lock free page queue */
    902 
    903 	/*
    904 	 * check to see if we need to generate some free pages waking
    905 	 * the pagedaemon.
    906 	 */
    907 
    908 	if (uvmexp.free < uvmexp.freemin || (uvmexp.free < uvmexp.freetarg &&
    909 	    uvmexp.inactive < uvmexp.inactarg))
    910 		wakeup(&uvm.pagedaemon);
    911 
    912 	/*
    913 	 * fail if any of these conditions is true:
    914 	 * [1]  there really are no free pages, or
    915 	 * [2]  only kernel "reserved" pages remain and
    916 	 *        the page isn't being allocated to a kernel object.
    917 	 * [3]  only pagedaemon "reserved" pages remain and
    918 	 *        the requestor isn't the pagedaemon.
    919 	 */
    920 
    921 	use_reserve = (flags & UVM_PGA_USERESERVE) ||
    922 		(obj && UVM_OBJ_IS_KERN_OBJECT(obj));
    923 	if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
    924 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
    925 	     !(use_reserve && curproc == uvm.pagedaemon_proc)))
    926 		goto fail;
    927 
    928 #if PGFL_NQUEUES != 2
    929 #error uvm_pagealloc_strat needs to be updated
    930 #endif
    931 
    932 	/*
    933 	 * If we want a zero'd page, try the ZEROS queue first, otherwise
    934 	 * we try the UNKNOWN queue first.
    935 	 */
    936 	if (flags & UVM_PGA_ZERO) {
    937 		try1 = PGFL_ZEROS;
    938 		try2 = PGFL_UNKNOWN;
    939 	} else {
    940 		try1 = PGFL_UNKNOWN;
    941 		try2 = PGFL_ZEROS;
    942 	}
    943 
    944  again:
    945 	switch (strat) {
    946 	case UVM_PGA_STRAT_NORMAL:
    947 		/* Check all freelists in descending priority order. */
    948 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    949 			pgfl = &uvm.page_free[lcv];
    950 			if ((pg = TAILQ_FIRST((freeq =
    951 			      &pgfl->pgfl_queues[try1]))) != NULL ||
    952 			    (pg = TAILQ_FIRST((freeq =
    953 			      &pgfl->pgfl_queues[try2]))) != NULL)
    954 				goto gotit;
    955 		}
    956 
    957 		/* No pages free! */
    958 		goto fail;
    959 
    960 	case UVM_PGA_STRAT_ONLY:
    961 	case UVM_PGA_STRAT_FALLBACK:
    962 		/* Attempt to allocate from the specified free list. */
    963 #ifdef DIAGNOSTIC
    964 		if (free_list >= VM_NFREELIST || free_list < 0)
    965 			panic("uvm_pagealloc_strat: bad free list %d",
    966 			    free_list);
    967 #endif
    968 		pgfl = &uvm.page_free[free_list];
    969 		if ((pg = TAILQ_FIRST((freeq =
    970 		      &pgfl->pgfl_queues[try1]))) != NULL ||
    971 		    (pg = TAILQ_FIRST((freeq =
    972 		      &pgfl->pgfl_queues[try2]))) != NULL)
    973 			goto gotit;
    974 
    975 		/* Fall back, if possible. */
    976 		if (strat == UVM_PGA_STRAT_FALLBACK) {
    977 			strat = UVM_PGA_STRAT_NORMAL;
    978 			goto again;
    979 		}
    980 
    981 		/* No pages free! */
    982 		goto fail;
    983 
    984 	default:
    985 		panic("uvm_pagealloc_strat: bad strat %d", strat);
    986 		/* NOTREACHED */
    987 	}
    988 
    989  gotit:
    990 	TAILQ_REMOVE(freeq, pg, pageq);
    991 	uvmexp.free--;
    992 
    993 	/* update zero'd page count */
    994 	if (pg->flags & PG_ZERO)
    995 		uvmexp.zeropages--;
    996 
    997 	/*
    998 	 * update allocation statistics and remember if we have to
    999 	 * zero the page
   1000 	 */
   1001 	if (flags & UVM_PGA_ZERO) {
   1002 		if (pg->flags & PG_ZERO) {
   1003 			uvmexp.pga_zerohit++;
   1004 			zeroit = 0;
   1005 		} else {
   1006 			uvmexp.pga_zeromiss++;
   1007 			zeroit = 1;
   1008 		}
   1009 	}
   1010 
   1011 	uvm_unlock_fpageq(s);		/* unlock free page queue */
   1012 
   1013 	pg->offset = off;
   1014 	pg->uobject = obj;
   1015 	pg->uanon = anon;
   1016 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
   1017 	pg->version++;
   1018 	pg->wire_count = 0;
   1019 	pg->loan_count = 0;
   1020 	if (anon) {
   1021 		anon->u.an_page = pg;
   1022 		pg->pqflags = PQ_ANON;
   1023 	} else {
   1024 		if (obj)
   1025 			uvm_pageinsert(pg);
   1026 		pg->pqflags = 0;
   1027 	}
   1028 #if defined(UVM_PAGE_TRKOWN)
   1029 	pg->owner_tag = NULL;
   1030 #endif
   1031 	UVM_PAGE_OWN(pg, "new alloc");
   1032 
   1033 	if (flags & UVM_PGA_ZERO) {
   1034 		/*
   1035 		 * A zero'd page is not clean.  If we got a page not already
   1036 		 * zero'd, then we have to zero it ourselves.
   1037 		 */
   1038 		pg->flags &= ~PG_CLEAN;
   1039 		if (zeroit)
   1040 			pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1041 	}
   1042 
   1043 	return(pg);
   1044 
   1045  fail:
   1046 	uvm_unlock_fpageq(s);
   1047 	return (NULL);
   1048 }
   1049 
   1050 /*
   1051  * uvm_pagerealloc: reallocate a page from one object to another
   1052  *
   1053  * => both objects must be locked
   1054  */
   1055 
   1056 void
   1057 uvm_pagerealloc(pg, newobj, newoff)
   1058 	struct vm_page *pg;
   1059 	struct uvm_object *newobj;
   1060 	voff_t newoff;
   1061 {
   1062 	/*
   1063 	 * remove it from the old object
   1064 	 */
   1065 
   1066 	if (pg->uobject) {
   1067 		uvm_pageremove(pg);
   1068 	}
   1069 
   1070 	/*
   1071 	 * put it in the new object
   1072 	 */
   1073 
   1074 	if (newobj) {
   1075 		pg->uobject = newobj;
   1076 		pg->offset = newoff;
   1077 		pg->version++;
   1078 		uvm_pageinsert(pg);
   1079 	}
   1080 
   1081 	return;
   1082 }
   1083 
   1084 
   1085 /*
   1086  * uvm_pagefree: free page
   1087  *
   1088  * => erase page's identity (i.e. remove from hash/object)
   1089  * => put page on free list
   1090  * => caller must lock owning object (either anon or uvm_object)
   1091  * => caller must lock page queues
   1092  * => assumes all valid mappings of pg are gone
   1093  */
   1094 
   1095 void uvm_pagefree(pg)
   1096 
   1097 struct vm_page *pg;
   1098 
   1099 {
   1100 	int s;
   1101 	int saved_loan_count = pg->loan_count;
   1102 
   1103 	/*
   1104 	 * if the page was an object page (and thus "TABLED"), remove it
   1105 	 * from the object.
   1106 	 */
   1107 
   1108 	if (pg->flags & PG_TABLED) {
   1109 
   1110 		/*
   1111 		 * if the object page is on loan we are going to drop ownership.
   1112 		 * it is possible that an anon will take over as owner for this
   1113 		 * page later on.   the anon will want a !PG_CLEAN page so that
   1114 		 * it knows it needs to allocate swap if it wants to page the
   1115 		 * page out.
   1116 		 */
   1117 
   1118 		if (saved_loan_count)
   1119 			pg->flags &= ~PG_CLEAN;	/* in case an anon takes over */
   1120 
   1121 		uvm_pageremove(pg);
   1122 
   1123 		/*
   1124 		 * if our page was on loan, then we just lost control over it
   1125 		 * (in fact, if it was loaned to an anon, the anon may have
   1126 		 * already taken over ownership of the page by now and thus
   1127 		 * changed the loan_count [e.g. in uvmfault_anonget()]) we just
   1128 		 * return (when the last loan is dropped, then the page can be
   1129 		 * freed by whatever was holding the last loan).
   1130 		 */
   1131 		if (saved_loan_count)
   1132 			return;
   1133 
   1134 	} else if (saved_loan_count && (pg->pqflags & PQ_ANON)) {
   1135 
   1136 		/*
   1137 		 * if our page is owned by an anon and is loaned out to the
   1138 		 * kernel then we just want to drop ownership and return.
   1139 		 * the kernel must free the page when all its loans clear ...
   1140 		 * note that the kernel can't change the loan status of our
   1141 		 * page as long as we are holding PQ lock.
   1142 		 */
   1143 		pg->pqflags &= ~PQ_ANON;
   1144 		pg->uanon = NULL;
   1145 		return;
   1146 	}
   1147 
   1148 #ifdef DIAGNOSTIC
   1149 	if (saved_loan_count) {
   1150 		printf("uvm_pagefree: warning: freeing page with a loan "
   1151 		    "count of %d\n", saved_loan_count);
   1152 		panic("uvm_pagefree: loan count");
   1153 	}
   1154 #endif
   1155 
   1156 
   1157 	/*
   1158 	 * now remove the page from the queues
   1159 	 */
   1160 
   1161 	if (pg->pqflags & PQ_ACTIVE) {
   1162 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
   1163 		pg->pqflags &= ~PQ_ACTIVE;
   1164 		uvmexp.active--;
   1165 	}
   1166 	if (pg->pqflags & PQ_INACTIVE) {
   1167 		if (pg->pqflags & PQ_SWAPBACKED)
   1168 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
   1169 		else
   1170 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
   1171 		pg->pqflags &= ~PQ_INACTIVE;
   1172 		uvmexp.inactive--;
   1173 	}
   1174 
   1175 	/*
   1176 	 * if the page was wired, unwire it now.
   1177 	 */
   1178 	if (pg->wire_count) {
   1179 		pg->wire_count = 0;
   1180 		uvmexp.wired--;
   1181 	}
   1182 
   1183 	/*
   1184 	 * and put on free queue
   1185 	 */
   1186 
   1187 	pg->flags &= ~PG_ZERO;
   1188 
   1189 	s = uvm_lock_fpageq();
   1190 	TAILQ_INSERT_TAIL(&uvm.page_free[
   1191 	    uvm_page_lookup_freelist(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq);
   1192 	pg->pqflags = PQ_FREE;
   1193 #ifdef DEBUG
   1194 	pg->uobject = (void *)0xdeadbeef;
   1195 	pg->offset = 0xdeadbeef;
   1196 	pg->uanon = (void *)0xdeadbeef;
   1197 #endif
   1198 	uvmexp.free++;
   1199 
   1200 	if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
   1201 		uvm.page_idle_zero = vm_page_zero_enable;
   1202 
   1203 	uvm_unlock_fpageq(s);
   1204 }
   1205 
   1206 #if defined(UVM_PAGE_TRKOWN)
   1207 /*
   1208  * uvm_page_own: set or release page ownership
   1209  *
   1210  * => this is a debugging function that keeps track of who sets PG_BUSY
   1211  *	and where they do it.   it can be used to track down problems
   1212  *	such a process setting "PG_BUSY" and never releasing it.
   1213  * => page's object [if any] must be locked
   1214  * => if "tag" is NULL then we are releasing page ownership
   1215  */
   1216 void
   1217 uvm_page_own(pg, tag)
   1218 	struct vm_page *pg;
   1219 	char *tag;
   1220 {
   1221 	/* gain ownership? */
   1222 	if (tag) {
   1223 		if (pg->owner_tag) {
   1224 			printf("uvm_page_own: page %p already owned "
   1225 			    "by proc %d [%s]\n", pg,
   1226 			     pg->owner, pg->owner_tag);
   1227 			panic("uvm_page_own");
   1228 		}
   1229 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
   1230 		pg->owner_tag = tag;
   1231 		return;
   1232 	}
   1233 
   1234 	/* drop ownership */
   1235 	if (pg->owner_tag == NULL) {
   1236 		printf("uvm_page_own: dropping ownership of an non-owned "
   1237 		    "page (%p)\n", pg);
   1238 		panic("uvm_page_own");
   1239 	}
   1240 	pg->owner_tag = NULL;
   1241 	return;
   1242 }
   1243 #endif
   1244 
   1245 /*
   1246  * uvm_pageidlezero: zero free pages while the system is idle.
   1247  *
   1248  * => we do at least one iteration per call, if we are below the target.
   1249  * => we loop until we either reach the target or whichqs indicates that
   1250  *	there is a process ready to run.
   1251  */
   1252 void
   1253 uvm_pageidlezero()
   1254 {
   1255 	struct vm_page *pg;
   1256 	struct pgfreelist *pgfl;
   1257 	int free_list, s;
   1258 
   1259 	do {
   1260 		s = uvm_lock_fpageq();
   1261 
   1262 		if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
   1263 			uvm.page_idle_zero = FALSE;
   1264 			uvm_unlock_fpageq(s);
   1265 			return;
   1266 		}
   1267 
   1268 		for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
   1269 			pgfl = &uvm.page_free[free_list];
   1270 			if ((pg = TAILQ_FIRST(&pgfl->pgfl_queues[
   1271 			    PGFL_UNKNOWN])) != NULL)
   1272 				break;
   1273 		}
   1274 
   1275 		if (pg == NULL) {
   1276 			/*
   1277 			 * No non-zero'd pages; don't bother trying again
   1278 			 * until we know we have non-zero'd pages free.
   1279 			 */
   1280 			uvm.page_idle_zero = FALSE;
   1281 			uvm_unlock_fpageq(s);
   1282 			return;
   1283 		}
   1284 
   1285 		TAILQ_REMOVE(&pgfl->pgfl_queues[PGFL_UNKNOWN], pg, pageq);
   1286 		uvmexp.free--;
   1287 		uvm_unlock_fpageq(s);
   1288 
   1289 #ifdef PMAP_PAGEIDLEZERO
   1290 		PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg));
   1291 #else
   1292 		/*
   1293 		 * XXX This will toast the cache unless the pmap_zero_page()
   1294 		 * XXX implementation does uncached access.
   1295 		 */
   1296 		pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1297 #endif
   1298 		pg->flags |= PG_ZERO;
   1299 
   1300 		s = uvm_lock_fpageq();
   1301 		TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_ZEROS], pg, pageq);
   1302 		uvmexp.free++;
   1303 		uvmexp.zeropages++;
   1304 		uvm_unlock_fpageq(s);
   1305 	} while (sched_whichqs == 0);
   1306 }
   1307