vm_map.h revision 103777
117683Spst/*
217683Spst * Copyright (c) 1991, 1993
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * This code is derived from software contributed to Berkeley by
617683Spst * The Mach Operating System project at Carnegie-Mellon University.
717683Spst *
817683Spst * Redistribution and use in source and binary forms, with or without
917683Spst * modification, are permitted provided that the following conditions
1017683Spst * are met:
1117683Spst * 1. Redistributions of source code must retain the above copyright
1217683Spst *    notice, this list of conditions and the following disclaimer.
1317683Spst * 2. Redistributions in binary form must reproduce the above copyright
1417683Spst *    notice, this list of conditions and the following disclaimer in the
1517683Spst *    documentation and/or other materials provided with the distribution.
1617683Spst * 3. All advertising materials mentioning features or use of this software
1717683Spst *    must display the following acknowledgement:
1817683Spst *	This product includes software developed by the University of
1917683Spst *	California, Berkeley and its contributors.
2017683Spst * 4. Neither the name of the University nor the names of its contributors
2156891Sfenner *    may be used to endorse or promote products derived from this software
2275110Sfenner *    without specific prior written permission.
2317683Spst *
2417683Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2517683Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2617683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2717683Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2817683Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2917683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3017683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3117683Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3256891Sfenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3317683Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3417683Spst * SUCH DAMAGE.
3517683Spst *
3617683Spst *	@(#)vm_map.h	8.9 (Berkeley) 5/17/95
3717683Spst *
3817683Spst *
3917683Spst * Copyright (c) 1987, 1990 Carnegie-Mellon University.
4017683Spst * All rights reserved.
4117683Spst *
4217683Spst * Authors: Avadis Tevanian, Jr., Michael Wayne Young
4317683Spst *
4417683Spst * Permission to use, copy, modify and distribute this software and
4517683Spst * its documentation is hereby granted, provided that both the copyright
4617683Spst * notice and this permission notice appear in all copies of the
4717683Spst * software, derivative works or modified versions, and any portions
4817683Spst * thereof, and that both notices appear in supporting documentation.
4917683Spst *
5017683Spst * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
5117683Spst * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
5217683Spst * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5317683Spst *
5475110Sfenner * Carnegie Mellon requests users of this software to return to
5556891Sfenner *
5656891Sfenner *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5756891Sfenner *  School of Computer Science
5856891Sfenner *  Carnegie Mellon University
5956891Sfenner *  Pittsburgh PA 15213-3890
6056891Sfenner *
6156891Sfenner * any improvements or extensions that they make and grant Carnegie the
6275110Sfenner * rights to redistribute these changes.
6356891Sfenner *
6475110Sfenner * $FreeBSD: head/sys/vm/vm_map.h 103777 2002-09-22 04:33:43Z alc $
6575110Sfenner */
6675110Sfenner
6775110Sfenner/*
6875110Sfenner *	Virtual memory map module definitions.
6917683Spst */
7017683Spst#ifndef	_VM_MAP_
7117683Spst#define	_VM_MAP_
7217683Spst
7317683Spst#include <sys/lock.h>
7417683Spst#include <sys/lockmgr.h>
7517683Spst
7617683Spst/*
7717683Spst *	Types defined:
7817683Spst *
7956891Sfenner *	vm_map_t		the high-level address map data structure.
8056891Sfenner *	vm_map_entry_t		an entry in an address map.
8117683Spst */
8217683Spst
8356891Sfennertypedef u_int vm_eflags_t;
8456891Sfenner
8517683Spst/*
8617683Spst *	Objects which live in maps may be either VM objects, or
8717683Spst *	another map (called a "sharing map") which denotes read-write
8817683Spst *	sharing with other maps.
8917683Spst */
9017683Spstunion vm_map_object {
9117683Spst	struct vm_object *vm_object;	/* object object */
9217683Spst	struct vm_map *sub_map;		/* belongs to another map */
9317683Spst};
9417683Spst
9517683Spst/*
9617683Spst *	Address map entries consist of start and end addresses,
9717683Spst *	a VM object (or sharing map) and offset into that object,
9817683Spst *	and user-exported inheritance and protection information.
9917683Spst *	Also included is control information for virtual copy operations.
10017683Spst */
10117683Spststruct vm_map_entry {
10217683Spst	struct vm_map_entry *prev;	/* previous entry */
10317683Spst	struct vm_map_entry *next;	/* next entry */
10417683Spst	struct vm_map_entry *left;	/* left child in binary search tree */
10517683Spst	struct vm_map_entry *right;	/* right child in binary search tree */
10617683Spst	vm_offset_t start;		/* start address */
10717683Spst	vm_offset_t end;		/* end address */
10817683Spst	vm_offset_t avail_ssize;	/* amt can grow if this is a stack */
10917683Spst	union vm_map_object object;	/* object I point to */
11017683Spst	vm_ooffset_t offset;		/* offset into object */
11117683Spst	vm_eflags_t eflags;		/* map entry flags */
11217683Spst	/* Only in task maps: */
11317683Spst	vm_prot_t protection;		/* protection code */
11417683Spst	vm_prot_t max_protection;	/* maximum protection */
11517683Spst	vm_inherit_t inheritance;	/* inheritance */
11617683Spst	int wired_count;		/* can be paged if = 0 */
11717683Spst	vm_pindex_t lastr;		/* last read */
11817683Spst};
11917683Spst
12017683Spst#define MAP_ENTRY_NOSYNC		0x0001
12117683Spst#define MAP_ENTRY_IS_SUB_MAP		0x0002
12217683Spst#define MAP_ENTRY_COW			0x0004
12317683Spst#define MAP_ENTRY_NEEDS_COPY		0x0008
12417683Spst#define MAP_ENTRY_NOFAULT		0x0010
12517683Spst#define MAP_ENTRY_USER_WIRED		0x0020
12617683Spst
12717683Spst#define MAP_ENTRY_BEHAV_NORMAL		0x0000	/* default behavior */
12817683Spst#define MAP_ENTRY_BEHAV_SEQUENTIAL	0x0040	/* expect sequential access */
12917683Spst#define MAP_ENTRY_BEHAV_RANDOM		0x0080	/* expect random access */
13017683Spst#define MAP_ENTRY_BEHAV_RESERVED	0x00C0	/* future use */
13117683Spst
13217683Spst#define MAP_ENTRY_BEHAV_MASK		0x00C0
13317683Spst
13417683Spst#define MAP_ENTRY_IN_TRANSITION		0x0100	/* entry being changed */
13517683Spst#define MAP_ENTRY_NEEDS_WAKEUP		0x0200	/* waiters in transition */
13617683Spst#define MAP_ENTRY_NOCOREDUMP		0x0400	/* don't include in a core */
13717683Spst
13817683Spst#ifdef	_KERNEL
13917683Spststatic __inline u_char
14017683Spstvm_map_entry_behavior(vm_map_entry_t entry)
14117683Spst{
14217683Spst	return (entry->eflags & MAP_ENTRY_BEHAV_MASK);
14317683Spst}
14417683Spst#endif	/* _KERNEL */
14517683Spst
14617683Spst/*
14717683Spst *	A map is a set of map entries.  These map entries are
14817683Spst *	organized both as a binary search tree and as a doubly-linked
14917683Spst *	list.  Both structures are ordered based upon the start and
15017683Spst *	end addresses contained within each map entry.  Sleator and
15117683Spst *	Tarjan's top-down splay algorithm is employed to control
15217683Spst *	height imbalance in the binary search tree.
15317683Spst *
15417683Spst *	Note: the lock structure cannot be the first element of vm_map
15517683Spst *	because this can result in a running lockup between two or more
15617683Spst *	system processes trying to kmem_alloc_wait() due to kmem_alloc_wait()
15717683Spst *	and free tsleep/waking up 'map' and the underlying lockmgr also
15817683Spst *	sleeping and waking up on 'map'.  The lockup occurs when the map fills
15917683Spst *	up.  The 'exec' map, for example.
16017683Spst *
16117683Spst * List of locks
16217683Spst *	(c)	const until freed
16317683Spst */
16417683Spststruct vm_map {
16517683Spst	struct vm_map_entry header;	/* List of entries */
16617683Spst	struct lock lock;		/* Lock for map data */
16717683Spst	int nentries;			/* Number of entries */
16817683Spst	vm_size_t size;			/* virtual size */
16917683Spst	u_char needs_wakeup;
17017683Spst	u_char system_map;		/* Am I a system map? */
17156891Sfenner	u_char infork;			/* Am I in fork processing? */
17256891Sfenner	vm_map_entry_t root;		/* Root of a binary search tree */
17356891Sfenner	unsigned int timestamp;		/* Version number */
17417683Spst	vm_map_entry_t first_free;	/* First free space hint */
17517683Spst	pmap_t pmap;			/* (c) Physical map */
17617683Spst#define	min_offset	header.start	/* (c) */
17717683Spst#define	max_offset	header.end	/* (c) */
17817683Spst};
17917683Spst
18017683Spst#ifdef	_KERNEL
18117683Spststatic __inline vm_offset_t
18217683Spstvm_map_max(vm_map_t map)
18317683Spst{
18475110Sfenner	return (map->max_offset);
18575110Sfenner}
18617683Spst
18775110Sfennerstatic __inline vm_offset_t
18875110Sfennervm_map_min(vm_map_t map)
18975110Sfenner{
19017683Spst	return (map->min_offset);
19175110Sfenner}
19217683Spst
19317683Spststatic __inline pmap_t
19417683Spstvm_map_pmap(vm_map_t map)
19517683Spst{
19617683Spst	return (map->pmap);
19717683Spst}
19817683Spst#endif	/* _KERNEL */
19975110Sfenner
20017683Spst/*
20117683Spst * Shareable process virtual address space.
20217683Spst *
20317683Spst * List of locks
20417683Spst *	(c)	const until freed
20556891Sfenner */
20656891Sfennerstruct vmspace {
207	struct vm_map vm_map;	/* VM address map */
208	struct pmap vm_pmap;	/* private physical map */
209	int vm_refcnt;		/* number of references */
210	struct shmmap_state *vm_shm;	/* SYS5 shared memory private data XXX */
211/* we copy from vm_startcopy to the end of the structure on fork */
212#define vm_startcopy vm_rssize
213	segsz_t vm_rssize;	/* current resident set size in pages */
214	segsz_t vm_swrss;	/* resident set size before last swap */
215	segsz_t vm_tsize;	/* text size (pages) XXX */
216	segsz_t vm_dsize;	/* data size (pages) XXX */
217	segsz_t vm_ssize;	/* stack size (pages) */
218	caddr_t vm_taddr;	/* (c) user virtual address of text */
219	caddr_t vm_daddr;	/* (c) user virtual address of data */
220	caddr_t vm_maxsaddr;	/* user VA at max stack growth */
221#define	vm_endcopy vm_freer
222	struct proc *vm_freer;	/* vm freed on whose behalf */
223};
224
225#ifdef	_KERNEL
226static __inline pmap_t
227vmspace_pmap(struct vmspace *vmspace)
228{
229	return &vmspace->vm_pmap;
230}
231#endif	/* _KERNEL */
232
233#ifdef	_KERNEL
234/*
235 *	Macros:		vm_map_lock, etc.
236 *	Function:
237 *		Perform locking on the data portion of a map.  Note that
238 *		these macros mimic procedure calls returning void.  The
239 *		semicolon is supplied by the user of these macros, not
240 *		by the macros themselves.  The macros can safely be used
241 *		as unbraced elements in a higher level statement.
242 */
243
244void _vm_map_lock(vm_map_t map, const char *file, int line);
245void _vm_map_unlock(vm_map_t map, const char *file, int line);
246void _vm_map_lock_read(vm_map_t map, const char *file, int line);
247void _vm_map_unlock_read(vm_map_t map, const char *file, int line);
248int _vm_map_trylock(vm_map_t map, const char *file, int line);
249int _vm_map_lock_upgrade(vm_map_t map, const char *file, int line);
250void _vm_map_lock_downgrade(vm_map_t map, const char *file, int line);
251int vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait);
252void vm_map_wakeup(vm_map_t map);
253
254#define	vm_map_lock(map)	_vm_map_lock(map, LOCK_FILE, LOCK_LINE)
255#define	vm_map_unlock(map)	_vm_map_unlock(map, LOCK_FILE, LOCK_LINE)
256#define	vm_map_lock_read(map)	_vm_map_lock_read(map, LOCK_FILE, LOCK_LINE)
257#define	vm_map_unlock_read(map)	_vm_map_unlock_read(map, LOCK_FILE, LOCK_LINE)
258#define	vm_map_trylock(map)	_vm_map_trylock(map, LOCK_FILE, LOCK_LINE)
259#define	vm_map_lock_upgrade(map)	\
260			_vm_map_lock_upgrade(map, LOCK_FILE, LOCK_LINE)
261#define	vm_map_lock_downgrade(map)	\
262			_vm_map_lock_downgrade(map, LOCK_FILE, LOCK_LINE)
263
264long vmspace_resident_count(struct vmspace *vmspace);
265#endif	/* _KERNEL */
266
267
268/* XXX: number of kernel maps and entries to statically allocate */
269#define MAX_KMAP	10
270#define	MAX_KMAPENT	128
271#define	MAX_MAPENT	128
272
273/*
274 * Copy-on-write flags for vm_map operations
275 */
276#define MAP_UNUSED_01		0x0001
277#define MAP_COPY_ON_WRITE	0x0002
278#define MAP_NOFAULT		0x0004
279#define MAP_PREFAULT		0x0008
280#define MAP_PREFAULT_PARTIAL	0x0010
281#define MAP_DISABLE_SYNCER	0x0020
282#define MAP_DISABLE_COREDUMP	0x0100
283#define MAP_PREFAULT_MADVISE	0x0200	/* from (user) madvise request */
284
285/*
286 * vm_fault option flags
287 */
288#define VM_FAULT_NORMAL 0		/* Nothing special */
289#define VM_FAULT_CHANGE_WIRING 1	/* Change the wiring as appropriate */
290#define VM_FAULT_USER_WIRE 2		/* Likewise, but for user purposes */
291#define VM_FAULT_WIRE_MASK (VM_FAULT_CHANGE_WIRING|VM_FAULT_USER_WIRE)
292#define VM_FAULT_DIRTY 8		/* Dirty the page */
293
294#ifdef _KERNEL
295boolean_t vm_map_check_protection (vm_map_t, vm_offset_t, vm_offset_t, vm_prot_t);
296struct pmap;
297vm_map_t vm_map_create (struct pmap *, vm_offset_t, vm_offset_t);
298int vm_map_delete (vm_map_t, vm_offset_t, vm_offset_t);
299int vm_map_find (vm_map_t, vm_object_t, vm_ooffset_t, vm_offset_t *, vm_size_t, boolean_t, vm_prot_t, vm_prot_t, int);
300int vm_map_findspace (vm_map_t, vm_offset_t, vm_size_t, vm_offset_t *);
301int vm_map_inherit (vm_map_t, vm_offset_t, vm_offset_t, vm_inherit_t);
302void vm_map_init (struct vm_map *, vm_offset_t, vm_offset_t);
303int vm_map_insert (vm_map_t, vm_object_t, vm_ooffset_t, vm_offset_t, vm_offset_t, vm_prot_t, vm_prot_t, int);
304int vm_map_lookup (vm_map_t *, vm_offset_t, vm_prot_t, vm_map_entry_t *, vm_object_t *,
305    vm_pindex_t *, vm_prot_t *, boolean_t *);
306void vm_map_lookup_done (vm_map_t, vm_map_entry_t);
307boolean_t vm_map_lookup_entry (vm_map_t, vm_offset_t, vm_map_entry_t *);
308int vm_map_clean (vm_map_t, vm_offset_t, vm_offset_t, boolean_t, boolean_t);
309int vm_map_protect (vm_map_t, vm_offset_t, vm_offset_t, vm_prot_t, boolean_t);
310int vm_map_remove (vm_map_t, vm_offset_t, vm_offset_t);
311void vm_map_startup (void);
312int vm_map_submap (vm_map_t, vm_offset_t, vm_offset_t, vm_map_t);
313int vm_map_madvise (vm_map_t, vm_offset_t, vm_offset_t, int);
314void vm_map_simplify_entry (vm_map_t, vm_map_entry_t);
315void vm_init2 (void);
316int vm_map_stack (vm_map_t, vm_offset_t, vm_size_t, vm_prot_t, vm_prot_t, int);
317int vm_map_growstack (struct proc *p, vm_offset_t addr);
318int vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
319    boolean_t user_unwire);
320int vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
321    boolean_t user_wire);
322int vmspace_swap_count (struct vmspace *vmspace);
323int vm_uiomove(vm_map_t, vm_object_t, off_t, int, vm_offset_t, int *);
324#endif				/* _KERNEL */
325#endif				/* _VM_MAP_ */
326