xref: /freebsd/sys/vm/vm_page.h (revision 26f9a76710a312a951848542b9ca1f44100450e2)
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  *
36df8bae1dSRodney W. Grimes  *	@(#)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.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  *	Resident memory system definitions.
67df8bae1dSRodney W. Grimes  */
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes #ifndef	_VM_PAGE_
70df8bae1dSRodney W. Grimes #define	_VM_PAGE_
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  *	Management of resident (logical) pages.
74df8bae1dSRodney W. Grimes  *
75df8bae1dSRodney W. Grimes  *	A small structure is kept for each resident
76df8bae1dSRodney W. Grimes  *	page, indexed by page number.  Each structure
77df8bae1dSRodney W. Grimes  *	is an element of several lists:
78df8bae1dSRodney W. Grimes  *
79df8bae1dSRodney W. Grimes  *		A hash table bucket used to quickly
80df8bae1dSRodney W. Grimes  *		perform object/offset lookups
81df8bae1dSRodney W. Grimes  *
82df8bae1dSRodney W. Grimes  *		A list of all pages for a given object,
83df8bae1dSRodney W. Grimes  *		so they can be quickly deactivated at
84df8bae1dSRodney W. Grimes  *		time of deallocation.
85df8bae1dSRodney W. Grimes  *
86df8bae1dSRodney W. Grimes  *		An ordered list of pages due for pageout.
87df8bae1dSRodney W. Grimes  *
88df8bae1dSRodney W. Grimes  *	In addition, the structure contains the object
89df8bae1dSRodney W. Grimes  *	and offset to which this page belongs (for pageout),
90df8bae1dSRodney W. Grimes  *	and sundry status bits.
91df8bae1dSRodney W. Grimes  *
92df8bae1dSRodney W. Grimes  *	Fields in this structure are locked either by the lock on the
93df8bae1dSRodney W. Grimes  *	object that the page belongs to (O) or by the lock on the page
94df8bae1dSRodney W. Grimes  *	queues (P).
95df8bae1dSRodney W. Grimes  */
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes TAILQ_HEAD(pglist, vm_page);
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes struct vm_page {
100df8bae1dSRodney W. Grimes 	TAILQ_ENTRY(vm_page)	pageq;		/* queue info for FIFO
101df8bae1dSRodney W. Grimes 						 * queue or free list (P) */
102df8bae1dSRodney W. Grimes 	TAILQ_ENTRY(vm_page)	hashq;		/* hash table links (O)*/
103df8bae1dSRodney W. Grimes 	TAILQ_ENTRY(vm_page)	listq;		/* pages in same object (O)*/
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	vm_object_t		object;		/* which object am I in (O,P)*/
106df8bae1dSRodney W. Grimes 	vm_offset_t		offset;		/* offset into object (O,P) */
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes 	u_short			wire_count;	/* wired down maps refs (P) */
109df8bae1dSRodney W. Grimes 	u_short			flags;		/* see below */
11026f9a767SRodney W. Grimes 	short			hold_count;	/* page hold count */
11126f9a767SRodney W. Grimes 	u_short			act_count;	/* page usage count */
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	vm_offset_t		phys_addr;	/* physical address of page */
114df8bae1dSRodney W. Grimes };
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes /*
117df8bae1dSRodney W. Grimes  * These are the flags defined for vm_page.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  * Note: PG_FILLED and PG_DIRTY are added for the filesystems.
120df8bae1dSRodney W. Grimes  */
121df8bae1dSRodney W. Grimes #define	PG_INACTIVE	0x0001		/* page is in inactive list (P) */
122df8bae1dSRodney W. Grimes #define	PG_ACTIVE	0x0002		/* page is in active list (P) */
123df8bae1dSRodney W. Grimes #define	PG_LAUNDRY	0x0004		/* page is being cleaned now (P)*/
124df8bae1dSRodney W. Grimes #define	PG_CLEAN	0x0008		/* page has not been modified */
125df8bae1dSRodney W. Grimes #define	PG_BUSY		0x0010		/* page is in transit (O) */
126df8bae1dSRodney W. Grimes #define	PG_WANTED	0x0020		/* someone is waiting for page (O) */
127df8bae1dSRodney W. Grimes #define	PG_TABLED	0x0040		/* page is in VP table (O) */
128df8bae1dSRodney W. Grimes #define	PG_COPYONWRITE	0x0080		/* must copy page before changing (O) */
129df8bae1dSRodney W. Grimes #define	PG_FICTITIOUS	0x0100		/* physical page doesn't exist (O) */
130df8bae1dSRodney W. Grimes #define	PG_FAKE		0x0200		/* page is placeholder for pagein (O) */
131df8bae1dSRodney W. Grimes #define	PG_FILLED	0x0400		/* client flag to set when filled */
132df8bae1dSRodney W. Grimes #define	PG_DIRTY	0x0800		/* client flag to set when dirty */
133df8bae1dSRodney W. Grimes #define	PG_PAGEROWNED	0x4000		/* DEBUG: async paging op in progress */
134df8bae1dSRodney W. Grimes #define	PG_PTPAGE	0x8000		/* DEBUG: is a user page table page */
135df8bae1dSRodney W. Grimes 
136df8bae1dSRodney W. Grimes #if	VM_PAGE_DEBUG
137df8bae1dSRodney W. Grimes #define	VM_PAGE_CHECK(mem) { \
138df8bae1dSRodney W. Grimes 	if ((((unsigned int) mem) < ((unsigned int) &vm_page_array[0])) || \
139df8bae1dSRodney W. Grimes 	    (((unsigned int) mem) > \
140df8bae1dSRodney W. Grimes 		((unsigned int) &vm_page_array[last_page-first_page])) || \
141df8bae1dSRodney W. Grimes 	    ((mem->flags & (PG_ACTIVE | PG_INACTIVE)) == \
142df8bae1dSRodney W. Grimes 		(PG_ACTIVE | PG_INACTIVE))) \
143df8bae1dSRodney W. Grimes 		panic("vm_page_check: not valid!"); \
144df8bae1dSRodney W. Grimes }
145df8bae1dSRodney W. Grimes #else /* VM_PAGE_DEBUG */
146df8bae1dSRodney W. Grimes #define	VM_PAGE_CHECK(mem)
147df8bae1dSRodney W. Grimes #endif /* VM_PAGE_DEBUG */
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes #ifdef KERNEL
150df8bae1dSRodney W. Grimes /*
151df8bae1dSRodney W. Grimes  *	Each pageable resident page falls into one of three lists:
152df8bae1dSRodney W. Grimes  *
153df8bae1dSRodney W. Grimes  *	free
154df8bae1dSRodney W. Grimes  *		Available for allocation now.
155df8bae1dSRodney W. Grimes  *	inactive
156df8bae1dSRodney W. Grimes  *		Not referenced in any map, but still has an
157df8bae1dSRodney W. Grimes  *		object/offset-page mapping, and may be dirty.
158df8bae1dSRodney W. Grimes  *		This is the list of pages that should be
159df8bae1dSRodney W. Grimes  *		paged out next.
160df8bae1dSRodney W. Grimes  *	active
161df8bae1dSRodney W. Grimes  *		A list of pages which have been placed in
162df8bae1dSRodney W. Grimes  *		at least one physical map.  This list is
163df8bae1dSRodney W. Grimes  *		ordered, in LRU-like fashion.
164df8bae1dSRodney W. Grimes  */
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes extern
167df8bae1dSRodney W. Grimes struct pglist	vm_page_queue_free;	/* memory free queue */
168df8bae1dSRodney W. Grimes extern
169df8bae1dSRodney W. Grimes struct pglist	vm_page_queue_active;	/* active memory queue */
170df8bae1dSRodney W. Grimes extern
171df8bae1dSRodney W. Grimes struct pglist	vm_page_queue_inactive;	/* inactive memory queue */
172df8bae1dSRodney W. Grimes 
173df8bae1dSRodney W. Grimes extern
174df8bae1dSRodney W. Grimes vm_page_t	vm_page_array;		/* First resident page in table */
175df8bae1dSRodney W. Grimes extern
176df8bae1dSRodney W. Grimes long		first_page;		/* first physical page number */
177df8bae1dSRodney W. Grimes 					/* ... represented in vm_page_array */
178df8bae1dSRodney W. Grimes extern
179df8bae1dSRodney W. Grimes long		last_page;		/* last physical page number */
180df8bae1dSRodney W. Grimes 					/* ... represented in vm_page_array */
181df8bae1dSRodney W. Grimes 					/* [INCLUSIVE] */
182df8bae1dSRodney W. Grimes extern
183df8bae1dSRodney W. Grimes vm_offset_t	first_phys_addr;	/* physical address for first_page */
184df8bae1dSRodney W. Grimes extern
185df8bae1dSRodney W. Grimes vm_offset_t	last_phys_addr;		/* physical address for last_page */
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes #define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes #define IS_VM_PHYSADDR(pa) \
190df8bae1dSRodney W. Grimes 		((pa) >= first_phys_addr && (pa) <= last_phys_addr)
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes #define PHYS_TO_VM_PAGE(pa) \
193df8bae1dSRodney W. Grimes 		(&vm_page_array[atop(pa) - first_page ])
194df8bae1dSRodney W. Grimes 
195df8bae1dSRodney W. Grimes extern
196df8bae1dSRodney W. Grimes simple_lock_data_t	vm_page_queue_lock;	/* lock on active and inactive
197df8bae1dSRodney W. Grimes 						   page queues */
198df8bae1dSRodney W. Grimes extern						/* lock on free page queue */
199df8bae1dSRodney W. Grimes simple_lock_data_t	vm_page_queue_free_lock;
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes /*
202df8bae1dSRodney W. Grimes  *	Functions implemented as macros
203df8bae1dSRodney W. Grimes  */
204df8bae1dSRodney W. Grimes 
205df8bae1dSRodney W. Grimes #define PAGE_ASSERT_WAIT(m, interruptible)	{ \
206df8bae1dSRodney W. Grimes 				(m)->flags |= PG_WANTED; \
207df8bae1dSRodney W. Grimes 				assert_wait((int) (m), (interruptible)); \
208df8bae1dSRodney W. Grimes 			}
209df8bae1dSRodney W. Grimes 
210df8bae1dSRodney W. Grimes #define PAGE_WAKEUP(m)	{ \
211df8bae1dSRodney W. Grimes 				(m)->flags &= ~PG_BUSY; \
212df8bae1dSRodney W. Grimes 				if ((m)->flags & PG_WANTED) { \
213df8bae1dSRodney W. Grimes 					(m)->flags &= ~PG_WANTED; \
21426f9a767SRodney W. Grimes 					wakeup((caddr_t) (m)); \
215df8bae1dSRodney W. Grimes 				} \
216df8bae1dSRodney W. Grimes 			}
217df8bae1dSRodney W. Grimes 
218df8bae1dSRodney W. Grimes #define	vm_page_lock_queues()	simple_lock(&vm_page_queue_lock)
219df8bae1dSRodney W. Grimes #define	vm_page_unlock_queues()	simple_unlock(&vm_page_queue_lock)
220df8bae1dSRodney W. Grimes 
221df8bae1dSRodney W. Grimes #define vm_page_set_modified(m)	{ (m)->flags &= ~PG_CLEAN; }
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes #define	VM_PAGE_INIT(mem, object, offset) { \
224df8bae1dSRodney W. Grimes 	(mem)->flags = PG_BUSY | PG_CLEAN | PG_FAKE; \
225df8bae1dSRodney W. Grimes 	vm_page_insert((mem), (object), (offset)); \
226df8bae1dSRodney W. Grimes 	(mem)->wire_count = 0; \
22726f9a767SRodney W. Grimes 	(mem)->hold_count = 0; \
22826f9a767SRodney W. Grimes 	(mem)->act_count = 0; \
229df8bae1dSRodney W. Grimes }
230df8bae1dSRodney W. Grimes 
231df8bae1dSRodney W. Grimes void		 vm_page_activate __P((vm_page_t));
232df8bae1dSRodney W. Grimes vm_page_t	 vm_page_alloc __P((vm_object_t, vm_offset_t));
233df8bae1dSRodney W. Grimes void		 vm_page_copy __P((vm_page_t, vm_page_t));
234df8bae1dSRodney W. Grimes void		 vm_page_deactivate __P((vm_page_t));
235df8bae1dSRodney W. Grimes void		 vm_page_free __P((vm_page_t));
236df8bae1dSRodney W. Grimes void		 vm_page_insert __P((vm_page_t, vm_object_t, vm_offset_t));
237df8bae1dSRodney W. Grimes vm_page_t	 vm_page_lookup __P((vm_object_t, vm_offset_t));
238df8bae1dSRodney W. Grimes void		 vm_page_remove __P((vm_page_t));
239df8bae1dSRodney W. Grimes void		 vm_page_rename __P((vm_page_t, vm_object_t, vm_offset_t));
24026f9a767SRodney W. Grimes vm_offset_t	 vm_page_startup __P((vm_offset_t, vm_offset_t, vm_offset_t));
241df8bae1dSRodney W. Grimes void		 vm_page_unwire __P((vm_page_t));
242df8bae1dSRodney W. Grimes void		 vm_page_wire __P((vm_page_t));
243df8bae1dSRodney W. Grimes boolean_t	 vm_page_zero_fill __P((vm_page_t));
244df8bae1dSRodney W. Grimes 
24526f9a767SRodney W. Grimes 
24626f9a767SRodney W. Grimes /*
24726f9a767SRodney W. Grimes  * Keep page from being freed by the page daemon
24826f9a767SRodney W. Grimes  * much of the same effect as wiring, except much lower
24926f9a767SRodney W. Grimes  * overhead and should be used only for *very* temporary
25026f9a767SRodney W. Grimes  * holding ("wiring").
25126f9a767SRodney W. Grimes  */
25226f9a767SRodney W. Grimes static inline void
25326f9a767SRodney W. Grimes vm_page_hold(mem)
25426f9a767SRodney W. Grimes 	vm_page_t mem;
25526f9a767SRodney W. Grimes {
25626f9a767SRodney W. Grimes 	mem->hold_count++;
25726f9a767SRodney W. Grimes }
25826f9a767SRodney W. Grimes 
25926f9a767SRodney W. Grimes static inline void
26026f9a767SRodney W. Grimes vm_page_unhold(mem)
26126f9a767SRodney W. Grimes 	vm_page_t mem;
26226f9a767SRodney W. Grimes {
26326f9a767SRodney W. Grimes 	if( --mem->hold_count < 0)
26426f9a767SRodney W. Grimes 		panic("vm_page_unhold: hold count < 0!!!");
26526f9a767SRodney W. Grimes }
26626f9a767SRodney W. Grimes 
267df8bae1dSRodney W. Grimes #endif /* KERNEL */
268df8bae1dSRodney W. Grimes #endif /* !_VM_PAGE_ */
269