Home | History | Annotate | Line # | Download | only in uvm
uvm_page.c revision 1.150
      1 /*	$NetBSD: uvm_page.c,v 1.150 2009/08/18 18:06:53 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 <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: uvm_page.c,v 1.150 2009/08/18 18:06:53 thorpej Exp $");
     75 
     76 #include "opt_uvmhist.h"
     77 #include "opt_readahead.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/malloc.h>
     82 #include <sys/sched.h>
     83 #include <sys/kernel.h>
     84 #include <sys/vnode.h>
     85 #include <sys/proc.h>
     86 #include <sys/atomic.h>
     87 #include <sys/cpu.h>
     88 
     89 #include <uvm/uvm.h>
     90 #include <uvm/uvm_pdpolicy.h>
     91 
     92 /*
     93  * global vars... XXXCDC: move to uvm. structure.
     94  */
     95 
     96 /*
     97  * physical memory config is stored in vm_physmem.
     98  */
     99 
    100 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];	/* XXXCDC: uvm.physmem */
    101 int vm_nphysseg = 0;				/* XXXCDC: uvm.nphysseg */
    102 
    103 /*
    104  * Some supported CPUs in a given architecture don't support all
    105  * of the things necessary to do idle page zero'ing efficiently.
    106  * We therefore provide a way to disable it from machdep code here.
    107  */
    108 /*
    109  * XXX disabled until we can find a way to do this without causing
    110  * problems for either CPU caches or DMA latency.
    111  */
    112 bool vm_page_zero_enable = false;
    113 
    114 /*
    115  * number of pages per-CPU to reserve for the kernel.
    116  */
    117 int vm_page_reserve_kernel = 5;
    118 
    119 /*
    120  * physical memory size;
    121  */
    122 int physmem;
    123 
    124 /*
    125  * local variables
    126  */
    127 
    128 /*
    129  * these variables record the values returned by vm_page_bootstrap,
    130  * for debugging purposes.  The implementation of uvm_pageboot_alloc
    131  * and pmap_startup here also uses them internally.
    132  */
    133 
    134 static vaddr_t      virtual_space_start;
    135 static vaddr_t      virtual_space_end;
    136 
    137 /*
    138  * we allocate an initial number of page colors in uvm_page_init(),
    139  * and remember them.  We may re-color pages as cache sizes are
    140  * discovered during the autoconfiguration phase.  But we can never
    141  * free the initial set of buckets, since they are allocated using
    142  * uvm_pageboot_alloc().
    143  */
    144 
    145 static bool have_recolored_pages /* = false */;
    146 
    147 MALLOC_DEFINE(M_VMPAGE, "VM page", "VM page");
    148 
    149 #ifdef DEBUG
    150 vaddr_t uvm_zerocheckkva;
    151 #endif /* DEBUG */
    152 
    153 /*
    154  * local prototypes
    155  */
    156 
    157 static void uvm_pageinsert(struct vm_page *);
    158 static void uvm_pageremove(struct vm_page *);
    159 
    160 /*
    161  * per-object tree of pages
    162  */
    163 
    164 static signed int
    165 uvm_page_compare_nodes(const struct rb_node *n1, const struct rb_node *n2)
    166 {
    167 	const struct vm_page *pg1 = (const void *)n1;
    168 	const struct vm_page *pg2 = (const void *)n2;
    169 	const voff_t a = pg1->offset;
    170 	const voff_t b = pg2->offset;
    171 
    172 	if (a < b)
    173 		return 1;
    174 	if (a > b)
    175 		return -1;
    176 	return 0;
    177 }
    178 
    179 static signed int
    180 uvm_page_compare_key(const struct rb_node *n, const void *key)
    181 {
    182 	const struct vm_page *pg = (const void *)n;
    183 	const voff_t a = pg->offset;
    184 	const voff_t b = *(const voff_t *)key;
    185 
    186 	if (a < b)
    187 		return 1;
    188 	if (a > b)
    189 		return -1;
    190 	return 0;
    191 }
    192 
    193 const struct rb_tree_ops uvm_page_tree_ops = {
    194 	.rbto_compare_nodes = uvm_page_compare_nodes,
    195 	.rbto_compare_key = uvm_page_compare_key,
    196 };
    197 
    198 /*
    199  * inline functions
    200  */
    201 
    202 /*
    203  * uvm_pageinsert: insert a page in the object.
    204  *
    205  * => caller must lock object
    206  * => caller must lock page queues
    207  * => call should have already set pg's object and offset pointers
    208  *    and bumped the version counter
    209  */
    210 
    211 static inline void
    212 uvm_pageinsert_list(struct uvm_object *uobj, struct vm_page *pg,
    213     struct vm_page *where)
    214 {
    215 
    216 	KASSERT(uobj == pg->uobject);
    217 	KASSERT(mutex_owned(&uobj->vmobjlock));
    218 	KASSERT((pg->flags & PG_TABLED) == 0);
    219 	KASSERT(where == NULL || (where->flags & PG_TABLED));
    220 	KASSERT(where == NULL || (where->uobject == uobj));
    221 
    222 	if (UVM_OBJ_IS_VNODE(uobj)) {
    223 		if (uobj->uo_npages == 0) {
    224 			struct vnode *vp = (struct vnode *)uobj;
    225 
    226 			vholdl(vp);
    227 		}
    228 		if (UVM_OBJ_IS_VTEXT(uobj)) {
    229 			atomic_inc_uint(&uvmexp.execpages);
    230 		} else {
    231 			atomic_inc_uint(&uvmexp.filepages);
    232 		}
    233 	} else if (UVM_OBJ_IS_AOBJ(uobj)) {
    234 		atomic_inc_uint(&uvmexp.anonpages);
    235 	}
    236 
    237 	if (where)
    238 		TAILQ_INSERT_AFTER(&uobj->memq, where, pg, listq.queue);
    239 	else
    240 		TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    241 	pg->flags |= PG_TABLED;
    242 	uobj->uo_npages++;
    243 }
    244 
    245 
    246 static inline void
    247 uvm_pageinsert_tree(struct uvm_object *uobj, struct vm_page *pg)
    248 {
    249 	bool success;
    250 
    251 	KASSERT(uobj == pg->uobject);
    252 	success = rb_tree_insert_node(&uobj->rb_tree, &pg->rb_node);
    253 	KASSERT(success);
    254 }
    255 
    256 static inline void
    257 uvm_pageinsert(struct vm_page *pg)
    258 {
    259 	struct uvm_object *uobj = pg->uobject;
    260 
    261 	uvm_pageinsert_tree(uobj, pg);
    262 	uvm_pageinsert_list(uobj, pg, NULL);
    263 }
    264 
    265 /*
    266  * uvm_page_remove: remove page from object.
    267  *
    268  * => caller must lock object
    269  * => caller must lock page queues
    270  */
    271 
    272 static inline void
    273 uvm_pageremove_list(struct uvm_object *uobj, struct vm_page *pg)
    274 {
    275 
    276 	KASSERT(uobj == pg->uobject);
    277 	KASSERT(mutex_owned(&uobj->vmobjlock));
    278 	KASSERT(pg->flags & PG_TABLED);
    279 
    280 	if (UVM_OBJ_IS_VNODE(uobj)) {
    281 		if (uobj->uo_npages == 1) {
    282 			struct vnode *vp = (struct vnode *)uobj;
    283 
    284 			holdrelel(vp);
    285 		}
    286 		if (UVM_OBJ_IS_VTEXT(uobj)) {
    287 			atomic_dec_uint(&uvmexp.execpages);
    288 		} else {
    289 			atomic_dec_uint(&uvmexp.filepages);
    290 		}
    291 	} else if (UVM_OBJ_IS_AOBJ(uobj)) {
    292 		atomic_dec_uint(&uvmexp.anonpages);
    293 	}
    294 
    295 	/* object should be locked */
    296 	uobj->uo_npages--;
    297 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    298 	pg->flags &= ~PG_TABLED;
    299 	pg->uobject = NULL;
    300 }
    301 
    302 static inline void
    303 uvm_pageremove_tree(struct uvm_object *uobj, struct vm_page *pg)
    304 {
    305 
    306 	KASSERT(uobj == pg->uobject);
    307 	rb_tree_remove_node(&uobj->rb_tree, &pg->rb_node);
    308 }
    309 
    310 static inline void
    311 uvm_pageremove(struct vm_page *pg)
    312 {
    313 	struct uvm_object *uobj = pg->uobject;
    314 
    315 	uvm_pageremove_tree(uobj, pg);
    316 	uvm_pageremove_list(uobj, pg);
    317 }
    318 
    319 static void
    320 uvm_page_init_buckets(struct pgfreelist *pgfl)
    321 {
    322 	int color, i;
    323 
    324 	for (color = 0; color < uvmexp.ncolors; color++) {
    325 		for (i = 0; i < PGFL_NQUEUES; i++) {
    326 			LIST_INIT(&pgfl->pgfl_buckets[color].pgfl_queues[i]);
    327 		}
    328 	}
    329 }
    330 
    331 /*
    332  * uvm_page_init: init the page system.   called from uvm_init().
    333  *
    334  * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
    335  */
    336 
    337 void
    338 uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp)
    339 {
    340 	vsize_t freepages, pagecount, bucketcount, n;
    341 	struct pgflbucket *bucketarray, *cpuarray;
    342 	struct vm_page *pagearray;
    343 	int lcv;
    344 	u_int i;
    345 	paddr_t paddr;
    346 
    347 	KASSERT(ncpu <= 1);
    348 	CTASSERT(sizeof(pagearray->offset) >= sizeof(struct uvm_cpu *));
    349 
    350 	/*
    351 	 * init the page queues and page queue locks, except the free
    352 	 * list; we allocate that later (with the initial vm_page
    353 	 * structures).
    354 	 */
    355 
    356 	curcpu()->ci_data.cpu_uvm = &uvm.cpus[0];
    357 	uvm_reclaim_init();
    358 	uvmpdpol_init();
    359 	mutex_init(&uvm_pageqlock, MUTEX_DRIVER, IPL_NONE);
    360 	mutex_init(&uvm_fpageqlock, MUTEX_DRIVER, IPL_VM);
    361 
    362 	/*
    363 	 * allocate vm_page structures.
    364 	 */
    365 
    366 	/*
    367 	 * sanity check:
    368 	 * before calling this function the MD code is expected to register
    369 	 * some free RAM with the uvm_page_physload() function.   our job
    370 	 * now is to allocate vm_page structures for this memory.
    371 	 */
    372 
    373 	if (vm_nphysseg == 0)
    374 		panic("uvm_page_bootstrap: no memory pre-allocated");
    375 
    376 	/*
    377 	 * first calculate the number of free pages...
    378 	 *
    379 	 * note that we use start/end rather than avail_start/avail_end.
    380 	 * this allows us to allocate extra vm_page structures in case we
    381 	 * want to return some memory to the pool after booting.
    382 	 */
    383 
    384 	freepages = 0;
    385 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    386 		freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
    387 
    388 	/*
    389 	 * Let MD code initialize the number of colors, or default
    390 	 * to 1 color if MD code doesn't care.
    391 	 */
    392 	if (uvmexp.ncolors == 0)
    393 		uvmexp.ncolors = 1;
    394 	uvmexp.colormask = uvmexp.ncolors - 1;
    395 
    396 	/*
    397 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
    398 	 * use.   for each page of memory we use we need a vm_page structure.
    399 	 * thus, the total number of pages we can use is the total size of
    400 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
    401 	 * structure.   we add one to freepages as a fudge factor to avoid
    402 	 * truncation errors (since we can only allocate in terms of whole
    403 	 * pages).
    404 	 */
    405 
    406 	bucketcount = uvmexp.ncolors * VM_NFREELIST;
    407 	pagecount = ((freepages + 1) << PAGE_SHIFT) /
    408 	    (PAGE_SIZE + sizeof(struct vm_page));
    409 
    410 	bucketarray = (void *)uvm_pageboot_alloc((bucketcount *
    411 	    sizeof(struct pgflbucket) * 2) + (pagecount *
    412 	    sizeof(struct vm_page)));
    413 	cpuarray = bucketarray + bucketcount;
    414 	pagearray = (struct vm_page *)(bucketarray + bucketcount * 2);
    415 
    416 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    417 		uvm.page_free[lcv].pgfl_buckets =
    418 		    (bucketarray + (lcv * uvmexp.ncolors));
    419 		uvm_page_init_buckets(&uvm.page_free[lcv]);
    420 		uvm.cpus[0].page_free[lcv].pgfl_buckets =
    421 		    (cpuarray + (lcv * uvmexp.ncolors));
    422 		uvm_page_init_buckets(&uvm.cpus[0].page_free[lcv]);
    423 	}
    424 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
    425 
    426 	/*
    427 	 * init the vm_page structures and put them in the correct place.
    428 	 */
    429 
    430 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    431 		n = vm_physmem[lcv].end - vm_physmem[lcv].start;
    432 
    433 		/* set up page array pointers */
    434 		vm_physmem[lcv].pgs = pagearray;
    435 		pagearray += n;
    436 		pagecount -= n;
    437 		vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
    438 
    439 		/* init and free vm_pages (we've already zeroed them) */
    440 		paddr = ptoa(vm_physmem[lcv].start);
    441 		for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
    442 			vm_physmem[lcv].pgs[i].phys_addr = paddr;
    443 #ifdef __HAVE_VM_PAGE_MD
    444 			VM_MDPAGE_INIT(&vm_physmem[lcv].pgs[i]);
    445 #endif
    446 			if (atop(paddr) >= vm_physmem[lcv].avail_start &&
    447 			    atop(paddr) <= vm_physmem[lcv].avail_end) {
    448 				uvmexp.npages++;
    449 				/* add page to free pool */
    450 				uvm_pagefree(&vm_physmem[lcv].pgs[i]);
    451 			}
    452 		}
    453 	}
    454 
    455 	/*
    456 	 * pass up the values of virtual_space_start and
    457 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
    458 	 * layers of the VM.
    459 	 */
    460 
    461 	*kvm_startp = round_page(virtual_space_start);
    462 	*kvm_endp = trunc_page(virtual_space_end);
    463 #ifdef DEBUG
    464 	/*
    465 	 * steal kva for uvm_pagezerocheck().
    466 	 */
    467 	uvm_zerocheckkva = *kvm_startp;
    468 	*kvm_startp += PAGE_SIZE;
    469 #endif /* DEBUG */
    470 
    471 	/*
    472 	 * init various thresholds.
    473 	 */
    474 
    475 	uvmexp.reserve_pagedaemon = 1;
    476 	uvmexp.reserve_kernel = vm_page_reserve_kernel;
    477 
    478 	/*
    479 	 * determine if we should zero pages in the idle loop.
    480 	 */
    481 
    482 	uvm.cpus[0].page_idle_zero = vm_page_zero_enable;
    483 
    484 	/*
    485 	 * done!
    486 	 */
    487 
    488 	uvm.page_init_done = true;
    489 }
    490 
    491 /*
    492  * uvm_setpagesize: set the page size
    493  *
    494  * => sets page_shift and page_mask from uvmexp.pagesize.
    495  */
    496 
    497 void
    498 uvm_setpagesize(void)
    499 {
    500 
    501 	/*
    502 	 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE
    503 	 * to be a constant (indicated by being a non-zero value).
    504 	 */
    505 	if (uvmexp.pagesize == 0) {
    506 		if (PAGE_SIZE == 0)
    507 			panic("uvm_setpagesize: uvmexp.pagesize not set");
    508 		uvmexp.pagesize = PAGE_SIZE;
    509 	}
    510 	uvmexp.pagemask = uvmexp.pagesize - 1;
    511 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
    512 		panic("uvm_setpagesize: page size not a power of two");
    513 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
    514 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
    515 			break;
    516 }
    517 
    518 /*
    519  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
    520  */
    521 
    522 vaddr_t
    523 uvm_pageboot_alloc(vsize_t size)
    524 {
    525 	static bool initialized = false;
    526 	vaddr_t addr;
    527 #if !defined(PMAP_STEAL_MEMORY)
    528 	vaddr_t vaddr;
    529 	paddr_t paddr;
    530 #endif
    531 
    532 	/*
    533 	 * on first call to this function, initialize ourselves.
    534 	 */
    535 	if (initialized == false) {
    536 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
    537 
    538 		/* round it the way we like it */
    539 		virtual_space_start = round_page(virtual_space_start);
    540 		virtual_space_end = trunc_page(virtual_space_end);
    541 
    542 		initialized = true;
    543 	}
    544 
    545 	/* round to page size */
    546 	size = round_page(size);
    547 
    548 #if defined(PMAP_STEAL_MEMORY)
    549 
    550 	/*
    551 	 * defer bootstrap allocation to MD code (it may want to allocate
    552 	 * from a direct-mapped segment).  pmap_steal_memory should adjust
    553 	 * virtual_space_start/virtual_space_end if necessary.
    554 	 */
    555 
    556 	addr = pmap_steal_memory(size, &virtual_space_start,
    557 	    &virtual_space_end);
    558 
    559 	return(addr);
    560 
    561 #else /* !PMAP_STEAL_MEMORY */
    562 
    563 	/*
    564 	 * allocate virtual memory for this request
    565 	 */
    566 	if (virtual_space_start == virtual_space_end ||
    567 	    (virtual_space_end - virtual_space_start) < size)
    568 		panic("uvm_pageboot_alloc: out of virtual space");
    569 
    570 	addr = virtual_space_start;
    571 
    572 #ifdef PMAP_GROWKERNEL
    573 	/*
    574 	 * If the kernel pmap can't map the requested space,
    575 	 * then allocate more resources for it.
    576 	 */
    577 	if (uvm_maxkaddr < (addr + size)) {
    578 		uvm_maxkaddr = pmap_growkernel(addr + size);
    579 		if (uvm_maxkaddr < (addr + size))
    580 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
    581 	}
    582 #endif
    583 
    584 	virtual_space_start += size;
    585 
    586 	/*
    587 	 * allocate and mapin physical pages to back new virtual pages
    588 	 */
    589 
    590 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
    591 	    vaddr += PAGE_SIZE) {
    592 
    593 		if (!uvm_page_physget(&paddr))
    594 			panic("uvm_pageboot_alloc: out of memory");
    595 
    596 		/*
    597 		 * Note this memory is no longer managed, so using
    598 		 * pmap_kenter is safe.
    599 		 */
    600 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
    601 	}
    602 	pmap_update(pmap_kernel());
    603 	return(addr);
    604 #endif	/* PMAP_STEAL_MEMORY */
    605 }
    606 
    607 #if !defined(PMAP_STEAL_MEMORY)
    608 /*
    609  * uvm_page_physget: "steal" one page from the vm_physmem structure.
    610  *
    611  * => attempt to allocate it off the end of a segment in which the "avail"
    612  *    values match the start/end values.   if we can't do that, then we
    613  *    will advance both values (making them equal, and removing some
    614  *    vm_page structures from the non-avail area).
    615  * => return false if out of memory.
    616  */
    617 
    618 /* subroutine: try to allocate from memory chunks on the specified freelist */
    619 static bool uvm_page_physget_freelist(paddr_t *, int);
    620 
    621 static bool
    622 uvm_page_physget_freelist(paddr_t *paddrp, int freelist)
    623 {
    624 	int lcv, x;
    625 
    626 	/* pass 1: try allocating from a matching end */
    627 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    628 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    629 #else
    630 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    631 #endif
    632 	{
    633 
    634 		if (uvm.page_init_done == true)
    635 			panic("uvm_page_physget: called _after_ bootstrap");
    636 
    637 		if (vm_physmem[lcv].free_list != freelist)
    638 			continue;
    639 
    640 		/* try from front */
    641 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
    642 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    643 			*paddrp = ptoa(vm_physmem[lcv].avail_start);
    644 			vm_physmem[lcv].avail_start++;
    645 			vm_physmem[lcv].start++;
    646 			/* nothing left?   nuke it */
    647 			if (vm_physmem[lcv].avail_start ==
    648 			    vm_physmem[lcv].end) {
    649 				if (vm_nphysseg == 1)
    650 				    panic("uvm_page_physget: out of memory!");
    651 				vm_nphysseg--;
    652 				for (x = lcv ; x < vm_nphysseg ; x++)
    653 					/* structure copy */
    654 					vm_physmem[x] = vm_physmem[x+1];
    655 			}
    656 			return (true);
    657 		}
    658 
    659 		/* try from rear */
    660 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
    661 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    662 			*paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
    663 			vm_physmem[lcv].avail_end--;
    664 			vm_physmem[lcv].end--;
    665 			/* nothing left?   nuke it */
    666 			if (vm_physmem[lcv].avail_end ==
    667 			    vm_physmem[lcv].start) {
    668 				if (vm_nphysseg == 1)
    669 				    panic("uvm_page_physget: out of memory!");
    670 				vm_nphysseg--;
    671 				for (x = lcv ; x < vm_nphysseg ; x++)
    672 					/* structure copy */
    673 					vm_physmem[x] = vm_physmem[x+1];
    674 			}
    675 			return (true);
    676 		}
    677 	}
    678 
    679 	/* pass2: forget about matching ends, just allocate something */
    680 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    681 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    682 #else
    683 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    684 #endif
    685 	{
    686 
    687 		/* any room in this bank? */
    688 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
    689 			continue;  /* nope */
    690 
    691 		*paddrp = ptoa(vm_physmem[lcv].avail_start);
    692 		vm_physmem[lcv].avail_start++;
    693 		/* truncate! */
    694 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
    695 
    696 		/* nothing left?   nuke it */
    697 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
    698 			if (vm_nphysseg == 1)
    699 				panic("uvm_page_physget: out of memory!");
    700 			vm_nphysseg--;
    701 			for (x = lcv ; x < vm_nphysseg ; x++)
    702 				/* structure copy */
    703 				vm_physmem[x] = vm_physmem[x+1];
    704 		}
    705 		return (true);
    706 	}
    707 
    708 	return (false);        /* whoops! */
    709 }
    710 
    711 bool
    712 uvm_page_physget(paddr_t *paddrp)
    713 {
    714 	int i;
    715 
    716 	/* try in the order of freelist preference */
    717 	for (i = 0; i < VM_NFREELIST; i++)
    718 		if (uvm_page_physget_freelist(paddrp, i) == true)
    719 			return (true);
    720 	return (false);
    721 }
    722 #endif /* PMAP_STEAL_MEMORY */
    723 
    724 /*
    725  * uvm_page_physload: load physical memory into VM system
    726  *
    727  * => all args are PFs
    728  * => all pages in start/end get vm_page structures
    729  * => areas marked by avail_start/avail_end get added to the free page pool
    730  * => we are limited to VM_PHYSSEG_MAX physical memory segments
    731  */
    732 
    733 void
    734 uvm_page_physload(paddr_t start, paddr_t end, paddr_t avail_start,
    735     paddr_t avail_end, int free_list)
    736 {
    737 	int preload, lcv;
    738 	psize_t npages;
    739 	struct vm_page *pgs;
    740 	struct vm_physseg *ps;
    741 
    742 	if (uvmexp.pagesize == 0)
    743 		panic("uvm_page_physload: page size not set!");
    744 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
    745 		panic("uvm_page_physload: bad free list %d", free_list);
    746 	if (start >= end)
    747 		panic("uvm_page_physload: start >= end");
    748 
    749 	/*
    750 	 * do we have room?
    751 	 */
    752 
    753 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
    754 		printf("uvm_page_physload: unable to load physical memory "
    755 		    "segment\n");
    756 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
    757 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
    758 		printf("\tincrease VM_PHYSSEG_MAX\n");
    759 		return;
    760 	}
    761 
    762 	/*
    763 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
    764 	 * called yet, so malloc is not available).
    765 	 */
    766 
    767 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    768 		if (vm_physmem[lcv].pgs)
    769 			break;
    770 	}
    771 	preload = (lcv == vm_nphysseg);
    772 
    773 	/*
    774 	 * if VM is already running, attempt to malloc() vm_page structures
    775 	 */
    776 
    777 	if (!preload) {
    778 #if defined(VM_PHYSSEG_NOADD)
    779 		panic("uvm_page_physload: tried to add RAM after vm_mem_init");
    780 #else
    781 		/* XXXCDC: need some sort of lockout for this case */
    782 		paddr_t paddr;
    783 		npages = end - start;  /* # of pages */
    784 		pgs = malloc(sizeof(struct vm_page) * npages,
    785 		    M_VMPAGE, M_NOWAIT);
    786 		if (pgs == NULL) {
    787 			printf("uvm_page_physload: can not malloc vm_page "
    788 			    "structs for segment\n");
    789 			printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
    790 			return;
    791 		}
    792 		/* zero data, init phys_addr and free_list, and free pages */
    793 		memset(pgs, 0, sizeof(struct vm_page) * npages);
    794 		for (lcv = 0, paddr = ptoa(start) ;
    795 				 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
    796 			pgs[lcv].phys_addr = paddr;
    797 			pgs[lcv].free_list = free_list;
    798 			if (atop(paddr) >= avail_start &&
    799 			    atop(paddr) <= avail_end)
    800 				uvm_pagefree(&pgs[lcv]);
    801 		}
    802 		/* XXXCDC: incomplete: need to update uvmexp.free, what else? */
    803 		/* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
    804 #endif
    805 	} else {
    806 		pgs = NULL;
    807 		npages = 0;
    808 	}
    809 
    810 	/*
    811 	 * now insert us in the proper place in vm_physmem[]
    812 	 */
    813 
    814 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
    815 	/* random: put it at the end (easy!) */
    816 	ps = &vm_physmem[vm_nphysseg];
    817 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
    818 	{
    819 		int x;
    820 		/* sort by address for binary search */
    821 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    822 			if (start < vm_physmem[lcv].start)
    823 				break;
    824 		ps = &vm_physmem[lcv];
    825 		/* move back other entries, if necessary ... */
    826 		for (x = vm_nphysseg ; x > lcv ; x--)
    827 			/* structure copy */
    828 			vm_physmem[x] = vm_physmem[x - 1];
    829 	}
    830 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    831 	{
    832 		int x;
    833 		/* sort by largest segment first */
    834 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    835 			if ((end - start) >
    836 			    (vm_physmem[lcv].end - vm_physmem[lcv].start))
    837 				break;
    838 		ps = &vm_physmem[lcv];
    839 		/* move back other entries, if necessary ... */
    840 		for (x = vm_nphysseg ; x > lcv ; x--)
    841 			/* structure copy */
    842 			vm_physmem[x] = vm_physmem[x - 1];
    843 	}
    844 #else
    845 	panic("uvm_page_physload: unknown physseg strategy selected!");
    846 #endif
    847 
    848 	ps->start = start;
    849 	ps->end = end;
    850 	ps->avail_start = avail_start;
    851 	ps->avail_end = avail_end;
    852 	if (preload) {
    853 		ps->pgs = NULL;
    854 	} else {
    855 		ps->pgs = pgs;
    856 		ps->lastpg = pgs + npages - 1;
    857 	}
    858 	ps->free_list = free_list;
    859 	vm_nphysseg++;
    860 
    861 	if (!preload) {
    862 		uvmpdpol_reinit();
    863 	}
    864 }
    865 
    866 /*
    867  * uvm_page_recolor: Recolor the pages if the new bucket count is
    868  * larger than the old one.
    869  */
    870 
    871 void
    872 uvm_page_recolor(int newncolors)
    873 {
    874 	struct pgflbucket *bucketarray, *cpuarray, *oldbucketarray;
    875 	struct pgfreelist gpgfl, pgfl;
    876 	struct vm_page *pg;
    877 	vsize_t bucketcount;
    878 	int lcv, color, i, ocolors;
    879 	struct uvm_cpu *ucpu;
    880 
    881 	if (newncolors <= uvmexp.ncolors)
    882 		return;
    883 
    884 	if (uvm.page_init_done == false) {
    885 		uvmexp.ncolors = newncolors;
    886 		return;
    887 	}
    888 
    889 	bucketcount = newncolors * VM_NFREELIST;
    890 	bucketarray = malloc(bucketcount * sizeof(struct pgflbucket) * 2,
    891 	    M_VMPAGE, M_NOWAIT);
    892 	cpuarray = bucketarray + bucketcount;
    893 	if (bucketarray == NULL) {
    894 		printf("WARNING: unable to allocate %ld page color buckets\n",
    895 		    (long) bucketcount);
    896 		return;
    897 	}
    898 
    899 	mutex_spin_enter(&uvm_fpageqlock);
    900 
    901 	/* Make sure we should still do this. */
    902 	if (newncolors <= uvmexp.ncolors) {
    903 		mutex_spin_exit(&uvm_fpageqlock);
    904 		free(bucketarray, M_VMPAGE);
    905 		return;
    906 	}
    907 
    908 	oldbucketarray = uvm.page_free[0].pgfl_buckets;
    909 	ocolors = uvmexp.ncolors;
    910 
    911 	uvmexp.ncolors = newncolors;
    912 	uvmexp.colormask = uvmexp.ncolors - 1;
    913 
    914 	ucpu = curcpu()->ci_data.cpu_uvm;
    915 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    916 		gpgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
    917 		pgfl.pgfl_buckets = (cpuarray + (lcv * uvmexp.ncolors));
    918 		uvm_page_init_buckets(&gpgfl);
    919 		uvm_page_init_buckets(&pgfl);
    920 		for (color = 0; color < ocolors; color++) {
    921 			for (i = 0; i < PGFL_NQUEUES; i++) {
    922 				while ((pg = LIST_FIRST(&uvm.page_free[
    923 				    lcv].pgfl_buckets[color].pgfl_queues[i]))
    924 				    != NULL) {
    925 					LIST_REMOVE(pg, pageq.list); /* global */
    926 					LIST_REMOVE(pg, listq.list); /* cpu */
    927 					LIST_INSERT_HEAD(&gpgfl.pgfl_buckets[
    928 					    VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
    929 					    i], pg, pageq.list);
    930 					LIST_INSERT_HEAD(&pgfl.pgfl_buckets[
    931 					    VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
    932 					    i], pg, listq.list);
    933 				}
    934 			}
    935 		}
    936 		uvm.page_free[lcv].pgfl_buckets = gpgfl.pgfl_buckets;
    937 		ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
    938 	}
    939 
    940 	if (have_recolored_pages) {
    941 		mutex_spin_exit(&uvm_fpageqlock);
    942 		free(oldbucketarray, M_VMPAGE);
    943 		return;
    944 	}
    945 
    946 	have_recolored_pages = true;
    947 	mutex_spin_exit(&uvm_fpageqlock);
    948 }
    949 
    950 /*
    951  * uvm_cpu_attach: initialize per-CPU data structures.
    952  */
    953 
    954 void
    955 uvm_cpu_attach(struct cpu_info *ci)
    956 {
    957 	struct pgflbucket *bucketarray;
    958 	struct pgfreelist pgfl;
    959 	struct uvm_cpu *ucpu;
    960 	vsize_t bucketcount;
    961 	int lcv;
    962 
    963 	if (CPU_IS_PRIMARY(ci)) {
    964 		/* Already done in uvm_page_init(). */
    965 		return;
    966 	}
    967 
    968 	/* Add more reserve pages for this CPU. */
    969 	uvmexp.reserve_kernel += vm_page_reserve_kernel;
    970 
    971 	/* Configure this CPU's free lists. */
    972 	bucketcount = uvmexp.ncolors * VM_NFREELIST;
    973 	bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
    974 	    M_VMPAGE, M_WAITOK);
    975 	ucpu = &uvm.cpus[cpu_index(ci)];
    976 	ci->ci_data.cpu_uvm = ucpu;
    977 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    978 		pgfl.pgfl_buckets = (bucketarray + (lcv * uvmexp.ncolors));
    979 		uvm_page_init_buckets(&pgfl);
    980 		ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
    981 	}
    982 }
    983 
    984 /*
    985  * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
    986  */
    987 
    988 static struct vm_page *
    989 uvm_pagealloc_pgfl(struct uvm_cpu *ucpu, int flist, int try1, int try2,
    990     int *trycolorp)
    991 {
    992 	struct pgflist *freeq;
    993 	struct vm_page *pg;
    994 	int color, trycolor = *trycolorp;
    995 	struct pgfreelist *gpgfl, *pgfl;
    996 
    997 	KASSERT(mutex_owned(&uvm_fpageqlock));
    998 
    999 	color = trycolor;
   1000 	pgfl = &ucpu->page_free[flist];
   1001 	gpgfl = &uvm.page_free[flist];
   1002 	do {
   1003 		/* cpu, try1 */
   1004 		if ((pg = LIST_FIRST((freeq =
   1005 		    &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) {
   1006 			VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--;
   1007 		    	uvmexp.cpuhit++;
   1008 			goto gotit;
   1009 		}
   1010 		/* global, try1 */
   1011 		if ((pg = LIST_FIRST((freeq =
   1012 		    &gpgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) {
   1013 			VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--;
   1014 		    	uvmexp.cpumiss++;
   1015 			goto gotit;
   1016 		}
   1017 		/* cpu, try2 */
   1018 		if ((pg = LIST_FIRST((freeq =
   1019 		    &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) {
   1020 			VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--;
   1021 		    	uvmexp.cpuhit++;
   1022 			goto gotit;
   1023 		}
   1024 		/* global, try2 */
   1025 		if ((pg = LIST_FIRST((freeq =
   1026 		    &gpgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) {
   1027 			VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--;
   1028 		    	uvmexp.cpumiss++;
   1029 			goto gotit;
   1030 		}
   1031 		color = (color + 1) & uvmexp.colormask;
   1032 	} while (color != trycolor);
   1033 
   1034 	return (NULL);
   1035 
   1036  gotit:
   1037 	LIST_REMOVE(pg, pageq.list);	/* global list */
   1038 	LIST_REMOVE(pg, listq.list);	/* per-cpu list */
   1039 	uvmexp.free--;
   1040 
   1041 	/* update zero'd page count */
   1042 	if (pg->flags & PG_ZERO)
   1043 		uvmexp.zeropages--;
   1044 
   1045 	if (color == trycolor)
   1046 		uvmexp.colorhit++;
   1047 	else {
   1048 		uvmexp.colormiss++;
   1049 		*trycolorp = color;
   1050 	}
   1051 
   1052 	return (pg);
   1053 }
   1054 
   1055 /*
   1056  * uvm_pagealloc_strat: allocate vm_page from a particular free list.
   1057  *
   1058  * => return null if no pages free
   1059  * => wake up pagedaemon if number of free pages drops below low water mark
   1060  * => if obj != NULL, obj must be locked (to put in obj's tree)
   1061  * => if anon != NULL, anon must be locked (to put in anon)
   1062  * => only one of obj or anon can be non-null
   1063  * => caller must activate/deactivate page if it is not wired.
   1064  * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
   1065  * => policy decision: it is more important to pull a page off of the
   1066  *	appropriate priority free list than it is to get a zero'd or
   1067  *	unknown contents page.  This is because we live with the
   1068  *	consequences of a bad free list decision for the entire
   1069  *	lifetime of the page, e.g. if the page comes from memory that
   1070  *	is slower to access.
   1071  */
   1072 
   1073 struct vm_page *
   1074 uvm_pagealloc_strat(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
   1075     int flags, int strat, int free_list)
   1076 {
   1077 	int lcv, try1, try2, zeroit = 0, color;
   1078 	struct uvm_cpu *ucpu;
   1079 	struct vm_page *pg;
   1080 	lwp_t *l;
   1081 
   1082 	KASSERT(obj == NULL || anon == NULL);
   1083 	KASSERT(anon == NULL || off == 0);
   1084 	KASSERT(off == trunc_page(off));
   1085 	KASSERT(obj == NULL || mutex_owned(&obj->vmobjlock));
   1086 	KASSERT(anon == NULL || mutex_owned(&anon->an_lock));
   1087 
   1088 	mutex_spin_enter(&uvm_fpageqlock);
   1089 
   1090 	/*
   1091 	 * This implements a global round-robin page coloring
   1092 	 * algorithm.
   1093 	 *
   1094 	 * XXXJRT: What about virtually-indexed caches?
   1095 	 */
   1096 
   1097 	ucpu = curcpu()->ci_data.cpu_uvm;
   1098 	color = ucpu->page_free_nextcolor;
   1099 
   1100 	/*
   1101 	 * check to see if we need to generate some free pages waking
   1102 	 * the pagedaemon.
   1103 	 */
   1104 
   1105 	uvm_kick_pdaemon();
   1106 
   1107 	/*
   1108 	 * fail if any of these conditions is true:
   1109 	 * [1]  there really are no free pages, or
   1110 	 * [2]  only kernel "reserved" pages remain and
   1111 	 *        reserved pages have not been requested.
   1112 	 * [3]  only pagedaemon "reserved" pages remain and
   1113 	 *        the requestor isn't the pagedaemon.
   1114 	 * we make kernel reserve pages available if called by a
   1115 	 * kernel thread or a realtime thread.
   1116 	 */
   1117 	l = curlwp;
   1118 	if (__predict_true(l != NULL) && lwp_eprio(l) >= PRI_KTHREAD) {
   1119 		flags |= UVM_PGA_USERESERVE;
   1120 	}
   1121 	if ((uvmexp.free <= uvmexp.reserve_kernel &&
   1122 	    (flags & UVM_PGA_USERESERVE) == 0) ||
   1123 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
   1124 	     curlwp != uvm.pagedaemon_lwp))
   1125 		goto fail;
   1126 
   1127 #if PGFL_NQUEUES != 2
   1128 #error uvm_pagealloc_strat needs to be updated
   1129 #endif
   1130 
   1131 	/*
   1132 	 * If we want a zero'd page, try the ZEROS queue first, otherwise
   1133 	 * we try the UNKNOWN queue first.
   1134 	 */
   1135 	if (flags & UVM_PGA_ZERO) {
   1136 		try1 = PGFL_ZEROS;
   1137 		try2 = PGFL_UNKNOWN;
   1138 	} else {
   1139 		try1 = PGFL_UNKNOWN;
   1140 		try2 = PGFL_ZEROS;
   1141 	}
   1142 
   1143  again:
   1144 	switch (strat) {
   1145 	case UVM_PGA_STRAT_NORMAL:
   1146 		/* Check freelists: descending priority (ascending id) order */
   1147 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
   1148 			pg = uvm_pagealloc_pgfl(ucpu, lcv,
   1149 			    try1, try2, &color);
   1150 			if (pg != NULL)
   1151 				goto gotit;
   1152 		}
   1153 
   1154 		/* No pages free! */
   1155 		goto fail;
   1156 
   1157 	case UVM_PGA_STRAT_ONLY:
   1158 	case UVM_PGA_STRAT_FALLBACK:
   1159 		/* Attempt to allocate from the specified free list. */
   1160 		KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
   1161 		pg = uvm_pagealloc_pgfl(ucpu, free_list,
   1162 		    try1, try2, &color);
   1163 		if (pg != NULL)
   1164 			goto gotit;
   1165 
   1166 		/* Fall back, if possible. */
   1167 		if (strat == UVM_PGA_STRAT_FALLBACK) {
   1168 			strat = UVM_PGA_STRAT_NORMAL;
   1169 			goto again;
   1170 		}
   1171 
   1172 		/* No pages free! */
   1173 		goto fail;
   1174 
   1175 	default:
   1176 		panic("uvm_pagealloc_strat: bad strat %d", strat);
   1177 		/* NOTREACHED */
   1178 	}
   1179 
   1180  gotit:
   1181 	/*
   1182 	 * We now know which color we actually allocated from; set
   1183 	 * the next color accordingly.
   1184 	 */
   1185 
   1186 	ucpu->page_free_nextcolor = (color + 1) & uvmexp.colormask;
   1187 
   1188 	/*
   1189 	 * update allocation statistics and remember if we have to
   1190 	 * zero the page
   1191 	 */
   1192 
   1193 	if (flags & UVM_PGA_ZERO) {
   1194 		if (pg->flags & PG_ZERO) {
   1195 			uvmexp.pga_zerohit++;
   1196 			zeroit = 0;
   1197 		} else {
   1198 			uvmexp.pga_zeromiss++;
   1199 			zeroit = 1;
   1200 		}
   1201 		if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) {
   1202 			ucpu->page_idle_zero = vm_page_zero_enable;
   1203 		}
   1204 	}
   1205 	KASSERT(pg->pqflags == PQ_FREE);
   1206 
   1207 	pg->offset = off;
   1208 	pg->uobject = obj;
   1209 	pg->uanon = anon;
   1210 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
   1211 	if (anon) {
   1212 		anon->an_page = pg;
   1213 		pg->pqflags = PQ_ANON;
   1214 		atomic_inc_uint(&uvmexp.anonpages);
   1215 	} else {
   1216 		if (obj) {
   1217 			uvm_pageinsert(pg);
   1218 		}
   1219 		pg->pqflags = 0;
   1220 	}
   1221 	mutex_spin_exit(&uvm_fpageqlock);
   1222 
   1223 #if defined(UVM_PAGE_TRKOWN)
   1224 	pg->owner_tag = NULL;
   1225 #endif
   1226 	UVM_PAGE_OWN(pg, "new alloc");
   1227 
   1228 	if (flags & UVM_PGA_ZERO) {
   1229 		/*
   1230 		 * A zero'd page is not clean.  If we got a page not already
   1231 		 * zero'd, then we have to zero it ourselves.
   1232 		 */
   1233 		pg->flags &= ~PG_CLEAN;
   1234 		if (zeroit)
   1235 			pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1236 	}
   1237 
   1238 	return(pg);
   1239 
   1240  fail:
   1241 	mutex_spin_exit(&uvm_fpageqlock);
   1242 	return (NULL);
   1243 }
   1244 
   1245 /*
   1246  * uvm_pagereplace: replace a page with another
   1247  *
   1248  * => object must be locked
   1249  */
   1250 
   1251 void
   1252 uvm_pagereplace(struct vm_page *oldpg, struct vm_page *newpg)
   1253 {
   1254 	struct uvm_object *uobj = oldpg->uobject;
   1255 
   1256 	KASSERT((oldpg->flags & PG_TABLED) != 0);
   1257 	KASSERT(uobj != NULL);
   1258 	KASSERT((newpg->flags & PG_TABLED) == 0);
   1259 	KASSERT(newpg->uobject == NULL);
   1260 	KASSERT(mutex_owned(&uobj->vmobjlock));
   1261 
   1262 	newpg->uobject = uobj;
   1263 	newpg->offset = oldpg->offset;
   1264 
   1265 	uvm_pageremove_tree(uobj, oldpg);
   1266 	uvm_pageinsert_tree(uobj, newpg);
   1267 	uvm_pageinsert_list(uobj, newpg, oldpg);
   1268 	uvm_pageremove_list(uobj, oldpg);
   1269 }
   1270 
   1271 /*
   1272  * uvm_pagerealloc: reallocate a page from one object to another
   1273  *
   1274  * => both objects must be locked
   1275  */
   1276 
   1277 void
   1278 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
   1279 {
   1280 	/*
   1281 	 * remove it from the old object
   1282 	 */
   1283 
   1284 	if (pg->uobject) {
   1285 		uvm_pageremove(pg);
   1286 	}
   1287 
   1288 	/*
   1289 	 * put it in the new object
   1290 	 */
   1291 
   1292 	if (newobj) {
   1293 		pg->uobject = newobj;
   1294 		pg->offset = newoff;
   1295 		uvm_pageinsert(pg);
   1296 	}
   1297 }
   1298 
   1299 #ifdef DEBUG
   1300 /*
   1301  * check if page is zero-filled
   1302  *
   1303  *  - called with free page queue lock held.
   1304  */
   1305 void
   1306 uvm_pagezerocheck(struct vm_page *pg)
   1307 {
   1308 	int *p, *ep;
   1309 
   1310 	KASSERT(uvm_zerocheckkva != 0);
   1311 	KASSERT(mutex_owned(&uvm_fpageqlock));
   1312 
   1313 	/*
   1314 	 * XXX assuming pmap_kenter_pa and pmap_kremove never call
   1315 	 * uvm page allocator.
   1316 	 *
   1317 	 * it might be better to have "CPU-local temporary map" pmap interface.
   1318 	 */
   1319 	pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ);
   1320 	p = (int *)uvm_zerocheckkva;
   1321 	ep = (int *)((char *)p + PAGE_SIZE);
   1322 	pmap_update(pmap_kernel());
   1323 	while (p < ep) {
   1324 		if (*p != 0)
   1325 			panic("PG_ZERO page isn't zero-filled");
   1326 		p++;
   1327 	}
   1328 	pmap_kremove(uvm_zerocheckkva, PAGE_SIZE);
   1329 	/*
   1330 	 * pmap_update() is not necessary here because no one except us
   1331 	 * uses this VA.
   1332 	 */
   1333 }
   1334 #endif /* DEBUG */
   1335 
   1336 /*
   1337  * uvm_pagefree: free page
   1338  *
   1339  * => erase page's identity (i.e. remove from object)
   1340  * => put page on free list
   1341  * => caller must lock owning object (either anon or uvm_object)
   1342  * => caller must lock page queues
   1343  * => assumes all valid mappings of pg are gone
   1344  */
   1345 
   1346 void
   1347 uvm_pagefree(struct vm_page *pg)
   1348 {
   1349 	struct pgflist *pgfl;
   1350 	struct uvm_cpu *ucpu;
   1351 	int index, color, queue;
   1352 	bool iszero;
   1353 
   1354 #ifdef DEBUG
   1355 	if (pg->uobject == (void *)0xdeadbeef &&
   1356 	    pg->uanon == (void *)0xdeadbeef) {
   1357 		panic("uvm_pagefree: freeing free page %p", pg);
   1358 	}
   1359 #endif /* DEBUG */
   1360 
   1361 	KASSERT((pg->flags & PG_PAGEOUT) == 0);
   1362 	KASSERT(!(pg->pqflags & PQ_FREE));
   1363 	KASSERT(mutex_owned(&uvm_pageqlock) || !uvmpdpol_pageisqueued_p(pg));
   1364 	KASSERT(pg->uobject == NULL || mutex_owned(&pg->uobject->vmobjlock));
   1365 	KASSERT(pg->uobject != NULL || pg->uanon == NULL ||
   1366 		mutex_owned(&pg->uanon->an_lock));
   1367 
   1368 	/*
   1369 	 * if the page is loaned, resolve the loan instead of freeing.
   1370 	 */
   1371 
   1372 	if (pg->loan_count) {
   1373 		KASSERT(pg->wire_count == 0);
   1374 
   1375 		/*
   1376 		 * if the page is owned by an anon then we just want to
   1377 		 * drop anon ownership.  the kernel will free the page when
   1378 		 * it is done with it.  if the page is owned by an object,
   1379 		 * remove it from the object and mark it dirty for the benefit
   1380 		 * of possible anon owners.
   1381 		 *
   1382 		 * regardless of previous ownership, wakeup any waiters,
   1383 		 * unbusy the page, and we're done.
   1384 		 */
   1385 
   1386 		if (pg->uobject != NULL) {
   1387 			uvm_pageremove(pg);
   1388 			pg->flags &= ~PG_CLEAN;
   1389 		} else if (pg->uanon != NULL) {
   1390 			if ((pg->pqflags & PQ_ANON) == 0) {
   1391 				pg->loan_count--;
   1392 			} else {
   1393 				pg->pqflags &= ~PQ_ANON;
   1394 				atomic_dec_uint(&uvmexp.anonpages);
   1395 			}
   1396 			pg->uanon->an_page = NULL;
   1397 			pg->uanon = NULL;
   1398 		}
   1399 		if (pg->flags & PG_WANTED) {
   1400 			wakeup(pg);
   1401 		}
   1402 		pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1);
   1403 #ifdef UVM_PAGE_TRKOWN
   1404 		pg->owner_tag = NULL;
   1405 #endif
   1406 		if (pg->loan_count) {
   1407 			KASSERT(pg->uobject == NULL);
   1408 			if (pg->uanon == NULL) {
   1409 				uvm_pagedequeue(pg);
   1410 			}
   1411 			return;
   1412 		}
   1413 	}
   1414 
   1415 	/*
   1416 	 * remove page from its object or anon.
   1417 	 */
   1418 
   1419 	if (pg->uobject != NULL) {
   1420 		uvm_pageremove(pg);
   1421 	} else if (pg->uanon != NULL) {
   1422 		pg->uanon->an_page = NULL;
   1423 		atomic_dec_uint(&uvmexp.anonpages);
   1424 	}
   1425 
   1426 	/*
   1427 	 * now remove the page from the queues.
   1428 	 */
   1429 
   1430 	uvm_pagedequeue(pg);
   1431 
   1432 	/*
   1433 	 * if the page was wired, unwire it now.
   1434 	 */
   1435 
   1436 	if (pg->wire_count) {
   1437 		pg->wire_count = 0;
   1438 		uvmexp.wired--;
   1439 	}
   1440 
   1441 	/*
   1442 	 * and put on free queue
   1443 	 */
   1444 
   1445 	iszero = (pg->flags & PG_ZERO);
   1446 	index = uvm_page_lookup_freelist(pg);
   1447 	color = VM_PGCOLOR_BUCKET(pg);
   1448 	queue = (iszero ? PGFL_ZEROS : PGFL_UNKNOWN);
   1449 
   1450 #ifdef DEBUG
   1451 	pg->uobject = (void *)0xdeadbeef;
   1452 	pg->uanon = (void *)0xdeadbeef;
   1453 #endif
   1454 
   1455 	mutex_spin_enter(&uvm_fpageqlock);
   1456 	pg->pqflags = PQ_FREE;
   1457 
   1458 #ifdef DEBUG
   1459 	if (iszero)
   1460 		uvm_pagezerocheck(pg);
   1461 #endif /* DEBUG */
   1462 
   1463 
   1464 	/* global list */
   1465 	pgfl = &uvm.page_free[index].pgfl_buckets[color].pgfl_queues[queue];
   1466 	LIST_INSERT_HEAD(pgfl, pg, pageq.list);
   1467 	uvmexp.free++;
   1468 	if (iszero) {
   1469 		uvmexp.zeropages++;
   1470 	}
   1471 
   1472 	/* per-cpu list */
   1473 	ucpu = curcpu()->ci_data.cpu_uvm;
   1474 	pg->offset = (uintptr_t)ucpu;
   1475 	pgfl = &ucpu->page_free[index].pgfl_buckets[color].pgfl_queues[queue];
   1476 	LIST_INSERT_HEAD(pgfl, pg, listq.list);
   1477 	ucpu->pages[queue]++;
   1478 	if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) {
   1479 		ucpu->page_idle_zero = vm_page_zero_enable;
   1480 	}
   1481 
   1482 	mutex_spin_exit(&uvm_fpageqlock);
   1483 }
   1484 
   1485 /*
   1486  * uvm_page_unbusy: unbusy an array of pages.
   1487  *
   1488  * => pages must either all belong to the same object, or all belong to anons.
   1489  * => if pages are object-owned, object must be locked.
   1490  * => if pages are anon-owned, anons must be locked.
   1491  * => caller must lock page queues if pages may be released.
   1492  * => caller must make sure that anon-owned pages are not PG_RELEASED.
   1493  */
   1494 
   1495 void
   1496 uvm_page_unbusy(struct vm_page **pgs, int npgs)
   1497 {
   1498 	struct vm_page *pg;
   1499 	int i;
   1500 	UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
   1501 
   1502 	for (i = 0; i < npgs; i++) {
   1503 		pg = pgs[i];
   1504 		if (pg == NULL || pg == PGO_DONTCARE) {
   1505 			continue;
   1506 		}
   1507 
   1508 		KASSERT(pg->uobject == NULL ||
   1509 		    mutex_owned(&pg->uobject->vmobjlock));
   1510 		KASSERT(pg->uobject != NULL ||
   1511 		    (pg->uanon != NULL && mutex_owned(&pg->uanon->an_lock)));
   1512 
   1513 		KASSERT(pg->flags & PG_BUSY);
   1514 		KASSERT((pg->flags & PG_PAGEOUT) == 0);
   1515 		if (pg->flags & PG_WANTED) {
   1516 			wakeup(pg);
   1517 		}
   1518 		if (pg->flags & PG_RELEASED) {
   1519 			UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
   1520 			KASSERT(pg->uobject != NULL ||
   1521 			    (pg->uanon != NULL && pg->uanon->an_ref > 0));
   1522 			pg->flags &= ~PG_RELEASED;
   1523 			uvm_pagefree(pg);
   1524 		} else {
   1525 			UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
   1526 			KASSERT((pg->flags & PG_FAKE) == 0);
   1527 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1528 			UVM_PAGE_OWN(pg, NULL);
   1529 		}
   1530 	}
   1531 }
   1532 
   1533 #if defined(UVM_PAGE_TRKOWN)
   1534 /*
   1535  * uvm_page_own: set or release page ownership
   1536  *
   1537  * => this is a debugging function that keeps track of who sets PG_BUSY
   1538  *	and where they do it.   it can be used to track down problems
   1539  *	such a process setting "PG_BUSY" and never releasing it.
   1540  * => page's object [if any] must be locked
   1541  * => if "tag" is NULL then we are releasing page ownership
   1542  */
   1543 void
   1544 uvm_page_own(struct vm_page *pg, const char *tag)
   1545 {
   1546 	struct uvm_object *uobj;
   1547 	struct vm_anon *anon;
   1548 
   1549 	KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
   1550 
   1551 	uobj = pg->uobject;
   1552 	anon = pg->uanon;
   1553 	if (uobj != NULL) {
   1554 		KASSERT(mutex_owned(&uobj->vmobjlock));
   1555 	} else if (anon != NULL) {
   1556 		KASSERT(mutex_owned(&anon->an_lock));
   1557 	}
   1558 
   1559 	KASSERT((pg->flags & PG_WANTED) == 0);
   1560 
   1561 	/* gain ownership? */
   1562 	if (tag) {
   1563 		KASSERT((pg->flags & PG_BUSY) != 0);
   1564 		if (pg->owner_tag) {
   1565 			printf("uvm_page_own: page %p already owned "
   1566 			    "by proc %d [%s]\n", pg,
   1567 			    pg->owner, pg->owner_tag);
   1568 			panic("uvm_page_own");
   1569 		}
   1570 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
   1571 		pg->lowner = (curlwp) ? curlwp->l_lid :  (lwpid_t) -1;
   1572 		pg->owner_tag = tag;
   1573 		return;
   1574 	}
   1575 
   1576 	/* drop ownership */
   1577 	KASSERT((pg->flags & PG_BUSY) == 0);
   1578 	if (pg->owner_tag == NULL) {
   1579 		printf("uvm_page_own: dropping ownership of an non-owned "
   1580 		    "page (%p)\n", pg);
   1581 		panic("uvm_page_own");
   1582 	}
   1583 	if (!uvmpdpol_pageisqueued_p(pg)) {
   1584 		KASSERT((pg->uanon == NULL && pg->uobject == NULL) ||
   1585 		    pg->wire_count > 0);
   1586 	} else {
   1587 		KASSERT(pg->wire_count == 0);
   1588 	}
   1589 	pg->owner_tag = NULL;
   1590 }
   1591 #endif
   1592 
   1593 /*
   1594  * uvm_pageidlezero: zero free pages while the system is idle.
   1595  *
   1596  * => try to complete one color bucket at a time, to reduce our impact
   1597  *	on the CPU cache.
   1598  * => we loop until we either reach the target or there is a lwp ready
   1599  *      to run, or MD code detects a reason to break early.
   1600  */
   1601 void
   1602 uvm_pageidlezero(void)
   1603 {
   1604 	struct vm_page *pg;
   1605 	struct pgfreelist *pgfl, *gpgfl;
   1606 	struct uvm_cpu *ucpu;
   1607 	int free_list, firstbucket, nextbucket;
   1608 
   1609 	ucpu = curcpu()->ci_data.cpu_uvm;
   1610 	if (!ucpu->page_idle_zero ||
   1611 	    ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) {
   1612 	    	ucpu->page_idle_zero = false;
   1613 		return;
   1614 	}
   1615 	mutex_enter(&uvm_fpageqlock);
   1616 	firstbucket = ucpu->page_free_nextcolor;
   1617 	nextbucket = firstbucket;
   1618 	do {
   1619 		for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
   1620 			if (sched_curcpu_runnable_p()) {
   1621 				goto quit;
   1622 			}
   1623 			pgfl = &ucpu->page_free[free_list];
   1624 			gpgfl = &uvm.page_free[free_list];
   1625 			while ((pg = LIST_FIRST(&pgfl->pgfl_buckets[
   1626 			    nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) {
   1627 				if (sched_curcpu_runnable_p()) {
   1628 					goto quit;
   1629 				}
   1630 				LIST_REMOVE(pg, pageq.list); /* global list */
   1631 				LIST_REMOVE(pg, listq.list); /* per-cpu list */
   1632 				ucpu->pages[PGFL_UNKNOWN]--;
   1633 				uvmexp.free--;
   1634 				KASSERT(pg->pqflags == PQ_FREE);
   1635 				pg->pqflags = 0;
   1636 				mutex_spin_exit(&uvm_fpageqlock);
   1637 #ifdef PMAP_PAGEIDLEZERO
   1638 				if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) {
   1639 
   1640 					/*
   1641 					 * The machine-dependent code detected
   1642 					 * some reason for us to abort zeroing
   1643 					 * pages, probably because there is a
   1644 					 * process now ready to run.
   1645 					 */
   1646 
   1647 					mutex_spin_enter(&uvm_fpageqlock);
   1648 					pg->pqflags = PQ_FREE;
   1649 					LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[
   1650 					    nextbucket].pgfl_queues[
   1651 					    PGFL_UNKNOWN], pg, pageq.list);
   1652 					LIST_INSERT_HEAD(&pgfl->pgfl_buckets[
   1653 					    nextbucket].pgfl_queues[
   1654 					    PGFL_UNKNOWN], pg, listq.list);
   1655 					ucpu->pages[PGFL_UNKNOWN]++;
   1656 					uvmexp.free++;
   1657 					uvmexp.zeroaborts++;
   1658 					goto quit;
   1659 				}
   1660 #else
   1661 				pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1662 #endif /* PMAP_PAGEIDLEZERO */
   1663 				pg->flags |= PG_ZERO;
   1664 
   1665 				mutex_spin_enter(&uvm_fpageqlock);
   1666 				pg->pqflags = PQ_FREE;
   1667 				LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[
   1668 				    nextbucket].pgfl_queues[PGFL_ZEROS],
   1669 				    pg, pageq.list);
   1670 				LIST_INSERT_HEAD(&pgfl->pgfl_buckets[
   1671 				    nextbucket].pgfl_queues[PGFL_ZEROS],
   1672 				    pg, listq.list);
   1673 				ucpu->pages[PGFL_ZEROS]++;
   1674 				uvmexp.free++;
   1675 				uvmexp.zeropages++;
   1676 			}
   1677 		}
   1678 		if (ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) {
   1679 			break;
   1680 		}
   1681 		nextbucket = (nextbucket + 1) & uvmexp.colormask;
   1682 	} while (nextbucket != firstbucket);
   1683 	ucpu->page_idle_zero = false;
   1684  quit:
   1685 	mutex_spin_exit(&uvm_fpageqlock);
   1686 }
   1687 
   1688 /*
   1689  * uvm_pagelookup: look up a page
   1690  *
   1691  * => caller should lock object to keep someone from pulling the page
   1692  *	out from under it
   1693  */
   1694 
   1695 struct vm_page *
   1696 uvm_pagelookup(struct uvm_object *obj, voff_t off)
   1697 {
   1698 	struct vm_page *pg;
   1699 
   1700 	KASSERT(mutex_owned(&obj->vmobjlock));
   1701 
   1702 	pg = (struct vm_page *)rb_tree_find_node(&obj->rb_tree, &off);
   1703 
   1704 	KASSERT(pg == NULL || obj->uo_npages != 0);
   1705 	KASSERT(pg == NULL || (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1706 		(pg->flags & PG_BUSY) != 0);
   1707 	return(pg);
   1708 }
   1709 
   1710 /*
   1711  * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
   1712  *
   1713  * => caller must lock page queues
   1714  */
   1715 
   1716 void
   1717 uvm_pagewire(struct vm_page *pg)
   1718 {
   1719 	KASSERT(mutex_owned(&uvm_pageqlock));
   1720 #if defined(READAHEAD_STATS)
   1721 	if ((pg->pqflags & PQ_READAHEAD) != 0) {
   1722 		uvm_ra_hit.ev_count++;
   1723 		pg->pqflags &= ~PQ_READAHEAD;
   1724 	}
   1725 #endif /* defined(READAHEAD_STATS) */
   1726 	if (pg->wire_count == 0) {
   1727 		uvm_pagedequeue(pg);
   1728 		uvmexp.wired++;
   1729 	}
   1730 	pg->wire_count++;
   1731 }
   1732 
   1733 /*
   1734  * uvm_pageunwire: unwire the page.
   1735  *
   1736  * => activate if wire count goes to zero.
   1737  * => caller must lock page queues
   1738  */
   1739 
   1740 void
   1741 uvm_pageunwire(struct vm_page *pg)
   1742 {
   1743 	KASSERT(mutex_owned(&uvm_pageqlock));
   1744 	pg->wire_count--;
   1745 	if (pg->wire_count == 0) {
   1746 		uvm_pageactivate(pg);
   1747 		uvmexp.wired--;
   1748 	}
   1749 }
   1750 
   1751 /*
   1752  * uvm_pagedeactivate: deactivate page
   1753  *
   1754  * => caller must lock page queues
   1755  * => caller must check to make sure page is not wired
   1756  * => object that page belongs to must be locked (so we can adjust pg->flags)
   1757  * => caller must clear the reference on the page before calling
   1758  */
   1759 
   1760 void
   1761 uvm_pagedeactivate(struct vm_page *pg)
   1762 {
   1763 
   1764 	KASSERT(mutex_owned(&uvm_pageqlock));
   1765 	KASSERT(pg->wire_count != 0 || uvmpdpol_pageisqueued_p(pg));
   1766 	uvmpdpol_pagedeactivate(pg);
   1767 }
   1768 
   1769 /*
   1770  * uvm_pageactivate: activate page
   1771  *
   1772  * => caller must lock page queues
   1773  */
   1774 
   1775 void
   1776 uvm_pageactivate(struct vm_page *pg)
   1777 {
   1778 
   1779 	KASSERT(mutex_owned(&uvm_pageqlock));
   1780 #if defined(READAHEAD_STATS)
   1781 	if ((pg->pqflags & PQ_READAHEAD) != 0) {
   1782 		uvm_ra_hit.ev_count++;
   1783 		pg->pqflags &= ~PQ_READAHEAD;
   1784 	}
   1785 #endif /* defined(READAHEAD_STATS) */
   1786 	if (pg->wire_count != 0) {
   1787 		return;
   1788 	}
   1789 	uvmpdpol_pageactivate(pg);
   1790 }
   1791 
   1792 /*
   1793  * uvm_pagedequeue: remove a page from any paging queue
   1794  */
   1795 
   1796 void
   1797 uvm_pagedequeue(struct vm_page *pg)
   1798 {
   1799 
   1800 	if (uvmpdpol_pageisqueued_p(pg)) {
   1801 		KASSERT(mutex_owned(&uvm_pageqlock));
   1802 	}
   1803 
   1804 	uvmpdpol_pagedequeue(pg);
   1805 }
   1806 
   1807 /*
   1808  * uvm_pageenqueue: add a page to a paging queue without activating.
   1809  * used where a page is not really demanded (yet).  eg. read-ahead
   1810  */
   1811 
   1812 void
   1813 uvm_pageenqueue(struct vm_page *pg)
   1814 {
   1815 
   1816 	KASSERT(mutex_owned(&uvm_pageqlock));
   1817 	if (pg->wire_count != 0) {
   1818 		return;
   1819 	}
   1820 	uvmpdpol_pageenqueue(pg);
   1821 }
   1822 
   1823 /*
   1824  * uvm_pagezero: zero fill a page
   1825  *
   1826  * => if page is part of an object then the object should be locked
   1827  *	to protect pg->flags.
   1828  */
   1829 
   1830 void
   1831 uvm_pagezero(struct vm_page *pg)
   1832 {
   1833 	pg->flags &= ~PG_CLEAN;
   1834 	pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1835 }
   1836 
   1837 /*
   1838  * uvm_pagecopy: copy a page
   1839  *
   1840  * => if page is part of an object then the object should be locked
   1841  *	to protect pg->flags.
   1842  */
   1843 
   1844 void
   1845 uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
   1846 {
   1847 
   1848 	dst->flags &= ~PG_CLEAN;
   1849 	pmap_copy_page(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
   1850 }
   1851 
   1852 /*
   1853  * uvm_pageismanaged: test it see that a page (specified by PA) is managed.
   1854  */
   1855 
   1856 bool
   1857 uvm_pageismanaged(paddr_t pa)
   1858 {
   1859 
   1860 	return (vm_physseg_find(atop(pa), NULL) != -1);
   1861 }
   1862 
   1863 /*
   1864  * uvm_page_lookup_freelist: look up the free list for the specified page
   1865  */
   1866 
   1867 int
   1868 uvm_page_lookup_freelist(struct vm_page *pg)
   1869 {
   1870 	int lcv;
   1871 
   1872 	lcv = vm_physseg_find(atop(VM_PAGE_TO_PHYS(pg)), NULL);
   1873 	KASSERT(lcv != -1);
   1874 	return (vm_physmem[lcv].free_list);
   1875 }
   1876