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/* #pragma ident	"@(#)sdt.c	1.9	08/07/01 SMI" */
27
28#ifdef KERNEL
29#ifndef _KERNEL
30#define _KERNEL /* Solaris vs. Darwin */
31#endif
32#endif
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/errno.h>
37#include <sys/stat.h>
38#include <sys/ioctl.h>
39#include <sys/conf.h>
40#include <sys/fcntl.h>
41#include <miscfs/devfs/devfs.h>
42
43
44#include <sys/dtrace.h>
45#include <sys/dtrace_impl.h>
46
47#include <sys/dtrace_glue.h>
48
49#include <sys/sdt_impl.h>
50extern int dtrace_kernel_symbol_mode;
51
52struct savearea_t; /* Used anonymously */
53typedef kern_return_t (*perfCallback)(int, struct savearea_t *, uintptr_t *, int);
54
55#if defined(__x86_64__)
56extern perfCallback tempDTraceTrapHook;
57extern kern_return_t fbt_perfCallback(int, struct savearea_t *, int, int);
58#define	SDT_PATCHVAL	0xf0
59#define	SDT_AFRAMES		6
60#else
61#error Unknown architecture
62#endif
63
64#define	SDT_PROBETAB_SIZE	0x1000		/* 4k entries -- 16K total */
65
66#define DTRACE_PROBE_PREFIX "_dtrace_probe$"
67
68static dev_info_t		*sdt_devi;
69static int			sdt_verbose = 0;
70sdt_probe_t		**sdt_probetab;
71int			sdt_probetab_size;
72int			sdt_probetab_mask;
73
74/*ARGSUSED*/
75static void
76__sdt_provide_module(void *arg, struct modctl *ctl)
77{
78#pragma unused(arg)
79	struct module *mp = (struct module *)ctl->mod_address;
80	char *modname = ctl->mod_modname;
81	sdt_probedesc_t *sdpd;
82	sdt_probe_t *sdp, *old;
83	sdt_provider_t *prov;
84	int len;
85
86	/*
87	 * One for all, and all for one:  if we haven't yet registered all of
88	 * our providers, we'll refuse to provide anything.
89	 */
90	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
91		if (prov->sdtp_id == DTRACE_PROVNONE)
92			return;
93	}
94
95	if (!mp || mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
96		return;
97
98	for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
99	    const char *name = sdpd->sdpd_name, *func;
100	    char *nname;
101		int i, j;
102		dtrace_id_t id;
103
104		for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
105			const char *prefpart, *prefix = prov->sdtp_prefix;
106
107			if ((prefpart = strstr(name, prefix))) {
108				name = prefpart + strlen(prefix);
109				break;
110			}
111		}
112
113		nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
114
115		for (i = 0, j = 0; name[j] != '\0'; i++) {
116			if (name[j] == '_' && name[j + 1] == '_') {
117				nname[i] = '-';
118				j += 2;
119			} else {
120				nname[i] = name[j++];
121			}
122		}
123
124		nname[i] = '\0';
125
126		sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
127		sdp->sdp_loadcnt = ctl->mod_loadcnt;
128		sdp->sdp_ctl = ctl;
129		sdp->sdp_name = nname;
130		sdp->sdp_namelen = len;
131		sdp->sdp_provider = prov;
132
133		func = sdpd->sdpd_func;
134
135		if (func == NULL)
136			func = "<unknown>";
137
138		/*
139		 * We have our provider.  Now create the probe.
140		 */
141		if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
142		    func, nname)) != DTRACE_IDNONE) {
143			old = dtrace_probe_arg(prov->sdtp_id, id);
144			ASSERT(old != NULL);
145
146			sdp->sdp_next = old->sdp_next;
147			sdp->sdp_id = id;
148			old->sdp_next = sdp;
149		} else {
150			sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
151			    modname, func, nname, SDT_AFRAMES, sdp);
152
153			mp->sdt_nprobes++;
154		}
155
156#if 0
157		printf ("__sdt_provide_module:  sdpd=0x%p  sdp=0x%p  name=%s, id=%d\n", sdpd, sdp, nname, sdp->sdp_id);
158#endif
159
160		sdp->sdp_hashnext =
161		    sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)];
162		sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp;
163
164		sdp->sdp_patchval = SDT_PATCHVAL;
165		sdp->sdp_patchpoint = (sdt_instr_t *)sdpd->sdpd_offset;
166		sdp->sdp_savedval = *sdp->sdp_patchpoint;
167	}
168}
169
170/*ARGSUSED*/
171static void
172sdt_destroy(void *arg, dtrace_id_t id, void *parg)
173{
174#pragma unused(arg,id)
175	sdt_probe_t *sdp = parg, *old, *last, *hash;
176	int ndx;
177#if !defined(__APPLE__)
178	struct modctl *ctl = sdp->sdp_ctl;
179
180	if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
181		if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
182		    ctl->mod_loaded)) {
183			((struct module *)(ctl->mod_mp))->sdt_nprobes--;
184		}
185	}
186#endif /* __APPLE__ */
187
188	while (sdp != NULL) {
189		old = sdp;
190
191		/*
192		 * Now we need to remove this probe from the sdt_probetab.
193		 */
194		ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint);
195		last = NULL;
196		hash = sdt_probetab[ndx];
197
198		while (hash != sdp) {
199			ASSERT(hash != NULL);
200			last = hash;
201			hash = hash->sdp_hashnext;
202		}
203
204		if (last != NULL) {
205			last->sdp_hashnext = sdp->sdp_hashnext;
206		} else {
207			sdt_probetab[ndx] = sdp->sdp_hashnext;
208		}
209
210		kmem_free(sdp->sdp_name, sdp->sdp_namelen);
211		sdp = sdp->sdp_next;
212		kmem_free(old, sizeof (sdt_probe_t));
213	}
214}
215
216/*ARGSUSED*/
217static int
218sdt_enable(void *arg, dtrace_id_t id, void *parg)
219{
220#pragma unused(arg,id)
221	sdt_probe_t *sdp = parg;
222	struct modctl *ctl = sdp->sdp_ctl;
223
224	ctl->mod_nenabled++;
225
226	/*
227	 * If this module has disappeared since we discovered its probes,
228	 * refuse to enable it.
229	 */
230	if (!ctl->mod_loaded) {
231		if (sdt_verbose) {
232			cmn_err(CE_NOTE, "sdt is failing for probe %s "
233			    "(module %s unloaded)",
234			    sdp->sdp_name, ctl->mod_modname);
235		}
236		goto err;
237	}
238
239	/*
240	 * Now check that our modctl has the expected load count.  If it
241	 * doesn't, this module must have been unloaded and reloaded -- and
242	 * we're not going to touch it.
243	 */
244	if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
245		if (sdt_verbose) {
246			cmn_err(CE_NOTE, "sdt is failing for probe %s "
247			    "(module %s reloaded)",
248			    sdp->sdp_name, ctl->mod_modname);
249		}
250		goto err;
251	}
252
253	dtrace_casptr(&tempDTraceTrapHook, NULL, fbt_perfCallback);
254	if (tempDTraceTrapHook != (perfCallback)fbt_perfCallback) {
255		if (sdt_verbose) {
256			cmn_err(CE_NOTE, "sdt_enable is failing for probe %s "
257			    "in module %s: tempDTraceTrapHook already occupied.",
258			    sdp->sdp_name, ctl->mod_modname);
259		}
260		return (0);
261	}
262
263	while (sdp != NULL) {
264		(void)ml_nofault_copy( (vm_offset_t)&sdp->sdp_patchval, (vm_offset_t)sdp->sdp_patchpoint,
265		                       (vm_size_t)sizeof(sdp->sdp_patchval));
266		sdp = sdp->sdp_next;
267	}
268
269err:
270	return (0);
271}
272
273/*ARGSUSED*/
274static void
275sdt_disable(void *arg, dtrace_id_t id, void *parg)
276{
277#pragma unused(arg,id)
278	sdt_probe_t *sdp = parg;
279	struct modctl *ctl = sdp->sdp_ctl;
280
281	ctl->mod_nenabled--;
282
283	if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt)
284		goto err;
285
286	while (sdp != NULL) {
287		(void)ml_nofault_copy( (vm_offset_t)&sdp->sdp_savedval, (vm_offset_t)sdp->sdp_patchpoint,
288		                       (vm_size_t)sizeof(sdp->sdp_savedval));
289		sdp = sdp->sdp_next;
290	}
291
292err:
293	;
294}
295
296static dtrace_pops_t sdt_pops = {
297	NULL,
298	sdt_provide_module,
299	sdt_enable,
300	sdt_disable,
301	NULL,
302	NULL,
303	sdt_getargdesc,
304	sdt_getarg,
305	NULL,
306	sdt_destroy
307};
308
309/*ARGSUSED*/
310static int
311sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
312{
313#pragma unused(cmd)
314	sdt_provider_t *prov;
315
316	if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
317	    0, DDI_PSEUDO, 0) == DDI_FAILURE) {
318		cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
319		ddi_remove_minor_node(devi, NULL);
320		return (DDI_FAILURE);
321	}
322
323	ddi_report_dev(devi);
324	sdt_devi = devi;
325
326	if (sdt_probetab_size == 0)
327		sdt_probetab_size = SDT_PROBETAB_SIZE;
328
329	sdt_probetab_mask = sdt_probetab_size - 1;
330	sdt_probetab =
331	    kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
332	dtrace_invop_add(sdt_invop);
333
334	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
335		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
336		    DTRACE_PRIV_KERNEL, NULL,
337		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
338			cmn_err(CE_WARN, "failed to register sdt provider %s",
339			    prov->sdtp_name);
340		}
341	}
342
343	return (DDI_SUCCESS);
344}
345
346#if !defined(__APPLE__)
347/*ARGSUSED*/
348static int
349sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
350{
351	sdt_provider_t *prov;
352
353	switch (cmd) {
354	case DDI_DETACH:
355		break;
356
357	case DDI_SUSPEND:
358		return (DDI_SUCCESS);
359
360	default:
361		return (DDI_FAILURE);
362	}
363
364	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
365		if (prov->sdtp_id != DTRACE_PROVNONE) {
366			if (dtrace_unregister(prov->sdtp_id) != 0)
367				return (DDI_FAILURE);
368
369			prov->sdtp_id = DTRACE_PROVNONE;
370		}
371	}
372
373	dtrace_invop_remove(sdt_invop);
374	kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
375
376	return (DDI_SUCCESS);
377}
378
379/*ARGSUSED*/
380static int
381sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
382{
383	int error;
384
385	switch (infocmd) {
386	case DDI_INFO_DEVT2DEVINFO:
387		*result = (void *)sdt_devi;
388		error = DDI_SUCCESS;
389		break;
390	case DDI_INFO_DEVT2INSTANCE:
391		*result = (void *)0;
392		error = DDI_SUCCESS;
393		break;
394	default:
395		error = DDI_FAILURE;
396	}
397	return (error);
398}
399
400/*ARGSUSED*/
401static int
402sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
403{
404	return (0);
405}
406
407static struct cb_ops sdt_cb_ops = {
408	sdt_open,		/* open */
409	nodev,			/* close */
410	nulldev,		/* strategy */
411	nulldev,		/* print */
412	nodev,			/* dump */
413	nodev,			/* read */
414	nodev,			/* write */
415	nodev,			/* ioctl */
416	nodev,			/* devmap */
417	nodev,			/* mmap */
418	nodev,			/* segmap */
419	nochpoll,		/* poll */
420	ddi_prop_op,		/* cb_prop_op */
421	0,			/* streamtab  */
422	D_NEW | D_MP		/* Driver compatibility flag */
423};
424
425static struct dev_ops sdt_ops = {
426	DEVO_REV,		/* devo_rev, */
427	0,			/* refcnt  */
428	sdt_info,		/* get_dev_info */
429	nulldev,		/* identify */
430	nulldev,		/* probe */
431	sdt_attach,		/* attach */
432	sdt_detach,		/* detach */
433	nodev,			/* reset */
434	&sdt_cb_ops,		/* driver operations */
435	NULL,			/* bus operations */
436	nodev			/* dev power */
437};
438
439/*
440 * Module linkage information for the kernel.
441 */
442static struct modldrv modldrv = {
443	&mod_driverops,		/* module type (this is a pseudo driver) */
444	"Statically Defined Tracing",	/* name of module */
445	&sdt_ops,		/* driver ops */
446};
447
448static struct modlinkage modlinkage = {
449	MODREV_1,
450	(void *)&modldrv,
451	NULL
452};
453
454int
455_init(void)
456{
457	return (mod_install(&modlinkage));
458}
459
460int
461_info(struct modinfo *modinfop)
462{
463	return (mod_info(&modlinkage, modinfop));
464}
465
466int
467_fini(void)
468{
469	return (mod_remove(&modlinkage));
470}
471#else
472d_open_t _sdt_open;
473
474int _sdt_open(dev_t dev, int flags, int devtype, struct proc *p)
475{
476#pragma unused(dev,flags,devtype,p)
477	return 0;
478}
479
480#define SDT_MAJOR  -24 /* let the kernel pick the device number */
481
482/*
483 * A struct describing which functions will get invoked for certain
484 * actions.
485 */
486static struct cdevsw sdt_cdevsw =
487{
488	_sdt_open,		/* open */
489	eno_opcl,			/* close */
490	eno_rdwrt,			/* read */
491	eno_rdwrt,			/* write */
492	eno_ioctl,			/* ioctl */
493	(stop_fcn_t *)nulldev, /* stop */
494	(reset_fcn_t *)nulldev, /* reset */
495	NULL,				/* tty's */
496	eno_select,			/* select */
497	eno_mmap,			/* mmap */
498	eno_strat,			/* strategy */
499	eno_getc,			/* getc */
500	eno_putc,			/* putc */
501	0					/* type */
502};
503
504static int gSDTInited = 0;
505static struct modctl g_sdt_kernctl;
506static struct module g_sdt_mach_module;
507
508#include <mach-o/nlist.h>
509#include <libkern/kernel_mach_header.h>
510
511void sdt_init( void )
512{
513	if (0 == gSDTInited)
514	{
515		int majdevno = cdevsw_add(SDT_MAJOR, &sdt_cdevsw);
516
517		if (majdevno < 0) {
518			printf("sdt_init: failed to allocate a major number!\n");
519			gSDTInited = 0;
520			return;
521		}
522
523		if (MH_MAGIC_KERNEL != _mh_execute_header.magic) {
524			g_sdt_kernctl.mod_address = (vm_address_t)NULL;
525			g_sdt_kernctl.mod_size = 0;
526		} else {
527			kernel_mach_header_t        *mh;
528			struct load_command         *cmd;
529			kernel_segment_command_t    *orig_ts = NULL, *orig_le = NULL;
530			struct symtab_command       *orig_st = NULL;
531			kernel_nlist_t		    *sym = NULL;
532			char                        *strings;
533			unsigned int 		    i;
534
535			g_sdt_mach_module.sdt_nprobes = 0;
536			g_sdt_mach_module.sdt_probes = NULL;
537
538			g_sdt_kernctl.mod_address = (vm_address_t)&g_sdt_mach_module;
539			g_sdt_kernctl.mod_size = 0;
540			strncpy((char *)&(g_sdt_kernctl.mod_modname), "mach_kernel", KMOD_MAX_NAME);
541
542			g_sdt_kernctl.mod_next = NULL;
543			g_sdt_kernctl.mod_stale = NULL;
544			g_sdt_kernctl.mod_id = 0;
545			g_sdt_kernctl.mod_loadcnt = 1;
546			g_sdt_kernctl.mod_loaded = 1;
547			g_sdt_kernctl.mod_flags = 0;
548			g_sdt_kernctl.mod_nenabled = 0;
549
550			mh = &_mh_execute_header;
551			cmd = (struct load_command*) &mh[1];
552			for (i = 0; i < mh->ncmds; i++) {
553				if (cmd->cmd == LC_SEGMENT_KERNEL) {
554					kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd;
555
556					if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT))
557						orig_ts = orig_sg;
558					else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT))
559						orig_le = orig_sg;
560					else if (LIT_STRNEQL(orig_sg->segname, ""))
561						orig_ts = orig_sg; /* kexts have a single unnamed segment */
562				}
563				else if (cmd->cmd == LC_SYMTAB)
564					orig_st = (struct symtab_command *) cmd;
565
566				cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize);
567			}
568
569			if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL))
570				return;
571
572			sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff);
573			strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff);
574
575			for (i = 0; i < orig_st->nsyms; i++) {
576				uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT);
577				char *name = strings + sym[i].n_un.n_strx;
578				const char *prev_name;
579				unsigned long best;
580				unsigned int j;
581
582				/* Check that the symbol is a global and that it has a name. */
583				if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type))
584					continue;
585
586				if (0 == sym[i].n_un.n_strx) /* iff a null, "", name. */
587					continue;
588
589				/* Lop off omnipresent leading underscore. */
590				if (*name == '_')
591					name += 1;
592
593				if (strncmp(name, DTRACE_PROBE_PREFIX, sizeof(DTRACE_PROBE_PREFIX) - 1) == 0) {
594					sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP);
595					int len = strlen(name) + 1;
596
597					sdpd->sdpd_name = kmem_alloc(len, KM_SLEEP);
598					strncpy(sdpd->sdpd_name, name, len); /* NUL termination is ensured. */
599
600					prev_name = "<unknown>";
601					best = 0;
602
603					/*
604					 * Find the symbol immediately preceding the sdt probe site just discovered,
605					 * that symbol names the function containing the sdt probe.
606					 */
607					for (j = 0; j < orig_st->nsyms; j++) {
608						uint8_t jn_type = sym[j].n_type & (N_TYPE | N_EXT);
609						char *jname = strings + sym[j].n_un.n_strx;
610
611						if (((N_SECT | N_EXT) != jn_type && (N_ABS | N_EXT) != jn_type))
612							continue;
613
614						if (0 == sym[j].n_un.n_strx) /* iff a null, "", name. */
615							continue;
616
617						if (*jname == '_')
618							jname += 1;
619
620						if (*(unsigned long *)sym[i].n_value <= (unsigned long)sym[j].n_value)
621							continue;
622
623						if ((unsigned long)sym[j].n_value > best) {
624							best = (unsigned long)sym[j].n_value;
625							prev_name = jname;
626						}
627					}
628
629					sdpd->sdpd_func = kmem_alloc((len = strlen(prev_name) + 1), KM_SLEEP);
630					strncpy(sdpd->sdpd_func, prev_name, len); /* NUL termination is ensured. */
631
632					sdpd->sdpd_offset = *(unsigned long *)sym[i].n_value;
633
634#if 0
635					printf("sdt_init: sdpd_offset=0x%lx, n_value=0x%lx, name=%s\n",
636					    sdpd->sdpd_offset,  *(unsigned long *)sym[i].n_value, name);
637#endif
638
639					sdpd->sdpd_next = g_sdt_mach_module.sdt_probes;
640					g_sdt_mach_module.sdt_probes = sdpd;
641				} else {
642					prev_name = name;
643				}
644			}
645		}
646
647		sdt_attach( (dev_info_t	*)(uintptr_t)majdevno, DDI_ATTACH );
648
649		gSDTInited = 1;
650	} else
651		panic("sdt_init: called twice!\n");
652}
653
654#undef SDT_MAJOR
655
656/*ARGSUSED*/
657void
658sdt_provide_module(void *arg, struct modctl *ctl)
659{
660#pragma unused(arg)
661	ASSERT(ctl != NULL);
662	ASSERT(dtrace_kernel_symbol_mode != DTRACE_KERNEL_SYMBOLS_NEVER);
663	lck_mtx_assert(&mod_lock, LCK_MTX_ASSERT_OWNED);
664
665	if (MOD_SDT_DONE(ctl))
666		return;
667
668	if (MOD_IS_MACH_KERNEL(ctl)) {
669		__sdt_provide_module(arg, &g_sdt_kernctl);
670
671		sdt_probedesc_t *sdpd = g_sdt_mach_module.sdt_probes;
672		while (sdpd) {
673			sdt_probedesc_t *this_sdpd = sdpd;
674			kmem_free((void *)sdpd->sdpd_name, strlen(sdpd->sdpd_name) + 1);
675			kmem_free((void *)sdpd->sdpd_func, strlen(sdpd->sdpd_func) + 1);
676			sdpd = sdpd->sdpd_next;
677			kmem_free((void *)this_sdpd, sizeof(sdt_probedesc_t));
678		}
679		g_sdt_mach_module.sdt_probes = NULL;
680	} else {
681		/* FIXME -- sdt in kext not yet supported */
682	}
683
684	/* Need to mark this module as completed */
685	ctl->mod_flags |= MODCTL_SDT_PROBES_PROVIDED;
686}
687
688#endif /* __APPLE__ */
689