uvm_page.h revision 1.87 1 /* $NetBSD: uvm_page.h,v 1.87 2019/12/15 21:11:35 ad 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 (at) 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 /*
71 * Management of resident (logical) pages.
72 *
73 * Each resident page has a vm_page structure, indexed by page number.
74 * There are several lists in the structure:
75 *
76 * - A red-black tree rooted with the containing object is used to
77 * quickly perform object+offset lookups.
78 * - A list of all pages for a given object, for a quick deactivation
79 * at a time of deallocation.
80 * - An ordered list of pages due for pageout.
81 *
82 * In addition, the structure contains the object and offset to which
83 * this page belongs (for pageout) and sundry status bits.
84 *
85 * Note that the page structure has no lock of its own. The page is
86 * generally protected by its owner's lock (UVM object or amap/anon).
87 * It should be noted that UVM has to serialize pmap(9) operations on
88 * the managed pages, e.g. for pmap_enter() calls. Hence, the lock
89 * order is as follows:
90 *
91 * [vmpage-owner-lock] ->
92 * any pmap locks (e.g. PV hash lock)
93 *
94 * Since the kernel is always self-consistent, no serialization is
95 * required for unmanaged mappings, e.g. for pmap_kenter_pa() calls.
96 *
97 * Field markings and the corresponding locks:
98 *
99 * f: free page queue lock, uvm_fpageqlock
100 * o: page owner (uvm_object::vmobjlock, vm_amap::am_lock, vm_anon::an_lock)
101 * i: vm_page::interlock
102 * => flags set and cleared only with o&i held can
103 * safely be tested for with only o held.
104 * o,i: o|i for read, o&i for write (depends on context - if could be loaned)
105 * => see uvm_loan.c
106 * w: wired page queue or uvm_pglistalloc:
107 * => wired page queue: o&i to change, stable from wire to unwire
108 * XXX What about concurrent or nested wire?
109 * => uvm_pglistalloc: owned by caller
110 * ?: locked by pmap or assumed page owner's lock
111 * p: locked by pagedaemon policy module (pdpolicy)
112 * c: cpu private
113 * s: stable, does not change
114 *
115 * UVM and pmap(9) may use uvm_page_locked_p() to assert whether the
116 * page owner's lock is acquired.
117 *
118 * A page can have one of four identities:
119 *
120 * o free
121 * => pageq.list is entry on global free page queue
122 * => listq.list is entry on per-CPU free page queue
123 * => uanon is unused (or (void *)0xdeadbeef for DEBUG)
124 * => uobject is unused (or (void *)0xdeadbeef for DEBUG)
125 * => PG_FREE is set in flags
126 * o owned by a uvm_object
127 * => pageq.queue is entry on wired page queue, if any
128 * => uanon is NULL or the vm_anon to which it has been O->A loaned
129 * => uobject is owner
130 * o owned by a vm_anon
131 * => pageq is unused (XXX correct?)
132 * => listq is unused (XXX correct?)
133 * => uanon is owner
134 * => uobject is NULL
135 * => PG_ANON is set in flags
136 * o allocated by uvm_pglistalloc
137 * => pageq.queue is entry on resulting pglist, owned by caller
138 * => listq is unused (XXX correct?)
139 * => uanon is unused
140 * => uobject is unused
141 *
142 * The following transitions are allowed:
143 *
144 * - uvm_pagealloc: free -> owned by a uvm_object/vm_anon
145 * - uvm_pagefree: owned by a uvm_object/vm_anon -> free
146 * - uvm_pglistalloc: free -> allocated by uvm_pglistalloc
147 * - uvm_pglistfree: allocated by uvm_pglistalloc -> free
148 */
149
150 struct vm_page {
151 union {
152 TAILQ_ENTRY(vm_page) queue; /* w: wired page queue
153 * or uvm_pglistalloc output */
154 LIST_ENTRY(vm_page) list; /* f: global free page queue */
155 } pageq;
156
157 union {
158 LIST_ENTRY(vm_page) list; /* f: CPU free page queue */
159 } listq;
160
161 struct vm_anon *uanon; /* o,i: anon */
162 struct uvm_object *uobject; /* o,i: object */
163 voff_t offset; /* o: offset into object */
164 uint16_t flags; /* o: object flags */
165 uint16_t spare; /* : spare for now */
166 uint32_t pqflags; /* p: pdpolicy queue flags */
167 uint32_t loan_count; /* o,i: num. active loans */
168 uint32_t wire_count; /* o,i: wired down map refs */
169 paddr_t phys_addr; /* o: physical address of pg */
170 kmutex_t interlock; /* s: lock on identity */
171
172 #ifdef __HAVE_VM_PAGE_MD
173 struct vm_page_md mdpage; /* ?: pmap-specific data */
174 #endif
175
176 #if defined(UVM_PAGE_TRKOWN)
177 /* debugging fields to track page ownership */
178 pid_t owner; /* proc that set PG_BUSY */
179 lwpid_t lowner; /* lwp that set PG_BUSY */
180 const char *owner_tag; /* why it was set busy */
181 #endif
182 };
183
184 /*
185 * Overview of UVM page flags.
186 *
187 * Locking notes:
188 *
189 * PG_, struct vm_page::flags => locked by owner
190 * PG_AOBJ => additionally locked by vm_page::interlock
191 * PG_ANON => additionally locked by vm_page::interlock
192 * PG_FREE => additionally locked by uvm_fpageqlock
193 * for uvm_pglistalloc()
194 *
195 * Flag descriptions:
196 *
197 * PG_BUSY:
198 * Page is long-term locked, usually because of I/O (transfer from the
199 * page memory to the backing store) is in progress. LWP attempting
200 * to access the page shall set PG_WANTED and wait.
201 *
202 * PG_WANTED:
203 * Indicates that the page, which is currently PG_BUSY, is wanted by
204 * some other LWP. The page owner (i.e. LWP which set PG_BUSY) is
205 * responsible to clear both flags and wake up any waiters once it has
206 * released the long-term lock (PG_BUSY).
207 *
208 * PG_RELEASED:
209 * Indicates that the page, which is currently PG_BUSY, should be freed
210 * after the release of long-term lock. It is responsibility of the
211 * owning LWP (i.e. which set PG_BUSY) to do it.
212 *
213 * PG_CLEAN:
214 * Page has not been modified since it was loaded from the backing
215 * store. If this flag is not set, page is considered "dirty".
216 * XXX: Currently it means that the page *might* be clean; will be
217 * fixed with yamt-pagecache merge.
218 *
219 * PG_FAKE:
220 * Page has been allocated, but not yet initialised. The flag is used
221 * to avoid overwriting of valid data, e.g. to prevent read from the
222 * backing store when in-core data is newer.
223 *
224 * PG_TABLED:
225 * Indicates that the page is currently in the object's offset queue,
226 * and that it should be removed from it once the page is freed. Used
227 * diagnostic purposes.
228 *
229 * PG_PAGEOUT:
230 * Indicates that the page is being paged-out in preparation for
231 * being freed.
232 *
233 * PG_RDONLY:
234 * Indicates that the page must be mapped read-only.
235 *
236 * PG_ZERO:
237 * Indicates that the page has been pre-zeroed. This flag is only
238 * set when the page is not in the queues and is cleared when the
239 * page is placed on the free list.
240 *
241 * PG_MARKER:
242 * Dummy marker page.
243 */
244
245 #define PG_BUSY 0x0001
246 #define PG_WANTED 0x0002
247 #define PG_TABLED 0x0004
248 #define PG_CLEAN 0x0008
249 #define PG_PAGEOUT 0x0010
250 #define PG_RELEASED 0x0020
251 #define PG_FAKE 0x0040
252 #define PG_RDONLY 0x0080
253 #define PG_AOBJ 0x0100 /* page is part of an anonymous
254 uvm_object */
255 #define PG_ANON 0x0200 /* page is part of an anon, rather
256 than an uvm_object */
257 #define PG_SWAPBACKED (PG_ANON|PG_AOBJ)
258 #define PG_READAHEAD 0x0400 /* read-ahead but not "hit" yet */
259 #define PG_FREE 0x0800 /* page is on free list */
260 #define PG_MARKER 0x1000
261 #define PG_PAGER1 0x2000 /* pager-specific flag */
262 #define PG_ZERO 0x4000
263
264 #define UVM_PGFLAGBITS \
265 "\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY" \
266 "\11AOBJ\12AOBJ\13READAHEAD\14FREE\15MARKER\16PAGER1\17ZERO"
267
268 /*
269 * physical memory layout structure
270 *
271 * MD vmparam.h must #define:
272 * VM_PHYSEG_MAX = max number of physical memory segments we support
273 * (if this is "1" then we revert to a "contig" case)
274 * VM_PHYSSEG_STRAT: memory sort/search options (for VM_PHYSEG_MAX > 1)
275 * - VM_PSTRAT_RANDOM: linear search (random order)
276 * - VM_PSTRAT_BSEARCH: binary search (sorted by address)
277 * - VM_PSTRAT_BIGFIRST: linear search (sorted by largest segment first)
278 * - others?
279 * XXXCDC: eventually we should purge all left-over global variables...
280 */
281 #define VM_PSTRAT_RANDOM 1
282 #define VM_PSTRAT_BSEARCH 2
283 #define VM_PSTRAT_BIGFIRST 3
284
285 #ifdef _KERNEL
286
287 /*
288 * globals
289 */
290
291 extern bool vm_page_zero_enable;
292
293 /*
294 * prototypes: the following prototypes define the interface to pages
295 */
296
297 void uvm_page_init(vaddr_t *, vaddr_t *);
298 #if defined(UVM_PAGE_TRKOWN)
299 void uvm_page_own(struct vm_page *, const char *);
300 #endif
301 #if !defined(PMAP_STEAL_MEMORY)
302 bool uvm_page_physget(paddr_t *);
303 #endif
304 void uvm_page_recolor(int);
305 void uvm_pageidlezero(void);
306
307 void uvm_pageactivate(struct vm_page *);
308 vaddr_t uvm_pageboot_alloc(vsize_t);
309 void uvm_pagecopy(struct vm_page *, struct vm_page *);
310 void uvm_pagedeactivate(struct vm_page *);
311 void uvm_pagedequeue(struct vm_page *);
312 void uvm_pageenqueue(struct vm_page *);
313 void uvm_pagefree(struct vm_page *);
314 void uvm_page_unbusy(struct vm_page **, int);
315 struct vm_page *uvm_pagelookup(struct uvm_object *, voff_t);
316 void uvm_pageunwire(struct vm_page *);
317 void uvm_pagewire(struct vm_page *);
318 void uvm_pagezero(struct vm_page *);
319 bool uvm_pageismanaged(paddr_t);
320 bool uvm_page_locked_p(struct vm_page *);
321
322 int uvm_page_lookup_freelist(struct vm_page *);
323
324 struct vm_page *uvm_phys_to_vm_page(paddr_t);
325 paddr_t uvm_vm_page_to_phys(const struct vm_page *);
326
327 #if defined(PMAP_DIRECT)
328 extern bool ubc_direct;
329 int uvm_direct_process(struct vm_page **, u_int, voff_t, vsize_t,
330 int (*)(void *, size_t, void *), void *);
331 #endif
332
333 /*
334 * macros
335 */
336
337 #define UVM_PAGE_TREE_PENALTY 4 /* XXX: a guess */
338
339 #define VM_PAGE_TO_PHYS(entry) uvm_vm_page_to_phys(entry)
340
341 #ifdef __HAVE_VM_PAGE_MD
342 #define VM_PAGE_TO_MD(pg) (&(pg)->mdpage)
343 #endif
344
345 /*
346 * Compute the page color bucket for a given page.
347 */
348 #define VM_PGCOLOR_BUCKET(pg) \
349 (atop(VM_PAGE_TO_PHYS((pg))) & uvmexp.colormask)
350
351 #define PHYS_TO_VM_PAGE(pa) uvm_phys_to_vm_page(pa)
352
353 #define VM_PAGE_IS_FREE(entry) ((entry)->flags & PG_FREE)
354 #define VM_FREE_PAGE_TO_CPU(pg) ((struct uvm_cpu *)((uintptr_t)pg->offset))
355
356 #ifdef DEBUG
357 void uvm_pagezerocheck(struct vm_page *);
358 #endif /* DEBUG */
359
360 #endif /* _KERNEL */
361
362 #endif /* _UVM_UVM_PAGE_H_ */
363