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