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