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