xref: /minix/sys/uvm/uvm_page.h (revision 84d9c625)
1 /*	$NetBSD: uvm_page.h,v 1.76 2013/10/25 14:30:21 martin Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993, The Regents of the University of California.
6  *
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * The Mach Operating System project at Carnegie-Mellon University.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)vm_page.h   7.3 (Berkeley) 4/21/91
37  * from: Id: uvm_page.h,v 1.1.2.6 1998/02/04 02:31:42 chuck Exp
38  *
39  *
40  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
41  * All rights reserved.
42  *
43  * Permission to use, copy, modify and distribute this software and
44  * its documentation is hereby granted, provided that both the copyright
45  * notice and this permission notice appear in all copies of the
46  * software, derivative works or modified versions, and any portions
47  * thereof, and that both notices appear in supporting documentation.
48  *
49  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
51  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52  *
53  * Carnegie Mellon requests users of this software to return to
54  *
55  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
56  *  School of Computer Science
57  *  Carnegie Mellon University
58  *  Pittsburgh PA 15213-3890
59  *
60  * any improvements or extensions that they make and grant Carnegie the
61  * rights to redistribute these changes.
62  */
63 
64 #ifndef _UVM_UVM_PAGE_H_
65 #define _UVM_UVM_PAGE_H_
66 
67 #include <uvm/uvm_extern.h>
68 #include <uvm/uvm_pglist.h>
69 
70 #include <sys/rbtree.h>
71 
72 /*
73  * Management of resident (logical) pages.
74  *
75  * Each resident page has a vm_page structure, indexed by page number.
76  * There are several lists in the structure:
77  *
78  * - A red-black tree rooted with the containing object is used to
79  *   quickly perform object+offset lookups.
80  * - A list of all pages for a given object, for a quick deactivation
81  *   at a time of deallocation.
82  * - An ordered list of pages due for pageout.
83  *
84  * In addition, the structure contains the object and offset to which
85  * this page belongs (for pageout) and sundry status bits.
86  *
87  * Note that the page structure has no lock of its own.  The page is
88  * generally protected by its owner's lock (UVM object or amap/anon).
89  * It should be noted that UVM has to serialize pmap(9) operations on
90  * the managed pages, e.g. for pmap_enter() calls.  Hence, the lock
91  * order is as follows:
92  *
93  *	[vmpage-owner-lock] ->
94  *		any pmap locks (e.g. PV hash lock)
95  *
96  * Since the kernel is always self-consistent, no serialization is
97  * required for unmanaged mappings, e.g. for pmap_kenter_pa() calls.
98  *
99  * Field markings and the corresponding locks:
100  *
101  * o:	page owner's lock (UVM object or amap/anon)
102  * p:	lock on the page queues
103  * o|p:	either lock can be acquired
104  * o&p:	both locks are required
105  * ?:	locked by pmap or assumed page owner's lock
106  *
107  * UVM and pmap(9) may use uvm_page_locked_p() to assert whether the
108  * page owner's lock is acquired.
109  */
110 
111 struct vm_page {
112 	struct rb_node		rb_node;	/* o: tree of pages in obj */
113 
114 	union {
115 		TAILQ_ENTRY(vm_page) queue;
116 		LIST_ENTRY(vm_page) list;
117 	} pageq;				/* p: queue info for FIFO
118 						 * queue or free list */
119 	union {
120 		TAILQ_ENTRY(vm_page) queue;
121 		LIST_ENTRY(vm_page) list;
122 	} listq;				/* o: pages in same object */
123 
124 	struct vm_anon		*uanon;		/* o,p: anon */
125 	struct uvm_object	*uobject;	/* o,p: object */
126 	voff_t			offset;		/* o,p: offset into object */
127 	uint16_t		flags;		/* o: object flags */
128 	uint16_t		loan_count;	/* number of active loans
129 						 * o|p: for reading
130 						 * o&p: for modification */
131 	uint16_t		wire_count;	/* p: wired down map refs */
132 	uint16_t		pqflags;	/* p: page queue flags */
133 	paddr_t			phys_addr;	/* physical address of page */
134 
135 #ifdef __HAVE_VM_PAGE_MD
136 	struct vm_page_md	mdpage;		/* ?: pmap-specific data */
137 #endif
138 
139 #if defined(UVM_PAGE_TRKOWN)
140 	/* debugging fields to track page ownership */
141 	pid_t			owner;		/* proc that set PG_BUSY */
142 	lwpid_t			lowner;		/* lwp that set PG_BUSY */
143 	const char		*owner_tag;	/* why it was set busy */
144 #endif
145 };
146 
147 /*
148  * Overview of UVM page flags.
149  *
150  * Locking notes:
151  *
152  * PG_, struct vm_page::flags	=> locked by the owner
153  * PQ_, struct vm_page::pqflags	=> locked by the page-queue lock
154  * PQ_FREE			=> additionally locked by free-queue lock
155  *
156  * Flag descriptions:
157  *
158  * PG_BUSY:
159  *	Page is long-term locked, usually because of I/O (transfer from the
160  *	page memory to the backing store) is in progress.  LWP attempting
161  *	to access the page shall set PG_WANTED and wait.
162  *
163  * PG_WANTED:
164  *	Indicates that the page, which is currently PG_BUSY, is wanted by
165  *	some other LWP.  The page owner (i.e. LWP which set PG_BUSY) is
166  *	responsible to clear both flags and wake up any waiters once it has
167  *	released the long-term lock (PG_BUSY).
168  *
169  * PG_RELEASED:
170  *	Indicates that the page, which is currently PG_BUSY, should be freed
171  *	after the release of long-term lock.  It is responsibility of the
172  *	owning LWP (i.e. which set PG_BUSY) to do it.
173  *
174  * PG_CLEAN:
175  *	Page has not been modified since it was loaded from the backing
176  *	store.  If this flag is not set, page is considered "dirty".
177  *	XXX: Currently it means that the page *might* be clean; will be
178  *	fixed with yamt-pagecache merge.
179  *
180  * PG_FAKE:
181  *	Page has been allocated, but not yet initialised.  The flag is used
182  *	to avoid overwriting of valid data, e.g. to prevent read from the
183  *	backing store when in-core data is newer.
184  *
185  * PG_TABLED:
186  *	Indicates that the page is currently in the object's offset queue,
187  *	and that it should be removed from it once the page is freed.  Used
188  *	diagnostic purposes.
189  *
190  * PG_PAGEOUT:
191  *	Indicates that the page is being paged-out in preparation for
192  *	being freed.
193  *
194  * PG_RDONLY:
195  *	Indicates that the page must be mapped read-only.
196  *
197  * PG_ZERO:
198  *	Indicates that the page has been pre-zeroed.  This flag is only
199  *	set when the page is not in the queues and is cleared when the
200  *	page is placed on the free list.
201  *
202  * PG_MARKER:
203  *	Dummy marker page.
204  */
205 
206 #define	PG_BUSY		0x0001
207 #define	PG_WANTED	0x0002
208 #define	PG_TABLED	0x0004
209 #define	PG_CLEAN	0x0008
210 #define	PG_PAGEOUT	0x0010
211 #define	PG_RELEASED	0x0020
212 #define	PG_FAKE		0x0040
213 #define	PG_RDONLY	0x0080
214 #define	PG_ZERO		0x0100
215 #define	PG_MARKER	0x0200
216 
217 #define PG_PAGER1	0x1000		/* pager-specific flag */
218 
219 #define	UVM_PGFLAGBITS \
220 	"\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY" \
221 	"\11ZERO\12MARKER\15PAGER1"
222 
223 #define PQ_FREE		0x0001		/* page is on free list */
224 #define PQ_ANON		0x0002		/* page is part of an anon, rather
225 					   than an uvm_object */
226 #define PQ_AOBJ		0x0004		/* page is part of an anonymous
227 					   uvm_object */
228 #define PQ_SWAPBACKED	(PQ_ANON|PQ_AOBJ)
229 #define PQ_READAHEAD	0x0008	/* read-ahead but has not been "hit" yet */
230 
231 #define PQ_PRIVATE1	0x0100
232 #define PQ_PRIVATE2	0x0200
233 #define PQ_PRIVATE3	0x0400
234 #define PQ_PRIVATE4	0x0800
235 #define PQ_PRIVATE5	0x1000
236 #define PQ_PRIVATE6	0x2000
237 #define PQ_PRIVATE7	0x4000
238 #define PQ_PRIVATE8	0x8000
239 
240 #define	UVM_PQFLAGBITS \
241 	"\20\1FREE\2ANON\3AOBJ\4READAHEAD" \
242 	"\11PRIVATE1\12PRIVATE2\13PRIVATE3\14PRIVATE4" \
243 	"\15PRIVATE5\16PRIVATE6\17PRIVATE7\20PRIVATE8"
244 
245 /*
246  * physical memory layout structure
247  *
248  * MD vmparam.h must #define:
249  *   VM_PHYSEG_MAX = max number of physical memory segments we support
250  *		   (if this is "1" then we revert to a "contig" case)
251  *   VM_PHYSSEG_STRAT: memory sort/search options (for VM_PHYSEG_MAX > 1)
252  * 	- VM_PSTRAT_RANDOM:   linear search (random order)
253  *	- VM_PSTRAT_BSEARCH:  binary search (sorted by address)
254  *	- VM_PSTRAT_BIGFIRST: linear search (sorted by largest segment first)
255  *      - others?
256  *   XXXCDC: eventually we should purge all left-over global variables...
257  */
258 #define VM_PSTRAT_RANDOM	1
259 #define VM_PSTRAT_BSEARCH	2
260 #define VM_PSTRAT_BIGFIRST	3
261 
262 /*
263  * vm_physseg: describes one segment of physical memory
264  */
265 struct vm_physseg {
266 	paddr_t	start;			/* PF# of first page in segment */
267 	paddr_t	end;			/* (PF# of last page in segment) + 1 */
268 	paddr_t	avail_start;		/* PF# of first free page in segment */
269 	paddr_t	avail_end;		/* (PF# of last free page in segment) +1  */
270 	struct	vm_page *pgs;		/* vm_page structures (from start) */
271 	struct	vm_page *lastpg;	/* vm_page structure for end */
272 	int	free_list;		/* which free list they belong on */
273 	u_int	start_hint;		/* start looking for free pages here */
274 					/* protected by uvm_fpageqlock */
275 #ifdef __HAVE_PMAP_PHYSSEG
276 	struct	pmap_physseg pmseg;	/* pmap specific (MD) data */
277 #endif
278 };
279 
280 #ifdef _KERNEL
281 
282 /*
283  * globals
284  */
285 
286 extern bool vm_page_zero_enable;
287 
288 /*
289  * physical memory config is stored in vm_physmem.
290  */
291 
292 #define	VM_PHYSMEM_PTR(i)	(&vm_physmem[i])
293 #if VM_PHYSSEG_MAX == 1
294 #define VM_PHYSMEM_PTR_SWAP(i, j) /* impossible */
295 #else
296 #define VM_PHYSMEM_PTR_SWAP(i, j) \
297 	do { vm_physmem[(i)] = vm_physmem[(j)]; } while (0)
298 #endif
299 
300 extern struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];
301 extern int vm_nphysseg;
302 
303 /*
304  * prototypes: the following prototypes define the interface to pages
305  */
306 
307 void uvm_page_init(vaddr_t *, vaddr_t *);
308 #if defined(UVM_PAGE_TRKOWN)
309 void uvm_page_own(struct vm_page *, const char *);
310 #endif
311 #if !defined(PMAP_STEAL_MEMORY)
312 bool uvm_page_physget(paddr_t *);
313 #endif
314 void uvm_page_recolor(int);
315 void uvm_pageidlezero(void);
316 
317 void uvm_pageactivate(struct vm_page *);
318 vaddr_t uvm_pageboot_alloc(vsize_t);
319 void uvm_pagecopy(struct vm_page *, struct vm_page *);
320 void uvm_pagedeactivate(struct vm_page *);
321 void uvm_pagedequeue(struct vm_page *);
322 void uvm_pageenqueue(struct vm_page *);
323 void uvm_pagefree(struct vm_page *);
324 void uvm_page_unbusy(struct vm_page **, int);
325 struct vm_page *uvm_pagelookup(struct uvm_object *, voff_t);
326 void uvm_pageunwire(struct vm_page *);
327 void uvm_pagewire(struct vm_page *);
328 void uvm_pagezero(struct vm_page *);
329 bool uvm_pageismanaged(paddr_t);
330 bool uvm_page_locked_p(struct vm_page *);
331 
332 int uvm_page_lookup_freelist(struct vm_page *);
333 
334 int vm_physseg_find(paddr_t, int *);
335 struct vm_page *uvm_phys_to_vm_page(paddr_t);
336 paddr_t uvm_vm_page_to_phys(const struct vm_page *);
337 
338 /*
339  * macros
340  */
341 
342 #define UVM_PAGE_TREE_PENALTY	4	/* XXX: a guess */
343 
344 #define VM_PAGE_TO_PHYS(entry)	uvm_vm_page_to_phys(entry)
345 
346 #ifdef __HAVE_VM_PAGE_MD
347 #define	VM_PAGE_TO_MD(pg)	(&(pg)->mdpage)
348 #endif
349 
350 /*
351  * Compute the page color bucket for a given page.
352  */
353 #define	VM_PGCOLOR_BUCKET(pg) \
354 	(atop(VM_PAGE_TO_PHYS((pg))) & uvmexp.colormask)
355 
356 #define	PHYS_TO_VM_PAGE(pa)	uvm_phys_to_vm_page(pa)
357 
358 #define VM_PAGE_IS_FREE(entry)  ((entry)->pqflags & PQ_FREE)
359 #define	VM_FREE_PAGE_TO_CPU(pg)	((struct uvm_cpu *)((uintptr_t)pg->offset))
360 
361 #ifdef DEBUG
362 void uvm_pagezerocheck(struct vm_page *);
363 #endif /* DEBUG */
364 
365 #endif /* _KERNEL */
366 
367 #endif /* _UVM_UVM_PAGE_H_ */
368