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