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