xref: /freebsd/sys/vm/vm_map.h (revision 98edb3e17869504d0ada58932efa96b71f899181)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. 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  *	@(#)vm_map.h	8.9 (Berkeley) 5/17/95
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  * $Id: vm_map.h,v 1.42 1999/06/19 18:42:51 alc Exp $
65  */
66 
67 /*
68  *	Virtual memory map module definitions.
69  */
70 
71 #ifndef	_VM_MAP_
72 #define	_VM_MAP_
73 
74 /*
75  *	Types defined:
76  *
77  *	vm_map_t		the high-level address map data structure.
78  *	vm_map_entry_t		an entry in an address map.
79  */
80 
81 /*
82  *	Objects which live in maps may be either VM objects, or
83  *	another map (called a "sharing map") which denotes read-write
84  *	sharing with other maps.
85  */
86 
87 union vm_map_object {
88 	struct vm_object *vm_object;	/* object object */
89 	struct vm_map *sub_map;		/* belongs to another map */
90 };
91 
92 /*
93  *	Address map entries consist of start and end addresses,
94  *	a VM object (or sharing map) and offset into that object,
95  *	and user-exported inheritance and protection information.
96  *	Also included is control information for virtual copy operations.
97  */
98 struct vm_map_entry {
99 	struct vm_map_entry *prev;	/* previous entry */
100 	struct vm_map_entry *next;	/* next entry */
101 	vm_offset_t start;		/* start address */
102 	vm_offset_t end;		/* end address */
103 	vm_offset_t avail_ssize;	/* amt can grow if this is a stack */
104 	union vm_map_object object;	/* object I point to */
105 	vm_ooffset_t offset;		/* offset into object */
106 	u_char eflags;			/* map entry flags */
107 	/* Only in task maps: */
108 	vm_prot_t protection;		/* protection code */
109 	vm_prot_t max_protection;	/* maximum protection */
110 	vm_inherit_t inheritance;	/* inheritance */
111 	int wired_count;		/* can be paged if = 0 */
112 };
113 
114 #define MAP_ENTRY_UNUSED_01		0x1
115 #define MAP_ENTRY_IS_SUB_MAP		0x2
116 #define MAP_ENTRY_COW			0x4
117 #define MAP_ENTRY_NEEDS_COPY		0x8
118 #define MAP_ENTRY_NOFAULT		0x10
119 #define MAP_ENTRY_USER_WIRED		0x20
120 
121 /*
122  *	Maps are doubly-linked lists of map entries, kept sorted
123  *	by address.  A single hint is provided to start
124  *	searches again from the last successful search,
125  *	insertion, or removal.
126  */
127 struct vm_map {
128 	struct lock lock;		/* Lock for map data */
129 	struct vm_map_entry header;	/* List of entries */
130 	int nentries;			/* Number of entries */
131 	vm_size_t size;			/* virtual size */
132 	unsigned char	system_map;			/* Am I a system map? */
133 	vm_map_entry_t hint;		/* hint for quick lookups */
134 	unsigned int timestamp;		/* Version number */
135 	vm_map_entry_t first_free;	/* First free space hint */
136 	struct pmap *pmap;		/* Physical map */
137 #define	min_offset		header.start
138 #define max_offset		header.end
139 };
140 
141 /*
142  * Shareable process virtual address space.
143  * May eventually be merged with vm_map.
144  * Several fields are temporary (text, data stuff).
145  */
146 struct vmspace {
147 	struct vm_map vm_map;	/* VM address map */
148 	struct pmap vm_pmap;	/* private physical map */
149 	int vm_refcnt;		/* number of references */
150 	caddr_t vm_shm;		/* SYS5 shared memory private data XXX */
151 /* we copy from vm_startcopy to the end of the structure on fork */
152 #define vm_startcopy vm_rssize
153 	segsz_t vm_rssize;	/* current resident set size in pages */
154 	segsz_t vm_swrss;	/* resident set size before last swap */
155 	segsz_t vm_tsize;	/* text size (pages) XXX */
156 	segsz_t vm_dsize;	/* data size (pages) XXX */
157 	segsz_t vm_ssize;	/* stack size (pages) */
158 	caddr_t vm_taddr;	/* user virtual address of text XXX */
159 	caddr_t vm_daddr;	/* user virtual address of data XXX */
160 	caddr_t vm_maxsaddr;	/* user VA at max stack growth */
161 	caddr_t vm_minsaddr;	/* user VA at max stack growth */
162 };
163 
164 /*
165  *	Macros:		vm_map_lock, etc.
166  *	Function:
167  *		Perform locking on the data portion of a map.
168  */
169 
170 #define	vm_map_lock_drain_interlock(map) { \
171 	lockmgr(&(map)->lock, LK_DRAIN|LK_INTERLOCK, \
172 		&(map)->ref_lock, curproc); \
173 	(map)->timestamp++; \
174 }
175 
176 #ifdef DIAGNOSTIC
177 /* #define MAP_LOCK_DIAGNOSTIC 1 */
178 #ifdef MAP_LOCK_DIAGNOSTIC
179 #define	vm_map_lock(map) { \
180 	printf ("locking map LK_EXCLUSIVE: 0x%x\n", map); \
181 	if (lockmgr(&(map)->lock, LK_EXCLUSIVE, (void *)0, curproc) != 0) { \
182 		panic("vm_map_lock: failed to get lock"); \
183 	} \
184 	(map)->timestamp++; \
185 }
186 #else
187 #define	vm_map_lock(map) { \
188 	if (lockmgr(&(map)->lock, LK_EXCLUSIVE, (void *)0, curproc) != 0) { \
189 		panic("vm_map_lock: failed to get lock"); \
190 	} \
191 	(map)->timestamp++; \
192 }
193 #endif
194 #else
195 #define	vm_map_lock(map) { \
196 	lockmgr(&(map)->lock, LK_EXCLUSIVE, (void *)0, curproc); \
197 	(map)->timestamp++; \
198 }
199 #endif /* DIAGNOSTIC */
200 
201 #if defined(MAP_LOCK_DIAGNOSTIC)
202 #define	vm_map_unlock(map) \
203 	do { \
204 		printf ("locking map LK_RELEASE: 0x%x\n", map); \
205 		lockmgr(&(map)->lock, LK_RELEASE, (void *)0, curproc); \
206 	} while (0);
207 #define	vm_map_lock_read(map) \
208 	do { \
209 		printf ("locking map LK_SHARED: 0x%x\n", map); \
210 		lockmgr(&(map)->lock, LK_SHARED, (void *)0, curproc); \
211 	} while (0);
212 #define	vm_map_unlock_read(map) \
213 	do { \
214 		printf ("locking map LK_RELEASE: 0x%x\n", map); \
215 		lockmgr(&(map)->lock, LK_RELEASE, (void *)0, curproc); \
216 	} while (0);
217 #else
218 #define	vm_map_unlock(map) \
219 	lockmgr(&(map)->lock, LK_RELEASE, (void *)0, curproc);
220 #define	vm_map_lock_read(map) \
221 	lockmgr(&(map)->lock, LK_SHARED, (void *)0, curproc);
222 #define	vm_map_unlock_read(map) \
223 	lockmgr(&(map)->lock, LK_RELEASE, (void *)0, curproc);
224 #endif
225 
226 static __inline__ int
227 _vm_map_lock_upgrade(vm_map_t map, struct proc *p) {
228 	int error;
229 #if defined(MAP_LOCK_DIAGNOSTIC)
230 	printf("locking map LK_EXCLUPGRADE: 0x%x\n", map);
231 #endif
232 	error = lockmgr(&map->lock, LK_EXCLUPGRADE, (void *)0, p);
233 	if (error == 0)
234 		map->timestamp++;
235 	return error;
236 }
237 
238 #define vm_map_lock_upgrade(map) _vm_map_lock_upgrade(map, curproc)
239 
240 #if defined(MAP_LOCK_DIAGNOSTIC)
241 #define vm_map_lock_downgrade(map) \
242 	do { \
243 		printf ("locking map LK_DOWNGRADE: 0x%x\n", map); \
244 		lockmgr(&(map)->lock, LK_DOWNGRADE, (void *)0, curproc); \
245 	} while (0);
246 #else
247 #define vm_map_lock_downgrade(map) \
248 	lockmgr(&(map)->lock, LK_DOWNGRADE, (void *)0, curproc);
249 #endif
250 
251 #define vm_map_set_recursive(map) { \
252 	simple_lock(&(map)->lock.lk_interlock); \
253 	(map)->lock.lk_flags |= LK_CANRECURSE; \
254 	simple_unlock(&(map)->lock.lk_interlock); \
255 }
256 #define vm_map_clear_recursive(map) { \
257 	simple_lock(&(map)->lock.lk_interlock); \
258 	(map)->lock.lk_flags &= ~LK_CANRECURSE; \
259 	simple_unlock(&(map)->lock.lk_interlock); \
260 }
261 
262 /*
263  *	Functions implemented as macros
264  */
265 #define		vm_map_min(map)		((map)->min_offset)
266 #define		vm_map_max(map)		((map)->max_offset)
267 #define		vm_map_pmap(map)	((map)->pmap)
268 
269 static __inline struct pmap *
270 vmspace_pmap(struct vmspace *vmspace)
271 {
272 	return &vmspace->vm_pmap;
273 }
274 
275 static __inline long
276 vmspace_resident_count(struct vmspace *vmspace)
277 {
278 	return pmap_resident_count(vmspace_pmap(vmspace));
279 }
280 
281 /* XXX: number of kernel maps and entries to statically allocate */
282 #define MAX_KMAP	10
283 #define	MAX_KMAPENT	128
284 #define	MAX_MAPENT	128
285 
286 /*
287  * Copy-on-write flags for vm_map operations
288  */
289 #define MAP_UNUSED_01		0x1
290 #define MAP_COPY_ON_WRITE	0x2
291 #define MAP_NOFAULT		0x4
292 #define MAP_PREFAULT		0x8
293 #define MAP_PREFAULT_PARTIAL	0x10
294 
295 /*
296  * vm_fault option flags
297  */
298 #define VM_FAULT_NORMAL 0		/* Nothing special */
299 #define VM_FAULT_CHANGE_WIRING 1	/* Change the wiring as appropriate */
300 #define VM_FAULT_USER_WIRE 2		/* Likewise, but for user purposes */
301 #define VM_FAULT_WIRE_MASK (VM_FAULT_CHANGE_WIRING|VM_FAULT_USER_WIRE)
302 #define	VM_FAULT_HOLD 4			/* Hold the page */
303 #define VM_FAULT_DIRTY 8		/* Dirty the page */
304 
305 #ifdef KERNEL
306 boolean_t vm_map_check_protection __P((vm_map_t, vm_offset_t, vm_offset_t, vm_prot_t));
307 struct pmap;
308 vm_map_t vm_map_create __P((struct pmap *, vm_offset_t, vm_offset_t));
309 int vm_map_delete __P((vm_map_t, vm_offset_t, vm_offset_t));
310 int vm_map_find __P((vm_map_t, vm_object_t, vm_ooffset_t, vm_offset_t *, vm_size_t, boolean_t, vm_prot_t, vm_prot_t, int));
311 int vm_map_findspace __P((vm_map_t, vm_offset_t, vm_size_t, vm_offset_t *));
312 int vm_map_inherit __P((vm_map_t, vm_offset_t, vm_offset_t, vm_inherit_t));
313 void vm_map_init __P((struct vm_map *, vm_offset_t, vm_offset_t));
314 int vm_map_insert __P((vm_map_t, vm_object_t, vm_ooffset_t, vm_offset_t, vm_offset_t, vm_prot_t, vm_prot_t, int));
315 int vm_map_lookup __P((vm_map_t *, vm_offset_t, vm_prot_t, vm_map_entry_t *, vm_object_t *,
316     vm_pindex_t *, vm_prot_t *, boolean_t *));
317 void vm_map_lookup_done __P((vm_map_t, vm_map_entry_t));
318 boolean_t vm_map_lookup_entry __P((vm_map_t, vm_offset_t, vm_map_entry_t *));
319 int vm_map_pageable __P((vm_map_t, vm_offset_t, vm_offset_t, boolean_t));
320 int vm_map_user_pageable __P((vm_map_t, vm_offset_t, vm_offset_t, boolean_t));
321 int vm_map_clean __P((vm_map_t, vm_offset_t, vm_offset_t, boolean_t, boolean_t));
322 int vm_map_protect __P((vm_map_t, vm_offset_t, vm_offset_t, vm_prot_t, boolean_t));
323 int vm_map_remove __P((vm_map_t, vm_offset_t, vm_offset_t));
324 void vm_map_startup __P((void));
325 int vm_map_submap __P((vm_map_t, vm_offset_t, vm_offset_t, vm_map_t));
326 void vm_map_madvise __P((vm_map_t, vm_offset_t, vm_offset_t, int));
327 void vm_map_simplify_entry __P((vm_map_t, vm_map_entry_t));
328 void vm_init2 __P((void));
329 int vm_uiomove __P((vm_map_t, vm_object_t, off_t, int, vm_offset_t, int *));
330 void vm_freeze_copyopts __P((vm_object_t, vm_pindex_t, vm_pindex_t));
331 int vm_map_stack __P((vm_map_t, vm_offset_t, vm_size_t, vm_prot_t, vm_prot_t, int));
332 int vm_map_growstack __P((struct proc *p, vm_offset_t addr));
333 
334 #endif
335 #endif				/* _VM_MAP_ */
336