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