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
5ad4023c4Sdp * Common Development and Distribution License (the "License").
6ad4023c4Sdp * 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 /*
22b9e93c10SJonathan Haslam * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
26b0f673c4SBryan Cantrill /*
27b0f673c4SBryan Cantrill * Copyright (c) 2012, Joyent, Inc. All rights reserved.
288e50396aSAdam H. Leventhal * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
29*bb65110fSLuqman Aden * Copyright 2024 Oxide Computer Company
30b0f673c4SBryan Cantrill */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
357c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/stack.h>
4010e6dadfSbrendan #include <sys/frame.h>
4110e6dadfSbrendan #include <sys/dtrace_impl.h>
4210e6dadfSbrendan #include <sys/cmn_err.h>
4310e6dadfSbrendan #include <sys/sysmacros.h>
4410e6dadfSbrendan #include <sys/privregs.h>
457c478bd9Sstevel@tonic-gate #include <sys/sdt_impl.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define SDT_PATCHVAL 0xf0
487c478bd9Sstevel@tonic-gate #define SDT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & sdt_probetab_mask)
497c478bd9Sstevel@tonic-gate #define SDT_PROBETAB_SIZE 0x1000 /* 4k entries -- 16K total */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static dev_info_t *sdt_devi;
527c478bd9Sstevel@tonic-gate static int sdt_verbose = 0;
537c478bd9Sstevel@tonic-gate static sdt_probe_t **sdt_probetab;
547c478bd9Sstevel@tonic-gate static int sdt_probetab_size;
557c478bd9Sstevel@tonic-gate static int sdt_probetab_mask;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
587c478bd9Sstevel@tonic-gate static int
sdt_invop(uintptr_t addr,uintptr_t * stack,uintptr_t eax)597c478bd9Sstevel@tonic-gate sdt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate uintptr_t stack0, stack1, stack2, stack3, stack4;
627c478bd9Sstevel@tonic-gate int i = 0;
637c478bd9Sstevel@tonic-gate sdt_probe_t *sdt = sdt_probetab[SDT_ADDR2NDX(addr)];
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * On amd64, stack[0] contains the dereferenced stack pointer,
677c478bd9Sstevel@tonic-gate * stack[1] contains savfp, stack[2] contains savpc. We want
687c478bd9Sstevel@tonic-gate * to step over these entries.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate i += 3;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate for (; sdt != NULL; sdt = sdt->sdp_hashnext) {
737c478bd9Sstevel@tonic-gate if ((uintptr_t)sdt->sdp_patchpoint == addr) {
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate * When accessing the arguments on the stack, we must
767c478bd9Sstevel@tonic-gate * protect against accessing beyond the stack. We can
777c478bd9Sstevel@tonic-gate * safely set NOFAULT here -- we know that interrupts
787c478bd9Sstevel@tonic-gate * are already disabled.
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
817c478bd9Sstevel@tonic-gate stack0 = stack[i++];
827c478bd9Sstevel@tonic-gate stack1 = stack[i++];
837c478bd9Sstevel@tonic-gate stack2 = stack[i++];
847c478bd9Sstevel@tonic-gate stack3 = stack[i++];
857c478bd9Sstevel@tonic-gate stack4 = stack[i++];
867c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
877c478bd9Sstevel@tonic-gate CPU_DTRACE_BADADDR);
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate dtrace_probe(sdt->sdp_id, stack0, stack1,
907c478bd9Sstevel@tonic-gate stack2, stack3, stack4);
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate return (DTRACE_INVOP_NOP);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate return (0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1007c478bd9Sstevel@tonic-gate static void
sdt_provide_module(void * arg,struct modctl * ctl)1017c478bd9Sstevel@tonic-gate sdt_provide_module(void *arg, struct modctl *ctl)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate struct module *mp = ctl->mod_mp;
1047c478bd9Sstevel@tonic-gate char *modname = ctl->mod_modname;
1057c478bd9Sstevel@tonic-gate sdt_probedesc_t *sdpd;
1067c478bd9Sstevel@tonic-gate sdt_probe_t *sdp, *old;
1077c478bd9Sstevel@tonic-gate sdt_provider_t *prov;
1087c478bd9Sstevel@tonic-gate int len;
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * One for all, and all for one: if we haven't yet registered all of
1127c478bd9Sstevel@tonic-gate * our providers, we'll refuse to provide anything.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
1157c478bd9Sstevel@tonic-gate if (prov->sdtp_id == DTRACE_PROVNONE)
1167c478bd9Sstevel@tonic-gate return;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate if (mp->sdt_nprobes != 0 || (sdpd = mp->sdt_probes) == NULL)
1207c478bd9Sstevel@tonic-gate return;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate for (sdpd = mp->sdt_probes; sdpd != NULL; sdpd = sdpd->sdpd_next) {
1237c478bd9Sstevel@tonic-gate char *name = sdpd->sdpd_name, *func, *nname;
1247c478bd9Sstevel@tonic-gate int i, j;
1257c478bd9Sstevel@tonic-gate sdt_provider_t *prov;
1267c478bd9Sstevel@tonic-gate ulong_t offs;
1277c478bd9Sstevel@tonic-gate dtrace_id_t id;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate for (prov = sdt_providers; prov->sdtp_prefix != NULL; prov++) {
1307c478bd9Sstevel@tonic-gate char *prefix = prov->sdtp_prefix;
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate if (strncmp(name, prefix, strlen(prefix)) == 0) {
1337c478bd9Sstevel@tonic-gate name += strlen(prefix);
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate nname = kmem_alloc(len = strlen(name) + 1, KM_SLEEP);
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate for (i = 0, j = 0; name[j] != '\0'; i++) {
1417c478bd9Sstevel@tonic-gate if (name[j] == '_' && name[j + 1] == '_') {
1427c478bd9Sstevel@tonic-gate nname[i] = '-';
1437c478bd9Sstevel@tonic-gate j += 2;
1447c478bd9Sstevel@tonic-gate } else {
1457c478bd9Sstevel@tonic-gate nname[i] = name[j++];
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate nname[i] = '\0';
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate sdp = kmem_zalloc(sizeof (sdt_probe_t), KM_SLEEP);
1527c478bd9Sstevel@tonic-gate sdp->sdp_loadcnt = ctl->mod_loadcnt;
1537c478bd9Sstevel@tonic-gate sdp->sdp_ctl = ctl;
1547c478bd9Sstevel@tonic-gate sdp->sdp_name = nname;
1557c478bd9Sstevel@tonic-gate sdp->sdp_namelen = len;
1567c478bd9Sstevel@tonic-gate sdp->sdp_provider = prov;
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate func = kobj_searchsym(mp, sdpd->sdpd_offset, &offs);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate if (func == NULL)
1617c478bd9Sstevel@tonic-gate func = "<unknown>";
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * We have our provider. Now create the probe.
1657c478bd9Sstevel@tonic-gate */
1667c478bd9Sstevel@tonic-gate if ((id = dtrace_probe_lookup(prov->sdtp_id, modname,
1677c478bd9Sstevel@tonic-gate func, nname)) != DTRACE_IDNONE) {
1687c478bd9Sstevel@tonic-gate old = dtrace_probe_arg(prov->sdtp_id, id);
1697c478bd9Sstevel@tonic-gate ASSERT(old != NULL);
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate sdp->sdp_next = old->sdp_next;
1727c478bd9Sstevel@tonic-gate sdp->sdp_id = id;
1737c478bd9Sstevel@tonic-gate old->sdp_next = sdp;
1747c478bd9Sstevel@tonic-gate } else {
1757c478bd9Sstevel@tonic-gate sdp->sdp_id = dtrace_probe_create(prov->sdtp_id,
1767c478bd9Sstevel@tonic-gate modname, func, nname, 3, sdp);
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate mp->sdt_nprobes++;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate sdp->sdp_hashnext =
1827c478bd9Sstevel@tonic-gate sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)];
1837c478bd9Sstevel@tonic-gate sdt_probetab[SDT_ADDR2NDX(sdpd->sdpd_offset)] = sdp;
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate sdp->sdp_patchval = SDT_PATCHVAL;
1867c478bd9Sstevel@tonic-gate sdp->sdp_patchpoint = (uint8_t *)sdpd->sdpd_offset;
1877c478bd9Sstevel@tonic-gate sdp->sdp_savedval = *sdp->sdp_patchpoint;
188*bb65110fSLuqman Aden sdp->sdp_is_tailcall =
189*bb65110fSLuqman Aden sdp->sdp_patchpoint[SDT_OFF_RET_IDX] == SDT_RET;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1947c478bd9Sstevel@tonic-gate static void
sdt_destroy(void * arg,dtrace_id_t id,void * parg)1957c478bd9Sstevel@tonic-gate sdt_destroy(void *arg, dtrace_id_t id, void *parg)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate sdt_probe_t *sdp = parg, *old, *last, *hash;
1987c478bd9Sstevel@tonic-gate struct modctl *ctl = sdp->sdp_ctl;
1997c478bd9Sstevel@tonic-gate int ndx;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (ctl != NULL && ctl->mod_loadcnt == sdp->sdp_loadcnt) {
2027c478bd9Sstevel@tonic-gate if ((ctl->mod_loadcnt == sdp->sdp_loadcnt &&
2037c478bd9Sstevel@tonic-gate ctl->mod_loaded)) {
2047c478bd9Sstevel@tonic-gate ((struct module *)(ctl->mod_mp))->sdt_nprobes--;
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate while (sdp != NULL) {
2097c478bd9Sstevel@tonic-gate old = sdp;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate * Now we need to remove this probe from the sdt_probetab.
2137c478bd9Sstevel@tonic-gate */
2147c478bd9Sstevel@tonic-gate ndx = SDT_ADDR2NDX(sdp->sdp_patchpoint);
2157c478bd9Sstevel@tonic-gate last = NULL;
2167c478bd9Sstevel@tonic-gate hash = sdt_probetab[ndx];
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate while (hash != sdp) {
2197c478bd9Sstevel@tonic-gate ASSERT(hash != NULL);
2207c478bd9Sstevel@tonic-gate last = hash;
2217c478bd9Sstevel@tonic-gate hash = hash->sdp_hashnext;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate if (last != NULL) {
2257c478bd9Sstevel@tonic-gate last->sdp_hashnext = sdp->sdp_hashnext;
2267c478bd9Sstevel@tonic-gate } else {
2277c478bd9Sstevel@tonic-gate sdt_probetab[ndx] = sdp->sdp_hashnext;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate kmem_free(sdp->sdp_name, sdp->sdp_namelen);
2317c478bd9Sstevel@tonic-gate sdp = sdp->sdp_next;
2327c478bd9Sstevel@tonic-gate kmem_free(old, sizeof (sdt_probe_t));
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
237b9e93c10SJonathan Haslam static int
sdt_enable(void * arg,dtrace_id_t id,void * parg)2387c478bd9Sstevel@tonic-gate sdt_enable(void *arg, dtrace_id_t id, void *parg)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate sdt_probe_t *sdp = parg;
2417c478bd9Sstevel@tonic-gate struct modctl *ctl = sdp->sdp_ctl;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate ctl->mod_nenabled++;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * If this module has disappeared since we discovered its probes,
2477c478bd9Sstevel@tonic-gate * refuse to enable it.
2487c478bd9Sstevel@tonic-gate */
2497c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded) {
2507c478bd9Sstevel@tonic-gate if (sdt_verbose) {
2517c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "sdt is failing for probe %s "
2527c478bd9Sstevel@tonic-gate "(module %s unloaded)",
2537c478bd9Sstevel@tonic-gate sdp->sdp_name, ctl->mod_modname);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate goto err;
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * Now check that our modctl has the expected load count. If it
2607c478bd9Sstevel@tonic-gate * doesn't, this module must have been unloaded and reloaded -- and
2617c478bd9Sstevel@tonic-gate * we're not going to touch it.
2627c478bd9Sstevel@tonic-gate */
2637c478bd9Sstevel@tonic-gate if (ctl->mod_loadcnt != sdp->sdp_loadcnt) {
2647c478bd9Sstevel@tonic-gate if (sdt_verbose) {
2657c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "sdt is failing for probe %s "
2667c478bd9Sstevel@tonic-gate "(module %s reloaded)",
2677c478bd9Sstevel@tonic-gate sdp->sdp_name, ctl->mod_modname);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate goto err;
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate while (sdp != NULL) {
2737c478bd9Sstevel@tonic-gate *sdp->sdp_patchpoint = sdp->sdp_patchval;
2747c478bd9Sstevel@tonic-gate sdp = sdp->sdp_next;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate err:
277b9e93c10SJonathan Haslam return (0);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2817c478bd9Sstevel@tonic-gate static void
sdt_disable(void * arg,dtrace_id_t id,void * parg)2827c478bd9Sstevel@tonic-gate sdt_disable(void *arg, dtrace_id_t id, void *parg)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate sdt_probe_t *sdp = parg;
2857c478bd9Sstevel@tonic-gate struct modctl *ctl = sdp->sdp_ctl;
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate ctl->mod_nenabled--;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded || ctl->mod_loadcnt != sdp->sdp_loadcnt)
2907c478bd9Sstevel@tonic-gate goto err;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate while (sdp != NULL) {
2937c478bd9Sstevel@tonic-gate *sdp->sdp_patchpoint = sdp->sdp_savedval;
2947c478bd9Sstevel@tonic-gate sdp = sdp->sdp_next;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate err:
2987c478bd9Sstevel@tonic-gate ;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
30110e6dadfSbrendan /*ARGSUSED*/
30210e6dadfSbrendan uint64_t
sdt_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)30310e6dadfSbrendan sdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
30410e6dadfSbrendan {
305*bb65110fSLuqman Aden sdt_probe_t *sdp = parg;
30610e6dadfSbrendan uintptr_t val;
30710e6dadfSbrendan struct frame *fp = (struct frame *)dtrace_getfp();
30810e6dadfSbrendan uintptr_t *stack;
30910e6dadfSbrendan int i;
31010e6dadfSbrendan /*
31110e6dadfSbrendan * A total of 6 arguments are passed via registers; any argument with
31210e6dadfSbrendan * index of 5 or lower is therefore in a register.
31310e6dadfSbrendan */
31410e6dadfSbrendan int inreg = 5;
31510e6dadfSbrendan
31610e6dadfSbrendan for (i = 1; i <= aframes; i++) {
31710e6dadfSbrendan fp = (struct frame *)(fp->fr_savfp);
31810e6dadfSbrendan
31910e6dadfSbrendan if (fp->fr_savpc == (pc_t)dtrace_invop_callsite) {
32010e6dadfSbrendan /*
32110e6dadfSbrendan * In the case of amd64, we will use the pointer to the
32210e6dadfSbrendan * regs structure that was pushed when we took the
32310e6dadfSbrendan * trap. To get this structure, we must increment
3248e50396aSAdam H. Leventhal * beyond the frame structure, the calling RIP, and
3258e50396aSAdam H. Leventhal * padding stored in dtrace_invop(). If the argument
3268e50396aSAdam H. Leventhal * that we're seeking is passed on the stack, we'll
3278e50396aSAdam H. Leventhal * pull the true stack pointer out of the saved
3288e50396aSAdam H. Leventhal * registers and decrement our argument by the number
3298e50396aSAdam H. Leventhal * of arguments passed in registers; if the argument
33010e6dadfSbrendan * we're seeking is passed in regsiters, we can just
33110e6dadfSbrendan * load it directly.
33210e6dadfSbrendan */
333b0a27161Sbrendan struct regs *rp = (struct regs *)((uintptr_t)&fp[1] +
3348e50396aSAdam H. Leventhal sizeof (uintptr_t) * 2);
33510e6dadfSbrendan
33610e6dadfSbrendan if (argno <= inreg) {
33710e6dadfSbrendan stack = (uintptr_t *)&rp->r_rdi;
33810e6dadfSbrendan } else {
33910e6dadfSbrendan stack = (uintptr_t *)(rp->r_rsp);
340b0a27161Sbrendan argno -= (inreg + 1);
341*bb65110fSLuqman Aden
342*bb65110fSLuqman Aden /*
343*bb65110fSLuqman Aden * If the probe was invoked as a tail call, the
344*bb65110fSLuqman Aden * compiler leaves the stack as if we had just
345*bb65110fSLuqman Aden * entered the fictitious __dtrace_probe_[name]
346*bb65110fSLuqman Aden * function, meaning we need to skip over the
347*bb65110fSLuqman Aden * saved return address to get to the stack
348*bb65110fSLuqman Aden * arguments.
349*bb65110fSLuqman Aden */
350*bb65110fSLuqman Aden if (sdp->sdp_is_tailcall)
351*bb65110fSLuqman Aden argno++;
35210e6dadfSbrendan }
35310e6dadfSbrendan goto load;
35410e6dadfSbrendan }
35510e6dadfSbrendan }
35610e6dadfSbrendan
3576c10f5d0Sbrendan /*
3586c10f5d0Sbrendan * We know that we did not come through a trap to get into
3596c10f5d0Sbrendan * dtrace_probe() -- the provider simply called dtrace_probe()
3606c10f5d0Sbrendan * directly. As this is the case, we need to shift the argument
3616c10f5d0Sbrendan * that we're looking for: the probe ID is the first argument to
3626c10f5d0Sbrendan * dtrace_probe(), so the argument n will actually be found where
3636c10f5d0Sbrendan * one would expect to find argument (n + 1).
3646c10f5d0Sbrendan */
3656c10f5d0Sbrendan argno++;
3666c10f5d0Sbrendan
3676c10f5d0Sbrendan if (argno <= inreg) {
3686c10f5d0Sbrendan /*
3696c10f5d0Sbrendan * This shouldn't happen. If the argument is passed in a
3706c10f5d0Sbrendan * register then it should have been, well, passed in a
3716c10f5d0Sbrendan * register...
3726c10f5d0Sbrendan */
3736c10f5d0Sbrendan DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3746c10f5d0Sbrendan return (0);
3756c10f5d0Sbrendan }
3766c10f5d0Sbrendan
3776c10f5d0Sbrendan argno -= (inreg + 1);
3786c10f5d0Sbrendan stack = (uintptr_t *)&fp[1];
3796c10f5d0Sbrendan
38010e6dadfSbrendan load:
38110e6dadfSbrendan DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
38210e6dadfSbrendan val = stack[argno];
38310e6dadfSbrendan DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
38410e6dadfSbrendan
38510e6dadfSbrendan return (val);
38610e6dadfSbrendan }
38710e6dadfSbrendan
3887c478bd9Sstevel@tonic-gate static dtrace_pops_t sdt_pops = {
3897c478bd9Sstevel@tonic-gate NULL,
3907c478bd9Sstevel@tonic-gate sdt_provide_module,
3917c478bd9Sstevel@tonic-gate sdt_enable,
3927c478bd9Sstevel@tonic-gate sdt_disable,
3937c478bd9Sstevel@tonic-gate NULL,
3947c478bd9Sstevel@tonic-gate NULL,
3957c478bd9Sstevel@tonic-gate sdt_getargdesc,
39610e6dadfSbrendan sdt_getarg,
3977c478bd9Sstevel@tonic-gate NULL,
3987c478bd9Sstevel@tonic-gate sdt_destroy
3997c478bd9Sstevel@tonic-gate };
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4027c478bd9Sstevel@tonic-gate static int
sdt_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)4037c478bd9Sstevel@tonic-gate sdt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate sdt_provider_t *prov;
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "sdt", S_IFCHR,
408fa30bfacSToomas Soome 0, DDI_PSEUDO, 0) == DDI_FAILURE) {
4097c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "/dev/sdt couldn't create minor node");
4107c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
4117c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate ddi_report_dev(devi);
4157c478bd9Sstevel@tonic-gate sdt_devi = devi;
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate if (sdt_probetab_size == 0)
4187c478bd9Sstevel@tonic-gate sdt_probetab_size = SDT_PROBETAB_SIZE;
4197c478bd9Sstevel@tonic-gate
4207c478bd9Sstevel@tonic-gate sdt_probetab_mask = sdt_probetab_size - 1;
4217c478bd9Sstevel@tonic-gate sdt_probetab =
4227c478bd9Sstevel@tonic-gate kmem_zalloc(sdt_probetab_size * sizeof (sdt_probe_t *), KM_SLEEP);
4237c478bd9Sstevel@tonic-gate dtrace_invop_add(sdt_invop);
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
426b0f673c4SBryan Cantrill uint32_t priv;
427b0f673c4SBryan Cantrill
428b0f673c4SBryan Cantrill if (prov->sdtp_priv == DTRACE_PRIV_NONE) {
429b0f673c4SBryan Cantrill priv = DTRACE_PRIV_KERNEL;
430b0f673c4SBryan Cantrill sdt_pops.dtps_mode = NULL;
431b0f673c4SBryan Cantrill } else {
432b0f673c4SBryan Cantrill priv = prov->sdtp_priv;
433b0f673c4SBryan Cantrill ASSERT(priv == DTRACE_PRIV_USER);
434b0f673c4SBryan Cantrill sdt_pops.dtps_mode = sdt_mode;
435b0f673c4SBryan Cantrill }
436b0f673c4SBryan Cantrill
4377c478bd9Sstevel@tonic-gate if (dtrace_register(prov->sdtp_name, prov->sdtp_attr,
438b0f673c4SBryan Cantrill priv, NULL, &sdt_pops, prov, &prov->sdtp_id) != 0) {
4397c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to register sdt provider %s",
4407c478bd9Sstevel@tonic-gate prov->sdtp_name);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4487c478bd9Sstevel@tonic-gate static int
sdt_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4497c478bd9Sstevel@tonic-gate sdt_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate sdt_provider_t *prov;
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate switch (cmd) {
4547c478bd9Sstevel@tonic-gate case DDI_DETACH:
4557c478bd9Sstevel@tonic-gate break;
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate case DDI_SUSPEND:
4587c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate default:
4617c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate for (prov = sdt_providers; prov->sdtp_name != NULL; prov++) {
4657c478bd9Sstevel@tonic-gate if (prov->sdtp_id != DTRACE_PROVNONE) {
4667c478bd9Sstevel@tonic-gate if (dtrace_unregister(prov->sdtp_id) != 0)
4677c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate prov->sdtp_id = DTRACE_PROVNONE;
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate dtrace_invop_remove(sdt_invop);
4747c478bd9Sstevel@tonic-gate kmem_free(sdt_probetab, sdt_probetab_size * sizeof (sdt_probe_t *));
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4807c478bd9Sstevel@tonic-gate static int
sdt_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)4817c478bd9Sstevel@tonic-gate sdt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate int error;
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate switch (infocmd) {
4867c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
4877c478bd9Sstevel@tonic-gate *result = (void *)sdt_devi;
4887c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
4897c478bd9Sstevel@tonic-gate break;
4907c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
4917c478bd9Sstevel@tonic-gate *result = (void *)0;
4927c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
4937c478bd9Sstevel@tonic-gate break;
4947c478bd9Sstevel@tonic-gate default:
4957c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate return (error);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5017c478bd9Sstevel@tonic-gate static int
sdt_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)5027c478bd9Sstevel@tonic-gate sdt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate return (0);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate static struct cb_ops sdt_cb_ops = {
5087c478bd9Sstevel@tonic-gate sdt_open, /* open */
5097c478bd9Sstevel@tonic-gate nodev, /* close */
5107c478bd9Sstevel@tonic-gate nulldev, /* strategy */
5117c478bd9Sstevel@tonic-gate nulldev, /* print */
5127c478bd9Sstevel@tonic-gate nodev, /* dump */
5137c478bd9Sstevel@tonic-gate nodev, /* read */
5147c478bd9Sstevel@tonic-gate nodev, /* write */
5157c478bd9Sstevel@tonic-gate nodev, /* ioctl */
5167c478bd9Sstevel@tonic-gate nodev, /* devmap */
5177c478bd9Sstevel@tonic-gate nodev, /* mmap */
5187c478bd9Sstevel@tonic-gate nodev, /* segmap */
5197c478bd9Sstevel@tonic-gate nochpoll, /* poll */
5207c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
5217c478bd9Sstevel@tonic-gate 0, /* streamtab */
5227c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */
5237c478bd9Sstevel@tonic-gate };
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate static struct dev_ops sdt_ops = {
5267c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev, */
5277c478bd9Sstevel@tonic-gate 0, /* refcnt */
5287c478bd9Sstevel@tonic-gate sdt_info, /* get_dev_info */
5297c478bd9Sstevel@tonic-gate nulldev, /* identify */
5307c478bd9Sstevel@tonic-gate nulldev, /* probe */
5317c478bd9Sstevel@tonic-gate sdt_attach, /* attach */
5327c478bd9Sstevel@tonic-gate sdt_detach, /* detach */
5337c478bd9Sstevel@tonic-gate nodev, /* reset */
5347c478bd9Sstevel@tonic-gate &sdt_cb_ops, /* driver operations */
5357c478bd9Sstevel@tonic-gate NULL, /* bus operations */
53619397407SSherry Moore nodev, /* dev power */
53719397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
5387c478bd9Sstevel@tonic-gate };
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
5427c478bd9Sstevel@tonic-gate */
5437c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
5447c478bd9Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */
5457c478bd9Sstevel@tonic-gate "Statically Defined Tracing", /* name of module */
5467c478bd9Sstevel@tonic-gate &sdt_ops, /* driver ops */
5477c478bd9Sstevel@tonic-gate };
5487c478bd9Sstevel@tonic-gate
5497c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
5507c478bd9Sstevel@tonic-gate MODREV_1,
5517c478bd9Sstevel@tonic-gate (void *)&modldrv,
5527c478bd9Sstevel@tonic-gate NULL
5537c478bd9Sstevel@tonic-gate };
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate int
_init(void)5567c478bd9Sstevel@tonic-gate _init(void)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)5627c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate int
_fini(void)5687c478bd9Sstevel@tonic-gate _fini(void)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
5717c478bd9Sstevel@tonic-gate }
572