xref: /freebsd/sys/vm/vm_page.h (revision 2c28a1054036c739c1de7ca1009bc4d3e2a56a67)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
17df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_page.h	8.2 (Berkeley) 12/13/93
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
642c28a105SAlan Cox  * $Id: vm_page.h,v 1.68 1999/08/15 05:36:43 alc Exp $
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Resident memory system definitions.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #ifndef	_VM_PAGE_
72df8bae1dSRodney W. Grimes #define	_VM_PAGE_
73df8bae1dSRodney W. Grimes 
740237469fSJulian Elischer #if !defined(KLD_MODULE)
7520f71813SJohn-Mark Gurney #include "opt_vmpage.h"
760237469fSJulian Elischer #endif
7720f71813SJohn-Mark Gurney 
78f919ebdeSDavid Greenman #include <vm/pmap.h>
79069e9bc1SDoug Rabson #include <machine/atomic.h>
80069e9bc1SDoug Rabson 
81df8bae1dSRodney W. Grimes /*
82df8bae1dSRodney W. Grimes  *	Management of resident (logical) pages.
83df8bae1dSRodney W. Grimes  *
84df8bae1dSRodney W. Grimes  *	A small structure is kept for each resident
85df8bae1dSRodney W. Grimes  *	page, indexed by page number.  Each structure
86df8bae1dSRodney W. Grimes  *	is an element of several lists:
87df8bae1dSRodney W. Grimes  *
88df8bae1dSRodney W. Grimes  *		A hash table bucket used to quickly
89df8bae1dSRodney W. Grimes  *		perform object/offset lookups
90df8bae1dSRodney W. Grimes  *
91df8bae1dSRodney W. Grimes  *		A list of all pages for a given object,
92df8bae1dSRodney W. Grimes  *		so they can be quickly deactivated at
93df8bae1dSRodney W. Grimes  *		time of deallocation.
94df8bae1dSRodney W. Grimes  *
95df8bae1dSRodney W. Grimes  *		An ordered list of pages due for pageout.
96df8bae1dSRodney W. Grimes  *
97df8bae1dSRodney W. Grimes  *	In addition, the structure contains the object
98df8bae1dSRodney W. Grimes  *	and offset to which this page belongs (for pageout),
99df8bae1dSRodney W. Grimes  *	and sundry status bits.
100df8bae1dSRodney W. Grimes  *
101df8bae1dSRodney W. Grimes  *	Fields in this structure are locked either by the lock on the
102df8bae1dSRodney W. Grimes  *	object that the page belongs to (O) or by the lock on the page
103df8bae1dSRodney W. Grimes  *	queues (P).
1044221e284SAlan Cox  *
1054221e284SAlan Cox  *	The 'valid' and 'dirty' fields are distinct.  A page may have dirty
1064221e284SAlan Cox  *	bits set without having associated valid bits set.  This is used by
1074221e284SAlan Cox  *	NFS to implement piecemeal writes.
108df8bae1dSRodney W. Grimes  */
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes TAILQ_HEAD(pglist, vm_page);
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes struct vm_page {
1130d94caffSDavid Greenman 	TAILQ_ENTRY(vm_page) pageq;	/* queue info for FIFO queue or free list (P) */
1141c7c3c6aSMatthew Dillon 	struct vm_page	*hnext;		/* hash table link (O,P)	*/
115df8bae1dSRodney W. Grimes 	TAILQ_ENTRY(vm_page) listq;	/* pages in same object (O) 	*/
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes 	vm_object_t object;		/* which object am I in (O,P)*/
118a316d390SJohn Dyson 	vm_pindex_t pindex;		/* offset into object (O,P) */
1190d94caffSDavid Greenman 	vm_offset_t phys_addr;		/* physical address of page */
1205070c7f8SJohn Dyson 	u_short	queue;			/* page queue index */
1215070c7f8SJohn Dyson 	u_short	flags,			/* see below */
1225070c7f8SJohn Dyson 		pc;			/* page color */
123df8bae1dSRodney W. Grimes 	u_short wire_count;		/* wired down maps refs (P) */
12426f9a767SRodney W. Grimes 	short hold_count;		/* page hold count */
125bd7e5f99SJohn Dyson 	u_char	act_count;		/* page usage count */
126bd7e5f99SJohn Dyson 	u_char	busy;			/* page busy count */
127bd7e5f99SJohn Dyson 	/* NOTE that these must support one bit per DEV_BSIZE in a page!!! */
128bd7e5f99SJohn Dyson 	/* so, on normal X86 kernels, they must be at least 8 bits wide */
129ecbb00a2SDoug Rabson #if PAGE_SIZE == 4096
130bd7e5f99SJohn Dyson 	u_char	valid;			/* map of valid DEV_BSIZE chunks */
131bd7e5f99SJohn Dyson 	u_char	dirty;			/* map of dirty DEV_BSIZE chunks */
132ecbb00a2SDoug Rabson #elif PAGE_SIZE == 8192
133ecbb00a2SDoug Rabson 	u_short	valid;			/* map of valid DEV_BSIZE chunks */
134ecbb00a2SDoug Rabson 	u_short	dirty;			/* map of dirty DEV_BSIZE chunks */
135ecbb00a2SDoug Rabson #endif
136df8bae1dSRodney W. Grimes };
137df8bae1dSRodney W. Grimes 
1385070c7f8SJohn Dyson /*
1391c7c3c6aSMatthew Dillon  * note SWAPBLK_NONE is a flag, basically the high bit.
1401c7c3c6aSMatthew Dillon  */
1411c7c3c6aSMatthew Dillon 
1421c7c3c6aSMatthew Dillon #define SWAPBLK_MASK	((daddr_t)((u_daddr_t)-1 >> 1))		/* mask */
1431c7c3c6aSMatthew Dillon #define SWAPBLK_NONE	((daddr_t)((u_daddr_t)SWAPBLK_MASK + 1))/* flag */
1441c7c3c6aSMatthew Dillon 
1450237469fSJulian Elischer #if !defined(KLD_MODULE)
1460237469fSJulian Elischer 
1471c7c3c6aSMatthew Dillon /*
1485070c7f8SJohn Dyson  * Page coloring parameters
1495070c7f8SJohn Dyson  */
150faa273d5SMatthew Dillon /* Each of PQ_FREE, and PQ_CACHE have PQ_HASH_SIZE entries */
1515070c7f8SJohn Dyson 
1525070c7f8SJohn Dyson /* Define one of the following */
153b9cefc08SJohn Dyson #if defined(PQ_HUGECACHE)
154b9cefc08SJohn Dyson #define PQ_PRIME1 31	/* Prime number somewhat less than PQ_HASH_SIZE */
155b9cefc08SJohn Dyson #define PQ_PRIME2 23	/* Prime number somewhat less than PQ_HASH_SIZE */
156b9cefc08SJohn Dyson #define PQ_L2_SIZE 256	/* A number of colors opt for 1M cache */
157b9cefc08SJohn Dyson #endif
158b9cefc08SJohn Dyson 
159b9cefc08SJohn Dyson /* Define one of the following */
1605070c7f8SJohn Dyson #if defined(PQ_LARGECACHE)
1615070c7f8SJohn Dyson #define PQ_PRIME1 31	/* Prime number somewhat less than PQ_HASH_SIZE */
1625070c7f8SJohn Dyson #define PQ_PRIME2 23	/* Prime number somewhat less than PQ_HASH_SIZE */
1635070c7f8SJohn Dyson #define PQ_L2_SIZE 128	/* A number of colors opt for 512K cache */
1645070c7f8SJohn Dyson #endif
1655070c7f8SJohn Dyson 
1665070c7f8SJohn Dyson 
1675070c7f8SJohn Dyson /*
1685070c7f8SJohn Dyson  * Use 'options PQ_NOOPT' to disable page coloring
1695070c7f8SJohn Dyson  */
1705070c7f8SJohn Dyson #if defined(PQ_NOOPT)
1715070c7f8SJohn Dyson #define PQ_PRIME1 1
1725070c7f8SJohn Dyson #define PQ_PRIME2 1
1735070c7f8SJohn Dyson #define PQ_L2_SIZE 1
1745070c7f8SJohn Dyson #endif
1755070c7f8SJohn Dyson 
176853a7bc8SJohn Dyson #if defined(PQ_NORMALCACHE)
1775070c7f8SJohn Dyson #define PQ_PRIME1 5	/* Prime number somewhat less than PQ_HASH_SIZE */
1785070c7f8SJohn Dyson #define PQ_PRIME2 3	/* Prime number somewhat less than PQ_HASH_SIZE */
1795070c7f8SJohn Dyson #define PQ_L2_SIZE 16	/* A reasonable number of colors (opt for 64K cache) */
1805070c7f8SJohn Dyson #endif
1815070c7f8SJohn Dyson 
1821aefb1d9SAlan Cox #if defined(PQ_MEDIUMCACHE)
183853a7bc8SJohn Dyson #define PQ_PRIME1 13	/* Prime number somewhat less than PQ_HASH_SIZE */
184853a7bc8SJohn Dyson #define PQ_PRIME2 7	/* Prime number somewhat less than PQ_HASH_SIZE */
185853a7bc8SJohn Dyson #define PQ_L2_SIZE 64	/* A number of colors opt for 256K cache */
186853a7bc8SJohn Dyson #endif
187853a7bc8SJohn Dyson 
1881aefb1d9SAlan Cox #if !defined(PQ_L2_SIZE)
1891aefb1d9SAlan Cox #define PQ_PRIME1 9	/* Produces a good PQ_L2_SIZE/3 + PQ_PRIME1 */
1901aefb1d9SAlan Cox #define PQ_PRIME2 5	/* Prime number somewhat less than PQ_HASH_SIZE */
1911aefb1d9SAlan Cox #define PQ_L2_SIZE 32	/* 512KB or smaller, 4-way set-associative cache */
1921aefb1d9SAlan Cox #endif
1931aefb1d9SAlan Cox 
1945070c7f8SJohn Dyson #define PQ_L2_MASK (PQ_L2_SIZE - 1)
1955070c7f8SJohn Dyson 
196175d1f69SAlan Cox #if 1
1973739d7daSMatt Jacob #define PQ_NONE 0
1983739d7daSMatt Jacob #define PQ_FREE	1
1993739d7daSMatt Jacob #define PQ_INACTIVE (1 + 1*PQ_L2_SIZE)
2003739d7daSMatt Jacob #define PQ_ACTIVE (2 + 1*PQ_L2_SIZE)
2013739d7daSMatt Jacob #define PQ_CACHE (3 + 1*PQ_L2_SIZE)
2023739d7daSMatt Jacob #define PQ_COUNT (3 + 2*PQ_L2_SIZE)
2033739d7daSMatt Jacob #else
204514bfcc4SAlan Cox #define PQ_NONE		PQ_COUNT
205514bfcc4SAlan Cox #define PQ_FREE		0
206514bfcc4SAlan Cox #define PQ_INACTIVE	PQ_L2_SIZE
207514bfcc4SAlan Cox #define PQ_ACTIVE	(1 +   PQ_L2_SIZE)
208514bfcc4SAlan Cox #define PQ_CACHE	(2 +   PQ_L2_SIZE)
209514bfcc4SAlan Cox #define PQ_COUNT	(2 + 2*PQ_L2_SIZE)
2103739d7daSMatt Jacob #endif
2115070c7f8SJohn Dyson 
2125070c7f8SJohn Dyson extern struct vpgqueues {
2135070c7f8SJohn Dyson 	struct pglist *pl;
2145070c7f8SJohn Dyson 	int	*cnt;
2155d2aec89SAlan Cox 	int	lcnt;
2165070c7f8SJohn Dyson } vm_page_queues[PQ_COUNT];
217bd7e5f99SJohn Dyson 
2180237469fSJulian Elischer #endif
2190237469fSJulian Elischer 
220df8bae1dSRodney W. Grimes /*
221df8bae1dSRodney W. Grimes  * These are the flags defined for vm_page.
222df8bae1dSRodney W. Grimes  *
223df8bae1dSRodney W. Grimes  * Note: PG_FILLED and PG_DIRTY are added for the filesystems.
224df8bae1dSRodney W. Grimes  */
2251c7c3c6aSMatthew Dillon #define	PG_BUSY		0x0001		/* page is in transit (O) */
2261c7c3c6aSMatthew Dillon #define	PG_WANTED	0x0002		/* someone is waiting for page (O) */
2271c7c3c6aSMatthew Dillon #define	PG_FICTITIOUS	0x0008		/* physical page doesn't exist (O) */
2281c7c3c6aSMatthew Dillon #define	PG_WRITEABLE	0x0010		/* page is mapped writeable */
2291c7c3c6aSMatthew Dillon #define PG_MAPPED	0x0020		/* page is mapped */
2301c7c3c6aSMatthew Dillon #define	PG_ZERO		0x0040		/* page is zeroed */
2311c7c3c6aSMatthew Dillon #define PG_REFERENCED	0x0080		/* page has been referenced */
2321c7c3c6aSMatthew Dillon #define PG_CLEANCHK	0x0100		/* page will be checked for cleaning */
2331c7c3c6aSMatthew Dillon #define PG_SWAPINPROG	0x0200		/* swap I/O in progress on page	     */
234df8bae1dSRodney W. Grimes 
23524a1cce3SDavid Greenman /*
23624a1cce3SDavid Greenman  * Misc constants.
23724a1cce3SDavid Greenman  */
23824a1cce3SDavid Greenman 
23924a1cce3SDavid Greenman #define ACT_DECLINE		1
24024a1cce3SDavid Greenman #define ACT_ADVANCE		3
24138efa82bSJohn Dyson #define ACT_INIT		5
2425070c7f8SJohn Dyson #define ACT_MAX			64
24324a1cce3SDavid Greenman #define PFCLUSTER_BEHIND	3
24424a1cce3SDavid Greenman #define PFCLUSTER_AHEAD		3
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes #ifdef KERNEL
247df8bae1dSRodney W. Grimes /*
24824a1cce3SDavid Greenman  * Each pageable resident page falls into one of four lists:
249df8bae1dSRodney W. Grimes  *
250df8bae1dSRodney W. Grimes  *	free
251df8bae1dSRodney W. Grimes  *		Available for allocation now.
25224a1cce3SDavid Greenman  *
25324a1cce3SDavid Greenman  * The following are all LRU sorted:
25424a1cce3SDavid Greenman  *
25524a1cce3SDavid Greenman  *	cache
25624a1cce3SDavid Greenman  *		Almost available for allocation. Still in an
25724a1cce3SDavid Greenman  *		object, but clean and immediately freeable at
25824a1cce3SDavid Greenman  *		non-interrupt times.
25924a1cce3SDavid Greenman  *
260df8bae1dSRodney W. Grimes  *	inactive
2616c5e9bbdSMike Pritchard  *		Low activity, candidates for reclamation.
262df8bae1dSRodney W. Grimes  *		This is the list of pages that should be
263df8bae1dSRodney W. Grimes  *		paged out next.
26424a1cce3SDavid Greenman  *
265df8bae1dSRodney W. Grimes  *	active
26624a1cce3SDavid Greenman  *		Pages that are "active" i.e. they have been
26724a1cce3SDavid Greenman  *		recently referenced.
26810ad4d48SJohn Dyson  *
26910ad4d48SJohn Dyson  *	zero
27010ad4d48SJohn Dyson  *		Pages that are really free and have been pre-zeroed
27110ad4d48SJohn Dyson  *
272df8bae1dSRodney W. Grimes  */
273df8bae1dSRodney W. Grimes 
2740237469fSJulian Elischer #if !defined(KLD_MODULE)
2750237469fSJulian Elischer 
2765070c7f8SJohn Dyson extern struct pglist vm_page_queue_free[PQ_L2_SIZE];/* memory free queue */
2770d94caffSDavid Greenman extern struct pglist vm_page_queue_active;	/* active memory queue */
2780d94caffSDavid Greenman extern struct pglist vm_page_queue_inactive;	/* inactive memory queue */
2795070c7f8SJohn Dyson extern struct pglist vm_page_queue_cache[PQ_L2_SIZE];/* cache memory queue */
280df8bae1dSRodney W. Grimes 
2810237469fSJulian Elischer #endif
2820237469fSJulian Elischer 
283a316d390SJohn Dyson extern int vm_page_zero_count;
284a316d390SJohn Dyson 
2850d94caffSDavid Greenman extern vm_page_t vm_page_array;		/* First resident page in table */
2860d94caffSDavid Greenman extern long first_page;			/* first physical page number */
2870d94caffSDavid Greenman 
288df8bae1dSRodney W. Grimes #define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes #define PHYS_TO_VM_PAGE(pa) \
291df8bae1dSRodney W. Grimes 		(&vm_page_array[atop(pa) - first_page ])
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes /*
294df8bae1dSRodney W. Grimes  *	Functions implemented as macros
295df8bae1dSRodney W. Grimes  */
296df8bae1dSRodney W. Grimes 
297e69763a3SDoug Rabson static __inline void
298e69763a3SDoug Rabson vm_page_flag_set(vm_page_t m, unsigned int bits)
299e69763a3SDoug Rabson {
300e69763a3SDoug Rabson 	atomic_set_short(&(m)->flags, bits);
301df8bae1dSRodney W. Grimes }
302df8bae1dSRodney W. Grimes 
303e69763a3SDoug Rabson static __inline void
304e69763a3SDoug Rabson vm_page_flag_clear(vm_page_t m, unsigned int bits)
305e69763a3SDoug Rabson {
306e69763a3SDoug Rabson 	atomic_clear_short(&(m)->flags, bits);
307df8bae1dSRodney W. Grimes }
308df8bae1dSRodney W. Grimes 
309e69763a3SDoug Rabson #if 0
310e69763a3SDoug Rabson static __inline void
311e69763a3SDoug Rabson vm_page_assert_wait(vm_page_t m, int interruptible)
312e69763a3SDoug Rabson {
313e69763a3SDoug Rabson 	vm_page_flag_set(m, PG_WANTED);
314e69763a3SDoug Rabson 	assert_wait((int) m, interruptible);
315e69763a3SDoug Rabson }
316e69763a3SDoug Rabson #endif
317069e9bc1SDoug Rabson 
318e69763a3SDoug Rabson static __inline void
319e69763a3SDoug Rabson vm_page_busy(vm_page_t m)
320e69763a3SDoug Rabson {
321a7039a1dSMatthew Dillon 	KASSERT((m->flags & PG_BUSY) == 0, ("vm_page_busy: page already busy!!!"));
322e69763a3SDoug Rabson 	vm_page_flag_set(m, PG_BUSY);
323e69763a3SDoug Rabson }
324e69763a3SDoug Rabson 
3251c7c3c6aSMatthew Dillon /*
3261c7c3c6aSMatthew Dillon  *	vm_page_flash:
3271c7c3c6aSMatthew Dillon  *
3281c7c3c6aSMatthew Dillon  *	wakeup anyone waiting for the page.
3291c7c3c6aSMatthew Dillon  */
3301c7c3c6aSMatthew Dillon 
331e69763a3SDoug Rabson static __inline void
3321c7c3c6aSMatthew Dillon vm_page_flash(vm_page_t m)
333e69763a3SDoug Rabson {
334e69763a3SDoug Rabson 	if (m->flags & PG_WANTED) {
335e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_WANTED);
336e69763a3SDoug Rabson 		wakeup(m);
337e69763a3SDoug Rabson 	}
338e69763a3SDoug Rabson }
339e69763a3SDoug Rabson 
3401c7c3c6aSMatthew Dillon /*
3411c7c3c6aSMatthew Dillon  *	vm_page_wakeup:
3421c7c3c6aSMatthew Dillon  *
3431c7c3c6aSMatthew Dillon  *	clear the PG_BUSY flag and wakeup anyone waiting for the
3441c7c3c6aSMatthew Dillon  *	page.
3451c7c3c6aSMatthew Dillon  *
3461c7c3c6aSMatthew Dillon  */
3471c7c3c6aSMatthew Dillon 
3481c7c3c6aSMatthew Dillon static __inline void
3491c7c3c6aSMatthew Dillon vm_page_wakeup(vm_page_t m)
3501c7c3c6aSMatthew Dillon {
351a7039a1dSMatthew Dillon 	KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!"));
3521c7c3c6aSMatthew Dillon 	vm_page_flag_clear(m, PG_BUSY);
3531c7c3c6aSMatthew Dillon 	vm_page_flash(m);
3541c7c3c6aSMatthew Dillon }
3551c7c3c6aSMatthew Dillon 
356161a8f65SMatthew Dillon /*
357161a8f65SMatthew Dillon  *
358161a8f65SMatthew Dillon  *
359161a8f65SMatthew Dillon  */
360161a8f65SMatthew Dillon 
361e69763a3SDoug Rabson static __inline void
362e69763a3SDoug Rabson vm_page_io_start(vm_page_t m)
363e69763a3SDoug Rabson {
364e69763a3SDoug Rabson 	atomic_add_char(&(m)->busy, 1);
365e69763a3SDoug Rabson }
366e69763a3SDoug Rabson 
367e69763a3SDoug Rabson static __inline void
368e69763a3SDoug Rabson vm_page_io_finish(vm_page_t m)
369e69763a3SDoug Rabson {
370e69763a3SDoug Rabson 	atomic_subtract_char(&m->busy, 1);
3711c7c3c6aSMatthew Dillon 	if (m->busy == 0)
3721c7c3c6aSMatthew Dillon 		vm_page_flash(m);
3738f9110f6SJohn Dyson }
3748f9110f6SJohn Dyson 
3758f9110f6SJohn Dyson 
3760d94caffSDavid Greenman #if PAGE_SIZE == 4096
3770d94caffSDavid Greenman #define VM_PAGE_BITS_ALL 0xff
3780d94caffSDavid Greenman #endif
3790d94caffSDavid Greenman 
3800d94caffSDavid Greenman #if PAGE_SIZE == 8192
3810d94caffSDavid Greenman #define VM_PAGE_BITS_ALL 0xffff
3820d94caffSDavid Greenman #endif
3830d94caffSDavid Greenman 
3846d40c3d3SDavid Greenman #define VM_ALLOC_NORMAL		0
3856d40c3d3SDavid Greenman #define VM_ALLOC_INTERRUPT	1
3866d40c3d3SDavid Greenman #define VM_ALLOC_SYSTEM		2
387bd7e5f99SJohn Dyson #define	VM_ALLOC_ZERO		3
38895461b45SJohn Dyson #define	VM_ALLOC_RETRY		0x80
3890d94caffSDavid Greenman 
390df8bae1dSRodney W. Grimes void vm_page_activate __P((vm_page_t));
391a316d390SJohn Dyson vm_page_t vm_page_alloc __P((vm_object_t, vm_pindex_t, int));
39295461b45SJohn Dyson vm_page_t vm_page_grab __P((vm_object_t, vm_pindex_t, int));
393d2fc5315SPoul-Henning Kamp void vm_page_cache __P((register vm_page_t));
3947f5fe93fSJohn Dyson static __inline void vm_page_copy __P((vm_page_t, vm_page_t));
3951c7c3c6aSMatthew Dillon static __inline void vm_page_free __P((vm_page_t));
3961c7c3c6aSMatthew Dillon static __inline void vm_page_free_zero __P((vm_page_t));
397df8bae1dSRodney W. Grimes void vm_page_deactivate __P((vm_page_t));
398a316d390SJohn Dyson void vm_page_insert __P((vm_page_t, vm_object_t, vm_pindex_t));
399a316d390SJohn Dyson vm_page_t vm_page_lookup __P((vm_object_t, vm_pindex_t));
400efcae3d3SMatthew Dillon void vm_page_remove __P((vm_page_t));
401a316d390SJohn Dyson void vm_page_rename __P((vm_page_t, vm_object_t, vm_pindex_t));
40226f9a767SRodney W. Grimes vm_offset_t vm_page_startup __P((vm_offset_t, vm_offset_t, vm_offset_t));
40373007561SDavid Greenman void vm_page_unwire __P((vm_page_t, int));
404df8bae1dSRodney W. Grimes void vm_page_wire __P((vm_page_t));
40567bf6868SJohn Dyson void vm_page_unqueue __P((vm_page_t));
40667bf6868SJohn Dyson void vm_page_unqueue_nowakeup __P((vm_page_t));
40710ad4d48SJohn Dyson void vm_page_set_validclean __P((vm_page_t, int, int));
4084221e284SAlan Cox void vm_page_set_dirty __P((vm_page_t, int, int));
4094221e284SAlan Cox void vm_page_clear_dirty __P((vm_page_t, int, int));
4100d94caffSDavid Greenman void vm_page_set_invalid __P((vm_page_t, int, int));
4117f5fe93fSJohn Dyson static __inline boolean_t vm_page_zero_fill __P((vm_page_t));
4120d94caffSDavid Greenman int vm_page_is_valid __P((vm_page_t, int, int));
4130d94caffSDavid Greenman void vm_page_test_dirty __P((vm_page_t));
414d2fc5315SPoul-Henning Kamp int vm_page_bits __P((int, int));
415a0e7b3e5SMatthew Dillon vm_page_t _vm_page_list_find __P((int, int));
4161c7c3c6aSMatthew Dillon #if 0
417ffc82b0aSJohn Dyson int vm_page_sleep(vm_page_t m, char *msg, char *busy);
4181c7c3c6aSMatthew Dillon int vm_page_asleep(vm_page_t m, char *msg, char *busy);
4191c7c3c6aSMatthew Dillon #endif
4208d17e694SJulian Elischer void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid);
421faa273d5SMatthew Dillon void vm_page_free_toq(vm_page_t m);
422d2fc5315SPoul-Henning Kamp 
42326f9a767SRodney W. Grimes /*
42426f9a767SRodney W. Grimes  * Keep page from being freed by the page daemon
42526f9a767SRodney W. Grimes  * much of the same effect as wiring, except much lower
42626f9a767SRodney W. Grimes  * overhead and should be used only for *very* temporary
42726f9a767SRodney W. Grimes  * holding ("wiring").
42826f9a767SRodney W. Grimes  */
4297666fb47SBruce Evans static __inline void
4304f21005bSBruce Evans vm_page_hold(vm_page_t mem)
43126f9a767SRodney W. Grimes {
43226f9a767SRodney W. Grimes 	mem->hold_count++;
43326f9a767SRodney W. Grimes }
43426f9a767SRodney W. Grimes 
4357666fb47SBruce Evans static __inline void
4364f21005bSBruce Evans vm_page_unhold(vm_page_t mem)
43726f9a767SRodney W. Grimes {
43827de4e40SDavid Greenman 	--mem->hold_count;
4395526d2d9SEivind Eklund 	KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!"));
44026f9a767SRodney W. Grimes }
44126f9a767SRodney W. Grimes 
442a7039a1dSMatthew Dillon /*
443a7039a1dSMatthew Dillon  * 	vm_page_protect:
444a7039a1dSMatthew Dillon  *
445a7039a1dSMatthew Dillon  *	Reduce the protection of a page.  This routine never
446a7039a1dSMatthew Dillon  *	raises the protection and therefore can be safely
447a7039a1dSMatthew Dillon  *	called if the page is already at VM_PROT_NONE ( it
448a7039a1dSMatthew Dillon  *	will be a NOP effectively ).
449a7039a1dSMatthew Dillon  */
450a7039a1dSMatthew Dillon 
4517666fb47SBruce Evans static __inline void
452f919ebdeSDavid Greenman vm_page_protect(vm_page_t mem, int prot)
453f919ebdeSDavid Greenman {
454f919ebdeSDavid Greenman 	if (prot == VM_PROT_NONE) {
455f919ebdeSDavid Greenman 		if (mem->flags & (PG_WRITEABLE|PG_MAPPED)) {
4568f9110f6SJohn Dyson 			pmap_page_protect(VM_PAGE_TO_PHYS(mem), VM_PROT_NONE);
457e69763a3SDoug Rabson 			vm_page_flag_clear(mem, PG_WRITEABLE|PG_MAPPED);
458f919ebdeSDavid Greenman 		}
459f919ebdeSDavid Greenman 	} else if ((prot == VM_PROT_READ) && (mem->flags & PG_WRITEABLE)) {
4608f9110f6SJohn Dyson 		pmap_page_protect(VM_PAGE_TO_PHYS(mem), VM_PROT_READ);
461e69763a3SDoug Rabson 		vm_page_flag_clear(mem, PG_WRITEABLE);
462f919ebdeSDavid Greenman 	}
463f919ebdeSDavid Greenman }
464f919ebdeSDavid Greenman 
4657f5fe93fSJohn Dyson /*
4667f5fe93fSJohn Dyson  *	vm_page_zero_fill:
4677f5fe93fSJohn Dyson  *
4687f5fe93fSJohn Dyson  *	Zero-fill the specified page.
4697f5fe93fSJohn Dyson  *	Written as a standard pagein routine, to
4707f5fe93fSJohn Dyson  *	be used by the zero-fill object.
4717f5fe93fSJohn Dyson  */
4727f5fe93fSJohn Dyson static __inline boolean_t
4737f5fe93fSJohn Dyson vm_page_zero_fill(m)
4747f5fe93fSJohn Dyson 	vm_page_t m;
4757f5fe93fSJohn Dyson {
4767f5fe93fSJohn Dyson 	pmap_zero_page(VM_PAGE_TO_PHYS(m));
4777f5fe93fSJohn Dyson 	return (TRUE);
4787f5fe93fSJohn Dyson }
4797f5fe93fSJohn Dyson 
4807f5fe93fSJohn Dyson /*
4817f5fe93fSJohn Dyson  *	vm_page_copy:
4827f5fe93fSJohn Dyson  *
4837f5fe93fSJohn Dyson  *	Copy one page to another
4847f5fe93fSJohn Dyson  */
4857f5fe93fSJohn Dyson static __inline void
4867f5fe93fSJohn Dyson vm_page_copy(src_m, dest_m)
4877f5fe93fSJohn Dyson 	vm_page_t src_m;
4887f5fe93fSJohn Dyson 	vm_page_t dest_m;
4897f5fe93fSJohn Dyson {
4907f5fe93fSJohn Dyson 	pmap_copy_page(VM_PAGE_TO_PHYS(src_m), VM_PAGE_TO_PHYS(dest_m));
4917f5fe93fSJohn Dyson 	dest_m->valid = VM_PAGE_BITS_ALL;
4927f5fe93fSJohn Dyson }
493f919ebdeSDavid Greenman 
4941c7c3c6aSMatthew Dillon /*
4951c7c3c6aSMatthew Dillon  *	vm_page_free:
4961c7c3c6aSMatthew Dillon  *
4971c7c3c6aSMatthew Dillon  *	Free a page
498faa273d5SMatthew Dillon  *
499faa273d5SMatthew Dillon  *	The clearing of PG_ZERO is a temporary safety until the code can be
500faa273d5SMatthew Dillon  *	reviewed to determine that PG_ZERO is being properly cleared on
501faa273d5SMatthew Dillon  *	write faults or maps.  PG_ZERO was previously cleared in
502faa273d5SMatthew Dillon  *	vm_page_alloc().
5031c7c3c6aSMatthew Dillon  */
5041c7c3c6aSMatthew Dillon static __inline void
5051c7c3c6aSMatthew Dillon vm_page_free(m)
5061c7c3c6aSMatthew Dillon 	vm_page_t m;
5071c7c3c6aSMatthew Dillon {
508faa273d5SMatthew Dillon 	vm_page_flag_clear(m, PG_ZERO);
509faa273d5SMatthew Dillon 	vm_page_free_toq(m);
5101c7c3c6aSMatthew Dillon }
5111c7c3c6aSMatthew Dillon 
5121c7c3c6aSMatthew Dillon /*
5131c7c3c6aSMatthew Dillon  *	vm_page_free_zero:
5141c7c3c6aSMatthew Dillon  *
5151c7c3c6aSMatthew Dillon  *	Free a page to the zerod-pages queue
5161c7c3c6aSMatthew Dillon  */
5171c7c3c6aSMatthew Dillon static __inline void
5181c7c3c6aSMatthew Dillon vm_page_free_zero(m)
5191c7c3c6aSMatthew Dillon 	vm_page_t m;
5201c7c3c6aSMatthew Dillon {
521faa273d5SMatthew Dillon 	vm_page_flag_set(m, PG_ZERO);
522faa273d5SMatthew Dillon 	vm_page_free_toq(m);
5231c7c3c6aSMatthew Dillon }
5241c7c3c6aSMatthew Dillon 
5251c7c3c6aSMatthew Dillon /*
5261c7c3c6aSMatthew Dillon  *	vm_page_sleep_busy:
5271c7c3c6aSMatthew Dillon  *
5281c7c3c6aSMatthew Dillon  *	Wait until page is no longer PG_BUSY or (if also_m_busy is TRUE)
5291c7c3c6aSMatthew Dillon  *	m->busy is zero.  Returns TRUE if it had to sleep ( including if
5301c7c3c6aSMatthew Dillon  *	it almost had to sleep and made temporary spl*() mods), FALSE
5311c7c3c6aSMatthew Dillon  *	otherwise.
5321c7c3c6aSMatthew Dillon  *
5331c7c3c6aSMatthew Dillon  *	This routine assumes that interrupts can only remove the busy
5341c7c3c6aSMatthew Dillon  *	status from a page, not set the busy status or change it from
5351c7c3c6aSMatthew Dillon  *	PG_BUSY to m->busy or vise versa (which would create a timing
5361c7c3c6aSMatthew Dillon  *	window).
5371c7c3c6aSMatthew Dillon  *
5381c7c3c6aSMatthew Dillon  *	Note that being an inline, this code will be well optimized.
5391c7c3c6aSMatthew Dillon  */
5401c7c3c6aSMatthew Dillon 
5411c7c3c6aSMatthew Dillon static __inline int
5421c7c3c6aSMatthew Dillon vm_page_sleep_busy(vm_page_t m, int also_m_busy, const char *msg)
5431c7c3c6aSMatthew Dillon {
5441c7c3c6aSMatthew Dillon 	if ((m->flags & PG_BUSY) || (also_m_busy && m->busy))  {
5451c7c3c6aSMatthew Dillon 		int s = splvm();
5461c7c3c6aSMatthew Dillon 		if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) {
5471c7c3c6aSMatthew Dillon 			/*
5481c7c3c6aSMatthew Dillon 			 * Page is busy. Wait and retry.
5491c7c3c6aSMatthew Dillon 			 */
5501c7c3c6aSMatthew Dillon 			vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
5511c7c3c6aSMatthew Dillon 			tsleep(m, PVM, msg, 0);
5521c7c3c6aSMatthew Dillon 		}
5531c7c3c6aSMatthew Dillon 		splx(s);
5541c7c3c6aSMatthew Dillon 		return(TRUE);
5551c7c3c6aSMatthew Dillon 		/* not reached */
5561c7c3c6aSMatthew Dillon 	}
5571c7c3c6aSMatthew Dillon 	return(FALSE);
5581c7c3c6aSMatthew Dillon }
5591c7c3c6aSMatthew Dillon 
560811c2e1aSJulian Elischer #if !defined(KLD_MODULE)
561811c2e1aSJulian Elischer 
562161a8f65SMatthew Dillon /*
563161a8f65SMatthew Dillon  *	vm_page_dirty:
564161a8f65SMatthew Dillon  *
565161a8f65SMatthew Dillon  *	make page all dirty
566161a8f65SMatthew Dillon  */
567161a8f65SMatthew Dillon 
568161a8f65SMatthew Dillon static __inline void
569161a8f65SMatthew Dillon vm_page_dirty(vm_page_t m)
570161a8f65SMatthew Dillon {
571161a8f65SMatthew Dillon 	KASSERT(m->queue - m->pc != PQ_CACHE, ("vm_page_dirty: page in cache!"));
572161a8f65SMatthew Dillon 	m->dirty = VM_PAGE_BITS_ALL;
573161a8f65SMatthew Dillon }
574161a8f65SMatthew Dillon 
5752c28a105SAlan Cox /*
5762c28a105SAlan Cox  *	vm_page_undirty:
5772c28a105SAlan Cox  *
5782c28a105SAlan Cox  *	Set page to not be dirty.  Note: does not clear pmap modify bits
5792c28a105SAlan Cox  */
5802c28a105SAlan Cox 
5812c28a105SAlan Cox static __inline void
5822c28a105SAlan Cox vm_page_undirty(vm_page_t m)
5832c28a105SAlan Cox {
5842c28a105SAlan Cox 	m->dirty = 0;
5852c28a105SAlan Cox }
5862c28a105SAlan Cox 
587a0e7b3e5SMatthew Dillon static __inline vm_page_t
588faa273d5SMatthew Dillon vm_page_list_find(int basequeue, int index, boolean_t prefer_zero)
589a0e7b3e5SMatthew Dillon {
590a0e7b3e5SMatthew Dillon 	vm_page_t m;
591a0e7b3e5SMatthew Dillon 
592a0e7b3e5SMatthew Dillon #if PQ_L2_SIZE > 1
593faa273d5SMatthew Dillon 	if (prefer_zero) {
594faa273d5SMatthew Dillon 		m = TAILQ_LAST(vm_page_queues[basequeue+index].pl, pglist);
595faa273d5SMatthew Dillon 	} else {
596a0e7b3e5SMatthew Dillon 		m = TAILQ_FIRST(vm_page_queues[basequeue+index].pl);
597faa273d5SMatthew Dillon 	}
598a0e7b3e5SMatthew Dillon 	if (m == NULL)
599a0e7b3e5SMatthew Dillon 		m = _vm_page_list_find(basequeue, index);
600a0e7b3e5SMatthew Dillon #else
601faa273d5SMatthew Dillon 	if (prefer_zero) {
602faa273d5SMatthew Dillon 		m = TAILQ_LAST(vm_page_queues[basequeue].pl, pglist);
603faa273d5SMatthew Dillon 	} else {
604a0e7b3e5SMatthew Dillon 		m = TAILQ_FIRST(vm_page_queues[basequeue].pl);
605faa273d5SMatthew Dillon 	}
606a0e7b3e5SMatthew Dillon #endif
607a0e7b3e5SMatthew Dillon 	return(m);
608a0e7b3e5SMatthew Dillon }
609161a8f65SMatthew Dillon 
6100237469fSJulian Elischer #endif
6110237469fSJulian Elischer 
612df8bae1dSRodney W. Grimes #endif				/* KERNEL */
613df8bae1dSRodney W. Grimes #endif				/* !_VM_PAGE_ */
614