uvm_page.h revision 1.20
1/*	$OpenBSD: uvm_page.h,v 1.20 2002/07/20 22:19:35 art Exp $	*/
2/*	$NetBSD: uvm_page.h,v 1.19 2000/12/28 08:24:55 chs Exp $	*/
3
4/*
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 * Copyright (c) 1991, 1993, The Regents of the University of California.
7 *
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * The Mach Operating System project at Carnegie-Mellon University.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *	This product includes software developed by Charles D. Cranor,
24 *      Washington University, the University of California, Berkeley and
25 *      its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 *    may be used to endorse or promote products derived from this software
28 *    without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 *	@(#)vm_page.h   7.3 (Berkeley) 4/21/91
43 * from: Id: uvm_page.h,v 1.1.2.6 1998/02/04 02:31:42 chuck Exp
44 *
45 *
46 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47 * All rights reserved.
48 *
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
54 *
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 *
59 * Carnegie Mellon requests users of this software to return to
60 *
61 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62 *  School of Computer Science
63 *  Carnegie Mellon University
64 *  Pittsburgh PA 15213-3890
65 *
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
68 */
69
70#ifndef _UVM_UVM_PAGE_H_
71#define _UVM_UVM_PAGE_H_
72
73/*
74 * uvm_page.h
75 */
76
77/*
78 *	Resident memory system definitions.
79 */
80
81/*
82 *	Management of resident (logical) pages.
83 *
84 *	A small structure is kept for each resident
85 *	page, indexed by page number.  Each structure
86 *	is an element of several lists:
87 *
88 *		A hash table bucket used to quickly
89 *		perform object/offset lookups
90 *
91 *		A list of all pages for a given object,
92 *		so they can be quickly deactivated at
93 *		time of deallocation.
94 *
95 *		An ordered list of pages due for pageout.
96 *
97 *	In addition, the structure contains the object
98 *	and offset to which this page belongs (for pageout),
99 *	and sundry status bits.
100 *
101 *	Fields in this structure are locked either by the lock on the
102 *	object that the page belongs to (O) or by the lock on the page
103 *	queues (P) [or both].
104 */
105
106/*
107 * locking note: the mach version of this data structure had bit
108 * fields for the flags, and the bit fields were divided into two
109 * items (depending on who locked what).  some time, in BSD, the bit
110 * fields were dumped and all the flags were lumped into one short.
111 * that is fine for a single threaded uniprocessor OS, but bad if you
112 * want to actual make use of locking (simple_lock's).  so, we've
113 * seperated things back out again.
114 *
115 * note the page structure has no lock of its own.
116 */
117
118#include <uvm/uvm_extern.h>
119#include <uvm/uvm_pglist.h>
120
121struct vm_page {
122	TAILQ_ENTRY(vm_page)	pageq;		/* queue info for FIFO
123						 * queue or free list (P) */
124	TAILQ_ENTRY(vm_page)	hashq;		/* hash table links (O)*/
125	TAILQ_ENTRY(vm_page)	listq;		/* pages in same object (O)*/
126
127	struct vm_anon		*uanon;		/* anon (O,P) */
128	struct uvm_object	*uobject;	/* object (O,P) */
129	voff_t			offset;		/* offset into object (O,P) */
130
131	u_short			flags;		/* object flags [O] */
132	u_short			version;	/* version count [O] */
133	u_short			wire_count;	/* wired down map refs [P] */
134	u_short			pqflags;	/* page queue flags [P] */
135	u_int			loan_count;	/* number of active loans
136						 * to read: [O or P]
137						 * to modify: [O _and_ P] */
138	paddr_t			phys_addr;	/* physical address of page */
139
140#ifdef __HAVE_VM_PAGE_MD
141	struct vm_page_md	mdpage;		/* pmap-specific data */
142#endif
143#if defined(UVM_PAGE_TRKOWN)
144	/* debugging fields to track page ownership */
145	pid_t			owner;		/* proc that set PG_BUSY */
146	char			*owner_tag;	/* why it was set busy */
147#endif
148};
149
150/*
151 * These are the flags defined for vm_page.
152 *
153 * Note: PG_FILLED and PG_DIRTY are added for the filesystems.
154 */
155
156/*
157 * locking rules:
158 *   PG_ ==> locked by object lock
159 *   PQ_ ==> lock by page queue lock
160 *   PQ_FREE is locked by free queue lock and is mutex with all other PQs
161 *
162 * PG_ZERO is used to indicate that a page has been pre-zero'd.  This flag
163 * is only set when the page is on no queues, and is cleared when the page
164 * is placed on the free list.
165 */
166
167#define	PG_BUSY		0x0001		/* page is locked */
168#define	PG_WANTED	0x0002		/* someone is waiting for page */
169#define	PG_TABLED	0x0004		/* page is in VP table  */
170#define	PG_CLEAN	0x0008		/* page has not been modified */
171#define PG_CLEANCHK	0x0010		/* clean bit has been checked */
172#define PG_RELEASED	0x0020		/* page released while paging */
173#define	PG_FAKE		0x0040		/* page is not yet initialized */
174#define PG_RDONLY	0x0080		/* page must be mapped read-only */
175#define PG_ZERO		0x0100		/* page is pre-zero'd */
176
177#define PG_PAGER1	0x1000		/* pager-specific flag */
178
179#define PQ_FREE		0x0001		/* page is on free list */
180#define PQ_INACTIVE	0x0002		/* page is in inactive list */
181#define PQ_ACTIVE	0x0004		/* page is in active list */
182#define PQ_ANON		0x0010		/* page is part of an anon, rather
183					   than an uvm_object */
184#define PQ_AOBJ		0x0020		/* page is part of an anonymous
185					   uvm_object */
186#define PQ_SWAPBACKED	(PQ_ANON|PQ_AOBJ)
187#define	PQ_ENCRYPT	0x0040		/* page needs {en,de}cryption */
188
189/*
190 * physical memory layout structure
191 *
192 * MD vmparam.h must #define:
193 *   VM_PHYSEG_MAX = max number of physical memory segments we support
194 *		   (if this is "1" then we revert to a "contig" case)
195 *   VM_PHYSSEG_STRAT: memory sort/search options (for VM_PHYSEG_MAX > 1)
196 * 	- VM_PSTRAT_RANDOM:   linear search (random order)
197 *	- VM_PSTRAT_BSEARCH:  binary search (sorted by address)
198 *	- VM_PSTRAT_BIGFIRST: linear search (sorted by largest segment first)
199 *      - others?
200 *   XXXCDC: eventually we should purge all left-over global variables...
201 */
202#define VM_PSTRAT_RANDOM	1
203#define VM_PSTRAT_BSEARCH	2
204#define VM_PSTRAT_BIGFIRST	3
205
206/*
207 * vm_physmemseg: describes one segment of physical memory
208 */
209struct vm_physseg {
210	paddr_t	start;			/* PF# of first page in segment */
211	paddr_t	end;			/* (PF# of last page in segment) + 1 */
212	paddr_t	avail_start;		/* PF# of first free page in segment */
213	paddr_t	avail_end;		/* (PF# of last free page in segment) +1  */
214	int	free_list;		/* which free list they belong on */
215	struct	vm_page *pgs;		/* vm_page structures (from start) */
216	struct	vm_page *lastpg;	/* vm_page structure for end */
217#ifdef __HAVE_PMAP_PHYSSEG
218	struct	pmap_physseg pmseg;	/* pmap specific (MD) data */
219#endif
220};
221
222#ifdef _KERNEL
223
224/*
225 * globals
226 */
227
228extern boolean_t vm_page_zero_enable;
229
230/*
231 *	Each pageable resident page falls into one of three lists:
232 *
233 *	free
234 *		Available for allocation now.
235 *	inactive
236 *		Not referenced in any map, but still has an
237 *		object/offset-page mapping, and may be dirty.
238 *		This is the list of pages that should be
239 *		paged out next.
240 *	active
241 *		A list of pages which have been placed in
242 *		at least one physical map.  This list is
243 *		ordered, in LRU-like fashion.
244 */
245
246extern struct pglist	vm_page_queue_free;	/* memory free queue */
247extern struct pglist	vm_page_queue_active;	/* active memory queue */
248extern struct pglist	vm_page_queue_inactive;	/* inactive memory queue */
249
250/*
251 * physical memory config is stored in vm_physmem.
252 */
253
254extern struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];
255extern int vm_nphysseg;
256
257/*
258 * handle inline options
259 */
260
261#ifdef UVM_PAGE_INLINE
262#define PAGE_INLINE static __inline
263#else
264#define PAGE_INLINE /* nothing */
265#endif /* UVM_PAGE_INLINE */
266
267/*
268 * prototypes: the following prototypes define the interface to pages
269 */
270
271void uvm_page_init(vaddr_t *, vaddr_t *);
272#if defined(UVM_PAGE_TRKOWN)
273void uvm_page_own(struct vm_page *, char *);
274#endif
275#if !defined(PMAP_STEAL_MEMORY)
276boolean_t uvm_page_physget(paddr_t *);
277#endif
278void uvm_page_rehash(void);
279void uvm_pageidlezero(void);
280
281PAGE_INLINE int uvm_lock_fpageq(void);
282PAGE_INLINE void uvm_unlock_fpageq(int);
283
284PAGE_INLINE void uvm_pageactivate(struct vm_page *);
285vaddr_t uvm_pageboot_alloc(vsize_t);
286PAGE_INLINE void uvm_pagecopy(struct vm_page *, struct vm_page *);
287PAGE_INLINE void uvm_pagedeactivate(struct vm_page *);
288void uvm_pagefree(struct vm_page *);
289void uvm_page_unbusy(struct vm_page **, int);
290PAGE_INLINE struct vm_page *uvm_pagelookup(struct uvm_object *, voff_t);
291PAGE_INLINE void uvm_pageunwire(struct vm_page *);
292PAGE_INLINE void uvm_pagewait(struct vm_page *, int);
293PAGE_INLINE void uvm_pagewake(struct vm_page *);
294PAGE_INLINE void uvm_pagewire(struct vm_page *);
295PAGE_INLINE void uvm_pagezero(struct vm_page *);
296
297PAGE_INLINE int uvm_page_lookup_freelist(struct vm_page *);
298
299static struct vm_page *PHYS_TO_VM_PAGE(paddr_t);
300static int vm_physseg_find(paddr_t, int *);
301
302/*
303 * macros
304 */
305
306#define uvm_lock_pageq()	simple_lock(&uvm.pageqlock)
307#define uvm_unlock_pageq()	simple_unlock(&uvm.pageqlock)
308
309#define uvm_pagehash(obj,off) \
310	(((unsigned long)obj+(unsigned long)atop(off)) & uvm.page_hashmask)
311
312#define	UVM_PAGEZERO_TARGET	(uvmexp.free)
313
314#define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
315
316/*
317 * when VM_PHYSSEG_MAX is 1, we can simplify these functions
318 */
319
320/*
321 * vm_physseg_find: find vm_physseg structure that belongs to a PA
322 */
323static __inline int
324vm_physseg_find(pframe, offp)
325	paddr_t pframe;
326	int	*offp;
327{
328#if VM_PHYSSEG_MAX == 1
329
330	/* 'contig' case */
331	if (pframe >= vm_physmem[0].start && pframe < vm_physmem[0].end) {
332		if (offp)
333			*offp = pframe - vm_physmem[0].start;
334		return(0);
335	}
336	return(-1);
337
338#elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
339	/* binary search for it */
340	int	start, len, try;
341
342	/*
343	 * if try is too large (thus target is less than than try) we reduce
344	 * the length to trunc(len/2) [i.e. everything smaller than "try"]
345	 *
346	 * if the try is too small (thus target is greater than try) then
347	 * we set the new start to be (try + 1).   this means we need to
348	 * reduce the length to (round(len/2) - 1).
349	 *
350	 * note "adjust" below which takes advantage of the fact that
351	 *  (round(len/2) - 1) == trunc((len - 1) / 2)
352	 * for any value of len we may have
353	 */
354
355	for (start = 0, len = vm_nphysseg ; len != 0 ; len = len / 2) {
356		try = start + (len / 2);	/* try in the middle */
357
358		/* start past our try? */
359		if (pframe >= vm_physmem[try].start) {
360			/* was try correct? */
361			if (pframe < vm_physmem[try].end) {
362				if (offp)
363					*offp = pframe - vm_physmem[try].start;
364				return(try);            /* got it */
365			}
366			start = try + 1;	/* next time, start here */
367			len--;			/* "adjust" */
368		} else {
369			/*
370			 * pframe before try, just reduce length of
371			 * region, done in "for" loop
372			 */
373		}
374	}
375	return(-1);
376
377#else
378	/* linear search for it */
379	int	lcv;
380
381	for (lcv = 0; lcv < vm_nphysseg; lcv++) {
382		if (pframe >= vm_physmem[lcv].start &&
383		    pframe < vm_physmem[lcv].end) {
384			if (offp)
385				*offp = pframe - vm_physmem[lcv].start;
386			return(lcv);		   /* got it */
387		}
388	}
389	return(-1);
390
391#endif
392}
393
394
395/*
396 * IS_VM_PHYSADDR: only used my mips/pmax/pica trap/pmap.
397 */
398
399#define IS_VM_PHYSADDR(PA) (vm_physseg_find(atop(PA), NULL) != -1)
400
401/*
402 * PHYS_TO_VM_PAGE: find vm_page for a PA.   used by MI code to get vm_pages
403 * back from an I/O mapping (ugh!).   used in some MD code as well.
404 */
405static __inline struct vm_page *
406PHYS_TO_VM_PAGE(pa)
407	paddr_t pa;
408{
409	paddr_t pf = atop(pa);
410	int	off;
411	int	psi;
412
413	psi = vm_physseg_find(pf, &off);
414	if (psi != -1)
415		return(&vm_physmem[psi].pgs[off]);
416	return(NULL);
417}
418
419#define VM_PAGE_IS_FREE(entry)  ((entry)->pqflags & PQ_FREE)
420
421#endif /* _KERNEL */
422
423#endif /* _UVM_UVM_PAGE_H_ */
424