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