Home | History | Annotate | Line # | Download | only in uvm
uvm_map.h revision 1.18
      1 /*	$NetBSD: uvm_map.h,v 1.18 2000/06/26 15:32:28 mrg 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 (at) 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) { \
     90 	if ((VA) > (ENTRY)->start) uvm_map_clip_start(MAP,ENTRY,VA); }
     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) { \
    100 	if ((VA) < (ENTRY)->end) uvm_map_clip_end(MAP,ENTRY,VA); }
    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 #include <uvm/uvm_anon.h>
    111 
    112 /*
    113  * types defined:
    114  *
    115  *	vm_map_t		the high-level address map data structure.
    116  *	vm_map_entry_t		an entry in an address map.
    117  *	vm_map_version_t	a timestamp of a map, for use with vm_map_lookup
    118  */
    119 
    120 /*
    121  * Objects which live in maps may be either VM objects, or another map
    122  * (called a "sharing map") which denotes read-write sharing with other maps.
    123  *
    124  * XXXCDC: private pager data goes here now
    125  */
    126 
    127 union vm_map_object {
    128 	struct uvm_object	*uvm_obj;	/* UVM OBJECT */
    129 	struct vm_map		*sub_map;	/* belongs to another map */
    130 };
    131 
    132 /*
    133  * Address map entries consist of start and end addresses,
    134  * a VM object (or sharing map) and offset into that object,
    135  * and user-exported inheritance and protection information.
    136  * Also included is control information for virtual copy operations.
    137  */
    138 struct vm_map_entry {
    139 	struct vm_map_entry	*prev;		/* previous entry */
    140 	struct vm_map_entry	*next;		/* next entry */
    141 	vaddr_t			start;		/* start address */
    142 	vaddr_t			end;		/* end address */
    143 	union vm_map_object	object;		/* object I point to */
    144 	voff_t			offset;		/* offset into object */
    145 	int			etype;		/* entry type */
    146 	vm_prot_t		protection;	/* protection code */
    147 	vm_prot_t		max_protection;	/* maximum protection */
    148 	vm_inherit_t		inheritance;	/* inheritance */
    149 	int			wired_count;	/* can be paged if == 0 */
    150 	struct vm_aref		aref;		/* anonymous overlay */
    151 	int			advice;		/* madvise advice */
    152 #define uvm_map_entry_stop_copy flags
    153 	u_int8_t		flags;		/* flags */
    154 
    155 #define UVM_MAP_STATIC		0x01		/* static map entry */
    156 
    157 };
    158 
    159 #define	VM_MAPENT_ISWIRED(entry)	((entry)->wired_count != 0)
    160 
    161 /*
    162  *	Maps are doubly-linked lists of map entries, kept sorted
    163  *	by address.  A single hint is provided to start
    164  *	searches again from the last successful search,
    165  *	insertion, or removal.
    166  *
    167  *	LOCKING PROTOCOL NOTES:
    168  *	-----------------------
    169  *
    170  *	VM map locking is a little complicated.  There are both shared
    171  *	and exclusive locks on maps.  However, it is sometimes required
    172  *	to downgrade an exclusive lock to a shared lock, and upgrade to
    173  *	an exclusive lock again (to perform error recovery).  However,
    174  *	another thread *must not* queue itself to receive an exclusive
    175  *	lock while before we upgrade back to exclusive, otherwise the
    176  *	error recovery becomes extremely difficult, if not impossible.
    177  *
    178  *	In order to prevent this scenario, we introduce the notion of
    179  *	a `busy' map.  A `busy' map is read-locked, but other threads
    180  *	attempting to write-lock wait for this flag to clear before
    181  *	entering the lock manager.  A map may only be marked busy
    182  *	when the map is write-locked (and then the map must be downgraded
    183  *	to read-locked), and may only be marked unbusy by the thread
    184  *	which marked it busy (holding *either* a read-lock or a
    185  *	write-lock, the latter being gained by an upgrade).
    186  *
    187  *	Access to the map `flags' member is controlled by the `flags_lock'
    188  *	simple lock.  Note that some flags are static (set once at map
    189  *	creation time, and never changed), and thus require no locking
    190  *	to check those flags.  All flags which are r/w must be set or
    191  *	cleared while the `flags_lock' is asserted.  Additional locking
    192  *	requirements are:
    193  *
    194  *		VM_MAP_PAGEABLE		r/o static flag; no locking required
    195  *
    196  *		VM_MAP_INTRSAFE		r/o static flag; no locking required
    197  *
    198  *		VM_MAP_WIREFUTURE	r/w; may only be set or cleared when
    199  *					map is write-locked.  may be tested
    200  *					without asserting `flags_lock'.
    201  *
    202  *		VM_MAP_BUSY		r/w; may only be set when map is
    203  *					write-locked, may only be cleared by
    204  *					thread which set it, map read-locked
    205  *					or write-locked.  must be tested
    206  *					while `flags_lock' is asserted.
    207  *
    208  *		VM_MAP_WANTLOCK		r/w; may only be set when the map
    209  *					is busy, and thread is attempting
    210  *					to write-lock.  must be tested
    211  *					while `flags_lock' is asserted.
    212  */
    213 struct vm_map {
    214 	struct pmap *		pmap;		/* Physical map */
    215 	lock_data_t		lock;		/* Lock for map data */
    216 	struct vm_map_entry	header;		/* List of entries */
    217 	int			nentries;	/* Number of entries */
    218 	vsize_t			size;		/* virtual size */
    219 	int			ref_count;	/* Reference count */
    220 	simple_lock_data_t	ref_lock;	/* Lock for ref_count field */
    221 	vm_map_entry_t		hint;		/* hint for quick lookups */
    222 	simple_lock_data_t	hint_lock;	/* lock for hint storage */
    223 	vm_map_entry_t		first_free;	/* First free space hint */
    224 	int			flags;		/* flags */
    225 	simple_lock_data_t	flags_lock;	/* Lock for flags field */
    226 	unsigned int		timestamp;	/* Version number */
    227 #define	min_offset		header.start
    228 #define max_offset		header.end
    229 };
    230 
    231 /* vm_map flags */
    232 #define	VM_MAP_PAGEABLE		0x01		/* ro: entries are pageable */
    233 #define	VM_MAP_INTRSAFE		0x02		/* ro: interrupt safe map */
    234 #define	VM_MAP_WIREFUTURE	0x04		/* rw: wire future mappings */
    235 #define	VM_MAP_BUSY		0x08		/* rw: map is busy */
    236 #define	VM_MAP_WANTLOCK		0x10		/* rw: want to write-lock */
    237 
    238 /* XXX: number of kernel maps and entries to statically allocate */
    239 #define MAX_KMAP	10
    240 
    241 #if !defined(MAX_KMAPENT)
    242 #if (50 + (2 * NPROC) > 1000)
    243 #define MAX_KMAPENT (50 + (2 * NPROC))
    244 #else
    245 #define	MAX_KMAPENT	1000  /* XXXCDC: no crash */
    246 #endif
    247 #endif	/* !defined MAX_KMAPENT */
    248 
    249 #ifdef _KERNEL
    250 #define	vm_map_modflags(map, set, clear)				\
    251 do {									\
    252 	simple_lock(&(map)->flags_lock);				\
    253 	(map)->flags = ((map)->flags | (set)) & ~(clear);		\
    254 	simple_unlock(&(map)->flags_lock);				\
    255 } while (0)
    256 #endif /* _KERNEL */
    257 
    258 /*
    259  *	Interrupt-safe maps must also be kept on a special list,
    260  *	to assist uvm_fault() in avoiding locking problems.
    261  */
    262 struct vm_map_intrsafe {
    263 	struct vm_map	vmi_map;
    264 	LIST_ENTRY(vm_map_intrsafe) vmi_list;
    265 };
    266 
    267 LIST_HEAD(vmi_list, vm_map_intrsafe);
    268 #ifdef _KERNEL
    269 extern simple_lock_data_t vmi_list_slock;
    270 extern struct vmi_list vmi_list;
    271 
    272 static __inline int vmi_list_lock __P((void));
    273 static __inline void vmi_list_unlock __P((int));
    274 
    275 static __inline int
    276 vmi_list_lock()
    277 {
    278 	int s;
    279 
    280 	s = splhigh();
    281 	simple_lock(&vmi_list_slock);
    282 	return (s);
    283 }
    284 
    285 static __inline void
    286 vmi_list_unlock(s)
    287 	int s;
    288 {
    289 
    290 	simple_unlock(&vmi_list_slock);
    291 	splx(s);
    292 }
    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 PMAP_GROWKERNEL
    310 extern vaddr_t	uvm_maxkaddr;
    311 #endif
    312 
    313 /*
    314  * protos: the following prototypes define the interface to vm_map
    315  */
    316 
    317 MAP_INLINE
    318 void		uvm_map_deallocate __P((vm_map_t));
    319 
    320 int		uvm_map_clean __P((vm_map_t, vaddr_t, vaddr_t, int));
    321 void		uvm_map_clip_start __P((vm_map_t, vm_map_entry_t, vaddr_t));
    322 void		uvm_map_clip_end __P((vm_map_t, vm_map_entry_t, vaddr_t));
    323 MAP_INLINE
    324 vm_map_t	uvm_map_create __P((pmap_t, vaddr_t, vaddr_t, int));
    325 int		uvm_map_extract __P((vm_map_t, vaddr_t, vsize_t,
    326 			vm_map_t, vaddr_t *, int));
    327 vm_map_entry_t	uvm_map_findspace __P((vm_map_t, vaddr_t, vsize_t, vaddr_t *,
    328 			struct uvm_object *, voff_t, boolean_t));
    329 int		uvm_map_inherit __P((vm_map_t, vaddr_t, vaddr_t, vm_inherit_t));
    330 int		uvm_map_advice __P((vm_map_t, vaddr_t, vaddr_t, int));
    331 void		uvm_map_init __P((void));
    332 boolean_t	uvm_map_lookup_entry __P((vm_map_t, vaddr_t, vm_map_entry_t *));
    333 MAP_INLINE
    334 void		uvm_map_reference __P((vm_map_t));
    335 int		uvm_map_replace __P((vm_map_t, vaddr_t, vaddr_t,
    336 			vm_map_entry_t, int));
    337 int		uvm_map_reserve __P((vm_map_t, vsize_t, vaddr_t, vaddr_t *));
    338 void		uvm_map_setup __P((vm_map_t, vaddr_t, vaddr_t, int));
    339 int		uvm_map_submap __P((vm_map_t, vaddr_t, vaddr_t, vm_map_t));
    340 MAP_INLINE
    341 int		uvm_unmap __P((vm_map_t, vaddr_t, vaddr_t));
    342 void		uvm_unmap_detach __P((vm_map_entry_t,int));
    343 int		uvm_unmap_remove __P((vm_map_t, vaddr_t, vaddr_t,
    344 				      vm_map_entry_t *));
    345 
    346 #endif /* _KERNEL */
    347 
    348 /*
    349  * VM map locking operations:
    350  *
    351  *	These operations perform locking on the data portion of the
    352  *	map.
    353  *
    354  *	vm_map_lock_try: try to lock a map, failing if it is already locked.
    355  *
    356  *	vm_map_lock: acquire an exclusive (write) lock on a map.
    357  *
    358  *	vm_map_lock_read: acquire a shared (read) lock on a map.
    359  *
    360  *	vm_map_unlock: release an exclusive lock on a map.
    361  *
    362  *	vm_map_unlock_read: release a shared lock on a map.
    363  *
    364  *	vm_map_downgrade: downgrade an exclusive lock to a shared lock.
    365  *
    366  *	vm_map_upgrade: upgrade a shared lock to an exclusive lock.
    367  *
    368  *	vm_map_busy: mark a map as busy.
    369  *
    370  *	vm_map_unbusy: clear busy status on a map.
    371  *
    372  * Note that "intrsafe" maps use only exclusive, spin locks.  We simply
    373  * use the sleep lock's interlock for this.
    374  */
    375 
    376 #ifdef _KERNEL
    377 /* XXX: clean up later */
    378 #include <sys/time.h>
    379 #include <sys/proc.h>	/* for tsleep(), wakeup() */
    380 #include <sys/systm.h>	/* for panic() */
    381 
    382 static __inline boolean_t vm_map_lock_try __P((vm_map_t));
    383 static __inline void vm_map_lock __P((vm_map_t));
    384 
    385 static __inline boolean_t
    386 vm_map_lock_try(map)
    387 	vm_map_t map;
    388 {
    389 	boolean_t rv;
    390 
    391 	if (map->flags & VM_MAP_INTRSAFE)
    392 		rv = simple_lock_try(&map->lock.lk_interlock);
    393 	else {
    394 		simple_lock(&map->flags_lock);
    395 		if (map->flags & VM_MAP_BUSY) {
    396 			simple_unlock(&map->flags_lock);
    397 			return (FALSE);
    398 		}
    399 		rv = (lockmgr(&map->lock, LK_EXCLUSIVE|LK_NOWAIT|LK_INTERLOCK,
    400 		    &map->flags_lock) == 0);
    401 	}
    402 
    403 	if (rv)
    404 		map->timestamp++;
    405 
    406 	return (rv);
    407 }
    408 
    409 static __inline void
    410 vm_map_lock(map)
    411 	vm_map_t map;
    412 {
    413 	int error;
    414 
    415 	if (map->flags & VM_MAP_INTRSAFE) {
    416 		simple_lock(&map->lock.lk_interlock);
    417 		return;
    418 	}
    419 
    420  try_again:
    421 	simple_lock(&map->flags_lock);
    422 	if (map->flags & VM_MAP_BUSY) {
    423 		map->flags |= VM_MAP_WANTLOCK;
    424 		simple_unlock(&map->flags_lock);
    425 		(void) tsleep(&map->flags, PVM, "vmmapbsy", 0);
    426 		goto try_again;
    427 	}
    428 
    429 	error = lockmgr(&map->lock, LK_EXCLUSIVE|LK_SLEEPFAIL|LK_INTERLOCK,
    430 	    &map->flags_lock);
    431 
    432 	if (error) {
    433 #ifdef DIAGNOSTIC
    434 		if (error != ENOLCK)
    435 			panic("vm_map_lock: failed to get lock");
    436 #endif
    437 		goto try_again;
    438 	}
    439 
    440 	(map)->timestamp++;
    441 }
    442 
    443 #ifdef DIAGNOSTIC
    444 #define	vm_map_lock_read(map)						\
    445 do {									\
    446 	if (map->flags & VM_MAP_INTRSAFE)				\
    447 		panic("vm_map_lock_read: intrsafe map");		\
    448 	(void) lockmgr(&(map)->lock, LK_SHARED, NULL);			\
    449 } while (0)
    450 #else
    451 #define	vm_map_lock_read(map)						\
    452 	(void) lockmgr(&(map)->lock, LK_SHARED, NULL)
    453 #endif
    454 
    455 #define	vm_map_unlock(map)						\
    456 do {									\
    457 	if ((map)->flags & VM_MAP_INTRSAFE)				\
    458 		simple_unlock(&(map)->lock.lk_interlock);		\
    459 	else								\
    460 		(void) lockmgr(&(map)->lock, LK_RELEASE, NULL);		\
    461 } while (0)
    462 
    463 #define	vm_map_unlock_read(map)						\
    464 	(void) lockmgr(&(map)->lock, LK_RELEASE, NULL)
    465 
    466 #define	vm_map_downgrade(map)						\
    467 	(void) lockmgr(&(map)->lock, LK_DOWNGRADE, NULL)
    468 
    469 #ifdef DIAGNOSTIC
    470 #define	vm_map_upgrade(map)						\
    471 do {									\
    472 	if (lockmgr(&(map)->lock, LK_UPGRADE, NULL) != 0)		\
    473 		panic("vm_map_upgrade: failed to upgrade lock");	\
    474 } while (0)
    475 #else
    476 #define	vm_map_upgrade(map)						\
    477 	(void) lockmgr(&(map)->lock, LK_UPGRADE, NULL)
    478 #endif
    479 
    480 #define	vm_map_busy(map)						\
    481 do {									\
    482 	simple_lock(&(map)->flags_lock);				\
    483 	(map)->flags |= VM_MAP_BUSY;					\
    484 	simple_unlock(&(map)->flags_lock);				\
    485 } while (0)
    486 
    487 #define	vm_map_unbusy(map)						\
    488 do {									\
    489 	int oflags;							\
    490 									\
    491 	simple_lock(&(map)->flags_lock);				\
    492 	oflags = (map)->flags;						\
    493 	(map)->flags &= ~(VM_MAP_BUSY|VM_MAP_WANTLOCK);			\
    494 	simple_unlock(&(map)->flags_lock);				\
    495 	if (oflags & VM_MAP_WANTLOCK)					\
    496 		wakeup(&(map)->flags);					\
    497 } while (0)
    498 #endif /* _KERNEL */
    499 
    500 /*
    501  *	Functions implemented as macros
    502  */
    503 #define		vm_map_min(map)		((map)->min_offset)
    504 #define		vm_map_max(map)		((map)->max_offset)
    505 #define		vm_map_pmap(map)	((map)->pmap)
    506 
    507 #endif /* _UVM_UVM_MAP_H_ */
    508