xref: /openbsd-src/sys/uvm/uvm_page.c (revision 824adb5411e4389b29bae28eba5c2c2bbd147f34)
1 /*	$OpenBSD: uvm_page.c,v 1.158 2021/09/05 11:44:46 mpi Exp $	*/
2 /*	$NetBSD: uvm_page.c,v 1.44 2000/11/27 08:40:04 chs Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * Copyright (c) 1991, 1993, The Regents of the University of California.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * The Mach Operating System project at Carnegie-Mellon University.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)vm_page.c   8.3 (Berkeley) 3/21/94
38  * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp
39  *
40  *
41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  */
64 
65 /*
66  * uvm_page.c: page ops.
67  */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sched.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/proc.h>
75 #include <sys/smr.h>
76 
77 #include <uvm/uvm.h>
78 
79 /*
80  * for object trees
81  */
82 RBT_GENERATE(uvm_objtree, vm_page, objt, uvm_pagecmp);
83 
84 int
85 uvm_pagecmp(const struct vm_page *a, const struct vm_page *b)
86 {
87 	return a->offset < b->offset ? -1 : a->offset > b->offset;
88 }
89 
90 /*
91  * global vars... XXXCDC: move to uvm. structure.
92  */
93 /*
94  * physical memory config is stored in vm_physmem.
95  */
96 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];	/* XXXCDC: uvm.physmem */
97 int vm_nphysseg = 0;				/* XXXCDC: uvm.nphysseg */
98 
99 /*
100  * Some supported CPUs in a given architecture don't support all
101  * of the things necessary to do idle page zero'ing efficiently.
102  * We therefore provide a way to disable it from machdep code here.
103  */
104 
105 /*
106  * local variables
107  */
108 /*
109  * these variables record the values returned by vm_page_bootstrap,
110  * for debugging purposes.  The implementation of uvm_pageboot_alloc
111  * and pmap_startup here also uses them internally.
112  */
113 static vaddr_t      virtual_space_start;
114 static vaddr_t      virtual_space_end;
115 
116 /*
117  * local prototypes
118  */
119 static void uvm_pageinsert(struct vm_page *);
120 static void uvm_pageremove(struct vm_page *);
121 
122 /*
123  * inline functions
124  */
125 /*
126  * uvm_pageinsert: insert a page in the object
127  *
128  * => caller must lock page queues XXX questionable
129  * => call should have already set pg's object and offset pointers
130  *    and bumped the version counter
131  */
132 inline static void
133 uvm_pageinsert(struct vm_page *pg)
134 {
135 	struct vm_page	*dupe;
136 
137 	KASSERT((pg->pg_flags & PG_TABLED) == 0);
138 	dupe = RBT_INSERT(uvm_objtree, &pg->uobject->memt, pg);
139 	/* not allowed to insert over another page */
140 	KASSERT(dupe == NULL);
141 	atomic_setbits_int(&pg->pg_flags, PG_TABLED);
142 	pg->uobject->uo_npages++;
143 }
144 
145 /*
146  * uvm_page_remove: remove page from object
147  *
148  * => caller must lock page queues
149  */
150 static inline void
151 uvm_pageremove(struct vm_page *pg)
152 {
153 	KASSERT(pg->pg_flags & PG_TABLED);
154 	RBT_REMOVE(uvm_objtree, &pg->uobject->memt, pg);
155 
156 	atomic_clearbits_int(&pg->pg_flags, PG_TABLED);
157 	pg->uobject->uo_npages--;
158 	pg->uobject = NULL;
159 	pg->pg_version++;
160 }
161 
162 /*
163  * uvm_page_init: init the page system.   called from uvm_init().
164  *
165  * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
166  */
167 void
168 uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp)
169 {
170 	vsize_t freepages, pagecount, n;
171 	vm_page_t pagearray, curpg;
172 	int lcv, i;
173 	paddr_t paddr, pgno;
174 	struct vm_physseg *seg;
175 
176 	/*
177 	 * init the page queues and page queue locks
178 	 */
179 
180 	TAILQ_INIT(&uvm.page_active);
181 	TAILQ_INIT(&uvm.page_inactive_swp);
182 	TAILQ_INIT(&uvm.page_inactive_obj);
183 	mtx_init(&uvm.pageqlock, IPL_VM);
184 	mtx_init(&uvm.fpageqlock, IPL_VM);
185 	uvm_pmr_init();
186 
187 	/*
188 	 * allocate vm_page structures.
189 	 */
190 
191 	/*
192 	 * sanity check:
193 	 * before calling this function the MD code is expected to register
194 	 * some free RAM with the uvm_page_physload() function.   our job
195 	 * now is to allocate vm_page structures for this memory.
196 	 */
197 
198 	if (vm_nphysseg == 0)
199 		panic("uvm_page_bootstrap: no memory pre-allocated");
200 
201 	/*
202 	 * first calculate the number of free pages...
203 	 *
204 	 * note that we use start/end rather than avail_start/avail_end.
205 	 * this allows us to allocate extra vm_page structures in case we
206 	 * want to return some memory to the pool after booting.
207 	 */
208 
209 	freepages = 0;
210 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++)
211 		freepages += (seg->end - seg->start);
212 
213 	/*
214 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
215 	 * use.   for each page of memory we use we need a vm_page structure.
216 	 * thus, the total number of pages we can use is the total size of
217 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
218 	 * structure.   we add one to freepages as a fudge factor to avoid
219 	 * truncation errors (since we can only allocate in terms of whole
220 	 * pages).
221 	 */
222 
223 	pagecount = (((paddr_t)freepages + 1) << PAGE_SHIFT) /
224 	    (PAGE_SIZE + sizeof(struct vm_page));
225 	pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount *
226 	    sizeof(struct vm_page));
227 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
228 
229 	/* init the vm_page structures and put them in the correct place. */
230 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) {
231 		n = seg->end - seg->start;
232 		if (n > pagecount) {
233 			panic("uvm_page_init: lost %ld page(s) in init",
234 			    (long)(n - pagecount));
235 			    /* XXXCDC: shouldn't happen? */
236 			/* n = pagecount; */
237 		}
238 
239 		/* set up page array pointers */
240 		seg->pgs = pagearray;
241 		pagearray += n;
242 		pagecount -= n;
243 		seg->lastpg = seg->pgs + (n - 1);
244 
245 		/* init and free vm_pages (we've already zeroed them) */
246 		pgno = seg->start;
247 		paddr = ptoa(pgno);
248 		for (i = 0, curpg = seg->pgs; i < n;
249 		    i++, curpg++, pgno++, paddr += PAGE_SIZE) {
250 			curpg->phys_addr = paddr;
251 			VM_MDPAGE_INIT(curpg);
252 			if (pgno >= seg->avail_start &&
253 			    pgno < seg->avail_end) {
254 				uvmexp.npages++;
255 			}
256 		}
257 
258 		/* Add pages to free pool. */
259 		uvm_pmr_freepages(&seg->pgs[seg->avail_start - seg->start],
260 		    seg->avail_end - seg->avail_start);
261 	}
262 
263 	/*
264 	 * pass up the values of virtual_space_start and
265 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
266 	 * layers of the VM.
267 	 */
268 
269 	*kvm_startp = round_page(virtual_space_start);
270 	*kvm_endp = trunc_page(virtual_space_end);
271 
272 	/* init locks for kernel threads */
273 	mtx_init(&uvm.aiodoned_lock, IPL_BIO);
274 
275 	/*
276 	 * init reserve thresholds
277 	 * XXXCDC - values may need adjusting
278 	 */
279 	uvmexp.reserve_pagedaemon = 4;
280 	uvmexp.reserve_kernel = 8;
281 	uvmexp.anonminpct = 10;
282 	uvmexp.vnodeminpct = 10;
283 	uvmexp.vtextminpct = 5;
284 	uvmexp.anonmin = uvmexp.anonminpct * 256 / 100;
285 	uvmexp.vnodemin = uvmexp.vnodeminpct * 256 / 100;
286 	uvmexp.vtextmin = uvmexp.vtextminpct * 256 / 100;
287 
288 	uvm.page_init_done = TRUE;
289 }
290 
291 /*
292  * uvm_setpagesize: set the page size
293  *
294  * => sets page_shift and page_mask from uvmexp.pagesize.
295  */
296 void
297 uvm_setpagesize(void)
298 {
299 	if (uvmexp.pagesize == 0)
300 		uvmexp.pagesize = DEFAULT_PAGE_SIZE;
301 	uvmexp.pagemask = uvmexp.pagesize - 1;
302 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
303 		panic("uvm_setpagesize: page size not a power of two");
304 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
305 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
306 			break;
307 }
308 
309 /*
310  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
311  */
312 vaddr_t
313 uvm_pageboot_alloc(vsize_t size)
314 {
315 #if defined(PMAP_STEAL_MEMORY)
316 	vaddr_t addr;
317 
318 	/*
319 	 * defer bootstrap allocation to MD code (it may want to allocate
320 	 * from a direct-mapped segment).  pmap_steal_memory should round
321 	 * off virtual_space_start/virtual_space_end.
322 	 */
323 
324 	addr = pmap_steal_memory(size, &virtual_space_start,
325 	    &virtual_space_end);
326 
327 	return addr;
328 
329 #else /* !PMAP_STEAL_MEMORY */
330 
331 	static boolean_t initialized = FALSE;
332 	vaddr_t addr, vaddr;
333 	paddr_t paddr;
334 
335 	/* round to page size */
336 	size = round_page(size);
337 
338 	/* on first call to this function, initialize ourselves. */
339 	if (initialized == FALSE) {
340 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
341 
342 		/* round it the way we like it */
343 		virtual_space_start = round_page(virtual_space_start);
344 		virtual_space_end = trunc_page(virtual_space_end);
345 
346 		initialized = TRUE;
347 	}
348 
349 	/* allocate virtual memory for this request */
350 	if (virtual_space_start == virtual_space_end ||
351 	    (virtual_space_end - virtual_space_start) < size)
352 		panic("uvm_pageboot_alloc: out of virtual space");
353 
354 	addr = virtual_space_start;
355 
356 #ifdef PMAP_GROWKERNEL
357 	/*
358 	 * If the kernel pmap can't map the requested space,
359 	 * then allocate more resources for it.
360 	 */
361 	if (uvm_maxkaddr < (addr + size)) {
362 		uvm_maxkaddr = pmap_growkernel(addr + size);
363 		if (uvm_maxkaddr < (addr + size))
364 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
365 	}
366 #endif
367 
368 	virtual_space_start += size;
369 
370 	/* allocate and mapin physical pages to back new virtual pages */
371 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
372 	    vaddr += PAGE_SIZE) {
373 		if (!uvm_page_physget(&paddr))
374 			panic("uvm_pageboot_alloc: out of memory");
375 
376 		/*
377 		 * Note this memory is no longer managed, so using
378 		 * pmap_kenter is safe.
379 		 */
380 		pmap_kenter_pa(vaddr, paddr, PROT_READ | PROT_WRITE);
381 	}
382 	pmap_update(pmap_kernel());
383 	return addr;
384 #endif	/* PMAP_STEAL_MEMORY */
385 }
386 
387 #if !defined(PMAP_STEAL_MEMORY)
388 /*
389  * uvm_page_physget: "steal" one page from the vm_physmem structure.
390  *
391  * => attempt to allocate it off the end of a segment in which the "avail"
392  *    values match the start/end values.   if we can't do that, then we
393  *    will advance both values (making them equal, and removing some
394  *    vm_page structures from the non-avail area).
395  * => return false if out of memory.
396  */
397 
398 boolean_t
399 uvm_page_physget(paddr_t *paddrp)
400 {
401 	int lcv;
402 	struct vm_physseg *seg;
403 
404 	/* pass 1: try allocating from a matching end */
405 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) || \
406 	(VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
407 	for (lcv = vm_nphysseg - 1, seg = vm_physmem + lcv; lcv >= 0;
408 	    lcv--, seg--)
409 #else
410 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++)
411 #endif
412 	{
413 		if (uvm.page_init_done == TRUE)
414 			panic("uvm_page_physget: called _after_ bootstrap");
415 
416 		/* try from front */
417 		if (seg->avail_start == seg->start &&
418 		    seg->avail_start < seg->avail_end) {
419 			*paddrp = ptoa(seg->avail_start);
420 			seg->avail_start++;
421 			seg->start++;
422 			/* nothing left?   nuke it */
423 			if (seg->avail_start == seg->end) {
424 				if (vm_nphysseg == 1)
425 				    panic("uvm_page_physget: out of memory!");
426 				vm_nphysseg--;
427 				for (; lcv < vm_nphysseg; lcv++, seg++)
428 					/* structure copy */
429 					seg[0] = seg[1];
430 			}
431 			return TRUE;
432 		}
433 
434 		/* try from rear */
435 		if (seg->avail_end == seg->end &&
436 		    seg->avail_start < seg->avail_end) {
437 			*paddrp = ptoa(seg->avail_end - 1);
438 			seg->avail_end--;
439 			seg->end--;
440 			/* nothing left?   nuke it */
441 			if (seg->avail_end == seg->start) {
442 				if (vm_nphysseg == 1)
443 				    panic("uvm_page_physget: out of memory!");
444 				vm_nphysseg--;
445 				for (; lcv < vm_nphysseg ; lcv++, seg++)
446 					/* structure copy */
447 					seg[0] = seg[1];
448 			}
449 			return TRUE;
450 		}
451 	}
452 
453 	/* pass2: forget about matching ends, just allocate something */
454 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) || \
455 	(VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
456 	for (lcv = vm_nphysseg - 1, seg = vm_physmem + lcv; lcv >= 0;
457 	    lcv--, seg--)
458 #else
459 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++)
460 #endif
461 	{
462 
463 		/* any room in this bank? */
464 		if (seg->avail_start >= seg->avail_end)
465 			continue;  /* nope */
466 
467 		*paddrp = ptoa(seg->avail_start);
468 		seg->avail_start++;
469 		/* truncate! */
470 		seg->start = seg->avail_start;
471 
472 		/* nothing left?   nuke it */
473 		if (seg->avail_start == seg->end) {
474 			if (vm_nphysseg == 1)
475 				panic("uvm_page_physget: out of memory!");
476 			vm_nphysseg--;
477 			for (; lcv < vm_nphysseg ; lcv++, seg++)
478 				/* structure copy */
479 				seg[0] = seg[1];
480 		}
481 		return TRUE;
482 	}
483 
484 	return FALSE;        /* whoops! */
485 }
486 
487 #endif /* PMAP_STEAL_MEMORY */
488 
489 /*
490  * uvm_page_physload: load physical memory into VM system
491  *
492  * => all args are PFs
493  * => all pages in start/end get vm_page structures
494  * => areas marked by avail_start/avail_end get added to the free page pool
495  * => we are limited to VM_PHYSSEG_MAX physical memory segments
496  */
497 
498 void
499 uvm_page_physload(paddr_t start, paddr_t end, paddr_t avail_start,
500     paddr_t avail_end, int flags)
501 {
502 	int preload, lcv;
503 	psize_t npages;
504 	struct vm_page *pgs;
505 	struct vm_physseg *ps, *seg;
506 
507 #ifdef DIAGNOSTIC
508 	if (uvmexp.pagesize == 0)
509 		panic("uvm_page_physload: page size not set!");
510 
511 	if (start >= end)
512 		panic("uvm_page_physload: start >= end");
513 #endif
514 
515 	/* do we have room? */
516 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
517 		printf("uvm_page_physload: unable to load physical memory "
518 		    "segment\n");
519 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
520 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
521 		printf("\tincrease VM_PHYSSEG_MAX\n");
522 		return;
523 	}
524 
525 	/*
526 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
527 	 * called yet, so malloc is not available).
528 	 */
529 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg; lcv++, seg++) {
530 		if (seg->pgs)
531 			break;
532 	}
533 	preload = (lcv == vm_nphysseg);
534 
535 	/* if VM is already running, attempt to malloc() vm_page structures */
536 	if (!preload) {
537 		/*
538 		 * XXXCDC: need some sort of lockout for this case
539 		 * right now it is only used by devices so it should be alright.
540 		 */
541  		paddr_t paddr;
542 
543  		npages = end - start;  /* # of pages */
544 
545 		pgs = km_alloc(npages * sizeof(*pgs), &kv_any, &kp_zero,
546 		    &kd_waitok);
547 		if (pgs == NULL) {
548 			printf("uvm_page_physload: can not malloc vm_page "
549 			    "structs for segment\n");
550 			printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
551 			return;
552 		}
553 		/* init phys_addr and free pages, XXX uvmexp.npages */
554 		for (lcv = 0, paddr = ptoa(start); lcv < npages;
555 		    lcv++, paddr += PAGE_SIZE) {
556 			pgs[lcv].phys_addr = paddr;
557 			VM_MDPAGE_INIT(&pgs[lcv]);
558 			if (atop(paddr) >= avail_start &&
559 			    atop(paddr) < avail_end) {
560 				if (flags & PHYSLOAD_DEVICE) {
561 					atomic_setbits_int(&pgs[lcv].pg_flags,
562 					    PG_DEV);
563 					pgs[lcv].wire_count = 1;
564 				} else {
565 #if defined(VM_PHYSSEG_NOADD)
566 		panic("uvm_page_physload: tried to add RAM after vm_mem_init");
567 #endif
568 				}
569 			}
570 		}
571 
572 		/* Add pages to free pool. */
573 		if ((flags & PHYSLOAD_DEVICE) == 0) {
574 			uvm_pmr_freepages(&pgs[avail_start - start],
575 			    avail_end - avail_start);
576 		}
577 
578 		/* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
579 	} else {
580 		/* gcc complains if these don't get init'd */
581 		pgs = NULL;
582 		npages = 0;
583 
584 	}
585 
586 	/* now insert us in the proper place in vm_physmem[] */
587 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
588 	/* random: put it at the end (easy!) */
589 	ps = &vm_physmem[vm_nphysseg];
590 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
591 	{
592 		int x;
593 		/* sort by address for binary search */
594 		for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg; lcv++, seg++)
595 			if (start < seg->start)
596 				break;
597 		ps = seg;
598 		/* move back other entries, if necessary ... */
599 		for (x = vm_nphysseg, seg = vm_physmem + x - 1; x > lcv;
600 		    x--, seg--)
601 			/* structure copy */
602 			seg[1] = seg[0];
603 	}
604 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
605 	{
606 		int x;
607 		/* sort by largest segment first */
608 		for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg; lcv++, seg++)
609 			if ((end - start) >
610 			    (seg->end - seg->start))
611 				break;
612 		ps = &vm_physmem[lcv];
613 		/* move back other entries, if necessary ... */
614 		for (x = vm_nphysseg, seg = vm_physmem + x - 1; x > lcv;
615 		    x--, seg--)
616 			/* structure copy */
617 			seg[1] = seg[0];
618 	}
619 #else
620 	panic("uvm_page_physload: unknown physseg strategy selected!");
621 #endif
622 
623 	ps->start = start;
624 	ps->end = end;
625 	ps->avail_start = avail_start;
626 	ps->avail_end = avail_end;
627 	if (preload) {
628 		ps->pgs = NULL;
629 	} else {
630 		ps->pgs = pgs;
631 		ps->lastpg = pgs + npages - 1;
632 	}
633 	vm_nphysseg++;
634 
635 	return;
636 }
637 
638 #ifdef DDB /* XXXCDC: TMP TMP TMP DEBUG DEBUG DEBUG */
639 
640 void uvm_page_physdump(void); /* SHUT UP GCC */
641 
642 /* call from DDB */
643 void
644 uvm_page_physdump(void)
645 {
646 	int lcv;
647 	struct vm_physseg *seg;
648 
649 	printf("uvm_page_physdump: physical memory config [segs=%d of %d]:\n",
650 	    vm_nphysseg, VM_PHYSSEG_MAX);
651 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++)
652 		printf("0x%llx->0x%llx [0x%llx->0x%llx]\n",
653 		    (long long)seg->start,
654 		    (long long)seg->end,
655 		    (long long)seg->avail_start,
656 		    (long long)seg->avail_end);
657 	printf("STRATEGY = ");
658 	switch (VM_PHYSSEG_STRAT) {
659 	case VM_PSTRAT_RANDOM: printf("RANDOM\n"); break;
660 	case VM_PSTRAT_BSEARCH: printf("BSEARCH\n"); break;
661 	case VM_PSTRAT_BIGFIRST: printf("BIGFIRST\n"); break;
662 	default: printf("<<UNKNOWN>>!!!!\n");
663 	}
664 }
665 #endif
666 
667 void
668 uvm_shutdown(void)
669 {
670 #ifdef UVM_SWAP_ENCRYPT
671 	uvm_swap_finicrypt_all();
672 #endif
673 	smr_flush();
674 }
675 
676 /*
677  * Perform insert of a given page in the specified anon of obj.
678  * This is basically, uvm_pagealloc, but with the page already given.
679  */
680 void
681 uvm_pagealloc_pg(struct vm_page *pg, struct uvm_object *obj, voff_t off,
682     struct vm_anon *anon)
683 {
684 	int	flags;
685 
686 	flags = PG_BUSY | PG_FAKE;
687 	pg->offset = off;
688 	pg->uobject = obj;
689 	pg->uanon = anon;
690 
691 	if (anon) {
692 		anon->an_page = pg;
693 		flags |= PQ_ANON;
694 	} else if (obj)
695 		uvm_pageinsert(pg);
696 	atomic_setbits_int(&pg->pg_flags, flags);
697 #if defined(UVM_PAGE_TRKOWN)
698 	pg->owner_tag = NULL;
699 #endif
700 	UVM_PAGE_OWN(pg, "new alloc");
701 }
702 
703 /*
704  * uvm_pglistalloc: allocate a list of pages
705  *
706  * => allocated pages are placed at the tail of rlist.  rlist is
707  *    assumed to be properly initialized by caller.
708  * => returns 0 on success or errno on failure
709  * => doesn't take into account clean non-busy pages on inactive list
710  *	that could be used(?)
711  * => params:
712  *	size		the size of the allocation, rounded to page size.
713  *	low		the low address of the allowed allocation range.
714  *	high		the high address of the allowed allocation range.
715  *	alignment	memory must be aligned to this power-of-two boundary.
716  *	boundary	no segment in the allocation may cross this
717  *			power-of-two boundary (relative to zero).
718  * => flags:
719  *	UVM_PLA_NOWAIT	fail if allocation fails
720  *	UVM_PLA_WAITOK	wait for memory to become avail
721  *	UVM_PLA_ZERO	return zeroed memory
722  */
723 int
724 uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
725     paddr_t boundary, struct pglist *rlist, int nsegs, int flags)
726 {
727 	KASSERT((alignment & (alignment - 1)) == 0);
728 	KASSERT((boundary & (boundary - 1)) == 0);
729 	KASSERT(!(flags & UVM_PLA_WAITOK) ^ !(flags & UVM_PLA_NOWAIT));
730 
731 	if (size == 0)
732 		return EINVAL;
733 	size = atop(round_page(size));
734 
735 	/*
736 	 * XXX uvm_pglistalloc is currently only used for kernel
737 	 * objects. Unlike the checks in uvm_pagealloc, below, here
738 	 * we are always allowed to use the kernel reserve.
739 	 */
740 	flags |= UVM_PLA_USERESERVE;
741 
742 	if ((high & PAGE_MASK) != PAGE_MASK) {
743 		printf("uvm_pglistalloc: Upper boundary 0x%lx "
744 		    "not on pagemask.\n", (unsigned long)high);
745 	}
746 
747 	/*
748 	 * Our allocations are always page granularity, so our alignment
749 	 * must be, too.
750 	 */
751 	if (alignment < PAGE_SIZE)
752 		alignment = PAGE_SIZE;
753 
754 	low = atop(roundup(low, alignment));
755 	/*
756 	 * high + 1 may result in overflow, in which case high becomes 0x0,
757 	 * which is the 'don't care' value.
758 	 * The only requirement in that case is that low is also 0x0, or the
759 	 * low<high assert will fail.
760 	 */
761 	high = atop(high + 1);
762 	alignment = atop(alignment);
763 	if (boundary < PAGE_SIZE && boundary != 0)
764 		boundary = PAGE_SIZE;
765 	boundary = atop(boundary);
766 
767 	return uvm_pmr_getpages(size, low, high, alignment, boundary, nsegs,
768 	    flags, rlist);
769 }
770 
771 /*
772  * uvm_pglistfree: free a list of pages
773  *
774  * => pages should already be unmapped
775  */
776 void
777 uvm_pglistfree(struct pglist *list)
778 {
779 	uvm_pmr_freepageq(list);
780 }
781 
782 /*
783  * interface used by the buffer cache to allocate a buffer at a time.
784  * The pages are allocated wired in DMA accessible memory
785  */
786 int
787 uvm_pagealloc_multi(struct uvm_object *obj, voff_t off, vsize_t size,
788     int flags)
789 {
790 	struct pglist    plist;
791 	struct vm_page  *pg;
792 	int              i, r;
793 
794 	KASSERT(UVM_OBJ_IS_BUFCACHE(obj));
795 	KERNEL_ASSERT_LOCKED();
796 
797 	TAILQ_INIT(&plist);
798 	r = uvm_pglistalloc(size, dma_constraint.ucr_low,
799 	    dma_constraint.ucr_high, 0, 0, &plist, atop(round_page(size)),
800 	    flags);
801 	if (r == 0) {
802 		i = 0;
803 		while ((pg = TAILQ_FIRST(&plist)) != NULL) {
804 			pg->wire_count = 1;
805 			atomic_setbits_int(&pg->pg_flags, PG_CLEAN | PG_FAKE);
806 			KASSERT((pg->pg_flags & PG_DEV) == 0);
807 			TAILQ_REMOVE(&plist, pg, pageq);
808 			uvm_pagealloc_pg(pg, obj, off + ptoa(i++), NULL);
809 		}
810 	}
811 	return r;
812 }
813 
814 /*
815  * interface used by the buffer cache to reallocate a buffer at a time.
816  * The pages are reallocated wired outside the DMA accessible region.
817  *
818  */
819 int
820 uvm_pagerealloc_multi(struct uvm_object *obj, voff_t off, vsize_t size,
821     int flags, struct uvm_constraint_range *where)
822 {
823 	struct pglist    plist;
824 	struct vm_page  *pg, *tpg;
825 	int              i, r;
826 	voff_t		offset;
827 
828 	KASSERT(UVM_OBJ_IS_BUFCACHE(obj));
829 	KERNEL_ASSERT_LOCKED();
830 
831 	TAILQ_INIT(&plist);
832 	if (size == 0)
833 		panic("size 0 uvm_pagerealloc");
834 	r = uvm_pglistalloc(size, where->ucr_low, where->ucr_high, 0,
835 	    0, &plist, atop(round_page(size)), flags);
836 	if (r == 0) {
837 		i = 0;
838 		while((pg = TAILQ_FIRST(&plist)) != NULL) {
839 			offset = off + ptoa(i++);
840 			tpg = uvm_pagelookup(obj, offset);
841 			KASSERT(tpg != NULL);
842 			pg->wire_count = 1;
843 			atomic_setbits_int(&pg->pg_flags, PG_CLEAN | PG_FAKE);
844 			KASSERT((pg->pg_flags & PG_DEV) == 0);
845 			TAILQ_REMOVE(&plist, pg, pageq);
846 			uvm_pagecopy(tpg, pg);
847 			KASSERT(tpg->wire_count == 1);
848 			tpg->wire_count = 0;
849 			uvm_pagefree(tpg);
850 			uvm_pagealloc_pg(pg, obj, offset, NULL);
851 		}
852 	}
853 	return r;
854 }
855 
856 /*
857  * uvm_pagealloc: allocate vm_page from a particular free list.
858  *
859  * => return null if no pages free
860  * => wake up pagedaemon if number of free pages drops below low water mark
861  * => only one of obj or anon can be non-null
862  * => caller must activate/deactivate page if it is not wired.
863  */
864 
865 struct vm_page *
866 uvm_pagealloc(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
867     int flags)
868 {
869 	struct vm_page *pg;
870 	struct pglist pgl;
871 	int pmr_flags;
872 
873 	KASSERT(obj == NULL || anon == NULL);
874 	KASSERT(anon == NULL || off == 0);
875 	KASSERT(off == trunc_page(off));
876 
877 	pmr_flags = UVM_PLA_NOWAIT;
878 
879 	/*
880 	 * We're allowed to use the kernel reserve if the page is
881 	 * being allocated to a kernel object.
882 	 */
883 	if ((flags & UVM_PGA_USERESERVE) ||
884 	    (obj != NULL && UVM_OBJ_IS_KERN_OBJECT(obj)))
885 	    	pmr_flags |= UVM_PLA_USERESERVE;
886 
887 	if (flags & UVM_PGA_ZERO)
888 		pmr_flags |= UVM_PLA_ZERO;
889 	TAILQ_INIT(&pgl);
890 	if (uvm_pmr_getpages(1, 0, 0, 1, 0, 1, pmr_flags, &pgl) != 0)
891 		goto fail;
892 
893 	pg = TAILQ_FIRST(&pgl);
894 	KASSERT(pg != NULL && TAILQ_NEXT(pg, pageq) == NULL);
895 
896 	uvm_pagealloc_pg(pg, obj, off, anon);
897 	KASSERT((pg->pg_flags & PG_DEV) == 0);
898 	if (flags & UVM_PGA_ZERO)
899 		atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
900 	else
901 		atomic_setbits_int(&pg->pg_flags, PG_CLEAN);
902 
903 	return pg;
904 
905 fail:
906 	return NULL;
907 }
908 
909 /*
910  * uvm_pagerealloc: reallocate a page from one object to another
911  */
912 
913 void
914 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
915 {
916 
917 	/* remove it from the old object */
918 	if (pg->uobject) {
919 		uvm_pageremove(pg);
920 	}
921 
922 	/* put it in the new object */
923 	if (newobj) {
924 		pg->uobject = newobj;
925 		pg->offset = newoff;
926 		pg->pg_version++;
927 		uvm_pageinsert(pg);
928 	}
929 }
930 
931 /*
932  * uvm_pageclean: clean page
933  *
934  * => erase page's identity (i.e. remove from object)
935  * => caller must lock page queues if `pg' is managed
936  * => assumes all valid mappings of pg are gone
937  */
938 void
939 uvm_pageclean(struct vm_page *pg)
940 {
941 	u_int flags_to_clear = 0;
942 
943 #if all_pmap_are_fixed
944 	if (pg->pg_flags & (PG_TABLED|PQ_ACTIVE|PQ_INACTIVE))
945 		MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
946 #endif
947 
948 #ifdef DEBUG
949 	if (pg->uobject == (void *)0xdeadbeef &&
950 	    pg->uanon == (void *)0xdeadbeef) {
951 		panic("uvm_pagefree: freeing free page %p", pg);
952 	}
953 #endif
954 
955 	KASSERT((pg->pg_flags & PG_DEV) == 0);
956 
957 	/*
958 	 * if the page was an object page (and thus "TABLED"), remove it
959 	 * from the object.
960 	 */
961 	if (pg->pg_flags & PG_TABLED)
962 		uvm_pageremove(pg);
963 
964 	/* now remove the page from the queues */
965 	if (pg->pg_flags & PQ_ACTIVE) {
966 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
967 		flags_to_clear |= PQ_ACTIVE;
968 		uvmexp.active--;
969 	}
970 	if (pg->pg_flags & PQ_INACTIVE) {
971 		if (pg->pg_flags & PQ_SWAPBACKED)
972 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
973 		else
974 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
975 		flags_to_clear |= PQ_INACTIVE;
976 		uvmexp.inactive--;
977 	}
978 
979 	/* if the page was wired, unwire it now. */
980 	if (pg->wire_count) {
981 		pg->wire_count = 0;
982 		uvmexp.wired--;
983 	}
984 	if (pg->uanon) {
985 		pg->uanon->an_page = NULL;
986 		pg->uanon = NULL;
987 	}
988 
989 	/* Clean page state bits. */
990 	flags_to_clear |= PQ_ANON|PQ_AOBJ|PQ_ENCRYPT|PG_ZERO|PG_FAKE|PG_BUSY|
991 	    PG_RELEASED|PG_CLEAN|PG_CLEANCHK;
992 	atomic_clearbits_int(&pg->pg_flags, flags_to_clear);
993 
994 #ifdef DEBUG
995 	pg->uobject = (void *)0xdeadbeef;
996 	pg->offset = 0xdeadbeef;
997 	pg->uanon = (void *)0xdeadbeef;
998 #endif
999 }
1000 
1001 /*
1002  * uvm_pagefree: free page
1003  *
1004  * => erase page's identity (i.e. remove from object)
1005  * => put page on free list
1006  * => caller must lock page queues if `pg' is managed
1007  * => assumes all valid mappings of pg are gone
1008  */
1009 void
1010 uvm_pagefree(struct vm_page *pg)
1011 {
1012 #if all_pmap_are_fixed
1013 	if (pg->pg_flags & (PG_TABLED|PQ_ACTIVE|PQ_INACTIVE))
1014 		MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1015 #endif
1016 
1017 	uvm_pageclean(pg);
1018 	uvm_pmr_freepages(pg, 1);
1019 }
1020 
1021 /*
1022  * uvm_page_unbusy: unbusy an array of pages.
1023  *
1024  * => pages must either all belong to the same object, or all belong to anons.
1025  * => if pages are anon-owned, anons must have 0 refcount.
1026  */
1027 void
1028 uvm_page_unbusy(struct vm_page **pgs, int npgs)
1029 {
1030 	struct vm_page *pg;
1031 	struct uvm_object *uobj;
1032 	int i;
1033 
1034 	for (i = 0; i < npgs; i++) {
1035 		pg = pgs[i];
1036 
1037 		if (pg == NULL || pg == PGO_DONTCARE) {
1038 			continue;
1039 		}
1040 		if (pg->pg_flags & PG_WANTED) {
1041 			wakeup(pg);
1042 		}
1043 		if (pg->pg_flags & PG_RELEASED) {
1044 			uobj = pg->uobject;
1045 			if (uobj != NULL) {
1046 				uvm_lock_pageq();
1047 				pmap_page_protect(pg, PROT_NONE);
1048 				/* XXX won't happen right now */
1049 				if (pg->pg_flags & PQ_AOBJ)
1050 					uao_dropswap(uobj,
1051 					    pg->offset >> PAGE_SHIFT);
1052 				uvm_pagefree(pg);
1053 				uvm_unlock_pageq();
1054 			} else {
1055 				atomic_clearbits_int(&pg->pg_flags, PG_BUSY);
1056 				UVM_PAGE_OWN(pg, NULL);
1057 				rw_enter(pg->uanon->an_lock, RW_WRITE);
1058 				uvm_anon_release(pg->uanon);
1059 			}
1060 		} else {
1061 			atomic_clearbits_int(&pg->pg_flags, PG_WANTED|PG_BUSY);
1062 			UVM_PAGE_OWN(pg, NULL);
1063 		}
1064 	}
1065 }
1066 
1067 #if defined(UVM_PAGE_TRKOWN)
1068 /*
1069  * uvm_page_own: set or release page ownership
1070  *
1071  * => this is a debugging function that keeps track of who sets PG_BUSY
1072  *	and where they do it.   it can be used to track down problems
1073  *	such a thread setting "PG_BUSY" and never releasing it.
1074  * => if "tag" is NULL then we are releasing page ownership
1075  */
1076 void
1077 uvm_page_own(struct vm_page *pg, char *tag)
1078 {
1079 	/* gain ownership? */
1080 	if (tag) {
1081 		if (pg->owner_tag) {
1082 			printf("uvm_page_own: page %p already owned "
1083 			    "by thread %d [%s]\n", pg,
1084 			     pg->owner, pg->owner_tag);
1085 			panic("uvm_page_own");
1086 		}
1087 		pg->owner = (curproc) ? curproc->p_tid :  (pid_t) -1;
1088 		pg->owner_tag = tag;
1089 		return;
1090 	}
1091 
1092 	/* drop ownership */
1093 	if (pg->owner_tag == NULL) {
1094 		printf("uvm_page_own: dropping ownership of an non-owned "
1095 		    "page (%p)\n", pg);
1096 		panic("uvm_page_own");
1097 	}
1098 	pg->owner_tag = NULL;
1099 	return;
1100 }
1101 #endif
1102 
1103 /*
1104  * when VM_PHYSSEG_MAX is 1, we can simplify these functions
1105  */
1106 
1107 #if VM_PHYSSEG_MAX > 1
1108 /*
1109  * vm_physseg_find: find vm_physseg structure that belongs to a PA
1110  */
1111 int
1112 vm_physseg_find(paddr_t pframe, int *offp)
1113 {
1114 	struct vm_physseg *seg;
1115 
1116 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
1117 	/* binary search for it */
1118 	int	start, len, try;
1119 
1120 	/*
1121 	 * if try is too large (thus target is less than than try) we reduce
1122 	 * the length to trunc(len/2) [i.e. everything smaller than "try"]
1123 	 *
1124 	 * if the try is too small (thus target is greater than try) then
1125 	 * we set the new start to be (try + 1).   this means we need to
1126 	 * reduce the length to (round(len/2) - 1).
1127 	 *
1128 	 * note "adjust" below which takes advantage of the fact that
1129 	 *  (round(len/2) - 1) == trunc((len - 1) / 2)
1130 	 * for any value of len we may have
1131 	 */
1132 
1133 	for (start = 0, len = vm_nphysseg ; len != 0 ; len = len / 2) {
1134 		try = start + (len / 2);	/* try in the middle */
1135 		seg = vm_physmem + try;
1136 
1137 		/* start past our try? */
1138 		if (pframe >= seg->start) {
1139 			/* was try correct? */
1140 			if (pframe < seg->end) {
1141 				if (offp)
1142 					*offp = pframe - seg->start;
1143 				return try;            /* got it */
1144 			}
1145 			start = try + 1;	/* next time, start here */
1146 			len--;			/* "adjust" */
1147 		} else {
1148 			/*
1149 			 * pframe before try, just reduce length of
1150 			 * region, done in "for" loop
1151 			 */
1152 		}
1153 	}
1154 	return -1;
1155 
1156 #else
1157 	/* linear search for it */
1158 	int	lcv;
1159 
1160 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) {
1161 		if (pframe >= seg->start && pframe < seg->end) {
1162 			if (offp)
1163 				*offp = pframe - seg->start;
1164 			return lcv;		   /* got it */
1165 		}
1166 	}
1167 	return -1;
1168 
1169 #endif
1170 }
1171 
1172 /*
1173  * PHYS_TO_VM_PAGE: find vm_page for a PA.   used by MI code to get vm_pages
1174  * back from an I/O mapping (ugh!).   used in some MD code as well.
1175  */
1176 struct vm_page *
1177 PHYS_TO_VM_PAGE(paddr_t pa)
1178 {
1179 	paddr_t pf = atop(pa);
1180 	int	off;
1181 	int	psi;
1182 
1183 	psi = vm_physseg_find(pf, &off);
1184 
1185 	return (psi == -1) ? NULL : &vm_physmem[psi].pgs[off];
1186 }
1187 #endif /* VM_PHYSSEG_MAX > 1 */
1188 
1189 /*
1190  * uvm_pagelookup: look up a page
1191  */
1192 struct vm_page *
1193 uvm_pagelookup(struct uvm_object *obj, voff_t off)
1194 {
1195 	/* XXX if stack is too much, handroll */
1196 	struct vm_page pg;
1197 
1198 	pg.offset = off;
1199 	return RBT_FIND(uvm_objtree, &obj->memt, &pg);
1200 }
1201 
1202 /*
1203  * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
1204  *
1205  * => caller must lock page queues
1206  */
1207 void
1208 uvm_pagewire(struct vm_page *pg)
1209 {
1210 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1211 
1212 	if (pg->wire_count == 0) {
1213 		if (pg->pg_flags & PQ_ACTIVE) {
1214 			TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1215 			atomic_clearbits_int(&pg->pg_flags, PQ_ACTIVE);
1216 			uvmexp.active--;
1217 		}
1218 		if (pg->pg_flags & PQ_INACTIVE) {
1219 			if (pg->pg_flags & PQ_SWAPBACKED)
1220 				TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
1221 			else
1222 				TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
1223 			atomic_clearbits_int(&pg->pg_flags, PQ_INACTIVE);
1224 			uvmexp.inactive--;
1225 		}
1226 		uvmexp.wired++;
1227 	}
1228 	pg->wire_count++;
1229 }
1230 
1231 /*
1232  * uvm_pageunwire: unwire the page.
1233  *
1234  * => activate if wire count goes to zero.
1235  * => caller must lock page queues
1236  */
1237 void
1238 uvm_pageunwire(struct vm_page *pg)
1239 {
1240 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1241 
1242 	pg->wire_count--;
1243 	if (pg->wire_count == 0) {
1244 		TAILQ_INSERT_TAIL(&uvm.page_active, pg, pageq);
1245 		uvmexp.active++;
1246 		atomic_setbits_int(&pg->pg_flags, PQ_ACTIVE);
1247 		uvmexp.wired--;
1248 	}
1249 }
1250 
1251 /*
1252  * uvm_pagedeactivate: deactivate page -- no pmaps have access to page
1253  *
1254  * => caller must lock page queues
1255  * => caller must check to make sure page is not wired
1256  * => object that page belongs to must be locked (so we can adjust pg->flags)
1257  */
1258 void
1259 uvm_pagedeactivate(struct vm_page *pg)
1260 {
1261 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1262 
1263 	if (pg->pg_flags & PQ_ACTIVE) {
1264 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1265 		atomic_clearbits_int(&pg->pg_flags, PQ_ACTIVE);
1266 		uvmexp.active--;
1267 	}
1268 	if ((pg->pg_flags & PQ_INACTIVE) == 0) {
1269 		KASSERT(pg->wire_count == 0);
1270 		if (pg->pg_flags & PQ_SWAPBACKED)
1271 			TAILQ_INSERT_TAIL(&uvm.page_inactive_swp, pg, pageq);
1272 		else
1273 			TAILQ_INSERT_TAIL(&uvm.page_inactive_obj, pg, pageq);
1274 		atomic_setbits_int(&pg->pg_flags, PQ_INACTIVE);
1275 		uvmexp.inactive++;
1276 		pmap_clear_reference(pg);
1277 		/*
1278 		 * update the "clean" bit.  this isn't 100%
1279 		 * accurate, and doesn't have to be.  we'll
1280 		 * re-sync it after we zap all mappings when
1281 		 * scanning the inactive list.
1282 		 */
1283 		if ((pg->pg_flags & PG_CLEAN) != 0 &&
1284 		    pmap_is_modified(pg))
1285 			atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
1286 	}
1287 }
1288 
1289 /*
1290  * uvm_pageactivate: activate page
1291  *
1292  * => caller must lock page queues
1293  */
1294 void
1295 uvm_pageactivate(struct vm_page *pg)
1296 {
1297 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1298 
1299 	if (pg->pg_flags & PQ_INACTIVE) {
1300 		if (pg->pg_flags & PQ_SWAPBACKED)
1301 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
1302 		else
1303 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
1304 		atomic_clearbits_int(&pg->pg_flags, PQ_INACTIVE);
1305 		uvmexp.inactive--;
1306 	}
1307 	if (pg->wire_count == 0) {
1308 		/*
1309 		 * if page is already active, remove it from list so we
1310 		 * can put it at tail.  if it wasn't active, then mark
1311 		 * it active and bump active count
1312 		 */
1313 		if (pg->pg_flags & PQ_ACTIVE)
1314 			TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1315 		else {
1316 			atomic_setbits_int(&pg->pg_flags, PQ_ACTIVE);
1317 			uvmexp.active++;
1318 		}
1319 
1320 		TAILQ_INSERT_TAIL(&uvm.page_active, pg, pageq);
1321 	}
1322 }
1323 
1324 /*
1325  * uvm_pagezero: zero fill a page
1326  */
1327 void
1328 uvm_pagezero(struct vm_page *pg)
1329 {
1330 	atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
1331 	pmap_zero_page(pg);
1332 }
1333 
1334 /*
1335  * uvm_pagecopy: copy a page
1336  */
1337 void
1338 uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
1339 {
1340 	atomic_clearbits_int(&dst->pg_flags, PG_CLEAN);
1341 	pmap_copy_page(src, dst);
1342 }
1343 
1344 /*
1345  * uvm_pagecount: count the number of physical pages in the address range.
1346  */
1347 psize_t
1348 uvm_pagecount(struct uvm_constraint_range* constraint)
1349 {
1350 	int lcv;
1351 	psize_t sz;
1352 	paddr_t low, high;
1353 	paddr_t ps_low, ps_high;
1354 
1355 	/* Algorithm uses page numbers. */
1356 	low = atop(constraint->ucr_low);
1357 	high = atop(constraint->ucr_high);
1358 
1359 	sz = 0;
1360 	for (lcv = 0; lcv < vm_nphysseg; lcv++) {
1361 		ps_low = MAX(low, vm_physmem[lcv].avail_start);
1362 		ps_high = MIN(high, vm_physmem[lcv].avail_end);
1363 		if (ps_low < ps_high)
1364 			sz += ps_high - ps_low;
1365 	}
1366 	return sz;
1367 }
1368