xref: /freebsd/sys/vm/vm_page.h (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	from: @(#)vm_page.h	8.2 (Berkeley) 12/13/93
33  *
34  *
35  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36  * All rights reserved.
37  *
38  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39  *
40  * Permission to use, copy, modify and distribute this software and
41  * its documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie the
58  * rights to redistribute these changes.
59  *
60  * $FreeBSD$
61  */
62 
63 /*
64  *	Resident memory system definitions.
65  */
66 
67 #ifndef	_VM_PAGE_
68 #define	_VM_PAGE_
69 
70 #if !defined(KLD_MODULE)
71 #include "opt_vmpage.h"
72 #endif
73 
74 #include <vm/pmap.h>
75 
76 /*
77  *	Management of resident (logical) pages.
78  *
79  *	A small structure is kept for each resident
80  *	page, indexed by page number.  Each structure
81  *	is an element of several lists:
82  *
83  *		A hash table bucket used to quickly
84  *		perform object/offset lookups
85  *
86  *		A list of all pages for a given object,
87  *		so they can be quickly deactivated at
88  *		time of deallocation.
89  *
90  *		An ordered list of pages due for pageout.
91  *
92  *	In addition, the structure contains the object
93  *	and offset to which this page belongs (for pageout),
94  *	and sundry status bits.
95  *
96  *	Fields in this structure are locked either by the lock on the
97  *	object that the page belongs to (O) or by the lock on the page
98  *	queues (P).
99  *
100  *	The 'valid' and 'dirty' fields are distinct.  A page may have dirty
101  *	bits set without having associated valid bits set.  This is used by
102  *	NFS to implement piecemeal writes.
103  */
104 
105 TAILQ_HEAD(pglist, vm_page);
106 
107 struct vm_page {
108 	TAILQ_ENTRY(vm_page) pageq;	/* queue info for FIFO queue or free list (P) */
109 	TAILQ_ENTRY(vm_page) listq;	/* pages in same object (O) 	*/
110 	struct vm_page *left;		/* splay tree link (O)		*/
111 	struct vm_page *right;		/* splay tree link (O)		*/
112 
113 	vm_object_t object;		/* which object am I in (O,P)*/
114 	vm_pindex_t pindex;		/* offset into object (O,P) */
115 	vm_paddr_t phys_addr;		/* physical address of page */
116 	struct md_page md;		/* machine dependant stuff */
117 	u_short	queue;			/* page queue index */
118 	u_short	flags,			/* see below */
119 		pc;			/* page color */
120 	u_short wire_count;		/* wired down maps refs (P) */
121 	u_int cow;			/* page cow mapping count */
122 	short hold_count;		/* page hold count */
123 	u_char	act_count;		/* page usage count */
124 	u_char	busy;			/* page busy count */
125 	/* NOTE that these must support one bit per DEV_BSIZE in a page!!! */
126 	/* so, on normal X86 kernels, they must be at least 8 bits wide */
127 #if PAGE_SIZE == 4096
128 	u_char	valid;			/* map of valid DEV_BSIZE chunks (O) */
129 	u_char	dirty;			/* map of dirty DEV_BSIZE chunks */
130 #elif PAGE_SIZE == 8192
131 	u_short	valid;			/* map of valid DEV_BSIZE chunks (O) */
132 	u_short	dirty;			/* map of dirty DEV_BSIZE chunks */
133 #elif PAGE_SIZE == 16384
134 	u_int valid;			/* map of valid DEV_BSIZE chunks (O) */
135 	u_int dirty;			/* map of dirty DEV_BSIZE chunks */
136 #elif PAGE_SIZE == 32768
137 	u_long valid;			/* map of valid DEV_BSIZE chunks (O) */
138 	u_long dirty;			/* map of dirty DEV_BSIZE chunks */
139 #endif
140 };
141 
142 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
143 #if PAGE_SIZE == 32768
144 #ifdef CTASSERT
145 CTASSERT(sizeof(u_long) >= 8);
146 #endif
147 #endif
148 
149 #if !defined(KLD_MODULE)
150 /*
151  * Page coloring parameters
152  */
153 
154 /* Backward compatibility for existing PQ_*CACHE config options. */
155 #if !defined(PQ_CACHESIZE)
156 #if defined(PQ_HUGECACHE)
157 #define PQ_CACHESIZE 1024
158 #elif defined(PQ_LARGECACHE)
159 #define PQ_CACHESIZE 512
160 #elif defined(PQ_MEDIUMCACHE)
161 #define PQ_CACHESIZE 256
162 #elif defined(PQ_NORMALCACHE)
163 #define PQ_CACHESIZE 64
164 #elif defined(PQ_NOOPT)
165 #define PQ_CACHESIZE 0
166 #else
167 #define PQ_CACHESIZE 128
168 #endif
169 #endif			/* !defined(PQ_CACHESIZE) */
170 
171 #if PQ_CACHESIZE >= 1024
172 #define PQ_PRIME1 31	/* Prime number somewhat less than PQ_L2_SIZE */
173 #define PQ_PRIME2 23	/* Prime number somewhat less than PQ_L2_SIZE */
174 #define PQ_L2_SIZE 256	/* A number of colors opt for 1M cache */
175 
176 #elif PQ_CACHESIZE >= 512
177 #define PQ_PRIME1 31	/* Prime number somewhat less than PQ_L2_SIZE */
178 #define PQ_PRIME2 23	/* Prime number somewhat less than PQ_L2_SIZE */
179 #define PQ_L2_SIZE 128	/* A number of colors opt for 512K cache */
180 
181 #elif PQ_CACHESIZE >= 256
182 #define PQ_PRIME1 13	/* Prime number somewhat less than PQ_L2_SIZE */
183 #define PQ_PRIME2 7	/* Prime number somewhat less than PQ_L2_SIZE */
184 #define PQ_L2_SIZE 64	/* A number of colors opt for 256K cache */
185 
186 #elif PQ_CACHESIZE >= 128
187 #define PQ_PRIME1 9	/* Produces a good PQ_L2_SIZE/3 + PQ_PRIME1 */
188 #define PQ_PRIME2 5	/* Prime number somewhat less than PQ_L2_SIZE */
189 #define PQ_L2_SIZE 32	/* A number of colors opt for 128k cache */
190 
191 #elif PQ_CACHESIZE >= 64
192 #define PQ_PRIME1 5	/* Prime number somewhat less than PQ_L2_SIZE */
193 #define PQ_PRIME2 3	/* Prime number somewhat less than PQ_L2_SIZE */
194 #define PQ_L2_SIZE 16	/* A reasonable number of colors (opt for 64K cache) */
195 
196 #else
197 #define PQ_PRIME1 1	/* Disable page coloring. */
198 #define PQ_PRIME2 1
199 #define PQ_L2_SIZE 1
200 
201 #endif
202 
203 #define PQ_L2_MASK (PQ_L2_SIZE - 1)
204 
205 /* PQ_CACHE and PQ_FREE represent PQ_L2_SIZE consecutive queues. */
206 #define PQ_NONE 0
207 #define PQ_FREE	1
208 #define PQ_INACTIVE (1 + 1*PQ_L2_SIZE)
209 #define PQ_ACTIVE (2 + 1*PQ_L2_SIZE)
210 #define PQ_CACHE (3 + 1*PQ_L2_SIZE)
211 #define PQ_HOLD  (3 + 2*PQ_L2_SIZE)
212 #define PQ_COUNT (4 + 2*PQ_L2_SIZE)
213 
214 struct vpgqueues {
215 	struct pglist pl;
216 	int	*cnt;
217 	int	lcnt;
218 };
219 
220 extern struct vpgqueues vm_page_queues[PQ_COUNT];
221 extern struct mtx vm_page_queue_free_mtx;
222 
223 #endif			/* !defined(KLD_MODULE) */
224 
225 /*
226  * These are the flags defined for vm_page.
227  *
228  * Note: PG_UNMANAGED (used by OBJT_PHYS) indicates that the page is
229  * 	 not under PV management but otherwise should be treated as a
230  *	 normal page.  Pages not under PV management cannot be paged out
231  *	 via the object/vm_page_t because there is no knowledge of their
232  *	 pte mappings, nor can they be removed from their objects via
233  *	 the object, and such pages are also not on any PQ queue.
234  */
235 #define	PG_BUSY		0x0001		/* page is in transit (O) */
236 #define	PG_WANTED	0x0002		/* someone is waiting for page (O) */
237 #define PG_WINATCFLS	0x0004		/* flush dirty page on inactive q */
238 #define	PG_FICTITIOUS	0x0008		/* physical page doesn't exist (O) */
239 #define	PG_WRITEABLE	0x0010		/* page is mapped writeable */
240 #define	PG_ZERO		0x0040		/* page is zeroed */
241 #define PG_REFERENCED	0x0080		/* page has been referenced */
242 #define PG_CLEANCHK	0x0100		/* page will be checked for cleaning */
243 #define PG_SWAPINPROG	0x0200		/* swap I/O in progress on page	     */
244 #define PG_NOSYNC	0x0400		/* do not collect for syncer */
245 #define PG_UNMANAGED	0x0800		/* No PV management for page */
246 #define PG_MARKER	0x1000		/* special queue marker page */
247 #define	PG_SLAB		0x2000		/* object pointer is actually a slab */
248 
249 /*
250  * Misc constants.
251  */
252 #define ACT_DECLINE		1
253 #define ACT_ADVANCE		3
254 #define ACT_INIT		5
255 #define ACT_MAX			64
256 
257 #ifdef _KERNEL
258 /*
259  * Each pageable resident page falls into one of four lists:
260  *
261  *	free
262  *		Available for allocation now.
263  *
264  * The following are all LRU sorted:
265  *
266  *	cache
267  *		Almost available for allocation. Still in an
268  *		object, but clean and immediately freeable at
269  *		non-interrupt times.
270  *
271  *	inactive
272  *		Low activity, candidates for reclamation.
273  *		This is the list of pages that should be
274  *		paged out next.
275  *
276  *	active
277  *		Pages that are "active" i.e. they have been
278  *		recently referenced.
279  *
280  *	zero
281  *		Pages that are really free and have been pre-zeroed
282  *
283  */
284 
285 extern int vm_page_zero_count;
286 
287 extern vm_page_t vm_page_array;		/* First resident page in table */
288 extern int vm_page_array_size;		/* number of vm_page_t's */
289 extern long first_page;			/* first physical page number */
290 
291 #define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
292 
293 #define PHYS_TO_VM_PAGE(pa) \
294 		(&vm_page_array[atop(pa) - first_page ])
295 
296 extern struct mtx vm_page_queue_mtx;
297 #define vm_page_lock_queues()   mtx_lock(&vm_page_queue_mtx)
298 #define vm_page_unlock_queues() mtx_unlock(&vm_page_queue_mtx)
299 
300 #if PAGE_SIZE == 4096
301 #define VM_PAGE_BITS_ALL 0xffu
302 #elif PAGE_SIZE == 8192
303 #define VM_PAGE_BITS_ALL 0xffffu
304 #elif PAGE_SIZE == 16384
305 #define VM_PAGE_BITS_ALL 0xffffffffu
306 #elif PAGE_SIZE == 32768
307 #define VM_PAGE_BITS_ALL 0xfffffffffffffffflu
308 #endif
309 
310 /* page allocation classes: */
311 #define VM_ALLOC_NORMAL		0
312 #define VM_ALLOC_INTERRUPT	1
313 #define VM_ALLOC_SYSTEM		2
314 #define	VM_ALLOC_CLASS_MASK	3
315 /* page allocation flags: */
316 #define	VM_ALLOC_WIRED		0x0020	/* non pageable */
317 #define	VM_ALLOC_ZERO		0x0040	/* Try to obtain a zeroed page */
318 #define	VM_ALLOC_RETRY		0x0080	/* vm_page_grab() only */
319 #define	VM_ALLOC_NOOBJ		0x0100	/* No associated object */
320 
321 void vm_page_flag_set(vm_page_t m, unsigned short bits);
322 void vm_page_flag_clear(vm_page_t m, unsigned short bits);
323 void vm_page_busy(vm_page_t m);
324 void vm_page_flash(vm_page_t m);
325 void vm_page_io_start(vm_page_t m);
326 void vm_page_io_finish(vm_page_t m);
327 void vm_page_hold(vm_page_t mem);
328 void vm_page_unhold(vm_page_t mem);
329 void vm_page_free(vm_page_t m);
330 void vm_page_free_zero(vm_page_t m);
331 int vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg);
332 void vm_page_dirty(vm_page_t m);
333 void vm_page_wakeup(vm_page_t m);
334 
335 void vm_pageq_init(void);
336 vm_page_t vm_pageq_add_new_page(vm_paddr_t pa);
337 void vm_pageq_enqueue(int queue, vm_page_t m);
338 void vm_pageq_remove_nowakeup(vm_page_t m);
339 void vm_pageq_remove(vm_page_t m);
340 vm_page_t vm_pageq_find(int basequeue, int index, boolean_t prefer_zero);
341 void vm_pageq_requeue(vm_page_t m);
342 
343 void vm_page_activate (vm_page_t);
344 vm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int);
345 vm_page_t vm_page_alloc_contig (vm_pindex_t, vm_paddr_t, vm_paddr_t,
346 	    vm_offset_t, vm_offset_t);
347 void vm_page_release_contig (vm_page_t, vm_pindex_t);
348 vm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int);
349 void vm_page_cache (register vm_page_t);
350 int vm_page_try_to_cache (vm_page_t);
351 int vm_page_try_to_free (vm_page_t);
352 void vm_page_dontneed (register vm_page_t);
353 void vm_page_deactivate (vm_page_t);
354 void vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t);
355 vm_page_t vm_page_lookup (vm_object_t, vm_pindex_t);
356 void vm_page_remove (vm_page_t);
357 void vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t);
358 vm_page_t vm_page_select_cache(int);
359 vm_page_t vm_page_splay(vm_pindex_t, vm_page_t);
360 vm_offset_t vm_page_startup(vm_offset_t vaddr);
361 void vm_page_unmanage (vm_page_t);
362 void vm_page_unwire (vm_page_t, int);
363 void vm_page_wire (vm_page_t);
364 void vm_page_set_validclean (vm_page_t, int, int);
365 void vm_page_clear_dirty (vm_page_t, int, int);
366 void vm_page_set_invalid (vm_page_t, int, int);
367 int vm_page_is_valid (vm_page_t, int, int);
368 void vm_page_test_dirty (vm_page_t);
369 int vm_page_bits (int, int);
370 void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid);
371 void vm_page_free_toq(vm_page_t m);
372 void vm_page_zero_idle_wakeup(void);
373 void vm_page_cowfault (vm_page_t);
374 void vm_page_cowsetup (vm_page_t);
375 void vm_page_cowclear (vm_page_t);
376 
377 /*
378  *	vm_page_undirty:
379  *
380  *	Set page to not be dirty.  Note: does not clear pmap modify bits
381  */
382 static __inline void
383 vm_page_undirty(vm_page_t m)
384 {
385 	m->dirty = 0;
386 }
387 
388 #endif				/* _KERNEL */
389 #endif				/* !_VM_PAGE_ */
390