xref: /netbsd-src/sys/uvm/uvm_map.h (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: uvm_map.h,v 1.49 2005/12/24 20:45:10 perry Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993, The Regents of the University of California.
6  *
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * The Mach Operating System project at Carnegie-Mellon University.
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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by Charles D. Cranor,
23  *      Washington University, the University of California, Berkeley and
24  *      its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)vm_map.h    8.3 (Berkeley) 3/15/94
42  * from: Id: uvm_map.h,v 1.1.2.3 1998/02/07 01:16:55 chs Exp
43  *
44  *
45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46  * All rights reserved.
47  *
48  * Permission to use, copy, modify and distribute this software and
49  * its documentation is hereby granted, provided that both the copyright
50  * notice and this permission notice appear in all copies of the
51  * software, derivative works or modified versions, and any portions
52  * thereof, and that both notices appear in supporting documentation.
53  *
54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57  *
58  * Carnegie Mellon requests users of this software to return to
59  *
60  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61  *  School of Computer Science
62  *  Carnegie Mellon University
63  *  Pittsburgh PA 15213-3890
64  *
65  * any improvements or extensions that they make and grant Carnegie the
66  * rights to redistribute these changes.
67  */
68 
69 #ifndef _UVM_UVM_MAP_H_
70 #define _UVM_UVM_MAP_H_
71 
72 /*
73  * uvm_map.h
74  */
75 
76 #ifdef _KERNEL
77 
78 /*
79  * macros
80  */
81 
82 /*
83  * UVM_MAP_CLIP_START: ensure that the entry begins at or after
84  * the starting address, if it doesn't we split the entry.
85  *
86  * => map must be locked by caller
87  */
88 
89 #define UVM_MAP_CLIP_START(MAP,ENTRY,VA,UMR) { \
90 	if ((VA) > (ENTRY)->start) uvm_map_clip_start(MAP,ENTRY,VA,UMR); }
91 
92 /*
93  * UVM_MAP_CLIP_END: ensure that the entry ends at or before
94  *      the ending address, if it does't we split the entry.
95  *
96  * => map must be locked by caller
97  */
98 
99 #define UVM_MAP_CLIP_END(MAP,ENTRY,VA,UMR) { \
100 	if ((VA) < (ENTRY)->end) uvm_map_clip_end(MAP,ENTRY,VA,UMR); }
101 
102 /*
103  * extract flags
104  */
105 #define UVM_EXTRACT_REMOVE	0x1	/* remove mapping from old map */
106 #define UVM_EXTRACT_CONTIG	0x2	/* try to keep it contig */
107 #define UVM_EXTRACT_QREF	0x4	/* use quick refs */
108 #define UVM_EXTRACT_FIXPROT	0x8	/* set prot to maxprot as we go */
109 
110 #endif /* _KERNEL */
111 
112 #include <sys/tree.h>
113 #include <sys/pool.h>
114 
115 #include <uvm/uvm_anon.h>
116 
117 /*
118  * Address map entries consist of start and end addresses,
119  * a VM object (or sharing map) and offset into that object,
120  * and user-exported inheritance and protection information.
121  * Also included is control information for virtual copy operations.
122  */
123 struct vm_map_entry {
124 	RB_ENTRY(vm_map_entry)	rb_entry;	/* tree information */
125 	vaddr_t			ownspace;	/* free space after */
126 	vaddr_t			space;		/* space in subtree */
127 	struct vm_map_entry	*prev;		/* previous entry */
128 	struct vm_map_entry	*next;		/* next entry */
129 	vaddr_t			start;		/* start address */
130 	vaddr_t			end;		/* end address */
131 	union {
132 		struct uvm_object *uvm_obj;	/* uvm object */
133 		struct vm_map	*sub_map;	/* belongs to another map */
134 	} object;				/* object I point to */
135 	voff_t			offset;		/* offset into object */
136 	int			etype;		/* entry type */
137 	vm_prot_t		protection;	/* protection code */
138 	vm_prot_t		max_protection;	/* maximum protection */
139 	vm_inherit_t		inheritance;	/* inheritance */
140 	int			wired_count;	/* can be paged if == 0 */
141 	struct vm_aref		aref;		/* anonymous overlay */
142 	int			advice;		/* madvise advice */
143 #define uvm_map_entry_stop_copy flags
144 	u_int8_t		flags;		/* flags */
145 
146 #define	UVM_MAP_KERNEL		0x01		/* kernel map entry */
147 #define	UVM_MAP_KMAPENT		0x02		/* contains map entries */
148 #define	UVM_MAP_FIRST		0x04		/* the first special entry */
149 #define	UVM_MAP_QUANTUM		0x08		/* allocated with
150 						 * UVM_FLAG_QUANTUM */
151 #define	UVM_MAP_NOMERGE		0x10		/* this entry is not mergable */
152 
153 };
154 
155 #define	VM_MAPENT_ISWIRED(entry)	((entry)->wired_count != 0)
156 
157 /*
158  *	Maps are doubly-linked lists of map entries, kept sorted
159  *	by address.  A single hint is provided to start
160  *	searches again from the last successful search,
161  *	insertion, or removal.
162  *
163  *	LOCKING PROTOCOL NOTES:
164  *	-----------------------
165  *
166  *	VM map locking is a little complicated.  There are both shared
167  *	and exclusive locks on maps.  However, it is sometimes required
168  *	to downgrade an exclusive lock to a shared lock, and upgrade to
169  *	an exclusive lock again (to perform error recovery).  However,
170  *	another thread *must not* queue itself to receive an exclusive
171  *	lock while before we upgrade back to exclusive, otherwise the
172  *	error recovery becomes extremely difficult, if not impossible.
173  *
174  *	In order to prevent this scenario, we introduce the notion of
175  *	a `busy' map.  A `busy' map is read-locked, but other threads
176  *	attempting to write-lock wait for this flag to clear before
177  *	entering the lock manager.  A map may only be marked busy
178  *	when the map is write-locked (and then the map must be downgraded
179  *	to read-locked), and may only be marked unbusy by the thread
180  *	which marked it busy (holding *either* a read-lock or a
181  *	write-lock, the latter being gained by an upgrade).
182  *
183  *	Access to the map `flags' member is controlled by the `flags_lock'
184  *	simple lock.  Note that some flags are static (set once at map
185  *	creation time, and never changed), and thus require no locking
186  *	to check those flags.  All flags which are r/w must be set or
187  *	cleared while the `flags_lock' is asserted.  Additional locking
188  *	requirements are:
189  *
190  *		VM_MAP_PAGEABLE		r/o static flag; no locking required
191  *
192  *		VM_MAP_INTRSAFE		r/o static flag; no locking required
193  *
194  *		VM_MAP_WIREFUTURE	r/w; may only be set or cleared when
195  *					map is write-locked.  may be tested
196  *					without asserting `flags_lock'.
197  *
198  *		VM_MAP_BUSY		r/w; may only be set when map is
199  *					write-locked, may only be cleared by
200  *					thread which set it, map read-locked
201  *					or write-locked.  must be tested
202  *					while `flags_lock' is asserted.
203  *
204  *		VM_MAP_WANTLOCK		r/w; may only be set when the map
205  *					is busy, and thread is attempting
206  *					to write-lock.  must be tested
207  *					while `flags_lock' is asserted.
208  *
209  *		VM_MAP_DYING		r/o; set when a vmspace is being
210  *					destroyed to indicate that updates
211  *					to the pmap can be skipped.
212  *
213  *		VM_MAP_TOPDOWN		r/o; set when the vmspace is
214  *					created if the unspecified map
215  *					allocations are to be arranged in
216  *					a "top down" manner.
217  */
218 struct vm_map {
219 	struct pmap *		pmap;		/* Physical map */
220 	struct lock		lock;		/* Lock for map data */
221 	RB_HEAD(uvm_tree, vm_map_entry) rbhead;	/* Tree for entries */
222 	struct vm_map_entry	header;		/* List of entries */
223 	int			nentries;	/* Number of entries */
224 	vsize_t			size;		/* virtual size */
225 	int			ref_count;	/* Reference count */
226 	struct simplelock	ref_lock;	/* Lock for ref_count field */
227 	struct vm_map_entry *	hint;		/* hint for quick lookups */
228 	struct simplelock	hint_lock;	/* lock for hint storage */
229 	struct vm_map_entry *	first_free;	/* First free space hint */
230 	int			flags;		/* flags */
231 	struct simplelock	flags_lock;	/* Lock for flags field */
232 	unsigned int		timestamp;	/* Version number */
233 };
234 
235 #if defined(_KERNEL)
236 struct vm_map_kernel {
237 	struct vm_map vmk_map;
238 	LIST_HEAD(, uvm_kmapent_hdr) vmk_kentry_free;
239 			/* Freelist of map entry */
240 	struct vm_map_entry	*vmk_merged_entries;
241 			/* Merged entries, kept for later splitting */
242 
243 #if !defined(PMAP_MAP_POOLPAGE)
244 	struct pool vmk_vacache; /* kva cache */
245 	struct pool_allocator vmk_vacache_allocator; /* ... and its allocator */
246 #endif
247 };
248 #endif /* defined(_KERNEL) */
249 
250 #define	VM_MAP_IS_KERNEL(map)	(vm_map_pmap(map) == pmap_kernel())
251 
252 /* vm_map flags */
253 #define	VM_MAP_PAGEABLE		0x01		/* ro: entries are pageable */
254 #define	VM_MAP_INTRSAFE		0x02		/* ro: interrupt safe map */
255 #define	VM_MAP_WIREFUTURE	0x04		/* rw: wire future mappings */
256 #define	VM_MAP_BUSY		0x08		/* rw: map is busy */
257 #define	VM_MAP_WANTLOCK		0x10		/* rw: want to write-lock */
258 #define	VM_MAP_DYING		0x20		/* rw: map is being destroyed */
259 #define	VM_MAP_TOPDOWN		0x40		/* ro: arrange map top-down */
260 #define	VM_MAP_VACACHE		0x80		/* ro: use kva cache */
261 #define	VM_MAP_WANTVA		0x100		/* rw: want va */
262 
263 #ifdef _KERNEL
264 struct uvm_mapent_reservation {
265 	struct vm_map_entry *umr_entries[2];
266 	int umr_nentries;
267 };
268 #define	UMR_EMPTY(umr)		((umr) == NULL || (umr)->umr_nentries == 0)
269 #define	UMR_GETENTRY(umr)	((umr)->umr_entries[--(umr)->umr_nentries])
270 #define	UMR_PUTENTRY(umr, ent)	\
271 	(umr)->umr_entries[(umr)->umr_nentries++] = (ent)
272 
273 struct uvm_map_args {
274 	struct vm_map_entry *uma_prev;
275 
276 	vaddr_t uma_start;
277 	vsize_t uma_size;
278 
279 	struct uvm_object *uma_uobj;
280 	voff_t uma_uoffset;
281 
282 	uvm_flag_t uma_flags;
283 };
284 #endif /* _KERNEL */
285 
286 #ifdef _KERNEL
287 #define	vm_map_modflags(map, set, clear)				\
288 do {									\
289 	simple_lock(&(map)->flags_lock);				\
290 	(map)->flags = ((map)->flags | (set)) & ~(clear);		\
291 	simple_unlock(&(map)->flags_lock);				\
292 } while (/*CONSTCOND*/ 0)
293 #endif /* _KERNEL */
294 
295 /*
296  * handle inline options
297  */
298 
299 #ifdef UVM_MAP_INLINE
300 #define MAP_INLINE static inline
301 #else
302 #define MAP_INLINE /* nothing */
303 #endif /* UVM_MAP_INLINE */
304 
305 /*
306  * globals:
307  */
308 
309 #ifdef _KERNEL
310 
311 #ifdef PMAP_GROWKERNEL
312 extern vaddr_t	uvm_maxkaddr;
313 #endif
314 
315 /*
316  * protos: the following prototypes define the interface to vm_map
317  */
318 
319 MAP_INLINE
320 void		uvm_map_deallocate(struct vm_map *);
321 
322 int		uvm_map_clean(struct vm_map *, vaddr_t, vaddr_t, int);
323 void		uvm_map_clip_start(struct vm_map *, struct vm_map_entry *,
324 		    vaddr_t, struct uvm_mapent_reservation *);
325 void		uvm_map_clip_end(struct vm_map *, struct vm_map_entry *,
326 		    vaddr_t, struct uvm_mapent_reservation *);
327 MAP_INLINE
328 struct vm_map	*uvm_map_create(pmap_t, vaddr_t, vaddr_t, int);
329 int		uvm_map_extract(struct vm_map *, vaddr_t, vsize_t,
330 		    struct vm_map *, vaddr_t *, int);
331 struct vm_map_entry *
332 		uvm_map_findspace(struct vm_map *, vaddr_t, vsize_t,
333 		    vaddr_t *, struct uvm_object *, voff_t, vsize_t, int);
334 int		uvm_map_inherit(struct vm_map *, vaddr_t, vaddr_t,
335 		    vm_inherit_t);
336 int		uvm_map_advice(struct vm_map *, vaddr_t, vaddr_t, int);
337 void		uvm_map_init(void);
338 boolean_t	uvm_map_lookup_entry(struct vm_map *, vaddr_t,
339 		    struct vm_map_entry **);
340 MAP_INLINE
341 void		uvm_map_reference(struct vm_map *);
342 int		uvm_map_replace(struct vm_map *, vaddr_t, vaddr_t,
343 		    struct vm_map_entry *, int);
344 int		uvm_map_reserve(struct vm_map *, vsize_t, vaddr_t, vsize_t,
345 		    vaddr_t *);
346 void		uvm_map_setup(struct vm_map *, vaddr_t, vaddr_t, int);
347 void		uvm_map_setup_kernel(struct vm_map_kernel *,
348 		    vaddr_t, vaddr_t, int);
349 MAP_INLINE
350 struct vm_map_kernel *
351 		vm_map_to_kernel(struct vm_map *);
352 int		uvm_map_submap(struct vm_map *, vaddr_t, vaddr_t,
353 		    struct vm_map *);
354 MAP_INLINE
355 void		uvm_unmap1(struct vm_map *, vaddr_t, vaddr_t, int);
356 #define	uvm_unmap(map, s, e)	uvm_unmap1((map), (s), (e), 0)
357 void		uvm_unmap_detach(struct vm_map_entry *,int);
358 void		uvm_unmap_remove(struct vm_map *, vaddr_t, vaddr_t,
359 		    struct vm_map_entry **, struct uvm_mapent_reservation *,
360 		    int);
361 
362 int		uvm_map_prepare(struct vm_map *, vaddr_t, vsize_t,
363 		    struct uvm_object *, voff_t, vsize_t, uvm_flag_t,
364 		    struct uvm_map_args *);
365 int		uvm_map_enter(struct vm_map *, const struct uvm_map_args *,
366 		    struct vm_map_entry *);
367 
368 int		uvm_mapent_reserve(struct vm_map *,
369 		    struct uvm_mapent_reservation *, int, int);
370 void		uvm_mapent_unreserve(struct vm_map *,
371 		    struct uvm_mapent_reservation *);
372 
373 int		uvm_mapent_trymerge(struct vm_map *,
374 		    struct vm_map_entry *, int);
375 #define	UVM_MERGE_COPYING	1
376 
377 #endif /* _KERNEL */
378 
379 /*
380  * VM map locking operations:
381  *
382  *	These operations perform locking on the data portion of the
383  *	map.
384  *
385  *	vm_map_lock_try: try to lock a map, failing if it is already locked.
386  *
387  *	vm_map_lock: acquire an exclusive (write) lock on a map.
388  *
389  *	vm_map_lock_read: acquire a shared (read) lock on a map.
390  *
391  *	vm_map_unlock: release an exclusive lock on a map.
392  *
393  *	vm_map_unlock_read: release a shared lock on a map.
394  *
395  *	vm_map_downgrade: downgrade an exclusive lock to a shared lock.
396  *
397  *	vm_map_upgrade: upgrade a shared lock to an exclusive lock.
398  *
399  *	vm_map_busy: mark a map as busy.
400  *
401  *	vm_map_unbusy: clear busy status on a map.
402  *
403  * Note that "intrsafe" maps use only exclusive, spin locks.  We simply
404  * use the sleep lock's interlock for this.
405  */
406 
407 #ifdef _KERNEL
408 /* XXX: clean up later */
409 #include <sys/time.h>
410 #include <sys/proc.h>	/* for tsleep(), wakeup() */
411 #include <sys/systm.h>	/* for panic() */
412 
413 static inline boolean_t	vm_map_lock_try(struct vm_map *);
414 static inline void		vm_map_lock(struct vm_map *);
415 extern const char vmmapbsy[];
416 
417 static inline boolean_t
418 vm_map_lock_try(struct vm_map *map)
419 {
420 	boolean_t rv;
421 
422 	if (map->flags & VM_MAP_INTRSAFE)
423 		rv = simple_lock_try(&map->lock.lk_interlock);
424 	else {
425 		simple_lock(&map->flags_lock);
426 		if (map->flags & VM_MAP_BUSY) {
427 			simple_unlock(&map->flags_lock);
428 			return (FALSE);
429 		}
430 		rv = (lockmgr(&map->lock, LK_EXCLUSIVE|LK_NOWAIT|LK_INTERLOCK,
431 		    &map->flags_lock) == 0);
432 	}
433 
434 	if (rv)
435 		map->timestamp++;
436 
437 	return (rv);
438 }
439 
440 static inline void
441 vm_map_lock(struct vm_map *map)
442 {
443 	int error;
444 
445 	if (map->flags & VM_MAP_INTRSAFE) {
446 		simple_lock(&map->lock.lk_interlock);
447 		return;
448 	}
449 
450  try_again:
451 	simple_lock(&map->flags_lock);
452 	while (map->flags & VM_MAP_BUSY) {
453 		map->flags |= VM_MAP_WANTLOCK;
454 		ltsleep(&map->flags, PVM, vmmapbsy, 0, &map->flags_lock);
455 	}
456 
457 	error = lockmgr(&map->lock, LK_EXCLUSIVE|LK_SLEEPFAIL|LK_INTERLOCK,
458 	    &map->flags_lock);
459 
460 	if (error) {
461 		KASSERT(error == ENOLCK);
462 		goto try_again;
463 	}
464 
465 	(map)->timestamp++;
466 }
467 
468 #ifdef DIAGNOSTIC
469 #define	vm_map_lock_read(map)						\
470 do {									\
471 	if ((map)->flags & VM_MAP_INTRSAFE)				\
472 		panic("vm_map_lock_read: intrsafe Map");		\
473 	(void) lockmgr(&(map)->lock, LK_SHARED, NULL);			\
474 } while (/*CONSTCOND*/ 0)
475 #else
476 #define	vm_map_lock_read(map)						\
477 	(void) lockmgr(&(map)->lock, LK_SHARED, NULL)
478 #endif
479 
480 #define	vm_map_unlock(map)						\
481 do {									\
482 	if ((map)->flags & VM_MAP_INTRSAFE)				\
483 		simple_unlock(&(map)->lock.lk_interlock);		\
484 	else								\
485 		(void) lockmgr(&(map)->lock, LK_RELEASE, NULL);		\
486 } while (/*CONSTCOND*/ 0)
487 
488 #define	vm_map_unlock_read(map)						\
489 	(void) lockmgr(&(map)->lock, LK_RELEASE, NULL)
490 
491 #define	vm_map_downgrade(map)						\
492 	(void) lockmgr(&(map)->lock, LK_DOWNGRADE, NULL)
493 
494 #ifdef DIAGNOSTIC
495 #define	vm_map_upgrade(map)						\
496 do {									\
497 	if (lockmgr(&(map)->lock, LK_UPGRADE, NULL) != 0)		\
498 		panic("vm_map_upgrade: failed to upgrade lock");	\
499 } while (/*CONSTCOND*/ 0)
500 #else
501 #define	vm_map_upgrade(map)						\
502 	(void) lockmgr(&(map)->lock, LK_UPGRADE, NULL)
503 #endif
504 
505 #define	vm_map_busy(map)						\
506 do {									\
507 	simple_lock(&(map)->flags_lock);				\
508 	(map)->flags |= VM_MAP_BUSY;					\
509 	simple_unlock(&(map)->flags_lock);				\
510 } while (/*CONSTCOND*/ 0)
511 
512 #define	vm_map_unbusy(map)						\
513 do {									\
514 	int oflags;							\
515 									\
516 	simple_lock(&(map)->flags_lock);				\
517 	oflags = (map)->flags;						\
518 	(map)->flags &= ~(VM_MAP_BUSY|VM_MAP_WANTLOCK);			\
519 	simple_unlock(&(map)->flags_lock);				\
520 	if (oflags & VM_MAP_WANTLOCK)					\
521 		wakeup(&(map)->flags);					\
522 } while (/*CONSTCOND*/ 0)
523 
524 #endif /* _KERNEL */
525 
526 /*
527  *	Functions implemented as macros
528  */
529 #define		vm_map_min(map)		((map)->header.end)
530 #define		vm_map_max(map)		((map)->header.start)
531 #define		vm_map_setmin(map, v)	((map)->header.end = (v))
532 #define		vm_map_setmax(map, v)	((map)->header.start = (v))
533 
534 #define		vm_map_pmap(map)	((map)->pmap)
535 
536 #endif /* _UVM_UVM_MAP_H_ */
537