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