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