xref: /illumos-gate/usr/src/uts/common/vm/vm_as.c (revision 05d3dc4b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
502ff05a9Svsakar  * Common Development and Distribution License (the "License").
602ff05a9Svsakar  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
228548bf79Snr  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * VM - address spaces.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <sys/errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/systm.h>
507c478bd9Sstevel@tonic-gate #include <sys/mman.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
527c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
537c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
547c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
557c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
567c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
587c478bd9Sstevel@tonic-gate #include <sys/debug.h>
597c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
607c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <vm/hat.h>
637c478bd9Sstevel@tonic-gate #include <vm/xhat.h>
647c478bd9Sstevel@tonic-gate #include <vm/as.h>
657c478bd9Sstevel@tonic-gate #include <vm/seg.h>
667c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
677c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
687c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
697c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
707c478bd9Sstevel@tonic-gate #include <vm/seg_spt.h>
717c478bd9Sstevel@tonic-gate #include <vm/page.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate clock_t deadlk_wait = 1; /* number of ticks to wait before retrying */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static struct kmem_cache *as_cache;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void as_setwatchprot(struct as *, caddr_t, size_t, uint_t);
787c478bd9Sstevel@tonic-gate static void as_clearwatchprot(struct as *, caddr_t, size_t);
7902ff05a9Svsakar int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Verifying the segment lists is very time-consuming; it may not be
847c478bd9Sstevel@tonic-gate  * desirable always to define VERIFY_SEGLIST when DEBUG is set.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate #ifdef DEBUG
877c478bd9Sstevel@tonic-gate #define	VERIFY_SEGLIST
887c478bd9Sstevel@tonic-gate int do_as_verify = 0;
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Allocate a new callback data structure entry and fill in the events of
937c478bd9Sstevel@tonic-gate  * interest, the address range of interest, and the callback argument.
947c478bd9Sstevel@tonic-gate  * Link the entry on the as->a_callbacks list. A callback entry for the
957c478bd9Sstevel@tonic-gate  * entire address space may be specified with vaddr = 0 and size = -1.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * CALLERS RESPONSIBILITY: If not calling from within the process context for
987c478bd9Sstevel@tonic-gate  * the specified as, the caller must guarantee persistence of the specified as
997c478bd9Sstevel@tonic-gate  * for the duration of this function (eg. pages being locked within the as
1007c478bd9Sstevel@tonic-gate  * will guarantee persistence).
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int
1037c478bd9Sstevel@tonic-gate as_add_callback(struct as *as, void (*cb_func)(), void *arg, uint_t events,
1047c478bd9Sstevel@tonic-gate 		caddr_t vaddr, size_t size, int sleepflag)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	struct as_callback 	*current_head, *cb;
1077c478bd9Sstevel@tonic-gate 	caddr_t 		saddr;
1087c478bd9Sstevel@tonic-gate 	size_t 			rsize;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* callback function and an event are mandatory */
1117c478bd9Sstevel@tonic-gate 	if ((cb_func == NULL) || ((events & AS_ALL_EVENT) == 0))
1127c478bd9Sstevel@tonic-gate 		return (EINVAL);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* Adding a callback after as_free has been called is not allowed */
1157c478bd9Sstevel@tonic-gate 	if (as == &kas)
1167c478bd9Sstevel@tonic-gate 		return (ENOMEM);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * vaddr = 0 and size = -1 is used to indicate that the callback range
1207c478bd9Sstevel@tonic-gate 	 * is the entire address space so no rounding is done in that case.
1217c478bd9Sstevel@tonic-gate 	 */
1227c478bd9Sstevel@tonic-gate 	if (size != -1) {
1237c478bd9Sstevel@tonic-gate 		saddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
1247c478bd9Sstevel@tonic-gate 		rsize = (((size_t)(vaddr + size) + PAGEOFFSET) & PAGEMASK) -
1257c478bd9Sstevel@tonic-gate 			(size_t)saddr;
1267c478bd9Sstevel@tonic-gate 		/* check for wraparound */
1277c478bd9Sstevel@tonic-gate 		if (saddr + rsize < saddr)
1287c478bd9Sstevel@tonic-gate 			return (ENOMEM);
1297c478bd9Sstevel@tonic-gate 	} else {
1307c478bd9Sstevel@tonic-gate 		if (vaddr != 0)
1317c478bd9Sstevel@tonic-gate 			return (EINVAL);
1327c478bd9Sstevel@tonic-gate 		saddr = vaddr;
1337c478bd9Sstevel@tonic-gate 		rsize = size;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/* Allocate and initialize a callback entry */
1377c478bd9Sstevel@tonic-gate 	cb = kmem_zalloc(sizeof (struct as_callback), sleepflag);
1387c478bd9Sstevel@tonic-gate 	if (cb == NULL)
1397c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	cb->ascb_func = cb_func;
1427c478bd9Sstevel@tonic-gate 	cb->ascb_arg = arg;
1437c478bd9Sstevel@tonic-gate 	cb->ascb_events = events;
1447c478bd9Sstevel@tonic-gate 	cb->ascb_saddr = saddr;
1457c478bd9Sstevel@tonic-gate 	cb->ascb_len = rsize;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* Add the entry to the list */
1487c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
1497c478bd9Sstevel@tonic-gate 	current_head = as->a_callbacks;
1507c478bd9Sstevel@tonic-gate 	as->a_callbacks = cb;
1517c478bd9Sstevel@tonic-gate 	cb->ascb_next = current_head;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * The call to this function may lose in a race with
1557c478bd9Sstevel@tonic-gate 	 * a pertinent event - eg. a thread does long term memory locking
1567c478bd9Sstevel@tonic-gate 	 * but before the callback is added another thread executes as_unmap.
1577c478bd9Sstevel@tonic-gate 	 * A broadcast here resolves that.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if ((cb->ascb_events & AS_UNMAPWAIT_EVENT) && AS_ISUNMAPWAIT(as)) {
1607c478bd9Sstevel@tonic-gate 		AS_CLRUNMAPWAIT(as);
1617c478bd9Sstevel@tonic-gate 		cv_broadcast(&as->a_cv);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
1657c478bd9Sstevel@tonic-gate 	return (0);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Search the callback list for an entry which pertains to arg.
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * This is called from within the client upon completion of the callback.
1727c478bd9Sstevel@tonic-gate  * RETURN VALUES:
1737c478bd9Sstevel@tonic-gate  *	AS_CALLBACK_DELETED  (callback entry found and deleted)
1747c478bd9Sstevel@tonic-gate  *	AS_CALLBACK_NOTFOUND (no callback entry found - this is ok)
1757c478bd9Sstevel@tonic-gate  *	AS_CALLBACK_DELETE_DEFERRED (callback is in process, delete of this
1767c478bd9Sstevel@tonic-gate  *			entry will be made in as_do_callbacks)
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  * If as_delete_callback encounters a matching entry with AS_CALLBACK_CALLED
1797c478bd9Sstevel@tonic-gate  * set, it indicates that as_do_callbacks is processing this entry.  The
1807c478bd9Sstevel@tonic-gate  * AS_ALL_EVENT events are cleared in the entry, and a broadcast is made
1817c478bd9Sstevel@tonic-gate  * to unblock as_do_callbacks, in case it is blocked.
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  * CALLERS RESPONSIBILITY: If not calling from within the process context for
1847c478bd9Sstevel@tonic-gate  * the specified as, the caller must guarantee persistence of the specified as
1857c478bd9Sstevel@tonic-gate  * for the duration of this function (eg. pages being locked within the as
1867c478bd9Sstevel@tonic-gate  * will guarantee persistence).
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate uint_t
1897c478bd9Sstevel@tonic-gate as_delete_callback(struct as *as, void *arg)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	struct as_callback **prevcb = &as->a_callbacks;
1927c478bd9Sstevel@tonic-gate 	struct as_callback *cb;
1937c478bd9Sstevel@tonic-gate 	uint_t rc = AS_CALLBACK_NOTFOUND;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
1967c478bd9Sstevel@tonic-gate 	for (cb = as->a_callbacks; cb; prevcb = &cb->ascb_next, cb = *prevcb) {
1977c478bd9Sstevel@tonic-gate 		if (cb->ascb_arg != arg)
1987c478bd9Sstevel@tonic-gate 			continue;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * If the events indicate AS_CALLBACK_CALLED, just clear
2027c478bd9Sstevel@tonic-gate 		 * AS_ALL_EVENT in the events field and wakeup the thread
2037c478bd9Sstevel@tonic-gate 		 * that may be waiting in as_do_callbacks.  as_do_callbacks
2047c478bd9Sstevel@tonic-gate 		 * will take care of removing this entry from the list.  In
2057c478bd9Sstevel@tonic-gate 		 * that case, return AS_CALLBACK_DELETE_DEFERRED.  Otherwise
2067c478bd9Sstevel@tonic-gate 		 * (AS_CALLBACK_CALLED not set), just remove it from the
2077c478bd9Sstevel@tonic-gate 		 * list, return the memory and return AS_CALLBACK_DELETED.
2087c478bd9Sstevel@tonic-gate 		 */
2097c478bd9Sstevel@tonic-gate 		if ((cb->ascb_events & AS_CALLBACK_CALLED) != 0) {
2107c478bd9Sstevel@tonic-gate 			/* leave AS_CALLBACK_CALLED */
2117c478bd9Sstevel@tonic-gate 			cb->ascb_events &= ~AS_ALL_EVENT;
2127c478bd9Sstevel@tonic-gate 			rc = AS_CALLBACK_DELETE_DEFERRED;
2137c478bd9Sstevel@tonic-gate 			cv_broadcast(&as->a_cv);
2147c478bd9Sstevel@tonic-gate 		} else {
2157c478bd9Sstevel@tonic-gate 			*prevcb = cb->ascb_next;
2167c478bd9Sstevel@tonic-gate 			kmem_free(cb, sizeof (struct as_callback));
2177c478bd9Sstevel@tonic-gate 			rc = AS_CALLBACK_DELETED;
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		break;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
2227c478bd9Sstevel@tonic-gate 	return (rc);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Searches the as callback list for a matching entry.
2277c478bd9Sstevel@tonic-gate  * Returns a pointer to the first matching callback, or NULL if
2287c478bd9Sstevel@tonic-gate  * nothing is found.
2297c478bd9Sstevel@tonic-gate  * This function never sleeps so it is ok to call it with more
2307c478bd9Sstevel@tonic-gate  * locks held but the (required) a_contents mutex.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * See also comment on as_do_callbacks below.
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate static struct as_callback *
2357c478bd9Sstevel@tonic-gate as_find_callback(struct as *as, uint_t events, caddr_t event_addr,
2367c478bd9Sstevel@tonic-gate 			size_t event_len)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	struct as_callback	*cb;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&as->a_contents));
2417c478bd9Sstevel@tonic-gate 	for (cb = as->a_callbacks; cb != NULL; cb = cb->ascb_next) {
2427c478bd9Sstevel@tonic-gate 		/*
2437c478bd9Sstevel@tonic-gate 		 * If the callback has not already been called, then
2447c478bd9Sstevel@tonic-gate 		 * check if events or address range pertains.  An event_len
2457c478bd9Sstevel@tonic-gate 		 * of zero means do an unconditional callback.
2467c478bd9Sstevel@tonic-gate 		 */
2477c478bd9Sstevel@tonic-gate 		if (((cb->ascb_events & AS_CALLBACK_CALLED) != 0) ||
2487c478bd9Sstevel@tonic-gate 		    ((event_len != 0) && (((cb->ascb_events & events) == 0) ||
2497c478bd9Sstevel@tonic-gate 		    (event_addr + event_len < cb->ascb_saddr) ||
2507c478bd9Sstevel@tonic-gate 		    (event_addr > (cb->ascb_saddr + cb->ascb_len))))) {
2517c478bd9Sstevel@tonic-gate 			continue;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		break;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	return (cb);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Executes a given callback and removes it from the callback list for
2607c478bd9Sstevel@tonic-gate  * this address space.
2617c478bd9Sstevel@tonic-gate  * This function may sleep so the caller must drop all locks except
2627c478bd9Sstevel@tonic-gate  * a_contents before calling this func.
2637c478bd9Sstevel@tonic-gate  *
2647c478bd9Sstevel@tonic-gate  * See also comments on as_do_callbacks below.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static void
2677c478bd9Sstevel@tonic-gate as_execute_callback(struct as *as, struct as_callback *cb,
2687c478bd9Sstevel@tonic-gate 				uint_t events)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	struct as_callback **prevcb;
2717c478bd9Sstevel@tonic-gate 	void	*cb_arg;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&as->a_contents) && (cb->ascb_events & events));
2747c478bd9Sstevel@tonic-gate 	cb->ascb_events |= AS_CALLBACK_CALLED;
2757c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
2767c478bd9Sstevel@tonic-gate 	(*cb->ascb_func)(as, cb->ascb_arg, events);
2777c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * the callback function is required to delete the callback
2807c478bd9Sstevel@tonic-gate 	 * when the callback function determines it is OK for
2817c478bd9Sstevel@tonic-gate 	 * this thread to continue. as_delete_callback will clear
2827c478bd9Sstevel@tonic-gate 	 * the AS_ALL_EVENT in the events field when it is deleted.
2837c478bd9Sstevel@tonic-gate 	 * If the callback function called as_delete_callback,
2847c478bd9Sstevel@tonic-gate 	 * events will already be cleared and there will be no blocking.
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate 	while ((cb->ascb_events & events) != 0) {
2877c478bd9Sstevel@tonic-gate 		cv_wait(&as->a_cv, &as->a_contents);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * This entry needs to be taken off the list. Normally, the
2917c478bd9Sstevel@tonic-gate 	 * callback func itself does that, but unfortunately the list
2927c478bd9Sstevel@tonic-gate 	 * may have changed while the callback was running because the
2937c478bd9Sstevel@tonic-gate 	 * a_contents mutex was dropped and someone else other than the
2947c478bd9Sstevel@tonic-gate 	 * callback func itself could have called as_delete_callback,
2957c478bd9Sstevel@tonic-gate 	 * so we have to search to find this entry again.  The entry
2967c478bd9Sstevel@tonic-gate 	 * must have AS_CALLBACK_CALLED, and have the same 'arg'.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	cb_arg = cb->ascb_arg;
2997c478bd9Sstevel@tonic-gate 	prevcb = &as->a_callbacks;
3007c478bd9Sstevel@tonic-gate 	for (cb = as->a_callbacks; cb != NULL;
3017c478bd9Sstevel@tonic-gate 	    prevcb = &cb->ascb_next, cb = *prevcb) {
3027c478bd9Sstevel@tonic-gate 		if (((cb->ascb_events & AS_CALLBACK_CALLED) == 0) ||
3037c478bd9Sstevel@tonic-gate 		    (cb_arg != cb->ascb_arg)) {
3047c478bd9Sstevel@tonic-gate 			continue;
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 		*prevcb = cb->ascb_next;
3077c478bd9Sstevel@tonic-gate 		kmem_free(cb, sizeof (struct as_callback));
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Check the callback list for a matching event and intersection of
3147c478bd9Sstevel@tonic-gate  * address range. If there is a match invoke the callback.  Skip an entry if:
3157c478bd9Sstevel@tonic-gate  *    - a callback is already in progress for this entry (AS_CALLBACK_CALLED)
3167c478bd9Sstevel@tonic-gate  *    - not event of interest
3177c478bd9Sstevel@tonic-gate  *    - not address range of interest
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  * An event_len of zero indicates a request for an unconditional callback
3207c478bd9Sstevel@tonic-gate  * (regardless of event), only the AS_CALLBACK_CALLED is checked.  The
3217c478bd9Sstevel@tonic-gate  * a_contents lock must be dropped before a callback, so only one callback
3227c478bd9Sstevel@tonic-gate  * can be done before returning. Return -1 (true) if a callback was
3237c478bd9Sstevel@tonic-gate  * executed and removed from the list, else return 0 (false).
3247c478bd9Sstevel@tonic-gate  *
3257c478bd9Sstevel@tonic-gate  * The logically separate parts, i.e. finding a matching callback and
3267c478bd9Sstevel@tonic-gate  * executing a given callback have been separated into two functions
3277c478bd9Sstevel@tonic-gate  * so that they can be called with different sets of locks held beyond
3287c478bd9Sstevel@tonic-gate  * the always-required a_contents. as_find_callback does not sleep so
3297c478bd9Sstevel@tonic-gate  * it is ok to call it if more locks than a_contents (i.e. the a_lock
3307c478bd9Sstevel@tonic-gate  * rwlock) are held. as_execute_callback on the other hand may sleep
3317c478bd9Sstevel@tonic-gate  * so all locks beyond a_contents must be dropped by the caller if one
3327c478bd9Sstevel@tonic-gate  * does not want to end comatose.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate static int
3357c478bd9Sstevel@tonic-gate as_do_callbacks(struct as *as, uint_t events, caddr_t event_addr,
3367c478bd9Sstevel@tonic-gate 			size_t event_len)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	struct as_callback *cb;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if ((cb = as_find_callback(as, events, event_addr, event_len))) {
3417c478bd9Sstevel@tonic-gate 		as_execute_callback(as, cb, events);
3427c478bd9Sstevel@tonic-gate 		return (-1);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Search for the segment containing addr. If a segment containing addr
3497c478bd9Sstevel@tonic-gate  * exists, that segment is returned.  If no such segment exists, and
3507c478bd9Sstevel@tonic-gate  * the list spans addresses greater than addr, then the first segment
3517c478bd9Sstevel@tonic-gate  * whose base is greater than addr is returned; otherwise, NULL is
3527c478bd9Sstevel@tonic-gate  * returned unless tail is true, in which case the last element of the
3537c478bd9Sstevel@tonic-gate  * list is returned.
3547c478bd9Sstevel@tonic-gate  *
3557c478bd9Sstevel@tonic-gate  * a_seglast is used to cache the last found segment for repeated
3567c478bd9Sstevel@tonic-gate  * searches to the same addr (which happens frequently).
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate struct seg *
3597c478bd9Sstevel@tonic-gate as_findseg(struct as *as, caddr_t addr, int tail)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	struct seg *seg = as->a_seglast;
3627c478bd9Sstevel@tonic-gate 	avl_index_t where;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (seg != NULL &&
3677c478bd9Sstevel@tonic-gate 	    seg->s_base <= addr &&
3687c478bd9Sstevel@tonic-gate 	    addr < seg->s_base + seg->s_size)
3697c478bd9Sstevel@tonic-gate 		return (seg);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	seg = avl_find(&as->a_segtree, &addr, &where);
3727c478bd9Sstevel@tonic-gate 	if (seg != NULL)
3737c478bd9Sstevel@tonic-gate 		return (as->a_seglast = seg);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	seg = avl_nearest(&as->a_segtree, where, AVL_AFTER);
3767c478bd9Sstevel@tonic-gate 	if (seg == NULL && tail)
3777c478bd9Sstevel@tonic-gate 		seg = avl_last(&as->a_segtree);
3787c478bd9Sstevel@tonic-gate 	return (as->a_seglast = seg);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #ifdef VERIFY_SEGLIST
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * verify that the linked list is coherent
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate static void
3867c478bd9Sstevel@tonic-gate as_verify(struct as *as)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	struct seg *seg, *seglast, *p, *n;
3897c478bd9Sstevel@tonic-gate 	uint_t nsegs = 0;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (do_as_verify == 0)
3927c478bd9Sstevel@tonic-gate 		return;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	seglast = as->a_seglast;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
3977c478bd9Sstevel@tonic-gate 		ASSERT(seg->s_as == as);
3987c478bd9Sstevel@tonic-gate 		p = AS_SEGPREV(as, seg);
3997c478bd9Sstevel@tonic-gate 		n = AS_SEGNEXT(as, seg);
4007c478bd9Sstevel@tonic-gate 		ASSERT(p == NULL || p->s_as == as);
4017c478bd9Sstevel@tonic-gate 		ASSERT(p == NULL || p->s_base < seg->s_base);
4027c478bd9Sstevel@tonic-gate 		ASSERT(n == NULL || n->s_base > seg->s_base);
4037c478bd9Sstevel@tonic-gate 		ASSERT(n != NULL || seg == avl_last(&as->a_segtree));
4047c478bd9Sstevel@tonic-gate 		if (seg == seglast)
4057c478bd9Sstevel@tonic-gate 			seglast = NULL;
4067c478bd9Sstevel@tonic-gate 		nsegs++;
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	ASSERT(seglast == NULL);
4097c478bd9Sstevel@tonic-gate 	ASSERT(avl_numnodes(&as->a_segtree) == nsegs);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate #endif /* VERIFY_SEGLIST */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * Add a new segment to the address space. The avl_find()
4157c478bd9Sstevel@tonic-gate  * may be expensive so we attempt to use last segment accessed
4167c478bd9Sstevel@tonic-gate  * in as_gap() as an insertion point.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate int
4197c478bd9Sstevel@tonic-gate as_addseg(struct as  *as, struct seg *newseg)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	struct seg *seg;
4227c478bd9Sstevel@tonic-gate 	caddr_t addr;
4237c478bd9Sstevel@tonic-gate 	caddr_t eaddr;
4247c478bd9Sstevel@tonic-gate 	avl_index_t where;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
4297c478bd9Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (as->a_lastgaphl != NULL) {
4327c478bd9Sstevel@tonic-gate 		struct seg *hseg = NULL;
4337c478bd9Sstevel@tonic-gate 		struct seg *lseg = NULL;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (as->a_lastgaphl->s_base > newseg->s_base) {
4367c478bd9Sstevel@tonic-gate 			hseg = as->a_lastgaphl;
4377c478bd9Sstevel@tonic-gate 			lseg = AVL_PREV(&as->a_segtree, hseg);
4387c478bd9Sstevel@tonic-gate 		} else {
4397c478bd9Sstevel@tonic-gate 			lseg = as->a_lastgaphl;
4407c478bd9Sstevel@tonic-gate 			hseg = AVL_NEXT(&as->a_segtree, lseg);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		if (hseg && lseg && lseg->s_base < newseg->s_base &&
4447c478bd9Sstevel@tonic-gate 		    hseg->s_base > newseg->s_base) {
4457c478bd9Sstevel@tonic-gate 			avl_insert_here(&as->a_segtree, newseg, lseg,
4467c478bd9Sstevel@tonic-gate 			    AVL_AFTER);
4477c478bd9Sstevel@tonic-gate 			as->a_lastgaphl = NULL;
4487c478bd9Sstevel@tonic-gate 			as->a_seglast = newseg;
4497c478bd9Sstevel@tonic-gate 			return (0);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		as->a_lastgaphl = NULL;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	addr = newseg->s_base;
4557c478bd9Sstevel@tonic-gate 	eaddr = addr + newseg->s_size;
4567c478bd9Sstevel@tonic-gate again:
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	seg = avl_find(&as->a_segtree, &addr, &where);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (seg == NULL)
4617c478bd9Sstevel@tonic-gate 		seg = avl_nearest(&as->a_segtree, where, AVL_AFTER);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (seg == NULL)
4647c478bd9Sstevel@tonic-gate 		seg = avl_last(&as->a_segtree);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (seg != NULL) {
4677c478bd9Sstevel@tonic-gate 		caddr_t base = seg->s_base;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		/*
4707c478bd9Sstevel@tonic-gate 		 * If top of seg is below the requested address, then
4717c478bd9Sstevel@tonic-gate 		 * the insertion point is at the end of the linked list,
4727c478bd9Sstevel@tonic-gate 		 * and seg points to the tail of the list.  Otherwise,
4737c478bd9Sstevel@tonic-gate 		 * the insertion point is immediately before seg.
4747c478bd9Sstevel@tonic-gate 		 */
4757c478bd9Sstevel@tonic-gate 		if (base + seg->s_size > addr) {
4767c478bd9Sstevel@tonic-gate 			if (addr >= base || eaddr > base) {
4777c478bd9Sstevel@tonic-gate #ifdef __sparc
4787c478bd9Sstevel@tonic-gate 				extern struct seg_ops segnf_ops;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 				/*
4817c478bd9Sstevel@tonic-gate 				 * no-fault segs must disappear if overlaid.
4827c478bd9Sstevel@tonic-gate 				 * XXX need new segment type so
4837c478bd9Sstevel@tonic-gate 				 * we don't have to check s_ops
4847c478bd9Sstevel@tonic-gate 				 */
4857c478bd9Sstevel@tonic-gate 				if (seg->s_ops == &segnf_ops) {
4867c478bd9Sstevel@tonic-gate 					seg_unmap(seg);
4877c478bd9Sstevel@tonic-gate 					goto again;
4887c478bd9Sstevel@tonic-gate 				}
4897c478bd9Sstevel@tonic-gate #endif
4907c478bd9Sstevel@tonic-gate 				return (-1);	/* overlapping segment */
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 	as->a_seglast = newseg;
4957c478bd9Sstevel@tonic-gate 	avl_insert(&as->a_segtree, newseg, where);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate #ifdef VERIFY_SEGLIST
4987c478bd9Sstevel@tonic-gate 	as_verify(as);
4997c478bd9Sstevel@tonic-gate #endif
5007c478bd9Sstevel@tonic-gate 	return (0);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate struct seg *
5047c478bd9Sstevel@tonic-gate as_removeseg(struct as *as, struct seg *seg)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	avl_tree_t *t;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
5117c478bd9Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (seg == NULL)
5147c478bd9Sstevel@tonic-gate 		return (NULL);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	t = &as->a_segtree;
5177c478bd9Sstevel@tonic-gate 	if (as->a_seglast == seg)
5187c478bd9Sstevel@tonic-gate 		as->a_seglast = NULL;
5197c478bd9Sstevel@tonic-gate 	as->a_lastgaphl = NULL;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * if this segment is at an address higher than
5237c478bd9Sstevel@tonic-gate 	 * a_lastgap, set a_lastgap to the next segment (NULL if last segment)
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	if (as->a_lastgap &&
5267c478bd9Sstevel@tonic-gate 	    (seg == as->a_lastgap || seg->s_base > as->a_lastgap->s_base))
5277c478bd9Sstevel@tonic-gate 		as->a_lastgap = AVL_NEXT(t, seg);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * remove the segment from the seg tree
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	avl_remove(t, seg);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate #ifdef VERIFY_SEGLIST
5357c478bd9Sstevel@tonic-gate 	as_verify(as);
5367c478bd9Sstevel@tonic-gate #endif
5377c478bd9Sstevel@tonic-gate 	return (seg);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate  * Find a segment containing addr.
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate struct seg *
5447c478bd9Sstevel@tonic-gate as_segat(struct as *as, caddr_t addr)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	struct seg *seg = as->a_seglast;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (seg != NULL && seg->s_base <= addr &&
5517c478bd9Sstevel@tonic-gate 	    addr < seg->s_base + seg->s_size)
5527c478bd9Sstevel@tonic-gate 		return (seg);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	seg = avl_find(&as->a_segtree, &addr, NULL);
5557c478bd9Sstevel@tonic-gate 	return (seg);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate  * Serialize all searches for holes in an address space to
5607c478bd9Sstevel@tonic-gate  * prevent two or more threads from allocating the same virtual
5617c478bd9Sstevel@tonic-gate  * address range.  The address space must not be "read/write"
5627c478bd9Sstevel@tonic-gate  * locked by the caller since we may block.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate void
5657c478bd9Sstevel@tonic-gate as_rangelock(struct as *as)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
5687c478bd9Sstevel@tonic-gate 	while (AS_ISCLAIMGAP(as))
5697c478bd9Sstevel@tonic-gate 		cv_wait(&as->a_cv, &as->a_contents);
5707c478bd9Sstevel@tonic-gate 	AS_SETCLAIMGAP(as);
5717c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * Release hold on a_state & AS_CLAIMGAP and signal any other blocked threads.
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate void
5787c478bd9Sstevel@tonic-gate as_rangeunlock(struct as *as)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
5817c478bd9Sstevel@tonic-gate 	AS_CLRCLAIMGAP(as);
5827c478bd9Sstevel@tonic-gate 	cv_signal(&as->a_cv);
5837c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * compar segments (or just an address) by segment address range
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@tonic-gate static int
5907c478bd9Sstevel@tonic-gate as_segcompar(const void *x, const void *y)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	struct seg *a = (struct seg *)x;
5937c478bd9Sstevel@tonic-gate 	struct seg *b = (struct seg *)y;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (a->s_base < b->s_base)
5967c478bd9Sstevel@tonic-gate 		return (-1);
5977c478bd9Sstevel@tonic-gate 	if (a->s_base >= b->s_base + b->s_size)
5987c478bd9Sstevel@tonic-gate 		return (1);
5997c478bd9Sstevel@tonic-gate 	return (0);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate void
6047c478bd9Sstevel@tonic-gate as_avlinit(struct as *as)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	avl_create(&as->a_segtree, as_segcompar, sizeof (struct seg),
6077c478bd9Sstevel@tonic-gate 	    offsetof(struct seg, s_tree));
6087c478bd9Sstevel@tonic-gate 	avl_create(&as->a_wpage, wp_compare, sizeof (struct watched_page),
6097c478bd9Sstevel@tonic-gate 	    offsetof(struct watched_page, wp_link));
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6137c478bd9Sstevel@tonic-gate static int
6147c478bd9Sstevel@tonic-gate as_constructor(void *buf, void *cdrarg, int kmflags)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	struct as *as = buf;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	mutex_init(&as->a_contents, NULL, MUTEX_DEFAULT, NULL);
6197c478bd9Sstevel@tonic-gate 	cv_init(&as->a_cv, NULL, CV_DEFAULT, NULL);
6207c478bd9Sstevel@tonic-gate 	rw_init(&as->a_lock, NULL, RW_DEFAULT, NULL);
6217c478bd9Sstevel@tonic-gate 	as_avlinit(as);
6227c478bd9Sstevel@tonic-gate 	return (0);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
6267c478bd9Sstevel@tonic-gate static void
6277c478bd9Sstevel@tonic-gate as_destructor(void *buf, void *cdrarg)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	struct as *as = buf;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	avl_destroy(&as->a_segtree);
6327c478bd9Sstevel@tonic-gate 	mutex_destroy(&as->a_contents);
6337c478bd9Sstevel@tonic-gate 	cv_destroy(&as->a_cv);
6347c478bd9Sstevel@tonic-gate 	rw_destroy(&as->a_lock);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate void
6387c478bd9Sstevel@tonic-gate as_init(void)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	as_cache = kmem_cache_create("as_cache", sizeof (struct as), 0,
6417c478bd9Sstevel@tonic-gate 		as_constructor, as_destructor, NULL, NULL, NULL, 0);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * Allocate and initialize an address space data structure.
6467c478bd9Sstevel@tonic-gate  * We call hat_alloc to allow any machine dependent
6477c478bd9Sstevel@tonic-gate  * information in the hat structure to be initialized.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate struct as *
6507c478bd9Sstevel@tonic-gate as_alloc(void)
6517c478bd9Sstevel@tonic-gate {
6527c478bd9Sstevel@tonic-gate 	struct as *as;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	as = kmem_cache_alloc(as_cache, KM_SLEEP);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	as->a_flags		= 0;
6577c478bd9Sstevel@tonic-gate 	as->a_vbits		= 0;
6587c478bd9Sstevel@tonic-gate 	as->a_hrm		= NULL;
6597c478bd9Sstevel@tonic-gate 	as->a_seglast		= NULL;
6607c478bd9Sstevel@tonic-gate 	as->a_size		= 0;
6617c478bd9Sstevel@tonic-gate 	as->a_updatedir		= 0;
6627c478bd9Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
6637c478bd9Sstevel@tonic-gate 	as->a_objectdir		= NULL;
6647c478bd9Sstevel@tonic-gate 	as->a_sizedir		= 0;
6657c478bd9Sstevel@tonic-gate 	as->a_userlimit		= (caddr_t)USERLIMIT;
6667c478bd9Sstevel@tonic-gate 	as->a_lastgap		= NULL;
6677c478bd9Sstevel@tonic-gate 	as->a_lastgaphl		= NULL;
6687c478bd9Sstevel@tonic-gate 	as->a_callbacks		= NULL;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
6717c478bd9Sstevel@tonic-gate 	as->a_hat = hat_alloc(as);	/* create hat for default system mmu */
6727c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	as->a_xhat = NULL;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	return (as);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * Free an address space data structure.
6817c478bd9Sstevel@tonic-gate  * Need to free the hat first and then
6827c478bd9Sstevel@tonic-gate  * all the segments on this as and finally
6837c478bd9Sstevel@tonic-gate  * the space for the as struct itself.
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate void
6867c478bd9Sstevel@tonic-gate as_free(struct as *as)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	struct hat *hat = as->a_hat;
6897c478bd9Sstevel@tonic-gate 	struct seg *seg, *next;
6907c478bd9Sstevel@tonic-gate 	int called = 0;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate top:
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * Invoke ALL callbacks. as_do_callbacks will do one callback
6957c478bd9Sstevel@tonic-gate 	 * per call, and not return (-1) until the callback has completed.
6967c478bd9Sstevel@tonic-gate 	 * When as_do_callbacks returns zero, all callbacks have completed.
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
6997c478bd9Sstevel@tonic-gate 	while (as->a_callbacks && as_do_callbacks(as, AS_ALL_EVENT, 0, 0));
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	/* This will prevent new XHATs from attaching to as */
7027c478bd9Sstevel@tonic-gate 	if (!called)
7037c478bd9Sstevel@tonic-gate 		AS_SETBUSY(as);
7047c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
7057c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (!called) {
7087c478bd9Sstevel@tonic-gate 		called = 1;
7097c478bd9Sstevel@tonic-gate 		hat_free_start(hat);
7107c478bd9Sstevel@tonic-gate 		if (as->a_xhat != NULL)
7117c478bd9Sstevel@tonic-gate 			xhat_free_start_all(as);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = next) {
7147c478bd9Sstevel@tonic-gate 		int err;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		next = AS_SEGNEXT(as, seg);
7177c478bd9Sstevel@tonic-gate 		err = SEGOP_UNMAP(seg, seg->s_base, seg->s_size);
7187c478bd9Sstevel@tonic-gate 		if (err == EAGAIN) {
7197c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
7207c478bd9Sstevel@tonic-gate 			if (as->a_callbacks) {
7217c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
7227c478bd9Sstevel@tonic-gate 			} else {
7237c478bd9Sstevel@tonic-gate 				/*
7247c478bd9Sstevel@tonic-gate 				 * Memory is currently locked. Wait for a
7257c478bd9Sstevel@tonic-gate 				 * cv_signal that it has been unlocked, then
7267c478bd9Sstevel@tonic-gate 				 * try the operation again.
7277c478bd9Sstevel@tonic-gate 				 */
7287c478bd9Sstevel@tonic-gate 				if (AS_ISUNMAPWAIT(as) == 0)
7297c478bd9Sstevel@tonic-gate 					cv_broadcast(&as->a_cv);
7307c478bd9Sstevel@tonic-gate 				AS_SETUNMAPWAIT(as);
7317c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
7327c478bd9Sstevel@tonic-gate 				while (AS_ISUNMAPWAIT(as))
7337c478bd9Sstevel@tonic-gate 					cv_wait(&as->a_cv, &as->a_contents);
7347c478bd9Sstevel@tonic-gate 			}
7357c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
7367c478bd9Sstevel@tonic-gate 			goto top;
7377c478bd9Sstevel@tonic-gate 		} else {
7387c478bd9Sstevel@tonic-gate 			/*
7397c478bd9Sstevel@tonic-gate 			 * We do not expect any other error return at this
7407c478bd9Sstevel@tonic-gate 			 * time. This is similar to an ASSERT in seg_unmap()
7417c478bd9Sstevel@tonic-gate 			 */
7427c478bd9Sstevel@tonic-gate 			ASSERT(err == 0);
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	hat_free_end(hat);
7467c478bd9Sstevel@tonic-gate 	if (as->a_xhat != NULL)
7477c478bd9Sstevel@tonic-gate 		xhat_free_end_all(as);
7487c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/* /proc stuff */
7517c478bd9Sstevel@tonic-gate 	ASSERT(avl_numnodes(&as->a_wpage) == 0);
7527c478bd9Sstevel@tonic-gate 	if (as->a_objectdir) {
7537c478bd9Sstevel@tonic-gate 		kmem_free(as->a_objectdir, as->a_sizedir * sizeof (vnode_t *));
7547c478bd9Sstevel@tonic-gate 		as->a_objectdir = NULL;
7557c478bd9Sstevel@tonic-gate 		as->a_sizedir = 0;
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
7597c478bd9Sstevel@tonic-gate 	 * Free the struct as back to kmem.  Assert it has no segments.
7607c478bd9Sstevel@tonic-gate 	 */
7617c478bd9Sstevel@tonic-gate 	ASSERT(avl_numnodes(&as->a_segtree) == 0);
7627c478bd9Sstevel@tonic-gate 	kmem_cache_free(as_cache, as);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate int
7667c478bd9Sstevel@tonic-gate as_dup(struct as *as, struct as **outas)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	struct as *newas;
7697c478bd9Sstevel@tonic-gate 	struct seg *seg, *newseg;
7707c478bd9Sstevel@tonic-gate 	int error;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
7737c478bd9Sstevel@tonic-gate 	as_clearwatch(as);
7747c478bd9Sstevel@tonic-gate 	newas = as_alloc();
7757c478bd9Sstevel@tonic-gate 	newas->a_userlimit = as->a_userlimit;
7767c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(newas, &newas->a_lock, RW_WRITER);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/* This will prevent new XHATs from attaching */
7797c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
7807c478bd9Sstevel@tonic-gate 	AS_SETBUSY(as);
7817c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
7827c478bd9Sstevel@tonic-gate 	mutex_enter(&newas->a_contents);
7837c478bd9Sstevel@tonic-gate 	AS_SETBUSY(newas);
7847c478bd9Sstevel@tonic-gate 	mutex_exit(&newas->a_contents);
7857c478bd9Sstevel@tonic-gate 
786*05d3dc4bSpaulsan 	(void) hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_SRD);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 		if (seg->s_flags & S_PURGE)
7917c478bd9Sstevel@tonic-gate 			continue;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		newseg = seg_alloc(newas, seg->s_base, seg->s_size);
7947c478bd9Sstevel@tonic-gate 		if (newseg == NULL) {
7957c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(newas, &newas->a_lock);
7967c478bd9Sstevel@tonic-gate 			as_setwatch(as);
7977c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
7987c478bd9Sstevel@tonic-gate 			AS_CLRBUSY(as);
7997c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
8007c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
8017c478bd9Sstevel@tonic-gate 			as_free(newas);
8027c478bd9Sstevel@tonic-gate 			return (-1);
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 		if ((error = SEGOP_DUP(seg, newseg)) != 0) {
8057c478bd9Sstevel@tonic-gate 			/*
8067c478bd9Sstevel@tonic-gate 			 * We call seg_free() on the new seg
8077c478bd9Sstevel@tonic-gate 			 * because the segment is not set up
8087c478bd9Sstevel@tonic-gate 			 * completely; i.e. it has no ops.
8097c478bd9Sstevel@tonic-gate 			 */
8107c478bd9Sstevel@tonic-gate 			as_setwatch(as);
8117c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
8127c478bd9Sstevel@tonic-gate 			AS_CLRBUSY(as);
8137c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
8147c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
8157c478bd9Sstevel@tonic-gate 			seg_free(newseg);
8167c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(newas, &newas->a_lock);
8177c478bd9Sstevel@tonic-gate 			as_free(newas);
8187c478bd9Sstevel@tonic-gate 			return (error);
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		newas->a_size += seg->s_size;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	error = hat_dup(as->a_hat, newas->a_hat, NULL, 0, HAT_DUP_ALL);
8247c478bd9Sstevel@tonic-gate 	if (as->a_xhat != NULL)
8257c478bd9Sstevel@tonic-gate 		error |= xhat_dup_all(as, newas, NULL, 0, HAT_DUP_ALL);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	mutex_enter(&newas->a_contents);
8287c478bd9Sstevel@tonic-gate 	AS_CLRBUSY(newas);
8297c478bd9Sstevel@tonic-gate 	mutex_exit(&newas->a_contents);
8307c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(newas, &newas->a_lock);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	as_setwatch(as);
8337c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
8347c478bd9Sstevel@tonic-gate 	AS_CLRBUSY(as);
8357c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
8367c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
8377c478bd9Sstevel@tonic-gate 	if (error != 0) {
8387c478bd9Sstevel@tonic-gate 		as_free(newas);
8397c478bd9Sstevel@tonic-gate 		return (error);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	*outas = newas;
8427c478bd9Sstevel@tonic-gate 	return (0);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * Handle a ``fault'' at addr for size bytes.
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate faultcode_t
8497c478bd9Sstevel@tonic-gate as_fault(struct hat *hat, struct as *as, caddr_t addr, size_t size,
8507c478bd9Sstevel@tonic-gate 	enum fault_type type, enum seg_rw rw)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	struct seg *seg;
8537c478bd9Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
8547c478bd9Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
8557c478bd9Sstevel@tonic-gate 	size_t ssize;
8567c478bd9Sstevel@tonic-gate 	faultcode_t res = 0;
8577c478bd9Sstevel@tonic-gate 	caddr_t addrsav;
8587c478bd9Sstevel@tonic-gate 	struct seg *segsav;
8597c478bd9Sstevel@tonic-gate 	int as_lock_held;
8607c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8617c478bd9Sstevel@tonic-gate 	int is_xhat = 0;
8627c478bd9Sstevel@tonic-gate 	int holding_wpage = 0;
8637c478bd9Sstevel@tonic-gate 	extern struct seg_ops   segdev_ops;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (as->a_hat != hat) {
8687c478bd9Sstevel@tonic-gate 		/* This must be an XHAT then */
8697c478bd9Sstevel@tonic-gate 		is_xhat = 1;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		if ((type != F_INVAL) || (as == &kas))
8727c478bd9Sstevel@tonic-gate 			return (FC_NOSUPPORT);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate retry:
8767c478bd9Sstevel@tonic-gate 	if (!is_xhat) {
8777c478bd9Sstevel@tonic-gate 		/*
8787c478bd9Sstevel@tonic-gate 		 * Indicate that the lwp is not to be stopped while waiting
8797c478bd9Sstevel@tonic-gate 		 * for a pagefault.  This is to avoid deadlock while debugging
8807c478bd9Sstevel@tonic-gate 		 * a process via /proc over NFS (in particular).
8817c478bd9Sstevel@tonic-gate 		 */
8828548bf79Snr 		if (lwp != NULL)
8837c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop++;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		/*
8867c478bd9Sstevel@tonic-gate 		 * same length must be used when we softlock and softunlock.
8877c478bd9Sstevel@tonic-gate 		 * We don't support softunlocking lengths less than
8887c478bd9Sstevel@tonic-gate 		 * the original length when there is largepage support.
8897c478bd9Sstevel@tonic-gate 		 * See seg_dev.c for more comments.
8907c478bd9Sstevel@tonic-gate 		 */
8917c478bd9Sstevel@tonic-gate 		switch (type) {
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		case F_SOFTLOCK:
8947c478bd9Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, softlock, 1);
8957c478bd9Sstevel@tonic-gate 			break;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		case F_SOFTUNLOCK:
8987c478bd9Sstevel@tonic-gate 			break;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		case F_PROT:
9017c478bd9Sstevel@tonic-gate 			CPU_STATS_ADD_K(vm, prot_fault, 1);
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		case F_INVAL:
9057c478bd9Sstevel@tonic-gate 			CPU_STATS_ENTER_K();
9067c478bd9Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, vm, as_fault, 1);
9077c478bd9Sstevel@tonic-gate 			if (as == &kas)
9087c478bd9Sstevel@tonic-gate 				CPU_STATS_ADDQ(CPU, vm, kernel_asflt, 1);
9097c478bd9Sstevel@tonic-gate 			CPU_STATS_EXIT_K();
9107c478bd9Sstevel@tonic-gate 			break;
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/* Kernel probe */
9157c478bd9Sstevel@tonic-gate 	TNF_PROBE_3(address_fault, "vm pagefault", /* CSTYLED */,
9167c478bd9Sstevel@tonic-gate 		tnf_opaque,	address,	addr,
9177c478bd9Sstevel@tonic-gate 		tnf_fault_type,	fault_type,	type,
9187c478bd9Sstevel@tonic-gate 		tnf_seg_access,	access,		rw);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
9217c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
9227c478bd9Sstevel@tonic-gate 		(size_t)raddr;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * XXX -- Don't grab the as lock for segkmap. We should grab it for
9267c478bd9Sstevel@tonic-gate 	 * correctness, but then we could be stuck holding this lock for
9277c478bd9Sstevel@tonic-gate 	 * a LONG time if the fault needs to be resolved on a slow
9287c478bd9Sstevel@tonic-gate 	 * filesystem, and then no-one will be able to exec new commands,
9297c478bd9Sstevel@tonic-gate 	 * as exec'ing requires the write lock on the as.
9307c478bd9Sstevel@tonic-gate 	 */
9317c478bd9Sstevel@tonic-gate 	if (as == &kas && segkmap && segkmap->s_base <= raddr &&
9327c478bd9Sstevel@tonic-gate 	    raddr + size < segkmap->s_base + segkmap->s_size) {
9337c478bd9Sstevel@tonic-gate 		/*
9347c478bd9Sstevel@tonic-gate 		 * if (as==&kas), this can't be XHAT: we've already returned
9357c478bd9Sstevel@tonic-gate 		 * FC_NOSUPPORT.
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		seg = segkmap;
9387c478bd9Sstevel@tonic-gate 		as_lock_held = 0;
9397c478bd9Sstevel@tonic-gate 	} else {
9407c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
9417c478bd9Sstevel@tonic-gate 		if (is_xhat && avl_numnodes(&as->a_wpage) != 0) {
9427c478bd9Sstevel@tonic-gate 			/*
9437c478bd9Sstevel@tonic-gate 			 * Grab and hold the writers' lock on the as
9447c478bd9Sstevel@tonic-gate 			 * if the fault is to a watched page.
9457c478bd9Sstevel@tonic-gate 			 * This will keep CPUs from "peeking" at the
9467c478bd9Sstevel@tonic-gate 			 * address range while we're temporarily boosting
9477c478bd9Sstevel@tonic-gate 			 * the permissions for the XHAT device to
9487c478bd9Sstevel@tonic-gate 			 * resolve the fault in the segment layer.
9497c478bd9Sstevel@tonic-gate 			 *
9507c478bd9Sstevel@tonic-gate 			 * We could check whether faulted address
9517c478bd9Sstevel@tonic-gate 			 * is within a watched page and only then grab
9527c478bd9Sstevel@tonic-gate 			 * the writer lock, but this is simpler.
9537c478bd9Sstevel@tonic-gate 			 */
9547c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
9557c478bd9Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		seg = as_segat(as, raddr);
9597c478bd9Sstevel@tonic-gate 		if (seg == NULL) {
9607c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
9618548bf79Snr 			if ((lwp != NULL) && (!is_xhat))
9627c478bd9Sstevel@tonic-gate 				lwp->lwp_nostop--;
9637c478bd9Sstevel@tonic-gate 			return (FC_NOMAP);
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 		as_lock_held = 1;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	addrsav = raddr;
9707c478bd9Sstevel@tonic-gate 	segsav = seg;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
9737c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
9747c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
9757c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
9767c478bd9Sstevel@tonic-gate 				res = FC_NOMAP;
9777c478bd9Sstevel@tonic-gate 				break;
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 		if (raddr + rsize > seg->s_base + seg->s_size)
9817c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
9827c478bd9Sstevel@tonic-gate 		else
9837c478bd9Sstevel@tonic-gate 			ssize = rsize;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		if (!is_xhat || (seg->s_ops != &segdev_ops)) {
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 			if (is_xhat && avl_numnodes(&as->a_wpage) != 0 &&
9887c478bd9Sstevel@tonic-gate 			    pr_is_watchpage_as(raddr, rw, as)) {
9897c478bd9Sstevel@tonic-gate 				/*
9907c478bd9Sstevel@tonic-gate 				 * Handle watch pages.  If we're faulting on a
9917c478bd9Sstevel@tonic-gate 				 * watched page from an X-hat, we have to
9927c478bd9Sstevel@tonic-gate 				 * restore the original permissions while we
9937c478bd9Sstevel@tonic-gate 				 * handle the fault.
9947c478bd9Sstevel@tonic-gate 				 */
9957c478bd9Sstevel@tonic-gate 				as_clearwatch(as);
9967c478bd9Sstevel@tonic-gate 				holding_wpage = 1;
9977c478bd9Sstevel@tonic-gate 			}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 			res = SEGOP_FAULT(hat, seg, raddr, ssize, type, rw);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 			/* Restore watchpoints */
10027c478bd9Sstevel@tonic-gate 			if (holding_wpage) {
10037c478bd9Sstevel@tonic-gate 				as_setwatch(as);
10047c478bd9Sstevel@tonic-gate 				holding_wpage = 0;
10057c478bd9Sstevel@tonic-gate 			}
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 			if (res != 0)
10087c478bd9Sstevel@tonic-gate 				break;
10097c478bd9Sstevel@tonic-gate 		} else {
10107c478bd9Sstevel@tonic-gate 			/* XHAT does not support seg_dev */
10117c478bd9Sstevel@tonic-gate 			res = FC_NOSUPPORT;
10127c478bd9Sstevel@tonic-gate 			break;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * If we were SOFTLOCKing and encountered a failure,
10187c478bd9Sstevel@tonic-gate 	 * we must SOFTUNLOCK the range we already did. (Maybe we
10197c478bd9Sstevel@tonic-gate 	 * should just panic if we are SOFTLOCKing or even SOFTUNLOCKing
10207c478bd9Sstevel@tonic-gate 	 * right here...)
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 	if (res != 0 && type == F_SOFTLOCK) {
10237c478bd9Sstevel@tonic-gate 		for (seg = segsav; addrsav < raddr; addrsav += ssize) {
10247c478bd9Sstevel@tonic-gate 			if (addrsav >= seg->s_base + seg->s_size)
10257c478bd9Sstevel@tonic-gate 				seg = AS_SEGNEXT(as, seg);
10267c478bd9Sstevel@tonic-gate 			ASSERT(seg != NULL);
10277c478bd9Sstevel@tonic-gate 			/*
10287c478bd9Sstevel@tonic-gate 			 * Now call the fault routine again to perform the
10297c478bd9Sstevel@tonic-gate 			 * unlock using S_OTHER instead of the rw variable
10307c478bd9Sstevel@tonic-gate 			 * since we never got a chance to touch the pages.
10317c478bd9Sstevel@tonic-gate 			 */
10327c478bd9Sstevel@tonic-gate 			if (raddr > seg->s_base + seg->s_size)
10337c478bd9Sstevel@tonic-gate 				ssize = seg->s_base + seg->s_size - addrsav;
10347c478bd9Sstevel@tonic-gate 			else
10357c478bd9Sstevel@tonic-gate 				ssize = raddr - addrsav;
10367c478bd9Sstevel@tonic-gate 			(void) SEGOP_FAULT(hat, seg, addrsav, ssize,
10377c478bd9Sstevel@tonic-gate 			    F_SOFTUNLOCK, S_OTHER);
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 	if (as_lock_held)
10417c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
10428548bf79Snr 	if ((lwp != NULL) && (!is_xhat))
10437c478bd9Sstevel@tonic-gate 		lwp->lwp_nostop--;
10448548bf79Snr 
10457c478bd9Sstevel@tonic-gate 	/*
10467c478bd9Sstevel@tonic-gate 	 * If the lower levels returned EDEADLK for a fault,
10477c478bd9Sstevel@tonic-gate 	 * It means that we should retry the fault.  Let's wait
10487c478bd9Sstevel@tonic-gate 	 * a bit also to let the deadlock causing condition clear.
10497c478bd9Sstevel@tonic-gate 	 * This is part of a gross hack to work around a design flaw
10507c478bd9Sstevel@tonic-gate 	 * in the ufs/sds logging code and should go away when the
10517c478bd9Sstevel@tonic-gate 	 * logging code is re-designed to fix the problem. See bug
10527c478bd9Sstevel@tonic-gate 	 * 4125102 for details of the problem.
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	if (FC_ERRNO(res) == EDEADLK) {
10557c478bd9Sstevel@tonic-gate 		delay(deadlk_wait);
10567c478bd9Sstevel@tonic-gate 		res = 0;
10577c478bd9Sstevel@tonic-gate 		goto retry;
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	return (res);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * Asynchronous ``fault'' at addr for size bytes.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate faultcode_t
10687c478bd9Sstevel@tonic-gate as_faulta(struct as *as, caddr_t addr, size_t size)
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	struct seg *seg;
10717c478bd9Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
10727c478bd9Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
10737c478bd9Sstevel@tonic-gate 	faultcode_t res = 0;
10747c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate retry:
10777c478bd9Sstevel@tonic-gate 	/*
10787c478bd9Sstevel@tonic-gate 	 * Indicate that the lwp is not to be stopped while waiting
10797c478bd9Sstevel@tonic-gate 	 * for a pagefault.  This is to avoid deadlock while debugging
10807c478bd9Sstevel@tonic-gate 	 * a process via /proc over NFS (in particular).
10817c478bd9Sstevel@tonic-gate 	 */
10828548bf79Snr 	if (lwp != NULL)
10837c478bd9Sstevel@tonic-gate 		lwp->lwp_nostop++;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
10867c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
10877c478bd9Sstevel@tonic-gate 		(size_t)raddr;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
10907c478bd9Sstevel@tonic-gate 	seg = as_segat(as, raddr);
10917c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
10927c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
10938548bf79Snr 		if (lwp != NULL)
10947c478bd9Sstevel@tonic-gate 			lwp->lwp_nostop--;
10957c478bd9Sstevel@tonic-gate 		return (FC_NOMAP);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	for (; rsize != 0; rsize -= PAGESIZE, raddr += PAGESIZE) {
10997c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
11007c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
11017c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
11027c478bd9Sstevel@tonic-gate 				res = FC_NOMAP;
11037c478bd9Sstevel@tonic-gate 				break;
11047c478bd9Sstevel@tonic-gate 			}
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 		res = SEGOP_FAULTA(seg, raddr);
11077c478bd9Sstevel@tonic-gate 		if (res != 0)
11087c478bd9Sstevel@tonic-gate 			break;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
11118548bf79Snr 	if (lwp != NULL)
11127c478bd9Sstevel@tonic-gate 		lwp->lwp_nostop--;
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * If the lower levels returned EDEADLK for a fault,
11157c478bd9Sstevel@tonic-gate 	 * It means that we should retry the fault.  Let's wait
11167c478bd9Sstevel@tonic-gate 	 * a bit also to let the deadlock causing condition clear.
11177c478bd9Sstevel@tonic-gate 	 * This is part of a gross hack to work around a design flaw
11187c478bd9Sstevel@tonic-gate 	 * in the ufs/sds logging code and should go away when the
11197c478bd9Sstevel@tonic-gate 	 * logging code is re-designed to fix the problem. See bug
11207c478bd9Sstevel@tonic-gate 	 * 4125102 for details of the problem.
11217c478bd9Sstevel@tonic-gate 	 */
11227c478bd9Sstevel@tonic-gate 	if (FC_ERRNO(res) == EDEADLK) {
11237c478bd9Sstevel@tonic-gate 		delay(deadlk_wait);
11247c478bd9Sstevel@tonic-gate 		res = 0;
11257c478bd9Sstevel@tonic-gate 		goto retry;
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	return (res);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * Set the virtual mapping for the interval from [addr : addr + size)
11327c478bd9Sstevel@tonic-gate  * in address space `as' to have the specified protection.
11337c478bd9Sstevel@tonic-gate  * It is ok for the range to cross over several segments,
11347c478bd9Sstevel@tonic-gate  * as long as they are contiguous.
11357c478bd9Sstevel@tonic-gate  */
11367c478bd9Sstevel@tonic-gate int
11377c478bd9Sstevel@tonic-gate as_setprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	struct seg *seg;
11407c478bd9Sstevel@tonic-gate 	struct as_callback *cb;
11417c478bd9Sstevel@tonic-gate 	size_t ssize;
11427c478bd9Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
11437c478bd9Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
11447c478bd9Sstevel@tonic-gate 	int error = 0, writer = 0;
11457c478bd9Sstevel@tonic-gate 	caddr_t saveraddr;
11467c478bd9Sstevel@tonic-gate 	size_t saversize;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate setprot_top:
11497c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
11507c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
11517c478bd9Sstevel@tonic-gate 		(size_t)raddr;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
11547c478bd9Sstevel@tonic-gate 		return (ENOMEM);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	saveraddr = raddr;
11577c478bd9Sstevel@tonic-gate 	saversize = rsize;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	/*
11607c478bd9Sstevel@tonic-gate 	 * Normally we only lock the as as a reader. But
11617c478bd9Sstevel@tonic-gate 	 * if due to setprot the segment driver needs to split
11627c478bd9Sstevel@tonic-gate 	 * a segment it will return IE_RETRY. Therefore we re-aquire
11637c478bd9Sstevel@tonic-gate 	 * the as lock as a writer so the segment driver can change
11647c478bd9Sstevel@tonic-gate 	 * the seg list. Also the segment driver will return IE_RETRY
11657c478bd9Sstevel@tonic-gate 	 * after it has changed the segment list so we therefore keep
11667c478bd9Sstevel@tonic-gate 	 * locking as a writer. Since these opeartions should be rare
11677c478bd9Sstevel@tonic-gate 	 * want to only lock as a writer when necessary.
11687c478bd9Sstevel@tonic-gate 	 */
11697c478bd9Sstevel@tonic-gate 	if (writer || avl_numnodes(&as->a_wpage) != 0) {
11707c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
11717c478bd9Sstevel@tonic-gate 	} else {
11727c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, rsize);
11767c478bd9Sstevel@tonic-gate 	seg = as_segat(as, raddr);
11777c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
11787c478bd9Sstevel@tonic-gate 		as_setwatch(as);
11797c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
11807c478bd9Sstevel@tonic-gate 		return (ENOMEM);
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
11847c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
11857c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
11867c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
11877c478bd9Sstevel@tonic-gate 				error = ENOMEM;
11887c478bd9Sstevel@tonic-gate 				break;
11897c478bd9Sstevel@tonic-gate 			}
11907c478bd9Sstevel@tonic-gate 		}
11917c478bd9Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
11927c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
11937c478bd9Sstevel@tonic-gate 		else
11947c478bd9Sstevel@tonic-gate 			ssize = rsize;
11957c478bd9Sstevel@tonic-gate 		error = SEGOP_SETPROT(seg, raddr, ssize, prot);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		if (error == IE_NOMEM) {
11987c478bd9Sstevel@tonic-gate 			error = EAGAIN;
11997c478bd9Sstevel@tonic-gate 			break;
12007c478bd9Sstevel@tonic-gate 		}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		if (error == IE_RETRY) {
12037c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
12047c478bd9Sstevel@tonic-gate 			writer = 1;
12057c478bd9Sstevel@tonic-gate 			goto setprot_top;
12067c478bd9Sstevel@tonic-gate 		}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		if (error == EAGAIN) {
12097c478bd9Sstevel@tonic-gate 			/*
12107c478bd9Sstevel@tonic-gate 			 * Make sure we have a_lock as writer.
12117c478bd9Sstevel@tonic-gate 			 */
12127c478bd9Sstevel@tonic-gate 			if (writer == 0) {
12137c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
12147c478bd9Sstevel@tonic-gate 				writer = 1;
12157c478bd9Sstevel@tonic-gate 				goto setprot_top;
12167c478bd9Sstevel@tonic-gate 			}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 			/*
12197c478bd9Sstevel@tonic-gate 			 * Memory is currently locked.  It must be unlocked
12207c478bd9Sstevel@tonic-gate 			 * before this operation can succeed through a retry.
12217c478bd9Sstevel@tonic-gate 			 * The possible reasons for locked memory and
12227c478bd9Sstevel@tonic-gate 			 * corresponding strategies for unlocking are:
12237c478bd9Sstevel@tonic-gate 			 * (1) Normal I/O
12247c478bd9Sstevel@tonic-gate 			 *	wait for a signal that the I/O operation
12257c478bd9Sstevel@tonic-gate 			 *	has completed and the memory is unlocked.
12267c478bd9Sstevel@tonic-gate 			 * (2) Asynchronous I/O
12277c478bd9Sstevel@tonic-gate 			 *	The aio subsystem does not unlock pages when
12287c478bd9Sstevel@tonic-gate 			 *	the I/O is completed. Those pages are unlocked
12297c478bd9Sstevel@tonic-gate 			 *	when the application calls aiowait/aioerror.
12307c478bd9Sstevel@tonic-gate 			 *	So, to prevent blocking forever, cv_broadcast()
12317c478bd9Sstevel@tonic-gate 			 *	is done to wake up aio_cleanup_thread.
12327c478bd9Sstevel@tonic-gate 			 *	Subsequently, segvn_reclaim will be called, and
12337c478bd9Sstevel@tonic-gate 			 *	that will do AS_CLRUNMAPWAIT() and wake us up.
12347c478bd9Sstevel@tonic-gate 			 * (3) Long term page locking:
12357c478bd9Sstevel@tonic-gate 			 *	Drivers intending to have pages locked for a
12367c478bd9Sstevel@tonic-gate 			 *	period considerably longer than for normal I/O
12377c478bd9Sstevel@tonic-gate 			 *	(essentially forever) may have registered for a
12387c478bd9Sstevel@tonic-gate 			 *	callback so they may unlock these pages on
12397c478bd9Sstevel@tonic-gate 			 *	request. This is needed to allow this operation
12407c478bd9Sstevel@tonic-gate 			 *	to succeed. Each entry on the callback list is
12417c478bd9Sstevel@tonic-gate 			 *	examined. If the event or address range pertains
12427c478bd9Sstevel@tonic-gate 			 *	the callback is invoked (unless it already is in
12437c478bd9Sstevel@tonic-gate 			 *	progress). The a_contents lock must be dropped
12447c478bd9Sstevel@tonic-gate 			 *	before the callback, so only one callback can
12457c478bd9Sstevel@tonic-gate 			 *	be done at a time. Go to the top and do more
12467c478bd9Sstevel@tonic-gate 			 *	until zero is returned. If zero is returned,
12477c478bd9Sstevel@tonic-gate 			 *	either there were no callbacks for this event
12487c478bd9Sstevel@tonic-gate 			 *	or they were already in progress.
12497c478bd9Sstevel@tonic-gate 			 */
12507c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
12517c478bd9Sstevel@tonic-gate 			if (as->a_callbacks &&
12527c478bd9Sstevel@tonic-gate 				(cb = as_find_callback(as, AS_SETPROT_EVENT,
12537c478bd9Sstevel@tonic-gate 						seg->s_base, seg->s_size))) {
12547c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
12557c478bd9Sstevel@tonic-gate 				as_execute_callback(as, cb, AS_SETPROT_EVENT);
12567c478bd9Sstevel@tonic-gate 			} else {
12577c478bd9Sstevel@tonic-gate 				if (AS_ISUNMAPWAIT(as) == 0)
12587c478bd9Sstevel@tonic-gate 					cv_broadcast(&as->a_cv);
12597c478bd9Sstevel@tonic-gate 				AS_SETUNMAPWAIT(as);
12607c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
12617c478bd9Sstevel@tonic-gate 				while (AS_ISUNMAPWAIT(as))
12627c478bd9Sstevel@tonic-gate 					cv_wait(&as->a_cv, &as->a_contents);
12637c478bd9Sstevel@tonic-gate 			}
12647c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
12657c478bd9Sstevel@tonic-gate 			goto setprot_top;
12667c478bd9Sstevel@tonic-gate 		} else if (error != 0)
12677c478bd9Sstevel@tonic-gate 			break;
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 	if (error != 0) {
12707c478bd9Sstevel@tonic-gate 		as_setwatch(as);
12717c478bd9Sstevel@tonic-gate 	} else {
12727c478bd9Sstevel@tonic-gate 		as_setwatchprot(as, saveraddr, saversize, prot);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
12757c478bd9Sstevel@tonic-gate 	return (error);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate  * Check to make sure that the interval [addr, addr + size)
12807c478bd9Sstevel@tonic-gate  * in address space `as' has at least the specified protection.
12817c478bd9Sstevel@tonic-gate  * It is ok for the range to cross over several segments, as long
12827c478bd9Sstevel@tonic-gate  * as they are contiguous.
12837c478bd9Sstevel@tonic-gate  */
12847c478bd9Sstevel@tonic-gate int
12857c478bd9Sstevel@tonic-gate as_checkprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	struct seg *seg;
12887c478bd9Sstevel@tonic-gate 	size_t ssize;
12897c478bd9Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
12907c478bd9Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
12917c478bd9Sstevel@tonic-gate 	int error = 0;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
12947c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
12957c478bd9Sstevel@tonic-gate 		(size_t)raddr;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
12987c478bd9Sstevel@tonic-gate 		return (ENOMEM);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * This is ugly as sin...
13027c478bd9Sstevel@tonic-gate 	 * Normally, we only acquire the address space readers lock.
13037c478bd9Sstevel@tonic-gate 	 * However, if the address space has watchpoints present,
13047c478bd9Sstevel@tonic-gate 	 * we must acquire the writer lock on the address space for
13057c478bd9Sstevel@tonic-gate 	 * the benefit of as_clearwatchprot() and as_setwatchprot().
13067c478bd9Sstevel@tonic-gate 	 */
13077c478bd9Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) != 0)
13087c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
13097c478bd9Sstevel@tonic-gate 	else
13107c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
13117c478bd9Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, rsize);
13127c478bd9Sstevel@tonic-gate 	seg = as_segat(as, raddr);
13137c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
13147c478bd9Sstevel@tonic-gate 		as_setwatch(as);
13157c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
13167c478bd9Sstevel@tonic-gate 		return (ENOMEM);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
13207c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
13217c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
13227c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
13237c478bd9Sstevel@tonic-gate 				error = ENOMEM;
13247c478bd9Sstevel@tonic-gate 				break;
13257c478bd9Sstevel@tonic-gate 			}
13267c478bd9Sstevel@tonic-gate 		}
13277c478bd9Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
13287c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
13297c478bd9Sstevel@tonic-gate 		else
13307c478bd9Sstevel@tonic-gate 			ssize = rsize;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 		error = SEGOP_CHECKPROT(seg, raddr, ssize, prot);
13337c478bd9Sstevel@tonic-gate 		if (error != 0)
13347c478bd9Sstevel@tonic-gate 			break;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 	as_setwatch(as);
13377c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
13387c478bd9Sstevel@tonic-gate 	return (error);
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate int
13427c478bd9Sstevel@tonic-gate as_unmap(struct as *as, caddr_t addr, size_t size)
13437c478bd9Sstevel@tonic-gate {
13447c478bd9Sstevel@tonic-gate 	struct seg *seg, *seg_next;
13457c478bd9Sstevel@tonic-gate 	struct as_callback *cb;
13467c478bd9Sstevel@tonic-gate 	caddr_t raddr, eaddr;
13477c478bd9Sstevel@tonic-gate 	size_t ssize;
13487c478bd9Sstevel@tonic-gate 	int err;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate top:
13517c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
13527c478bd9Sstevel@tonic-gate 	eaddr = (caddr_t)(((uintptr_t)(addr + size) + PAGEOFFSET) &
13537c478bd9Sstevel@tonic-gate 	    (uintptr_t)PAGEMASK);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
13587c478bd9Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * Use as_findseg to find the first segment in the range, then
13627c478bd9Sstevel@tonic-gate 	 * step through the segments in order, following s_next.
13637c478bd9Sstevel@tonic-gate 	 */
13647c478bd9Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, eaddr - raddr);
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	for (seg = as_findseg(as, raddr, 0); seg != NULL; seg = seg_next) {
13677c478bd9Sstevel@tonic-gate 		if (eaddr <= seg->s_base)
13687c478bd9Sstevel@tonic-gate 			break;		/* eaddr was in a gap; all done */
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 		/* this is implied by the test above */
13717c478bd9Sstevel@tonic-gate 		ASSERT(raddr < eaddr);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 		if (raddr < seg->s_base)
13747c478bd9Sstevel@tonic-gate 			raddr = seg->s_base; 	/* raddr was in a gap */
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		if (eaddr > (seg->s_base + seg->s_size))
13777c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
13787c478bd9Sstevel@tonic-gate 		else
13797c478bd9Sstevel@tonic-gate 			ssize = eaddr - raddr;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 		/*
13827c478bd9Sstevel@tonic-gate 		 * Save next segment pointer since seg can be
13837c478bd9Sstevel@tonic-gate 		 * destroyed during the segment unmap operation.
13847c478bd9Sstevel@tonic-gate 		 */
13857c478bd9Sstevel@tonic-gate 		seg_next = AS_SEGNEXT(as, seg);
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 		err = SEGOP_UNMAP(seg, raddr, ssize);
13887c478bd9Sstevel@tonic-gate 		if (err == EAGAIN) {
13897c478bd9Sstevel@tonic-gate 			/*
13907c478bd9Sstevel@tonic-gate 			 * Memory is currently locked.  It must be unlocked
13917c478bd9Sstevel@tonic-gate 			 * before this operation can succeed through a retry.
13927c478bd9Sstevel@tonic-gate 			 * The possible reasons for locked memory and
13937c478bd9Sstevel@tonic-gate 			 * corresponding strategies for unlocking are:
13947c478bd9Sstevel@tonic-gate 			 * (1) Normal I/O
13957c478bd9Sstevel@tonic-gate 			 *	wait for a signal that the I/O operation
13967c478bd9Sstevel@tonic-gate 			 *	has completed and the memory is unlocked.
13977c478bd9Sstevel@tonic-gate 			 * (2) Asynchronous I/O
13987c478bd9Sstevel@tonic-gate 			 *	The aio subsystem does not unlock pages when
13997c478bd9Sstevel@tonic-gate 			 *	the I/O is completed. Those pages are unlocked
14007c478bd9Sstevel@tonic-gate 			 *	when the application calls aiowait/aioerror.
14017c478bd9Sstevel@tonic-gate 			 *	So, to prevent blocking forever, cv_broadcast()
14027c478bd9Sstevel@tonic-gate 			 *	is done to wake up aio_cleanup_thread.
14037c478bd9Sstevel@tonic-gate 			 *	Subsequently, segvn_reclaim will be called, and
14047c478bd9Sstevel@tonic-gate 			 *	that will do AS_CLRUNMAPWAIT() and wake us up.
14057c478bd9Sstevel@tonic-gate 			 * (3) Long term page locking:
14067c478bd9Sstevel@tonic-gate 			 *	Drivers intending to have pages locked for a
14077c478bd9Sstevel@tonic-gate 			 *	period considerably longer than for normal I/O
14087c478bd9Sstevel@tonic-gate 			 *	(essentially forever) may have registered for a
14097c478bd9Sstevel@tonic-gate 			 *	callback so they may unlock these pages on
14107c478bd9Sstevel@tonic-gate 			 *	request. This is needed to allow this operation
14117c478bd9Sstevel@tonic-gate 			 *	to succeed. Each entry on the callback list is
14127c478bd9Sstevel@tonic-gate 			 *	examined. If the event or address range pertains
14137c478bd9Sstevel@tonic-gate 			 *	the callback is invoked (unless it already is in
14147c478bd9Sstevel@tonic-gate 			 *	progress). The a_contents lock must be dropped
14157c478bd9Sstevel@tonic-gate 			 *	before the callback, so only one callback can
14167c478bd9Sstevel@tonic-gate 			 *	be done at a time. Go to the top and do more
14177c478bd9Sstevel@tonic-gate 			 *	until zero is returned. If zero is returned,
14187c478bd9Sstevel@tonic-gate 			 *	either there were no callbacks for this event
14197c478bd9Sstevel@tonic-gate 			 *	or they were already in progress.
14207c478bd9Sstevel@tonic-gate 			 */
14217c478bd9Sstevel@tonic-gate 			as_setwatch(as);
14227c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
14237c478bd9Sstevel@tonic-gate 			if (as->a_callbacks &&
14247c478bd9Sstevel@tonic-gate 				(cb = as_find_callback(as, AS_UNMAP_EVENT,
14257c478bd9Sstevel@tonic-gate 						seg->s_base, seg->s_size))) {
14267c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
14277c478bd9Sstevel@tonic-gate 				as_execute_callback(as, cb, AS_UNMAP_EVENT);
14287c478bd9Sstevel@tonic-gate 			} else {
14297c478bd9Sstevel@tonic-gate 				if (AS_ISUNMAPWAIT(as) == 0)
14307c478bd9Sstevel@tonic-gate 					cv_broadcast(&as->a_cv);
14317c478bd9Sstevel@tonic-gate 				AS_SETUNMAPWAIT(as);
14327c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
14337c478bd9Sstevel@tonic-gate 				while (AS_ISUNMAPWAIT(as))
14347c478bd9Sstevel@tonic-gate 					cv_wait(&as->a_cv, &as->a_contents);
14357c478bd9Sstevel@tonic-gate 			}
14367c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
14377c478bd9Sstevel@tonic-gate 			goto top;
14387c478bd9Sstevel@tonic-gate 		} else if (err == IE_RETRY) {
14397c478bd9Sstevel@tonic-gate 			as_setwatch(as);
14407c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
14417c478bd9Sstevel@tonic-gate 			goto top;
14427c478bd9Sstevel@tonic-gate 		} else if (err) {
14437c478bd9Sstevel@tonic-gate 			as_setwatch(as);
14447c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
14457c478bd9Sstevel@tonic-gate 			return (-1);
14467c478bd9Sstevel@tonic-gate 		}
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 		as->a_size -= ssize;
14497c478bd9Sstevel@tonic-gate 		raddr += ssize;
14507c478bd9Sstevel@tonic-gate 	}
14517c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
14527c478bd9Sstevel@tonic-gate 	return (0);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate static int
145607b65a64Saguzovsk as_map_segvn_segs(struct as *as, caddr_t addr, size_t size, uint_t szcvec,
14577c478bd9Sstevel@tonic-gate     int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
14587c478bd9Sstevel@tonic-gate {
14597c478bd9Sstevel@tonic-gate 	uint_t szc;
14607c478bd9Sstevel@tonic-gate 	uint_t nszc;
14617c478bd9Sstevel@tonic-gate 	int error;
14627c478bd9Sstevel@tonic-gate 	caddr_t a;
14637c478bd9Sstevel@tonic-gate 	caddr_t eaddr;
14647c478bd9Sstevel@tonic-gate 	size_t segsize;
14657c478bd9Sstevel@tonic-gate 	struct seg *seg;
14667c478bd9Sstevel@tonic-gate 	size_t pgsz;
146707b65a64Saguzovsk 	int do_off = (vn_a->vp != NULL || vn_a->amp != NULL);
146807b65a64Saguzovsk 	uint_t save_szcvec;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
14717c478bd9Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
14727c478bd9Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
147307b65a64Saguzovsk 	ASSERT(vn_a->vp == NULL || vn_a->amp == NULL);
147407b65a64Saguzovsk 	if (!do_off) {
147507b65a64Saguzovsk 		vn_a->offset = 0;
147607b65a64Saguzovsk 	}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if (szcvec <= 1) {
14797c478bd9Sstevel@tonic-gate 		seg = seg_alloc(as, addr, size);
14807c478bd9Sstevel@tonic-gate 		if (seg == NULL) {
14817c478bd9Sstevel@tonic-gate 			return (ENOMEM);
14827c478bd9Sstevel@tonic-gate 		}
14837c478bd9Sstevel@tonic-gate 		vn_a->szc = 0;
14847c478bd9Sstevel@tonic-gate 		error = (*crfp)(seg, vn_a);
14857c478bd9Sstevel@tonic-gate 		if (error != 0) {
14867c478bd9Sstevel@tonic-gate 			seg_free(seg);
14873a30c6acSsusans 		} else {
14883a30c6acSsusans 			as->a_size += size;
14897c478bd9Sstevel@tonic-gate 		}
14907c478bd9Sstevel@tonic-gate 		return (error);
14917c478bd9Sstevel@tonic-gate 	}
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	eaddr = addr + size;
14947c478bd9Sstevel@tonic-gate 	save_szcvec = szcvec;
14957c478bd9Sstevel@tonic-gate 	szcvec >>= 1;
14967c478bd9Sstevel@tonic-gate 	szc = 0;
14977c478bd9Sstevel@tonic-gate 	nszc = 0;
14987c478bd9Sstevel@tonic-gate 	while (szcvec) {
14997c478bd9Sstevel@tonic-gate 		if ((szcvec & 0x1) == 0) {
15007c478bd9Sstevel@tonic-gate 			nszc++;
15017c478bd9Sstevel@tonic-gate 			szcvec >>= 1;
15027c478bd9Sstevel@tonic-gate 			continue;
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 		nszc++;
15057c478bd9Sstevel@tonic-gate 		pgsz = page_get_pagesize(nszc);
15067c478bd9Sstevel@tonic-gate 		a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
15077c478bd9Sstevel@tonic-gate 		if (a != addr) {
15087c478bd9Sstevel@tonic-gate 			ASSERT(a < eaddr);
15097c478bd9Sstevel@tonic-gate 			segsize = a - addr;
15107c478bd9Sstevel@tonic-gate 			seg = seg_alloc(as, addr, segsize);
15117c478bd9Sstevel@tonic-gate 			if (seg == NULL) {
15127c478bd9Sstevel@tonic-gate 				return (ENOMEM);
15137c478bd9Sstevel@tonic-gate 			}
15147c478bd9Sstevel@tonic-gate 			vn_a->szc = szc;
15157c478bd9Sstevel@tonic-gate 			error = (*crfp)(seg, vn_a);
15167c478bd9Sstevel@tonic-gate 			if (error != 0) {
15177c478bd9Sstevel@tonic-gate 				seg_free(seg);
15187c478bd9Sstevel@tonic-gate 				return (error);
15197c478bd9Sstevel@tonic-gate 			}
15203a30c6acSsusans 			as->a_size += segsize;
15217c478bd9Sstevel@tonic-gate 			*segcreated = 1;
152207b65a64Saguzovsk 			if (do_off) {
152307b65a64Saguzovsk 				vn_a->offset += segsize;
152407b65a64Saguzovsk 			}
15257c478bd9Sstevel@tonic-gate 			addr = a;
15267c478bd9Sstevel@tonic-gate 		}
15277c478bd9Sstevel@tonic-gate 		szc = nszc;
15287c478bd9Sstevel@tonic-gate 		szcvec >>= 1;
15297c478bd9Sstevel@tonic-gate 	}
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	ASSERT(addr < eaddr);
15327c478bd9Sstevel@tonic-gate 	szcvec = save_szcvec | 1; /* add 8K pages */
15337c478bd9Sstevel@tonic-gate 	while (szcvec) {
15347c478bd9Sstevel@tonic-gate 		a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
15357c478bd9Sstevel@tonic-gate 		ASSERT(a >= addr);
15367c478bd9Sstevel@tonic-gate 		if (a != addr) {
15377c478bd9Sstevel@tonic-gate 			segsize = a - addr;
15387c478bd9Sstevel@tonic-gate 			seg = seg_alloc(as, addr, segsize);
15397c478bd9Sstevel@tonic-gate 			if (seg == NULL) {
15407c478bd9Sstevel@tonic-gate 				return (ENOMEM);
15417c478bd9Sstevel@tonic-gate 			}
15427c478bd9Sstevel@tonic-gate 			vn_a->szc = szc;
15437c478bd9Sstevel@tonic-gate 			error = (*crfp)(seg, vn_a);
15447c478bd9Sstevel@tonic-gate 			if (error != 0) {
15457c478bd9Sstevel@tonic-gate 				seg_free(seg);
15467c478bd9Sstevel@tonic-gate 				return (error);
15477c478bd9Sstevel@tonic-gate 			}
15483a30c6acSsusans 			as->a_size += segsize;
15497c478bd9Sstevel@tonic-gate 			*segcreated = 1;
155007b65a64Saguzovsk 			if (do_off) {
155107b65a64Saguzovsk 				vn_a->offset += segsize;
155207b65a64Saguzovsk 			}
15537c478bd9Sstevel@tonic-gate 			addr = a;
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 		szcvec &= ~(1 << szc);
15567c478bd9Sstevel@tonic-gate 		if (szcvec) {
15577c478bd9Sstevel@tonic-gate 			szc = highbit(szcvec) - 1;
15587c478bd9Sstevel@tonic-gate 			pgsz = page_get_pagesize(szc);
15597c478bd9Sstevel@tonic-gate 		}
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 	ASSERT(addr == eaddr);
15627c478bd9Sstevel@tonic-gate 
156307b65a64Saguzovsk 	return (0);
156407b65a64Saguzovsk }
156507b65a64Saguzovsk 
156607b65a64Saguzovsk static int
156707b65a64Saguzovsk as_map_vnsegs(struct as *as, caddr_t addr, size_t size,
156807b65a64Saguzovsk     int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
156907b65a64Saguzovsk {
1570ec25b48fSsusans 	uint_t mapflags = vn_a->flags & (MAP_TEXT | MAP_INITDATA);
1571ec25b48fSsusans 	int type = (vn_a->type == MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM;
1572ec25b48fSsusans 	uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags,
1573ec25b48fSsusans 	    type, 0);
157407b65a64Saguzovsk 	int error;
157507b65a64Saguzovsk 	struct seg *seg;
157607b65a64Saguzovsk 	struct vattr va;
157707b65a64Saguzovsk 	u_offset_t eoff;
157807b65a64Saguzovsk 	size_t save_size = 0;
15792cb27123Saguzovsk 	extern size_t textrepl_size_thresh;
158007b65a64Saguzovsk 
158107b65a64Saguzovsk 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
158207b65a64Saguzovsk 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
158307b65a64Saguzovsk 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
158407b65a64Saguzovsk 	ASSERT(vn_a->vp != NULL);
158507b65a64Saguzovsk 	ASSERT(vn_a->amp == NULL);
158607b65a64Saguzovsk 
158707b65a64Saguzovsk again:
158807b65a64Saguzovsk 	if (szcvec <= 1) {
158907b65a64Saguzovsk 		seg = seg_alloc(as, addr, size);
159007b65a64Saguzovsk 		if (seg == NULL) {
159107b65a64Saguzovsk 			return (ENOMEM);
159207b65a64Saguzovsk 		}
159307b65a64Saguzovsk 		vn_a->szc = 0;
159407b65a64Saguzovsk 		error = (*crfp)(seg, vn_a);
159507b65a64Saguzovsk 		if (error != 0) {
159607b65a64Saguzovsk 			seg_free(seg);
15973a30c6acSsusans 		} else {
15983a30c6acSsusans 			as->a_size += size;
159907b65a64Saguzovsk 		}
160007b65a64Saguzovsk 		return (error);
160107b65a64Saguzovsk 	}
160207b65a64Saguzovsk 
160307b65a64Saguzovsk 	va.va_mask = AT_SIZE;
160407b65a64Saguzovsk 	if (VOP_GETATTR(vn_a->vp, &va, ATTR_HINT, vn_a->cred) != 0) {
160507b65a64Saguzovsk 		szcvec = 0;
160607b65a64Saguzovsk 		goto again;
160707b65a64Saguzovsk 	}
160807b65a64Saguzovsk 	eoff = vn_a->offset & PAGEMASK;
160907b65a64Saguzovsk 	if (eoff >= va.va_size) {
161007b65a64Saguzovsk 		szcvec = 0;
161107b65a64Saguzovsk 		goto again;
161207b65a64Saguzovsk 	}
161307b65a64Saguzovsk 	eoff += size;
161407b65a64Saguzovsk 	if (btopr(va.va_size) < btopr(eoff)) {
161507b65a64Saguzovsk 		save_size = size;
161607b65a64Saguzovsk 		size = va.va_size - (vn_a->offset & PAGEMASK);
161707b65a64Saguzovsk 		size = P2ROUNDUP_TYPED(size, PAGESIZE, size_t);
1618ec25b48fSsusans 		szcvec = map_pgszcvec(addr, size, (uintptr_t)addr, mapflags,
1619ec25b48fSsusans 		    type, 0);
162007b65a64Saguzovsk 		if (szcvec <= 1) {
162107b65a64Saguzovsk 			size = save_size;
162207b65a64Saguzovsk 			goto again;
162307b65a64Saguzovsk 		}
162407b65a64Saguzovsk 	}
162507b65a64Saguzovsk 
16262cb27123Saguzovsk 	if (size > textrepl_size_thresh) {
16272cb27123Saguzovsk 		vn_a->flags |= _MAP_TEXTREPL;
16282cb27123Saguzovsk 	}
162907b65a64Saguzovsk 	error = as_map_segvn_segs(as, addr, size, szcvec, crfp, vn_a,
163007b65a64Saguzovsk 	    segcreated);
163107b65a64Saguzovsk 	if (error != 0) {
163207b65a64Saguzovsk 		return (error);
163307b65a64Saguzovsk 	}
16347c478bd9Sstevel@tonic-gate 	if (save_size) {
163507b65a64Saguzovsk 		addr += size;
16367c478bd9Sstevel@tonic-gate 		size = save_size - size;
163707b65a64Saguzovsk 		szcvec = 0;
16387c478bd9Sstevel@tonic-gate 		goto again;
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate 	return (0);
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate 
1643ec25b48fSsusans /*
1644ec25b48fSsusans  * as_map_ansegs: shared or private anonymous memory.  Note that the flags
1645ec25b48fSsusans  * passed to map_pgszvec cannot be MAP_INITDATA, for anon.
1646ec25b48fSsusans  */
164707b65a64Saguzovsk static int
1648ec25b48fSsusans as_map_ansegs(struct as *as, caddr_t addr, size_t size,
164907b65a64Saguzovsk     int (*crfp)(), struct segvn_crargs *vn_a, int *segcreated)
165007b65a64Saguzovsk {
1651ec25b48fSsusans 	uint_t szcvec;
1652ec25b48fSsusans 	uchar_t type;
1653ec25b48fSsusans 
1654ec25b48fSsusans 	ASSERT(vn_a->type == MAP_SHARED || vn_a->type == MAP_PRIVATE);
1655ec25b48fSsusans 	if (vn_a->type == MAP_SHARED) {
1656ec25b48fSsusans 		type = MAPPGSZC_SHM;
1657ec25b48fSsusans 	} else if (vn_a->type == MAP_PRIVATE) {
1658ec25b48fSsusans 		if (vn_a->szc == AS_MAP_HEAP) {
1659ec25b48fSsusans 			type = MAPPGSZC_HEAP;
1660ec25b48fSsusans 		} else if (vn_a->szc == AS_MAP_STACK) {
1661ec25b48fSsusans 			type = MAPPGSZC_STACK;
1662ec25b48fSsusans 		} else {
1663ec25b48fSsusans 			type = MAPPGSZC_PRIVM;
1664ec25b48fSsusans 		}
1665ec25b48fSsusans 	}
1666ec25b48fSsusans 	szcvec = map_pgszcvec(addr, size, vn_a->amp == NULL ?
1667ec25b48fSsusans 	    (uintptr_t)addr : (uintptr_t)P2ROUNDUP(vn_a->offset, PAGESIZE),
1668ec25b48fSsusans 	    (vn_a->flags & MAP_TEXT), type, 0);
166907b65a64Saguzovsk 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
167007b65a64Saguzovsk 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
167107b65a64Saguzovsk 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
167207b65a64Saguzovsk 	ASSERT(vn_a->vp == NULL);
167307b65a64Saguzovsk 
167407b65a64Saguzovsk 	return (as_map_segvn_segs(as, addr, size, szcvec,
167507b65a64Saguzovsk 	    crfp, vn_a, segcreated));
167607b65a64Saguzovsk }
167707b65a64Saguzovsk 
16787c478bd9Sstevel@tonic-gate int
16797c478bd9Sstevel@tonic-gate as_map(struct as *as, caddr_t addr, size_t size, int (*crfp)(), void *argsp)
168002ff05a9Svsakar {
168102ff05a9Svsakar 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
168202ff05a9Svsakar 	return (as_map_locked(as, addr, size, crfp, argsp));
168302ff05a9Svsakar }
168402ff05a9Svsakar 
168502ff05a9Svsakar int
168602ff05a9Svsakar as_map_locked(struct as *as, caddr_t addr, size_t size, int (*crfp)(),
168702ff05a9Svsakar 		void *argsp)
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate 	struct seg *seg = NULL;
16907c478bd9Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
16917c478bd9Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
16927c478bd9Sstevel@tonic-gate 	int error;
1693ec25b48fSsusans 	int unmap = 0;
16947c478bd9Sstevel@tonic-gate 	struct proc *p = curproc;
1695232cfe63Ssusans 	struct segvn_crargs crargs;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
16987c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
16997c478bd9Sstevel@tonic-gate 		(size_t)raddr;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	/*
17027c478bd9Sstevel@tonic-gate 	 * check for wrap around
17037c478bd9Sstevel@tonic-gate 	 */
17047c478bd9Sstevel@tonic-gate 	if ((raddr + rsize < raddr) || (as->a_size > (ULONG_MAX - size))) {
17057c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
17067c478bd9Sstevel@tonic-gate 		return (ENOMEM);
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	as->a_updatedir = 1;	/* inform /proc */
17107c478bd9Sstevel@tonic-gate 	gethrestime(&as->a_updatetime);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (as != &kas && as->a_size + rsize > (size_t)p->p_vmem_ctl) {
17137c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_VMEM], p->p_rctls, p,
17167c478bd9Sstevel@tonic-gate 		    RCA_UNSAFE_ALL);
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 		return (ENOMEM);
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 
1721ec25b48fSsusans 	if (AS_MAP_CHECK_VNODE_LPOOB(crfp, argsp)) {
1722232cfe63Ssusans 		crargs = *(struct segvn_crargs *)argsp;
1723232cfe63Ssusans 		error = as_map_vnsegs(as, raddr, rsize, crfp, &crargs, &unmap);
1724ec25b48fSsusans 		if (error != 0) {
1725ec25b48fSsusans 			AS_LOCK_EXIT(as, &as->a_lock);
1726ec25b48fSsusans 			if (unmap) {
1727ec25b48fSsusans 				(void) as_unmap(as, addr, size);
1728ec25b48fSsusans 			}
1729ec25b48fSsusans 			return (error);
173007b65a64Saguzovsk 		}
1731ec25b48fSsusans 	} else if (AS_MAP_CHECK_ANON_LPOOB(crfp, argsp)) {
1732232cfe63Ssusans 		crargs = *(struct segvn_crargs *)argsp;
1733232cfe63Ssusans 		error = as_map_ansegs(as, raddr, rsize, crfp, &crargs, &unmap);
17347c478bd9Sstevel@tonic-gate 		if (error != 0) {
17357c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
17367c478bd9Sstevel@tonic-gate 			if (unmap) {
17377c478bd9Sstevel@tonic-gate 				(void) as_unmap(as, addr, size);
17387c478bd9Sstevel@tonic-gate 			}
17397c478bd9Sstevel@tonic-gate 			return (error);
17407c478bd9Sstevel@tonic-gate 		}
17417c478bd9Sstevel@tonic-gate 	} else {
17427c478bd9Sstevel@tonic-gate 		seg = seg_alloc(as, addr, size);
17437c478bd9Sstevel@tonic-gate 		if (seg == NULL) {
17447c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
17457c478bd9Sstevel@tonic-gate 			return (ENOMEM);
17467c478bd9Sstevel@tonic-gate 		}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		error = (*crfp)(seg, argsp);
17497c478bd9Sstevel@tonic-gate 		if (error != 0) {
17507c478bd9Sstevel@tonic-gate 			seg_free(seg);
17517c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
17527c478bd9Sstevel@tonic-gate 			return (error);
17537c478bd9Sstevel@tonic-gate 		}
17543a30c6acSsusans 		/*
17553a30c6acSsusans 		 * Add size now so as_unmap will work if as_ctl fails.
17563a30c6acSsusans 		 */
17573a30c6acSsusans 		as->a_size += rsize;
17587c478bd9Sstevel@tonic-gate 	}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	as_setwatch(as);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	/*
17637c478bd9Sstevel@tonic-gate 	 * If the address space is locked,
17647c478bd9Sstevel@tonic-gate 	 * establish memory locks for the new segment.
17657c478bd9Sstevel@tonic-gate 	 */
17667c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
17677c478bd9Sstevel@tonic-gate 	if (AS_ISPGLCK(as)) {
17687c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
17697c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
17707c478bd9Sstevel@tonic-gate 		error = as_ctl(as, addr, size, MC_LOCK, 0, 0, NULL, 0);
17717c478bd9Sstevel@tonic-gate 		if (error != 0)
17727c478bd9Sstevel@tonic-gate 			(void) as_unmap(as, addr, size);
17737c478bd9Sstevel@tonic-gate 	} else {
17747c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
17757c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 	return (error);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate /*
17827c478bd9Sstevel@tonic-gate  * Delete all segments in the address space marked with S_PURGE.
17837c478bd9Sstevel@tonic-gate  * This is currently used for Sparc V9 nofault ASI segments (seg_nf.c).
17847c478bd9Sstevel@tonic-gate  * These segments are deleted as a first step before calls to as_gap(), so
17857c478bd9Sstevel@tonic-gate  * that they don't affect mmap() or shmat().
17867c478bd9Sstevel@tonic-gate  */
17877c478bd9Sstevel@tonic-gate void
17887c478bd9Sstevel@tonic-gate as_purge(struct as *as)
17897c478bd9Sstevel@tonic-gate {
17907c478bd9Sstevel@tonic-gate 	struct seg *seg;
17917c478bd9Sstevel@tonic-gate 	struct seg *next_seg;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	/*
17947c478bd9Sstevel@tonic-gate 	 * the setting of NEEDSPURGE is protect by as_rangelock(), so
17957c478bd9Sstevel@tonic-gate 	 * no need to grab a_contents mutex for this check
17967c478bd9Sstevel@tonic-gate 	 */
17977c478bd9Sstevel@tonic-gate 	if ((as->a_flags & AS_NEEDSPURGE) == 0)
17987c478bd9Sstevel@tonic-gate 		return;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
18017c478bd9Sstevel@tonic-gate 	next_seg = NULL;
18027c478bd9Sstevel@tonic-gate 	seg = AS_SEGFIRST(as);
18037c478bd9Sstevel@tonic-gate 	while (seg != NULL) {
18047c478bd9Sstevel@tonic-gate 		next_seg = AS_SEGNEXT(as, seg);
18057c478bd9Sstevel@tonic-gate 		if (seg->s_flags & S_PURGE)
18067c478bd9Sstevel@tonic-gate 			SEGOP_UNMAP(seg, seg->s_base, seg->s_size);
18077c478bd9Sstevel@tonic-gate 		seg = next_seg;
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
18127c478bd9Sstevel@tonic-gate 	as->a_flags &= ~AS_NEEDSPURGE;
18137c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate /*
18177c478bd9Sstevel@tonic-gate  * Find a hole of at least size minlen within [base, base + len).
18187c478bd9Sstevel@tonic-gate  *
18197c478bd9Sstevel@tonic-gate  * If flags specifies AH_HI, the hole will have the highest possible address
18207c478bd9Sstevel@tonic-gate  * in the range.  We use the as->a_lastgap field to figure out where to
18217c478bd9Sstevel@tonic-gate  * start looking for a gap.
18227c478bd9Sstevel@tonic-gate  *
18237c478bd9Sstevel@tonic-gate  * Otherwise, the gap will have the lowest possible address.
18247c478bd9Sstevel@tonic-gate  *
18257c478bd9Sstevel@tonic-gate  * If flags specifies AH_CONTAIN, the hole will contain the address addr.
18267c478bd9Sstevel@tonic-gate  *
18277c478bd9Sstevel@tonic-gate  * If an adequate hole is found, base and len are set to reflect the part of
18287c478bd9Sstevel@tonic-gate  * the hole that is within range, and 0 is returned, otherwise,
18297c478bd9Sstevel@tonic-gate  * -1 is returned.
18307c478bd9Sstevel@tonic-gate  *
18317c478bd9Sstevel@tonic-gate  * NOTE: This routine is not correct when base+len overflows caddr_t.
18327c478bd9Sstevel@tonic-gate  */
18337c478bd9Sstevel@tonic-gate int
18347c478bd9Sstevel@tonic-gate as_gap(struct as *as, size_t minlen, caddr_t *basep, size_t *lenp, uint_t flags,
18357c478bd9Sstevel@tonic-gate     caddr_t addr)
18367c478bd9Sstevel@tonic-gate {
18377c478bd9Sstevel@tonic-gate 	caddr_t lobound = *basep;
18387c478bd9Sstevel@tonic-gate 	caddr_t hibound = lobound + *lenp;
18397c478bd9Sstevel@tonic-gate 	struct seg *lseg, *hseg;
18407c478bd9Sstevel@tonic-gate 	caddr_t lo, hi;
18417c478bd9Sstevel@tonic-gate 	int forward;
18427c478bd9Sstevel@tonic-gate 	caddr_t save_base;
18437c478bd9Sstevel@tonic-gate 	size_t save_len;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	save_base = *basep;
18467c478bd9Sstevel@tonic-gate 	save_len = *lenp;
18477c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
18487c478bd9Sstevel@tonic-gate 	if (AS_SEGFIRST(as) == NULL) {
18497c478bd9Sstevel@tonic-gate 		if (valid_va_range(basep, lenp, minlen, flags & AH_DIR)) {
18507c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
18517c478bd9Sstevel@tonic-gate 			return (0);
18527c478bd9Sstevel@tonic-gate 		} else {
18537c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
18547c478bd9Sstevel@tonic-gate 			*basep = save_base;
18557c478bd9Sstevel@tonic-gate 			*lenp = save_len;
18567c478bd9Sstevel@tonic-gate 			return (-1);
18577c478bd9Sstevel@tonic-gate 		}
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * Set up to iterate over all the inter-segment holes in the given
18627c478bd9Sstevel@tonic-gate 	 * direction.  lseg is NULL for the lowest-addressed hole and hseg is
18637c478bd9Sstevel@tonic-gate 	 * NULL for the highest-addressed hole.  If moving backwards, we reset
18647c478bd9Sstevel@tonic-gate 	 * sseg to denote the highest-addressed segment.
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	forward = (flags & AH_DIR) == AH_LO;
18677c478bd9Sstevel@tonic-gate 	if (forward) {
18687c478bd9Sstevel@tonic-gate 		hseg = as_findseg(as, lobound, 1);
18697c478bd9Sstevel@tonic-gate 		lseg = AS_SEGPREV(as, hseg);
18707c478bd9Sstevel@tonic-gate 	} else {
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 		/*
18737c478bd9Sstevel@tonic-gate 		 * If allocating at least as much as the last allocation,
18747c478bd9Sstevel@tonic-gate 		 * use a_lastgap's base as a better estimate of hibound.
18757c478bd9Sstevel@tonic-gate 		 */
18767c478bd9Sstevel@tonic-gate 		if (as->a_lastgap &&
18777c478bd9Sstevel@tonic-gate 		    minlen >= as->a_lastgap->s_size &&
18787c478bd9Sstevel@tonic-gate 		    hibound >= as->a_lastgap->s_base)
18797c478bd9Sstevel@tonic-gate 			hibound = as->a_lastgap->s_base;
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		hseg = as_findseg(as, hibound, 1);
18827c478bd9Sstevel@tonic-gate 		if (hseg->s_base + hseg->s_size < hibound) {
18837c478bd9Sstevel@tonic-gate 			lseg = hseg;
18847c478bd9Sstevel@tonic-gate 			hseg = NULL;
18857c478bd9Sstevel@tonic-gate 		} else {
18867c478bd9Sstevel@tonic-gate 			lseg = AS_SEGPREV(as, hseg);
18877c478bd9Sstevel@tonic-gate 		}
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	for (;;) {
18917c478bd9Sstevel@tonic-gate 		/*
18927c478bd9Sstevel@tonic-gate 		 * Set lo and hi to the hole's boundaries.  (We should really
18937c478bd9Sstevel@tonic-gate 		 * use MAXADDR in place of hibound in the expression below,
18947c478bd9Sstevel@tonic-gate 		 * but can't express it easily; using hibound in its place is
18957c478bd9Sstevel@tonic-gate 		 * harmless.)
18967c478bd9Sstevel@tonic-gate 		 */
18977c478bd9Sstevel@tonic-gate 		lo = (lseg == NULL) ? 0 : lseg->s_base + lseg->s_size;
18987c478bd9Sstevel@tonic-gate 		hi = (hseg == NULL) ? hibound : hseg->s_base;
18997c478bd9Sstevel@tonic-gate 		/*
19007c478bd9Sstevel@tonic-gate 		 * If the iteration has moved past the interval from lobound
19017c478bd9Sstevel@tonic-gate 		 * to hibound it's pointless to continue.
19027c478bd9Sstevel@tonic-gate 		 */
19037c478bd9Sstevel@tonic-gate 		if ((forward && lo > hibound) || (!forward && hi < lobound))
19047c478bd9Sstevel@tonic-gate 			break;
19057c478bd9Sstevel@tonic-gate 		else if (lo > hibound || hi < lobound)
19067c478bd9Sstevel@tonic-gate 			goto cont;
19077c478bd9Sstevel@tonic-gate 		/*
19087c478bd9Sstevel@tonic-gate 		 * Candidate hole lies at least partially within the allowable
19097c478bd9Sstevel@tonic-gate 		 * range.  Restrict it to fall completely within that range,
19107c478bd9Sstevel@tonic-gate 		 * i.e., to [max(lo, lobound), min(hi, hibound)].
19117c478bd9Sstevel@tonic-gate 		 */
19127c478bd9Sstevel@tonic-gate 		if (lo < lobound)
19137c478bd9Sstevel@tonic-gate 			lo = lobound;
19147c478bd9Sstevel@tonic-gate 		if (hi > hibound)
19157c478bd9Sstevel@tonic-gate 			hi = hibound;
19167c478bd9Sstevel@tonic-gate 		/*
19177c478bd9Sstevel@tonic-gate 		 * Verify that the candidate hole is big enough and meets
19187c478bd9Sstevel@tonic-gate 		 * hardware constraints.
19197c478bd9Sstevel@tonic-gate 		 */
19207c478bd9Sstevel@tonic-gate 		*basep = lo;
19217c478bd9Sstevel@tonic-gate 		*lenp = hi - lo;
19227c478bd9Sstevel@tonic-gate 		if (valid_va_range(basep, lenp, minlen,
19237c478bd9Sstevel@tonic-gate 		    forward ? AH_LO : AH_HI) &&
19247c478bd9Sstevel@tonic-gate 		    ((flags & AH_CONTAIN) == 0 ||
19257c478bd9Sstevel@tonic-gate 		    (*basep <= addr && *basep + *lenp > addr))) {
19267c478bd9Sstevel@tonic-gate 			if (!forward)
19277c478bd9Sstevel@tonic-gate 				as->a_lastgap = hseg;
19287c478bd9Sstevel@tonic-gate 			if (hseg != NULL)
19297c478bd9Sstevel@tonic-gate 				as->a_lastgaphl = hseg;
19307c478bd9Sstevel@tonic-gate 			else
19317c478bd9Sstevel@tonic-gate 				as->a_lastgaphl = lseg;
19327c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
19337c478bd9Sstevel@tonic-gate 			return (0);
19347c478bd9Sstevel@tonic-gate 		}
19357c478bd9Sstevel@tonic-gate 	cont:
19367c478bd9Sstevel@tonic-gate 		/*
19377c478bd9Sstevel@tonic-gate 		 * Move to the next hole.
19387c478bd9Sstevel@tonic-gate 		 */
19397c478bd9Sstevel@tonic-gate 		if (forward) {
19407c478bd9Sstevel@tonic-gate 			lseg = hseg;
19417c478bd9Sstevel@tonic-gate 			if (lseg == NULL)
19427c478bd9Sstevel@tonic-gate 				break;
19437c478bd9Sstevel@tonic-gate 			hseg = AS_SEGNEXT(as, hseg);
19447c478bd9Sstevel@tonic-gate 		} else {
19457c478bd9Sstevel@tonic-gate 			hseg = lseg;
19467c478bd9Sstevel@tonic-gate 			if (hseg == NULL)
19477c478bd9Sstevel@tonic-gate 				break;
19487c478bd9Sstevel@tonic-gate 			lseg = AS_SEGPREV(as, lseg);
19497c478bd9Sstevel@tonic-gate 		}
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 	*basep = save_base;
19527c478bd9Sstevel@tonic-gate 	*lenp = save_len;
19537c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
19547c478bd9Sstevel@tonic-gate 	return (-1);
19557c478bd9Sstevel@tonic-gate }
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate /*
19587c478bd9Sstevel@tonic-gate  * Return the next range within [base, base + len) that is backed
19597c478bd9Sstevel@tonic-gate  * with "real memory".  Skip holes and non-seg_vn segments.
19607c478bd9Sstevel@tonic-gate  * We're lazy and only return one segment at a time.
19617c478bd9Sstevel@tonic-gate  */
19627c478bd9Sstevel@tonic-gate int
19637c478bd9Sstevel@tonic-gate as_memory(struct as *as, caddr_t *basep, size_t *lenp)
19647c478bd9Sstevel@tonic-gate {
19657c478bd9Sstevel@tonic-gate 	extern struct seg_ops segspt_shmops;	/* needs a header file */
19667c478bd9Sstevel@tonic-gate 	struct seg *seg;
19677c478bd9Sstevel@tonic-gate 	caddr_t addr, eaddr;
19687c478bd9Sstevel@tonic-gate 	caddr_t segend;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	addr = *basep;
19737c478bd9Sstevel@tonic-gate 	eaddr = addr + *lenp;
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	seg = as_findseg(as, addr, 0);
19767c478bd9Sstevel@tonic-gate 	if (seg != NULL)
19777c478bd9Sstevel@tonic-gate 		addr = MAX(seg->s_base, addr);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	for (;;) {
19807c478bd9Sstevel@tonic-gate 		if (seg == NULL || addr >= eaddr || eaddr <= seg->s_base) {
19817c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
19827c478bd9Sstevel@tonic-gate 			return (EINVAL);
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		if (seg->s_ops == &segvn_ops) {
19867c478bd9Sstevel@tonic-gate 			segend = seg->s_base + seg->s_size;
19877c478bd9Sstevel@tonic-gate 			break;
19887c478bd9Sstevel@tonic-gate 		}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 		/*
19917c478bd9Sstevel@tonic-gate 		 * We do ISM by looking into the private data
19927c478bd9Sstevel@tonic-gate 		 * to determine the real size of the segment.
19937c478bd9Sstevel@tonic-gate 		 */
19947c478bd9Sstevel@tonic-gate 		if (seg->s_ops == &segspt_shmops) {
19957c478bd9Sstevel@tonic-gate 			segend = seg->s_base + spt_realsize(seg);
19967c478bd9Sstevel@tonic-gate 			if (addr < segend)
19977c478bd9Sstevel@tonic-gate 				break;
19987c478bd9Sstevel@tonic-gate 		}
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 		seg = AS_SEGNEXT(as, seg);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 		if (seg != NULL)
20037c478bd9Sstevel@tonic-gate 			addr = seg->s_base;
20047c478bd9Sstevel@tonic-gate 	}
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	*basep = addr;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	if (segend > eaddr)
20097c478bd9Sstevel@tonic-gate 		*lenp = eaddr - addr;
20107c478bd9Sstevel@tonic-gate 	else
20117c478bd9Sstevel@tonic-gate 		*lenp = segend - addr;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
20147c478bd9Sstevel@tonic-gate 	return (0);
20157c478bd9Sstevel@tonic-gate }
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate /*
20187c478bd9Sstevel@tonic-gate  * Swap the pages associated with the address space as out to
20197c478bd9Sstevel@tonic-gate  * secondary storage, returning the number of bytes actually
20207c478bd9Sstevel@tonic-gate  * swapped.
20217c478bd9Sstevel@tonic-gate  *
20227c478bd9Sstevel@tonic-gate  * The value returned is intended to correlate well with the process's
20237c478bd9Sstevel@tonic-gate  * memory requirements.  Its usefulness for this purpose depends on
20247c478bd9Sstevel@tonic-gate  * how well the segment-level routines do at returning accurate
20257c478bd9Sstevel@tonic-gate  * information.
20267c478bd9Sstevel@tonic-gate  */
20277c478bd9Sstevel@tonic-gate size_t
20287c478bd9Sstevel@tonic-gate as_swapout(struct as *as)
20297c478bd9Sstevel@tonic-gate {
20307c478bd9Sstevel@tonic-gate 	struct seg *seg;
20317c478bd9Sstevel@tonic-gate 	size_t swpcnt = 0;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/*
20347c478bd9Sstevel@tonic-gate 	 * Kernel-only processes have given up their address
20357c478bd9Sstevel@tonic-gate 	 * spaces.  Of course, we shouldn't be attempting to
20367c478bd9Sstevel@tonic-gate 	 * swap out such processes in the first place...
20377c478bd9Sstevel@tonic-gate 	 */
20387c478bd9Sstevel@tonic-gate 	if (as == NULL)
20397c478bd9Sstevel@tonic-gate 		return (0);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	/* Prevent XHATs from attaching */
20447c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
20457c478bd9Sstevel@tonic-gate 	AS_SETBUSY(as);
20467c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20507c478bd9Sstevel@tonic-gate 	 * Free all mapping resources associated with the address
20517c478bd9Sstevel@tonic-gate 	 * space.  The segment-level swapout routines capitalize
20527c478bd9Sstevel@tonic-gate 	 * on this unmapping by scavanging pages that have become
20537c478bd9Sstevel@tonic-gate 	 * unmapped here.
20547c478bd9Sstevel@tonic-gate 	 */
20557c478bd9Sstevel@tonic-gate 	hat_swapout(as->a_hat);
20567c478bd9Sstevel@tonic-gate 	if (as->a_xhat != NULL)
20577c478bd9Sstevel@tonic-gate 		xhat_swapout_all(as);
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
20607c478bd9Sstevel@tonic-gate 	AS_CLRBUSY(as);
20617c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	/*
20647c478bd9Sstevel@tonic-gate 	 * Call the swapout routines of all segments in the address
20657c478bd9Sstevel@tonic-gate 	 * space to do the actual work, accumulating the amount of
20667c478bd9Sstevel@tonic-gate 	 * space reclaimed.
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
20697c478bd9Sstevel@tonic-gate 		struct seg_ops *ov = seg->s_ops;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 		/*
20727c478bd9Sstevel@tonic-gate 		 * We have to check to see if the seg has
20737c478bd9Sstevel@tonic-gate 		 * an ops vector because the seg may have
20747c478bd9Sstevel@tonic-gate 		 * been in the middle of being set up when
20757c478bd9Sstevel@tonic-gate 		 * the process was picked for swapout.
20767c478bd9Sstevel@tonic-gate 		 */
20777c478bd9Sstevel@tonic-gate 		if ((ov != NULL) && (ov->swapout != NULL))
20787c478bd9Sstevel@tonic-gate 			swpcnt += SEGOP_SWAPOUT(seg);
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
20817c478bd9Sstevel@tonic-gate 	return (swpcnt);
20827c478bd9Sstevel@tonic-gate }
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate /*
20857c478bd9Sstevel@tonic-gate  * Determine whether data from the mappings in interval [addr, addr + size)
20867c478bd9Sstevel@tonic-gate  * are in the primary memory (core) cache.
20877c478bd9Sstevel@tonic-gate  */
20887c478bd9Sstevel@tonic-gate int
20897c478bd9Sstevel@tonic-gate as_incore(struct as *as, caddr_t addr,
20907c478bd9Sstevel@tonic-gate     size_t size, char *vec, size_t *sizep)
20917c478bd9Sstevel@tonic-gate {
20927c478bd9Sstevel@tonic-gate 	struct seg *seg;
20937c478bd9Sstevel@tonic-gate 	size_t ssize;
20947c478bd9Sstevel@tonic-gate 	caddr_t raddr;		/* rounded down addr */
20957c478bd9Sstevel@tonic-gate 	size_t rsize;		/* rounded up size */
20967c478bd9Sstevel@tonic-gate 	size_t isize;			/* iteration size */
20977c478bd9Sstevel@tonic-gate 	int error = 0;		/* result, assume success */
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	*sizep = 0;
21007c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
21017c478bd9Sstevel@tonic-gate 	rsize = ((((size_t)addr + size) + PAGEOFFSET) & PAGEMASK) -
21027c478bd9Sstevel@tonic-gate 		(size_t)raddr;
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
21057c478bd9Sstevel@tonic-gate 		return (ENOMEM);
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
21087c478bd9Sstevel@tonic-gate 	seg = as_segat(as, raddr);
21097c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
21107c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
21117c478bd9Sstevel@tonic-gate 		return (-1);
21127c478bd9Sstevel@tonic-gate 	}
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
21157c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
21167c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
21177c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
21187c478bd9Sstevel@tonic-gate 				error = -1;
21197c478bd9Sstevel@tonic-gate 				break;
21207c478bd9Sstevel@tonic-gate 			}
21217c478bd9Sstevel@tonic-gate 		}
21227c478bd9Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
21237c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
21247c478bd9Sstevel@tonic-gate 		else
21257c478bd9Sstevel@tonic-gate 			ssize = rsize;
21267c478bd9Sstevel@tonic-gate 		*sizep += isize = SEGOP_INCORE(seg, raddr, ssize, vec);
21277c478bd9Sstevel@tonic-gate 		if (isize != ssize) {
21287c478bd9Sstevel@tonic-gate 			error = -1;
21297c478bd9Sstevel@tonic-gate 			break;
21307c478bd9Sstevel@tonic-gate 		}
21317c478bd9Sstevel@tonic-gate 		vec += btopr(ssize);
21327c478bd9Sstevel@tonic-gate 	}
21337c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
21347c478bd9Sstevel@tonic-gate 	return (error);
21357c478bd9Sstevel@tonic-gate }
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate static void
21387c478bd9Sstevel@tonic-gate as_segunlock(struct seg *seg, caddr_t addr, int attr,
21397c478bd9Sstevel@tonic-gate 	ulong_t *bitmap, size_t position, size_t npages)
21407c478bd9Sstevel@tonic-gate {
21417c478bd9Sstevel@tonic-gate 	caddr_t	range_start;
21427c478bd9Sstevel@tonic-gate 	size_t	pos1 = position;
21437c478bd9Sstevel@tonic-gate 	size_t	pos2;
21447c478bd9Sstevel@tonic-gate 	size_t	size;
21457c478bd9Sstevel@tonic-gate 	size_t  end_pos = npages + position;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	while (bt_range(bitmap, &pos1, &pos2, end_pos)) {
21487c478bd9Sstevel@tonic-gate 		size = ptob((pos2 - pos1));
21497c478bd9Sstevel@tonic-gate 		range_start = (caddr_t)((uintptr_t)addr +
21507c478bd9Sstevel@tonic-gate 			ptob(pos1 - position));
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 		(void) SEGOP_LOCKOP(seg, range_start, size, attr, MC_UNLOCK,
21537c478bd9Sstevel@tonic-gate 			(ulong_t *)NULL, (size_t)NULL);
21547c478bd9Sstevel@tonic-gate 		pos1 = pos2;
21557c478bd9Sstevel@tonic-gate 	}
21567c478bd9Sstevel@tonic-gate }
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate static void
21597c478bd9Sstevel@tonic-gate as_unlockerr(struct as *as, int attr, ulong_t *mlock_map,
21607c478bd9Sstevel@tonic-gate 	caddr_t raddr, size_t rsize)
21617c478bd9Sstevel@tonic-gate {
21627c478bd9Sstevel@tonic-gate 	struct seg *seg = as_segat(as, raddr);
21637c478bd9Sstevel@tonic-gate 	size_t ssize;
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	while (rsize != 0) {
21667c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size)
21677c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
21707c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
21717c478bd9Sstevel@tonic-gate 		else
21727c478bd9Sstevel@tonic-gate 			ssize = rsize;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 		as_segunlock(seg, raddr, attr, mlock_map, 0, btopr(ssize));
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 		rsize -= ssize;
21777c478bd9Sstevel@tonic-gate 		raddr += ssize;
21787c478bd9Sstevel@tonic-gate 	}
21797c478bd9Sstevel@tonic-gate }
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate /*
21827c478bd9Sstevel@tonic-gate  * Cache control operations over the interval [addr, addr + size) in
21837c478bd9Sstevel@tonic-gate  * address space "as".
21847c478bd9Sstevel@tonic-gate  */
21857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21867c478bd9Sstevel@tonic-gate int
21877c478bd9Sstevel@tonic-gate as_ctl(struct as *as, caddr_t addr, size_t size, int func, int attr,
21887c478bd9Sstevel@tonic-gate     uintptr_t arg, ulong_t *lock_map, size_t pos)
21897c478bd9Sstevel@tonic-gate {
21907c478bd9Sstevel@tonic-gate 	struct seg *seg;	/* working segment */
21917c478bd9Sstevel@tonic-gate 	caddr_t raddr;		/* rounded down addr */
21927c478bd9Sstevel@tonic-gate 	caddr_t initraddr;	/* saved initial rounded down addr */
21937c478bd9Sstevel@tonic-gate 	size_t rsize;		/* rounded up size */
21947c478bd9Sstevel@tonic-gate 	size_t initrsize;	/* saved initial rounded up size */
21957c478bd9Sstevel@tonic-gate 	size_t ssize;		/* size of seg */
21967c478bd9Sstevel@tonic-gate 	int error = 0;			/* result */
21977c478bd9Sstevel@tonic-gate 	size_t mlock_size;	/* size of bitmap */
21987c478bd9Sstevel@tonic-gate 	ulong_t *mlock_map;	/* pointer to bitmap used */
21997c478bd9Sstevel@tonic-gate 				/* to represent the locked */
22007c478bd9Sstevel@tonic-gate 				/* pages. */
22017c478bd9Sstevel@tonic-gate retry:
22027c478bd9Sstevel@tonic-gate 	if (error == IE_RETRY)
22037c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
22047c478bd9Sstevel@tonic-gate 	else
22057c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	/*
22087c478bd9Sstevel@tonic-gate 	 * If these are address space lock/unlock operations, loop over
22097c478bd9Sstevel@tonic-gate 	 * all segments in the address space, as appropriate.
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	if (func == MC_LOCKAS) {
22127c478bd9Sstevel@tonic-gate 		size_t npages, idx;
22137c478bd9Sstevel@tonic-gate 		size_t rlen = 0;	/* rounded as length */
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 		idx = pos;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		if (arg & MCL_FUTURE) {
22187c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
22197c478bd9Sstevel@tonic-gate 			AS_SETPGLCK(as);
22207c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
22217c478bd9Sstevel@tonic-gate 		}
22227c478bd9Sstevel@tonic-gate 		if ((arg & MCL_CURRENT) == 0) {
22237c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
22247c478bd9Sstevel@tonic-gate 			return (0);
22257c478bd9Sstevel@tonic-gate 		}
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 		seg = AS_SEGFIRST(as);
22287c478bd9Sstevel@tonic-gate 		if (seg == NULL) {
22297c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
22307c478bd9Sstevel@tonic-gate 			return (0);
22317c478bd9Sstevel@tonic-gate 		}
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 		do {
22347c478bd9Sstevel@tonic-gate 			raddr = (caddr_t)((uintptr_t)seg->s_base &
22357c478bd9Sstevel@tonic-gate 			    (uintptr_t)PAGEMASK);
22367c478bd9Sstevel@tonic-gate 			rlen += (((uintptr_t)(seg->s_base + seg->s_size) +
22377c478bd9Sstevel@tonic-gate 				PAGEOFFSET) & PAGEMASK) - (uintptr_t)raddr;
22387c478bd9Sstevel@tonic-gate 		} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 		mlock_size = BT_BITOUL(btopr(rlen));
22417c478bd9Sstevel@tonic-gate 		if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size *
22427c478bd9Sstevel@tonic-gate 			sizeof (ulong_t), KM_NOSLEEP)) == NULL) {
22437c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
22447c478bd9Sstevel@tonic-gate 				return (EAGAIN);
22457c478bd9Sstevel@tonic-gate 		}
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 		for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
22487c478bd9Sstevel@tonic-gate 			error = SEGOP_LOCKOP(seg, seg->s_base,
22497c478bd9Sstevel@tonic-gate 			    seg->s_size, attr, MC_LOCK, mlock_map, pos);
22507c478bd9Sstevel@tonic-gate 			if (error != 0)
22517c478bd9Sstevel@tonic-gate 				break;
22527c478bd9Sstevel@tonic-gate 			pos += seg_pages(seg);
22537c478bd9Sstevel@tonic-gate 		}
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 		if (error) {
22567c478bd9Sstevel@tonic-gate 			for (seg = AS_SEGFIRST(as); seg != NULL;
22577c478bd9Sstevel@tonic-gate 				seg = AS_SEGNEXT(as, seg)) {
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 				raddr = (caddr_t)((uintptr_t)seg->s_base &
22607c478bd9Sstevel@tonic-gate 					(uintptr_t)PAGEMASK);
22617c478bd9Sstevel@tonic-gate 				npages = seg_pages(seg);
22627c478bd9Sstevel@tonic-gate 				as_segunlock(seg, raddr, attr, mlock_map,
22637c478bd9Sstevel@tonic-gate 					idx, npages);
22647c478bd9Sstevel@tonic-gate 				idx += npages;
22657c478bd9Sstevel@tonic-gate 			}
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 		kmem_free(mlock_map, mlock_size * sizeof (ulong_t));
22697c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
22707c478bd9Sstevel@tonic-gate 		goto lockerr;
22717c478bd9Sstevel@tonic-gate 	} else if (func == MC_UNLOCKAS) {
22727c478bd9Sstevel@tonic-gate 		mutex_enter(&as->a_contents);
22737c478bd9Sstevel@tonic-gate 		AS_CLRPGLCK(as);
22747c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 		for (seg = AS_SEGFIRST(as); seg; seg = AS_SEGNEXT(as, seg)) {
22777c478bd9Sstevel@tonic-gate 			error = SEGOP_LOCKOP(seg, seg->s_base,
22787c478bd9Sstevel@tonic-gate 			    seg->s_size, attr, MC_UNLOCK, NULL, 0);
22797c478bd9Sstevel@tonic-gate 			if (error != 0)
22807c478bd9Sstevel@tonic-gate 				break;
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
22847c478bd9Sstevel@tonic-gate 		goto lockerr;
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	/*
22887c478bd9Sstevel@tonic-gate 	 * Normalize addresses and sizes.
22897c478bd9Sstevel@tonic-gate 	 */
22907c478bd9Sstevel@tonic-gate 	initraddr = raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
22917c478bd9Sstevel@tonic-gate 	initrsize = rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
22927c478bd9Sstevel@tonic-gate 		(size_t)raddr;
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	if (raddr + rsize < raddr) {		/* check for wraparound */
22957c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
22967c478bd9Sstevel@tonic-gate 		return (ENOMEM);
22977c478bd9Sstevel@tonic-gate 	}
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	/*
23007c478bd9Sstevel@tonic-gate 	 * Get initial segment.
23017c478bd9Sstevel@tonic-gate 	 */
23027c478bd9Sstevel@tonic-gate 	if ((seg = as_segat(as, raddr)) == NULL) {
23037c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
23047c478bd9Sstevel@tonic-gate 		return (ENOMEM);
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	if (func == MC_LOCK) {
23087c478bd9Sstevel@tonic-gate 		mlock_size = BT_BITOUL(btopr(rsize));
23097c478bd9Sstevel@tonic-gate 		if ((mlock_map = (ulong_t *)kmem_zalloc(mlock_size *
23107c478bd9Sstevel@tonic-gate 			sizeof (ulong_t), KM_NOSLEEP)) == NULL) {
23117c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
23127c478bd9Sstevel@tonic-gate 				return (EAGAIN);
23137c478bd9Sstevel@tonic-gate 		}
23147c478bd9Sstevel@tonic-gate 	}
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	/*
23177c478bd9Sstevel@tonic-gate 	 * Loop over all segments.  If a hole in the address range is
23187c478bd9Sstevel@tonic-gate 	 * discovered, then fail.  For each segment, perform the appropriate
23197c478bd9Sstevel@tonic-gate 	 * control operation.
23207c478bd9Sstevel@tonic-gate 	 */
23217c478bd9Sstevel@tonic-gate 	while (rsize != 0) {
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 		/*
23247c478bd9Sstevel@tonic-gate 		 * Make sure there's no hole, calculate the portion
23257c478bd9Sstevel@tonic-gate 		 * of the next segment to be operated over.
23267c478bd9Sstevel@tonic-gate 		 */
23277c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
23287c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
23297c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
23307c478bd9Sstevel@tonic-gate 				if (func == MC_LOCK) {
23317c478bd9Sstevel@tonic-gate 					as_unlockerr(as, attr, mlock_map,
23327c478bd9Sstevel@tonic-gate 						initraddr, initrsize - rsize);
23337c478bd9Sstevel@tonic-gate 					kmem_free(mlock_map,
23347c478bd9Sstevel@tonic-gate 						mlock_size * sizeof (ulong_t));
23357c478bd9Sstevel@tonic-gate 				}
23367c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
23377c478bd9Sstevel@tonic-gate 				return (ENOMEM);
23387c478bd9Sstevel@tonic-gate 			}
23397c478bd9Sstevel@tonic-gate 		}
23407c478bd9Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size))
23417c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
23427c478bd9Sstevel@tonic-gate 		else
23437c478bd9Sstevel@tonic-gate 			ssize = rsize;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 		/*
23467c478bd9Sstevel@tonic-gate 		 * Dispatch on specific function.
23477c478bd9Sstevel@tonic-gate 		 */
23487c478bd9Sstevel@tonic-gate 		switch (func) {
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 		/*
23517c478bd9Sstevel@tonic-gate 		 * Synchronize cached data from mappings with backing
23527c478bd9Sstevel@tonic-gate 		 * objects.
23537c478bd9Sstevel@tonic-gate 		 */
23547c478bd9Sstevel@tonic-gate 		case MC_SYNC:
23557c478bd9Sstevel@tonic-gate 			if (error = SEGOP_SYNC(seg, raddr, ssize,
23567c478bd9Sstevel@tonic-gate 			    attr, (uint_t)arg)) {
23577c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
23587c478bd9Sstevel@tonic-gate 				return (error);
23597c478bd9Sstevel@tonic-gate 			}
23607c478bd9Sstevel@tonic-gate 			break;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 		/*
23637c478bd9Sstevel@tonic-gate 		 * Lock pages in memory.
23647c478bd9Sstevel@tonic-gate 		 */
23657c478bd9Sstevel@tonic-gate 		case MC_LOCK:
23667c478bd9Sstevel@tonic-gate 			if (error = SEGOP_LOCKOP(seg, raddr, ssize,
23677c478bd9Sstevel@tonic-gate 				attr, func, mlock_map, pos)) {
23687c478bd9Sstevel@tonic-gate 				as_unlockerr(as, attr, mlock_map, initraddr,
23697c478bd9Sstevel@tonic-gate 					initrsize - rsize + ssize);
23707c478bd9Sstevel@tonic-gate 				kmem_free(mlock_map, mlock_size *
23717c478bd9Sstevel@tonic-gate 					sizeof (ulong_t));
23727c478bd9Sstevel@tonic-gate 				AS_LOCK_EXIT(as, &as->a_lock);
23737c478bd9Sstevel@tonic-gate 				goto lockerr;
23747c478bd9Sstevel@tonic-gate 			}
23757c478bd9Sstevel@tonic-gate 			break;
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 		/*
23787c478bd9Sstevel@tonic-gate 		 * Unlock mapped pages.
23797c478bd9Sstevel@tonic-gate 		 */
23807c478bd9Sstevel@tonic-gate 		case MC_UNLOCK:
23817c478bd9Sstevel@tonic-gate 			(void) SEGOP_LOCKOP(seg, raddr, ssize, attr, func,
23827c478bd9Sstevel@tonic-gate 				(ulong_t *)NULL, (size_t)NULL);
23837c478bd9Sstevel@tonic-gate 			break;
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 		/*
23867c478bd9Sstevel@tonic-gate 		 * Store VM advise for mapped pages in segment layer.
23877c478bd9Sstevel@tonic-gate 		 */
23887c478bd9Sstevel@tonic-gate 		case MC_ADVISE:
23897c478bd9Sstevel@tonic-gate 			error = SEGOP_ADVISE(seg, raddr, ssize, (uint_t)arg);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 			/*
23927c478bd9Sstevel@tonic-gate 			 * Check for regular errors and special retry error
23937c478bd9Sstevel@tonic-gate 			 */
23947c478bd9Sstevel@tonic-gate 			if (error) {
23957c478bd9Sstevel@tonic-gate 				if (error == IE_RETRY) {
23967c478bd9Sstevel@tonic-gate 					/*
23977c478bd9Sstevel@tonic-gate 					 * Need to acquire writers lock, so
23987c478bd9Sstevel@tonic-gate 					 * have to drop readers lock and start
23997c478bd9Sstevel@tonic-gate 					 * all over again
24007c478bd9Sstevel@tonic-gate 					 */
24017c478bd9Sstevel@tonic-gate 					AS_LOCK_EXIT(as, &as->a_lock);
24027c478bd9Sstevel@tonic-gate 					goto retry;
24037c478bd9Sstevel@tonic-gate 				} else if (error == IE_REATTACH) {
24047c478bd9Sstevel@tonic-gate 					/*
24057c478bd9Sstevel@tonic-gate 					 * Find segment for current address
24067c478bd9Sstevel@tonic-gate 					 * because current segment just got
24077c478bd9Sstevel@tonic-gate 					 * split or concatenated
24087c478bd9Sstevel@tonic-gate 					 */
24097c478bd9Sstevel@tonic-gate 					seg = as_segat(as, raddr);
24107c478bd9Sstevel@tonic-gate 					if (seg == NULL) {
24117c478bd9Sstevel@tonic-gate 						AS_LOCK_EXIT(as, &as->a_lock);
24127c478bd9Sstevel@tonic-gate 						return (ENOMEM);
24137c478bd9Sstevel@tonic-gate 					}
24147c478bd9Sstevel@tonic-gate 				} else {
24157c478bd9Sstevel@tonic-gate 					/*
24167c478bd9Sstevel@tonic-gate 					 * Regular error
24177c478bd9Sstevel@tonic-gate 					 */
24187c478bd9Sstevel@tonic-gate 					AS_LOCK_EXIT(as, &as->a_lock);
24197c478bd9Sstevel@tonic-gate 					return (error);
24207c478bd9Sstevel@tonic-gate 				}
24217c478bd9Sstevel@tonic-gate 			}
24227c478bd9Sstevel@tonic-gate 			break;
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 		/*
24257c478bd9Sstevel@tonic-gate 		 * Can't happen.
24267c478bd9Sstevel@tonic-gate 		 */
24277c478bd9Sstevel@tonic-gate 		default:
24287c478bd9Sstevel@tonic-gate 			panic("as_ctl: bad operation %d", func);
24297c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
24307c478bd9Sstevel@tonic-gate 		}
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 		rsize -= ssize;
24337c478bd9Sstevel@tonic-gate 		raddr += ssize;
24347c478bd9Sstevel@tonic-gate 	}
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	if (func == MC_LOCK)
24377c478bd9Sstevel@tonic-gate 		kmem_free(mlock_map, mlock_size * sizeof (ulong_t));
24387c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
24397c478bd9Sstevel@tonic-gate 	return (0);
24407c478bd9Sstevel@tonic-gate lockerr:
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	/*
24437c478bd9Sstevel@tonic-gate 	 * If the lower levels returned EDEADLK for a segment lockop,
24447c478bd9Sstevel@tonic-gate 	 * it means that we should retry the operation.  Let's wait
24457c478bd9Sstevel@tonic-gate 	 * a bit also to let the deadlock causing condition clear.
24467c478bd9Sstevel@tonic-gate 	 * This is part of a gross hack to work around a design flaw
24477c478bd9Sstevel@tonic-gate 	 * in the ufs/sds logging code and should go away when the
24487c478bd9Sstevel@tonic-gate 	 * logging code is re-designed to fix the problem. See bug
24497c478bd9Sstevel@tonic-gate 	 * 4125102 for details of the problem.
24507c478bd9Sstevel@tonic-gate 	 */
24517c478bd9Sstevel@tonic-gate 	if (error == EDEADLK) {
24527c478bd9Sstevel@tonic-gate 		delay(deadlk_wait);
24537c478bd9Sstevel@tonic-gate 		error = 0;
24547c478bd9Sstevel@tonic-gate 		goto retry;
24557c478bd9Sstevel@tonic-gate 	}
24567c478bd9Sstevel@tonic-gate 	return (error);
24577c478bd9Sstevel@tonic-gate }
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate /*
24607c478bd9Sstevel@tonic-gate  * Special code for exec to move the stack segment from its interim
24617c478bd9Sstevel@tonic-gate  * place in the old address to the right place in the new address space.
24627c478bd9Sstevel@tonic-gate  */
24637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24647c478bd9Sstevel@tonic-gate int
24657c478bd9Sstevel@tonic-gate as_exec(struct as *oas, caddr_t ostka, size_t stksz,
24667c478bd9Sstevel@tonic-gate     struct as *nas, caddr_t nstka, uint_t hatflag)
24677c478bd9Sstevel@tonic-gate {
24687c478bd9Sstevel@tonic-gate 	struct seg *stkseg;
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(oas, &oas->a_lock, RW_WRITER);
24717c478bd9Sstevel@tonic-gate 	stkseg = as_segat(oas, ostka);
24727c478bd9Sstevel@tonic-gate 	stkseg = as_removeseg(oas, stkseg);
24737c478bd9Sstevel@tonic-gate 	ASSERT(stkseg != NULL);
24747c478bd9Sstevel@tonic-gate 	ASSERT(stkseg->s_base == ostka && stkseg->s_size == stksz);
24757c478bd9Sstevel@tonic-gate 	stkseg->s_as = nas;
24767c478bd9Sstevel@tonic-gate 	stkseg->s_base = nstka;
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	/*
24797c478bd9Sstevel@tonic-gate 	 * It's ok to lock the address space we are about to exec to.
24807c478bd9Sstevel@tonic-gate 	 */
24817c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(nas, &nas->a_lock, RW_WRITER);
24827c478bd9Sstevel@tonic-gate 	ASSERT(avl_numnodes(&nas->a_wpage) == 0);
24837c478bd9Sstevel@tonic-gate 	nas->a_size += stkseg->s_size;
24847c478bd9Sstevel@tonic-gate 	oas->a_size -= stkseg->s_size;
24857c478bd9Sstevel@tonic-gate 	(void) as_addseg(nas, stkseg);
24867c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(nas, &nas->a_lock);
24877c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(oas, &oas->a_lock);
24887c478bd9Sstevel@tonic-gate 	return (0);
24897c478bd9Sstevel@tonic-gate }
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate static int
24927c478bd9Sstevel@tonic-gate f_decode(faultcode_t fault_err)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate 	int error = 0;
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	switch (FC_CODE(fault_err)) {
24977c478bd9Sstevel@tonic-gate 	case FC_OBJERR:
24987c478bd9Sstevel@tonic-gate 		error = FC_ERRNO(fault_err);
24997c478bd9Sstevel@tonic-gate 		break;
25007c478bd9Sstevel@tonic-gate 	case FC_PROT:
25017c478bd9Sstevel@tonic-gate 		error = EACCES;
25027c478bd9Sstevel@tonic-gate 		break;
25037c478bd9Sstevel@tonic-gate 	default:
25047c478bd9Sstevel@tonic-gate 		error = EFAULT;
25057c478bd9Sstevel@tonic-gate 		break;
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 	return (error);
25087c478bd9Sstevel@tonic-gate }
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate /*
25117c478bd9Sstevel@tonic-gate  * lock pages in a given address space. Return shadow list. If
25127c478bd9Sstevel@tonic-gate  * the list is NULL, the MMU mapping is also locked.
25137c478bd9Sstevel@tonic-gate  */
25147c478bd9Sstevel@tonic-gate int
25157c478bd9Sstevel@tonic-gate as_pagelock(struct as *as, struct page ***ppp, caddr_t addr,
25167c478bd9Sstevel@tonic-gate     size_t size, enum seg_rw rw)
25177c478bd9Sstevel@tonic-gate {
25187c478bd9Sstevel@tonic-gate 	size_t rsize;
25197c478bd9Sstevel@tonic-gate 	caddr_t base;
25207c478bd9Sstevel@tonic-gate 	caddr_t raddr;
25217c478bd9Sstevel@tonic-gate 	faultcode_t fault_err;
25227c478bd9Sstevel@tonic-gate 	struct seg *seg;
25237c478bd9Sstevel@tonic-gate 	int res;
25247c478bd9Sstevel@tonic-gate 	int prefaulted = 0;
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_START,
25277c478bd9Sstevel@tonic-gate 	    "as_pagelock_start: addr %p size %ld", addr, size);
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
25307c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
25317c478bd9Sstevel@tonic-gate 		(size_t)raddr;
25327c478bd9Sstevel@tonic-gate top:
25337c478bd9Sstevel@tonic-gate 	/*
25347c478bd9Sstevel@tonic-gate 	 * if the request crosses two segments let
25357c478bd9Sstevel@tonic-gate 	 * as_fault handle it.
25367c478bd9Sstevel@tonic-gate 	 */
25377c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
25387c478bd9Sstevel@tonic-gate 	seg = as_findseg(as, addr, 0);
25397c478bd9Sstevel@tonic-gate 	if ((seg == NULL) || ((base = seg->s_base) > addr) ||
25407c478bd9Sstevel@tonic-gate 	    (addr + size) > base + seg->s_size) {
25417c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
25427c478bd9Sstevel@tonic-gate 		goto slow;
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_START,
25467c478bd9Sstevel@tonic-gate 	    "seg_lock_1_start: raddr %p rsize %ld", raddr, rsize);
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 	/*
25497c478bd9Sstevel@tonic-gate 	 * try to lock pages and pass back shadow list
25507c478bd9Sstevel@tonic-gate 	 */
25517c478bd9Sstevel@tonic-gate 	res = SEGOP_PAGELOCK(seg, raddr, rsize, ppp, L_PAGELOCK, rw);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_SEG_LOCK_END, "seg_lock_1_end");
25547c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
25557c478bd9Sstevel@tonic-gate 	if (res == 0) {
25567c478bd9Sstevel@tonic-gate 		return (0);
25577c478bd9Sstevel@tonic-gate 	} else if (res == ENOTSUP || prefaulted) {
25587c478bd9Sstevel@tonic-gate 		/*
25597c478bd9Sstevel@tonic-gate 		 * (1) segment driver doesn't support PAGELOCK fastpath, or
25607c478bd9Sstevel@tonic-gate 		 * (2) we've already tried fast path unsuccessfully after
25617c478bd9Sstevel@tonic-gate 		 *    faulting in the addr range below; system might be
25627c478bd9Sstevel@tonic-gate 		 *    thrashing or there may not be enough availrmem.
25637c478bd9Sstevel@tonic-gate 		 */
25647c478bd9Sstevel@tonic-gate 		goto slow;
25657c478bd9Sstevel@tonic-gate 	}
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_FAULT_START,
25687c478bd9Sstevel@tonic-gate 	    "as_fault_start: addr %p size %ld", addr, size);
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	/*
25717c478bd9Sstevel@tonic-gate 	 * we might get here because of some COW fault or non
25727c478bd9Sstevel@tonic-gate 	 * existing page. Let as_fault deal with it. Just load
25737c478bd9Sstevel@tonic-gate 	 * the page, don't lock the MMU mapping.
25747c478bd9Sstevel@tonic-gate 	 */
25757c478bd9Sstevel@tonic-gate 	fault_err = as_fault(as->a_hat, as, addr, size, F_INVAL, rw);
25767c478bd9Sstevel@tonic-gate 	if (fault_err != 0) {
25777c478bd9Sstevel@tonic-gate 		return (f_decode(fault_err));
25787c478bd9Sstevel@tonic-gate 	}
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	prefaulted = 1;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	/*
25837c478bd9Sstevel@tonic-gate 	 * try fast path again; since we've dropped a_lock,
25847c478bd9Sstevel@tonic-gate 	 * we need to try the dance from the start to see if
25857c478bd9Sstevel@tonic-gate 	 * the addr range is still valid.
25867c478bd9Sstevel@tonic-gate 	 */
25877c478bd9Sstevel@tonic-gate 	goto top;
25887c478bd9Sstevel@tonic-gate slow:
25897c478bd9Sstevel@tonic-gate 	/*
25907c478bd9Sstevel@tonic-gate 	 * load the page and lock the MMU mapping.
25917c478bd9Sstevel@tonic-gate 	 */
25927c478bd9Sstevel@tonic-gate 	fault_err = as_fault(as->a_hat, as, addr, size, F_SOFTLOCK, rw);
25937c478bd9Sstevel@tonic-gate 	if (fault_err != 0) {
25947c478bd9Sstevel@tonic-gate 		return (f_decode(fault_err));
25957c478bd9Sstevel@tonic-gate 	}
25967c478bd9Sstevel@tonic-gate 	*ppp = NULL;
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_LOCK_END, "as_pagelock_end");
25997c478bd9Sstevel@tonic-gate 	return (0);
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate /*
26037c478bd9Sstevel@tonic-gate  * unlock pages in a given address range
26047c478bd9Sstevel@tonic-gate  */
26057c478bd9Sstevel@tonic-gate void
26067c478bd9Sstevel@tonic-gate as_pageunlock(struct as *as, struct page **pp, caddr_t addr, size_t size,
26077c478bd9Sstevel@tonic-gate     enum seg_rw rw)
26087c478bd9Sstevel@tonic-gate {
26097c478bd9Sstevel@tonic-gate 	struct seg *seg;
26107c478bd9Sstevel@tonic-gate 	size_t rsize;
26117c478bd9Sstevel@tonic-gate 	caddr_t raddr;
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_START,
26147c478bd9Sstevel@tonic-gate 	    "as_pageunlock_start: addr %p size %ld", addr, size);
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	/*
26177c478bd9Sstevel@tonic-gate 	 * if the shadow list is NULL, as_pagelock was
26187c478bd9Sstevel@tonic-gate 	 * falling back to as_fault
26197c478bd9Sstevel@tonic-gate 	 */
26207c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
26217c478bd9Sstevel@tonic-gate 		(void) as_fault(as->a_hat, as, addr, size, F_SOFTUNLOCK, rw);
26227c478bd9Sstevel@tonic-gate 		return;
26237c478bd9Sstevel@tonic-gate 	}
26247c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
26257c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
26267c478bd9Sstevel@tonic-gate 		(size_t)raddr;
26277c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
26287c478bd9Sstevel@tonic-gate 	seg = as_findseg(as, addr, 0);
26297c478bd9Sstevel@tonic-gate 	ASSERT(seg);
26307c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_PHYSIO, TR_PHYSIO_SEG_UNLOCK_START,
26317c478bd9Sstevel@tonic-gate 	    "seg_unlock_start: raddr %p rsize %ld", raddr, rsize);
26327c478bd9Sstevel@tonic-gate 	SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGEUNLOCK, rw);
26337c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
26347c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_PHYSIO, TR_PHYSIO_AS_UNLOCK_END, "as_pageunlock_end");
26357c478bd9Sstevel@tonic-gate }
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate /*
26387c478bd9Sstevel@tonic-gate  * reclaim cached pages in a given address range
26397c478bd9Sstevel@tonic-gate  */
26407c478bd9Sstevel@tonic-gate void
26417c478bd9Sstevel@tonic-gate as_pagereclaim(struct as *as, struct page **pp, caddr_t addr,
26427c478bd9Sstevel@tonic-gate     size_t size, enum seg_rw rw)
26437c478bd9Sstevel@tonic-gate {
26447c478bd9Sstevel@tonic-gate 	struct seg *seg;
26457c478bd9Sstevel@tonic-gate 	size_t rsize;
26467c478bd9Sstevel@tonic-gate 	caddr_t raddr;
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	ASSERT(AS_READ_HELD(as, &as->a_lock));
26497c478bd9Sstevel@tonic-gate 	ASSERT(pp != NULL);
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
26527c478bd9Sstevel@tonic-gate 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
26537c478bd9Sstevel@tonic-gate 		(size_t)raddr;
26547c478bd9Sstevel@tonic-gate 	seg = as_findseg(as, addr, 0);
26557c478bd9Sstevel@tonic-gate 	ASSERT(seg);
26567c478bd9Sstevel@tonic-gate 	SEGOP_PAGELOCK(seg, raddr, rsize, &pp, L_PAGERECLAIM, rw);
26577c478bd9Sstevel@tonic-gate }
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate #define	MAXPAGEFLIP	4
26607c478bd9Sstevel@tonic-gate #define	MAXPAGEFLIPSIZ	MAXPAGEFLIP*PAGESIZE
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate int
26637c478bd9Sstevel@tonic-gate as_setpagesize(struct as *as, caddr_t addr, size_t size, uint_t szc,
26647c478bd9Sstevel@tonic-gate     boolean_t wait)
26657c478bd9Sstevel@tonic-gate {
26667c478bd9Sstevel@tonic-gate 	struct seg *seg;
26677c478bd9Sstevel@tonic-gate 	size_t ssize;
26687c478bd9Sstevel@tonic-gate 	caddr_t raddr;			/* rounded down addr */
26697c478bd9Sstevel@tonic-gate 	size_t rsize;			/* rounded up size */
26707c478bd9Sstevel@tonic-gate 	int error = 0;
26717c478bd9Sstevel@tonic-gate 	size_t pgsz = page_get_pagesize(szc);
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate setpgsz_top:
26747c478bd9Sstevel@tonic-gate 	if (!IS_P2ALIGNED(addr, pgsz) || !IS_P2ALIGNED(size, pgsz)) {
26757c478bd9Sstevel@tonic-gate 		return (EINVAL);
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	raddr = addr;
26797c478bd9Sstevel@tonic-gate 	rsize = size;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	if (raddr + rsize < raddr)		/* check for wraparound */
26827c478bd9Sstevel@tonic-gate 		return (ENOMEM);
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
26857c478bd9Sstevel@tonic-gate 	as_clearwatchprot(as, raddr, rsize);
26867c478bd9Sstevel@tonic-gate 	seg = as_segat(as, raddr);
26877c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
26887c478bd9Sstevel@tonic-gate 		as_setwatch(as);
26897c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
26907c478bd9Sstevel@tonic-gate 		return (ENOMEM);
26917c478bd9Sstevel@tonic-gate 	}
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
26947c478bd9Sstevel@tonic-gate 		if (raddr >= seg->s_base + seg->s_size) {
26957c478bd9Sstevel@tonic-gate 			seg = AS_SEGNEXT(as, seg);
26967c478bd9Sstevel@tonic-gate 			if (seg == NULL || raddr != seg->s_base) {
26977c478bd9Sstevel@tonic-gate 				error = ENOMEM;
26987c478bd9Sstevel@tonic-gate 				break;
26997c478bd9Sstevel@tonic-gate 			}
27007c478bd9Sstevel@tonic-gate 		}
27017c478bd9Sstevel@tonic-gate 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
27027c478bd9Sstevel@tonic-gate 			ssize = seg->s_base + seg->s_size - raddr;
27037c478bd9Sstevel@tonic-gate 		} else {
27047c478bd9Sstevel@tonic-gate 			ssize = rsize;
27057c478bd9Sstevel@tonic-gate 		}
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 		error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc);
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 		if (error == IE_NOMEM) {
27107c478bd9Sstevel@tonic-gate 			error = EAGAIN;
27117c478bd9Sstevel@tonic-gate 			break;
27127c478bd9Sstevel@tonic-gate 		}
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 		if (error == IE_RETRY) {
27157c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
27167c478bd9Sstevel@tonic-gate 			goto setpgsz_top;
27177c478bd9Sstevel@tonic-gate 		}
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 		if (error == ENOTSUP) {
27207c478bd9Sstevel@tonic-gate 			error = EINVAL;
27217c478bd9Sstevel@tonic-gate 			break;
27227c478bd9Sstevel@tonic-gate 		}
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 		if (wait && (error == EAGAIN)) {
27257c478bd9Sstevel@tonic-gate 			/*
27267c478bd9Sstevel@tonic-gate 			 * Memory is currently locked.  It must be unlocked
27277c478bd9Sstevel@tonic-gate 			 * before this operation can succeed through a retry.
27287c478bd9Sstevel@tonic-gate 			 * The possible reasons for locked memory and
27297c478bd9Sstevel@tonic-gate 			 * corresponding strategies for unlocking are:
27307c478bd9Sstevel@tonic-gate 			 * (1) Normal I/O
27317c478bd9Sstevel@tonic-gate 			 *	wait for a signal that the I/O operation
27327c478bd9Sstevel@tonic-gate 			 *	has completed and the memory is unlocked.
27337c478bd9Sstevel@tonic-gate 			 * (2) Asynchronous I/O
27347c478bd9Sstevel@tonic-gate 			 *	The aio subsystem does not unlock pages when
27357c478bd9Sstevel@tonic-gate 			 *	the I/O is completed. Those pages are unlocked
27367c478bd9Sstevel@tonic-gate 			 *	when the application calls aiowait/aioerror.
27377c478bd9Sstevel@tonic-gate 			 *	So, to prevent blocking forever, cv_broadcast()
27387c478bd9Sstevel@tonic-gate 			 *	is done to wake up aio_cleanup_thread.
27397c478bd9Sstevel@tonic-gate 			 *	Subsequently, segvn_reclaim will be called, and
27407c478bd9Sstevel@tonic-gate 			 *	that will do AS_CLRUNMAPWAIT() and wake us up.
27417c478bd9Sstevel@tonic-gate 			 * (3) Long term page locking:
27427c478bd9Sstevel@tonic-gate 			 *	This is not relevant for as_setpagesize()
27437c478bd9Sstevel@tonic-gate 			 *	because we cannot change the page size for
27447c478bd9Sstevel@tonic-gate 			 *	driver memory. The attempt to do so will
27457c478bd9Sstevel@tonic-gate 			 *	fail with a different error than EAGAIN so
27467c478bd9Sstevel@tonic-gate 			 *	there's no need to trigger as callbacks like
27477c478bd9Sstevel@tonic-gate 			 *	as_unmap, as_setprot or as_free would do.
27487c478bd9Sstevel@tonic-gate 			 */
27497c478bd9Sstevel@tonic-gate 			mutex_enter(&as->a_contents);
27507c478bd9Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(as) == 0) {
27517c478bd9Sstevel@tonic-gate 				cv_broadcast(&as->a_cv);
27527c478bd9Sstevel@tonic-gate 			}
27537c478bd9Sstevel@tonic-gate 			AS_SETUNMAPWAIT(as);
27547c478bd9Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
27557c478bd9Sstevel@tonic-gate 			while (AS_ISUNMAPWAIT(as)) {
27567c478bd9Sstevel@tonic-gate 				cv_wait(&as->a_cv, &as->a_contents);
27577c478bd9Sstevel@tonic-gate 			}
27587c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
27597c478bd9Sstevel@tonic-gate 			goto setpgsz_top;
27607c478bd9Sstevel@tonic-gate 		} else if (error != 0) {
27617c478bd9Sstevel@tonic-gate 			break;
27627c478bd9Sstevel@tonic-gate 		}
27637c478bd9Sstevel@tonic-gate 	}
27647c478bd9Sstevel@tonic-gate 	as_setwatch(as);
27657c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
27667c478bd9Sstevel@tonic-gate 	return (error);
27677c478bd9Sstevel@tonic-gate }
27687c478bd9Sstevel@tonic-gate 
2769ec25b48fSsusans /*
2770ec25b48fSsusans  * as_iset3_default_lpsize() just calls SEGOP_SETPAGESIZE() on all segments
2771ec25b48fSsusans  * in its chunk where s_szc is less than the szc we want to set.
2772ec25b48fSsusans  */
2773ec25b48fSsusans static int
2774ec25b48fSsusans as_iset3_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc,
2775ec25b48fSsusans     int *retry)
2776ec25b48fSsusans {
2777ec25b48fSsusans 	struct seg *seg;
2778ec25b48fSsusans 	size_t ssize;
2779ec25b48fSsusans 	int error;
2780ec25b48fSsusans 
2781ec25b48fSsusans 	seg = as_segat(as, raddr);
2782ec25b48fSsusans 	if (seg == NULL) {
2783ec25b48fSsusans 		panic("as_iset3_default_lpsize: no seg");
2784ec25b48fSsusans 	}
2785ec25b48fSsusans 
2786ec25b48fSsusans 	for (; rsize != 0; rsize -= ssize, raddr += ssize) {
2787ec25b48fSsusans 		if (raddr >= seg->s_base + seg->s_size) {
2788ec25b48fSsusans 			seg = AS_SEGNEXT(as, seg);
2789ec25b48fSsusans 			if (seg == NULL || raddr != seg->s_base) {
2790ec25b48fSsusans 				panic("as_iset3_default_lpsize: as changed");
2791ec25b48fSsusans 			}
2792ec25b48fSsusans 		}
2793ec25b48fSsusans 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
2794ec25b48fSsusans 			ssize = seg->s_base + seg->s_size - raddr;
2795ec25b48fSsusans 		} else {
2796ec25b48fSsusans 			ssize = rsize;
2797ec25b48fSsusans 		}
2798ec25b48fSsusans 
2799ec25b48fSsusans 		if (szc > seg->s_szc) {
2800ec25b48fSsusans 			error = SEGOP_SETPAGESIZE(seg, raddr, ssize, szc);
2801ec25b48fSsusans 			/* Only retry on EINVAL segments that have no vnode. */
2802ec25b48fSsusans 			if (error == EINVAL) {
2803ec25b48fSsusans 				vnode_t *vp = NULL;
2804ec25b48fSsusans 				if ((SEGOP_GETTYPE(seg, raddr) & MAP_SHARED) &&
2805ec25b48fSsusans 				    (SEGOP_GETVP(seg, raddr, &vp) != 0 ||
2806ec25b48fSsusans 				    vp == NULL)) {
2807ec25b48fSsusans 					*retry = 1;
2808ec25b48fSsusans 				} else {
2809ec25b48fSsusans 					*retry = 0;
2810ec25b48fSsusans 				}
2811ec25b48fSsusans 			}
2812ec25b48fSsusans 			if (error) {
2813ec25b48fSsusans 				return (error);
2814ec25b48fSsusans 			}
2815ec25b48fSsusans 		}
2816ec25b48fSsusans 	}
2817ec25b48fSsusans 	return (0);
2818ec25b48fSsusans }
2819ec25b48fSsusans 
2820ec25b48fSsusans /*
2821ec25b48fSsusans  * as_iset2_default_lpsize() calls as_iset3_default_lpsize() to set the
2822ec25b48fSsusans  * pagesize on each segment in its range, but if any fails with EINVAL,
2823ec25b48fSsusans  * then it reduces the pagesizes to the next size in the bitmap and
2824ec25b48fSsusans  * retries as_iset3_default_lpsize(). The reason why the code retries
2825ec25b48fSsusans  * smaller allowed sizes on EINVAL is because (a) the anon offset may not
2826ec25b48fSsusans  * match the bigger sizes, and (b) it's hard to get this offset (to begin
2827ec25b48fSsusans  * with) to pass to map_pgszcvec().
2828ec25b48fSsusans  */
2829ec25b48fSsusans static int
2830ec25b48fSsusans as_iset2_default_lpsize(struct as *as, caddr_t addr, size_t size, uint_t szc,
2831ec25b48fSsusans     uint_t szcvec)
2832ec25b48fSsusans {
2833ec25b48fSsusans 	int error;
2834ec25b48fSsusans 	int retry;
2835ec25b48fSsusans 
2836ec25b48fSsusans 	for (;;) {
2837ec25b48fSsusans 		error = as_iset3_default_lpsize(as, addr, size, szc, &retry);
2838ec25b48fSsusans 		if (error == EINVAL && retry) {
2839ec25b48fSsusans 			szcvec &= ~(1 << szc);
2840ec25b48fSsusans 			if (szcvec <= 1) {
2841ec25b48fSsusans 				return (EINVAL);
2842ec25b48fSsusans 			}
2843ec25b48fSsusans 			szc = highbit(szcvec) - 1;
2844ec25b48fSsusans 		} else {
2845ec25b48fSsusans 			return (error);
2846ec25b48fSsusans 		}
2847ec25b48fSsusans 	}
2848ec25b48fSsusans }
2849ec25b48fSsusans 
2850ec25b48fSsusans /*
2851ec25b48fSsusans  * as_iset1_default_lpsize() breaks its chunk into areas where existing
2852ec25b48fSsusans  * segments have a smaller szc than we want to set. For each such area,
2853ec25b48fSsusans  * it calls as_iset2_default_lpsize()
2854ec25b48fSsusans  */
2855ec25b48fSsusans static int
2856ec25b48fSsusans as_iset1_default_lpsize(struct as *as, caddr_t raddr, size_t rsize, uint_t szc,
2857ec25b48fSsusans     uint_t szcvec)
2858ec25b48fSsusans {
2859ec25b48fSsusans 	struct seg *seg;
2860ec25b48fSsusans 	size_t ssize;
2861ec25b48fSsusans 	caddr_t setaddr = raddr;
2862ec25b48fSsusans 	size_t setsize = 0;
2863ec25b48fSsusans 	int set;
2864ec25b48fSsusans 	int error;
2865ec25b48fSsusans 
2866ec25b48fSsusans 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
2867ec25b48fSsusans 
2868ec25b48fSsusans 	seg = as_segat(as, raddr);
2869ec25b48fSsusans 	if (seg == NULL) {
2870ec25b48fSsusans 		panic("as_iset1_default_lpsize: no seg");
2871ec25b48fSsusans 	}
2872ec25b48fSsusans 	if (seg->s_szc < szc) {
2873ec25b48fSsusans 		set = 1;
2874ec25b48fSsusans 	} else {
2875ec25b48fSsusans 		set = 0;
2876ec25b48fSsusans 	}
2877ec25b48fSsusans 
2878ec25b48fSsusans 	for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) {
2879ec25b48fSsusans 		if (raddr >= seg->s_base + seg->s_size) {
2880ec25b48fSsusans 			seg = AS_SEGNEXT(as, seg);
2881ec25b48fSsusans 			if (seg == NULL || raddr != seg->s_base) {
2882ec25b48fSsusans 				panic("as_iset1_default_lpsize: as changed");
2883ec25b48fSsusans 			}
2884ec25b48fSsusans 			if (seg->s_szc >= szc && set) {
2885ec25b48fSsusans 				ASSERT(setsize != 0);
2886ec25b48fSsusans 				error = as_iset2_default_lpsize(as,
2887ec25b48fSsusans 				    setaddr, setsize, szc, szcvec);
2888ec25b48fSsusans 				if (error) {
2889ec25b48fSsusans 					return (error);
2890ec25b48fSsusans 				}
2891ec25b48fSsusans 				set = 0;
2892ec25b48fSsusans 			} else if (seg->s_szc < szc && !set) {
2893ec25b48fSsusans 				setaddr = raddr;
2894ec25b48fSsusans 				setsize = 0;
2895ec25b48fSsusans 				set = 1;
2896ec25b48fSsusans 			}
2897ec25b48fSsusans 		}
2898ec25b48fSsusans 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
2899ec25b48fSsusans 			ssize = seg->s_base + seg->s_size - raddr;
2900ec25b48fSsusans 		} else {
2901ec25b48fSsusans 			ssize = rsize;
2902ec25b48fSsusans 		}
2903ec25b48fSsusans 	}
2904ec25b48fSsusans 	error = 0;
2905ec25b48fSsusans 	if (set) {
2906ec25b48fSsusans 		ASSERT(setsize != 0);
2907ec25b48fSsusans 		error = as_iset2_default_lpsize(as, setaddr, setsize,
2908ec25b48fSsusans 		    szc, szcvec);
2909ec25b48fSsusans 	}
2910ec25b48fSsusans 	return (error);
2911ec25b48fSsusans }
2912ec25b48fSsusans 
2913ec25b48fSsusans /*
2914ec25b48fSsusans  * as_iset_default_lpsize() breaks its chunk according to the size code bitmap
2915ec25b48fSsusans  * returned by map_pgszcvec() (similar to as_map_segvn_segs()), and passes each
2916ec25b48fSsusans  * chunk to as_iset1_default_lpsize().
2917ec25b48fSsusans  */
2918ec25b48fSsusans static int
2919ec25b48fSsusans as_iset_default_lpsize(struct as *as, caddr_t addr, size_t size, int flags,
2920ec25b48fSsusans     int type)
2921ec25b48fSsusans {
2922ec25b48fSsusans 	int rtype = (type & MAP_SHARED) ? MAPPGSZC_SHM : MAPPGSZC_PRIVM;
2923ec25b48fSsusans 	uint_t szcvec = map_pgszcvec(addr, size, (uintptr_t)addr,
2924ec25b48fSsusans 					flags, rtype, 1);
2925ec25b48fSsusans 	uint_t szc;
2926ec25b48fSsusans 	uint_t nszc;
2927ec25b48fSsusans 	int error;
2928ec25b48fSsusans 	caddr_t a;
2929ec25b48fSsusans 	caddr_t eaddr;
2930ec25b48fSsusans 	size_t segsize;
2931ec25b48fSsusans 	size_t pgsz;
2932ec25b48fSsusans 	uint_t save_szcvec;
2933ec25b48fSsusans 
2934ec25b48fSsusans 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
2935ec25b48fSsusans 	ASSERT(IS_P2ALIGNED(addr, PAGESIZE));
2936ec25b48fSsusans 	ASSERT(IS_P2ALIGNED(size, PAGESIZE));
2937ec25b48fSsusans 
2938ec25b48fSsusans 	szcvec &= ~1;
2939ec25b48fSsusans 	if (szcvec <= 1) {	/* skip if base page size */
2940ec25b48fSsusans 		return (0);
2941ec25b48fSsusans 	}
2942ec25b48fSsusans 
2943ec25b48fSsusans 	/* Get the pagesize of the first larger page size. */
2944ec25b48fSsusans 	szc = lowbit(szcvec) - 1;
2945ec25b48fSsusans 	pgsz = page_get_pagesize(szc);
2946ec25b48fSsusans 	eaddr = addr + size;
2947ec25b48fSsusans 	addr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
2948ec25b48fSsusans 	eaddr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
2949ec25b48fSsusans 
2950ec25b48fSsusans 	save_szcvec = szcvec;
2951ec25b48fSsusans 	szcvec >>= (szc + 1);
2952ec25b48fSsusans 	nszc = szc;
2953ec25b48fSsusans 	while (szcvec) {
2954ec25b48fSsusans 		if ((szcvec & 0x1) == 0) {
2955ec25b48fSsusans 			nszc++;
2956ec25b48fSsusans 			szcvec >>= 1;
2957ec25b48fSsusans 			continue;
2958ec25b48fSsusans 		}
2959ec25b48fSsusans 		nszc++;
2960ec25b48fSsusans 		pgsz = page_get_pagesize(nszc);
2961ec25b48fSsusans 		a = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
2962ec25b48fSsusans 		if (a != addr) {
2963ec25b48fSsusans 			ASSERT(szc > 0);
2964ec25b48fSsusans 			ASSERT(a < eaddr);
2965ec25b48fSsusans 			segsize = a - addr;
2966ec25b48fSsusans 			error = as_iset1_default_lpsize(as, addr, segsize, szc,
2967ec25b48fSsusans 			    save_szcvec);
2968ec25b48fSsusans 			if (error) {
2969ec25b48fSsusans 				return (error);
2970ec25b48fSsusans 			}
2971ec25b48fSsusans 			addr = a;
2972ec25b48fSsusans 		}
2973ec25b48fSsusans 		szc = nszc;
2974ec25b48fSsusans 		szcvec >>= 1;
2975ec25b48fSsusans 	}
2976ec25b48fSsusans 
2977ec25b48fSsusans 	ASSERT(addr < eaddr);
2978ec25b48fSsusans 	szcvec = save_szcvec;
2979ec25b48fSsusans 	while (szcvec) {
2980ec25b48fSsusans 		a = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
2981ec25b48fSsusans 		ASSERT(a >= addr);
2982ec25b48fSsusans 		if (a != addr) {
2983ec25b48fSsusans 			ASSERT(szc > 0);
2984ec25b48fSsusans 			segsize = a - addr;
2985ec25b48fSsusans 			error = as_iset1_default_lpsize(as, addr, segsize, szc,
2986ec25b48fSsusans 			    save_szcvec);
2987ec25b48fSsusans 			if (error) {
2988ec25b48fSsusans 				return (error);
2989ec25b48fSsusans 			}
2990ec25b48fSsusans 			addr = a;
2991ec25b48fSsusans 		}
2992ec25b48fSsusans 		szcvec &= ~(1 << szc);
2993ec25b48fSsusans 		if (szcvec) {
2994ec25b48fSsusans 			szc = highbit(szcvec) - 1;
2995ec25b48fSsusans 			pgsz = page_get_pagesize(szc);
2996ec25b48fSsusans 		}
2997ec25b48fSsusans 	}
2998ec25b48fSsusans 	ASSERT(addr == eaddr);
2999ec25b48fSsusans 
3000ec25b48fSsusans 	return (0);
3001ec25b48fSsusans }
3002ec25b48fSsusans 
3003ec25b48fSsusans /*
3004ec25b48fSsusans  * Set the default large page size for the range. Called via memcntl with
3005ec25b48fSsusans  * page size set to 0. as_set_default_lpsize breaks the range down into
3006ec25b48fSsusans  * chunks with the same type/flags, ignores-non segvn segments, and passes
3007ec25b48fSsusans  * each chunk to as_iset_default_lpsize().
3008ec25b48fSsusans  */
3009ec25b48fSsusans int
3010ec25b48fSsusans as_set_default_lpsize(struct as *as, caddr_t addr, size_t size)
3011ec25b48fSsusans {
3012ec25b48fSsusans 	struct seg *seg;
3013ec25b48fSsusans 	caddr_t raddr;
3014ec25b48fSsusans 	size_t rsize;
3015ec25b48fSsusans 	size_t ssize;
3016ec25b48fSsusans 	int rtype, rflags;
3017ec25b48fSsusans 	int stype, sflags;
3018ec25b48fSsusans 	int error;
3019ec25b48fSsusans 	caddr_t	setaddr;
3020ec25b48fSsusans 	size_t setsize;
3021ec25b48fSsusans 	int segvn;
3022ec25b48fSsusans 
3023ec25b48fSsusans 	if (size == 0)
3024ec25b48fSsusans 		return (0);
3025ec25b48fSsusans 
3026ec25b48fSsusans 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3027ec25b48fSsusans again:
3028ec25b48fSsusans 	error = 0;
3029ec25b48fSsusans 
3030ec25b48fSsusans 	raddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
3031ec25b48fSsusans 	rsize = (((size_t)(addr + size) + PAGEOFFSET) & PAGEMASK) -
3032ec25b48fSsusans 	    (size_t)raddr;
3033ec25b48fSsusans 
3034ec25b48fSsusans 	if (raddr + rsize < raddr) {		/* check for wraparound */
3035ec25b48fSsusans 		AS_LOCK_EXIT(as, &as->a_lock);
3036ec25b48fSsusans 		return (ENOMEM);
3037ec25b48fSsusans 	}
3038ec25b48fSsusans 	as_clearwatchprot(as, raddr, rsize);
3039ec25b48fSsusans 	seg = as_segat(as, raddr);
3040ec25b48fSsusans 	if (seg == NULL) {
3041ec25b48fSsusans 		as_setwatch(as);
3042ec25b48fSsusans 		AS_LOCK_EXIT(as, &as->a_lock);
3043ec25b48fSsusans 		return (ENOMEM);
3044ec25b48fSsusans 	}
3045ec25b48fSsusans 	if (seg->s_ops == &segvn_ops) {
3046ec25b48fSsusans 		rtype = SEGOP_GETTYPE(seg, addr);
3047ec25b48fSsusans 		rflags = rtype & (MAP_TEXT | MAP_INITDATA);
3048ec25b48fSsusans 		rtype = rtype & (MAP_SHARED | MAP_PRIVATE);
3049ec25b48fSsusans 		segvn = 1;
3050ec25b48fSsusans 	} else {
3051ec25b48fSsusans 		segvn = 0;
3052ec25b48fSsusans 	}
3053ec25b48fSsusans 	setaddr = raddr;
3054ec25b48fSsusans 	setsize = 0;
3055ec25b48fSsusans 
3056ec25b48fSsusans 	for (; rsize != 0; rsize -= ssize, raddr += ssize, setsize += ssize) {
3057ec25b48fSsusans 		if (raddr >= (seg->s_base + seg->s_size)) {
3058ec25b48fSsusans 			seg = AS_SEGNEXT(as, seg);
3059ec25b48fSsusans 			if (seg == NULL || raddr != seg->s_base) {
3060ec25b48fSsusans 				error = ENOMEM;
3061ec25b48fSsusans 				break;
3062ec25b48fSsusans 			}
3063ec25b48fSsusans 			if (seg->s_ops == &segvn_ops) {
3064ec25b48fSsusans 				stype = SEGOP_GETTYPE(seg, raddr);
3065ec25b48fSsusans 				sflags = stype & (MAP_TEXT | MAP_INITDATA);
3066ec25b48fSsusans 				stype &= (MAP_SHARED | MAP_PRIVATE);
3067ec25b48fSsusans 				if (segvn && (rflags != sflags ||
3068ec25b48fSsusans 				    rtype != stype)) {
3069ec25b48fSsusans 					/*
3070ec25b48fSsusans 					 * The next segment is also segvn but
3071ec25b48fSsusans 					 * has different flags and/or type.
3072ec25b48fSsusans 					 */
3073ec25b48fSsusans 					ASSERT(setsize != 0);
3074ec25b48fSsusans 					error = as_iset_default_lpsize(as,
3075ec25b48fSsusans 					    setaddr, setsize, rflags, rtype);
3076ec25b48fSsusans 					if (error) {
3077ec25b48fSsusans 						break;
3078ec25b48fSsusans 					}
3079ec25b48fSsusans 					rflags = sflags;
3080ec25b48fSsusans 					rtype = stype;
3081ec25b48fSsusans 					setaddr = raddr;
3082ec25b48fSsusans 					setsize = 0;
3083ec25b48fSsusans 				} else if (!segvn) {
3084ec25b48fSsusans 					rflags = sflags;
3085ec25b48fSsusans 					rtype = stype;
3086ec25b48fSsusans 					setaddr = raddr;
3087ec25b48fSsusans 					setsize = 0;
3088ec25b48fSsusans 					segvn = 1;
3089ec25b48fSsusans 				}
3090ec25b48fSsusans 			} else if (segvn) {
3091ec25b48fSsusans 				/* The next segment is not segvn. */
3092ec25b48fSsusans 				ASSERT(setsize != 0);
3093ec25b48fSsusans 				error = as_iset_default_lpsize(as,
3094ec25b48fSsusans 				    setaddr, setsize, rflags, rtype);
3095ec25b48fSsusans 				if (error) {
3096ec25b48fSsusans 					break;
3097ec25b48fSsusans 				}
3098ec25b48fSsusans 				segvn = 0;
3099ec25b48fSsusans 			}
3100ec25b48fSsusans 		}
3101ec25b48fSsusans 		if ((raddr + rsize) > (seg->s_base + seg->s_size)) {
3102ec25b48fSsusans 			ssize = seg->s_base + seg->s_size - raddr;
3103ec25b48fSsusans 		} else {
3104ec25b48fSsusans 			ssize = rsize;
3105ec25b48fSsusans 		}
3106ec25b48fSsusans 	}
3107ec25b48fSsusans 	if (error == 0 && segvn) {
3108ec25b48fSsusans 		/* The last chunk when rsize == 0. */
3109ec25b48fSsusans 		ASSERT(setsize != 0);
3110ec25b48fSsusans 		error = as_iset_default_lpsize(as, setaddr, setsize,
3111ec25b48fSsusans 		    rflags, rtype);
3112ec25b48fSsusans 	}
3113ec25b48fSsusans 
3114ec25b48fSsusans 	if (error == IE_RETRY) {
3115ec25b48fSsusans 		goto again;
3116ec25b48fSsusans 	} else if (error == IE_NOMEM) {
3117ec25b48fSsusans 		error = EAGAIN;
3118ec25b48fSsusans 	} else if (error == ENOTSUP) {
3119ec25b48fSsusans 		error = EINVAL;
3120ec25b48fSsusans 	} else if (error == EAGAIN) {
3121ec25b48fSsusans 		mutex_enter(&as->a_contents);
3122ec25b48fSsusans 		if (AS_ISUNMAPWAIT(as) == 0) {
3123ec25b48fSsusans 			cv_broadcast(&as->a_cv);
3124ec25b48fSsusans 		}
3125ec25b48fSsusans 		AS_SETUNMAPWAIT(as);
3126ec25b48fSsusans 		AS_LOCK_EXIT(as, &as->a_lock);
3127ec25b48fSsusans 		while (AS_ISUNMAPWAIT(as)) {
3128ec25b48fSsusans 			cv_wait(&as->a_cv, &as->a_contents);
3129ec25b48fSsusans 		}
3130ec25b48fSsusans 		mutex_exit(&as->a_contents);
3131ec25b48fSsusans 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3132ec25b48fSsusans 		goto again;
3133ec25b48fSsusans 	}
3134ec25b48fSsusans 
3135ec25b48fSsusans 	as_setwatch(as);
3136ec25b48fSsusans 	AS_LOCK_EXIT(as, &as->a_lock);
3137ec25b48fSsusans 	return (error);
3138ec25b48fSsusans }
3139ec25b48fSsusans 
31407c478bd9Sstevel@tonic-gate /*
31417c478bd9Sstevel@tonic-gate  * Setup all of the uninitialized watched pages that we can.
31427c478bd9Sstevel@tonic-gate  */
31437c478bd9Sstevel@tonic-gate void
31447c478bd9Sstevel@tonic-gate as_setwatch(struct as *as)
31457c478bd9Sstevel@tonic-gate {
31467c478bd9Sstevel@tonic-gate 	struct watched_page *pwp;
31477c478bd9Sstevel@tonic-gate 	struct seg *seg;
31487c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
31497c478bd9Sstevel@tonic-gate 	uint_t prot;
31507c478bd9Sstevel@tonic-gate 	int  err, retrycnt;
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
31537c478bd9Sstevel@tonic-gate 		return;
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	for (pwp = avl_first(&as->a_wpage); pwp != NULL;
31587c478bd9Sstevel@tonic-gate 	    pwp = AVL_NEXT(&as->a_wpage, pwp)) {
31597c478bd9Sstevel@tonic-gate 		retrycnt = 0;
31607c478bd9Sstevel@tonic-gate 	retry:
31617c478bd9Sstevel@tonic-gate 		vaddr = pwp->wp_vaddr;
31627c478bd9Sstevel@tonic-gate 		if (pwp->wp_oprot != 0 ||	/* already set up */
31637c478bd9Sstevel@tonic-gate 		    (seg = as_segat(as, vaddr)) == NULL ||
31647c478bd9Sstevel@tonic-gate 		    SEGOP_GETPROT(seg, vaddr, 0, &prot) != 0)
31657c478bd9Sstevel@tonic-gate 			continue;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 		pwp->wp_oprot = prot;
31687c478bd9Sstevel@tonic-gate 		if (pwp->wp_read)
31697c478bd9Sstevel@tonic-gate 			prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
31707c478bd9Sstevel@tonic-gate 		if (pwp->wp_write)
31717c478bd9Sstevel@tonic-gate 			prot &= ~PROT_WRITE;
31727c478bd9Sstevel@tonic-gate 		if (pwp->wp_exec)
31737c478bd9Sstevel@tonic-gate 			prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
31747c478bd9Sstevel@tonic-gate 		if (!(pwp->wp_flags & WP_NOWATCH) && prot != pwp->wp_oprot) {
31757c478bd9Sstevel@tonic-gate 			err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot);
31767c478bd9Sstevel@tonic-gate 			if (err == IE_RETRY) {
31777c478bd9Sstevel@tonic-gate 				pwp->wp_oprot = 0;
31787c478bd9Sstevel@tonic-gate 				ASSERT(retrycnt == 0);
31797c478bd9Sstevel@tonic-gate 				retrycnt++;
31807c478bd9Sstevel@tonic-gate 				goto retry;
31817c478bd9Sstevel@tonic-gate 			}
31827c478bd9Sstevel@tonic-gate 		}
31837c478bd9Sstevel@tonic-gate 		pwp->wp_prot = prot;
31847c478bd9Sstevel@tonic-gate 	}
31857c478bd9Sstevel@tonic-gate }
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate /*
31887c478bd9Sstevel@tonic-gate  * Clear all of the watched pages in the address space.
31897c478bd9Sstevel@tonic-gate  */
31907c478bd9Sstevel@tonic-gate void
31917c478bd9Sstevel@tonic-gate as_clearwatch(struct as *as)
31927c478bd9Sstevel@tonic-gate {
31937c478bd9Sstevel@tonic-gate 	struct watched_page *pwp;
31947c478bd9Sstevel@tonic-gate 	struct seg *seg;
31957c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
31967c478bd9Sstevel@tonic-gate 	uint_t prot;
31977c478bd9Sstevel@tonic-gate 	int err, retrycnt;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
32007c478bd9Sstevel@tonic-gate 		return;
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	for (pwp = avl_first(&as->a_wpage); pwp != NULL;
32057c478bd9Sstevel@tonic-gate 	    pwp = AVL_NEXT(&as->a_wpage, pwp)) {
32067c478bd9Sstevel@tonic-gate 		retrycnt = 0;
32077c478bd9Sstevel@tonic-gate 	retry:
32087c478bd9Sstevel@tonic-gate 		vaddr = pwp->wp_vaddr;
32097c478bd9Sstevel@tonic-gate 		if (pwp->wp_oprot == 0 ||	/* not set up */
32107c478bd9Sstevel@tonic-gate 		    (seg = as_segat(as, vaddr)) == NULL)
32117c478bd9Sstevel@tonic-gate 			continue;
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 		if ((prot = pwp->wp_oprot) != pwp->wp_prot) {
32147c478bd9Sstevel@tonic-gate 			err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, prot);
32157c478bd9Sstevel@tonic-gate 			if (err == IE_RETRY) {
32167c478bd9Sstevel@tonic-gate 				ASSERT(retrycnt == 0);
32177c478bd9Sstevel@tonic-gate 				retrycnt++;
32187c478bd9Sstevel@tonic-gate 				goto retry;
32197c478bd9Sstevel@tonic-gate 			}
32207c478bd9Sstevel@tonic-gate 		}
32217c478bd9Sstevel@tonic-gate 		pwp->wp_oprot = 0;
32227c478bd9Sstevel@tonic-gate 		pwp->wp_prot = 0;
32237c478bd9Sstevel@tonic-gate 	}
32247c478bd9Sstevel@tonic-gate }
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate /*
32277c478bd9Sstevel@tonic-gate  * Force a new setup for all the watched pages in the range.
32287c478bd9Sstevel@tonic-gate  */
32297c478bd9Sstevel@tonic-gate static void
32307c478bd9Sstevel@tonic-gate as_setwatchprot(struct as *as, caddr_t addr, size_t size, uint_t prot)
32317c478bd9Sstevel@tonic-gate {
32327c478bd9Sstevel@tonic-gate 	struct watched_page *pwp;
32337c478bd9Sstevel@tonic-gate 	struct watched_page tpw;
32347c478bd9Sstevel@tonic-gate 	caddr_t eaddr = addr + size;
32357c478bd9Sstevel@tonic-gate 	caddr_t vaddr;
32367c478bd9Sstevel@tonic-gate 	struct seg *seg;
32377c478bd9Sstevel@tonic-gate 	int err, retrycnt;
32387c478bd9Sstevel@tonic-gate 	uint_t	wprot;
32397c478bd9Sstevel@tonic-gate 	avl_index_t where;
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
32427c478bd9Sstevel@tonic-gate 		return;
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
32477c478bd9Sstevel@tonic-gate 	if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL)
32487c478bd9Sstevel@tonic-gate 		pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER);
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 	while (pwp != NULL && pwp->wp_vaddr < eaddr) {
32517c478bd9Sstevel@tonic-gate 		retrycnt = 0;
32527c478bd9Sstevel@tonic-gate 		vaddr = pwp->wp_vaddr;
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 		wprot = prot;
32557c478bd9Sstevel@tonic-gate 		if (pwp->wp_read)
32567c478bd9Sstevel@tonic-gate 			wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
32577c478bd9Sstevel@tonic-gate 		if (pwp->wp_write)
32587c478bd9Sstevel@tonic-gate 			wprot &= ~PROT_WRITE;
32597c478bd9Sstevel@tonic-gate 		if (pwp->wp_exec)
32607c478bd9Sstevel@tonic-gate 			wprot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
32617c478bd9Sstevel@tonic-gate 		if (!(pwp->wp_flags & WP_NOWATCH) && wprot != pwp->wp_oprot) {
32627c478bd9Sstevel@tonic-gate 		retry:
32637c478bd9Sstevel@tonic-gate 			seg = as_segat(as, vaddr);
32647c478bd9Sstevel@tonic-gate 			if (seg == NULL) {
32657c478bd9Sstevel@tonic-gate 				panic("as_setwatchprot: no seg");
32667c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
32677c478bd9Sstevel@tonic-gate 			}
32687c478bd9Sstevel@tonic-gate 			err = SEGOP_SETPROT(seg, vaddr, PAGESIZE, wprot);
32697c478bd9Sstevel@tonic-gate 			if (err == IE_RETRY) {
32707c478bd9Sstevel@tonic-gate 				ASSERT(retrycnt == 0);
32717c478bd9Sstevel@tonic-gate 				retrycnt++;
32727c478bd9Sstevel@tonic-gate 				goto retry;
32737c478bd9Sstevel@tonic-gate 			}
32747c478bd9Sstevel@tonic-gate 		}
32757c478bd9Sstevel@tonic-gate 		pwp->wp_oprot = prot;
32767c478bd9Sstevel@tonic-gate 		pwp->wp_prot = wprot;
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 		pwp = AVL_NEXT(&as->a_wpage, pwp);
32797c478bd9Sstevel@tonic-gate 	}
32807c478bd9Sstevel@tonic-gate }
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate /*
32837c478bd9Sstevel@tonic-gate  * Clear all of the watched pages in the range.
32847c478bd9Sstevel@tonic-gate  */
32857c478bd9Sstevel@tonic-gate static void
32867c478bd9Sstevel@tonic-gate as_clearwatchprot(struct as *as, caddr_t addr, size_t size)
32877c478bd9Sstevel@tonic-gate {
32887c478bd9Sstevel@tonic-gate 	caddr_t eaddr = addr + size;
32897c478bd9Sstevel@tonic-gate 	struct watched_page *pwp;
32907c478bd9Sstevel@tonic-gate 	struct watched_page tpw;
32917c478bd9Sstevel@tonic-gate 	uint_t prot;
32927c478bd9Sstevel@tonic-gate 	struct seg *seg;
32937c478bd9Sstevel@tonic-gate 	int err, retrycnt;
32947c478bd9Sstevel@tonic-gate 	avl_index_t where;
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	if (avl_numnodes(&as->a_wpage) == 0)
32977c478bd9Sstevel@tonic-gate 		return;
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
33007c478bd9Sstevel@tonic-gate 	if ((pwp = avl_find(&as->a_wpage, &tpw, &where)) == NULL)
33017c478bd9Sstevel@tonic-gate 		pwp = avl_nearest(&as->a_wpage, where, AVL_AFTER);
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	while (pwp != NULL && pwp->wp_vaddr < eaddr) {
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 		if ((prot = pwp->wp_oprot) != 0) {
33087c478bd9Sstevel@tonic-gate 			retrycnt = 0;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 			if (prot != pwp->wp_prot) {
33117c478bd9Sstevel@tonic-gate 			retry:
33127c478bd9Sstevel@tonic-gate 				seg = as_segat(as, pwp->wp_vaddr);
33137c478bd9Sstevel@tonic-gate 				if (seg == NULL)
33147c478bd9Sstevel@tonic-gate 					continue;
33157c478bd9Sstevel@tonic-gate 				err = SEGOP_SETPROT(seg, pwp->wp_vaddr,
33167c478bd9Sstevel@tonic-gate 				    PAGESIZE, prot);
33177c478bd9Sstevel@tonic-gate 				if (err == IE_RETRY) {
33187c478bd9Sstevel@tonic-gate 					ASSERT(retrycnt == 0);
33197c478bd9Sstevel@tonic-gate 					retrycnt++;
33207c478bd9Sstevel@tonic-gate 					goto retry;
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 				}
33237c478bd9Sstevel@tonic-gate 			}
33247c478bd9Sstevel@tonic-gate 			pwp->wp_oprot = 0;
33257c478bd9Sstevel@tonic-gate 			pwp->wp_prot = 0;
33267c478bd9Sstevel@tonic-gate 		}
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 		pwp = AVL_NEXT(&as->a_wpage, pwp);
33297c478bd9Sstevel@tonic-gate 	}
33307c478bd9Sstevel@tonic-gate }
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate void
33337c478bd9Sstevel@tonic-gate as_signal_proc(struct as *as, k_siginfo_t *siginfo)
33347c478bd9Sstevel@tonic-gate {
33357c478bd9Sstevel@tonic-gate 	struct proc *p;
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
33387c478bd9Sstevel@tonic-gate 	for (p = practive; p; p = p->p_next) {
33397c478bd9Sstevel@tonic-gate 		if (p->p_as == as) {
33407c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
33417c478bd9Sstevel@tonic-gate 			if (p->p_as == as)
33427c478bd9Sstevel@tonic-gate 				sigaddq(p, NULL, siginfo, KM_NOSLEEP);
33437c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
33447c478bd9Sstevel@tonic-gate 		}
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
33477c478bd9Sstevel@tonic-gate }
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate /*
33507c478bd9Sstevel@tonic-gate  * return memory object ID
33517c478bd9Sstevel@tonic-gate  */
33527c478bd9Sstevel@tonic-gate int
33537c478bd9Sstevel@tonic-gate as_getmemid(struct as *as, caddr_t addr, memid_t *memidp)
33547c478bd9Sstevel@tonic-gate {
33557c478bd9Sstevel@tonic-gate 	struct seg	*seg;
33567c478bd9Sstevel@tonic-gate 	int		sts;
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
33597c478bd9Sstevel@tonic-gate 	seg = as_segat(as, addr);
33607c478bd9Sstevel@tonic-gate 	if (seg == NULL) {
33617c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
33627c478bd9Sstevel@tonic-gate 		return (EFAULT);
33637c478bd9Sstevel@tonic-gate 	}
33647c478bd9Sstevel@tonic-gate 	/*
33657c478bd9Sstevel@tonic-gate 	 * catch old drivers which may not support getmemid
33667c478bd9Sstevel@tonic-gate 	 */
33677c478bd9Sstevel@tonic-gate 	if (seg->s_ops->getmemid == NULL) {
33687c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
33697c478bd9Sstevel@tonic-gate 		return (ENODEV);
33707c478bd9Sstevel@tonic-gate 	}
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	sts = SEGOP_GETMEMID(seg, addr, memidp);
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(as, &as->a_lock);
33757c478bd9Sstevel@tonic-gate 	return (sts);
33767c478bd9Sstevel@tonic-gate }
3377