xref: /onnv-gate/usr/src/uts/common/vm/as.h (revision 2768)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2768Ssl108498  * Common Development and Distribution License (the "License").
6*2768Ssl108498  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2768Ssl108498  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	 All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #ifndef	_VM_AS_H
400Sstevel@tonic-gate #define	_VM_AS_H
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/watchpoint.h>
450Sstevel@tonic-gate #include <vm/seg.h>
460Sstevel@tonic-gate #include <vm/faultcode.h>
470Sstevel@tonic-gate #include <vm/hat.h>
480Sstevel@tonic-gate #include <sys/avl.h>
49*2768Ssl108498 #include <sys/proc.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #ifdef	__cplusplus
520Sstevel@tonic-gate extern "C" {
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * VM - Address spaces.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Each address space consists of a sorted list of segments
610Sstevel@tonic-gate  * and machine dependent address translation information.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * All the hard work is in the segment drivers and the
640Sstevel@tonic-gate  * hardware address translation code.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * The segment list is represented as an AVL tree.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * The address space lock (a_lock) is a long term lock which serializes
690Sstevel@tonic-gate  * access to certain operations (as_map, as_unmap) and protects the
700Sstevel@tonic-gate  * underlying generic segment data (seg.h) along with some fields in the
710Sstevel@tonic-gate  * address space structure as shown below:
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  *	address space structure 	segment structure
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *	a_segtree			s_base
760Sstevel@tonic-gate  *	a_size				s_size
770Sstevel@tonic-gate  *	a_lastgap			s_link
780Sstevel@tonic-gate  *	a_seglast			s_ops
790Sstevel@tonic-gate  *					s_as
800Sstevel@tonic-gate  *					s_data
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * The address space contents lock (a_contents) is a short term
830Sstevel@tonic-gate  * lock that protects most of the data in the address space structure.
840Sstevel@tonic-gate  * This lock is always acquired after the "a_lock" in all situations
850Sstevel@tonic-gate  * except while dealing with AS_CLAIMGAP to avoid deadlocks.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * The following fields are protected by this lock:
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  *	a_flags (AS_PAGLCK, AS_CLAIMGAP, etc.)
900Sstevel@tonic-gate  *	a_unmapwait
910Sstevel@tonic-gate  *	a_seglast
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * The address space lock (a_lock) is always held prior to any segment
940Sstevel@tonic-gate  * operation.  Some segment drivers use the address space lock to protect
950Sstevel@tonic-gate  * some or all of their segment private data, provided the version of
960Sstevel@tonic-gate  * "a_lock" (read vs. write) is consistent with the use of the data.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * The following fields are protected by the hat layer lock:
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *	a_vbits
1010Sstevel@tonic-gate  *	a_hat
1020Sstevel@tonic-gate  *	a_hrm
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate struct as {
1060Sstevel@tonic-gate 	kmutex_t a_contents;	/* protect certain fields in the structure */
1070Sstevel@tonic-gate 	uchar_t  a_flags;	/* as attributes */
1080Sstevel@tonic-gate 	uchar_t	a_vbits;	/* used for collecting statistics */
1090Sstevel@tonic-gate 	kcondvar_t a_cv;	/* used by as_rangelock */
1100Sstevel@tonic-gate 	struct	hat *a_hat;	/* hat structure */
1110Sstevel@tonic-gate 	struct	hrmstat *a_hrm; /* ref and mod bits */
1120Sstevel@tonic-gate 	caddr_t	a_userlimit;	/* highest allowable address in this as */
1130Sstevel@tonic-gate 	struct seg *a_seglast;	/* last segment hit on the addr space */
1140Sstevel@tonic-gate 	krwlock_t a_lock;	/* protects segment related fields */
1150Sstevel@tonic-gate 	size_t	a_size;		/* size of address space */
1160Sstevel@tonic-gate 	struct seg *a_lastgap;	/* last seg found by as_gap() w/ AS_HI (mmap) */
1170Sstevel@tonic-gate 	struct seg *a_lastgaphl; /* last seg saved in as_gap() either for */
1180Sstevel@tonic-gate 				/* AS_HI or AS_LO used in as_addseg() */
1190Sstevel@tonic-gate 	avl_tree_t a_segtree;	/* segments in this address space. (AVL tree) */
1200Sstevel@tonic-gate 	avl_tree_t a_wpage;	/* watched pages (procfs) */
1210Sstevel@tonic-gate 	uchar_t	a_updatedir;	/* mappings changed, rebuild a_objectdir */
1220Sstevel@tonic-gate 	timespec_t a_updatetime;	/* time when mappings last changed */
1230Sstevel@tonic-gate 	vnode_t	**a_objectdir;	/* object directory (procfs) */
1240Sstevel@tonic-gate 	size_t	a_sizedir;	/* size of object directory */
1250Sstevel@tonic-gate 	struct as_callback *a_callbacks; /* callback list */
1260Sstevel@tonic-gate 	void *a_xhat;		/* list of xhat providers */
127*2768Ssl108498 	proc_t	*a_proc;	/* back pointer to proc */
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	AS_PAGLCK		0x80
1310Sstevel@tonic-gate #define	AS_CLAIMGAP		0x40
1320Sstevel@tonic-gate #define	AS_UNMAPWAIT		0x20
1330Sstevel@tonic-gate #define	AS_NEEDSPURGE		0x10	/* mostly for seg_nf, see as_purge() */
1340Sstevel@tonic-gate #define	AS_BUSY			0x01	/* needed by XHAT framework */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define	AS_ISPGLCK(as)		((as)->a_flags & AS_PAGLCK)
1370Sstevel@tonic-gate #define	AS_ISCLAIMGAP(as)	((as)->a_flags & AS_CLAIMGAP)
1380Sstevel@tonic-gate #define	AS_ISUNMAPWAIT(as)	((as)->a_flags & AS_UNMAPWAIT)
1390Sstevel@tonic-gate #define	AS_ISBUSY(as)		((as)->a_flags & AS_BUSY)
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #define	AS_SETPGLCK(as)		((as)->a_flags |= AS_PAGLCK)
1430Sstevel@tonic-gate #define	AS_SETCLAIMGAP(as)	((as)->a_flags |= AS_CLAIMGAP)
1440Sstevel@tonic-gate #define	AS_SETUNMAPWAIT(as)	((as)->a_flags |= AS_UNMAPWAIT)
1450Sstevel@tonic-gate #define	AS_SETBUSY(as)		((as)->a_flags |= AS_BUSY)
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #define	AS_CLRPGLCK(as)		((as)->a_flags &= ~AS_PAGLCK)
1480Sstevel@tonic-gate #define	AS_CLRCLAIMGAP(as)	((as)->a_flags &= ~AS_CLAIMGAP)
1490Sstevel@tonic-gate #define	AS_CLRUNMAPWAIT(as)	((as)->a_flags &= ~AS_UNMAPWAIT)
1500Sstevel@tonic-gate #define	AS_CLRBUSY(as)		((as)->a_flags &= ~AS_BUSY)
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate #define	AS_TYPE_64BIT(as)	\
1530Sstevel@tonic-gate 	    (((as)->a_userlimit > (caddr_t)UINT32_MAX) ? 1 : 0)
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * The as_callback is the basic structure which supports the ability to
1570Sstevel@tonic-gate  * inform clients of specific events pertaining to address space management.
1580Sstevel@tonic-gate  * A user calls as_add_callback to register an address space callback
1590Sstevel@tonic-gate  * for a range of pages, specifying the events that need to occur.
1600Sstevel@tonic-gate  * When as_do_callbacks is called and finds a 'matching' entry, the
1610Sstevel@tonic-gate  * callback is called once, and the callback function MUST call
1620Sstevel@tonic-gate  * as_delete_callback when all callback activities are complete.
1630Sstevel@tonic-gate  * The thread calling as_do_callbacks blocks until the as_delete_callback
1640Sstevel@tonic-gate  * is called.  This allows for asynchorous events to subside before the
1650Sstevel@tonic-gate  * as_do_callbacks thread continues.
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  * An example of the need for this is a driver which has done long-term
1680Sstevel@tonic-gate  * locking of memory.  Address space management operations (events) such
1690Sstevel@tonic-gate  * as as_free, as_umap, and as_setprot will block indefinitely until the
1700Sstevel@tonic-gate  * pertinent memory is unlocked.  The callback mechanism provides the
1710Sstevel@tonic-gate  * way to inform the driver of the event so that the driver may do the
1720Sstevel@tonic-gate  * necessary unlocking.
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * The contents of this structure is protected by a_contents lock
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate typedef void (*callback_func_t)(struct as *, void *, uint_t);
1770Sstevel@tonic-gate struct as_callback {
1780Sstevel@tonic-gate 	struct as_callback	*ascb_next;		/* list link */
1790Sstevel@tonic-gate 	uint_t			ascb_events;		/* event types */
1800Sstevel@tonic-gate 	callback_func_t		ascb_func;   		/* callback function */
1810Sstevel@tonic-gate 	void			*ascb_arg;		/* callback argument */
1820Sstevel@tonic-gate 	caddr_t			ascb_saddr;		/* start address */
1830Sstevel@tonic-gate 	size_t			ascb_len;		/* address range */
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate  * Callback events
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate #define	AS_FREE_EVENT		0x1
1890Sstevel@tonic-gate #define	AS_SETPROT_EVENT	0x2
1900Sstevel@tonic-gate #define	AS_UNMAP_EVENT		0x4
1910Sstevel@tonic-gate #define	AS_CALLBACK_CALLED	((uint_t)(1U << (8 * sizeof (uint_t) - 1U)))
1920Sstevel@tonic-gate #define	AS_UNMAPWAIT_EVENT				\
1930Sstevel@tonic-gate 		(AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT)
1940Sstevel@tonic-gate #define	AS_ALL_EVENT					\
1950Sstevel@tonic-gate 		(AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT)
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /* Return code values for as_callback_delete */
1990Sstevel@tonic-gate enum as_cbdelete_rc {
2000Sstevel@tonic-gate 	AS_CALLBACK_DELETED,
2010Sstevel@tonic-gate 	AS_CALLBACK_NOTFOUND,
2020Sstevel@tonic-gate 	AS_CALLBACK_DELETE_DEFERRED
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate #ifdef _KERNEL
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * Flags for as_gap.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate #define	AH_DIR		0x1	/* direction flag mask */
2110Sstevel@tonic-gate #define	AH_LO		0x0	/* find lowest hole */
2120Sstevel@tonic-gate #define	AH_HI		0x1	/* find highest hole */
2130Sstevel@tonic-gate #define	AH_CONTAIN	0x2	/* hole must contain `addr' */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate extern struct as kas;		/* kernel's address space */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Macros for address space locking.
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate #define	AS_LOCK_ENTER(as, lock, type)		rw_enter((lock), (type))
2210Sstevel@tonic-gate #define	AS_LOCK_EXIT(as, lock)			rw_exit((lock))
2220Sstevel@tonic-gate #define	AS_LOCK_DESTROY(as, lock)		rw_destroy((lock))
2230Sstevel@tonic-gate #define	AS_LOCK_TRYENTER(as, lock, type)	rw_tryenter((lock), (type))
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Macros to test lock states.
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate #define	AS_LOCK_HELD(as, lock)		RW_LOCK_HELD((lock))
2290Sstevel@tonic-gate #define	AS_READ_HELD(as, lock)		RW_READ_HELD((lock))
2300Sstevel@tonic-gate #define	AS_WRITE_HELD(as, lock)		RW_WRITE_HELD((lock))
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * macros to walk thru segment lists
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate #define	AS_SEGFIRST(as)		avl_first(&(as)->a_segtree)
2360Sstevel@tonic-gate #define	AS_SEGNEXT(as, seg)	AVL_NEXT(&(as)->a_segtree, (seg))
2370Sstevel@tonic-gate #define	AS_SEGPREV(as, seg)	AVL_PREV(&(as)->a_segtree, (seg))
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate void	as_init(void);
2400Sstevel@tonic-gate void	as_avlinit(struct as *);
2410Sstevel@tonic-gate struct	seg *as_segat(struct as *as, caddr_t addr);
2420Sstevel@tonic-gate void	as_rangelock(struct as *as);
2430Sstevel@tonic-gate void	as_rangeunlock(struct as *as);
244*2768Ssl108498 struct	as *as_alloc();
2450Sstevel@tonic-gate void	as_free(struct as *as);
2460Sstevel@tonic-gate int	as_dup(struct as *as, struct as **outas);
2470Sstevel@tonic-gate struct	seg *as_findseg(struct as *as, caddr_t addr, int tail);
2480Sstevel@tonic-gate int	as_addseg(struct as *as, struct seg *newseg);
2490Sstevel@tonic-gate struct	seg *as_removeseg(struct as *as, struct seg *seg);
2500Sstevel@tonic-gate faultcode_t as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size,
2510Sstevel@tonic-gate 		enum fault_type type, enum seg_rw rw);
2520Sstevel@tonic-gate faultcode_t as_faulta(struct as *as, caddr_t addr, size_t size);
2530Sstevel@tonic-gate int	as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot);
2540Sstevel@tonic-gate int	as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot);
2550Sstevel@tonic-gate int	as_unmap(struct as *as, caddr_t addr, size_t size);
2560Sstevel@tonic-gate int	as_map(struct as *as, caddr_t addr, size_t size, int ((*crfp)()),
2570Sstevel@tonic-gate 		void *argsp);
2580Sstevel@tonic-gate void	as_purge(struct as *as);
2590Sstevel@tonic-gate int	as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp,
2600Sstevel@tonic-gate 		uint_t flags, caddr_t addr);
2610Sstevel@tonic-gate int	as_memory(struct as *as, caddr_t *basep, size_t *lenp);
2620Sstevel@tonic-gate size_t	as_swapout(struct as *as);
2630Sstevel@tonic-gate int	as_incore(struct as *as, caddr_t addr, size_t size, char *vec,
2640Sstevel@tonic-gate 		size_t *sizep);
2650Sstevel@tonic-gate int	as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr,
2660Sstevel@tonic-gate 		uintptr_t arg, ulong_t *lock_map, size_t pos);
2670Sstevel@tonic-gate int	as_exec(struct as *oas, caddr_t ostka, size_t stksz,
2680Sstevel@tonic-gate 		struct as *nas, caddr_t nstka, uint_t hatflag);
2690Sstevel@tonic-gate int	as_pagelock(struct as *as, struct page ***ppp, caddr_t addr,
2700Sstevel@tonic-gate 		size_t size, enum seg_rw rw);
2710Sstevel@tonic-gate void	as_pageunlock(struct as *as, struct page **pp, caddr_t addr,
2720Sstevel@tonic-gate 		size_t size, enum seg_rw rw);
2730Sstevel@tonic-gate void	as_pagereclaim(struct as *as, struct page **pp, caddr_t addr,
2740Sstevel@tonic-gate 		size_t size, enum seg_rw rw);
2750Sstevel@tonic-gate int	as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc,
2760Sstevel@tonic-gate 		boolean_t wait);
2770Sstevel@tonic-gate void	as_setwatch(struct as *as);
2780Sstevel@tonic-gate void	as_clearwatch(struct as *as);
2790Sstevel@tonic-gate int	as_getmemid(struct as *, caddr_t, memid_t *);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate int	as_add_callback(struct as *, void (*)(), void *, uint_t,
2820Sstevel@tonic-gate 			caddr_t, size_t, int);
2830Sstevel@tonic-gate uint_t	as_delete_callback(struct as *, void *);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate #endif	/* _KERNEL */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate #ifdef	__cplusplus
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate #endif
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate #endif	/* _VM_AS_H */
292