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_object.h	8.3 (Berkeley) 1/12/94
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$
611541Srgrimes */
621541Srgrimes
631541Srgrimes/*
641541Srgrimes *	Virtual memory object module definitions.
651541Srgrimes */
661541Srgrimes
671541Srgrimes#ifndef	_VM_OBJECT_
681541Srgrimes#define	_VM_OBJECT_
691541Srgrimes
7018942Sbde#include <sys/queue.h>
71108117Salc#include <sys/_lock.h>
72108117Salc#include <sys/_mutex.h>
73248084Sattilio#include <sys/_rwlock.h>
747090Sbde
75248449Sattilio#include <vm/_vm_radix.h>
76248449Sattilio
771541Srgrimes/*
781541Srgrimes *	Types defined:
791541Srgrimes *
801541Srgrimes *	vm_object_t		Virtual memory object.
8142957Sdillon *
82248084Sattilio *	The root of cached pages pool is protected by both the per-object lock
83237451Sattilio *	and the free pages queue mutex.
84248449Sattilio *	On insert in the cache radix trie, the per-object lock is expected
85237451Sattilio *	to be already held and the free pages queue mutex will be
86237451Sattilio *	acquired during the operation too.
87248449Sattilio *	On remove and lookup from the cache radix trie, only the free
88237451Sattilio *	pages queue mutex is expected to be locked.
89237451Sattilio *	These rules allow for reliably checking for the presence of cached
90237451Sattilio *	pages with only the per-object lock held, thereby reducing contention
91237451Sattilio *	for the free pages queue mutex.
92237451Sattilio *
9396042Salc * List of locks
9496042Salc *	(c)	const until freed
95248084Sattilio *	(o)	per-object lock
96237451Sattilio *	(f)	free pages queue mutex
9796042Salc *
981541Srgrimes */
991541Srgrimes
1001541Srgrimesstruct vm_object {
101248084Sattilio	struct rwlock lock;
10260938Sjake	TAILQ_ENTRY(vm_object) object_list; /* list of all objects */
103115127Salc	LIST_HEAD(, vm_object) shadow_head; /* objects that this is a shadow for */
104115127Salc	LIST_ENTRY(vm_object) shadow_list; /* chain of shadow objects */
105254141Sattilio	TAILQ_HEAD(respgs, vm_page) memq; /* list of resident pages */
106248449Sattilio	struct vm_radix rtree;		/* root of the resident page radix trie*/
107118838Salc	vm_pindex_t size;		/* Object size */
10832702Sdyson	int generation;			/* generation ID */
1095455Sdg	int ref_count;			/* How many refs?? */
11014316Sdyson	int shadow_count;		/* how many objects that this is a shadow for */
111195649Salc	vm_memattr_t memattr;		/* default memory attribute for pages */
11252568Salc	objtype_t type;			/* type of pager */
1136129Sdg	u_short flags;			/* see below */
11496042Salc	u_short pg_color;		/* (c) color of first page in obj */
115229934Skib	u_int paging_in_progress;	/* Paging (in or out) so don't collapse or destroy */
1166129Sdg	int resident_page_count;	/* number of resident pages */
1179507Sdg	struct vm_object *backing_object; /* object that I'm a shadow of */
11812767Sdyson	vm_ooffset_t backing_object_offset;/* Offset in backing object */
11960938Sjake	TAILQ_ENTRY(vm_object) pager_object_list; /* list of all objects of this pager type */
120174940Salc	LIST_HEAD(, vm_reserv) rvq;	/* list of reservations */
121248449Sattilio	struct vm_radix cache;		/* (o + f) root of the cache page radix trie */
1229507Sdg	void *handle;
1239507Sdg	union {
12442957Sdillon		/*
12542957Sdillon		 * VNode pager
12642957Sdillon		 *
12742957Sdillon		 *	vnp_size - current size of file
12842957Sdillon		 */
1299507Sdg		struct {
13042957Sdillon			off_t vnp_size;
131232071Skib			vm_ooffset_t writemappings;
1329507Sdg		} vnp;
13342957Sdillon
13442957Sdillon		/*
13542957Sdillon		 * Device pager
13642957Sdillon		 *
13742957Sdillon		 *	devp_pglist - list of allocated pages
13842957Sdillon		 */
1399507Sdg		struct {
14060938Sjake			TAILQ_HEAD(, vm_page) devp_pglist;
141227530Skib			struct cdev_pager_ops *ops;
142245226Sken			struct cdev *dev;
1439507Sdg		} devp;
14442957Sdillon
14542957Sdillon		/*
146195840Sjhb		 * SG pager
147195840Sjhb		 *
148195840Sjhb		 *	sgp_pglist - list of allocated pages
149195840Sjhb		 */
150195840Sjhb		struct {
151195840Sjhb			TAILQ_HEAD(, vm_page) sgp_pglist;
152195840Sjhb		} sgp;
153195840Sjhb
154195840Sjhb		/*
15542957Sdillon		 * Swap pager
15642957Sdillon		 *
157250030Skib		 *	swp_tmpfs - back-pointer to the tmpfs vnode,
158250030Skib		 *		     if any, which uses the vm object
159250030Skib		 *		     as backing store.  The handle
160250030Skib		 *		     cannot be reused for linking,
161250030Skib		 *		     because the vnode can be
162250030Skib		 *		     reclaimed and recreated, making
163250030Skib		 *		     the handle changed and hash-chain
164250030Skib		 *		     invalid.
165250030Skib		 *
16642957Sdillon		 *	swp_bcount - number of swap 'swblock' metablocks, each
16742957Sdillon		 *		     contains up to 16 swapblk assignments.
16842957Sdillon		 *		     see vm/swap_pager.h
16942957Sdillon		 */
1709548Sdg		struct {
171250030Skib			void *swp_tmpfs;
17242957Sdillon			int swp_bcount;
1739548Sdg		} swp;
1749507Sdg	} un_pager;
175216128Strasz	struct ucred *cred;
176194766Skib	vm_ooffset_t charge;
1771541Srgrimes};
1785455Sdg
1791541Srgrimes/*
1801541Srgrimes * Flags
1811541Srgrimes */
182244043Salc#define	OBJ_FICTITIOUS	0x0001		/* (c) contains fictitious pages */
183244043Salc#define	OBJ_UNMANAGED	0x0002		/* (c) contains unmanaged pages */
1849507Sdg#define OBJ_ACTIVE	0x0004		/* active objects */
1859507Sdg#define OBJ_DEAD	0x0008		/* dead objects (during rundown) */
18635694Sdyson#define	OBJ_NOSPLIT	0x0010		/* dont split this object */
18732286Sdyson#define OBJ_PIPWNT	0x0040		/* paging in progress wanted */
188200770Skib#define OBJ_MIGHTBEDIRTY 0x0100		/* object might be dirty, only for vnode */
189269174Skib#define	OBJ_TMPFS_NODE	0x0200		/* object belongs to tmpfs VREG node */
190174940Salc#define	OBJ_COLORED	0x1000		/* pg_color is defined */
19135497Sdyson#define	OBJ_ONEMAPPING	0x2000		/* One USE (a single, non-forked) mapping flag */
192137297Salc#define	OBJ_DISCONNECTWNT 0x4000	/* disconnect from vnode wanted */
193269174Skib#define	OBJ_TMPFS	0x8000		/* has tmpfs vnode allocated */
1941541Srgrimes
19512767Sdyson#define IDX_TO_OFF(idx) (((vm_ooffset_t)(idx)) << PAGE_SHIFT)
19612767Sdyson#define OFF_TO_IDX(off) ((vm_pindex_t)(((vm_ooffset_t)(off)) >> PAGE_SHIFT))
1971541Srgrimes
19855206Speter#ifdef	_KERNEL
1997090Sbde
20054467Sdillon#define OBJPC_SYNC	0x1			/* sync I/O */
20154467Sdillon#define OBJPC_INVAL	0x2			/* invalidate */
20254467Sdillon#define OBJPC_NOSYNC	0x4			/* skip if PG_NOSYNC */
20334206Sdyson
204223677Salc/*
205223677Salc * The following options are supported by vm_object_page_remove().
206223677Salc */
207223677Salc#define	OBJPR_CLEANONLY	0x1		/* Don't remove dirty pages. */
208223677Salc#define	OBJPR_NOTMAPPED	0x2		/* Don't unmap pages. */
209253189Skib#define	OBJPR_NOTWIRED	0x4		/* Don't remove wired pages. */
210223677Salc
21160938SjakeTAILQ_HEAD(object_q, vm_object);
2121541Srgrimes
2139759Sbdeextern struct object_q vm_object_list;	/* list of allocated objects */
21495112Salcextern struct mtx vm_object_list_mtx;	/* lock for object list and count */
2151541Srgrimes
216115655Salcextern struct vm_object kernel_object_store;
217115655Salcextern struct vm_object kmem_object_store;
2185455Sdg
219115655Salc#define	kernel_object	(&kernel_object_store)
220115655Salc#define	kmem_object	(&kmem_object_store)
221115655Salc
222248084Sattilio#define	VM_OBJECT_ASSERT_LOCKED(object)					\
223248084Sattilio	rw_assert(&(object)->lock, RA_LOCKED)
224248084Sattilio#define	VM_OBJECT_ASSERT_RLOCKED(object)				\
225248084Sattilio	rw_assert(&(object)->lock, RA_RLOCKED)
226248084Sattilio#define	VM_OBJECT_ASSERT_WLOCKED(object)				\
227248084Sattilio	rw_assert(&(object)->lock, RA_WLOCKED)
228269914Skib#define	VM_OBJECT_ASSERT_UNLOCKED(object)				\
229269914Skib	rw_assert(&(object)->lock, RA_UNLOCKED)
230250884Sattilio#define	VM_OBJECT_LOCK_DOWNGRADE(object)				\
231250884Sattilio	rw_downgrade(&(object)->lock)
232248084Sattilio#define	VM_OBJECT_RLOCK(object)						\
233248084Sattilio	rw_rlock(&(object)->lock)
234248084Sattilio#define	VM_OBJECT_RUNLOCK(object)					\
235248084Sattilio	rw_runlock(&(object)->lock)
236248084Sattilio#define	VM_OBJECT_SLEEP(object, wchan, pri, wmesg, timo)		\
237248084Sattilio	rw_sleep((wchan), &(object)->lock, (pri), (wmesg), (timo))
238248084Sattilio#define	VM_OBJECT_TRYRLOCK(object)					\
239248084Sattilio	rw_try_rlock(&(object)->lock)
240248084Sattilio#define	VM_OBJECT_TRYWLOCK(object)					\
241248084Sattilio	rw_try_wlock(&(object)->lock)
242269914Skib#define	VM_OBJECT_TRYUPGRADE(object)					\
243269914Skib	rw_try_upgrade(&(object)->lock)
244248084Sattilio#define	VM_OBJECT_WLOCK(object)						\
245248084Sattilio	rw_wlock(&(object)->lock)
246248084Sattilio#define	VM_OBJECT_WUNLOCK(object)					\
247248084Sattilio	rw_wunlock(&(object)->lock)
248113445Salc
249121821Salc/*
250121821Salc *	The object must be locked or thread private.
251121821Salc */
252121821Salcstatic __inline void
253121821Salcvm_object_set_flag(vm_object_t object, u_short bits)
254121821Salc{
255121821Salc
256121821Salc	object->flags |= bits;
257121821Salc}
258121821Salc
25979248Sdillonvoid vm_object_clear_flag(vm_object_t object, u_short bits);
26079248Sdillonvoid vm_object_pip_add(vm_object_t object, short i);
26179248Sdillonvoid vm_object_pip_subtract(vm_object_t object, short i);
26279248Sdillonvoid vm_object_pip_wakeup(vm_object_t object);
26379248Sdillonvoid vm_object_pip_wakeupn(vm_object_t object, short i);
26479248Sdillonvoid vm_object_pip_wait(vm_object_t object, char *waitid);
26538517Sdfr
266248082Sattiliostatic __inline boolean_t
267248082Sattiliovm_object_cache_is_empty(vm_object_t object)
268248082Sattilio{
269248082Sattilio
270248449Sattilio	return (vm_radix_is_empty(&object->cache));
271248082Sattilio}
272248082Sattilio
27398824Siedowsevm_object_t vm_object_allocate (objtype_t, vm_pindex_t);
274194766Skibboolean_t vm_object_coalesce(vm_object_t, vm_ooffset_t, vm_size_t, vm_size_t,
275194766Skib   boolean_t);
27679248Sdillonvoid vm_object_collapse (vm_object_t);
27779248Sdillonvoid vm_object_deallocate (vm_object_t);
278179159Supsvoid vm_object_destroy (vm_object_t);
27979248Sdillonvoid vm_object_terminate (vm_object_t);
28085517Sdillonvoid vm_object_set_writeable_dirty (vm_object_t);
28179248Sdillonvoid vm_object_init (void);
282233191Sjhbvoid vm_object_madvise(vm_object_t, vm_pindex_t, vm_pindex_t, int);
283227070Sjhbvoid vm_object_page_cache(vm_object_t object, vm_pindex_t start,
284227070Sjhb    vm_pindex_t end);
285233100Skibboolean_t vm_object_page_clean(vm_object_t object, vm_ooffset_t start,
286218345Salc    vm_ooffset_t end, int flags);
287223677Salcvoid vm_object_page_remove(vm_object_t object, vm_pindex_t start,
288223677Salc    vm_pindex_t end, int options);
289194209Salcboolean_t vm_object_populate(vm_object_t, vm_pindex_t, vm_pindex_t);
290216731Salcvoid vm_object_print(long addr, boolean_t have_addr, long count, char *modif);
29179248Sdillonvoid vm_object_reference (vm_object_t);
292121907Salcvoid vm_object_reference_locked(vm_object_t);
293195649Salcint  vm_object_set_memattr(vm_object_t object, vm_memattr_t memattr);
29479248Sdillonvoid vm_object_shadow (vm_object_t *, vm_ooffset_t *, vm_size_t);
29597753Salcvoid vm_object_split(vm_map_entry_t);
296233100Skibboolean_t vm_object_sync(vm_object_t, vm_ooffset_t, vm_size_t, boolean_t,
297122349Salc    boolean_t);
298270920Skibvoid vm_object_unwire(vm_object_t object, vm_ooffset_t offset,
299270920Skib    vm_size_t length, uint8_t queue);
30055206Speter#endif				/* _KERNEL */
3015455Sdg
3025455Sdg#endif				/* _VM_OBJECT_ */
303