summaryrefslogtreecommitdiffstats
path: root/src/VBox/ExtPacks/VBoxDTrace/onnv/uts/common/os/dtrace_subr.c
blob: f2a9ac1b7d545f1c8d1be9a3ea371cf0fd9623c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/dtrace.h>
#include <sys/cmn_err.h>
#include <sys/tnf.h>
#include <sys/atomic.h>
#include <sys/prsystm.h>
#include <sys/modctl.h>
#include <sys/aio_impl.h>

#ifdef __sparc
#include <sys/privregs.h>
#endif

void (*dtrace_cpu_init)(processorid_t);
void (*dtrace_modload)(struct modctl *);
void (*dtrace_modunload)(struct modctl *);
void (*dtrace_helpers_cleanup)(void);
void (*dtrace_helpers_fork)(proc_t *, proc_t *);
void (*dtrace_cpustart_init)(void);
void (*dtrace_cpustart_fini)(void);
void (*dtrace_cpc_fire)(uint64_t);

void (*dtrace_debugger_init)(void);
void (*dtrace_debugger_fini)(void);

dtrace_vtime_state_t dtrace_vtime_active = 0;
dtrace_cacheid_t dtrace_predcache_id = DTRACE_CACHEIDNONE + 1;

/*
 * dtrace_cpc_in_use usage statement: this global variable is used by the cpc
 * hardware overflow interrupt handler and the kernel cpc framework to check
 * whether or not the DTrace cpc provider is currently in use. The variable is
 * set before counters are enabled with the first enabling and cleared when
 * the last enabling is disabled. Its value at any given time indicates the
 * number of active dcpc based enablings. The global 'kcpc_cpuctx_lock' rwlock
 * is held during initial setting to protect races between kcpc_open() and the
 * first enabling. The locking provided by the DTrace subsystem, the kernel
 * cpc framework and the cpu management framework protect consumers from race
 * conditions on enabling and disabling probes.
 */
uint32_t dtrace_cpc_in_use = 0;

typedef struct dtrace_hrestime {
	lock_t		dthr_lock;		/* lock for this element */
	timestruc_t	dthr_hrestime;		/* hrestime value */
	int64_t		dthr_adj;		/* hrestime_adj value */
	hrtime_t	dthr_hrtime;		/* hrtime value */
} dtrace_hrestime_t;

static dtrace_hrestime_t dtrace_hrestime[2];

/*
 * Making available adjustable high-resolution time in DTrace is regrettably
 * more complicated than one might think it should be.  The problem is that
 * the variables related to adjusted high-resolution time (hrestime,
 * hrestime_adj and friends) are adjusted under hres_lock -- and this lock may
 * be held when we enter probe context.  One might think that we could address
 * this by having a single snapshot copy that is stored under a different lock
 * from hres_tick(), using the snapshot iff hres_lock is locked in probe
 * context.  Unfortunately, this too won't work:  because hres_lock is grabbed
 * in more than just hres_tick() context, we could enter probe context
 * concurrently on two different CPUs with both locks (hres_lock and the
 * snapshot lock) held.  As this implies, the fundamental problem is that we
 * need to have access to a snapshot of these variables that we _know_ will
 * not be locked in probe context.  To effect this, we have two snapshots
 * protected by two different locks, and we mandate that these snapshots are
 * recorded in succession by a single thread calling dtrace_hres_tick().  (We
 * assure this by calling it out of the same CY_HIGH_LEVEL cyclic that calls
 * hres_tick().)  A single thread can't be in two places at once:  one of the
 * snapshot locks is guaranteed to be unheld at all times.  The
 * dtrace_gethrestime() algorithm is thus to check first one snapshot and then
 * the other to find the unlocked snapshot.
 */
void
dtrace_hres_tick(void)
{
	int i;
	ushort_t spl;

	for (i = 0; i < 2; i++) {
		dtrace_hrestime_t tmp;

		spl = hr_clock_lock();
		tmp.dthr_hrestime = hrestime;
		tmp.dthr_adj = hrestime_adj;
		tmp.dthr_hrtime = dtrace_gethrtime();
		hr_clock_unlock(spl);

		lock_set(&dtrace_hrestime[i].dthr_lock);
		dtrace_hrestime[i].dthr_hrestime = tmp.dthr_hrestime;
		dtrace_hrestime[i].dthr_adj = tmp.dthr_adj;
		dtrace_hrestime[i].dthr_hrtime = tmp.dthr_hrtime;
		dtrace_membar_producer();

		/*
		 * To allow for lock-free examination of this lock, we use
		 * the same trick that is used hres_lock; for more details,
		 * see the description of this technique in sun4u/sys/clock.h.
		 */
		dtrace_hrestime[i].dthr_lock++;
	}
}

hrtime_t
dtrace_gethrestime(void)
{
	dtrace_hrestime_t snap;
	hrtime_t now;
	int i = 0, adj, nslt;

	for (;;) {
		snap.dthr_lock = dtrace_hrestime[i].dthr_lock;
		dtrace_membar_consumer();
		snap.dthr_hrestime = dtrace_hrestime[i].dthr_hrestime;
		snap.dthr_hrtime = dtrace_hrestime[i].dthr_hrtime;
		snap.dthr_adj = dtrace_hrestime[i].dthr_adj;
		dtrace_membar_consumer();

		if ((snap.dthr_lock & ~1) == dtrace_hrestime[i].dthr_lock)
			break;

		/*
		 * If we're here, the lock was either locked, or it
		 * transitioned while we were taking the snapshot.  Either
		 * way, we're going to try the other dtrace_hrestime element;
		 * we know that it isn't possible for both to be locked
		 * simultaneously, so we will ultimately get a good snapshot.
		 */
		i ^= 1;
	}

	/*
	 * We have a good snapshot.  Now perform any necessary adjustments.
	 */
	nslt = dtrace_gethrtime() - snap.dthr_hrtime;
	ASSERT(nslt >= 0);

	now = ((hrtime_t)snap.dthr_hrestime.tv_sec * (hrtime_t)NANOSEC) +
	    snap.dthr_hrestime.tv_nsec;

	if (snap.dthr_adj != 0) {
		if (snap.dthr_adj > 0) {
			adj = (nslt >> adj_shift);
			if (adj > snap.dthr_adj)
				adj = (int)snap.dthr_adj;
		} else {
			adj = -(nslt >> adj_shift);
			if (adj < snap.dthr_adj)
				adj = (int)snap.dthr_adj;
		}
		now += adj;
	}

	return (now);
}

void
dtrace_vtime_enable(void)
{
	dtrace_vtime_state_t state, nstate;

	do {
		state = dtrace_vtime_active;

		switch (state) {
		case DTRACE_VTIME_INACTIVE:
			nstate = DTRACE_VTIME_ACTIVE;
			break;

		case DTRACE_VTIME_INACTIVE_TNF:
			nstate = DTRACE_VTIME_ACTIVE_TNF;
			break;

		case DTRACE_VTIME_ACTIVE:
		case DTRACE_VTIME_ACTIVE_TNF:
			panic("DTrace virtual time already enabled");
			/*NOTREACHED*/
		}

	} while	(cas32((uint32_t *)&dtrace_vtime_active,
	    state, nstate) != state);
}

void
dtrace_vtime_disable(void)
{
	dtrace_vtime_state_t state, nstate;

	do {
		state = dtrace_vtime_active;

		switch (state) {
		case DTRACE_VTIME_ACTIVE:
			nstate = DTRACE_VTIME_INACTIVE;
			break;

		case DTRACE_VTIME_ACTIVE_TNF:
			nstate = DTRACE_VTIME_INACTIVE_TNF;
			break;

		case DTRACE_VTIME_INACTIVE:
		case DTRACE_VTIME_INACTIVE_TNF:
			panic("DTrace virtual time already disabled");
			/*NOTREACHED*/
		}

	} while	(cas32((uint32_t *)&dtrace_vtime_active,
	    state, nstate) != state);
}

void
dtrace_vtime_enable_tnf(void)
{
	dtrace_vtime_state_t state, nstate;

	do {
		state = dtrace_vtime_active;

		switch (state) {
		case DTRACE_VTIME_ACTIVE:
			nstate = DTRACE_VTIME_ACTIVE_TNF;
			break;

		case DTRACE_VTIME_INACTIVE:
			nstate = DTRACE_VTIME_INACTIVE_TNF;
			break;

		case DTRACE_VTIME_ACTIVE_TNF:
		case DTRACE_VTIME_INACTIVE_TNF:
			panic("TNF already active");
			/*NOTREACHED*/
		}

	} while	(cas32((uint32_t *)&dtrace_vtime_active,
	    state, nstate) != state);
}

void
dtrace_vtime_disable_tnf(void)
{
	dtrace_vtime_state_t state, nstate;

	do {
		state = dtrace_vtime_active;

		switch (state) {
		case DTRACE_VTIME_ACTIVE_TNF:
			nstate = DTRACE_VTIME_ACTIVE;
			break;

		case DTRACE_VTIME_INACTIVE_TNF:
			nstate = DTRACE_VTIME_INACTIVE;
			break;

		case DTRACE_VTIME_ACTIVE:
		case DTRACE_VTIME_INACTIVE:
			panic("TNF already inactive");
			/*NOTREACHED*/
		}

	} while	(cas32((uint32_t *)&dtrace_vtime_active,
	    state, nstate) != state);
}

void
dtrace_vtime_switch(kthread_t *next)
{
	dtrace_icookie_t cookie;
	hrtime_t ts;

	if (tnf_tracing_active) {
		tnf_thread_switch(next);

		if (dtrace_vtime_active == DTRACE_VTIME_INACTIVE_TNF)
			return;
	}

	cookie = dtrace_interrupt_disable();
	ts = dtrace_gethrtime();

	if (curthread->t_dtrace_start != 0) {
		curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
		curthread->t_dtrace_start = 0;
	}

	next->t_dtrace_start = ts;

	dtrace_interrupt_enable(cookie);
}

void (*dtrace_fasttrap_fork_ptr)(proc_t *, proc_t *);
void (*dtrace_fasttrap_exec_ptr)(proc_t *);
void (*dtrace_fasttrap_exit_ptr)(proc_t *);

/*
 * This function is called by cfork() in the event that it appears that
 * there may be dtrace tracepoints active in the parent process's address
 * space. This first confirms the existence of dtrace tracepoints in the
 * parent process and calls into the fasttrap module to remove the
 * corresponding tracepoints from the child. By knowing that there are
 * existing tracepoints, and ensuring they can't be removed, we can rely
 * on the fasttrap module remaining loaded.
 */
void
dtrace_fasttrap_fork(proc_t *p, proc_t *cp)
{
	ASSERT(p->p_proc_flag & P_PR_LOCK);
	ASSERT(p->p_dtrace_count > 0);
	ASSERT(dtrace_fasttrap_fork_ptr != NULL);

	dtrace_fasttrap_fork_ptr(p, cp);
}