xref: /openbsd-src/sys/uvm/uvm_page.h (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: uvm_page.h,v 1.48 2011/05/30 22:25:24 oga Exp $	*/
2 /*	$NetBSD: uvm_page.h,v 1.19 2000/12/28 08:24:55 chs Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
6  * Copyright (c) 1991, 1993, The Regents of the University of California.
7  *
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * The Mach Operating System project at Carnegie-Mellon University.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by Charles D. Cranor,
24  *      Washington University, the University of California, Berkeley and
25  *      its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)vm_page.h   7.3 (Berkeley) 4/21/91
43  * from: Id: uvm_page.h,v 1.1.2.6 1998/02/04 02:31:42 chuck Exp
44  *
45  *
46  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47  * All rights reserved.
48  *
49  * Permission to use, copy, modify and distribute this software and
50  * its documentation is hereby granted, provided that both the copyright
51  * notice and this permission notice appear in all copies of the
52  * software, derivative works or modified versions, and any portions
53  * thereof, and that both notices appear in supporting documentation.
54  *
55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58  *
59  * Carnegie Mellon requests users of this software to return to
60  *
61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62  *  School of Computer Science
63  *  Carnegie Mellon University
64  *  Pittsburgh PA 15213-3890
65  *
66  * any improvements or extensions that they make and grant Carnegie the
67  * rights to redistribute these changes.
68  */
69 
70 #ifndef _UVM_UVM_PAGE_H_
71 #define _UVM_UVM_PAGE_H_
72 
73 /*
74  * uvm_page.h
75  */
76 
77 /*
78  *	Resident memory system definitions.
79  */
80 
81 /*
82  *	Management of resident (logical) pages.
83  *
84  *	A small structure is kept for each resident
85  *	page, indexed by page number.  Each structure
86  *	contains a list used for manipulating pages, and
87  *	a tree structure for in object/offset lookups
88  *
89  *	In addition, the structure contains the object
90  *	and offset to which this page belongs (for pageout),
91  *	and sundry status bits.
92  *
93  *	Fields in this structure are locked either by the lock on the
94  *	object that the page belongs to (O) or by the lock on the page
95  *	queues (P) [or both].
96  */
97 
98 #include <uvm/uvm_extern.h>
99 
100 TAILQ_HEAD(pglist, vm_page);
101 
102 struct vm_page {
103 	TAILQ_ENTRY(vm_page)	pageq;		/* queue info for FIFO
104 						 * queue or free list (P) */
105 	RB_ENTRY(vm_page)	objt;		/* object tree (O)*/
106 
107 	struct vm_anon		*uanon;		/* anon (O,P) */
108 	struct uvm_object	*uobject;	/* object (O,P) */
109 	voff_t			offset;		/* offset into object (O,P) */
110 
111 	u_int			pg_flags;	/* object flags [O or P] */
112 
113 	u_int			pg_version;	/* version count [O] */
114 	u_int			wire_count;	/* wired down map refs [P] */
115 
116 	u_int			loan_count;	/* number of active loans
117 						 * to read: [O or P]
118 						 * to modify: [O _and_ P] */
119 	paddr_t			phys_addr;	/* physical address of page */
120 	psize_t			fpgsz;		/* free page range size */
121 
122 #ifdef __HAVE_VM_PAGE_MD
123 	struct vm_page_md	mdpage;		/* pmap-specific data */
124 #endif
125 #if defined(UVM_PAGE_TRKOWN)
126 	/* debugging fields to track page ownership */
127 	pid_t			owner;		/* proc that set PG_BUSY */
128 	char			*owner_tag;	/* why it was set busy */
129 #endif
130 };
131 
132 /*
133  * These are the flags defined for vm_page.
134  *
135  * Note: PG_FILLED and PG_DIRTY are added for the filesystems.
136  */
137 
138 /*
139  * locking rules:
140  *   PG_ ==> locked by object lock
141  *   PQ_ ==> lock by page queue lock
142  *   PQ_FREE is locked by free queue lock and is mutex with all other PQs
143  *   pg_flags may only be changed using the atomic operations.
144  *
145  * PG_ZERO is used to indicate that a page has been pre-zero'd.  This flag
146  * is only set when the page is on no queues, and is cleared when the page
147  * is placed on the free list.
148  */
149 
150 #define	PG_BUSY		0x00000001	/* page is locked */
151 #define	PG_WANTED	0x00000002	/* someone is waiting for page */
152 #define	PG_TABLED	0x00000004	/* page is in VP table  */
153 #define	PG_CLEAN	0x00000008	/* page has not been modified */
154 #define PG_CLEANCHK	0x00000010	/* clean bit has been checked */
155 #define PG_RELEASED	0x00000020	/* page released while paging */
156 #define	PG_FAKE		0x00000040	/* page is not yet initialized */
157 #define PG_RDONLY	0x00000080	/* page must be mapped read-only */
158 #define PG_ZERO		0x00000100	/* page is pre-zero'd */
159 #define PG_DEV		0x00000200	/* page is in device space, lay off */
160 
161 #define PG_PAGER1	0x00001000	/* pager-specific flag */
162 #define PG_MASK		0x0000ffff
163 
164 #define PQ_FREE		0x00010000	/* page is on free list */
165 #define PQ_INACTIVE	0x00020000	/* page is in inactive list */
166 #define PQ_ACTIVE	0x00040000	/* page is in active list */
167 #define PQ_ANON		0x00100000	/* page is part of an anon, rather
168 					   than an uvm_object */
169 #define PQ_AOBJ		0x00200000	/* page is part of an anonymous
170 					   uvm_object */
171 #define PQ_SWAPBACKED	(PQ_ANON|PQ_AOBJ)
172 #define	PQ_ENCRYPT	0x00400000	/* page needs {en,de}cryption */
173 #define PQ_MASK		0x00ff0000
174 
175 #define PG_PMAP0	0x01000000	/* Used by some pmaps. */
176 #define PG_PMAP1	0x02000000	/* Used by some pmaps. */
177 #define PG_PMAP2	0x04000000	/* Used by some pmaps. */
178 #define PG_PMAP3	0x08000000	/* Used by some pmaps. */
179 
180 /*
181  * physical memory layout structure
182  *
183  * MD vmparam.h must #define:
184  *   VM_PHYSEG_MAX = max number of physical memory segments we support
185  *		   (if this is "1" then we revert to a "contig" case)
186  *   VM_PHYSSEG_STRAT: memory sort/search options (for VM_PHYSEG_MAX > 1)
187  * 	- VM_PSTRAT_RANDOM:   linear search (random order)
188  *	- VM_PSTRAT_BSEARCH:  binary search (sorted by address)
189  *	- VM_PSTRAT_BIGFIRST: linear search (sorted by largest segment first)
190  *      - others?
191  *   XXXCDC: eventually we should purge all left-over global variables...
192  */
193 #define VM_PSTRAT_RANDOM	1
194 #define VM_PSTRAT_BSEARCH	2
195 #define VM_PSTRAT_BIGFIRST	3
196 
197 /*
198  * vm_physmemseg: describes one segment of physical memory
199  */
200 struct vm_physseg {
201 	paddr_t	start;			/* PF# of first page in segment */
202 	paddr_t	end;			/* (PF# of last page in segment) + 1 */
203 	paddr_t	avail_start;		/* PF# of first free page in segment */
204 	paddr_t	avail_end;		/* (PF# of last free page in segment) +1  */
205 	struct	vm_page *pgs;		/* vm_page structures (from start) */
206 	struct	vm_page *lastpg;	/* vm_page structure for end */
207 #ifdef __HAVE_PMAP_PHYSSEG
208 	struct	pmap_physseg pmseg;	/* pmap specific (MD) data */
209 #endif
210 };
211 
212 #ifdef _KERNEL
213 
214 /*
215  * globals
216  */
217 
218 extern boolean_t vm_page_zero_enable;
219 
220 /*
221  * physical memory config is stored in vm_physmem.
222  */
223 
224 extern struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];
225 extern int vm_nphysseg;
226 
227 /*
228  * prototypes: the following prototypes define the interface to pages
229  */
230 
231 void		uvm_page_init(vaddr_t *, vaddr_t *);
232 #if defined(UVM_PAGE_TRKOWN)
233 void		uvm_page_own(struct vm_page *, char *);
234 #endif
235 #if !defined(PMAP_STEAL_MEMORY)
236 boolean_t	uvm_page_physget(paddr_t *);
237 #endif
238 void		uvm_pageidlezero(void);
239 
240 void		uvm_pageactivate(struct vm_page *);
241 vaddr_t		uvm_pageboot_alloc(vsize_t);
242 void		uvm_pagecopy(struct vm_page *, struct vm_page *);
243 void		uvm_pagedeactivate(struct vm_page *);
244 void		uvm_pagefree(struct vm_page *);
245 void		uvm_page_unbusy(struct vm_page **, int);
246 struct vm_page	*uvm_pagelookup(struct uvm_object *, voff_t);
247 void		uvm_pageunwire(struct vm_page *);
248 void		uvm_pagewait(struct vm_page *, int);
249 void		uvm_pagewake(struct vm_page *);
250 void		uvm_pagewire(struct vm_page *);
251 void		uvm_pagezero(struct vm_page *);
252 void		uvm_pagealloc_pg(struct vm_page *, struct uvm_object *,
253 		    voff_t, struct vm_anon *);
254 
255 struct uvm_constraint_range; /* XXX move to uvm_extern.h? */
256 psize_t		uvm_pagecount(struct uvm_constraint_range*);
257 
258 #if  VM_PHYSSEG_MAX == 1
259 /*
260  * Inline functions for archs like the vax where function calls are expensive.
261  */
262 /*
263  * vm_physseg_find: find vm_physseg structure that belongs to a PA
264  */
265 static __inline int
266 vm_physseg_find(paddr_t pframe, int *offp)
267 {
268 	/* 'contig' case */
269 	if (pframe >= vm_physmem[0].start && pframe < vm_physmem[0].end) {
270 		if (offp)
271 			*offp = pframe - vm_physmem[0].start;
272 		return(0);
273 	}
274 	return(-1);
275 }
276 
277 /*
278  * PHYS_TO_VM_PAGE: find vm_page for a PA.   used by MI code to get vm_pages
279  * back from an I/O mapping (ugh!).   used in some MD code as well.
280  */
281 static __inline struct vm_page *
282 PHYS_TO_VM_PAGE(paddr_t pa)
283 {
284 	paddr_t pf = atop(pa);
285 	int	off;
286 	int	psi;
287 
288 	psi = vm_physseg_find(pf, &off);
289 
290 	return ((psi == -1) ? NULL : &vm_physmem[psi].pgs[off]);
291 }
292 #else
293 /* if VM_PHYSSEG_MAX > 1 they're not inline, they're in uvm_page.c. */
294 struct vm_page	*PHYS_TO_VM_PAGE(paddr_t);
295 int		vm_physseg_find(paddr_t, int *);
296 #endif
297 
298 /*
299  * macros
300  */
301 
302 #define uvm_lock_pageq()	simple_lock(&uvm.pageqlock)
303 #define uvm_unlock_pageq()	simple_unlock(&uvm.pageqlock)
304 #define uvm_lock_fpageq()	mtx_enter(&uvm.fpageqlock);
305 #define uvm_unlock_fpageq()	mtx_leave(&uvm.fpageqlock);
306 
307 #define	UVM_PAGEZERO_TARGET	(uvmexp.free)
308 
309 #define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
310 
311 #define VM_PAGE_IS_FREE(entry)  ((entry)->pg_flags & PQ_FREE)
312 
313 #define	PADDR_IS_DMA_REACHABLE(paddr)	\
314 	(dma_constraint.ucr_low <= paddr && dma_constraint.ucr_high > paddr)
315 
316 #endif /* _KERNEL */
317 
318 #endif /* _UVM_UVM_PAGE_H_ */
319