xref: /openbsd-src/sys/uvm/uvm_page.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /*	$OpenBSD: uvm_page.c,v 1.154 2020/12/02 16:32:00 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 = (struct vm_page *)uvm_km_zalloc(kernel_map,
546 		    npages * sizeof(*pgs));
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 
795 	TAILQ_INIT(&plist);
796 	r = uvm_pglistalloc(size, dma_constraint.ucr_low,
797 	    dma_constraint.ucr_high, 0, 0, &plist, atop(round_page(size)),
798 	    flags);
799 	if (r == 0) {
800 		i = 0;
801 		while ((pg = TAILQ_FIRST(&plist)) != NULL) {
802 			pg->wire_count = 1;
803 			atomic_setbits_int(&pg->pg_flags, PG_CLEAN | PG_FAKE);
804 			KASSERT((pg->pg_flags & PG_DEV) == 0);
805 			TAILQ_REMOVE(&plist, pg, pageq);
806 			uvm_pagealloc_pg(pg, obj, off + ptoa(i++), NULL);
807 		}
808 	}
809 	return r;
810 }
811 
812 /*
813  * interface used by the buffer cache to reallocate a buffer at a time.
814  * The pages are reallocated wired outside the DMA accessible region.
815  *
816  */
817 int
818 uvm_pagerealloc_multi(struct uvm_object *obj, voff_t off, vsize_t size,
819     int flags, struct uvm_constraint_range *where)
820 {
821 	struct pglist    plist;
822 	struct vm_page  *pg, *tpg;
823 	int              i, r;
824 	voff_t		offset;
825 
826 
827 	TAILQ_INIT(&plist);
828 	if (size == 0)
829 		panic("size 0 uvm_pagerealloc");
830 	r = uvm_pglistalloc(size, where->ucr_low, where->ucr_high, 0,
831 	    0, &plist, atop(round_page(size)), flags);
832 	if (r == 0) {
833 		i = 0;
834 		while((pg = TAILQ_FIRST(&plist)) != NULL) {
835 			offset = off + ptoa(i++);
836 			tpg = uvm_pagelookup(obj, offset);
837 			KASSERT(tpg != NULL);
838 			pg->wire_count = 1;
839 			atomic_setbits_int(&pg->pg_flags, PG_CLEAN | PG_FAKE);
840 			KASSERT((pg->pg_flags & PG_DEV) == 0);
841 			TAILQ_REMOVE(&plist, pg, pageq);
842 			uvm_pagecopy(tpg, pg);
843 			KASSERT(tpg->wire_count == 1);
844 			tpg->wire_count = 0;
845 			uvm_pagefree(tpg);
846 			uvm_pagealloc_pg(pg, obj, offset, NULL);
847 		}
848 	}
849 	return r;
850 }
851 
852 /*
853  * uvm_pagealloc: allocate vm_page from a particular free list.
854  *
855  * => return null if no pages free
856  * => wake up pagedaemon if number of free pages drops below low water mark
857  * => only one of obj or anon can be non-null
858  * => caller must activate/deactivate page if it is not wired.
859  */
860 
861 struct vm_page *
862 uvm_pagealloc(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
863     int flags)
864 {
865 	struct vm_page *pg;
866 	struct pglist pgl;
867 	int pmr_flags;
868 
869 	KASSERT(obj == NULL || anon == NULL);
870 	KASSERT(anon == NULL || off == 0);
871 	KASSERT(off == trunc_page(off));
872 
873 	pmr_flags = UVM_PLA_NOWAIT;
874 
875 	/*
876 	 * We're allowed to use the kernel reserve if the page is
877 	 * being allocated to a kernel object.
878 	 */
879 	if ((flags & UVM_PGA_USERESERVE) ||
880 	    (obj != NULL && UVM_OBJ_IS_KERN_OBJECT(obj)))
881 	    	pmr_flags |= UVM_PLA_USERESERVE;
882 
883 	if (flags & UVM_PGA_ZERO)
884 		pmr_flags |= UVM_PLA_ZERO;
885 	TAILQ_INIT(&pgl);
886 	if (uvm_pmr_getpages(1, 0, 0, 1, 0, 1, pmr_flags, &pgl) != 0)
887 		goto fail;
888 
889 	pg = TAILQ_FIRST(&pgl);
890 	KASSERT(pg != NULL && TAILQ_NEXT(pg, pageq) == NULL);
891 
892 	uvm_pagealloc_pg(pg, obj, off, anon);
893 	KASSERT((pg->pg_flags & PG_DEV) == 0);
894 	if (flags & UVM_PGA_ZERO)
895 		atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
896 	else
897 		atomic_setbits_int(&pg->pg_flags, PG_CLEAN);
898 
899 	return(pg);
900 
901 fail:
902 	return (NULL);
903 }
904 
905 /*
906  * uvm_pagerealloc: reallocate a page from one object to another
907  */
908 
909 void
910 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
911 {
912 
913 	/* remove it from the old object */
914 	if (pg->uobject) {
915 		uvm_pageremove(pg);
916 	}
917 
918 	/* put it in the new object */
919 	if (newobj) {
920 		pg->uobject = newobj;
921 		pg->offset = newoff;
922 		pg->pg_version++;
923 		uvm_pageinsert(pg);
924 	}
925 }
926 
927 /*
928  * uvm_pageclean: clean page
929  *
930  * => erase page's identity (i.e. remove from object)
931  * => caller must lock page queues if `pg' is managed
932  * => assumes all valid mappings of pg are gone
933  */
934 void
935 uvm_pageclean(struct vm_page *pg)
936 {
937 	u_int flags_to_clear = 0;
938 
939 #if all_pmap_are_fixed
940 	if (pg->pg_flags & (PG_TABLED|PQ_ACTIVE|PQ_INACTIVE))
941 		MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
942 #endif
943 
944 #ifdef DEBUG
945 	if (pg->uobject == (void *)0xdeadbeef &&
946 	    pg->uanon == (void *)0xdeadbeef) {
947 		panic("uvm_pagefree: freeing free page %p", pg);
948 	}
949 #endif
950 
951 	KASSERT((pg->pg_flags & PG_DEV) == 0);
952 
953 	/*
954 	 * if the page was an object page (and thus "TABLED"), remove it
955 	 * from the object.
956 	 */
957 	if (pg->pg_flags & PG_TABLED)
958 		uvm_pageremove(pg);
959 
960 	/* now remove the page from the queues */
961 	if (pg->pg_flags & PQ_ACTIVE) {
962 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
963 		flags_to_clear |= PQ_ACTIVE;
964 		uvmexp.active--;
965 	}
966 	if (pg->pg_flags & PQ_INACTIVE) {
967 		if (pg->pg_flags & PQ_SWAPBACKED)
968 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
969 		else
970 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
971 		flags_to_clear |= PQ_INACTIVE;
972 		uvmexp.inactive--;
973 	}
974 
975 	/* if the page was wired, unwire it now. */
976 	if (pg->wire_count) {
977 		pg->wire_count = 0;
978 		uvmexp.wired--;
979 	}
980 	if (pg->uanon) {
981 		pg->uanon->an_page = NULL;
982 		pg->uanon = NULL;
983 	}
984 
985 	/* Clean page state bits. */
986 	flags_to_clear |= PQ_ANON|PQ_AOBJ|PQ_ENCRYPT|PG_ZERO|PG_FAKE|PG_BUSY|
987 	    PG_RELEASED|PG_CLEAN|PG_CLEANCHK;
988 	atomic_clearbits_int(&pg->pg_flags, flags_to_clear);
989 
990 #ifdef DEBUG
991 	pg->uobject = (void *)0xdeadbeef;
992 	pg->offset = 0xdeadbeef;
993 	pg->uanon = (void *)0xdeadbeef;
994 #endif
995 }
996 
997 /*
998  * uvm_pagefree: free page
999  *
1000  * => erase page's identity (i.e. remove from object)
1001  * => put page on free list
1002  * => caller must lock page queues if `pg' is managed
1003  * => assumes all valid mappings of pg are gone
1004  */
1005 void
1006 uvm_pagefree(struct vm_page *pg)
1007 {
1008 #if all_pmap_are_fixed
1009 	if (pg->pg_flags & (PG_TABLED|PQ_ACTIVE|PQ_INACTIVE))
1010 		MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1011 #endif
1012 
1013 	uvm_pageclean(pg);
1014 	uvm_pmr_freepages(pg, 1);
1015 }
1016 
1017 /*
1018  * uvm_page_unbusy: unbusy an array of pages.
1019  *
1020  * => pages must either all belong to the same object, or all belong to anons.
1021  * => if pages are anon-owned, anons must have 0 refcount.
1022  */
1023 void
1024 uvm_page_unbusy(struct vm_page **pgs, int npgs)
1025 {
1026 	struct vm_page *pg;
1027 	struct uvm_object *uobj;
1028 	int i;
1029 
1030 	for (i = 0; i < npgs; i++) {
1031 		pg = pgs[i];
1032 
1033 		if (pg == NULL || pg == PGO_DONTCARE) {
1034 			continue;
1035 		}
1036 		if (pg->pg_flags & PG_WANTED) {
1037 			wakeup(pg);
1038 		}
1039 		if (pg->pg_flags & PG_RELEASED) {
1040 			uobj = pg->uobject;
1041 			if (uobj != NULL) {
1042 				uvm_lock_pageq();
1043 				pmap_page_protect(pg, PROT_NONE);
1044 				/* XXX won't happen right now */
1045 				if (pg->pg_flags & PQ_AOBJ)
1046 					uao_dropswap(uobj,
1047 					    pg->offset >> PAGE_SHIFT);
1048 				uvm_pagefree(pg);
1049 				uvm_unlock_pageq();
1050 			} else {
1051 				atomic_clearbits_int(&pg->pg_flags, PG_BUSY);
1052 				UVM_PAGE_OWN(pg, NULL);
1053 				uvm_anfree(pg->uanon);
1054 			}
1055 		} else {
1056 			atomic_clearbits_int(&pg->pg_flags, PG_WANTED|PG_BUSY);
1057 			UVM_PAGE_OWN(pg, NULL);
1058 		}
1059 	}
1060 }
1061 
1062 #if defined(UVM_PAGE_TRKOWN)
1063 /*
1064  * uvm_page_own: set or release page ownership
1065  *
1066  * => this is a debugging function that keeps track of who sets PG_BUSY
1067  *	and where they do it.   it can be used to track down problems
1068  *	such a thread setting "PG_BUSY" and never releasing it.
1069  * => if "tag" is NULL then we are releasing page ownership
1070  */
1071 void
1072 uvm_page_own(struct vm_page *pg, char *tag)
1073 {
1074 	/* gain ownership? */
1075 	if (tag) {
1076 		if (pg->owner_tag) {
1077 			printf("uvm_page_own: page %p already owned "
1078 			    "by thread %d [%s]\n", pg,
1079 			     pg->owner, pg->owner_tag);
1080 			panic("uvm_page_own");
1081 		}
1082 		pg->owner = (curproc) ? curproc->p_tid :  (pid_t) -1;
1083 		pg->owner_tag = tag;
1084 		return;
1085 	}
1086 
1087 	/* drop ownership */
1088 	if (pg->owner_tag == NULL) {
1089 		printf("uvm_page_own: dropping ownership of an non-owned "
1090 		    "page (%p)\n", pg);
1091 		panic("uvm_page_own");
1092 	}
1093 	pg->owner_tag = NULL;
1094 	return;
1095 }
1096 #endif
1097 
1098 /*
1099  * when VM_PHYSSEG_MAX is 1, we can simplify these functions
1100  */
1101 
1102 #if VM_PHYSSEG_MAX > 1
1103 /*
1104  * vm_physseg_find: find vm_physseg structure that belongs to a PA
1105  */
1106 int
1107 vm_physseg_find(paddr_t pframe, int *offp)
1108 {
1109 	struct vm_physseg *seg;
1110 
1111 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
1112 	/* binary search for it */
1113 	int	start, len, try;
1114 
1115 	/*
1116 	 * if try is too large (thus target is less than than try) we reduce
1117 	 * the length to trunc(len/2) [i.e. everything smaller than "try"]
1118 	 *
1119 	 * if the try is too small (thus target is greater than try) then
1120 	 * we set the new start to be (try + 1).   this means we need to
1121 	 * reduce the length to (round(len/2) - 1).
1122 	 *
1123 	 * note "adjust" below which takes advantage of the fact that
1124 	 *  (round(len/2) - 1) == trunc((len - 1) / 2)
1125 	 * for any value of len we may have
1126 	 */
1127 
1128 	for (start = 0, len = vm_nphysseg ; len != 0 ; len = len / 2) {
1129 		try = start + (len / 2);	/* try in the middle */
1130 		seg = vm_physmem + try;
1131 
1132 		/* start past our try? */
1133 		if (pframe >= seg->start) {
1134 			/* was try correct? */
1135 			if (pframe < seg->end) {
1136 				if (offp)
1137 					*offp = pframe - seg->start;
1138 				return(try);            /* got it */
1139 			}
1140 			start = try + 1;	/* next time, start here */
1141 			len--;			/* "adjust" */
1142 		} else {
1143 			/*
1144 			 * pframe before try, just reduce length of
1145 			 * region, done in "for" loop
1146 			 */
1147 		}
1148 	}
1149 	return(-1);
1150 
1151 #else
1152 	/* linear search for it */
1153 	int	lcv;
1154 
1155 	for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) {
1156 		if (pframe >= seg->start && pframe < seg->end) {
1157 			if (offp)
1158 				*offp = pframe - seg->start;
1159 			return(lcv);		   /* got it */
1160 		}
1161 	}
1162 	return(-1);
1163 
1164 #endif
1165 }
1166 
1167 /*
1168  * PHYS_TO_VM_PAGE: find vm_page for a PA.   used by MI code to get vm_pages
1169  * back from an I/O mapping (ugh!).   used in some MD code as well.
1170  */
1171 struct vm_page *
1172 PHYS_TO_VM_PAGE(paddr_t pa)
1173 {
1174 	paddr_t pf = atop(pa);
1175 	int	off;
1176 	int	psi;
1177 
1178 	psi = vm_physseg_find(pf, &off);
1179 
1180 	return ((psi == -1) ? NULL : &vm_physmem[psi].pgs[off]);
1181 }
1182 #endif /* VM_PHYSSEG_MAX > 1 */
1183 
1184 /*
1185  * uvm_pagelookup: look up a page
1186  */
1187 struct vm_page *
1188 uvm_pagelookup(struct uvm_object *obj, voff_t off)
1189 {
1190 	/* XXX if stack is too much, handroll */
1191 	struct vm_page pg;
1192 
1193 	pg.offset = off;
1194 	return (RBT_FIND(uvm_objtree, &obj->memt, &pg));
1195 }
1196 
1197 /*
1198  * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
1199  *
1200  * => caller must lock page queues
1201  */
1202 void
1203 uvm_pagewire(struct vm_page *pg)
1204 {
1205 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1206 
1207 	if (pg->wire_count == 0) {
1208 		if (pg->pg_flags & PQ_ACTIVE) {
1209 			TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1210 			atomic_clearbits_int(&pg->pg_flags, PQ_ACTIVE);
1211 			uvmexp.active--;
1212 		}
1213 		if (pg->pg_flags & PQ_INACTIVE) {
1214 			if (pg->pg_flags & PQ_SWAPBACKED)
1215 				TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
1216 			else
1217 				TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
1218 			atomic_clearbits_int(&pg->pg_flags, PQ_INACTIVE);
1219 			uvmexp.inactive--;
1220 		}
1221 		uvmexp.wired++;
1222 	}
1223 	pg->wire_count++;
1224 }
1225 
1226 /*
1227  * uvm_pageunwire: unwire the page.
1228  *
1229  * => activate if wire count goes to zero.
1230  * => caller must lock page queues
1231  */
1232 void
1233 uvm_pageunwire(struct vm_page *pg)
1234 {
1235 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1236 
1237 	pg->wire_count--;
1238 	if (pg->wire_count == 0) {
1239 		TAILQ_INSERT_TAIL(&uvm.page_active, pg, pageq);
1240 		uvmexp.active++;
1241 		atomic_setbits_int(&pg->pg_flags, PQ_ACTIVE);
1242 		uvmexp.wired--;
1243 	}
1244 }
1245 
1246 /*
1247  * uvm_pagedeactivate: deactivate page -- no pmaps have access to page
1248  *
1249  * => caller must lock page queues
1250  * => caller must check to make sure page is not wired
1251  * => object that page belongs to must be locked (so we can adjust pg->flags)
1252  */
1253 void
1254 uvm_pagedeactivate(struct vm_page *pg)
1255 {
1256 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1257 
1258 	if (pg->pg_flags & PQ_ACTIVE) {
1259 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1260 		atomic_clearbits_int(&pg->pg_flags, PQ_ACTIVE);
1261 		uvmexp.active--;
1262 	}
1263 	if ((pg->pg_flags & PQ_INACTIVE) == 0) {
1264 		KASSERT(pg->wire_count == 0);
1265 		if (pg->pg_flags & PQ_SWAPBACKED)
1266 			TAILQ_INSERT_TAIL(&uvm.page_inactive_swp, pg, pageq);
1267 		else
1268 			TAILQ_INSERT_TAIL(&uvm.page_inactive_obj, pg, pageq);
1269 		atomic_setbits_int(&pg->pg_flags, PQ_INACTIVE);
1270 		uvmexp.inactive++;
1271 		pmap_clear_reference(pg);
1272 		/*
1273 		 * update the "clean" bit.  this isn't 100%
1274 		 * accurate, and doesn't have to be.  we'll
1275 		 * re-sync it after we zap all mappings when
1276 		 * scanning the inactive list.
1277 		 */
1278 		if ((pg->pg_flags & PG_CLEAN) != 0 &&
1279 		    pmap_is_modified(pg))
1280 			atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
1281 	}
1282 }
1283 
1284 /*
1285  * uvm_pageactivate: activate page
1286  *
1287  * => caller must lock page queues
1288  */
1289 void
1290 uvm_pageactivate(struct vm_page *pg)
1291 {
1292 	MUTEX_ASSERT_LOCKED(&uvm.pageqlock);
1293 
1294 	if (pg->pg_flags & PQ_INACTIVE) {
1295 		if (pg->pg_flags & PQ_SWAPBACKED)
1296 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
1297 		else
1298 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
1299 		atomic_clearbits_int(&pg->pg_flags, PQ_INACTIVE);
1300 		uvmexp.inactive--;
1301 	}
1302 	if (pg->wire_count == 0) {
1303 		/*
1304 		 * if page is already active, remove it from list so we
1305 		 * can put it at tail.  if it wasn't active, then mark
1306 		 * it active and bump active count
1307 		 */
1308 		if (pg->pg_flags & PQ_ACTIVE)
1309 			TAILQ_REMOVE(&uvm.page_active, pg, pageq);
1310 		else {
1311 			atomic_setbits_int(&pg->pg_flags, PQ_ACTIVE);
1312 			uvmexp.active++;
1313 		}
1314 
1315 		TAILQ_INSERT_TAIL(&uvm.page_active, pg, pageq);
1316 	}
1317 }
1318 
1319 /*
1320  * uvm_pagezero: zero fill a page
1321  */
1322 void
1323 uvm_pagezero(struct vm_page *pg)
1324 {
1325 	atomic_clearbits_int(&pg->pg_flags, PG_CLEAN);
1326 	pmap_zero_page(pg);
1327 }
1328 
1329 /*
1330  * uvm_pagecopy: copy a page
1331  */
1332 void
1333 uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
1334 {
1335 	atomic_clearbits_int(&dst->pg_flags, PG_CLEAN);
1336 	pmap_copy_page(src, dst);
1337 }
1338 
1339 /*
1340  * uvm_pagecount: count the number of physical pages in the address range.
1341  */
1342 psize_t
1343 uvm_pagecount(struct uvm_constraint_range* constraint)
1344 {
1345 	int lcv;
1346 	psize_t sz;
1347 	paddr_t low, high;
1348 	paddr_t ps_low, ps_high;
1349 
1350 	/* Algorithm uses page numbers. */
1351 	low = atop(constraint->ucr_low);
1352 	high = atop(constraint->ucr_high);
1353 
1354 	sz = 0;
1355 	for (lcv = 0; lcv < vm_nphysseg; lcv++) {
1356 		ps_low = MAX(low, vm_physmem[lcv].avail_start);
1357 		ps_high = MIN(high, vm_physmem[lcv].avail_end);
1358 		if (ps_low < ps_high)
1359 			sz += ps_high - ps_low;
1360 	}
1361 	return sz;
1362 }
1363