kernel - VM rework part 7 - Initial vm_map_backing index
[dragonfly.git] / sys / vm / vm_object.h
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.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * The Mach Operating System project at Carnegie-Mellon University.
8  *
9  * This code is derived from software contributed to The DragonFly Project
10  * by Matthew Dillon <dillon@backplane.com>
11  *
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.
23  *
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.
35  *
36  *      from: @(#)vm_object.h   8.3 (Berkeley) 1/12/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
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.
49  *
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.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  *
64  * $FreeBSD: src/sys/vm/vm_object.h,v 1.63.2.3 2003/05/26 19:17:56 alc Exp $
65  */
66
67 /*
68  *      Virtual memory object module definitions.
69  */
70
71 #ifndef _VM_VM_OBJECT_H_
72 #define _VM_VM_OBJECT_H_
73
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
101
102 #ifdef _KERNEL
103
104 #ifndef _SYS_REFCOUNT_H_
105 #include <sys/refcount.h>
106 #endif
107
108 #endif
109
110 struct swblock;
111 struct swblock_rb_tree;
112 int rb_swblock_compare(struct swblock *, struct swblock *);
113
114 RB_PROTOTYPE2(swblock_rb_tree, swblock, swb_entry, rb_swblock_compare,
115               vm_pindex_t);
116 RB_HEAD(swblock_rb_tree, swblock);
117
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 */
127 };
128 typedef u_char objtype_t;
129
130 /*
131  * A VM object which represents an arbitrarily sized data store.
132  *
133  * vm_objects are soft-locked with their token, meaning that any
134  * blocking can allow other threads to squeeze in some work.
135  */
136 struct vm_object {
137         struct lwkt_token token;
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 */
154         
155 #if defined(DEBUG_LOCKS)
156         /* 
157          * Record threads holding a vm_object
158          */
159
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
166
167         union {
168                 /*
169                  * Device pager
170                  *
171                  *      devp_pglist - list of allocated pages
172                  */
173                 struct {
174                         TAILQ_HEAD(, vm_page) devp_pglist;
175                         struct cdev_pager_ops *ops;
176                         struct cdev *dev;
177                 } devp;
178         } un_pager;
179
180         /*
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.
184          */
185         struct swblock_rb_tree swblock_root;
186         long            swblock_count;
187         struct md_object md;            /* machine specific (typ pmap) */
188 };
189
190 /*
191  * Flags
192  *
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).
197  *
198  *                  This flag indicates that any given page index within the
199  *                  object is only mapped to at most one vm_map_entry.
200  *
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.
204  *
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.
209  */
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 */
222
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))
225
226 #define VMOBJ_HSIZE     256
227 #define VMOBJ_HMASK     (VMOBJ_HSIZE - 1)
228
229 #ifdef  _KERNEL
230
231 #define OBJPC_SYNC      0x1                     /* sync I/O */
232 #define OBJPC_INVAL     0x2                     /* invalidate */
233 #define OBJPC_NOSYNC    0x4                     /* skip if PG_NOSYNC */
234
235 #if 0
236 /*
237  * Used to chain vm_object deallocations
238  */
239 struct vm_object_dealloc_list {
240         struct vm_object_dealloc_list *next;
241         vm_object_t     object;
242 };
243 #endif
244
245 TAILQ_HEAD(object_q, vm_object);
246
247 struct vm_object_hash {
248         struct object_q         list;
249         struct lwkt_token       token;
250 } __cachealign;
251
252 extern struct vm_object_hash vm_object_hash[VMOBJ_HSIZE];
253
254  /* lock for object list and count */
255
256 extern struct vm_object kernel_object;  /* the single kernel object */
257 extern int vm_shared_fault;
258
259 #endif                          /* _KERNEL */
260
261 #ifdef _KERNEL
262
263 #define VM_OBJECT_LOCK(object)          vm_object_hold(object)
264 #define VM_OBJECT_UNLOCK(object)        vm_object_drop(object)
265
266 static __inline void
267 vm_object_set_flag(vm_object_t object, u_int bits)
268 {
269         atomic_set_short(&object->flags, bits);
270 }
271
272 static __inline void
273 vm_object_clear_flag(vm_object_t object, u_int bits)
274 {
275         atomic_clear_short(&object->flags, bits);
276 }
277
278 static __inline void
279 vm_object_pip_add(vm_object_t object, u_int i)
280 {
281         refcount_acquire_n(&object->paging_in_progress, i);
282 }
283
284 static __inline void
285 vm_object_pip_wakeup_n(vm_object_t object, u_int i)
286 {
287         refcount_release_wakeup_n(&object->paging_in_progress, i);
288 }
289
290 static __inline void
291 vm_object_pip_wakeup(vm_object_t object)
292 {
293         vm_object_pip_wakeup_n(object, 1);
294 }
295
296 static __inline void
297 vm_object_pip_wait(vm_object_t object, char *waitid)
298 {
299         refcount_wait(&object->paging_in_progress, waitid);
300 }
301
302 static __inline lwkt_token_t
303 vm_object_token(vm_object_t obj)
304 {
305         return (&obj->token);
306 }
307
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_pmap_copy_1 (vm_object_t, vm_pindex_t, vm_pindex_t);
321 void vm_object_pmap_remove (vm_object_t, vm_pindex_t, vm_pindex_t);
322 void vm_object_madvise (vm_object_t, vm_pindex_t, vm_pindex_t, int);
323 void vm_object_init2 (void);
324 vm_page_t vm_fault_object_page(vm_object_t, vm_ooffset_t,
325                                 vm_prot_t, int, int *, int *);
326 void vm_object_lock_swap(void);
327 void vm_object_lock(vm_object_t);
328 void vm_object_lock_shared(vm_object_t);
329 void vm_object_unlock(vm_object_t);
330
331 #if defined(DEBUG_LOCKS)
332
333 #define VMOBJDEBUG(x)   debug ## x
334 #define VMOBJDBARGS     , char *file, int line
335 #define VMOBJDBFWD      , file, line
336
337 #define vm_object_hold(obj)                     \
338                 debugvm_object_hold(obj, __FILE__, __LINE__)
339 #define vm_object_hold_try(obj)                 \
340                 debugvm_object_hold_try(obj, __FILE__, __LINE__)
341 #define vm_object_hold_shared(obj)              \
342                 debugvm_object_hold_shared(obj, __FILE__, __LINE__)
343 #define vm_object_drop(obj)                     \
344                 debugvm_object_drop(obj, __FILE__, __LINE__)
345 #define vm_object_reference_quick(obj)          \
346                 debugvm_object_reference_quick(obj, __FILE__, __LINE__)
347 #define vm_object_reference_locked(obj)         \
348                 debugvm_object_reference_locked(obj, __FILE__, __LINE__)
349 #if 0
350 #define vm_object_reference_locked_chain_held(obj)              \
351                 debugvm_object_reference_locked_chain_held(     \
352                                         obj, __FILE__, __LINE__)
353 #endif
354 #define vm_object_deallocate(obj)               \
355                 debugvm_object_deallocate(obj, __FILE__, __LINE__)
356 #define vm_object_deallocate_locked(obj)        \
357                 debugvm_object_deallocate_locked(obj, __FILE__, __LINE__)
358
359 #else
360
361 #define VMOBJDEBUG(x)   x
362 #define VMOBJDBARGS
363 #define VMOBJDBFWD
364
365 #endif
366
367 void VMOBJDEBUG(vm_object_hold)(vm_object_t object VMOBJDBARGS);
368 int VMOBJDEBUG(vm_object_hold_try)(vm_object_t object VMOBJDBARGS);
369 void VMOBJDEBUG(vm_object_hold_shared)(vm_object_t object VMOBJDBARGS);
370 void VMOBJDEBUG(vm_object_drop)(vm_object_t object VMOBJDBARGS);
371 void VMOBJDEBUG(vm_object_reference_quick)(vm_object_t object VMOBJDBARGS);
372 void VMOBJDEBUG(vm_object_reference_locked)(vm_object_t object VMOBJDBARGS);
373 #if 0
374 void VMOBJDEBUG(vm_object_reference_locked_chain_held)(
375                         vm_object_t object VMOBJDBARGS);
376 #endif
377 void VMOBJDEBUG(vm_object_deallocate)(vm_object_t object VMOBJDBARGS);
378 void VMOBJDEBUG(vm_object_deallocate_locked)(vm_object_t object VMOBJDBARGS);
379
380 void vm_object_upgrade(vm_object_t);
381 void vm_object_downgrade(vm_object_t);
382 int vm_quickcolor(void);
383
384 #endif                          /* _KERNEL */
385
386 #endif                          /* _VM_VM_OBJECT_H_ */