Print this page
2917 DTrace in a zone should have limited provider access
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/sparc/dtrace/sdt.c
+++ new/usr/src/uts/sparc/dtrace/sdt.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 +/*
27 + * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 + */
26 29
27 30 #include <sys/modctl.h>
28 31 #include <sys/sunddi.h>
29 32 #include <sys/dtrace.h>
30 33 #include <sys/kobj.h>
31 34 #include <sys/stat.h>
32 35 #include <sys/conf.h>
33 36 #include <vm/seg_kmem.h>
34 37 #include <sys/stack.h>
35 38 #include <sys/sdt_impl.h>
36 39
37 40 static dev_info_t *sdt_devi;
38 41
39 42 int sdt_verbose = 0;
40 43
41 44 #define SDT_REG_G0 0
42 45 #define SDT_REG_O0 8
43 46 #define SDT_REG_O1 9
44 47 #define SDT_REG_O2 10
45 48 #define SDT_REG_O3 11
46 49 #define SDT_REG_O4 12
47 50 #define SDT_REG_O5 13
48 51 #define SDT_REG_I0 24
49 52 #define SDT_REG_I1 25
50 53 #define SDT_REG_I2 26
51 54 #define SDT_REG_I3 27
52 55 #define SDT_REG_I4 28
53 56 #define SDT_REG_I5 29
54 57
55 58 #define SDT_SIMM13_MASK 0x1fff
56 59 #define SDT_SIMM13_MAX ((int32_t)0xfff)
57 60 #define SDT_CALL(from, to) (((uint32_t)1 << 30) | \
58 61 (((uintptr_t)(to) - (uintptr_t)(from) >> 2) & \
59 62 0x3fffffff))
60 63 #define SDT_SAVE (0x9de3a000 | (-SA(MINFRAME) & SDT_SIMM13_MASK))
61 64 #define SDT_RET 0x81c7e008
62 65 #define SDT_RESTORE 0x81e80000
63 66
64 67 #define SDT_OP_SETHI 0x1000000
65 68 #define SDT_OP_OR 0x80100000
66 69
67 70 #define SDT_FMT2_RD_SHIFT 25
68 71 #define SDT_IMM22_SHIFT 10
69 72 #define SDT_IMM22_MASK 0x3fffff
70 73 #define SDT_IMM10_MASK 0x3ff
71 74
72 75 #define SDT_FMT3_RD_SHIFT 25
73 76 #define SDT_FMT3_RS1_SHIFT 14
74 77 #define SDT_FMT3_RS2_SHIFT 0
75 78 #define SDT_FMT3_IMM (1 << 13)
76 79
77 80 #define SDT_MOV(rs, rd) \
78 81 (SDT_OP_OR | (SDT_REG_G0 << SDT_FMT3_RS1_SHIFT) | \
79 82 ((rs) << SDT_FMT3_RS2_SHIFT) | ((rd) << SDT_FMT3_RD_SHIFT))
80 83
81 84 #define SDT_ORLO(rs, val, rd) \
82 85 (SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
83 86 ((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_IMM10_MASK))
84 87
85 88 #define SDT_ORSIMM13(rs, val, rd) \
86 89 (SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
87 90 ((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_SIMM13_MASK))
88 91
89 92 #define SDT_SETHI(val, reg) \
90 93 (SDT_OP_SETHI | (reg << SDT_FMT2_RD_SHIFT) | \
91 94 ((val >> SDT_IMM22_SHIFT) & SDT_IMM22_MASK))
92 95
93 96 #define SDT_ENTRY_SIZE (11 * sizeof (uint32_t))
94 97
95 98 static void
96 99 sdt_initialize(sdt_probe_t *sdp, uint32_t **trampoline)
97 100 {
98 101 uint32_t *instr = *trampoline;
99 102
100 103 *instr++ = SDT_SAVE;
101 104
102 105 if (sdp->sdp_id > (uint32_t)SDT_SIMM13_MAX) {
103 106 *instr++ = SDT_SETHI(sdp->sdp_id, SDT_REG_O0);
104 107 *instr++ = SDT_ORLO(SDT_REG_O0, sdp->sdp_id, SDT_REG_O0);
105 108 } else {
106 109 *instr++ = SDT_ORSIMM13(SDT_REG_G0, sdp->sdp_id, SDT_REG_O0);
107 110 }
108 111
109 112 *instr++ = SDT_MOV(SDT_REG_I0, SDT_REG_O1);
110 113 *instr++ = SDT_MOV(SDT_REG_I1, SDT_REG_O2);
111 114 *instr++ = SDT_MOV(SDT_REG_I2, SDT_REG_O3);
112 115 *instr++ = SDT_MOV(SDT_REG_I3, SDT_REG_O4);
113 116 *instr = SDT_CALL(instr, dtrace_probe);
114 117 instr++;
115 118 *instr++ = SDT_MOV(SDT_REG_I4, SDT_REG_O5);
116 119
117 120 *instr++ = SDT_RET;
118 121 *instr++ = SDT_RESTORE;
119 122 *trampoline = instr;
120 123 }
121 124
122 125 /*ARGSUSED*/
123 126 static void
124 127 sdt_provide_module(void *arg, struct modctl *ctl)
125 128 {
126 129 struct module *mp = ctl->mod_mp;
127 130 char *modname = ctl->mod_modname;
128 131 int primary, nprobes = 0;
129 132 sdt_probedesc_t *sdpd;
130 133 sdt_probe_t *sdp, *old;
131 134 uint32_t *tab;
132 135 sdt_provider_t *prov;
133 136 int len;
134 137
135 138 /*
136 139 * One for all, and all for one: if we haven't yet registered all of
137 140 * our providers, we'll refuse to provide anything.
138 141 */
139 142 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
140 143 if (prov->sdtp_id == DTRACE_PROVNONE)
141 144 return;
142 145 }
143 146
144 147 if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
145 148 return;
146 149
147 150 kobj_textwin_alloc(mp);
148 151
149 152 /*
150 153 * Hack to identify unix/genunix/krtld.
151 154 */
152 155 primary = vmem_contains(heap_arena, (void *)ctl,
153 156 sizeof (struct modctl)) == 0;
154 157
155 158 /*
156 159 * If there hasn't been an sdt table allocated, we'll do so now.
157 160 */
158 161 if (mp->sdt_tab == NULL) {
159 162 for (; sdpd != NULL; sdpd = sdpd->sdpd_next) {
160 163 nprobes++;
161 164 }
162 165
163 166 /*
164 167 * We could (should?) determine precisely the size of the
165 168 * table -- but a reasonable maximum will suffice.
166 169 */
167 170 mp->sdt_size = nprobes * SDT_ENTRY_SIZE;
168 171 mp->sdt_tab = kobj_texthole_alloc(mp->text, mp->sdt_size);
169 172
170 173 if (mp->sdt_tab == NULL) {
171 174 cmn_err(CE_WARN, "couldn't allocate SDT table "
172 175 "for module %s", modname);
173 176 return;
174 177 }
175 178 }
176 179
177 180 tab = (uint32_t *)mp->sdt_tab;
178 181
179 182 for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
180 183 char *name = sdpd->sdpd_name, *func, *nname;
181 184 int i, j;
182 185 sdt_provider_t *prov;
183 186 ulong_t offs;
184 187 dtrace_id_t id;
185 188
186 189 for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
187 190 char *prefix = prov->sdtp_prefix;
188 191
189 192 if (strncmp(name, prefix, strlen(prefix)) == 0) {
190 193 name += strlen(prefix);
191 194 break;
192 195 }
193 196 }
194 197
195 198 nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
196 199
197 200 for (i = 0, j = 0; name[j] != '\0'; i++) {
198 201 if (name[j] == '_' && name[j + 1] == '_') {
199 202 nname[i] = '-';
200 203 j += 2;
201 204 } else {
202 205 nname[i] = name[j++];
203 206 }
204 207 }
205 208
206 209 nname[i] = '\0';
207 210
208 211 sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
209 212 sdp->sdp_loadcnt = ctl->mod_loadcnt;
210 213 sdp->sdp_primary = primary;
211 214 sdp->sdp_ctl = ctl;
212 215 sdp->sdp_name = nname;
213 216 sdp->sdp_namelen = len;
214 217 sdp->sdp_provider = prov;
215 218
216 219 func = kobj_searchsym(mp, sdpd->sdpd_offset +
217 220 (uintptr_t)mp->text, &offs);
218 221
219 222 if (func == NULL)
220 223 func = "<unknown>";
221 224
222 225 /*
223 226 * We have our provider. Now create the probe.
224 227 */
225 228 if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
226 229 func, nname)) != DTRACE_IDNONE) {
227 230 old = dtrace_probe_arg(prov->sdtp_id, id);
228 231 ASSERT(old != NULL);
229 232
230 233 sdp->sdp_next = old->sdp_next;
231 234 sdp->sdp_id = id;
232 235 old->sdp_next = sdp;
233 236 } else {
234 237 sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
235 238 modname, func, nname, 1, sdp);
236 239
237 240 mp->sdt_nprobes++;
238 241 }
239 242
240 243 sdp->sdp_patchval = SDT_CALL((uintptr_t)mp->text +
241 244 sdpd->sdpd_offset, tab);
242 245 sdp->sdp_patchpoint = (uint32_t *)((uintptr_t)mp->textwin +
243 246 sdpd->sdpd_offset);
244 247 sdp->sdp_savedval = *sdp->sdp_patchpoint;
245 248 sdt_initialize(sdp, &tab);
246 249 }
247 250 }
248 251
249 252 /*ARGSUSED*/
250 253 static void
251 254 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
252 255 {
253 256 sdt_probe_t *sdp = parg, *old;
254 257 struct modctl *ctl = sdp->sdp_ctl;
255 258
256 259 if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
257 260 if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
258 261 ctl->mod_loaded) || sdp->sdp_primary) {
259 262 ((struct module *)(ctl->mod_mp))->sdt_nprobes--;
260 263 }
261 264 }
262 265
263 266 while (sdp != NULL) {
264 267 old = sdp;
265 268 kmem_free(sdp->sdp_name, sdp->sdp_namelen);
266 269 sdp = sdp->sdp_next;
267 270 kmem_free(old, sizeof (sdt_probe_t));
268 271 }
269 272 }
270 273
271 274 /*ARGSUSED*/
272 275 static int
273 276 sdt_enable(void *arg, dtrace_id_t id, void *parg)
274 277 {
275 278 sdt_probe_t *sdp = parg;
276 279 struct modctl *ctl = sdp->sdp_ctl;
277 280
278 281 ctl->mod_nenabled++;
279 282
280 283 /*
281 284 * If this module has disappeared since we discovered its probes,
282 285 * refuse to enable it.
283 286 */
284 287 if (!sdp->sdp_primary && !ctl->mod_loaded) {
285 288 if (sdt_verbose) {
286 289 cmn_err(CE_NOTE, "sdt is failing for probe %s "
287 290 "(module %s unloaded)",
288 291 sdp->sdp_name, ctl->mod_modname);
289 292 }
290 293 goto err;
291 294 }
292 295
293 296 /*
294 297 * Now check that our modctl has the expected load count. If it
295 298 * doesn't, this module must have been unloaded and reloaded -- and
296 299 * we're not going to touch it.
297 300 */
298 301 if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
299 302 if (sdt_verbose) {
300 303 cmn_err(CE_NOTE, "sdt is failing for probe %s "
301 304 "(module %s reloaded)",
302 305 sdp->sdp_name, ctl->mod_modname);
303 306 }
304 307 goto err;
305 308 }
306 309
307 310 while (sdp != NULL) {
308 311 *sdp->sdp_patchpoint = sdp->sdp_patchval;
309 312 sdp = sdp->sdp_next;
310 313 }
311 314
312 315 err:
313 316 return (0);
314 317 }
315 318
316 319 /*ARGSUSED*/
317 320 static void
318 321 sdt_disable(void *arg, dtrace_id_t id, void *parg)
319 322 {
320 323 sdt_probe_t *sdp = parg;
321 324 struct modctl *ctl = sdp->sdp_ctl;
322 325
323 326 ASSERT(ctl->mod_nenabled > 0);
324 327 ctl->mod_nenabled--;
325 328
326 329 if ((!sdp->sdp_primary && !ctl->mod_loaded) ||
327 330 (ctl->mod_loadcnt != sdp->sdp_loadcnt))
328 331 goto err;
329 332
330 333 while (sdp != NULL) {
331 334 *sdp->sdp_patchpoint = sdp->sdp_savedval;
332 335 sdp = sdp->sdp_next;
333 336 }
334 337
335 338 err:
336 339 ;
337 340 }
338 341
339 342 static dtrace_pops_t sdt_pops = {
340 343 NULL,
341 344 sdt_provide_module,
342 345 sdt_enable,
343 346 sdt_disable,
344 347 NULL,
345 348 NULL,
346 349 sdt_getargdesc,
347 350 NULL,
348 351 NULL,
349 352 sdt_destroy
350 353 };
351 354
352 355 static int
353 356 sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
354 357 {
355 358 sdt_provider_t *prov;
356 359
357 360 switch (cmd) {
358 361 case DDI_ATTACH:
359 362 break;
360 363 case DDI_RESUME:
361 364 return (DDI_SUCCESS);
362 365 default:
363 366 return (DDI_FAILURE);
364 367 }
365 368
↓ open down ↓ |
330 lines elided |
↑ open up ↑ |
366 369 if (ddi_create_minor_node(devi, "sdt", S_IFCHR, 0,
367 370 DDI_PSEUDO, NULL) == DDI_FAILURE) {
368 371 ddi_remove_minor_node(devi, NULL);
369 372 return (DDI_FAILURE);
370 373 }
371 374
372 375 ddi_report_dev(devi);
373 376 sdt_devi = devi;
374 377
375 378 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
379 + uint32_t priv;
380 +
381 + if (prov->sdtp_priv == DTRACE_PRIV_NONE) {
382 + priv = DTRACE_PRIV_KERNEL;
383 + sdt_pops.dtps_mode = NULL;
384 + } else {
385 + priv = prov->sdtp_priv;
386 + ASSERT(priv == DTRACE_PRIV_USER);
387 + sdt_pops.dtps_mode = sdt_mode;
388 + }
389 +
376 390 if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
377 - DTRACE_PRIV_KERNEL, NULL,
378 - &sdt_pops, prov, &prov->sdtp_id) != 0) {
391 + priv, NULL, &sdt_pops, prov, &prov->sdtp_id) != 0) {
379 392 cmn_err(CE_WARN, "failed to register sdt provider %s",
380 393 prov->sdtp_name);
381 394 }
382 395 }
383 396
384 397 return (DDI_SUCCESS);
385 398 }
386 399
387 400 static int
388 401 sdt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
389 402 {
390 403 sdt_provider_t *prov;
391 404
392 405 switch (cmd) {
393 406 case DDI_DETACH:
394 407 break;
395 408 case DDI_SUSPEND:
396 409 return (DDI_SUCCESS);
397 410 default:
398 411 return (DDI_FAILURE);
399 412 }
400 413
401 414 for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
402 415 if (prov->sdtp_id != DTRACE_PROVNONE) {
403 416 if (dtrace_unregister(prov->sdtp_id) != 0)
404 417 return (DDI_FAILURE);
405 418 prov->sdtp_id = DTRACE_PROVNONE;
406 419 }
407 420 }
408 421
409 422 ddi_remove_minor_node(devi, NULL);
410 423 return (DDI_SUCCESS);
411 424 }
412 425
413 426 /*ARGSUSED*/
414 427 static int
415 428 sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
416 429 {
417 430 int error;
418 431
419 432 switch (infocmd) {
420 433 case DDI_INFO_DEVT2DEVINFO:
421 434 *result = (void *)sdt_devi;
422 435 error = DDI_SUCCESS;
423 436 break;
424 437 case DDI_INFO_DEVT2INSTANCE:
425 438 *result = (void *)0;
426 439 error = DDI_SUCCESS;
427 440 break;
428 441 default:
429 442 error = DDI_FAILURE;
430 443 }
431 444 return (error);
432 445 }
433 446
434 447 /*ARGSUSED*/
435 448 static int
436 449 sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
437 450 {
438 451 return (0);
439 452 }
440 453
441 454 static struct cb_ops sdt_cb_ops = {
442 455 sdt_open, /* open */
443 456 nodev, /* close */
444 457 nulldev, /* strategy */
445 458 nulldev, /* print */
446 459 nodev, /* dump */
447 460 nodev, /* read */
448 461 nodev, /* write */
449 462 nodev, /* ioctl */
450 463 nodev, /* devmap */
451 464 nodev, /* mmap */
452 465 nodev, /* segmap */
453 466 nochpoll, /* poll */
454 467 ddi_prop_op, /* cb_prop_op */
455 468 0, /* streamtab */
456 469 D_NEW | D_MP /* Driver compatibility flag */
457 470 };
458 471
459 472 static struct dev_ops sdt_ops = {
460 473 DEVO_REV, /* devo_rev, */
461 474 0, /* refcnt */
462 475 sdt_info, /* get_dev_info */
463 476 nulldev, /* identify */
464 477 nulldev, /* probe */
465 478 sdt_attach, /* attach */
466 479 sdt_detach, /* detach */
467 480 nodev, /* reset */
468 481 &sdt_cb_ops, /* driver operations */
469 482 NULL, /* bus operations */
470 483 nodev, /* dev power */
471 484 ddi_quiesce_not_needed, /* quiesce */
472 485 };
473 486
474 487 /*
475 488 * Module linkage information for the kernel.
476 489 */
477 490 static struct modldrv modldrv = {
478 491 &mod_driverops, /* module type (this is a pseudo driver) */
479 492 "Statically Defined Tracing", /* name of module */
480 493 &sdt_ops, /* driver ops */
481 494 };
482 495
483 496 static struct modlinkage modlinkage = {
484 497 MODREV_1,
485 498 (void *)&modldrv,
486 499 NULL
487 500 };
488 501
489 502 int
490 503 _init(void)
491 504 {
492 505 return (mod_install(&modlinkage));
493 506 }
494 507
495 508 int
496 509 _info(struct modinfo *modinfop)
497 510 {
498 511 return (mod_info(&modlinkage, modinfop));
499 512 }
500 513
501 514 int
502 515 _fini(void)
503 516 {
504 517 return (mod_remove(&modlinkage));
505 518 }
↓ open down ↓ |
117 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX