1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* 31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*0Sstevel@tonic-gate * The Regents of the University of California 33*0Sstevel@tonic-gate * All Rights Reserved 34*0Sstevel@tonic-gate * 35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*0Sstevel@tonic-gate * contributors. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * VM - address spaces. 44*0Sstevel@tonic-gate */ 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate #include <sys/types.h> 47*0Sstevel@tonic-gate #include <sys/t_lock.h> 48*0Sstevel@tonic-gate #include <sys/param.h> 49*0Sstevel@tonic-gate #include <sys/errno.h> 50*0Sstevel@tonic-gate #include <sys/systm.h> 51*0Sstevel@tonic-gate #include <sys/mman.h> 52*0Sstevel@tonic-gate #include <sys/sysmacros.h> 53*0Sstevel@tonic-gate #include <sys/cpuvar.h> 54*0Sstevel@tonic-gate #include <sys/sysinfo.h> 55*0Sstevel@tonic-gate #include <sys/kmem.h> 56*0Sstevel@tonic-gate #include <sys/vnode.h> 57*0Sstevel@tonic-gate #include <sys/vmsystm.h> 58*0Sstevel@tonic-gate #include <sys/cmn_err.h> 59*0Sstevel@tonic-gate #include <sys/debug.h> 60*0Sstevel@tonic-gate #include <sys/tnf_probe.h> 61*0Sstevel@tonic-gate #include <sys/vtrace.h> 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate #include <vm/hat.h> 64*0Sstevel@tonic-gate #include <vm/xhat.h> 65*0Sstevel@tonic-gate #include <vm/as.h> 66*0Sstevel@tonic-gate #include <vm/seg.h> 67*0Sstevel@tonic-gate #include <vm/seg_vn.h> 68*0Sstevel@tonic-gate #include <vm/seg_dev.h> 69*0Sstevel@tonic-gate #include <vm/seg_kmem.h> 70*0Sstevel@tonic-gate #include <vm/seg_map.h> 71*0Sstevel@tonic-gate #include <vm/seg_spt.h> 72*0Sstevel@tonic-gate #include <vm/page.h> 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate clock_t deadlk_wait = 1; /* number of ticks to wait before retrying */ 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate static struct kmem_cache *as_cache; 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate static void as_setwatchprot(struct as *, caddr_t, size_t, uint_t); 79*0Sstevel@tonic-gate static void as_clearwatchprot(struct as *, caddr_t, size_t); 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate /* 83*0Sstevel@tonic-gate * Verifying the segment lists is very time-consuming; it may not be 84*0Sstevel@tonic-gate * desirable always to define VERIFY_SEGLIST when DEBUG is set. 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate #ifdef DEBUG 87*0Sstevel@tonic-gate #define VERIFY_SEGLIST 88*0Sstevel@tonic-gate int do_as_verify = 0; 89*0Sstevel@tonic-gate #endif 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate /* 92*0Sstevel@tonic-gate * Allocate a new callback data structure entry and fill in the events of 93*0Sstevel@tonic-gate * interest, the address range of interest, and the callback argument. 94*0Sstevel@tonic-gate * Link the entry on the as->a_callbacks list. A callback entry for the 95*0Sstevel@tonic-gate * entire address space may be specified with vaddr = 0 and size = -1. 96*0Sstevel@tonic-gate * 97*0Sstevel@tonic-gate * CALLERS RESPONSIBILITY: If not calling from within the process context for 98*0Sstevel@tonic-gate * the specified as, the caller must guarantee persistence of the specified as 99*0Sstevel@tonic-gate * for the duration of this function (eg. pages being locked within the as 100*0Sstevel@tonic-gate * will guarantee persistence). 101*0Sstevel@tonic-gate */ 102*0Sstevel@tonic-gate int 103*0Sstevel@tonic-gate as_add_callback(struct as *as, void (*cb_func)(), void *arg, uint_t events, 104*0Sstevel@tonic-gate caddr_t vaddr, size_t size, int sleepflag) 105*0Sstevel@tonic-gate { 106*0Sstevel@tonic-gate struct as_callback *current_head, *cb; 107*0Sstevel@tonic-gate caddr_t saddr; 108*0Sstevel@tonic-gate size_t rsize; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate /* callback function and an event are mandatory */ 111*0Sstevel@tonic-gate if ((cb_func == NULL) || ((events & AS_ALL_EVENT) == 0)) 112*0Sstevel@tonic-gate return (EINVAL); 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate /* Adding a callback after as_free has been called is not allowed */ 115*0Sstevel@tonic-gate if (as == &kas) 116*0Sstevel@tonic-gate return (ENOMEM); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 119*0Sstevel@tonic-gate * vaddr = 0 and size = -1 is used to indicate that the callback range 120*0Sstevel@tonic-gate * is the entire address space so no rounding is done in that case. 121*0Sstevel@tonic-gate */ 122*0Sstevel@tonic-gate if (size != -1) { 123*0Sstevel@tonic-gate saddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK); 124*0Sstevel@tonic-gate rsize = (((size_t)(vaddr + size) + PAGEOFFSET) & PAGEMASK) - 125*0Sstevel@tonic-gate (size_t)saddr; 126*0Sstevel@tonic-gate /* check for wraparound */ 127*0Sstevel@tonic-gate if (saddr + rsize < saddr) 128*0Sstevel@tonic-gate return (ENOMEM); 129*0Sstevel@tonic-gate } else { 130*0Sstevel@tonic-gate if (vaddr != 0) 131*0Sstevel@tonic-gate return (EINVAL); 132*0Sstevel@tonic-gate saddr = vaddr; 133*0Sstevel@tonic-gate rsize = size; 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate /* Allocate and initialize a callback entry */ 137*0Sstevel@tonic-gate cb = kmem_zalloc(sizeof (struct as_callback), sleepflag); 138*0Sstevel@tonic-gate if (cb == NULL) 139*0Sstevel@tonic-gate return (EAGAIN); 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate cb->ascb_func = cb_func; 142*0Sstevel@tonic-gate cb->ascb_arg = arg; 143*0Sstevel@tonic-gate cb->ascb_events = events; 144*0Sstevel@tonic-gate cb->ascb_saddr = saddr; 145*0Sstevel@tonic-gate cb->ascb_len = rsize; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate /* Add the entry to the list */ 148*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 149*0Sstevel@tonic-gate current_head = as->a_callbacks; 150*0Sstevel@tonic-gate as->a_callbacks = cb; 151*0Sstevel@tonic-gate cb->ascb_next = current_head; 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate /* 154*0Sstevel@tonic-gate * The call to this function may lose in a race with 155*0Sstevel@tonic-gate * a pertinent event - eg. a thread does long term memory locking 156*0Sstevel@tonic-gate * but before the callback is added another thread executes as_unmap. 157*0Sstevel@tonic-gate * A broadcast here resolves that. 158*0Sstevel@tonic-gate */ 159*0Sstevel@tonic-gate if ((cb->ascb_events & AS_UNMAPWAIT_EVENT) && AS_ISUNMAPWAIT(as)) { 160*0Sstevel@tonic-gate AS_CLRUNMAPWAIT(as); 161*0Sstevel@tonic-gate cv_broadcast(&as->a_cv); 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 165*0Sstevel@tonic-gate return (0); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* 169*0Sstevel@tonic-gate * Search the callback list for an entry which pertains to arg. 170*0Sstevel@tonic-gate * 171*0Sstevel@tonic-gate * This is called from within the client upon completion of the callback. 172*0Sstevel@tonic-gate * RETURN VALUES: 173*0Sstevel@tonic-gate * AS_CALLBACK_DELETED (callback entry found and deleted) 174*0Sstevel@tonic-gate * AS_CALLBACK_NOTFOUND (no callback entry found - this is ok) 175*0Sstevel@tonic-gate * AS_CALLBACK_DELETE_DEFERRED (callback is in process, delete of this 176*0Sstevel@tonic-gate * entry will be made in as_do_callbacks) 177*0Sstevel@tonic-gate * 178*0Sstevel@tonic-gate * If as_delete_callback encounters a matching entry with AS_CALLBACK_CALLED 179*0Sstevel@tonic-gate * set, it indicates that as_do_callbacks is processing this entry. The 180*0Sstevel@tonic-gate * AS_ALL_EVENT events are cleared in the entry, and a broadcast is made 181*0Sstevel@tonic-gate * to unblock as_do_callbacks, in case it is blocked. 182*0Sstevel@tonic-gate * 183*0Sstevel@tonic-gate * CALLERS RESPONSIBILITY: If not calling from within the process context for 184*0Sstevel@tonic-gate * the specified as, the caller must guarantee persistence of the specified as 185*0Sstevel@tonic-gate * for the duration of this function (eg. pages being locked within the as 186*0Sstevel@tonic-gate * will guarantee persistence). 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate uint_t 189*0Sstevel@tonic-gate as_delete_callback(struct as *as, void *arg) 190*0Sstevel@tonic-gate { 191*0Sstevel@tonic-gate struct as_callback **prevcb = &as->a_callbacks; 192*0Sstevel@tonic-gate struct as_callback *cb; 193*0Sstevel@tonic-gate uint_t rc = AS_CALLBACK_NOTFOUND; 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 196*0Sstevel@tonic-gate for (cb = as->a_callbacks; cb; prevcb = &cb->ascb_next, cb = *prevcb) { 197*0Sstevel@tonic-gate if (cb->ascb_arg != arg) 198*0Sstevel@tonic-gate continue; 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate /* 201*0Sstevel@tonic-gate * If the events indicate AS_CALLBACK_CALLED, just clear 202*0Sstevel@tonic-gate * AS_ALL_EVENT in the events field and wakeup the thread 203*0Sstevel@tonic-gate * that may be waiting in as_do_callbacks. as_do_callbacks 204*0Sstevel@tonic-gate * will take care of removing this entry from the list. In 205*0Sstevel@tonic-gate * that case, return AS_CALLBACK_DELETE_DEFERRED. Otherwise 206*0Sstevel@tonic-gate * (AS_CALLBACK_CALLED not set), just remove it from the 207*0Sstevel@tonic-gate * list, return the memory and return AS_CALLBACK_DELETED. 208*0Sstevel@tonic-gate */ 209*0Sstevel@tonic-gate if ((cb->ascb_events & AS_CALLBACK_CALLED) != 0) { 210*0Sstevel@tonic-gate /* leave AS_CALLBACK_CALLED */ 211*0Sstevel@tonic-gate cb->ascb_events &= ~AS_ALL_EVENT; 212*0Sstevel@tonic-gate rc = AS_CALLBACK_DELETE_DEFERRED; 213*0Sstevel@tonic-gate cv_broadcast(&as->a_cv); 214*0Sstevel@tonic-gate } else { 215*0Sstevel@tonic-gate *prevcb = cb->ascb_next; 216*0Sstevel@tonic-gate kmem_free(cb, sizeof (struct as_callback)); 217*0Sstevel@tonic-gate rc = AS_CALLBACK_DELETED; 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate break; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 222*0Sstevel@tonic-gate return (rc); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate /* 226*0Sstevel@tonic-gate * Searches the as callback list for a matching entry. 227*0Sstevel@tonic-gate * Returns a pointer to the first matching callback, or NULL if 228*0Sstevel@tonic-gate * nothing is found. 229*0Sstevel@tonic-gate * This function never sleeps so it is ok to call it with more 230*0Sstevel@tonic-gate * locks held but the (required) a_contents mutex. 231*0Sstevel@tonic-gate * 232*0Sstevel@tonic-gate * See also comment on as_do_callbacks below. 233*0Sstevel@tonic-gate */ 234*0Sstevel@tonic-gate static struct as_callback * 235*0Sstevel@tonic-gate as_find_callback(struct as *as, uint_t events, caddr_t event_addr, 236*0Sstevel@tonic-gate size_t event_len) 237*0Sstevel@tonic-gate { 238*0Sstevel@tonic-gate struct as_callback *cb; 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&as->a_contents)); 241*0Sstevel@tonic-gate for (cb = as->a_callbacks; cb != NULL; cb = cb->ascb_next) { 242*0Sstevel@tonic-gate /* 243*0Sstevel@tonic-gate * If the callback has not already been called, then 244*0Sstevel@tonic-gate * check if events or address range pertains. An event_len 245*0Sstevel@tonic-gate * of zero means do an unconditional callback. 246*0Sstevel@tonic-gate */ 247*0Sstevel@tonic-gate if (((cb->ascb_events & AS_CALLBACK_CALLED) != 0) || 248*0Sstevel@tonic-gate ((event_len != 0) && (((cb->ascb_events & events) == 0) || 249*0Sstevel@tonic-gate (event_addr + event_len < cb->ascb_saddr) || 250*0Sstevel@tonic-gate (event_addr > (cb->ascb_saddr + cb->ascb_len))))) { 251*0Sstevel@tonic-gate continue; 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate break; 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate return (cb); 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate /* 259*0Sstevel@tonic-gate * Executes a given callback and removes it from the callback list for 260*0Sstevel@tonic-gate * this address space. 261*0Sstevel@tonic-gate * This function may sleep so the caller must drop all locks except 262*0Sstevel@tonic-gate * a_contents before calling this func. 263*0Sstevel@tonic-gate * 264*0Sstevel@tonic-gate * See also comments on as_do_callbacks below. 265*0Sstevel@tonic-gate */ 266*0Sstevel@tonic-gate static void 267*0Sstevel@tonic-gate as_execute_callback(struct as *as, struct as_callback *cb, 268*0Sstevel@tonic-gate uint_t events) 269*0Sstevel@tonic-gate { 270*0Sstevel@tonic-gate struct as_callback **prevcb; 271*0Sstevel@tonic-gate void *cb_arg; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate ASSERT(MUTEX_HELD(&as->a_contents) && (cb->ascb_events & events)); 274*0Sstevel@tonic-gate cb->ascb_events |= AS_CALLBACK_CALLED; 275*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 276*0Sstevel@tonic-gate (*cb->ascb_func)(as, cb->ascb_arg, events); 277*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 278*0Sstevel@tonic-gate /* 279*0Sstevel@tonic-gate * the callback function is required to delete the callback 280*0Sstevel@tonic-gate * when the callback function determines it is OK for 281*0Sstevel@tonic-gate * this thread to continue. as_delete_callback will clear 282*0Sstevel@tonic-gate * the AS_ALL_EVENT in the events field when it is deleted. 283*0Sstevel@tonic-gate * If the callback function called as_delete_callback, 284*0Sstevel@tonic-gate * events will already be cleared and there will be no blocking. 285*0Sstevel@tonic-gate */ 286*0Sstevel@tonic-gate while ((cb->ascb_events & events) != 0) { 287*0Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate /* 290*0Sstevel@tonic-gate * This entry needs to be taken off the list. Normally, the 291*0Sstevel@tonic-gate * callback func itself does that, but unfortunately the list 292*0Sstevel@tonic-gate * may have changed while the callback was running because the 293*0Sstevel@tonic-gate * a_contents mutex was dropped and someone else other than the 294*0Sstevel@tonic-gate * callback func itself could have called as_delete_callback, 295*0Sstevel@tonic-gate * so we have to search to find this entry again. The entry 296*0Sstevel@tonic-gate * must have AS_CALLBACK_CALLED, and have the same 'arg'. 297*0Sstevel@tonic-gate */ 298*0Sstevel@tonic-gate cb_arg = cb->ascb_arg; 299*0Sstevel@tonic-gate prevcb = &as->a_callbacks; 300*0Sstevel@tonic-gate for (cb = as->a_callbacks; cb != NULL; 301*0Sstevel@tonic-gate prevcb = &cb->ascb_next, cb = *prevcb) { 302*0Sstevel@tonic-gate if (((cb->ascb_events & AS_CALLBACK_CALLED) == 0) || 303*0Sstevel@tonic-gate (cb_arg != cb->ascb_arg)) { 304*0Sstevel@tonic-gate continue; 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate *prevcb = cb->ascb_next; 307*0Sstevel@tonic-gate kmem_free(cb, sizeof (struct as_callback)); 308*0Sstevel@tonic-gate break; 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate /* 313*0Sstevel@tonic-gate * Check the callback list for a matching event and intersection of 314*0Sstevel@tonic-gate * address range. If there is a match invoke the callback. Skip an entry if: 315*0Sstevel@tonic-gate * - a callback is already in progress for this entry (AS_CALLBACK_CALLED) 316*0Sstevel@tonic-gate * - not event of interest 317*0Sstevel@tonic-gate * - not address range of interest 318*0Sstevel@tonic-gate * 319*0Sstevel@tonic-gate * An event_len of zero indicates a request for an unconditional callback 320*0Sstevel@tonic-gate * (regardless of event), only the AS_CALLBACK_CALLED is checked. The 321*0Sstevel@tonic-gate * a_contents lock must be dropped before a callback, so only one callback 322*0Sstevel@tonic-gate * can be done before returning. Return -1 (true) if a callback was 323*0Sstevel@tonic-gate * executed and removed from the list, else return 0 (false). 324*0Sstevel@tonic-gate * 325*0Sstevel@tonic-gate * The logically separate parts, i.e. finding a matching callback and 326*0Sstevel@tonic-gate * executing a given callback have been separated into two functions 327*0Sstevel@tonic-gate * so that they can be called with different sets of locks held beyond 328*0Sstevel@tonic-gate * the always-required a_contents. as_find_callback does not sleep so 329*0Sstevel@tonic-gate * it is ok to call it if more locks than a_contents (i.e. the a_lock 330*0Sstevel@tonic-gate * rwlock) are held. as_execute_callback on the other hand may sleep 331*0Sstevel@tonic-gate * so all locks beyond a_contents must be dropped by the caller if one 332*0Sstevel@tonic-gate * does not want to end comatose. 333*0Sstevel@tonic-gate */ 334*0Sstevel@tonic-gate static int 335*0Sstevel@tonic-gate as_do_callbacks(struct as *as, uint_t events, caddr_t event_addr, 336*0Sstevel@tonic-gate size_t event_len) 337*0Sstevel@tonic-gate { 338*0Sstevel@tonic-gate struct as_callback *cb; 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate if ((cb = as_find_callback(as, events, event_addr, event_len))) { 341*0Sstevel@tonic-gate as_execute_callback(as, cb, events); 342*0Sstevel@tonic-gate return (-1); 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate return (0); 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate /* 348*0Sstevel@tonic-gate * Search for the segment containing addr. If a segment containing addr 349*0Sstevel@tonic-gate * exists, that segment is returned. If no such segment exists, and 350*0Sstevel@tonic-gate * the list spans addresses greater than addr, then the first segment 351*0Sstevel@tonic-gate * whose base is greater than addr is returned; otherwise, NULL is 352*0Sstevel@tonic-gate * returned unless tail is true, in which case the last element of the 353*0Sstevel@tonic-gate * list is returned. 354*0Sstevel@tonic-gate * 355*0Sstevel@tonic-gate * a_seglast is used to cache the last found segment for repeated 356*0Sstevel@tonic-gate * searches to the same addr (which happens frequently). 357*0Sstevel@tonic-gate */ 358*0Sstevel@tonic-gate struct seg * 359*0Sstevel@tonic-gate as_findseg(struct as *as, caddr_t addr, int tail) 360*0Sstevel@tonic-gate { 361*0Sstevel@tonic-gate struct seg *seg = as->a_seglast; 362*0Sstevel@tonic-gate avl_index_t where; 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate if (seg != NULL && 367*0Sstevel@tonic-gate seg->s_base <= addr && 368*0Sstevel@tonic-gate addr < seg->s_base + seg->s_size) 369*0Sstevel@tonic-gate return (seg); 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate seg = avl_find(&as->a_segtree, &addr, &where); 372*0Sstevel@tonic-gate if (seg != NULL) 373*0Sstevel@tonic-gate return (as->a_seglast = seg); 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate seg = avl_nearest(&as->a_segtree, where, AVL_AFTER); 376*0Sstevel@tonic-gate if (seg == NULL && tail) 377*0Sstevel@tonic-gate seg = avl_last(&as->a_segtree); 378*0Sstevel@tonic-gate return (as->a_seglast = seg); 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate 381*0Sstevel@tonic-gate #ifdef VERIFY_SEGLIST 382*0Sstevel@tonic-gate /* 383*0Sstevel@tonic-gate * verify that the linked list is coherent 384*0Sstevel@tonic-gate */ 385*0Sstevel@tonic-gate static void 386*0Sstevel@tonic-gate as_verify(struct as *as) 387*0Sstevel@tonic-gate { 388*0Sstevel@tonic-gate struct seg *seg, *seglast, *p, *n; 389*0Sstevel@tonic-gate uint_t nsegs = 0; 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate if (do_as_verify == 0) 392*0Sstevel@tonic-gate return; 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate seglast = as->a_seglast; 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 397*0Sstevel@tonic-gate ASSERT(seg->s_as == as); 398*0Sstevel@tonic-gate p = AS_SEGPREV(as, seg); 399*0Sstevel@tonic-gate n = AS_SEGNEXT(as, seg); 400*0Sstevel@tonic-gate ASSERT(p == NULL || p->s_as == as); 401*0Sstevel@tonic-gate ASSERT(p == NULL || p->s_base < seg->s_base); 402*0Sstevel@tonic-gate ASSERT(n == NULL || n->s_base > seg->s_base); 403*0Sstevel@tonic-gate ASSERT(n != NULL || seg == avl_last(&as->a_segtree)); 404*0Sstevel@tonic-gate if (seg == seglast) 405*0Sstevel@tonic-gate seglast = NULL; 406*0Sstevel@tonic-gate nsegs++; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate ASSERT(seglast == NULL); 409*0Sstevel@tonic-gate ASSERT(avl_numnodes(&as->a_segtree) == nsegs); 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate #endif /* VERIFY_SEGLIST */ 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate /* 414*0Sstevel@tonic-gate * Add a new segment to the address space. The avl_find() 415*0Sstevel@tonic-gate * may be expensive so we attempt to use last segment accessed 416*0Sstevel@tonic-gate * in as_gap() as an insertion point. 417*0Sstevel@tonic-gate */ 418*0Sstevel@tonic-gate int 419*0Sstevel@tonic-gate as_addseg(struct as *as, struct seg *newseg) 420*0Sstevel@tonic-gate { 421*0Sstevel@tonic-gate struct seg *seg; 422*0Sstevel@tonic-gate caddr_t addr; 423*0Sstevel@tonic-gate caddr_t eaddr; 424*0Sstevel@tonic-gate avl_index_t where; 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 429*0Sstevel@tonic-gate gethrestime(&as->a_updatetime); 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate if (as->a_lastgaphl != NULL) { 432*0Sstevel@tonic-gate struct seg *hseg = NULL; 433*0Sstevel@tonic-gate struct seg *lseg = NULL; 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate if (as->a_lastgaphl->s_base > newseg->s_base) { 436*0Sstevel@tonic-gate hseg = as->a_lastgaphl; 437*0Sstevel@tonic-gate lseg = AVL_PREV(&as->a_segtree, hseg); 438*0Sstevel@tonic-gate } else { 439*0Sstevel@tonic-gate lseg = as->a_lastgaphl; 440*0Sstevel@tonic-gate hseg = AVL_NEXT(&as->a_segtree, lseg); 441*0Sstevel@tonic-gate } 442*0Sstevel@tonic-gate 443*0Sstevel@tonic-gate if (hseg && lseg && lseg->s_base < newseg->s_base && 444*0Sstevel@tonic-gate hseg->s_base > newseg->s_base) { 445*0Sstevel@tonic-gate avl_insert_here(&as->a_segtree, newseg, lseg, 446*0Sstevel@tonic-gate AVL_AFTER); 447*0Sstevel@tonic-gate as->a_lastgaphl = NULL; 448*0Sstevel@tonic-gate as->a_seglast = newseg; 449*0Sstevel@tonic-gate return (0); 450*0Sstevel@tonic-gate } 451*0Sstevel@tonic-gate as->a_lastgaphl = NULL; 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate addr = newseg->s_base; 455*0Sstevel@tonic-gate eaddr = addr + newseg->s_size; 456*0Sstevel@tonic-gate again: 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate seg = avl_find(&as->a_segtree, &addr, &where); 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate if (seg == NULL) 461*0Sstevel@tonic-gate seg = avl_nearest(&as->a_segtree, where, AVL_AFTER); 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate if (seg == NULL) 464*0Sstevel@tonic-gate seg = avl_last(&as->a_segtree); 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gate if (seg != NULL) { 467*0Sstevel@tonic-gate caddr_t base = seg->s_base; 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate /* 470*0Sstevel@tonic-gate * If top of seg is below the requested address, then 471*0Sstevel@tonic-gate * the insertion point is at the end of the linked list, 472*0Sstevel@tonic-gate * and seg points to the tail of the list. Otherwise, 473*0Sstevel@tonic-gate * the insertion point is immediately before seg. 474*0Sstevel@tonic-gate */ 475*0Sstevel@tonic-gate if (base + seg->s_size > addr) { 476*0Sstevel@tonic-gate if (addr >= base || eaddr > base) { 477*0Sstevel@tonic-gate #ifdef __sparc 478*0Sstevel@tonic-gate extern struct seg_ops segnf_ops; 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate /* 481*0Sstevel@tonic-gate * no-fault segs must disappear if overlaid. 482*0Sstevel@tonic-gate * XXX need new segment type so 483*0Sstevel@tonic-gate * we don't have to check s_ops 484*0Sstevel@tonic-gate */ 485*0Sstevel@tonic-gate if (seg->s_ops == &segnf_ops) { 486*0Sstevel@tonic-gate seg_unmap(seg); 487*0Sstevel@tonic-gate goto again; 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate #endif 490*0Sstevel@tonic-gate return (-1); /* overlapping segment */ 491*0Sstevel@tonic-gate } 492*0Sstevel@tonic-gate } 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate as->a_seglast = newseg; 495*0Sstevel@tonic-gate avl_insert(&as->a_segtree, newseg, where); 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate #ifdef VERIFY_SEGLIST 498*0Sstevel@tonic-gate as_verify(as); 499*0Sstevel@tonic-gate #endif 500*0Sstevel@tonic-gate return (0); 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate struct seg * 504*0Sstevel@tonic-gate as_removeseg(struct as *as, struct seg *seg) 505*0Sstevel@tonic-gate { 506*0Sstevel@tonic-gate avl_tree_t *t; 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 509*0Sstevel@tonic-gate 510*0Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 511*0Sstevel@tonic-gate gethrestime(&as->a_updatetime); 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate if (seg == NULL) 514*0Sstevel@tonic-gate return (NULL); 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate t = &as->a_segtree; 517*0Sstevel@tonic-gate if (as->a_seglast == seg) 518*0Sstevel@tonic-gate as->a_seglast = NULL; 519*0Sstevel@tonic-gate as->a_lastgaphl = NULL; 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate /* 522*0Sstevel@tonic-gate * if this segment is at an address higher than 523*0Sstevel@tonic-gate * a_lastgap, set a_lastgap to the next segment (NULL if last segment) 524*0Sstevel@tonic-gate */ 525*0Sstevel@tonic-gate if (as->a_lastgap && 526*0Sstevel@tonic-gate (seg == as->a_lastgap || seg->s_base > as->a_lastgap->s_base)) 527*0Sstevel@tonic-gate as->a_lastgap = AVL_NEXT(t, seg); 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate /* 530*0Sstevel@tonic-gate * remove the segment from the seg tree 531*0Sstevel@tonic-gate */ 532*0Sstevel@tonic-gate avl_remove(t, seg); 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate #ifdef VERIFY_SEGLIST 535*0Sstevel@tonic-gate as_verify(as); 536*0Sstevel@tonic-gate #endif 537*0Sstevel@tonic-gate return (seg); 538*0Sstevel@tonic-gate } 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate /* 541*0Sstevel@tonic-gate * Find a segment containing addr. 542*0Sstevel@tonic-gate */ 543*0Sstevel@tonic-gate struct seg * 544*0Sstevel@tonic-gate as_segat(struct as *as, caddr_t addr) 545*0Sstevel@tonic-gate { 546*0Sstevel@tonic-gate struct seg *seg = as->a_seglast; 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate if (seg != NULL && seg->s_base <= addr && 551*0Sstevel@tonic-gate addr < seg->s_base + seg->s_size) 552*0Sstevel@tonic-gate return (seg); 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate seg = avl_find(&as->a_segtree, &addr, NULL); 555*0Sstevel@tonic-gate return (seg); 556*0Sstevel@tonic-gate } 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate /* 559*0Sstevel@tonic-gate * Serialize all searches for holes in an address space to 560*0Sstevel@tonic-gate * prevent two or more threads from allocating the same virtual 561*0Sstevel@tonic-gate * address range. The address space must not be "read/write" 562*0Sstevel@tonic-gate * locked by the caller since we may block. 563*0Sstevel@tonic-gate */ 564*0Sstevel@tonic-gate void 565*0Sstevel@tonic-gate as_rangelock(struct as *as) 566*0Sstevel@tonic-gate { 567*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 568*0Sstevel@tonic-gate while (AS_ISCLAIMGAP(as)) 569*0Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 570*0Sstevel@tonic-gate AS_SETCLAIMGAP(as); 571*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 572*0Sstevel@tonic-gate } 573*0Sstevel@tonic-gate 574*0Sstevel@tonic-gate /* 575*0Sstevel@tonic-gate * Release hold on a_state & AS_CLAIMGAP and signal any other blocked threads. 576*0Sstevel@tonic-gate */ 577*0Sstevel@tonic-gate void 578*0Sstevel@tonic-gate as_rangeunlock(struct as *as) 579*0Sstevel@tonic-gate { 580*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 581*0Sstevel@tonic-gate AS_CLRCLAIMGAP(as); 582*0Sstevel@tonic-gate cv_signal(&as->a_cv); 583*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gate /* 587*0Sstevel@tonic-gate * compar segments (or just an address) by segment address range 588*0Sstevel@tonic-gate */ 589*0Sstevel@tonic-gate static int 590*0Sstevel@tonic-gate as_segcompar(const void *x, const void *y) 591*0Sstevel@tonic-gate { 592*0Sstevel@tonic-gate struct seg *a = (struct seg *)x; 593*0Sstevel@tonic-gate struct seg *b = (struct seg *)y; 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gate if (a->s_base < b->s_base) 596*0Sstevel@tonic-gate return (-1); 597*0Sstevel@tonic-gate if (a->s_base >= b->s_base + b->s_size) 598*0Sstevel@tonic-gate return (1); 599*0Sstevel@tonic-gate return (0); 600*0Sstevel@tonic-gate } 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gate void 604*0Sstevel@tonic-gate as_avlinit(struct as *as) 605*0Sstevel@tonic-gate { 606*0Sstevel@tonic-gate avl_create(&as->a_segtree, as_segcompar, sizeof (struct seg), 607*0Sstevel@tonic-gate offsetof(struct seg, s_tree)); 608*0Sstevel@tonic-gate avl_create(&as->a_wpage, wp_compare, sizeof (struct watched_page), 609*0Sstevel@tonic-gate offsetof(struct watched_page, wp_link)); 610*0Sstevel@tonic-gate } 611*0Sstevel@tonic-gate 612*0Sstevel@tonic-gate /*ARGSUSED*/ 613*0Sstevel@tonic-gate static int 614*0Sstevel@tonic-gate as_constructor(void *buf, void *cdrarg, int kmflags) 615*0Sstevel@tonic-gate { 616*0Sstevel@tonic-gate struct as *as = buf; 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate mutex_init(&as->a_contents, NULL, MUTEX_DEFAULT, NULL); 619*0Sstevel@tonic-gate cv_init(&as->a_cv, NULL, CV_DEFAULT, NULL); 620*0Sstevel@tonic-gate rw_init(&as->a_lock, NULL, RW_DEFAULT, NULL); 621*0Sstevel@tonic-gate as_avlinit(as); 622*0Sstevel@tonic-gate return (0); 623*0Sstevel@tonic-gate } 624*0Sstevel@tonic-gate 625*0Sstevel@tonic-gate /*ARGSUSED1*/ 626*0Sstevel@tonic-gate static void 627*0Sstevel@tonic-gate as_destructor(void *buf, void *cdrarg) 628*0Sstevel@tonic-gate { 629*0Sstevel@tonic-gate struct as *as = buf; 630*0Sstevel@tonic-gate 631*0Sstevel@tonic-gate avl_destroy(&as->a_segtree); 632*0Sstevel@tonic-gate mutex_destroy(&as->a_contents); 633*0Sstevel@tonic-gate cv_destroy(&as->a_cv); 634*0Sstevel@tonic-gate rw_destroy(&as->a_lock); 635*0Sstevel@tonic-gate } 636*0Sstevel@tonic-gate 637*0Sstevel@tonic-gate void 638*0Sstevel@tonic-gate as_init(void) 639*0Sstevel@tonic-gate { 640*0Sstevel@tonic-gate as_cache = kmem_cache_create("as_cache", sizeof (struct as), 0, 641*0Sstevel@tonic-gate as_constructor, as_destructor, NULL, NULL, NULL, 0); 642*0Sstevel@tonic-gate } 643*0Sstevel@tonic-gate 644*0Sstevel@tonic-gate /* 645*0Sstevel@tonic-gate * Allocate and initialize an address space data structure. 646*0Sstevel@tonic-gate * We call hat_alloc to allow any machine dependent 647*0Sstevel@tonic-gate * information in the hat structure to be initialized. 648*0Sstevel@tonic-gate */ 649*0Sstevel@tonic-gate struct as * 650*0Sstevel@tonic-gate as_alloc(void) 651*0Sstevel@tonic-gate { 652*0Sstevel@tonic-gate struct as *as; 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gate as = kmem_cache_alloc(as_cache, KM_SLEEP); 655*0Sstevel@tonic-gate 656*0Sstevel@tonic-gate as->a_flags = 0; 657*0Sstevel@tonic-gate as->a_vbits = 0; 658*0Sstevel@tonic-gate as->a_hrm = NULL; 659*0Sstevel@tonic-gate as->a_seglast = NULL; 660*0Sstevel@tonic-gate as->a_size = 0; 661*0Sstevel@tonic-gate as->a_updatedir = 0; 662*0Sstevel@tonic-gate gethrestime(&as->a_updatetime); 663*0Sstevel@tonic-gate as->a_objectdir = NULL; 664*0Sstevel@tonic-gate as->a_sizedir = 0; 665*0Sstevel@tonic-gate as->a_userlimit = (caddr_t)USERLIMIT; 666*0Sstevel@tonic-gate as->a_lastgap = NULL; 667*0Sstevel@tonic-gate as->a_lastgaphl = NULL; 668*0Sstevel@tonic-gate as->a_callbacks = NULL; 669*0Sstevel@tonic-gate 670*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 671*0Sstevel@tonic-gate as->a_hat = hat_alloc(as); /* create hat for default system mmu */ 672*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 673*0Sstevel@tonic-gate 674*0Sstevel@tonic-gate as->a_xhat = NULL; 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate return (as); 677*0Sstevel@tonic-gate } 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate /* 680*0Sstevel@tonic-gate * Free an address space data structure. 681*0Sstevel@tonic-gate * Need to free the hat first and then 682*0Sstevel@tonic-gate * all the segments on this as and finally 683*0Sstevel@tonic-gate * the space for the as struct itself. 684*0Sstevel@tonic-gate */ 685*0Sstevel@tonic-gate void 686*0Sstevel@tonic-gate as_free(struct as *as) 687*0Sstevel@tonic-gate { 688*0Sstevel@tonic-gate struct hat *hat = as->a_hat; 689*0Sstevel@tonic-gate struct seg *seg, *next; 690*0Sstevel@tonic-gate int called = 0; 691*0Sstevel@tonic-gate 692*0Sstevel@tonic-gate top: 693*0Sstevel@tonic-gate /* 694*0Sstevel@tonic-gate * Invoke ALL callbacks. as_do_callbacks will do one callback 695*0Sstevel@tonic-gate * per call, and not return (-1) until the callback has completed. 696*0Sstevel@tonic-gate * When as_do_callbacks returns zero, all callbacks have completed. 697*0Sstevel@tonic-gate */ 698*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 699*0Sstevel@tonic-gate while (as->a_callbacks && as_do_callbacks(as, AS_ALL_EVENT, 0, 0)); 700*0Sstevel@tonic-gate 701*0Sstevel@tonic-gate /* This will prevent new XHATs from attaching to as */ 702*0Sstevel@tonic-gate if (!called) 703*0Sstevel@tonic-gate AS_SETBUSY(as); 704*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 705*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate if (!called) { 708*0Sstevel@tonic-gate called = 1; 709*0Sstevel@tonic-gate hat_free_start(hat); 710*0Sstevel@tonic-gate if (as->a_xhat != NULL) 711*0Sstevel@tonic-gate xhat_free_start_all(as); 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = next) { 714*0Sstevel@tonic-gate int err; 715*0Sstevel@tonic-gate 716*0Sstevel@tonic-gate next = AS_SEGNEXT(as, seg); 717*0Sstevel@tonic-gate err = SEGOP_UNMAP(seg, seg->s_base, seg->s_size); 718*0Sstevel@tonic-gate if (err == EAGAIN) { 719*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 720*0Sstevel@tonic-gate if (as->a_callbacks) { 721*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 722*0Sstevel@tonic-gate } else { 723*0Sstevel@tonic-gate /* 724*0Sstevel@tonic-gate * Memory is currently locked. Wait for a 725*0Sstevel@tonic-gate * cv_signal that it has been unlocked, then 726*0Sstevel@tonic-gate * try the operation again. 727*0Sstevel@tonic-gate */ 728*0Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) 729*0Sstevel@tonic-gate cv_broadcast(&as->a_cv); 730*0Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 731*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 732*0Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) 733*0Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 734*0Sstevel@tonic-gate } 735*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 736*0Sstevel@tonic-gate goto top; 737*0Sstevel@tonic-gate } else { 738*0Sstevel@tonic-gate /* 739*0Sstevel@tonic-gate * We do not expect any other error return at this 740*0Sstevel@tonic-gate * time. This is similar to an ASSERT in seg_unmap() 741*0Sstevel@tonic-gate */ 742*0Sstevel@tonic-gate ASSERT(err == 0); 743*0Sstevel@tonic-gate } 744*0Sstevel@tonic-gate } 745*0Sstevel@tonic-gate hat_free_end(hat); 746*0Sstevel@tonic-gate if (as->a_xhat != NULL) 747*0Sstevel@tonic-gate xhat_free_end_all(as); 748*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 749*0Sstevel@tonic-gate 750*0Sstevel@tonic-gate /* /proc stuff */ 751*0Sstevel@tonic-gate ASSERT(avl_numnodes(&as->a_wpage) == 0); 752*0Sstevel@tonic-gate if (as->a_objectdir) { 753*0Sstevel@tonic-gate kmem_free(as->a_objectdir, as->a_sizedir * sizeof (vnode_t *)); 754*0Sstevel@tonic-gate as->a_objectdir = NULL; 755*0Sstevel@tonic-gate as->a_sizedir = 0; 756*0Sstevel@tonic-gate } 757*0Sstevel@tonic-gate 758*0Sstevel@tonic-gate /* 759*0Sstevel@tonic-gate * Free the struct as back to kmem. Assert it has no segments. 760*0Sstevel@tonic-gate */ 761*0Sstevel@tonic-gate ASSERT(avl_numnodes(&as->a_segtree) == 0); 762*0Sstevel@tonic-gate kmem_cache_free(as_cache, as); 763*0Sstevel@tonic-gate } 764*0Sstevel@tonic-gate 765*0Sstevel@tonic-gate int 766*0Sstevel@tonic-gate as_dup(struct as *as, struct as **outas) 767*0Sstevel@tonic-gate { 768*0Sstevel@tonic-gate struct as *newas; 769*0Sstevel@tonic-gate struct seg *seg, *newseg; 770*0Sstevel@tonic-gate int error; 771*0Sstevel@tonic-gate 772*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 773*0Sstevel@tonic-gate as_clearwatch(as); 774*0Sstevel@tonic-gate newas = as_alloc(); 775*0Sstevel@tonic-gate newas->a_userlimit = as->a_userlimit; 776*0Sstevel@tonic-gate AS_LOCK_ENTER(newas, &newas->a_lock, RW_WRITER); 777*0Sstevel@tonic-gate 778*0Sstevel@tonic-gate /* This will prevent new XHATs from attaching */ 779*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 780*0Sstevel@tonic-gate AS_SETBUSY(as); 781*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 782*0Sstevel@tonic-gate mutex_enter(&newas->a_contents); 783*0Sstevel@tonic-gate AS_SETBUSY(newas); 784*0Sstevel@tonic-gate mutex_exit(&newas->a_contents); 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate 787*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 788*0Sstevel@tonic-gate 789*0Sstevel@tonic-gate if (seg->s_flags & S_PURGE) 790*0Sstevel@tonic-gate continue; 791*0Sstevel@tonic-gate 792*0Sstevel@tonic-gate newseg = seg_alloc(newas, seg->s_base, seg->s_size); 793*0Sstevel@tonic-gate if (newseg == NULL) { 794*0Sstevel@tonic-gate AS_LOCK_EXIT(newas, &newas->a_lock); 795*0Sstevel@tonic-gate as_setwatch(as); 796*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 797*0Sstevel@tonic-gate AS_CLRBUSY(as); 798*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 799*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 800*0Sstevel@tonic-gate as_free(newas); 801*0Sstevel@tonic-gate return (-1); 802*0Sstevel@tonic-gate } 803*0Sstevel@tonic-gate if ((error = SEGOP_DUP(seg, newseg)) != 0) { 804*0Sstevel@tonic-gate /* 805*0Sstevel@tonic-gate * We call seg_free() on the new seg 806*0Sstevel@tonic-gate * because the segment is not set up 807*0Sstevel@tonic-gate * completely; i.e. it has no ops. 808*0Sstevel@tonic-gate */ 809*0Sstevel@tonic-gate as_setwatch(as); 810*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 811*0Sstevel@tonic-gate AS_CLRBUSY(as); 812*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 813*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 814*0Sstevel@tonic-gate seg_free(newseg); 815*0Sstevel@tonic-gate AS_LOCK_EXIT(newas, &newas->a_lock); 816*0Sstevel@tonic-gate as_free(newas); 817*0Sstevel@tonic-gate return (error); 818*0Sstevel@tonic-gate } 819*0Sstevel@tonic-gate newas->a_size += seg->s_size; 820*0Sstevel@tonic-gate } 821*0Sstevel@tonic-gate 822*0Sstevel@tonic-gate error = hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_ALL); 823*0Sstevel@tonic-gate if (as->a_xhat != NULL) 824*0Sstevel@tonic-gate error |= xhat_dup_all(as, newas, NULL, 0, HAT_DUP_ALL); 825*0Sstevel@tonic-gate 826*0Sstevel@tonic-gate mutex_enter(&newas->a_contents); 827*0Sstevel@tonic-gate AS_CLRBUSY(newas); 828*0Sstevel@tonic-gate mutex_exit(&newas->a_contents); 829*0Sstevel@tonic-gate AS_LOCK_EXIT(newas, &newas->a_lock); 830*0Sstevel@tonic-gate 831*0Sstevel@tonic-gate as_setwatch(as); 832*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 833*0Sstevel@tonic-gate AS_CLRBUSY(as); 834*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 835*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 836*0Sstevel@tonic-gate if (error != 0) { 837*0Sstevel@tonic-gate as_free(newas); 838*0Sstevel@tonic-gate return (error); 839*0Sstevel@tonic-gate } 840*0Sstevel@tonic-gate *outas = newas; 841*0Sstevel@tonic-gate return (0); 842*0Sstevel@tonic-gate } 843*0Sstevel@tonic-gate 844*0Sstevel@tonic-gate /* 845*0Sstevel@tonic-gate * Handle a ``fault'' at addr for size bytes. 846*0Sstevel@tonic-gate */ 847*0Sstevel@tonic-gate faultcode_t 848*0Sstevel@tonic-gate as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size, 849*0Sstevel@tonic-gate enum fault_type type, enum seg_rw rw) 850*0Sstevel@tonic-gate { 851*0Sstevel@tonic-gate struct seg *seg; 852*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 853*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 854*0Sstevel@tonic-gate size_t ssize; 855*0Sstevel@tonic-gate faultcode_t res = 0; 856*0Sstevel@tonic-gate caddr_t addrsav; 857*0Sstevel@tonic-gate struct seg *segsav; 858*0Sstevel@tonic-gate int as_lock_held; 859*0Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 860*0Sstevel@tonic-gate int is_xhat = 0; 861*0Sstevel@tonic-gate int holding_wpage = 0; 862*0Sstevel@tonic-gate extern struct seg_ops segdev_ops; 863*0Sstevel@tonic-gate 864*0Sstevel@tonic-gate 865*0Sstevel@tonic-gate 866*0Sstevel@tonic-gate if (as->a_hat != hat) { 867*0Sstevel@tonic-gate /* This must be an XHAT then */ 868*0Sstevel@tonic-gate is_xhat = 1; 869*0Sstevel@tonic-gate 870*0Sstevel@tonic-gate if ((type != F_INVAL) || (as == &kas)) 871*0Sstevel@tonic-gate return (FC_NOSUPPORT); 872*0Sstevel@tonic-gate } 873*0Sstevel@tonic-gate 874*0Sstevel@tonic-gate retry: 875*0Sstevel@tonic-gate if (!is_xhat) { 876*0Sstevel@tonic-gate /* 877*0Sstevel@tonic-gate * Indicate that the lwp is not to be stopped while waiting 878*0Sstevel@tonic-gate * for a pagefault. This is to avoid deadlock while debugging 879*0Sstevel@tonic-gate * a process via /proc over NFS (in particular). 880*0Sstevel@tonic-gate */ 881*0Sstevel@tonic-gate if (lwp != NULL) 882*0Sstevel@tonic-gate lwp->lwp_nostop++; 883*0Sstevel@tonic-gate 884*0Sstevel@tonic-gate /* 885*0Sstevel@tonic-gate * same length must be used when we softlock and softunlock. 886*0Sstevel@tonic-gate * We don't support softunlocking lengths less than 887*0Sstevel@tonic-gate * the original length when there is largepage support. 888*0Sstevel@tonic-gate * See seg_dev.c for more comments. 889*0Sstevel@tonic-gate */ 890*0Sstevel@tonic-gate switch (type) { 891*0Sstevel@tonic-gate 892*0Sstevel@tonic-gate case F_SOFTLOCK: 893*0Sstevel@tonic-gate CPU_STATS_ADD_K(vm, softlock, 1); 894*0Sstevel@tonic-gate break; 895*0Sstevel@tonic-gate 896*0Sstevel@tonic-gate case F_SOFTUNLOCK: 897*0Sstevel@tonic-gate break; 898*0Sstevel@tonic-gate 899*0Sstevel@tonic-gate case F_PROT: 900*0Sstevel@tonic-gate CPU_STATS_ADD_K(vm, prot_fault, 1); 901*0Sstevel@tonic-gate break; 902*0Sstevel@tonic-gate 903*0Sstevel@tonic-gate case F_INVAL: 904*0Sstevel@tonic-gate CPU_STATS_ENTER_K(); 905*0Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, vm, as_fault, 1); 906*0Sstevel@tonic-gate if (as == &kas) 907*0Sstevel@tonic-gate CPU_STATS_ADDQ(CPU, vm, kernel_asflt, 1); 908*0Sstevel@tonic-gate CPU_STATS_EXIT_K(); 909*0Sstevel@tonic-gate break; 910*0Sstevel@tonic-gate } 911*0Sstevel@tonic-gate } 912*0Sstevel@tonic-gate 913*0Sstevel@tonic-gate /* Kernel probe */ 914*0Sstevel@tonic-gate TNF_PROBE_3(address_fault, "vm pagefault", /* CSTYLED */, 915*0Sstevel@tonic-gate tnf_opaque, address, addr, 916*0Sstevel@tonic-gate tnf_fault_type, fault_type, type, 917*0Sstevel@tonic-gate tnf_seg_access, access, rw); 918*0Sstevel@tonic-gate 919*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 920*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 921*0Sstevel@tonic-gate (size_t)raddr; 922*0Sstevel@tonic-gate 923*0Sstevel@tonic-gate /* 924*0Sstevel@tonic-gate * XXX -- Don't grab the as lock for segkmap. We should grab it for 925*0Sstevel@tonic-gate * correctness, but then we could be stuck holding this lock for 926*0Sstevel@tonic-gate * a LONG time if the fault needs to be resolved on a slow 927*0Sstevel@tonic-gate * filesystem, and then no-one will be able to exec new commands, 928*0Sstevel@tonic-gate * as exec'ing requires the write lock on the as. 929*0Sstevel@tonic-gate */ 930*0Sstevel@tonic-gate if (as == &kas && segkmap && segkmap->s_base <= raddr && 931*0Sstevel@tonic-gate raddr + size < segkmap->s_base + segkmap->s_size) { 932*0Sstevel@tonic-gate /* 933*0Sstevel@tonic-gate * if (as==&kas), this can't be XHAT: we've already returned 934*0Sstevel@tonic-gate * FC_NOSUPPORT. 935*0Sstevel@tonic-gate */ 936*0Sstevel@tonic-gate seg = segkmap; 937*0Sstevel@tonic-gate as_lock_held = 0; 938*0Sstevel@tonic-gate } else { 939*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 940*0Sstevel@tonic-gate if (is_xhat && avl_numnodes(&as->a_wpage) != 0) { 941*0Sstevel@tonic-gate /* 942*0Sstevel@tonic-gate * Grab and hold the writers' lock on the as 943*0Sstevel@tonic-gate * if the fault is to a watched page. 944*0Sstevel@tonic-gate * This will keep CPUs from "peeking" at the 945*0Sstevel@tonic-gate * address range while we're temporarily boosting 946*0Sstevel@tonic-gate * the permissions for the XHAT device to 947*0Sstevel@tonic-gate * resolve the fault in the segment layer. 948*0Sstevel@tonic-gate * 949*0Sstevel@tonic-gate * We could check whether faulted address 950*0Sstevel@tonic-gate * is within a watched page and only then grab 951*0Sstevel@tonic-gate * the writer lock, but this is simpler. 952*0Sstevel@tonic-gate */ 953*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 954*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 955*0Sstevel@tonic-gate } 956*0Sstevel@tonic-gate 957*0Sstevel@tonic-gate seg = as_segat(as, raddr); 958*0Sstevel@tonic-gate if (seg == NULL) { 959*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 960*0Sstevel@tonic-gate if ((lwp != NULL) && (!is_xhat)) 961*0Sstevel@tonic-gate lwp->lwp_nostop--; 962*0Sstevel@tonic-gate return (FC_NOMAP); 963*0Sstevel@tonic-gate } 964*0Sstevel@tonic-gate 965*0Sstevel@tonic-gate as_lock_held = 1; 966*0Sstevel@tonic-gate } 967*0Sstevel@tonic-gate 968*0Sstevel@tonic-gate addrsav = raddr; 969*0Sstevel@tonic-gate segsav = seg; 970*0Sstevel@tonic-gate 971*0Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 972*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 973*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 974*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 975*0Sstevel@tonic-gate res = FC_NOMAP; 976*0Sstevel@tonic-gate break; 977*0Sstevel@tonic-gate } 978*0Sstevel@tonic-gate } 979*0Sstevel@tonic-gate if (raddr + rsize > seg->s_base + seg->s_size) 980*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 981*0Sstevel@tonic-gate else 982*0Sstevel@tonic-gate ssize = rsize; 983*0Sstevel@tonic-gate 984*0Sstevel@tonic-gate if (!is_xhat || (seg->s_ops != &segdev_ops)) { 985*0Sstevel@tonic-gate 986*0Sstevel@tonic-gate if (is_xhat && avl_numnodes(&as->a_wpage) != 0 && 987*0Sstevel@tonic-gate pr_is_watchpage_as(raddr, rw, as)) { 988*0Sstevel@tonic-gate /* 989*0Sstevel@tonic-gate * Handle watch pages. If we're faulting on a 990*0Sstevel@tonic-gate * watched page from an X-hat, we have to 991*0Sstevel@tonic-gate * restore the original permissions while we 992*0Sstevel@tonic-gate * handle the fault. 993*0Sstevel@tonic-gate */ 994*0Sstevel@tonic-gate as_clearwatch(as); 995*0Sstevel@tonic-gate holding_wpage = 1; 996*0Sstevel@tonic-gate } 997*0Sstevel@tonic-gate 998*0Sstevel@tonic-gate res = SEGOP_FAULT(hat, seg, raddr, ssize, type, rw); 999*0Sstevel@tonic-gate 1000*0Sstevel@tonic-gate /* Restore watchpoints */ 1001*0Sstevel@tonic-gate if (holding_wpage) { 1002*0Sstevel@tonic-gate as_setwatch(as); 1003*0Sstevel@tonic-gate holding_wpage = 0; 1004*0Sstevel@tonic-gate } 1005*0Sstevel@tonic-gate 1006*0Sstevel@tonic-gate if (res != 0) 1007*0Sstevel@tonic-gate break; 1008*0Sstevel@tonic-gate } else { 1009*0Sstevel@tonic-gate /* XHAT does not support seg_dev */ 1010*0Sstevel@tonic-gate res = FC_NOSUPPORT; 1011*0Sstevel@tonic-gate break; 1012*0Sstevel@tonic-gate } 1013*0Sstevel@tonic-gate } 1014*0Sstevel@tonic-gate 1015*0Sstevel@tonic-gate /* 1016*0Sstevel@tonic-gate * If we were SOFTLOCKing and encountered a failure, 1017*0Sstevel@tonic-gate * we must SOFTUNLOCK the range we already did. (Maybe we 1018*0Sstevel@tonic-gate * should just panic if we are SOFTLOCKing or even SOFTUNLOCKing 1019*0Sstevel@tonic-gate * right here...) 1020*0Sstevel@tonic-gate */ 1021*0Sstevel@tonic-gate if (res != 0 && type == F_SOFTLOCK) { 1022*0Sstevel@tonic-gate for (seg = segsav; addrsav < raddr; addrsav += ssize) { 1023*0Sstevel@tonic-gate if (addrsav >= seg->s_base + seg->s_size) 1024*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 1025*0Sstevel@tonic-gate ASSERT(seg != NULL); 1026*0Sstevel@tonic-gate /* 1027*0Sstevel@tonic-gate * Now call the fault routine again to perform the 1028*0Sstevel@tonic-gate * unlock using S_OTHER instead of the rw variable 1029*0Sstevel@tonic-gate * since we never got a chance to touch the pages. 1030*0Sstevel@tonic-gate */ 1031*0Sstevel@tonic-gate if (raddr > seg->s_base + seg->s_size) 1032*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - addrsav; 1033*0Sstevel@tonic-gate else 1034*0Sstevel@tonic-gate ssize = raddr - addrsav; 1035*0Sstevel@tonic-gate (void) SEGOP_FAULT(hat, seg, addrsav, ssize, 1036*0Sstevel@tonic-gate F_SOFTUNLOCK, S_OTHER); 1037*0Sstevel@tonic-gate } 1038*0Sstevel@tonic-gate } 1039*0Sstevel@tonic-gate if (as_lock_held) 1040*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1041*0Sstevel@tonic-gate if ((lwp != NULL) && (!is_xhat)) 1042*0Sstevel@tonic-gate lwp->lwp_nostop--; 1043*0Sstevel@tonic-gate /* 1044*0Sstevel@tonic-gate * If the lower levels returned EDEADLK for a fault, 1045*0Sstevel@tonic-gate * It means that we should retry the fault. Let's wait 1046*0Sstevel@tonic-gate * a bit also to let the deadlock causing condition clear. 1047*0Sstevel@tonic-gate * This is part of a gross hack to work around a design flaw 1048*0Sstevel@tonic-gate * in the ufs/sds logging code and should go away when the 1049*0Sstevel@tonic-gate * logging code is re-designed to fix the problem. See bug 1050*0Sstevel@tonic-gate * 4125102 for details of the problem. 1051*0Sstevel@tonic-gate */ 1052*0Sstevel@tonic-gate if (FC_ERRNO(res) == EDEADLK) { 1053*0Sstevel@tonic-gate delay(deadlk_wait); 1054*0Sstevel@tonic-gate res = 0; 1055*0Sstevel@tonic-gate goto retry; 1056*0Sstevel@tonic-gate } 1057*0Sstevel@tonic-gate return (res); 1058*0Sstevel@tonic-gate } 1059*0Sstevel@tonic-gate 1060*0Sstevel@tonic-gate 1061*0Sstevel@tonic-gate 1062*0Sstevel@tonic-gate /* 1063*0Sstevel@tonic-gate * Asynchronous ``fault'' at addr for size bytes. 1064*0Sstevel@tonic-gate */ 1065*0Sstevel@tonic-gate faultcode_t 1066*0Sstevel@tonic-gate as_faulta(struct as *as, caddr_t addr, size_t size) 1067*0Sstevel@tonic-gate { 1068*0Sstevel@tonic-gate struct seg *seg; 1069*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 1070*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 1071*0Sstevel@tonic-gate faultcode_t res = 0; 1072*0Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 1073*0Sstevel@tonic-gate 1074*0Sstevel@tonic-gate retry: 1075*0Sstevel@tonic-gate /* 1076*0Sstevel@tonic-gate * Indicate that the lwp is not to be stopped while waiting 1077*0Sstevel@tonic-gate * for a pagefault. This is to avoid deadlock while debugging 1078*0Sstevel@tonic-gate * a process via /proc over NFS (in particular). 1079*0Sstevel@tonic-gate */ 1080*0Sstevel@tonic-gate if (lwp != NULL) 1081*0Sstevel@tonic-gate lwp->lwp_nostop++; 1082*0Sstevel@tonic-gate 1083*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1084*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 1085*0Sstevel@tonic-gate (size_t)raddr; 1086*0Sstevel@tonic-gate 1087*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1088*0Sstevel@tonic-gate seg = as_segat(as, raddr); 1089*0Sstevel@tonic-gate if (seg == NULL) { 1090*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1091*0Sstevel@tonic-gate if (lwp != NULL) 1092*0Sstevel@tonic-gate lwp->lwp_nostop--; 1093*0Sstevel@tonic-gate return (FC_NOMAP); 1094*0Sstevel@tonic-gate } 1095*0Sstevel@tonic-gate 1096*0Sstevel@tonic-gate for (; rsize != 0; rsize -= PAGESIZE, raddr += PAGESIZE) { 1097*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 1098*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 1099*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 1100*0Sstevel@tonic-gate res = FC_NOMAP; 1101*0Sstevel@tonic-gate break; 1102*0Sstevel@tonic-gate } 1103*0Sstevel@tonic-gate } 1104*0Sstevel@tonic-gate res = SEGOP_FAULTA(seg, raddr); 1105*0Sstevel@tonic-gate if (res != 0) 1106*0Sstevel@tonic-gate break; 1107*0Sstevel@tonic-gate } 1108*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1109*0Sstevel@tonic-gate if (lwp != NULL) 1110*0Sstevel@tonic-gate lwp->lwp_nostop--; 1111*0Sstevel@tonic-gate /* 1112*0Sstevel@tonic-gate * If the lower levels returned EDEADLK for a fault, 1113*0Sstevel@tonic-gate * It means that we should retry the fault. Let's wait 1114*0Sstevel@tonic-gate * a bit also to let the deadlock causing condition clear. 1115*0Sstevel@tonic-gate * This is part of a gross hack to work around a design flaw 1116*0Sstevel@tonic-gate * in the ufs/sds logging code and should go away when the 1117*0Sstevel@tonic-gate * logging code is re-designed to fix the problem. See bug 1118*0Sstevel@tonic-gate * 4125102 for details of the problem. 1119*0Sstevel@tonic-gate */ 1120*0Sstevel@tonic-gate if (FC_ERRNO(res) == EDEADLK) { 1121*0Sstevel@tonic-gate delay(deadlk_wait); 1122*0Sstevel@tonic-gate res = 0; 1123*0Sstevel@tonic-gate goto retry; 1124*0Sstevel@tonic-gate } 1125*0Sstevel@tonic-gate return (res); 1126*0Sstevel@tonic-gate } 1127*0Sstevel@tonic-gate 1128*0Sstevel@tonic-gate /* 1129*0Sstevel@tonic-gate * Set the virtual mapping for the interval from [addr : addr + size) 1130*0Sstevel@tonic-gate * in address space `as' to have the specified protection. 1131*0Sstevel@tonic-gate * It is ok for the range to cross over several segments, 1132*0Sstevel@tonic-gate * as long as they are contiguous. 1133*0Sstevel@tonic-gate */ 1134*0Sstevel@tonic-gate int 1135*0Sstevel@tonic-gate as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot) 1136*0Sstevel@tonic-gate { 1137*0Sstevel@tonic-gate struct seg *seg; 1138*0Sstevel@tonic-gate struct as_callback *cb; 1139*0Sstevel@tonic-gate size_t ssize; 1140*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 1141*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 1142*0Sstevel@tonic-gate int error = 0, writer = 0; 1143*0Sstevel@tonic-gate caddr_t saveraddr; 1144*0Sstevel@tonic-gate size_t saversize; 1145*0Sstevel@tonic-gate 1146*0Sstevel@tonic-gate setprot_top: 1147*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1148*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 1149*0Sstevel@tonic-gate (size_t)raddr; 1150*0Sstevel@tonic-gate 1151*0Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 1152*0Sstevel@tonic-gate return (ENOMEM); 1153*0Sstevel@tonic-gate 1154*0Sstevel@tonic-gate saveraddr = raddr; 1155*0Sstevel@tonic-gate saversize = rsize; 1156*0Sstevel@tonic-gate 1157*0Sstevel@tonic-gate /* 1158*0Sstevel@tonic-gate * Normally we only lock the as as a reader. But 1159*0Sstevel@tonic-gate * if due to setprot the segment driver needs to split 1160*0Sstevel@tonic-gate * a segment it will return IE_RETRY. Therefore we re-aquire 1161*0Sstevel@tonic-gate * the as lock as a writer so the segment driver can change 1162*0Sstevel@tonic-gate * the seg list. Also the segment driver will return IE_RETRY 1163*0Sstevel@tonic-gate * after it has changed the segment list so we therefore keep 1164*0Sstevel@tonic-gate * locking as a writer. Since these opeartions should be rare 1165*0Sstevel@tonic-gate * want to only lock as a writer when necessary. 1166*0Sstevel@tonic-gate */ 1167*0Sstevel@tonic-gate if (writer || avl_numnodes(&as->a_wpage) != 0) { 1168*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1169*0Sstevel@tonic-gate } else { 1170*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1171*0Sstevel@tonic-gate } 1172*0Sstevel@tonic-gate 1173*0Sstevel@tonic-gate as_clearwatchprot(as, raddr, rsize); 1174*0Sstevel@tonic-gate seg = as_segat(as, raddr); 1175*0Sstevel@tonic-gate if (seg == NULL) { 1176*0Sstevel@tonic-gate as_setwatch(as); 1177*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1178*0Sstevel@tonic-gate return (ENOMEM); 1179*0Sstevel@tonic-gate } 1180*0Sstevel@tonic-gate 1181*0Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 1182*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 1183*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 1184*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 1185*0Sstevel@tonic-gate error = ENOMEM; 1186*0Sstevel@tonic-gate break; 1187*0Sstevel@tonic-gate } 1188*0Sstevel@tonic-gate } 1189*0Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 1190*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 1191*0Sstevel@tonic-gate else 1192*0Sstevel@tonic-gate ssize = rsize; 1193*0Sstevel@tonic-gate error = SEGOP_SETPROT(seg, raddr, ssize, prot); 1194*0Sstevel@tonic-gate 1195*0Sstevel@tonic-gate if (error == IE_NOMEM) { 1196*0Sstevel@tonic-gate error = EAGAIN; 1197*0Sstevel@tonic-gate break; 1198*0Sstevel@tonic-gate } 1199*0Sstevel@tonic-gate 1200*0Sstevel@tonic-gate if (error == IE_RETRY) { 1201*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1202*0Sstevel@tonic-gate writer = 1; 1203*0Sstevel@tonic-gate goto setprot_top; 1204*0Sstevel@tonic-gate } 1205*0Sstevel@tonic-gate 1206*0Sstevel@tonic-gate if (error == EAGAIN) { 1207*0Sstevel@tonic-gate /* 1208*0Sstevel@tonic-gate * Make sure we have a_lock as writer. 1209*0Sstevel@tonic-gate */ 1210*0Sstevel@tonic-gate if (writer == 0) { 1211*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1212*0Sstevel@tonic-gate writer = 1; 1213*0Sstevel@tonic-gate goto setprot_top; 1214*0Sstevel@tonic-gate } 1215*0Sstevel@tonic-gate 1216*0Sstevel@tonic-gate /* 1217*0Sstevel@tonic-gate * Memory is currently locked. It must be unlocked 1218*0Sstevel@tonic-gate * before this operation can succeed through a retry. 1219*0Sstevel@tonic-gate * The possible reasons for locked memory and 1220*0Sstevel@tonic-gate * corresponding strategies for unlocking are: 1221*0Sstevel@tonic-gate * (1) Normal I/O 1222*0Sstevel@tonic-gate * wait for a signal that the I/O operation 1223*0Sstevel@tonic-gate * has completed and the memory is unlocked. 1224*0Sstevel@tonic-gate * (2) Asynchronous I/O 1225*0Sstevel@tonic-gate * The aio subsystem does not unlock pages when 1226*0Sstevel@tonic-gate * the I/O is completed. Those pages are unlocked 1227*0Sstevel@tonic-gate * when the application calls aiowait/aioerror. 1228*0Sstevel@tonic-gate * So, to prevent blocking forever, cv_broadcast() 1229*0Sstevel@tonic-gate * is done to wake up aio_cleanup_thread. 1230*0Sstevel@tonic-gate * Subsequently, segvn_reclaim will be called, and 1231*0Sstevel@tonic-gate * that will do AS_CLRUNMAPWAIT() and wake us up. 1232*0Sstevel@tonic-gate * (3) Long term page locking: 1233*0Sstevel@tonic-gate * Drivers intending to have pages locked for a 1234*0Sstevel@tonic-gate * period considerably longer than for normal I/O 1235*0Sstevel@tonic-gate * (essentially forever) may have registered for a 1236*0Sstevel@tonic-gate * callback so they may unlock these pages on 1237*0Sstevel@tonic-gate * request. This is needed to allow this operation 1238*0Sstevel@tonic-gate * to succeed. Each entry on the callback list is 1239*0Sstevel@tonic-gate * examined. If the event or address range pertains 1240*0Sstevel@tonic-gate * the callback is invoked (unless it already is in 1241*0Sstevel@tonic-gate * progress). The a_contents lock must be dropped 1242*0Sstevel@tonic-gate * before the callback, so only one callback can 1243*0Sstevel@tonic-gate * be done at a time. Go to the top and do more 1244*0Sstevel@tonic-gate * until zero is returned. If zero is returned, 1245*0Sstevel@tonic-gate * either there were no callbacks for this event 1246*0Sstevel@tonic-gate * or they were already in progress. 1247*0Sstevel@tonic-gate */ 1248*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 1249*0Sstevel@tonic-gate if (as->a_callbacks && 1250*0Sstevel@tonic-gate (cb = as_find_callback(as, AS_SETPROT_EVENT, 1251*0Sstevel@tonic-gate seg->s_base, seg->s_size))) { 1252*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1253*0Sstevel@tonic-gate as_execute_callback(as, cb, AS_SETPROT_EVENT); 1254*0Sstevel@tonic-gate } else { 1255*0Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) 1256*0Sstevel@tonic-gate cv_broadcast(&as->a_cv); 1257*0Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 1258*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1259*0Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) 1260*0Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 1261*0Sstevel@tonic-gate } 1262*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1263*0Sstevel@tonic-gate goto setprot_top; 1264*0Sstevel@tonic-gate } else if (error != 0) 1265*0Sstevel@tonic-gate break; 1266*0Sstevel@tonic-gate } 1267*0Sstevel@tonic-gate if (error != 0) { 1268*0Sstevel@tonic-gate as_setwatch(as); 1269*0Sstevel@tonic-gate } else { 1270*0Sstevel@tonic-gate as_setwatchprot(as, saveraddr, saversize, prot); 1271*0Sstevel@tonic-gate } 1272*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1273*0Sstevel@tonic-gate return (error); 1274*0Sstevel@tonic-gate } 1275*0Sstevel@tonic-gate 1276*0Sstevel@tonic-gate /* 1277*0Sstevel@tonic-gate * Check to make sure that the interval [addr, addr + size) 1278*0Sstevel@tonic-gate * in address space `as' has at least the specified protection. 1279*0Sstevel@tonic-gate * It is ok for the range to cross over several segments, as long 1280*0Sstevel@tonic-gate * as they are contiguous. 1281*0Sstevel@tonic-gate */ 1282*0Sstevel@tonic-gate int 1283*0Sstevel@tonic-gate as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot) 1284*0Sstevel@tonic-gate { 1285*0Sstevel@tonic-gate struct seg *seg; 1286*0Sstevel@tonic-gate size_t ssize; 1287*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 1288*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 1289*0Sstevel@tonic-gate int error = 0; 1290*0Sstevel@tonic-gate 1291*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1292*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 1293*0Sstevel@tonic-gate (size_t)raddr; 1294*0Sstevel@tonic-gate 1295*0Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 1296*0Sstevel@tonic-gate return (ENOMEM); 1297*0Sstevel@tonic-gate 1298*0Sstevel@tonic-gate /* 1299*0Sstevel@tonic-gate * This is ugly as sin... 1300*0Sstevel@tonic-gate * Normally, we only acquire the address space readers lock. 1301*0Sstevel@tonic-gate * However, if the address space has watchpoints present, 1302*0Sstevel@tonic-gate * we must acquire the writer lock on the address space for 1303*0Sstevel@tonic-gate * the benefit of as_clearwatchprot() and as_setwatchprot(). 1304*0Sstevel@tonic-gate */ 1305*0Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) != 0) 1306*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1307*0Sstevel@tonic-gate else 1308*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1309*0Sstevel@tonic-gate as_clearwatchprot(as, raddr, rsize); 1310*0Sstevel@tonic-gate seg = as_segat(as, raddr); 1311*0Sstevel@tonic-gate if (seg == NULL) { 1312*0Sstevel@tonic-gate as_setwatch(as); 1313*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1314*0Sstevel@tonic-gate return (ENOMEM); 1315*0Sstevel@tonic-gate } 1316*0Sstevel@tonic-gate 1317*0Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 1318*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 1319*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 1320*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 1321*0Sstevel@tonic-gate error = ENOMEM; 1322*0Sstevel@tonic-gate break; 1323*0Sstevel@tonic-gate } 1324*0Sstevel@tonic-gate } 1325*0Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 1326*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 1327*0Sstevel@tonic-gate else 1328*0Sstevel@tonic-gate ssize = rsize; 1329*0Sstevel@tonic-gate 1330*0Sstevel@tonic-gate error = SEGOP_CHECKPROT(seg, raddr, ssize, prot); 1331*0Sstevel@tonic-gate if (error != 0) 1332*0Sstevel@tonic-gate break; 1333*0Sstevel@tonic-gate } 1334*0Sstevel@tonic-gate as_setwatch(as); 1335*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1336*0Sstevel@tonic-gate return (error); 1337*0Sstevel@tonic-gate } 1338*0Sstevel@tonic-gate 1339*0Sstevel@tonic-gate int 1340*0Sstevel@tonic-gate as_unmap(struct as *as, caddr_t addr, size_t size) 1341*0Sstevel@tonic-gate { 1342*0Sstevel@tonic-gate struct seg *seg, *seg_next; 1343*0Sstevel@tonic-gate struct as_callback *cb; 1344*0Sstevel@tonic-gate caddr_t raddr, eaddr; 1345*0Sstevel@tonic-gate size_t ssize; 1346*0Sstevel@tonic-gate int err; 1347*0Sstevel@tonic-gate 1348*0Sstevel@tonic-gate top: 1349*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1350*0Sstevel@tonic-gate eaddr = (caddr_t)(((uintptr_t)(addr + size) + PAGEOFFSET) & 1351*0Sstevel@tonic-gate (uintptr_t)PAGEMASK); 1352*0Sstevel@tonic-gate 1353*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1354*0Sstevel@tonic-gate 1355*0Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 1356*0Sstevel@tonic-gate gethrestime(&as->a_updatetime); 1357*0Sstevel@tonic-gate 1358*0Sstevel@tonic-gate /* 1359*0Sstevel@tonic-gate * Use as_findseg to find the first segment in the range, then 1360*0Sstevel@tonic-gate * step through the segments in order, following s_next. 1361*0Sstevel@tonic-gate */ 1362*0Sstevel@tonic-gate as_clearwatchprot(as, raddr, eaddr - raddr); 1363*0Sstevel@tonic-gate 1364*0Sstevel@tonic-gate for (seg = as_findseg(as, raddr, 0); seg != NULL; seg = seg_next) { 1365*0Sstevel@tonic-gate if (eaddr <= seg->s_base) 1366*0Sstevel@tonic-gate break; /* eaddr was in a gap; all done */ 1367*0Sstevel@tonic-gate 1368*0Sstevel@tonic-gate /* this is implied by the test above */ 1369*0Sstevel@tonic-gate ASSERT(raddr < eaddr); 1370*0Sstevel@tonic-gate 1371*0Sstevel@tonic-gate if (raddr < seg->s_base) 1372*0Sstevel@tonic-gate raddr = seg->s_base; /* raddr was in a gap */ 1373*0Sstevel@tonic-gate 1374*0Sstevel@tonic-gate if (eaddr > (seg->s_base + seg->s_size)) 1375*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 1376*0Sstevel@tonic-gate else 1377*0Sstevel@tonic-gate ssize = eaddr - raddr; 1378*0Sstevel@tonic-gate 1379*0Sstevel@tonic-gate /* 1380*0Sstevel@tonic-gate * Save next segment pointer since seg can be 1381*0Sstevel@tonic-gate * destroyed during the segment unmap operation. 1382*0Sstevel@tonic-gate */ 1383*0Sstevel@tonic-gate seg_next = AS_SEGNEXT(as, seg); 1384*0Sstevel@tonic-gate 1385*0Sstevel@tonic-gate err = SEGOP_UNMAP(seg, raddr, ssize); 1386*0Sstevel@tonic-gate if (err == EAGAIN) { 1387*0Sstevel@tonic-gate /* 1388*0Sstevel@tonic-gate * Memory is currently locked. It must be unlocked 1389*0Sstevel@tonic-gate * before this operation can succeed through a retry. 1390*0Sstevel@tonic-gate * The possible reasons for locked memory and 1391*0Sstevel@tonic-gate * corresponding strategies for unlocking are: 1392*0Sstevel@tonic-gate * (1) Normal I/O 1393*0Sstevel@tonic-gate * wait for a signal that the I/O operation 1394*0Sstevel@tonic-gate * has completed and the memory is unlocked. 1395*0Sstevel@tonic-gate * (2) Asynchronous I/O 1396*0Sstevel@tonic-gate * The aio subsystem does not unlock pages when 1397*0Sstevel@tonic-gate * the I/O is completed. Those pages are unlocked 1398*0Sstevel@tonic-gate * when the application calls aiowait/aioerror. 1399*0Sstevel@tonic-gate * So, to prevent blocking forever, cv_broadcast() 1400*0Sstevel@tonic-gate * is done to wake up aio_cleanup_thread. 1401*0Sstevel@tonic-gate * Subsequently, segvn_reclaim will be called, and 1402*0Sstevel@tonic-gate * that will do AS_CLRUNMAPWAIT() and wake us up. 1403*0Sstevel@tonic-gate * (3) Long term page locking: 1404*0Sstevel@tonic-gate * Drivers intending to have pages locked for a 1405*0Sstevel@tonic-gate * period considerably longer than for normal I/O 1406*0Sstevel@tonic-gate * (essentially forever) may have registered for a 1407*0Sstevel@tonic-gate * callback so they may unlock these pages on 1408*0Sstevel@tonic-gate * request. This is needed to allow this operation 1409*0Sstevel@tonic-gate * to succeed. Each entry on the callback list is 1410*0Sstevel@tonic-gate * examined. If the event or address range pertains 1411*0Sstevel@tonic-gate * the callback is invoked (unless it already is in 1412*0Sstevel@tonic-gate * progress). The a_contents lock must be dropped 1413*0Sstevel@tonic-gate * before the callback, so only one callback can 1414*0Sstevel@tonic-gate * be done at a time. Go to the top and do more 1415*0Sstevel@tonic-gate * until zero is returned. If zero is returned, 1416*0Sstevel@tonic-gate * either there were no callbacks for this event 1417*0Sstevel@tonic-gate * or they were already in progress. 1418*0Sstevel@tonic-gate */ 1419*0Sstevel@tonic-gate as_setwatch(as); 1420*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 1421*0Sstevel@tonic-gate if (as->a_callbacks && 1422*0Sstevel@tonic-gate (cb = as_find_callback(as, AS_UNMAP_EVENT, 1423*0Sstevel@tonic-gate seg->s_base, seg->s_size))) { 1424*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1425*0Sstevel@tonic-gate as_execute_callback(as, cb, AS_UNMAP_EVENT); 1426*0Sstevel@tonic-gate } else { 1427*0Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) 1428*0Sstevel@tonic-gate cv_broadcast(&as->a_cv); 1429*0Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 1430*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1431*0Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) 1432*0Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 1433*0Sstevel@tonic-gate } 1434*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1435*0Sstevel@tonic-gate goto top; 1436*0Sstevel@tonic-gate } else if (err == IE_RETRY) { 1437*0Sstevel@tonic-gate as_setwatch(as); 1438*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1439*0Sstevel@tonic-gate goto top; 1440*0Sstevel@tonic-gate } else if (err) { 1441*0Sstevel@tonic-gate as_setwatch(as); 1442*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1443*0Sstevel@tonic-gate return (-1); 1444*0Sstevel@tonic-gate } 1445*0Sstevel@tonic-gate 1446*0Sstevel@tonic-gate as->a_size -= ssize; 1447*0Sstevel@tonic-gate raddr += ssize; 1448*0Sstevel@tonic-gate } 1449*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1450*0Sstevel@tonic-gate return (0); 1451*0Sstevel@tonic-gate } 1452*0Sstevel@tonic-gate 1453*0Sstevel@tonic-gate static int 1454*0Sstevel@tonic-gate as_map_vnsegs(struct as *as, caddr_t addr, size_t size, 1455*0Sstevel@tonic-gate int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated) 1456*0Sstevel@tonic-gate { 1457*0Sstevel@tonic-gate int text = vn_a->flags & MAP_TEXT; 1458*0Sstevel@tonic-gate uint_t szcvec = map_execseg_pgszcvec(text, addr, size); 1459*0Sstevel@tonic-gate uint_t szc; 1460*0Sstevel@tonic-gate uint_t nszc; 1461*0Sstevel@tonic-gate int error; 1462*0Sstevel@tonic-gate caddr_t a; 1463*0Sstevel@tonic-gate caddr_t eaddr; 1464*0Sstevel@tonic-gate size_t segsize; 1465*0Sstevel@tonic-gate struct seg *seg; 1466*0Sstevel@tonic-gate uint_t save_szcvec; 1467*0Sstevel@tonic-gate size_t pgsz; 1468*0Sstevel@tonic-gate struct vattr va; 1469*0Sstevel@tonic-gate u_offset_t eoff; 1470*0Sstevel@tonic-gate size_t save_size = 0; 1471*0Sstevel@tonic-gate 1472*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 1473*0Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(addr, PAGESIZE)); 1474*0Sstevel@tonic-gate ASSERT(IS_P2ALIGNED(size, PAGESIZE)); 1475*0Sstevel@tonic-gate ASSERT(vn_a->vp != NULL); 1476*0Sstevel@tonic-gate ASSERT(vn_a->amp == NULL); 1477*0Sstevel@tonic-gate 1478*0Sstevel@tonic-gate again: 1479*0Sstevel@tonic-gate if (szcvec <= 1) { 1480*0Sstevel@tonic-gate seg = seg_alloc(as, addr, size); 1481*0Sstevel@tonic-gate if (seg == NULL) { 1482*0Sstevel@tonic-gate return (ENOMEM); 1483*0Sstevel@tonic-gate } 1484*0Sstevel@tonic-gate vn_a->szc = 0; 1485*0Sstevel@tonic-gate error = (*crfp)(seg, vn_a); 1486*0Sstevel@tonic-gate if (error != 0) { 1487*0Sstevel@tonic-gate seg_free(seg); 1488*0Sstevel@tonic-gate } 1489*0Sstevel@tonic-gate return (error); 1490*0Sstevel@tonic-gate } 1491*0Sstevel@tonic-gate 1492*0Sstevel@tonic-gate va.va_mask = AT_SIZE; 1493*0Sstevel@tonic-gate if (VOP_GETATTR(vn_a->vp, &va, ATTR_HINT, vn_a->cred) != 0) { 1494*0Sstevel@tonic-gate szcvec = 0; 1495*0Sstevel@tonic-gate goto again; 1496*0Sstevel@tonic-gate } 1497*0Sstevel@tonic-gate eoff = vn_a->offset & PAGEMASK; 1498*0Sstevel@tonic-gate if (eoff >= va.va_size) { 1499*0Sstevel@tonic-gate szcvec = 0; 1500*0Sstevel@tonic-gate goto again; 1501*0Sstevel@tonic-gate } 1502*0Sstevel@tonic-gate eoff += size; 1503*0Sstevel@tonic-gate if (btopr(va.va_size) < btopr(eoff)) { 1504*0Sstevel@tonic-gate save_size = size; 1505*0Sstevel@tonic-gate size = va.va_size - (vn_a->offset & PAGEMASK); 1506*0Sstevel@tonic-gate size = P2ROUNDUP_TYPED(size, PAGESIZE, size_t); 1507*0Sstevel@tonic-gate szcvec = map_execseg_pgszcvec(text, addr, size); 1508*0Sstevel@tonic-gate if (szcvec <= 1) { 1509*0Sstevel@tonic-gate size = save_size; 1510*0Sstevel@tonic-gate goto again; 1511*0Sstevel@tonic-gate } 1512*0Sstevel@tonic-gate } 1513*0Sstevel@tonic-gate 1514*0Sstevel@tonic-gate eaddr = addr + size; 1515*0Sstevel@tonic-gate save_szcvec = szcvec; 1516*0Sstevel@tonic-gate szcvec >>= 1; 1517*0Sstevel@tonic-gate szc = 0; 1518*0Sstevel@tonic-gate nszc = 0; 1519*0Sstevel@tonic-gate while (szcvec) { 1520*0Sstevel@tonic-gate if ((szcvec & 0x1) == 0) { 1521*0Sstevel@tonic-gate nszc++; 1522*0Sstevel@tonic-gate szcvec >>= 1; 1523*0Sstevel@tonic-gate continue; 1524*0Sstevel@tonic-gate } 1525*0Sstevel@tonic-gate nszc++; 1526*0Sstevel@tonic-gate pgsz = page_get_pagesize(nszc); 1527*0Sstevel@tonic-gate a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz); 1528*0Sstevel@tonic-gate if (a != addr) { 1529*0Sstevel@tonic-gate ASSERT(a < eaddr); 1530*0Sstevel@tonic-gate segsize = a - addr; 1531*0Sstevel@tonic-gate seg = seg_alloc(as, addr, segsize); 1532*0Sstevel@tonic-gate if (seg == NULL) { 1533*0Sstevel@tonic-gate return (ENOMEM); 1534*0Sstevel@tonic-gate } 1535*0Sstevel@tonic-gate vn_a->szc = szc; 1536*0Sstevel@tonic-gate error = (*crfp)(seg, vn_a); 1537*0Sstevel@tonic-gate if (error != 0) { 1538*0Sstevel@tonic-gate seg_free(seg); 1539*0Sstevel@tonic-gate return (error); 1540*0Sstevel@tonic-gate } 1541*0Sstevel@tonic-gate *segcreated = 1; 1542*0Sstevel@tonic-gate vn_a->offset += segsize; 1543*0Sstevel@tonic-gate addr = a; 1544*0Sstevel@tonic-gate } 1545*0Sstevel@tonic-gate szc = nszc; 1546*0Sstevel@tonic-gate szcvec >>= 1; 1547*0Sstevel@tonic-gate } 1548*0Sstevel@tonic-gate 1549*0Sstevel@tonic-gate ASSERT(addr < eaddr); 1550*0Sstevel@tonic-gate szcvec = save_szcvec | 1; /* add 8K pages */ 1551*0Sstevel@tonic-gate while (szcvec) { 1552*0Sstevel@tonic-gate a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz); 1553*0Sstevel@tonic-gate ASSERT(a >= addr); 1554*0Sstevel@tonic-gate if (a != addr) { 1555*0Sstevel@tonic-gate segsize = a - addr; 1556*0Sstevel@tonic-gate seg = seg_alloc(as, addr, segsize); 1557*0Sstevel@tonic-gate if (seg == NULL) { 1558*0Sstevel@tonic-gate return (ENOMEM); 1559*0Sstevel@tonic-gate } 1560*0Sstevel@tonic-gate vn_a->szc = szc; 1561*0Sstevel@tonic-gate error = (*crfp)(seg, vn_a); 1562*0Sstevel@tonic-gate if (error != 0) { 1563*0Sstevel@tonic-gate seg_free(seg); 1564*0Sstevel@tonic-gate return (error); 1565*0Sstevel@tonic-gate } 1566*0Sstevel@tonic-gate *segcreated = 1; 1567*0Sstevel@tonic-gate vn_a->offset += segsize; 1568*0Sstevel@tonic-gate addr = a; 1569*0Sstevel@tonic-gate } 1570*0Sstevel@tonic-gate szcvec &= ~(1 << szc); 1571*0Sstevel@tonic-gate if (szcvec) { 1572*0Sstevel@tonic-gate szc = highbit(szcvec) - 1; 1573*0Sstevel@tonic-gate pgsz = page_get_pagesize(szc); 1574*0Sstevel@tonic-gate } 1575*0Sstevel@tonic-gate } 1576*0Sstevel@tonic-gate ASSERT(addr == eaddr); 1577*0Sstevel@tonic-gate 1578*0Sstevel@tonic-gate if (save_size) { 1579*0Sstevel@tonic-gate size = save_size - size; 1580*0Sstevel@tonic-gate goto again; 1581*0Sstevel@tonic-gate } 1582*0Sstevel@tonic-gate 1583*0Sstevel@tonic-gate return (0); 1584*0Sstevel@tonic-gate } 1585*0Sstevel@tonic-gate 1586*0Sstevel@tonic-gate int 1587*0Sstevel@tonic-gate as_map(struct as *as, caddr_t addr, size_t size, int (*crfp)(), void *argsp) 1588*0Sstevel@tonic-gate { 1589*0Sstevel@tonic-gate struct seg *seg = NULL; 1590*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 1591*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 1592*0Sstevel@tonic-gate int error; 1593*0Sstevel@tonic-gate struct proc *p = curproc; 1594*0Sstevel@tonic-gate 1595*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1596*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 1597*0Sstevel@tonic-gate (size_t)raddr; 1598*0Sstevel@tonic-gate 1599*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1600*0Sstevel@tonic-gate 1601*0Sstevel@tonic-gate /* 1602*0Sstevel@tonic-gate * check for wrap around 1603*0Sstevel@tonic-gate */ 1604*0Sstevel@tonic-gate if ((raddr + rsize < raddr) || (as->a_size > (ULONG_MAX - size))) { 1605*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1606*0Sstevel@tonic-gate return (ENOMEM); 1607*0Sstevel@tonic-gate } 1608*0Sstevel@tonic-gate 1609*0Sstevel@tonic-gate as->a_updatedir = 1; /* inform /proc */ 1610*0Sstevel@tonic-gate gethrestime(&as->a_updatetime); 1611*0Sstevel@tonic-gate 1612*0Sstevel@tonic-gate if (as != &kas && as->a_size + rsize > (size_t)p->p_vmem_ctl) { 1613*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1614*0Sstevel@tonic-gate 1615*0Sstevel@tonic-gate (void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p, 1616*0Sstevel@tonic-gate RCA_UNSAFE_ALL); 1617*0Sstevel@tonic-gate 1618*0Sstevel@tonic-gate return (ENOMEM); 1619*0Sstevel@tonic-gate } 1620*0Sstevel@tonic-gate 1621*0Sstevel@tonic-gate if (AS_MAP_VNSEGS_USELPGS(crfp, argsp)) { 1622*0Sstevel@tonic-gate int unmap = 0; 1623*0Sstevel@tonic-gate error = as_map_vnsegs(as, raddr, rsize, crfp, 1624*0Sstevel@tonic-gate (struct segvn_crargs *)argsp, &unmap); 1625*0Sstevel@tonic-gate if (error != 0) { 1626*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1627*0Sstevel@tonic-gate if (unmap) { 1628*0Sstevel@tonic-gate (void) as_unmap(as, addr, size); 1629*0Sstevel@tonic-gate } 1630*0Sstevel@tonic-gate return (error); 1631*0Sstevel@tonic-gate } 1632*0Sstevel@tonic-gate } else { 1633*0Sstevel@tonic-gate seg = seg_alloc(as, addr, size); 1634*0Sstevel@tonic-gate if (seg == NULL) { 1635*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1636*0Sstevel@tonic-gate return (ENOMEM); 1637*0Sstevel@tonic-gate } 1638*0Sstevel@tonic-gate 1639*0Sstevel@tonic-gate error = (*crfp)(seg, argsp); 1640*0Sstevel@tonic-gate if (error != 0) { 1641*0Sstevel@tonic-gate seg_free(seg); 1642*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1643*0Sstevel@tonic-gate return (error); 1644*0Sstevel@tonic-gate } 1645*0Sstevel@tonic-gate } 1646*0Sstevel@tonic-gate 1647*0Sstevel@tonic-gate /* 1648*0Sstevel@tonic-gate * Add size now so as_unmap will work if as_ctl fails. 1649*0Sstevel@tonic-gate */ 1650*0Sstevel@tonic-gate as->a_size += rsize; 1651*0Sstevel@tonic-gate 1652*0Sstevel@tonic-gate as_setwatch(as); 1653*0Sstevel@tonic-gate 1654*0Sstevel@tonic-gate /* 1655*0Sstevel@tonic-gate * If the address space is locked, 1656*0Sstevel@tonic-gate * establish memory locks for the new segment. 1657*0Sstevel@tonic-gate */ 1658*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 1659*0Sstevel@tonic-gate if (AS_ISPGLCK(as)) { 1660*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1661*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1662*0Sstevel@tonic-gate error = as_ctl(as, addr, size, MC_LOCK, 0, 0, NULL, 0); 1663*0Sstevel@tonic-gate if (error != 0) 1664*0Sstevel@tonic-gate (void) as_unmap(as, addr, size); 1665*0Sstevel@tonic-gate } else { 1666*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1667*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1668*0Sstevel@tonic-gate } 1669*0Sstevel@tonic-gate return (error); 1670*0Sstevel@tonic-gate } 1671*0Sstevel@tonic-gate 1672*0Sstevel@tonic-gate 1673*0Sstevel@tonic-gate /* 1674*0Sstevel@tonic-gate * Delete all segments in the address space marked with S_PURGE. 1675*0Sstevel@tonic-gate * This is currently used for Sparc V9 nofault ASI segments (seg_nf.c). 1676*0Sstevel@tonic-gate * These segments are deleted as a first step before calls to as_gap(), so 1677*0Sstevel@tonic-gate * that they don't affect mmap() or shmat(). 1678*0Sstevel@tonic-gate */ 1679*0Sstevel@tonic-gate void 1680*0Sstevel@tonic-gate as_purge(struct as *as) 1681*0Sstevel@tonic-gate { 1682*0Sstevel@tonic-gate struct seg *seg; 1683*0Sstevel@tonic-gate struct seg *next_seg; 1684*0Sstevel@tonic-gate 1685*0Sstevel@tonic-gate /* 1686*0Sstevel@tonic-gate * the setting of NEEDSPURGE is protect by as_rangelock(), so 1687*0Sstevel@tonic-gate * no need to grab a_contents mutex for this check 1688*0Sstevel@tonic-gate */ 1689*0Sstevel@tonic-gate if ((as->a_flags & AS_NEEDSPURGE) == 0) 1690*0Sstevel@tonic-gate return; 1691*0Sstevel@tonic-gate 1692*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1693*0Sstevel@tonic-gate next_seg = NULL; 1694*0Sstevel@tonic-gate seg = AS_SEGFIRST(as); 1695*0Sstevel@tonic-gate while (seg != NULL) { 1696*0Sstevel@tonic-gate next_seg = AS_SEGNEXT(as, seg); 1697*0Sstevel@tonic-gate if (seg->s_flags & S_PURGE) 1698*0Sstevel@tonic-gate SEGOP_UNMAP(seg, seg->s_base, seg->s_size); 1699*0Sstevel@tonic-gate seg = next_seg; 1700*0Sstevel@tonic-gate } 1701*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1702*0Sstevel@tonic-gate 1703*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 1704*0Sstevel@tonic-gate as->a_flags &= ~AS_NEEDSPURGE; 1705*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1706*0Sstevel@tonic-gate } 1707*0Sstevel@tonic-gate 1708*0Sstevel@tonic-gate /* 1709*0Sstevel@tonic-gate * Find a hole of at least size minlen within [base, base + len). 1710*0Sstevel@tonic-gate * 1711*0Sstevel@tonic-gate * If flags specifies AH_HI, the hole will have the highest possible address 1712*0Sstevel@tonic-gate * in the range. We use the as->a_lastgap field to figure out where to 1713*0Sstevel@tonic-gate * start looking for a gap. 1714*0Sstevel@tonic-gate * 1715*0Sstevel@tonic-gate * Otherwise, the gap will have the lowest possible address. 1716*0Sstevel@tonic-gate * 1717*0Sstevel@tonic-gate * If flags specifies AH_CONTAIN, the hole will contain the address addr. 1718*0Sstevel@tonic-gate * 1719*0Sstevel@tonic-gate * If an adequate hole is found, base and len are set to reflect the part of 1720*0Sstevel@tonic-gate * the hole that is within range, and 0 is returned, otherwise, 1721*0Sstevel@tonic-gate * -1 is returned. 1722*0Sstevel@tonic-gate * 1723*0Sstevel@tonic-gate * NOTE: This routine is not correct when base+len overflows caddr_t. 1724*0Sstevel@tonic-gate */ 1725*0Sstevel@tonic-gate int 1726*0Sstevel@tonic-gate as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, uint_t flags, 1727*0Sstevel@tonic-gate caddr_t addr) 1728*0Sstevel@tonic-gate { 1729*0Sstevel@tonic-gate caddr_t lobound = *basep; 1730*0Sstevel@tonic-gate caddr_t hibound = lobound + *lenp; 1731*0Sstevel@tonic-gate struct seg *lseg, *hseg; 1732*0Sstevel@tonic-gate caddr_t lo, hi; 1733*0Sstevel@tonic-gate int forward; 1734*0Sstevel@tonic-gate caddr_t save_base; 1735*0Sstevel@tonic-gate size_t save_len; 1736*0Sstevel@tonic-gate 1737*0Sstevel@tonic-gate save_base = *basep; 1738*0Sstevel@tonic-gate save_len = *lenp; 1739*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1740*0Sstevel@tonic-gate if (AS_SEGFIRST(as) == NULL) { 1741*0Sstevel@tonic-gate if (valid_va_range(basep, lenp, minlen, flags & AH_DIR)) { 1742*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1743*0Sstevel@tonic-gate return (0); 1744*0Sstevel@tonic-gate } else { 1745*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1746*0Sstevel@tonic-gate *basep = save_base; 1747*0Sstevel@tonic-gate *lenp = save_len; 1748*0Sstevel@tonic-gate return (-1); 1749*0Sstevel@tonic-gate } 1750*0Sstevel@tonic-gate } 1751*0Sstevel@tonic-gate 1752*0Sstevel@tonic-gate /* 1753*0Sstevel@tonic-gate * Set up to iterate over all the inter-segment holes in the given 1754*0Sstevel@tonic-gate * direction. lseg is NULL for the lowest-addressed hole and hseg is 1755*0Sstevel@tonic-gate * NULL for the highest-addressed hole. If moving backwards, we reset 1756*0Sstevel@tonic-gate * sseg to denote the highest-addressed segment. 1757*0Sstevel@tonic-gate */ 1758*0Sstevel@tonic-gate forward = (flags & AH_DIR) == AH_LO; 1759*0Sstevel@tonic-gate if (forward) { 1760*0Sstevel@tonic-gate hseg = as_findseg(as, lobound, 1); 1761*0Sstevel@tonic-gate lseg = AS_SEGPREV(as, hseg); 1762*0Sstevel@tonic-gate } else { 1763*0Sstevel@tonic-gate 1764*0Sstevel@tonic-gate /* 1765*0Sstevel@tonic-gate * If allocating at least as much as the last allocation, 1766*0Sstevel@tonic-gate * use a_lastgap's base as a better estimate of hibound. 1767*0Sstevel@tonic-gate */ 1768*0Sstevel@tonic-gate if (as->a_lastgap && 1769*0Sstevel@tonic-gate minlen >= as->a_lastgap->s_size && 1770*0Sstevel@tonic-gate hibound >= as->a_lastgap->s_base) 1771*0Sstevel@tonic-gate hibound = as->a_lastgap->s_base; 1772*0Sstevel@tonic-gate 1773*0Sstevel@tonic-gate hseg = as_findseg(as, hibound, 1); 1774*0Sstevel@tonic-gate if (hseg->s_base + hseg->s_size < hibound) { 1775*0Sstevel@tonic-gate lseg = hseg; 1776*0Sstevel@tonic-gate hseg = NULL; 1777*0Sstevel@tonic-gate } else { 1778*0Sstevel@tonic-gate lseg = AS_SEGPREV(as, hseg); 1779*0Sstevel@tonic-gate } 1780*0Sstevel@tonic-gate } 1781*0Sstevel@tonic-gate 1782*0Sstevel@tonic-gate for (;;) { 1783*0Sstevel@tonic-gate /* 1784*0Sstevel@tonic-gate * Set lo and hi to the hole's boundaries. (We should really 1785*0Sstevel@tonic-gate * use MAXADDR in place of hibound in the expression below, 1786*0Sstevel@tonic-gate * but can't express it easily; using hibound in its place is 1787*0Sstevel@tonic-gate * harmless.) 1788*0Sstevel@tonic-gate */ 1789*0Sstevel@tonic-gate lo = (lseg == NULL) ? 0 : lseg->s_base + lseg->s_size; 1790*0Sstevel@tonic-gate hi = (hseg == NULL) ? hibound : hseg->s_base; 1791*0Sstevel@tonic-gate /* 1792*0Sstevel@tonic-gate * If the iteration has moved past the interval from lobound 1793*0Sstevel@tonic-gate * to hibound it's pointless to continue. 1794*0Sstevel@tonic-gate */ 1795*0Sstevel@tonic-gate if ((forward && lo > hibound) || (!forward && hi < lobound)) 1796*0Sstevel@tonic-gate break; 1797*0Sstevel@tonic-gate else if (lo > hibound || hi < lobound) 1798*0Sstevel@tonic-gate goto cont; 1799*0Sstevel@tonic-gate /* 1800*0Sstevel@tonic-gate * Candidate hole lies at least partially within the allowable 1801*0Sstevel@tonic-gate * range. Restrict it to fall completely within that range, 1802*0Sstevel@tonic-gate * i.e., to [max(lo, lobound), min(hi, hibound)]. 1803*0Sstevel@tonic-gate */ 1804*0Sstevel@tonic-gate if (lo < lobound) 1805*0Sstevel@tonic-gate lo = lobound; 1806*0Sstevel@tonic-gate if (hi > hibound) 1807*0Sstevel@tonic-gate hi = hibound; 1808*0Sstevel@tonic-gate /* 1809*0Sstevel@tonic-gate * Verify that the candidate hole is big enough and meets 1810*0Sstevel@tonic-gate * hardware constraints. 1811*0Sstevel@tonic-gate */ 1812*0Sstevel@tonic-gate *basep = lo; 1813*0Sstevel@tonic-gate *lenp = hi - lo; 1814*0Sstevel@tonic-gate if (valid_va_range(basep, lenp, minlen, 1815*0Sstevel@tonic-gate forward ? AH_LO : AH_HI) && 1816*0Sstevel@tonic-gate ((flags & AH_CONTAIN) == 0 || 1817*0Sstevel@tonic-gate (*basep <= addr && *basep + *lenp > addr))) { 1818*0Sstevel@tonic-gate if (!forward) 1819*0Sstevel@tonic-gate as->a_lastgap = hseg; 1820*0Sstevel@tonic-gate if (hseg != NULL) 1821*0Sstevel@tonic-gate as->a_lastgaphl = hseg; 1822*0Sstevel@tonic-gate else 1823*0Sstevel@tonic-gate as->a_lastgaphl = lseg; 1824*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1825*0Sstevel@tonic-gate return (0); 1826*0Sstevel@tonic-gate } 1827*0Sstevel@tonic-gate cont: 1828*0Sstevel@tonic-gate /* 1829*0Sstevel@tonic-gate * Move to the next hole. 1830*0Sstevel@tonic-gate */ 1831*0Sstevel@tonic-gate if (forward) { 1832*0Sstevel@tonic-gate lseg = hseg; 1833*0Sstevel@tonic-gate if (lseg == NULL) 1834*0Sstevel@tonic-gate break; 1835*0Sstevel@tonic-gate hseg = AS_SEGNEXT(as, hseg); 1836*0Sstevel@tonic-gate } else { 1837*0Sstevel@tonic-gate hseg = lseg; 1838*0Sstevel@tonic-gate if (hseg == NULL) 1839*0Sstevel@tonic-gate break; 1840*0Sstevel@tonic-gate lseg = AS_SEGPREV(as, lseg); 1841*0Sstevel@tonic-gate } 1842*0Sstevel@tonic-gate } 1843*0Sstevel@tonic-gate *basep = save_base; 1844*0Sstevel@tonic-gate *lenp = save_len; 1845*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1846*0Sstevel@tonic-gate return (-1); 1847*0Sstevel@tonic-gate } 1848*0Sstevel@tonic-gate 1849*0Sstevel@tonic-gate /* 1850*0Sstevel@tonic-gate * Return the next range within [base, base + len) that is backed 1851*0Sstevel@tonic-gate * with "real memory". Skip holes and non-seg_vn segments. 1852*0Sstevel@tonic-gate * We're lazy and only return one segment at a time. 1853*0Sstevel@tonic-gate */ 1854*0Sstevel@tonic-gate int 1855*0Sstevel@tonic-gate as_memory(struct as *as, caddr_t *basep, size_t *lenp) 1856*0Sstevel@tonic-gate { 1857*0Sstevel@tonic-gate extern struct seg_ops segspt_shmops; /* needs a header file */ 1858*0Sstevel@tonic-gate struct seg *seg; 1859*0Sstevel@tonic-gate caddr_t addr, eaddr; 1860*0Sstevel@tonic-gate caddr_t segend; 1861*0Sstevel@tonic-gate 1862*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1863*0Sstevel@tonic-gate 1864*0Sstevel@tonic-gate addr = *basep; 1865*0Sstevel@tonic-gate eaddr = addr + *lenp; 1866*0Sstevel@tonic-gate 1867*0Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 1868*0Sstevel@tonic-gate if (seg != NULL) 1869*0Sstevel@tonic-gate addr = MAX(seg->s_base, addr); 1870*0Sstevel@tonic-gate 1871*0Sstevel@tonic-gate for (;;) { 1872*0Sstevel@tonic-gate if (seg == NULL || addr >= eaddr || eaddr <= seg->s_base) { 1873*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1874*0Sstevel@tonic-gate return (EINVAL); 1875*0Sstevel@tonic-gate } 1876*0Sstevel@tonic-gate 1877*0Sstevel@tonic-gate if (seg->s_ops == &segvn_ops) { 1878*0Sstevel@tonic-gate segend = seg->s_base + seg->s_size; 1879*0Sstevel@tonic-gate break; 1880*0Sstevel@tonic-gate } 1881*0Sstevel@tonic-gate 1882*0Sstevel@tonic-gate /* 1883*0Sstevel@tonic-gate * We do ISM by looking into the private data 1884*0Sstevel@tonic-gate * to determine the real size of the segment. 1885*0Sstevel@tonic-gate */ 1886*0Sstevel@tonic-gate if (seg->s_ops == &segspt_shmops) { 1887*0Sstevel@tonic-gate segend = seg->s_base + spt_realsize(seg); 1888*0Sstevel@tonic-gate if (addr < segend) 1889*0Sstevel@tonic-gate break; 1890*0Sstevel@tonic-gate } 1891*0Sstevel@tonic-gate 1892*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 1893*0Sstevel@tonic-gate 1894*0Sstevel@tonic-gate if (seg != NULL) 1895*0Sstevel@tonic-gate addr = seg->s_base; 1896*0Sstevel@tonic-gate } 1897*0Sstevel@tonic-gate 1898*0Sstevel@tonic-gate *basep = addr; 1899*0Sstevel@tonic-gate 1900*0Sstevel@tonic-gate if (segend > eaddr) 1901*0Sstevel@tonic-gate *lenp = eaddr - addr; 1902*0Sstevel@tonic-gate else 1903*0Sstevel@tonic-gate *lenp = segend - addr; 1904*0Sstevel@tonic-gate 1905*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1906*0Sstevel@tonic-gate return (0); 1907*0Sstevel@tonic-gate } 1908*0Sstevel@tonic-gate 1909*0Sstevel@tonic-gate /* 1910*0Sstevel@tonic-gate * Swap the pages associated with the address space as out to 1911*0Sstevel@tonic-gate * secondary storage, returning the number of bytes actually 1912*0Sstevel@tonic-gate * swapped. 1913*0Sstevel@tonic-gate * 1914*0Sstevel@tonic-gate * The value returned is intended to correlate well with the process's 1915*0Sstevel@tonic-gate * memory requirements. Its usefulness for this purpose depends on 1916*0Sstevel@tonic-gate * how well the segment-level routines do at returning accurate 1917*0Sstevel@tonic-gate * information. 1918*0Sstevel@tonic-gate */ 1919*0Sstevel@tonic-gate size_t 1920*0Sstevel@tonic-gate as_swapout(struct as *as) 1921*0Sstevel@tonic-gate { 1922*0Sstevel@tonic-gate struct seg *seg; 1923*0Sstevel@tonic-gate size_t swpcnt = 0; 1924*0Sstevel@tonic-gate 1925*0Sstevel@tonic-gate /* 1926*0Sstevel@tonic-gate * Kernel-only processes have given up their address 1927*0Sstevel@tonic-gate * spaces. Of course, we shouldn't be attempting to 1928*0Sstevel@tonic-gate * swap out such processes in the first place... 1929*0Sstevel@tonic-gate */ 1930*0Sstevel@tonic-gate if (as == NULL) 1931*0Sstevel@tonic-gate return (0); 1932*0Sstevel@tonic-gate 1933*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 1934*0Sstevel@tonic-gate 1935*0Sstevel@tonic-gate /* Prevent XHATs from attaching */ 1936*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 1937*0Sstevel@tonic-gate AS_SETBUSY(as); 1938*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1939*0Sstevel@tonic-gate 1940*0Sstevel@tonic-gate 1941*0Sstevel@tonic-gate /* 1942*0Sstevel@tonic-gate * Free all mapping resources associated with the address 1943*0Sstevel@tonic-gate * space. The segment-level swapout routines capitalize 1944*0Sstevel@tonic-gate * on this unmapping by scavanging pages that have become 1945*0Sstevel@tonic-gate * unmapped here. 1946*0Sstevel@tonic-gate */ 1947*0Sstevel@tonic-gate hat_swapout(as->a_hat); 1948*0Sstevel@tonic-gate if (as->a_xhat != NULL) 1949*0Sstevel@tonic-gate xhat_swapout_all(as); 1950*0Sstevel@tonic-gate 1951*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 1952*0Sstevel@tonic-gate AS_CLRBUSY(as); 1953*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 1954*0Sstevel@tonic-gate 1955*0Sstevel@tonic-gate /* 1956*0Sstevel@tonic-gate * Call the swapout routines of all segments in the address 1957*0Sstevel@tonic-gate * space to do the actual work, accumulating the amount of 1958*0Sstevel@tonic-gate * space reclaimed. 1959*0Sstevel@tonic-gate */ 1960*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 1961*0Sstevel@tonic-gate struct seg_ops *ov = seg->s_ops; 1962*0Sstevel@tonic-gate 1963*0Sstevel@tonic-gate /* 1964*0Sstevel@tonic-gate * We have to check to see if the seg has 1965*0Sstevel@tonic-gate * an ops vector because the seg may have 1966*0Sstevel@tonic-gate * been in the middle of being set up when 1967*0Sstevel@tonic-gate * the process was picked for swapout. 1968*0Sstevel@tonic-gate */ 1969*0Sstevel@tonic-gate if ((ov != NULL) && (ov->swapout != NULL)) 1970*0Sstevel@tonic-gate swpcnt += SEGOP_SWAPOUT(seg); 1971*0Sstevel@tonic-gate } 1972*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 1973*0Sstevel@tonic-gate return (swpcnt); 1974*0Sstevel@tonic-gate } 1975*0Sstevel@tonic-gate 1976*0Sstevel@tonic-gate /* 1977*0Sstevel@tonic-gate * Determine whether data from the mappings in interval [addr, addr + size) 1978*0Sstevel@tonic-gate * are in the primary memory (core) cache. 1979*0Sstevel@tonic-gate */ 1980*0Sstevel@tonic-gate int 1981*0Sstevel@tonic-gate as_incore(struct as *as, caddr_t addr, 1982*0Sstevel@tonic-gate size_t size, char *vec, size_t *sizep) 1983*0Sstevel@tonic-gate { 1984*0Sstevel@tonic-gate struct seg *seg; 1985*0Sstevel@tonic-gate size_t ssize; 1986*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 1987*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 1988*0Sstevel@tonic-gate size_t isize; /* iteration size */ 1989*0Sstevel@tonic-gate int error = 0; /* result, assume success */ 1990*0Sstevel@tonic-gate 1991*0Sstevel@tonic-gate *sizep = 0; 1992*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 1993*0Sstevel@tonic-gate rsize = ((((size_t)addr + size) + PAGEOFFSET) & PAGEMASK) - 1994*0Sstevel@tonic-gate (size_t)raddr; 1995*0Sstevel@tonic-gate 1996*0Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 1997*0Sstevel@tonic-gate return (ENOMEM); 1998*0Sstevel@tonic-gate 1999*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2000*0Sstevel@tonic-gate seg = as_segat(as, raddr); 2001*0Sstevel@tonic-gate if (seg == NULL) { 2002*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2003*0Sstevel@tonic-gate return (-1); 2004*0Sstevel@tonic-gate } 2005*0Sstevel@tonic-gate 2006*0Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 2007*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 2008*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 2009*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 2010*0Sstevel@tonic-gate error = -1; 2011*0Sstevel@tonic-gate break; 2012*0Sstevel@tonic-gate } 2013*0Sstevel@tonic-gate } 2014*0Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 2015*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 2016*0Sstevel@tonic-gate else 2017*0Sstevel@tonic-gate ssize = rsize; 2018*0Sstevel@tonic-gate *sizep += isize = SEGOP_INCORE(seg, raddr, ssize, vec); 2019*0Sstevel@tonic-gate if (isize != ssize) { 2020*0Sstevel@tonic-gate error = -1; 2021*0Sstevel@tonic-gate break; 2022*0Sstevel@tonic-gate } 2023*0Sstevel@tonic-gate vec += btopr(ssize); 2024*0Sstevel@tonic-gate } 2025*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2026*0Sstevel@tonic-gate return (error); 2027*0Sstevel@tonic-gate } 2028*0Sstevel@tonic-gate 2029*0Sstevel@tonic-gate static void 2030*0Sstevel@tonic-gate as_segunlock(struct seg *seg, caddr_t addr, int attr, 2031*0Sstevel@tonic-gate ulong_t *bitmap, size_t position, size_t npages) 2032*0Sstevel@tonic-gate { 2033*0Sstevel@tonic-gate caddr_t range_start; 2034*0Sstevel@tonic-gate size_t pos1 = position; 2035*0Sstevel@tonic-gate size_t pos2; 2036*0Sstevel@tonic-gate size_t size; 2037*0Sstevel@tonic-gate size_t end_pos = npages + position; 2038*0Sstevel@tonic-gate 2039*0Sstevel@tonic-gate while (bt_range(bitmap, &pos1, &pos2, end_pos)) { 2040*0Sstevel@tonic-gate size = ptob((pos2 - pos1)); 2041*0Sstevel@tonic-gate range_start = (caddr_t)((uintptr_t)addr + 2042*0Sstevel@tonic-gate ptob(pos1 - position)); 2043*0Sstevel@tonic-gate 2044*0Sstevel@tonic-gate (void) SEGOP_LOCKOP(seg, range_start, size, attr, MC_UNLOCK, 2045*0Sstevel@tonic-gate (ulong_t *)NULL, (size_t)NULL); 2046*0Sstevel@tonic-gate pos1 = pos2; 2047*0Sstevel@tonic-gate } 2048*0Sstevel@tonic-gate } 2049*0Sstevel@tonic-gate 2050*0Sstevel@tonic-gate static void 2051*0Sstevel@tonic-gate as_unlockerr(struct as *as, int attr, ulong_t *mlock_map, 2052*0Sstevel@tonic-gate caddr_t raddr, size_t rsize) 2053*0Sstevel@tonic-gate { 2054*0Sstevel@tonic-gate struct seg *seg = as_segat(as, raddr); 2055*0Sstevel@tonic-gate size_t ssize; 2056*0Sstevel@tonic-gate 2057*0Sstevel@tonic-gate while (rsize != 0) { 2058*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) 2059*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 2060*0Sstevel@tonic-gate 2061*0Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 2062*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 2063*0Sstevel@tonic-gate else 2064*0Sstevel@tonic-gate ssize = rsize; 2065*0Sstevel@tonic-gate 2066*0Sstevel@tonic-gate as_segunlock(seg, raddr, attr, mlock_map, 0, btopr(ssize)); 2067*0Sstevel@tonic-gate 2068*0Sstevel@tonic-gate rsize -= ssize; 2069*0Sstevel@tonic-gate raddr += ssize; 2070*0Sstevel@tonic-gate } 2071*0Sstevel@tonic-gate } 2072*0Sstevel@tonic-gate 2073*0Sstevel@tonic-gate /* 2074*0Sstevel@tonic-gate * Cache control operations over the interval [addr, addr + size) in 2075*0Sstevel@tonic-gate * address space "as". 2076*0Sstevel@tonic-gate */ 2077*0Sstevel@tonic-gate /*ARGSUSED*/ 2078*0Sstevel@tonic-gate int 2079*0Sstevel@tonic-gate as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr, 2080*0Sstevel@tonic-gate uintptr_t arg, ulong_t *lock_map, size_t pos) 2081*0Sstevel@tonic-gate { 2082*0Sstevel@tonic-gate struct seg *seg; /* working segment */ 2083*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 2084*0Sstevel@tonic-gate caddr_t initraddr; /* saved initial rounded down addr */ 2085*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 2086*0Sstevel@tonic-gate size_t initrsize; /* saved initial rounded up size */ 2087*0Sstevel@tonic-gate size_t ssize; /* size of seg */ 2088*0Sstevel@tonic-gate int error = 0; /* result */ 2089*0Sstevel@tonic-gate size_t mlock_size; /* size of bitmap */ 2090*0Sstevel@tonic-gate ulong_t *mlock_map; /* pointer to bitmap used */ 2091*0Sstevel@tonic-gate /* to represent the locked */ 2092*0Sstevel@tonic-gate /* pages. */ 2093*0Sstevel@tonic-gate retry: 2094*0Sstevel@tonic-gate if (error == IE_RETRY) 2095*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 2096*0Sstevel@tonic-gate else 2097*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2098*0Sstevel@tonic-gate 2099*0Sstevel@tonic-gate /* 2100*0Sstevel@tonic-gate * If these are address space lock/unlock operations, loop over 2101*0Sstevel@tonic-gate * all segments in the address space, as appropriate. 2102*0Sstevel@tonic-gate */ 2103*0Sstevel@tonic-gate if (func == MC_LOCKAS) { 2104*0Sstevel@tonic-gate size_t npages, idx; 2105*0Sstevel@tonic-gate size_t rlen = 0; /* rounded as length */ 2106*0Sstevel@tonic-gate 2107*0Sstevel@tonic-gate idx = pos; 2108*0Sstevel@tonic-gate 2109*0Sstevel@tonic-gate if (arg & MCL_FUTURE) { 2110*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 2111*0Sstevel@tonic-gate AS_SETPGLCK(as); 2112*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 2113*0Sstevel@tonic-gate } 2114*0Sstevel@tonic-gate if ((arg & MCL_CURRENT) == 0) { 2115*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2116*0Sstevel@tonic-gate return (0); 2117*0Sstevel@tonic-gate } 2118*0Sstevel@tonic-gate 2119*0Sstevel@tonic-gate seg = AS_SEGFIRST(as); 2120*0Sstevel@tonic-gate if (seg == NULL) { 2121*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2122*0Sstevel@tonic-gate return (0); 2123*0Sstevel@tonic-gate } 2124*0Sstevel@tonic-gate 2125*0Sstevel@tonic-gate do { 2126*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)seg->s_base & 2127*0Sstevel@tonic-gate (uintptr_t)PAGEMASK); 2128*0Sstevel@tonic-gate rlen += (((uintptr_t)(seg->s_base + seg->s_size) + 2129*0Sstevel@tonic-gate PAGEOFFSET) & PAGEMASK) - (uintptr_t)raddr; 2130*0Sstevel@tonic-gate } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 2131*0Sstevel@tonic-gate 2132*0Sstevel@tonic-gate mlock_size = BT_BITOUL(btopr(rlen)); 2133*0Sstevel@tonic-gate if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size * 2134*0Sstevel@tonic-gate sizeof (ulong_t), KM_NOSLEEP)) == NULL) { 2135*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2136*0Sstevel@tonic-gate return (EAGAIN); 2137*0Sstevel@tonic-gate } 2138*0Sstevel@tonic-gate 2139*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) { 2140*0Sstevel@tonic-gate error = SEGOP_LOCKOP(seg, seg->s_base, 2141*0Sstevel@tonic-gate seg->s_size, attr, MC_LOCK, mlock_map, pos); 2142*0Sstevel@tonic-gate if (error != 0) 2143*0Sstevel@tonic-gate break; 2144*0Sstevel@tonic-gate pos += seg_pages(seg); 2145*0Sstevel@tonic-gate } 2146*0Sstevel@tonic-gate 2147*0Sstevel@tonic-gate if (error) { 2148*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; 2149*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg)) { 2150*0Sstevel@tonic-gate 2151*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)seg->s_base & 2152*0Sstevel@tonic-gate (uintptr_t)PAGEMASK); 2153*0Sstevel@tonic-gate npages = seg_pages(seg); 2154*0Sstevel@tonic-gate as_segunlock(seg, raddr, attr, mlock_map, 2155*0Sstevel@tonic-gate idx, npages); 2156*0Sstevel@tonic-gate idx += npages; 2157*0Sstevel@tonic-gate } 2158*0Sstevel@tonic-gate } 2159*0Sstevel@tonic-gate 2160*0Sstevel@tonic-gate kmem_free(mlock_map, mlock_size * sizeof (ulong_t)); 2161*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2162*0Sstevel@tonic-gate goto lockerr; 2163*0Sstevel@tonic-gate } else if (func == MC_UNLOCKAS) { 2164*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 2165*0Sstevel@tonic-gate AS_CLRPGLCK(as); 2166*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 2167*0Sstevel@tonic-gate 2168*0Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) { 2169*0Sstevel@tonic-gate error = SEGOP_LOCKOP(seg, seg->s_base, 2170*0Sstevel@tonic-gate seg->s_size, attr, MC_UNLOCK, NULL, 0); 2171*0Sstevel@tonic-gate if (error != 0) 2172*0Sstevel@tonic-gate break; 2173*0Sstevel@tonic-gate } 2174*0Sstevel@tonic-gate 2175*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2176*0Sstevel@tonic-gate goto lockerr; 2177*0Sstevel@tonic-gate } 2178*0Sstevel@tonic-gate 2179*0Sstevel@tonic-gate /* 2180*0Sstevel@tonic-gate * Normalize addresses and sizes. 2181*0Sstevel@tonic-gate */ 2182*0Sstevel@tonic-gate initraddr = raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 2183*0Sstevel@tonic-gate initrsize = rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 2184*0Sstevel@tonic-gate (size_t)raddr; 2185*0Sstevel@tonic-gate 2186*0Sstevel@tonic-gate if (raddr + rsize < raddr) { /* check for wraparound */ 2187*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2188*0Sstevel@tonic-gate return (ENOMEM); 2189*0Sstevel@tonic-gate } 2190*0Sstevel@tonic-gate 2191*0Sstevel@tonic-gate /* 2192*0Sstevel@tonic-gate * Get initial segment. 2193*0Sstevel@tonic-gate */ 2194*0Sstevel@tonic-gate if ((seg = as_segat(as, raddr)) == NULL) { 2195*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2196*0Sstevel@tonic-gate return (ENOMEM); 2197*0Sstevel@tonic-gate } 2198*0Sstevel@tonic-gate 2199*0Sstevel@tonic-gate if (func == MC_LOCK) { 2200*0Sstevel@tonic-gate mlock_size = BT_BITOUL(btopr(rsize)); 2201*0Sstevel@tonic-gate if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size * 2202*0Sstevel@tonic-gate sizeof (ulong_t), KM_NOSLEEP)) == NULL) { 2203*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2204*0Sstevel@tonic-gate return (EAGAIN); 2205*0Sstevel@tonic-gate } 2206*0Sstevel@tonic-gate } 2207*0Sstevel@tonic-gate 2208*0Sstevel@tonic-gate /* 2209*0Sstevel@tonic-gate * Loop over all segments. If a hole in the address range is 2210*0Sstevel@tonic-gate * discovered, then fail. For each segment, perform the appropriate 2211*0Sstevel@tonic-gate * control operation. 2212*0Sstevel@tonic-gate */ 2213*0Sstevel@tonic-gate while (rsize != 0) { 2214*0Sstevel@tonic-gate 2215*0Sstevel@tonic-gate /* 2216*0Sstevel@tonic-gate * Make sure there's no hole, calculate the portion 2217*0Sstevel@tonic-gate * of the next segment to be operated over. 2218*0Sstevel@tonic-gate */ 2219*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 2220*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 2221*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 2222*0Sstevel@tonic-gate if (func == MC_LOCK) { 2223*0Sstevel@tonic-gate as_unlockerr(as, attr, mlock_map, 2224*0Sstevel@tonic-gate initraddr, initrsize - rsize); 2225*0Sstevel@tonic-gate kmem_free(mlock_map, 2226*0Sstevel@tonic-gate mlock_size * sizeof (ulong_t)); 2227*0Sstevel@tonic-gate } 2228*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2229*0Sstevel@tonic-gate return (ENOMEM); 2230*0Sstevel@tonic-gate } 2231*0Sstevel@tonic-gate } 2232*0Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) 2233*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 2234*0Sstevel@tonic-gate else 2235*0Sstevel@tonic-gate ssize = rsize; 2236*0Sstevel@tonic-gate 2237*0Sstevel@tonic-gate /* 2238*0Sstevel@tonic-gate * Dispatch on specific function. 2239*0Sstevel@tonic-gate */ 2240*0Sstevel@tonic-gate switch (func) { 2241*0Sstevel@tonic-gate 2242*0Sstevel@tonic-gate /* 2243*0Sstevel@tonic-gate * Synchronize cached data from mappings with backing 2244*0Sstevel@tonic-gate * objects. 2245*0Sstevel@tonic-gate */ 2246*0Sstevel@tonic-gate case MC_SYNC: 2247*0Sstevel@tonic-gate if (error = SEGOP_SYNC(seg, raddr, ssize, 2248*0Sstevel@tonic-gate attr, (uint_t)arg)) { 2249*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2250*0Sstevel@tonic-gate return (error); 2251*0Sstevel@tonic-gate } 2252*0Sstevel@tonic-gate break; 2253*0Sstevel@tonic-gate 2254*0Sstevel@tonic-gate /* 2255*0Sstevel@tonic-gate * Lock pages in memory. 2256*0Sstevel@tonic-gate */ 2257*0Sstevel@tonic-gate case MC_LOCK: 2258*0Sstevel@tonic-gate if (error = SEGOP_LOCKOP(seg, raddr, ssize, 2259*0Sstevel@tonic-gate attr, func, mlock_map, pos)) { 2260*0Sstevel@tonic-gate as_unlockerr(as, attr, mlock_map, initraddr, 2261*0Sstevel@tonic-gate initrsize - rsize + ssize); 2262*0Sstevel@tonic-gate kmem_free(mlock_map, mlock_size * 2263*0Sstevel@tonic-gate sizeof (ulong_t)); 2264*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2265*0Sstevel@tonic-gate goto lockerr; 2266*0Sstevel@tonic-gate } 2267*0Sstevel@tonic-gate break; 2268*0Sstevel@tonic-gate 2269*0Sstevel@tonic-gate /* 2270*0Sstevel@tonic-gate * Unlock mapped pages. 2271*0Sstevel@tonic-gate */ 2272*0Sstevel@tonic-gate case MC_UNLOCK: 2273*0Sstevel@tonic-gate (void) SEGOP_LOCKOP(seg, raddr, ssize, attr, func, 2274*0Sstevel@tonic-gate (ulong_t *)NULL, (size_t)NULL); 2275*0Sstevel@tonic-gate break; 2276*0Sstevel@tonic-gate 2277*0Sstevel@tonic-gate /* 2278*0Sstevel@tonic-gate * Store VM advise for mapped pages in segment layer. 2279*0Sstevel@tonic-gate */ 2280*0Sstevel@tonic-gate case MC_ADVISE: 2281*0Sstevel@tonic-gate error = SEGOP_ADVISE(seg, raddr, ssize, (uint_t)arg); 2282*0Sstevel@tonic-gate 2283*0Sstevel@tonic-gate /* 2284*0Sstevel@tonic-gate * Check for regular errors and special retry error 2285*0Sstevel@tonic-gate */ 2286*0Sstevel@tonic-gate if (error) { 2287*0Sstevel@tonic-gate if (error == IE_RETRY) { 2288*0Sstevel@tonic-gate /* 2289*0Sstevel@tonic-gate * Need to acquire writers lock, so 2290*0Sstevel@tonic-gate * have to drop readers lock and start 2291*0Sstevel@tonic-gate * all over again 2292*0Sstevel@tonic-gate */ 2293*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2294*0Sstevel@tonic-gate goto retry; 2295*0Sstevel@tonic-gate } else if (error == IE_REATTACH) { 2296*0Sstevel@tonic-gate /* 2297*0Sstevel@tonic-gate * Find segment for current address 2298*0Sstevel@tonic-gate * because current segment just got 2299*0Sstevel@tonic-gate * split or concatenated 2300*0Sstevel@tonic-gate */ 2301*0Sstevel@tonic-gate seg = as_segat(as, raddr); 2302*0Sstevel@tonic-gate if (seg == NULL) { 2303*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2304*0Sstevel@tonic-gate return (ENOMEM); 2305*0Sstevel@tonic-gate } 2306*0Sstevel@tonic-gate } else { 2307*0Sstevel@tonic-gate /* 2308*0Sstevel@tonic-gate * Regular error 2309*0Sstevel@tonic-gate */ 2310*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2311*0Sstevel@tonic-gate return (error); 2312*0Sstevel@tonic-gate } 2313*0Sstevel@tonic-gate } 2314*0Sstevel@tonic-gate break; 2315*0Sstevel@tonic-gate 2316*0Sstevel@tonic-gate /* 2317*0Sstevel@tonic-gate * Can't happen. 2318*0Sstevel@tonic-gate */ 2319*0Sstevel@tonic-gate default: 2320*0Sstevel@tonic-gate panic("as_ctl: bad operation %d", func); 2321*0Sstevel@tonic-gate /*NOTREACHED*/ 2322*0Sstevel@tonic-gate } 2323*0Sstevel@tonic-gate 2324*0Sstevel@tonic-gate rsize -= ssize; 2325*0Sstevel@tonic-gate raddr += ssize; 2326*0Sstevel@tonic-gate } 2327*0Sstevel@tonic-gate 2328*0Sstevel@tonic-gate if (func == MC_LOCK) 2329*0Sstevel@tonic-gate kmem_free(mlock_map, mlock_size * sizeof (ulong_t)); 2330*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2331*0Sstevel@tonic-gate return (0); 2332*0Sstevel@tonic-gate lockerr: 2333*0Sstevel@tonic-gate 2334*0Sstevel@tonic-gate /* 2335*0Sstevel@tonic-gate * If the lower levels returned EDEADLK for a segment lockop, 2336*0Sstevel@tonic-gate * it means that we should retry the operation. Let's wait 2337*0Sstevel@tonic-gate * a bit also to let the deadlock causing condition clear. 2338*0Sstevel@tonic-gate * This is part of a gross hack to work around a design flaw 2339*0Sstevel@tonic-gate * in the ufs/sds logging code and should go away when the 2340*0Sstevel@tonic-gate * logging code is re-designed to fix the problem. See bug 2341*0Sstevel@tonic-gate * 4125102 for details of the problem. 2342*0Sstevel@tonic-gate */ 2343*0Sstevel@tonic-gate if (error == EDEADLK) { 2344*0Sstevel@tonic-gate delay(deadlk_wait); 2345*0Sstevel@tonic-gate error = 0; 2346*0Sstevel@tonic-gate goto retry; 2347*0Sstevel@tonic-gate } 2348*0Sstevel@tonic-gate return (error); 2349*0Sstevel@tonic-gate } 2350*0Sstevel@tonic-gate 2351*0Sstevel@tonic-gate /* 2352*0Sstevel@tonic-gate * Special code for exec to move the stack segment from its interim 2353*0Sstevel@tonic-gate * place in the old address to the right place in the new address space. 2354*0Sstevel@tonic-gate */ 2355*0Sstevel@tonic-gate /*ARGSUSED*/ 2356*0Sstevel@tonic-gate int 2357*0Sstevel@tonic-gate as_exec(struct as *oas, caddr_t ostka, size_t stksz, 2358*0Sstevel@tonic-gate struct as *nas, caddr_t nstka, uint_t hatflag) 2359*0Sstevel@tonic-gate { 2360*0Sstevel@tonic-gate struct seg *stkseg; 2361*0Sstevel@tonic-gate 2362*0Sstevel@tonic-gate AS_LOCK_ENTER(oas, &oas->a_lock, RW_WRITER); 2363*0Sstevel@tonic-gate stkseg = as_segat(oas, ostka); 2364*0Sstevel@tonic-gate stkseg = as_removeseg(oas, stkseg); 2365*0Sstevel@tonic-gate ASSERT(stkseg != NULL); 2366*0Sstevel@tonic-gate ASSERT(stkseg->s_base == ostka && stkseg->s_size == stksz); 2367*0Sstevel@tonic-gate stkseg->s_as = nas; 2368*0Sstevel@tonic-gate stkseg->s_base = nstka; 2369*0Sstevel@tonic-gate 2370*0Sstevel@tonic-gate /* 2371*0Sstevel@tonic-gate * It's ok to lock the address space we are about to exec to. 2372*0Sstevel@tonic-gate */ 2373*0Sstevel@tonic-gate AS_LOCK_ENTER(nas, &nas->a_lock, RW_WRITER); 2374*0Sstevel@tonic-gate ASSERT(avl_numnodes(&nas->a_wpage) == 0); 2375*0Sstevel@tonic-gate nas->a_size += stkseg->s_size; 2376*0Sstevel@tonic-gate oas->a_size -= stkseg->s_size; 2377*0Sstevel@tonic-gate (void) as_addseg(nas, stkseg); 2378*0Sstevel@tonic-gate AS_LOCK_EXIT(nas, &nas->a_lock); 2379*0Sstevel@tonic-gate AS_LOCK_EXIT(oas, &oas->a_lock); 2380*0Sstevel@tonic-gate return (0); 2381*0Sstevel@tonic-gate } 2382*0Sstevel@tonic-gate 2383*0Sstevel@tonic-gate static int 2384*0Sstevel@tonic-gate f_decode(faultcode_t fault_err) 2385*0Sstevel@tonic-gate { 2386*0Sstevel@tonic-gate int error = 0; 2387*0Sstevel@tonic-gate 2388*0Sstevel@tonic-gate switch (FC_CODE(fault_err)) { 2389*0Sstevel@tonic-gate case FC_OBJERR: 2390*0Sstevel@tonic-gate error = FC_ERRNO(fault_err); 2391*0Sstevel@tonic-gate break; 2392*0Sstevel@tonic-gate case FC_PROT: 2393*0Sstevel@tonic-gate error = EACCES; 2394*0Sstevel@tonic-gate break; 2395*0Sstevel@tonic-gate default: 2396*0Sstevel@tonic-gate error = EFAULT; 2397*0Sstevel@tonic-gate break; 2398*0Sstevel@tonic-gate } 2399*0Sstevel@tonic-gate return (error); 2400*0Sstevel@tonic-gate } 2401*0Sstevel@tonic-gate 2402*0Sstevel@tonic-gate /* 2403*0Sstevel@tonic-gate * lock pages in a given address space. Return shadow list. If 2404*0Sstevel@tonic-gate * the list is NULL, the MMU mapping is also locked. 2405*0Sstevel@tonic-gate */ 2406*0Sstevel@tonic-gate int 2407*0Sstevel@tonic-gate as_pagelock(struct as *as, struct page ***ppp, caddr_t addr, 2408*0Sstevel@tonic-gate size_t size, enum seg_rw rw) 2409*0Sstevel@tonic-gate { 2410*0Sstevel@tonic-gate size_t rsize; 2411*0Sstevel@tonic-gate caddr_t base; 2412*0Sstevel@tonic-gate caddr_t raddr; 2413*0Sstevel@tonic-gate faultcode_t fault_err; 2414*0Sstevel@tonic-gate struct seg *seg; 2415*0Sstevel@tonic-gate int res; 2416*0Sstevel@tonic-gate int prefaulted = 0; 2417*0Sstevel@tonic-gate 2418*0Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_START, 2419*0Sstevel@tonic-gate "as_pagelock_start: addr %p size %ld", addr, size); 2420*0Sstevel@tonic-gate 2421*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 2422*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 2423*0Sstevel@tonic-gate (size_t)raddr; 2424*0Sstevel@tonic-gate top: 2425*0Sstevel@tonic-gate /* 2426*0Sstevel@tonic-gate * if the request crosses two segments let 2427*0Sstevel@tonic-gate * as_fault handle it. 2428*0Sstevel@tonic-gate */ 2429*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2430*0Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 2431*0Sstevel@tonic-gate if ((seg == NULL) || ((base = seg->s_base) > addr) || 2432*0Sstevel@tonic-gate (addr + size) > base + seg->s_size) { 2433*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2434*0Sstevel@tonic-gate goto slow; 2435*0Sstevel@tonic-gate } 2436*0Sstevel@tonic-gate 2437*0Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_START, 2438*0Sstevel@tonic-gate "seg_lock_1_start: raddr %p rsize %ld", raddr, rsize); 2439*0Sstevel@tonic-gate 2440*0Sstevel@tonic-gate /* 2441*0Sstevel@tonic-gate * try to lock pages and pass back shadow list 2442*0Sstevel@tonic-gate */ 2443*0Sstevel@tonic-gate res = SEGOP_PAGELOCK(seg, raddr, rsize, ppp, L_PAGELOCK, rw); 2444*0Sstevel@tonic-gate 2445*0Sstevel@tonic-gate TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_END, "seg_lock_1_end"); 2446*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2447*0Sstevel@tonic-gate if (res == 0) { 2448*0Sstevel@tonic-gate return (0); 2449*0Sstevel@tonic-gate } else if (res == ENOTSUP || prefaulted) { 2450*0Sstevel@tonic-gate /* 2451*0Sstevel@tonic-gate * (1) segment driver doesn't support PAGELOCK fastpath, or 2452*0Sstevel@tonic-gate * (2) we've already tried fast path unsuccessfully after 2453*0Sstevel@tonic-gate * faulting in the addr range below; system might be 2454*0Sstevel@tonic-gate * thrashing or there may not be enough availrmem. 2455*0Sstevel@tonic-gate */ 2456*0Sstevel@tonic-gate goto slow; 2457*0Sstevel@tonic-gate } 2458*0Sstevel@tonic-gate 2459*0Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_FAULT_START, 2460*0Sstevel@tonic-gate "as_fault_start: addr %p size %ld", addr, size); 2461*0Sstevel@tonic-gate 2462*0Sstevel@tonic-gate /* 2463*0Sstevel@tonic-gate * we might get here because of some COW fault or non 2464*0Sstevel@tonic-gate * existing page. Let as_fault deal with it. Just load 2465*0Sstevel@tonic-gate * the page, don't lock the MMU mapping. 2466*0Sstevel@tonic-gate */ 2467*0Sstevel@tonic-gate fault_err = as_fault(as->a_hat, as, addr, size, F_INVAL, rw); 2468*0Sstevel@tonic-gate if (fault_err != 0) { 2469*0Sstevel@tonic-gate return (f_decode(fault_err)); 2470*0Sstevel@tonic-gate } 2471*0Sstevel@tonic-gate 2472*0Sstevel@tonic-gate prefaulted = 1; 2473*0Sstevel@tonic-gate 2474*0Sstevel@tonic-gate /* 2475*0Sstevel@tonic-gate * try fast path again; since we've dropped a_lock, 2476*0Sstevel@tonic-gate * we need to try the dance from the start to see if 2477*0Sstevel@tonic-gate * the addr range is still valid. 2478*0Sstevel@tonic-gate */ 2479*0Sstevel@tonic-gate goto top; 2480*0Sstevel@tonic-gate slow: 2481*0Sstevel@tonic-gate /* 2482*0Sstevel@tonic-gate * load the page and lock the MMU mapping. 2483*0Sstevel@tonic-gate */ 2484*0Sstevel@tonic-gate fault_err = as_fault(as->a_hat, as, addr, size, F_SOFTLOCK, rw); 2485*0Sstevel@tonic-gate if (fault_err != 0) { 2486*0Sstevel@tonic-gate return (f_decode(fault_err)); 2487*0Sstevel@tonic-gate } 2488*0Sstevel@tonic-gate *ppp = NULL; 2489*0Sstevel@tonic-gate 2490*0Sstevel@tonic-gate TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_END, "as_pagelock_end"); 2491*0Sstevel@tonic-gate return (0); 2492*0Sstevel@tonic-gate } 2493*0Sstevel@tonic-gate 2494*0Sstevel@tonic-gate /* 2495*0Sstevel@tonic-gate * unlock pages in a given address range 2496*0Sstevel@tonic-gate */ 2497*0Sstevel@tonic-gate void 2498*0Sstevel@tonic-gate as_pageunlock(struct as *as, struct page **pp, caddr_t addr, size_t size, 2499*0Sstevel@tonic-gate enum seg_rw rw) 2500*0Sstevel@tonic-gate { 2501*0Sstevel@tonic-gate struct seg *seg; 2502*0Sstevel@tonic-gate size_t rsize; 2503*0Sstevel@tonic-gate caddr_t raddr; 2504*0Sstevel@tonic-gate 2505*0Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_START, 2506*0Sstevel@tonic-gate "as_pageunlock_start: addr %p size %ld", addr, size); 2507*0Sstevel@tonic-gate 2508*0Sstevel@tonic-gate /* 2509*0Sstevel@tonic-gate * if the shadow list is NULL, as_pagelock was 2510*0Sstevel@tonic-gate * falling back to as_fault 2511*0Sstevel@tonic-gate */ 2512*0Sstevel@tonic-gate if (pp == NULL) { 2513*0Sstevel@tonic-gate (void) as_fault(as->a_hat, as, addr, size, F_SOFTUNLOCK, rw); 2514*0Sstevel@tonic-gate return; 2515*0Sstevel@tonic-gate } 2516*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 2517*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 2518*0Sstevel@tonic-gate (size_t)raddr; 2519*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2520*0Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 2521*0Sstevel@tonic-gate ASSERT(seg); 2522*0Sstevel@tonic-gate TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_UNLOCK_START, 2523*0Sstevel@tonic-gate "seg_unlock_start: raddr %p rsize %ld", raddr, rsize); 2524*0Sstevel@tonic-gate SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGEUNLOCK, rw); 2525*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2526*0Sstevel@tonic-gate TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_END, "as_pageunlock_end"); 2527*0Sstevel@tonic-gate } 2528*0Sstevel@tonic-gate 2529*0Sstevel@tonic-gate /* 2530*0Sstevel@tonic-gate * reclaim cached pages in a given address range 2531*0Sstevel@tonic-gate */ 2532*0Sstevel@tonic-gate void 2533*0Sstevel@tonic-gate as_pagereclaim(struct as *as, struct page **pp, caddr_t addr, 2534*0Sstevel@tonic-gate size_t size, enum seg_rw rw) 2535*0Sstevel@tonic-gate { 2536*0Sstevel@tonic-gate struct seg *seg; 2537*0Sstevel@tonic-gate size_t rsize; 2538*0Sstevel@tonic-gate caddr_t raddr; 2539*0Sstevel@tonic-gate 2540*0Sstevel@tonic-gate ASSERT(AS_READ_HELD(as, &as->a_lock)); 2541*0Sstevel@tonic-gate ASSERT(pp != NULL); 2542*0Sstevel@tonic-gate 2543*0Sstevel@tonic-gate raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 2544*0Sstevel@tonic-gate rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) - 2545*0Sstevel@tonic-gate (size_t)raddr; 2546*0Sstevel@tonic-gate seg = as_findseg(as, addr, 0); 2547*0Sstevel@tonic-gate ASSERT(seg); 2548*0Sstevel@tonic-gate SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGERECLAIM, rw); 2549*0Sstevel@tonic-gate } 2550*0Sstevel@tonic-gate 2551*0Sstevel@tonic-gate #define MAXPAGEFLIP 4 2552*0Sstevel@tonic-gate #define MAXPAGEFLIPSIZ MAXPAGEFLIP*PAGESIZE 2553*0Sstevel@tonic-gate 2554*0Sstevel@tonic-gate int 2555*0Sstevel@tonic-gate as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc, 2556*0Sstevel@tonic-gate boolean_t wait) 2557*0Sstevel@tonic-gate { 2558*0Sstevel@tonic-gate struct seg *seg; 2559*0Sstevel@tonic-gate size_t ssize; 2560*0Sstevel@tonic-gate caddr_t raddr; /* rounded down addr */ 2561*0Sstevel@tonic-gate size_t rsize; /* rounded up size */ 2562*0Sstevel@tonic-gate int error = 0; 2563*0Sstevel@tonic-gate size_t pgsz = page_get_pagesize(szc); 2564*0Sstevel@tonic-gate 2565*0Sstevel@tonic-gate setpgsz_top: 2566*0Sstevel@tonic-gate if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(size, pgsz)) { 2567*0Sstevel@tonic-gate return (EINVAL); 2568*0Sstevel@tonic-gate } 2569*0Sstevel@tonic-gate 2570*0Sstevel@tonic-gate raddr = addr; 2571*0Sstevel@tonic-gate rsize = size; 2572*0Sstevel@tonic-gate 2573*0Sstevel@tonic-gate if (raddr + rsize < raddr) /* check for wraparound */ 2574*0Sstevel@tonic-gate return (ENOMEM); 2575*0Sstevel@tonic-gate 2576*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 2577*0Sstevel@tonic-gate as_clearwatchprot(as, raddr, rsize); 2578*0Sstevel@tonic-gate seg = as_segat(as, raddr); 2579*0Sstevel@tonic-gate if (seg == NULL) { 2580*0Sstevel@tonic-gate as_setwatch(as); 2581*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2582*0Sstevel@tonic-gate return (ENOMEM); 2583*0Sstevel@tonic-gate } 2584*0Sstevel@tonic-gate 2585*0Sstevel@tonic-gate for (; rsize != 0; rsize -= ssize, raddr += ssize) { 2586*0Sstevel@tonic-gate if (raddr >= seg->s_base + seg->s_size) { 2587*0Sstevel@tonic-gate seg = AS_SEGNEXT(as, seg); 2588*0Sstevel@tonic-gate if (seg == NULL || raddr != seg->s_base) { 2589*0Sstevel@tonic-gate error = ENOMEM; 2590*0Sstevel@tonic-gate break; 2591*0Sstevel@tonic-gate } 2592*0Sstevel@tonic-gate } 2593*0Sstevel@tonic-gate if ((raddr + rsize) > (seg->s_base + seg->s_size)) { 2594*0Sstevel@tonic-gate ssize = seg->s_base + seg->s_size - raddr; 2595*0Sstevel@tonic-gate } else { 2596*0Sstevel@tonic-gate ssize = rsize; 2597*0Sstevel@tonic-gate } 2598*0Sstevel@tonic-gate 2599*0Sstevel@tonic-gate error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc); 2600*0Sstevel@tonic-gate 2601*0Sstevel@tonic-gate if (error == IE_NOMEM) { 2602*0Sstevel@tonic-gate error = EAGAIN; 2603*0Sstevel@tonic-gate break; 2604*0Sstevel@tonic-gate } 2605*0Sstevel@tonic-gate 2606*0Sstevel@tonic-gate if (error == IE_RETRY) { 2607*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2608*0Sstevel@tonic-gate goto setpgsz_top; 2609*0Sstevel@tonic-gate } 2610*0Sstevel@tonic-gate 2611*0Sstevel@tonic-gate if (error == ENOTSUP) { 2612*0Sstevel@tonic-gate error = EINVAL; 2613*0Sstevel@tonic-gate break; 2614*0Sstevel@tonic-gate } 2615*0Sstevel@tonic-gate 2616*0Sstevel@tonic-gate if (wait && (error == EAGAIN)) { 2617*0Sstevel@tonic-gate /* 2618*0Sstevel@tonic-gate * Memory is currently locked. It must be unlocked 2619*0Sstevel@tonic-gate * before this operation can succeed through a retry. 2620*0Sstevel@tonic-gate * The possible reasons for locked memory and 2621*0Sstevel@tonic-gate * corresponding strategies for unlocking are: 2622*0Sstevel@tonic-gate * (1) Normal I/O 2623*0Sstevel@tonic-gate * wait for a signal that the I/O operation 2624*0Sstevel@tonic-gate * has completed and the memory is unlocked. 2625*0Sstevel@tonic-gate * (2) Asynchronous I/O 2626*0Sstevel@tonic-gate * The aio subsystem does not unlock pages when 2627*0Sstevel@tonic-gate * the I/O is completed. Those pages are unlocked 2628*0Sstevel@tonic-gate * when the application calls aiowait/aioerror. 2629*0Sstevel@tonic-gate * So, to prevent blocking forever, cv_broadcast() 2630*0Sstevel@tonic-gate * is done to wake up aio_cleanup_thread. 2631*0Sstevel@tonic-gate * Subsequently, segvn_reclaim will be called, and 2632*0Sstevel@tonic-gate * that will do AS_CLRUNMAPWAIT() and wake us up. 2633*0Sstevel@tonic-gate * (3) Long term page locking: 2634*0Sstevel@tonic-gate * This is not relevant for as_setpagesize() 2635*0Sstevel@tonic-gate * because we cannot change the page size for 2636*0Sstevel@tonic-gate * driver memory. The attempt to do so will 2637*0Sstevel@tonic-gate * fail with a different error than EAGAIN so 2638*0Sstevel@tonic-gate * there's no need to trigger as callbacks like 2639*0Sstevel@tonic-gate * as_unmap, as_setprot or as_free would do. 2640*0Sstevel@tonic-gate */ 2641*0Sstevel@tonic-gate mutex_enter(&as->a_contents); 2642*0Sstevel@tonic-gate if (AS_ISUNMAPWAIT(as) == 0) { 2643*0Sstevel@tonic-gate cv_broadcast(&as->a_cv); 2644*0Sstevel@tonic-gate } 2645*0Sstevel@tonic-gate AS_SETUNMAPWAIT(as); 2646*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2647*0Sstevel@tonic-gate while (AS_ISUNMAPWAIT(as)) { 2648*0Sstevel@tonic-gate cv_wait(&as->a_cv, &as->a_contents); 2649*0Sstevel@tonic-gate } 2650*0Sstevel@tonic-gate mutex_exit(&as->a_contents); 2651*0Sstevel@tonic-gate goto setpgsz_top; 2652*0Sstevel@tonic-gate } else if (error != 0) { 2653*0Sstevel@tonic-gate break; 2654*0Sstevel@tonic-gate } 2655*0Sstevel@tonic-gate } 2656*0Sstevel@tonic-gate as_setwatch(as); 2657*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2658*0Sstevel@tonic-gate return (error); 2659*0Sstevel@tonic-gate } 2660*0Sstevel@tonic-gate 2661*0Sstevel@tonic-gate /* 2662*0Sstevel@tonic-gate * Setup all of the uninitialized watched pages that we can. 2663*0Sstevel@tonic-gate */ 2664*0Sstevel@tonic-gate void 2665*0Sstevel@tonic-gate as_setwatch(struct as *as) 2666*0Sstevel@tonic-gate { 2667*0Sstevel@tonic-gate struct watched_page *pwp; 2668*0Sstevel@tonic-gate struct seg *seg; 2669*0Sstevel@tonic-gate caddr_t vaddr; 2670*0Sstevel@tonic-gate uint_t prot; 2671*0Sstevel@tonic-gate int err, retrycnt; 2672*0Sstevel@tonic-gate 2673*0Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 2674*0Sstevel@tonic-gate return; 2675*0Sstevel@tonic-gate 2676*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 2677*0Sstevel@tonic-gate 2678*0Sstevel@tonic-gate for (pwp = avl_first(&as->a_wpage); pwp != NULL; 2679*0Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp)) { 2680*0Sstevel@tonic-gate retrycnt = 0; 2681*0Sstevel@tonic-gate retry: 2682*0Sstevel@tonic-gate vaddr = pwp->wp_vaddr; 2683*0Sstevel@tonic-gate if (pwp->wp_oprot != 0 || /* already set up */ 2684*0Sstevel@tonic-gate (seg = as_segat(as, vaddr)) == NULL || 2685*0Sstevel@tonic-gate SEGOP_GETPROT(seg, vaddr, 0, &prot) != 0) 2686*0Sstevel@tonic-gate continue; 2687*0Sstevel@tonic-gate 2688*0Sstevel@tonic-gate pwp->wp_oprot = prot; 2689*0Sstevel@tonic-gate if (pwp->wp_read) 2690*0Sstevel@tonic-gate prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 2691*0Sstevel@tonic-gate if (pwp->wp_write) 2692*0Sstevel@tonic-gate prot &= ~PROT_WRITE; 2693*0Sstevel@tonic-gate if (pwp->wp_exec) 2694*0Sstevel@tonic-gate prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 2695*0Sstevel@tonic-gate if (!(pwp->wp_flags & WP_NOWATCH) && prot != pwp->wp_oprot) { 2696*0Sstevel@tonic-gate err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot); 2697*0Sstevel@tonic-gate if (err == IE_RETRY) { 2698*0Sstevel@tonic-gate pwp->wp_oprot = 0; 2699*0Sstevel@tonic-gate ASSERT(retrycnt == 0); 2700*0Sstevel@tonic-gate retrycnt++; 2701*0Sstevel@tonic-gate goto retry; 2702*0Sstevel@tonic-gate } 2703*0Sstevel@tonic-gate } 2704*0Sstevel@tonic-gate pwp->wp_prot = prot; 2705*0Sstevel@tonic-gate } 2706*0Sstevel@tonic-gate } 2707*0Sstevel@tonic-gate 2708*0Sstevel@tonic-gate /* 2709*0Sstevel@tonic-gate * Clear all of the watched pages in the address space. 2710*0Sstevel@tonic-gate */ 2711*0Sstevel@tonic-gate void 2712*0Sstevel@tonic-gate as_clearwatch(struct as *as) 2713*0Sstevel@tonic-gate { 2714*0Sstevel@tonic-gate struct watched_page *pwp; 2715*0Sstevel@tonic-gate struct seg *seg; 2716*0Sstevel@tonic-gate caddr_t vaddr; 2717*0Sstevel@tonic-gate uint_t prot; 2718*0Sstevel@tonic-gate int err, retrycnt; 2719*0Sstevel@tonic-gate 2720*0Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 2721*0Sstevel@tonic-gate return; 2722*0Sstevel@tonic-gate 2723*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 2724*0Sstevel@tonic-gate 2725*0Sstevel@tonic-gate for (pwp = avl_first(&as->a_wpage); pwp != NULL; 2726*0Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp)) { 2727*0Sstevel@tonic-gate retrycnt = 0; 2728*0Sstevel@tonic-gate retry: 2729*0Sstevel@tonic-gate vaddr = pwp->wp_vaddr; 2730*0Sstevel@tonic-gate if (pwp->wp_oprot == 0 || /* not set up */ 2731*0Sstevel@tonic-gate (seg = as_segat(as, vaddr)) == NULL) 2732*0Sstevel@tonic-gate continue; 2733*0Sstevel@tonic-gate 2734*0Sstevel@tonic-gate if ((prot = pwp->wp_oprot) != pwp->wp_prot) { 2735*0Sstevel@tonic-gate err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot); 2736*0Sstevel@tonic-gate if (err == IE_RETRY) { 2737*0Sstevel@tonic-gate ASSERT(retrycnt == 0); 2738*0Sstevel@tonic-gate retrycnt++; 2739*0Sstevel@tonic-gate goto retry; 2740*0Sstevel@tonic-gate } 2741*0Sstevel@tonic-gate } 2742*0Sstevel@tonic-gate pwp->wp_oprot = 0; 2743*0Sstevel@tonic-gate pwp->wp_prot = 0; 2744*0Sstevel@tonic-gate } 2745*0Sstevel@tonic-gate } 2746*0Sstevel@tonic-gate 2747*0Sstevel@tonic-gate /* 2748*0Sstevel@tonic-gate * Force a new setup for all the watched pages in the range. 2749*0Sstevel@tonic-gate */ 2750*0Sstevel@tonic-gate static void 2751*0Sstevel@tonic-gate as_setwatchprot(struct as *as, caddr_t addr, size_t size, uint_t prot) 2752*0Sstevel@tonic-gate { 2753*0Sstevel@tonic-gate struct watched_page *pwp; 2754*0Sstevel@tonic-gate struct watched_page tpw; 2755*0Sstevel@tonic-gate caddr_t eaddr = addr + size; 2756*0Sstevel@tonic-gate caddr_t vaddr; 2757*0Sstevel@tonic-gate struct seg *seg; 2758*0Sstevel@tonic-gate int err, retrycnt; 2759*0Sstevel@tonic-gate uint_t wprot; 2760*0Sstevel@tonic-gate avl_index_t where; 2761*0Sstevel@tonic-gate 2762*0Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 2763*0Sstevel@tonic-gate return; 2764*0Sstevel@tonic-gate 2765*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 2766*0Sstevel@tonic-gate 2767*0Sstevel@tonic-gate tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 2768*0Sstevel@tonic-gate if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL) 2769*0Sstevel@tonic-gate pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER); 2770*0Sstevel@tonic-gate 2771*0Sstevel@tonic-gate while (pwp != NULL && pwp->wp_vaddr < eaddr) { 2772*0Sstevel@tonic-gate retrycnt = 0; 2773*0Sstevel@tonic-gate vaddr = pwp->wp_vaddr; 2774*0Sstevel@tonic-gate 2775*0Sstevel@tonic-gate wprot = prot; 2776*0Sstevel@tonic-gate if (pwp->wp_read) 2777*0Sstevel@tonic-gate wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 2778*0Sstevel@tonic-gate if (pwp->wp_write) 2779*0Sstevel@tonic-gate wprot &= ~PROT_WRITE; 2780*0Sstevel@tonic-gate if (pwp->wp_exec) 2781*0Sstevel@tonic-gate wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 2782*0Sstevel@tonic-gate if (!(pwp->wp_flags & WP_NOWATCH) && wprot != pwp->wp_oprot) { 2783*0Sstevel@tonic-gate retry: 2784*0Sstevel@tonic-gate seg = as_segat(as, vaddr); 2785*0Sstevel@tonic-gate if (seg == NULL) { 2786*0Sstevel@tonic-gate panic("as_setwatchprot: no seg"); 2787*0Sstevel@tonic-gate /*NOTREACHED*/ 2788*0Sstevel@tonic-gate } 2789*0Sstevel@tonic-gate err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, wprot); 2790*0Sstevel@tonic-gate if (err == IE_RETRY) { 2791*0Sstevel@tonic-gate ASSERT(retrycnt == 0); 2792*0Sstevel@tonic-gate retrycnt++; 2793*0Sstevel@tonic-gate goto retry; 2794*0Sstevel@tonic-gate } 2795*0Sstevel@tonic-gate } 2796*0Sstevel@tonic-gate pwp->wp_oprot = prot; 2797*0Sstevel@tonic-gate pwp->wp_prot = wprot; 2798*0Sstevel@tonic-gate 2799*0Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp); 2800*0Sstevel@tonic-gate } 2801*0Sstevel@tonic-gate } 2802*0Sstevel@tonic-gate 2803*0Sstevel@tonic-gate /* 2804*0Sstevel@tonic-gate * Clear all of the watched pages in the range. 2805*0Sstevel@tonic-gate */ 2806*0Sstevel@tonic-gate static void 2807*0Sstevel@tonic-gate as_clearwatchprot(struct as *as, caddr_t addr, size_t size) 2808*0Sstevel@tonic-gate { 2809*0Sstevel@tonic-gate caddr_t eaddr = addr + size; 2810*0Sstevel@tonic-gate struct watched_page *pwp; 2811*0Sstevel@tonic-gate struct watched_page tpw; 2812*0Sstevel@tonic-gate uint_t prot; 2813*0Sstevel@tonic-gate struct seg *seg; 2814*0Sstevel@tonic-gate int err, retrycnt; 2815*0Sstevel@tonic-gate avl_index_t where; 2816*0Sstevel@tonic-gate 2817*0Sstevel@tonic-gate if (avl_numnodes(&as->a_wpage) == 0) 2818*0Sstevel@tonic-gate return; 2819*0Sstevel@tonic-gate 2820*0Sstevel@tonic-gate tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 2821*0Sstevel@tonic-gate if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL) 2822*0Sstevel@tonic-gate pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER); 2823*0Sstevel@tonic-gate 2824*0Sstevel@tonic-gate ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 2825*0Sstevel@tonic-gate 2826*0Sstevel@tonic-gate while (pwp != NULL && pwp->wp_vaddr < eaddr) { 2827*0Sstevel@tonic-gate ASSERT(addr >= pwp->wp_vaddr); 2828*0Sstevel@tonic-gate 2829*0Sstevel@tonic-gate if ((prot = pwp->wp_oprot) != 0) { 2830*0Sstevel@tonic-gate retrycnt = 0; 2831*0Sstevel@tonic-gate 2832*0Sstevel@tonic-gate if (prot != pwp->wp_prot) { 2833*0Sstevel@tonic-gate retry: 2834*0Sstevel@tonic-gate seg = as_segat(as, pwp->wp_vaddr); 2835*0Sstevel@tonic-gate if (seg == NULL) 2836*0Sstevel@tonic-gate continue; 2837*0Sstevel@tonic-gate err = SEGOP_SETPROT(seg, pwp->wp_vaddr, 2838*0Sstevel@tonic-gate PAGESIZE, prot); 2839*0Sstevel@tonic-gate if (err == IE_RETRY) { 2840*0Sstevel@tonic-gate ASSERT(retrycnt == 0); 2841*0Sstevel@tonic-gate retrycnt++; 2842*0Sstevel@tonic-gate goto retry; 2843*0Sstevel@tonic-gate 2844*0Sstevel@tonic-gate } 2845*0Sstevel@tonic-gate } 2846*0Sstevel@tonic-gate pwp->wp_oprot = 0; 2847*0Sstevel@tonic-gate pwp->wp_prot = 0; 2848*0Sstevel@tonic-gate } 2849*0Sstevel@tonic-gate 2850*0Sstevel@tonic-gate pwp = AVL_NEXT(&as->a_wpage, pwp); 2851*0Sstevel@tonic-gate } 2852*0Sstevel@tonic-gate } 2853*0Sstevel@tonic-gate 2854*0Sstevel@tonic-gate void 2855*0Sstevel@tonic-gate as_signal_proc(struct as *as, k_siginfo_t *siginfo) 2856*0Sstevel@tonic-gate { 2857*0Sstevel@tonic-gate struct proc *p; 2858*0Sstevel@tonic-gate 2859*0Sstevel@tonic-gate mutex_enter(&pidlock); 2860*0Sstevel@tonic-gate for (p = practive; p; p = p->p_next) { 2861*0Sstevel@tonic-gate if (p->p_as == as) { 2862*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 2863*0Sstevel@tonic-gate if (p->p_as == as) 2864*0Sstevel@tonic-gate sigaddq(p, NULL, siginfo, KM_NOSLEEP); 2865*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 2866*0Sstevel@tonic-gate } 2867*0Sstevel@tonic-gate } 2868*0Sstevel@tonic-gate mutex_exit(&pidlock); 2869*0Sstevel@tonic-gate } 2870*0Sstevel@tonic-gate 2871*0Sstevel@tonic-gate /* 2872*0Sstevel@tonic-gate * return memory object ID 2873*0Sstevel@tonic-gate */ 2874*0Sstevel@tonic-gate int 2875*0Sstevel@tonic-gate as_getmemid(struct as *as, caddr_t addr, memid_t *memidp) 2876*0Sstevel@tonic-gate { 2877*0Sstevel@tonic-gate struct seg *seg; 2878*0Sstevel@tonic-gate int sts; 2879*0Sstevel@tonic-gate 2880*0Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2881*0Sstevel@tonic-gate seg = as_segat(as, addr); 2882*0Sstevel@tonic-gate if (seg == NULL) { 2883*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2884*0Sstevel@tonic-gate return (EFAULT); 2885*0Sstevel@tonic-gate } 2886*0Sstevel@tonic-gate /* 2887*0Sstevel@tonic-gate * catch old drivers which may not support getmemid 2888*0Sstevel@tonic-gate */ 2889*0Sstevel@tonic-gate if (seg->s_ops->getmemid == NULL) { 2890*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2891*0Sstevel@tonic-gate return (ENODEV); 2892*0Sstevel@tonic-gate } 2893*0Sstevel@tonic-gate 2894*0Sstevel@tonic-gate sts = SEGOP_GETMEMID(seg, addr, memidp); 2895*0Sstevel@tonic-gate 2896*0Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 2897*0Sstevel@tonic-gate return (sts); 2898*0Sstevel@tonic-gate } 2899