xref: /onnv-gate/usr/src/uts/sfmmu/vm/xhat_sfmmu.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/cmn_err.h>
32*0Sstevel@tonic-gate #include <sys/mman.h>
33*0Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
34*0Sstevel@tonic-gate #include <vm/xhat.h>
35*0Sstevel@tonic-gate #include <vm/xhat_sfmmu.h>
36*0Sstevel@tonic-gate #include <vm/page.h>
37*0Sstevel@tonic-gate #include <vm/as.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * Allocates a block that includes both struct xhat and
43*0Sstevel@tonic-gate  * provider-specific data.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate struct xhat_hme_blk *
xhat_alloc_xhatblk(struct xhat * xhat)46*0Sstevel@tonic-gate xhat_alloc_xhatblk(struct xhat *xhat)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate 	struct xhat_hme_blk *xblk;
49*0Sstevel@tonic-gate 	xblk_cache_t	*xblkcache = xhat->xhat_provider->xblkcache;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	mutex_enter(&xblkcache->lock);
54*0Sstevel@tonic-gate 	if (xblkcache->free_blks) {
55*0Sstevel@tonic-gate 		xblk = (struct xhat_hme_blk *)
56*0Sstevel@tonic-gate 		    sfmmu_hmetohblk(xblkcache->free_blks);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 		/*
59*0Sstevel@tonic-gate 		 * Since we are always walking the list in the
60*0Sstevel@tonic-gate 		 * forward direction, we don't update prev pointers
61*0Sstevel@tonic-gate 		 */
62*0Sstevel@tonic-gate 		xblkcache->free_blks = xblk->xblk_hme[0].hme_next;
63*0Sstevel@tonic-gate 		mutex_exit(&xblkcache->lock);
64*0Sstevel@tonic-gate 	} else {
65*0Sstevel@tonic-gate 		mutex_exit(&xblkcache->lock);
66*0Sstevel@tonic-gate 		xblk = kmem_cache_alloc(xblkcache->cache, KM_SLEEP);
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	return (xblk);
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Return the block to free_blks pool. The memory will
75*0Sstevel@tonic-gate  * be freed in the reclaim routine.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate void
xhat_free_xhatblk(struct xhat_hme_blk * xblk)78*0Sstevel@tonic-gate xhat_free_xhatblk(struct xhat_hme_blk *xblk)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	xblk_cache_t	*xblkcache = xblk->xhat_hme_blk_hat->
81*0Sstevel@tonic-gate 	    xhat_provider->xblkcache;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	mutex_enter(&xblkcache->lock);
85*0Sstevel@tonic-gate 	xblk->xblk_hme[0].hme_next = xblkcache->free_blks;
86*0Sstevel@tonic-gate 	xblkcache->free_blks = &xblk->xblk_hme[0];
87*0Sstevel@tonic-gate 	mutex_exit(&xblkcache->lock);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * Ran by kmem reaper thread. Also called when
93*0Sstevel@tonic-gate  * provider unregisters
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate void
xhat_xblkcache_reclaim(void * arg)96*0Sstevel@tonic-gate xhat_xblkcache_reclaim(void *arg)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	xhat_provider_t *provider = (xhat_provider_t *)arg;
99*0Sstevel@tonic-gate 	struct sf_hment	*sfhme;
100*0Sstevel@tonic-gate 	struct xhat_hme_blk	*xblk;
101*0Sstevel@tonic-gate 	xblk_cache_t	*xblkcache;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	if (provider == NULL)
104*0Sstevel@tonic-gate 		cmn_err(CE_PANIC, "xhat_xblkcache_reclaim() is passed NULL");
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	xblkcache = provider->xblkcache;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	while (xblkcache->free_blks != NULL) {
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		/*
112*0Sstevel@tonic-gate 		 * Put free blocks on a separate list
113*0Sstevel@tonic-gate 		 * and free free_blks pointer.
114*0Sstevel@tonic-gate 		 */
115*0Sstevel@tonic-gate 		mutex_enter(&xblkcache->lock);
116*0Sstevel@tonic-gate 		sfhme = xblkcache->free_blks;
117*0Sstevel@tonic-gate 		xblkcache->free_blks = NULL;
118*0Sstevel@tonic-gate 		mutex_exit(&xblkcache->lock);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 		while (sfhme != NULL) {
121*0Sstevel@tonic-gate 			xblk = (struct xhat_hme_blk *)sfmmu_hmetohblk(sfhme);
122*0Sstevel@tonic-gate 			ASSERT(xblk->xhat_hme_blk_misc.xhat_bit == 1);
123*0Sstevel@tonic-gate 			sfhme = sfhme->hme_next;
124*0Sstevel@tonic-gate 			kmem_cache_free(xblkcache->cache, xblk);
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate  * Insert the xhat block (or, more precisely, the sf_hment)
134*0Sstevel@tonic-gate  * into page's p_mapping list.
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate pfn_t
xhat_insert_xhatblk(page_t * pp,struct xhat * xhat,void ** blk)137*0Sstevel@tonic-gate xhat_insert_xhatblk(page_t *pp, struct xhat *xhat, void **blk)
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate 	kmutex_t *pml;
140*0Sstevel@tonic-gate 	pfn_t pfn;
141*0Sstevel@tonic-gate 	struct xhat_hme_blk *xblk;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	xblk = xhat_alloc_xhatblk(xhat);
146*0Sstevel@tonic-gate 	if (xblk == NULL)
147*0Sstevel@tonic-gate 		return (0);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* Add a "user" to the XHAT */
150*0Sstevel@tonic-gate 	xhat_hat_hold(xhat);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	xblk->xhat_hme_blk_hat = xhat;
153*0Sstevel@tonic-gate 	xblk->xhat_hme_blk_misc.xhat_bit = 1;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	pml = sfmmu_mlist_enter(pp);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/* Insert at the head of p_mapping list */
159*0Sstevel@tonic-gate 	xblk->xblk_hme[0].hme_prev = NULL;
160*0Sstevel@tonic-gate 	xblk->xblk_hme[0].hme_next = pp->p_mapping;
161*0Sstevel@tonic-gate 	xblk->xblk_hme[0].hme_page = pp;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	/* Only one tte per xhat_hme_blk, at least for now */
164*0Sstevel@tonic-gate 	xblk->xblk_hme[0].hme_tte.tte_hmenum = 0;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (pp->p_mapping) {
167*0Sstevel@tonic-gate 		((struct sf_hment *)(pp->p_mapping))->hme_prev =
168*0Sstevel@tonic-gate 		    &(xblk->xblk_hme[0]);
169*0Sstevel@tonic-gate 		ASSERT(pp->p_share > 0);
170*0Sstevel@tonic-gate 	} else	{
171*0Sstevel@tonic-gate 		/* EMPTY */
172*0Sstevel@tonic-gate 		ASSERT(pp->p_share == 0);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 	pp->p_mapping = &(xblk->xblk_hme[0]);
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	/*
177*0Sstevel@tonic-gate 	 * Update number of mappings.
178*0Sstevel@tonic-gate 	 */
179*0Sstevel@tonic-gate 	pp->p_share++;
180*0Sstevel@tonic-gate 	pfn = pp->p_pagenum;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	sfmmu_mlist_exit(pml);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	*blk = XBLK2PROVBLK(xblk);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	return (pfn);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate  * mlist_locked indicates whether the mapping list
192*0Sstevel@tonic-gate  * is locked. If provider did not lock it himself, the
193*0Sstevel@tonic-gate  * only time it is locked in HAT layer is in
194*0Sstevel@tonic-gate  * hat_pageunload().
195*0Sstevel@tonic-gate  */
196*0Sstevel@tonic-gate int
xhat_delete_xhatblk(void * blk,int mlist_locked)197*0Sstevel@tonic-gate xhat_delete_xhatblk(void *blk, int mlist_locked)
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate 	struct xhat_hme_blk *xblk = PROVBLK2XBLK(blk);
200*0Sstevel@tonic-gate 	page_t *pp = xblk->xblk_hme[0].hme_page;
201*0Sstevel@tonic-gate 	kmutex_t *pml;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	ASSERT(pp != NULL);
205*0Sstevel@tonic-gate 	ASSERT(pp->p_share > 0);
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	if (!mlist_locked)
208*0Sstevel@tonic-gate 		pml = sfmmu_mlist_enter(pp);
209*0Sstevel@tonic-gate 	else
210*0Sstevel@tonic-gate 		ASSERT(sfmmu_mlist_held(pp));
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	pp->p_share--;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	if (xblk->xblk_hme[0].hme_prev) {
215*0Sstevel@tonic-gate 		ASSERT(pp->p_mapping != &(xblk->xblk_hme[0]));
216*0Sstevel@tonic-gate 		ASSERT(xblk->xblk_hme[0].hme_prev->hme_page == pp);
217*0Sstevel@tonic-gate 		xblk->xblk_hme[0].hme_prev->hme_next =
218*0Sstevel@tonic-gate 		    xblk->xblk_hme[0].hme_next;
219*0Sstevel@tonic-gate 	} else {
220*0Sstevel@tonic-gate 		ASSERT(pp->p_mapping == &(xblk->xblk_hme[0]));
221*0Sstevel@tonic-gate 		pp->p_mapping = xblk->xblk_hme[0].hme_next;
222*0Sstevel@tonic-gate 		ASSERT((pp->p_mapping == NULL) ?
223*0Sstevel@tonic-gate 			(pp->p_share == 0) : 1);
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	if (xblk->xblk_hme->hme_next) {
227*0Sstevel@tonic-gate 		ASSERT(xblk->xblk_hme[0].hme_next->hme_page == pp);
228*0Sstevel@tonic-gate 		xblk->xblk_hme[0].hme_next->hme_prev =
229*0Sstevel@tonic-gate 		    xblk->xblk_hme[0].hme_prev;
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (!mlist_locked)
233*0Sstevel@tonic-gate 		sfmmu_mlist_exit(pml);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	xhat_hat_rele(xblk->xhat_hme_blk_hat);
236*0Sstevel@tonic-gate 	xhat_free_xhatblk(xblk);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	return (0);
240*0Sstevel@tonic-gate }
241