kernel - VM rework part 9 - Precursor work for terminal pv_entry removal
[dragonfly.git] / sys / vm / vm_object.h
blob98edacddf0636a6b4342a64ea95e4b1ec2026109
1 /*
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
14 * are met:
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
34 * SUCH DAMAGE.
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_
74 #ifndef _SYS_TYPES_H_
75 #include <sys/types.h>
76 #endif
77 #if defined(_KERNEL) && !defined(_SYS_SYSTM_H_)
78 #include <sys/systm.h>
79 #endif
80 #ifndef _SYS_QUEUE_H_
81 #include <sys/queue.h>
82 #endif
83 #ifndef _SYS_TREE_H_
84 #include <sys/tree.h>
85 #endif
86 #ifndef _SYS_THREAD_H_
87 #include <sys/thread.h>
88 #endif
89 #ifndef _MACHINE_PMAP_H_
90 #include <machine/pmap.h>
91 #endif
92 #ifndef _CPU_ATOMIC_H_
93 #include <machine/atomic.h>
94 #endif
95 #ifndef _VM_VM_H_
96 #include <vm/vm.h>
97 #endif
98 #ifndef _VM_VM_PAGE_H_
99 #include <vm/vm_page.h>
100 #endif
102 #ifdef _KERNEL
104 #ifndef _SYS_REFCOUNT_H_
105 #include <sys/refcount.h>
106 #endif
108 #endif
110 struct swblock;
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,
115 vm_pindex_t);
116 RB_HEAD(swblock_rb_tree, swblock);
118 enum obj_type {
119 OBJT_DEFAULT,
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 */
123 OBJT_MGTDEVICE,
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.
136 struct vm_object {
137 struct lwkt_token token; /* everything else */
138 struct spinlock spin; /* backing_list */
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 */
144 int ref_count;
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];
164 int debug_index;
165 #endif
167 union {
169 * Device pager
171 * devp_pglist - list of allocated pages
173 struct {
174 TAILQ_HEAD(, vm_page) devp_pglist;
175 struct cdev_pager_ops *ops;
176 struct cdev *dev;
177 } devp;
178 } un_pager;
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;
186 long swblock_count;
187 struct md_object md; /* machine specific (typ pmap) */
191 * Flags
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)
229 #ifdef _KERNEL
231 #define OBJPC_SYNC 0x1 /* sync I/O */
232 #define OBJPC_INVAL 0x2 /* invalidate */
233 #define OBJPC_NOSYNC 0x4 /* skip if PG_NOSYNC */
235 #if 0
237 * Used to chain vm_object deallocations
239 struct vm_object_dealloc_list {
240 struct vm_object_dealloc_list *next;
241 vm_object_t object;
243 #endif
245 TAILQ_HEAD(object_q, vm_object);
247 struct vm_object_hash {
248 struct object_q list;
249 struct lwkt_token token;
250 } __cachealign;
252 extern struct vm_object_hash vm_object_hash[VMOBJ_HSIZE];
254 /* lock for object list and count */
256 extern struct vm_object kernel_object; /* the single kernel object */
257 extern int vm_shared_fault;
259 #endif /* _KERNEL */
261 #ifdef _KERNEL
263 #define VM_OBJECT_LOCK(object) vm_object_hold(object)
264 #define VM_OBJECT_UNLOCK(object) vm_object_drop(object)
266 static __inline void
267 vm_object_set_flag(vm_object_t object, u_int bits)
269 atomic_set_short(&object->flags, bits);
272 static __inline void
273 vm_object_clear_flag(vm_object_t object, u_int bits)
275 atomic_clear_short(&object->flags, bits);
278 static __inline void
279 vm_object_pip_add(vm_object_t object, u_int i)
281 refcount_acquire_n(&object->paging_in_progress, i);
284 static __inline void
285 vm_object_pip_wakeup_n(vm_object_t object, u_int i)
287 refcount_release_wakeup_n(&object->paging_in_progress, i);
290 static __inline void
291 vm_object_pip_wakeup(vm_object_t object)
293 vm_object_pip_wakeup_n(object, 1);
296 static __inline void
297 vm_object_pip_wait(vm_object_t object, char *waitid)
299 refcount_wait(&object->paging_in_progress, waitid);
302 static __inline lwkt_token_t
303 vm_object_token(vm_object_t obj)
305 return (&obj->token);
308 vm_object_t vm_object_allocate (objtype_t, vm_pindex_t);
309 vm_object_t vm_object_allocate_hold (objtype_t, vm_pindex_t);
310 void _vm_object_allocate (objtype_t, vm_pindex_t, vm_object_t);
311 boolean_t vm_object_coalesce (vm_object_t, vm_pindex_t, vm_size_t, vm_size_t);
312 vm_object_t vm_object_collapse (vm_object_t, vm_object_t);
313 void vm_object_terminate (vm_object_t);
314 void vm_object_set_writeable_dirty (vm_object_t);
315 void vm_object_init(vm_object_t, vm_pindex_t);
316 void vm_object_init1 (void);
317 void vm_object_page_clean (vm_object_t, vm_pindex_t, vm_pindex_t, int);
318 void vm_object_page_remove (vm_object_t, vm_pindex_t, vm_pindex_t, boolean_t);
319 void vm_object_pmap_copy (vm_object_t, vm_pindex_t, vm_pindex_t);
320 void vm_object_madvise (vm_object_t, vm_pindex_t, vm_pindex_t, int);
321 void vm_object_init2 (void);
322 vm_page_t vm_fault_object_page(vm_object_t, vm_ooffset_t,
323 vm_prot_t, int, int *, int *);
324 void vm_object_lock_swap(void);
325 void vm_object_lock(vm_object_t);
326 void vm_object_lock_shared(vm_object_t);
327 void vm_object_unlock(vm_object_t);
329 #if defined(DEBUG_LOCKS)
331 #define VMOBJDEBUG(x) debug ## x
332 #define VMOBJDBARGS , char *file, int line
333 #define VMOBJDBFWD , file, line
335 #define vm_object_hold(obj) \
336 debugvm_object_hold(obj, __FILE__, __LINE__)
337 #define vm_object_hold_try(obj) \
338 debugvm_object_hold_try(obj, __FILE__, __LINE__)
339 #define vm_object_hold_shared(obj) \
340 debugvm_object_hold_shared(obj, __FILE__, __LINE__)
341 #define vm_object_drop(obj) \
342 debugvm_object_drop(obj, __FILE__, __LINE__)
343 #define vm_object_reference_quick(obj) \
344 debugvm_object_reference_quick(obj, __FILE__, __LINE__)
345 #define vm_object_reference_locked(obj) \
346 debugvm_object_reference_locked(obj, __FILE__, __LINE__)
347 #if 0
348 #define vm_object_reference_locked_chain_held(obj) \
349 debugvm_object_reference_locked_chain_held( \
350 obj, __FILE__, __LINE__)
351 #endif
352 #define vm_object_deallocate(obj) \
353 debugvm_object_deallocate(obj, __FILE__, __LINE__)
354 #define vm_object_deallocate_locked(obj) \
355 debugvm_object_deallocate_locked(obj, __FILE__, __LINE__)
357 #else
359 #define VMOBJDEBUG(x) x
360 #define VMOBJDBARGS
361 #define VMOBJDBFWD
363 #endif
365 void VMOBJDEBUG(vm_object_hold)(vm_object_t object VMOBJDBARGS);
366 int VMOBJDEBUG(vm_object_hold_try)(vm_object_t object VMOBJDBARGS);
367 void VMOBJDEBUG(vm_object_hold_shared)(vm_object_t object VMOBJDBARGS);
368 void VMOBJDEBUG(vm_object_drop)(vm_object_t object VMOBJDBARGS);
369 void VMOBJDEBUG(vm_object_reference_quick)(vm_object_t object VMOBJDBARGS);
370 void VMOBJDEBUG(vm_object_reference_locked)(vm_object_t object VMOBJDBARGS);
371 #if 0
372 void VMOBJDEBUG(vm_object_reference_locked_chain_held)(
373 vm_object_t object VMOBJDBARGS);
374 #endif
375 void VMOBJDEBUG(vm_object_deallocate)(vm_object_t object VMOBJDBARGS);
376 void VMOBJDEBUG(vm_object_deallocate_locked)(vm_object_t object VMOBJDBARGS);
378 void vm_object_upgrade(vm_object_t);
379 void vm_object_downgrade(vm_object_t);
380 int vm_quickcolor(void);
382 #endif /* _KERNEL */
384 #endif /* _VM_VM_OBJECT_H_ */