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