xref: /onnv-gate/usr/src/uts/common/vm/vm_as.c (revision 10169:116daeae7223)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51899Svsakar  * Common Development and Distribution License (the "License").
61899Svsakar  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229121SVamsi.Krishna@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * VM - address spaces.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/t_lock.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <sys/errno.h>
470Sstevel@tonic-gate #include <sys/systm.h>
480Sstevel@tonic-gate #include <sys/mman.h>
490Sstevel@tonic-gate #include <sys/sysmacros.h>
500Sstevel@tonic-gate #include <sys/cpuvar.h>
510Sstevel@tonic-gate #include <sys/sysinfo.h>
520Sstevel@tonic-gate #include <sys/kmem.h>
530Sstevel@tonic-gate #include <sys/vnode.h>
540Sstevel@tonic-gate #include <sys/vmsystm.h>
550Sstevel@tonic-gate #include <sys/cmn_err.h>
560Sstevel@tonic-gate #include <sys/debug.h>
570Sstevel@tonic-gate #include <sys/tnf_probe.h>
580Sstevel@tonic-gate #include <sys/vtrace.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <vm/hat.h>
610Sstevel@tonic-gate #include <vm/xhat.h>
620Sstevel@tonic-gate #include <vm/as.h>
630Sstevel@tonic-gate #include <vm/seg.h>
640Sstevel@tonic-gate #include <vm/seg_vn.h>
650Sstevel@tonic-gate #include <vm/seg_dev.h>
660Sstevel@tonic-gate #include <vm/seg_kmem.h>
670Sstevel@tonic-gate #include <vm/seg_map.h>
680Sstevel@tonic-gate #include <vm/seg_spt.h>
690Sstevel@tonic-gate #include <vm/page.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate clock_t deadlk_wait = 1; /* number of ticks to wait before retrying */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static struct kmem_cache *as_cache;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static void as_setwatchprot(struct as *, caddr_t, size_t, uint_t);
760Sstevel@tonic-gate static void as_clearwatchprot(struct as *, caddr_t, size_t);
771899Svsakar int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Verifying the segment lists is very time-consuming; it may not be
820Sstevel@tonic-gate  * desirable always to define VERIFY_SEGLIST when DEBUG is set.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #ifdef DEBUG
850Sstevel@tonic-gate #define	VERIFY_SEGLIST
860Sstevel@tonic-gate int do_as_verify = 0;
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Allocate a new callback data structure entry and fill in the events of
910Sstevel@tonic-gate  * interest, the address range of interest, and the callback argument.
920Sstevel@tonic-gate  * Link the entry on the as->a_callbacks list. A callback entry for the
930Sstevel@tonic-gate  * entire address space may be specified with vaddr = 0 and size = -1.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * CALLERS RESPONSIBILITY: If not calling from within the process context for
960Sstevel@tonic-gate  * the specified as, the caller must guarantee persistence of the specified as
970Sstevel@tonic-gate  * for the duration of this function (eg. pages being locked within the as
980Sstevel@tonic-gate  * will guarantee persistence).
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate int
1010Sstevel@tonic-gate as_add_callback(struct as *as, void (*cb_func)(), void *arg, uint_t events,
1020Sstevel@tonic-gate 		caddr_t vaddr, size_t size, int sleepflag)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	struct as_callback 	*current_head, *cb;
1050Sstevel@tonic-gate 	caddr_t 		saddr;
1060Sstevel@tonic-gate 	size_t 			rsize;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/* callback function and an event are mandatory */
1090Sstevel@tonic-gate 	if ((cb_func == NULL) || ((events & AS_ALL_EVENT) == 0))
1100Sstevel@tonic-gate 		return (EINVAL);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/* Adding a callback after as_free has been called is not allowed */
1130Sstevel@tonic-gate 	if (as == &kas)
1140Sstevel@tonic-gate 		return (ENOMEM);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * vaddr = 0 and size = -1 is used to indicate that the callback range
1180Sstevel@tonic-gate 	 * is the entire address space so no rounding is done in that case.
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	if (size != -1) {
1210Sstevel@tonic-gate 		saddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
1220Sstevel@tonic-gate 		rsize = (((size_t)(vaddr + size) + PAGEOFFSET) & PAGEMASK) -
1235084Sjohnlev 		    (size_t)saddr;
1240Sstevel@tonic-gate 		/* check for wraparound */
1250Sstevel@tonic-gate 		if (saddr + rsize < saddr)
1260Sstevel@tonic-gate 			return (ENOMEM);
1270Sstevel@tonic-gate 	} else {
1280Sstevel@tonic-gate 		if (vaddr != 0)
1290Sstevel@tonic-gate 			return (EINVAL);
1300Sstevel@tonic-gate 		saddr = vaddr;
1310Sstevel@tonic-gate 		rsize = size;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/* Allocate and initialize a callback entry */
1350Sstevel@tonic-gate 	cb = kmem_zalloc(sizeof (struct as_callback), sleepflag);
1360Sstevel@tonic-gate 	if (cb == NULL)
1370Sstevel@tonic-gate 		return (EAGAIN);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	cb->ascb_func = cb_func;
1400Sstevel@tonic-gate 	cb->ascb_arg = arg;
1410Sstevel@tonic-gate 	cb->ascb_events = events;
1420Sstevel@tonic-gate 	cb->ascb_saddr = saddr;
1430Sstevel@tonic-gate 	cb->ascb_len = rsize;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/* Add the entry to the list */
1460Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
1470Sstevel@tonic-gate 	current_head = as->a_callbacks;
1480Sstevel@tonic-gate 	as->a_callbacks = cb;
1490Sstevel@tonic-gate 	cb->ascb_next = current_head;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * The call to this function may lose in a race with
1530Sstevel@tonic-gate 	 * a pertinent event - eg. a thread does long term memory locking
1540Sstevel@tonic-gate 	 * but before the callback is added another thread executes as_unmap.
1550Sstevel@tonic-gate 	 * A broadcast here resolves that.
1560Sstevel@tonic-gate 	 */
1570Sstevel@tonic-gate 	if ((cb->ascb_events & AS_UNMAPWAIT_EVENT) && AS_ISUNMAPWAIT(as)) {
1580Sstevel@tonic-gate 		AS_CLRUNMAPWAIT(as);
1590Sstevel@tonic-gate 		cv_broadcast(&as->a_cv);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
1630Sstevel@tonic-gate 	return (0);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Search the callback list for an entry which pertains to arg.
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * This is called from within the client upon completion of the callback.
1700Sstevel@tonic-gate  * RETURN VALUES:
1710Sstevel@tonic-gate  *	AS_CALLBACK_DELETED  (callback entry found and deleted)
1720Sstevel@tonic-gate  *	AS_CALLBACK_NOTFOUND (no callback entry found - this is ok)
1730Sstevel@tonic-gate  *	AS_CALLBACK_DELETE_DEFERRED (callback is in process, delete of this
1740Sstevel@tonic-gate  *			entry will be made in as_do_callbacks)
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * If as_delete_callback encounters a matching entry with AS_CALLBACK_CALLED
1770Sstevel@tonic-gate  * set, it indicates that as_do_callbacks is processing this entry.  The
1780Sstevel@tonic-gate  * AS_ALL_EVENT events are cleared in the entry, and a broadcast is made
1790Sstevel@tonic-gate  * to unblock as_do_callbacks, in case it is blocked.
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  * CALLERS RESPONSIBILITY: If not calling from within the process context for
1820Sstevel@tonic-gate  * the specified as, the caller must guarantee persistence of the specified as
1830Sstevel@tonic-gate  * for the duration of this function (eg. pages being locked within the as
1840Sstevel@tonic-gate  * will guarantee persistence).
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate uint_t
1870Sstevel@tonic-gate as_delete_callback(struct as *as, void *arg)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	struct as_callback **prevcb = &as->a_callbacks;
1900Sstevel@tonic-gate 	struct as_callback *cb;
1910Sstevel@tonic-gate 	uint_t rc = AS_CALLBACK_NOTFOUND;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
1940Sstevel@tonic-gate 	for (cb = as->a_callbacks; cb; prevcb = &cb->ascb_next, cb = *prevcb) {
1950Sstevel@tonic-gate 		if (cb->ascb_arg != arg)
1960Sstevel@tonic-gate 			continue;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		/*
1990Sstevel@tonic-gate 		 * If the events indicate AS_CALLBACK_CALLED, just clear
2000Sstevel@tonic-gate 		 * AS_ALL_EVENT in the events field and wakeup the thread
2010Sstevel@tonic-gate 		 * that may be waiting in as_do_callbacks.  as_do_callbacks
2020Sstevel@tonic-gate 		 * will take care of removing this entry from the list.  In
2030Sstevel@tonic-gate 		 * that case, return AS_CALLBACK_DELETE_DEFERRED.  Otherwise
2040Sstevel@tonic-gate 		 * (AS_CALLBACK_CALLED not set), just remove it from the
2050Sstevel@tonic-gate 		 * list, return the memory and return AS_CALLBACK_DELETED.
2060Sstevel@tonic-gate 		 */
2070Sstevel@tonic-gate 		if ((cb->ascb_events & AS_CALLBACK_CALLED) != 0) {
2080Sstevel@tonic-gate 			/* leave AS_CALLBACK_CALLED */
2090Sstevel@tonic-gate 			cb->ascb_events &= ~AS_ALL_EVENT;
2100Sstevel@tonic-gate 			rc = AS_CALLBACK_DELETE_DEFERRED;
2110Sstevel@tonic-gate 			cv_broadcast(&as->a_cv);
2120Sstevel@tonic-gate 		} else {
2130Sstevel@tonic-gate 			*prevcb = cb->ascb_next;
2140Sstevel@tonic-gate 			kmem_free(cb, sizeof (struct as_callback));
2150Sstevel@tonic-gate 			rc = AS_CALLBACK_DELETED;
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 		break;
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
2200Sstevel@tonic-gate 	return (rc);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Searches the as callback list for a matching entry.
2250Sstevel@tonic-gate  * Returns a pointer to the first matching callback, or NULL if
2260Sstevel@tonic-gate  * nothing is found.
2270Sstevel@tonic-gate  * This function never sleeps so it is ok to call it with more
2280Sstevel@tonic-gate  * locks held but the (required) a_contents mutex.
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * See also comment on as_do_callbacks below.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate static struct as_callback *
2330Sstevel@tonic-gate as_find_callback(struct as *as, uint_t events, caddr_t event_addr,
2340Sstevel@tonic-gate 			size_t event_len)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	struct as_callback	*cb;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&as->a_contents));
2390Sstevel@tonic-gate 	for (cb = as->a_callbacks; cb != NULL; cb = cb->ascb_next) {
2400Sstevel@tonic-gate 		/*
2410Sstevel@tonic-gate 		 * If the callback has not already been called, then
2420Sstevel@tonic-gate 		 * check if events or address range pertains.  An event_len
2430Sstevel@tonic-gate 		 * of zero means do an unconditional callback.
2440Sstevel@tonic-gate 		 */
2450Sstevel@tonic-gate 		if (((cb->ascb_events & AS_CALLBACK_CALLED) != 0) ||
2460Sstevel@tonic-gate 		    ((event_len != 0) && (((cb->ascb_events & events) == 0) ||
2470Sstevel@tonic-gate 		    (event_addr + event_len < cb->ascb_saddr) ||
2480Sstevel@tonic-gate 		    (event_addr > (cb->ascb_saddr + cb->ascb_len))))) {
2490Sstevel@tonic-gate 			continue;
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		break;
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	return (cb);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * Executes a given callback and removes it from the callback list for
2580Sstevel@tonic-gate  * this address space.
2590Sstevel@tonic-gate  * This function may sleep so the caller must drop all locks except
2600Sstevel@tonic-gate  * a_contents before calling this func.
2610Sstevel@tonic-gate  *
2620Sstevel@tonic-gate  * See also comments on as_do_callbacks below.
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate static void
2650Sstevel@tonic-gate as_execute_callback(struct as *as, struct as_callback *cb,
2660Sstevel@tonic-gate 				uint_t events)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	struct as_callback **prevcb;
2690Sstevel@tonic-gate 	void	*cb_arg;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&as->a_contents) && (cb->ascb_events & events));
2720Sstevel@tonic-gate 	cb->ascb_events |= AS_CALLBACK_CALLED;
2730Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
2740Sstevel@tonic-gate 	(*cb->ascb_func)(as, cb->ascb_arg, events);
2750Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * the callback function is required to delete the callback
2780Sstevel@tonic-gate 	 * when the callback function determines it is OK for
2790Sstevel@tonic-gate 	 * this thread to continue. as_delete_callback will clear
2800Sstevel@tonic-gate 	 * the AS_ALL_EVENT in the events field when it is deleted.
2810Sstevel@tonic-gate 	 * If the callback function called as_delete_callback,
2820Sstevel@tonic-gate 	 * events will already be cleared and there will be no blocking.
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	while ((cb->ascb_events & events) != 0) {
2850Sstevel@tonic-gate 		cv_wait(&as->a_cv, &as->a_contents);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * This entry needs to be taken off the list. Normally, the
2890Sstevel@tonic-gate 	 * callback func itself does that, but unfortunately the list
2900Sstevel@tonic-gate 	 * may have changed while the callback was running because the
2910Sstevel@tonic-gate 	 * a_contents mutex was dropped and someone else other than the
2920Sstevel@tonic-gate 	 * callback func itself could have called as_delete_callback,
2930Sstevel@tonic-gate 	 * so we have to search to find this entry again.  The entry
2940Sstevel@tonic-gate 	 * must have AS_CALLBACK_CALLED, and have the same 'arg'.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	cb_arg = cb->ascb_arg;
2970Sstevel@tonic-gate 	prevcb = &as->a_callbacks;
2980Sstevel@tonic-gate 	for (cb = as->a_callbacks; cb != NULL;
2990Sstevel@tonic-gate 	    prevcb = &cb->ascb_next, cb = *prevcb) {
3000Sstevel@tonic-gate 		if (((cb->ascb_events & AS_CALLBACK_CALLED) == 0) ||
3010Sstevel@tonic-gate 		    (cb_arg != cb->ascb_arg)) {
3020Sstevel@tonic-gate 			continue;
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 		*prevcb = cb->ascb_next;
3050Sstevel@tonic-gate 		kmem_free(cb, sizeof (struct as_callback));
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Check the callback list for a matching event and intersection of
3120Sstevel@tonic-gate  * address range. If there is a match invoke the callback.  Skip an entry if:
3130Sstevel@tonic-gate  *    - a callback is already in progress for this entry (AS_CALLBACK_CALLED)
3140Sstevel@tonic-gate  *    - not event of interest
3150Sstevel@tonic-gate  *    - not address range of interest
3160Sstevel@tonic-gate  *
3170Sstevel@tonic-gate  * An event_len of zero indicates a request for an unconditional callback
3180Sstevel@tonic-gate  * (regardless of event), only the AS_CALLBACK_CALLED is checked.  The
3190Sstevel@tonic-gate  * a_contents lock must be dropped before a callback, so only one callback
3200Sstevel@tonic-gate  * can be done before returning. Return -1 (true) if a callback was
3210Sstevel@tonic-gate  * executed and removed from the list, else return 0 (false).
3220Sstevel@tonic-gate  *
3230Sstevel@tonic-gate  * The logically separate parts, i.e. finding a matching callback and
3240Sstevel@tonic-gate  * executing a given callback have been separated into two functions
3250Sstevel@tonic-gate  * so that they can be called with different sets of locks held beyond
3260Sstevel@tonic-gate  * the always-required a_contents. as_find_callback does not sleep so
3270Sstevel@tonic-gate  * it is ok to call it if more locks than a_contents (i.e. the a_lock
3280Sstevel@tonic-gate  * rwlock) are held. as_execute_callback on the other hand may sleep
3290Sstevel@tonic-gate  * so all locks beyond a_contents must be dropped by the caller if one
3300Sstevel@tonic-gate  * does not want to end comatose.
3310Sstevel@tonic-gate  */
3320Sstevel@tonic-gate static int
3330Sstevel@tonic-gate as_do_callbacks(struct as *as, uint_t events, caddr_t event_addr,
3340Sstevel@tonic-gate 			size_t event_len)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	struct as_callback *cb;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if ((cb = as_find_callback(as, events, event_addr, event_len))) {
3390Sstevel@tonic-gate 		as_execute_callback(as, cb, events);
3400Sstevel@tonic-gate 		return (-1);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	return (0);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * Search for the segment containing addr. If a segment containing addr
3470Sstevel@tonic-gate  * exists, that segment is returned.  If no such segment exists, and
3480Sstevel@tonic-gate  * the list spans addresses greater than addr, then the first segment
3490Sstevel@tonic-gate  * whose base is greater than addr is returned; otherwise, NULL is
3500Sstevel@tonic-gate  * returned unless tail is true, in which case the last element of the
3510Sstevel@tonic-gate  * list is returned.
3520Sstevel@tonic-gate  *
3530Sstevel@tonic-gate  * a_seglast is used to cache the last found segment for repeated
3540Sstevel@tonic-gate  * searches to the same addr (which happens frequently).
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate struct seg *
3570Sstevel@tonic-gate as_findseg(struct as *as, caddr_t addr, int tail)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	struct seg *seg = as->a_seglast;
3600Sstevel@tonic-gate 	avl_index_t where;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (seg != NULL &&
3650Sstevel@tonic-gate 	    seg->s_base <= addr &&
3660Sstevel@tonic-gate 	    addr < seg->s_base + seg->s_size)
3670Sstevel@tonic-gate 		return (seg);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	seg = avl_find(&as->a_segtree, &addr, &where);
3700Sstevel@tonic-gate 	if (seg != NULL)
3710Sstevel@tonic-gate 		return (as->a_seglast = seg);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	seg = avl_nearest(&as->a_segtree, where, AVL_AFTER);
3740Sstevel@tonic-gate 	if (seg == NULL && tail)
3750Sstevel@tonic-gate 		seg = avl_last(&as->a_segtree);
3760Sstevel@tonic-gate 	return (as->a_seglast = seg);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate #ifdef VERIFY_SEGLIST
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * verify that the linked list is coherent
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate static void
3840Sstevel@tonic-gate as_verify(struct as *as)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	struct seg *seg, *seglast, *p, *n;
3870Sstevel@tonic-gate 	uint_t nsegs = 0;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (do_as_verify == 0)
3900Sstevel@tonic-gate 		return;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	seglast = as->a_seglast;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
3950Sstevel@tonic-gate 		ASSERT(seg->s_as == as);
3960Sstevel@tonic-gate 		p = AS_SEGPREV(as, seg);
3970Sstevel@tonic-gate 		n = AS_SEGNEXT(as, seg);
3980Sstevel@tonic-gate 		ASSERT(p == NULL || p->s_as == as);
3990Sstevel@tonic-gate 		ASSERT(p == NULL || p->s_base < seg->s_base);
4000Sstevel@tonic-gate 		ASSERT(n == NULL || n->s_base > seg->s_base);
4010Sstevel@tonic-gate 		ASSERT(n != NULL || seg == avl_last(&as->a_segtree));
4020Sstevel@tonic-gate 		if (seg == seglast)
4030Sstevel@tonic-gate 			seglast = NULL;
4040Sstevel@tonic-gate 		nsegs++;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	ASSERT(seglast == NULL);
4070Sstevel@tonic-gate 	ASSERT(avl_numnodes(&as->a_segtree) == nsegs);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate #endif /* VERIFY_SEGLIST */
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate  * Add a new segment to the address space. The avl_find()
4130Sstevel@tonic-gate  * may be expensive so we attempt to use last segment accessed
4140Sstevel@tonic-gate  * in as_gap() as an insertion point.
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate int
4170Sstevel@tonic-gate as_addseg(struct as  *as, struct seg *newseg)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	struct seg *seg;
4200Sstevel@tonic-gate 	caddr_t addr;
4210Sstevel@tonic-gate 	caddr_t eaddr;
4220Sstevel@tonic-gate 	avl_index_t where;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
4270Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (as->a_lastgaphl != NULL) {
4300Sstevel@tonic-gate 		struct seg *hseg = NULL;
4310Sstevel@tonic-gate 		struct seg *lseg = NULL;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		if (as->a_lastgaphl->s_base > newseg->s_base) {
4340Sstevel@tonic-gate 			hseg = as->a_lastgaphl;
4350Sstevel@tonic-gate 			lseg = AVL_PREV(&as->a_segtree, hseg);
4360Sstevel@tonic-gate 		} else {
4370Sstevel@tonic-gate 			lseg = as->a_lastgaphl;
4380Sstevel@tonic-gate 			hseg = AVL_NEXT(&as->a_segtree, lseg);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		if (hseg && lseg && lseg->s_base < newseg->s_base &&
4420Sstevel@tonic-gate 		    hseg->s_base > newseg->s_base) {
4430Sstevel@tonic-gate 			avl_insert_here(&as->a_segtree, newseg, lseg,
4440Sstevel@tonic-gate 			    AVL_AFTER);
4450Sstevel@tonic-gate 			as->a_lastgaphl = NULL;
4460Sstevel@tonic-gate 			as->a_seglast = newseg;
4470Sstevel@tonic-gate 			return (0);
4480Sstevel@tonic-gate 		}
4490Sstevel@tonic-gate 		as->a_lastgaphl = NULL;
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	addr = newseg->s_base;
4530Sstevel@tonic-gate 	eaddr = addr + newseg->s_size;
4540Sstevel@tonic-gate again:
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	seg = avl_find(&as->a_segtree, &addr, &where);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (seg == NULL)
4590Sstevel@tonic-gate 		seg = avl_nearest(&as->a_segtree, where, AVL_AFTER);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	if (seg == NULL)
4620Sstevel@tonic-gate 		seg = avl_last(&as->a_segtree);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (seg != NULL) {
4650Sstevel@tonic-gate 		caddr_t base = seg->s_base;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		/*
4680Sstevel@tonic-gate 		 * If top of seg is below the requested address, then
4690Sstevel@tonic-gate 		 * the insertion point is at the end of the linked list,
4700Sstevel@tonic-gate 		 * and seg points to the tail of the list.  Otherwise,
4710Sstevel@tonic-gate 		 * the insertion point is immediately before seg.
4720Sstevel@tonic-gate 		 */
4730Sstevel@tonic-gate 		if (base + seg->s_size > addr) {
4740Sstevel@tonic-gate 			if (addr >= base || eaddr > base) {
4750Sstevel@tonic-gate #ifdef __sparc
4760Sstevel@tonic-gate 				extern struct seg_ops segnf_ops;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 				/*
4790Sstevel@tonic-gate 				 * no-fault segs must disappear if overlaid.
4800Sstevel@tonic-gate 				 * XXX need new segment type so
4810Sstevel@tonic-gate 				 * we don't have to check s_ops
4820Sstevel@tonic-gate 				 */
4830Sstevel@tonic-gate 				if (seg->s_ops == &segnf_ops) {
4840Sstevel@tonic-gate 					seg_unmap(seg);
4850Sstevel@tonic-gate 					goto again;
4860Sstevel@tonic-gate 				}
4870Sstevel@tonic-gate #endif
4880Sstevel@tonic-gate 				return (-1);	/* overlapping segment */
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	as->a_seglast = newseg;
4930Sstevel@tonic-gate 	avl_insert(&as->a_segtree, newseg, where);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate #ifdef VERIFY_SEGLIST
4960Sstevel@tonic-gate 	as_verify(as);
4970Sstevel@tonic-gate #endif
4980Sstevel@tonic-gate 	return (0);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate struct seg *
5020Sstevel@tonic-gate as_removeseg(struct as *as, struct seg *seg)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	avl_tree_t *t;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
5090Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (seg == NULL)
5120Sstevel@tonic-gate 		return (NULL);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	t = &as->a_segtree;
5150Sstevel@tonic-gate 	if (as->a_seglast == seg)
5160Sstevel@tonic-gate 		as->a_seglast = NULL;
5170Sstevel@tonic-gate 	as->a_lastgaphl = NULL;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/*
5200Sstevel@tonic-gate 	 * if this segment is at an address higher than
5210Sstevel@tonic-gate 	 * a_lastgap, set a_lastgap to the next segment (NULL if last segment)
5220Sstevel@tonic-gate 	 */
5230Sstevel@tonic-gate 	if (as->a_lastgap &&
5240Sstevel@tonic-gate 	    (seg == as->a_lastgap || seg->s_base > as->a_lastgap->s_base))
5250Sstevel@tonic-gate 		as->a_lastgap = AVL_NEXT(t, seg);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * remove the segment from the seg tree
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	avl_remove(t, seg);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate #ifdef VERIFY_SEGLIST
5330Sstevel@tonic-gate 	as_verify(as);
5340Sstevel@tonic-gate #endif
5350Sstevel@tonic-gate 	return (seg);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * Find a segment containing addr.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate struct seg *
5420Sstevel@tonic-gate as_segat(struct as *as, caddr_t addr)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	struct seg *seg = as->a_seglast;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if (seg != NULL && seg->s_base <= addr &&
5490Sstevel@tonic-gate 	    addr < seg->s_base + seg->s_size)
5500Sstevel@tonic-gate 		return (seg);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	seg = avl_find(&as->a_segtree, &addr, NULL);
5530Sstevel@tonic-gate 	return (seg);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate  * Serialize all searches for holes in an address space to
5580Sstevel@tonic-gate  * prevent two or more threads from allocating the same virtual
5590Sstevel@tonic-gate  * address range.  The address space must not be "read/write"
5600Sstevel@tonic-gate  * locked by the caller since we may block.
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate void
5630Sstevel@tonic-gate as_rangelock(struct as *as)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
5660Sstevel@tonic-gate 	while (AS_ISCLAIMGAP(as))
5670Sstevel@tonic-gate 		cv_wait(&as->a_cv, &as->a_contents);
5680Sstevel@tonic-gate 	AS_SETCLAIMGAP(as);
5690Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate  * Release hold on a_state & AS_CLAIMGAP and signal any other blocked threads.
5740Sstevel@tonic-gate  */
5750Sstevel@tonic-gate void
5760Sstevel@tonic-gate as_rangeunlock(struct as *as)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
5790Sstevel@tonic-gate 	AS_CLRCLAIMGAP(as);
5800Sstevel@tonic-gate 	cv_signal(&as->a_cv);
5810Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate  * compar segments (or just an address) by segment address range
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate static int
5880Sstevel@tonic-gate as_segcompar(const void *x, const void *y)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	struct seg *a = (struct seg *)x;
5910Sstevel@tonic-gate 	struct seg *b = (struct seg *)y;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (a->s_base < b->s_base)
5940Sstevel@tonic-gate 		return (-1);
5950Sstevel@tonic-gate 	if (a->s_base >= b->s_base + b->s_size)
5960Sstevel@tonic-gate 		return (1);
5970Sstevel@tonic-gate 	return (0);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate void
6020Sstevel@tonic-gate as_avlinit(struct as *as)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	avl_create(&as->a_segtree, as_segcompar, sizeof (struct seg),
6050Sstevel@tonic-gate 	    offsetof(struct seg, s_tree));
6060Sstevel@tonic-gate 	avl_create(&as->a_wpage, wp_compare, sizeof (struct watched_page),
6070Sstevel@tonic-gate 	    offsetof(struct watched_page, wp_link));
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*ARGSUSED*/
6110Sstevel@tonic-gate static int
6120Sstevel@tonic-gate as_constructor(void *buf, void *cdrarg, int kmflags)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	struct as *as = buf;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	mutex_init(&as->a_contents, NULL, MUTEX_DEFAULT, NULL);
6170Sstevel@tonic-gate 	cv_init(&as->a_cv, NULL, CV_DEFAULT, NULL);
6180Sstevel@tonic-gate 	rw_init(&as->a_lock, NULL, RW_DEFAULT, NULL);
6190Sstevel@tonic-gate 	as_avlinit(as);
6200Sstevel@tonic-gate 	return (0);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*ARGSUSED1*/
6240Sstevel@tonic-gate static void
6250Sstevel@tonic-gate as_destructor(void *buf, void *cdrarg)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	struct as *as = buf;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	avl_destroy(&as->a_segtree);
6300Sstevel@tonic-gate 	mutex_destroy(&as->a_contents);
6310Sstevel@tonic-gate 	cv_destroy(&as->a_cv);
6320Sstevel@tonic-gate 	rw_destroy(&as->a_lock);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate void
6360Sstevel@tonic-gate as_init(void)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	as_cache = kmem_cache_create("as_cache", sizeof (struct as), 0,
6395084Sjohnlev 	    as_constructor, as_destructor, NULL, NULL, NULL, 0);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate  * Allocate and initialize an address space data structure.
6440Sstevel@tonic-gate  * We call hat_alloc to allow any machine dependent
6450Sstevel@tonic-gate  * information in the hat structure to be initialized.
6460Sstevel@tonic-gate  */
6470Sstevel@tonic-gate struct as *
6480Sstevel@tonic-gate as_alloc(void)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	struct as *as;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	as = kmem_cache_alloc(as_cache, KM_SLEEP);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	as->a_flags		= 0;
6550Sstevel@tonic-gate 	as->a_vbits		= 0;
6560Sstevel@tonic-gate 	as->a_hrm		= NULL;
6570Sstevel@tonic-gate 	as->a_seglast		= NULL;
6580Sstevel@tonic-gate 	as->a_size		= 0;
659*10169SSudheer.Abdul-Salam@Sun.COM 	as->a_resvsize		= 0;
6600Sstevel@tonic-gate 	as->a_updatedir		= 0;
6610Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
6620Sstevel@tonic-gate 	as->a_objectdir		= NULL;
6630Sstevel@tonic-gate 	as->a_sizedir		= 0;
6640Sstevel@tonic-gate 	as->a_userlimit		= (caddr_t)USERLIMIT;
6650Sstevel@tonic-gate 	as->a_lastgap		= NULL;
6660Sstevel@tonic-gate 	as->a_lastgaphl		= NULL;
6670Sstevel@tonic-gate 	as->a_callbacks		= NULL;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
6700Sstevel@tonic-gate 	as->a_hat = hat_alloc(as);	/* create hat for default system mmu */
6710Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	as->a_xhat = NULL;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	return (as);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*
6790Sstevel@tonic-gate  * Free an address space data structure.
6800Sstevel@tonic-gate  * Need to free the hat first and then
6810Sstevel@tonic-gate  * all the segments on this as and finally
6820Sstevel@tonic-gate  * the space for the as struct itself.
6830Sstevel@tonic-gate  */
6840Sstevel@tonic-gate void
6850Sstevel@tonic-gate as_free(struct as *as)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	struct hat *hat = as->a_hat;
6880Sstevel@tonic-gate 	struct seg *seg, *next;
6890Sstevel@tonic-gate 	int called = 0;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate top:
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Invoke ALL callbacks. as_do_callbacks will do one callback
6940Sstevel@tonic-gate 	 * per call, and not return (-1) until the callback has completed.
6950Sstevel@tonic-gate 	 * When as_do_callbacks returns zero, all callbacks have completed.
6960Sstevel@tonic-gate 	 */
6970Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
6985084Sjohnlev 	while (as->a_callbacks && as_do_callbacks(as, AS_ALL_EVENT, 0, 0))
6995084Sjohnlev 		;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/* This will prevent new XHATs from attaching to as */
7020Sstevel@tonic-gate 	if (!called)
7030Sstevel@tonic-gate 		AS_SETBUSY(as);
7040Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
7050Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (!called) {
7080Sstevel@tonic-gate 		called = 1;
7090Sstevel@tonic-gate 		hat_free_start(hat);
7100Sstevel@tonic-gate 		if (as->a_xhat != NULL)
7110Sstevel@tonic-gate 			xhat_free_start_all(as);
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = next) {
7140Sstevel@tonic-gate 		int err;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 		next = AS_SEGNEXT(as, seg);
7176695Saguzovsk retry:
7180Sstevel@tonic-gate 		err = SEGOP_UNMAP(seg, seg->s_base, seg->s_size);
7190Sstevel@tonic-gate 		if (err == EAGAIN) {
7200Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
7210Sstevel@tonic-gate 			if (as->a_callbacks) {
7220Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
7236695Saguzovsk 			} else if (!AS_ISNOUNMAPWAIT(as)) {
7240Sstevel@tonic-gate 				/*
7250Sstevel@tonic-gate 				 * Memory is currently locked. Wait for a
7260Sstevel@tonic-gate 				 * cv_signal that it has been unlocked, then
7270Sstevel@tonic-gate 				 * try the operation again.
7280Sstevel@tonic-gate 				 */
7290Sstevel@tonic-gate 				if (AS_ISUNMAPWAIT(as) == 0)
7300Sstevel@tonic-gate 					cv_broadcast(&as->a_cv);
7310Sstevel@tonic-gate 				AS_SETUNMAPWAIT(as);
7320Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
7330Sstevel@tonic-gate 				while (AS_ISUNMAPWAIT(as))
7340Sstevel@tonic-gate 					cv_wait(&as->a_cv, &as->a_contents);
7356695Saguzovsk 			} else {
7366695Saguzovsk 				/*
7376695Saguzovsk 				 * We may have raced with
7386695Saguzovsk 				 * segvn_reclaim()/segspt_reclaim(). In this
7396695Saguzovsk 				 * case clean nounmapwait flag and retry since
7406695Saguzovsk 				 * softlockcnt in this segment may be already
7416695Saguzovsk 				 * 0.  We don't drop as writer lock so our
7426695Saguzovsk 				 * number of retries without sleeping should
7436695Saguzovsk 				 * be very small. See segvn_reclaim() for
7446695Saguzovsk 				 * more comments.
7456695Saguzovsk 				 */
7466695Saguzovsk 				AS_CLRNOUNMAPWAIT(as);
7476695Saguzovsk 				mutex_exit(&as->a_contents);
7486695Saguzovsk 				goto retry;
7490Sstevel@tonic-gate 			}
7500Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
7510Sstevel@tonic-gate 			goto top;
7520Sstevel@tonic-gate 		} else {
7530Sstevel@tonic-gate 			/*
7540Sstevel@tonic-gate 			 * We do not expect any other error return at this
7550Sstevel@tonic-gate 			 * time. This is similar to an ASSERT in seg_unmap()
7560Sstevel@tonic-gate 			 */
7570Sstevel@tonic-gate 			ASSERT(err == 0);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	hat_free_end(hat);
7610Sstevel@tonic-gate 	if (as->a_xhat != NULL)
7620Sstevel@tonic-gate 		xhat_free_end_all(as);
7630Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/* /proc stuff */
7660Sstevel@tonic-gate 	ASSERT(avl_numnodes(&as->a_wpage) == 0);
7670Sstevel@tonic-gate 	if (as->a_objectdir) {
7680Sstevel@tonic-gate 		kmem_free(as->a_objectdir, as->a_sizedir * sizeof (vnode_t *));
7690Sstevel@tonic-gate 		as->a_objectdir = NULL;
7700Sstevel@tonic-gate 		as->a_sizedir = 0;
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/*
7740Sstevel@tonic-gate 	 * Free the struct as back to kmem.  Assert it has no segments.
7750Sstevel@tonic-gate 	 */
7760Sstevel@tonic-gate 	ASSERT(avl_numnodes(&as->a_segtree) == 0);
7770Sstevel@tonic-gate 	kmem_cache_free(as_cache, as);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate int
7819121SVamsi.Krishna@Sun.COM as_dup(struct as *as, struct proc *forkedproc)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	struct as *newas;
7840Sstevel@tonic-gate 	struct seg *seg, *newseg;
785*10169SSudheer.Abdul-Salam@Sun.COM 	size_t	purgesize = 0;
7860Sstevel@tonic-gate 	int error;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
7890Sstevel@tonic-gate 	as_clearwatch(as);
7900Sstevel@tonic-gate 	newas = as_alloc();
7910Sstevel@tonic-gate 	newas->a_userlimit = as->a_userlimit;
7929121SVamsi.Krishna@Sun.COM 	newas->a_proc = forkedproc;
7937209Svk210190 
7940Sstevel@tonic-gate 	AS_LOCK_ENTER(newas, &newas->a_lock, RW_WRITER);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/* This will prevent new XHATs from attaching */
7970Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
7980Sstevel@tonic-gate 	AS_SETBUSY(as);
7990Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
8000Sstevel@tonic-gate 	mutex_enter(&newas->a_contents);
8010Sstevel@tonic-gate 	AS_SETBUSY(newas);
8020Sstevel@tonic-gate 	mutex_exit(&newas->a_contents);
8030Sstevel@tonic-gate 
8044528Spaulsan 	(void) hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_SRD);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
8070Sstevel@tonic-gate 
808*10169SSudheer.Abdul-Salam@Sun.COM 		if (seg->s_flags & S_PURGE) {
809*10169SSudheer.Abdul-Salam@Sun.COM 			purgesize += seg->s_size;
8100Sstevel@tonic-gate 			continue;
811*10169SSudheer.Abdul-Salam@Sun.COM 		}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		newseg = seg_alloc(newas, seg->s_base, seg->s_size);
8140Sstevel@tonic-gate 		if (newseg == NULL) {
8150Sstevel@tonic-gate 			AS_LOCK_EXIT(newas, &newas->a_lock);
8160Sstevel@tonic-gate 			as_setwatch(as);
8170Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
8180Sstevel@tonic-gate 			AS_CLRBUSY(as);
8190Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
8200Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
8210Sstevel@tonic-gate 			as_free(newas);
8220Sstevel@tonic-gate 			return (-1);
8230Sstevel@tonic-gate 		}
8240Sstevel@tonic-gate 		if ((error = SEGOP_DUP(seg, newseg)) != 0) {
8250Sstevel@tonic-gate 			/*
8260Sstevel@tonic-gate 			 * We call seg_free() on the new seg
8270Sstevel@tonic-gate 			 * because the segment is not set up
8280Sstevel@tonic-gate 			 * completely; i.e. it has no ops.
8290Sstevel@tonic-gate 			 */
8300Sstevel@tonic-gate 			as_setwatch(as);
8310Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
8320Sstevel@tonic-gate 			AS_CLRBUSY(as);
8330Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
8340Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
8350Sstevel@tonic-gate 			seg_free(newseg);
8360Sstevel@tonic-gate 			AS_LOCK_EXIT(newas, &newas->a_lock);
8370Sstevel@tonic-gate 			as_free(newas);
8380Sstevel@tonic-gate 			return (error);
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 		newas->a_size += seg->s_size;
8410Sstevel@tonic-gate 	}
842*10169SSudheer.Abdul-Salam@Sun.COM 	newas->a_resvsize = as->a_resvsize - purgesize;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	error = hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_ALL);
8450Sstevel@tonic-gate 	if (as->a_xhat != NULL)
8460Sstevel@tonic-gate 		error |= xhat_dup_all(as, newas, NULL, 0, HAT_DUP_ALL);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	mutex_enter(&newas->a_contents);
8490Sstevel@tonic-gate 	AS_CLRBUSY(newas);
8500Sstevel@tonic-gate 	mutex_exit(&newas->a_contents);
8510Sstevel@tonic-gate 	AS_LOCK_EXIT(newas, &newas->a_lock);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	as_setwatch(as);
8540Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
8550Sstevel@tonic-gate 	AS_CLRBUSY(as);
8560Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
8570Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
8580Sstevel@tonic-gate 	if (error != 0) {
8590Sstevel@tonic-gate 		as_free(newas);
8600Sstevel@tonic-gate 		return (error);
8610Sstevel@tonic-gate 	}
8629121SVamsi.Krishna@Sun.COM 	forkedproc->p_as = newas;
8630Sstevel@tonic-gate 	return (0);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate /*
8670Sstevel@tonic-gate  * Handle a ``fault'' at addr for size bytes.
8680Sstevel@tonic-gate  */
8690Sstevel@tonic-gate faultcode_t
8700Sstevel@tonic-gate as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size,
8710Sstevel@tonic-gate 	enum fault_type type, enum seg_rw rw)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate 	struct seg *seg;
8740Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
8750Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
8760Sstevel@tonic-gate 	size_t ssize;
8770Sstevel@tonic-gate 	faultcode_t res = 0;
8780Sstevel@tonic-gate 	caddr_t addrsav;
8790Sstevel@tonic-gate 	struct seg *segsav;
8800Sstevel@tonic-gate 	int as_lock_held;
8810Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8820Sstevel@tonic-gate 	int is_xhat = 0;
8830Sstevel@tonic-gate 	int holding_wpage = 0;
8840Sstevel@tonic-gate 	extern struct seg_ops   segdev_ops;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (as->a_hat != hat) {
8890Sstevel@tonic-gate 		/* This must be an XHAT then */
8900Sstevel@tonic-gate 		is_xhat = 1;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		if ((type != F_INVAL) || (as == &kas))
8930Sstevel@tonic-gate 			return (FC_NOSUPPORT);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate retry:
8970Sstevel@tonic-gate 	if (!is_xhat) {
8980Sstevel@tonic-gate 		/*
8990Sstevel@tonic-gate 		 * Indicate that the lwp is not to be stopped while waiting
9000Sstevel@tonic-gate 		 * for a pagefault.  This is to avoid deadlock while debugging
9010Sstevel@tonic-gate 		 * a process via /proc over NFS (in particular).
9020Sstevel@tonic-gate 		 */
9033930Snr123932 		if (lwp != NULL)
9040Sstevel@tonic-gate 			lwp->lwp_nostop++;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		/*
9070Sstevel@tonic-gate 		 * same length must be used when we softlock and softunlock.
9080Sstevel@tonic-gate 		 * We don't support softunlocking lengths less than
9090Sstevel@tonic-gate 		 * the original length when there is largepage support.
9100Sstevel@tonic-gate 		 * See seg_dev.c for more comments.
9110Sstevel@tonic-gate 		 */
9120Sstevel@tonic-gate 		switch (type) {
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		case F_SOFTLOCK:
9150Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, softlock, 1);
9160Sstevel@tonic-gate 			break;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		case F_SOFTUNLOCK:
9190Sstevel@tonic-gate 			break;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		case F_PROT:
9220Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, prot_fault, 1);
9230Sstevel@tonic-gate 			break;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 		case F_INVAL:
9260Sstevel@tonic-gate 			CPU_STATS_ENTER_K();
9270Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, vm, as_fault, 1);
9280Sstevel@tonic-gate 			if (as == &kas)
9290Sstevel@tonic-gate 				CPU_STATS_ADDQ(CPU, vm, kernel_asflt, 1);
9300Sstevel@tonic-gate 			CPU_STATS_EXIT_K();
9310Sstevel@tonic-gate 			break;
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	/* Kernel probe */
9360Sstevel@tonic-gate 	TNF_PROBE_3(address_fault, "vm pagefault", /* CSTYLED */,
9375084Sjohnlev 	    tnf_opaque,	address,	addr,
9385084Sjohnlev 	    tnf_fault_type,	fault_type,	type,
9395084Sjohnlev 	    tnf_seg_access,	access,		rw);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
9420Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
9435084Sjohnlev 	    (size_t)raddr;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * XXX -- Don't grab the as lock for segkmap. We should grab it for
9470Sstevel@tonic-gate 	 * correctness, but then we could be stuck holding this lock for
9480Sstevel@tonic-gate 	 * a LONG time if the fault needs to be resolved on a slow
9490Sstevel@tonic-gate 	 * filesystem, and then no-one will be able to exec new commands,
9500Sstevel@tonic-gate 	 * as exec'ing requires the write lock on the as.
9510Sstevel@tonic-gate 	 */
9520Sstevel@tonic-gate 	if (as == &kas && segkmap && segkmap->s_base <= raddr &&
9530Sstevel@tonic-gate 	    raddr + size < segkmap->s_base + segkmap->s_size) {
9540Sstevel@tonic-gate 		/*
9550Sstevel@tonic-gate 		 * if (as==&kas), this can't be XHAT: we've already returned
9560Sstevel@tonic-gate 		 * FC_NOSUPPORT.
9570Sstevel@tonic-gate 		 */
9580Sstevel@tonic-gate 		seg = segkmap;
9590Sstevel@tonic-gate 		as_lock_held = 0;
9600Sstevel@tonic-gate 	} else {
9610Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
9620Sstevel@tonic-gate 		if (is_xhat && avl_numnodes(&as->a_wpage) != 0) {
9630Sstevel@tonic-gate 			/*
9640Sstevel@tonic-gate 			 * Grab and hold the writers' lock on the as
9650Sstevel@tonic-gate 			 * if the fault is to a watched page.
9660Sstevel@tonic-gate 			 * This will keep CPUs from "peeking" at the
9670Sstevel@tonic-gate 			 * address range while we're temporarily boosting
9680Sstevel@tonic-gate 			 * the permissions for the XHAT device to
9690Sstevel@tonic-gate 			 * resolve the fault in the segment layer.
9700Sstevel@tonic-gate 			 *
9710Sstevel@tonic-gate 			 * We could check whether faulted address
9720Sstevel@tonic-gate 			 * is within a watched page and only then grab
9730Sstevel@tonic-gate 			 * the writer lock, but this is simpler.
9740Sstevel@tonic-gate 			 */
9750Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
9760Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 		seg = as_segat(as, raddr);
9800Sstevel@tonic-gate 		if (seg == NULL) {
9810Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
9823930Snr123932 			if ((lwp != NULL) && (!is_xhat))
9830Sstevel@tonic-gate 				lwp->lwp_nostop--;
9840Sstevel@tonic-gate 			return (FC_NOMAP);
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		as_lock_held = 1;
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	addrsav = raddr;
9910Sstevel@tonic-gate 	segsav = seg;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
9940Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
9950Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
9960Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
9970Sstevel@tonic-gate 				res = FC_NOMAP;
9980Sstevel@tonic-gate 				break;
9990Sstevel@tonic-gate 			}
10000Sstevel@tonic-gate 		}
10010Sstevel@tonic-gate 		if (raddr + rsize > seg->s_base + seg->s_size)
10020Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
10030Sstevel@tonic-gate 		else
10040Sstevel@tonic-gate 			ssize = rsize;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 		if (!is_xhat || (seg->s_ops != &segdev_ops)) {
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 			if (is_xhat && avl_numnodes(&as->a_wpage) != 0 &&
10090Sstevel@tonic-gate 			    pr_is_watchpage_as(raddr, rw, as)) {
10100Sstevel@tonic-gate 				/*
10110Sstevel@tonic-gate 				 * Handle watch pages.  If we're faulting on a
10120Sstevel@tonic-gate 				 * watched page from an X-hat, we have to
10130Sstevel@tonic-gate 				 * restore the original permissions while we
10140Sstevel@tonic-gate 				 * handle the fault.
10150Sstevel@tonic-gate 				 */
10160Sstevel@tonic-gate 				as_clearwatch(as);
10170Sstevel@tonic-gate 				holding_wpage = 1;
10180Sstevel@tonic-gate 			}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 			res = SEGOP_FAULT(hat, seg, raddr, ssize, type, rw);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 			/* Restore watchpoints */
10230Sstevel@tonic-gate 			if (holding_wpage) {
10240Sstevel@tonic-gate 				as_setwatch(as);
10250Sstevel@tonic-gate 				holding_wpage = 0;
10260Sstevel@tonic-gate 			}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			if (res != 0)
10290Sstevel@tonic-gate 				break;
10300Sstevel@tonic-gate 		} else {
10310Sstevel@tonic-gate 			/* XHAT does not support seg_dev */
10320Sstevel@tonic-gate 			res = FC_NOSUPPORT;
10330Sstevel@tonic-gate 			break;
10340Sstevel@tonic-gate 		}
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	/*
10380Sstevel@tonic-gate 	 * If we were SOFTLOCKing and encountered a failure,
10390Sstevel@tonic-gate 	 * we must SOFTUNLOCK the range we already did. (Maybe we
10400Sstevel@tonic-gate 	 * should just panic if we are SOFTLOCKing or even SOFTUNLOCKing
10410Sstevel@tonic-gate 	 * right here...)
10420Sstevel@tonic-gate 	 */
10430Sstevel@tonic-gate 	if (res != 0 && type == F_SOFTLOCK) {
10440Sstevel@tonic-gate 		for (seg = segsav; addrsav < raddr; addrsav += ssize) {
10450Sstevel@tonic-gate 			if (addrsav >= seg->s_base + seg->s_size)
10460Sstevel@tonic-gate 				seg = AS_SEGNEXT(as, seg);
10470Sstevel@tonic-gate 			ASSERT(seg != NULL);
10480Sstevel@tonic-gate 			/*
10490Sstevel@tonic-gate 			 * Now call the fault routine again to perform the
10500Sstevel@tonic-gate 			 * unlock using S_OTHER instead of the rw variable
10510Sstevel@tonic-gate 			 * since we never got a chance to touch the pages.
10520Sstevel@tonic-gate 			 */
10530Sstevel@tonic-gate 			if (raddr > seg->s_base + seg->s_size)
10540Sstevel@tonic-gate 				ssize = seg->s_base + seg->s_size - addrsav;
10550Sstevel@tonic-gate 			else
10560Sstevel@tonic-gate 				ssize = raddr - addrsav;
10570Sstevel@tonic-gate 			(void) SEGOP_FAULT(hat, seg, addrsav, ssize,
10580Sstevel@tonic-gate 			    F_SOFTUNLOCK, S_OTHER);
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 	if (as_lock_held)
10620Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
10633930Snr123932 	if ((lwp != NULL) && (!is_xhat))
10640Sstevel@tonic-gate 		lwp->lwp_nostop--;
10653930Snr123932 
10660Sstevel@tonic-gate 	/*
10670Sstevel@tonic-gate 	 * If the lower levels returned EDEADLK for a fault,
10680Sstevel@tonic-gate 	 * It means that we should retry the fault.  Let's wait
10690Sstevel@tonic-gate 	 * a bit also to let the deadlock causing condition clear.
10700Sstevel@tonic-gate 	 * This is part of a gross hack to work around a design flaw
10710Sstevel@tonic-gate 	 * in the ufs/sds logging code and should go away when the
10720Sstevel@tonic-gate 	 * logging code is re-designed to fix the problem. See bug
10730Sstevel@tonic-gate 	 * 4125102 for details of the problem.
10740Sstevel@tonic-gate 	 */
10750Sstevel@tonic-gate 	if (FC_ERRNO(res) == EDEADLK) {
10760Sstevel@tonic-gate 		delay(deadlk_wait);
10770Sstevel@tonic-gate 		res = 0;
10780Sstevel@tonic-gate 		goto retry;
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 	return (res);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate  * Asynchronous ``fault'' at addr for size bytes.
10870Sstevel@tonic-gate  */
10880Sstevel@tonic-gate faultcode_t
10890Sstevel@tonic-gate as_faulta(struct as *as, caddr_t addr, size_t size)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate 	struct seg *seg;
10920Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
10930Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
10940Sstevel@tonic-gate 	faultcode_t res = 0;
10950Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate retry:
10980Sstevel@tonic-gate 	/*
10990Sstevel@tonic-gate 	 * Indicate that the lwp is not to be stopped while waiting
11000Sstevel@tonic-gate 	 * for a pagefault.  This is to avoid deadlock while debugging
11010Sstevel@tonic-gate 	 * a process via /proc over NFS (in particular).
11020Sstevel@tonic-gate 	 */
11033930Snr123932 	if (lwp != NULL)
11040Sstevel@tonic-gate 		lwp->lwp_nostop++;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
11070Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
11085084Sjohnlev 	    (size_t)raddr;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
11110Sstevel@tonic-gate 	seg = as_segat(as, raddr);
11120Sstevel@tonic-gate 	if (seg == NULL) {
11130Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
11143930Snr123932 		if (lwp != NULL)
11150Sstevel@tonic-gate 			lwp->lwp_nostop--;
11160Sstevel@tonic-gate 		return (FC_NOMAP);
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	for (; rsize != 0; rsize -= PAGESIZE, raddr += PAGESIZE) {
11200Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
11210Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
11220Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
11230Sstevel@tonic-gate 				res = FC_NOMAP;
11240Sstevel@tonic-gate 				break;
11250Sstevel@tonic-gate 			}
11260Sstevel@tonic-gate 		}
11270Sstevel@tonic-gate 		res = SEGOP_FAULTA(seg, raddr);
11280Sstevel@tonic-gate 		if (res != 0)
11290Sstevel@tonic-gate 			break;
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
11323930Snr123932 	if (lwp != NULL)
11330Sstevel@tonic-gate 		lwp->lwp_nostop--;
11340Sstevel@tonic-gate 	/*
11350Sstevel@tonic-gate 	 * If the lower levels returned EDEADLK for a fault,
11360Sstevel@tonic-gate 	 * It means that we should retry the fault.  Let's wait
11370Sstevel@tonic-gate 	 * a bit also to let the deadlock causing condition clear.
11380Sstevel@tonic-gate 	 * This is part of a gross hack to work around a design flaw
11390Sstevel@tonic-gate 	 * in the ufs/sds logging code and should go away when the
11400Sstevel@tonic-gate 	 * logging code is re-designed to fix the problem. See bug
11410Sstevel@tonic-gate 	 * 4125102 for details of the problem.
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	if (FC_ERRNO(res) == EDEADLK) {
11440Sstevel@tonic-gate 		delay(deadlk_wait);
11450Sstevel@tonic-gate 		res = 0;
11460Sstevel@tonic-gate 		goto retry;
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 	return (res);
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate  * Set the virtual mapping for the interval from [addr : addr + size)
11530Sstevel@tonic-gate  * in address space `as' to have the specified protection.
11540Sstevel@tonic-gate  * It is ok for the range to cross over several segments,
11550Sstevel@tonic-gate  * as long as they are contiguous.
11560Sstevel@tonic-gate  */
11570Sstevel@tonic-gate int
11580Sstevel@tonic-gate as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
11590Sstevel@tonic-gate {
11600Sstevel@tonic-gate 	struct seg *seg;
11610Sstevel@tonic-gate 	struct as_callback *cb;
11620Sstevel@tonic-gate 	size_t ssize;
11630Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
11640Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
11650Sstevel@tonic-gate 	int error = 0, writer = 0;
11660Sstevel@tonic-gate 	caddr_t saveraddr;
11670Sstevel@tonic-gate 	size_t saversize;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate setprot_top:
11700Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
11710Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
11725084Sjohnlev 	    (size_t)raddr;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
11750Sstevel@tonic-gate 		return (ENOMEM);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	saveraddr = raddr;
11780Sstevel@tonic-gate 	saversize = rsize;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/*
11810Sstevel@tonic-gate 	 * Normally we only lock the as as a reader. But
11820Sstevel@tonic-gate 	 * if due to setprot the segment driver needs to split
11835331Samw 	 * a segment it will return IE_RETRY. Therefore we re-acquire
11840Sstevel@tonic-gate 	 * the as lock as a writer so the segment driver can change
11850Sstevel@tonic-gate 	 * the seg list. Also the segment driver will return IE_RETRY
11860Sstevel@tonic-gate 	 * after it has changed the segment list so we therefore keep
11870Sstevel@tonic-gate 	 * locking as a writer. Since these opeartions should be rare
11880Sstevel@tonic-gate 	 * want to only lock as a writer when necessary.
11890Sstevel@tonic-gate 	 */
11900Sstevel@tonic-gate 	if (writer || avl_numnodes(&as->a_wpage) != 0) {
11910Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
11920Sstevel@tonic-gate 	} else {
11930Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, rsize);
11970Sstevel@tonic-gate 	seg = as_segat(as, raddr);
11980Sstevel@tonic-gate 	if (seg == NULL) {
11990Sstevel@tonic-gate 		as_setwatch(as);
12000Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
12010Sstevel@tonic-gate 		return (ENOMEM);
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
12050Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
12060Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
12070Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
12080Sstevel@tonic-gate 				error = ENOMEM;
12090Sstevel@tonic-gate 				break;
12100Sstevel@tonic-gate 			}
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
12130Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
12140Sstevel@tonic-gate 		else
12150Sstevel@tonic-gate 			ssize = rsize;
12166695Saguzovsk retry:
12170Sstevel@tonic-gate 		error = SEGOP_SETPROT(seg, raddr, ssize, prot);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		if (error == IE_NOMEM) {
12200Sstevel@tonic-gate 			error = EAGAIN;
12210Sstevel@tonic-gate 			break;
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 		if (error == IE_RETRY) {
12250Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
12260Sstevel@tonic-gate 			writer = 1;
12270Sstevel@tonic-gate 			goto setprot_top;
12280Sstevel@tonic-gate 		}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 		if (error == EAGAIN) {
12310Sstevel@tonic-gate 			/*
12320Sstevel@tonic-gate 			 * Make sure we have a_lock as writer.
12330Sstevel@tonic-gate 			 */
12340Sstevel@tonic-gate 			if (writer == 0) {
12350Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
12360Sstevel@tonic-gate 				writer = 1;
12370Sstevel@tonic-gate 				goto setprot_top;
12380Sstevel@tonic-gate 			}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 			/*
12410Sstevel@tonic-gate 			 * Memory is currently locked.  It must be unlocked
12420Sstevel@tonic-gate 			 * before this operation can succeed through a retry.
12430Sstevel@tonic-gate 			 * The possible reasons for locked memory and
12440Sstevel@tonic-gate 			 * corresponding strategies for unlocking are:
12450Sstevel@tonic-gate 			 * (1) Normal I/O
12460Sstevel@tonic-gate 			 *	wait for a signal that the I/O operation
12470Sstevel@tonic-gate 			 *	has completed and the memory is unlocked.
12480Sstevel@tonic-gate 			 * (2) Asynchronous I/O
12490Sstevel@tonic-gate 			 *	The aio subsystem does not unlock pages when
12500Sstevel@tonic-gate 			 *	the I/O is completed. Those pages are unlocked
12510Sstevel@tonic-gate 			 *	when the application calls aiowait/aioerror.
12520Sstevel@tonic-gate 			 *	So, to prevent blocking forever, cv_broadcast()
12530Sstevel@tonic-gate 			 *	is done to wake up aio_cleanup_thread.
12540Sstevel@tonic-gate 			 *	Subsequently, segvn_reclaim will be called, and
12550Sstevel@tonic-gate 			 *	that will do AS_CLRUNMAPWAIT() and wake us up.
12560Sstevel@tonic-gate 			 * (3) Long term page locking:
12570Sstevel@tonic-gate 			 *	Drivers intending to have pages locked for a
12580Sstevel@tonic-gate 			 *	period considerably longer than for normal I/O
12590Sstevel@tonic-gate 			 *	(essentially forever) may have registered for a
12600Sstevel@tonic-gate 			 *	callback so they may unlock these pages on
12610Sstevel@tonic-gate 			 *	request. This is needed to allow this operation
12620Sstevel@tonic-gate 			 *	to succeed. Each entry on the callback list is
12630Sstevel@tonic-gate 			 *	examined. If the event or address range pertains
12640Sstevel@tonic-gate 			 *	the callback is invoked (unless it already is in
12650Sstevel@tonic-gate 			 *	progress). The a_contents lock must be dropped
12660Sstevel@tonic-gate 			 *	before the callback, so only one callback can
12670Sstevel@tonic-gate 			 *	be done at a time. Go to the top and do more
12680Sstevel@tonic-gate 			 *	until zero is returned. If zero is returned,
12690Sstevel@tonic-gate 			 *	either there were no callbacks for this event
12700Sstevel@tonic-gate 			 *	or they were already in progress.
12710Sstevel@tonic-gate 			 */
12720Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
12730Sstevel@tonic-gate 			if (as->a_callbacks &&
12745084Sjohnlev 			    (cb = as_find_callback(as, AS_SETPROT_EVENT,
12755084Sjohnlev 			    seg->s_base, seg->s_size))) {
12760Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
12770Sstevel@tonic-gate 				as_execute_callback(as, cb, AS_SETPROT_EVENT);
12786695Saguzovsk 			} else if (!AS_ISNOUNMAPWAIT(as)) {
12790Sstevel@tonic-gate 				if (AS_ISUNMAPWAIT(as) == 0)
12800Sstevel@tonic-gate 					cv_broadcast(&as->a_cv);
12810Sstevel@tonic-gate 				AS_SETUNMAPWAIT(as);
12820Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
12830Sstevel@tonic-gate 				while (AS_ISUNMAPWAIT(as))
12840Sstevel@tonic-gate 					cv_wait(&as->a_cv, &as->a_contents);
12856695Saguzovsk 			} else {
12866695Saguzovsk 				/*
12876695Saguzovsk 				 * We may have raced with
12886695Saguzovsk 				 * segvn_reclaim()/segspt_reclaim(). In this
12896695Saguzovsk 				 * case clean nounmapwait flag and retry since
12906695Saguzovsk 				 * softlockcnt in this segment may be already
12916695Saguzovsk 				 * 0.  We don't drop as writer lock so our
12926695Saguzovsk 				 * number of retries without sleeping should
12936695Saguzovsk 				 * be very small. See segvn_reclaim() for
12946695Saguzovsk 				 * more comments.
12956695Saguzovsk 				 */
12966695Saguzovsk 				AS_CLRNOUNMAPWAIT(as);
12976695Saguzovsk 				mutex_exit(&as->a_contents);
12986695Saguzovsk 				goto retry;
12990Sstevel@tonic-gate 			}
13000Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
13010Sstevel@tonic-gate 			goto setprot_top;
13020Sstevel@tonic-gate 		} else if (error != 0)
13030Sstevel@tonic-gate 			break;
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 	if (error != 0) {
13060Sstevel@tonic-gate 		as_setwatch(as);
13070Sstevel@tonic-gate 	} else {
13080Sstevel@tonic-gate 		as_setwatchprot(as, saveraddr, saversize, prot);
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
13110Sstevel@tonic-gate 	return (error);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * Check to make sure that the interval [addr, addr + size)
13160Sstevel@tonic-gate  * in address space `as' has at least the specified protection.
13170Sstevel@tonic-gate  * It is ok for the range to cross over several segments, as long
13180Sstevel@tonic-gate  * as they are contiguous.
13190Sstevel@tonic-gate  */
13200Sstevel@tonic-gate int
13210Sstevel@tonic-gate as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
13220Sstevel@tonic-gate {
13230Sstevel@tonic-gate 	struct seg *seg;
13240Sstevel@tonic-gate 	size_t ssize;
13250Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
13260Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
13270Sstevel@tonic-gate 	int error = 0;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
13300Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
13315084Sjohnlev 	    (size_t)raddr;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
13340Sstevel@tonic-gate 		return (ENOMEM);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * This is ugly as sin...
13380Sstevel@tonic-gate 	 * Normally, we only acquire the address space readers lock.
13390Sstevel@tonic-gate 	 * However, if the address space has watchpoints present,
13400Sstevel@tonic-gate 	 * we must acquire the writer lock on the address space for
13410Sstevel@tonic-gate 	 * the benefit of as_clearwatchprot() and as_setwatchprot().
13420Sstevel@tonic-gate 	 */
13430Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) != 0)
13440Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
13450Sstevel@tonic-gate 	else
13460Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
13470Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, rsize);
13480Sstevel@tonic-gate 	seg = as_segat(as, raddr);
13490Sstevel@tonic-gate 	if (seg == NULL) {
13500Sstevel@tonic-gate 		as_setwatch(as);
13510Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
13520Sstevel@tonic-gate 		return (ENOMEM);
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
13560Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
13570Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
13580Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
13590Sstevel@tonic-gate 				error = ENOMEM;
13600Sstevel@tonic-gate 				break;
13610Sstevel@tonic-gate 			}
13620Sstevel@tonic-gate 		}
13630Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
13640Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
13650Sstevel@tonic-gate 		else
13660Sstevel@tonic-gate 			ssize = rsize;
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 		error = SEGOP_CHECKPROT(seg, raddr, ssize, prot);
13690Sstevel@tonic-gate 		if (error != 0)
13700Sstevel@tonic-gate 			break;
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate 	as_setwatch(as);
13730Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
13740Sstevel@tonic-gate 	return (error);
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate int
13780Sstevel@tonic-gate as_unmap(struct as *as, caddr_t addr, size_t size)
13790Sstevel@tonic-gate {
13800Sstevel@tonic-gate 	struct seg *seg, *seg_next;
13810Sstevel@tonic-gate 	struct as_callback *cb;
13820Sstevel@tonic-gate 	caddr_t raddr, eaddr;
1383*10169SSudheer.Abdul-Salam@Sun.COM 	size_t ssize, rsize = 0;
13840Sstevel@tonic-gate 	int err;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate top:
13870Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
13880Sstevel@tonic-gate 	eaddr = (caddr_t)(((uintptr_t)(addr + size) + PAGEOFFSET) &
13890Sstevel@tonic-gate 	    (uintptr_t)PAGEMASK);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
13940Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/*
13970Sstevel@tonic-gate 	 * Use as_findseg to find the first segment in the range, then
13980Sstevel@tonic-gate 	 * step through the segments in order, following s_next.
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, eaddr - raddr);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	for (seg = as_findseg(as, raddr, 0); seg != NULL; seg = seg_next) {
14030Sstevel@tonic-gate 		if (eaddr <= seg->s_base)
14040Sstevel@tonic-gate 			break;		/* eaddr was in a gap; all done */
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 		/* this is implied by the test above */
14070Sstevel@tonic-gate 		ASSERT(raddr < eaddr);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 		if (raddr < seg->s_base)
14100Sstevel@tonic-gate 			raddr = seg->s_base; 	/* raddr was in a gap */
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		if (eaddr > (seg->s_base + seg->s_size))
14130Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
14140Sstevel@tonic-gate 		else
14150Sstevel@tonic-gate 			ssize = eaddr - raddr;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 		/*
14180Sstevel@tonic-gate 		 * Save next segment pointer since seg can be
14190Sstevel@tonic-gate 		 * destroyed during the segment unmap operation.
14200Sstevel@tonic-gate 		 */
14210Sstevel@tonic-gate 		seg_next = AS_SEGNEXT(as, seg);
14220Sstevel@tonic-gate 
1423*10169SSudheer.Abdul-Salam@Sun.COM 		/*
1424*10169SSudheer.Abdul-Salam@Sun.COM 		 * We didn't count /dev/null mappings, so ignore them here.
1425*10169SSudheer.Abdul-Salam@Sun.COM 		 * We'll handle MAP_NORESERVE cases in segvn_unmap(). (Again,
1426*10169SSudheer.Abdul-Salam@Sun.COM 		 * we have to do this check here while we have seg.)
1427*10169SSudheer.Abdul-Salam@Sun.COM 		 */
1428*10169SSudheer.Abdul-Salam@Sun.COM 		if (!SEG_IS_DEVNULL_MAPPING(seg) &&
1429*10169SSudheer.Abdul-Salam@Sun.COM 		    !SEG_IS_PARTIAL_RESV(seg))
1430*10169SSudheer.Abdul-Salam@Sun.COM 			rsize = ssize;
1431*10169SSudheer.Abdul-Salam@Sun.COM 
14326695Saguzovsk retry:
14330Sstevel@tonic-gate 		err = SEGOP_UNMAP(seg, raddr, ssize);
14340Sstevel@tonic-gate 		if (err == EAGAIN) {
14350Sstevel@tonic-gate 			/*
14360Sstevel@tonic-gate 			 * Memory is currently locked.  It must be unlocked
14370Sstevel@tonic-gate 			 * before this operation can succeed through a retry.
14380Sstevel@tonic-gate 			 * The possible reasons for locked memory and
14390Sstevel@tonic-gate 			 * corresponding strategies for unlocking are:
14400Sstevel@tonic-gate 			 * (1) Normal I/O
14410Sstevel@tonic-gate 			 *	wait for a signal that the I/O operation
14420Sstevel@tonic-gate 			 *	has completed and the memory is unlocked.
14430Sstevel@tonic-gate 			 * (2) Asynchronous I/O
14440Sstevel@tonic-gate 			 *	The aio subsystem does not unlock pages when
14450Sstevel@tonic-gate 			 *	the I/O is completed. Those pages are unlocked
14460Sstevel@tonic-gate 			 *	when the application calls aiowait/aioerror.
14470Sstevel@tonic-gate 			 *	So, to prevent blocking forever, cv_broadcast()
14480Sstevel@tonic-gate 			 *	is done to wake up aio_cleanup_thread.
14490Sstevel@tonic-gate 			 *	Subsequently, segvn_reclaim will be called, and
14500Sstevel@tonic-gate 			 *	that will do AS_CLRUNMAPWAIT() and wake us up.
14510Sstevel@tonic-gate 			 * (3) Long term page locking:
14520Sstevel@tonic-gate 			 *	Drivers intending to have pages locked for a
14530Sstevel@tonic-gate 			 *	period considerably longer than for normal I/O
14540Sstevel@tonic-gate 			 *	(essentially forever) may have registered for a
14550Sstevel@tonic-gate 			 *	callback so they may unlock these pages on
14560Sstevel@tonic-gate 			 *	request. This is needed to allow this operation
14570Sstevel@tonic-gate 			 *	to succeed. Each entry on the callback list is
14580Sstevel@tonic-gate 			 *	examined. If the event or address range pertains
14590Sstevel@tonic-gate 			 *	the callback is invoked (unless it already is in
14600Sstevel@tonic-gate 			 *	progress). The a_contents lock must be dropped
14610Sstevel@tonic-gate 			 *	before the callback, so only one callback can
14620Sstevel@tonic-gate 			 *	be done at a time. Go to the top and do more
14630Sstevel@tonic-gate 			 *	until zero is returned. If zero is returned,
14640Sstevel@tonic-gate 			 *	either there were no callbacks for this event
14650Sstevel@tonic-gate 			 *	or they were already in progress.
14660Sstevel@tonic-gate 			 */
14670Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
14680Sstevel@tonic-gate 			if (as->a_callbacks &&
14695084Sjohnlev 			    (cb = as_find_callback(as, AS_UNMAP_EVENT,
14705084Sjohnlev 			    seg->s_base, seg->s_size))) {
14710Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
14720Sstevel@tonic-gate 				as_execute_callback(as, cb, AS_UNMAP_EVENT);
14736695Saguzovsk 			} else if (!AS_ISNOUNMAPWAIT(as)) {
14740Sstevel@tonic-gate 				if (AS_ISUNMAPWAIT(as) == 0)
14750Sstevel@tonic-gate 					cv_broadcast(&as->a_cv);
14760Sstevel@tonic-gate 				AS_SETUNMAPWAIT(as);
14770Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
14780Sstevel@tonic-gate 				while (AS_ISUNMAPWAIT(as))
14790Sstevel@tonic-gate 					cv_wait(&as->a_cv, &as->a_contents);
14806695Saguzovsk 			} else {
14816695Saguzovsk 				/*
14826695Saguzovsk 				 * We may have raced with
14836695Saguzovsk 				 * segvn_reclaim()/segspt_reclaim(). In this
14846695Saguzovsk 				 * case clean nounmapwait flag and retry since
14856695Saguzovsk 				 * softlockcnt in this segment may be already
14866695Saguzovsk 				 * 0.  We don't drop as writer lock so our
14876695Saguzovsk 				 * number of retries without sleeping should
14886695Saguzovsk 				 * be very small. See segvn_reclaim() for
14896695Saguzovsk 				 * more comments.
14906695Saguzovsk 				 */
14916695Saguzovsk 				AS_CLRNOUNMAPWAIT(as);
14926695Saguzovsk 				mutex_exit(&as->a_contents);
14936695Saguzovsk 				goto retry;
14940Sstevel@tonic-gate 			}
14950Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
14960Sstevel@tonic-gate 			goto top;
14970Sstevel@tonic-gate 		} else if (err == IE_RETRY) {
14980Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
14990Sstevel@tonic-gate 			goto top;
15000Sstevel@tonic-gate 		} else if (err) {
15010Sstevel@tonic-gate 			as_setwatch(as);
15020Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
15030Sstevel@tonic-gate 			return (-1);
15040Sstevel@tonic-gate 		}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 		as->a_size -= ssize;
1507*10169SSudheer.Abdul-Salam@Sun.COM 		as->a_resvsize -= rsize;
15080Sstevel@tonic-gate 		raddr += ssize;
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
15110Sstevel@tonic-gate 	return (0);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate static int
15152414Saguzovsk as_map_segvn_segs(struct as *as, caddr_t addr, size_t size, uint_t szcvec,
15160Sstevel@tonic-gate     int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate 	uint_t szc;
15190Sstevel@tonic-gate 	uint_t nszc;
15200Sstevel@tonic-gate 	int error;
15210Sstevel@tonic-gate 	caddr_t a;
15220Sstevel@tonic-gate 	caddr_t eaddr;
15230Sstevel@tonic-gate 	size_t segsize;
15240Sstevel@tonic-gate 	struct seg *seg;
15252414Saguzovsk 	size_t pgsz;
15262414Saguzovsk 	int do_off = (vn_a->vp != NULL || vn_a->amp != NULL);
15270Sstevel@tonic-gate 	uint_t save_szcvec;
15282414Saguzovsk 
15292414Saguzovsk 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
15302414Saguzovsk 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
15312414Saguzovsk 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
15322414Saguzovsk 	ASSERT(vn_a->vp == NULL || vn_a->amp == NULL);
15332414Saguzovsk 	if (!do_off) {
15342414Saguzovsk 		vn_a->offset = 0;
15352414Saguzovsk 	}
15362414Saguzovsk 
15372414Saguzovsk 	if (szcvec <= 1) {
15382414Saguzovsk 		seg = seg_alloc(as, addr, size);
15392414Saguzovsk 		if (seg == NULL) {
15402414Saguzovsk 			return (ENOMEM);
15412414Saguzovsk 		}
15422414Saguzovsk 		vn_a->szc = 0;
15432414Saguzovsk 		error = (*crfp)(seg, vn_a);
15442414Saguzovsk 		if (error != 0) {
15452414Saguzovsk 			seg_free(seg);
15464225Ssusans 		} else {
15474225Ssusans 			as->a_size += size;
1548*10169SSudheer.Abdul-Salam@Sun.COM 			/*
1549*10169SSudheer.Abdul-Salam@Sun.COM 			 * We'll count MAP_NORESERVE mappings as we fault
1550*10169SSudheer.Abdul-Salam@Sun.COM 			 * pages in.
1551*10169SSudheer.Abdul-Salam@Sun.COM 			 */
1552*10169SSudheer.Abdul-Salam@Sun.COM 			if (!SEG_IS_PARTIAL_RESV(seg))
1553*10169SSudheer.Abdul-Salam@Sun.COM 				as->a_resvsize += size;
15542414Saguzovsk 		}
15552414Saguzovsk 		return (error);
15562414Saguzovsk 	}
15572414Saguzovsk 
15582414Saguzovsk 	eaddr = addr + size;
15592414Saguzovsk 	save_szcvec = szcvec;
15602414Saguzovsk 	szcvec >>= 1;
15612414Saguzovsk 	szc = 0;
15622414Saguzovsk 	nszc = 0;
15632414Saguzovsk 	while (szcvec) {
15642414Saguzovsk 		if ((szcvec & 0x1) == 0) {
15652414Saguzovsk 			nszc++;
15662414Saguzovsk 			szcvec >>= 1;
15672414Saguzovsk 			continue;
15682414Saguzovsk 		}
15692414Saguzovsk 		nszc++;
15702414Saguzovsk 		pgsz = page_get_pagesize(nszc);
15712414Saguzovsk 		a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
15722414Saguzovsk 		if (a != addr) {
15732414Saguzovsk 			ASSERT(a < eaddr);
15742414Saguzovsk 			segsize = a - addr;
15752414Saguzovsk 			seg = seg_alloc(as, addr, segsize);
15762414Saguzovsk 			if (seg == NULL) {
15772414Saguzovsk 				return (ENOMEM);
15782414Saguzovsk 			}
15792414Saguzovsk 			vn_a->szc = szc;
15802414Saguzovsk 			error = (*crfp)(seg, vn_a);
15812414Saguzovsk 			if (error != 0) {
15822414Saguzovsk 				seg_free(seg);
15832414Saguzovsk 				return (error);
15842414Saguzovsk 			}
15854225Ssusans 			as->a_size += segsize;
1586*10169SSudheer.Abdul-Salam@Sun.COM 			/*
1587*10169SSudheer.Abdul-Salam@Sun.COM 			 * We'll count MAP_NORESERVE mappings as we fault
1588*10169SSudheer.Abdul-Salam@Sun.COM 			 * pages in.  We don't count /dev/null mappings at all.
1589*10169SSudheer.Abdul-Salam@Sun.COM 			 */
1590*10169SSudheer.Abdul-Salam@Sun.COM 			if (!SEG_IS_DEVNULL_MAPPING(seg) &&
1591*10169SSudheer.Abdul-Salam@Sun.COM 			    !SEG_IS_PARTIAL_RESV(seg))
1592*10169SSudheer.Abdul-Salam@Sun.COM 				as->a_resvsize += segsize;
1593*10169SSudheer.Abdul-Salam@Sun.COM 
15942414Saguzovsk 			*segcreated = 1;
15952414Saguzovsk 			if (do_off) {
15962414Saguzovsk 				vn_a->offset += segsize;
15972414Saguzovsk 			}
15982414Saguzovsk 			addr = a;
15992414Saguzovsk 		}
16002414Saguzovsk 		szc = nszc;
16012414Saguzovsk 		szcvec >>= 1;
16022414Saguzovsk 	}
16032414Saguzovsk 
16042414Saguzovsk 	ASSERT(addr < eaddr);
16052414Saguzovsk 	szcvec = save_szcvec | 1; /* add 8K pages */
16062414Saguzovsk 	while (szcvec) {
16072414Saguzovsk 		a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
16082414Saguzovsk 		ASSERT(a >= addr);
16092414Saguzovsk 		if (a != addr) {
16102414Saguzovsk 			segsize = a - addr;
16112414Saguzovsk 			seg = seg_alloc(as, addr, segsize);
16122414Saguzovsk 			if (seg == NULL) {
16132414Saguzovsk 				return (ENOMEM);
16142414Saguzovsk 			}
16152414Saguzovsk 			vn_a->szc = szc;
16162414Saguzovsk 			error = (*crfp)(seg, vn_a);
16172414Saguzovsk 			if (error != 0) {
16182414Saguzovsk 				seg_free(seg);
16192414Saguzovsk 				return (error);
16202414Saguzovsk 			}
16214225Ssusans 			as->a_size += segsize;
1622*10169SSudheer.Abdul-Salam@Sun.COM 			/*
1623*10169SSudheer.Abdul-Salam@Sun.COM 			 * We'll count MAP_NORESERVE mappings as we fault
1624*10169SSudheer.Abdul-Salam@Sun.COM 			 * pages in.  We don't count /dev/null mappings at all.
1625*10169SSudheer.Abdul-Salam@Sun.COM 			 */
1626*10169SSudheer.Abdul-Salam@Sun.COM 			if (!SEG_IS_DEVNULL_MAPPING(seg) &&
1627*10169SSudheer.Abdul-Salam@Sun.COM 			    !SEG_IS_PARTIAL_RESV(seg))
1628*10169SSudheer.Abdul-Salam@Sun.COM 				as->a_resvsize += segsize;
1629*10169SSudheer.Abdul-Salam@Sun.COM 
16302414Saguzovsk 			*segcreated = 1;
16312414Saguzovsk 			if (do_off) {
16322414Saguzovsk 				vn_a->offset += segsize;
16332414Saguzovsk 			}
16342414Saguzovsk 			addr = a;
16352414Saguzovsk 		}
16362414Saguzovsk 		szcvec &= ~(1 << szc);
16372414Saguzovsk 		if (szcvec) {
16382414Saguzovsk 			szc = highbit(szcvec) - 1;
16392414Saguzovsk 			pgsz = page_get_pagesize(szc);
16402414Saguzovsk 		}
16412414Saguzovsk 	}
16422414Saguzovsk 	ASSERT(addr == eaddr);
16432414Saguzovsk 
16442414Saguzovsk 	return (0);
16452414Saguzovsk }
16462414Saguzovsk 
16472414Saguzovsk static int
16482414Saguzovsk as_map_vnsegs(struct as *as, caddr_t addr, size_t size,
16492414Saguzovsk     int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
16502414Saguzovsk {
16512991Ssusans 	uint_t mapflags = vn_a->flags & (MAP_TEXT | MAP_INITDATA);
16522991Ssusans 	int type = (vn_a->type == MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM;
16532991Ssusans 	uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags,
16542991Ssusans 	    type, 0);
16552414Saguzovsk 	int error;
16562414Saguzovsk 	struct seg *seg;
16570Sstevel@tonic-gate 	struct vattr va;
16580Sstevel@tonic-gate 	u_offset_t eoff;
16590Sstevel@tonic-gate 	size_t save_size = 0;
16604426Saguzovsk 	extern size_t textrepl_size_thresh;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
16630Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
16640Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
16650Sstevel@tonic-gate 	ASSERT(vn_a->vp != NULL);
16660Sstevel@tonic-gate 	ASSERT(vn_a->amp == NULL);
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate again:
16690Sstevel@tonic-gate 	if (szcvec <= 1) {
16700Sstevel@tonic-gate 		seg = seg_alloc(as, addr, size);
16710Sstevel@tonic-gate 		if (seg == NULL) {
16720Sstevel@tonic-gate 			return (ENOMEM);
16730Sstevel@tonic-gate 		}
16740Sstevel@tonic-gate 		vn_a->szc = 0;
16750Sstevel@tonic-gate 		error = (*crfp)(seg, vn_a);
16760Sstevel@tonic-gate 		if (error != 0) {
16770Sstevel@tonic-gate 			seg_free(seg);
16784225Ssusans 		} else {
16794225Ssusans 			as->a_size += size;
1680*10169SSudheer.Abdul-Salam@Sun.COM 			/*
1681*10169SSudheer.Abdul-Salam@Sun.COM 			 * We'll count MAP_NORESERVE mappings as we fault
1682*10169SSudheer.Abdul-Salam@Sun.COM 			 * pages in.
1683*10169SSudheer.Abdul-Salam@Sun.COM 			 */
1684*10169SSudheer.Abdul-Salam@Sun.COM 			if (!SEG_IS_PARTIAL_RESV(seg))
1685*10169SSudheer.Abdul-Salam@Sun.COM 				as->a_resvsize += size;
16860Sstevel@tonic-gate 		}
16870Sstevel@tonic-gate 		return (error);
16880Sstevel@tonic-gate 	}
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
16915331Samw 	if (VOP_GETATTR(vn_a->vp, &va, ATTR_HINT, vn_a->cred, NULL) != 0) {
16920Sstevel@tonic-gate 		szcvec = 0;
16930Sstevel@tonic-gate 		goto again;
16940Sstevel@tonic-gate 	}
16950Sstevel@tonic-gate 	eoff = vn_a->offset & PAGEMASK;
16960Sstevel@tonic-gate 	if (eoff >= va.va_size) {
16970Sstevel@tonic-gate 		szcvec = 0;
16980Sstevel@tonic-gate 		goto again;
16990Sstevel@tonic-gate 	}
17000Sstevel@tonic-gate 	eoff += size;
17010Sstevel@tonic-gate 	if (btopr(va.va_size) < btopr(eoff)) {
17020Sstevel@tonic-gate 		save_size = size;
17030Sstevel@tonic-gate 		size = va.va_size - (vn_a->offset & PAGEMASK);
17040Sstevel@tonic-gate 		size = P2ROUNDUP_TYPED(size, PAGESIZE, size_t);
17052991Ssusans 		szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags,
17062991Ssusans 		    type, 0);
17070Sstevel@tonic-gate 		if (szcvec <= 1) {
17080Sstevel@tonic-gate 			size = save_size;
17090Sstevel@tonic-gate 			goto again;
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 
17134426Saguzovsk 	if (size > textrepl_size_thresh) {
17144426Saguzovsk 		vn_a->flags |= _MAP_TEXTREPL;
17154426Saguzovsk 	}
17162414Saguzovsk 	error = as_map_segvn_segs(as, addr, size, szcvec, crfp, vn_a,
17172414Saguzovsk 	    segcreated);
17182414Saguzovsk 	if (error != 0) {
17192414Saguzovsk 		return (error);
17200Sstevel@tonic-gate 	}
17210Sstevel@tonic-gate 	if (save_size) {
17222414Saguzovsk 		addr += size;
17230Sstevel@tonic-gate 		size = save_size - size;
17242414Saguzovsk 		szcvec = 0;
17250Sstevel@tonic-gate 		goto again;
17260Sstevel@tonic-gate 	}
17272414Saguzovsk 	return (0);
17282414Saguzovsk }
17290Sstevel@tonic-gate 
17302991Ssusans /*
17312991Ssusans  * as_map_ansegs: shared or private anonymous memory.  Note that the flags
17322991Ssusans  * passed to map_pgszvec cannot be MAP_INITDATA, for anon.
17332991Ssusans  */
17342414Saguzovsk static int
17352991Ssusans as_map_ansegs(struct as *as, caddr_t addr, size_t size,
17362414Saguzovsk     int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
17372414Saguzovsk {
17382991Ssusans 	uint_t szcvec;
17392991Ssusans 	uchar_t type;
17402991Ssusans 
17412991Ssusans 	ASSERT(vn_a->type == MAP_SHARED || vn_a->type == MAP_PRIVATE);
17422991Ssusans 	if (vn_a->type == MAP_SHARED) {
17432991Ssusans 		type = MAPPGSZC_SHM;
17442991Ssusans 	} else if (vn_a->type == MAP_PRIVATE) {
17452991Ssusans 		if (vn_a->szc == AS_MAP_HEAP) {
17462991Ssusans 			type = MAPPGSZC_HEAP;
17472991Ssusans 		} else if (vn_a->szc == AS_MAP_STACK) {
17482991Ssusans 			type = MAPPGSZC_STACK;
17492991Ssusans 		} else {
17502991Ssusans 			type = MAPPGSZC_PRIVM;
17512991Ssusans 		}
17522991Ssusans 	}
17532991Ssusans 	szcvec = map_pgszcvec(addr, size, vn_a->amp == NULL ?
17542991Ssusans 	    (uintptr_t)addr : (uintptr_t)P2ROUNDUP(vn_a->offset, PAGESIZE),
17552991Ssusans 	    (vn_a->flags & MAP_TEXT), type, 0);
17562414Saguzovsk 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
17572414Saguzovsk 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
17582414Saguzovsk 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
17592414Saguzovsk 	ASSERT(vn_a->vp == NULL);
17602414Saguzovsk 
17612414Saguzovsk 	return (as_map_segvn_segs(as, addr, size, szcvec,
17622414Saguzovsk 	    crfp, vn_a, segcreated));
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate int
17660Sstevel@tonic-gate as_map(struct as *as, caddr_t addr, size_t size, int (*crfp)(), void *argsp)
17670Sstevel@tonic-gate {
17681899Svsakar 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
17691899Svsakar 	return (as_map_locked(as, addr, size, crfp, argsp));
17701899Svsakar }
17711899Svsakar 
17721899Svsakar int
17731899Svsakar as_map_locked(struct as *as, caddr_t addr, size_t size, int (*crfp)(),
17741899Svsakar 		void *argsp)
17751899Svsakar {
17760Sstevel@tonic-gate 	struct seg *seg = NULL;
17770Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
17780Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
17790Sstevel@tonic-gate 	int error;
17802991Ssusans 	int unmap = 0;
17810Sstevel@tonic-gate 	struct proc *p = curproc;
17823183Ssusans 	struct segvn_crargs crargs;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
17850Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
17865084Sjohnlev 	    (size_t)raddr;
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	/*
17890Sstevel@tonic-gate 	 * check for wrap around
17900Sstevel@tonic-gate 	 */
17910Sstevel@tonic-gate 	if ((raddr + rsize < raddr) || (as->a_size > (ULONG_MAX - size))) {
17920Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
17930Sstevel@tonic-gate 		return (ENOMEM);
17940Sstevel@tonic-gate 	}
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
17970Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	if (as != &kas && as->a_size + rsize > (size_t)p->p_vmem_ctl) {
18000Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p,
18030Sstevel@tonic-gate 		    RCA_UNSAFE_ALL);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 		return (ENOMEM);
18060Sstevel@tonic-gate 	}
18070Sstevel@tonic-gate 
18082991Ssusans 	if (AS_MAP_CHECK_VNODE_LPOOB(crfp, argsp)) {
18093183Ssusans 		crargs = *(struct segvn_crargs *)argsp;
18103183Ssusans 		error = as_map_vnsegs(as, raddr, rsize, crfp, &crargs, &unmap);
18112991Ssusans 		if (error != 0) {
18122991Ssusans 			AS_LOCK_EXIT(as, &as->a_lock);
18132991Ssusans 			if (unmap) {
18142991Ssusans 				(void) as_unmap(as, addr, size);
18152991Ssusans 			}
18162991Ssusans 			return (error);
18172414Saguzovsk 		}
18182991Ssusans 	} else if (AS_MAP_CHECK_ANON_LPOOB(crfp, argsp)) {
18193183Ssusans 		crargs = *(struct segvn_crargs *)argsp;
18203183Ssusans 		error = as_map_ansegs(as, raddr, rsize, crfp, &crargs, &unmap);
18210Sstevel@tonic-gate 		if (error != 0) {
18220Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
18230Sstevel@tonic-gate 			if (unmap) {
18240Sstevel@tonic-gate 				(void) as_unmap(as, addr, size);
18250Sstevel@tonic-gate 			}
18260Sstevel@tonic-gate 			return (error);
18270Sstevel@tonic-gate 		}
18280Sstevel@tonic-gate 	} else {
18290Sstevel@tonic-gate 		seg = seg_alloc(as, addr, size);
18300Sstevel@tonic-gate 		if (seg == NULL) {
18310Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
18320Sstevel@tonic-gate 			return (ENOMEM);
18330Sstevel@tonic-gate 		}
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 		error = (*crfp)(seg, argsp);
18360Sstevel@tonic-gate 		if (error != 0) {
18370Sstevel@tonic-gate 			seg_free(seg);
18380Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
18390Sstevel@tonic-gate 			return (error);
18400Sstevel@tonic-gate 		}
18414225Ssusans 		/*
18424225Ssusans 		 * Add size now so as_unmap will work if as_ctl fails.
18434225Ssusans 		 */
18444225Ssusans 		as->a_size += rsize;
1845*10169SSudheer.Abdul-Salam@Sun.COM 		/*
1846*10169SSudheer.Abdul-Salam@Sun.COM 		 * We'll count MAP_NORESERVE mappings as we fault
1847*10169SSudheer.Abdul-Salam@Sun.COM 		 * pages in.  We don't count /dev/null mappings at all.
1848*10169SSudheer.Abdul-Salam@Sun.COM 		 */
1849*10169SSudheer.Abdul-Salam@Sun.COM 		if (!SEG_IS_DEVNULL_MAPPING(seg) &&
1850*10169SSudheer.Abdul-Salam@Sun.COM 		    !SEG_IS_PARTIAL_RESV(seg))
1851*10169SSudheer.Abdul-Salam@Sun.COM 			as->a_resvsize += rsize;
18520Sstevel@tonic-gate 	}
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	as_setwatch(as);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	/*
18570Sstevel@tonic-gate 	 * If the address space is locked,
18580Sstevel@tonic-gate 	 * establish memory locks for the new segment.
18590Sstevel@tonic-gate 	 */
18600Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
18610Sstevel@tonic-gate 	if (AS_ISPGLCK(as)) {
18620Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
18630Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
18640Sstevel@tonic-gate 		error = as_ctl(as, addr, size, MC_LOCK, 0, 0, NULL, 0);
18650Sstevel@tonic-gate 		if (error != 0)
18660Sstevel@tonic-gate 			(void) as_unmap(as, addr, size);
18670Sstevel@tonic-gate 	} else {
18680Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
18690Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
18700Sstevel@tonic-gate 	}
18710Sstevel@tonic-gate 	return (error);
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate /*
18760Sstevel@tonic-gate  * Delete all segments in the address space marked with S_PURGE.
18770Sstevel@tonic-gate  * This is currently used for Sparc V9 nofault ASI segments (seg_nf.c).
18780Sstevel@tonic-gate  * These segments are deleted as a first step before calls to as_gap(), so
18790Sstevel@tonic-gate  * that they don't affect mmap() or shmat().
18800Sstevel@tonic-gate  */
18810Sstevel@tonic-gate void
18820Sstevel@tonic-gate as_purge(struct as *as)
18830Sstevel@tonic-gate {
18840Sstevel@tonic-gate 	struct seg *seg;
18850Sstevel@tonic-gate 	struct seg *next_seg;
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	/*
18880Sstevel@tonic-gate 	 * the setting of NEEDSPURGE is protect by as_rangelock(), so
18890Sstevel@tonic-gate 	 * no need to grab a_contents mutex for this check
18900Sstevel@tonic-gate 	 */
18910Sstevel@tonic-gate 	if ((as->a_flags & AS_NEEDSPURGE) == 0)
18920Sstevel@tonic-gate 		return;
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
18950Sstevel@tonic-gate 	next_seg = NULL;
18960Sstevel@tonic-gate 	seg = AS_SEGFIRST(as);
18970Sstevel@tonic-gate 	while (seg != NULL) {
18980Sstevel@tonic-gate 		next_seg = AS_SEGNEXT(as, seg);
18990Sstevel@tonic-gate 		if (seg->s_flags & S_PURGE)
19000Sstevel@tonic-gate 			SEGOP_UNMAP(seg, seg->s_base, seg->s_size);
19010Sstevel@tonic-gate 		seg = next_seg;
19020Sstevel@tonic-gate 	}
19030Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
19060Sstevel@tonic-gate 	as->a_flags &= ~AS_NEEDSPURGE;
19070Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
19080Sstevel@tonic-gate }
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate /*
19115668Smec  * Find a hole within [*basep, *basep + *lenp), which contains a mappable
19125668Smec  * range of addresses at least "minlen" long, where the base of the range is
19135668Smec  * at "off" phase from an "align" boundary and there is space for a
19145668Smec  * "redzone"-sized redzone on eithe rside of the range.  Thus,
19155668Smec  * if align was 4M and off was 16k, the user wants a hole which will start
19165668Smec  * 16k into a 4M page.
19170Sstevel@tonic-gate  *
19180Sstevel@tonic-gate  * If flags specifies AH_HI, the hole will have the highest possible address
19190Sstevel@tonic-gate  * in the range.  We use the as->a_lastgap field to figure out where to
19200Sstevel@tonic-gate  * start looking for a gap.
19210Sstevel@tonic-gate  *
19220Sstevel@tonic-gate  * Otherwise, the gap will have the lowest possible address.
19230Sstevel@tonic-gate  *
19240Sstevel@tonic-gate  * If flags specifies AH_CONTAIN, the hole will contain the address addr.
19250Sstevel@tonic-gate  *
19265668Smec  * If an adequate hole is found, *basep and *lenp are set to reflect the part of
19275668Smec  * the hole that is within range, and 0 is returned. On failure, -1 is returned.
19280Sstevel@tonic-gate  *
19290Sstevel@tonic-gate  * NOTE: This routine is not correct when base+len overflows caddr_t.
19300Sstevel@tonic-gate  */
19310Sstevel@tonic-gate int
19325668Smec as_gap_aligned(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp,
19335668Smec     uint_t flags, caddr_t addr, size_t align, size_t redzone, size_t off)
19340Sstevel@tonic-gate {
19350Sstevel@tonic-gate 	caddr_t lobound = *basep;
19360Sstevel@tonic-gate 	caddr_t hibound = lobound + *lenp;
19370Sstevel@tonic-gate 	struct seg *lseg, *hseg;
19380Sstevel@tonic-gate 	caddr_t lo, hi;
19390Sstevel@tonic-gate 	int forward;
19400Sstevel@tonic-gate 	caddr_t save_base;
19410Sstevel@tonic-gate 	size_t save_len;
19426978Smec 	size_t save_minlen;
19436978Smec 	size_t save_redzone;
19446978Smec 	int fast_path = 1;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	save_base = *basep;
19470Sstevel@tonic-gate 	save_len = *lenp;
19486978Smec 	save_minlen = minlen;
19496978Smec 	save_redzone = redzone;
19506978Smec 
19516978Smec 	/*
19526978Smec 	 * For the first pass/fast_path, just add align and redzone into
19536978Smec 	 * minlen since if we get an allocation, we can guarantee that it
19546978Smec 	 * will fit the alignment and redzone requested.
19556978Smec 	 * This increases the chance that hibound will be adjusted to
19566978Smec 	 * a_lastgap->s_base which will likely allow us to find an
19576978Smec 	 * acceptable hole in the address space quicker.
19586978Smec 	 * If we can't find a hole with this fast_path, then we look for
19596978Smec 	 * smaller holes in which the alignment and offset may allow
19606978Smec 	 * the allocation to fit.
19616978Smec 	 */
19626978Smec 	minlen += align;
19636978Smec 	minlen += 2 * redzone;
19646978Smec 	redzone = 0;
19656978Smec 
19660Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
19670Sstevel@tonic-gate 	if (AS_SEGFIRST(as) == NULL) {
19685668Smec 		if (valid_va_range_aligned(basep, lenp, minlen, flags & AH_DIR,
19695668Smec 		    align, redzone, off)) {
19700Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
19710Sstevel@tonic-gate 			return (0);
19720Sstevel@tonic-gate 		} else {
19730Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
19740Sstevel@tonic-gate 			*basep = save_base;
19750Sstevel@tonic-gate 			*lenp = save_len;
19760Sstevel@tonic-gate 			return (-1);
19770Sstevel@tonic-gate 		}
19780Sstevel@tonic-gate 	}
19790Sstevel@tonic-gate 
19806978Smec retry:
19810Sstevel@tonic-gate 	/*
19820Sstevel@tonic-gate 	 * Set up to iterate over all the inter-segment holes in the given
19830Sstevel@tonic-gate 	 * direction.  lseg is NULL for the lowest-addressed hole and hseg is
19840Sstevel@tonic-gate 	 * NULL for the highest-addressed hole.  If moving backwards, we reset
19850Sstevel@tonic-gate 	 * sseg to denote the highest-addressed segment.
19860Sstevel@tonic-gate 	 */
19870Sstevel@tonic-gate 	forward = (flags & AH_DIR) == AH_LO;
19880Sstevel@tonic-gate 	if (forward) {
19890Sstevel@tonic-gate 		hseg = as_findseg(as, lobound, 1);
19900Sstevel@tonic-gate 		lseg = AS_SEGPREV(as, hseg);
19910Sstevel@tonic-gate 	} else {
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 		/*
19940Sstevel@tonic-gate 		 * If allocating at least as much as the last allocation,
19950Sstevel@tonic-gate 		 * use a_lastgap's base as a better estimate of hibound.
19960Sstevel@tonic-gate 		 */
19970Sstevel@tonic-gate 		if (as->a_lastgap &&
19980Sstevel@tonic-gate 		    minlen >= as->a_lastgap->s_size &&
19990Sstevel@tonic-gate 		    hibound >= as->a_lastgap->s_base)
20000Sstevel@tonic-gate 			hibound = as->a_lastgap->s_base;
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 		hseg = as_findseg(as, hibound, 1);
20030Sstevel@tonic-gate 		if (hseg->s_base + hseg->s_size < hibound) {
20040Sstevel@tonic-gate 			lseg = hseg;
20050Sstevel@tonic-gate 			hseg = NULL;
20060Sstevel@tonic-gate 		} else {
20070Sstevel@tonic-gate 			lseg = AS_SEGPREV(as, hseg);
20080Sstevel@tonic-gate 		}
20090Sstevel@tonic-gate 	}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	for (;;) {
20120Sstevel@tonic-gate 		/*
20130Sstevel@tonic-gate 		 * Set lo and hi to the hole's boundaries.  (We should really
20140Sstevel@tonic-gate 		 * use MAXADDR in place of hibound in the expression below,
20150Sstevel@tonic-gate 		 * but can't express it easily; using hibound in its place is
20160Sstevel@tonic-gate 		 * harmless.)
20170Sstevel@tonic-gate 		 */
20180Sstevel@tonic-gate 		lo = (lseg == NULL) ? 0 : lseg->s_base + lseg->s_size;
20190Sstevel@tonic-gate 		hi = (hseg == NULL) ? hibound : hseg->s_base;
20200Sstevel@tonic-gate 		/*
20210Sstevel@tonic-gate 		 * If the iteration has moved past the interval from lobound
20220Sstevel@tonic-gate 		 * to hibound it's pointless to continue.
20230Sstevel@tonic-gate 		 */
20240Sstevel@tonic-gate 		if ((forward && lo > hibound) || (!forward && hi < lobound))
20250Sstevel@tonic-gate 			break;
20260Sstevel@tonic-gate 		else if (lo > hibound || hi < lobound)
20270Sstevel@tonic-gate 			goto cont;
20280Sstevel@tonic-gate 		/*
20290Sstevel@tonic-gate 		 * Candidate hole lies at least partially within the allowable
20300Sstevel@tonic-gate 		 * range.  Restrict it to fall completely within that range,
20310Sstevel@tonic-gate 		 * i.e., to [max(lo, lobound), min(hi, hibound)].
20320Sstevel@tonic-gate 		 */
20330Sstevel@tonic-gate 		if (lo < lobound)
20340Sstevel@tonic-gate 			lo = lobound;
20350Sstevel@tonic-gate 		if (hi > hibound)
20360Sstevel@tonic-gate 			hi = hibound;
20370Sstevel@tonic-gate 		/*
20380Sstevel@tonic-gate 		 * Verify that the candidate hole is big enough and meets
20396978Smec 		 * hardware constraints.  If the hole is too small, no need
20406978Smec 		 * to do the further checks since they will fail.
20410Sstevel@tonic-gate 		 */
20420Sstevel@tonic-gate 		*basep = lo;
20430Sstevel@tonic-gate 		*lenp = hi - lo;
20446978Smec 		if (*lenp >= minlen && valid_va_range_aligned(basep, lenp,
20456978Smec 		    minlen, forward ? AH_LO : AH_HI, align, redzone, off) &&
20460Sstevel@tonic-gate 		    ((flags & AH_CONTAIN) == 0 ||
20470Sstevel@tonic-gate 		    (*basep <= addr && *basep + *lenp > addr))) {
20480Sstevel@tonic-gate 			if (!forward)
20490Sstevel@tonic-gate 				as->a_lastgap = hseg;
20500Sstevel@tonic-gate 			if (hseg != NULL)
20510Sstevel@tonic-gate 				as->a_lastgaphl = hseg;
20520Sstevel@tonic-gate 			else
20530Sstevel@tonic-gate 				as->a_lastgaphl = lseg;
20540Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
20550Sstevel@tonic-gate 			return (0);
20560Sstevel@tonic-gate 		}
20570Sstevel@tonic-gate 	cont:
20580Sstevel@tonic-gate 		/*
20590Sstevel@tonic-gate 		 * Move to the next hole.
20600Sstevel@tonic-gate 		 */
20610Sstevel@tonic-gate 		if (forward) {
20620Sstevel@tonic-gate 			lseg = hseg;
20630Sstevel@tonic-gate 			if (lseg == NULL)
20640Sstevel@tonic-gate 				break;
20650Sstevel@tonic-gate 			hseg = AS_SEGNEXT(as, hseg);
20660Sstevel@tonic-gate 		} else {
20670Sstevel@tonic-gate 			hseg = lseg;
20680Sstevel@tonic-gate 			if (hseg == NULL)
20690Sstevel@tonic-gate 				break;
20700Sstevel@tonic-gate 			lseg = AS_SEGPREV(as, lseg);
20710Sstevel@tonic-gate 		}
20720Sstevel@tonic-gate 	}
20736978Smec 	if (fast_path && (align != 0 || save_redzone != 0)) {
20746978Smec 		fast_path = 0;
20756978Smec 		minlen = save_minlen;
20766978Smec 		redzone = save_redzone;
20776978Smec 		goto retry;
20786978Smec 	}
20790Sstevel@tonic-gate 	*basep = save_base;
20800Sstevel@tonic-gate 	*lenp = save_len;
20810Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
20820Sstevel@tonic-gate 	return (-1);
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate /*
20865668Smec  * Find a hole of at least size minlen within [*basep, *basep + *lenp).
20875668Smec  *
20885668Smec  * If flags specifies AH_HI, the hole will have the highest possible address
20895668Smec  * in the range.  We use the as->a_lastgap field to figure out where to
20905668Smec  * start looking for a gap.
20915668Smec  *
20925668Smec  * Otherwise, the gap will have the lowest possible address.
20935668Smec  *
20945668Smec  * If flags specifies AH_CONTAIN, the hole will contain the address addr.
20955668Smec  *
20965668Smec  * If an adequate hole is found, base and len are set to reflect the part of
20975668Smec  * the hole that is within range, and 0 is returned, otherwise,
20985668Smec  * -1 is returned.
20995668Smec  *
21005668Smec  * NOTE: This routine is not correct when base+len overflows caddr_t.
21015668Smec  */
21025668Smec int
21035668Smec as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, uint_t flags,
21045668Smec     caddr_t addr)
21055668Smec {
21065668Smec 
21075668Smec 	return (as_gap_aligned(as, minlen, basep, lenp, flags, addr, 0, 0, 0));
21085668Smec }
21095668Smec 
21105668Smec /*
21110Sstevel@tonic-gate  * Return the next range within [base, base + len) that is backed
21120Sstevel@tonic-gate  * with "real memory".  Skip holes and non-seg_vn segments.
21130Sstevel@tonic-gate  * We're lazy and only return one segment at a time.
21140Sstevel@tonic-gate  */
21150Sstevel@tonic-gate int
21160Sstevel@tonic-gate as_memory(struct as *as, caddr_t *basep, size_t *lenp)
21170Sstevel@tonic-gate {
21180Sstevel@tonic-gate 	extern struct seg_ops segspt_shmops;	/* needs a header file */
21190Sstevel@tonic-gate 	struct seg *seg;
21200Sstevel@tonic-gate 	caddr_t addr, eaddr;
21210Sstevel@tonic-gate 	caddr_t segend;
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	addr = *basep;
21260Sstevel@tonic-gate 	eaddr = addr + *lenp;
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	seg = as_findseg(as, addr, 0);
21290Sstevel@tonic-gate 	if (seg != NULL)
21300Sstevel@tonic-gate 		addr = MAX(seg->s_base, addr);
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	for (;;) {
21330Sstevel@tonic-gate 		if (seg == NULL || addr >= eaddr || eaddr <= seg->s_base) {
21340Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
21350Sstevel@tonic-gate 			return (EINVAL);
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 		if (seg->s_ops == &segvn_ops) {
21390Sstevel@tonic-gate 			segend = seg->s_base + seg->s_size;
21400Sstevel@tonic-gate 			break;
21410Sstevel@tonic-gate 		}
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 		/*
21440Sstevel@tonic-gate 		 * We do ISM by looking into the private data
21450Sstevel@tonic-gate 		 * to determine the real size of the segment.
21460Sstevel@tonic-gate 		 */
21470Sstevel@tonic-gate 		if (seg->s_ops == &segspt_shmops) {
21480Sstevel@tonic-gate 			segend = seg->s_base + spt_realsize(seg);
21490Sstevel@tonic-gate 			if (addr < segend)
21500Sstevel@tonic-gate 				break;
21510Sstevel@tonic-gate 		}
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		seg = AS_SEGNEXT(as, seg);
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 		if (seg != NULL)
21560Sstevel@tonic-gate 			addr = seg->s_base;
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	*basep = addr;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	if (segend > eaddr)
21620Sstevel@tonic-gate 		*lenp = eaddr - addr;
21630Sstevel@tonic-gate 	else
21640Sstevel@tonic-gate 		*lenp = segend - addr;
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
21670Sstevel@tonic-gate 	return (0);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate /*
21710Sstevel@tonic-gate  * Swap the pages associated with the address space as out to
21720Sstevel@tonic-gate  * secondary storage, returning the number of bytes actually
21730Sstevel@tonic-gate  * swapped.
21740Sstevel@tonic-gate  *
21750Sstevel@tonic-gate  * The value returned is intended to correlate well with the process's
21760Sstevel@tonic-gate  * memory requirements.  Its usefulness for this purpose depends on
21770Sstevel@tonic-gate  * how well the segment-level routines do at returning accurate
21780Sstevel@tonic-gate  * information.
21790Sstevel@tonic-gate  */
21800Sstevel@tonic-gate size_t
21810Sstevel@tonic-gate as_swapout(struct as *as)
21820Sstevel@tonic-gate {
21830Sstevel@tonic-gate 	struct seg *seg;
21840Sstevel@tonic-gate 	size_t swpcnt = 0;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	/*
21870Sstevel@tonic-gate 	 * Kernel-only processes have given up their address
21880Sstevel@tonic-gate 	 * spaces.  Of course, we shouldn't be attempting to
21890Sstevel@tonic-gate 	 * swap out such processes in the first place...
21900Sstevel@tonic-gate 	 */
21910Sstevel@tonic-gate 	if (as == NULL)
21920Sstevel@tonic-gate 		return (0);
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	/* Prevent XHATs from attaching */
21970Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
21980Sstevel@tonic-gate 	AS_SETBUSY(as);
21990Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	/*
22030Sstevel@tonic-gate 	 * Free all mapping resources associated with the address
22040Sstevel@tonic-gate 	 * space.  The segment-level swapout routines capitalize
22050Sstevel@tonic-gate 	 * on this unmapping by scavanging pages that have become
22060Sstevel@tonic-gate 	 * unmapped here.
22070Sstevel@tonic-gate 	 */
22080Sstevel@tonic-gate 	hat_swapout(as->a_hat);
22090Sstevel@tonic-gate 	if (as->a_xhat != NULL)
22100Sstevel@tonic-gate 		xhat_swapout_all(as);
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
22130Sstevel@tonic-gate 	AS_CLRBUSY(as);
22140Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	/*
22170Sstevel@tonic-gate 	 * Call the swapout routines of all segments in the address
22180Sstevel@tonic-gate 	 * space to do the actual work, accumulating the amount of
22190Sstevel@tonic-gate 	 * space reclaimed.
22200Sstevel@tonic-gate 	 */
22210Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
22220Sstevel@tonic-gate 		struct seg_ops *ov = seg->s_ops;
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 		/*
22250Sstevel@tonic-gate 		 * We have to check to see if the seg has
22260Sstevel@tonic-gate 		 * an ops vector because the seg may have
22270Sstevel@tonic-gate 		 * been in the middle of being set up when
22280Sstevel@tonic-gate 		 * the process was picked for swapout.
22290Sstevel@tonic-gate 		 */
22300Sstevel@tonic-gate 		if ((ov != NULL) && (ov->swapout != NULL))
22310Sstevel@tonic-gate 			swpcnt += SEGOP_SWAPOUT(seg);
22320Sstevel@tonic-gate 	}
22330Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
22340Sstevel@tonic-gate 	return (swpcnt);
22350Sstevel@tonic-gate }
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate /*
22380Sstevel@tonic-gate  * Determine whether data from the mappings in interval [addr, addr + size)
22390Sstevel@tonic-gate  * are in the primary memory (core) cache.
22400Sstevel@tonic-gate  */
22410Sstevel@tonic-gate int
22420Sstevel@tonic-gate as_incore(struct as *as, caddr_t addr,
22430Sstevel@tonic-gate     size_t size, char *vec, size_t *sizep)
22440Sstevel@tonic-gate {
22450Sstevel@tonic-gate 	struct seg *seg;
22460Sstevel@tonic-gate 	size_t ssize;
22470Sstevel@tonic-gate 	caddr_t raddr;		/* rounded down addr */
22480Sstevel@tonic-gate 	size_t rsize;		/* rounded up size */
22490Sstevel@tonic-gate 	size_t isize;			/* iteration size */
22500Sstevel@tonic-gate 	int error = 0;		/* result, assume success */
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	*sizep = 0;
22530Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
22540Sstevel@tonic-gate 	rsize = ((((size_t)addr + size) + PAGEOFFSET) & PAGEMASK) -
22555084Sjohnlev 	    (size_t)raddr;
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
22580Sstevel@tonic-gate 		return (ENOMEM);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
22610Sstevel@tonic-gate 	seg = as_segat(as, raddr);
22620Sstevel@tonic-gate 	if (seg == NULL) {
22630Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
22640Sstevel@tonic-gate 		return (-1);
22650Sstevel@tonic-gate 	}
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
22680Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
22690Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
22700Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
22710Sstevel@tonic-gate 				error = -1;
22720Sstevel@tonic-gate 				break;
22730Sstevel@tonic-gate 			}
22740Sstevel@tonic-gate 		}
22750Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
22760Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
22770Sstevel@tonic-gate 		else
22780Sstevel@tonic-gate 			ssize = rsize;
22790Sstevel@tonic-gate 		*sizep += isize = SEGOP_INCORE(seg, raddr, ssize, vec);
22800Sstevel@tonic-gate 		if (isize != ssize) {
22810Sstevel@tonic-gate 			error = -1;
22820Sstevel@tonic-gate 			break;
22830Sstevel@tonic-gate 		}
22840Sstevel@tonic-gate 		vec += btopr(ssize);
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
22870Sstevel@tonic-gate 	return (error);
22880Sstevel@tonic-gate }
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate static void
22910Sstevel@tonic-gate as_segunlock(struct seg *seg, caddr_t addr, int attr,
22920Sstevel@tonic-gate 	ulong_t *bitmap, size_t position, size_t npages)
22930Sstevel@tonic-gate {
22940Sstevel@tonic-gate 	caddr_t	range_start;
22950Sstevel@tonic-gate 	size_t	pos1 = position;
22960Sstevel@tonic-gate 	size_t	pos2;
22970Sstevel@tonic-gate 	size_t	size;
22980Sstevel@tonic-gate 	size_t  end_pos = npages + position;
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	while (bt_range(bitmap, &pos1, &pos2, end_pos)) {
23010Sstevel@tonic-gate 		size = ptob((pos2 - pos1));
23020Sstevel@tonic-gate 		range_start = (caddr_t)((uintptr_t)addr +
23035084Sjohnlev 		    ptob(pos1 - position));
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 		(void) SEGOP_LOCKOP(seg, range_start, size, attr, MC_UNLOCK,
23065084Sjohnlev 		    (ulong_t *)NULL, (size_t)NULL);
23070Sstevel@tonic-gate 		pos1 = pos2;
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate static void
23120Sstevel@tonic-gate as_unlockerr(struct as *as, int attr, ulong_t *mlock_map,
23130Sstevel@tonic-gate 	caddr_t raddr, size_t rsize)
23140Sstevel@tonic-gate {
23150Sstevel@tonic-gate 	struct seg *seg = as_segat(as, raddr);
23160Sstevel@tonic-gate 	size_t ssize;
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	while (rsize != 0) {
23190Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size)
23200Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
23230Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
23240Sstevel@tonic-gate 		else
23250Sstevel@tonic-gate 			ssize = rsize;
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 		as_segunlock(seg, raddr, attr, mlock_map, 0, btopr(ssize));
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 		rsize -= ssize;
23300Sstevel@tonic-gate 		raddr += ssize;
23310Sstevel@tonic-gate 	}
23320Sstevel@tonic-gate }
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate /*
23350Sstevel@tonic-gate  * Cache control operations over the interval [addr, addr + size) in
23360Sstevel@tonic-gate  * address space "as".
23370Sstevel@tonic-gate  */
23380Sstevel@tonic-gate /*ARGSUSED*/
23390Sstevel@tonic-gate int
23400Sstevel@tonic-gate as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr,
23410Sstevel@tonic-gate     uintptr_t arg, ulong_t *lock_map, size_t pos)
23420Sstevel@tonic-gate {
23430Sstevel@tonic-gate 	struct seg *seg;	/* working segment */
23440Sstevel@tonic-gate 	caddr_t raddr;		/* rounded down addr */
23450Sstevel@tonic-gate 	caddr_t initraddr;	/* saved initial rounded down addr */
23460Sstevel@tonic-gate 	size_t rsize;		/* rounded up size */
23470Sstevel@tonic-gate 	size_t initrsize;	/* saved initial rounded up size */
23480Sstevel@tonic-gate 	size_t ssize;		/* size of seg */
23490Sstevel@tonic-gate 	int error = 0;			/* result */
23500Sstevel@tonic-gate 	size_t mlock_size;	/* size of bitmap */
23510Sstevel@tonic-gate 	ulong_t *mlock_map;	/* pointer to bitmap used */
23520Sstevel@tonic-gate 				/* to represent the locked */
23530Sstevel@tonic-gate 				/* pages. */
23540Sstevel@tonic-gate retry:
23550Sstevel@tonic-gate 	if (error == IE_RETRY)
23560Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
23570Sstevel@tonic-gate 	else
23580Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	/*
23610Sstevel@tonic-gate 	 * If these are address space lock/unlock operations, loop over
23620Sstevel@tonic-gate 	 * all segments in the address space, as appropriate.
23630Sstevel@tonic-gate 	 */
23640Sstevel@tonic-gate 	if (func == MC_LOCKAS) {
23650Sstevel@tonic-gate 		size_t npages, idx;
23660Sstevel@tonic-gate 		size_t rlen = 0;	/* rounded as length */
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 		idx = pos;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 		if (arg & MCL_FUTURE) {
23710Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
23720Sstevel@tonic-gate 			AS_SETPGLCK(as);
23730Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
23740Sstevel@tonic-gate 		}
23750Sstevel@tonic-gate 		if ((arg & MCL_CURRENT) == 0) {
23760Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
23770Sstevel@tonic-gate 			return (0);
23780Sstevel@tonic-gate 		}
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 		seg = AS_SEGFIRST(as);
23810Sstevel@tonic-gate 		if (seg == NULL) {
23820Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
23830Sstevel@tonic-gate 			return (0);
23840Sstevel@tonic-gate 		}
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 		do {
23870Sstevel@tonic-gate 			raddr = (caddr_t)((uintptr_t)seg->s_base &
23880Sstevel@tonic-gate 			    (uintptr_t)PAGEMASK);
23890Sstevel@tonic-gate 			rlen += (((uintptr_t)(seg->s_base + seg->s_size) +
23905084Sjohnlev 			    PAGEOFFSET) & PAGEMASK) - (uintptr_t)raddr;
23910Sstevel@tonic-gate 		} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 		mlock_size = BT_BITOUL(btopr(rlen));
23940Sstevel@tonic-gate 		if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size *
23955084Sjohnlev 		    sizeof (ulong_t), KM_NOSLEEP)) == NULL) {
23960Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
23970Sstevel@tonic-gate 				return (EAGAIN);
23980Sstevel@tonic-gate 		}
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 		for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
24010Sstevel@tonic-gate 			error = SEGOP_LOCKOP(seg, seg->s_base,
24020Sstevel@tonic-gate 			    seg->s_size, attr, MC_LOCK, mlock_map, pos);
24030Sstevel@tonic-gate 			if (error != 0)
24040Sstevel@tonic-gate 				break;
24050Sstevel@tonic-gate 			pos += seg_pages(seg);
24060Sstevel@tonic-gate 		}
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 		if (error) {
24090Sstevel@tonic-gate 			for (seg = AS_SEGFIRST(as); seg != NULL;
24105084Sjohnlev 			    seg = AS_SEGNEXT(as, seg)) {
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 				raddr = (caddr_t)((uintptr_t)seg->s_base &
24135084Sjohnlev 				    (uintptr_t)PAGEMASK);
24140Sstevel@tonic-gate 				npages = seg_pages(seg);
24150Sstevel@tonic-gate 				as_segunlock(seg, raddr, attr, mlock_map,
24165084Sjohnlev 				    idx, npages);
24170Sstevel@tonic-gate 				idx += npages;
24180Sstevel@tonic-gate 			}
24190Sstevel@tonic-gate 		}
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 		kmem_free(mlock_map, mlock_size * sizeof (ulong_t));
24220Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
24230Sstevel@tonic-gate 		goto lockerr;
24240Sstevel@tonic-gate 	} else if (func == MC_UNLOCKAS) {
24250Sstevel@tonic-gate 		mutex_enter(&as->a_contents);
24260Sstevel@tonic-gate 		AS_CLRPGLCK(as);
24270Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 		for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
24300Sstevel@tonic-gate 			error = SEGOP_LOCKOP(seg, seg->s_base,
24310Sstevel@tonic-gate 			    seg->s_size, attr, MC_UNLOCK, NULL, 0);
24320Sstevel@tonic-gate 			if (error != 0)
24330Sstevel@tonic-gate 				break;
24340Sstevel@tonic-gate 		}
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
24370Sstevel@tonic-gate 		goto lockerr;
24380Sstevel@tonic-gate 	}
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	/*
24410Sstevel@tonic-gate 	 * Normalize addresses and sizes.
24420Sstevel@tonic-gate 	 */
24430Sstevel@tonic-gate 	initraddr = raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
24440Sstevel@tonic-gate 	initrsize = rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
24455084Sjohnlev 	    (size_t)raddr;
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	if (raddr + rsize < raddr) {		/* check for wraparound */
24480Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
24490Sstevel@tonic-gate 		return (ENOMEM);
24500Sstevel@tonic-gate 	}
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	/*
24530Sstevel@tonic-gate 	 * Get initial segment.
24540Sstevel@tonic-gate 	 */
24550Sstevel@tonic-gate 	if ((seg = as_segat(as, raddr)) == NULL) {
24560Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
24570Sstevel@tonic-gate 		return (ENOMEM);
24580Sstevel@tonic-gate 	}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	if (func == MC_LOCK) {
24610Sstevel@tonic-gate 		mlock_size = BT_BITOUL(btopr(rsize));
24620Sstevel@tonic-gate 		if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size *
24635084Sjohnlev 		    sizeof (ulong_t), KM_NOSLEEP)) == NULL) {
24640Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
24650Sstevel@tonic-gate 				return (EAGAIN);
24660Sstevel@tonic-gate 		}
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * Loop over all segments.  If a hole in the address range is
24710Sstevel@tonic-gate 	 * discovered, then fail.  For each segment, perform the appropriate
24720Sstevel@tonic-gate 	 * control operation.
24730Sstevel@tonic-gate 	 */
24740Sstevel@tonic-gate 	while (rsize != 0) {
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 		/*
24770Sstevel@tonic-gate 		 * Make sure there's no hole, calculate the portion
24780Sstevel@tonic-gate 		 * of the next segment to be operated over.
24790Sstevel@tonic-gate 		 */
24800Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
24810Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
24820Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
24830Sstevel@tonic-gate 				if (func == MC_LOCK) {
24840Sstevel@tonic-gate 					as_unlockerr(as, attr, mlock_map,
24855084Sjohnlev 					    initraddr, initrsize - rsize);
24860Sstevel@tonic-gate 					kmem_free(mlock_map,
24875084Sjohnlev 					    mlock_size * sizeof (ulong_t));
24880Sstevel@tonic-gate 				}
24890Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
24900Sstevel@tonic-gate 				return (ENOMEM);
24910Sstevel@tonic-gate 			}
24920Sstevel@tonic-gate 		}
24930Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
24940Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
24950Sstevel@tonic-gate 		else
24960Sstevel@tonic-gate 			ssize = rsize;
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 		/*
24990Sstevel@tonic-gate 		 * Dispatch on specific function.
25000Sstevel@tonic-gate 		 */
25010Sstevel@tonic-gate 		switch (func) {
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		/*
25040Sstevel@tonic-gate 		 * Synchronize cached data from mappings with backing
25050Sstevel@tonic-gate 		 * objects.
25060Sstevel@tonic-gate 		 */
25070Sstevel@tonic-gate 		case MC_SYNC:
25080Sstevel@tonic-gate 			if (error = SEGOP_SYNC(seg, raddr, ssize,
25090Sstevel@tonic-gate 			    attr, (uint_t)arg)) {
25100Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
25110Sstevel@tonic-gate 				return (error);
25120Sstevel@tonic-gate 			}
25130Sstevel@tonic-gate 			break;
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 		/*
25160Sstevel@tonic-gate 		 * Lock pages in memory.
25170Sstevel@tonic-gate 		 */
25180Sstevel@tonic-gate 		case MC_LOCK:
25190Sstevel@tonic-gate 			if (error = SEGOP_LOCKOP(seg, raddr, ssize,
25205084Sjohnlev 			    attr, func, mlock_map, pos)) {
25210Sstevel@tonic-gate 				as_unlockerr(as, attr, mlock_map, initraddr,
25225084Sjohnlev 				    initrsize - rsize + ssize);
25230Sstevel@tonic-gate 				kmem_free(mlock_map, mlock_size *
25245084Sjohnlev 				    sizeof (ulong_t));
25250Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
25260Sstevel@tonic-gate 				goto lockerr;
25270Sstevel@tonic-gate 			}
25280Sstevel@tonic-gate 			break;
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 		/*
25310Sstevel@tonic-gate 		 * Unlock mapped pages.
25320Sstevel@tonic-gate 		 */
25330Sstevel@tonic-gate 		case MC_UNLOCK:
25340Sstevel@tonic-gate 			(void) SEGOP_LOCKOP(seg, raddr, ssize, attr, func,
25355084Sjohnlev 			    (ulong_t *)NULL, (size_t)NULL);
25360Sstevel@tonic-gate 			break;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 		/*
25390Sstevel@tonic-gate 		 * Store VM advise for mapped pages in segment layer.
25400Sstevel@tonic-gate 		 */
25410Sstevel@tonic-gate 		case MC_ADVISE:
25420Sstevel@tonic-gate 			error = SEGOP_ADVISE(seg, raddr, ssize, (uint_t)arg);
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 			/*
25450Sstevel@tonic-gate 			 * Check for regular errors and special retry error
25460Sstevel@tonic-gate 			 */
25470Sstevel@tonic-gate 			if (error) {
25480Sstevel@tonic-gate 				if (error == IE_RETRY) {
25490Sstevel@tonic-gate 					/*
25500Sstevel@tonic-gate 					 * Need to acquire writers lock, so
25510Sstevel@tonic-gate 					 * have to drop readers lock and start
25520Sstevel@tonic-gate 					 * all over again
25530Sstevel@tonic-gate 					 */
25540Sstevel@tonic-gate 					AS_LOCK_EXIT(as, &as->a_lock);
25550Sstevel@tonic-gate 					goto retry;
25560Sstevel@tonic-gate 				} else if (error == IE_REATTACH) {
25570Sstevel@tonic-gate 					/*
25580Sstevel@tonic-gate 					 * Find segment for current address
25590Sstevel@tonic-gate 					 * because current segment just got
25600Sstevel@tonic-gate 					 * split or concatenated
25610Sstevel@tonic-gate 					 */
25620Sstevel@tonic-gate 					seg = as_segat(as, raddr);
25630Sstevel@tonic-gate 					if (seg == NULL) {
25640Sstevel@tonic-gate 						AS_LOCK_EXIT(as, &as->a_lock);
25650Sstevel@tonic-gate 						return (ENOMEM);
25660Sstevel@tonic-gate 					}
25670Sstevel@tonic-gate 				} else {
25680Sstevel@tonic-gate 					/*
25690Sstevel@tonic-gate 					 * Regular error
25700Sstevel@tonic-gate 					 */
25710Sstevel@tonic-gate 					AS_LOCK_EXIT(as, &as->a_lock);
25720Sstevel@tonic-gate 					return (error);
25730Sstevel@tonic-gate 				}
25740Sstevel@tonic-gate 			}
25750Sstevel@tonic-gate 			break;
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 		/*
25780Sstevel@tonic-gate 		 * Can't happen.
25790Sstevel@tonic-gate 		 */
25800Sstevel@tonic-gate 		default:
25810Sstevel@tonic-gate 			panic("as_ctl: bad operation %d", func);
25820Sstevel@tonic-gate 			/*NOTREACHED*/
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 		rsize -= ssize;
25860Sstevel@tonic-gate 		raddr += ssize;
25870Sstevel@tonic-gate 	}
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	if (func == MC_LOCK)
25900Sstevel@tonic-gate 		kmem_free(mlock_map, mlock_size * sizeof (ulong_t));
25910Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
25920Sstevel@tonic-gate 	return (0);
25930Sstevel@tonic-gate lockerr:
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 	/*
25960Sstevel@tonic-gate 	 * If the lower levels returned EDEADLK for a segment lockop,
25970Sstevel@tonic-gate 	 * it means that we should retry the operation.  Let's wait
25980Sstevel@tonic-gate 	 * a bit also to let the deadlock causing condition clear.
25990Sstevel@tonic-gate 	 * This is part of a gross hack to work around a design flaw
26000Sstevel@tonic-gate 	 * in the ufs/sds logging code and should go away when the
26010Sstevel@tonic-gate 	 * logging code is re-designed to fix the problem. See bug
26020Sstevel@tonic-gate 	 * 4125102 for details of the problem.
26030Sstevel@tonic-gate 	 */
26040Sstevel@tonic-gate 	if (error == EDEADLK) {
26050Sstevel@tonic-gate 		delay(deadlk_wait);
26060Sstevel@tonic-gate 		error = 0;
26070Sstevel@tonic-gate 		goto retry;
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 	return (error);
26100Sstevel@tonic-gate }
26110Sstevel@tonic-gate 
26125084Sjohnlev int
26135084Sjohnlev fc_decode(faultcode_t fault_err)
26140Sstevel@tonic-gate {
26150Sstevel@tonic-gate 	int error = 0;
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	switch (FC_CODE(fault_err)) {
26180Sstevel@tonic-gate 	case FC_OBJERR:
26190Sstevel@tonic-gate 		error = FC_ERRNO(fault_err);
26200Sstevel@tonic-gate 		break;
26210Sstevel@tonic-gate 	case FC_PROT:
26220Sstevel@tonic-gate 		error = EACCES;
26230Sstevel@tonic-gate 		break;
26240Sstevel@tonic-gate 	default:
26250Sstevel@tonic-gate 		error = EFAULT;
26260Sstevel@tonic-gate 		break;
26270Sstevel@tonic-gate 	}
26280Sstevel@tonic-gate 	return (error);
26290Sstevel@tonic-gate }
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate /*
26326695Saguzovsk  * Pagelock pages from a range that spans more than 1 segment.  Obtain shadow
26336695Saguzovsk  * lists from each segment and copy them to one contiguous shadow list (plist)
26346695Saguzovsk  * as expected by the caller.  Save pointers to per segment shadow lists at
26356695Saguzovsk  * the tail of plist so that they can be used during as_pageunlock().
26366695Saguzovsk  */
26376695Saguzovsk static int
26386695Saguzovsk as_pagelock_segs(struct as *as, struct seg *seg, struct page ***ppp,
26396695Saguzovsk     caddr_t addr, size_t size, enum seg_rw rw)
26406695Saguzovsk {
26416695Saguzovsk 	caddr_t sv_addr = addr;
26426695Saguzovsk 	size_t sv_size = size;
26436695Saguzovsk 	struct seg *sv_seg = seg;
26446695Saguzovsk 	ulong_t segcnt = 1;
26456695Saguzovsk 	ulong_t cnt;
26466695Saguzovsk 	size_t ssize;
26476695Saguzovsk 	pgcnt_t npages = btop(size);
26486695Saguzovsk 	page_t **plist;
26496695Saguzovsk 	page_t **pl;
26506695Saguzovsk 	int error;
26516695Saguzovsk 	caddr_t eaddr;
26526695Saguzovsk 	faultcode_t fault_err = 0;
26536695Saguzovsk 	pgcnt_t pl_off;
26546695Saguzovsk 	extern struct seg_ops segspt_shmops;
26556695Saguzovsk 
26566695Saguzovsk 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
26576695Saguzovsk 	ASSERT(seg != NULL);
26586695Saguzovsk 	ASSERT(addr >= seg->s_base && addr < seg->s_base + seg->s_size);
26596695Saguzovsk 	ASSERT(addr + size > seg->s_base + seg->s_size);
26606695Saguzovsk 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
26616695Saguzovsk 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
26626695Saguzovsk 
26636695Saguzovsk 	/*
26646695Saguzovsk 	 * Count the number of segments covered by the range we are about to
26656695Saguzovsk 	 * lock. The segment count is used to size the shadow list we return
26666695Saguzovsk 	 * back to the caller.
26676695Saguzovsk 	 */
26686695Saguzovsk 	for (; size != 0; size -= ssize, addr += ssize) {
26696695Saguzovsk 		if (addr >= seg->s_base + seg->s_size) {
26706695Saguzovsk 
26716695Saguzovsk 			seg = AS_SEGNEXT(as, seg);
26726695Saguzovsk 			if (seg == NULL || addr != seg->s_base) {
26736695Saguzovsk 				AS_LOCK_EXIT(as, &as->a_lock);
26746695Saguzovsk 				return (EFAULT);
26756695Saguzovsk 			}
26766695Saguzovsk 			/*
26776695Saguzovsk 			 * Do a quick check if subsequent segments
26786695Saguzovsk 			 * will most likely support pagelock.
26796695Saguzovsk 			 */
26806695Saguzovsk 			if (seg->s_ops == &segvn_ops) {
26816695Saguzovsk 				vnode_t *vp;
26826695Saguzovsk 
26836695Saguzovsk 				if (SEGOP_GETVP(seg, addr, &vp) != 0 ||
26846695Saguzovsk 				    vp != NULL) {
26856695Saguzovsk 					AS_LOCK_EXIT(as, &as->a_lock);
26866695Saguzovsk 					goto slow;
26876695Saguzovsk 				}
26886695Saguzovsk 			} else if (seg->s_ops != &segspt_shmops) {
26896695Saguzovsk 				AS_LOCK_EXIT(as, &as->a_lock);
26906695Saguzovsk 				goto slow;
26916695Saguzovsk 			}
26926695Saguzovsk 			segcnt++;
26936695Saguzovsk 		}
26946695Saguzovsk 		if (addr + size > seg->s_base + seg->s_size) {
26956695Saguzovsk 			ssize = seg->s_base + seg->s_size - addr;
26966695Saguzovsk 		} else {
26976695Saguzovsk 			ssize = size;
26986695Saguzovsk 		}
26996695Saguzovsk 	}
27006695Saguzovsk 	ASSERT(segcnt > 1);
27016695Saguzovsk 
27026695Saguzovsk 	plist = kmem_zalloc((npages + segcnt) * sizeof (page_t *), KM_SLEEP);
27036695Saguzovsk 
27046695Saguzovsk 	addr = sv_addr;
27056695Saguzovsk 	size = sv_size;
27066695Saguzovsk 	seg = sv_seg;
27076695Saguzovsk 
27086695Saguzovsk 	for (cnt = 0, pl_off = 0; size != 0; size -= ssize, addr += ssize) {
27096695Saguzovsk 		if (addr >= seg->s_base + seg->s_size) {
27106695Saguzovsk 			seg = AS_SEGNEXT(as, seg);
27116695Saguzovsk 			ASSERT(seg != NULL && addr == seg->s_base);
27126695Saguzovsk 			cnt++;
27136695Saguzovsk 			ASSERT(cnt < segcnt);
27146695Saguzovsk 		}
27156695Saguzovsk 		if (addr + size > seg->s_base + seg->s_size) {
27166695Saguzovsk 			ssize = seg->s_base + seg->s_size - addr;
27176695Saguzovsk 		} else {
27186695Saguzovsk 			ssize = size;
27196695Saguzovsk 		}
27206695Saguzovsk 		pl = &plist[npages + cnt];
27216695Saguzovsk 		error = SEGOP_PAGELOCK(seg, addr, ssize, (page_t ***)pl,
27226695Saguzovsk 		    L_PAGELOCK, rw);
27236695Saguzovsk 		if (error) {
27246695Saguzovsk 			break;
27256695Saguzovsk 		}
27266695Saguzovsk 		ASSERT(plist[npages + cnt] != NULL);
27276695Saguzovsk 		ASSERT(pl_off + btop(ssize) <= npages);
27286695Saguzovsk 		bcopy(plist[npages + cnt], &plist[pl_off],
27296695Saguzovsk 		    btop(ssize) * sizeof (page_t *));
27306695Saguzovsk 		pl_off += btop(ssize);
27316695Saguzovsk 	}
27326695Saguzovsk 
27336695Saguzovsk 	if (size == 0) {
27346695Saguzovsk 		AS_LOCK_EXIT(as, &as->a_lock);
27356695Saguzovsk 		ASSERT(cnt == segcnt - 1);
27366695Saguzovsk 		*ppp = plist;
27376695Saguzovsk 		return (0);
27386695Saguzovsk 	}
27396695Saguzovsk 
27406695Saguzovsk 	/*
27416695Saguzovsk 	 * one of pagelock calls failed. The error type is in error variable.
27426695Saguzovsk 	 * Unlock what we've locked so far and retry with F_SOFTLOCK if error
27436695Saguzovsk 	 * type is either EFAULT or ENOTSUP. Otherwise just return the error
27446695Saguzovsk 	 * back to the caller.
27456695Saguzovsk 	 */
27466695Saguzovsk 
27476695Saguzovsk 	eaddr = addr;
27486695Saguzovsk 	seg = sv_seg;
27496695Saguzovsk 
27506695Saguzovsk 	for (cnt = 0, addr = sv_addr; addr < eaddr; addr += ssize) {
27516695Saguzovsk 		if (addr >= seg->s_base + seg->s_size) {
27526695Saguzovsk 			seg = AS_SEGNEXT(as, seg);
27536695Saguzovsk 			ASSERT(seg != NULL && addr == seg->s_base);
27546695Saguzovsk 			cnt++;
27556695Saguzovsk 			ASSERT(cnt < segcnt);
27566695Saguzovsk 		}
27576695Saguzovsk 		if (eaddr > seg->s_base + seg->s_size) {
27586695Saguzovsk 			ssize = seg->s_base + seg->s_size - addr;
27596695Saguzovsk 		} else {
27606695Saguzovsk 			ssize = eaddr - addr;
27616695Saguzovsk 		}
27626695Saguzovsk 		pl = &plist[npages + cnt];
27636695Saguzovsk 		ASSERT(*pl != NULL);
27646695Saguzovsk 		(void) SEGOP_PAGELOCK(seg, addr, ssize, (page_t ***)pl,
27656695Saguzovsk 		    L_PAGEUNLOCK, rw);
27666695Saguzovsk 	}
27676695Saguzovsk 
27686695Saguzovsk 	AS_LOCK_EXIT(as, &as->a_lock);
27696695Saguzovsk 
27706695Saguzovsk 	kmem_free(plist, (npages + segcnt) * sizeof (page_t *));
27716695Saguzovsk 
27726695Saguzovsk 	if (error != ENOTSUP && error != EFAULT) {
27736695Saguzovsk 		return (error);
27746695Saguzovsk 	}
27756695Saguzovsk 
27766695Saguzovsk slow:
27776695Saguzovsk 	/*
27786695Saguzovsk 	 * If we are here because pagelock failed due to the need to cow fault
27796695Saguzovsk 	 * in the pages we want to lock F_SOFTLOCK will do this job and in
27806695Saguzovsk 	 * next as_pagelock() call for this address range pagelock will
27816695Saguzovsk 	 * hopefully succeed.
27826695Saguzovsk 	 */
27836695Saguzovsk 	fault_err = as_fault(as->a_hat, as, sv_addr, sv_size, F_SOFTLOCK, rw);
27846695Saguzovsk 	if (fault_err != 0) {
27856695Saguzovsk 		return (fc_decode(fault_err));
27866695Saguzovsk 	}
27876695Saguzovsk 	*ppp = NULL;
27886695Saguzovsk 
27896695Saguzovsk 	return (0);
27906695Saguzovsk }
27916695Saguzovsk 
27926695Saguzovsk /*
27930Sstevel@tonic-gate  * lock pages in a given address space. Return shadow list. If
27940Sstevel@tonic-gate  * the list is NULL, the MMU mapping is also locked.
27950Sstevel@tonic-gate  */
27960Sstevel@tonic-gate int
27970Sstevel@tonic-gate as_pagelock(struct as *as, struct page ***ppp, caddr_t addr,
27980Sstevel@tonic-gate     size_t size, enum seg_rw rw)
27990Sstevel@tonic-gate {
28000Sstevel@tonic-gate 	size_t rsize;
28010Sstevel@tonic-gate 	caddr_t raddr;
28020Sstevel@tonic-gate 	faultcode_t fault_err;
28030Sstevel@tonic-gate 	struct seg *seg;
28046695Saguzovsk 	int err;
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_START,
28070Sstevel@tonic-gate 	    "as_pagelock_start: addr %p size %ld", addr, size);
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
28100Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
28115084Sjohnlev 	    (size_t)raddr;
28126695Saguzovsk 
28130Sstevel@tonic-gate 	/*
28140Sstevel@tonic-gate 	 * if the request crosses two segments let
28150Sstevel@tonic-gate 	 * as_fault handle it.
28160Sstevel@tonic-gate 	 */
28170Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
28186695Saguzovsk 
28196695Saguzovsk 	seg = as_segat(as, raddr);
28206695Saguzovsk 	if (seg == NULL) {
28210Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
28226695Saguzovsk 		return (EFAULT);
28236695Saguzovsk 	}
28246695Saguzovsk 	ASSERT(raddr >= seg->s_base && raddr < seg->s_base + seg->s_size);
28256695Saguzovsk 	if (raddr + rsize > seg->s_base + seg->s_size) {
28266695Saguzovsk 		return (as_pagelock_segs(as, seg, ppp, raddr, rsize, rw));
28276695Saguzovsk 	}
28286695Saguzovsk 	if (raddr + rsize <= raddr) {
28296695Saguzovsk 		AS_LOCK_EXIT(as, &as->a_lock);
28306695Saguzovsk 		return (EFAULT);
28310Sstevel@tonic-gate 	}
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_START,
28340Sstevel@tonic-gate 	    "seg_lock_1_start: raddr %p rsize %ld", raddr, rsize);
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 	/*
28370Sstevel@tonic-gate 	 * try to lock pages and pass back shadow list
28380Sstevel@tonic-gate 	 */
28396695Saguzovsk 	err = SEGOP_PAGELOCK(seg, raddr, rsize, ppp, L_PAGELOCK, rw);
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_END, "seg_lock_1_end");
28426695Saguzovsk 
28430Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
28446695Saguzovsk 
28456695Saguzovsk 	if (err == 0 || (err != ENOTSUP && err != EFAULT)) {
28466695Saguzovsk 		return (err);
28470Sstevel@tonic-gate 	}
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 	/*
28506695Saguzovsk 	 * Use F_SOFTLOCK to lock the pages because pagelock failed either due
28516695Saguzovsk 	 * to no pagelock support for this segment or pages need to be cow
28526695Saguzovsk 	 * faulted in. If fault is needed F_SOFTLOCK will do this job for
28536695Saguzovsk 	 * this as_pagelock() call and in the next as_pagelock() call for the
28546695Saguzovsk 	 * same address range pagelock call will hopefull succeed.
28550Sstevel@tonic-gate 	 */
28560Sstevel@tonic-gate 	fault_err = as_fault(as->a_hat, as, addr, size, F_SOFTLOCK, rw);
28570Sstevel@tonic-gate 	if (fault_err != 0) {
28585084Sjohnlev 		return (fc_decode(fault_err));
28590Sstevel@tonic-gate 	}
28600Sstevel@tonic-gate 	*ppp = NULL;
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_END, "as_pagelock_end");
28630Sstevel@tonic-gate 	return (0);
28640Sstevel@tonic-gate }
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate /*
28676695Saguzovsk  * unlock pages locked by as_pagelock_segs().  Retrieve per segment shadow
28686695Saguzovsk  * lists from the end of plist and call pageunlock interface for each segment.
28696695Saguzovsk  * Drop as lock and free plist.
28706695Saguzovsk  */
28716695Saguzovsk static void
28726695Saguzovsk as_pageunlock_segs(struct as *as, struct seg *seg, caddr_t addr, size_t size,
28736695Saguzovsk     struct page **plist, enum seg_rw rw)
28746695Saguzovsk {
28756695Saguzovsk 	ulong_t cnt;
28766695Saguzovsk 	caddr_t eaddr = addr + size;
28776695Saguzovsk 	pgcnt_t npages = btop(size);
28786695Saguzovsk 	size_t ssize;
28796695Saguzovsk 	page_t **pl;
28806695Saguzovsk 
28816695Saguzovsk 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
28826695Saguzovsk 	ASSERT(seg != NULL);
28836695Saguzovsk 	ASSERT(addr >= seg->s_base && addr < seg->s_base + seg->s_size);
28846695Saguzovsk 	ASSERT(addr + size > seg->s_base + seg->s_size);
28856695Saguzovsk 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
28866695Saguzovsk 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
28876695Saguzovsk 	ASSERT(plist != NULL);
28886695Saguzovsk 
28896695Saguzovsk 	for (cnt = 0; addr < eaddr; addr += ssize) {
28906695Saguzovsk 		if (addr >= seg->s_base + seg->s_size) {
28916695Saguzovsk 			seg = AS_SEGNEXT(as, seg);
28926695Saguzovsk 			ASSERT(seg != NULL && addr == seg->s_base);
28936695Saguzovsk 			cnt++;
28946695Saguzovsk 		}
28956695Saguzovsk 		if (eaddr > seg->s_base + seg->s_size) {
28966695Saguzovsk 			ssize = seg->s_base + seg->s_size - addr;
28976695Saguzovsk 		} else {
28986695Saguzovsk 			ssize = eaddr - addr;
28996695Saguzovsk 		}
29006695Saguzovsk 		pl = &plist[npages + cnt];
29016695Saguzovsk 		ASSERT(*pl != NULL);
29026695Saguzovsk 		(void) SEGOP_PAGELOCK(seg, addr, ssize, (page_t ***)pl,
29036695Saguzovsk 		    L_PAGEUNLOCK, rw);
29046695Saguzovsk 	}
29056695Saguzovsk 	ASSERT(cnt > 0);
29066695Saguzovsk 	AS_LOCK_EXIT(as, &as->a_lock);
29076695Saguzovsk 
29086695Saguzovsk 	cnt++;
29096695Saguzovsk 	kmem_free(plist, (npages + cnt) * sizeof (page_t *));
29106695Saguzovsk }
29116695Saguzovsk 
29126695Saguzovsk /*
29130Sstevel@tonic-gate  * unlock pages in a given address range
29140Sstevel@tonic-gate  */
29150Sstevel@tonic-gate void
29160Sstevel@tonic-gate as_pageunlock(struct as *as, struct page **pp, caddr_t addr, size_t size,
29170Sstevel@tonic-gate     enum seg_rw rw)
29180Sstevel@tonic-gate {
29190Sstevel@tonic-gate 	struct seg *seg;
29200Sstevel@tonic-gate 	size_t rsize;
29210Sstevel@tonic-gate 	caddr_t raddr;
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_START,
29240Sstevel@tonic-gate 	    "as_pageunlock_start: addr %p size %ld", addr, size);
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	/*
29270Sstevel@tonic-gate 	 * if the shadow list is NULL, as_pagelock was
29280Sstevel@tonic-gate 	 * falling back to as_fault
29290Sstevel@tonic-gate 	 */
29300Sstevel@tonic-gate 	if (pp == NULL) {
29310Sstevel@tonic-gate 		(void) as_fault(as->a_hat, as, addr, size, F_SOFTUNLOCK, rw);
29320Sstevel@tonic-gate 		return;
29330Sstevel@tonic-gate 	}
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
29360Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
29375084Sjohnlev 	    (size_t)raddr;
29386695Saguzovsk 
29396695Saguzovsk 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
29406695Saguzovsk 	seg = as_segat(as, raddr);
29416695Saguzovsk 	ASSERT(seg != NULL);
29426695Saguzovsk 
29436695Saguzovsk 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_UNLOCK_START,
29446695Saguzovsk 	    "seg_unlock_start: raddr %p rsize %ld", raddr, rsize);
29456695Saguzovsk 
29466695Saguzovsk 	ASSERT(raddr >= seg->s_base && raddr < seg->s_base + seg->s_size);
29476695Saguzovsk 	if (raddr + rsize <= seg->s_base + seg->s_size) {
29486695Saguzovsk 		SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGEUNLOCK, rw);
29496695Saguzovsk 	} else {
29506695Saguzovsk 		as_pageunlock_segs(as, seg, raddr, rsize, pp, rw);
29516695Saguzovsk 		return;
29526695Saguzovsk 	}
29536695Saguzovsk 	AS_LOCK_EXIT(as, &as->a_lock);
29546695Saguzovsk 	TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_END, "as_pageunlock_end");
29550Sstevel@tonic-gate }
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate int
29580Sstevel@tonic-gate as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc,
29590Sstevel@tonic-gate     boolean_t wait)
29600Sstevel@tonic-gate {
29610Sstevel@tonic-gate 	struct seg *seg;
29620Sstevel@tonic-gate 	size_t ssize;
29630Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
29640Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
29650Sstevel@tonic-gate 	int error = 0;
29660Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate setpgsz_top:
29690Sstevel@tonic-gate 	if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(size, pgsz)) {
29700Sstevel@tonic-gate 		return (EINVAL);
29710Sstevel@tonic-gate 	}
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate 	raddr = addr;
29740Sstevel@tonic-gate 	rsize = size;
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
29770Sstevel@tonic-gate 		return (ENOMEM);
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
29800Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, rsize);
29810Sstevel@tonic-gate 	seg = as_segat(as, raddr);
29820Sstevel@tonic-gate 	if (seg == NULL) {
29830Sstevel@tonic-gate 		as_setwatch(as);
29840Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
29850Sstevel@tonic-gate 		return (ENOMEM);
29860Sstevel@tonic-gate 	}
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
29890Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
29900Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
29910Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
29920Sstevel@tonic-gate 				error = ENOMEM;
29930Sstevel@tonic-gate 				break;
29940Sstevel@tonic-gate 			}
29950Sstevel@tonic-gate 		}
29960Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
29970Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
29980Sstevel@tonic-gate 		} else {
29990Sstevel@tonic-gate 			ssize = rsize;
30000Sstevel@tonic-gate 		}
30010Sstevel@tonic-gate 
30026695Saguzovsk retry:
30030Sstevel@tonic-gate 		error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc);
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 		if (error == IE_NOMEM) {
30060Sstevel@tonic-gate 			error = EAGAIN;
30070Sstevel@tonic-gate 			break;
30080Sstevel@tonic-gate 		}
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 		if (error == IE_RETRY) {
30110Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
30120Sstevel@tonic-gate 			goto setpgsz_top;
30130Sstevel@tonic-gate 		}
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 		if (error == ENOTSUP) {
30160Sstevel@tonic-gate 			error = EINVAL;
30170Sstevel@tonic-gate 			break;
30180Sstevel@tonic-gate 		}
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 		if (wait && (error == EAGAIN)) {
30210Sstevel@tonic-gate 			/*
30220Sstevel@tonic-gate 			 * Memory is currently locked.  It must be unlocked
30230Sstevel@tonic-gate 			 * before this operation can succeed through a retry.
30240Sstevel@tonic-gate 			 * The possible reasons for locked memory and
30250Sstevel@tonic-gate 			 * corresponding strategies for unlocking are:
30260Sstevel@tonic-gate 			 * (1) Normal I/O
30270Sstevel@tonic-gate 			 *	wait for a signal that the I/O operation
30280Sstevel@tonic-gate 			 *	has completed and the memory is unlocked.
30290Sstevel@tonic-gate 			 * (2) Asynchronous I/O
30300Sstevel@tonic-gate 			 *	The aio subsystem does not unlock pages when
30310Sstevel@tonic-gate 			 *	the I/O is completed. Those pages are unlocked
30320Sstevel@tonic-gate 			 *	when the application calls aiowait/aioerror.
30330Sstevel@tonic-gate 			 *	So, to prevent blocking forever, cv_broadcast()
30340Sstevel@tonic-gate 			 *	is done to wake up aio_cleanup_thread.
30350Sstevel@tonic-gate 			 *	Subsequently, segvn_reclaim will be called, and
30360Sstevel@tonic-gate 			 *	that will do AS_CLRUNMAPWAIT() and wake us up.
30370Sstevel@tonic-gate 			 * (3) Long term page locking:
30380Sstevel@tonic-gate 			 *	This is not relevant for as_setpagesize()
30390Sstevel@tonic-gate 			 *	because we cannot change the page size for
30400Sstevel@tonic-gate 			 *	driver memory. The attempt to do so will
30410Sstevel@tonic-gate 			 *	fail with a different error than EAGAIN so
30420Sstevel@tonic-gate 			 *	there's no need to trigger as callbacks like
30430Sstevel@tonic-gate 			 *	as_unmap, as_setprot or as_free would do.
30440Sstevel@tonic-gate 			 */
30450Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
30466695Saguzovsk 			if (!AS_ISNOUNMAPWAIT(as)) {
30476695Saguzovsk 				if (AS_ISUNMAPWAIT(as) == 0) {
30486695Saguzovsk 					cv_broadcast(&as->a_cv);
30496695Saguzovsk 				}
30506695Saguzovsk 				AS_SETUNMAPWAIT(as);
30516695Saguzovsk 				AS_LOCK_EXIT(as, &as->a_lock);
30526695Saguzovsk 				while (AS_ISUNMAPWAIT(as)) {
30536695Saguzovsk 					cv_wait(&as->a_cv, &as->a_contents);
30546695Saguzovsk 				}
30556695Saguzovsk 			} else {
30566695Saguzovsk 				/*
30576695Saguzovsk 				 * We may have raced with
30586695Saguzovsk 				 * segvn_reclaim()/segspt_reclaim(). In this
30596695Saguzovsk 				 * case clean nounmapwait flag and retry since
30606695Saguzovsk 				 * softlockcnt in this segment may be already
30616695Saguzovsk 				 * 0.  We don't drop as writer lock so our
30626695Saguzovsk 				 * number of retries without sleeping should
30636695Saguzovsk 				 * be very small. See segvn_reclaim() for
30646695Saguzovsk 				 * more comments.
30656695Saguzovsk 				 */
30666695Saguzovsk 				AS_CLRNOUNMAPWAIT(as);
30676695Saguzovsk 				mutex_exit(&as->a_contents);
30686695Saguzovsk 				goto retry;
30690Sstevel@tonic-gate 			}
30700Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
30710Sstevel@tonic-gate 			goto setpgsz_top;
30720Sstevel@tonic-gate 		} else if (error != 0) {
30730Sstevel@tonic-gate 			break;
30740Sstevel@tonic-gate 		}
30750Sstevel@tonic-gate 	}
30760Sstevel@tonic-gate 	as_setwatch(as);
30770Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
30780Sstevel@tonic-gate 	return (error);
30790Sstevel@tonic-gate }
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate /*
30822991Ssusans  * as_iset3_default_lpsize() just calls SEGOP_SETPAGESIZE() on all segments
30832991Ssusans  * in its chunk where s_szc is less than the szc we want to set.
30842991Ssusans  */
30852991Ssusans static int
30862991Ssusans as_iset3_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc,
30872991Ssusans     int *retry)
30882991Ssusans {
30892991Ssusans 	struct seg *seg;
30902991Ssusans 	size_t ssize;
30912991Ssusans 	int error;
30922991Ssusans 
30936695Saguzovsk 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
30946695Saguzovsk 
30952991Ssusans 	seg = as_segat(as, raddr);
30962991Ssusans 	if (seg == NULL) {
30972991Ssusans 		panic("as_iset3_default_lpsize: no seg");
30982991Ssusans 	}
30992991Ssusans 
31002991Ssusans 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
31012991Ssusans 		if (raddr >= seg->s_base + seg->s_size) {
31022991Ssusans 			seg = AS_SEGNEXT(as, seg);
31032991Ssusans 			if (seg == NULL || raddr != seg->s_base) {
31042991Ssusans 				panic("as_iset3_default_lpsize: as changed");
31052991Ssusans 			}
31062991Ssusans 		}
31072991Ssusans 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
31082991Ssusans 			ssize = seg->s_base + seg->s_size - raddr;
31092991Ssusans 		} else {
31102991Ssusans 			ssize = rsize;
31112991Ssusans 		}
31122991Ssusans 
31132991Ssusans 		if (szc > seg->s_szc) {
31142991Ssusans 			error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc);
31152991Ssusans 			/* Only retry on EINVAL segments that have no vnode. */
31162991Ssusans 			if (error == EINVAL) {
31172991Ssusans 				vnode_t *vp = NULL;
31182991Ssusans 				if ((SEGOP_GETTYPE(seg, raddr) & MAP_SHARED) &&
31192991Ssusans 				    (SEGOP_GETVP(seg, raddr, &vp) != 0 ||
31202991Ssusans 				    vp == NULL)) {
31212991Ssusans 					*retry = 1;
31222991Ssusans 				} else {
31232991Ssusans 					*retry = 0;
31242991Ssusans 				}
31252991Ssusans 			}
31262991Ssusans 			if (error) {
31272991Ssusans 				return (error);
31282991Ssusans 			}
31292991Ssusans 		}
31302991Ssusans 	}
31312991Ssusans 	return (0);
31322991Ssusans }
31332991Ssusans 
31342991Ssusans /*
31352991Ssusans  * as_iset2_default_lpsize() calls as_iset3_default_lpsize() to set the
31362991Ssusans  * pagesize on each segment in its range, but if any fails with EINVAL,
31372991Ssusans  * then it reduces the pagesizes to the next size in the bitmap and
31382991Ssusans  * retries as_iset3_default_lpsize(). The reason why the code retries
31392991Ssusans  * smaller allowed sizes on EINVAL is because (a) the anon offset may not
31402991Ssusans  * match the bigger sizes, and (b) it's hard to get this offset (to begin
31412991Ssusans  * with) to pass to map_pgszcvec().
31422991Ssusans  */
31432991Ssusans static int
31442991Ssusans as_iset2_default_lpsize(struct as *as, caddr_t addr, size_t size, uint_t szc,
31452991Ssusans     uint_t szcvec)
31462991Ssusans {
31472991Ssusans 	int error;
31482991Ssusans 	int retry;
31492991Ssusans 
31506695Saguzovsk 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
31516695Saguzovsk 
31522991Ssusans 	for (;;) {
31532991Ssusans 		error = as_iset3_default_lpsize(as, addr, size, szc, &retry);
31542991Ssusans 		if (error == EINVAL && retry) {
31552991Ssusans 			szcvec &= ~(1 << szc);
31562991Ssusans 			if (szcvec <= 1) {
31572991Ssusans 				return (EINVAL);
31582991Ssusans 			}
31592991Ssusans 			szc = highbit(szcvec) - 1;
31602991Ssusans 		} else {
31612991Ssusans 			return (error);
31622991Ssusans 		}
31632991Ssusans 	}
31642991Ssusans }
31652991Ssusans 
31662991Ssusans /*
31672991Ssusans  * as_iset1_default_lpsize() breaks its chunk into areas where existing
31682991Ssusans  * segments have a smaller szc than we want to set. For each such area,
31692991Ssusans  * it calls as_iset2_default_lpsize()
31702991Ssusans  */
31712991Ssusans static int
31722991Ssusans as_iset1_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc,
31732991Ssusans     uint_t szcvec)
31742991Ssusans {
31752991Ssusans 	struct seg *seg;
31762991Ssusans 	size_t ssize;
31772991Ssusans 	caddr_t setaddr = raddr;
31782991Ssusans 	size_t setsize = 0;
31792991Ssusans 	int set;
31802991Ssusans 	int error;
31812991Ssusans 
31822991Ssusans 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
31832991Ssusans 
31842991Ssusans 	seg = as_segat(as, raddr);
31852991Ssusans 	if (seg == NULL) {
31862991Ssusans 		panic("as_iset1_default_lpsize: no seg");
31872991Ssusans 	}
31882991Ssusans 	if (seg->s_szc < szc) {
31892991Ssusans 		set = 1;
31902991Ssusans 	} else {
31912991Ssusans 		set = 0;
31922991Ssusans 	}
31932991Ssusans 
31942991Ssusans 	for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) {
31952991Ssusans 		if (raddr >= seg->s_base + seg->s_size) {
31962991Ssusans 			seg = AS_SEGNEXT(as, seg);
31972991Ssusans 			if (seg == NULL || raddr != seg->s_base) {
31982991Ssusans 				panic("as_iset1_default_lpsize: as changed");
31992991Ssusans 			}
32002991Ssusans 			if (seg->s_szc >= szc && set) {
32012991Ssusans 				ASSERT(setsize != 0);
32022991Ssusans 				error = as_iset2_default_lpsize(as,
32032991Ssusans 				    setaddr, setsize, szc, szcvec);
32042991Ssusans 				if (error) {
32052991Ssusans 					return (error);
32062991Ssusans 				}
32072991Ssusans 				set = 0;
32082991Ssusans 			} else if (seg->s_szc < szc && !set) {
32092991Ssusans 				setaddr = raddr;
32102991Ssusans 				setsize = 0;
32112991Ssusans 				set = 1;
32122991Ssusans 			}
32132991Ssusans 		}
32142991Ssusans 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
32152991Ssusans 			ssize = seg->s_base + seg->s_size - raddr;
32162991Ssusans 		} else {
32172991Ssusans 			ssize = rsize;
32182991Ssusans 		}
32192991Ssusans 	}
32202991Ssusans 	error = 0;
32212991Ssusans 	if (set) {
32222991Ssusans 		ASSERT(setsize != 0);
32232991Ssusans 		error = as_iset2_default_lpsize(as, setaddr, setsize,
32242991Ssusans 		    szc, szcvec);
32252991Ssusans 	}
32262991Ssusans 	return (error);
32272991Ssusans }
32282991Ssusans 
32292991Ssusans /*
32302991Ssusans  * as_iset_default_lpsize() breaks its chunk according to the size code bitmap
32312991Ssusans  * returned by map_pgszcvec() (similar to as_map_segvn_segs()), and passes each
32322991Ssusans  * chunk to as_iset1_default_lpsize().
32332991Ssusans  */
32342991Ssusans static int
32352991Ssusans as_iset_default_lpsize(struct as *as, caddr_t addr, size_t size, int flags,
32362991Ssusans     int type)
32372991Ssusans {
32382991Ssusans 	int rtype = (type & MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM;
32392991Ssusans 	uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr,
32405084Sjohnlev 	    flags, rtype, 1);
32412991Ssusans 	uint_t szc;
32422991Ssusans 	uint_t nszc;
32432991Ssusans 	int error;
32442991Ssusans 	caddr_t a;
32452991Ssusans 	caddr_t eaddr;
32462991Ssusans 	size_t segsize;
32472991Ssusans 	size_t pgsz;
32482991Ssusans 	uint_t save_szcvec;
32492991Ssusans 
32502991Ssusans 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
32512991Ssusans 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
32522991Ssusans 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
32532991Ssusans 
32542991Ssusans 	szcvec &= ~1;
32552991Ssusans 	if (szcvec <= 1) {	/* skip if base page size */
32562991Ssusans 		return (0);
32572991Ssusans 	}
32582991Ssusans 
32592991Ssusans 	/* Get the pagesize of the first larger page size. */
32602991Ssusans 	szc = lowbit(szcvec) - 1;
32612991Ssusans 	pgsz = page_get_pagesize(szc);
32622991Ssusans 	eaddr = addr + size;
32632991Ssusans 	addr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
32642991Ssusans 	eaddr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
32652991Ssusans 
32662991Ssusans 	save_szcvec = szcvec;
32672991Ssusans 	szcvec >>= (szc + 1);
32682991Ssusans 	nszc = szc;
32692991Ssusans 	while (szcvec) {
32702991Ssusans 		if ((szcvec & 0x1) == 0) {
32712991Ssusans 			nszc++;
32722991Ssusans 			szcvec >>= 1;
32732991Ssusans 			continue;
32742991Ssusans 		}
32752991Ssusans 		nszc++;
32762991Ssusans 		pgsz = page_get_pagesize(nszc);
32772991Ssusans 		a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
32782991Ssusans 		if (a != addr) {
32792991Ssusans 			ASSERT(szc > 0);
32802991Ssusans 			ASSERT(a < eaddr);
32812991Ssusans 			segsize = a - addr;
32822991Ssusans 			error = as_iset1_default_lpsize(as, addr, segsize, szc,
32832991Ssusans 			    save_szcvec);
32842991Ssusans 			if (error) {
32852991Ssusans 				return (error);
32862991Ssusans 			}
32872991Ssusans 			addr = a;
32882991Ssusans 		}
32892991Ssusans 		szc = nszc;
32902991Ssusans 		szcvec >>= 1;
32912991Ssusans 	}
32922991Ssusans 
32932991Ssusans 	ASSERT(addr < eaddr);
32942991Ssusans 	szcvec = save_szcvec;
32952991Ssusans 	while (szcvec) {
32962991Ssusans 		a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
32972991Ssusans 		ASSERT(a >= addr);
32982991Ssusans 		if (a != addr) {
32992991Ssusans 			ASSERT(szc > 0);
33002991Ssusans 			segsize = a - addr;
33012991Ssusans 			error = as_iset1_default_lpsize(as, addr, segsize, szc,
33022991Ssusans 			    save_szcvec);
33032991Ssusans 			if (error) {
33042991Ssusans 				return (error);
33052991Ssusans 			}
33062991Ssusans 			addr = a;
33072991Ssusans 		}
33082991Ssusans 		szcvec &= ~(1 << szc);
33092991Ssusans 		if (szcvec) {
33102991Ssusans 			szc = highbit(szcvec) - 1;
33112991Ssusans 			pgsz = page_get_pagesize(szc);
33122991Ssusans 		}
33132991Ssusans 	}
33142991Ssusans 	ASSERT(addr == eaddr);
33152991Ssusans 
33162991Ssusans 	return (0);
33172991Ssusans }
33182991Ssusans 
33192991Ssusans /*
33202991Ssusans  * Set the default large page size for the range. Called via memcntl with
33212991Ssusans  * page size set to 0. as_set_default_lpsize breaks the range down into
33222991Ssusans  * chunks with the same type/flags, ignores-non segvn segments, and passes
33232991Ssusans  * each chunk to as_iset_default_lpsize().
33242991Ssusans  */
33252991Ssusans int
33262991Ssusans as_set_default_lpsize(struct as *as, caddr_t addr, size_t size)
33272991Ssusans {
33282991Ssusans 	struct seg *seg;
33292991Ssusans 	caddr_t raddr;
33302991Ssusans 	size_t rsize;
33312991Ssusans 	size_t ssize;
33322991Ssusans 	int rtype, rflags;
33332991Ssusans 	int stype, sflags;
33342991Ssusans 	int error;
33352991Ssusans 	caddr_t	setaddr;
33362991Ssusans 	size_t setsize;
33372991Ssusans 	int segvn;
33382991Ssusans 
33392991Ssusans 	if (size == 0)
33402991Ssusans 		return (0);
33412991Ssusans 
33422991Ssusans 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
33432991Ssusans again:
33442991Ssusans 	error = 0;
33452991Ssusans 
33462991Ssusans 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
33472991Ssusans 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
33482991Ssusans 	    (size_t)raddr;
33492991Ssusans 
33502991Ssusans 	if (raddr + rsize < raddr) {		/* check for wraparound */
33512991Ssusans 		AS_LOCK_EXIT(as, &as->a_lock);
33522991Ssusans 		return (ENOMEM);
33532991Ssusans 	}
33542991Ssusans 	as_clearwatchprot(as, raddr, rsize);
33552991Ssusans 	seg = as_segat(as, raddr);
33562991Ssusans 	if (seg == NULL) {
33572991Ssusans 		as_setwatch(as);
33582991Ssusans 		AS_LOCK_EXIT(as, &as->a_lock);
33592991Ssusans 		return (ENOMEM);
33602991Ssusans 	}
33612991Ssusans 	if (seg->s_ops == &segvn_ops) {
33622991Ssusans 		rtype = SEGOP_GETTYPE(seg, addr);
33632991Ssusans 		rflags = rtype & (MAP_TEXT | MAP_INITDATA);
33642991Ssusans 		rtype = rtype & (MAP_SHARED | MAP_PRIVATE);
33652991Ssusans 		segvn = 1;
33662991Ssusans 	} else {
33672991Ssusans 		segvn = 0;
33682991Ssusans 	}
33692991Ssusans 	setaddr = raddr;
33702991Ssusans 	setsize = 0;
33712991Ssusans 
33722991Ssusans 	for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) {
33732991Ssusans 		if (raddr >= (seg->s_base + seg->s_size)) {
33742991Ssusans 			seg = AS_SEGNEXT(as, seg);
33752991Ssusans 			if (seg == NULL || raddr != seg->s_base) {
33762991Ssusans 				error = ENOMEM;
33772991Ssusans 				break;
33782991Ssusans 			}
33792991Ssusans 			if (seg->s_ops == &segvn_ops) {
33802991Ssusans 				stype = SEGOP_GETTYPE(seg, raddr);
33812991Ssusans 				sflags = stype & (MAP_TEXT | MAP_INITDATA);
33822991Ssusans 				stype &= (MAP_SHARED | MAP_PRIVATE);
33832991Ssusans 				if (segvn && (rflags != sflags ||
33842991Ssusans 				    rtype != stype)) {
33852991Ssusans 					/*
33862991Ssusans 					 * The next segment is also segvn but
33872991Ssusans 					 * has different flags and/or type.
33882991Ssusans 					 */
33892991Ssusans 					ASSERT(setsize != 0);
33902991Ssusans 					error = as_iset_default_lpsize(as,
33912991Ssusans 					    setaddr, setsize, rflags, rtype);
33922991Ssusans 					if (error) {
33932991Ssusans 						break;
33942991Ssusans 					}
33952991Ssusans 					rflags = sflags;
33962991Ssusans 					rtype = stype;
33972991Ssusans 					setaddr = raddr;
33982991Ssusans 					setsize = 0;
33992991Ssusans 				} else if (!segvn) {
34002991Ssusans 					rflags = sflags;
34012991Ssusans 					rtype = stype;
34022991Ssusans 					setaddr = raddr;
34032991Ssusans 					setsize = 0;
34042991Ssusans 					segvn = 1;
34052991Ssusans 				}
34062991Ssusans 			} else if (segvn) {
34072991Ssusans 				/* The next segment is not segvn. */
34082991Ssusans 				ASSERT(setsize != 0);
34092991Ssusans 				error = as_iset_default_lpsize(as,
34102991Ssusans 				    setaddr, setsize, rflags, rtype);
34112991Ssusans 				if (error) {
34122991Ssusans 					break;
34132991Ssusans 				}
34142991Ssusans 				segvn = 0;
34152991Ssusans 			}
34162991Ssusans 		}
34172991Ssusans 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
34182991Ssusans 			ssize = seg->s_base + seg->s_size - raddr;
34192991Ssusans 		} else {
34202991Ssusans 			ssize = rsize;
34212991Ssusans 		}
34222991Ssusans 	}
34232991Ssusans 	if (error == 0 && segvn) {
34242991Ssusans 		/* The last chunk when rsize == 0. */
34252991Ssusans 		ASSERT(setsize != 0);
34262991Ssusans 		error = as_iset_default_lpsize(as, setaddr, setsize,
34272991Ssusans 		    rflags, rtype);
34282991Ssusans 	}
34292991Ssusans 
34302991Ssusans 	if (error == IE_RETRY) {
34312991Ssusans 		goto again;
34322991Ssusans 	} else if (error == IE_NOMEM) {
34332991Ssusans 		error = EAGAIN;
34342991Ssusans 	} else if (error == ENOTSUP) {
34352991Ssusans 		error = EINVAL;
34362991Ssusans 	} else if (error == EAGAIN) {
34372991Ssusans 		mutex_enter(&as->a_contents);
34386695Saguzovsk 		if (!AS_ISNOUNMAPWAIT(as)) {
34396695Saguzovsk 			if (AS_ISUNMAPWAIT(as) == 0) {
34406695Saguzovsk 				cv_broadcast(&as->a_cv);
34416695Saguzovsk 			}
34426695Saguzovsk 			AS_SETUNMAPWAIT(as);
34436695Saguzovsk 			AS_LOCK_EXIT(as, &as->a_lock);
34446695Saguzovsk 			while (AS_ISUNMAPWAIT(as)) {
34456695Saguzovsk 				cv_wait(&as->a_cv, &as->a_contents);
34466695Saguzovsk 			}
34476695Saguzovsk 			mutex_exit(&as->a_contents);
34486695Saguzovsk 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
34496695Saguzovsk 		} else {
34506695Saguzovsk 			/*
34516695Saguzovsk 			 * We may have raced with
34526695Saguzovsk 			 * segvn_reclaim()/segspt_reclaim(). In this case
34536695Saguzovsk 			 * clean nounmapwait flag and retry since softlockcnt
34546695Saguzovsk 			 * in this segment may be already 0.  We don't drop as
34556695Saguzovsk 			 * writer lock so our number of retries without
34566695Saguzovsk 			 * sleeping should be very small. See segvn_reclaim()
34576695Saguzovsk 			 * for more comments.
34586695Saguzovsk 			 */
34596695Saguzovsk 			AS_CLRNOUNMAPWAIT(as);
34606695Saguzovsk 			mutex_exit(&as->a_contents);
34612991Ssusans 		}
34622991Ssusans 		goto again;
34632991Ssusans 	}
34642991Ssusans 
34652991Ssusans 	as_setwatch(as);
34662991Ssusans 	AS_LOCK_EXIT(as, &as->a_lock);
34672991Ssusans 	return (error);
34682991Ssusans }
34692991Ssusans 
34702991Ssusans /*
34710Sstevel@tonic-gate  * Setup all of the uninitialized watched pages that we can.
34720Sstevel@tonic-gate  */
34730Sstevel@tonic-gate void
34740Sstevel@tonic-gate as_setwatch(struct as *as)
34750Sstevel@tonic-gate {
34760Sstevel@tonic-gate 	struct watched_page *pwp;
34770Sstevel@tonic-gate 	struct seg *seg;
34780Sstevel@tonic-gate 	caddr_t vaddr;
34790Sstevel@tonic-gate 	uint_t prot;
34800Sstevel@tonic-gate 	int  err, retrycnt;
34810Sstevel@tonic-gate 
34820Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
34830Sstevel@tonic-gate 		return;
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 	for (pwp = avl_first(&as->a_wpage); pwp != NULL;
34880Sstevel@tonic-gate 	    pwp = AVL_NEXT(&as->a_wpage, pwp)) {
34890Sstevel@tonic-gate 		retrycnt = 0;
34900Sstevel@tonic-gate 	retry:
34910Sstevel@tonic-gate 		vaddr = pwp->wp_vaddr;
34920Sstevel@tonic-gate 		if (pwp->wp_oprot != 0 ||	/* already set up */
34930Sstevel@tonic-gate 		    (seg = as_segat(as, vaddr)) == NULL ||
34940Sstevel@tonic-gate 		    SEGOP_GETPROT(seg, vaddr, 0, &prot) != 0)
34950Sstevel@tonic-gate 			continue;
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 		pwp->wp_oprot = prot;
34980Sstevel@tonic-gate 		if (pwp->wp_read)
34990Sstevel@tonic-gate 			prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
35000Sstevel@tonic-gate 		if (pwp->wp_write)
35010Sstevel@tonic-gate 			prot &= ~PROT_WRITE;
35020Sstevel@tonic-gate 		if (pwp->wp_exec)
35030Sstevel@tonic-gate 			prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
35040Sstevel@tonic-gate 		if (!(pwp->wp_flags & WP_NOWATCH) && prot != pwp->wp_oprot) {
35050Sstevel@tonic-gate 			err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot);
35060Sstevel@tonic-gate 			if (err == IE_RETRY) {
35070Sstevel@tonic-gate 				pwp->wp_oprot = 0;
35080Sstevel@tonic-gate 				ASSERT(retrycnt == 0);
35090Sstevel@tonic-gate 				retrycnt++;
35100Sstevel@tonic-gate 				goto retry;
35110Sstevel@tonic-gate 			}
35120Sstevel@tonic-gate 		}
35130Sstevel@tonic-gate 		pwp->wp_prot = prot;
35140Sstevel@tonic-gate 	}
35150Sstevel@tonic-gate }
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate /*
35180Sstevel@tonic-gate  * Clear all of the watched pages in the address space.
35190Sstevel@tonic-gate  */
35200Sstevel@tonic-gate void
35210Sstevel@tonic-gate as_clearwatch(struct as *as)
35220Sstevel@tonic-gate {
35230Sstevel@tonic-gate 	struct watched_page *pwp;
35240Sstevel@tonic-gate 	struct seg *seg;
35250Sstevel@tonic-gate 	caddr_t vaddr;
35260Sstevel@tonic-gate 	uint_t prot;
35270Sstevel@tonic-gate 	int err, retrycnt;
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
35300Sstevel@tonic-gate 		return;
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 	for (pwp = avl_first(&as->a_wpage); pwp != NULL;
35350Sstevel@tonic-gate 	    pwp = AVL_NEXT(&as->a_wpage, pwp)) {
35360Sstevel@tonic-gate 		retrycnt = 0;
35370Sstevel@tonic-gate 	retry:
35380Sstevel@tonic-gate 		vaddr = pwp->wp_vaddr;
35390Sstevel@tonic-gate 		if (pwp->wp_oprot == 0 ||	/* not set up */
35400Sstevel@tonic-gate 		    (seg = as_segat(as, vaddr)) == NULL)
35410Sstevel@tonic-gate 			continue;
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 		if ((prot = pwp->wp_oprot) != pwp->wp_prot) {
35440Sstevel@tonic-gate 			err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot);
35450Sstevel@tonic-gate 			if (err == IE_RETRY) {
35460Sstevel@tonic-gate 				ASSERT(retrycnt == 0);
35470Sstevel@tonic-gate 				retrycnt++;
35480Sstevel@tonic-gate 				goto retry;
35490Sstevel@tonic-gate 			}
35500Sstevel@tonic-gate 		}
35510Sstevel@tonic-gate 		pwp->wp_oprot = 0;
35520Sstevel@tonic-gate 		pwp->wp_prot = 0;
35530Sstevel@tonic-gate 	}
35540Sstevel@tonic-gate }
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate /*
35570Sstevel@tonic-gate  * Force a new setup for all the watched pages in the range.
35580Sstevel@tonic-gate  */
35590Sstevel@tonic-gate static void
35600Sstevel@tonic-gate as_setwatchprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
35610Sstevel@tonic-gate {
35620Sstevel@tonic-gate 	struct watched_page *pwp;
35630Sstevel@tonic-gate 	struct watched_page tpw;
35640Sstevel@tonic-gate 	caddr_t eaddr = addr + size;
35650Sstevel@tonic-gate 	caddr_t vaddr;
35660Sstevel@tonic-gate 	struct seg *seg;
35670Sstevel@tonic-gate 	int err, retrycnt;
35680Sstevel@tonic-gate 	uint_t	wprot;
35690Sstevel@tonic-gate 	avl_index_t where;
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
35720Sstevel@tonic-gate 		return;
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
35770Sstevel@tonic-gate 	if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL)
35780Sstevel@tonic-gate 		pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER);
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate 	while (pwp != NULL && pwp->wp_vaddr < eaddr) {
35810Sstevel@tonic-gate 		retrycnt = 0;
35820Sstevel@tonic-gate 		vaddr = pwp->wp_vaddr;
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 		wprot = prot;
35850Sstevel@tonic-gate 		if (pwp->wp_read)
35860Sstevel@tonic-gate 			wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
35870Sstevel@tonic-gate 		if (pwp->wp_write)
35880Sstevel@tonic-gate 			wprot &= ~PROT_WRITE;
35890Sstevel@tonic-gate 		if (pwp->wp_exec)
35900Sstevel@tonic-gate 			wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
35910Sstevel@tonic-gate 		if (!(pwp->wp_flags & WP_NOWATCH) && wprot != pwp->wp_oprot) {
35920Sstevel@tonic-gate 		retry:
35930Sstevel@tonic-gate 			seg = as_segat(as, vaddr);
35940Sstevel@tonic-gate 			if (seg == NULL) {
35950Sstevel@tonic-gate 				panic("as_setwatchprot: no seg");
35960Sstevel@tonic-gate 				/*NOTREACHED*/
35970Sstevel@tonic-gate 			}
35980Sstevel@tonic-gate 			err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, wprot);
35990Sstevel@tonic-gate 			if (err == IE_RETRY) {
36000Sstevel@tonic-gate 				ASSERT(retrycnt == 0);
36010Sstevel@tonic-gate 				retrycnt++;
36020Sstevel@tonic-gate 				goto retry;
36030Sstevel@tonic-gate 			}
36040Sstevel@tonic-gate 		}
36050Sstevel@tonic-gate 		pwp->wp_oprot = prot;
36060Sstevel@tonic-gate 		pwp->wp_prot = wprot;
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 		pwp = AVL_NEXT(&as->a_wpage, pwp);
36090Sstevel@tonic-gate 	}
36100Sstevel@tonic-gate }
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate /*
36130Sstevel@tonic-gate  * Clear all of the watched pages in the range.
36140Sstevel@tonic-gate  */
36150Sstevel@tonic-gate static void
36160Sstevel@tonic-gate as_clearwatchprot(struct as *as, caddr_t addr, size_t size)
36170Sstevel@tonic-gate {
36180Sstevel@tonic-gate 	caddr_t eaddr = addr + size;
36190Sstevel@tonic-gate 	struct watched_page *pwp;
36200Sstevel@tonic-gate 	struct watched_page tpw;
36210Sstevel@tonic-gate 	uint_t prot;
36220Sstevel@tonic-gate 	struct seg *seg;
36230Sstevel@tonic-gate 	int err, retrycnt;
36240Sstevel@tonic-gate 	avl_index_t where;
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
36270Sstevel@tonic-gate 		return;
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 	tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
36300Sstevel@tonic-gate 	if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL)
36310Sstevel@tonic-gate 		pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER);
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	while (pwp != NULL && pwp->wp_vaddr < eaddr) {
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 		if ((prot = pwp->wp_oprot) != 0) {
36380Sstevel@tonic-gate 			retrycnt = 0;
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate 			if (prot != pwp->wp_prot) {
36410Sstevel@tonic-gate 			retry:
36420Sstevel@tonic-gate 				seg = as_segat(as, pwp->wp_vaddr);
36430Sstevel@tonic-gate 				if (seg == NULL)
36440Sstevel@tonic-gate 					continue;
36450Sstevel@tonic-gate 				err = SEGOP_SETPROT(seg, pwp->wp_vaddr,
36460Sstevel@tonic-gate 				    PAGESIZE, prot);
36470Sstevel@tonic-gate 				if (err == IE_RETRY) {
36480Sstevel@tonic-gate 					ASSERT(retrycnt == 0);
36490Sstevel@tonic-gate 					retrycnt++;
36500Sstevel@tonic-gate 					goto retry;
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 				}
36530Sstevel@tonic-gate 			}
36540Sstevel@tonic-gate 			pwp->wp_oprot = 0;
36550Sstevel@tonic-gate 			pwp->wp_prot = 0;
36560Sstevel@tonic-gate 		}
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 		pwp = AVL_NEXT(&as->a_wpage, pwp);
36590Sstevel@tonic-gate 	}
36600Sstevel@tonic-gate }
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate void
36630Sstevel@tonic-gate as_signal_proc(struct as *as, k_siginfo_t *siginfo)
36640Sstevel@tonic-gate {
36650Sstevel@tonic-gate 	struct proc *p;
36660Sstevel@tonic-gate 
36670Sstevel@tonic-gate 	mutex_enter(&pidlock);
36680Sstevel@tonic-gate 	for (p = practive; p; p = p->p_next) {
36690Sstevel@tonic-gate 		if (p->p_as == as) {
36700Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
36710Sstevel@tonic-gate 			if (p->p_as == as)
36720Sstevel@tonic-gate 				sigaddq(p, NULL, siginfo, KM_NOSLEEP);
36730Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
36740Sstevel@tonic-gate 		}
36750Sstevel@tonic-gate 	}
36760Sstevel@tonic-gate 	mutex_exit(&pidlock);
36770Sstevel@tonic-gate }
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate /*
36800Sstevel@tonic-gate  * return memory object ID
36810Sstevel@tonic-gate  */
36820Sstevel@tonic-gate int
36830Sstevel@tonic-gate as_getmemid(struct as *as, caddr_t addr, memid_t *memidp)
36840Sstevel@tonic-gate {
36850Sstevel@tonic-gate 	struct seg	*seg;
36860Sstevel@tonic-gate 	int		sts;
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
36890Sstevel@tonic-gate 	seg = as_segat(as, addr);
36900Sstevel@tonic-gate 	if (seg == NULL) {
36910Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
36920Sstevel@tonic-gate 		return (EFAULT);
36930Sstevel@tonic-gate 	}
36940Sstevel@tonic-gate 	/*
36950Sstevel@tonic-gate 	 * catch old drivers which may not support getmemid
36960Sstevel@tonic-gate 	 */
36970Sstevel@tonic-gate 	if (seg->s_ops->getmemid == NULL) {
36980Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
36990Sstevel@tonic-gate 		return (ENODEV);
37000Sstevel@tonic-gate 	}
37010Sstevel@tonic-gate 
37020Sstevel@tonic-gate 	sts = SEGOP_GETMEMID(seg, addr, memidp);
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
37050Sstevel@tonic-gate 	return (sts);
37060Sstevel@tonic-gate }
3707