vm_page.c revision 101250
1210008Srdivacky/*
2210008Srdivacky * Copyright (c) 1991 Regents of the University of California.
3210008Srdivacky * All rights reserved.
4210008Srdivacky *
5210008Srdivacky * This code is derived from software contributed to Berkeley by
6210008Srdivacky * The Mach Operating System project at Carnegie-Mellon University.
7210008Srdivacky *
8210008Srdivacky * Redistribution and use in source and binary forms, with or without
9210008Srdivacky * modification, are permitted provided that the following conditions
10210008Srdivacky * are met:
11210008Srdivacky * 1. Redistributions of source code must retain the above copyright
12210008Srdivacky *    notice, this list of conditions and the following disclaimer.
13210008Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
14210008Srdivacky *    notice, this list of conditions and the following disclaimer in the
15210008Srdivacky *    documentation and/or other materials provided with the distribution.
16249423Sdim * 3. All advertising materials mentioning features or use of this software
17210008Srdivacky *    must display the following acknowledgement:
18210008Srdivacky *	This product includes software developed by the University of
19249423Sdim *	California, Berkeley and its contributors.
20249423Sdim * 4. Neither the name of the University nor the names of its contributors
21249423Sdim *    may be used to endorse or promote products derived from this software
22210008Srdivacky *    without specific prior written permission.
23210008Srdivacky *
24210008Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25210008Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26210008Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27210008Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28210008Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29210008Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30210008Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31210008Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32218893Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33218893Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34210008Srdivacky * SUCH DAMAGE.
35210008Srdivacky *
36210008Srdivacky *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
37210008Srdivacky * $FreeBSD: head/sys/vm/vm_page.c 101250 2002-08-03 01:29:52Z alc $
38210008Srdivacky */
39210008Srdivacky
40210008Srdivacky/*
41210008Srdivacky * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42210008Srdivacky * All rights reserved.
43234353Sdim *
44210008Srdivacky * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45210008Srdivacky *
46210008Srdivacky * Permission to use, copy, modify and distribute this software and
47210008Srdivacky * its documentation is hereby granted, provided that both the copyright
48221345Sdim * notice and this permission notice appear in all copies of the
49221345Sdim * software, derivative works or modified versions, and any portions
50221345Sdim * thereof, and that both notices appear in supporting documentation.
51221345Sdim *
52210008Srdivacky * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53210008Srdivacky * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54218893Sdim * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55218893Sdim *
56218893Sdim * Carnegie Mellon requests users of this software to return to
57218893Sdim *
58218893Sdim *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
59218893Sdim *  School of Computer Science
60221345Sdim *  Carnegie Mellon University
61218893Sdim *  Pittsburgh PA 15213-3890
62218893Sdim *
63218893Sdim * any improvements or extensions that they make and grant Carnegie the
64218893Sdim * rights to redistribute these changes.
65239462Sdim */
66221345Sdim
67218893Sdim/*
68218893Sdim *			GENERAL RULES ON VM_PAGE MANIPULATION
69218893Sdim *
70210008Srdivacky *	- a pageq mutex is required when adding or removing a page from a
71210008Srdivacky *	  page queue (vm_page_queue[]), regardless of other mutexes or the
72210008Srdivacky *	  busy state of a page.
73210008Srdivacky *
74210008Srdivacky *	- a hash chain mutex is required when associating or disassociating
75210008Srdivacky *	  a page from the VM PAGE CACHE hash table (vm_page_buckets),
76239462Sdim *	  regardless of other mutexes or the busy state of a page.
77239462Sdim *
78239462Sdim *	- either a hash chain mutex OR a busied page is required in order
79239462Sdim *	  to modify the page flags.  A hash chain mutex must be obtained in
80239462Sdim *	  order to busy a page.  A page's flags cannot be modified by a
81239462Sdim *	  hash chain mutex if the page is marked busy.
82239462Sdim *
83239462Sdim *	- The object memq mutex is held when inserting or removing
84239462Sdim *	  pages from an object (vm_page_insert() or vm_page_remove()).  This
85239462Sdim *	  is different from the object's main mutex.
86239462Sdim *
87239462Sdim *	Generally speaking, you have to be aware of side effects when running
88249423Sdim *	vm_page ops.  A vm_page_lookup() will return with the hash chain
89249423Sdim *	locked, whether it was able to lookup the page or not.  vm_page_free(),
90249423Sdim *	vm_page_cache(), vm_page_activate(), and a number of other routines
91249423Sdim *	will release the hash chain mutex for you.  Intermediate manipulation
92249423Sdim *	routines such as vm_page_flag_set() expect the hash chain to be held
93249423Sdim *	on entry and the hash chain will remain held on return.
94249423Sdim *
95249423Sdim *	pageq scanning can only occur with the pageq in question locked.
96249423Sdim *	We have a known bottleneck with the active queue, but the cache
97249423Sdim *	and free queues are actually arrays already.
98249423Sdim */
99249423Sdim
100249423Sdim/*
101249423Sdim *	Resident memory management module.
102210008Srdivacky */
103210008Srdivacky
104234353Sdim#include <sys/param.h>
105210008Srdivacky#include <sys/systm.h>
106210008Srdivacky#include <sys/lock.h>
107218893Sdim#include <sys/malloc.h>
108218893Sdim#include <sys/mutex.h>
109218893Sdim#include <sys/proc.h>
110218893Sdim#include <sys/vmmeter.h>
111218893Sdim#include <sys/vnode.h>
112218893Sdim
113234353Sdim#include <vm/vm.h>
114234353Sdim#include <vm/vm_param.h>
115210008Srdivacky#include <vm/vm_kern.h>
116210008Srdivacky#include <vm/vm_object.h>
117210008Srdivacky#include <vm/vm_page.h>
118210008Srdivacky#include <vm/vm_pageout.h>
119210008Srdivacky#include <vm/vm_pager.h>
120210008Srdivacky#include <vm/vm_extern.h>
121210008Srdivacky#include <vm/uma.h>
122210008Srdivacky#include <vm/uma_int.h>
123234353Sdim
124210008Srdivacky/*
125210008Srdivacky *	Associated with page of user-allocatable memory is a
126210008Srdivacky *	page structure.
127210008Srdivacky */
128210008Srdivackystatic struct mtx vm_page_buckets_mtx;
129210008Srdivackystatic struct vm_page **vm_page_buckets; /* Array of buckets */
130210008Srdivackystatic int vm_page_bucket_count;	/* How big is array? */
131218893Sdimstatic int vm_page_hash_mask;		/* Mask for hash function */
132218893Sdim
133210008Srdivackystruct mtx vm_page_queue_mtx;
134210008Srdivackystruct mtx vm_page_queue_free_mtx;
135210008Srdivacky
136243830Sdimvm_page_t vm_page_array = 0;
137251662Sdimint vm_page_array_size = 0;
138218893Sdimlong first_page = 0;
139234353Sdimint vm_page_zero_count = 0;
140210008Srdivacky
141239462Sdim/*
142210008Srdivacky *	vm_set_page_size:
143239462Sdim *
144239462Sdim *	Sets the page size, perhaps based upon the memory
145210008Srdivacky *	size.  Must be called before any use of page-size
146218893Sdim *	dependent functions.
147218893Sdim */
148218893Sdimvoid
149221345Sdimvm_set_page_size(void)
150218893Sdim{
151218893Sdim	if (cnt.v_page_size == 0)
152218893Sdim		cnt.v_page_size = PAGE_SIZE;
153218893Sdim	if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
154218893Sdim		panic("vm_set_page_size: page size not a power of two");
155218893Sdim}
156218893Sdim
157218893Sdim/*
158263508Sdim *	vm_page_startup:
159218893Sdim *
160218893Sdim *	Initializes the resident memory module.
161218893Sdim *
162218893Sdim *	Allocates memory for the page cells, and
163218893Sdim *	for the object/offset-to-page hash table headers.
164218893Sdim *	Each page cell is initialized and placed on the free list.
165218893Sdim */
166218893Sdimvm_offset_t
167218893Sdimvm_page_startup(vm_offset_t starta, vm_offset_t enda, vm_offset_t vaddr)
168218893Sdim{
169218893Sdim	vm_offset_t mapped;
170234353Sdim	struct vm_page **bucket;
171221345Sdim	vm_size_t npages, page_range;
172221345Sdim	vm_offset_t new_end;
173218893Sdim	int i;
174218893Sdim	vm_offset_t pa;
175218893Sdim	int nblocks;
176239462Sdim	vm_offset_t last_pa;
177234353Sdim
178234353Sdim	/* the biggest memory array is the second group of pages */
179234353Sdim	vm_offset_t end;
180218893Sdim	vm_offset_t biggestone, biggestsize;
181218893Sdim
182218893Sdim	vm_offset_t total;
183223017Sdim	vm_size_t bootpages;
184224145Sdim
185226633Sdim	total = 0;
186218893Sdim	biggestsize = 0;
187218893Sdim	biggestone = 0;
188210008Srdivacky	nblocks = 0;
189224145Sdim	vaddr = round_page(vaddr);
190224145Sdim
191224145Sdim	for (i = 0; phys_avail[i + 1]; i += 2) {
192210008Srdivacky		phys_avail[i] = round_page(phys_avail[i]);
193210008Srdivacky		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
194210008Srdivacky	}
195210008Srdivacky
196210008Srdivacky	for (i = 0; phys_avail[i + 1]; i += 2) {
197210008Srdivacky		vm_size_t size = phys_avail[i + 1] - phys_avail[i];
198210008Srdivacky
199210008Srdivacky		if (size > biggestsize) {
200210008Srdivacky			biggestone = i;
201210008Srdivacky			biggestsize = size;
202210008Srdivacky		}
203221345Sdim		++nblocks;
204221345Sdim		total += size;
205221345Sdim	}
206210008Srdivacky
207210008Srdivacky	end = phys_avail[biggestone+1];
208210008Srdivacky
209210008Srdivacky	/*
210210008Srdivacky	 * Initialize the locks.
211210008Srdivacky	 */
212210008Srdivacky	mtx_init(&vm_page_queue_mtx, "vm page queue mutex", NULL, MTX_DEF);
213210008Srdivacky	mtx_init(&vm_page_queue_free_mtx, "vm page queue free mutex", NULL,
214210008Srdivacky	   MTX_SPIN);
215210008Srdivacky
216210008Srdivacky	/*
217212904Sdim	 * Initialize the queue headers for the free queue, the active queue
218210008Srdivacky	 * and the inactive queue.
219210008Srdivacky	 */
220210008Srdivacky	vm_pageq_init();
221212904Sdim
222210008Srdivacky	/*
223210008Srdivacky	 * Allocate memory for use when boot strapping the kernel memory allocator
224218893Sdim	 */
225218893Sdim	bootpages = UMA_BOOT_PAGES * UMA_SLAB_SIZE;
226218893Sdim	new_end = end - bootpages;
227218893Sdim	new_end = trunc_page(new_end);
228218893Sdim	mapped = pmap_map(&vaddr, new_end, end,
229218893Sdim	    VM_PROT_READ | VM_PROT_WRITE);
230218893Sdim	bzero((caddr_t) mapped, end - new_end);
231218893Sdim	uma_startup((caddr_t)mapped);
232218893Sdim
233218893Sdim	end = new_end;
234218893Sdim
235218893Sdim	/*
236218893Sdim	 * Allocate (and initialize) the hash table buckets.
237210008Srdivacky	 *
238210008Srdivacky	 * The number of buckets MUST BE a power of 2, and the actual value is
239210008Srdivacky	 * the next power of 2 greater than the number of physical pages in
240212904Sdim	 * the system.
241212904Sdim	 *
242210008Srdivacky	 * We make the hash table approximately 2x the number of pages to
243210008Srdivacky	 * reduce the chain length.  This is about the same size using the
244210008Srdivacky	 * singly-linked list as the 1x hash table we were using before
245210008Srdivacky	 * using TAILQ but the chain length will be smaller.
246210008Srdivacky	 *
247210008Srdivacky	 * Note: This computation can be tweaked if desired.
248218893Sdim	 */
249234353Sdim	if (vm_page_bucket_count == 0) {
250234353Sdim		vm_page_bucket_count = 1;
251234353Sdim		while (vm_page_bucket_count < atop(total))
252234353Sdim			vm_page_bucket_count <<= 1;
253218893Sdim	}
254234353Sdim	vm_page_bucket_count <<= 1;
255218893Sdim	vm_page_hash_mask = vm_page_bucket_count - 1;
256210008Srdivacky
257210008Srdivacky	/*
258210008Srdivacky	 * Validate these addresses.
259234353Sdim	 */
260210008Srdivacky	new_end = end - vm_page_bucket_count * sizeof(struct vm_page *);
261210008Srdivacky	new_end = trunc_page(new_end);
262210008Srdivacky	mapped = pmap_map(&vaddr, new_end, end,
263210008Srdivacky	    VM_PROT_READ | VM_PROT_WRITE);
264210008Srdivacky	bzero((caddr_t) mapped, end - new_end);
265210008Srdivacky
266234353Sdim	mtx_init(&vm_page_buckets_mtx, "vm page buckets mutex", NULL, MTX_SPIN);
267221345Sdim	vm_page_buckets = (struct vm_page **)mapped;
268221345Sdim	bucket = vm_page_buckets;
269221345Sdim	for (i = 0; i < vm_page_bucket_count; i++) {
270221345Sdim		*bucket = NULL;
271221345Sdim		bucket++;
272221345Sdim	}
273221345Sdim
274210008Srdivacky	/*
275210008Srdivacky	 * Compute the number of pages of memory that will be available for
276210008Srdivacky	 * use (taking into account the overhead of a page structure per
277210008Srdivacky	 * page).
278210008Srdivacky	 */
279210008Srdivacky	first_page = phys_avail[0] / PAGE_SIZE;
280210008Srdivacky	page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page;
281210008Srdivacky	npages = (total - (page_range * sizeof(struct vm_page)) -
282210008Srdivacky	    (end - new_end)) / PAGE_SIZE;
283210008Srdivacky	end = new_end;
284234353Sdim
285218893Sdim	/*
286210008Srdivacky	 * Initialize the mem entry structures now, and put them in the free
287210008Srdivacky	 * queue.
288210008Srdivacky	 */
289210008Srdivacky	new_end = trunc_page(end - page_range * sizeof(struct vm_page));
290263508Sdim	mapped = pmap_map(&vaddr, new_end, end,
291210008Srdivacky	    VM_PROT_READ | VM_PROT_WRITE);
292210008Srdivacky	vm_page_array = (vm_page_t) mapped;
293210008Srdivacky
294210008Srdivacky	/*
295263508Sdim	 * Clear all of the page structures
296263508Sdim	 */
297263508Sdim	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
298263508Sdim	vm_page_array_size = page_range;
299263508Sdim
300210008Srdivacky	/*
301210008Srdivacky	 * Construct the free queue(s) in descending order (by physical
302210008Srdivacky	 * address) so that the first 16MB of physical memory is allocated
303210008Srdivacky	 * last rather than first.  On large-memory machines, this avoids
304210008Srdivacky	 * the exhaustion of low physical memory before isa_dmainit has run.
305251662Sdim	 */
306251662Sdim	cnt.v_page_count = 0;
307251662Sdim	cnt.v_free_count = 0;
308251662Sdim	for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) {
309210008Srdivacky		pa = phys_avail[i];
310210008Srdivacky		if (i == biggestone)
311210008Srdivacky			last_pa = new_end;
312210008Srdivacky		else
313218893Sdim			last_pa = phys_avail[i + 1];
314218893Sdim		while (pa < last_pa && npages-- > 0) {
315218893Sdim			vm_pageq_add_new_page(pa);
316210008Srdivacky			pa += PAGE_SIZE;
317210008Srdivacky		}
318210008Srdivacky	}
319210008Srdivacky	return (vaddr);
320210008Srdivacky}
321210008Srdivacky
322210008Srdivacky/*
323210008Srdivacky *	vm_page_hash:
324224145Sdim *
325210008Srdivacky *	Distributes the object/offset key pair among hash buckets.
326210008Srdivacky *
327210008Srdivacky *	NOTE:  This macro depends on vm_page_bucket_count being a power of 2.
328210008Srdivacky *	This routine may not block.
329224145Sdim *
330224145Sdim *	We try to randomize the hash based on the object to spread the pages
331224145Sdim *	out in the hash table without it costing us too much.
332224145Sdim */
333218893Sdimstatic __inline int
334218893Sdimvm_page_hash(vm_object_t object, vm_pindex_t pindex)
335218893Sdim{
336218893Sdim	int i = ((uintptr_t)object + pindex) ^ object->hash_rand;
337218893Sdim
338218893Sdim	return (i & vm_page_hash_mask);
339218893Sdim}
340210008Srdivacky
341210008Srdivackyvoid
342218893Sdimvm_page_flag_set(vm_page_t m, unsigned short bits)
343218893Sdim{
344218893Sdim	GIANT_REQUIRED;
345210008Srdivacky	m->flags |= bits;
346210008Srdivacky}
347210008Srdivacky
348210008Srdivackyvoid
349210008Srdivackyvm_page_flag_clear(vm_page_t m, unsigned short bits)
350210008Srdivacky{
351221345Sdim	GIANT_REQUIRED;
352221345Sdim	m->flags &= ~bits;
353210008Srdivacky}
354218893Sdim
355218893Sdimvoid
356210008Srdivackyvm_page_busy(vm_page_t m)
357210008Srdivacky{
358210008Srdivacky	KASSERT((m->flags & PG_BUSY) == 0,
359234353Sdim	    ("vm_page_busy: page already busy!!!"));
360263508Sdim	vm_page_flag_set(m, PG_BUSY);
361210008Srdivacky}
362210008Srdivacky
363218893Sdim/*
364218893Sdim *      vm_page_flash:
365234353Sdim *
366218893Sdim *      wakeup anyone waiting for the page.
367218893Sdim */
368234353Sdimvoid
369218893Sdimvm_page_flash(vm_page_t m)
370218893Sdim{
371218893Sdim	if (m->flags & PG_WANTED) {
372218893Sdim		vm_page_flag_clear(m, PG_WANTED);
373210008Srdivacky		wakeup(m);
374210008Srdivacky	}
375234353Sdim}
376218893Sdim
377218893Sdim/*
378234353Sdim *      vm_page_wakeup:
379218893Sdim *
380218893Sdim *      clear the PG_BUSY flag and wakeup anyone waiting for the
381234353Sdim *      page.
382224145Sdim *
383224145Sdim */
384224145Sdimvoid
385224145Sdimvm_page_wakeup(vm_page_t m)
386234353Sdim{
387234353Sdim	KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!"));
388234353Sdim	vm_page_flag_clear(m, PG_BUSY);
389234353Sdim	vm_page_flash(m);
390234353Sdim}
391234353Sdim
392234353Sdim/*
393234353Sdim *
394234353Sdim *
395234353Sdim */
396234353Sdimvoid
397218893Sdimvm_page_io_start(vm_page_t m)
398234353Sdim{
399218893Sdim
400210008Srdivacky	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
401210008Srdivacky	m->busy++;
402210008Srdivacky}
403210008Srdivacky
404210008Srdivackyvoid
405210008Srdivackyvm_page_io_finish(vm_page_t m)
406210008Srdivacky{
407210008Srdivacky
408210008Srdivacky	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
409210008Srdivacky	m->busy--;
410210008Srdivacky	if (m->busy == 0)
411212904Sdim		vm_page_flash(m);
412212904Sdim}
413212904Sdim
414212904Sdim/*
415210008Srdivacky * Keep page from being freed by the page daemon
416210008Srdivacky * much of the same effect as wiring, except much lower
417210008Srdivacky * overhead and should be used only for *very* temporary
418210008Srdivacky * holding ("wiring").
419210008Srdivacky */
420210008Srdivackyvoid
421218893Sdimvm_page_hold(vm_page_t mem)
422234353Sdim{
423210008Srdivacky        GIANT_REQUIRED;
424210008Srdivacky        mem->hold_count++;
425210008Srdivacky}
426210008Srdivacky
427210008Srdivackyvoid
428210008Srdivackyvm_page_unhold(vm_page_t mem)
429210008Srdivacky{
430210008Srdivacky	GIANT_REQUIRED;
431210008Srdivacky	--mem->hold_count;
432210008Srdivacky	KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
433234353Sdim	if (mem->hold_count == 0 && mem->queue == PQ_HOLD)
434210008Srdivacky		vm_page_free_toq(mem);
435210008Srdivacky}
436210008Srdivacky
437226633Sdim/*
438210008Srdivacky *	vm_page_protect:
439210008Srdivacky *
440210008Srdivacky *	Reduce the protection of a page.  This routine never raises the
441210008Srdivacky *	protection and therefore can be safely called if the page is already
442210008Srdivacky *	at VM_PROT_NONE (it will be a NOP effectively ).
443239462Sdim */
444210008Srdivackyvoid
445210008Srdivackyvm_page_protect(vm_page_t mem, int prot)
446210008Srdivacky{
447210008Srdivacky	if (prot == VM_PROT_NONE) {
448210008Srdivacky		if (mem->flags & (PG_WRITEABLE|PG_MAPPED)) {
449221345Sdim			pmap_page_protect(mem, VM_PROT_NONE);
450221345Sdim			vm_page_flag_clear(mem, PG_WRITEABLE|PG_MAPPED);
451221345Sdim		}
452221345Sdim	} else if ((prot == VM_PROT_READ) && (mem->flags & PG_WRITEABLE)) {
453210008Srdivacky		pmap_page_protect(mem, VM_PROT_READ);
454234353Sdim		vm_page_flag_clear(mem, PG_WRITEABLE);
455210008Srdivacky	}
456210008Srdivacky}
457210008Srdivacky/*
458210008Srdivacky *	vm_page_zero_fill:
459210008Srdivacky *
460210008Srdivacky *	Zero-fill the specified page.
461218893Sdim *	Written as a standard pagein routine, to
462218893Sdim *	be used by the zero-fill object.
463210008Srdivacky */
464210008Srdivackyboolean_t
465210008Srdivackyvm_page_zero_fill(vm_page_t m)
466210008Srdivacky{
467210008Srdivacky	pmap_zero_page(m);
468210008Srdivacky	return (TRUE);
469210008Srdivacky}
470210008Srdivacky
471210008Srdivacky/*
472210008Srdivacky *	vm_page_zero_fill_area:
473210008Srdivacky *
474210008Srdivacky *	Like vm_page_zero_fill but only fill the specified area.
475210008Srdivacky */
476210008Srdivackyboolean_t
477210008Srdivackyvm_page_zero_fill_area(vm_page_t m, int off, int size)
478210008Srdivacky{
479210008Srdivacky	pmap_zero_page_area(m, off, size);
480210008Srdivacky	return (TRUE);
481210008Srdivacky}
482210008Srdivacky
483210008Srdivacky/*
484210008Srdivacky *	vm_page_copy:
485210008Srdivacky *
486210008Srdivacky *	Copy one page to another
487218893Sdim */
488218893Sdimvoid
489218893Sdimvm_page_copy(vm_page_t src_m, vm_page_t dest_m)
490210008Srdivacky{
491210008Srdivacky	pmap_copy_page(src_m, dest_m);
492210008Srdivacky	dest_m->valid = VM_PAGE_BITS_ALL;
493210008Srdivacky}
494210008Srdivacky
495210008Srdivacky/*
496210008Srdivacky *	vm_page_free:
497210008Srdivacky *
498210008Srdivacky *	Free a page
499210008Srdivacky *
500210008Srdivacky *	The clearing of PG_ZERO is a temporary safety until the code can be
501210008Srdivacky *	reviewed to determine that PG_ZERO is being properly cleared on
502210008Srdivacky *	write faults or maps.  PG_ZERO was previously cleared in
503210008Srdivacky *	vm_page_alloc().
504210008Srdivacky */
505210008Srdivackyvoid
506234353Sdimvm_page_free(vm_page_t m)
507210008Srdivacky{
508210008Srdivacky	vm_page_flag_clear(m, PG_ZERO);
509218893Sdim	vm_page_free_toq(m);
510210008Srdivacky	vm_page_zero_idle_wakeup();
511218893Sdim}
512218893Sdim
513210008Srdivacky/*
514210008Srdivacky *	vm_page_free_zero:
515210008Srdivacky *
516212904Sdim *	Free a page to the zerod-pages queue
517210008Srdivacky */
518210008Srdivackyvoid
519210008Srdivackyvm_page_free_zero(vm_page_t m)
520210008Srdivacky{
521210008Srdivacky	vm_page_flag_set(m, PG_ZERO);
522212904Sdim	vm_page_free_toq(m);
523234353Sdim}
524234353Sdim
525223017Sdim/*
526223017Sdim *	vm_page_sleep_busy:
527210008Srdivacky *
528210008Srdivacky *	Wait until page is no longer PG_BUSY or (if also_m_busy is TRUE)
529210008Srdivacky *	m->busy is zero.  Returns TRUE if it had to sleep ( including if
530212904Sdim *	it almost had to sleep and made temporary spl*() mods), FALSE
531234353Sdim *	otherwise.
532234353Sdim *
533223017Sdim *	This routine assumes that interrupts can only remove the busy
534223017Sdim *	status from a page, not set the busy status or change it from
535210008Srdivacky *	PG_BUSY to m->busy or vise versa (which would create a timing
536210008Srdivacky *	window).
537210008Srdivacky */
538210008Srdivackyint
539210008Srdivackyvm_page_sleep_busy(vm_page_t m, int also_m_busy, const char *msg)
540218893Sdim{
541218893Sdim	GIANT_REQUIRED;
542234353Sdim	if ((m->flags & PG_BUSY) || (also_m_busy && m->busy))  {
543210008Srdivacky		int s = splvm();
544210008Srdivacky		if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) {
545210008Srdivacky			/*
546210008Srdivacky			 * Page is busy. Wait and retry.
547210008Srdivacky			 */
548210008Srdivacky			vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
549210008Srdivacky			tsleep(m, PVM, msg, 0);
550210008Srdivacky		}
551210008Srdivacky		splx(s);
552210008Srdivacky		return (TRUE);
553210008Srdivacky		/* not reached */
554210008Srdivacky	}
555210008Srdivacky	return (FALSE);
556210008Srdivacky}
557210008Srdivacky
558210008Srdivacky/*
559210008Srdivacky *	vm_page_sleep_if_busy:
560210008Srdivacky *
561210008Srdivacky *	Sleep and release the page queues lock if PG_BUSY is set or,
562210008Srdivacky *	if also_m_busy is TRUE, busy is non-zero.  Returns TRUE if the
563210008Srdivacky *	thread slept and the page queues lock was released.
564210008Srdivacky *	Otherwise, retains the page queues lock and returns FALSE.
565210008Srdivacky */
566210008Srdivackyint
567210008Srdivackyvm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg)
568218893Sdim{
569218893Sdim
570218893Sdim	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
571218893Sdim	if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) {
572210008Srdivacky		vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
573210008Srdivacky		msleep(m, &vm_page_queue_mtx, PDROP | PVM, msg, 0);
574210008Srdivacky		return (TRUE);
575210008Srdivacky	}
576210008Srdivacky	return (FALSE);
577210008Srdivacky}
578210008Srdivacky
579210008Srdivacky/*
580210008Srdivacky *	vm_page_dirty:
581234353Sdim *
582210008Srdivacky *	make page all dirty
583210008Srdivacky */
584210008Srdivackyvoid
585210008Srdivackyvm_page_dirty(vm_page_t m)
586218893Sdim{
587218893Sdim	KASSERT(m->queue - m->pc != PQ_CACHE,
588210008Srdivacky	    ("vm_page_dirty: page in cache!"));
589210008Srdivacky	m->dirty = VM_PAGE_BITS_ALL;
590210008Srdivacky}
591210008Srdivacky
592210008Srdivacky/*
593210008Srdivacky *	vm_page_undirty:
594210008Srdivacky *
595263508Sdim *	Set page to not be dirty.  Note: does not clear pmap modify bits
596263508Sdim */
597234353Sdimvoid
598210008Srdivackyvm_page_undirty(vm_page_t m)
599210008Srdivacky{
600210008Srdivacky	m->dirty = 0;
601210008Srdivacky}
602210008Srdivacky
603210008Srdivacky/*
604210008Srdivacky *	vm_page_insert:		[ internal use only ]
605210008Srdivacky *
606210008Srdivacky *	Inserts the given mem entry into the object and object list.
607218893Sdim *
608234353Sdim *	The pagetables are not updated but will presumably fault the page
609210008Srdivacky *	in if necessary, or if a kernel page the caller will at some point
610210008Srdivacky *	enter the page into the kernel's pmap.  We are not allowed to block
611210008Srdivacky *	here so we *can't* do this anyway.
612210008Srdivacky *
613210008Srdivacky *	The object and page must be locked, and must be splhigh.
614210008Srdivacky *	This routine may not block.
615210008Srdivacky */
616210008Srdivackyvoid
617210008Srdivackyvm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
618218893Sdim{
619210008Srdivacky	struct vm_page **bucket;
620210008Srdivacky
621210008Srdivacky	GIANT_REQUIRED;
622210008Srdivacky
623210008Srdivacky	if (m->object != NULL)
624221345Sdim		panic("vm_page_insert: already inserted");
625221345Sdim
626210008Srdivacky	/*
627210008Srdivacky	 * Record the object/offset pair in this page
628210008Srdivacky	 */
629210008Srdivacky	m->object = object;
630239462Sdim	m->pindex = pindex;
631221345Sdim
632210008Srdivacky	/*
633210008Srdivacky	 * Insert it into the object_object/offset hash table
634210008Srdivacky	 */
635210008Srdivacky	bucket = &vm_page_buckets[vm_page_hash(object, pindex)];
636210008Srdivacky	mtx_lock_spin(&vm_page_buckets_mtx);
637210008Srdivacky	m->hnext = *bucket;
638210008Srdivacky	*bucket = m;
639210008Srdivacky	mtx_unlock_spin(&vm_page_buckets_mtx);
640210008Srdivacky
641210008Srdivacky	/*
642210008Srdivacky	 * Now link into the object's list of backed pages.
643210008Srdivacky	 */
644210008Srdivacky	TAILQ_INSERT_TAIL(&object->memq, m, listq);
645221345Sdim	object->generation++;
646221345Sdim
647210008Srdivacky	/*
648210008Srdivacky	 * show that the object has one more resident page.
649210008Srdivacky	 */
650210008Srdivacky	object->resident_page_count++;
651239462Sdim
652221345Sdim	/*
653210008Srdivacky	 * Since we are inserting a new and possibly dirty page,
654210008Srdivacky	 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags.
655210008Srdivacky	 */
656210008Srdivacky	if (m->flags & PG_WRITEABLE)
657210008Srdivacky		vm_object_set_writeable_dirty(object);
658210008Srdivacky}
659210008Srdivacky
660210008Srdivacky/*
661210008Srdivacky *	vm_page_remove:
662210008Srdivacky *				NOTE: used by device pager as well -wfj
663210008Srdivacky *
664210008Srdivacky *	Removes the given mem entry from the object/offset-page
665210008Srdivacky *	table and the object page list, but do not invalidate/terminate
666210008Srdivacky *	the backing store.
667210008Srdivacky *
668210008Srdivacky *	The object and page must be locked, and at splhigh.
669210008Srdivacky *	The underlying pmap entry (if any) is NOT removed here.
670210008Srdivacky *	This routine may not block.
671210008Srdivacky */
672void
673vm_page_remove(vm_page_t m)
674{
675	vm_object_t object;
676	vm_page_t *bucket;
677
678	GIANT_REQUIRED;
679
680	if (m->object == NULL)
681		return;
682
683	if ((m->flags & PG_BUSY) == 0) {
684		panic("vm_page_remove: page not busy");
685	}
686
687	/*
688	 * Basically destroy the page.
689	 */
690	vm_page_wakeup(m);
691
692	object = m->object;
693
694	/*
695	 * Remove from the object_object/offset hash table.  The object
696	 * must be on the hash queue, we will panic if it isn't
697	 */
698	bucket = &vm_page_buckets[vm_page_hash(m->object, m->pindex)];
699	mtx_lock_spin(&vm_page_buckets_mtx);
700	while (*bucket != m) {
701		if (*bucket == NULL)
702			panic("vm_page_remove(): page not found in hash");
703		bucket = &(*bucket)->hnext;
704	}
705	*bucket = m->hnext;
706	m->hnext = NULL;
707	mtx_unlock_spin(&vm_page_buckets_mtx);
708
709	/*
710	 * Now remove from the object's list of backed pages.
711	 */
712	TAILQ_REMOVE(&object->memq, m, listq);
713
714	/*
715	 * And show that the object has one fewer resident page.
716	 */
717	object->resident_page_count--;
718	object->generation++;
719
720	m->object = NULL;
721}
722
723/*
724 *	vm_page_lookup:
725 *
726 *	Returns the page associated with the object/offset
727 *	pair specified; if none is found, NULL is returned.
728 *
729 *	The object must be locked.  No side effects.
730 *	This routine may not block.
731 *	This is a critical path routine
732 */
733vm_page_t
734vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
735{
736	vm_page_t m;
737	struct vm_page **bucket;
738
739	/*
740	 * Search the hash table for this object/offset pair
741	 */
742	bucket = &vm_page_buckets[vm_page_hash(object, pindex)];
743	mtx_lock_spin(&vm_page_buckets_mtx);
744	for (m = *bucket; m != NULL; m = m->hnext)
745		if (m->object == object && m->pindex == pindex)
746			break;
747	mtx_unlock_spin(&vm_page_buckets_mtx);
748	return (m);
749}
750
751/*
752 *	vm_page_rename:
753 *
754 *	Move the given memory entry from its
755 *	current object to the specified target object/offset.
756 *
757 *	The object must be locked.
758 *	This routine may not block.
759 *
760 *	Note: this routine will raise itself to splvm(), the caller need not.
761 *
762 *	Note: swap associated with the page must be invalidated by the move.  We
763 *	      have to do this for several reasons:  (1) we aren't freeing the
764 *	      page, (2) we are dirtying the page, (3) the VM system is probably
765 *	      moving the page from object A to B, and will then later move
766 *	      the backing store from A to B and we can't have a conflict.
767 *
768 *	Note: we *always* dirty the page.  It is necessary both for the
769 *	      fact that we moved it, and because we may be invalidating
770 *	      swap.  If the page is on the cache, we have to deactivate it
771 *	      or vm_page_dirty() will panic.  Dirty pages are not allowed
772 *	      on the cache.
773 */
774void
775vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
776{
777	int s;
778
779	s = splvm();
780	vm_page_lock_queues();
781	vm_page_remove(m);
782	vm_page_insert(m, new_object, new_pindex);
783	if (m->queue - m->pc == PQ_CACHE)
784		vm_page_deactivate(m);
785	vm_page_dirty(m);
786	vm_page_unlock_queues();
787	splx(s);
788}
789
790/*
791 *	vm_page_select_cache:
792 *
793 *	Find a page on the cache queue with color optimization.  As pages
794 *	might be found, but not applicable, they are deactivated.  This
795 *	keeps us from using potentially busy cached pages.
796 *
797 *	This routine must be called at splvm().
798 *	This routine may not block.
799 */
800static vm_page_t
801vm_page_select_cache(vm_object_t object, vm_pindex_t pindex)
802{
803	vm_page_t m;
804
805	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
806	while (TRUE) {
807		m = vm_pageq_find(
808		    PQ_CACHE,
809		    (pindex + object->pg_color) & PQ_L2_MASK,
810		    FALSE
811		);
812		if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy ||
813			       m->hold_count || m->wire_count)) {
814			vm_page_deactivate(m);
815			continue;
816		}
817		return m;
818	}
819}
820
821/*
822 *	vm_page_select_free:
823 *
824 *	Find a free or zero page, with specified preference.
825 *
826 *	This routine must be called at splvm().
827 *	This routine may not block.
828 */
829static __inline vm_page_t
830vm_page_select_free(vm_object_t object, vm_pindex_t pindex, boolean_t prefer_zero)
831{
832	vm_page_t m;
833
834	m = vm_pageq_find(
835		PQ_FREE,
836		(pindex + object->pg_color) & PQ_L2_MASK,
837		prefer_zero
838	);
839	return (m);
840}
841
842/*
843 *	vm_page_alloc:
844 *
845 *	Allocate and return a memory cell associated
846 *	with this VM object/offset pair.
847 *
848 *	page_req classes:
849 *	VM_ALLOC_NORMAL		normal process request
850 *	VM_ALLOC_SYSTEM		system *really* needs a page
851 *	VM_ALLOC_INTERRUPT	interrupt time request
852 *	VM_ALLOC_ZERO		zero page
853 *
854 *	This routine may not block.
855 *
856 *	Additional special handling is required when called from an
857 *	interrupt (VM_ALLOC_INTERRUPT).  We are not allowed to mess with
858 *	the page cache in this case.
859 */
860vm_page_t
861vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
862{
863	vm_page_t m = NULL;
864	int page_req, s;
865
866	GIANT_REQUIRED;
867
868	KASSERT(!vm_page_lookup(object, pindex),
869		("vm_page_alloc: page already allocated"));
870
871	page_req = req & VM_ALLOC_CLASS_MASK;
872
873	/*
874	 * The pager is allowed to eat deeper into the free page list.
875	 */
876	if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
877		page_req = VM_ALLOC_SYSTEM;
878	};
879
880	s = splvm();
881loop:
882	mtx_lock_spin(&vm_page_queue_free_mtx);
883	if (cnt.v_free_count > cnt.v_free_reserved) {
884		/*
885		 * Allocate from the free queue if there are plenty of pages
886		 * in it.
887		 */
888		m = vm_page_select_free(object, pindex,
889					(req & VM_ALLOC_ZERO) != 0);
890	} else if (
891	    (page_req == VM_ALLOC_SYSTEM &&
892	     cnt.v_cache_count == 0 &&
893	     cnt.v_free_count > cnt.v_interrupt_free_min) ||
894	    (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0)
895	) {
896		/*
897		 * Interrupt or system, dig deeper into the free list.
898		 */
899		m = vm_page_select_free(object, pindex, FALSE);
900	} else if (page_req != VM_ALLOC_INTERRUPT) {
901		mtx_unlock_spin(&vm_page_queue_free_mtx);
902		/*
903		 * Allocatable from cache (non-interrupt only).  On success,
904		 * we must free the page and try again, thus ensuring that
905		 * cnt.v_*_free_min counters are replenished.
906		 */
907		vm_page_lock_queues();
908		if ((m = vm_page_select_cache(object, pindex)) == NULL) {
909			vm_page_unlock_queues();
910			splx(s);
911#if defined(DIAGNOSTIC)
912			if (cnt.v_cache_count > 0)
913				printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count);
914#endif
915			vm_pageout_deficit++;
916			pagedaemon_wakeup();
917			return (NULL);
918		}
919		KASSERT(m->dirty == 0, ("Found dirty cache page %p", m));
920		vm_page_busy(m);
921		vm_page_protect(m, VM_PROT_NONE);
922		vm_page_free(m);
923		vm_page_unlock_queues();
924		goto loop;
925	} else {
926		/*
927		 * Not allocatable from cache from interrupt, give up.
928		 */
929		mtx_unlock_spin(&vm_page_queue_free_mtx);
930		splx(s);
931		vm_pageout_deficit++;
932		pagedaemon_wakeup();
933		return (NULL);
934	}
935
936	/*
937	 *  At this point we had better have found a good page.
938	 */
939
940	KASSERT(
941	    m != NULL,
942	    ("vm_page_alloc(): missing page on free queue\n")
943	);
944
945	/*
946	 * Remove from free queue
947	 */
948
949	vm_pageq_remove_nowakeup(m);
950
951	/*
952	 * Initialize structure.  Only the PG_ZERO flag is inherited.
953	 */
954	if (m->flags & PG_ZERO) {
955		vm_page_zero_count--;
956		m->flags = PG_ZERO | PG_BUSY;
957	} else {
958		m->flags = PG_BUSY;
959	}
960	if (req & VM_ALLOC_WIRED) {
961		cnt.v_wire_count++;
962		m->wire_count = 1;
963	} else
964		m->wire_count = 0;
965	m->hold_count = 0;
966	m->act_count = 0;
967	m->busy = 0;
968	m->valid = 0;
969	KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m));
970	mtx_unlock_spin(&vm_page_queue_free_mtx);
971
972	/*
973	 * vm_page_insert() is safe prior to the splx().  Note also that
974	 * inserting a page here does not insert it into the pmap (which
975	 * could cause us to block allocating memory).  We cannot block
976	 * anywhere.
977	 */
978	vm_page_insert(m, object, pindex);
979
980	/*
981	 * Don't wakeup too often - wakeup the pageout daemon when
982	 * we would be nearly out of memory.
983	 */
984	if (vm_paging_needed())
985		pagedaemon_wakeup();
986
987	splx(s);
988	return (m);
989}
990
991/*
992 *	vm_wait:	(also see VM_WAIT macro)
993 *
994 *	Block until free pages are available for allocation
995 *	- Called in various places before memory allocations.
996 */
997void
998vm_wait(void)
999{
1000	int s;
1001
1002	s = splvm();
1003	if (curproc == pageproc) {
1004		vm_pageout_pages_needed = 1;
1005		tsleep(&vm_pageout_pages_needed, PSWP, "VMWait", 0);
1006	} else {
1007		if (!vm_pages_needed) {
1008			vm_pages_needed = 1;
1009			wakeup(&vm_pages_needed);
1010		}
1011		tsleep(&cnt.v_free_count, PVM, "vmwait", 0);
1012	}
1013	splx(s);
1014}
1015
1016/*
1017 *	vm_waitpfault:	(also see VM_WAITPFAULT macro)
1018 *
1019 *	Block until free pages are available for allocation
1020 *	- Called only in vm_fault so that processes page faulting
1021 *	  can be easily tracked.
1022 *	- Sleeps at a lower priority than vm_wait() so that vm_wait()ing
1023 *	  processes will be able to grab memory first.  Do not change
1024 *	  this balance without careful testing first.
1025 */
1026void
1027vm_waitpfault(void)
1028{
1029	int s;
1030
1031	s = splvm();
1032	if (!vm_pages_needed) {
1033		vm_pages_needed = 1;
1034		wakeup(&vm_pages_needed);
1035	}
1036	tsleep(&cnt.v_free_count, PUSER, "pfault", 0);
1037	splx(s);
1038}
1039
1040/*
1041 *	vm_page_activate:
1042 *
1043 *	Put the specified page on the active list (if appropriate).
1044 *	Ensure that act_count is at least ACT_INIT but do not otherwise
1045 *	mess with it.
1046 *
1047 *	The page queues must be locked.
1048 *	This routine may not block.
1049 */
1050void
1051vm_page_activate(vm_page_t m)
1052{
1053	int s;
1054
1055	GIANT_REQUIRED;
1056	s = splvm();
1057	if (m->queue != PQ_ACTIVE) {
1058		if ((m->queue - m->pc) == PQ_CACHE)
1059			cnt.v_reactivated++;
1060		vm_pageq_remove(m);
1061		if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1062			if (m->act_count < ACT_INIT)
1063				m->act_count = ACT_INIT;
1064			vm_pageq_enqueue(PQ_ACTIVE, m);
1065		}
1066	} else {
1067		if (m->act_count < ACT_INIT)
1068			m->act_count = ACT_INIT;
1069	}
1070	splx(s);
1071}
1072
1073/*
1074 *	vm_page_free_wakeup:
1075 *
1076 *	Helper routine for vm_page_free_toq() and vm_page_cache().  This
1077 *	routine is called when a page has been added to the cache or free
1078 *	queues.
1079 *
1080 *	This routine may not block.
1081 *	This routine must be called at splvm()
1082 */
1083static __inline void
1084vm_page_free_wakeup(void)
1085{
1086	/*
1087	 * if pageout daemon needs pages, then tell it that there are
1088	 * some free.
1089	 */
1090	if (vm_pageout_pages_needed &&
1091	    cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
1092		wakeup(&vm_pageout_pages_needed);
1093		vm_pageout_pages_needed = 0;
1094	}
1095	/*
1096	 * wakeup processes that are waiting on memory if we hit a
1097	 * high water mark. And wakeup scheduler process if we have
1098	 * lots of memory. this process will swapin processes.
1099	 */
1100	if (vm_pages_needed && !vm_page_count_min()) {
1101		vm_pages_needed = 0;
1102		wakeup(&cnt.v_free_count);
1103	}
1104}
1105
1106/*
1107 *	vm_page_free_toq:
1108 *
1109 *	Returns the given page to the PQ_FREE list,
1110 *	disassociating it with any VM object.
1111 *
1112 *	Object and page must be locked prior to entry.
1113 *	This routine may not block.
1114 */
1115
1116void
1117vm_page_free_toq(vm_page_t m)
1118{
1119	int s;
1120	struct vpgqueues *pq;
1121	vm_object_t object = m->object;
1122
1123	GIANT_REQUIRED;
1124	s = splvm();
1125	cnt.v_tfree++;
1126
1127	if (m->busy || ((m->queue - m->pc) == PQ_FREE)) {
1128		printf(
1129		"vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n",
1130		    (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0,
1131		    m->hold_count);
1132		if ((m->queue - m->pc) == PQ_FREE)
1133			panic("vm_page_free: freeing free page");
1134		else
1135			panic("vm_page_free: freeing busy page");
1136	}
1137
1138	/*
1139	 * unqueue, then remove page.  Note that we cannot destroy
1140	 * the page here because we do not want to call the pager's
1141	 * callback routine until after we've put the page on the
1142	 * appropriate free queue.
1143	 */
1144	vm_pageq_remove_nowakeup(m);
1145	vm_page_remove(m);
1146
1147	/*
1148	 * If fictitious remove object association and
1149	 * return, otherwise delay object association removal.
1150	 */
1151	if ((m->flags & PG_FICTITIOUS) != 0) {
1152		splx(s);
1153		return;
1154	}
1155
1156	m->valid = 0;
1157	vm_page_undirty(m);
1158
1159	if (m->wire_count != 0) {
1160		if (m->wire_count > 1) {
1161			panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx",
1162				m->wire_count, (long)m->pindex);
1163		}
1164		panic("vm_page_free: freeing wired page\n");
1165	}
1166
1167	/*
1168	 * If we've exhausted the object's resident pages we want to free
1169	 * it up.
1170	 */
1171	if (object &&
1172	    (object->type == OBJT_VNODE) &&
1173	    ((object->flags & OBJ_DEAD) == 0)
1174	) {
1175		struct vnode *vp = (struct vnode *)object->handle;
1176
1177		if (vp && VSHOULDFREE(vp))
1178			vfree(vp);
1179	}
1180
1181	/*
1182	 * Clear the UNMANAGED flag when freeing an unmanaged page.
1183	 */
1184	if (m->flags & PG_UNMANAGED) {
1185		m->flags &= ~PG_UNMANAGED;
1186	} else {
1187#ifdef __alpha__
1188		pmap_page_is_free(m);
1189#endif
1190	}
1191
1192	if (m->hold_count != 0) {
1193		m->flags &= ~PG_ZERO;
1194		m->queue = PQ_HOLD;
1195	} else
1196		m->queue = PQ_FREE + m->pc;
1197	pq = &vm_page_queues[m->queue];
1198	mtx_lock_spin(&vm_page_queue_free_mtx);
1199	pq->lcnt++;
1200	++(*pq->cnt);
1201
1202	/*
1203	 * Put zero'd pages on the end ( where we look for zero'd pages
1204	 * first ) and non-zerod pages at the head.
1205	 */
1206	if (m->flags & PG_ZERO) {
1207		TAILQ_INSERT_TAIL(&pq->pl, m, pageq);
1208		++vm_page_zero_count;
1209	} else {
1210		TAILQ_INSERT_HEAD(&pq->pl, m, pageq);
1211	}
1212	mtx_unlock_spin(&vm_page_queue_free_mtx);
1213	vm_page_free_wakeup();
1214	splx(s);
1215}
1216
1217/*
1218 *	vm_page_unmanage:
1219 *
1220 * 	Prevent PV management from being done on the page.  The page is
1221 *	removed from the paging queues as if it were wired, and as a
1222 *	consequence of no longer being managed the pageout daemon will not
1223 *	touch it (since there is no way to locate the pte mappings for the
1224 *	page).  madvise() calls that mess with the pmap will also no longer
1225 *	operate on the page.
1226 *
1227 *	Beyond that the page is still reasonably 'normal'.  Freeing the page
1228 *	will clear the flag.
1229 *
1230 *	This routine is used by OBJT_PHYS objects - objects using unswappable
1231 *	physical memory as backing store rather then swap-backed memory and
1232 *	will eventually be extended to support 4MB unmanaged physical
1233 *	mappings.
1234 */
1235void
1236vm_page_unmanage(vm_page_t m)
1237{
1238	int s;
1239
1240	s = splvm();
1241	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1242	if ((m->flags & PG_UNMANAGED) == 0) {
1243		if (m->wire_count == 0)
1244			vm_pageq_remove(m);
1245	}
1246	vm_page_flag_set(m, PG_UNMANAGED);
1247	splx(s);
1248}
1249
1250/*
1251 *	vm_page_wire:
1252 *
1253 *	Mark this page as wired down by yet
1254 *	another map, removing it from paging queues
1255 *	as necessary.
1256 *
1257 *	The page queues must be locked.
1258 *	This routine may not block.
1259 */
1260void
1261vm_page_wire(vm_page_t m)
1262{
1263	int s;
1264
1265	/*
1266	 * Only bump the wire statistics if the page is not already wired,
1267	 * and only unqueue the page if it is on some queue (if it is unmanaged
1268	 * it is already off the queues).
1269	 */
1270	s = splvm();
1271	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1272	if (m->wire_count == 0) {
1273		if ((m->flags & PG_UNMANAGED) == 0)
1274			vm_pageq_remove(m);
1275		cnt.v_wire_count++;
1276	}
1277	m->wire_count++;
1278	KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
1279	splx(s);
1280}
1281
1282/*
1283 *	vm_page_unwire:
1284 *
1285 *	Release one wiring of this page, potentially
1286 *	enabling it to be paged again.
1287 *
1288 *	Many pages placed on the inactive queue should actually go
1289 *	into the cache, but it is difficult to figure out which.  What
1290 *	we do instead, if the inactive target is well met, is to put
1291 *	clean pages at the head of the inactive queue instead of the tail.
1292 *	This will cause them to be moved to the cache more quickly and
1293 *	if not actively re-referenced, freed more quickly.  If we just
1294 *	stick these pages at the end of the inactive queue, heavy filesystem
1295 *	meta-data accesses can cause an unnecessary paging load on memory bound
1296 *	processes.  This optimization causes one-time-use metadata to be
1297 *	reused more quickly.
1298 *
1299 *	BUT, if we are in a low-memory situation we have no choice but to
1300 *	put clean pages on the cache queue.
1301 *
1302 *	A number of routines use vm_page_unwire() to guarantee that the page
1303 *	will go into either the inactive or active queues, and will NEVER
1304 *	be placed in the cache - for example, just after dirtying a page.
1305 *	dirty pages in the cache are not allowed.
1306 *
1307 *	The page queues must be locked.
1308 *	This routine may not block.
1309 */
1310void
1311vm_page_unwire(vm_page_t m, int activate)
1312{
1313	int s;
1314
1315	s = splvm();
1316	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1317	if (m->wire_count > 0) {
1318		m->wire_count--;
1319		if (m->wire_count == 0) {
1320			cnt.v_wire_count--;
1321			if (m->flags & PG_UNMANAGED) {
1322				;
1323			} else if (activate)
1324				vm_pageq_enqueue(PQ_ACTIVE, m);
1325			else {
1326				vm_page_flag_clear(m, PG_WINATCFLS);
1327				vm_pageq_enqueue(PQ_INACTIVE, m);
1328			}
1329		}
1330	} else {
1331		panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count);
1332	}
1333	splx(s);
1334}
1335
1336
1337/*
1338 * Move the specified page to the inactive queue.  If the page has
1339 * any associated swap, the swap is deallocated.
1340 *
1341 * Normally athead is 0 resulting in LRU operation.  athead is set
1342 * to 1 if we want this page to be 'as if it were placed in the cache',
1343 * except without unmapping it from the process address space.
1344 *
1345 * This routine may not block.
1346 */
1347static __inline void
1348_vm_page_deactivate(vm_page_t m, int athead)
1349{
1350	int s;
1351
1352	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1353	/*
1354	 * Ignore if already inactive.
1355	 */
1356	if (m->queue == PQ_INACTIVE)
1357		return;
1358
1359	s = splvm();
1360	if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) {
1361		if ((m->queue - m->pc) == PQ_CACHE)
1362			cnt.v_reactivated++;
1363		vm_page_flag_clear(m, PG_WINATCFLS);
1364		vm_pageq_remove(m);
1365		if (athead)
1366			TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1367		else
1368			TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq);
1369		m->queue = PQ_INACTIVE;
1370		vm_page_queues[PQ_INACTIVE].lcnt++;
1371		cnt.v_inactive_count++;
1372	}
1373	splx(s);
1374}
1375
1376void
1377vm_page_deactivate(vm_page_t m)
1378{
1379    _vm_page_deactivate(m, 0);
1380}
1381
1382/*
1383 * vm_page_try_to_cache:
1384 *
1385 * Returns 0 on failure, 1 on success
1386 */
1387int
1388vm_page_try_to_cache(vm_page_t m)
1389{
1390
1391	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1392	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1393	    (m->flags & (PG_BUSY|PG_UNMANAGED))) {
1394		return (0);
1395	}
1396	vm_page_test_dirty(m);
1397	if (m->dirty)
1398		return (0);
1399	vm_page_cache(m);
1400	return (1);
1401}
1402
1403/*
1404 * vm_page_try_to_free()
1405 *
1406 *	Attempt to free the page.  If we cannot free it, we do nothing.
1407 *	1 is returned on success, 0 on failure.
1408 */
1409int
1410vm_page_try_to_free(vm_page_t m)
1411{
1412
1413	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1414	if (m->dirty || m->hold_count || m->busy || m->wire_count ||
1415	    (m->flags & (PG_BUSY|PG_UNMANAGED))) {
1416		return (0);
1417	}
1418	vm_page_test_dirty(m);
1419	if (m->dirty)
1420		return (0);
1421	vm_page_busy(m);
1422	vm_page_protect(m, VM_PROT_NONE);
1423	vm_page_free(m);
1424	return (1);
1425}
1426
1427/*
1428 * vm_page_cache
1429 *
1430 * Put the specified page onto the page cache queue (if appropriate).
1431 *
1432 * This routine may not block.
1433 */
1434void
1435vm_page_cache(vm_page_t m)
1436{
1437	int s;
1438
1439	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1440	if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || m->wire_count) {
1441		printf("vm_page_cache: attempting to cache busy page\n");
1442		return;
1443	}
1444	if ((m->queue - m->pc) == PQ_CACHE)
1445		return;
1446
1447	/*
1448	 * Remove all pmaps and indicate that the page is not
1449	 * writeable or mapped.
1450	 */
1451	vm_page_protect(m, VM_PROT_NONE);
1452	if (m->dirty != 0) {
1453		panic("vm_page_cache: caching a dirty page, pindex: %ld",
1454			(long)m->pindex);
1455	}
1456	s = splvm();
1457	vm_pageq_remove_nowakeup(m);
1458	vm_pageq_enqueue(PQ_CACHE + m->pc, m);
1459	vm_page_free_wakeup();
1460	splx(s);
1461}
1462
1463/*
1464 * vm_page_dontneed
1465 *
1466 *	Cache, deactivate, or do nothing as appropriate.  This routine
1467 *	is typically used by madvise() MADV_DONTNEED.
1468 *
1469 *	Generally speaking we want to move the page into the cache so
1470 *	it gets reused quickly.  However, this can result in a silly syndrome
1471 *	due to the page recycling too quickly.  Small objects will not be
1472 *	fully cached.  On the otherhand, if we move the page to the inactive
1473 *	queue we wind up with a problem whereby very large objects
1474 *	unnecessarily blow away our inactive and cache queues.
1475 *
1476 *	The solution is to move the pages based on a fixed weighting.  We
1477 *	either leave them alone, deactivate them, or move them to the cache,
1478 *	where moving them to the cache has the highest weighting.
1479 *	By forcing some pages into other queues we eventually force the
1480 *	system to balance the queues, potentially recovering other unrelated
1481 *	space from active.  The idea is to not force this to happen too
1482 *	often.
1483 */
1484void
1485vm_page_dontneed(vm_page_t m)
1486{
1487	static int dnweight;
1488	int dnw;
1489	int head;
1490
1491	mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1492	dnw = ++dnweight;
1493
1494	/*
1495	 * occassionally leave the page alone
1496	 */
1497	if ((dnw & 0x01F0) == 0 ||
1498	    m->queue == PQ_INACTIVE ||
1499	    m->queue - m->pc == PQ_CACHE
1500	) {
1501		if (m->act_count >= ACT_INIT)
1502			--m->act_count;
1503		return;
1504	}
1505
1506	if (m->dirty == 0)
1507		vm_page_test_dirty(m);
1508
1509	if (m->dirty || (dnw & 0x0070) == 0) {
1510		/*
1511		 * Deactivate the page 3 times out of 32.
1512		 */
1513		head = 0;
1514	} else {
1515		/*
1516		 * Cache the page 28 times out of every 32.  Note that
1517		 * the page is deactivated instead of cached, but placed
1518		 * at the head of the queue instead of the tail.
1519		 */
1520		head = 1;
1521	}
1522	_vm_page_deactivate(m, head);
1523}
1524
1525/*
1526 * Grab a page, waiting until we are waken up due to the page
1527 * changing state.  We keep on waiting, if the page continues
1528 * to be in the object.  If the page doesn't exist, allocate it.
1529 *
1530 * This routine may block.
1531 */
1532vm_page_t
1533vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
1534{
1535	vm_page_t m;
1536	int s, generation;
1537
1538	GIANT_REQUIRED;
1539retrylookup:
1540	if ((m = vm_page_lookup(object, pindex)) != NULL) {
1541		if (m->busy || (m->flags & PG_BUSY)) {
1542			generation = object->generation;
1543
1544			s = splvm();
1545			while ((object->generation == generation) &&
1546					(m->busy || (m->flags & PG_BUSY))) {
1547				vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1548				tsleep(m, PVM, "pgrbwt", 0);
1549				if ((allocflags & VM_ALLOC_RETRY) == 0) {
1550					splx(s);
1551					return NULL;
1552				}
1553			}
1554			splx(s);
1555			goto retrylookup;
1556		} else {
1557			vm_page_lock_queues();
1558			if (allocflags & VM_ALLOC_WIRED)
1559				vm_page_wire(m);
1560			vm_page_busy(m);
1561			vm_page_unlock_queues();
1562			return m;
1563		}
1564	}
1565
1566	m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1567	if (m == NULL) {
1568		VM_WAIT;
1569		if ((allocflags & VM_ALLOC_RETRY) == 0)
1570			return NULL;
1571		goto retrylookup;
1572	}
1573
1574	return m;
1575}
1576
1577/*
1578 * Mapping function for valid bits or for dirty bits in
1579 * a page.  May not block.
1580 *
1581 * Inputs are required to range within a page.
1582 */
1583__inline int
1584vm_page_bits(int base, int size)
1585{
1586	int first_bit;
1587	int last_bit;
1588
1589	KASSERT(
1590	    base + size <= PAGE_SIZE,
1591	    ("vm_page_bits: illegal base/size %d/%d", base, size)
1592	);
1593
1594	if (size == 0)		/* handle degenerate case */
1595		return (0);
1596
1597	first_bit = base >> DEV_BSHIFT;
1598	last_bit = (base + size - 1) >> DEV_BSHIFT;
1599
1600	return ((2 << last_bit) - (1 << first_bit));
1601}
1602
1603/*
1604 *	vm_page_set_validclean:
1605 *
1606 *	Sets portions of a page valid and clean.  The arguments are expected
1607 *	to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
1608 *	of any partial chunks touched by the range.  The invalid portion of
1609 *	such chunks will be zero'd.
1610 *
1611 *	This routine may not block.
1612 *
1613 *	(base + size) must be less then or equal to PAGE_SIZE.
1614 */
1615void
1616vm_page_set_validclean(vm_page_t m, int base, int size)
1617{
1618	int pagebits;
1619	int frag;
1620	int endoff;
1621
1622	GIANT_REQUIRED;
1623	if (size == 0)	/* handle degenerate case */
1624		return;
1625
1626	/*
1627	 * If the base is not DEV_BSIZE aligned and the valid
1628	 * bit is clear, we have to zero out a portion of the
1629	 * first block.
1630	 */
1631	if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
1632	    (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
1633		pmap_zero_page_area(m, frag, base - frag);
1634
1635	/*
1636	 * If the ending offset is not DEV_BSIZE aligned and the
1637	 * valid bit is clear, we have to zero out a portion of
1638	 * the last block.
1639	 */
1640	endoff = base + size;
1641	if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
1642	    (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
1643		pmap_zero_page_area(m, endoff,
1644		    DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
1645
1646	/*
1647	 * Set valid, clear dirty bits.  If validating the entire
1648	 * page we can safely clear the pmap modify bit.  We also
1649	 * use this opportunity to clear the PG_NOSYNC flag.  If a process
1650	 * takes a write fault on a MAP_NOSYNC memory area the flag will
1651	 * be set again.
1652	 *
1653	 * We set valid bits inclusive of any overlap, but we can only
1654	 * clear dirty bits for DEV_BSIZE chunks that are fully within
1655	 * the range.
1656	 */
1657	pagebits = vm_page_bits(base, size);
1658	m->valid |= pagebits;
1659#if 0	/* NOT YET */
1660	if ((frag = base & (DEV_BSIZE - 1)) != 0) {
1661		frag = DEV_BSIZE - frag;
1662		base += frag;
1663		size -= frag;
1664		if (size < 0)
1665			size = 0;
1666	}
1667	pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
1668#endif
1669	m->dirty &= ~pagebits;
1670	if (base == 0 && size == PAGE_SIZE) {
1671		pmap_clear_modify(m);
1672		vm_page_flag_clear(m, PG_NOSYNC);
1673	}
1674}
1675
1676#if 0
1677
1678void
1679vm_page_set_dirty(vm_page_t m, int base, int size)
1680{
1681	m->dirty |= vm_page_bits(base, size);
1682}
1683
1684#endif
1685
1686void
1687vm_page_clear_dirty(vm_page_t m, int base, int size)
1688{
1689	GIANT_REQUIRED;
1690	m->dirty &= ~vm_page_bits(base, size);
1691}
1692
1693/*
1694 *	vm_page_set_invalid:
1695 *
1696 *	Invalidates DEV_BSIZE'd chunks within a page.  Both the
1697 *	valid and dirty bits for the effected areas are cleared.
1698 *
1699 *	May not block.
1700 */
1701void
1702vm_page_set_invalid(vm_page_t m, int base, int size)
1703{
1704	int bits;
1705
1706	GIANT_REQUIRED;
1707	bits = vm_page_bits(base, size);
1708	m->valid &= ~bits;
1709	m->dirty &= ~bits;
1710	m->object->generation++;
1711}
1712
1713/*
1714 * vm_page_zero_invalid()
1715 *
1716 *	The kernel assumes that the invalid portions of a page contain
1717 *	garbage, but such pages can be mapped into memory by user code.
1718 *	When this occurs, we must zero out the non-valid portions of the
1719 *	page so user code sees what it expects.
1720 *
1721 *	Pages are most often semi-valid when the end of a file is mapped
1722 *	into memory and the file's size is not page aligned.
1723 */
1724void
1725vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
1726{
1727	int b;
1728	int i;
1729
1730	/*
1731	 * Scan the valid bits looking for invalid sections that
1732	 * must be zerod.  Invalid sub-DEV_BSIZE'd areas ( where the
1733	 * valid bit may be set ) have already been zerod by
1734	 * vm_page_set_validclean().
1735	 */
1736	for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
1737		if (i == (PAGE_SIZE / DEV_BSIZE) ||
1738		    (m->valid & (1 << i))
1739		) {
1740			if (i > b) {
1741				pmap_zero_page_area(m,
1742				    b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
1743			}
1744			b = i + 1;
1745		}
1746	}
1747
1748	/*
1749	 * setvalid is TRUE when we can safely set the zero'd areas
1750	 * as being valid.  We can do this if there are no cache consistancy
1751	 * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
1752	 */
1753	if (setvalid)
1754		m->valid = VM_PAGE_BITS_ALL;
1755}
1756
1757/*
1758 *	vm_page_is_valid:
1759 *
1760 *	Is (partial) page valid?  Note that the case where size == 0
1761 *	will return FALSE in the degenerate case where the page is
1762 *	entirely invalid, and TRUE otherwise.
1763 *
1764 *	May not block.
1765 */
1766int
1767vm_page_is_valid(vm_page_t m, int base, int size)
1768{
1769	int bits = vm_page_bits(base, size);
1770
1771	if (m->valid && ((m->valid & bits) == bits))
1772		return 1;
1773	else
1774		return 0;
1775}
1776
1777/*
1778 * update dirty bits from pmap/mmu.  May not block.
1779 */
1780void
1781vm_page_test_dirty(vm_page_t m)
1782{
1783	if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) {
1784		vm_page_dirty(m);
1785	}
1786}
1787
1788int so_zerocp_fullpage = 0;
1789
1790void
1791vm_page_cowfault(vm_page_t m)
1792{
1793	vm_page_t mnew;
1794	vm_object_t object;
1795	vm_pindex_t pindex;
1796
1797	object = m->object;
1798	pindex = m->pindex;
1799	vm_page_busy(m);
1800
1801 retry_alloc:
1802	vm_page_remove(m);
1803	mnew = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
1804	if (mnew == NULL) {
1805		vm_page_insert(m, object, pindex);
1806		VM_WAIT;
1807		goto retry_alloc;
1808	}
1809
1810	if (m->cow == 0) {
1811		/*
1812		 * check to see if we raced with an xmit complete when
1813		 * waiting to allocate a page.  If so, put things back
1814		 * the way they were
1815		 */
1816		vm_page_busy(mnew);
1817		vm_page_free(mnew);
1818		vm_page_insert(m, object, pindex);
1819	} else { /* clear COW & copy page */
1820		if (so_zerocp_fullpage) {
1821			mnew->valid = VM_PAGE_BITS_ALL;
1822		} else {
1823			vm_page_copy(m, mnew);
1824		}
1825		vm_page_dirty(mnew);
1826		vm_page_flag_clear(mnew, PG_BUSY);
1827	}
1828}
1829
1830void
1831vm_page_cowclear(vm_page_t m)
1832{
1833
1834	/* XXX KDM find out if giant is required here. */
1835	GIANT_REQUIRED;
1836	if (m->cow) {
1837		atomic_subtract_int(&m->cow, 1);
1838		/*
1839		 * let vm_fault add back write permission  lazily
1840		 */
1841	}
1842	/*
1843	 *  sf_buf_free() will free the page, so we needn't do it here
1844	 */
1845}
1846
1847void
1848vm_page_cowsetup(vm_page_t m)
1849{
1850	/* XXX KDM find out if giant is required here */
1851	GIANT_REQUIRED;
1852	atomic_add_int(&m->cow, 1);
1853	vm_page_protect(m, VM_PROT_READ);
1854}
1855
1856#include "opt_ddb.h"
1857#ifdef DDB
1858#include <sys/kernel.h>
1859
1860#include <ddb/ddb.h>
1861
1862DB_SHOW_COMMAND(page, vm_page_print_page_info)
1863{
1864	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
1865	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
1866	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
1867	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
1868	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
1869	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
1870	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
1871	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
1872	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
1873	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
1874}
1875
1876DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
1877{
1878	int i;
1879	db_printf("PQ_FREE:");
1880	for (i = 0; i < PQ_L2_SIZE; i++) {
1881		db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt);
1882	}
1883	db_printf("\n");
1884
1885	db_printf("PQ_CACHE:");
1886	for (i = 0; i < PQ_L2_SIZE; i++) {
1887		db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt);
1888	}
1889	db_printf("\n");
1890
1891	db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
1892		vm_page_queues[PQ_ACTIVE].lcnt,
1893		vm_page_queues[PQ_INACTIVE].lcnt);
1894}
1895#endif /* DDB */
1896