Home | History | Annotate | Line # | Download | only in uvm
uvm_page.c revision 1.53
      1 /*	$NetBSD: uvm_page.c,v 1.53 2001/04/24 04:31:18 thorpej 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 	static boolean_t initialized = FALSE;
    391 	vaddr_t addr;
    392 #if !defined(PMAP_STEAL_MEMORY)
    393 	vaddr_t vaddr;
    394 	paddr_t paddr;
    395 #endif
    396 
    397 	/*
    398 	 * on first call to this function, initialize ourselves.
    399 	 */
    400 	if (initialized == FALSE) {
    401 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
    402 
    403 		/* round it the way we like it */
    404 		virtual_space_start = round_page(virtual_space_start);
    405 		virtual_space_end = trunc_page(virtual_space_end);
    406 
    407 		initialized = TRUE;
    408 	}
    409 
    410 	/* round to page size */
    411 	size = round_page(size);
    412 
    413 #if defined(PMAP_STEAL_MEMORY)
    414 
    415 	/*
    416 	 * defer bootstrap allocation to MD code (it may want to allocate
    417 	 * from a direct-mapped segment).  pmap_steal_memory should adjust
    418 	 * virtual_space_start/virtual_space_end if necessary.
    419 	 */
    420 
    421 	addr = pmap_steal_memory(size, &virtual_space_start,
    422 	    &virtual_space_end);
    423 
    424 	return(addr);
    425 
    426 #else /* !PMAP_STEAL_MEMORY */
    427 
    428 	/*
    429 	 * allocate virtual memory for this request
    430 	 */
    431 	if (virtual_space_start == virtual_space_end ||
    432 	    (virtual_space_end - virtual_space_start) < size)
    433 		panic("uvm_pageboot_alloc: out of virtual space");
    434 
    435 	addr = virtual_space_start;
    436 
    437 #ifdef PMAP_GROWKERNEL
    438 	/*
    439 	 * If the kernel pmap can't map the requested space,
    440 	 * then allocate more resources for it.
    441 	 */
    442 	if (uvm_maxkaddr < (addr + size)) {
    443 		uvm_maxkaddr = pmap_growkernel(addr + size);
    444 		if (uvm_maxkaddr < (addr + size))
    445 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
    446 	}
    447 #endif
    448 
    449 	virtual_space_start += size;
    450 
    451 	/*
    452 	 * allocate and mapin physical pages to back new virtual pages
    453 	 */
    454 
    455 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
    456 	    vaddr += PAGE_SIZE) {
    457 
    458 		if (!uvm_page_physget(&paddr))
    459 			panic("uvm_pageboot_alloc: out of memory");
    460 
    461 		/*
    462 		 * Note this memory is no longer managed, so using
    463 		 * pmap_kenter is safe.
    464 		 */
    465 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
    466 	}
    467 	pmap_update();
    468 	return(addr);
    469 #endif	/* PMAP_STEAL_MEMORY */
    470 }
    471 
    472 #if !defined(PMAP_STEAL_MEMORY)
    473 /*
    474  * uvm_page_physget: "steal" one page from the vm_physmem structure.
    475  *
    476  * => attempt to allocate it off the end of a segment in which the "avail"
    477  *    values match the start/end values.   if we can't do that, then we
    478  *    will advance both values (making them equal, and removing some
    479  *    vm_page structures from the non-avail area).
    480  * => return false if out of memory.
    481  */
    482 
    483 /* subroutine: try to allocate from memory chunks on the specified freelist */
    484 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int));
    485 
    486 static boolean_t
    487 uvm_page_physget_freelist(paddrp, freelist)
    488 	paddr_t *paddrp;
    489 	int freelist;
    490 {
    491 	int lcv, x;
    492 
    493 	/* pass 1: try allocating from a matching end */
    494 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    495 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    496 #else
    497 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    498 #endif
    499 	{
    500 
    501 		if (uvm.page_init_done == TRUE)
    502 			panic("uvm_page_physget: called _after_ bootstrap");
    503 
    504 		if (vm_physmem[lcv].free_list != freelist)
    505 			continue;
    506 
    507 		/* try from front */
    508 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
    509 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    510 			*paddrp = ptoa(vm_physmem[lcv].avail_start);
    511 			vm_physmem[lcv].avail_start++;
    512 			vm_physmem[lcv].start++;
    513 			/* nothing left?   nuke it */
    514 			if (vm_physmem[lcv].avail_start ==
    515 			    vm_physmem[lcv].end) {
    516 				if (vm_nphysseg == 1)
    517 				    panic("vum_page_physget: out of memory!");
    518 				vm_nphysseg--;
    519 				for (x = lcv ; x < vm_nphysseg ; x++)
    520 					/* structure copy */
    521 					vm_physmem[x] = vm_physmem[x+1];
    522 			}
    523 			return (TRUE);
    524 		}
    525 
    526 		/* try from rear */
    527 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
    528 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    529 			*paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
    530 			vm_physmem[lcv].avail_end--;
    531 			vm_physmem[lcv].end--;
    532 			/* nothing left?   nuke it */
    533 			if (vm_physmem[lcv].avail_end ==
    534 			    vm_physmem[lcv].start) {
    535 				if (vm_nphysseg == 1)
    536 				    panic("uvm_page_physget: out of memory!");
    537 				vm_nphysseg--;
    538 				for (x = lcv ; x < vm_nphysseg ; x++)
    539 					/* structure copy */
    540 					vm_physmem[x] = vm_physmem[x+1];
    541 			}
    542 			return (TRUE);
    543 		}
    544 	}
    545 
    546 	/* pass2: forget about matching ends, just allocate something */
    547 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    548 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    549 #else
    550 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    551 #endif
    552 	{
    553 
    554 		/* any room in this bank? */
    555 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
    556 			continue;  /* nope */
    557 
    558 		*paddrp = ptoa(vm_physmem[lcv].avail_start);
    559 		vm_physmem[lcv].avail_start++;
    560 		/* truncate! */
    561 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
    562 
    563 		/* nothing left?   nuke it */
    564 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
    565 			if (vm_nphysseg == 1)
    566 				panic("uvm_page_physget: out of memory!");
    567 			vm_nphysseg--;
    568 			for (x = lcv ; x < vm_nphysseg ; x++)
    569 				/* structure copy */
    570 				vm_physmem[x] = vm_physmem[x+1];
    571 		}
    572 		return (TRUE);
    573 	}
    574 
    575 	return (FALSE);        /* whoops! */
    576 }
    577 
    578 boolean_t
    579 uvm_page_physget(paddrp)
    580 	paddr_t *paddrp;
    581 {
    582 	int i;
    583 
    584 	/* try in the order of freelist preference */
    585 	for (i = 0; i < VM_NFREELIST; i++)
    586 		if (uvm_page_physget_freelist(paddrp, i) == TRUE)
    587 			return (TRUE);
    588 	return (FALSE);
    589 }
    590 #endif /* PMAP_STEAL_MEMORY */
    591 
    592 /*
    593  * uvm_page_physload: load physical memory into VM system
    594  *
    595  * => all args are PFs
    596  * => all pages in start/end get vm_page structures
    597  * => areas marked by avail_start/avail_end get added to the free page pool
    598  * => we are limited to VM_PHYSSEG_MAX physical memory segments
    599  */
    600 
    601 void
    602 uvm_page_physload(start, end, avail_start, avail_end, free_list)
    603 	paddr_t start, end, avail_start, avail_end;
    604 	int free_list;
    605 {
    606 	int preload, lcv;
    607 	psize_t npages;
    608 	struct vm_page *pgs;
    609 	struct vm_physseg *ps;
    610 
    611 	if (uvmexp.pagesize == 0)
    612 		panic("uvm_page_physload: page size not set!");
    613 
    614 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
    615 		panic("uvm_page_physload: bad free list %d\n", free_list);
    616 
    617 	if (start >= end)
    618 		panic("uvm_page_physload: start >= end");
    619 
    620 	/*
    621 	 * do we have room?
    622 	 */
    623 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
    624 		printf("uvm_page_physload: unable to load physical memory "
    625 		    "segment\n");
    626 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
    627 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
    628 		printf("\tincrease VM_PHYSSEG_MAX\n");
    629 		return;
    630 	}
    631 
    632 	/*
    633 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
    634 	 * called yet, so malloc is not available).
    635 	 */
    636 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    637 		if (vm_physmem[lcv].pgs)
    638 			break;
    639 	}
    640 	preload = (lcv == vm_nphysseg);
    641 
    642 	/*
    643 	 * if VM is already running, attempt to malloc() vm_page structures
    644 	 */
    645 	if (!preload) {
    646 #if defined(VM_PHYSSEG_NOADD)
    647 		panic("uvm_page_physload: tried to add RAM after vm_mem_init");
    648 #else
    649 		/* XXXCDC: need some sort of lockout for this case */
    650 		paddr_t paddr;
    651 		npages = end - start;  /* # of pages */
    652 		pgs = malloc(sizeof(struct vm_page) * npages,
    653 		    M_VMPAGE, M_NOWAIT);
    654 		if (pgs == NULL) {
    655 			printf("uvm_page_physload: can not malloc vm_page "
    656 			    "structs for segment\n");
    657 			printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
    658 			return;
    659 		}
    660 		/* zero data, init phys_addr and free_list, and free pages */
    661 		memset(pgs, 0, sizeof(struct vm_page) * npages);
    662 		for (lcv = 0, paddr = ptoa(start) ;
    663 				 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
    664 			pgs[lcv].phys_addr = paddr;
    665 			pgs[lcv].free_list = free_list;
    666 			if (atop(paddr) >= avail_start &&
    667 			    atop(paddr) <= avail_end)
    668 				uvm_pagefree(&pgs[lcv]);
    669 		}
    670 		/* XXXCDC: incomplete: need to update uvmexp.free, what else? */
    671 		/* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
    672 #endif
    673 	} else {
    674 
    675 		/* gcc complains if these don't get init'd */
    676 		pgs = NULL;
    677 		npages = 0;
    678 
    679 	}
    680 
    681 	/*
    682 	 * now insert us in the proper place in vm_physmem[]
    683 	 */
    684 
    685 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
    686 
    687 	/* random: put it at the end (easy!) */
    688 	ps = &vm_physmem[vm_nphysseg];
    689 
    690 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
    691 
    692 	{
    693 		int x;
    694 		/* sort by address for binary search */
    695 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    696 			if (start < vm_physmem[lcv].start)
    697 				break;
    698 		ps = &vm_physmem[lcv];
    699 		/* move back other entries, if necessary ... */
    700 		for (x = vm_nphysseg ; x > lcv ; x--)
    701 			/* structure copy */
    702 			vm_physmem[x] = vm_physmem[x - 1];
    703 	}
    704 
    705 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    706 
    707 	{
    708 		int x;
    709 		/* sort by largest segment first */
    710 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    711 			if ((end - start) >
    712 			    (vm_physmem[lcv].end - vm_physmem[lcv].start))
    713 				break;
    714 		ps = &vm_physmem[lcv];
    715 		/* move back other entries, if necessary ... */
    716 		for (x = vm_nphysseg ; x > lcv ; x--)
    717 			/* structure copy */
    718 			vm_physmem[x] = vm_physmem[x - 1];
    719 	}
    720 
    721 #else
    722 
    723 	panic("uvm_page_physload: unknown physseg strategy selected!");
    724 
    725 #endif
    726 
    727 	ps->start = start;
    728 	ps->end = end;
    729 	ps->avail_start = avail_start;
    730 	ps->avail_end = avail_end;
    731 	if (preload) {
    732 		ps->pgs = NULL;
    733 	} else {
    734 		ps->pgs = pgs;
    735 		ps->lastpg = pgs + npages - 1;
    736 	}
    737 	ps->free_list = free_list;
    738 	vm_nphysseg++;
    739 
    740 	/*
    741 	 * done!
    742 	 */
    743 
    744 	if (!preload)
    745 		uvm_page_rehash();
    746 
    747 	return;
    748 }
    749 
    750 /*
    751  * uvm_page_rehash: reallocate hash table based on number of free pages.
    752  */
    753 
    754 void
    755 uvm_page_rehash()
    756 {
    757 	int freepages, lcv, bucketcount, s, oldcount;
    758 	struct pglist *newbuckets, *oldbuckets;
    759 	struct vm_page *pg;
    760 	size_t newsize, oldsize;
    761 
    762 	/*
    763 	 * compute number of pages that can go in the free pool
    764 	 */
    765 
    766 	freepages = 0;
    767 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    768 		freepages +=
    769 		    (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
    770 
    771 	/*
    772 	 * compute number of buckets needed for this number of pages
    773 	 */
    774 
    775 	bucketcount = 1;
    776 	while (bucketcount < freepages)
    777 		bucketcount = bucketcount * 2;
    778 
    779 	/*
    780 	 * compute the size of the current table and new table.
    781 	 */
    782 
    783 	oldbuckets = uvm.page_hash;
    784 	oldcount = uvm.page_nhash;
    785 	oldsize = round_page(sizeof(struct pglist) * oldcount);
    786 	newsize = round_page(sizeof(struct pglist) * bucketcount);
    787 
    788 	/*
    789 	 * allocate the new buckets
    790 	 */
    791 
    792 	newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize);
    793 	if (newbuckets == NULL) {
    794 		printf("uvm_page_physrehash: WARNING: could not grow page "
    795 		    "hash table\n");
    796 		return;
    797 	}
    798 	for (lcv = 0 ; lcv < bucketcount ; lcv++)
    799 		TAILQ_INIT(&newbuckets[lcv]);
    800 
    801 	/*
    802 	 * now replace the old buckets with the new ones and rehash everything
    803 	 */
    804 
    805 	s = splvm();
    806 	simple_lock(&uvm.hashlock);
    807 	uvm.page_hash = newbuckets;
    808 	uvm.page_nhash = bucketcount;
    809 	uvm.page_hashmask = bucketcount - 1;  /* power of 2 */
    810 
    811 	/* ... and rehash */
    812 	for (lcv = 0 ; lcv < oldcount ; lcv++) {
    813 		while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
    814 			TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
    815 			TAILQ_INSERT_TAIL(
    816 			  &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
    817 			  pg, hashq);
    818 		}
    819 	}
    820 	simple_unlock(&uvm.hashlock);
    821 	splx(s);
    822 
    823 	/*
    824 	 * free old bucket array if is not the boot-time table
    825 	 */
    826 
    827 	if (oldbuckets != &uvm_bootbucket)
    828 		uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize);
    829 
    830 	/*
    831 	 * done
    832 	 */
    833 	return;
    834 }
    835 
    836 
    837 #if 1 /* XXXCDC: TMP TMP TMP DEBUG DEBUG DEBUG */
    838 
    839 void uvm_page_physdump __P((void)); /* SHUT UP GCC */
    840 
    841 /* call from DDB */
    842 void
    843 uvm_page_physdump()
    844 {
    845 	int lcv;
    846 
    847 	printf("rehash: physical memory config [segs=%d of %d]:\n",
    848 				 vm_nphysseg, VM_PHYSSEG_MAX);
    849 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    850 		printf("0x%llx->0x%llx [0x%llx->0x%llx]\n",
    851 		    (long long)vm_physmem[lcv].start,
    852 		    (long long)vm_physmem[lcv].end,
    853 		    (long long)vm_physmem[lcv].avail_start,
    854 		    (long long)vm_physmem[lcv].avail_end);
    855 	printf("STRATEGY = ");
    856 	switch (VM_PHYSSEG_STRAT) {
    857 	case VM_PSTRAT_RANDOM: printf("RANDOM\n"); break;
    858 	case VM_PSTRAT_BSEARCH: printf("BSEARCH\n"); break;
    859 	case VM_PSTRAT_BIGFIRST: printf("BIGFIRST\n"); break;
    860 	default: printf("<<UNKNOWN>>!!!!\n");
    861 	}
    862 	printf("number of buckets = %d\n", uvm.page_nhash);
    863 }
    864 #endif
    865 
    866 /*
    867  * uvm_pagealloc_strat: allocate vm_page from a particular free list.
    868  *
    869  * => return null if no pages free
    870  * => wake up pagedaemon if number of free pages drops below low water mark
    871  * => if obj != NULL, obj must be locked (to put in hash)
    872  * => if anon != NULL, anon must be locked (to put in anon)
    873  * => only one of obj or anon can be non-null
    874  * => caller must activate/deactivate page if it is not wired.
    875  * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
    876  * => policy decision: it is more important to pull a page off of the
    877  *	appropriate priority free list than it is to get a zero'd or
    878  *	unknown contents page.  This is because we live with the
    879  *	consequences of a bad free list decision for the entire
    880  *	lifetime of the page, e.g. if the page comes from memory that
    881  *	is slower to access.
    882  */
    883 
    884 struct vm_page *
    885 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list)
    886 	struct uvm_object *obj;
    887 	voff_t off;
    888 	int flags;
    889 	struct vm_anon *anon;
    890 	int strat, free_list;
    891 {
    892 	int lcv, try1, try2, s, zeroit = 0;
    893 	struct vm_page *pg;
    894 	struct pglist *freeq;
    895 	struct pgfreelist *pgfl;
    896 	boolean_t use_reserve;
    897 
    898 	KASSERT(obj == NULL || anon == NULL);
    899 	KASSERT(off == trunc_page(off));
    900 
    901 	LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock));
    902 	LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock));
    903 
    904 	s = uvm_lock_fpageq();
    905 
    906 	/*
    907 	 * check to see if we need to generate some free pages waking
    908 	 * the pagedaemon.
    909 	 */
    910 
    911 	if (uvmexp.free + uvmexp.paging < uvmexp.freemin ||
    912 	    (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
    913 	     uvmexp.inactive < uvmexp.inactarg)) {
    914 		wakeup(&uvm.pagedaemon);
    915 	}
    916 
    917 	/*
    918 	 * fail if any of these conditions is true:
    919 	 * [1]  there really are no free pages, or
    920 	 * [2]  only kernel "reserved" pages remain and
    921 	 *        the page isn't being allocated to a kernel object.
    922 	 * [3]  only pagedaemon "reserved" pages remain and
    923 	 *        the requestor isn't the pagedaemon.
    924 	 */
    925 
    926 	use_reserve = (flags & UVM_PGA_USERESERVE) ||
    927 		(obj && UVM_OBJ_IS_KERN_OBJECT(obj));
    928 	if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
    929 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
    930 	     !(use_reserve && curproc == uvm.pagedaemon_proc)))
    931 		goto fail;
    932 
    933 #if PGFL_NQUEUES != 2
    934 #error uvm_pagealloc_strat needs to be updated
    935 #endif
    936 
    937 	/*
    938 	 * If we want a zero'd page, try the ZEROS queue first, otherwise
    939 	 * we try the UNKNOWN queue first.
    940 	 */
    941 	if (flags & UVM_PGA_ZERO) {
    942 		try1 = PGFL_ZEROS;
    943 		try2 = PGFL_UNKNOWN;
    944 	} else {
    945 		try1 = PGFL_UNKNOWN;
    946 		try2 = PGFL_ZEROS;
    947 	}
    948 
    949  again:
    950 	switch (strat) {
    951 	case UVM_PGA_STRAT_NORMAL:
    952 		/* Check all freelists in descending priority order. */
    953 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    954 			pgfl = &uvm.page_free[lcv];
    955 			if ((pg = TAILQ_FIRST((freeq =
    956 			      &pgfl->pgfl_queues[try1]))) != NULL ||
    957 			    (pg = TAILQ_FIRST((freeq =
    958 			      &pgfl->pgfl_queues[try2]))) != NULL)
    959 				goto gotit;
    960 		}
    961 
    962 		/* No pages free! */
    963 		goto fail;
    964 
    965 	case UVM_PGA_STRAT_ONLY:
    966 	case UVM_PGA_STRAT_FALLBACK:
    967 		/* Attempt to allocate from the specified free list. */
    968 		KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
    969 		pgfl = &uvm.page_free[free_list];
    970 		if ((pg = TAILQ_FIRST((freeq =
    971 		      &pgfl->pgfl_queues[try1]))) != NULL ||
    972 		    (pg = TAILQ_FIRST((freeq =
    973 		      &pgfl->pgfl_queues[try2]))) != NULL)
    974 			goto gotit;
    975 
    976 		/* Fall back, if possible. */
    977 		if (strat == UVM_PGA_STRAT_FALLBACK) {
    978 			strat = UVM_PGA_STRAT_NORMAL;
    979 			goto again;
    980 		}
    981 
    982 		/* No pages free! */
    983 		goto fail;
    984 
    985 	default:
    986 		panic("uvm_pagealloc_strat: bad strat %d", strat);
    987 		/* NOTREACHED */
    988 	}
    989 
    990  gotit:
    991 	TAILQ_REMOVE(freeq, pg, pageq);
    992 	uvmexp.free--;
    993 
    994 	/* update zero'd page count */
    995 	if (pg->flags & PG_ZERO)
    996 		uvmexp.zeropages--;
    997 
    998 	/*
    999 	 * update allocation statistics and remember if we have to
   1000 	 * zero the page
   1001 	 */
   1002 	if (flags & UVM_PGA_ZERO) {
   1003 		if (pg->flags & PG_ZERO) {
   1004 			uvmexp.pga_zerohit++;
   1005 			zeroit = 0;
   1006 		} else {
   1007 			uvmexp.pga_zeromiss++;
   1008 			zeroit = 1;
   1009 		}
   1010 	}
   1011 
   1012 	uvm_unlock_fpageq(s);		/* unlock free page queue */
   1013 
   1014 	pg->offset = off;
   1015 	pg->uobject = obj;
   1016 	pg->uanon = anon;
   1017 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
   1018 	pg->version++;
   1019 	if (anon) {
   1020 		anon->u.an_page = pg;
   1021 		pg->pqflags = PQ_ANON;
   1022 		uvmexp.anonpages++;
   1023 	} else {
   1024 		if (obj)
   1025 			uvm_pageinsert(pg);
   1026 		pg->pqflags = 0;
   1027 	}
   1028 #if defined(UVM_PAGE_TRKOWN)
   1029 	pg->owner_tag = NULL;
   1030 #endif
   1031 	UVM_PAGE_OWN(pg, "new alloc");
   1032 
   1033 	if (flags & UVM_PGA_ZERO) {
   1034 		/*
   1035 		 * A zero'd page is not clean.  If we got a page not already
   1036 		 * zero'd, then we have to zero it ourselves.
   1037 		 */
   1038 		pg->flags &= ~PG_CLEAN;
   1039 		if (zeroit)
   1040 			pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1041 	}
   1042 
   1043 	return(pg);
   1044 
   1045  fail:
   1046 	uvm_unlock_fpageq(s);
   1047 	return (NULL);
   1048 }
   1049 
   1050 /*
   1051  * uvm_pagerealloc: reallocate a page from one object to another
   1052  *
   1053  * => both objects must be locked
   1054  */
   1055 
   1056 void
   1057 uvm_pagerealloc(pg, newobj, newoff)
   1058 	struct vm_page *pg;
   1059 	struct uvm_object *newobj;
   1060 	voff_t newoff;
   1061 {
   1062 	/*
   1063 	 * remove it from the old object
   1064 	 */
   1065 
   1066 	if (pg->uobject) {
   1067 		uvm_pageremove(pg);
   1068 	}
   1069 
   1070 	/*
   1071 	 * put it in the new object
   1072 	 */
   1073 
   1074 	if (newobj) {
   1075 		pg->uobject = newobj;
   1076 		pg->offset = newoff;
   1077 		pg->version++;
   1078 		uvm_pageinsert(pg);
   1079 	}
   1080 }
   1081 
   1082 
   1083 /*
   1084  * uvm_pagefree: free page
   1085  *
   1086  * => erase page's identity (i.e. remove from hash/object)
   1087  * => put page on free list
   1088  * => caller must lock owning object (either anon or uvm_object)
   1089  * => caller must lock page queues
   1090  * => assumes all valid mappings of pg are gone
   1091  */
   1092 
   1093 void
   1094 uvm_pagefree(pg)
   1095 	struct vm_page *pg;
   1096 {
   1097 	int s;
   1098 	int saved_loan_count = pg->loan_count;
   1099 
   1100 #ifdef DEBUG
   1101 	if (pg->uobject == (void *)0xdeadbeef &&
   1102 	    pg->uanon == (void *)0xdeadbeef) {
   1103 		panic("uvm_pagefree: freeing free page %p\n", pg);
   1104 	}
   1105 #endif
   1106 
   1107 	/*
   1108 	 * if the page was an object page (and thus "TABLED"), remove it
   1109 	 * from the object.
   1110 	 */
   1111 
   1112 	if (pg->flags & PG_TABLED) {
   1113 
   1114 		/*
   1115 		 * if the object page is on loan we are going to drop ownership.
   1116 		 * it is possible that an anon will take over as owner for this
   1117 		 * page later on.   the anon will want a !PG_CLEAN page so that
   1118 		 * it knows it needs to allocate swap if it wants to page the
   1119 		 * page out.
   1120 		 */
   1121 
   1122 		if (saved_loan_count)
   1123 			pg->flags &= ~PG_CLEAN;	/* in case an anon takes over */
   1124 		uvm_pageremove(pg);
   1125 
   1126 		/*
   1127 		 * if our page was on loan, then we just lost control over it
   1128 		 * (in fact, if it was loaned to an anon, the anon may have
   1129 		 * already taken over ownership of the page by now and thus
   1130 		 * changed the loan_count [e.g. in uvmfault_anonget()]) we just
   1131 		 * return (when the last loan is dropped, then the page can be
   1132 		 * freed by whatever was holding the last loan).
   1133 		 */
   1134 
   1135 		if (saved_loan_count)
   1136 			return;
   1137 	} else if (saved_loan_count && (pg->pqflags & PQ_ANON)) {
   1138 
   1139 		/*
   1140 		 * if our page is owned by an anon and is loaned out to the
   1141 		 * kernel then we just want to drop ownership and return.
   1142 		 * the kernel must free the page when all its loans clear ...
   1143 		 * note that the kernel can't change the loan status of our
   1144 		 * page as long as we are holding PQ lock.
   1145 		 */
   1146 
   1147 		pg->pqflags &= ~PQ_ANON;
   1148 		pg->uanon = NULL;
   1149 		return;
   1150 	}
   1151 	KASSERT(saved_loan_count == 0);
   1152 
   1153 	/*
   1154 	 * now remove the page from the queues
   1155 	 */
   1156 
   1157 	if (pg->pqflags & PQ_ACTIVE) {
   1158 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
   1159 		pg->pqflags &= ~PQ_ACTIVE;
   1160 		uvmexp.active--;
   1161 	}
   1162 	if (pg->pqflags & PQ_INACTIVE) {
   1163 		if (pg->pqflags & PQ_SWAPBACKED)
   1164 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
   1165 		else
   1166 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
   1167 		pg->pqflags &= ~PQ_INACTIVE;
   1168 		uvmexp.inactive--;
   1169 	}
   1170 
   1171 	/*
   1172 	 * if the page was wired, unwire it now.
   1173 	 */
   1174 
   1175 	if (pg->wire_count) {
   1176 		pg->wire_count = 0;
   1177 		uvmexp.wired--;
   1178 	}
   1179 	if (pg->uanon) {
   1180 		uvmexp.anonpages--;
   1181 	}
   1182 
   1183 	/*
   1184 	 * and put on free queue
   1185 	 */
   1186 
   1187 	pg->flags &= ~PG_ZERO;
   1188 
   1189 	s = uvm_lock_fpageq();
   1190 	TAILQ_INSERT_TAIL(&uvm.page_free[
   1191 	    uvm_page_lookup_freelist(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq);
   1192 	pg->pqflags = PQ_FREE;
   1193 #ifdef DEBUG
   1194 	pg->uobject = (void *)0xdeadbeef;
   1195 	pg->offset = 0xdeadbeef;
   1196 	pg->uanon = (void *)0xdeadbeef;
   1197 #endif
   1198 	uvmexp.free++;
   1199 
   1200 	if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
   1201 		uvm.page_idle_zero = vm_page_zero_enable;
   1202 
   1203 	uvm_unlock_fpageq(s);
   1204 }
   1205 
   1206 /*
   1207  * uvm_page_unbusy: unbusy an array of pages.
   1208  *
   1209  * => pages must either all belong to the same object, or all belong to anons.
   1210  * => if pages are object-owned, object must be locked.
   1211  * => if pages are anon-owned, anons must be unlockd and have 0 refcount.
   1212  */
   1213 
   1214 void
   1215 uvm_page_unbusy(pgs, npgs)
   1216 	struct vm_page **pgs;
   1217 	int npgs;
   1218 {
   1219 	struct vm_page *pg;
   1220 	struct uvm_object *uobj;
   1221 	int i;
   1222 	UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
   1223 
   1224 	for (i = 0; i < npgs; i++) {
   1225 		pg = pgs[i];
   1226 
   1227 		if (pg == NULL) {
   1228 			continue;
   1229 		}
   1230 		if (pg->flags & PG_WANTED) {
   1231 			wakeup(pg);
   1232 		}
   1233 		if (pg->flags & PG_RELEASED) {
   1234 			UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
   1235 			uobj = pg->uobject;
   1236 			if (uobj != NULL) {
   1237 				uobj->pgops->pgo_releasepg(pg, NULL);
   1238 			} else {
   1239 				pg->flags &= ~(PG_BUSY);
   1240 				UVM_PAGE_OWN(pg, NULL);
   1241 				uvm_anfree(pg->uanon);
   1242 			}
   1243 		} else {
   1244 			UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
   1245 			KASSERT(pg->wire_count ||
   1246 				(pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)));
   1247 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1248 			UVM_PAGE_OWN(pg, NULL);
   1249 		}
   1250 	}
   1251 }
   1252 
   1253 #if defined(UVM_PAGE_TRKOWN)
   1254 /*
   1255  * uvm_page_own: set or release page ownership
   1256  *
   1257  * => this is a debugging function that keeps track of who sets PG_BUSY
   1258  *	and where they do it.   it can be used to track down problems
   1259  *	such a process setting "PG_BUSY" and never releasing it.
   1260  * => page's object [if any] must be locked
   1261  * => if "tag" is NULL then we are releasing page ownership
   1262  */
   1263 void
   1264 uvm_page_own(pg, tag)
   1265 	struct vm_page *pg;
   1266 	char *tag;
   1267 {
   1268 	/* gain ownership? */
   1269 	if (tag) {
   1270 		if (pg->owner_tag) {
   1271 			printf("uvm_page_own: page %p already owned "
   1272 			    "by proc %d [%s]\n", pg,
   1273 			     pg->owner, pg->owner_tag);
   1274 			panic("uvm_page_own");
   1275 		}
   1276 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
   1277 		pg->owner_tag = tag;
   1278 		return;
   1279 	}
   1280 
   1281 	/* drop ownership */
   1282 	if (pg->owner_tag == NULL) {
   1283 		printf("uvm_page_own: dropping ownership of an non-owned "
   1284 		    "page (%p)\n", pg);
   1285 		panic("uvm_page_own");
   1286 	}
   1287 	pg->owner_tag = NULL;
   1288 	return;
   1289 }
   1290 #endif
   1291 
   1292 /*
   1293  * uvm_pageidlezero: zero free pages while the system is idle.
   1294  *
   1295  * => we do at least one iteration per call, if we are below the target.
   1296  * => we loop until we either reach the target or whichqs indicates that
   1297  *	there is a process ready to run.
   1298  */
   1299 void
   1300 uvm_pageidlezero()
   1301 {
   1302 	struct vm_page *pg;
   1303 	struct pgfreelist *pgfl;
   1304 	int free_list, s;
   1305 
   1306 	do {
   1307 		s = uvm_lock_fpageq();
   1308 
   1309 		if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
   1310 			uvm.page_idle_zero = FALSE;
   1311 			uvm_unlock_fpageq(s);
   1312 			return;
   1313 		}
   1314 
   1315 		for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
   1316 			pgfl = &uvm.page_free[free_list];
   1317 			if ((pg = TAILQ_FIRST(&pgfl->pgfl_queues[
   1318 			    PGFL_UNKNOWN])) != NULL)
   1319 				break;
   1320 		}
   1321 
   1322 		if (pg == NULL) {
   1323 			/*
   1324 			 * No non-zero'd pages; don't bother trying again
   1325 			 * until we know we have non-zero'd pages free.
   1326 			 */
   1327 			uvm.page_idle_zero = FALSE;
   1328 			uvm_unlock_fpageq(s);
   1329 			return;
   1330 		}
   1331 
   1332 		TAILQ_REMOVE(&pgfl->pgfl_queues[PGFL_UNKNOWN], pg, pageq);
   1333 		uvmexp.free--;
   1334 		uvm_unlock_fpageq(s);
   1335 
   1336 #ifdef PMAP_PAGEIDLEZERO
   1337 		if (PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg)) == FALSE) {
   1338 			/*
   1339 			 * The machine-dependent code detected some
   1340 			 * reason for us to abort zeroing pages,
   1341 			 * probably because there is a process now
   1342 			 * ready to run.
   1343 			 */
   1344 			s = uvm_lock_fpageq();
   1345 			TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_UNKNOWN],
   1346 			    pg, pageq);
   1347 			uvmexp.free++;
   1348 			uvmexp.zeroaborts++;
   1349 			uvm_unlock_fpageq(s);
   1350 			return;
   1351 		}
   1352 #else
   1353 		/*
   1354 		 * XXX This will toast the cache unless the pmap_zero_page()
   1355 		 * XXX implementation does uncached access.
   1356 		 */
   1357 		pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1358 #endif
   1359 		pg->flags |= PG_ZERO;
   1360 
   1361 		s = uvm_lock_fpageq();
   1362 		TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_ZEROS], pg, pageq);
   1363 		uvmexp.free++;
   1364 		uvmexp.zeropages++;
   1365 		uvm_unlock_fpageq(s);
   1366 	} while (sched_whichqs == 0);
   1367 }
   1368