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