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