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