vm_page.h revision 209792
1139825Simp/*- 21541Srgrimes * Copyright (c) 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * The Mach Operating System project at Carnegie-Mellon University. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 4. Neither the name of the University nor the names of its contributors 171541Srgrimes * may be used to endorse or promote products derived from this software 181541Srgrimes * without specific prior written permission. 191541Srgrimes * 201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301541Srgrimes * SUCH DAMAGE. 311541Srgrimes * 321817Sdg * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93 331541Srgrimes * 341541Srgrimes * 351541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 361541Srgrimes * All rights reserved. 371541Srgrimes * 381541Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 395455Sdg * 401541Srgrimes * Permission to use, copy, modify and distribute this software and 411541Srgrimes * its documentation is hereby granted, provided that both the copyright 421541Srgrimes * notice and this permission notice appear in all copies of the 431541Srgrimes * software, derivative works or modified versions, and any portions 441541Srgrimes * thereof, and that both notices appear in supporting documentation. 455455Sdg * 465455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 475455Sdg * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 481541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 495455Sdg * 501541Srgrimes * Carnegie Mellon requests users of this software to return to 511541Srgrimes * 521541Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 531541Srgrimes * School of Computer Science 541541Srgrimes * Carnegie Mellon University 551541Srgrimes * Pittsburgh PA 15213-3890 561541Srgrimes * 571541Srgrimes * any improvements or extensions that they make and grant Carnegie the 581541Srgrimes * rights to redistribute these changes. 591817Sdg * 6050477Speter * $FreeBSD: head/sys/vm/vm_page.h 209792 2010-07-08 08:37:51Z kib $ 611541Srgrimes */ 621541Srgrimes 631541Srgrimes/* 641541Srgrimes * Resident memory system definitions. 651541Srgrimes */ 661541Srgrimes 671541Srgrimes#ifndef _VM_PAGE_ 681541Srgrimes#define _VM_PAGE_ 691541Srgrimes 706816Sdg#include <vm/pmap.h> 7138517Sdfr 721541Srgrimes/* 731541Srgrimes * Management of resident (logical) pages. 741541Srgrimes * 751541Srgrimes * A small structure is kept for each resident 761541Srgrimes * page, indexed by page number. Each structure 771541Srgrimes * is an element of several lists: 781541Srgrimes * 791541Srgrimes * A hash table bucket used to quickly 801541Srgrimes * perform object/offset lookups 811541Srgrimes * 821541Srgrimes * A list of all pages for a given object, 831541Srgrimes * so they can be quickly deactivated at 841541Srgrimes * time of deallocation. 851541Srgrimes * 861541Srgrimes * An ordered list of pages due for pageout. 871541Srgrimes * 881541Srgrimes * In addition, the structure contains the object 891541Srgrimes * and offset to which this page belongs (for pageout), 901541Srgrimes * and sundry status bits. 911541Srgrimes * 921541Srgrimes * Fields in this structure are locked either by the lock on the 93207460Skmacy * object that the page belongs to (O), its corresponding page lock (P), 94207460Skmacy * or by the lock on the page queues (Q). 95207460Skmacy * 961541Srgrimes */ 971541Srgrimes 9860938SjakeTAILQ_HEAD(pglist, vm_page); 991541Srgrimes 1001541Srgrimesstruct vm_page { 101207460Skmacy TAILQ_ENTRY(vm_page) pageq; /* queue info for FIFO queue or free list (Q) */ 10260938Sjake TAILQ_ENTRY(vm_page) listq; /* pages in same object (O) */ 103105407Sdillon struct vm_page *left; /* splay tree link (O) */ 104105407Sdillon struct vm_page *right; /* splay tree link (O) */ 1051541Srgrimes 106207669Salc vm_object_t object; /* which object am I in (O,P)*/ 107207460Skmacy vm_pindex_t pindex; /* offset into object (O,Q) */ 108121511Salc vm_paddr_t phys_addr; /* physical address of page */ 10960755Speter struct md_page md; /* machine dependant stuff */ 110207702Salc uint8_t queue; /* page queue index (P,Q) */ 111186719Skib int8_t segind; 112170816Salc u_short flags; /* see below */ 113170816Salc uint8_t order; /* index of the buddy queue */ 114170816Salc uint8_t pool; 115207905Salc u_short cow; /* page cow mapping count (P) */ 116207706Salc u_int wire_count; /* wired down maps refs (P) */ 117207460Skmacy short hold_count; /* page hold count (P) */ 118161125Salc u_short oflags; /* page flags (O) */ 119207740Salc u_char act_count; /* page usage count (P) */ 120139338Salc u_char busy; /* page busy count (O) */ 12113490Sdyson /* NOTE that these must support one bit per DEV_BSIZE in a page!!! */ 12213490Sdyson /* so, on normal X86 kernels, they must be at least 8 bits wide */ 12336735Sdfr#if PAGE_SIZE == 4096 124121511Salc u_char valid; /* map of valid DEV_BSIZE chunks (O) */ 125207905Salc u_char dirty; /* map of dirty DEV_BSIZE chunks (O) */ 12636735Sdfr#elif PAGE_SIZE == 8192 127121511Salc u_short valid; /* map of valid DEV_BSIZE chunks (O) */ 128207905Salc u_short dirty; /* map of dirty DEV_BSIZE chunks (O) */ 129119354Smarcel#elif PAGE_SIZE == 16384 130121511Salc u_int valid; /* map of valid DEV_BSIZE chunks (O) */ 131207905Salc u_int dirty; /* map of dirty DEV_BSIZE chunks (O) */ 132119354Smarcel#elif PAGE_SIZE == 32768 133121511Salc u_long valid; /* map of valid DEV_BSIZE chunks (O) */ 134207905Salc u_long dirty; /* map of dirty DEV_BSIZE chunks (O) */ 13536735Sdfr#endif 1361541Srgrimes}; 1371541Srgrimes 138161125Salc/* 139161125Salc * Page flags stored in oflags: 140161125Salc * 141161125Salc * Access to these page flags is synchronized by the lock on the object 142161125Salc * containing the page (O). 143161125Salc */ 144163604Salc#define VPO_BUSY 0x0001 /* page is in transit */ 145161125Salc#define VPO_WANTED 0x0002 /* someone is waiting for page */ 146161125Salc#define VPO_SWAPINPROG 0x0200 /* swap I/O in progress on page */ 147161257Salc#define VPO_NOSYNC 0x0400 /* do not collect for syncer */ 148161125Salc 149153940Snetchild#define PQ_NONE 0 150170816Salc#define PQ_INACTIVE 1 151170816Salc#define PQ_ACTIVE 2 152172317Salc#define PQ_HOLD 3 153172317Salc#define PQ_COUNT 4 15418169Sdyson 15552647Salcstruct vpgqueues { 15652647Salc struct pglist pl; 15718169Sdyson int *cnt; 15852647Salc}; 15918169Sdyson 160177342Salcextern struct vpgqueues vm_page_queues[PQ_COUNT]; 16152647Salc 162197750Salcstruct vpglocks { 163197750Salc struct mtx data; 164197750Salc char pad[CACHE_LINE_SIZE - sizeof(struct mtx)]; 165197750Salc} __aligned(CACHE_LINE_SIZE); 166197750Salc 167197750Salcextern struct vpglocks vm_page_queue_free_lock; 168207410Skmacyextern struct vpglocks pa_lock[]; 169197750Salc 170207410Skmacy#if defined(__arm__) 171207410Skmacy#define PDRSHIFT PDR_SHIFT 172207410Skmacy#elif !defined(PDRSHIFT) 173207410Skmacy#define PDRSHIFT 21 174207410Skmacy#endif 175207410Skmacy 176207410Skmacy#define pa_index(pa) ((pa) >> PDRSHIFT) 177207410Skmacy#define PA_LOCKPTR(pa) &pa_lock[pa_index((pa)) % PA_LOCK_COUNT].data 178207410Skmacy#define PA_LOCKOBJPTR(pa) ((struct lock_object *)PA_LOCKPTR((pa))) 179207410Skmacy#define PA_LOCK(pa) mtx_lock(PA_LOCKPTR(pa)) 180207410Skmacy#define PA_TRYLOCK(pa) mtx_trylock(PA_LOCKPTR(pa)) 181207410Skmacy#define PA_UNLOCK(pa) mtx_unlock(PA_LOCKPTR(pa)) 182207410Skmacy#define PA_UNLOCK_COND(pa) \ 183207410Skmacy do { \ 184208504Salc if ((pa) != 0) { \ 185208504Salc PA_UNLOCK((pa)); \ 186208504Salc (pa) = 0; \ 187208504Salc } \ 188207410Skmacy } while (0) 189207410Skmacy 190207410Skmacy#define PA_LOCK_ASSERT(pa, a) mtx_assert(PA_LOCKPTR(pa), (a)) 191207410Skmacy 192207410Skmacy#define vm_page_lockptr(m) (PA_LOCKPTR(VM_PAGE_TO_PHYS((m)))) 193207410Skmacy#define vm_page_lock(m) mtx_lock(vm_page_lockptr((m))) 194207410Skmacy#define vm_page_unlock(m) mtx_unlock(vm_page_lockptr((m))) 195207410Skmacy#define vm_page_trylock(m) mtx_trylock(vm_page_lockptr((m))) 196207410Skmacy#define vm_page_lock_assert(m, a) mtx_assert(vm_page_lockptr((m)), (a)) 197207410Skmacy 198197750Salc#define vm_page_queue_free_mtx vm_page_queue_free_lock.data 1991541Srgrimes/* 2001541Srgrimes * These are the flags defined for vm_page. 2011541Srgrimes * 20261081Sdillon * Note: PG_UNMANAGED (used by OBJT_PHYS) indicates that the page is 20361081Sdillon * not under PV management but otherwise should be treated as a 20461081Sdillon * normal page. Pages not under PV management cannot be paged out 20561081Sdillon * via the object/vm_page_t because there is no knowledge of their 20661081Sdillon * pte mappings, nor can they be removed from their objects via 20761081Sdillon * the object, and such pages are also not on any PQ queue. 208207905Salc * 209208990Salc * PG_REFERENCED may be cleared only if the object containing the page is 210208990Salc * locked. 211208990Salc * 212208645Salc * PG_WRITEABLE is set exclusively on managed pages by pmap_enter(). When it 213208645Salc * does so, the page must be VPO_BUSY. 2141541Srgrimes */ 215172317Salc#define PG_CACHED 0x0001 /* page is cached */ 216170816Salc#define PG_FREE 0x0002 /* page is free */ 21770374Sdillon#define PG_WINATCFLS 0x0004 /* flush dirty page on inactive q */ 21842957Sdillon#define PG_FICTITIOUS 0x0008 /* physical page doesn't exist (O) */ 21942957Sdillon#define PG_WRITEABLE 0x0010 /* page is mapped writeable */ 22042957Sdillon#define PG_ZERO 0x0040 /* page is zeroed */ 22142957Sdillon#define PG_REFERENCED 0x0080 /* page has been referenced */ 22261081Sdillon#define PG_UNMANAGED 0x0800 /* No PV management for page */ 22368885Sdillon#define PG_MARKER 0x1000 /* special queue marker page */ 224103531Sjeff#define PG_SLAB 0x2000 /* object pointer is actually a slab */ 2251541Srgrimes 2269507Sdg/* 2279507Sdg * Misc constants. 2289507Sdg */ 2299507Sdg#define ACT_DECLINE 1 2309507Sdg#define ACT_ADVANCE 3 23116750Sdyson#define ACT_INIT 5 23218169Sdyson#define ACT_MAX 64 2339507Sdg 23455206Speter#ifdef _KERNEL 235169291Salc 236169291Salc#include <vm/vm_param.h> 237169291Salc 2381541Srgrimes/* 239171420Salc * Each pageable resident page falls into one of five lists: 2401541Srgrimes * 2415455Sdg * free 2421541Srgrimes * Available for allocation now. 2439507Sdg * 2449507Sdg * cache 245172317Salc * Almost available for allocation. Still associated with 246172317Salc * an object, but clean and immediately freeable. 2479507Sdg * 248171420Salc * hold 249171420Salc * Will become free after a pending I/O operation 250171420Salc * completes. 251171420Salc * 252171420Salc * The following lists are LRU sorted: 253171420Salc * 2541541Srgrimes * inactive 25513765Smpp * Low activity, candidates for reclamation. 2561541Srgrimes * This is the list of pages that should be 2571541Srgrimes * paged out next. 2589507Sdg * 2591541Srgrimes * active 2609507Sdg * Pages that are "active" i.e. they have been 2619507Sdg * recently referenced. 26210544Sdyson * 2631541Srgrimes */ 2641541Srgrimes 26512767Sdysonextern int vm_page_zero_count; 26612767Sdyson 2675455Sdgextern vm_page_t vm_page_array; /* First resident page in table */ 26860755Speterextern int vm_page_array_size; /* number of vm_page_t's */ 2695455Sdgextern long first_page; /* first physical page number */ 2701541Srgrimes 271170816Salc#define VM_PAGE_IS_FREE(m) (((m)->flags & PG_FREE) != 0) 272170816Salc 2731541Srgrimes#define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) 2741541Srgrimes 275170816Salcvm_page_t vm_phys_paddr_to_vm_page(vm_paddr_t pa); 276170816Salc 277169291Salcstatic __inline vm_page_t PHYS_TO_VM_PAGE(vm_paddr_t pa); 2781541Srgrimes 279169291Salcstatic __inline vm_page_t 280169291SalcPHYS_TO_VM_PAGE(vm_paddr_t pa) 281169291Salc{ 282169291Salc#ifdef VM_PHYSSEG_SPARSE 283170816Salc return (vm_phys_paddr_to_vm_page(pa)); 284169291Salc#elif defined(VM_PHYSSEG_DENSE) 285169291Salc return (&vm_page_array[atop(pa) - first_page]); 286169291Salc#else 287169291Salc#error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined." 288169291Salc#endif 289169291Salc} 290169291Salc 291197750Salcextern struct vpglocks vm_page_queue_lock; 292197750Salc 293197750Salc#define vm_page_queue_mtx vm_page_queue_lock.data 29499927Salc#define vm_page_lock_queues() mtx_lock(&vm_page_queue_mtx) 29599927Salc#define vm_page_unlock_queues() mtx_unlock(&vm_page_queue_mtx) 2961541Srgrimes 2975455Sdg#if PAGE_SIZE == 4096 298119356Smarcel#define VM_PAGE_BITS_ALL 0xffu 299119356Smarcel#elif PAGE_SIZE == 8192 300119356Smarcel#define VM_PAGE_BITS_ALL 0xffffu 301119356Smarcel#elif PAGE_SIZE == 16384 302119356Smarcel#define VM_PAGE_BITS_ALL 0xffffffffu 303119356Smarcel#elif PAGE_SIZE == 32768 304119356Smarcel#define VM_PAGE_BITS_ALL 0xfffffffffffffffflu 3055455Sdg#endif 3061541Srgrimes 307100276Salc/* page allocation classes: */ 30833109Sdyson#define VM_ALLOC_NORMAL 0 30933109Sdyson#define VM_ALLOC_INTERRUPT 1 31033109Sdyson#define VM_ALLOC_SYSTEM 2 311100276Salc#define VM_ALLOC_CLASS_MASK 3 312100276Salc/* page allocation flags: */ 313106276Sjeff#define VM_ALLOC_WIRED 0x0020 /* non pageable */ 314106276Sjeff#define VM_ALLOC_ZERO 0x0040 /* Try to obtain a zeroed page */ 315209792Skib#define VM_ALLOC_RETRY 0x0080 /* Mandatory with vm_page_grab() */ 316106276Sjeff#define VM_ALLOC_NOOBJ 0x0100 /* No associated object */ 317136850Salc#define VM_ALLOC_NOBUSY 0x0200 /* Do not busy the page */ 318172317Salc#define VM_ALLOC_IFCACHED 0x0400 /* Fail if the page is not cached */ 319172317Salc#define VM_ALLOC_IFNOTCACHED 0x0800 /* Fail if the page is cached */ 320209713Skib#define VM_ALLOC_IGN_SBUSY 0x1000 /* vm_page_grab() only */ 3215455Sdg 322209713Skib#define VM_ALLOC_COUNT_SHIFT 16 323209713Skib#define VM_ALLOC_COUNT(count) ((count) << VM_ALLOC_COUNT_SHIFT) 324209713Skib#define VM_ALLOC_COUNT_MASK VM_ALLOC_COUNT(0xffff) 325209713Skib 32679248Sdillonvoid vm_page_flag_set(vm_page_t m, unsigned short bits); 32779248Sdillonvoid vm_page_flag_clear(vm_page_t m, unsigned short bits); 32879248Sdillonvoid vm_page_busy(vm_page_t m); 32979248Sdillonvoid vm_page_flash(vm_page_t m); 33079248Sdillonvoid vm_page_io_start(vm_page_t m); 33179248Sdillonvoid vm_page_io_finish(vm_page_t m); 33279248Sdillonvoid vm_page_hold(vm_page_t mem); 33379248Sdillonvoid vm_page_unhold(vm_page_t mem); 33479248Sdillonvoid vm_page_free(vm_page_t m); 33579248Sdillonvoid vm_page_free_zero(vm_page_t m); 33679248Sdillonvoid vm_page_dirty(vm_page_t m); 33779248Sdillonvoid vm_page_wakeup(vm_page_t m); 33879248Sdillon 33979263Sdillonvoid vm_pageq_remove(vm_page_t m); 34079263Sdillon 34179248Sdillonvoid vm_page_activate (vm_page_t); 34279248Sdillonvm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int); 34379248Sdillonvm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int); 344193126Salcvoid vm_page_cache(vm_page_t); 345172341Salcvoid vm_page_cache_free(vm_object_t, vm_pindex_t, vm_pindex_t); 346172317Salcvoid vm_page_cache_remove(vm_page_t); 347172317Salcvoid vm_page_cache_transfer(vm_object_t, vm_pindex_t, vm_object_t); 34879248Sdillonint vm_page_try_to_cache (vm_page_t); 34979248Sdillonint vm_page_try_to_free (vm_page_t); 350193126Salcvoid vm_page_dontneed(vm_page_t); 35179248Sdillonvoid vm_page_deactivate (vm_page_t); 352209685Skibvm_page_t vm_page_find_least(vm_object_t, vm_pindex_t); 35379248Sdillonvoid vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t); 35479248Sdillonvm_page_t vm_page_lookup (vm_object_t, vm_pindex_t); 355209407Salcvm_page_t vm_page_next(vm_page_t m); 356207410Skmacyint vm_page_pa_tryrelock(pmap_t, vm_paddr_t, vm_paddr_t *); 357209407Salcvm_page_t vm_page_prev(vm_page_t m); 35879248Sdillonvoid vm_page_remove (vm_page_t); 35979248Sdillonvoid vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t); 360177414Salcvoid vm_page_requeue(vm_page_t m); 361192034Salcvoid vm_page_set_valid(vm_page_t m, int base, int size); 362161674Salcvoid vm_page_sleep(vm_page_t m, const char *msg); 363106422Salcvm_page_t vm_page_splay(vm_pindex_t, vm_page_t); 364127868Salcvm_offset_t vm_page_startup(vm_offset_t vaddr); 36579248Sdillonvoid vm_page_unwire (vm_page_t, int); 36679248Sdillonvoid vm_page_wire (vm_page_t); 36779248Sdillonvoid vm_page_set_validclean (vm_page_t, int, int); 36879248Sdillonvoid vm_page_clear_dirty (vm_page_t, int, int); 36979248Sdillonvoid vm_page_set_invalid (vm_page_t, int, int); 37079248Sdillonint vm_page_is_valid (vm_page_t, int, int); 37179248Sdillonvoid vm_page_test_dirty (vm_page_t); 37279248Sdillonint vm_page_bits (int, int); 37345347Sjulianvoid vm_page_zero_invalid(vm_page_t m, boolean_t setvalid); 37443752Sdillonvoid vm_page_free_toq(vm_page_t m); 37582314Spetervoid vm_page_zero_idle_wakeup(void); 37698849Skenvoid vm_page_cowfault (vm_page_t); 377186719Skibint vm_page_cowsetup(vm_page_t); 37898849Skenvoid vm_page_cowclear (vm_page_t); 37998849Sken 380105549Salc/* 381161674Salc * vm_page_sleep_if_busy: 382161674Salc * 383163604Salc * Sleep and release the page queues lock if VPO_BUSY is set or, 384161674Salc * if also_m_busy is TRUE, busy is non-zero. Returns TRUE if the 385161674Salc * thread slept and the page queues lock was released. 386161674Salc * Otherwise, retains the page queues lock and returns FALSE. 387161674Salc * 388161674Salc * The object containing the given page must be locked. 389161674Salc */ 390161674Salcstatic __inline int 391161674Salcvm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg) 392161674Salc{ 393161674Salc 394163604Salc if ((m->oflags & VPO_BUSY) || (also_m_busy && m->busy)) { 395161674Salc vm_page_sleep(m, msg); 396161674Salc return (TRUE); 397161674Salc } 398161674Salc return (FALSE); 399161674Salc} 400161674Salc 401161674Salc/* 402105549Salc * vm_page_undirty: 403105549Salc * 404105549Salc * Set page to not be dirty. Note: does not clear pmap modify bits 405105549Salc */ 406105549Salcstatic __inline void 407105549Salcvm_page_undirty(vm_page_t m) 408105549Salc{ 409105549Salc m->dirty = 0; 410105549Salc} 411105549Salc 41255206Speter#endif /* _KERNEL */ 4135455Sdg#endif /* !_VM_PAGE_ */ 414