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