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