xref: /titanic_51/usr/src/uts/sparc/dtrace/sdt.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
35*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/sdt_impl.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate static dev_info_t		*sdt_devi;
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate int sdt_verbose = 0;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	SDT_REG_G0		0
44*7c478bd9Sstevel@tonic-gate #define	SDT_REG_O0		8
45*7c478bd9Sstevel@tonic-gate #define	SDT_REG_O1		9
46*7c478bd9Sstevel@tonic-gate #define	SDT_REG_O2		10
47*7c478bd9Sstevel@tonic-gate #define	SDT_REG_O3		11
48*7c478bd9Sstevel@tonic-gate #define	SDT_REG_O4		12
49*7c478bd9Sstevel@tonic-gate #define	SDT_REG_O5		13
50*7c478bd9Sstevel@tonic-gate #define	SDT_REG_I0		24
51*7c478bd9Sstevel@tonic-gate #define	SDT_REG_I1		25
52*7c478bd9Sstevel@tonic-gate #define	SDT_REG_I2		26
53*7c478bd9Sstevel@tonic-gate #define	SDT_REG_I3		27
54*7c478bd9Sstevel@tonic-gate #define	SDT_REG_I4		28
55*7c478bd9Sstevel@tonic-gate #define	SDT_REG_I5		29
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #define	SDT_SIMM13_MASK		0x1fff
58*7c478bd9Sstevel@tonic-gate #define	SDT_SIMM13_MAX		((int32_t)0xfff)
59*7c478bd9Sstevel@tonic-gate #define	SDT_CALL(from, to)	(((uint32_t)1 << 30) | \
60*7c478bd9Sstevel@tonic-gate 				(((uintptr_t)(to) - (uintptr_t)(from) >> 2) & \
61*7c478bd9Sstevel@tonic-gate 				0x3fffffff))
62*7c478bd9Sstevel@tonic-gate #define	SDT_SAVE		(0x9de3a000 | (-SA(MINFRAME) & SDT_SIMM13_MASK))
63*7c478bd9Sstevel@tonic-gate #define	SDT_RET			0x81c7e008
64*7c478bd9Sstevel@tonic-gate #define	SDT_RESTORE		0x81e80000
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #define	SDT_OP_SETHI		0x1000000
67*7c478bd9Sstevel@tonic-gate #define	SDT_OP_OR		0x80100000
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate #define	SDT_FMT2_RD_SHIFT	25
70*7c478bd9Sstevel@tonic-gate #define	SDT_IMM22_SHIFT		10
71*7c478bd9Sstevel@tonic-gate #define	SDT_IMM22_MASK		0x3fffff
72*7c478bd9Sstevel@tonic-gate #define	SDT_IMM10_MASK		0x3ff
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #define	SDT_FMT3_RD_SHIFT	25
75*7c478bd9Sstevel@tonic-gate #define	SDT_FMT3_RS1_SHIFT	14
76*7c478bd9Sstevel@tonic-gate #define	SDT_FMT3_RS2_SHIFT	0
77*7c478bd9Sstevel@tonic-gate #define	SDT_FMT3_IMM		(1 << 13)
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #define	SDT_MOV(rs, rd) \
80*7c478bd9Sstevel@tonic-gate 	(SDT_OP_OR | (SDT_REG_G0 << SDT_FMT3_RS1_SHIFT) | \
81*7c478bd9Sstevel@tonic-gate 	((rs) << SDT_FMT3_RS2_SHIFT) | ((rd) << SDT_FMT3_RD_SHIFT))
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #define	SDT_ORLO(rs, val, rd) \
84*7c478bd9Sstevel@tonic-gate 	(SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
85*7c478bd9Sstevel@tonic-gate 	((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_IMM10_MASK))
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate #define	SDT_ORSIMM13(rs, val, rd) \
88*7c478bd9Sstevel@tonic-gate 	(SDT_OP_OR | ((rs) << SDT_FMT3_RS1_SHIFT) | \
89*7c478bd9Sstevel@tonic-gate 	((rd) << SDT_FMT3_RD_SHIFT) | SDT_FMT3_IMM | ((val) & SDT_SIMM13_MASK))
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #define	SDT_SETHI(val, reg)	\
92*7c478bd9Sstevel@tonic-gate 	(SDT_OP_SETHI | (reg << SDT_FMT2_RD_SHIFT) | \
93*7c478bd9Sstevel@tonic-gate 	((val >> SDT_IMM22_SHIFT) & SDT_IMM22_MASK))
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate #define	SDT_ENTRY_SIZE	(11 * sizeof (uint32_t))
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate static void
98*7c478bd9Sstevel@tonic-gate sdt_initialize(sdt_probe_t *sdp, uint32_t **trampoline)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	uint32_t *instr = *trampoline;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_SAVE;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (sdp->sdp_id > (uint32_t)SDT_SIMM13_MAX)  {
105*7c478bd9Sstevel@tonic-gate 		*instr++ = SDT_SETHI(sdp->sdp_id, SDT_REG_O0);
106*7c478bd9Sstevel@tonic-gate 		*instr++ = SDT_ORLO(SDT_REG_O0, sdp->sdp_id, SDT_REG_O0);
107*7c478bd9Sstevel@tonic-gate 	} else {
108*7c478bd9Sstevel@tonic-gate 		*instr++ = SDT_ORSIMM13(SDT_REG_G0, sdp->sdp_id, SDT_REG_O0);
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I0, SDT_REG_O1);
112*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I1, SDT_REG_O2);
113*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I2, SDT_REG_O3);
114*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I3, SDT_REG_O4);
115*7c478bd9Sstevel@tonic-gate 	*instr = SDT_CALL(instr, dtrace_probe);
116*7c478bd9Sstevel@tonic-gate 	instr++;
117*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_MOV(SDT_REG_I4, SDT_REG_O5);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_RET;
120*7c478bd9Sstevel@tonic-gate 	*instr++ = SDT_RESTORE;
121*7c478bd9Sstevel@tonic-gate 	*trampoline = instr;
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
125*7c478bd9Sstevel@tonic-gate static void
126*7c478bd9Sstevel@tonic-gate sdt_provide_module(void *arg, struct modctl *ctl)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
129*7c478bd9Sstevel@tonic-gate 	char *modname = ctl->mod_modname;
130*7c478bd9Sstevel@tonic-gate 	int primary, nprobes = 0;
131*7c478bd9Sstevel@tonic-gate 	sdt_probedesc_t *sdpd;
132*7c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp, *old;
133*7c478bd9Sstevel@tonic-gate 	uint32_t *tab;
134*7c478bd9Sstevel@tonic-gate 	sdt_provider_t *prov;
135*7c478bd9Sstevel@tonic-gate 	int len;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/*
138*7c478bd9Sstevel@tonic-gate 	 * One for all, and all for one:  if we haven't yet registered all of
139*7c478bd9Sstevel@tonic-gate 	 * our providers, we'll refuse to provide anything.
140*7c478bd9Sstevel@tonic-gate 	 */
141*7c478bd9Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
142*7c478bd9Sstevel@tonic-gate 		if (prov->sdtp_id == DTRACE_PROVNONE)
143*7c478bd9Sstevel@tonic-gate 			return;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
147*7c478bd9Sstevel@tonic-gate 		return;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	kobj_textwin_alloc(mp);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/*
152*7c478bd9Sstevel@tonic-gate 	 * Hack to identify unix/genunix/krtld.
153*7c478bd9Sstevel@tonic-gate 	 */
154*7c478bd9Sstevel@tonic-gate 	primary = vmem_contains(heap_arena, (void *)ctl,
155*7c478bd9Sstevel@tonic-gate 	    sizeof (struct modctl)) == 0;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	/*
158*7c478bd9Sstevel@tonic-gate 	 * If there hasn't been an sdt table allocated, we'll do so now.
159*7c478bd9Sstevel@tonic-gate 	 */
160*7c478bd9Sstevel@tonic-gate 	if (mp->sdt_tab == NULL) {
161*7c478bd9Sstevel@tonic-gate 		for (; sdpd != NULL; sdpd = sdpd->sdpd_next) {
162*7c478bd9Sstevel@tonic-gate 			nprobes++;
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		/*
166*7c478bd9Sstevel@tonic-gate 		 * We could (should?) determine precisely the size of the
167*7c478bd9Sstevel@tonic-gate 		 * table -- but a reasonable maximum will suffice.
168*7c478bd9Sstevel@tonic-gate 		 */
169*7c478bd9Sstevel@tonic-gate 		mp->sdt_size = nprobes * SDT_ENTRY_SIZE;
170*7c478bd9Sstevel@tonic-gate 		mp->sdt_tab = kobj_texthole_alloc(mp->text, mp->sdt_size);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 		if (mp->sdt_tab == NULL) {
173*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "couldn't allocate SDT table "
174*7c478bd9Sstevel@tonic-gate 			    "for module %s", modname);
175*7c478bd9Sstevel@tonic-gate 			return;
176*7c478bd9Sstevel@tonic-gate 		}
177*7c478bd9Sstevel@tonic-gate 	}
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	tab = (uint32_t *)mp->sdt_tab;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
182*7c478bd9Sstevel@tonic-gate 		char *name = sdpd->sdpd_name, *func, *nname;
183*7c478bd9Sstevel@tonic-gate 		int i, j;
184*7c478bd9Sstevel@tonic-gate 		sdt_provider_t *prov;
185*7c478bd9Sstevel@tonic-gate 		ulong_t offs;
186*7c478bd9Sstevel@tonic-gate 		dtrace_id_t id;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
189*7c478bd9Sstevel@tonic-gate 			char *prefix = prov->sdtp_prefix;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 			if (strncmp(name, prefix, strlen(prefix)) == 0) {
192*7c478bd9Sstevel@tonic-gate 				name += strlen(prefix);
193*7c478bd9Sstevel@tonic-gate 				break;
194*7c478bd9Sstevel@tonic-gate 			}
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 		nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; name[j] != '\0'; i++) {
200*7c478bd9Sstevel@tonic-gate 			if (name[j] == '_' && name[j + 1] == '_') {
201*7c478bd9Sstevel@tonic-gate 				nname[i] = '-';
202*7c478bd9Sstevel@tonic-gate 				j += 2;
203*7c478bd9Sstevel@tonic-gate 			} else {
204*7c478bd9Sstevel@tonic-gate 				nname[i] = name[j++];
205*7c478bd9Sstevel@tonic-gate 			}
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		nname[i] = '\0';
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
211*7c478bd9Sstevel@tonic-gate 		sdp->sdp_loadcnt = ctl->mod_loadcnt;
212*7c478bd9Sstevel@tonic-gate 		sdp->sdp_primary = primary;
213*7c478bd9Sstevel@tonic-gate 		sdp->sdp_ctl = ctl;
214*7c478bd9Sstevel@tonic-gate 		sdp->sdp_name = nname;
215*7c478bd9Sstevel@tonic-gate 		sdp->sdp_namelen = len;
216*7c478bd9Sstevel@tonic-gate 		sdp->sdp_provider = prov;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 		func = kobj_searchsym(mp, sdpd->sdpd_offset +
219*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)mp->text, &offs);
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		if (func == NULL)
222*7c478bd9Sstevel@tonic-gate 			func = "<unknown>";
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		/*
225*7c478bd9Sstevel@tonic-gate 		 * We have our provider.  Now create the probe.
226*7c478bd9Sstevel@tonic-gate 		 */
227*7c478bd9Sstevel@tonic-gate 		if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
228*7c478bd9Sstevel@tonic-gate 		    func, nname)) != DTRACE_IDNONE) {
229*7c478bd9Sstevel@tonic-gate 			old = dtrace_probe_arg(prov->sdtp_id, id);
230*7c478bd9Sstevel@tonic-gate 			ASSERT(old != NULL);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 			sdp->sdp_next = old->sdp_next;
233*7c478bd9Sstevel@tonic-gate 			sdp->sdp_id = id;
234*7c478bd9Sstevel@tonic-gate 			old->sdp_next = sdp;
235*7c478bd9Sstevel@tonic-gate 		} else {
236*7c478bd9Sstevel@tonic-gate 			sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
237*7c478bd9Sstevel@tonic-gate 			    modname, func, nname, 1, sdp);
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 			mp->sdt_nprobes++;
240*7c478bd9Sstevel@tonic-gate 		}
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		sdp->sdp_patchval = SDT_CALL((uintptr_t)mp->text +
243*7c478bd9Sstevel@tonic-gate 		    sdpd->sdpd_offset, tab);
244*7c478bd9Sstevel@tonic-gate 		sdp->sdp_patchpoint = (uint32_t *)((uintptr_t)mp->textwin +
245*7c478bd9Sstevel@tonic-gate 		    sdpd->sdpd_offset);
246*7c478bd9Sstevel@tonic-gate 		sdp->sdp_savedval = *sdp->sdp_patchpoint;
247*7c478bd9Sstevel@tonic-gate 		sdt_initialize(sdp, &tab);
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
252*7c478bd9Sstevel@tonic-gate static void
253*7c478bd9Sstevel@tonic-gate sdt_destroy(void *arg, dtrace_id_t id, void *parg)
254*7c478bd9Sstevel@tonic-gate {
255*7c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp = parg, *old;
256*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
259*7c478bd9Sstevel@tonic-gate 		if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
260*7c478bd9Sstevel@tonic-gate 		    ctl->mod_loaded) || sdp->sdp_primary) {
261*7c478bd9Sstevel@tonic-gate 			((struct module *)(ctl->mod_mp))->sdt_nprobes--;
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
266*7c478bd9Sstevel@tonic-gate 		old = sdp;
267*7c478bd9Sstevel@tonic-gate 		kmem_free(sdp->sdp_name, sdp->sdp_namelen);
268*7c478bd9Sstevel@tonic-gate 		sdp = sdp->sdp_next;
269*7c478bd9Sstevel@tonic-gate 		kmem_free(old, sizeof (sdt_probe_t));
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
274*7c478bd9Sstevel@tonic-gate static void
275*7c478bd9Sstevel@tonic-gate sdt_enable(void *arg, dtrace_id_t id, void *parg)
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp = parg;
278*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled++;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	/*
283*7c478bd9Sstevel@tonic-gate 	 * If this module has disappeared since we discovered its probes,
284*7c478bd9Sstevel@tonic-gate 	 * refuse to enable it.
285*7c478bd9Sstevel@tonic-gate 	 */
286*7c478bd9Sstevel@tonic-gate 	if (!sdp->sdp_primary && !ctl->mod_loaded) {
287*7c478bd9Sstevel@tonic-gate 		if (sdt_verbose) {
288*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
289*7c478bd9Sstevel@tonic-gate 			    "(module %s unloaded)",
290*7c478bd9Sstevel@tonic-gate 			    sdp->sdp_name, ctl->mod_modname);
291*7c478bd9Sstevel@tonic-gate 		}
292*7c478bd9Sstevel@tonic-gate 		goto err;
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	/*
296*7c478bd9Sstevel@tonic-gate 	 * Now check that our modctl has the expected load count.  If it
297*7c478bd9Sstevel@tonic-gate 	 * doesn't, this module must have been unloaded and reloaded -- and
298*7c478bd9Sstevel@tonic-gate 	 * we're not going to touch it.
299*7c478bd9Sstevel@tonic-gate 	 */
300*7c478bd9Sstevel@tonic-gate 	if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
301*7c478bd9Sstevel@tonic-gate 		if (sdt_verbose) {
302*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "sdt is failing for probe %s "
303*7c478bd9Sstevel@tonic-gate 			    "(module %s reloaded)",
304*7c478bd9Sstevel@tonic-gate 			    sdp->sdp_name, ctl->mod_modname);
305*7c478bd9Sstevel@tonic-gate 		}
306*7c478bd9Sstevel@tonic-gate 		goto err;
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
310*7c478bd9Sstevel@tonic-gate 		*sdp->sdp_patchpoint = sdp->sdp_patchval;
311*7c478bd9Sstevel@tonic-gate 		sdp = sdp->sdp_next;
312*7c478bd9Sstevel@tonic-gate 	}
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate err:
315*7c478bd9Sstevel@tonic-gate 	;
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
319*7c478bd9Sstevel@tonic-gate static void
320*7c478bd9Sstevel@tonic-gate sdt_disable(void *arg, dtrace_id_t id, void *parg)
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	sdt_probe_t *sdp = parg;
323*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = sdp->sdp_ctl;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
326*7c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled--;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	if ((!sdp->sdp_primary && !ctl->mod_loaded) ||
329*7c478bd9Sstevel@tonic-gate 	    (ctl->mod_loadcnt != sdp->sdp_loadcnt))
330*7c478bd9Sstevel@tonic-gate 		goto err;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
333*7c478bd9Sstevel@tonic-gate 		*sdp->sdp_patchpoint = sdp->sdp_savedval;
334*7c478bd9Sstevel@tonic-gate 		sdp = sdp->sdp_next;
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate err:
338*7c478bd9Sstevel@tonic-gate 	;
339*7c478bd9Sstevel@tonic-gate }
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate static dtrace_pops_t sdt_pops = {
342*7c478bd9Sstevel@tonic-gate 	NULL,
343*7c478bd9Sstevel@tonic-gate 	sdt_provide_module,
344*7c478bd9Sstevel@tonic-gate 	sdt_enable,
345*7c478bd9Sstevel@tonic-gate 	sdt_disable,
346*7c478bd9Sstevel@tonic-gate 	NULL,
347*7c478bd9Sstevel@tonic-gate 	NULL,
348*7c478bd9Sstevel@tonic-gate 	sdt_getargdesc,
349*7c478bd9Sstevel@tonic-gate 	NULL,
350*7c478bd9Sstevel@tonic-gate 	NULL,
351*7c478bd9Sstevel@tonic-gate 	sdt_destroy
352*7c478bd9Sstevel@tonic-gate };
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate static int
355*7c478bd9Sstevel@tonic-gate sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 	sdt_provider_t *prov;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
360*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
361*7c478bd9Sstevel@tonic-gate 		break;
362*7c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
363*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
364*7c478bd9Sstevel@tonic-gate 	default:
365*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
366*7c478bd9Sstevel@tonic-gate 	}
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "sdt", S_IFCHR, 0,
369*7c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE) {
370*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
371*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
375*7c478bd9Sstevel@tonic-gate 	sdt_devi = devi;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
378*7c478bd9Sstevel@tonic-gate 		if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
379*7c478bd9Sstevel@tonic-gate 		    DTRACE_PRIV_KERNEL, 0,
380*7c478bd9Sstevel@tonic-gate 		    &sdt_pops, prov, &prov->sdtp_id) != 0) {
381*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to register sdt provider %s",
382*7c478bd9Sstevel@tonic-gate 			    prov->sdtp_name);
383*7c478bd9Sstevel@tonic-gate 		}
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
387*7c478bd9Sstevel@tonic-gate }
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate static int
390*7c478bd9Sstevel@tonic-gate sdt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
391*7c478bd9Sstevel@tonic-gate {
392*7c478bd9Sstevel@tonic-gate 	sdt_provider_t *prov;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
395*7c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
396*7c478bd9Sstevel@tonic-gate 		break;
397*7c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
398*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
399*7c478bd9Sstevel@tonic-gate 	default:
400*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
401*7c478bd9Sstevel@tonic-gate 	}
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
404*7c478bd9Sstevel@tonic-gate 		if (prov->sdtp_id != DTRACE_PROVNONE) {
405*7c478bd9Sstevel@tonic-gate 			if (dtrace_unregister(prov->sdtp_id) != 0)
406*7c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
407*7c478bd9Sstevel@tonic-gate 			prov->sdtp_id = DTRACE_PROVNONE;
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 	}
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
412*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
413*7c478bd9Sstevel@tonic-gate }
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
416*7c478bd9Sstevel@tonic-gate static int
417*7c478bd9Sstevel@tonic-gate sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	int error;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
422*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
423*7c478bd9Sstevel@tonic-gate 		*result = (void *)sdt_devi;
424*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
425*7c478bd9Sstevel@tonic-gate 		break;
426*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
427*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
428*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
429*7c478bd9Sstevel@tonic-gate 		break;
430*7c478bd9Sstevel@tonic-gate 	default:
431*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
432*7c478bd9Sstevel@tonic-gate 	}
433*7c478bd9Sstevel@tonic-gate 	return (error);
434*7c478bd9Sstevel@tonic-gate }
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
437*7c478bd9Sstevel@tonic-gate static int
438*7c478bd9Sstevel@tonic-gate sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
439*7c478bd9Sstevel@tonic-gate {
440*7c478bd9Sstevel@tonic-gate 	return (0);
441*7c478bd9Sstevel@tonic-gate }
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate static struct cb_ops sdt_cb_ops = {
444*7c478bd9Sstevel@tonic-gate 	sdt_open,		/* open */
445*7c478bd9Sstevel@tonic-gate 	nodev,			/* close */
446*7c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
447*7c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
448*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
449*7c478bd9Sstevel@tonic-gate 	nodev,			/* read */
450*7c478bd9Sstevel@tonic-gate 	nodev,			/* write */
451*7c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl */
452*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
453*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
454*7c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
455*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
456*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
457*7c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
458*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
459*7c478bd9Sstevel@tonic-gate };
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate static struct dev_ops sdt_ops = {
462*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
463*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
464*7c478bd9Sstevel@tonic-gate 	sdt_info,		/* get_dev_info */
465*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
466*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
467*7c478bd9Sstevel@tonic-gate 	sdt_attach,		/* attach */
468*7c478bd9Sstevel@tonic-gate 	sdt_detach,		/* detach */
469*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
470*7c478bd9Sstevel@tonic-gate 	&sdt_cb_ops,		/* driver operations */
471*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
472*7c478bd9Sstevel@tonic-gate 	nodev			/* dev power */
473*7c478bd9Sstevel@tonic-gate };
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /*
476*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
477*7c478bd9Sstevel@tonic-gate  */
478*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
479*7c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
480*7c478bd9Sstevel@tonic-gate 	"Statically Defined Tracing",	/* name of module */
481*7c478bd9Sstevel@tonic-gate 	&sdt_ops,		/* driver ops */
482*7c478bd9Sstevel@tonic-gate };
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
485*7c478bd9Sstevel@tonic-gate 	MODREV_1,
486*7c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
487*7c478bd9Sstevel@tonic-gate 	NULL
488*7c478bd9Sstevel@tonic-gate };
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate int
491*7c478bd9Sstevel@tonic-gate _init(void)
492*7c478bd9Sstevel@tonic-gate {
493*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate int
497*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
498*7c478bd9Sstevel@tonic-gate {
499*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
500*7c478bd9Sstevel@tonic-gate }
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate int
503*7c478bd9Sstevel@tonic-gate _fini(void)
504*7c478bd9Sstevel@tonic-gate {
505*7c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
506*7c478bd9Sstevel@tonic-gate }
507