Home | History | Annotate | Line # | Download | only in uvm
uvm_page.c revision 1.51
      1 /*	$NetBSD: uvm_page.c,v 1.51 2001/03/09 01:02:12 chs 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, 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 (i = 0; i < PGFL_NQUEUES; i++)
    226 			TAILQ_INIT(&uvm.page_free[lcv].pgfl_queues[i]);
    227 	}
    228 	TAILQ_INIT(&uvm.page_active);
    229 	TAILQ_INIT(&uvm.page_inactive_swp);
    230 	TAILQ_INIT(&uvm.page_inactive_obj);
    231 	simple_lock_init(&uvm.pageqlock);
    232 	simple_lock_init(&uvm.fpageqlock);
    233 
    234 	/*
    235 	 * init the <obj,offset> => <page> hash table.  for now
    236 	 * we just have one bucket (the bootstrap bucket).  later on we
    237 	 * will allocate new buckets as we dynamically resize the hash table.
    238 	 */
    239 
    240 	uvm.page_nhash = 1;			/* 1 bucket */
    241 	uvm.page_hashmask = 0;			/* mask for hash function */
    242 	uvm.page_hash = &uvm_bootbucket;	/* install bootstrap bucket */
    243 	TAILQ_INIT(uvm.page_hash);		/* init hash table */
    244 	simple_lock_init(&uvm.hashlock);	/* init hash table lock */
    245 
    246 	/*
    247 	 * allocate vm_page structures.
    248 	 */
    249 
    250 	/*
    251 	 * sanity check:
    252 	 * before calling this function the MD code is expected to register
    253 	 * some free RAM with the uvm_page_physload() function.   our job
    254 	 * now is to allocate vm_page structures for this memory.
    255 	 */
    256 
    257 	if (vm_nphysseg == 0)
    258 		panic("uvm_page_bootstrap: no memory pre-allocated");
    259 
    260 	/*
    261 	 * first calculate the number of free pages...
    262 	 *
    263 	 * note that we use start/end rather than avail_start/avail_end.
    264 	 * this allows us to allocate extra vm_page structures in case we
    265 	 * want to return some memory to the pool after booting.
    266 	 */
    267 
    268 	freepages = 0;
    269 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    270 		freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
    271 
    272 	/*
    273 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
    274 	 * use.   for each page of memory we use we need a vm_page structure.
    275 	 * thus, the total number of pages we can use is the total size of
    276 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
    277 	 * structure.   we add one to freepages as a fudge factor to avoid
    278 	 * truncation errors (since we can only allocate in terms of whole
    279 	 * pages).
    280 	 */
    281 
    282 	pagecount = ((freepages + 1) << PAGE_SHIFT) /
    283 	    (PAGE_SIZE + sizeof(struct vm_page));
    284 	pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount *
    285 	    sizeof(struct vm_page));
    286 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
    287 
    288 	/*
    289 	 * init the vm_page structures and put them in the correct place.
    290 	 */
    291 
    292 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    293 		n = vm_physmem[lcv].end - vm_physmem[lcv].start;
    294 		if (n > pagecount) {
    295 			printf("uvm_page_init: lost %ld page(s) in init\n",
    296 			    (long)(n - pagecount));
    297 			panic("uvm_page_init");  /* XXXCDC: shouldn't happen? */
    298 			/* n = pagecount; */
    299 		}
    300 
    301 		/* set up page array pointers */
    302 		vm_physmem[lcv].pgs = pagearray;
    303 		pagearray += n;
    304 		pagecount -= n;
    305 		vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
    306 
    307 		/* init and free vm_pages (we've already zeroed them) */
    308 		paddr = ptoa(vm_physmem[lcv].start);
    309 		for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
    310 			vm_physmem[lcv].pgs[i].phys_addr = paddr;
    311 			if (atop(paddr) >= vm_physmem[lcv].avail_start &&
    312 			    atop(paddr) <= vm_physmem[lcv].avail_end) {
    313 				uvmexp.npages++;
    314 				/* add page to free pool */
    315 				uvm_pagefree(&vm_physmem[lcv].pgs[i]);
    316 			}
    317 		}
    318 	}
    319 
    320 	/*
    321 	 * pass up the values of virtual_space_start and
    322 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
    323 	 * layers of the VM.
    324 	 */
    325 
    326 	*kvm_startp = round_page(virtual_space_start);
    327 	*kvm_endp = trunc_page(virtual_space_end);
    328 
    329 	/*
    330 	 * init locks for kernel threads
    331 	 */
    332 
    333 	simple_lock_init(&uvm.pagedaemon_lock);
    334 	simple_lock_init(&uvm.aiodoned_lock);
    335 
    336 	/*
    337 	 * init various thresholds.
    338 	 * XXXCDC - values may need adjusting
    339 	 */
    340 
    341 	uvmexp.reserve_pagedaemon = 1;
    342 	uvmexp.reserve_kernel = 5;
    343 	uvmexp.anonminpct = 10;
    344 	uvmexp.vnodeminpct = 10;
    345 	uvmexp.vtextminpct = 5;
    346 	uvmexp.anonmin = uvmexp.anonminpct * 256 / 100;
    347 	uvmexp.vnodemin = uvmexp.vnodeminpct * 256 / 100;
    348 	uvmexp.vtextmin = uvmexp.vtextminpct * 256 / 100;
    349 
    350 	/*
    351 	 * determine if we should zero pages in the idle loop.
    352 	 */
    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  */
    368 
    369 void
    370 uvm_setpagesize()
    371 {
    372 	if (uvmexp.pagesize == 0)
    373 		uvmexp.pagesize = DEFAULT_PAGE_SIZE;
    374 	uvmexp.pagemask = uvmexp.pagesize - 1;
    375 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
    376 		panic("uvm_setpagesize: page size not a power of two");
    377 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
    378 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
    379 			break;
    380 }
    381 
    382 /*
    383  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
    384  */
    385 
    386 vaddr_t
    387 uvm_pageboot_alloc(size)
    388 	vsize_t size;
    389 {
    390 #if defined(PMAP_STEAL_MEMORY)
    391 	vaddr_t addr;
    392 
    393 	/*
    394 	 * defer bootstrap allocation to MD code (it may want to allocate
    395 	 * from a direct-mapped segment).  pmap_steal_memory should round
    396 	 * off virtual_space_start/virtual_space_end.
    397 	 */
    398 
    399 	addr = pmap_steal_memory(size, &virtual_space_start,
    400 	    &virtual_space_end);
    401 
    402 	return(addr);
    403 
    404 #else /* !PMAP_STEAL_MEMORY */
    405 
    406 	static boolean_t initialized = FALSE;
    407 	vaddr_t addr, vaddr;
    408 	paddr_t paddr;
    409 
    410 	/* round to page size */
    411 	size = round_page(size);
    412 
    413 	/*
    414 	 * on first call to this function, initialize ourselves.
    415 	 */
    416 	if (initialized == FALSE) {
    417 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
    418 
    419 		/* round it the way we like it */
    420 		virtual_space_start = round_page(virtual_space_start);
    421 		virtual_space_end = trunc_page(virtual_space_end);
    422 
    423 		initialized = TRUE;
    424 	}
    425 
    426 	/*
    427 	 * allocate virtual memory for this request
    428 	 */
    429 	if (virtual_space_start == virtual_space_end ||
    430 	    (virtual_space_end - virtual_space_start) < size)
    431 		panic("uvm_pageboot_alloc: out of virtual space");
    432 
    433 	addr = virtual_space_start;
    434 
    435 #ifdef PMAP_GROWKERNEL
    436 	/*
    437 	 * If the kernel pmap can't map the requested space,
    438 	 * then allocate more resources for it.
    439 	 */
    440 	if (uvm_maxkaddr < (addr + size)) {
    441 		uvm_maxkaddr = pmap_growkernel(addr + size);
    442 		if (uvm_maxkaddr < (addr + size))
    443 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
    444 	}
    445 #endif
    446 
    447 	virtual_space_start += size;
    448 
    449 	/*
    450 	 * allocate and mapin physical pages to back new virtual pages
    451 	 */
    452 
    453 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
    454 	    vaddr += PAGE_SIZE) {
    455 
    456 		if (!uvm_page_physget(&paddr))
    457 			panic("uvm_pageboot_alloc: out of memory");
    458 
    459 		/*
    460 		 * Note this memory is no longer managed, so using
    461 		 * pmap_kenter is safe.
    462 		 */
    463 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
    464 	}
    465 	return(addr);
    466 #endif	/* PMAP_STEAL_MEMORY */
    467 }
    468 
    469 #if !defined(PMAP_STEAL_MEMORY)
    470 /*
    471  * uvm_page_physget: "steal" one page from the vm_physmem structure.
    472  *
    473  * => attempt to allocate it off the end of a segment in which the "avail"
    474  *    values match the start/end values.   if we can't do that, then we
    475  *    will advance both values (making them equal, and removing some
    476  *    vm_page structures from the non-avail area).
    477  * => return false if out of memory.
    478  */
    479 
    480 /* subroutine: try to allocate from memory chunks on the specified freelist */
    481 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int));
    482 
    483 static boolean_t
    484 uvm_page_physget_freelist(paddrp, freelist)
    485 	paddr_t *paddrp;
    486 	int freelist;
    487 {
    488 	int lcv, x;
    489 
    490 	/* pass 1: try allocating from a matching end */
    491 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    492 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    493 #else
    494 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    495 #endif
    496 	{
    497 
    498 		if (uvm.page_init_done == TRUE)
    499 			panic("uvm_page_physget: called _after_ bootstrap");
    500 
    501 		if (vm_physmem[lcv].free_list != freelist)
    502 			continue;
    503 
    504 		/* try from front */
    505 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
    506 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    507 			*paddrp = ptoa(vm_physmem[lcv].avail_start);
    508 			vm_physmem[lcv].avail_start++;
    509 			vm_physmem[lcv].start++;
    510 			/* nothing left?   nuke it */
    511 			if (vm_physmem[lcv].avail_start ==
    512 			    vm_physmem[lcv].end) {
    513 				if (vm_nphysseg == 1)
    514 				    panic("vum_page_physget: out of memory!");
    515 				vm_nphysseg--;
    516 				for (x = lcv ; x < vm_nphysseg ; x++)
    517 					/* structure copy */
    518 					vm_physmem[x] = vm_physmem[x+1];
    519 			}
    520 			return (TRUE);
    521 		}
    522 
    523 		/* try from rear */
    524 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
    525 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    526 			*paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
    527 			vm_physmem[lcv].avail_end--;
    528 			vm_physmem[lcv].end--;
    529 			/* nothing left?   nuke it */
    530 			if (vm_physmem[lcv].avail_end ==
    531 			    vm_physmem[lcv].start) {
    532 				if (vm_nphysseg == 1)
    533 				    panic("uvm_page_physget: out of memory!");
    534 				vm_nphysseg--;
    535 				for (x = lcv ; x < vm_nphysseg ; x++)
    536 					/* structure copy */
    537 					vm_physmem[x] = vm_physmem[x+1];
    538 			}
    539 			return (TRUE);
    540 		}
    541 	}
    542 
    543 	/* pass2: forget about matching ends, just allocate something */
    544 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    545 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    546 #else
    547 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    548 #endif
    549 	{
    550 
    551 		/* any room in this bank? */
    552 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
    553 			continue;  /* nope */
    554 
    555 		*paddrp = ptoa(vm_physmem[lcv].avail_start);
    556 		vm_physmem[lcv].avail_start++;
    557 		/* truncate! */
    558 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
    559 
    560 		/* nothing left?   nuke it */
    561 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
    562 			if (vm_nphysseg == 1)
    563 				panic("uvm_page_physget: out of memory!");
    564 			vm_nphysseg--;
    565 			for (x = lcv ; x < vm_nphysseg ; x++)
    566 				/* structure copy */
    567 				vm_physmem[x] = vm_physmem[x+1];
    568 		}
    569 		return (TRUE);
    570 	}
    571 
    572 	return (FALSE);        /* whoops! */
    573 }
    574 
    575 boolean_t
    576 uvm_page_physget(paddrp)
    577 	paddr_t *paddrp;
    578 {
    579 	int i;
    580 
    581 	/* try in the order of freelist preference */
    582 	for (i = 0; i < VM_NFREELIST; i++)
    583 		if (uvm_page_physget_freelist(paddrp, i) == TRUE)
    584 			return (TRUE);
    585 	return (FALSE);
    586 }
    587 #endif /* PMAP_STEAL_MEMORY */
    588 
    589 /*
    590  * uvm_page_physload: load physical memory into VM system
    591  *
    592  * => all args are PFs
    593  * => all pages in start/end get vm_page structures
    594  * => areas marked by avail_start/avail_end get added to the free page pool
    595  * => we are limited to VM_PHYSSEG_MAX physical memory segments
    596  */
    597 
    598 void
    599 uvm_page_physload(start, end, avail_start, avail_end, free_list)
    600 	paddr_t start, end, avail_start, avail_end;
    601 	int free_list;
    602 {
    603 	int preload, lcv;
    604 	psize_t npages;
    605 	struct vm_page *pgs;
    606 	struct vm_physseg *ps;
    607 
    608 	if (uvmexp.pagesize == 0)
    609 		panic("uvm_page_physload: page size not set!");
    610 
    611 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
    612 		panic("uvm_page_physload: bad free list %d\n", free_list);
    613 
    614 	if (start >= end)
    615 		panic("uvm_page_physload: start >= end");
    616 
    617 	/*
    618 	 * do we have room?
    619 	 */
    620 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
    621 		printf("uvm_page_physload: unable to load physical memory "
    622 		    "segment\n");
    623 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
    624 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
    625 		printf("\tincrease VM_PHYSSEG_MAX\n");
    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("uvm_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 		pgs = malloc(sizeof(struct vm_page) * npages,
    650 		    M_VMPAGE, M_NOWAIT);
    651 		if (pgs == NULL) {
    652 			printf("uvm_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("uvm_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 = splvm();
    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 	KASSERT(obj == NULL || anon == NULL);
    896 	KASSERT(off == trunc_page(off));
    897 
    898 	LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock));
    899 	LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock));
    900 
    901 	s = uvm_lock_fpageq();
    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.paging < uvmexp.freemin ||
    909 	    (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
    910 	     uvmexp.inactive < uvmexp.inactarg)) {
    911 		wakeup(&uvm.pagedaemon);
    912 	}
    913 
    914 	/*
    915 	 * fail if any of these conditions is true:
    916 	 * [1]  there really are no free pages, or
    917 	 * [2]  only kernel "reserved" pages remain and
    918 	 *        the page isn't being allocated to a kernel object.
    919 	 * [3]  only pagedaemon "reserved" pages remain and
    920 	 *        the requestor isn't the pagedaemon.
    921 	 */
    922 
    923 	use_reserve = (flags & UVM_PGA_USERESERVE) ||
    924 		(obj && UVM_OBJ_IS_KERN_OBJECT(obj));
    925 	if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
    926 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
    927 	     !(use_reserve && curproc == uvm.pagedaemon_proc)))
    928 		goto fail;
    929 
    930 #if PGFL_NQUEUES != 2
    931 #error uvm_pagealloc_strat needs to be updated
    932 #endif
    933 
    934 	/*
    935 	 * If we want a zero'd page, try the ZEROS queue first, otherwise
    936 	 * we try the UNKNOWN queue first.
    937 	 */
    938 	if (flags & UVM_PGA_ZERO) {
    939 		try1 = PGFL_ZEROS;
    940 		try2 = PGFL_UNKNOWN;
    941 	} else {
    942 		try1 = PGFL_UNKNOWN;
    943 		try2 = PGFL_ZEROS;
    944 	}
    945 
    946  again:
    947 	switch (strat) {
    948 	case UVM_PGA_STRAT_NORMAL:
    949 		/* Check all freelists in descending priority order. */
    950 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    951 			pgfl = &uvm.page_free[lcv];
    952 			if ((pg = TAILQ_FIRST((freeq =
    953 			      &pgfl->pgfl_queues[try1]))) != NULL ||
    954 			    (pg = TAILQ_FIRST((freeq =
    955 			      &pgfl->pgfl_queues[try2]))) != NULL)
    956 				goto gotit;
    957 		}
    958 
    959 		/* No pages free! */
    960 		goto fail;
    961 
    962 	case UVM_PGA_STRAT_ONLY:
    963 	case UVM_PGA_STRAT_FALLBACK:
    964 		/* Attempt to allocate from the specified free list. */
    965 		KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
    966 		pgfl = &uvm.page_free[free_list];
    967 		if ((pg = TAILQ_FIRST((freeq =
    968 		      &pgfl->pgfl_queues[try1]))) != NULL ||
    969 		    (pg = TAILQ_FIRST((freeq =
    970 		      &pgfl->pgfl_queues[try2]))) != NULL)
    971 			goto gotit;
    972 
    973 		/* Fall back, if possible. */
    974 		if (strat == UVM_PGA_STRAT_FALLBACK) {
    975 			strat = UVM_PGA_STRAT_NORMAL;
    976 			goto again;
    977 		}
    978 
    979 		/* No pages free! */
    980 		goto fail;
    981 
    982 	default:
    983 		panic("uvm_pagealloc_strat: bad strat %d", strat);
    984 		/* NOTREACHED */
    985 	}
    986 
    987  gotit:
    988 	TAILQ_REMOVE(freeq, pg, pageq);
    989 	uvmexp.free--;
    990 
    991 	/* update zero'd page count */
    992 	if (pg->flags & PG_ZERO)
    993 		uvmexp.zeropages--;
    994 
    995 	/*
    996 	 * update allocation statistics and remember if we have to
    997 	 * zero the page
    998 	 */
    999 	if (flags & UVM_PGA_ZERO) {
   1000 		if (pg->flags & PG_ZERO) {
   1001 			uvmexp.pga_zerohit++;
   1002 			zeroit = 0;
   1003 		} else {
   1004 			uvmexp.pga_zeromiss++;
   1005 			zeroit = 1;
   1006 		}
   1007 	}
   1008 
   1009 	uvm_unlock_fpageq(s);		/* unlock free page queue */
   1010 
   1011 	pg->offset = off;
   1012 	pg->uobject = obj;
   1013 	pg->uanon = anon;
   1014 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
   1015 	pg->version++;
   1016 	if (anon) {
   1017 		anon->u.an_page = pg;
   1018 		pg->pqflags = PQ_ANON;
   1019 		uvmexp.anonpages++;
   1020 	} else {
   1021 		if (obj)
   1022 			uvm_pageinsert(pg);
   1023 		pg->pqflags = 0;
   1024 	}
   1025 #if defined(UVM_PAGE_TRKOWN)
   1026 	pg->owner_tag = NULL;
   1027 #endif
   1028 	UVM_PAGE_OWN(pg, "new alloc");
   1029 
   1030 	if (flags & UVM_PGA_ZERO) {
   1031 		/*
   1032 		 * A zero'd page is not clean.  If we got a page not already
   1033 		 * zero'd, then we have to zero it ourselves.
   1034 		 */
   1035 		pg->flags &= ~PG_CLEAN;
   1036 		if (zeroit)
   1037 			pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1038 	}
   1039 
   1040 	return(pg);
   1041 
   1042  fail:
   1043 	uvm_unlock_fpageq(s);
   1044 	return (NULL);
   1045 }
   1046 
   1047 /*
   1048  * uvm_pagerealloc: reallocate a page from one object to another
   1049  *
   1050  * => both objects must be locked
   1051  */
   1052 
   1053 void
   1054 uvm_pagerealloc(pg, newobj, newoff)
   1055 	struct vm_page *pg;
   1056 	struct uvm_object *newobj;
   1057 	voff_t newoff;
   1058 {
   1059 	/*
   1060 	 * remove it from the old object
   1061 	 */
   1062 
   1063 	if (pg->uobject) {
   1064 		uvm_pageremove(pg);
   1065 	}
   1066 
   1067 	/*
   1068 	 * put it in the new object
   1069 	 */
   1070 
   1071 	if (newobj) {
   1072 		pg->uobject = newobj;
   1073 		pg->offset = newoff;
   1074 		pg->version++;
   1075 		uvm_pageinsert(pg);
   1076 	}
   1077 }
   1078 
   1079 
   1080 /*
   1081  * uvm_pagefree: free page
   1082  *
   1083  * => erase page's identity (i.e. remove from hash/object)
   1084  * => put page on free list
   1085  * => caller must lock owning object (either anon or uvm_object)
   1086  * => caller must lock page queues
   1087  * => assumes all valid mappings of pg are gone
   1088  */
   1089 
   1090 void
   1091 uvm_pagefree(pg)
   1092 	struct vm_page *pg;
   1093 {
   1094 	int s;
   1095 	int saved_loan_count = pg->loan_count;
   1096 
   1097 #ifdef DEBUG
   1098 	if (pg->uobject == (void *)0xdeadbeef &&
   1099 	    pg->uanon == (void *)0xdeadbeef) {
   1100 		panic("uvm_pagefree: freeing free page %p\n", pg);
   1101 	}
   1102 #endif
   1103 
   1104 	/*
   1105 	 * if the page was an object page (and thus "TABLED"), remove it
   1106 	 * from the object.
   1107 	 */
   1108 
   1109 	if (pg->flags & PG_TABLED) {
   1110 
   1111 		/*
   1112 		 * if the object page is on loan we are going to drop ownership.
   1113 		 * it is possible that an anon will take over as owner for this
   1114 		 * page later on.   the anon will want a !PG_CLEAN page so that
   1115 		 * it knows it needs to allocate swap if it wants to page the
   1116 		 * page out.
   1117 		 */
   1118 
   1119 		if (saved_loan_count)
   1120 			pg->flags &= ~PG_CLEAN;	/* in case an anon takes over */
   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 
   1132 		if (saved_loan_count)
   1133 			return;
   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 
   1144 		pg->pqflags &= ~PQ_ANON;
   1145 		pg->uanon = NULL;
   1146 		return;
   1147 	}
   1148 	KASSERT(saved_loan_count == 0);
   1149 
   1150 	/*
   1151 	 * now remove the page from the queues
   1152 	 */
   1153 
   1154 	if (pg->pqflags & PQ_ACTIVE) {
   1155 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
   1156 		pg->pqflags &= ~PQ_ACTIVE;
   1157 		uvmexp.active--;
   1158 	}
   1159 	if (pg->pqflags & PQ_INACTIVE) {
   1160 		if (pg->pqflags & PQ_SWAPBACKED)
   1161 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
   1162 		else
   1163 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
   1164 		pg->pqflags &= ~PQ_INACTIVE;
   1165 		uvmexp.inactive--;
   1166 	}
   1167 
   1168 	/*
   1169 	 * if the page was wired, unwire it now.
   1170 	 */
   1171 
   1172 	if (pg->wire_count) {
   1173 		pg->wire_count = 0;
   1174 		uvmexp.wired--;
   1175 	}
   1176 	if (pg->uanon) {
   1177 		uvmexp.anonpages--;
   1178 	}
   1179 
   1180 	/*
   1181 	 * and put on free queue
   1182 	 */
   1183 
   1184 	pg->flags &= ~PG_ZERO;
   1185 
   1186 	s = uvm_lock_fpageq();
   1187 	TAILQ_INSERT_TAIL(&uvm.page_free[
   1188 	    uvm_page_lookup_freelist(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq);
   1189 	pg->pqflags = PQ_FREE;
   1190 #ifdef DEBUG
   1191 	pg->uobject = (void *)0xdeadbeef;
   1192 	pg->offset = 0xdeadbeef;
   1193 	pg->uanon = (void *)0xdeadbeef;
   1194 #endif
   1195 	uvmexp.free++;
   1196 
   1197 	if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
   1198 		uvm.page_idle_zero = vm_page_zero_enable;
   1199 
   1200 	uvm_unlock_fpageq(s);
   1201 }
   1202 
   1203 /*
   1204  * uvm_page_unbusy: unbusy an array of pages.
   1205  *
   1206  * => pages must either all belong to the same object, or all belong to anons.
   1207  * => if pages are object-owned, object must be locked.
   1208  * => if pages are anon-owned, anons must be unlockd and have 0 refcount.
   1209  */
   1210 
   1211 void
   1212 uvm_page_unbusy(pgs, npgs)
   1213 	struct vm_page **pgs;
   1214 	int npgs;
   1215 {
   1216 	struct vm_page *pg;
   1217 	struct uvm_object *uobj;
   1218 	int i;
   1219 	UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
   1220 
   1221 	for (i = 0; i < npgs; i++) {
   1222 		pg = pgs[i];
   1223 
   1224 		if (pg == NULL) {
   1225 			continue;
   1226 		}
   1227 		if (pg->flags & PG_WANTED) {
   1228 			wakeup(pg);
   1229 		}
   1230 		if (pg->flags & PG_RELEASED) {
   1231 			UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
   1232 			uobj = pg->uobject;
   1233 			if (uobj != NULL) {
   1234 				uobj->pgops->pgo_releasepg(pg, NULL);
   1235 			} else {
   1236 				pg->flags &= ~(PG_BUSY);
   1237 				UVM_PAGE_OWN(pg, NULL);
   1238 				uvm_anfree(pg->uanon);
   1239 			}
   1240 		} else {
   1241 			UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
   1242 			KASSERT(pg->wire_count ||
   1243 				(pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)));
   1244 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1245 			UVM_PAGE_OWN(pg, NULL);
   1246 		}
   1247 	}
   1248 }
   1249 
   1250 #if defined(UVM_PAGE_TRKOWN)
   1251 /*
   1252  * uvm_page_own: set or release page ownership
   1253  *
   1254  * => this is a debugging function that keeps track of who sets PG_BUSY
   1255  *	and where they do it.   it can be used to track down problems
   1256  *	such a process setting "PG_BUSY" and never releasing it.
   1257  * => page's object [if any] must be locked
   1258  * => if "tag" is NULL then we are releasing page ownership
   1259  */
   1260 void
   1261 uvm_page_own(pg, tag)
   1262 	struct vm_page *pg;
   1263 	char *tag;
   1264 {
   1265 	/* gain ownership? */
   1266 	if (tag) {
   1267 		if (pg->owner_tag) {
   1268 			printf("uvm_page_own: page %p already owned "
   1269 			    "by proc %d [%s]\n", pg,
   1270 			     pg->owner, pg->owner_tag);
   1271 			panic("uvm_page_own");
   1272 		}
   1273 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
   1274 		pg->owner_tag = tag;
   1275 		return;
   1276 	}
   1277 
   1278 	/* drop ownership */
   1279 	if (pg->owner_tag == NULL) {
   1280 		printf("uvm_page_own: dropping ownership of an non-owned "
   1281 		    "page (%p)\n", pg);
   1282 		panic("uvm_page_own");
   1283 	}
   1284 	pg->owner_tag = NULL;
   1285 	return;
   1286 }
   1287 #endif
   1288 
   1289 /*
   1290  * uvm_pageidlezero: zero free pages while the system is idle.
   1291  *
   1292  * => we do at least one iteration per call, if we are below the target.
   1293  * => we loop until we either reach the target or whichqs indicates that
   1294  *	there is a process ready to run.
   1295  */
   1296 void
   1297 uvm_pageidlezero()
   1298 {
   1299 	struct vm_page *pg;
   1300 	struct pgfreelist *pgfl;
   1301 	int free_list, s;
   1302 
   1303 	do {
   1304 		s = uvm_lock_fpageq();
   1305 
   1306 		if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
   1307 			uvm.page_idle_zero = FALSE;
   1308 			uvm_unlock_fpageq(s);
   1309 			return;
   1310 		}
   1311 
   1312 		for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
   1313 			pgfl = &uvm.page_free[free_list];
   1314 			if ((pg = TAILQ_FIRST(&pgfl->pgfl_queues[
   1315 			    PGFL_UNKNOWN])) != NULL)
   1316 				break;
   1317 		}
   1318 
   1319 		if (pg == NULL) {
   1320 			/*
   1321 			 * No non-zero'd pages; don't bother trying again
   1322 			 * until we know we have non-zero'd pages free.
   1323 			 */
   1324 			uvm.page_idle_zero = FALSE;
   1325 			uvm_unlock_fpageq(s);
   1326 			return;
   1327 		}
   1328 
   1329 		TAILQ_REMOVE(&pgfl->pgfl_queues[PGFL_UNKNOWN], pg, pageq);
   1330 		uvmexp.free--;
   1331 		uvm_unlock_fpageq(s);
   1332 
   1333 #ifdef PMAP_PAGEIDLEZERO
   1334 		if (PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg)) == FALSE) {
   1335 			/*
   1336 			 * The machine-dependent code detected some
   1337 			 * reason for us to abort zeroing pages,
   1338 			 * probably because there is a process now
   1339 			 * ready to run.
   1340 			 */
   1341 			s = uvm_lock_fpageq();
   1342 			TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_UNKNOWN],
   1343 			    pg, pageq);
   1344 			uvmexp.free++;
   1345 			uvmexp.zeroaborts++;
   1346 			uvm_unlock_fpageq(s);
   1347 			return;
   1348 		}
   1349 #else
   1350 		/*
   1351 		 * XXX This will toast the cache unless the pmap_zero_page()
   1352 		 * XXX implementation does uncached access.
   1353 		 */
   1354 		pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1355 #endif
   1356 		pg->flags |= PG_ZERO;
   1357 
   1358 		s = uvm_lock_fpageq();
   1359 		TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_ZEROS], pg, pageq);
   1360 		uvmexp.free++;
   1361 		uvmexp.zeropages++;
   1362 		uvm_unlock_fpageq(s);
   1363 	} while (sched_whichqs == 0);
   1364 }
   1365