xref: /onnv-gate/usr/src/uts/sparc/dtrace/sdt.c (revision 8803:8c01b39012c9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51677Sdp  * Common Development and Distribution License (the "License").
61677Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8803SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/modctl.h>
280Sstevel@tonic-gate #include <sys/sunddi.h>
290Sstevel@tonic-gate #include <sys/dtrace.h>
300Sstevel@tonic-gate #include <sys/kobj.h>
310Sstevel@tonic-gate #include <sys/stat.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <vm/seg_kmem.h>
340Sstevel@tonic-gate #include <sys/stack.h>
350Sstevel@tonic-gate #include <sys/sdt_impl.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static dev_info_t		*sdt_devi;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate int sdt_verbose = 0;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	SDT_REG_G0		0
420Sstevel@tonic-gate #define	SDT_REG_O0		8
430Sstevel@tonic-gate #define	SDT_REG_O1		9
440Sstevel@tonic-gate #define	SDT_REG_O2		10
450Sstevel@tonic-gate #define	SDT_REG_O3		11
460Sstevel@tonic-gate #define	SDT_REG_O4		12
470Sstevel@tonic-gate #define	SDT_REG_O5		13
480Sstevel@tonic-gate #define	SDT_REG_I0		24
490Sstevel@tonic-gate #define	SDT_REG_I1		25
500Sstevel@tonic-gate #define	SDT_REG_I2		26
510Sstevel@tonic-gate #define	SDT_REG_I3		27
520Sstevel@tonic-gate #define	SDT_REG_I4		28
530Sstevel@tonic-gate #define	SDT_REG_I5		29
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	SDT_SIMM13_MASK		0x1fff
560Sstevel@tonic-gate #define	SDT_SIMM13_MAX		((int32_t)0xfff)
570Sstevel@tonic-gate #define	SDT_CALL(from, to)	(((uint32_t)1 << 30) | \
580Sstevel@tonic-gate 				(((uintptr_t)(to) - (uintptr_t)(from) >> 2) & \
590Sstevel@tonic-gate 				0x3fffffff))
600Sstevel@tonic-gate #define	SDT_SAVE		(0x9de3a000 | (-SA(MINFRAME) & SDT_SIMM13_MASK))
610Sstevel@tonic-gate #define	SDT_RET			0x81c7e008
620Sstevel@tonic-gate #define	SDT_RESTORE		0x81e80000
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	SDT_OP_SETHI		0x1000000
650Sstevel@tonic-gate #define	SDT_OP_OR		0x80100000
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	SDT_FMT2_RD_SHIFT	25
680Sstevel@tonic-gate #define	SDT_IMM22_SHIFT		10
690Sstevel@tonic-gate #define	SDT_IMM22_MASK		0x3fffff
700Sstevel@tonic-gate #define	SDT_IMM10_MASK		0x3ff
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	SDT_FMT3_RD_SHIFT	25
730Sstevel@tonic-gate #define	SDT_FMT3_RS1_SHIFT	14
740Sstevel@tonic-gate #define	SDT_FMT3_RS2_SHIFT	0
750Sstevel@tonic-gate #define	SDT_FMT3_IMM		(1 << 13)
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	SDT_MOV(rs, rd) \
780Sstevel@tonic-gate 	(SDT_OP_OR | (SDT_REG_G0 << SDT_FMT3_RS1_SHIFT) | \
790Sstevel@tonic-gate 	((rs) << SDT_FMT3_RS2_SHIFT) | ((rd) << SDT_FMT3_RD_SHIFT))
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	SDT_ORLO(rs, val, rd) \
820Sstevel@tonic-gate 	(SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
830Sstevel@tonic-gate 	((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_IMM10_MASK))
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	SDT_ORSIMM13(rs, val, rd) \
860Sstevel@tonic-gate 	(SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
870Sstevel@tonic-gate 	((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_SIMM13_MASK))
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	SDT_SETHI(val, reg)	\
900Sstevel@tonic-gate 	(SDT_OP_SETHI | (reg << SDT_FMT2_RD_SHIFT) | \
910Sstevel@tonic-gate 	((val >> SDT_IMM22_SHIFT) & SDT_IMM22_MASK))
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	SDT_ENTRY_SIZE	(11 * sizeof (uint32_t))
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static void
sdt_initialize(sdt_probe_t * sdp,uint32_t ** trampoline)960Sstevel@tonic-gate sdt_initialize(sdt_probe_t *sdp, uint32_t **trampoline)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	uint32_t *instr = *trampoline;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	*instr++ = SDT_SAVE;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (sdp->sdp_id > (uint32_t)SDT_SIMM13_MAX)  {
1030Sstevel@tonic-gate 		*instr++ = SDT_SETHI(sdp->sdp_id, SDT_REG_O0);
1040Sstevel@tonic-gate 		*instr++ = SDT_ORLO(SDT_REG_O0, sdp->sdp_id, SDT_REG_O0);
1050Sstevel@tonic-gate 	} else {
1060Sstevel@tonic-gate 		*instr++ = SDT_ORSIMM13(SDT_REG_G0, sdp->sdp_id, SDT_REG_O0);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I0, SDT_REG_O1);
1100Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I1, SDT_REG_O2);
1110Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I2, SDT_REG_O3);
1120Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I3, SDT_REG_O4);
1130Sstevel@tonic-gate 	*instr = SDT_CALL(instr, dtrace_probe);
1140Sstevel@tonic-gate 	instr++;
1150Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I4, SDT_REG_O5);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	*instr++ = SDT_RET;
1180Sstevel@tonic-gate 	*instr++ = SDT_RESTORE;
1190Sstevel@tonic-gate 	*trampoline = instr;
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*ARGSUSED*/
1230Sstevel@tonic-gate static void
sdt_provide_module(void * arg,struct modctl * ctl)1240Sstevel@tonic-gate sdt_provide_module(void *arg, struct modctl *ctl)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
1270Sstevel@tonic-gate 	char *modname = ctl->mod_modname;
1280Sstevel@tonic-gate 	int primary, nprobes = 0;
1290Sstevel@tonic-gate 	sdt_probedesc_t *sdpd;
1300Sstevel@tonic-gate 	sdt_probe_t *sdp, *old;
1310Sstevel@tonic-gate 	uint32_t *tab;
1320Sstevel@tonic-gate 	sdt_provider_t *prov;
1330Sstevel@tonic-gate 	int len;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	/*
1360Sstevel@tonic-gate 	 * One for all, and all for one:  if we haven't yet registered all of
1370Sstevel@tonic-gate 	 * our providers, we'll refuse to provide anything.
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
1400Sstevel@tonic-gate 		if (prov->sdtp_id == DTRACE_PROVNONE)
1410Sstevel@tonic-gate 			return;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
1450Sstevel@tonic-gate 		return;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	kobj_textwin_alloc(mp);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * Hack to identify unix/genunix/krtld.
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	primary = vmem_contains(heap_arena, (void *)ctl,
1530Sstevel@tonic-gate 	    sizeof (struct modctl)) == 0;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * If there hasn't been an sdt table allocated, we'll do so now.
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	if (mp->sdt_tab == NULL) {
1590Sstevel@tonic-gate 		for (; sdpd != NULL; sdpd = sdpd->sdpd_next) {
1600Sstevel@tonic-gate 			nprobes++;
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		/*
1640Sstevel@tonic-gate 		 * We could (should?) determine precisely the size of the
1650Sstevel@tonic-gate 		 * table -- but a reasonable maximum will suffice.
1660Sstevel@tonic-gate 		 */
1670Sstevel@tonic-gate 		mp->sdt_size = nprobes * SDT_ENTRY_SIZE;
1680Sstevel@tonic-gate 		mp->sdt_tab = kobj_texthole_alloc(mp->text, mp->sdt_size);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 		if (mp->sdt_tab == NULL) {
1710Sstevel@tonic-gate 			cmn_err(CE_WARN, "couldn't allocate SDT table "
1720Sstevel@tonic-gate 			    "for module %s", modname);
1730Sstevel@tonic-gate 			return;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	tab = (uint32_t *)mp->sdt_tab;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
1800Sstevel@tonic-gate 		char *name = sdpd->sdpd_name, *func, *nname;
1810Sstevel@tonic-gate 		int i, j;
1820Sstevel@tonic-gate 		sdt_provider_t *prov;
1830Sstevel@tonic-gate 		ulong_t offs;
1840Sstevel@tonic-gate 		dtrace_id_t id;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
1870Sstevel@tonic-gate 			char *prefix = prov->sdtp_prefix;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 			if (strncmp(name, prefix, strlen(prefix)) == 0) {
1900Sstevel@tonic-gate 				name += strlen(prefix);
1910Sstevel@tonic-gate 				break;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		for (i = 0, j = 0; name[j] != '\0'; i++) {
1980Sstevel@tonic-gate 			if (name[j] == '_' && name[j + 1] == '_') {
1990Sstevel@tonic-gate 				nname[i] = '-';
2000Sstevel@tonic-gate 				j += 2;
2010Sstevel@tonic-gate 			} else {
2020Sstevel@tonic-gate 				nname[i] = name[j++];
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		nname[i] = '\0';
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
2090Sstevel@tonic-gate 		sdp->sdp_loadcnt = ctl->mod_loadcnt;
2100Sstevel@tonic-gate 		sdp->sdp_primary = primary;
2110Sstevel@tonic-gate 		sdp->sdp_ctl = ctl;
2120Sstevel@tonic-gate 		sdp->sdp_name = nname;
2130Sstevel@tonic-gate 		sdp->sdp_namelen = len;
2140Sstevel@tonic-gate 		sdp->sdp_provider = prov;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		func = kobj_searchsym(mp, sdpd->sdpd_offset +
2170Sstevel@tonic-gate 		    (uintptr_t)mp->text, &offs);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		if (func == NULL)
2200Sstevel@tonic-gate 			func = "<unknown>";
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		/*
2230Sstevel@tonic-gate 		 * We have our provider.  Now create the probe.
2240Sstevel@tonic-gate 		 */
2250Sstevel@tonic-gate 		if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
2260Sstevel@tonic-gate 		    func, nname)) != DTRACE_IDNONE) {
2270Sstevel@tonic-gate 			old = dtrace_probe_arg(prov->sdtp_id, id);
2280Sstevel@tonic-gate 			ASSERT(old != NULL);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 			sdp->sdp_next = old->sdp_next;
2310Sstevel@tonic-gate 			sdp->sdp_id = id;
2320Sstevel@tonic-gate 			old->sdp_next = sdp;
2330Sstevel@tonic-gate 		} else {
2340Sstevel@tonic-gate 			sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
2350Sstevel@tonic-gate 			    modname, func, nname, 1, sdp);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 			mp->sdt_nprobes++;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		sdp->sdp_patchval = SDT_CALL((uintptr_t)mp->text +
2410Sstevel@tonic-gate 		    sdpd->sdpd_offset, tab);
2420Sstevel@tonic-gate 		sdp->sdp_patchpoint = (uint32_t *)((uintptr_t)mp->textwin +
2430Sstevel@tonic-gate 		    sdpd->sdpd_offset);
2440Sstevel@tonic-gate 		sdp->sdp_savedval = *sdp->sdp_patchpoint;
2450Sstevel@tonic-gate 		sdt_initialize(sdp, &tab);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*ARGSUSED*/
2500Sstevel@tonic-gate static void
sdt_destroy(void * arg,dtrace_id_t id,void * parg)2510Sstevel@tonic-gate sdt_destroy(void *arg, dtrace_id_t id, void *parg)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	sdt_probe_t *sdp = parg, *old;
2540Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
2570Sstevel@tonic-gate 		if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
2580Sstevel@tonic-gate 		    ctl->mod_loaded) || sdp->sdp_primary) {
2590Sstevel@tonic-gate 			((struct module *)(ctl->mod_mp))->sdt_nprobes--;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	while (sdp != NULL) {
2640Sstevel@tonic-gate 		old = sdp;
2650Sstevel@tonic-gate 		kmem_free(sdp->sdp_name, sdp->sdp_namelen);
2660Sstevel@tonic-gate 		sdp = sdp->sdp_next;
2670Sstevel@tonic-gate 		kmem_free(old, sizeof (sdt_probe_t));
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*ARGSUSED*/
272*8803SJonathan.Haslam@Sun.COM static int
sdt_enable(void * arg,dtrace_id_t id,void * parg)2730Sstevel@tonic-gate sdt_enable(void *arg, dtrace_id_t id, void *parg)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	sdt_probe_t *sdp = parg;
2760Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	ctl->mod_nenabled++;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/*
2810Sstevel@tonic-gate 	 * If this module has disappeared since we discovered its probes,
2820Sstevel@tonic-gate 	 * refuse to enable it.
2830Sstevel@tonic-gate 	 */
2840Sstevel@tonic-gate 	if (!sdp->sdp_primary && !ctl->mod_loaded) {
2850Sstevel@tonic-gate 		if (sdt_verbose) {
2860Sstevel@tonic-gate 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
2870Sstevel@tonic-gate 			    "(module %s unloaded)",
2880Sstevel@tonic-gate 			    sdp->sdp_name, ctl->mod_modname);
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		goto err;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * Now check that our modctl has the expected load count.  If it
2950Sstevel@tonic-gate 	 * doesn't, this module must have been unloaded and reloaded -- and
2960Sstevel@tonic-gate 	 * we're not going to touch it.
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
2990Sstevel@tonic-gate 		if (sdt_verbose) {
3000Sstevel@tonic-gate 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
3010Sstevel@tonic-gate 			    "(module %s reloaded)",
3020Sstevel@tonic-gate 			    sdp->sdp_name, ctl->mod_modname);
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 		goto err;
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	while (sdp != NULL) {
3080Sstevel@tonic-gate 		*sdp->sdp_patchpoint = sdp->sdp_patchval;
3090Sstevel@tonic-gate 		sdp = sdp->sdp_next;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate err:
313*8803SJonathan.Haslam@Sun.COM 	return (0);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*ARGSUSED*/
3170Sstevel@tonic-gate static void
sdt_disable(void * arg,dtrace_id_t id,void * parg)3180Sstevel@tonic-gate sdt_disable(void *arg, dtrace_id_t id, void *parg)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	sdt_probe_t *sdp = parg;
3210Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
3240Sstevel@tonic-gate 	ctl->mod_nenabled--;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if ((!sdp->sdp_primary && !ctl->mod_loaded) ||
3270Sstevel@tonic-gate 	    (ctl->mod_loadcnt != sdp->sdp_loadcnt))
3280Sstevel@tonic-gate 		goto err;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	while (sdp != NULL) {
3310Sstevel@tonic-gate 		*sdp->sdp_patchpoint = sdp->sdp_savedval;
3320Sstevel@tonic-gate 		sdp = sdp->sdp_next;
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate err:
3360Sstevel@tonic-gate 	;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate static dtrace_pops_t sdt_pops = {
3400Sstevel@tonic-gate 	NULL,
3410Sstevel@tonic-gate 	sdt_provide_module,
3420Sstevel@tonic-gate 	sdt_enable,
3430Sstevel@tonic-gate 	sdt_disable,
3440Sstevel@tonic-gate 	NULL,
3450Sstevel@tonic-gate 	NULL,
3460Sstevel@tonic-gate 	sdt_getargdesc,
3470Sstevel@tonic-gate 	NULL,
3480Sstevel@tonic-gate 	NULL,
3490Sstevel@tonic-gate 	sdt_destroy
3500Sstevel@tonic-gate };
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate static int
sdt_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)3530Sstevel@tonic-gate sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	sdt_provider_t *prov;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	switch (cmd) {
3580Sstevel@tonic-gate 	case DDI_ATTACH:
3590Sstevel@tonic-gate 		break;
3600Sstevel@tonic-gate 	case DDI_RESUME:
3610Sstevel@tonic-gate 		return (DDI_SUCCESS);
3620Sstevel@tonic-gate 	default:
3630Sstevel@tonic-gate 		return (DDI_FAILURE);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "sdt", S_IFCHR, 0,
3670Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE) {
3680Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
3690Sstevel@tonic-gate 		return (DDI_FAILURE);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	ddi_report_dev(devi);
3730Sstevel@tonic-gate 	sdt_devi = devi;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
3760Sstevel@tonic-gate 		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
3771677Sdp 		    DTRACE_PRIV_KERNEL, NULL,
3780Sstevel@tonic-gate 		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
3790Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to register sdt provider %s",
3800Sstevel@tonic-gate 			    prov->sdtp_name);
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	return (DDI_SUCCESS);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate static int
sdt_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)3880Sstevel@tonic-gate sdt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	sdt_provider_t *prov;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	switch (cmd) {
3930Sstevel@tonic-gate 	case DDI_DETACH:
3940Sstevel@tonic-gate 		break;
3950Sstevel@tonic-gate 	case DDI_SUSPEND:
3960Sstevel@tonic-gate 		return (DDI_SUCCESS);
3970Sstevel@tonic-gate 	default:
3980Sstevel@tonic-gate 		return (DDI_FAILURE);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
4020Sstevel@tonic-gate 		if (prov->sdtp_id != DTRACE_PROVNONE) {
4030Sstevel@tonic-gate 			if (dtrace_unregister(prov->sdtp_id) != 0)
4040Sstevel@tonic-gate 				return (DDI_FAILURE);
4050Sstevel@tonic-gate 			prov->sdtp_id = DTRACE_PROVNONE;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
4100Sstevel@tonic-gate 	return (DDI_SUCCESS);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*ARGSUSED*/
4140Sstevel@tonic-gate static int
sdt_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4150Sstevel@tonic-gate sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	int error;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	switch (infocmd) {
4200Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4210Sstevel@tonic-gate 		*result = (void *)sdt_devi;
4220Sstevel@tonic-gate 		error = DDI_SUCCESS;
4230Sstevel@tonic-gate 		break;
4240Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4250Sstevel@tonic-gate 		*result = (void *)0;
4260Sstevel@tonic-gate 		error = DDI_SUCCESS;
4270Sstevel@tonic-gate 		break;
4280Sstevel@tonic-gate 	default:
4290Sstevel@tonic-gate 		error = DDI_FAILURE;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	return (error);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*ARGSUSED*/
4350Sstevel@tonic-gate static int
sdt_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)4360Sstevel@tonic-gate sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	return (0);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate static struct cb_ops sdt_cb_ops = {
4420Sstevel@tonic-gate 	sdt_open,		/* open */
4430Sstevel@tonic-gate 	nodev,			/* close */
4440Sstevel@tonic-gate 	nulldev,		/* strategy */
4450Sstevel@tonic-gate 	nulldev,		/* print */
4460Sstevel@tonic-gate 	nodev,			/* dump */
4470Sstevel@tonic-gate 	nodev,			/* read */
4480Sstevel@tonic-gate 	nodev,			/* write */
4490Sstevel@tonic-gate 	nodev,			/* ioctl */
4500Sstevel@tonic-gate 	nodev,			/* devmap */
4510Sstevel@tonic-gate 	nodev,			/* mmap */
4520Sstevel@tonic-gate 	nodev,			/* segmap */
4530Sstevel@tonic-gate 	nochpoll,		/* poll */
4540Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
4550Sstevel@tonic-gate 	0,			/* streamtab  */
4560Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
4570Sstevel@tonic-gate };
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate static struct dev_ops sdt_ops = {
4600Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
4610Sstevel@tonic-gate 	0,			/* refcnt  */
4620Sstevel@tonic-gate 	sdt_info,		/* get_dev_info */
4630Sstevel@tonic-gate 	nulldev,		/* identify */
4640Sstevel@tonic-gate 	nulldev,		/* probe */
4650Sstevel@tonic-gate 	sdt_attach,		/* attach */
4660Sstevel@tonic-gate 	sdt_detach,		/* detach */
4670Sstevel@tonic-gate 	nodev,			/* reset */
4680Sstevel@tonic-gate 	&sdt_cb_ops,		/* driver operations */
4690Sstevel@tonic-gate 	NULL,			/* bus operations */
4707656SSherry.Moore@Sun.COM 	nodev,			/* dev power */
4717656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
4720Sstevel@tonic-gate };
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * Module linkage information for the kernel.
4760Sstevel@tonic-gate  */
4770Sstevel@tonic-gate static struct modldrv modldrv = {
4780Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
4790Sstevel@tonic-gate 	"Statically Defined Tracing",	/* name of module */
4800Sstevel@tonic-gate 	&sdt_ops,		/* driver ops */
4810Sstevel@tonic-gate };
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate static struct modlinkage modlinkage = {
4840Sstevel@tonic-gate 	MODREV_1,
4850Sstevel@tonic-gate 	(void *)&modldrv,
4860Sstevel@tonic-gate 	NULL
4870Sstevel@tonic-gate };
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate int
_init(void)4900Sstevel@tonic-gate _init(void)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	return (mod_install(&modlinkage));
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4960Sstevel@tonic-gate _info(struct modinfo *modinfop)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate int
_fini(void)5020Sstevel@tonic-gate _fini(void)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
5050Sstevel@tonic-gate }
506