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