xref: /titanic_51/usr/src/uts/sparc/dtrace/sdt.c (revision ad4023c40b055806dce2bde9ee9e87e5016b5135)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*ad4023c4Sdp  * Common Development and Distribution License (the "License").
6*ad4023c4Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ad4023c4Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
297c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
317c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/stack.h>
367c478bd9Sstevel@tonic-gate #include <sys/sdt_impl.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static dev_info_t		*sdt_devi;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int sdt_verbose = 0;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	SDT_REG_G0		0
437c478bd9Sstevel@tonic-gate #define	SDT_REG_O0		8
447c478bd9Sstevel@tonic-gate #define	SDT_REG_O1		9
457c478bd9Sstevel@tonic-gate #define	SDT_REG_O2		10
467c478bd9Sstevel@tonic-gate #define	SDT_REG_O3		11
477c478bd9Sstevel@tonic-gate #define	SDT_REG_O4		12
487c478bd9Sstevel@tonic-gate #define	SDT_REG_O5		13
497c478bd9Sstevel@tonic-gate #define	SDT_REG_I0		24
507c478bd9Sstevel@tonic-gate #define	SDT_REG_I1		25
517c478bd9Sstevel@tonic-gate #define	SDT_REG_I2		26
527c478bd9Sstevel@tonic-gate #define	SDT_REG_I3		27
537c478bd9Sstevel@tonic-gate #define	SDT_REG_I4		28
547c478bd9Sstevel@tonic-gate #define	SDT_REG_I5		29
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	SDT_SIMM13_MASK		0x1fff
577c478bd9Sstevel@tonic-gate #define	SDT_SIMM13_MAX		((int32_t)0xfff)
587c478bd9Sstevel@tonic-gate #define	SDT_CALL(from, to)	(((uint32_t)1 << 30) | \
597c478bd9Sstevel@tonic-gate 				(((uintptr_t)(to) - (uintptr_t)(from) >> 2) & \
607c478bd9Sstevel@tonic-gate 				0x3fffffff))
617c478bd9Sstevel@tonic-gate #define	SDT_SAVE		(0x9de3a000 | (-SA(MINFRAME) & SDT_SIMM13_MASK))
627c478bd9Sstevel@tonic-gate #define	SDT_RET			0x81c7e008
637c478bd9Sstevel@tonic-gate #define	SDT_RESTORE		0x81e80000
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	SDT_OP_SETHI		0x1000000
667c478bd9Sstevel@tonic-gate #define	SDT_OP_OR		0x80100000
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	SDT_FMT2_RD_SHIFT	25
697c478bd9Sstevel@tonic-gate #define	SDT_IMM22_SHIFT		10
707c478bd9Sstevel@tonic-gate #define	SDT_IMM22_MASK		0x3fffff
717c478bd9Sstevel@tonic-gate #define	SDT_IMM10_MASK		0x3ff
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	SDT_FMT3_RD_SHIFT	25
747c478bd9Sstevel@tonic-gate #define	SDT_FMT3_RS1_SHIFT	14
757c478bd9Sstevel@tonic-gate #define	SDT_FMT3_RS2_SHIFT	0
767c478bd9Sstevel@tonic-gate #define	SDT_FMT3_IMM		(1 << 13)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	SDT_MOV(rs, rd) \
797c478bd9Sstevel@tonic-gate 	(SDT_OP_OR | (SDT_REG_G0 << SDT_FMT3_RS1_SHIFT) | \
807c478bd9Sstevel@tonic-gate 	((rs) << SDT_FMT3_RS2_SHIFT) | ((rd) << SDT_FMT3_RD_SHIFT))
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	SDT_ORLO(rs, val, rd) \
837c478bd9Sstevel@tonic-gate 	(SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
847c478bd9Sstevel@tonic-gate 	((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_IMM10_MASK))
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	SDT_ORSIMM13(rs, val, rd) \
877c478bd9Sstevel@tonic-gate 	(SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
887c478bd9Sstevel@tonic-gate 	((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_SIMM13_MASK))
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	SDT_SETHI(val, reg)	\
917c478bd9Sstevel@tonic-gate 	(SDT_OP_SETHI | (reg << SDT_FMT2_RD_SHIFT) | \
927c478bd9Sstevel@tonic-gate 	((val >> SDT_IMM22_SHIFT) & SDT_IMM22_MASK))
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	SDT_ENTRY_SIZE	(11 * sizeof (uint32_t))
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void
977c478bd9Sstevel@tonic-gate sdt_initialize(sdt_probe_t *sdp, uint32_t **trampoline)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	uint32_t *instr = *trampoline;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	*instr++ = SDT_SAVE;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (sdp->sdp_id > (uint32_t)SDT_SIMM13_MAX)  {
1047c478bd9Sstevel@tonic-gate 		*instr++ = SDT_SETHI(sdp->sdp_id, SDT_REG_O0);
1057c478bd9Sstevel@tonic-gate 		*instr++ = SDT_ORLO(SDT_REG_O0, sdp->sdp_id, SDT_REG_O0);
1067c478bd9Sstevel@tonic-gate 	} else {
1077c478bd9Sstevel@tonic-gate 		*instr++ = SDT_ORSIMM13(SDT_REG_G0, sdp->sdp_id, SDT_REG_O0);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I0, SDT_REG_O1);
1117c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I1, SDT_REG_O2);
1127c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I2, SDT_REG_O3);
1137c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I3, SDT_REG_O4);
1147c478bd9Sstevel@tonic-gate 	*instr = SDT_CALL(instr, dtrace_probe);
1157c478bd9Sstevel@tonic-gate 	instr++;
1167c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I4, SDT_REG_O5);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	*instr++ = SDT_RET;
1197c478bd9Sstevel@tonic-gate 	*instr++ = SDT_RESTORE;
1207c478bd9Sstevel@tonic-gate 	*trampoline = instr;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1247c478bd9Sstevel@tonic-gate static void
1257c478bd9Sstevel@tonic-gate sdt_provide_module(void *arg, struct modctl *ctl)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
1287c478bd9Sstevel@tonic-gate 	char *modname = ctl->mod_modname;
1297c478bd9Sstevel@tonic-gate 	int primary, nprobes = 0;
1307c478bd9Sstevel@tonic-gate 	sdt_probedesc_t *sdpd;
1317c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp, *old;
1327c478bd9Sstevel@tonic-gate 	uint32_t *tab;
1337c478bd9Sstevel@tonic-gate 	sdt_provider_t *prov;
1347c478bd9Sstevel@tonic-gate 	int len;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * One for all, and all for one:  if we haven't yet registered all of
1387c478bd9Sstevel@tonic-gate 	 * our providers, we'll refuse to provide anything.
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
1417c478bd9Sstevel@tonic-gate 		if (prov->sdtp_id == DTRACE_PROVNONE)
1427c478bd9Sstevel@tonic-gate 			return;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
1467c478bd9Sstevel@tonic-gate 		return;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	kobj_textwin_alloc(mp);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * Hack to identify unix/genunix/krtld.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	primary = vmem_contains(heap_arena, (void *)ctl,
1547c478bd9Sstevel@tonic-gate 	    sizeof (struct modctl)) == 0;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * If there hasn't been an sdt table allocated, we'll do so now.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if (mp->sdt_tab == NULL) {
1607c478bd9Sstevel@tonic-gate 		for (; sdpd != NULL; sdpd = sdpd->sdpd_next) {
1617c478bd9Sstevel@tonic-gate 			nprobes++;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		/*
1657c478bd9Sstevel@tonic-gate 		 * We could (should?) determine precisely the size of the
1667c478bd9Sstevel@tonic-gate 		 * table -- but a reasonable maximum will suffice.
1677c478bd9Sstevel@tonic-gate 		 */
1687c478bd9Sstevel@tonic-gate 		mp->sdt_size = nprobes * SDT_ENTRY_SIZE;
1697c478bd9Sstevel@tonic-gate 		mp->sdt_tab = kobj_texthole_alloc(mp->text, mp->sdt_size);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		if (mp->sdt_tab == NULL) {
1727c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "couldn't allocate SDT table "
1737c478bd9Sstevel@tonic-gate 			    "for module %s", modname);
1747c478bd9Sstevel@tonic-gate 			return;
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	tab = (uint32_t *)mp->sdt_tab;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
1817c478bd9Sstevel@tonic-gate 		char *name = sdpd->sdpd_name, *func, *nname;
1827c478bd9Sstevel@tonic-gate 		int i, j;
1837c478bd9Sstevel@tonic-gate 		sdt_provider_t *prov;
1847c478bd9Sstevel@tonic-gate 		ulong_t offs;
1857c478bd9Sstevel@tonic-gate 		dtrace_id_t id;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
1887c478bd9Sstevel@tonic-gate 			char *prefix = prov->sdtp_prefix;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 			if (strncmp(name, prefix, strlen(prefix)) == 0) {
1917c478bd9Sstevel@tonic-gate 				name += strlen(prefix);
1927c478bd9Sstevel@tonic-gate 				break;
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; name[j] != '\0'; i++) {
1997c478bd9Sstevel@tonic-gate 			if (name[j] == '_' && name[j + 1] == '_') {
2007c478bd9Sstevel@tonic-gate 				nname[i] = '-';
2017c478bd9Sstevel@tonic-gate 				j += 2;
2027c478bd9Sstevel@tonic-gate 			} else {
2037c478bd9Sstevel@tonic-gate 				nname[i] = name[j++];
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		nname[i] = '\0';
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
2107c478bd9Sstevel@tonic-gate 		sdp->sdp_loadcnt = ctl->mod_loadcnt;
2117c478bd9Sstevel@tonic-gate 		sdp->sdp_primary = primary;
2127c478bd9Sstevel@tonic-gate 		sdp->sdp_ctl = ctl;
2137c478bd9Sstevel@tonic-gate 		sdp->sdp_name = nname;
2147c478bd9Sstevel@tonic-gate 		sdp->sdp_namelen = len;
2157c478bd9Sstevel@tonic-gate 		sdp->sdp_provider = prov;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		func = kobj_searchsym(mp, sdpd->sdpd_offset +
2187c478bd9Sstevel@tonic-gate 		    (uintptr_t)mp->text, &offs);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		if (func == NULL)
2217c478bd9Sstevel@tonic-gate 			func = "<unknown>";
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		/*
2247c478bd9Sstevel@tonic-gate 		 * We have our provider.  Now create the probe.
2257c478bd9Sstevel@tonic-gate 		 */
2267c478bd9Sstevel@tonic-gate 		if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
2277c478bd9Sstevel@tonic-gate 		    func, nname)) != DTRACE_IDNONE) {
2287c478bd9Sstevel@tonic-gate 			old = dtrace_probe_arg(prov->sdtp_id, id);
2297c478bd9Sstevel@tonic-gate 			ASSERT(old != NULL);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 			sdp->sdp_next = old->sdp_next;
2327c478bd9Sstevel@tonic-gate 			sdp->sdp_id = id;
2337c478bd9Sstevel@tonic-gate 			old->sdp_next = sdp;
2347c478bd9Sstevel@tonic-gate 		} else {
2357c478bd9Sstevel@tonic-gate 			sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
2367c478bd9Sstevel@tonic-gate 			    modname, func, nname, 1, sdp);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 			mp->sdt_nprobes++;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		sdp->sdp_patchval = SDT_CALL((uintptr_t)mp->text +
2427c478bd9Sstevel@tonic-gate 		    sdpd->sdpd_offset, tab);
2437c478bd9Sstevel@tonic-gate 		sdp->sdp_patchpoint = (uint32_t *)((uintptr_t)mp->textwin +
2447c478bd9Sstevel@tonic-gate 		    sdpd->sdpd_offset);
2457c478bd9Sstevel@tonic-gate 		sdp->sdp_savedval = *sdp->sdp_patchpoint;
2467c478bd9Sstevel@tonic-gate 		sdt_initialize(sdp, &tab);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2517c478bd9Sstevel@tonic-gate static void
2527c478bd9Sstevel@tonic-gate sdt_destroy(void *arg, dtrace_id_t id, void *parg)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp = parg, *old;
2557c478bd9Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
2587c478bd9Sstevel@tonic-gate 		if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
2597c478bd9Sstevel@tonic-gate 		    ctl->mod_loaded) || sdp->sdp_primary) {
2607c478bd9Sstevel@tonic-gate 			((struct module *)(ctl->mod_mp))->sdt_nprobes--;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
2657c478bd9Sstevel@tonic-gate 		old = sdp;
2667c478bd9Sstevel@tonic-gate 		kmem_free(sdp->sdp_name, sdp->sdp_namelen);
2677c478bd9Sstevel@tonic-gate 		sdp = sdp->sdp_next;
2687c478bd9Sstevel@tonic-gate 		kmem_free(old, sizeof (sdt_probe_t));
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2737c478bd9Sstevel@tonic-gate static void
2747c478bd9Sstevel@tonic-gate sdt_enable(void *arg, dtrace_id_t id, void *parg)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp = parg;
2777c478bd9Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled++;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * If this module has disappeared since we discovered its probes,
2837c478bd9Sstevel@tonic-gate 	 * refuse to enable it.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	if (!sdp->sdp_primary && !ctl->mod_loaded) {
2867c478bd9Sstevel@tonic-gate 		if (sdt_verbose) {
2877c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
2887c478bd9Sstevel@tonic-gate 			    "(module %s unloaded)",
2897c478bd9Sstevel@tonic-gate 			    sdp->sdp_name, ctl->mod_modname);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		goto err;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * Now check that our modctl has the expected load count.  If it
2967c478bd9Sstevel@tonic-gate 	 * doesn't, this module must have been unloaded and reloaded -- and
2977c478bd9Sstevel@tonic-gate 	 * we're not going to touch it.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
3007c478bd9Sstevel@tonic-gate 		if (sdt_verbose) {
3017c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
3027c478bd9Sstevel@tonic-gate 			    "(module %s reloaded)",
3037c478bd9Sstevel@tonic-gate 			    sdp->sdp_name, ctl->mod_modname);
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 		goto err;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
3097c478bd9Sstevel@tonic-gate 		*sdp->sdp_patchpoint = sdp->sdp_patchval;
3107c478bd9Sstevel@tonic-gate 		sdp = sdp->sdp_next;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate err:
3147c478bd9Sstevel@tonic-gate 	;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3187c478bd9Sstevel@tonic-gate static void
3197c478bd9Sstevel@tonic-gate sdt_disable(void *arg, dtrace_id_t id, void *parg)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp = parg;
3227c478bd9Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
3257c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled--;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if ((!sdp->sdp_primary && !ctl->mod_loaded) ||
3287c478bd9Sstevel@tonic-gate 	    (ctl->mod_loadcnt != sdp->sdp_loadcnt))
3297c478bd9Sstevel@tonic-gate 		goto err;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
3327c478bd9Sstevel@tonic-gate 		*sdp->sdp_patchpoint = sdp->sdp_savedval;
3337c478bd9Sstevel@tonic-gate 		sdp = sdp->sdp_next;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate err:
3377c478bd9Sstevel@tonic-gate 	;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static dtrace_pops_t sdt_pops = {
3417c478bd9Sstevel@tonic-gate 	NULL,
3427c478bd9Sstevel@tonic-gate 	sdt_provide_module,
3437c478bd9Sstevel@tonic-gate 	sdt_enable,
3447c478bd9Sstevel@tonic-gate 	sdt_disable,
3457c478bd9Sstevel@tonic-gate 	NULL,
3467c478bd9Sstevel@tonic-gate 	NULL,
3477c478bd9Sstevel@tonic-gate 	sdt_getargdesc,
3487c478bd9Sstevel@tonic-gate 	NULL,
3497c478bd9Sstevel@tonic-gate 	NULL,
3507c478bd9Sstevel@tonic-gate 	sdt_destroy
3517c478bd9Sstevel@tonic-gate };
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static int
3547c478bd9Sstevel@tonic-gate sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	sdt_provider_t *prov;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	switch (cmd) {
3597c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
3607c478bd9Sstevel@tonic-gate 		break;
3617c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3627c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3637c478bd9Sstevel@tonic-gate 	default:
3647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "sdt", S_IFCHR, 0,
3687c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE) {
3697c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
3707c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
3747c478bd9Sstevel@tonic-gate 	sdt_devi = devi;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
3777c478bd9Sstevel@tonic-gate 		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
378*ad4023c4Sdp 		    DTRACE_PRIV_KERNEL, NULL,
3797c478bd9Sstevel@tonic-gate 		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
3807c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to register sdt provider %s",
3817c478bd9Sstevel@tonic-gate 			    prov->sdtp_name);
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static int
3897c478bd9Sstevel@tonic-gate sdt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	sdt_provider_t *prov;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	switch (cmd) {
3947c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
3977c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3987c478bd9Sstevel@tonic-gate 	default:
3997c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
4037c478bd9Sstevel@tonic-gate 		if (prov->sdtp_id != DTRACE_PROVNONE) {
4047c478bd9Sstevel@tonic-gate 			if (dtrace_unregister(prov->sdtp_id) != 0)
4057c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
4067c478bd9Sstevel@tonic-gate 			prov->sdtp_id = DTRACE_PROVNONE;
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
4117c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4157c478bd9Sstevel@tonic-gate static int
4167c478bd9Sstevel@tonic-gate sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	int error;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	switch (infocmd) {
4217c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4227c478bd9Sstevel@tonic-gate 		*result = (void *)sdt_devi;
4237c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
4247c478bd9Sstevel@tonic-gate 		break;
4257c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4267c478bd9Sstevel@tonic-gate 		*result = (void *)0;
4277c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 	default:
4307c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	return (error);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4367c478bd9Sstevel@tonic-gate static int
4377c478bd9Sstevel@tonic-gate sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	return (0);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate static struct cb_ops sdt_cb_ops = {
4437c478bd9Sstevel@tonic-gate 	sdt_open,		/* open */
4447c478bd9Sstevel@tonic-gate 	nodev,			/* close */
4457c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
4467c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
4477c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
4487c478bd9Sstevel@tonic-gate 	nodev,			/* read */
4497c478bd9Sstevel@tonic-gate 	nodev,			/* write */
4507c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl */
4517c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
4527c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
4537c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
4547c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
4557c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
4567c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
4577c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
4587c478bd9Sstevel@tonic-gate };
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate static struct dev_ops sdt_ops = {
4617c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
4627c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
4637c478bd9Sstevel@tonic-gate 	sdt_info,		/* get_dev_info */
4647c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
4657c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
4667c478bd9Sstevel@tonic-gate 	sdt_attach,		/* attach */
4677c478bd9Sstevel@tonic-gate 	sdt_detach,		/* detach */
4687c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
4697c478bd9Sstevel@tonic-gate 	&sdt_cb_ops,		/* driver operations */
4707c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
4717c478bd9Sstevel@tonic-gate 	nodev			/* dev power */
4727c478bd9Sstevel@tonic-gate };
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
4787c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
4797c478bd9Sstevel@tonic-gate 	"Statically Defined Tracing",	/* name of module */
4807c478bd9Sstevel@tonic-gate 	&sdt_ops,		/* driver ops */
4817c478bd9Sstevel@tonic-gate };
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
4847c478bd9Sstevel@tonic-gate 	MODREV_1,
4857c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
4867c478bd9Sstevel@tonic-gate 	NULL
4877c478bd9Sstevel@tonic-gate };
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate int
4907c478bd9Sstevel@tonic-gate _init(void)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate int
4967c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate int
5027c478bd9Sstevel@tonic-gate _fini(void)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
5057c478bd9Sstevel@tonic-gate }
506