xref: /titanic_44/usr/src/uts/common/vm/xhat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
34*7c478bd9Sstevel@tonic-gate #include <vm/xhat.h>
35*7c478bd9Sstevel@tonic-gate #include <vm/page.h>
36*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate int xhat_debug = 0;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate krwlock_t xhat_provider_rwlock;
41*7c478bd9Sstevel@tonic-gate xhat_provider_t *xhat_provider = NULL;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate void
xhat_init()44*7c478bd9Sstevel@tonic-gate xhat_init()
45*7c478bd9Sstevel@tonic-gate {
46*7c478bd9Sstevel@tonic-gate 	rw_init(&xhat_provider_rwlock, NULL, RW_DEFAULT, NULL);
47*7c478bd9Sstevel@tonic-gate }
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate int
xhat_provider_register(xhat_provider_t * provider)52*7c478bd9Sstevel@tonic-gate xhat_provider_register(xhat_provider_t *provider)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	/* strlen("_cache") = 7 */
55*7c478bd9Sstevel@tonic-gate 	char	cache_name[XHAT_CACHE_NAMELEN + 7];
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if (provider->xhat_provider_version != XHAT_PROVIDER_VERSION) {
59*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "XHAT provider version mismatch");
60*7c478bd9Sstevel@tonic-gate 		return (-1);
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if ((XHAT_POPS(provider)->xhat_alloc == NULL) ||
64*7c478bd9Sstevel@tonic-gate 	    (XHAT_POPS(provider)->xhat_free == NULL)) {
65*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Malformed XHAT provider");
66*7c478bd9Sstevel@tonic-gate 		return (-1);
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/* Allocate kmem_cache which will manage xhat blocks */
70*7c478bd9Sstevel@tonic-gate 	provider->xblkcache->free_blks = NULL;
71*7c478bd9Sstevel@tonic-gate 	(void) strncpy(cache_name, provider->xhat_provider_name,
72*7c478bd9Sstevel@tonic-gate 	    XHAT_CACHE_NAMELEN);
73*7c478bd9Sstevel@tonic-gate 	(void) strcat(cache_name, "_cache");
74*7c478bd9Sstevel@tonic-gate 	provider->xblkcache->cache = kmem_cache_create(cache_name,
75*7c478bd9Sstevel@tonic-gate 	    provider->xhat_provider_blk_size, 0, NULL, NULL,
76*7c478bd9Sstevel@tonic-gate 	    provider->xblkcache->reclaim,
77*7c478bd9Sstevel@tonic-gate 	    (void *)provider, NULL, 0);
78*7c478bd9Sstevel@tonic-gate 	if (provider->xblkcache->cache == NULL) {
79*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Failed to allocate cache for %s",
80*7c478bd9Sstevel@tonic-gate 		    provider->xhat_provider_name);
81*7c478bd9Sstevel@tonic-gate 		return (-1);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	mutex_init(&provider->xblkcache->lock, NULL, MUTEX_DEFAULT, NULL);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	/* Insert provider in the global list */
88*7c478bd9Sstevel@tonic-gate 	rw_enter(&xhat_provider_rwlock, RW_WRITER);
89*7c478bd9Sstevel@tonic-gate 	provider->next = xhat_provider;
90*7c478bd9Sstevel@tonic-gate 	provider->prev = NULL;
91*7c478bd9Sstevel@tonic-gate 	if (xhat_provider)
92*7c478bd9Sstevel@tonic-gate 		xhat_provider->prev = provider;
93*7c478bd9Sstevel@tonic-gate 	xhat_provider = provider;
94*7c478bd9Sstevel@tonic-gate 	xhat_provider->xhat_provider_refcnt = 0;
95*7c478bd9Sstevel@tonic-gate 	rw_exit(&xhat_provider_rwlock);
96*7c478bd9Sstevel@tonic-gate 	return (0);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate int
xhat_provider_unregister(xhat_provider_t * provider)102*7c478bd9Sstevel@tonic-gate xhat_provider_unregister(xhat_provider_t *provider)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	if (provider->xhat_provider_version != XHAT_PROVIDER_VERSION)
105*7c478bd9Sstevel@tonic-gate 		return (-1);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	rw_enter(&xhat_provider_rwlock, RW_WRITER);
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (provider->xhat_provider_refcnt) {
110*7c478bd9Sstevel@tonic-gate 		rw_exit(&xhat_provider_rwlock);
111*7c478bd9Sstevel@tonic-gate 		return (-1);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (provider->next)
115*7c478bd9Sstevel@tonic-gate 		provider->next->prev = provider->prev;
116*7c478bd9Sstevel@tonic-gate 	if (provider->prev)
117*7c478bd9Sstevel@tonic-gate 		provider->prev->next = provider->next;
118*7c478bd9Sstevel@tonic-gate 	else
119*7c478bd9Sstevel@tonic-gate 		xhat_provider = provider->next;
120*7c478bd9Sstevel@tonic-gate 	provider->prev = NULL;
121*7c478bd9Sstevel@tonic-gate 	provider->next = NULL;
122*7c478bd9Sstevel@tonic-gate 	rw_exit(&xhat_provider_rwlock);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	/* Free all xblks that are sitting on free_blks list */
125*7c478bd9Sstevel@tonic-gate 	provider->xblkcache->reclaim(provider);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(provider->xblkcache->cache);
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	return (0);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /* Attaches an XHAT to the address space */
135*7c478bd9Sstevel@tonic-gate int
xhat_attach_xhat(xhat_provider_t * provider,struct as * as,struct xhat ** xhatp,void * arg)136*7c478bd9Sstevel@tonic-gate xhat_attach_xhat(xhat_provider_t *provider, struct as *as,
137*7c478bd9Sstevel@tonic-gate     struct xhat **xhatp, void *arg)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	struct xhat *xh;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	xh = XHAT_POPS(provider)->xhat_alloc(arg);
144*7c478bd9Sstevel@tonic-gate 	if (xh == NULL) {
145*7c478bd9Sstevel@tonic-gate 		*xhatp = NULL;
146*7c478bd9Sstevel@tonic-gate 		return (XH_PRVDR);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	mutex_init(&xh->xhat_lock, NULL, MUTEX_DEFAULT, NULL);
150*7c478bd9Sstevel@tonic-gate 	xh->xhat_provider = provider;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	rw_enter(&xhat_provider_rwlock, RW_WRITER);
153*7c478bd9Sstevel@tonic-gate 	provider->xhat_provider_refcnt++;
154*7c478bd9Sstevel@tonic-gate 	rw_exit(&xhat_provider_rwlock);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	/* Is address space busy (being freed, dup'd or swapped)? */
159*7c478bd9Sstevel@tonic-gate 	if (AS_ISBUSY(as)) {
160*7c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
161*7c478bd9Sstevel@tonic-gate 		XHAT_POPS(provider)->xhat_free(xh);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		rw_enter(&xhat_provider_rwlock, RW_WRITER);
164*7c478bd9Sstevel@tonic-gate 		provider->xhat_provider_refcnt--;
165*7c478bd9Sstevel@tonic-gate 		rw_exit(&xhat_provider_rwlock);
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 		*xhatp = NULL;
168*7c478bd9Sstevel@tonic-gate 		return (XH_ASBUSY);
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	xh->xhat_as = as;
172*7c478bd9Sstevel@tonic-gate 	xh->xhat_refcnt = 0;
173*7c478bd9Sstevel@tonic-gate 	xh->holder = NULL;
174*7c478bd9Sstevel@tonic-gate 	xh->arg = arg;
175*7c478bd9Sstevel@tonic-gate 	xh->next = (struct xhat *)as->a_xhat;
176*7c478bd9Sstevel@tonic-gate 	if (xh->next)
177*7c478bd9Sstevel@tonic-gate 		xh->next->prev = xh;
178*7c478bd9Sstevel@tonic-gate 	as->a_xhat = xh;
179*7c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
180*7c478bd9Sstevel@tonic-gate 	*xhatp = xh;
181*7c478bd9Sstevel@tonic-gate 	return (0);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate int
xhat_detach_xhat(xhat_provider_t * provider,struct as * as)186*7c478bd9Sstevel@tonic-gate xhat_detach_xhat(xhat_provider_t *provider, struct as *as)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	struct xhat *xh;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	for (xh = (struct xhat *)as->a_xhat; xh != NULL; xh = xh->next)
194*7c478bd9Sstevel@tonic-gate 		if (xh->xhat_provider == provider) {
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 			if (xh->holder != NULL) {
198*7c478bd9Sstevel@tonic-gate 				/*
199*7c478bd9Sstevel@tonic-gate 				 * The address space is being freed,
200*7c478bd9Sstevel@tonic-gate 				 * dup'd or swapped out.
201*7c478bd9Sstevel@tonic-gate 				 * If we are the thread which doing one
202*7c478bd9Sstevel@tonic-gate 				 * of those operations, we can go ahead
203*7c478bd9Sstevel@tonic-gate 				 * and free up the XHAT.
204*7c478bd9Sstevel@tonic-gate 				 * Otherwise, return.
205*7c478bd9Sstevel@tonic-gate 				 */
206*7c478bd9Sstevel@tonic-gate 				if (xh->holder != curthread) {
207*7c478bd9Sstevel@tonic-gate 					mutex_exit(&as->a_contents);
208*7c478bd9Sstevel@tonic-gate 					return (XH_ASBUSY);
209*7c478bd9Sstevel@tonic-gate 				} else
210*7c478bd9Sstevel@tonic-gate 					xhat_hat_rele(xh);
211*7c478bd9Sstevel@tonic-gate 			}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 			if (xh->xhat_refcnt > 0) {
214*7c478bd9Sstevel@tonic-gate 				/*
215*7c478bd9Sstevel@tonic-gate 				 * There are still "users" of the XHAT.
216*7c478bd9Sstevel@tonic-gate 				 * This may be either because the caller
217*7c478bd9Sstevel@tonic-gate 				 * forgot to free something up (which is a bug)
218*7c478bd9Sstevel@tonic-gate 				 * or because xhat_op_all() is in progress.
219*7c478bd9Sstevel@tonic-gate 				 * Since we are not allowing any of
220*7c478bd9Sstevel@tonic-gate 				 * xhat_op_all's ops to call xhat_detach_xhat(),
221*7c478bd9Sstevel@tonic-gate 				 * This can only be some other thread. It
222*7c478bd9Sstevel@tonic-gate 				 * may want to wait a bit and retry.
223*7c478bd9Sstevel@tonic-gate 				 */
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 				/* Restore the hold on the XHAT */
227*7c478bd9Sstevel@tonic-gate 				if (xh->holder == curthread)
228*7c478bd9Sstevel@tonic-gate 					xhat_hat_hold(xh);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 				mutex_exit(&as->a_contents);
231*7c478bd9Sstevel@tonic-gate 				return (XH_XHHELD);
232*7c478bd9Sstevel@tonic-gate 			}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 			rw_enter(&xhat_provider_rwlock, RW_WRITER);
235*7c478bd9Sstevel@tonic-gate 			provider->xhat_provider_refcnt--;
236*7c478bd9Sstevel@tonic-gate 			rw_exit(&xhat_provider_rwlock);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 			if (xh->next)
239*7c478bd9Sstevel@tonic-gate 				xh->next->prev = xh->prev;
240*7c478bd9Sstevel@tonic-gate 			if (xh->prev)
241*7c478bd9Sstevel@tonic-gate 				xh->prev->next = xh->next;
242*7c478bd9Sstevel@tonic-gate 			else
243*7c478bd9Sstevel@tonic-gate 				as->a_xhat = (void *) xh->next;
244*7c478bd9Sstevel@tonic-gate 			mutex_exit(&as->a_contents);
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 			XHAT_POPS(provider)->xhat_free(xh);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 			return (0);
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
251*7c478bd9Sstevel@tonic-gate 	return (XH_NOTATTCHD);
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate void
xhat_hat_hold(struct xhat * xhat)255*7c478bd9Sstevel@tonic-gate xhat_hat_hold(struct xhat *xhat)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate 	mutex_enter(&xhat->xhat_lock);
258*7c478bd9Sstevel@tonic-gate 	xhat->xhat_refcnt++;
259*7c478bd9Sstevel@tonic-gate 	mutex_exit(&xhat->xhat_lock);
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate void
xhat_hat_rele(struct xhat * xhat)263*7c478bd9Sstevel@tonic-gate xhat_hat_rele(struct xhat *xhat)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	mutex_enter(&xhat->xhat_lock);
266*7c478bd9Sstevel@tonic-gate 	xhat->xhat_refcnt--;
267*7c478bd9Sstevel@tonic-gate 	ASSERT(xhat->xhat_refcnt >= 0);
268*7c478bd9Sstevel@tonic-gate 	mutex_exit(&xhat->xhat_lock);
269*7c478bd9Sstevel@tonic-gate }
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate int
xhat_hat_holders(struct xhat * xhat)273*7c478bd9Sstevel@tonic-gate xhat_hat_holders(struct xhat *xhat)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	return (xhat->xhat_refcnt);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate /*
280*7c478bd9Sstevel@tonic-gate  * Assumes that address space is already locked
281*7c478bd9Sstevel@tonic-gate  * and that AS_FREE is set for as->a_flags.
282*7c478bd9Sstevel@tonic-gate  */
283*7c478bd9Sstevel@tonic-gate void
xhat_free_start_all(struct as * as)284*7c478bd9Sstevel@tonic-gate xhat_free_start_all(struct as *as)
285*7c478bd9Sstevel@tonic-gate {
286*7c478bd9Sstevel@tonic-gate 	struct xhat *xh, *xh_nxt;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	ASSERT(AS_ISBUSY(as));
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
292*7c478bd9Sstevel@tonic-gate 	xh = (struct xhat *)as->a_xhat;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	/*
295*7c478bd9Sstevel@tonic-gate 	 * Simply calling xhat_hat_hold() won't work because we will
296*7c478bd9Sstevel@tonic-gate 	 * not be able to succeed in xhat_detach_xhat(), which may
297*7c478bd9Sstevel@tonic-gate 	 * get called from here. We need to know _who_ the holder is.
298*7c478bd9Sstevel@tonic-gate 	 */
299*7c478bd9Sstevel@tonic-gate 	if (xh != NULL) {
300*7c478bd9Sstevel@tonic-gate 		xhat_hat_hold(xh);
301*7c478bd9Sstevel@tonic-gate 		ASSERT(xh->holder == NULL);
302*7c478bd9Sstevel@tonic-gate 		xh->holder = curthread;
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	while (xh != NULL) {
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 		xh_nxt = xh->next;
308*7c478bd9Sstevel@tonic-gate 		if (xh_nxt != NULL) {
309*7c478bd9Sstevel@tonic-gate 			ASSERT(xh_nxt->holder == NULL);
310*7c478bd9Sstevel@tonic-gate 			xhat_hat_hold(xh_nxt);
311*7c478bd9Sstevel@tonic-gate 			xh_nxt->holder = curthread;
312*7c478bd9Sstevel@tonic-gate 		}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 		XHAT_FREE_START(xh);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 		mutex_enter(&as->a_contents);
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 		xh = xh_nxt;
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate /*
329*7c478bd9Sstevel@tonic-gate  * Assumes that address space is already locked.
330*7c478bd9Sstevel@tonic-gate  * Since xhat_free_start_all() must have been called
331*7c478bd9Sstevel@tonic-gate  * earlier, for all XHATs holder is set to curthread.
332*7c478bd9Sstevel@tonic-gate  * Also, since AS_BUSY is set for as->a_flags, no new
333*7c478bd9Sstevel@tonic-gate  * XHATs could have been added.
334*7c478bd9Sstevel@tonic-gate  */
335*7c478bd9Sstevel@tonic-gate void
xhat_free_end_all(struct as * as)336*7c478bd9Sstevel@tonic-gate xhat_free_end_all(struct as *as)
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	struct xhat *xh, *xh_nxt;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	ASSERT(AS_ISBUSY(as));
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
344*7c478bd9Sstevel@tonic-gate 	xh = (struct xhat *)as->a_xhat;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	while (xh != NULL) {
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 		ASSERT(xh->holder == curthread);
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 		xh_nxt = xh->next;
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 		XHAT_FREE_END(xh);
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 		mutex_enter(&as->a_contents);
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 		xh = xh_nxt;
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate /* Assumes that address space is already locked */
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
369*7c478bd9Sstevel@tonic-gate int
xhat_dup_all(struct as * as,struct as * newas,caddr_t addr,size_t len,uint_t flag)370*7c478bd9Sstevel@tonic-gate xhat_dup_all(struct as *as, struct as *newas, caddr_t addr, size_t len,
371*7c478bd9Sstevel@tonic-gate     uint_t flag)
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 	/* This is not supported. Should we return some sort of error? */
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	ASSERT(AS_ISBUSY(as));
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	return (0);
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate /* Assumes that address space is already locked */
382*7c478bd9Sstevel@tonic-gate void
xhat_swapout_all(struct as * as)383*7c478bd9Sstevel@tonic-gate xhat_swapout_all(struct as *as)
384*7c478bd9Sstevel@tonic-gate {
385*7c478bd9Sstevel@tonic-gate 	struct xhat *xh, *xh_nxt;
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate 	ASSERT(AS_ISBUSY(as));
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
391*7c478bd9Sstevel@tonic-gate 	xh = (struct xhat *)as->a_xhat;
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	if (xh != NULL) {
394*7c478bd9Sstevel@tonic-gate 		xhat_hat_hold(xh);
395*7c478bd9Sstevel@tonic-gate 		ASSERT(xh->holder == NULL);
396*7c478bd9Sstevel@tonic-gate 		xh->holder = curthread;
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	while (xh != NULL) {
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 		xh_nxt = xh->next;
403*7c478bd9Sstevel@tonic-gate 		if (xh_nxt != NULL) {
404*7c478bd9Sstevel@tonic-gate 			ASSERT(xh_nxt->holder == NULL);
405*7c478bd9Sstevel@tonic-gate 			xhat_hat_hold(xh_nxt);
406*7c478bd9Sstevel@tonic-gate 			xh_nxt->holder = curthread;
407*7c478bd9Sstevel@tonic-gate 		}
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 		XHAT_SWAPOUT(xh);
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 		mutex_enter(&as->a_contents);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 		/*
416*7c478bd9Sstevel@tonic-gate 		 * If the xh is still there (i.e. swapout did not
417*7c478bd9Sstevel@tonic-gate 		 * destroy it), clear the holder field.
418*7c478bd9Sstevel@tonic-gate 		 * xh_nxt->prev couldn't have been changed in xhat_attach_xhat()
419*7c478bd9Sstevel@tonic-gate 		 * because AS_BUSY is set. xhat_detach_xhat() also couldn't
420*7c478bd9Sstevel@tonic-gate 		 * have modified it because (holder != NULL).
421*7c478bd9Sstevel@tonic-gate 		 * If there is only one XHAT, just see if a_xhat still
422*7c478bd9Sstevel@tonic-gate 		 * points to us.
423*7c478bd9Sstevel@tonic-gate 		 */
424*7c478bd9Sstevel@tonic-gate 		if (((xh_nxt != NULL) && (xh_nxt->prev == xh)) ||
425*7c478bd9Sstevel@tonic-gate 		    ((as->a_xhat != NULL) && (as->a_xhat == xh))) {
426*7c478bd9Sstevel@tonic-gate 			xhat_hat_rele(xh);
427*7c478bd9Sstevel@tonic-gate 			xh->holder = NULL;
428*7c478bd9Sstevel@tonic-gate 		}
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 		xh = xh_nxt;
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
434*7c478bd9Sstevel@tonic-gate }
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate /*
440*7c478bd9Sstevel@tonic-gate  * In the following routines, the appropriate xhat_op
441*7c478bd9Sstevel@tonic-gate  * should never attempt to call xhat_detach_xhat(): it will
442*7c478bd9Sstevel@tonic-gate  * never succeed since the XHAT is held.
443*7c478bd9Sstevel@tonic-gate  */
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate #define	XHAT_UNLOAD_CALLBACK_OP	(0)
447*7c478bd9Sstevel@tonic-gate #define	XHAT_SETATTR_OP		(1)
448*7c478bd9Sstevel@tonic-gate #define	XHAT_CLRATTR_OP		(2)
449*7c478bd9Sstevel@tonic-gate #define	XHAT_CHGATTR_OP		(3)
450*7c478bd9Sstevel@tonic-gate #define	XHAT_CHGPROT_OP		(4)
451*7c478bd9Sstevel@tonic-gate #define	XHAT_UNSHARE_OP		(5)
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate static void
xhat_op_all(int op,struct as * as,caddr_t addr,size_t len,uint_t flags,void * ptr)455*7c478bd9Sstevel@tonic-gate xhat_op_all(int op, struct as *as, caddr_t addr,
456*7c478bd9Sstevel@tonic-gate     size_t len, uint_t flags, void *ptr)
457*7c478bd9Sstevel@tonic-gate {
458*7c478bd9Sstevel@tonic-gate 	struct xhat *xh, *xh_nxt;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	mutex_enter(&as->a_contents);
461*7c478bd9Sstevel@tonic-gate 	xh = (struct xhat *)as->a_xhat;
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	while (xh != NULL) {
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 		xhat_hat_hold(xh);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 		xh_nxt = xh->next;
468*7c478bd9Sstevel@tonic-gate 		if (xh_nxt != NULL)
469*7c478bd9Sstevel@tonic-gate 			xhat_hat_hold(xh_nxt);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 		mutex_exit(&as->a_contents);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 		switch (op) {
474*7c478bd9Sstevel@tonic-gate 		case XHAT_UNLOAD_CALLBACK_OP:
475*7c478bd9Sstevel@tonic-gate 			XHAT_UNLOAD_CALLBACK(xh, addr,
476*7c478bd9Sstevel@tonic-gate 			    len, flags, (hat_callback_t *)ptr);
477*7c478bd9Sstevel@tonic-gate 			break;
478*7c478bd9Sstevel@tonic-gate 		case XHAT_SETATTR_OP:
479*7c478bd9Sstevel@tonic-gate 			XHAT_SETATTR(xh, addr, len, flags);
480*7c478bd9Sstevel@tonic-gate 			break;
481*7c478bd9Sstevel@tonic-gate 		case XHAT_CLRATTR_OP:
482*7c478bd9Sstevel@tonic-gate 			XHAT_CLRATTR(xh, addr, len, flags);
483*7c478bd9Sstevel@tonic-gate 			break;
484*7c478bd9Sstevel@tonic-gate 		case XHAT_CHGATTR_OP:
485*7c478bd9Sstevel@tonic-gate 			XHAT_CHGATTR(xh, addr, len, flags);
486*7c478bd9Sstevel@tonic-gate 			break;
487*7c478bd9Sstevel@tonic-gate 		case XHAT_CHGPROT_OP:
488*7c478bd9Sstevel@tonic-gate 			XHAT_CHGPROT(xh, addr, len, flags);
489*7c478bd9Sstevel@tonic-gate 			break;
490*7c478bd9Sstevel@tonic-gate 		case XHAT_UNSHARE_OP:
491*7c478bd9Sstevel@tonic-gate 			XHAT_UNSHARE(xh, addr, len);
492*7c478bd9Sstevel@tonic-gate 			break;
493*7c478bd9Sstevel@tonic-gate 		default:
494*7c478bd9Sstevel@tonic-gate 			panic("Unknown op %d in xhat_op_all", op);
495*7c478bd9Sstevel@tonic-gate 		}
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 		mutex_enter(&as->a_contents);
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 		/*
500*7c478bd9Sstevel@tonic-gate 		 * Both pointers are still valid because both
501*7c478bd9Sstevel@tonic-gate 		 * XHATs are held.
502*7c478bd9Sstevel@tonic-gate 		 */
503*7c478bd9Sstevel@tonic-gate 		xhat_hat_rele(xh);
504*7c478bd9Sstevel@tonic-gate 		if (xh_nxt != NULL)
505*7c478bd9Sstevel@tonic-gate 			xhat_hat_rele(xh_nxt);
506*7c478bd9Sstevel@tonic-gate 		xh = xh_nxt;
507*7c478bd9Sstevel@tonic-gate 	}
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	mutex_exit(&as->a_contents);
510*7c478bd9Sstevel@tonic-gate }
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate void
xhat_unload_callback_all(struct as * as,caddr_t addr,size_t len,uint_t flags,hat_callback_t * callback)515*7c478bd9Sstevel@tonic-gate xhat_unload_callback_all(struct as *as, caddr_t addr, size_t len, uint_t flags,
516*7c478bd9Sstevel@tonic-gate     hat_callback_t *callback)
517*7c478bd9Sstevel@tonic-gate {
518*7c478bd9Sstevel@tonic-gate 	xhat_op_all(XHAT_UNLOAD_CALLBACK_OP, as, addr, len, flags, callback);
519*7c478bd9Sstevel@tonic-gate }
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate void
xhat_setattr_all(struct as * as,caddr_t addr,size_t len,uint_t attr)523*7c478bd9Sstevel@tonic-gate xhat_setattr_all(struct as *as, caddr_t addr, size_t len, uint_t attr)
524*7c478bd9Sstevel@tonic-gate {
525*7c478bd9Sstevel@tonic-gate 	xhat_op_all(XHAT_SETATTR_OP, as, addr, len, attr, NULL);
526*7c478bd9Sstevel@tonic-gate }
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate void
xhat_clrattr_all(struct as * as,caddr_t addr,size_t len,uint_t attr)531*7c478bd9Sstevel@tonic-gate xhat_clrattr_all(struct as *as, caddr_t addr, size_t len, uint_t attr)
532*7c478bd9Sstevel@tonic-gate {
533*7c478bd9Sstevel@tonic-gate 	xhat_op_all(XHAT_CLRATTR_OP, as, addr, len, attr, NULL);
534*7c478bd9Sstevel@tonic-gate }
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate void
xhat_chgattr_all(struct as * as,caddr_t addr,size_t len,uint_t attr)538*7c478bd9Sstevel@tonic-gate xhat_chgattr_all(struct as *as, caddr_t addr, size_t len, uint_t attr)
539*7c478bd9Sstevel@tonic-gate {
540*7c478bd9Sstevel@tonic-gate 	xhat_op_all(XHAT_CHGATTR_OP, as, addr, len, attr, NULL);
541*7c478bd9Sstevel@tonic-gate }
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate void
xhat_chgprot_all(struct as * as,caddr_t addr,size_t len,uint_t prot)545*7c478bd9Sstevel@tonic-gate xhat_chgprot_all(struct as *as, caddr_t addr, size_t len, uint_t prot)
546*7c478bd9Sstevel@tonic-gate {
547*7c478bd9Sstevel@tonic-gate 	xhat_op_all(XHAT_CHGPROT_OP, as, addr, len, prot, NULL);
548*7c478bd9Sstevel@tonic-gate }
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate void
xhat_unshare_all(struct as * as,caddr_t addr,size_t len)552*7c478bd9Sstevel@tonic-gate xhat_unshare_all(struct as *as, caddr_t addr, size_t len)
553*7c478bd9Sstevel@tonic-gate {
554*7c478bd9Sstevel@tonic-gate 	xhat_op_all(XHAT_UNSHARE_OP, as, addr, len, 0, NULL);
555*7c478bd9Sstevel@tonic-gate }
556