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; /* total 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 size_t a_resvsize; /* size of reserved part of address space */ 127 }; 128 129 #define AS_PAGLCK 0x80 130 #define AS_CLAIMGAP 0x40 131 #define AS_UNMAPWAIT 0x20 132 #define AS_NEEDSPURGE 0x10 /* mostly for seg_nf, see as_purge() */ 133 #define AS_NOUNMAPWAIT 0x02 134 #define AS_BUSY 0x01 /* needed by XHAT framework */ 135 136 #define AS_ISPGLCK(as) ((as)->a_flags & AS_PAGLCK) 137 #define AS_ISCLAIMGAP(as) ((as)->a_flags & AS_CLAIMGAP) 138 #define AS_ISUNMAPWAIT(as) ((as)->a_flags & AS_UNMAPWAIT) 139 #define AS_ISBUSY(as) ((as)->a_flags & AS_BUSY) 140 #define AS_ISNOUNMAPWAIT(as) ((as)->a_flags & AS_NOUNMAPWAIT) 141 142 #define AS_SETPGLCK(as) ((as)->a_flags |= AS_PAGLCK) 143 #define AS_SETCLAIMGAP(as) ((as)->a_flags |= AS_CLAIMGAP) 144 #define AS_SETUNMAPWAIT(as) ((as)->a_flags |= AS_UNMAPWAIT) 145 #define AS_SETBUSY(as) ((as)->a_flags |= AS_BUSY) 146 #define AS_SETNOUNMAPWAIT(as) ((as)->a_flags |= AS_NOUNMAPWAIT) 147 148 #define AS_CLRPGLCK(as) ((as)->a_flags &= ~AS_PAGLCK) 149 #define AS_CLRCLAIMGAP(as) ((as)->a_flags &= ~AS_CLAIMGAP) 150 #define AS_CLRUNMAPWAIT(as) ((as)->a_flags &= ~AS_UNMAPWAIT) 151 #define AS_CLRBUSY(as) ((as)->a_flags &= ~AS_BUSY) 152 #define AS_CLRNOUNMAPWAIT(as) ((as)->a_flags &= ~AS_NOUNMAPWAIT) 153 154 #define AS_TYPE_64BIT(as) \ 155 (((as)->a_userlimit > (caddr_t)UINT32_MAX) ? 1 : 0) 156 157 /* 158 * Flags for as_map/as_map_ansegs 159 */ 160 #define AS_MAP_NO_LPOOB ((uint_t)-1) 161 #define AS_MAP_HEAP ((uint_t)-2) 162 #define AS_MAP_STACK ((uint_t)-3) 163 164 /* 165 * The as_callback is the basic structure which supports the ability to 166 * inform clients of specific events pertaining to address space management. 167 * A user calls as_add_callback to register an address space callback 168 * for a range of pages, specifying the events that need to occur. 169 * When as_do_callbacks is called and finds a 'matching' entry, the 170 * callback is called once, and the callback function MUST call 171 * as_delete_callback when all callback activities are complete. 172 * The thread calling as_do_callbacks blocks until the as_delete_callback 173 * is called. This allows for asynchorous events to subside before the 174 * as_do_callbacks thread continues. 175 * 176 * An example of the need for this is a driver which has done long-term 177 * locking of memory. Address space management operations (events) such 178 * as as_free, as_umap, and as_setprot will block indefinitely until the 179 * pertinent memory is unlocked. The callback mechanism provides the 180 * way to inform the driver of the event so that the driver may do the 181 * necessary unlocking. 182 * 183 * The contents of this structure is protected by a_contents lock 184 */ 185 typedef void (*callback_func_t)(struct as *, void *, uint_t); 186 struct as_callback { 187 struct as_callback *ascb_next; /* list link */ 188 uint_t ascb_events; /* event types */ 189 callback_func_t ascb_func; /* callback function */ 190 void *ascb_arg; /* callback argument */ 191 caddr_t ascb_saddr; /* start address */ 192 size_t ascb_len; /* address range */ 193 }; 194 /* 195 * Callback events 196 */ 197 #define AS_FREE_EVENT 0x1 198 #define AS_SETPROT_EVENT 0x2 199 #define AS_UNMAP_EVENT 0x4 200 #define AS_CALLBACK_CALLED ((uint_t)(1U << (8 * sizeof (uint_t) - 1U))) 201 #define AS_UNMAPWAIT_EVENT \ 202 (AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT) 203 #define AS_ALL_EVENT \ 204 (AS_FREE_EVENT | AS_SETPROT_EVENT | AS_UNMAP_EVENT) 205 206 207 /* Return code values for as_callback_delete */ 208 enum as_cbdelete_rc { 209 AS_CALLBACK_DELETED, 210 AS_CALLBACK_NOTFOUND, 211 AS_CALLBACK_DELETE_DEFERRED 212 }; 213 214 #ifdef _KERNEL 215 216 /* 217 * Flags for as_gap. 218 */ 219 #define AH_DIR 0x1 /* direction flag mask */ 220 #define AH_LO 0x0 /* find lowest hole */ 221 #define AH_HI 0x1 /* find highest hole */ 222 #define AH_CONTAIN 0x2 /* hole must contain `addr' */ 223 224 extern struct as kas; /* kernel's address space */ 225 226 /* 227 * Macros for address space locking. 228 */ 229 #define AS_LOCK_ENTER(as, lock, type) rw_enter((lock), (type)) 230 #define AS_LOCK_EXIT(as, lock) rw_exit((lock)) 231 #define AS_LOCK_DESTROY(as, lock) rw_destroy((lock)) 232 #define AS_LOCK_TRYENTER(as, lock, type) rw_tryenter((lock), (type)) 233 234 /* 235 * Macros to test lock states. 236 */ 237 #define AS_LOCK_HELD(as, lock) RW_LOCK_HELD((lock)) 238 #define AS_READ_HELD(as, lock) RW_READ_HELD((lock)) 239 #define AS_WRITE_HELD(as, lock) RW_WRITE_HELD((lock)) 240 241 /* 242 * macros to walk thru segment lists 243 */ 244 #define AS_SEGFIRST(as) avl_first(&(as)->a_segtree) 245 #define AS_SEGNEXT(as, seg) AVL_NEXT(&(as)->a_segtree, (seg)) 246 #define AS_SEGPREV(as, seg) AVL_PREV(&(as)->a_segtree, (seg)) 247 248 void as_init(void); 249 void as_avlinit(struct as *); 250 struct seg *as_segat(struct as *as, caddr_t addr); 251 void as_rangelock(struct as *as); 252 void as_rangeunlock(struct as *as); 253 struct as *as_alloc(); 254 void as_free(struct as *as); 255 int as_dup(struct as *as, struct proc *forkedproc); 256 struct seg *as_findseg(struct as *as, caddr_t addr, int tail); 257 int as_addseg(struct as *as, struct seg *newseg); 258 struct seg *as_removeseg(struct as *as, struct seg *seg); 259 faultcode_t as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size, 260 enum fault_type type, enum seg_rw rw); 261 faultcode_t as_faulta(struct as *as, caddr_t addr, size_t size); 262 int as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot); 263 int as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot); 264 int as_unmap(struct as *as, caddr_t addr, size_t size); 265 int as_map(struct as *as, caddr_t addr, size_t size, int ((*crfp)()), 266 void *argsp); 267 void as_purge(struct as *as); 268 int as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, 269 uint_t flags, caddr_t addr); 270 int as_gap_aligned(struct as *as, size_t minlen, caddr_t *basep, 271 size_t *lenp, uint_t flags, caddr_t addr, size_t align, 272 size_t redzone, size_t off); 273 274 int as_memory(struct as *as, caddr_t *basep, size_t *lenp); 275 size_t as_swapout(struct as *as); 276 int as_incore(struct as *as, caddr_t addr, size_t size, char *vec, 277 size_t *sizep); 278 int as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr, 279 uintptr_t arg, ulong_t *lock_map, size_t pos); 280 int as_pagelock(struct as *as, struct page ***ppp, caddr_t addr, 281 size_t size, enum seg_rw rw); 282 void as_pageunlock(struct as *as, struct page **pp, caddr_t addr, 283 size_t size, enum seg_rw rw); 284 int as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc, 285 boolean_t wait); 286 int as_set_default_lpsize(struct as *as, caddr_t addr, size_t size); 287 void as_setwatch(struct as *as); 288 void as_clearwatch(struct as *as); 289 int as_getmemid(struct as *, caddr_t, memid_t *); 290 291 int as_add_callback(struct as *, void (*)(), void *, uint_t, 292 caddr_t, size_t, int); 293 uint_t as_delete_callback(struct as *, void *); 294 295 #endif /* _KERNEL */ 296 297 #ifdef __cplusplus 298 } 299 #endif 300 301 #endif /* _VM_AS_H */ 302