xref: /netbsd-src/sys/uvm/uvm_map.h (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: uvm_map.h,v 1.54 2006/05/25 14:27:28 yamt 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	0x01	/* remove mapping from old map */
106 #define UVM_EXTRACT_CONTIG	0x02	/* try to keep it contig */
107 #define UVM_EXTRACT_QREF	0x04	/* use quick refs */
108 #define UVM_EXTRACT_FIXPROT	0x08	/* set prot to maxprot as we go */
109 #define UVM_EXTRACT_RESERVED	0x10	/* caller did uvm_map_reserve() */
110 
111 #endif /* _KERNEL */
112 
113 #include <sys/tree.h>
114 #include <sys/pool.h>
115 
116 #include <uvm/uvm_anon.h>
117 
118 /*
119  * Address map entries consist of start and end addresses,
120  * a VM object (or sharing map) and offset into that object,
121  * and user-exported inheritance and protection information.
122  * Also included is control information for virtual copy operations.
123  */
124 struct vm_map_entry {
125 	RB_ENTRY(vm_map_entry)	rb_entry;	/* tree information */
126 	vaddr_t			ownspace;	/* free space after */
127 	vaddr_t			space;		/* space in subtree */
128 	struct vm_map_entry	*prev;		/* previous entry */
129 	struct vm_map_entry	*next;		/* next entry */
130 	vaddr_t			start;		/* start address */
131 	vaddr_t			end;		/* end address */
132 	union {
133 		struct uvm_object *uvm_obj;	/* uvm object */
134 		struct vm_map	*sub_map;	/* belongs to another map */
135 	} object;				/* object I point to */
136 	voff_t			offset;		/* offset into object */
137 	int			etype;		/* entry type */
138 	vm_prot_t		protection;	/* protection code */
139 	vm_prot_t		max_protection;	/* maximum protection */
140 	vm_inherit_t		inheritance;	/* inheritance */
141 	int			wired_count;	/* can be paged if == 0 */
142 	struct vm_aref		aref;		/* anonymous overlay */
143 	int			advice;		/* madvise advice */
144 #define uvm_map_entry_stop_copy flags
145 	u_int8_t		flags;		/* flags */
146 
147 #define	UVM_MAP_KERNEL		0x01		/* kernel map entry */
148 #define	UVM_MAP_KMAPENT		0x02		/* contains map entries */
149 #define	UVM_MAP_FIRST		0x04		/* the first special entry */
150 #define	UVM_MAP_QUANTUM		0x08		/* allocated with
151 						 * UVM_FLAG_QUANTUM */
152 #define	UVM_MAP_NOMERGE		0x10		/* this entry is not mergable */
153 
154 };
155 
156 #define	VM_MAPENT_ISWIRED(entry)	((entry)->wired_count != 0)
157 
158 /*
159  *	Maps are doubly-linked lists of map entries, kept sorted
160  *	by address.  A single hint is provided to start
161  *	searches again from the last successful search,
162  *	insertion, or removal.
163  *
164  *	LOCKING PROTOCOL NOTES:
165  *	-----------------------
166  *
167  *	VM map locking is a little complicated.  There are both shared
168  *	and exclusive locks on maps.  However, it is sometimes required
169  *	to downgrade an exclusive lock to a shared lock, and upgrade to
170  *	an exclusive lock again (to perform error recovery).  However,
171  *	another thread *must not* queue itself to receive an exclusive
172  *	lock while before we upgrade back to exclusive, otherwise the
173  *	error recovery becomes extremely difficult, if not impossible.
174  *
175  *	In order to prevent this scenario, we introduce the notion of
176  *	a `busy' map.  A `busy' map is read-locked, but other threads
177  *	attempting to write-lock wait for this flag to clear before
178  *	entering the lock manager.  A map may only be marked busy
179  *	when the map is write-locked (and then the map must be downgraded
180  *	to read-locked), and may only be marked unbusy by the thread
181  *	which marked it busy (holding *either* a read-lock or a
182  *	write-lock, the latter being gained by an upgrade).
183  *
184  *	Access to the map `flags' member is controlled by the `flags_lock'
185  *	simple lock.  Note that some flags are static (set once at map
186  *	creation time, and never changed), and thus require no locking
187  *	to check those flags.  All flags which are r/w must be set or
188  *	cleared while the `flags_lock' is asserted.  Additional locking
189  *	requirements are:
190  *
191  *		VM_MAP_PAGEABLE		r/o static flag; no locking required
192  *
193  *		VM_MAP_INTRSAFE		r/o static flag; no locking required
194  *
195  *		VM_MAP_WIREFUTURE	r/w; may only be set or cleared when
196  *					map is write-locked.  may be tested
197  *					without asserting `flags_lock'.
198  *
199  *		VM_MAP_BUSY		r/w; may only be set when map is
200  *					write-locked, may only be cleared by
201  *					thread which set it, map read-locked
202  *					or write-locked.  must be tested
203  *					while `flags_lock' is asserted.
204  *
205  *		VM_MAP_WANTLOCK		r/w; may only be set when the map
206  *					is busy, and thread is attempting
207  *					to write-lock.  must be tested
208  *					while `flags_lock' is asserted.
209  *
210  *		VM_MAP_DYING		r/o; set when a vmspace is being
211  *					destroyed to indicate that updates
212  *					to the pmap can be skipped.
213  *
214  *		VM_MAP_TOPDOWN		r/o; set when the vmspace is
215  *					created if the unspecified map
216  *					allocations are to be arranged in
217  *					a "top down" manner.
218  */
219 struct vm_map {
220 	struct pmap *		pmap;		/* Physical map */
221 	struct lock		lock;		/* Lock for map data */
222 	RB_HEAD(uvm_tree, vm_map_entry) rbhead;	/* Tree for entries */
223 	struct vm_map_entry	header;		/* List of entries */
224 	int			nentries;	/* Number of entries */
225 	vsize_t			size;		/* virtual size */
226 	int			ref_count;	/* Reference count */
227 	struct simplelock	ref_lock;	/* Lock for ref_count field */
228 	struct vm_map_entry *	hint;		/* hint for quick lookups */
229 	struct simplelock	hint_lock;	/* lock for hint storage */
230 	struct vm_map_entry *	first_free;	/* First free space hint */
231 	int			flags;		/* flags */
232 	struct simplelock	flags_lock;	/* Lock for flags field */
233 	unsigned int		timestamp;	/* Version number */
234 };
235 
236 #if defined(_KERNEL)
237 
238 #include <sys/callback.h>
239 
240 struct vm_map_kernel {
241 	struct vm_map vmk_map;
242 	LIST_HEAD(, uvm_kmapent_hdr) vmk_kentry_free;
243 			/* Freelist of map entry */
244 	struct vm_map_entry	*vmk_merged_entries;
245 			/* Merged entries, kept for later splitting */
246 
247 	struct callback_head vmk_reclaim_callback;
248 #if !defined(PMAP_MAP_POOLPAGE)
249 	struct pool vmk_vacache; /* kva cache */
250 	struct pool_allocator vmk_vacache_allocator; /* ... and its allocator */
251 #endif
252 };
253 #endif /* defined(_KERNEL) */
254 
255 #define	VM_MAP_IS_KERNEL(map)	(vm_map_pmap(map) == pmap_kernel())
256 
257 /* vm_map flags */
258 #define	VM_MAP_PAGEABLE		0x01		/* ro: entries are pageable */
259 #define	VM_MAP_INTRSAFE		0x02		/* ro: interrupt safe map */
260 #define	VM_MAP_WIREFUTURE	0x04		/* rw: wire future mappings */
261 #define	VM_MAP_BUSY		0x08		/* rw: map is busy */
262 #define	VM_MAP_WANTLOCK		0x10		/* rw: want to write-lock */
263 #define	VM_MAP_DYING		0x20		/* rw: map is being destroyed */
264 #define	VM_MAP_TOPDOWN		0x40		/* ro: arrange map top-down */
265 #define	VM_MAP_VACACHE		0x80		/* ro: use kva cache */
266 #define	VM_MAP_WANTVA		0x100		/* rw: want va */
267 
268 #ifdef _KERNEL
269 struct uvm_mapent_reservation {
270 	struct vm_map_entry *umr_entries[2];
271 	int umr_nentries;
272 };
273 #define	UMR_EMPTY(umr)		((umr) == NULL || (umr)->umr_nentries == 0)
274 #define	UMR_GETENTRY(umr)	((umr)->umr_entries[--(umr)->umr_nentries])
275 #define	UMR_PUTENTRY(umr, ent)	\
276 	(umr)->umr_entries[(umr)->umr_nentries++] = (ent)
277 
278 struct uvm_map_args {
279 	struct vm_map_entry *uma_prev;
280 
281 	vaddr_t uma_start;
282 	vsize_t uma_size;
283 
284 	struct uvm_object *uma_uobj;
285 	voff_t uma_uoffset;
286 
287 	uvm_flag_t uma_flags;
288 };
289 #endif /* _KERNEL */
290 
291 #ifdef _KERNEL
292 #define	vm_map_modflags(map, set, clear)				\
293 do {									\
294 	simple_lock(&(map)->flags_lock);				\
295 	(map)->flags = ((map)->flags | (set)) & ~(clear);		\
296 	simple_unlock(&(map)->flags_lock);				\
297 } while (/*CONSTCOND*/ 0)
298 #endif /* _KERNEL */
299 
300 /*
301  * globals:
302  */
303 
304 #ifdef _KERNEL
305 
306 #ifdef PMAP_GROWKERNEL
307 extern vaddr_t	uvm_maxkaddr;
308 #endif
309 
310 /*
311  * protos: the following prototypes define the interface to vm_map
312  */
313 
314 void		uvm_map_deallocate(struct vm_map *);
315 
316 int		uvm_map_clean(struct vm_map *, vaddr_t, vaddr_t, int);
317 void		uvm_map_clip_start(struct vm_map *, struct vm_map_entry *,
318 		    vaddr_t, struct uvm_mapent_reservation *);
319 void		uvm_map_clip_end(struct vm_map *, struct vm_map_entry *,
320 		    vaddr_t, struct uvm_mapent_reservation *);
321 struct vm_map	*uvm_map_create(pmap_t, vaddr_t, vaddr_t, int);
322 int		uvm_map_extract(struct vm_map *, vaddr_t, vsize_t,
323 		    struct vm_map *, vaddr_t *, int);
324 struct vm_map_entry *
325 		uvm_map_findspace(struct vm_map *, vaddr_t, vsize_t,
326 		    vaddr_t *, struct uvm_object *, voff_t, vsize_t, int);
327 int		uvm_map_inherit(struct vm_map *, vaddr_t, vaddr_t,
328 		    vm_inherit_t);
329 int		uvm_map_advice(struct vm_map *, vaddr_t, vaddr_t, int);
330 void		uvm_map_init(void);
331 boolean_t	uvm_map_lookup_entry(struct vm_map *, vaddr_t,
332 		    struct vm_map_entry **);
333 void		uvm_map_reference(struct vm_map *);
334 int		uvm_map_replace(struct vm_map *, vaddr_t, vaddr_t,
335 		    struct vm_map_entry *, int);
336 int		uvm_map_reserve(struct vm_map *, vsize_t, vaddr_t, vsize_t,
337 		    vaddr_t *, uvm_flag_t);
338 void		uvm_map_setup(struct vm_map *, vaddr_t, vaddr_t, int);
339 void		uvm_map_setup_kernel(struct vm_map_kernel *,
340 		    vaddr_t, vaddr_t, int);
341 struct vm_map_kernel *
342 		vm_map_to_kernel(struct vm_map *);
343 int		uvm_map_submap(struct vm_map *, vaddr_t, vaddr_t,
344 		    struct vm_map *);
345 void		uvm_unmap1(struct vm_map *, vaddr_t, vaddr_t, int);
346 #define	uvm_unmap(map, s, e)	uvm_unmap1((map), (s), (e), 0)
347 void		uvm_unmap_detach(struct vm_map_entry *,int);
348 void		uvm_unmap_remove(struct vm_map *, vaddr_t, vaddr_t,
349 		    struct vm_map_entry **, struct uvm_mapent_reservation *,
350 		    int);
351 
352 int		uvm_map_prepare(struct vm_map *, vaddr_t, vsize_t,
353 		    struct uvm_object *, voff_t, vsize_t, uvm_flag_t,
354 		    struct uvm_map_args *);
355 int		uvm_map_enter(struct vm_map *, const struct uvm_map_args *,
356 		    struct vm_map_entry *);
357 
358 int		uvm_mapent_reserve(struct vm_map *,
359 		    struct uvm_mapent_reservation *, int, int);
360 void		uvm_mapent_unreserve(struct vm_map *,
361 		    struct uvm_mapent_reservation *);
362 
363 vsize_t		uvm_mapent_overhead(vsize_t, int);
364 
365 int		uvm_mapent_trymerge(struct vm_map *,
366 		    struct vm_map_entry *, int);
367 #define	UVM_MERGE_COPYING	1
368 
369 #endif /* _KERNEL */
370 
371 /*
372  * VM map locking operations:
373  *
374  *	These operations perform locking on the data portion of the
375  *	map.
376  *
377  *	vm_map_lock_try: try to lock a map, failing if it is already locked.
378  *
379  *	vm_map_lock: acquire an exclusive (write) lock on a map.
380  *
381  *	vm_map_lock_read: acquire a shared (read) lock on a map.
382  *
383  *	vm_map_unlock: release an exclusive lock on a map.
384  *
385  *	vm_map_unlock_read: release a shared lock on a map.
386  *
387  *	vm_map_downgrade: downgrade an exclusive lock to a shared lock.
388  *
389  *	vm_map_upgrade: upgrade a shared lock to an exclusive lock.
390  *
391  *	vm_map_busy: mark a map as busy.
392  *
393  *	vm_map_unbusy: clear busy status on a map.
394  *
395  * Note that "intrsafe" maps use only exclusive, spin locks.  We simply
396  * use the sleep lock's interlock for this.
397  */
398 
399 #ifdef _KERNEL
400 /* XXX: clean up later */
401 #include <sys/time.h>
402 #include <sys/proc.h>	/* for tsleep(), wakeup() */
403 #include <sys/systm.h>	/* for panic() */
404 
405 static __inline boolean_t	vm_map_lock_try(struct vm_map *);
406 static __inline void		vm_map_lock(struct vm_map *);
407 extern const char vmmapbsy[];
408 
409 static __inline boolean_t
410 vm_map_lock_try(struct vm_map *map)
411 {
412 	boolean_t rv;
413 
414 	if (map->flags & VM_MAP_INTRSAFE)
415 		rv = simple_lock_try(&map->lock.lk_interlock);
416 	else {
417 		simple_lock(&map->flags_lock);
418 		if (map->flags & VM_MAP_BUSY) {
419 			simple_unlock(&map->flags_lock);
420 			return (FALSE);
421 		}
422 		rv = (lockmgr(&map->lock, LK_EXCLUSIVE|LK_NOWAIT|LK_INTERLOCK,
423 		    &map->flags_lock) == 0);
424 	}
425 
426 	if (rv)
427 		map->timestamp++;
428 
429 	return (rv);
430 }
431 
432 static __inline void
433 vm_map_lock(struct vm_map *map)
434 {
435 	int error;
436 
437 	if (map->flags & VM_MAP_INTRSAFE) {
438 		simple_lock(&map->lock.lk_interlock);
439 		return;
440 	}
441 
442  try_again:
443 	simple_lock(&map->flags_lock);
444 	while (map->flags & VM_MAP_BUSY) {
445 		map->flags |= VM_MAP_WANTLOCK;
446 		ltsleep(&map->flags, PVM, vmmapbsy, 0, &map->flags_lock);
447 	}
448 
449 	error = lockmgr(&map->lock, LK_EXCLUSIVE|LK_SLEEPFAIL|LK_INTERLOCK,
450 	    &map->flags_lock);
451 
452 	if (error) {
453 		KASSERT(error == ENOLCK);
454 		goto try_again;
455 	}
456 
457 	(map)->timestamp++;
458 }
459 
460 #ifdef DIAGNOSTIC
461 #define	vm_map_lock_read(map)						\
462 do {									\
463 	if ((map)->flags & VM_MAP_INTRSAFE)				\
464 		panic("vm_map_lock_read: intrsafe Map");		\
465 	(void) lockmgr(&(map)->lock, LK_SHARED, NULL);			\
466 } while (/*CONSTCOND*/ 0)
467 #else
468 #define	vm_map_lock_read(map)						\
469 	(void) lockmgr(&(map)->lock, LK_SHARED, NULL)
470 #endif
471 
472 #define	vm_map_unlock(map)						\
473 do {									\
474 	if ((map)->flags & VM_MAP_INTRSAFE)				\
475 		simple_unlock(&(map)->lock.lk_interlock);		\
476 	else								\
477 		(void) lockmgr(&(map)->lock, LK_RELEASE, NULL);		\
478 } while (/*CONSTCOND*/ 0)
479 
480 #define	vm_map_unlock_read(map)						\
481 	(void) lockmgr(&(map)->lock, LK_RELEASE, NULL)
482 
483 #define	vm_map_downgrade(map)						\
484 	(void) lockmgr(&(map)->lock, LK_DOWNGRADE, NULL)
485 
486 #ifdef DIAGNOSTIC
487 #define	vm_map_upgrade(map)						\
488 do {									\
489 	if (lockmgr(&(map)->lock, LK_UPGRADE, NULL) != 0)		\
490 		panic("vm_map_upgrade: failed to upgrade lock");	\
491 } while (/*CONSTCOND*/ 0)
492 #else
493 #define	vm_map_upgrade(map)						\
494 	(void) lockmgr(&(map)->lock, LK_UPGRADE, NULL)
495 #endif
496 
497 #define	vm_map_busy(map)						\
498 do {									\
499 	simple_lock(&(map)->flags_lock);				\
500 	(map)->flags |= VM_MAP_BUSY;					\
501 	simple_unlock(&(map)->flags_lock);				\
502 } while (/*CONSTCOND*/ 0)
503 
504 #define	vm_map_unbusy(map)						\
505 do {									\
506 	int oflags;							\
507 									\
508 	simple_lock(&(map)->flags_lock);				\
509 	oflags = (map)->flags;						\
510 	(map)->flags &= ~(VM_MAP_BUSY|VM_MAP_WANTLOCK);			\
511 	simple_unlock(&(map)->flags_lock);				\
512 	if (oflags & VM_MAP_WANTLOCK)					\
513 		wakeup(&(map)->flags);					\
514 } while (/*CONSTCOND*/ 0)
515 
516 boolean_t vm_map_starved_p(struct vm_map *);
517 
518 #endif /* _KERNEL */
519 
520 /*
521  *	Functions implemented as macros
522  */
523 #define		vm_map_min(map)		((map)->header.end)
524 #define		vm_map_max(map)		((map)->header.start)
525 #define		vm_map_setmin(map, v)	((map)->header.end = (v))
526 #define		vm_map_setmax(map, v)	((map)->header.start = (v))
527 
528 #define		vm_map_pmap(map)	((map)->pmap)
529 
530 #endif /* _UVM_UVM_MAP_H_ */
531