vm_page.h revision 98849
15455Sdg/* 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 * 3. All advertising materials mentioning features or use of this software 171541Srgrimes * must display the following acknowledgement: 181541Srgrimes * This product includes software developed by the University of 191541Srgrimes * California, Berkeley and its contributors. 201541Srgrimes * 4. Neither the name of the University nor the names of its contributors 211541Srgrimes * may be used to endorse or promote products derived from this software 221541Srgrimes * without specific prior written permission. 231541Srgrimes * 241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341541Srgrimes * SUCH DAMAGE. 351541Srgrimes * 361817Sdg * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93 371541Srgrimes * 381541Srgrimes * 391541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 401541Srgrimes * All rights reserved. 411541Srgrimes * 421541Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 435455Sdg * 441541Srgrimes * Permission to use, copy, modify and distribute this software and 451541Srgrimes * its documentation is hereby granted, provided that both the copyright 461541Srgrimes * notice and this permission notice appear in all copies of the 471541Srgrimes * software, derivative works or modified versions, and any portions 481541Srgrimes * thereof, and that both notices appear in supporting documentation. 495455Sdg * 505455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 515455Sdg * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 521541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 535455Sdg * 541541Srgrimes * Carnegie Mellon requests users of this software to return to 551541Srgrimes * 561541Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 571541Srgrimes * School of Computer Science 581541Srgrimes * Carnegie Mellon University 591541Srgrimes * Pittsburgh PA 15213-3890 601541Srgrimes * 611541Srgrimes * any improvements or extensions that they make and grant Carnegie the 621541Srgrimes * rights to redistribute these changes. 631817Sdg * 6450477Speter * $FreeBSD: head/sys/vm/vm_page.h 98849 2002-06-26 03:37:47Z ken $ 651541Srgrimes */ 661541Srgrimes 671541Srgrimes/* 681541Srgrimes * Resident memory system definitions. 691541Srgrimes */ 701541Srgrimes 711541Srgrimes#ifndef _VM_PAGE_ 721541Srgrimes#define _VM_PAGE_ 731541Srgrimes 7444754Sjulian#if !defined(KLD_MODULE) 7537282Sjmg#include "opt_vmpage.h" 7644754Sjulian#endif 7737282Sjmg 786816Sdg#include <vm/pmap.h> 7938517Sdfr 801541Srgrimes/* 811541Srgrimes * Management of resident (logical) pages. 821541Srgrimes * 831541Srgrimes * A small structure is kept for each resident 841541Srgrimes * page, indexed by page number. Each structure 851541Srgrimes * is an element of several lists: 861541Srgrimes * 871541Srgrimes * A hash table bucket used to quickly 881541Srgrimes * perform object/offset lookups 891541Srgrimes * 901541Srgrimes * A list of all pages for a given object, 911541Srgrimes * so they can be quickly deactivated at 921541Srgrimes * time of deallocation. 931541Srgrimes * 941541Srgrimes * An ordered list of pages due for pageout. 951541Srgrimes * 961541Srgrimes * In addition, the structure contains the object 971541Srgrimes * and offset to which this page belongs (for pageout), 981541Srgrimes * and sundry status bits. 991541Srgrimes * 1001541Srgrimes * Fields in this structure are locked either by the lock on the 1011541Srgrimes * object that the page belongs to (O) or by the lock on the page 1021541Srgrimes * queues (P). 10346349Salc * 10446349Salc * The 'valid' and 'dirty' fields are distinct. A page may have dirty 10546349Salc * bits set without having associated valid bits set. This is used by 10646349Salc * NFS to implement piecemeal writes. 1071541Srgrimes */ 1081541Srgrimes 10960938SjakeTAILQ_HEAD(pglist, vm_page); 1101541Srgrimes 1111541Srgrimesstruct vm_page { 11260938Sjake TAILQ_ENTRY(vm_page) pageq; /* queue info for FIFO queue or free list (P) */ 11342957Sdillon struct vm_page *hnext; /* hash table link (O,P) */ 11460938Sjake TAILQ_ENTRY(vm_page) listq; /* pages in same object (O) */ 1151541Srgrimes 11642957Sdillon vm_object_t object; /* which object am I in (O,P)*/ 11712767Sdyson vm_pindex_t pindex; /* offset into object (O,P) */ 1185455Sdg vm_offset_t phys_addr; /* physical address of page */ 11960755Speter struct md_page md; /* machine dependant stuff */ 12018169Sdyson u_short queue; /* page queue index */ 12118169Sdyson u_short flags, /* see below */ 12218169Sdyson pc; /* page color */ 1235455Sdg u_short wire_count; /* wired down maps refs (P) */ 1245455Sdg short hold_count; /* page hold count */ 12513490Sdyson u_char act_count; /* page usage count */ 12613490Sdyson u_char busy; /* page busy count */ 12713490Sdyson /* NOTE that these must support one bit per DEV_BSIZE in a page!!! */ 12813490Sdyson /* so, on normal X86 kernels, they must be at least 8 bits wide */ 12936735Sdfr#if PAGE_SIZE == 4096 13013490Sdyson u_char valid; /* map of valid DEV_BSIZE chunks */ 13113490Sdyson u_char dirty; /* map of dirty DEV_BSIZE chunks */ 13236735Sdfr#elif PAGE_SIZE == 8192 13336735Sdfr u_short valid; /* map of valid DEV_BSIZE chunks */ 13436735Sdfr u_short dirty; /* map of dirty DEV_BSIZE chunks */ 13536735Sdfr#endif 13698849Sken u_int cow; /* page cow mapping count */ 1371541Srgrimes}; 1381541Srgrimes 13918169Sdyson/* 14051337Sdillon * note: currently use SWAPBLK_NONE as an absolute value rather then 14151337Sdillon * a flag bit. 14242957Sdillon */ 14342957Sdillon 14442957Sdillon#define SWAPBLK_MASK ((daddr_t)((u_daddr_t)-1 >> 1)) /* mask */ 14542957Sdillon#define SWAPBLK_NONE ((daddr_t)((u_daddr_t)SWAPBLK_MASK + 1))/* flag */ 14642957Sdillon 14744754Sjulian#if !defined(KLD_MODULE) 14842957Sdillon/* 14918169Sdyson * Page coloring parameters 15018169Sdyson */ 15143752Sdillon/* Each of PQ_FREE, and PQ_CACHE have PQ_HASH_SIZE entries */ 15218169Sdyson 15362568Sjhb/* Backward compatibility for existing PQ_*CACHE config options. */ 15462568Sjhb#if !defined(PQ_CACHESIZE) 15536326Sdyson#if defined(PQ_HUGECACHE) 15662568Sjhb#define PQ_CACHESIZE 1024 15762941Salfred#elif defined(PQ_LARGECACHE) 15862568Sjhb#define PQ_CACHESIZE 512 15962941Salfred#elif defined(PQ_MEDIUMCACHE) 16062568Sjhb#define PQ_CACHESIZE 256 16162941Salfred#elif defined(PQ_NORMALCACHE) 16262568Sjhb#define PQ_CACHESIZE 64 16362941Salfred#elif defined(PQ_NOOPT) 16462568Sjhb#define PQ_CACHESIZE 0 16562568Sjhb#else 16662568Sjhb#define PQ_CACHESIZE 128 16762568Sjhb#endif 16892029Seivind#endif /* !defined(PQ_CACHESIZE) */ 16962568Sjhb 17062568Sjhb#if PQ_CACHESIZE >= 1024 17136326Sdyson#define PQ_PRIME1 31 /* Prime number somewhat less than PQ_HASH_SIZE */ 17236326Sdyson#define PQ_PRIME2 23 /* Prime number somewhat less than PQ_HASH_SIZE */ 17336326Sdyson#define PQ_L2_SIZE 256 /* A number of colors opt for 1M cache */ 17436326Sdyson 17562941Salfred#elif PQ_CACHESIZE >= 512 17618169Sdyson#define PQ_PRIME1 31 /* Prime number somewhat less than PQ_HASH_SIZE */ 17718169Sdyson#define PQ_PRIME2 23 /* Prime number somewhat less than PQ_HASH_SIZE */ 17818169Sdyson#define PQ_L2_SIZE 128 /* A number of colors opt for 512K cache */ 17918169Sdyson 18062941Salfred#elif PQ_CACHESIZE >= 256 18162568Sjhb#define PQ_PRIME1 13 /* Prime number somewhat less than PQ_HASH_SIZE */ 18262568Sjhb#define PQ_PRIME2 7 /* Prime number somewhat less than PQ_HASH_SIZE */ 18362568Sjhb#define PQ_L2_SIZE 64 /* A number of colors opt for 256K cache */ 18418169Sdyson 18562941Salfred#elif PQ_CACHESIZE >= 128 18662568Sjhb#define PQ_PRIME1 9 /* Produces a good PQ_L2_SIZE/3 + PQ_PRIME1 */ 18762568Sjhb#define PQ_PRIME2 5 /* Prime number somewhat less than PQ_HASH_SIZE */ 18862568Sjhb#define PQ_L2_SIZE 32 /* A number of colors opt for 128k cache */ 18918169Sdyson 19062941Salfred#elif PQ_CACHESIZE >= 64 19118169Sdyson#define PQ_PRIME1 5 /* Prime number somewhat less than PQ_HASH_SIZE */ 19218169Sdyson#define PQ_PRIME2 3 /* Prime number somewhat less than PQ_HASH_SIZE */ 19318169Sdyson#define PQ_L2_SIZE 16 /* A reasonable number of colors (opt for 64K cache) */ 19418169Sdyson 19562568Sjhb#else 19662568Sjhb#define PQ_PRIME1 1 /* Disable page coloring. */ 19762568Sjhb#define PQ_PRIME2 1 19862568Sjhb#define PQ_L2_SIZE 1 19918779Sdyson 20049666Salc#endif 20149666Salc 20218169Sdyson#define PQ_L2_MASK (PQ_L2_SIZE - 1) 20318169Sdyson 20449813Smjacob#define PQ_NONE 0 20549813Smjacob#define PQ_FREE 1 20649813Smjacob#define PQ_INACTIVE (1 + 1*PQ_L2_SIZE) 20749813Smjacob#define PQ_ACTIVE (2 + 1*PQ_L2_SIZE) 20849813Smjacob#define PQ_CACHE (3 + 1*PQ_L2_SIZE) 20990944Stegge#define PQ_HOLD (3 + 2*PQ_L2_SIZE) 21090944Stegge#define PQ_COUNT (4 + 2*PQ_L2_SIZE) 21113490Sdyson 21252647Salcstruct vpgqueues { 21352647Salc struct pglist pl; 21418169Sdyson int *cnt; 21549326Salc int lcnt; 21652647Salc}; 21718169Sdyson 21852647Salcextern struct vpgqueues vm_page_queues[PQ_COUNT]; 21952647Salc 22092029Seivind#endif /* !defined(KLD_MODULE) */ 22144754Sjulian 2221541Srgrimes/* 2231541Srgrimes * These are the flags defined for vm_page. 2241541Srgrimes * 2251541Srgrimes * Note: PG_FILLED and PG_DIRTY are added for the filesystems. 22661081Sdillon * 22761081Sdillon * Note: PG_UNMANAGED (used by OBJT_PHYS) indicates that the page is 22861081Sdillon * not under PV management but otherwise should be treated as a 22961081Sdillon * normal page. Pages not under PV management cannot be paged out 23061081Sdillon * via the object/vm_page_t because there is no knowledge of their 23161081Sdillon * pte mappings, nor can they be removed from their objects via 23261081Sdillon * the object, and such pages are also not on any PQ queue. 2331541Srgrimes */ 23442957Sdillon#define PG_BUSY 0x0001 /* page is in transit (O) */ 23542957Sdillon#define PG_WANTED 0x0002 /* someone is waiting for page (O) */ 23670374Sdillon#define PG_WINATCFLS 0x0004 /* flush dirty page on inactive q */ 23742957Sdillon#define PG_FICTITIOUS 0x0008 /* physical page doesn't exist (O) */ 23842957Sdillon#define PG_WRITEABLE 0x0010 /* page is mapped writeable */ 23942957Sdillon#define PG_MAPPED 0x0020 /* page is mapped */ 24042957Sdillon#define PG_ZERO 0x0040 /* page is zeroed */ 24142957Sdillon#define PG_REFERENCED 0x0080 /* page has been referenced */ 24242957Sdillon#define PG_CLEANCHK 0x0100 /* page will be checked for cleaning */ 24342957Sdillon#define PG_SWAPINPROG 0x0200 /* swap I/O in progress on page */ 24454467Sdillon#define PG_NOSYNC 0x0400 /* do not collect for syncer */ 24561081Sdillon#define PG_UNMANAGED 0x0800 /* No PV management for page */ 24668885Sdillon#define PG_MARKER 0x1000 /* special queue marker page */ 2471541Srgrimes 2489507Sdg/* 2499507Sdg * Misc constants. 2509507Sdg */ 2519507Sdg#define ACT_DECLINE 1 2529507Sdg#define ACT_ADVANCE 3 25316750Sdyson#define ACT_INIT 5 25418169Sdyson#define ACT_MAX 64 2559507Sdg#define PFCLUSTER_BEHIND 3 2569507Sdg#define PFCLUSTER_AHEAD 3 2579507Sdg 25855206Speter#ifdef _KERNEL 2591541Srgrimes/* 2609507Sdg * Each pageable resident page falls into one of four lists: 2611541Srgrimes * 2625455Sdg * free 2631541Srgrimes * Available for allocation now. 2649507Sdg * 2659507Sdg * The following are all LRU sorted: 2669507Sdg * 2679507Sdg * cache 2689507Sdg * Almost available for allocation. Still in an 2699507Sdg * object, but clean and immediately freeable at 2709507Sdg * non-interrupt times. 2719507Sdg * 2721541Srgrimes * inactive 27313765Smpp * Low activity, candidates for reclamation. 2741541Srgrimes * This is the list of pages that should be 2751541Srgrimes * paged out next. 2769507Sdg * 2771541Srgrimes * active 2789507Sdg * Pages that are "active" i.e. they have been 2799507Sdg * recently referenced. 28010544Sdyson * 28110544Sdyson * zero 28210544Sdyson * Pages that are really free and have been pre-zeroed 28310544Sdyson * 2841541Srgrimes */ 2851541Srgrimes 28612767Sdysonextern int vm_page_zero_count; 28712767Sdyson 2885455Sdgextern vm_page_t vm_page_array; /* First resident page in table */ 28960755Speterextern int vm_page_array_size; /* number of vm_page_t's */ 2905455Sdgextern long first_page; /* first physical page number */ 2911541Srgrimes 2921541Srgrimes#define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) 2931541Srgrimes 2941541Srgrimes#define PHYS_TO_VM_PAGE(pa) \ 2951541Srgrimes (&vm_page_array[atop(pa) - first_page ]) 2961541Srgrimes 2971541Srgrimes 2985455Sdg#if PAGE_SIZE == 4096 2995455Sdg#define VM_PAGE_BITS_ALL 0xff 3005455Sdg#endif 3011541Srgrimes 3025455Sdg#if PAGE_SIZE == 8192 3035455Sdg#define VM_PAGE_BITS_ALL 0xffff 3045455Sdg#endif 3051549Srgrimes 30633109Sdyson#define VM_ALLOC_NORMAL 0 30733109Sdyson#define VM_ALLOC_INTERRUPT 1 30833109Sdyson#define VM_ALLOC_SYSTEM 2 30998823Sjeff#define VM_ALLOC_ZERO 0x40 31033109Sdyson#define VM_ALLOC_RETRY 0x80 3115455Sdg 31279248Sdillonvoid vm_page_flag_set(vm_page_t m, unsigned short bits); 31379248Sdillonvoid vm_page_flag_clear(vm_page_t m, unsigned short bits); 31479248Sdillonvoid vm_page_busy(vm_page_t m); 31579248Sdillonvoid vm_page_flash(vm_page_t m); 31679248Sdillonvoid vm_page_io_start(vm_page_t m); 31779248Sdillonvoid vm_page_io_finish(vm_page_t m); 31879248Sdillonvoid vm_page_hold(vm_page_t mem); 31979248Sdillonvoid vm_page_unhold(vm_page_t mem); 32079248Sdillonvoid vm_page_protect(vm_page_t mem, int prot); 32179248Sdillonboolean_t vm_page_zero_fill(vm_page_t m); 32295598Speterboolean_t vm_page_zero_fill_area(vm_page_t m, int off, int len); 32379248Sdillonvoid vm_page_copy(vm_page_t src_m, vm_page_t dest_m); 32479248Sdillonvoid vm_page_free(vm_page_t m); 32579248Sdillonvoid vm_page_free_zero(vm_page_t m); 32679248Sdillonint vm_page_sleep_busy(vm_page_t m, int also_m_busy, const char *msg); 32779248Sdillonvoid vm_page_dirty(vm_page_t m); 32879248Sdillonvoid vm_page_undirty(vm_page_t m); 32979248Sdillonvoid vm_page_wakeup(vm_page_t m); 33079248Sdillon 33179263Sdillonvoid vm_pageq_init(void); 33279263Sdillonstruct vpgqueues *vm_pageq_aquire(int queue); 33379263Sdillonvoid vm_pageq_release(struct vpgqueues *vpq); 33479263Sdillonvm_page_t vm_pageq_add_new_page(vm_offset_t pa); 33591641Salcvoid vm_pageq_enqueue(int queue, vm_page_t m); 33679263Sdillonvoid vm_pageq_remove_nowakeup(vm_page_t m); 33779263Sdillonvoid vm_pageq_remove(vm_page_t m); 33879263Sdillonvm_page_t vm_pageq_find(int basequeue, int index, boolean_t prefer_zero); 33979263Sdillonvoid vm_pageq_requeue(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); 34479248Sdillonvoid vm_page_cache (register vm_page_t); 34579248Sdillonint vm_page_try_to_cache (vm_page_t); 34679248Sdillonint vm_page_try_to_free (vm_page_t); 34779248Sdillonvoid vm_page_dontneed (register vm_page_t); 34879248Sdillonvoid vm_page_deactivate (vm_page_t); 34979248Sdillonvoid vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t); 35079248Sdillonvm_page_t vm_page_lookup (vm_object_t, vm_pindex_t); 35179248Sdillonvoid vm_page_remove (vm_page_t); 35279248Sdillonvoid vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t); 35379248Sdillonvm_offset_t vm_page_startup (vm_offset_t, vm_offset_t, vm_offset_t); 35479248Sdillonvoid vm_page_unmanage (vm_page_t); 35579248Sdillonvoid vm_page_unwire (vm_page_t, int); 35679248Sdillonvoid vm_page_wire (vm_page_t); 35779248Sdillonvoid vm_page_set_validclean (vm_page_t, int, int); 35879248Sdillonvoid vm_page_set_dirty (vm_page_t, int, int); 35979248Sdillonvoid vm_page_clear_dirty (vm_page_t, int, int); 36079248Sdillonvoid vm_page_set_invalid (vm_page_t, int, int); 36179248Sdillonint vm_page_is_valid (vm_page_t, int, int); 36279248Sdillonvoid vm_page_test_dirty (vm_page_t); 36379248Sdillonint vm_page_bits (int, int); 36445347Sjulianvoid vm_page_zero_invalid(vm_page_t m, boolean_t setvalid); 36543752Sdillonvoid vm_page_free_toq(vm_page_t m); 36682314Spetervoid vm_page_zero_idle_wakeup(void); 36798849Skenvoid vm_page_cowfault (vm_page_t); 36898849Skenvoid vm_page_cowsetup (vm_page_t); 36998849Skenvoid vm_page_cowclear (vm_page_t); 37098849Sken 37155206Speter#endif /* _KERNEL */ 3725455Sdg#endif /* !_VM_PAGE_ */ 373