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