vm_page.h revision 18779
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 *
6418779Sdyson * $Id: vm_page.h,v 1.32 1996/09/08 20:44:46 dyson Exp $
651541Srgrimes */
661541Srgrimes
671541Srgrimes/*
681541Srgrimes *	Resident memory system definitions.
691541Srgrimes */
701541Srgrimes
711541Srgrimes#ifndef	_VM_PAGE_
721541Srgrimes#define	_VM_PAGE_
731541Srgrimes
746816Sdg#include <vm/pmap.h>
751541Srgrimes/*
761541Srgrimes *	Management of resident (logical) pages.
771541Srgrimes *
781541Srgrimes *	A small structure is kept for each resident
791541Srgrimes *	page, indexed by page number.  Each structure
801541Srgrimes *	is an element of several lists:
811541Srgrimes *
821541Srgrimes *		A hash table bucket used to quickly
831541Srgrimes *		perform object/offset lookups
841541Srgrimes *
851541Srgrimes *		A list of all pages for a given object,
861541Srgrimes *		so they can be quickly deactivated at
871541Srgrimes *		time of deallocation.
881541Srgrimes *
891541Srgrimes *		An ordered list of pages due for pageout.
901541Srgrimes *
911541Srgrimes *	In addition, the structure contains the object
921541Srgrimes *	and offset to which this page belongs (for pageout),
931541Srgrimes *	and sundry status bits.
941541Srgrimes *
951541Srgrimes *	Fields in this structure are locked either by the lock on the
961541Srgrimes *	object that the page belongs to (O) or by the lock on the page
971541Srgrimes *	queues (P).
981541Srgrimes */
991541Srgrimes
1001541SrgrimesTAILQ_HEAD(pglist, vm_page);
1011541Srgrimes
1021541Srgrimesstruct vm_page {
1035455Sdg	TAILQ_ENTRY(vm_page) pageq;	/* queue info for FIFO queue or free list (P) */
1045455Sdg	TAILQ_ENTRY(vm_page) hashq;	/* hash table links (O) */
1055455Sdg	TAILQ_ENTRY(vm_page) listq;	/* pages in same object (O) */
1061541Srgrimes
1075455Sdg	vm_object_t object;		/* which object am I in (O,P) */
10812767Sdyson	vm_pindex_t pindex;		/* offset into object (O,P) */
1095455Sdg	vm_offset_t phys_addr;		/* physical address of page */
11018169Sdyson	u_short	queue;			/* page queue index */
11118169Sdyson	u_short	flags,			/* see below */
11218169Sdyson		pc;			/* page color */
1135455Sdg	u_short wire_count;		/* wired down maps refs (P) */
1145455Sdg	short hold_count;		/* page hold count */
11513490Sdyson	u_char	act_count;		/* page usage count */
11613490Sdyson	u_char	busy;			/* page busy count */
11713490Sdyson	/* NOTE that these must support one bit per DEV_BSIZE in a page!!! */
11813490Sdyson	/* so, on normal X86 kernels, they must be at least 8 bits wide */
11913490Sdyson	u_char	valid;			/* map of valid DEV_BSIZE chunks */
12013490Sdyson	u_char	dirty;			/* map of dirty DEV_BSIZE chunks */
1211541Srgrimes};
1221541Srgrimes
12318169Sdyson/*
12418169Sdyson * Page coloring parameters
12518169Sdyson */
12618169Sdyson/* Each of PQ_FREE, PQ_ZERO and PQ_CACHE have PQ_HASH_SIZE entries */
12718169Sdyson
12818169Sdyson/* Define one of the following */
12918169Sdyson#if defined(PQ_LARGECACHE)
13018169Sdyson#define PQ_PRIME1 31	/* Prime number somewhat less than PQ_HASH_SIZE */
13118169Sdyson#define PQ_PRIME2 23	/* Prime number somewhat less than PQ_HASH_SIZE */
13218169Sdyson#define PQ_PRIME3 17	/* Prime number somewhat less than PQ_HASH_SIZE */
13318169Sdyson#define PQ_L2_SIZE 128	/* A number of colors opt for 512K cache */
13418169Sdyson#define PQ_L1_SIZE 2	/* Two page L1 cache */
13518169Sdyson#endif
13618169Sdyson
13718169Sdyson
13818169Sdyson/*
13918169Sdyson * Use 'options PQ_NOOPT' to disable page coloring
14018169Sdyson */
14118169Sdyson#if defined(PQ_NOOPT)
14218169Sdyson#define PQ_PRIME1 1
14318169Sdyson#define PQ_PRIME2 1
14418169Sdyson#define PQ_PRIME3 1
14518169Sdyson#define PQ_L2_SIZE 1
14618169Sdyson#define PQ_L1_SIZE 1
14718169Sdyson#endif
14818169Sdyson
14918779Sdyson#if defined(PQ_NORMALCACHE)
15018169Sdyson#define PQ_PRIME1 5	/* Prime number somewhat less than PQ_HASH_SIZE */
15118169Sdyson#define PQ_PRIME2 3	/* Prime number somewhat less than PQ_HASH_SIZE */
15218169Sdyson#define PQ_PRIME3 11	/* Prime number somewhat less than PQ_HASH_SIZE */
15318169Sdyson#define PQ_L2_SIZE 16	/* A reasonable number of colors (opt for 64K cache) */
15418169Sdyson#define PQ_L1_SIZE 2	/* Two page L1 cache */
15518169Sdyson#endif
15618169Sdyson
15718779Sdyson#if defined(PQ_MEDIUMCACHE) || !defined(PQ_L2_SIZE)
15818779Sdyson#define PQ_PRIME1 13	/* Prime number somewhat less than PQ_HASH_SIZE */
15918779Sdyson#define PQ_PRIME2 7	/* Prime number somewhat less than PQ_HASH_SIZE */
16018779Sdyson#define PQ_PRIME3 5	/* Prime number somewhat less than PQ_HASH_SIZE */
16118779Sdyson#define PQ_L2_SIZE 64	/* A number of colors opt for 256K cache */
16218779Sdyson#define PQ_L1_SIZE 2	/* Two page L1 cache */
16318779Sdyson#endif
16418779Sdyson
16518169Sdyson#define PQ_L2_MASK (PQ_L2_SIZE - 1)
16618169Sdyson
16713490Sdyson#define PQ_NONE 0
16813490Sdyson#define PQ_FREE	1
16918169Sdyson#define PQ_ZERO (1 + PQ_L2_SIZE)
17018169Sdyson#define PQ_INACTIVE (1 + 2*PQ_L2_SIZE)
17118169Sdyson#define PQ_ACTIVE (2 + 2*PQ_L2_SIZE)
17218169Sdyson#define PQ_CACHE (3 + 2*PQ_L2_SIZE)
17318169Sdyson#define PQ_COUNT (3 + 3*PQ_L2_SIZE)
17413490Sdyson
17518169Sdysonextern struct vpgqueues {
17618169Sdyson	struct pglist *pl;
17718169Sdyson	int	*cnt;
17818169Sdyson	int	*lcnt;
17918169Sdyson} vm_page_queues[PQ_COUNT];
18018169Sdyson
1811541Srgrimes/*
1821541Srgrimes * These are the flags defined for vm_page.
1831541Srgrimes *
1841541Srgrimes * Note: PG_FILLED and PG_DIRTY are added for the filesystems.
1851541Srgrimes */
18613490Sdyson#define	PG_BUSY		0x01		/* page is in transit (O) */
18713490Sdyson#define	PG_WANTED	0x02		/* someone is waiting for page (O) */
18813490Sdyson#define	PG_TABLED	0x04		/* page is in VP table (O) */
18913490Sdyson#define	PG_FICTITIOUS	0x08		/* physical page doesn't exist (O) */
19013490Sdyson#define	PG_WRITEABLE	0x10		/* page is mapped writeable */
19113490Sdyson#define PG_MAPPED	0x20		/* page is mapped */
19213490Sdyson#define	PG_ZERO		0x40		/* page is zeroed */
19313490Sdyson#define PG_REFERENCED	0x80		/* page has been referenced */
19413490Sdyson#define PG_CLEANCHK	0x100		/* page has been checked for cleaning */
1951541Srgrimes
1969507Sdg/*
1979507Sdg * Misc constants.
1989507Sdg */
1991541Srgrimes
2009507Sdg#define ACT_DECLINE		1
2019507Sdg#define ACT_ADVANCE		3
20216750Sdyson#define ACT_INIT		5
20318169Sdyson#define ACT_MAX			64
2049507Sdg#define PFCLUSTER_BEHIND	3
2059507Sdg#define PFCLUSTER_AHEAD		3
2069507Sdg
2071541Srgrimes#ifdef KERNEL
2081541Srgrimes/*
2099507Sdg * Each pageable resident page falls into one of four lists:
2101541Srgrimes *
2115455Sdg *	free
2121541Srgrimes *		Available for allocation now.
2139507Sdg *
2149507Sdg * The following are all LRU sorted:
2159507Sdg *
2169507Sdg *	cache
2179507Sdg *		Almost available for allocation. Still in an
2189507Sdg *		object, but clean and immediately freeable at
2199507Sdg *		non-interrupt times.
2209507Sdg *
2211541Srgrimes *	inactive
22213765Smpp *		Low activity, candidates for reclamation.
2231541Srgrimes *		This is the list of pages that should be
2241541Srgrimes *		paged out next.
2259507Sdg *
2261541Srgrimes *	active
2279507Sdg *		Pages that are "active" i.e. they have been
2289507Sdg *		recently referenced.
22910544Sdyson *
23010544Sdyson *	zero
23110544Sdyson *		Pages that are really free and have been pre-zeroed
23210544Sdyson *
2331541Srgrimes */
2341541Srgrimes
23518169Sdysonextern struct pglist vm_page_queue_free[PQ_L2_SIZE];/* memory free queue */
23618169Sdysonextern struct pglist vm_page_queue_zero[PQ_L2_SIZE];/* zeroed memory free queue */
2375455Sdgextern struct pglist vm_page_queue_active;	/* active memory queue */
2385455Sdgextern struct pglist vm_page_queue_inactive;	/* inactive memory queue */
23918169Sdysonextern struct pglist vm_page_queue_cache[PQ_L2_SIZE];/* cache memory queue */
2401541Srgrimes
24112767Sdysonextern int vm_page_zero_count;
24212767Sdyson
2435455Sdgextern vm_page_t vm_page_array;		/* First resident page in table */
2445455Sdgextern long first_page;			/* first physical page number */
2451541Srgrimes
2465455Sdg /* ... represented in vm_page_array */
2475455Sdgextern long last_page;			/* last physical page number */
2485455Sdg
2495455Sdg /* ... represented in vm_page_array */
2505455Sdg /* [INCLUSIVE] */
2515455Sdgextern vm_offset_t first_phys_addr;	/* physical address for first_page */
2525455Sdgextern vm_offset_t last_phys_addr;	/* physical address for last_page */
2535455Sdg
2541541Srgrimes#define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
2551541Srgrimes
2561541Srgrimes#define IS_VM_PHYSADDR(pa) \
2571541Srgrimes		((pa) >= first_phys_addr && (pa) <= last_phys_addr)
2581541Srgrimes
2591541Srgrimes#define PHYS_TO_VM_PAGE(pa) \
2601541Srgrimes		(&vm_page_array[atop(pa) - first_page ])
2611541Srgrimes
2621541Srgrimes/*
2631541Srgrimes *	Functions implemented as macros
2641541Srgrimes */
2651541Srgrimes
2661541Srgrimes#define PAGE_ASSERT_WAIT(m, interruptible)	{ \
2671541Srgrimes				(m)->flags |= PG_WANTED; \
2681541Srgrimes				assert_wait((int) (m), (interruptible)); \
2691541Srgrimes			}
2701541Srgrimes
2711541Srgrimes#define PAGE_WAKEUP(m)	{ \
2721541Srgrimes				(m)->flags &= ~PG_BUSY; \
2731541Srgrimes				if ((m)->flags & PG_WANTED) { \
2741541Srgrimes					(m)->flags &= ~PG_WANTED; \
2751549Srgrimes					wakeup((caddr_t) (m)); \
2761541Srgrimes				} \
2771541Srgrimes			}
2781541Srgrimes
2795455Sdg#if PAGE_SIZE == 4096
2805455Sdg#define VM_PAGE_BITS_ALL 0xff
2815455Sdg#endif
2821541Srgrimes
2835455Sdg#if PAGE_SIZE == 8192
2845455Sdg#define VM_PAGE_BITS_ALL 0xffff
2855455Sdg#endif
2861549Srgrimes
2875841Sdg#define VM_ALLOC_NORMAL 0
2885841Sdg#define VM_ALLOC_INTERRUPT 1
2895841Sdg#define VM_ALLOC_SYSTEM 2
29013490Sdyson#define	VM_ALLOC_ZERO	3
2915455Sdg
2925455Sdgvoid vm_page_activate __P((vm_page_t));
29312767Sdysonvm_page_t vm_page_alloc __P((vm_object_t, vm_pindex_t, int));
2946357Sphkvoid vm_page_cache __P((register vm_page_t));
29515811Sdysonstatic __inline void vm_page_copy __P((vm_page_t, vm_page_t));
2965455Sdgvoid vm_page_deactivate __P((vm_page_t));
2975455Sdgvoid vm_page_free __P((vm_page_t));
29816122Sdysonvoid vm_page_free_zero __P((vm_page_t));
29912767Sdysonvoid vm_page_insert __P((vm_page_t, vm_object_t, vm_pindex_t));
30012767Sdysonvm_page_t vm_page_lookup __P((vm_object_t, vm_pindex_t));
3015455Sdgvoid vm_page_remove __P((vm_page_t));
30212767Sdysonvoid vm_page_rename __P((vm_page_t, vm_object_t, vm_pindex_t));
3035455Sdgvm_offset_t vm_page_startup __P((vm_offset_t, vm_offset_t, vm_offset_t));
3045455Sdgvoid vm_page_unwire __P((vm_page_t));
3055455Sdgvoid vm_page_wire __P((vm_page_t));
30617334Sdysonvoid vm_page_unqueue __P((vm_page_t));
30717334Sdysonvoid vm_page_unqueue_nowakeup __P((vm_page_t));
30810544Sdysonvoid vm_page_set_validclean __P((vm_page_t, int, int));
3095455Sdgvoid vm_page_set_invalid __P((vm_page_t, int, int));
31015811Sdysonstatic __inline boolean_t vm_page_zero_fill __P((vm_page_t));
3115455Sdgint vm_page_is_valid __P((vm_page_t, int, int));
3125455Sdgvoid vm_page_test_dirty __P((vm_page_t));
3136357Sphkint vm_page_bits __P((int, int));
31418169Sdysonvm_page_t vm_page_list_find __P((int, int));
31518169Sdysonint vm_page_queue_index __P((vm_offset_t, int));
31618169Sdysonvm_page_t vm_page_select __P((vm_object_t, vm_pindex_t, int));
3175455Sdg
3181549Srgrimes/*
3191549Srgrimes * Keep page from being freed by the page daemon
3201549Srgrimes * much of the same effect as wiring, except much lower
3211549Srgrimes * overhead and should be used only for *very* temporary
3221549Srgrimes * holding ("wiring").
3231549Srgrimes */
3248010Sbdestatic __inline void
3254461Sbdevm_page_hold(vm_page_t mem)
3261549Srgrimes{
3271549Srgrimes	mem->hold_count++;
3281549Srgrimes}
3291549Srgrimes
3303745Swollman#ifdef DIAGNOSTIC
3313745Swollman#include <sys/systm.h>		/* make GCC shut up */
3323745Swollman#endif
3333745Swollman
3348010Sbdestatic __inline void
3354461Sbdevm_page_unhold(vm_page_t mem)
3361549Srgrimes{
3373660Sdg#ifdef DIAGNOSTIC
3385455Sdg	if (--mem->hold_count < 0)
3391549Srgrimes		panic("vm_page_unhold: hold count < 0!!!");
3403660Sdg#else
3413660Sdg	--mem->hold_count;
3423660Sdg#endif
3431549Srgrimes}
3441549Srgrimes
3458010Sbdestatic __inline void
3466816Sdgvm_page_protect(vm_page_t mem, int prot)
3476816Sdg{
3486816Sdg	if (prot == VM_PROT_NONE) {
3496816Sdg		if (mem->flags & (PG_WRITEABLE|PG_MAPPED)) {
35017334Sdyson			pmap_page_protect(VM_PAGE_TO_PHYS(mem), prot);
3516816Sdg			mem->flags &= ~(PG_WRITEABLE|PG_MAPPED);
3526816Sdg		}
3536816Sdg	} else if ((prot == VM_PROT_READ) && (mem->flags & PG_WRITEABLE)) {
35417334Sdyson		pmap_page_protect(VM_PAGE_TO_PHYS(mem), prot);
3556816Sdg		mem->flags &= ~PG_WRITEABLE;
3566816Sdg	}
3576816Sdg}
3586816Sdg
35915811Sdyson/*
36015811Sdyson *	vm_page_zero_fill:
36115811Sdyson *
36215811Sdyson *	Zero-fill the specified page.
36315811Sdyson *	Written as a standard pagein routine, to
36415811Sdyson *	be used by the zero-fill object.
36515811Sdyson */
36615811Sdysonstatic __inline boolean_t
36715811Sdysonvm_page_zero_fill(m)
36815811Sdyson	vm_page_t m;
36915811Sdyson{
37015811Sdyson	pmap_zero_page(VM_PAGE_TO_PHYS(m));
37115811Sdyson	return (TRUE);
37215811Sdyson}
3736816Sdg
37415811Sdyson/*
37515811Sdyson *	vm_page_copy:
37615811Sdyson *
37715811Sdyson *	Copy one page to another
37815811Sdyson */
37915811Sdysonstatic __inline void
38015811Sdysonvm_page_copy(src_m, dest_m)
38115811Sdyson	vm_page_t src_m;
38215811Sdyson	vm_page_t dest_m;
38315811Sdyson{
38415811Sdyson	pmap_copy_page(VM_PAGE_TO_PHYS(src_m), VM_PAGE_TO_PHYS(dest_m));
38515811Sdyson	dest_m->valid = VM_PAGE_BITS_ALL;
38615811Sdyson}
38715811Sdyson
3885455Sdg#endif				/* KERNEL */
3895455Sdg#endif				/* !_VM_PAGE_ */
390