xref: /titanic_44/usr/src/uts/common/vm/as.h (revision 342440ec94087b8c751c580ab9ed6c693d31d418)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /*	 All Rights Reserved   */
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #ifndef	_VM_AS_H
40 #define	_VM_AS_H
41 
42 #pragma ident	"%Z%%M%	%I%	%E% SMI"
43 
44 #include <sys/watchpoint.h>
45 #include <vm/seg.h>
46 #include <vm/faultcode.h>
47 #include <vm/hat.h>
48 #include <sys/avl.h>
49 #include <sys/proc.h>
50 
51 #ifdef	__cplusplus
52 extern "C" {
53 #endif
54 
55 /*
56  * VM - Address spaces.
57  */
58 
59 /*
60  * Each address space consists of a sorted list of segments
61  * and machine dependent address translation information.
62  *
63  * All the hard work is in the segment drivers and the
64  * hardware address translation code.
65  *
66  * The segment list is represented as an AVL tree.
67  *
68  * The address space lock (a_lock) is a long term lock which serializes
69  * access to certain operations (as_map, as_unmap) and protects the
70  * underlying generic segment data (seg.h) along with some fields in the
71  * address space structure as shown below:
72  *
73  *	address space structure 	segment structure
74  *
75  *	a_segtree			s_base
76  *	a_size				s_size
77  *	a_lastgap			s_link
78  *	a_seglast			s_ops
79  *					s_as
80  *					s_data
81  *
82  * The address space contents lock (a_contents) is a short term
83  * lock that protects most of the data in the address space structure.
84  * This lock is always acquired after the "a_lock" in all situations
85  * except while dealing with AS_CLAIMGAP to avoid deadlocks.
86  *
87  * The following fields are protected by this lock:
88  *
89  *	a_flags (AS_PAGLCK, AS_CLAIMGAP, etc.)
90  *	a_unmapwait
91  *	a_seglast
92  *
93  * The address space lock (a_lock) is always held prior to any segment
94  * operation.  Some segment drivers use the address space lock to protect
95  * some or all of their segment private data, provided the version of
96  * "a_lock" (read vs. write) is consistent with the use of the data.
97  *
98  * The following fields are protected by the hat layer lock:
99  *
100  *	a_vbits
101  *	a_hat
102  *	a_hrm
103  */
104 
105 struct as {
106 	kmutex_t a_contents;	/* protect certain fields in the structure */
107 	uchar_t  a_flags;	/* as attributes */
108 	uchar_t	a_vbits;	/* used for collecting statistics */
109 	kcondvar_t a_cv;	/* used by as_rangelock */
110 	struct	hat *a_hat;	/* hat structure */
111 	struct	hrmstat *a_hrm; /* ref and mod bits */
112 	caddr_t	a_userlimit;	/* highest allowable address in this as */
113 	struct seg *a_seglast;	/* last segment hit on the addr space */
114 	krwlock_t a_lock;	/* protects segment related fields */
115 	size_t	a_size;		/* size of address space */
116 	struct seg *a_lastgap;	/* last seg found by as_gap() w/ AS_HI (mmap) */
117 	struct seg *a_lastgaphl; /* last seg saved in as_gap() either for */
118 				/* AS_HI or AS_LO used in as_addseg() */
119 	avl_tree_t a_segtree;	/* segments in this address space. (AVL tree) */
120 	avl_tree_t a_wpage;	/* watched pages (procfs) */
121 	uchar_t	a_updatedir;	/* mappings changed, rebuild a_objectdir */
122 	timespec_t a_updatetime;	/* time when mappings last changed */
123 	vnode_t	**a_objectdir;	/* object directory (procfs) */
124 	size_t	a_sizedir;	/* size of object directory */
125 	struct as_callback *a_callbacks; /* callback list */
126 	void *a_xhat;		/* list of xhat providers */
127 	proc_t	*a_proc;	/* back pointer to proc */
128 };
129 
130 #define	AS_PAGLCK		0x80
131 #define	AS_CLAIMGAP		0x40
132 #define	AS_UNMAPWAIT		0x20
133 #define	AS_NEEDSPURGE		0x10	/* mostly for seg_nf, see as_purge() */
134 #define	AS_NOUNMAPWAIT		0x02
135 #define	AS_BUSY			0x01	/* needed by XHAT framework */
136 
137 #define	AS_ISPGLCK(as)		((as)->a_flags & AS_PAGLCK)
138 #define	AS_ISCLAIMGAP(as)	((as)->a_flags & AS_CLAIMGAP)
139 #define	AS_ISUNMAPWAIT(as)	((as)->a_flags & AS_UNMAPWAIT)
140 #define	AS_ISBUSY(as)		((as)->a_flags & AS_BUSY)
141 #define	AS_ISNOUNMAPWAIT(as)	((as)->a_flags & AS_NOUNMAPWAIT)
142 
143 #define	AS_SETPGLCK(as)		((as)->a_flags |= AS_PAGLCK)
144 #define	AS_SETCLAIMGAP(as)	((as)->a_flags |= AS_CLAIMGAP)
145 #define	AS_SETUNMAPWAIT(as)	((as)->a_flags |= AS_UNMAPWAIT)
146 #define	AS_SETBUSY(as)		((as)->a_flags |= AS_BUSY)
147 #define	AS_SETNOUNMAPWAIT(as)	((as)->a_flags |= AS_NOUNMAPWAIT)
148 
149 #define	AS_CLRPGLCK(as)		((as)->a_flags &= ~AS_PAGLCK)
150 #define	AS_CLRCLAIMGAP(as)	((as)->a_flags &= ~AS_CLAIMGAP)
151 #define	AS_CLRUNMAPWAIT(as)	((as)->a_flags &= ~AS_UNMAPWAIT)
152 #define	AS_CLRBUSY(as)		((as)->a_flags &= ~AS_BUSY)
153 #define	AS_CLRNOUNMAPWAIT(as)	((as)->a_flags &= ~AS_NOUNMAPWAIT)
154 
155 #define	AS_TYPE_64BIT(as)	\
156 	    (((as)->a_userlimit > (caddr_t)UINT32_MAX) ? 1 : 0)
157 
158 /*
159  * Flags for as_map/as_map_ansegs
160  */
161 #define	AS_MAP_NO_LPOOB		((uint_t)-1)
162 #define	AS_MAP_HEAP		((uint_t)-2)
163 #define	AS_MAP_STACK		((uint_t)-3)
164 
165 /*
166  * The as_callback is the basic structure which supports the ability to
167  * inform clients of specific events pertaining to address space management.
168  * A user calls as_add_callback to register an address space callback
169  * for a range of pages, specifying the events that need to occur.
170  * When as_do_callbacks is called and finds a 'matching' entry, the
171  * callback is called once, and the callback function MUST call
172  * as_delete_callback when all callback activities are complete.
173  * The thread calling as_do_callbacks blocks until the as_delete_callback
174  * is called.  This allows for asynchorous events to subside before the
175  * as_do_callbacks thread continues.
176  *
177  * An example of the need for this is a driver which has done long-term
178  * locking of memory.  Address space management operations (events) such
179  * as as_free, as_umap, and as_setprot will block indefinitely until the
180  * pertinent memory is unlocked.  The callback mechanism provides the
181  * way to inform the driver of the event so that the driver may do the
182  * necessary unlocking.
183  *
184  * The contents of this structure is protected by a_contents lock
185  */
186 typedef void (*callback_func_t)(struct as *, void *, uint_t);
187 struct as_callback {
188 	struct as_callback	*ascb_next;		/* list link */
189 	uint_t			ascb_events;		/* event types */
190 	callback_func_t		ascb_func;   		/* callback function */
191 	void			*ascb_arg;		/* callback argument */
192 	caddr_t			ascb_saddr;		/* start address */
193 	size_t			ascb_len;		/* address range */
194 };
195 /*
196  * Callback events
197  */
198 #define	AS_FREE_EVENT		0x1
199 #define	AS_SETPROT_EVENT	0x2
200 #define	AS_UNMAP_EVENT		0x4
201 #define	AS_CALLBACK_CALLED	((uint_t)(1U << (8 * sizeof (uint_t) - 1U)))
202 #define	AS_UNMAPWAIT_EVENT				\
203 		(AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT)
204 #define	AS_ALL_EVENT					\
205 		(AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT)
206 
207 
208 /* Return code values for as_callback_delete */
209 enum as_cbdelete_rc {
210 	AS_CALLBACK_DELETED,
211 	AS_CALLBACK_NOTFOUND,
212 	AS_CALLBACK_DELETE_DEFERRED
213 };
214 
215 #ifdef _KERNEL
216 
217 /*
218  * Flags for as_gap.
219  */
220 #define	AH_DIR		0x1	/* direction flag mask */
221 #define	AH_LO		0x0	/* find lowest hole */
222 #define	AH_HI		0x1	/* find highest hole */
223 #define	AH_CONTAIN	0x2	/* hole must contain `addr' */
224 
225 extern struct as kas;		/* kernel's address space */
226 
227 /*
228  * Macros for address space locking.
229  */
230 #define	AS_LOCK_ENTER(as, lock, type)		rw_enter((lock), (type))
231 #define	AS_LOCK_EXIT(as, lock)			rw_exit((lock))
232 #define	AS_LOCK_DESTROY(as, lock)		rw_destroy((lock))
233 #define	AS_LOCK_TRYENTER(as, lock, type)	rw_tryenter((lock), (type))
234 
235 /*
236  * Macros to test lock states.
237  */
238 #define	AS_LOCK_HELD(as, lock)		RW_LOCK_HELD((lock))
239 #define	AS_READ_HELD(as, lock)		RW_READ_HELD((lock))
240 #define	AS_WRITE_HELD(as, lock)		RW_WRITE_HELD((lock))
241 
242 /*
243  * macros to walk thru segment lists
244  */
245 #define	AS_SEGFIRST(as)		avl_first(&(as)->a_segtree)
246 #define	AS_SEGNEXT(as, seg)	AVL_NEXT(&(as)->a_segtree, (seg))
247 #define	AS_SEGPREV(as, seg)	AVL_PREV(&(as)->a_segtree, (seg))
248 
249 void	as_init(void);
250 void	as_avlinit(struct as *);
251 struct	seg *as_segat(struct as *as, caddr_t addr);
252 void	as_rangelock(struct as *as);
253 void	as_rangeunlock(struct as *as);
254 struct	as *as_alloc();
255 void	as_free(struct as *as);
256 int	as_dup(struct as *as, struct as **outas);
257 struct	seg *as_findseg(struct as *as, caddr_t addr, int tail);
258 int	as_addseg(struct as *as, struct seg *newseg);
259 struct	seg *as_removeseg(struct as *as, struct seg *seg);
260 faultcode_t as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size,
261 		enum fault_type type, enum seg_rw rw);
262 faultcode_t as_faulta(struct as *as, caddr_t addr, size_t size);
263 int	as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot);
264 int	as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot);
265 int	as_unmap(struct as *as, caddr_t addr, size_t size);
266 int	as_map(struct as *as, caddr_t addr, size_t size, int ((*crfp)()),
267 		void *argsp);
268 void	as_purge(struct as *as);
269 int	as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp,
270 		uint_t flags, caddr_t addr);
271 int	as_gap_aligned(struct as *as, size_t minlen, caddr_t *basep,
272 	    size_t *lenp, uint_t flags, caddr_t addr, size_t align,
273 	    size_t redzone, size_t off);
274 
275 int	as_memory(struct as *as, caddr_t *basep, size_t *lenp);
276 size_t	as_swapout(struct as *as);
277 int	as_incore(struct as *as, caddr_t addr, size_t size, char *vec,
278 		size_t *sizep);
279 int	as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr,
280 		uintptr_t arg, ulong_t *lock_map, size_t pos);
281 int	as_exec(struct as *oas, caddr_t ostka, size_t stksz,
282 		struct as *nas, caddr_t nstka, uint_t hatflag);
283 int	as_pagelock(struct as *as, struct page ***ppp, caddr_t addr,
284 		size_t size, enum seg_rw rw);
285 void	as_pageunlock(struct as *as, struct page **pp, caddr_t addr,
286 		size_t size, enum seg_rw rw);
287 int	as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc,
288 		boolean_t wait);
289 int	as_set_default_lpsize(struct as *as, caddr_t addr, size_t size);
290 void	as_setwatch(struct as *as);
291 void	as_clearwatch(struct as *as);
292 int	as_getmemid(struct as *, caddr_t, memid_t *);
293 
294 int	as_add_callback(struct as *, void (*)(), void *, uint_t,
295 			caddr_t, size_t, int);
296 uint_t	as_delete_callback(struct as *, void *);
297 
298 #endif	/* _KERNEL */
299 
300 #ifdef	__cplusplus
301 }
302 #endif
303 
304 #endif	/* _VM_AS_H */
305