2 * Copyright (c) 2019 The DragonFly Project. All rights reserved.
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * The Mach Operating System project at Carnegie-Mellon University.
9 * This code is derived from software contributed to The DragonFly Project
10 * by Matthew Dillon <dillon@backplane.com>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * from: @(#)vm_object.h 8.3 (Berkeley) 1/12/94
39 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40 * All rights reserved.
42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
44 * Permission to use, copy, modify and distribute this software and
45 * its documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 * Carnegie Mellon requests users of this software to return to
56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
64 * $FreeBSD: src/sys/vm/vm_object.h,v 1.63.2.3 2003/05/26 19:17:56 alc Exp $
68 * Virtual memory object module definitions.
71 #ifndef _VM_VM_OBJECT_H_
72 #define _VM_VM_OBJECT_H_
75 #include <sys/types.h>
77 #if defined(_KERNEL) && !defined(_SYS_SYSTM_H_)
78 #include <sys/systm.h>
81 #include <sys/queue.h>
86 #ifndef _SYS_THREAD_H_
87 #include <sys/thread.h>
89 #ifndef _MACHINE_PMAP_H_
90 #include <machine/pmap.h>
92 #ifndef _CPU_ATOMIC_H_
93 #include <machine/atomic.h>
98 #ifndef _VM_VM_PAGE_H_
99 #include <vm/vm_page.h>
104 #ifndef _SYS_REFCOUNT_H_
105 #include <sys/refcount.h>
111 struct swblock_rb_tree
;
112 int rb_swblock_compare(struct swblock
*, struct swblock
*);
114 RB_PROTOTYPE2(swblock_rb_tree
, swblock
, swb_entry
, rb_swblock_compare
,
116 RB_HEAD(swblock_rb_tree
, swblock
);
120 OBJT_SWAP
, /* object backed by swap blocks */
121 OBJT_VNODE
, /* object backed by file pages (vnode) */
122 OBJT_DEVICE
, /* object backed by device pages */
124 OBJT_PHYS
, /* object backed by physical pages */
125 OBJT_DEAD
, /* dead object */
126 OBJT_MARKER
/* marker object */
128 typedef u_char objtype_t
;
131 * A VM object which represents an arbitrarily sized data store.
133 * vm_objects are soft-locked with their token, meaning that any
134 * blocking can allow other threads to squeeze in some work.
137 struct lwkt_token token
; /* everything else */
138 struct lock backing_lk
; /* lock for backing_list only */
139 TAILQ_ENTRY(vm_object
) object_entry
;
140 TAILQ_HEAD(, vm_map_backing
) backing_list
;
141 struct vm_page_rb_tree rb_memq
; /* resident pages */
142 int generation
; /* generation ID */
143 vm_pindex_t size
; /* Object size */
145 vm_memattr_t memattr
; /* default memory attribute for pages */
146 objtype_t type
; /* type of pager */
147 u_short flags
; /* see below */
148 u_short pg_color
; /* color of first page in obj */
149 u_int paging_in_progress
; /* Activity in progress */
150 long resident_page_count
; /* number of resident pages */
151 TAILQ_ENTRY(vm_object
) pager_object_entry
; /* optional use by pager */
152 void *handle
; /* control handle: vp, etc */
153 int hold_count
; /* count prevents destruction */
155 #if defined(DEBUG_LOCKS)
157 * Record threads holding a vm_object
160 #define VMOBJ_DEBUG_ARRAY_SIZE (32)
161 char debug_hold_thrs
[VMOBJ_DEBUG_ARRAY_SIZE
][64];
162 const char *debug_hold_file
[VMOBJ_DEBUG_ARRAY_SIZE
];
163 int debug_hold_line
[VMOBJ_DEBUG_ARRAY_SIZE
];
171 * devp_pglist - list of allocated pages
174 TAILQ_HEAD(, vm_page
) devp_pglist
;
175 struct cdev_pager_ops
*ops
;
181 * OBJT_SWAP and OBJT_VNODE VM objects may have swap backing
182 * store. For vnodes the swap backing store acts as a fast
183 * data cache but the vnode contains the official data.
185 struct swblock_rb_tree swblock_root
;
187 struct md_object md
; /* machine specific (typ pmap) */
193 * OBJ_ONEMAPPING - Only applies to DEFAULT and SWAP objects. It may be
194 * gratuitously re-cleared in other cases but will already
195 * be clear in those cases. It might not be set on other
196 * object types (particularly OBJT_VNODE).
198 * This flag indicates that any given page index within the
199 * object is only mapped to at most one vm_map_entry.
201 * WARNING! An obj->refs of 1 does NOT allow you to
202 * re-set this bit because the object might be part of
203 * a shared chain of vm_map_backing structures.
205 * OBJ_NOPAGEIN - vn and tmpfs set this flag, indicating to swapoff
206 * that the objects aren't intended to have any vm_page's,
207 * only swap blocks. vn and tmpfs don't know how to deal
208 * with any actual pages.
210 #define OBJ_UNUSED0001 0x0001
211 #define OBJ_UNUSED0002 0x0002
212 #define OBJ_ACTIVE 0x0004 /* active objects */
213 #define OBJ_DEAD 0x0008 /* dead objects (during rundown) */
214 #define OBJ_NOSPLIT 0x0010 /* dont split this object */
215 #define OBJ_NOPAGEIN 0x0040 /* special OBJT_SWAP */
216 #define OBJ_WRITEABLE 0x0080 /* object has been made writable */
217 #define OBJ_MIGHTBEDIRTY 0x0100 /* object might be dirty */
218 #define OBJ_CLEANING 0x0200
219 #define OBJ_DEADWNT 0x1000 /* waiting because object is dead */
220 #define OBJ_ONEMAPPING 0x2000
221 #define OBJ_NOMSYNC 0x4000 /* disable msync() system call */
223 #define IDX_TO_OFF(idx) (((vm_ooffset_t)(idx)) << PAGE_SHIFT)
224 #define OFF_TO_IDX(off) ((vm_pindex_t)(((vm_ooffset_t)(off)) >> PAGE_SHIFT))
226 #define VMOBJ_HSIZE 256
227 #define VMOBJ_HMASK (VMOBJ_HSIZE - 1)
231 #define OBJPC_SYNC 0x0001 /* sync I/O */
232 #define OBJPC_INVAL 0x0002 /* invalidate */
233 #define OBJPC_NOSYNC 0x0004 /* skip if PG_NOSYNC */
234 #define OBJPC_IGNORE_CLEANCHK 0x0008
235 #define OBJPC_CLUSTER_OK 0x0010 /* used only by vnode pager */
236 #define OBJPC_TRY_TO_CACHE 0x0020 /* typically used only in pageout path */
237 #define OBJPC_ALLOW_ACTIVE 0x0040 /* dito */
241 * Used to chain vm_object deallocations
243 struct vm_object_dealloc_list
{
244 struct vm_object_dealloc_list
*next
;
249 TAILQ_HEAD(object_q
, vm_object
);
251 struct vm_object_hash
{
252 struct object_q list
;
253 struct lwkt_token token
;
256 extern struct vm_object_hash vm_object_hash
[VMOBJ_HSIZE
];
258 /* lock for object list and count */
260 extern struct vm_object
*kernel_object
; /* the single kernel object */
261 extern int vm_shared_fault
;
267 #define VM_OBJECT_LOCK(object) vm_object_hold(object)
268 #define VM_OBJECT_UNLOCK(object) vm_object_drop(object)
271 vm_object_set_flag(vm_object_t object
, u_int bits
)
273 atomic_set_short(&object
->flags
, bits
);
277 vm_object_clear_flag(vm_object_t object
, u_int bits
)
279 atomic_clear_short(&object
->flags
, bits
);
283 vm_object_pip_add(vm_object_t object
, u_int i
)
285 refcount_acquire_n(&object
->paging_in_progress
, i
);
289 vm_object_pip_wakeup_n(vm_object_t object
, u_int i
)
291 refcount_release_wakeup_n(&object
->paging_in_progress
, i
);
295 vm_object_pip_wakeup(vm_object_t object
)
297 vm_object_pip_wakeup_n(object
, 1);
301 vm_object_pip_wait(vm_object_t object
, char *waitid
)
303 refcount_wait(&object
->paging_in_progress
, waitid
);
306 static __inline lwkt_token_t
307 vm_object_token(vm_object_t obj
)
309 return (&obj
->token
);
312 vm_object_t
vm_object_allocate (objtype_t
, vm_pindex_t
);
313 vm_object_t
vm_object_allocate_hold (objtype_t
, vm_pindex_t
);
314 void _vm_object_allocate (objtype_t
, vm_pindex_t
, vm_object_t
, const char *);
315 boolean_t
vm_object_coalesce (vm_object_t
, vm_pindex_t
, vm_size_t
, vm_size_t
);
316 vm_object_t
vm_object_collapse (vm_object_t
, vm_object_t
);
317 void vm_object_terminate (vm_object_t
);
318 void vm_object_set_writeable_dirty (vm_object_t
);
319 void vm_object_init(vm_object_t
, vm_pindex_t
);
320 void vm_object_init1 (void);
321 void vm_object_page_clean (vm_object_t
, vm_pindex_t
, vm_pindex_t
, int);
322 void vm_object_page_remove (vm_object_t
, vm_pindex_t
, vm_pindex_t
, boolean_t
);
323 void vm_object_pmap_copy (vm_object_t
, vm_pindex_t
, vm_pindex_t
);
324 void vm_object_madvise (vm_object_t
, vm_pindex_t
, vm_pindex_t
, int);
325 void vm_object_init2 (void);
326 vm_page_t
vm_fault_object_page(vm_object_t
, vm_ooffset_t
,
327 vm_prot_t
, int, int *, int *);
328 void vm_object_lock_swap(void);
329 void vm_object_lock(vm_object_t
);
330 void vm_object_lock_shared(vm_object_t
);
331 void vm_object_unlock(vm_object_t
);
333 #if defined(DEBUG_LOCKS)
335 #define VMOBJDEBUG(x) debug ## x
336 #define VMOBJDBARGS , char *file, int line
337 #define VMOBJDBFWD , file, line
339 #define vm_object_hold(obj) \
340 debugvm_object_hold(obj, __FILE__, __LINE__)
341 #define vm_object_hold_try(obj) \
342 debugvm_object_hold_try(obj, __FILE__, __LINE__)
343 #define vm_object_hold_shared(obj) \
344 debugvm_object_hold_shared(obj, __FILE__, __LINE__)
345 #define vm_object_drop(obj) \
346 debugvm_object_drop(obj, __FILE__, __LINE__)
347 #define vm_object_reference_quick(obj) \
348 debugvm_object_reference_quick(obj, __FILE__, __LINE__)
349 #define vm_object_reference_locked(obj) \
350 debugvm_object_reference_locked(obj, __FILE__, __LINE__)
352 #define vm_object_reference_locked_chain_held(obj) \
353 debugvm_object_reference_locked_chain_held( \
354 obj, __FILE__, __LINE__)
356 #define vm_object_deallocate(obj) \
357 debugvm_object_deallocate(obj, __FILE__, __LINE__)
358 #define vm_object_deallocate_locked(obj) \
359 debugvm_object_deallocate_locked(obj, __FILE__, __LINE__)
363 #define VMOBJDEBUG(x) x
369 void VMOBJDEBUG(vm_object_hold
)(vm_object_t object VMOBJDBARGS
);
370 int VMOBJDEBUG(vm_object_hold_try
)(vm_object_t object VMOBJDBARGS
);
371 void VMOBJDEBUG(vm_object_hold_shared
)(vm_object_t object VMOBJDBARGS
);
372 void VMOBJDEBUG(vm_object_drop
)(vm_object_t object VMOBJDBARGS
);
373 void VMOBJDEBUG(vm_object_reference_quick
)(vm_object_t object VMOBJDBARGS
);
374 void VMOBJDEBUG(vm_object_reference_locked
)(vm_object_t object VMOBJDBARGS
);
376 void VMOBJDEBUG(vm_object_reference_locked_chain_held
)(
377 vm_object_t object VMOBJDBARGS
);
379 void VMOBJDEBUG(vm_object_deallocate
)(vm_object_t object VMOBJDBARGS
);
380 void VMOBJDEBUG(vm_object_deallocate_locked
)(vm_object_t object VMOBJDBARGS
);
382 void vm_object_upgrade(vm_object_t
);
383 void vm_object_downgrade(vm_object_t
);
384 int vm_quickcolor(void);
388 #endif /* _VM_VM_OBJECT_H_ */