xref: /illumos-gate/usr/src/uts/intel/dtrace/sdt.c (revision 6c10f5d00ef774bc0c97c0ef38374f86a4ee2007)
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 2008 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/frame.h>
37 #include <sys/dtrace_impl.h>
38 #include <sys/cmn_err.h>
39 #include <sys/sysmacros.h>
40 #include <sys/privregs.h>
41 #include <sys/sdt_impl.h>
42 
43 #define	SDT_PATCHVAL	0xf0
44 #define	SDT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & sdt_probetab_mask)
45 #define	SDT_PROBETAB_SIZE	0x1000		/* 4k entries -- 16K total */
46 
47 static dev_info_t		*sdt_devi;
48 static int			sdt_verbose = 0;
49 static sdt_probe_t		**sdt_probetab;
50 static int			sdt_probetab_size;
51 static int			sdt_probetab_mask;
52 
53 /*ARGSUSED*/
54 static int
55 sdt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
56 {
57 	uintptr_t stack0, stack1, stack2, stack3, stack4;
58 	int i = 0;
59 	sdt_probe_t *sdt = sdt_probetab[SDT_ADDR2NDX(addr)];
60 
61 #ifdef __amd64
62 	/*
63 	 * On amd64, stack[0] contains the dereferenced stack pointer,
64 	 * stack[1] contains savfp, stack[2] contains savpc.  We want
65 	 * to step over these entries.
66 	 */
67 	i += 3;
68 #endif
69 
70 	for (; sdt != NULL; sdt = sdt->sdp_hashnext) {
71 		if ((uintptr_t)sdt->sdp_patchpoint == addr) {
72 			/*
73 			 * When accessing the arguments on the stack, we must
74 			 * protect against accessing beyond the stack.  We can
75 			 * safely set NOFAULT here -- we know that interrupts
76 			 * are already disabled.
77 			 */
78 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
79 			stack0 = stack[i++];
80 			stack1 = stack[i++];
81 			stack2 = stack[i++];
82 			stack3 = stack[i++];
83 			stack4 = stack[i++];
84 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
85 			    CPU_DTRACE_BADADDR);
86 
87 			dtrace_probe(sdt->sdp_id, stack0, stack1,
88 			    stack2, stack3, stack4);
89 
90 			return (DTRACE_INVOP_NOP);
91 		}
92 	}
93 
94 	return (0);
95 }
96 
97 /*ARGSUSED*/
98 static void
99 sdt_provide_module(void *arg, struct modctl *ctl)
100 {
101 	struct module *mp = ctl->mod_mp;
102 	char *modname = ctl->mod_modname;
103 	sdt_probedesc_t *sdpd;
104 	sdt_probe_t *sdp, *old;
105 	sdt_provider_t *prov;
106 	int len;
107 
108 	/*
109 	 * One for all, and all for one:  if we haven't yet registered all of
110 	 * our providers, we'll refuse to provide anything.
111 	 */
112 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
113 		if (prov->sdtp_id == DTRACE_PROVNONE)
114 			return;
115 	}
116 
117 	if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
118 		return;
119 
120 	for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
121 		char *name = sdpd->sdpd_name, *func, *nname;
122 		int i, j;
123 		sdt_provider_t *prov;
124 		ulong_t offs;
125 		dtrace_id_t id;
126 
127 		for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
128 			char *prefix = prov->sdtp_prefix;
129 
130 			if (strncmp(name, prefix, strlen(prefix)) == 0) {
131 				name += strlen(prefix);
132 				break;
133 			}
134 		}
135 
136 		nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
137 
138 		for (i = 0, j = 0; name[j] != '\0'; i++) {
139 			if (name[j] == '_' && name[j + 1] == '_') {
140 				nname[i] = '-';
141 				j += 2;
142 			} else {
143 				nname[i] = name[j++];
144 			}
145 		}
146 
147 		nname[i] = '\0';
148 
149 		sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
150 		sdp->sdp_loadcnt = ctl->mod_loadcnt;
151 		sdp->sdp_ctl = ctl;
152 		sdp->sdp_name = nname;
153 		sdp->sdp_namelen = len;
154 		sdp->sdp_provider = prov;
155 
156 		func = kobj_searchsym(mp, sdpd->sdpd_offset, &offs);
157 
158 		if (func == NULL)
159 			func = "<unknown>";
160 
161 		/*
162 		 * We have our provider.  Now create the probe.
163 		 */
164 		if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
165 		    func, nname)) != DTRACE_IDNONE) {
166 			old = dtrace_probe_arg(prov->sdtp_id, id);
167 			ASSERT(old != NULL);
168 
169 			sdp->sdp_next = old->sdp_next;
170 			sdp->sdp_id = id;
171 			old->sdp_next = sdp;
172 		} else {
173 			sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
174 			    modname, func, nname, 3, sdp);
175 
176 			mp->sdt_nprobes++;
177 		}
178 
179 		sdp->sdp_hashnext =
180 		    sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)];
181 		sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp;
182 
183 		sdp->sdp_patchval = SDT_PATCHVAL;
184 		sdp->sdp_patchpoint = (uint8_t *)sdpd->sdpd_offset;
185 		sdp->sdp_savedval = *sdp->sdp_patchpoint;
186 	}
187 }
188 
189 /*ARGSUSED*/
190 static void
191 sdt_destroy(void *arg, dtrace_id_t id, void *parg)
192 {
193 	sdt_probe_t *sdp = parg, *old, *last, *hash;
194 	struct modctl *ctl = sdp->sdp_ctl;
195 	int ndx;
196 
197 	if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
198 		if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
199 		    ctl->mod_loaded)) {
200 			((struct module *)(ctl->mod_mp))->sdt_nprobes--;
201 		}
202 	}
203 
204 	while (sdp != NULL) {
205 		old = sdp;
206 
207 		/*
208 		 * Now we need to remove this probe from the sdt_probetab.
209 		 */
210 		ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint);
211 		last = NULL;
212 		hash = sdt_probetab[ndx];
213 
214 		while (hash != sdp) {
215 			ASSERT(hash != NULL);
216 			last = hash;
217 			hash = hash->sdp_hashnext;
218 		}
219 
220 		if (last != NULL) {
221 			last->sdp_hashnext = sdp->sdp_hashnext;
222 		} else {
223 			sdt_probetab[ndx] = sdp->sdp_hashnext;
224 		}
225 
226 		kmem_free(sdp->sdp_name, sdp->sdp_namelen);
227 		sdp = sdp->sdp_next;
228 		kmem_free(old, sizeof (sdt_probe_t));
229 	}
230 }
231 
232 /*ARGSUSED*/
233 static void
234 sdt_enable(void *arg, dtrace_id_t id, void *parg)
235 {
236 	sdt_probe_t *sdp = parg;
237 	struct modctl *ctl = sdp->sdp_ctl;
238 
239 	ctl->mod_nenabled++;
240 
241 	/*
242 	 * If this module has disappeared since we discovered its probes,
243 	 * refuse to enable it.
244 	 */
245 	if (!ctl->mod_loaded) {
246 		if (sdt_verbose) {
247 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
248 			    "(module %s unloaded)",
249 			    sdp->sdp_name, ctl->mod_modname);
250 		}
251 		goto err;
252 	}
253 
254 	/*
255 	 * Now check that our modctl has the expected load count.  If it
256 	 * doesn't, this module must have been unloaded and reloaded -- and
257 	 * we're not going to touch it.
258 	 */
259 	if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
260 		if (sdt_verbose) {
261 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
262 			    "(module %s reloaded)",
263 			    sdp->sdp_name, ctl->mod_modname);
264 		}
265 		goto err;
266 	}
267 
268 	while (sdp != NULL) {
269 		*sdp->sdp_patchpoint = sdp->sdp_patchval;
270 		sdp = sdp->sdp_next;
271 	}
272 err:
273 	;
274 }
275 
276 /*ARGSUSED*/
277 static void
278 sdt_disable(void *arg, dtrace_id_t id, void *parg)
279 {
280 	sdt_probe_t *sdp = parg;
281 	struct modctl *ctl = sdp->sdp_ctl;
282 
283 	ctl->mod_nenabled--;
284 
285 	if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt)
286 		goto err;
287 
288 	while (sdp != NULL) {
289 		*sdp->sdp_patchpoint = sdp->sdp_savedval;
290 		sdp = sdp->sdp_next;
291 	}
292 
293 err:
294 	;
295 }
296 
297 /*ARGSUSED*/
298 uint64_t
299 sdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
300 {
301 	uintptr_t val;
302 	struct frame *fp = (struct frame *)dtrace_getfp();
303 	uintptr_t *stack;
304 	int i;
305 #if defined(__amd64)
306 	/*
307 	 * A total of 6 arguments are passed via registers; any argument with
308 	 * index of 5 or lower is therefore in a register.
309 	 */
310 	int inreg = 5;
311 #endif
312 
313 	for (i = 1; i <= aframes; i++) {
314 		fp = (struct frame *)(fp->fr_savfp);
315 
316 		if (fp->fr_savpc == (pc_t)dtrace_invop_callsite) {
317 #if !defined(__amd64)
318 			/*
319 			 * If we pass through the invalid op handler, we will
320 			 * use the pointer that it passed to the stack as the
321 			 * second argument to dtrace_invop() as the pointer to
322 			 * the stack.
323 			 */
324 			stack = ((uintptr_t **)&fp[1])[1];
325 #else
326 			/*
327 			 * In the case of amd64, we will use the pointer to the
328 			 * regs structure that was pushed when we took the
329 			 * trap.  To get this structure, we must increment
330 			 * beyond the frame structure.  If the argument that
331 			 * we're seeking is passed on the stack, we'll pull
332 			 * the true stack pointer out of the saved registers
333 			 * and decrement our argument by the number of
334 			 * arguments passed in registers; if the argument
335 			 * we're seeking is passed in regsiters, we can just
336 			 * load it directly.
337 			 */
338 			struct regs *rp = (struct regs *)&fp[1];
339 
340 			if (argno <= inreg) {
341 				stack = (uintptr_t *)&rp->r_rdi;
342 			} else {
343 				stack = (uintptr_t *)(rp->r_rsp);
344 				argno -= inreg;
345 			}
346 #endif
347 			goto load;
348 		}
349 	}
350 
351 	/*
352 	 * We know that we did not come through a trap to get into
353 	 * dtrace_probe() -- the provider simply called dtrace_probe()
354 	 * directly.  As this is the case, we need to shift the argument
355 	 * that we're looking for:  the probe ID is the first argument to
356 	 * dtrace_probe(), so the argument n will actually be found where
357 	 * one would expect to find argument (n + 1).
358 	 */
359 	argno++;
360 
361 #if defined(__amd64)
362 	if (argno <= inreg) {
363 		/*
364 		 * This shouldn't happen.  If the argument is passed in a
365 		 * register then it should have been, well, passed in a
366 		 * register...
367 		 */
368 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
369 		return (0);
370 	}
371 
372 	argno -= (inreg + 1);
373 #endif
374 	stack = (uintptr_t *)&fp[1];
375 
376 load:
377 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
378 	val = stack[argno];
379 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
380 
381 	return (val);
382 }
383 
384 static dtrace_pops_t sdt_pops = {
385 	NULL,
386 	sdt_provide_module,
387 	sdt_enable,
388 	sdt_disable,
389 	NULL,
390 	NULL,
391 	sdt_getargdesc,
392 	sdt_getarg,
393 	NULL,
394 	sdt_destroy
395 };
396 
397 /*ARGSUSED*/
398 static int
399 sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
400 {
401 	sdt_provider_t *prov;
402 
403 	if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
404 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
405 		cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
406 		ddi_remove_minor_node(devi, NULL);
407 		return (DDI_FAILURE);
408 	}
409 
410 	ddi_report_dev(devi);
411 	sdt_devi = devi;
412 
413 	if (sdt_probetab_size == 0)
414 		sdt_probetab_size = SDT_PROBETAB_SIZE;
415 
416 	sdt_probetab_mask = sdt_probetab_size - 1;
417 	sdt_probetab =
418 	    kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
419 	dtrace_invop_add(sdt_invop);
420 
421 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
422 		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
423 		    DTRACE_PRIV_KERNEL, NULL,
424 		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
425 			cmn_err(CE_WARN, "failed to register sdt provider %s",
426 			    prov->sdtp_name);
427 		}
428 	}
429 
430 	return (DDI_SUCCESS);
431 }
432 
433 /*ARGSUSED*/
434 static int
435 sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
436 {
437 	sdt_provider_t *prov;
438 
439 	switch (cmd) {
440 	case DDI_DETACH:
441 		break;
442 
443 	case DDI_SUSPEND:
444 		return (DDI_SUCCESS);
445 
446 	default:
447 		return (DDI_FAILURE);
448 	}
449 
450 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
451 		if (prov->sdtp_id != DTRACE_PROVNONE) {
452 			if (dtrace_unregister(prov->sdtp_id) != 0)
453 				return (DDI_FAILURE);
454 
455 			prov->sdtp_id = DTRACE_PROVNONE;
456 		}
457 	}
458 
459 	dtrace_invop_remove(sdt_invop);
460 	kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
461 
462 	return (DDI_SUCCESS);
463 }
464 
465 /*ARGSUSED*/
466 static int
467 sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
468 {
469 	int error;
470 
471 	switch (infocmd) {
472 	case DDI_INFO_DEVT2DEVINFO:
473 		*result = (void *)sdt_devi;
474 		error = DDI_SUCCESS;
475 		break;
476 	case DDI_INFO_DEVT2INSTANCE:
477 		*result = (void *)0;
478 		error = DDI_SUCCESS;
479 		break;
480 	default:
481 		error = DDI_FAILURE;
482 	}
483 	return (error);
484 }
485 
486 /*ARGSUSED*/
487 static int
488 sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
489 {
490 	return (0);
491 }
492 
493 static struct cb_ops sdt_cb_ops = {
494 	sdt_open,		/* open */
495 	nodev,			/* close */
496 	nulldev,		/* strategy */
497 	nulldev,		/* print */
498 	nodev,			/* dump */
499 	nodev,			/* read */
500 	nodev,			/* write */
501 	nodev,			/* ioctl */
502 	nodev,			/* devmap */
503 	nodev,			/* mmap */
504 	nodev,			/* segmap */
505 	nochpoll,		/* poll */
506 	ddi_prop_op,		/* cb_prop_op */
507 	0,			/* streamtab  */
508 	D_NEW | D_MP		/* Driver compatibility flag */
509 };
510 
511 static struct dev_ops sdt_ops = {
512 	DEVO_REV,		/* devo_rev, */
513 	0,			/* refcnt  */
514 	sdt_info,		/* get_dev_info */
515 	nulldev,		/* identify */
516 	nulldev,		/* probe */
517 	sdt_attach,		/* attach */
518 	sdt_detach,		/* detach */
519 	nodev,			/* reset */
520 	&sdt_cb_ops,		/* driver operations */
521 	NULL,			/* bus operations */
522 	nodev			/* dev power */
523 };
524 
525 /*
526  * Module linkage information for the kernel.
527  */
528 static struct modldrv modldrv = {
529 	&mod_driverops,		/* module type (this is a pseudo driver) */
530 	"Statically Defined Tracing",	/* name of module */
531 	&sdt_ops,		/* driver ops */
532 };
533 
534 static struct modlinkage modlinkage = {
535 	MODREV_1,
536 	(void *)&modldrv,
537 	NULL
538 };
539 
540 int
541 _init(void)
542 {
543 	return (mod_install(&modlinkage));
544 }
545 
546 int
547 _info(struct modinfo *modinfop)
548 {
549 	return (mod_info(&modlinkage, modinfop));
550 }
551 
552 int
553 _fini(void)
554 {
555 	return (mod_remove(&modlinkage));
556 }
557