pkcs11: fix stdbool symbol name conflict
[unleashed.git] / kernel / os / dtrace_subr.c
blobe04b343d614515e7cd182f6f09433a6316402446
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
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2016 Joyent, Inc.
28 #include <sys/dtrace.h>
29 #include <sys/cmn_err.h>
30 #include <sys/tnf.h>
31 #include <sys/atomic.h>
32 #include <sys/prsystm.h>
33 #include <sys/modctl.h>
34 #include <sys/aio_impl.h>
37 void (*dtrace_cpu_init)(processorid_t);
38 void (*dtrace_modload)(struct modctl *);
39 void (*dtrace_modunload)(struct modctl *);
40 void (*dtrace_helpers_cleanup)(proc_t *);
41 void (*dtrace_helpers_fork)(proc_t *, proc_t *);
42 void (*dtrace_cpustart_init)(void);
43 void (*dtrace_cpustart_fini)(void);
44 void (*dtrace_cpc_fire)(uint64_t);
45 void (*dtrace_closef)(void);
47 void (*dtrace_debugger_init)(void);
48 void (*dtrace_debugger_fini)(void);
50 dtrace_vtime_state_t dtrace_vtime_active = 0;
51 dtrace_cacheid_t dtrace_predcache_id = DTRACE_CACHEIDNONE + 1;
54 * dtrace_cpc_in_use usage statement: this global variable is used by the cpc
55 * hardware overflow interrupt handler and the kernel cpc framework to check
56 * whether or not the DTrace cpc provider is currently in use. The variable is
57 * set before counters are enabled with the first enabling and cleared when
58 * the last enabling is disabled. Its value at any given time indicates the
59 * number of active dcpc based enablings. The global 'kcpc_cpuctx_lock' rwlock
60 * is held during initial setting to protect races between kcpc_open() and the
61 * first enabling. The locking provided by the DTrace subsystem, the kernel
62 * cpc framework and the cpu management framework protect consumers from race
63 * conditions on enabling and disabling probes.
65 uint32_t dtrace_cpc_in_use = 0;
67 typedef struct dtrace_hrestime {
68 lock_t dthr_lock; /* lock for this element */
69 timestruc_t dthr_hrestime; /* hrestime value */
70 int64_t dthr_adj; /* hrestime_adj value */
71 hrtime_t dthr_hrtime; /* hrtime value */
72 } dtrace_hrestime_t;
74 static dtrace_hrestime_t dtrace_hrestime[2];
77 * Making available adjustable high-resolution time in DTrace is regrettably
78 * more complicated than one might think it should be. The problem is that
79 * the variables related to adjusted high-resolution time (hrestime,
80 * hrestime_adj and friends) are adjusted under hres_lock -- and this lock may
81 * be held when we enter probe context. One might think that we could address
82 * this by having a single snapshot copy that is stored under a different lock
83 * from hres_tick(), using the snapshot iff hres_lock is locked in probe
84 * context. Unfortunately, this too won't work: because hres_lock is grabbed
85 * in more than just hres_tick() context, we could enter probe context
86 * concurrently on two different CPUs with both locks (hres_lock and the
87 * snapshot lock) held. As this implies, the fundamental problem is that we
88 * need to have access to a snapshot of these variables that we _know_ will
89 * not be locked in probe context. To effect this, we have two snapshots
90 * protected by two different locks, and we mandate that these snapshots are
91 * recorded in succession by a single thread calling dtrace_hres_tick(). (We
92 * assure this by calling it out of the same CY_HIGH_LEVEL cyclic that calls
93 * hres_tick().) A single thread can't be in two places at once: one of the
94 * snapshot locks is guaranteed to be unheld at all times. The
95 * dtrace_gethrestime() algorithm is thus to check first one snapshot and then
96 * the other to find the unlocked snapshot.
98 void
99 dtrace_hres_tick(void)
101 int i;
102 ushort_t spl;
104 for (i = 0; i < 2; i++) {
105 dtrace_hrestime_t tmp;
107 spl = hr_clock_lock();
108 tmp.dthr_hrestime = hrestime;
109 tmp.dthr_adj = hrestime_adj;
110 tmp.dthr_hrtime = dtrace_gethrtime();
111 hr_clock_unlock(spl);
113 lock_set(&dtrace_hrestime[i].dthr_lock);
114 dtrace_hrestime[i].dthr_hrestime = tmp.dthr_hrestime;
115 dtrace_hrestime[i].dthr_adj = tmp.dthr_adj;
116 dtrace_hrestime[i].dthr_hrtime = tmp.dthr_hrtime;
117 dtrace_membar_producer();
120 * To allow for lock-free examination of this lock, we use
121 * the same trick that is used hres_lock; for more details,
122 * see the description of this technique in sun4u/sys/clock.h.
124 dtrace_hrestime[i].dthr_lock++;
128 hrtime_t
129 dtrace_gethrestime(void)
131 dtrace_hrestime_t snap;
132 hrtime_t now;
133 int i = 0, adj, nslt;
135 for (;;) {
136 snap.dthr_lock = dtrace_hrestime[i].dthr_lock;
137 dtrace_membar_consumer();
138 snap.dthr_hrestime = dtrace_hrestime[i].dthr_hrestime;
139 snap.dthr_hrtime = dtrace_hrestime[i].dthr_hrtime;
140 snap.dthr_adj = dtrace_hrestime[i].dthr_adj;
141 dtrace_membar_consumer();
143 if ((snap.dthr_lock & ~1) == dtrace_hrestime[i].dthr_lock)
144 break;
147 * If we're here, the lock was either locked, or it
148 * transitioned while we were taking the snapshot. Either
149 * way, we're going to try the other dtrace_hrestime element;
150 * we know that it isn't possible for both to be locked
151 * simultaneously, so we will ultimately get a good snapshot.
153 i ^= 1;
157 * We have a good snapshot. Now perform any necessary adjustments.
159 nslt = dtrace_gethrtime() - snap.dthr_hrtime;
160 ASSERT(nslt >= 0);
162 now = ((hrtime_t)snap.dthr_hrestime.tv_sec * (hrtime_t)NANOSEC) +
163 snap.dthr_hrestime.tv_nsec;
165 if (snap.dthr_adj != 0) {
166 if (snap.dthr_adj > 0) {
167 adj = (nslt >> adj_shift);
168 if (adj > snap.dthr_adj)
169 adj = (int)snap.dthr_adj;
170 } else {
171 adj = -(nslt >> adj_shift);
172 if (adj < snap.dthr_adj)
173 adj = (int)snap.dthr_adj;
175 now += adj;
178 return (now);
181 void
182 dtrace_vtime_enable(void)
184 dtrace_vtime_state_t state, nstate;
186 do {
187 state = dtrace_vtime_active;
189 switch (state) {
190 case DTRACE_VTIME_INACTIVE:
191 nstate = DTRACE_VTIME_ACTIVE;
192 break;
194 case DTRACE_VTIME_INACTIVE_TNF:
195 nstate = DTRACE_VTIME_ACTIVE_TNF;
196 break;
198 case DTRACE_VTIME_ACTIVE:
199 case DTRACE_VTIME_ACTIVE_TNF:
200 panic("DTrace virtual time already enabled");
201 /*NOTREACHED*/
204 } while (atomic_cas_32((uint32_t *)&dtrace_vtime_active,
205 state, nstate) != state);
208 void
209 dtrace_vtime_disable(void)
211 dtrace_vtime_state_t state, nstate;
213 do {
214 state = dtrace_vtime_active;
216 switch (state) {
217 case DTRACE_VTIME_ACTIVE:
218 nstate = DTRACE_VTIME_INACTIVE;
219 break;
221 case DTRACE_VTIME_ACTIVE_TNF:
222 nstate = DTRACE_VTIME_INACTIVE_TNF;
223 break;
225 case DTRACE_VTIME_INACTIVE:
226 case DTRACE_VTIME_INACTIVE_TNF:
227 panic("DTrace virtual time already disabled");
228 /*NOTREACHED*/
231 } while (atomic_cas_32((uint32_t *)&dtrace_vtime_active,
232 state, nstate) != state);
235 void
236 dtrace_vtime_enable_tnf(void)
238 dtrace_vtime_state_t state, nstate;
240 do {
241 state = dtrace_vtime_active;
243 switch (state) {
244 case DTRACE_VTIME_ACTIVE:
245 nstate = DTRACE_VTIME_ACTIVE_TNF;
246 break;
248 case DTRACE_VTIME_INACTIVE:
249 nstate = DTRACE_VTIME_INACTIVE_TNF;
250 break;
252 case DTRACE_VTIME_ACTIVE_TNF:
253 case DTRACE_VTIME_INACTIVE_TNF:
254 panic("TNF already active");
255 /*NOTREACHED*/
258 } while (atomic_cas_32((uint32_t *)&dtrace_vtime_active,
259 state, nstate) != state);
262 void
263 dtrace_vtime_disable_tnf(void)
265 dtrace_vtime_state_t state, nstate;
267 do {
268 state = dtrace_vtime_active;
270 switch (state) {
271 case DTRACE_VTIME_ACTIVE_TNF:
272 nstate = DTRACE_VTIME_ACTIVE;
273 break;
275 case DTRACE_VTIME_INACTIVE_TNF:
276 nstate = DTRACE_VTIME_INACTIVE;
277 break;
279 case DTRACE_VTIME_ACTIVE:
280 case DTRACE_VTIME_INACTIVE:
281 panic("TNF already inactive");
282 /*NOTREACHED*/
285 } while (atomic_cas_32((uint32_t *)&dtrace_vtime_active,
286 state, nstate) != state);
289 void
290 dtrace_vtime_switch(kthread_t *next)
292 dtrace_icookie_t cookie;
293 hrtime_t ts;
295 if (tnf_tracing_active) {
296 tnf_thread_switch(next);
298 if (dtrace_vtime_active == DTRACE_VTIME_INACTIVE_TNF)
299 return;
302 cookie = dtrace_interrupt_disable();
303 ts = dtrace_gethrtime();
305 if (curthread->t_dtrace_start != 0) {
306 curthread->t_dtrace_vtime += ts - curthread->t_dtrace_start;
307 curthread->t_dtrace_start = 0;
310 next->t_dtrace_start = ts;
312 dtrace_interrupt_enable(cookie);
315 void (*dtrace_fasttrap_fork_ptr)(proc_t *, proc_t *);
316 void (*dtrace_fasttrap_exec_ptr)(proc_t *);
317 void (*dtrace_fasttrap_exit_ptr)(proc_t *);
320 * This function is called by cfork() in the event that it appears that
321 * there may be dtrace tracepoints active in the parent process's address
322 * space. This first confirms the existence of dtrace tracepoints in the
323 * parent process and calls into the fasttrap module to remove the
324 * corresponding tracepoints from the child. By knowing that there are
325 * existing tracepoints, and ensuring they can't be removed, we can rely
326 * on the fasttrap module remaining loaded.
328 void
329 dtrace_fasttrap_fork(proc_t *p, proc_t *cp)
331 ASSERT(p->p_proc_flag & P_PR_LOCK);
332 ASSERT(p->p_dtrace_count > 0);
333 ASSERT(dtrace_fasttrap_fork_ptr != NULL);
335 dtrace_fasttrap_fork_ptr(p, cp);