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