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 short hold_count; /* page hold count */ 122 u_char act_count; /* page usage count */ 123 u_char busy; /* page busy count */ 124 /* NOTE that these must support one bit per DEV_BSIZE in a page!!! */ 125 /* so, on normal X86 kernels, they must be at least 8 bits wide */ 126 #if PAGE_SIZE == 4096 127 u_char valid; /* map of valid DEV_BSIZE chunks (O) */ 128 u_char dirty; /* map of dirty DEV_BSIZE chunks */ 129 #elif PAGE_SIZE == 8192 130 u_short valid; /* map of valid DEV_BSIZE chunks (O) */ 131 u_short dirty; /* map of dirty DEV_BSIZE chunks */ 132 #elif PAGE_SIZE == 16384 133 u_int valid; /* map of valid DEV_BSIZE chunks (O) */ 134 u_int dirty; /* map of dirty DEV_BSIZE chunks */ 135 #elif PAGE_SIZE == 32768 136 u_long valid; /* map of valid DEV_BSIZE chunks (O) */ 137 u_long dirty; /* map of dirty DEV_BSIZE chunks */ 138 #endif 139 u_int cow; /* page cow mapping count */ 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 /* 150 * note: currently use SWAPBLK_NONE as an absolute value rather then 151 * a flag bit. 152 */ 153 154 #define SWAPBLK_MASK ((daddr_t)((u_daddr_t)-1 >> 1)) /* mask */ 155 #define SWAPBLK_NONE ((daddr_t)((u_daddr_t)SWAPBLK_MASK + 1))/* flag */ 156 157 #if !defined(KLD_MODULE) 158 /* 159 * Page coloring parameters 160 */ 161 /* Each of PQ_FREE, and PQ_CACHE have PQ_HASH_SIZE entries */ 162 163 /* Backward compatibility for existing PQ_*CACHE config options. */ 164 #if !defined(PQ_CACHESIZE) 165 #if defined(PQ_HUGECACHE) 166 #define PQ_CACHESIZE 1024 167 #elif defined(PQ_LARGECACHE) 168 #define PQ_CACHESIZE 512 169 #elif defined(PQ_MEDIUMCACHE) 170 #define PQ_CACHESIZE 256 171 #elif defined(PQ_NORMALCACHE) 172 #define PQ_CACHESIZE 64 173 #elif defined(PQ_NOOPT) 174 #define PQ_CACHESIZE 0 175 #else 176 #define PQ_CACHESIZE 128 177 #endif 178 #endif /* !defined(PQ_CACHESIZE) */ 179 180 #if PQ_CACHESIZE >= 1024 181 #define PQ_PRIME1 31 /* Prime number somewhat less than PQ_HASH_SIZE */ 182 #define PQ_PRIME2 23 /* Prime number somewhat less than PQ_HASH_SIZE */ 183 #define PQ_L2_SIZE 256 /* A number of colors opt for 1M cache */ 184 185 #elif PQ_CACHESIZE >= 512 186 #define PQ_PRIME1 31 /* Prime number somewhat less than PQ_HASH_SIZE */ 187 #define PQ_PRIME2 23 /* Prime number somewhat less than PQ_HASH_SIZE */ 188 #define PQ_L2_SIZE 128 /* A number of colors opt for 512K cache */ 189 190 #elif PQ_CACHESIZE >= 256 191 #define PQ_PRIME1 13 /* Prime number somewhat less than PQ_HASH_SIZE */ 192 #define PQ_PRIME2 7 /* Prime number somewhat less than PQ_HASH_SIZE */ 193 #define PQ_L2_SIZE 64 /* A number of colors opt for 256K cache */ 194 195 #elif PQ_CACHESIZE >= 128 196 #define PQ_PRIME1 9 /* Produces a good PQ_L2_SIZE/3 + PQ_PRIME1 */ 197 #define PQ_PRIME2 5 /* Prime number somewhat less than PQ_HASH_SIZE */ 198 #define PQ_L2_SIZE 32 /* A number of colors opt for 128k cache */ 199 200 #elif PQ_CACHESIZE >= 64 201 #define PQ_PRIME1 5 /* Prime number somewhat less than PQ_HASH_SIZE */ 202 #define PQ_PRIME2 3 /* Prime number somewhat less than PQ_HASH_SIZE */ 203 #define PQ_L2_SIZE 16 /* A reasonable number of colors (opt for 64K cache) */ 204 205 #else 206 #define PQ_PRIME1 1 /* Disable page coloring. */ 207 #define PQ_PRIME2 1 208 #define PQ_L2_SIZE 1 209 210 #endif 211 212 #define PQ_L2_MASK (PQ_L2_SIZE - 1) 213 214 #define PQ_NONE 0 215 #define PQ_FREE 1 216 #define PQ_INACTIVE (1 + 1*PQ_L2_SIZE) 217 #define PQ_ACTIVE (2 + 1*PQ_L2_SIZE) 218 #define PQ_CACHE (3 + 1*PQ_L2_SIZE) 219 #define PQ_HOLD (3 + 2*PQ_L2_SIZE) 220 #define PQ_COUNT (4 + 2*PQ_L2_SIZE) 221 222 struct vpgqueues { 223 struct pglist pl; 224 int *cnt; 225 int lcnt; 226 }; 227 228 extern struct vpgqueues vm_page_queues[PQ_COUNT]; 229 extern struct mtx vm_page_queue_free_mtx; 230 231 #endif /* !defined(KLD_MODULE) */ 232 233 /* 234 * These are the flags defined for vm_page. 235 * 236 * Note: PG_FILLED and PG_DIRTY are added for the filesystems. 237 * 238 * Note: PG_UNMANAGED (used by OBJT_PHYS) indicates that the page is 239 * not under PV management but otherwise should be treated as a 240 * normal page. Pages not under PV management cannot be paged out 241 * via the object/vm_page_t because there is no knowledge of their 242 * pte mappings, nor can they be removed from their objects via 243 * the object, and such pages are also not on any PQ queue. 244 */ 245 #define PG_BUSY 0x0001 /* page is in transit (O) */ 246 #define PG_WANTED 0x0002 /* someone is waiting for page (O) */ 247 #define PG_WINATCFLS 0x0004 /* flush dirty page on inactive q */ 248 #define PG_FICTITIOUS 0x0008 /* physical page doesn't exist (O) */ 249 #define PG_WRITEABLE 0x0010 /* page is mapped writeable */ 250 #define PG_ZERO 0x0040 /* page is zeroed */ 251 #define PG_REFERENCED 0x0080 /* page has been referenced */ 252 #define PG_CLEANCHK 0x0100 /* page will be checked for cleaning */ 253 #define PG_SWAPINPROG 0x0200 /* swap I/O in progress on page */ 254 #define PG_NOSYNC 0x0400 /* do not collect for syncer */ 255 #define PG_UNMANAGED 0x0800 /* No PV management for page */ 256 #define PG_MARKER 0x1000 /* special queue marker page */ 257 #define PG_SLAB 0x2000 /* object pointer is actually a slab */ 258 259 /* 260 * Misc constants. 261 */ 262 #define ACT_DECLINE 1 263 #define ACT_ADVANCE 3 264 #define ACT_INIT 5 265 #define ACT_MAX 64 266 267 #ifdef _KERNEL 268 /* 269 * Each pageable resident page falls into one of four lists: 270 * 271 * free 272 * Available for allocation now. 273 * 274 * The following are all LRU sorted: 275 * 276 * cache 277 * Almost available for allocation. Still in an 278 * object, but clean and immediately freeable at 279 * non-interrupt times. 280 * 281 * inactive 282 * Low activity, candidates for reclamation. 283 * This is the list of pages that should be 284 * paged out next. 285 * 286 * active 287 * Pages that are "active" i.e. they have been 288 * recently referenced. 289 * 290 * zero 291 * Pages that are really free and have been pre-zeroed 292 * 293 */ 294 295 extern int vm_page_zero_count; 296 297 extern vm_page_t vm_page_array; /* First resident page in table */ 298 extern int vm_page_array_size; /* number of vm_page_t's */ 299 extern long first_page; /* first physical page number */ 300 301 #define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) 302 303 #define PHYS_TO_VM_PAGE(pa) \ 304 (&vm_page_array[atop(pa) - first_page ]) 305 306 extern struct mtx vm_page_queue_mtx; 307 #define vm_page_lock_queues() mtx_lock(&vm_page_queue_mtx) 308 #define vm_page_unlock_queues() mtx_unlock(&vm_page_queue_mtx) 309 310 #if PAGE_SIZE == 4096 311 #define VM_PAGE_BITS_ALL 0xffu 312 #elif PAGE_SIZE == 8192 313 #define VM_PAGE_BITS_ALL 0xffffu 314 #elif PAGE_SIZE == 16384 315 #define VM_PAGE_BITS_ALL 0xffffffffu 316 #elif PAGE_SIZE == 32768 317 #define VM_PAGE_BITS_ALL 0xfffffffffffffffflu 318 #endif 319 320 /* page allocation classes: */ 321 #define VM_ALLOC_NORMAL 0 322 #define VM_ALLOC_INTERRUPT 1 323 #define VM_ALLOC_SYSTEM 2 324 #define VM_ALLOC_CLASS_MASK 3 325 /* page allocation flags: */ 326 #define VM_ALLOC_WIRED 0x0020 /* non pageable */ 327 #define VM_ALLOC_ZERO 0x0040 /* Try to obtain a zeroed page */ 328 #define VM_ALLOC_RETRY 0x0080 /* vm_page_grab() only */ 329 #define VM_ALLOC_NOOBJ 0x0100 /* No associated object */ 330 331 void vm_page_flag_set(vm_page_t m, unsigned short bits); 332 void vm_page_flag_clear(vm_page_t m, unsigned short bits); 333 void vm_page_busy(vm_page_t m); 334 void vm_page_flash(vm_page_t m); 335 void vm_page_io_start(vm_page_t m); 336 void vm_page_io_finish(vm_page_t m); 337 void vm_page_hold(vm_page_t mem); 338 void vm_page_unhold(vm_page_t mem); 339 void vm_page_free(vm_page_t m); 340 void vm_page_free_zero(vm_page_t m); 341 int vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg); 342 void vm_page_dirty(vm_page_t m); 343 void vm_page_wakeup(vm_page_t m); 344 345 void vm_pageq_init(void); 346 vm_page_t vm_pageq_add_new_page(vm_paddr_t pa); 347 void vm_pageq_enqueue(int queue, vm_page_t m); 348 void vm_pageq_remove_nowakeup(vm_page_t m); 349 void vm_pageq_remove(vm_page_t m); 350 vm_page_t vm_pageq_find(int basequeue, int index, boolean_t prefer_zero); 351 void vm_pageq_requeue(vm_page_t m); 352 353 void vm_page_activate (vm_page_t); 354 vm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int); 355 vm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int); 356 void vm_page_cache (register vm_page_t); 357 int vm_page_try_to_cache (vm_page_t); 358 int vm_page_try_to_free (vm_page_t); 359 void vm_page_dontneed (register vm_page_t); 360 void vm_page_deactivate (vm_page_t); 361 void vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t); 362 vm_page_t vm_page_lookup (vm_object_t, vm_pindex_t); 363 void vm_page_remove (vm_page_t); 364 void vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t); 365 vm_page_t vm_page_select_cache(int); 366 vm_page_t vm_page_splay(vm_pindex_t, vm_page_t); 367 vm_offset_t vm_page_startup(vm_offset_t vaddr); 368 void vm_page_unmanage (vm_page_t); 369 void vm_page_unwire (vm_page_t, int); 370 void vm_page_wire (vm_page_t); 371 void vm_page_set_validclean (vm_page_t, int, int); 372 void vm_page_clear_dirty (vm_page_t, int, int); 373 void vm_page_set_invalid (vm_page_t, int, int); 374 int vm_page_is_valid (vm_page_t, int, int); 375 void vm_page_test_dirty (vm_page_t); 376 int vm_page_bits (int, int); 377 void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid); 378 void vm_page_free_toq(vm_page_t m); 379 void vm_page_zero_idle_wakeup(void); 380 void vm_page_cowfault (vm_page_t); 381 void vm_page_cowsetup (vm_page_t); 382 void vm_page_cowclear (vm_page_t); 383 384 /* 385 * vm_page_undirty: 386 * 387 * Set page to not be dirty. Note: does not clear pmap modify bits 388 */ 389 static __inline void 390 vm_page_undirty(vm_page_t m) 391 { 392 m->dirty = 0; 393 } 394 395 #endif /* _KERNEL */ 396 #endif /* !_VM_PAGE_ */ 397