xref: /illumos-gate/usr/src/uts/common/os/dtrace_subr.c (revision 2570281cf351044b6936651ce26dbe1f801dcbd8)
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
5ab9a77c7Sahl  * Common Development and Distribution License (the "License").
6ab9a77c7Sahl  * 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  */
21ab9a77c7Sahl 
227c478bd9Sstevel@tonic-gate /*
23b9e93c10SJonathan Haslam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
259d7cab14SPatrick Mooney  * Copyright 2016 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
297c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
307c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
317c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
327c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef __sparc
367c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate void (*dtrace_cpu_init)(processorid_t);
407c478bd9Sstevel@tonic-gate void (*dtrace_modload)(struct modctl *);
417c478bd9Sstevel@tonic-gate void (*dtrace_modunload)(struct modctl *);
429d7cab14SPatrick Mooney void (*dtrace_helpers_cleanup)(proc_t *);
437c478bd9Sstevel@tonic-gate void (*dtrace_helpers_fork)(proc_t *, proc_t *);
447c478bd9Sstevel@tonic-gate void (*dtrace_cpustart_init)(void);
457c478bd9Sstevel@tonic-gate void (*dtrace_cpustart_fini)(void);
46b9e93c10SJonathan Haslam void (*dtrace_cpc_fire)(uint64_t);
47b0f673c4SBryan Cantrill void (*dtrace_closef)(void);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate void (*dtrace_debugger_init)(void);
507c478bd9Sstevel@tonic-gate void (*dtrace_debugger_fini)(void);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate dtrace_vtime_state_t dtrace_vtime_active = 0;
537c478bd9Sstevel@tonic-gate dtrace_cacheid_t dtrace_predcache_id = DTRACE_CACHEIDNONE + 1;
547c478bd9Sstevel@tonic-gate 
55b9e93c10SJonathan Haslam /*
56b9e93c10SJonathan Haslam  * dtrace_cpc_in_use usage statement: this global variable is used by the cpc
57b9e93c10SJonathan Haslam  * hardware overflow interrupt handler and the kernel cpc framework to check
58b9e93c10SJonathan Haslam  * whether or not the DTrace cpc provider is currently in use. The variable is
59b9e93c10SJonathan Haslam  * set before counters are enabled with the first enabling and cleared when
60b9e93c10SJonathan Haslam  * the last enabling is disabled. Its value at any given time indicates the
61b9e93c10SJonathan Haslam  * number of active dcpc based enablings. The global 'kcpc_cpuctx_lock' rwlock
62b9e93c10SJonathan Haslam  * is held during initial setting to protect races between kcpc_open() and the
63b9e93c10SJonathan Haslam  * first enabling. The locking provided by the DTrace subsystem, the kernel
64b9e93c10SJonathan Haslam  * cpc framework and the cpu management framework protect consumers from race
65b9e93c10SJonathan Haslam  * conditions on enabling and disabling probes.
66b9e93c10SJonathan Haslam  */
67b9e93c10SJonathan Haslam uint32_t dtrace_cpc_in_use = 0;
68b9e93c10SJonathan Haslam 
697c478bd9Sstevel@tonic-gate typedef struct dtrace_hrestime {
707c478bd9Sstevel@tonic-gate 	lock_t		dthr_lock;		/* lock for this element */
717c478bd9Sstevel@tonic-gate 	timestruc_t	dthr_hrestime;		/* hrestime value */
727c478bd9Sstevel@tonic-gate 	int64_t		dthr_adj;		/* hrestime_adj value */
737c478bd9Sstevel@tonic-gate 	hrtime_t	dthr_hrtime;		/* hrtime value */
747c478bd9Sstevel@tonic-gate } dtrace_hrestime_t;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static dtrace_hrestime_t dtrace_hrestime[2];
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Making available adjustable high-resolution time in DTrace is regrettably
807c478bd9Sstevel@tonic-gate  * more complicated than one might think it should be.  The problem is that
817c478bd9Sstevel@tonic-gate  * the variables related to adjusted high-resolution time (hrestime,
827c478bd9Sstevel@tonic-gate  * hrestime_adj and friends) are adjusted under hres_lock -- and this lock may
837c478bd9Sstevel@tonic-gate  * be held when we enter probe context.  One might think that we could address
847c478bd9Sstevel@tonic-gate  * this by having a single snapshot copy that is stored under a different lock
857c478bd9Sstevel@tonic-gate  * from hres_tick(), using the snapshot iff hres_lock is locked in probe
867c478bd9Sstevel@tonic-gate  * context.  Unfortunately, this too won't work:  because hres_lock is grabbed
877c478bd9Sstevel@tonic-gate  * in more than just hres_tick() context, we could enter probe context
887c478bd9Sstevel@tonic-gate  * concurrently on two different CPUs with both locks (hres_lock and the
897c478bd9Sstevel@tonic-gate  * snapshot lock) held.  As this implies, the fundamental problem is that we
907c478bd9Sstevel@tonic-gate  * need to have access to a snapshot of these variables that we _know_ will
917c478bd9Sstevel@tonic-gate  * not be locked in probe context.  To effect this, we have two snapshots
927c478bd9Sstevel@tonic-gate  * protected by two different locks, and we mandate that these snapshots are
937c478bd9Sstevel@tonic-gate  * recorded in succession by a single thread calling dtrace_hres_tick().  (We
947c478bd9Sstevel@tonic-gate  * assure this by calling it out of the same CY_HIGH_LEVEL cyclic that calls
957c478bd9Sstevel@tonic-gate  * hres_tick().)  A single thread can't be in two places at once:  one of the
967c478bd9Sstevel@tonic-gate  * snapshot locks is guaranteed to be unheld at all times.  The
977c478bd9Sstevel@tonic-gate  * dtrace_gethrestime() algorithm is thus to check first one snapshot and then
987c478bd9Sstevel@tonic-gate  * the other to find the unlocked snapshot.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate void
dtrace_hres_tick(void)1017c478bd9Sstevel@tonic-gate dtrace_hres_tick(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	int i;
1047c478bd9Sstevel@tonic-gate 	ushort_t spl;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
1077c478bd9Sstevel@tonic-gate 		dtrace_hrestime_t tmp;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		spl = hr_clock_lock();
1107c478bd9Sstevel@tonic-gate 		tmp.dthr_hrestime = hrestime;
1117c478bd9Sstevel@tonic-gate 		tmp.dthr_adj = hrestime_adj;
1127c478bd9Sstevel@tonic-gate 		tmp.dthr_hrtime = dtrace_gethrtime();
1137c478bd9Sstevel@tonic-gate 		hr_clock_unlock(spl);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		lock_set(&dtrace_hrestime[i].dthr_lock);
1167c478bd9Sstevel@tonic-gate 		dtrace_hrestime[i].dthr_hrestime = tmp.dthr_hrestime;
1177c478bd9Sstevel@tonic-gate 		dtrace_hrestime[i].dthr_adj = tmp.dthr_adj;
1187c478bd9Sstevel@tonic-gate 		dtrace_hrestime[i].dthr_hrtime = tmp.dthr_hrtime;
1197c478bd9Sstevel@tonic-gate 		dtrace_membar_producer();
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/*
1227c478bd9Sstevel@tonic-gate 		 * To allow for lock-free examination of this lock, we use
1237c478bd9Sstevel@tonic-gate 		 * the same trick that is used hres_lock; for more details,
1247c478bd9Sstevel@tonic-gate 		 * see the description of this technique in sun4u/sys/clock.h.
1257c478bd9Sstevel@tonic-gate 		 */
1267c478bd9Sstevel@tonic-gate 		dtrace_hrestime[i].dthr_lock++;
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate hrtime_t
dtrace_gethrestime(void)1317c478bd9Sstevel@tonic-gate dtrace_gethrestime(void)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	dtrace_hrestime_t snap;
1347c478bd9Sstevel@tonic-gate 	hrtime_t now;
1357c478bd9Sstevel@tonic-gate 	int i = 0, adj, nslt;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	for (;;) {
1387c478bd9Sstevel@tonic-gate 		snap.dthr_lock = dtrace_hrestime[i].dthr_lock;
1397c478bd9Sstevel@tonic-gate 		dtrace_membar_consumer();
1407c478bd9Sstevel@tonic-gate 		snap.dthr_hrestime = dtrace_hrestime[i].dthr_hrestime;
1417c478bd9Sstevel@tonic-gate 		snap.dthr_hrtime = dtrace_hrestime[i].dthr_hrtime;
1427c478bd9Sstevel@tonic-gate 		snap.dthr_adj = dtrace_hrestime[i].dthr_adj;
1437c478bd9Sstevel@tonic-gate 		dtrace_membar_consumer();
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		if ((snap.dthr_lock & ~1) == dtrace_hrestime[i].dthr_lock)
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		/*
1497c478bd9Sstevel@tonic-gate 		 * If we're here, the lock was either locked, or it
1507c478bd9Sstevel@tonic-gate 		 * transitioned while we were taking the snapshot.  Either
1517c478bd9Sstevel@tonic-gate 		 * way, we're going to try the other dtrace_hrestime element;
1527c478bd9Sstevel@tonic-gate 		 * we know that it isn't possible for both to be locked
1537c478bd9Sstevel@tonic-gate 		 * simultaneously, so we will ultimately get a good snapshot.
1547c478bd9Sstevel@tonic-gate 		 */
1557c478bd9Sstevel@tonic-gate 		i ^= 1;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * We have a good snapshot.  Now perform any necessary adjustments.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	nslt = dtrace_gethrtime() - snap.dthr_hrtime;
1627c478bd9Sstevel@tonic-gate 	ASSERT(nslt >= 0);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	now = ((hrtime_t)snap.dthr_hrestime.tv_sec * (hrtime_t)NANOSEC) +
1657c478bd9Sstevel@tonic-gate 	    snap.dthr_hrestime.tv_nsec;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (snap.dthr_adj != 0) {
1687c478bd9Sstevel@tonic-gate 		if (snap.dthr_adj > 0) {
1697c478bd9Sstevel@tonic-gate 			adj = (nslt >> adj_shift);
1707c478bd9Sstevel@tonic-gate 			if (adj > snap.dthr_adj)
1717c478bd9Sstevel@tonic-gate 				adj = (int)snap.dthr_adj;
1727c478bd9Sstevel@tonic-gate 		} else {
1737c478bd9Sstevel@tonic-gate 			adj = -(nslt >> adj_shift);
1747c478bd9Sstevel@tonic-gate 			if (adj < snap.dthr_adj)
1757c478bd9Sstevel@tonic-gate 				adj = (int)snap.dthr_adj;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 		now += adj;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	return (now);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate void
dtrace_vtime_enable(void)1847c478bd9Sstevel@tonic-gate dtrace_vtime_enable(void)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	dtrace_vtime_state_t state, nstate;
1877c478bd9Sstevel@tonic-gate 
188*c6f039c7SToomas Soome 	nstate = DTRACE_VTIME_INACTIVE;
1897c478bd9Sstevel@tonic-gate 	do {
1907c478bd9Sstevel@tonic-gate 		state = dtrace_vtime_active;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		switch (state) {
1937c478bd9Sstevel@tonic-gate 		case DTRACE_VTIME_INACTIVE:
1947c478bd9Sstevel@tonic-gate 			nstate = DTRACE_VTIME_ACTIVE;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case DTRACE_VTIME_ACTIVE:
1987c478bd9Sstevel@tonic-gate 			panic("DTrace virtual time already enabled");
1997c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 
20275d94465SJosef 'Jeff' Sipek 	} while	(atomic_cas_32((uint32_t *)&dtrace_vtime_active,
2037c478bd9Sstevel@tonic-gate 	    state, nstate) != state);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate void
dtrace_vtime_disable(void)2077c478bd9Sstevel@tonic-gate dtrace_vtime_disable(void)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	dtrace_vtime_state_t state, nstate;
2107c478bd9Sstevel@tonic-gate 
211*c6f039c7SToomas Soome 	nstate = DTRACE_VTIME_INACTIVE;
2127c478bd9Sstevel@tonic-gate 	do {
2137c478bd9Sstevel@tonic-gate 		state = dtrace_vtime_active;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		switch (state) {
2167c478bd9Sstevel@tonic-gate 		case DTRACE_VTIME_ACTIVE:
2177c478bd9Sstevel@tonic-gate 			nstate = DTRACE_VTIME_INACTIVE;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case DTRACE_VTIME_INACTIVE:
2217c478bd9Sstevel@tonic-gate 			panic("DTrace virtual time already disabled");
2227c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 
22575d94465SJosef 'Jeff' Sipek 	} while	(atomic_cas_32((uint32_t *)&dtrace_vtime_active,
2267c478bd9Sstevel@tonic-gate 	    state, nstate) != state);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate void
dtrace_vtime_switch(kthread_t * next)2307c478bd9Sstevel@tonic-gate dtrace_vtime_switch(kthread_t *next)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	dtrace_icookie_t cookie;
2337c478bd9Sstevel@tonic-gate 	hrtime_t ts;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	cookie = dtrace_interrupt_disable();
2367c478bd9Sstevel@tonic-gate 	ts = dtrace_gethrtime();
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (curthread->t_dtrace_start != 0) {
2397c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
2407c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_start = 0;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	next->t_dtrace_start = ts;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	dtrace_interrupt_enable(cookie);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate void (*dtrace_fasttrap_fork_ptr)(proc_t *, proc_t *);
2497c478bd9Sstevel@tonic-gate void (*dtrace_fasttrap_exec_ptr)(proc_t *);
2507c478bd9Sstevel@tonic-gate void (*dtrace_fasttrap_exit_ptr)(proc_t *);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * This function is called by cfork() in the event that it appears that
2547c478bd9Sstevel@tonic-gate  * there may be dtrace tracepoints active in the parent process's address
2557c478bd9Sstevel@tonic-gate  * space. This first confirms the existence of dtrace tracepoints in the
2567c478bd9Sstevel@tonic-gate  * parent process and calls into the fasttrap module to remove the
2577c478bd9Sstevel@tonic-gate  * corresponding tracepoints from the child. By knowing that there are
2587c478bd9Sstevel@tonic-gate  * existing tracepoints, and ensuring they can't be removed, we can rely
2597c478bd9Sstevel@tonic-gate  * on the fasttrap module remaining loaded.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate void
dtrace_fasttrap_fork(proc_t * p,proc_t * cp)2627c478bd9Sstevel@tonic-gate dtrace_fasttrap_fork(proc_t *p, proc_t *cp)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
265ab9a77c7Sahl 	ASSERT(p->p_dtrace_count > 0);
2667c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_fork_ptr != NULL);
2677c478bd9Sstevel@tonic-gate 
268ab9a77c7Sahl 	dtrace_fasttrap_fork_ptr(p, cp);
2697c478bd9Sstevel@tonic-gate }
270