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