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