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