xref: /titanic_41/usr/src/uts/intel/dtrace/sdt.c (revision 79d2f1e1d69dd8621f962e48584f1e2434b3c837)
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.  When using this stack, we must step
323 			 * beyond the EIP/RIP that was pushed when the trap was
324 			 * taken -- hence the "+ 1" below.
325 			 */
326 			stack = ((uintptr_t **)&fp[1])[1];
327 #else
328 			/*
329 			 * In the case of amd64, we will use the pointer to the
330 			 * regs structure that was pushed when we took the
331 			 * trap.  To get this structure, we must increment
332 			 * beyond the frame structure, and then again beyond
333 			 * the calling RIP stored in dtrace_invop().  If the
334 			 * argument that we're seeking is passed on the stack,
335 			 * we'll pull the true stack pointer out of the saved
336 			 * registers and decrement our argument by the number
337 			 * of arguments passed in registers; if the argument
338 			 * we're seeking is passed in regsiters, we can just
339 			 * load it directly.
340 			 */
341 			struct regs *rp = (struct regs *)((uintptr_t)&fp[1] +
342 			    sizeof (uintptr_t));
343 
344 			if (argno <= inreg) {
345 				stack = (uintptr_t *)&rp->r_rdi;
346 			} else {
347 				stack = (uintptr_t *)(rp->r_rsp);
348 				argno -= inreg;
349 			}
350 #endif
351 			goto load;
352 		}
353 	}
354 
355 load:
356 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
357 	val = stack[argno];
358 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
359 
360 	return (val);
361 }
362 
363 static dtrace_pops_t sdt_pops = {
364 	NULL,
365 	sdt_provide_module,
366 	sdt_enable,
367 	sdt_disable,
368 	NULL,
369 	NULL,
370 	sdt_getargdesc,
371 	sdt_getarg,
372 	NULL,
373 	sdt_destroy
374 };
375 
376 /*ARGSUSED*/
377 static int
378 sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
379 {
380 	sdt_provider_t *prov;
381 
382 	if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
383 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
384 		cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
385 		ddi_remove_minor_node(devi, NULL);
386 		return (DDI_FAILURE);
387 	}
388 
389 	ddi_report_dev(devi);
390 	sdt_devi = devi;
391 
392 	if (sdt_probetab_size == 0)
393 		sdt_probetab_size = SDT_PROBETAB_SIZE;
394 
395 	sdt_probetab_mask = sdt_probetab_size - 1;
396 	sdt_probetab =
397 	    kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
398 	dtrace_invop_add(sdt_invop);
399 
400 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
401 		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
402 		    DTRACE_PRIV_KERNEL, NULL,
403 		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
404 			cmn_err(CE_WARN, "failed to register sdt provider %s",
405 			    prov->sdtp_name);
406 		}
407 	}
408 
409 	return (DDI_SUCCESS);
410 }
411 
412 /*ARGSUSED*/
413 static int
414 sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
415 {
416 	sdt_provider_t *prov;
417 
418 	switch (cmd) {
419 	case DDI_DETACH:
420 		break;
421 
422 	case DDI_SUSPEND:
423 		return (DDI_SUCCESS);
424 
425 	default:
426 		return (DDI_FAILURE);
427 	}
428 
429 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
430 		if (prov->sdtp_id != DTRACE_PROVNONE) {
431 			if (dtrace_unregister(prov->sdtp_id) != 0)
432 				return (DDI_FAILURE);
433 
434 			prov->sdtp_id = DTRACE_PROVNONE;
435 		}
436 	}
437 
438 	dtrace_invop_remove(sdt_invop);
439 	kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
440 
441 	return (DDI_SUCCESS);
442 }
443 
444 /*ARGSUSED*/
445 static int
446 sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
447 {
448 	int error;
449 
450 	switch (infocmd) {
451 	case DDI_INFO_DEVT2DEVINFO:
452 		*result = (void *)sdt_devi;
453 		error = DDI_SUCCESS;
454 		break;
455 	case DDI_INFO_DEVT2INSTANCE:
456 		*result = (void *)0;
457 		error = DDI_SUCCESS;
458 		break;
459 	default:
460 		error = DDI_FAILURE;
461 	}
462 	return (error);
463 }
464 
465 /*ARGSUSED*/
466 static int
467 sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
468 {
469 	return (0);
470 }
471 
472 static struct cb_ops sdt_cb_ops = {
473 	sdt_open,		/* open */
474 	nodev,			/* close */
475 	nulldev,		/* strategy */
476 	nulldev,		/* print */
477 	nodev,			/* dump */
478 	nodev,			/* read */
479 	nodev,			/* write */
480 	nodev,			/* ioctl */
481 	nodev,			/* devmap */
482 	nodev,			/* mmap */
483 	nodev,			/* segmap */
484 	nochpoll,		/* poll */
485 	ddi_prop_op,		/* cb_prop_op */
486 	0,			/* streamtab  */
487 	D_NEW | D_MP		/* Driver compatibility flag */
488 };
489 
490 static struct dev_ops sdt_ops = {
491 	DEVO_REV,		/* devo_rev, */
492 	0,			/* refcnt  */
493 	sdt_info,		/* get_dev_info */
494 	nulldev,		/* identify */
495 	nulldev,		/* probe */
496 	sdt_attach,		/* attach */
497 	sdt_detach,		/* detach */
498 	nodev,			/* reset */
499 	&sdt_cb_ops,		/* driver operations */
500 	NULL,			/* bus operations */
501 	nodev			/* dev power */
502 };
503 
504 /*
505  * Module linkage information for the kernel.
506  */
507 static struct modldrv modldrv = {
508 	&mod_driverops,		/* module type (this is a pseudo driver) */
509 	"Statically Defined Tracing",	/* name of module */
510 	&sdt_ops,		/* driver ops */
511 };
512 
513 static struct modlinkage modlinkage = {
514 	MODREV_1,
515 	(void *)&modldrv,
516 	NULL
517 };
518 
519 int
520 _init(void)
521 {
522 	return (mod_install(&modlinkage));
523 }
524 
525 int
526 _info(struct modinfo *modinfop)
527 {
528 	return (mod_info(&modlinkage, modinfop));
529 }
530 
531 int
532 _fini(void)
533 {
534 	return (mod_remove(&modlinkage));
535 }
536