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 <vm/hat_sfmmu.h>
34*7c478bd9Sstevel@tonic-gate #include <vm/xhat.h>
35*7c478bd9Sstevel@tonic-gate #include <vm/xhat_sfmmu.h>
36*7c478bd9Sstevel@tonic-gate #include <vm/page.h>
37*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate * Allocates a block that includes both struct xhat and
43*7c478bd9Sstevel@tonic-gate * provider-specific data.
44*7c478bd9Sstevel@tonic-gate */
45*7c478bd9Sstevel@tonic-gate struct xhat_hme_blk *
xhat_alloc_xhatblk(struct xhat * xhat)46*7c478bd9Sstevel@tonic-gate xhat_alloc_xhatblk(struct xhat *xhat)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate struct xhat_hme_blk *xblk;
49*7c478bd9Sstevel@tonic-gate xblk_cache_t *xblkcache = xhat->xhat_provider->xblkcache;
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate mutex_enter(&xblkcache->lock);
54*7c478bd9Sstevel@tonic-gate if (xblkcache->free_blks) {
55*7c478bd9Sstevel@tonic-gate xblk = (struct xhat_hme_blk *)
56*7c478bd9Sstevel@tonic-gate sfmmu_hmetohblk(xblkcache->free_blks);
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate * Since we are always walking the list in the
60*7c478bd9Sstevel@tonic-gate * forward direction, we don't update prev pointers
61*7c478bd9Sstevel@tonic-gate */
62*7c478bd9Sstevel@tonic-gate xblkcache->free_blks = xblk->xblk_hme[0].hme_next;
63*7c478bd9Sstevel@tonic-gate mutex_exit(&xblkcache->lock);
64*7c478bd9Sstevel@tonic-gate } else {
65*7c478bd9Sstevel@tonic-gate mutex_exit(&xblkcache->lock);
66*7c478bd9Sstevel@tonic-gate xblk = kmem_cache_alloc(xblkcache->cache, KM_SLEEP);
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate return (xblk);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate * Return the block to free_blks pool. The memory will
75*7c478bd9Sstevel@tonic-gate * be freed in the reclaim routine.
76*7c478bd9Sstevel@tonic-gate */
77*7c478bd9Sstevel@tonic-gate void
xhat_free_xhatblk(struct xhat_hme_blk * xblk)78*7c478bd9Sstevel@tonic-gate xhat_free_xhatblk(struct xhat_hme_blk *xblk)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate xblk_cache_t *xblkcache = xblk->xhat_hme_blk_hat->
81*7c478bd9Sstevel@tonic-gate xhat_provider->xblkcache;
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate mutex_enter(&xblkcache->lock);
85*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_next = xblkcache->free_blks;
86*7c478bd9Sstevel@tonic-gate xblkcache->free_blks = &xblk->xblk_hme[0];
87*7c478bd9Sstevel@tonic-gate mutex_exit(&xblkcache->lock);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate * Ran by kmem reaper thread. Also called when
93*7c478bd9Sstevel@tonic-gate * provider unregisters
94*7c478bd9Sstevel@tonic-gate */
95*7c478bd9Sstevel@tonic-gate void
xhat_xblkcache_reclaim(void * arg)96*7c478bd9Sstevel@tonic-gate xhat_xblkcache_reclaim(void *arg)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate xhat_provider_t *provider = (xhat_provider_t *)arg;
99*7c478bd9Sstevel@tonic-gate struct sf_hment *sfhme;
100*7c478bd9Sstevel@tonic-gate struct xhat_hme_blk *xblk;
101*7c478bd9Sstevel@tonic-gate xblk_cache_t *xblkcache;
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate if (provider == NULL)
104*7c478bd9Sstevel@tonic-gate cmn_err(CE_PANIC, "xhat_xblkcache_reclaim() is passed NULL");
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate xblkcache = provider->xblkcache;
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate while (xblkcache->free_blks != NULL) {
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate * Put free blocks on a separate list
113*7c478bd9Sstevel@tonic-gate * and free free_blks pointer.
114*7c478bd9Sstevel@tonic-gate */
115*7c478bd9Sstevel@tonic-gate mutex_enter(&xblkcache->lock);
116*7c478bd9Sstevel@tonic-gate sfhme = xblkcache->free_blks;
117*7c478bd9Sstevel@tonic-gate xblkcache->free_blks = NULL;
118*7c478bd9Sstevel@tonic-gate mutex_exit(&xblkcache->lock);
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate while (sfhme != NULL) {
121*7c478bd9Sstevel@tonic-gate xblk = (struct xhat_hme_blk *)sfmmu_hmetohblk(sfhme);
122*7c478bd9Sstevel@tonic-gate ASSERT(xblk->xhat_hme_blk_misc.xhat_bit == 1);
123*7c478bd9Sstevel@tonic-gate sfhme = sfhme->hme_next;
124*7c478bd9Sstevel@tonic-gate kmem_cache_free(xblkcache->cache, xblk);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate /*
133*7c478bd9Sstevel@tonic-gate * Insert the xhat block (or, more precisely, the sf_hment)
134*7c478bd9Sstevel@tonic-gate * into page's p_mapping list.
135*7c478bd9Sstevel@tonic-gate */
136*7c478bd9Sstevel@tonic-gate pfn_t
xhat_insert_xhatblk(page_t * pp,struct xhat * xhat,void ** blk)137*7c478bd9Sstevel@tonic-gate xhat_insert_xhatblk(page_t *pp, struct xhat *xhat, void **blk)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate kmutex_t *pml;
140*7c478bd9Sstevel@tonic-gate pfn_t pfn;
141*7c478bd9Sstevel@tonic-gate struct xhat_hme_blk *xblk;
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate xblk = xhat_alloc_xhatblk(xhat);
146*7c478bd9Sstevel@tonic-gate if (xblk == NULL)
147*7c478bd9Sstevel@tonic-gate return (0);
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate /* Add a "user" to the XHAT */
150*7c478bd9Sstevel@tonic-gate xhat_hat_hold(xhat);
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate xblk->xhat_hme_blk_hat = xhat;
153*7c478bd9Sstevel@tonic-gate xblk->xhat_hme_blk_misc.xhat_bit = 1;
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate pml = sfmmu_mlist_enter(pp);
156*7c478bd9Sstevel@tonic-gate
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate /* Insert at the head of p_mapping list */
159*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_prev = NULL;
160*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_next = pp->p_mapping;
161*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_page = pp;
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate /* Only one tte per xhat_hme_blk, at least for now */
164*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_tte.tte_hmenum = 0;
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate if (pp->p_mapping) {
167*7c478bd9Sstevel@tonic-gate ((struct sf_hment *)(pp->p_mapping))->hme_prev =
168*7c478bd9Sstevel@tonic-gate &(xblk->xblk_hme[0]);
169*7c478bd9Sstevel@tonic-gate ASSERT(pp->p_share > 0);
170*7c478bd9Sstevel@tonic-gate } else {
171*7c478bd9Sstevel@tonic-gate /* EMPTY */
172*7c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0);
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate pp->p_mapping = &(xblk->xblk_hme[0]);
175*7c478bd9Sstevel@tonic-gate
176*7c478bd9Sstevel@tonic-gate /*
177*7c478bd9Sstevel@tonic-gate * Update number of mappings.
178*7c478bd9Sstevel@tonic-gate */
179*7c478bd9Sstevel@tonic-gate pp->p_share++;
180*7c478bd9Sstevel@tonic-gate pfn = pp->p_pagenum;
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate sfmmu_mlist_exit(pml);
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate *blk = XBLK2PROVBLK(xblk);
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate return (pfn);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate * mlist_locked indicates whether the mapping list
192*7c478bd9Sstevel@tonic-gate * is locked. If provider did not lock it himself, the
193*7c478bd9Sstevel@tonic-gate * only time it is locked in HAT layer is in
194*7c478bd9Sstevel@tonic-gate * hat_pageunload().
195*7c478bd9Sstevel@tonic-gate */
196*7c478bd9Sstevel@tonic-gate int
xhat_delete_xhatblk(void * blk,int mlist_locked)197*7c478bd9Sstevel@tonic-gate xhat_delete_xhatblk(void *blk, int mlist_locked)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate struct xhat_hme_blk *xblk = PROVBLK2XBLK(blk);
200*7c478bd9Sstevel@tonic-gate page_t *pp = xblk->xblk_hme[0].hme_page;
201*7c478bd9Sstevel@tonic-gate kmutex_t *pml;
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate ASSERT(pp != NULL);
205*7c478bd9Sstevel@tonic-gate ASSERT(pp->p_share > 0);
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate if (!mlist_locked)
208*7c478bd9Sstevel@tonic-gate pml = sfmmu_mlist_enter(pp);
209*7c478bd9Sstevel@tonic-gate else
210*7c478bd9Sstevel@tonic-gate ASSERT(sfmmu_mlist_held(pp));
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate pp->p_share--;
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate if (xblk->xblk_hme[0].hme_prev) {
215*7c478bd9Sstevel@tonic-gate ASSERT(pp->p_mapping != &(xblk->xblk_hme[0]));
216*7c478bd9Sstevel@tonic-gate ASSERT(xblk->xblk_hme[0].hme_prev->hme_page == pp);
217*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_prev->hme_next =
218*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_next;
219*7c478bd9Sstevel@tonic-gate } else {
220*7c478bd9Sstevel@tonic-gate ASSERT(pp->p_mapping == &(xblk->xblk_hme[0]));
221*7c478bd9Sstevel@tonic-gate pp->p_mapping = xblk->xblk_hme[0].hme_next;
222*7c478bd9Sstevel@tonic-gate ASSERT((pp->p_mapping == NULL) ?
223*7c478bd9Sstevel@tonic-gate (pp->p_share == 0) : 1);
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate if (xblk->xblk_hme->hme_next) {
227*7c478bd9Sstevel@tonic-gate ASSERT(xblk->xblk_hme[0].hme_next->hme_page == pp);
228*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_next->hme_prev =
229*7c478bd9Sstevel@tonic-gate xblk->xblk_hme[0].hme_prev;
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate
232*7c478bd9Sstevel@tonic-gate if (!mlist_locked)
233*7c478bd9Sstevel@tonic-gate sfmmu_mlist_exit(pml);
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate xhat_hat_rele(xblk->xhat_hme_blk_hat);
236*7c478bd9Sstevel@tonic-gate xhat_free_xhatblk(xblk);
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate return (0);
240*7c478bd9Sstevel@tonic-gate }
241