xref: /titanic_51/usr/src/uts/intel/dtrace/sdt.c (revision e61a481d2b9ca0c0f387e11b15dcabfc982bb464)
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 *)((uintptr_t)&fp[1] +
339 			    sizeof (uintptr_t));
340 
341 			if (argno <= inreg) {
342 				stack = (uintptr_t *)&rp->r_rdi;
343 			} else {
344 				stack = (uintptr_t *)(rp->r_rsp);
345 				argno -= (inreg + 1);
346 			}
347 #endif
348 			goto load;
349 		}
350 	}
351 
352 	/*
353 	 * We know that we did not come through a trap to get into
354 	 * dtrace_probe() -- the provider simply called dtrace_probe()
355 	 * directly.  As this is the case, we need to shift the argument
356 	 * that we're looking for:  the probe ID is the first argument to
357 	 * dtrace_probe(), so the argument n will actually be found where
358 	 * one would expect to find argument (n + 1).
359 	 */
360 	argno++;
361 
362 #if defined(__amd64)
363 	if (argno <= inreg) {
364 		/*
365 		 * This shouldn't happen.  If the argument is passed in a
366 		 * register then it should have been, well, passed in a
367 		 * register...
368 		 */
369 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
370 		return (0);
371 	}
372 
373 	argno -= (inreg + 1);
374 #endif
375 	stack = (uintptr_t *)&fp[1];
376 
377 load:
378 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
379 	val = stack[argno];
380 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
381 
382 	return (val);
383 }
384 
385 static dtrace_pops_t sdt_pops = {
386 	NULL,
387 	sdt_provide_module,
388 	sdt_enable,
389 	sdt_disable,
390 	NULL,
391 	NULL,
392 	sdt_getargdesc,
393 	sdt_getarg,
394 	NULL,
395 	sdt_destroy
396 };
397 
398 /*ARGSUSED*/
399 static int
400 sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
401 {
402 	sdt_provider_t *prov;
403 
404 	if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
405 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
406 		cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
407 		ddi_remove_minor_node(devi, NULL);
408 		return (DDI_FAILURE);
409 	}
410 
411 	ddi_report_dev(devi);
412 	sdt_devi = devi;
413 
414 	if (sdt_probetab_size == 0)
415 		sdt_probetab_size = SDT_PROBETAB_SIZE;
416 
417 	sdt_probetab_mask = sdt_probetab_size - 1;
418 	sdt_probetab =
419 	    kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
420 	dtrace_invop_add(sdt_invop);
421 
422 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
423 		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
424 		    DTRACE_PRIV_KERNEL, NULL,
425 		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
426 			cmn_err(CE_WARN, "failed to register sdt provider %s",
427 			    prov->sdtp_name);
428 		}
429 	}
430 
431 	return (DDI_SUCCESS);
432 }
433 
434 /*ARGSUSED*/
435 static int
436 sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
437 {
438 	sdt_provider_t *prov;
439 
440 	switch (cmd) {
441 	case DDI_DETACH:
442 		break;
443 
444 	case DDI_SUSPEND:
445 		return (DDI_SUCCESS);
446 
447 	default:
448 		return (DDI_FAILURE);
449 	}
450 
451 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
452 		if (prov->sdtp_id != DTRACE_PROVNONE) {
453 			if (dtrace_unregister(prov->sdtp_id) != 0)
454 				return (DDI_FAILURE);
455 
456 			prov->sdtp_id = DTRACE_PROVNONE;
457 		}
458 	}
459 
460 	dtrace_invop_remove(sdt_invop);
461 	kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
462 
463 	return (DDI_SUCCESS);
464 }
465 
466 /*ARGSUSED*/
467 static int
468 sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
469 {
470 	int error;
471 
472 	switch (infocmd) {
473 	case DDI_INFO_DEVT2DEVINFO:
474 		*result = (void *)sdt_devi;
475 		error = DDI_SUCCESS;
476 		break;
477 	case DDI_INFO_DEVT2INSTANCE:
478 		*result = (void *)0;
479 		error = DDI_SUCCESS;
480 		break;
481 	default:
482 		error = DDI_FAILURE;
483 	}
484 	return (error);
485 }
486 
487 /*ARGSUSED*/
488 static int
489 sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
490 {
491 	return (0);
492 }
493 
494 static struct cb_ops sdt_cb_ops = {
495 	sdt_open,		/* open */
496 	nodev,			/* close */
497 	nulldev,		/* strategy */
498 	nulldev,		/* print */
499 	nodev,			/* dump */
500 	nodev,			/* read */
501 	nodev,			/* write */
502 	nodev,			/* ioctl */
503 	nodev,			/* devmap */
504 	nodev,			/* mmap */
505 	nodev,			/* segmap */
506 	nochpoll,		/* poll */
507 	ddi_prop_op,		/* cb_prop_op */
508 	0,			/* streamtab  */
509 	D_NEW | D_MP		/* Driver compatibility flag */
510 };
511 
512 static struct dev_ops sdt_ops = {
513 	DEVO_REV,		/* devo_rev, */
514 	0,			/* refcnt  */
515 	sdt_info,		/* get_dev_info */
516 	nulldev,		/* identify */
517 	nulldev,		/* probe */
518 	sdt_attach,		/* attach */
519 	sdt_detach,		/* detach */
520 	nodev,			/* reset */
521 	&sdt_cb_ops,		/* driver operations */
522 	NULL,			/* bus operations */
523 	nodev			/* dev power */
524 };
525 
526 /*
527  * Module linkage information for the kernel.
528  */
529 static struct modldrv modldrv = {
530 	&mod_driverops,		/* module type (this is a pseudo driver) */
531 	"Statically Defined Tracing",	/* name of module */
532 	&sdt_ops,		/* driver ops */
533 };
534 
535 static struct modlinkage modlinkage = {
536 	MODREV_1,
537 	(void *)&modldrv,
538 	NULL
539 };
540 
541 int
542 _init(void)
543 {
544 	return (mod_install(&modlinkage));
545 }
546 
547 int
548 _info(struct modinfo *modinfop)
549 {
550 	return (mod_info(&modlinkage, modinfop));
551 }
552 
553 int
554 _fini(void)
555 {
556 	return (mod_remove(&modlinkage));
557 }
558