Print this page
4469 DTrace helper tracing should be dynamic
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/dtrace/dtrace.c
+++ new/usr/src/uts/common/dtrace/dtrace.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 26 */
27 27
28 28 /*
29 29 * DTrace - Dynamic Tracing for Solaris
30 30 *
31 31 * This is the implementation of the Solaris Dynamic Tracing framework
32 32 * (DTrace). The user-visible interface to DTrace is described at length in
33 33 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace
34 34 * library, the in-kernel DTrace framework, and the DTrace providers are
35 35 * described in the block comments in the <sys/dtrace.h> header file. The
36 36 * internal architecture of DTrace is described in the block comments in the
37 37 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace
38 38 * implementation very much assume mastery of all of these sources; if one has
39 39 * an unanswered question about the implementation, one should consult them
40 40 * first.
41 41 *
42 42 * The functions here are ordered roughly as follows:
43 43 *
44 44 * - Probe context functions
45 45 * - Probe hashing functions
46 46 * - Non-probe context utility functions
47 47 * - Matching functions
48 48 * - Provider-to-Framework API functions
49 49 * - Probe management functions
50 50 * - DIF object functions
51 51 * - Format functions
52 52 * - Predicate functions
53 53 * - ECB functions
54 54 * - Buffer functions
55 55 * - Enabling functions
56 56 * - DOF functions
57 57 * - Anonymous enabling functions
58 58 * - Consumer state functions
59 59 * - Helper functions
60 60 * - Hook functions
61 61 * - Driver cookbook functions
62 62 *
63 63 * Each group of functions begins with a block comment labelled the "DTrace
64 64 * [Group] Functions", allowing one to find each block by searching forward
65 65 * on capital-f functions.
66 66 */
67 67 #include <sys/errno.h>
68 68 #include <sys/stat.h>
69 69 #include <sys/modctl.h>
70 70 #include <sys/conf.h>
71 71 #include <sys/systm.h>
72 72 #include <sys/ddi.h>
73 73 #include <sys/sunddi.h>
74 74 #include <sys/cpuvar.h>
75 75 #include <sys/kmem.h>
76 76 #include <sys/strsubr.h>
77 77 #include <sys/sysmacros.h>
78 78 #include <sys/dtrace_impl.h>
79 79 #include <sys/atomic.h>
80 80 #include <sys/cmn_err.h>
81 81 #include <sys/mutex_impl.h>
82 82 #include <sys/rwlock_impl.h>
83 83 #include <sys/ctf_api.h>
84 84 #include <sys/panic.h>
85 85 #include <sys/priv_impl.h>
86 86 #include <sys/policy.h>
87 87 #include <sys/cred_impl.h>
88 88 #include <sys/procfs_isa.h>
89 89 #include <sys/taskq.h>
90 90 #include <sys/mkdev.h>
91 91 #include <sys/kdi.h>
92 92 #include <sys/zone.h>
93 93 #include <sys/socket.h>
94 94 #include <netinet/in.h>
95 95 #include "strtolctype.h"
96 96
97 97 /*
98 98 * DTrace Tunable Variables
99 99 *
100 100 * The following variables may be tuned by adding a line to /etc/system that
101 101 * includes both the name of the DTrace module ("dtrace") and the name of the
102 102 * variable. For example:
103 103 *
104 104 * set dtrace:dtrace_destructive_disallow = 1
105 105 *
106 106 * In general, the only variables that one should be tuning this way are those
107 107 * that affect system-wide DTrace behavior, and for which the default behavior
108 108 * is undesirable. Most of these variables are tunable on a per-consumer
109 109 * basis using DTrace options, and need not be tuned on a system-wide basis.
110 110 * When tuning these variables, avoid pathological values; while some attempt
111 111 * is made to verify the integrity of these variables, they are not considered
112 112 * part of the supported interface to DTrace, and they are therefore not
113 113 * checked comprehensively. Further, these variables should not be tuned
114 114 * dynamically via "mdb -kw" or other means; they should only be tuned via
115 115 * /etc/system.
116 116 */
117 117 int dtrace_destructive_disallow = 0;
118 118 dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024);
119 119 size_t dtrace_difo_maxsize = (256 * 1024);
120 120 dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024);
121 121 size_t dtrace_global_maxsize = (16 * 1024);
122 122 size_t dtrace_actions_max = (16 * 1024);
123 123 size_t dtrace_retain_max = 1024;
124 124 dtrace_optval_t dtrace_helper_actions_max = 1024;
125 125 dtrace_optval_t dtrace_helper_providers_max = 32;
126 126 dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024);
127 127 size_t dtrace_strsize_default = 256;
128 128 dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */
129 129 dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */
130 130 dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */
131 131 dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */
132 132 dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */
133 133 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */
134 134 dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */
135 135 dtrace_optval_t dtrace_nspec_default = 1;
136 136 dtrace_optval_t dtrace_specsize_default = 32 * 1024;
137 137 dtrace_optval_t dtrace_stackframes_default = 20;
138 138 dtrace_optval_t dtrace_ustackframes_default = 20;
139 139 dtrace_optval_t dtrace_jstackframes_default = 50;
140 140 dtrace_optval_t dtrace_jstackstrsize_default = 512;
141 141 int dtrace_msgdsize_max = 128;
142 142 hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */
143 143 hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */
144 144 int dtrace_devdepth_max = 32;
145 145 int dtrace_err_verbose;
146 146 hrtime_t dtrace_deadman_interval = NANOSEC;
147 147 hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
148 148 hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
149 149 hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC;
150 150
151 151 /*
152 152 * DTrace External Variables
153 153 *
154 154 * As dtrace(7D) is a kernel module, any DTrace variables are obviously
155 155 * available to DTrace consumers via the backtick (`) syntax. One of these,
156 156 * dtrace_zero, is made deliberately so: it is provided as a source of
157 157 * well-known, zero-filled memory. While this variable is not documented,
158 158 * it is used by some translators as an implementation detail.
159 159 */
160 160 const char dtrace_zero[256] = { 0 }; /* zero-filled memory */
161 161
162 162 /*
163 163 * DTrace Internal Variables
164 164 */
165 165 static dev_info_t *dtrace_devi; /* device info */
166 166 static vmem_t *dtrace_arena; /* probe ID arena */
167 167 static vmem_t *dtrace_minor; /* minor number arena */
168 168 static taskq_t *dtrace_taskq; /* task queue */
169 169 static dtrace_probe_t **dtrace_probes; /* array of all probes */
170 170 static int dtrace_nprobes; /* number of probes */
171 171 static dtrace_provider_t *dtrace_provider; /* provider list */
172 172 static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */
173 173 static int dtrace_opens; /* number of opens */
174 174 static int dtrace_helpers; /* number of helpers */
175 175 static int dtrace_getf; /* number of unpriv getf()s */
176 176 static void *dtrace_softstate; /* softstate pointer */
177 177 static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */
178 178 static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */
179 179 static dtrace_hash_t *dtrace_byname; /* probes hashed by name */
180 180 static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */
181 181 static int dtrace_toxranges; /* number of toxic ranges */
182 182 static int dtrace_toxranges_max; /* size of toxic range array */
183 183 static dtrace_anon_t dtrace_anon; /* anonymous enabling */
184 184 static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */
185 185 static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */
186 186 static kthread_t *dtrace_panicked; /* panicking thread */
187 187 static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */
188 188 static dtrace_genid_t dtrace_probegen; /* current probe generation */
189 189 static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */
190 190 static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */
191 191 static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */
192 192 static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */
193 193 static int dtrace_dynvar_failclean; /* dynvars failed to clean */
194 194
195 195 /*
196 196 * DTrace Locking
197 197 * DTrace is protected by three (relatively coarse-grained) locks:
198 198 *
199 199 * (1) dtrace_lock is required to manipulate essentially any DTrace state,
200 200 * including enabling state, probes, ECBs, consumer state, helper state,
201 201 * etc. Importantly, dtrace_lock is _not_ required when in probe context;
202 202 * probe context is lock-free -- synchronization is handled via the
203 203 * dtrace_sync() cross call mechanism.
204 204 *
205 205 * (2) dtrace_provider_lock is required when manipulating provider state, or
206 206 * when provider state must be held constant.
207 207 *
208 208 * (3) dtrace_meta_lock is required when manipulating meta provider state, or
209 209 * when meta provider state must be held constant.
210 210 *
211 211 * The lock ordering between these three locks is dtrace_meta_lock before
212 212 * dtrace_provider_lock before dtrace_lock. (In particular, there are
213 213 * several places where dtrace_provider_lock is held by the framework as it
214 214 * calls into the providers -- which then call back into the framework,
215 215 * grabbing dtrace_lock.)
216 216 *
217 217 * There are two other locks in the mix: mod_lock and cpu_lock. With respect
218 218 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
219 219 * role as a coarse-grained lock; it is acquired before both of these locks.
220 220 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must
221 221 * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
222 222 * mod_lock is similar with respect to dtrace_provider_lock in that it must be
223 223 * acquired _between_ dtrace_provider_lock and dtrace_lock.
224 224 */
225 225 static kmutex_t dtrace_lock; /* probe state lock */
226 226 static kmutex_t dtrace_provider_lock; /* provider state lock */
227 227 static kmutex_t dtrace_meta_lock; /* meta-provider state lock */
228 228
229 229 /*
230 230 * DTrace Provider Variables
231 231 *
232 232 * These are the variables relating to DTrace as a provider (that is, the
233 233 * provider of the BEGIN, END, and ERROR probes).
234 234 */
235 235 static dtrace_pattr_t dtrace_provider_attr = {
236 236 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
237 237 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
238 238 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
239 239 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
240 240 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
241 241 };
242 242
243 243 static void
244 244 dtrace_nullop(void)
245 245 {}
246 246
247 247 static int
248 248 dtrace_enable_nullop(void)
249 249 {
250 250 return (0);
251 251 }
252 252
253 253 static dtrace_pops_t dtrace_provider_ops = {
254 254 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop,
255 255 (void (*)(void *, struct modctl *))dtrace_nullop,
256 256 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop,
257 257 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
258 258 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
259 259 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
260 260 NULL,
261 261 NULL,
↓ open down ↓ |
261 lines elided |
↑ open up ↑ |
262 262 NULL,
263 263 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop
264 264 };
265 265
266 266 static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */
267 267 static dtrace_id_t dtrace_probeid_end; /* special END probe */
268 268 dtrace_id_t dtrace_probeid_error; /* special ERROR probe */
269 269
270 270 /*
271 271 * DTrace Helper Tracing Variables
272 - */
273 -uint32_t dtrace_helptrace_next = 0;
274 -uint32_t dtrace_helptrace_nlocals;
275 -char *dtrace_helptrace_buffer;
276 -int dtrace_helptrace_bufsize = 512 * 1024;
277 -
278 -#ifdef DEBUG
279 -int dtrace_helptrace_enabled = 1;
280 -#else
281 -int dtrace_helptrace_enabled = 0;
282 -#endif
272 + *
273 + * These variables should be set dynamically to enable helper tracing. The
274 + * only variables that should be set are dtrace_helptrace_enable (which should
275 + * be set to a non-zero value to allocate helper tracing buffers on the next
276 + * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a
277 + * non-zero value to deallocate helper tracing buffers on the next close of
278 + * /dev/dtrace). When (and only when) helper tracing is disabled, the
279 + * buffer size may also be set via dtrace_helptrace_bufsize.
280 + */
281 +int dtrace_helptrace_enable = 0;
282 +int dtrace_helptrace_disable = 0;
283 +int dtrace_helptrace_bufsize = 16 * 1024 * 1024;
284 +uint32_t dtrace_helptrace_nlocals;
285 +static dtrace_helptrace_t *dtrace_helptrace_buffer;
286 +static uint32_t dtrace_helptrace_next = 0;
287 +static int dtrace_helptrace_wrapped = 0;
283 288
284 289 /*
285 290 * DTrace Error Hashing
286 291 *
287 292 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
288 293 * table. This is very useful for checking coverage of tests that are
289 294 * expected to induce DIF or DOF processing errors, and may be useful for
290 295 * debugging problems in the DIF code generator or in DOF generation . The
291 296 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
292 297 */
293 298 #ifdef DEBUG
294 299 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ];
295 300 static const char *dtrace_errlast;
296 301 static kthread_t *dtrace_errthread;
297 302 static kmutex_t dtrace_errlock;
298 303 #endif
299 304
300 305 /*
301 306 * DTrace Macros and Constants
302 307 *
303 308 * These are various macros that are useful in various spots in the
304 309 * implementation, along with a few random constants that have no meaning
305 310 * outside of the implementation. There is no real structure to this cpp
306 311 * mishmash -- but is there ever?
307 312 */
308 313 #define DTRACE_HASHSTR(hash, probe) \
309 314 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
310 315
311 316 #define DTRACE_HASHNEXT(hash, probe) \
312 317 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
313 318
314 319 #define DTRACE_HASHPREV(hash, probe) \
315 320 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
316 321
317 322 #define DTRACE_HASHEQ(hash, lhs, rhs) \
318 323 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
319 324 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
320 325
321 326 #define DTRACE_AGGHASHSIZE_SLEW 17
322 327
323 328 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
324 329
325 330 /*
326 331 * The key for a thread-local variable consists of the lower 61 bits of the
327 332 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
328 333 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
329 334 * equal to a variable identifier. This is necessary (but not sufficient) to
330 335 * assure that global associative arrays never collide with thread-local
331 336 * variables. To guarantee that they cannot collide, we must also define the
332 337 * order for keying dynamic variables. That order is:
333 338 *
334 339 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
335 340 *
336 341 * Because the variable-key and the tls-key are in orthogonal spaces, there is
337 342 * no way for a global variable key signature to match a thread-local key
338 343 * signature.
339 344 */
340 345 #define DTRACE_TLS_THRKEY(where) { \
341 346 uint_t intr = 0; \
342 347 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
343 348 for (; actv; actv >>= 1) \
344 349 intr++; \
345 350 ASSERT(intr < (1 << 3)); \
346 351 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
347 352 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
348 353 }
349 354
350 355 #define DT_BSWAP_8(x) ((x) & 0xff)
351 356 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
352 357 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
353 358 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
354 359
355 360 #define DT_MASK_LO 0x00000000FFFFFFFFULL
356 361
357 362 #define DTRACE_STORE(type, tomax, offset, what) \
358 363 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
359 364
360 365 #ifndef __x86
361 366 #define DTRACE_ALIGNCHECK(addr, size, flags) \
362 367 if (addr & (size - 1)) { \
363 368 *flags |= CPU_DTRACE_BADALIGN; \
364 369 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
365 370 return (0); \
366 371 }
367 372 #else
368 373 #define DTRACE_ALIGNCHECK(addr, size, flags)
369 374 #endif
370 375
371 376 /*
372 377 * Test whether a range of memory starting at testaddr of size testsz falls
373 378 * within the range of memory described by addr, sz. We take care to avoid
374 379 * problems with overflow and underflow of the unsigned quantities, and
375 380 * disallow all negative sizes. Ranges of size 0 are allowed.
376 381 */
377 382 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
378 383 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \
379 384 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \
380 385 (testaddr) + (testsz) >= (testaddr))
381 386
382 387 /*
383 388 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
384 389 * alloc_sz on the righthand side of the comparison in order to avoid overflow
385 390 * or underflow in the comparison with it. This is simpler than the INRANGE
386 391 * check above, because we know that the dtms_scratch_ptr is valid in the
387 392 * range. Allocations of size zero are allowed.
388 393 */
389 394 #define DTRACE_INSCRATCH(mstate, alloc_sz) \
390 395 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
391 396 (mstate)->dtms_scratch_ptr >= (alloc_sz))
392 397
393 398 #define DTRACE_LOADFUNC(bits) \
394 399 /*CSTYLED*/ \
395 400 uint##bits##_t \
396 401 dtrace_load##bits(uintptr_t addr) \
397 402 { \
398 403 size_t size = bits / NBBY; \
399 404 /*CSTYLED*/ \
400 405 uint##bits##_t rval; \
401 406 int i; \
402 407 volatile uint16_t *flags = (volatile uint16_t *) \
403 408 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
404 409 \
405 410 DTRACE_ALIGNCHECK(addr, size, flags); \
406 411 \
407 412 for (i = 0; i < dtrace_toxranges; i++) { \
408 413 if (addr >= dtrace_toxrange[i].dtt_limit) \
409 414 continue; \
410 415 \
411 416 if (addr + size <= dtrace_toxrange[i].dtt_base) \
412 417 continue; \
413 418 \
414 419 /* \
415 420 * This address falls within a toxic region; return 0. \
416 421 */ \
417 422 *flags |= CPU_DTRACE_BADADDR; \
418 423 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
419 424 return (0); \
420 425 } \
421 426 \
422 427 *flags |= CPU_DTRACE_NOFAULT; \
423 428 /*CSTYLED*/ \
424 429 rval = *((volatile uint##bits##_t *)addr); \
425 430 *flags &= ~CPU_DTRACE_NOFAULT; \
426 431 \
427 432 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \
428 433 }
429 434
430 435 #ifdef _LP64
431 436 #define dtrace_loadptr dtrace_load64
432 437 #else
433 438 #define dtrace_loadptr dtrace_load32
434 439 #endif
435 440
436 441 #define DTRACE_DYNHASH_FREE 0
437 442 #define DTRACE_DYNHASH_SINK 1
438 443 #define DTRACE_DYNHASH_VALID 2
439 444
440 445 #define DTRACE_MATCH_FAIL -1
441 446 #define DTRACE_MATCH_NEXT 0
442 447 #define DTRACE_MATCH_DONE 1
443 448 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
444 449 #define DTRACE_STATE_ALIGN 64
445 450
446 451 #define DTRACE_FLAGS2FLT(flags) \
447 452 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
448 453 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
449 454 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
450 455 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
451 456 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
452 457 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
453 458 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
454 459 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
455 460 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
456 461 DTRACEFLT_UNKNOWN)
457 462
458 463 #define DTRACEACT_ISSTRING(act) \
459 464 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
460 465 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
461 466
462 467 static size_t dtrace_strlen(const char *, size_t);
463 468 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
464 469 static void dtrace_enabling_provide(dtrace_provider_t *);
465 470 static int dtrace_enabling_match(dtrace_enabling_t *, int *);
466 471 static void dtrace_enabling_matchall(void);
467 472 static void dtrace_enabling_reap(void);
468 473 static dtrace_state_t *dtrace_anon_grab(void);
469 474 static uint64_t dtrace_helper(int, dtrace_mstate_t *,
470 475 dtrace_state_t *, uint64_t, uint64_t);
471 476 static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
472 477 static void dtrace_buffer_drop(dtrace_buffer_t *);
473 478 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when);
474 479 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
475 480 dtrace_state_t *, dtrace_mstate_t *);
476 481 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
477 482 dtrace_optval_t);
478 483 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
479 484 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
480 485 static int dtrace_priv_proc(dtrace_state_t *, dtrace_mstate_t *);
481 486 static void dtrace_getf_barrier(void);
482 487
483 488 /*
484 489 * DTrace Probe Context Functions
485 490 *
486 491 * These functions are called from probe context. Because probe context is
487 492 * any context in which C may be called, arbitrarily locks may be held,
488 493 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
489 494 * As a result, functions called from probe context may only call other DTrace
490 495 * support functions -- they may not interact at all with the system at large.
491 496 * (Note that the ASSERT macro is made probe-context safe by redefining it in
492 497 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
493 498 * loads are to be performed from probe context, they _must_ be in terms of
494 499 * the safe dtrace_load*() variants.
495 500 *
496 501 * Some functions in this block are not actually called from probe context;
497 502 * for these functions, there will be a comment above the function reading
498 503 * "Note: not called from probe context."
499 504 */
500 505 void
501 506 dtrace_panic(const char *format, ...)
502 507 {
503 508 va_list alist;
504 509
505 510 va_start(alist, format);
506 511 dtrace_vpanic(format, alist);
507 512 va_end(alist);
508 513 }
509 514
510 515 int
511 516 dtrace_assfail(const char *a, const char *f, int l)
512 517 {
513 518 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
514 519
515 520 /*
516 521 * We just need something here that even the most clever compiler
517 522 * cannot optimize away.
518 523 */
519 524 return (a[(uintptr_t)f]);
520 525 }
521 526
522 527 /*
523 528 * Atomically increment a specified error counter from probe context.
524 529 */
525 530 static void
526 531 dtrace_error(uint32_t *counter)
527 532 {
528 533 /*
529 534 * Most counters stored to in probe context are per-CPU counters.
530 535 * However, there are some error conditions that are sufficiently
531 536 * arcane that they don't merit per-CPU storage. If these counters
532 537 * are incremented concurrently on different CPUs, scalability will be
533 538 * adversely affected -- but we don't expect them to be white-hot in a
534 539 * correctly constructed enabling...
535 540 */
536 541 uint32_t oval, nval;
537 542
538 543 do {
539 544 oval = *counter;
540 545
541 546 if ((nval = oval + 1) == 0) {
542 547 /*
543 548 * If the counter would wrap, set it to 1 -- assuring
544 549 * that the counter is never zero when we have seen
545 550 * errors. (The counter must be 32-bits because we
546 551 * aren't guaranteed a 64-bit compare&swap operation.)
547 552 * To save this code both the infamy of being fingered
548 553 * by a priggish news story and the indignity of being
549 554 * the target of a neo-puritan witch trial, we're
550 555 * carefully avoiding any colorful description of the
551 556 * likelihood of this condition -- but suffice it to
552 557 * say that it is only slightly more likely than the
553 558 * overflow of predicate cache IDs, as discussed in
554 559 * dtrace_predicate_create().
555 560 */
556 561 nval = 1;
557 562 }
558 563 } while (dtrace_cas32(counter, oval, nval) != oval);
559 564 }
560 565
561 566 /*
562 567 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
563 568 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
564 569 */
565 570 DTRACE_LOADFUNC(8)
566 571 DTRACE_LOADFUNC(16)
567 572 DTRACE_LOADFUNC(32)
568 573 DTRACE_LOADFUNC(64)
569 574
570 575 static int
571 576 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
572 577 {
573 578 if (dest < mstate->dtms_scratch_base)
574 579 return (0);
575 580
576 581 if (dest + size < dest)
577 582 return (0);
578 583
579 584 if (dest + size > mstate->dtms_scratch_ptr)
580 585 return (0);
581 586
582 587 return (1);
583 588 }
584 589
585 590 static int
586 591 dtrace_canstore_statvar(uint64_t addr, size_t sz,
587 592 dtrace_statvar_t **svars, int nsvars)
588 593 {
589 594 int i;
590 595
591 596 for (i = 0; i < nsvars; i++) {
592 597 dtrace_statvar_t *svar = svars[i];
593 598
594 599 if (svar == NULL || svar->dtsv_size == 0)
595 600 continue;
596 601
597 602 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size))
598 603 return (1);
599 604 }
600 605
601 606 return (0);
602 607 }
603 608
604 609 /*
605 610 * Check to see if the address is within a memory region to which a store may
606 611 * be issued. This includes the DTrace scratch areas, and any DTrace variable
607 612 * region. The caller of dtrace_canstore() is responsible for performing any
608 613 * alignment checks that are needed before stores are actually executed.
609 614 */
610 615 static int
611 616 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
612 617 dtrace_vstate_t *vstate)
613 618 {
614 619 /*
615 620 * First, check to see if the address is in scratch space...
616 621 */
617 622 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
618 623 mstate->dtms_scratch_size))
619 624 return (1);
620 625
621 626 /*
622 627 * Now check to see if it's a dynamic variable. This check will pick
623 628 * up both thread-local variables and any global dynamically-allocated
624 629 * variables.
625 630 */
626 631 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base,
627 632 vstate->dtvs_dynvars.dtds_size)) {
628 633 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
629 634 uintptr_t base = (uintptr_t)dstate->dtds_base +
630 635 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
631 636 uintptr_t chunkoffs;
632 637
633 638 /*
634 639 * Before we assume that we can store here, we need to make
635 640 * sure that it isn't in our metadata -- storing to our
636 641 * dynamic variable metadata would corrupt our state. For
637 642 * the range to not include any dynamic variable metadata,
638 643 * it must:
639 644 *
640 645 * (1) Start above the hash table that is at the base of
641 646 * the dynamic variable space
642 647 *
643 648 * (2) Have a starting chunk offset that is beyond the
644 649 * dtrace_dynvar_t that is at the base of every chunk
645 650 *
646 651 * (3) Not span a chunk boundary
647 652 *
648 653 */
649 654 if (addr < base)
650 655 return (0);
651 656
652 657 chunkoffs = (addr - base) % dstate->dtds_chunksize;
653 658
654 659 if (chunkoffs < sizeof (dtrace_dynvar_t))
655 660 return (0);
656 661
657 662 if (chunkoffs + sz > dstate->dtds_chunksize)
658 663 return (0);
659 664
660 665 return (1);
661 666 }
662 667
663 668 /*
664 669 * Finally, check the static local and global variables. These checks
665 670 * take the longest, so we perform them last.
666 671 */
667 672 if (dtrace_canstore_statvar(addr, sz,
668 673 vstate->dtvs_locals, vstate->dtvs_nlocals))
669 674 return (1);
670 675
671 676 if (dtrace_canstore_statvar(addr, sz,
672 677 vstate->dtvs_globals, vstate->dtvs_nglobals))
673 678 return (1);
674 679
675 680 return (0);
676 681 }
677 682
678 683
679 684 /*
680 685 * Convenience routine to check to see if the address is within a memory
681 686 * region in which a load may be issued given the user's privilege level;
682 687 * if not, it sets the appropriate error flags and loads 'addr' into the
683 688 * illegal value slot.
684 689 *
685 690 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
686 691 * appropriate memory access protection.
687 692 */
688 693 static int
689 694 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
690 695 dtrace_vstate_t *vstate)
691 696 {
692 697 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
693 698 file_t *fp;
694 699
695 700 /*
696 701 * If we hold the privilege to read from kernel memory, then
697 702 * everything is readable.
698 703 */
699 704 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
700 705 return (1);
701 706
702 707 /*
703 708 * You can obviously read that which you can store.
704 709 */
705 710 if (dtrace_canstore(addr, sz, mstate, vstate))
706 711 return (1);
707 712
708 713 /*
709 714 * We're allowed to read from our own string table.
710 715 */
711 716 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab,
712 717 mstate->dtms_difo->dtdo_strlen))
713 718 return (1);
714 719
715 720 if (vstate->dtvs_state != NULL &&
716 721 dtrace_priv_proc(vstate->dtvs_state, mstate)) {
717 722 proc_t *p;
718 723
719 724 /*
720 725 * When we have privileges to the current process, there are
721 726 * several context-related kernel structures that are safe to
722 727 * read, even absent the privilege to read from kernel memory.
723 728 * These reads are safe because these structures contain only
724 729 * state that (1) we're permitted to read, (2) is harmless or
725 730 * (3) contains pointers to additional kernel state that we're
726 731 * not permitted to read (and as such, do not present an
727 732 * opportunity for privilege escalation). Finally (and
728 733 * critically), because of the nature of their relation with
729 734 * the current thread context, the memory associated with these
730 735 * structures cannot change over the duration of probe context,
731 736 * and it is therefore impossible for this memory to be
732 737 * deallocated and reallocated as something else while it's
733 738 * being operated upon.
734 739 */
735 740 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t)))
736 741 return (1);
737 742
738 743 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr,
739 744 sz, curthread->t_procp, sizeof (proc_t))) {
740 745 return (1);
741 746 }
742 747
743 748 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz,
744 749 curthread->t_cred, sizeof (cred_t))) {
745 750 return (1);
746 751 }
747 752
748 753 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz,
749 754 &(p->p_pidp->pid_id), sizeof (pid_t))) {
750 755 return (1);
751 756 }
752 757
753 758 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz,
754 759 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) {
755 760 return (1);
756 761 }
757 762 }
758 763
759 764 if ((fp = mstate->dtms_getf) != NULL) {
760 765 uintptr_t psz = sizeof (void *);
761 766 vnode_t *vp;
762 767 vnodeops_t *op;
763 768
764 769 /*
765 770 * When getf() returns a file_t, the enabling is implicitly
766 771 * granted the (transient) right to read the returned file_t
767 772 * as well as the v_path and v_op->vnop_name of the underlying
768 773 * vnode. These accesses are allowed after a successful
769 774 * getf() because the members that they refer to cannot change
770 775 * once set -- and the barrier logic in the kernel's closef()
771 776 * path assures that the file_t and its referenced vode_t
772 777 * cannot themselves be stale (that is, it impossible for
773 778 * either dtms_getf itself or its f_vnode member to reference
774 779 * freed memory).
775 780 */
776 781 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t)))
777 782 return (1);
778 783
779 784 if ((vp = fp->f_vnode) != NULL) {
780 785 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz))
781 786 return (1);
782 787
783 788 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz,
784 789 vp->v_path, strlen(vp->v_path) + 1)) {
785 790 return (1);
786 791 }
787 792
788 793 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz))
789 794 return (1);
790 795
791 796 if ((op = vp->v_op) != NULL &&
792 797 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) {
793 798 return (1);
794 799 }
795 800
796 801 if (op != NULL && op->vnop_name != NULL &&
797 802 DTRACE_INRANGE(addr, sz, op->vnop_name,
798 803 strlen(op->vnop_name) + 1)) {
799 804 return (1);
800 805 }
801 806 }
802 807 }
803 808
804 809 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
805 810 *illval = addr;
806 811 return (0);
807 812 }
808 813
809 814 /*
810 815 * Convenience routine to check to see if a given string is within a memory
811 816 * region in which a load may be issued given the user's privilege level;
812 817 * this exists so that we don't need to issue unnecessary dtrace_strlen()
813 818 * calls in the event that the user has all privileges.
814 819 */
815 820 static int
816 821 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
817 822 dtrace_vstate_t *vstate)
818 823 {
819 824 size_t strsz;
820 825
821 826 /*
822 827 * If we hold the privilege to read from kernel memory, then
823 828 * everything is readable.
824 829 */
825 830 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
826 831 return (1);
827 832
828 833 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz);
829 834 if (dtrace_canload(addr, strsz, mstate, vstate))
830 835 return (1);
831 836
832 837 return (0);
833 838 }
834 839
835 840 /*
836 841 * Convenience routine to check to see if a given variable is within a memory
837 842 * region in which a load may be issued given the user's privilege level.
838 843 */
839 844 static int
840 845 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate,
841 846 dtrace_vstate_t *vstate)
842 847 {
843 848 size_t sz;
844 849 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
845 850
846 851 /*
847 852 * If we hold the privilege to read from kernel memory, then
848 853 * everything is readable.
849 854 */
850 855 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
851 856 return (1);
852 857
853 858 if (type->dtdt_kind == DIF_TYPE_STRING)
854 859 sz = dtrace_strlen(src,
855 860 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1;
856 861 else
857 862 sz = type->dtdt_size;
858 863
859 864 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate));
860 865 }
861 866
862 867 /*
863 868 * Convert a string to a signed integer using safe loads.
864 869 *
865 870 * NOTE: This function uses various macros from strtolctype.h to manipulate
866 871 * digit values, etc -- these have all been checked to ensure they make
867 872 * no additional function calls.
868 873 */
869 874 static int64_t
870 875 dtrace_strtoll(char *input, int base, size_t limit)
871 876 {
872 877 uintptr_t pos = (uintptr_t)input;
873 878 int64_t val = 0;
874 879 int x;
875 880 boolean_t neg = B_FALSE;
876 881 char c, cc, ccc;
877 882 uintptr_t end = pos + limit;
878 883
879 884 /*
880 885 * Consume any whitespace preceding digits.
881 886 */
882 887 while ((c = dtrace_load8(pos)) == ' ' || c == '\t')
883 888 pos++;
884 889
885 890 /*
886 891 * Handle an explicit sign if one is present.
887 892 */
888 893 if (c == '-' || c == '+') {
889 894 if (c == '-')
890 895 neg = B_TRUE;
891 896 c = dtrace_load8(++pos);
892 897 }
893 898
894 899 /*
895 900 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
896 901 * if present.
897 902 */
898 903 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' ||
899 904 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) {
900 905 pos += 2;
901 906 c = ccc;
902 907 }
903 908
904 909 /*
905 910 * Read in contiguous digits until the first non-digit character.
906 911 */
907 912 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base;
908 913 c = dtrace_load8(++pos))
909 914 val = val * base + x;
910 915
911 916 return (neg ? -val : val);
912 917 }
913 918
914 919 /*
915 920 * Compare two strings using safe loads.
916 921 */
917 922 static int
918 923 dtrace_strncmp(char *s1, char *s2, size_t limit)
919 924 {
920 925 uint8_t c1, c2;
921 926 volatile uint16_t *flags;
922 927
923 928 if (s1 == s2 || limit == 0)
924 929 return (0);
925 930
926 931 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
927 932
928 933 do {
929 934 if (s1 == NULL) {
930 935 c1 = '\0';
931 936 } else {
932 937 c1 = dtrace_load8((uintptr_t)s1++);
933 938 }
934 939
935 940 if (s2 == NULL) {
936 941 c2 = '\0';
937 942 } else {
938 943 c2 = dtrace_load8((uintptr_t)s2++);
939 944 }
940 945
941 946 if (c1 != c2)
942 947 return (c1 - c2);
943 948 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
944 949
945 950 return (0);
946 951 }
947 952
948 953 /*
949 954 * Compute strlen(s) for a string using safe memory accesses. The additional
950 955 * len parameter is used to specify a maximum length to ensure completion.
951 956 */
952 957 static size_t
953 958 dtrace_strlen(const char *s, size_t lim)
954 959 {
955 960 uint_t len;
956 961
957 962 for (len = 0; len != lim; len++) {
958 963 if (dtrace_load8((uintptr_t)s++) == '\0')
959 964 break;
960 965 }
961 966
962 967 return (len);
963 968 }
964 969
965 970 /*
966 971 * Check if an address falls within a toxic region.
967 972 */
968 973 static int
969 974 dtrace_istoxic(uintptr_t kaddr, size_t size)
970 975 {
971 976 uintptr_t taddr, tsize;
972 977 int i;
973 978
974 979 for (i = 0; i < dtrace_toxranges; i++) {
975 980 taddr = dtrace_toxrange[i].dtt_base;
976 981 tsize = dtrace_toxrange[i].dtt_limit - taddr;
977 982
978 983 if (kaddr - taddr < tsize) {
979 984 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
980 985 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr;
981 986 return (1);
982 987 }
983 988
984 989 if (taddr - kaddr < size) {
985 990 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
986 991 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr;
987 992 return (1);
988 993 }
989 994 }
990 995
991 996 return (0);
992 997 }
993 998
994 999 /*
995 1000 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
996 1001 * memory specified by the DIF program. The dst is assumed to be safe memory
997 1002 * that we can store to directly because it is managed by DTrace. As with
998 1003 * standard bcopy, overlapping copies are handled properly.
999 1004 */
1000 1005 static void
1001 1006 dtrace_bcopy(const void *src, void *dst, size_t len)
1002 1007 {
1003 1008 if (len != 0) {
1004 1009 uint8_t *s1 = dst;
1005 1010 const uint8_t *s2 = src;
1006 1011
1007 1012 if (s1 <= s2) {
1008 1013 do {
1009 1014 *s1++ = dtrace_load8((uintptr_t)s2++);
1010 1015 } while (--len != 0);
1011 1016 } else {
1012 1017 s2 += len;
1013 1018 s1 += len;
1014 1019
1015 1020 do {
1016 1021 *--s1 = dtrace_load8((uintptr_t)--s2);
1017 1022 } while (--len != 0);
1018 1023 }
1019 1024 }
1020 1025 }
1021 1026
1022 1027 /*
1023 1028 * Copy src to dst using safe memory accesses, up to either the specified
1024 1029 * length, or the point that a nul byte is encountered. The src is assumed to
1025 1030 * be unsafe memory specified by the DIF program. The dst is assumed to be
1026 1031 * safe memory that we can store to directly because it is managed by DTrace.
1027 1032 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1028 1033 */
1029 1034 static void
1030 1035 dtrace_strcpy(const void *src, void *dst, size_t len)
1031 1036 {
1032 1037 if (len != 0) {
1033 1038 uint8_t *s1 = dst, c;
1034 1039 const uint8_t *s2 = src;
1035 1040
1036 1041 do {
1037 1042 *s1++ = c = dtrace_load8((uintptr_t)s2++);
1038 1043 } while (--len != 0 && c != '\0');
1039 1044 }
1040 1045 }
1041 1046
1042 1047 /*
1043 1048 * Copy src to dst, deriving the size and type from the specified (BYREF)
1044 1049 * variable type. The src is assumed to be unsafe memory specified by the DIF
1045 1050 * program. The dst is assumed to be DTrace variable memory that is of the
1046 1051 * specified type; we assume that we can store to directly.
1047 1052 */
1048 1053 static void
1049 1054 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type)
1050 1055 {
1051 1056 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1052 1057
1053 1058 if (type->dtdt_kind == DIF_TYPE_STRING) {
1054 1059 dtrace_strcpy(src, dst, type->dtdt_size);
1055 1060 } else {
1056 1061 dtrace_bcopy(src, dst, type->dtdt_size);
1057 1062 }
1058 1063 }
1059 1064
1060 1065 /*
1061 1066 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1062 1067 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1063 1068 * safe memory that we can access directly because it is managed by DTrace.
1064 1069 */
1065 1070 static int
1066 1071 dtrace_bcmp(const void *s1, const void *s2, size_t len)
1067 1072 {
1068 1073 volatile uint16_t *flags;
1069 1074
1070 1075 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
1071 1076
1072 1077 if (s1 == s2)
1073 1078 return (0);
1074 1079
1075 1080 if (s1 == NULL || s2 == NULL)
1076 1081 return (1);
1077 1082
1078 1083 if (s1 != s2 && len != 0) {
1079 1084 const uint8_t *ps1 = s1;
1080 1085 const uint8_t *ps2 = s2;
1081 1086
1082 1087 do {
1083 1088 if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1084 1089 return (1);
1085 1090 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1086 1091 }
1087 1092 return (0);
1088 1093 }
1089 1094
1090 1095 /*
1091 1096 * Zero the specified region using a simple byte-by-byte loop. Note that this
1092 1097 * is for safe DTrace-managed memory only.
1093 1098 */
1094 1099 static void
1095 1100 dtrace_bzero(void *dst, size_t len)
1096 1101 {
1097 1102 uchar_t *cp;
1098 1103
1099 1104 for (cp = dst; len != 0; len--)
1100 1105 *cp++ = 0;
1101 1106 }
1102 1107
1103 1108 static void
1104 1109 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1105 1110 {
1106 1111 uint64_t result[2];
1107 1112
1108 1113 result[0] = addend1[0] + addend2[0];
1109 1114 result[1] = addend1[1] + addend2[1] +
1110 1115 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1111 1116
1112 1117 sum[0] = result[0];
1113 1118 sum[1] = result[1];
1114 1119 }
1115 1120
1116 1121 /*
1117 1122 * Shift the 128-bit value in a by b. If b is positive, shift left.
1118 1123 * If b is negative, shift right.
1119 1124 */
1120 1125 static void
1121 1126 dtrace_shift_128(uint64_t *a, int b)
1122 1127 {
1123 1128 uint64_t mask;
1124 1129
1125 1130 if (b == 0)
1126 1131 return;
1127 1132
1128 1133 if (b < 0) {
1129 1134 b = -b;
1130 1135 if (b >= 64) {
1131 1136 a[0] = a[1] >> (b - 64);
1132 1137 a[1] = 0;
1133 1138 } else {
1134 1139 a[0] >>= b;
1135 1140 mask = 1LL << (64 - b);
1136 1141 mask -= 1;
1137 1142 a[0] |= ((a[1] & mask) << (64 - b));
1138 1143 a[1] >>= b;
1139 1144 }
1140 1145 } else {
1141 1146 if (b >= 64) {
1142 1147 a[1] = a[0] << (b - 64);
1143 1148 a[0] = 0;
1144 1149 } else {
1145 1150 a[1] <<= b;
1146 1151 mask = a[0] >> (64 - b);
1147 1152 a[1] |= mask;
1148 1153 a[0] <<= b;
1149 1154 }
1150 1155 }
1151 1156 }
1152 1157
1153 1158 /*
1154 1159 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1155 1160 * use native multiplication on those, and then re-combine into the
1156 1161 * resulting 128-bit value.
1157 1162 *
1158 1163 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1159 1164 * hi1 * hi2 << 64 +
1160 1165 * hi1 * lo2 << 32 +
1161 1166 * hi2 * lo1 << 32 +
1162 1167 * lo1 * lo2
1163 1168 */
1164 1169 static void
1165 1170 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1166 1171 {
1167 1172 uint64_t hi1, hi2, lo1, lo2;
1168 1173 uint64_t tmp[2];
1169 1174
1170 1175 hi1 = factor1 >> 32;
1171 1176 hi2 = factor2 >> 32;
1172 1177
1173 1178 lo1 = factor1 & DT_MASK_LO;
1174 1179 lo2 = factor2 & DT_MASK_LO;
1175 1180
1176 1181 product[0] = lo1 * lo2;
1177 1182 product[1] = hi1 * hi2;
1178 1183
1179 1184 tmp[0] = hi1 * lo2;
1180 1185 tmp[1] = 0;
1181 1186 dtrace_shift_128(tmp, 32);
1182 1187 dtrace_add_128(product, tmp, product);
1183 1188
1184 1189 tmp[0] = hi2 * lo1;
1185 1190 tmp[1] = 0;
1186 1191 dtrace_shift_128(tmp, 32);
1187 1192 dtrace_add_128(product, tmp, product);
1188 1193 }
1189 1194
1190 1195 /*
1191 1196 * This privilege check should be used by actions and subroutines to
1192 1197 * verify that the user credentials of the process that enabled the
1193 1198 * invoking ECB match the target credentials
1194 1199 */
1195 1200 static int
1196 1201 dtrace_priv_proc_common_user(dtrace_state_t *state)
1197 1202 {
1198 1203 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1199 1204
1200 1205 /*
1201 1206 * We should always have a non-NULL state cred here, since if cred
1202 1207 * is null (anonymous tracing), we fast-path bypass this routine.
1203 1208 */
1204 1209 ASSERT(s_cr != NULL);
1205 1210
1206 1211 if ((cr = CRED()) != NULL &&
1207 1212 s_cr->cr_uid == cr->cr_uid &&
1208 1213 s_cr->cr_uid == cr->cr_ruid &&
1209 1214 s_cr->cr_uid == cr->cr_suid &&
1210 1215 s_cr->cr_gid == cr->cr_gid &&
1211 1216 s_cr->cr_gid == cr->cr_rgid &&
1212 1217 s_cr->cr_gid == cr->cr_sgid)
1213 1218 return (1);
1214 1219
1215 1220 return (0);
1216 1221 }
1217 1222
1218 1223 /*
1219 1224 * This privilege check should be used by actions and subroutines to
1220 1225 * verify that the zone of the process that enabled the invoking ECB
1221 1226 * matches the target credentials
1222 1227 */
1223 1228 static int
1224 1229 dtrace_priv_proc_common_zone(dtrace_state_t *state)
1225 1230 {
1226 1231 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1227 1232
1228 1233 /*
1229 1234 * We should always have a non-NULL state cred here, since if cred
1230 1235 * is null (anonymous tracing), we fast-path bypass this routine.
1231 1236 */
1232 1237 ASSERT(s_cr != NULL);
1233 1238
1234 1239 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone)
1235 1240 return (1);
1236 1241
1237 1242 return (0);
1238 1243 }
1239 1244
1240 1245 /*
1241 1246 * This privilege check should be used by actions and subroutines to
1242 1247 * verify that the process has not setuid or changed credentials.
1243 1248 */
1244 1249 static int
1245 1250 dtrace_priv_proc_common_nocd()
1246 1251 {
1247 1252 proc_t *proc;
1248 1253
1249 1254 if ((proc = ttoproc(curthread)) != NULL &&
1250 1255 !(proc->p_flag & SNOCD))
1251 1256 return (1);
1252 1257
1253 1258 return (0);
1254 1259 }
1255 1260
1256 1261 static int
1257 1262 dtrace_priv_proc_destructive(dtrace_state_t *state, dtrace_mstate_t *mstate)
1258 1263 {
1259 1264 int action = state->dts_cred.dcr_action;
1260 1265
1261 1266 if (!(mstate->dtms_access & DTRACE_ACCESS_PROC))
1262 1267 goto bad;
1263 1268
1264 1269 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1265 1270 dtrace_priv_proc_common_zone(state) == 0)
1266 1271 goto bad;
1267 1272
1268 1273 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1269 1274 dtrace_priv_proc_common_user(state) == 0)
1270 1275 goto bad;
1271 1276
1272 1277 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1273 1278 dtrace_priv_proc_common_nocd() == 0)
1274 1279 goto bad;
1275 1280
1276 1281 return (1);
1277 1282
1278 1283 bad:
1279 1284 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1280 1285
1281 1286 return (0);
1282 1287 }
1283 1288
1284 1289 static int
1285 1290 dtrace_priv_proc_control(dtrace_state_t *state, dtrace_mstate_t *mstate)
1286 1291 {
1287 1292 if (mstate->dtms_access & DTRACE_ACCESS_PROC) {
1288 1293 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1289 1294 return (1);
1290 1295
1291 1296 if (dtrace_priv_proc_common_zone(state) &&
1292 1297 dtrace_priv_proc_common_user(state) &&
1293 1298 dtrace_priv_proc_common_nocd())
1294 1299 return (1);
1295 1300 }
1296 1301
1297 1302 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1298 1303
1299 1304 return (0);
1300 1305 }
1301 1306
1302 1307 static int
1303 1308 dtrace_priv_proc(dtrace_state_t *state, dtrace_mstate_t *mstate)
1304 1309 {
1305 1310 if ((mstate->dtms_access & DTRACE_ACCESS_PROC) &&
1306 1311 (state->dts_cred.dcr_action & DTRACE_CRA_PROC))
1307 1312 return (1);
1308 1313
1309 1314 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1310 1315
1311 1316 return (0);
1312 1317 }
1313 1318
1314 1319 static int
1315 1320 dtrace_priv_kernel(dtrace_state_t *state)
1316 1321 {
1317 1322 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1318 1323 return (1);
1319 1324
1320 1325 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1321 1326
1322 1327 return (0);
1323 1328 }
1324 1329
1325 1330 static int
1326 1331 dtrace_priv_kernel_destructive(dtrace_state_t *state)
1327 1332 {
1328 1333 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1329 1334 return (1);
1330 1335
1331 1336 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1332 1337
1333 1338 return (0);
1334 1339 }
1335 1340
1336 1341 /*
1337 1342 * Determine if the dte_cond of the specified ECB allows for processing of
1338 1343 * the current probe to continue. Note that this routine may allow continued
1339 1344 * processing, but with access(es) stripped from the mstate's dtms_access
1340 1345 * field.
1341 1346 */
1342 1347 static int
1343 1348 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate,
1344 1349 dtrace_ecb_t *ecb)
1345 1350 {
1346 1351 dtrace_probe_t *probe = ecb->dte_probe;
1347 1352 dtrace_provider_t *prov = probe->dtpr_provider;
1348 1353 dtrace_pops_t *pops = &prov->dtpv_pops;
1349 1354 int mode = DTRACE_MODE_NOPRIV_DROP;
1350 1355
1351 1356 ASSERT(ecb->dte_cond);
1352 1357
1353 1358 if (pops->dtps_mode != NULL) {
1354 1359 mode = pops->dtps_mode(prov->dtpv_arg,
1355 1360 probe->dtpr_id, probe->dtpr_arg);
1356 1361
1357 1362 ASSERT(mode & (DTRACE_MODE_USER | DTRACE_MODE_KERNEL));
1358 1363 ASSERT(mode & (DTRACE_MODE_NOPRIV_RESTRICT |
1359 1364 DTRACE_MODE_NOPRIV_DROP));
1360 1365 }
1361 1366
1362 1367 /*
1363 1368 * If the dte_cond bits indicate that this consumer is only allowed to
1364 1369 * see user-mode firings of this probe, check that the probe was fired
1365 1370 * while in a user context. If that's not the case, use the policy
1366 1371 * specified by the provider to determine if we drop the probe or
1367 1372 * merely restrict operation.
1368 1373 */
1369 1374 if (ecb->dte_cond & DTRACE_COND_USERMODE) {
1370 1375 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP);
1371 1376
1372 1377 if (!(mode & DTRACE_MODE_USER)) {
1373 1378 if (mode & DTRACE_MODE_NOPRIV_DROP)
1374 1379 return (0);
1375 1380
1376 1381 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
1377 1382 }
1378 1383 }
1379 1384
1380 1385 /*
1381 1386 * This is more subtle than it looks. We have to be absolutely certain
1382 1387 * that CRED() isn't going to change out from under us so it's only
1383 1388 * legit to examine that structure if we're in constrained situations.
1384 1389 * Currently, the only times we'll this check is if a non-super-user
1385 1390 * has enabled the profile or syscall providers -- providers that
1386 1391 * allow visibility of all processes. For the profile case, the check
1387 1392 * above will ensure that we're examining a user context.
1388 1393 */
1389 1394 if (ecb->dte_cond & DTRACE_COND_OWNER) {
1390 1395 cred_t *cr;
1391 1396 cred_t *s_cr = state->dts_cred.dcr_cred;
1392 1397 proc_t *proc;
1393 1398
1394 1399 ASSERT(s_cr != NULL);
1395 1400
1396 1401 if ((cr = CRED()) == NULL ||
1397 1402 s_cr->cr_uid != cr->cr_uid ||
1398 1403 s_cr->cr_uid != cr->cr_ruid ||
1399 1404 s_cr->cr_uid != cr->cr_suid ||
1400 1405 s_cr->cr_gid != cr->cr_gid ||
1401 1406 s_cr->cr_gid != cr->cr_rgid ||
1402 1407 s_cr->cr_gid != cr->cr_sgid ||
1403 1408 (proc = ttoproc(curthread)) == NULL ||
1404 1409 (proc->p_flag & SNOCD)) {
1405 1410 if (mode & DTRACE_MODE_NOPRIV_DROP)
1406 1411 return (0);
1407 1412
1408 1413 mstate->dtms_access &= ~DTRACE_ACCESS_PROC;
1409 1414 }
1410 1415 }
1411 1416
1412 1417 /*
1413 1418 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not
1414 1419 * in our zone, check to see if our mode policy is to restrict rather
1415 1420 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC
1416 1421 * and DTRACE_ACCESS_ARGS
1417 1422 */
1418 1423 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
1419 1424 cred_t *cr;
1420 1425 cred_t *s_cr = state->dts_cred.dcr_cred;
1421 1426
1422 1427 ASSERT(s_cr != NULL);
1423 1428
1424 1429 if ((cr = CRED()) == NULL ||
1425 1430 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) {
1426 1431 if (mode & DTRACE_MODE_NOPRIV_DROP)
1427 1432 return (0);
1428 1433
1429 1434 mstate->dtms_access &=
1430 1435 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS);
1431 1436 }
1432 1437 }
1433 1438
1434 1439 /*
1435 1440 * By merits of being in this code path at all, we have limited
1436 1441 * privileges. If the provider has indicated that limited privileges
1437 1442 * are to denote restricted operation, strip off the ability to access
1438 1443 * arguments.
1439 1444 */
1440 1445 if (mode & DTRACE_MODE_LIMITEDPRIV_RESTRICT)
1441 1446 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
1442 1447
1443 1448 return (1);
1444 1449 }
1445 1450
1446 1451 /*
1447 1452 * Note: not called from probe context. This function is called
1448 1453 * asynchronously (and at a regular interval) from outside of probe context to
1449 1454 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1450 1455 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1451 1456 */
1452 1457 void
1453 1458 dtrace_dynvar_clean(dtrace_dstate_t *dstate)
1454 1459 {
1455 1460 dtrace_dynvar_t *dirty;
1456 1461 dtrace_dstate_percpu_t *dcpu;
1457 1462 dtrace_dynvar_t **rinsep;
1458 1463 int i, j, work = 0;
1459 1464
1460 1465 for (i = 0; i < NCPU; i++) {
1461 1466 dcpu = &dstate->dtds_percpu[i];
1462 1467 rinsep = &dcpu->dtdsc_rinsing;
1463 1468
1464 1469 /*
1465 1470 * If the dirty list is NULL, there is no dirty work to do.
1466 1471 */
1467 1472 if (dcpu->dtdsc_dirty == NULL)
1468 1473 continue;
1469 1474
1470 1475 if (dcpu->dtdsc_rinsing != NULL) {
1471 1476 /*
1472 1477 * If the rinsing list is non-NULL, then it is because
1473 1478 * this CPU was selected to accept another CPU's
1474 1479 * dirty list -- and since that time, dirty buffers
1475 1480 * have accumulated. This is a highly unlikely
1476 1481 * condition, but we choose to ignore the dirty
1477 1482 * buffers -- they'll be picked up a future cleanse.
1478 1483 */
1479 1484 continue;
1480 1485 }
1481 1486
1482 1487 if (dcpu->dtdsc_clean != NULL) {
1483 1488 /*
1484 1489 * If the clean list is non-NULL, then we're in a
1485 1490 * situation where a CPU has done deallocations (we
1486 1491 * have a non-NULL dirty list) but no allocations (we
1487 1492 * also have a non-NULL clean list). We can't simply
1488 1493 * move the dirty list into the clean list on this
1489 1494 * CPU, yet we also don't want to allow this condition
1490 1495 * to persist, lest a short clean list prevent a
1491 1496 * massive dirty list from being cleaned (which in
1492 1497 * turn could lead to otherwise avoidable dynamic
1493 1498 * drops). To deal with this, we look for some CPU
1494 1499 * with a NULL clean list, NULL dirty list, and NULL
1495 1500 * rinsing list -- and then we borrow this CPU to
1496 1501 * rinse our dirty list.
1497 1502 */
1498 1503 for (j = 0; j < NCPU; j++) {
1499 1504 dtrace_dstate_percpu_t *rinser;
1500 1505
1501 1506 rinser = &dstate->dtds_percpu[j];
1502 1507
1503 1508 if (rinser->dtdsc_rinsing != NULL)
1504 1509 continue;
1505 1510
1506 1511 if (rinser->dtdsc_dirty != NULL)
1507 1512 continue;
1508 1513
1509 1514 if (rinser->dtdsc_clean != NULL)
1510 1515 continue;
1511 1516
1512 1517 rinsep = &rinser->dtdsc_rinsing;
1513 1518 break;
1514 1519 }
1515 1520
1516 1521 if (j == NCPU) {
1517 1522 /*
1518 1523 * We were unable to find another CPU that
1519 1524 * could accept this dirty list -- we are
1520 1525 * therefore unable to clean it now.
1521 1526 */
1522 1527 dtrace_dynvar_failclean++;
1523 1528 continue;
1524 1529 }
1525 1530 }
1526 1531
1527 1532 work = 1;
1528 1533
1529 1534 /*
1530 1535 * Atomically move the dirty list aside.
1531 1536 */
1532 1537 do {
1533 1538 dirty = dcpu->dtdsc_dirty;
1534 1539
1535 1540 /*
1536 1541 * Before we zap the dirty list, set the rinsing list.
1537 1542 * (This allows for a potential assertion in
1538 1543 * dtrace_dynvar(): if a free dynamic variable appears
1539 1544 * on a hash chain, either the dirty list or the
1540 1545 * rinsing list for some CPU must be non-NULL.)
1541 1546 */
1542 1547 *rinsep = dirty;
1543 1548 dtrace_membar_producer();
1544 1549 } while (dtrace_casptr(&dcpu->dtdsc_dirty,
1545 1550 dirty, NULL) != dirty);
1546 1551 }
1547 1552
1548 1553 if (!work) {
1549 1554 /*
1550 1555 * We have no work to do; we can simply return.
1551 1556 */
1552 1557 return;
1553 1558 }
1554 1559
1555 1560 dtrace_sync();
1556 1561
1557 1562 for (i = 0; i < NCPU; i++) {
1558 1563 dcpu = &dstate->dtds_percpu[i];
1559 1564
1560 1565 if (dcpu->dtdsc_rinsing == NULL)
1561 1566 continue;
1562 1567
1563 1568 /*
1564 1569 * We are now guaranteed that no hash chain contains a pointer
1565 1570 * into this dirty list; we can make it clean.
1566 1571 */
1567 1572 ASSERT(dcpu->dtdsc_clean == NULL);
1568 1573 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1569 1574 dcpu->dtdsc_rinsing = NULL;
1570 1575 }
1571 1576
1572 1577 /*
1573 1578 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1574 1579 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1575 1580 * This prevents a race whereby a CPU incorrectly decides that
1576 1581 * the state should be something other than DTRACE_DSTATE_CLEAN
1577 1582 * after dtrace_dynvar_clean() has completed.
1578 1583 */
1579 1584 dtrace_sync();
1580 1585
1581 1586 dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1582 1587 }
1583 1588
1584 1589 /*
1585 1590 * Depending on the value of the op parameter, this function looks-up,
1586 1591 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1587 1592 * allocation is requested, this function will return a pointer to a
1588 1593 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1589 1594 * variable can be allocated. If NULL is returned, the appropriate counter
1590 1595 * will be incremented.
1591 1596 */
1592 1597 dtrace_dynvar_t *
1593 1598 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1594 1599 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1595 1600 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1596 1601 {
1597 1602 uint64_t hashval = DTRACE_DYNHASH_VALID;
1598 1603 dtrace_dynhash_t *hash = dstate->dtds_hash;
1599 1604 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1600 1605 processorid_t me = CPU->cpu_id, cpu = me;
1601 1606 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
1602 1607 size_t bucket, ksize;
1603 1608 size_t chunksize = dstate->dtds_chunksize;
1604 1609 uintptr_t kdata, lock, nstate;
1605 1610 uint_t i;
1606 1611
1607 1612 ASSERT(nkeys != 0);
1608 1613
1609 1614 /*
1610 1615 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1611 1616 * algorithm. For the by-value portions, we perform the algorithm in
1612 1617 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1613 1618 * bit, and seems to have only a minute effect on distribution. For
1614 1619 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1615 1620 * over each referenced byte. It's painful to do this, but it's much
1616 1621 * better than pathological hash distribution. The efficacy of the
1617 1622 * hashing algorithm (and a comparison with other algorithms) may be
1618 1623 * found by running the ::dtrace_dynstat MDB dcmd.
1619 1624 */
1620 1625 for (i = 0; i < nkeys; i++) {
1621 1626 if (key[i].dttk_size == 0) {
1622 1627 uint64_t val = key[i].dttk_value;
1623 1628
1624 1629 hashval += (val >> 48) & 0xffff;
1625 1630 hashval += (hashval << 10);
1626 1631 hashval ^= (hashval >> 6);
1627 1632
1628 1633 hashval += (val >> 32) & 0xffff;
1629 1634 hashval += (hashval << 10);
1630 1635 hashval ^= (hashval >> 6);
1631 1636
1632 1637 hashval += (val >> 16) & 0xffff;
1633 1638 hashval += (hashval << 10);
1634 1639 hashval ^= (hashval >> 6);
1635 1640
1636 1641 hashval += val & 0xffff;
1637 1642 hashval += (hashval << 10);
1638 1643 hashval ^= (hashval >> 6);
1639 1644 } else {
1640 1645 /*
1641 1646 * This is incredibly painful, but it beats the hell
1642 1647 * out of the alternative.
1643 1648 */
1644 1649 uint64_t j, size = key[i].dttk_size;
1645 1650 uintptr_t base = (uintptr_t)key[i].dttk_value;
1646 1651
1647 1652 if (!dtrace_canload(base, size, mstate, vstate))
1648 1653 break;
1649 1654
1650 1655 for (j = 0; j < size; j++) {
1651 1656 hashval += dtrace_load8(base + j);
1652 1657 hashval += (hashval << 10);
1653 1658 hashval ^= (hashval >> 6);
1654 1659 }
1655 1660 }
1656 1661 }
1657 1662
1658 1663 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1659 1664 return (NULL);
1660 1665
1661 1666 hashval += (hashval << 3);
1662 1667 hashval ^= (hashval >> 11);
1663 1668 hashval += (hashval << 15);
1664 1669
1665 1670 /*
1666 1671 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1667 1672 * comes out to be one of our two sentinel hash values. If this
1668 1673 * actually happens, we set the hashval to be a value known to be a
1669 1674 * non-sentinel value.
1670 1675 */
1671 1676 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1672 1677 hashval = DTRACE_DYNHASH_VALID;
1673 1678
1674 1679 /*
1675 1680 * Yes, it's painful to do a divide here. If the cycle count becomes
1676 1681 * important here, tricks can be pulled to reduce it. (However, it's
1677 1682 * critical that hash collisions be kept to an absolute minimum;
1678 1683 * they're much more painful than a divide.) It's better to have a
1679 1684 * solution that generates few collisions and still keeps things
1680 1685 * relatively simple.
1681 1686 */
1682 1687 bucket = hashval % dstate->dtds_hashsize;
1683 1688
1684 1689 if (op == DTRACE_DYNVAR_DEALLOC) {
1685 1690 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1686 1691
1687 1692 for (;;) {
1688 1693 while ((lock = *lockp) & 1)
1689 1694 continue;
1690 1695
1691 1696 if (dtrace_casptr((void *)lockp,
1692 1697 (void *)lock, (void *)(lock + 1)) == (void *)lock)
1693 1698 break;
1694 1699 }
1695 1700
1696 1701 dtrace_membar_producer();
1697 1702 }
1698 1703
1699 1704 top:
1700 1705 prev = NULL;
1701 1706 lock = hash[bucket].dtdh_lock;
1702 1707
1703 1708 dtrace_membar_consumer();
1704 1709
1705 1710 start = hash[bucket].dtdh_chain;
1706 1711 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1707 1712 start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1708 1713 op != DTRACE_DYNVAR_DEALLOC));
1709 1714
1710 1715 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1711 1716 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1712 1717 dtrace_key_t *dkey = &dtuple->dtt_key[0];
1713 1718
1714 1719 if (dvar->dtdv_hashval != hashval) {
1715 1720 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1716 1721 /*
1717 1722 * We've reached the sink, and therefore the
1718 1723 * end of the hash chain; we can kick out of
1719 1724 * the loop knowing that we have seen a valid
1720 1725 * snapshot of state.
1721 1726 */
1722 1727 ASSERT(dvar->dtdv_next == NULL);
1723 1728 ASSERT(dvar == &dtrace_dynhash_sink);
1724 1729 break;
1725 1730 }
1726 1731
1727 1732 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
1728 1733 /*
1729 1734 * We've gone off the rails: somewhere along
1730 1735 * the line, one of the members of this hash
1731 1736 * chain was deleted. Note that we could also
1732 1737 * detect this by simply letting this loop run
1733 1738 * to completion, as we would eventually hit
1734 1739 * the end of the dirty list. However, we
1735 1740 * want to avoid running the length of the
1736 1741 * dirty list unnecessarily (it might be quite
1737 1742 * long), so we catch this as early as
1738 1743 * possible by detecting the hash marker. In
1739 1744 * this case, we simply set dvar to NULL and
1740 1745 * break; the conditional after the loop will
1741 1746 * send us back to top.
1742 1747 */
1743 1748 dvar = NULL;
1744 1749 break;
1745 1750 }
1746 1751
1747 1752 goto next;
1748 1753 }
1749 1754
1750 1755 if (dtuple->dtt_nkeys != nkeys)
1751 1756 goto next;
1752 1757
1753 1758 for (i = 0; i < nkeys; i++, dkey++) {
1754 1759 if (dkey->dttk_size != key[i].dttk_size)
1755 1760 goto next; /* size or type mismatch */
1756 1761
1757 1762 if (dkey->dttk_size != 0) {
1758 1763 if (dtrace_bcmp(
1759 1764 (void *)(uintptr_t)key[i].dttk_value,
1760 1765 (void *)(uintptr_t)dkey->dttk_value,
1761 1766 dkey->dttk_size))
1762 1767 goto next;
1763 1768 } else {
1764 1769 if (dkey->dttk_value != key[i].dttk_value)
1765 1770 goto next;
1766 1771 }
1767 1772 }
1768 1773
1769 1774 if (op != DTRACE_DYNVAR_DEALLOC)
1770 1775 return (dvar);
1771 1776
1772 1777 ASSERT(dvar->dtdv_next == NULL ||
1773 1778 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
1774 1779
1775 1780 if (prev != NULL) {
1776 1781 ASSERT(hash[bucket].dtdh_chain != dvar);
1777 1782 ASSERT(start != dvar);
1778 1783 ASSERT(prev->dtdv_next == dvar);
1779 1784 prev->dtdv_next = dvar->dtdv_next;
1780 1785 } else {
1781 1786 if (dtrace_casptr(&hash[bucket].dtdh_chain,
1782 1787 start, dvar->dtdv_next) != start) {
1783 1788 /*
1784 1789 * We have failed to atomically swing the
1785 1790 * hash table head pointer, presumably because
1786 1791 * of a conflicting allocation on another CPU.
1787 1792 * We need to reread the hash chain and try
1788 1793 * again.
1789 1794 */
1790 1795 goto top;
1791 1796 }
1792 1797 }
1793 1798
1794 1799 dtrace_membar_producer();
1795 1800
1796 1801 /*
1797 1802 * Now set the hash value to indicate that it's free.
1798 1803 */
1799 1804 ASSERT(hash[bucket].dtdh_chain != dvar);
1800 1805 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1801 1806
1802 1807 dtrace_membar_producer();
1803 1808
1804 1809 /*
1805 1810 * Set the next pointer to point at the dirty list, and
1806 1811 * atomically swing the dirty pointer to the newly freed dvar.
1807 1812 */
1808 1813 do {
1809 1814 next = dcpu->dtdsc_dirty;
1810 1815 dvar->dtdv_next = next;
1811 1816 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
1812 1817
1813 1818 /*
1814 1819 * Finally, unlock this hash bucket.
1815 1820 */
1816 1821 ASSERT(hash[bucket].dtdh_lock == lock);
1817 1822 ASSERT(lock & 1);
1818 1823 hash[bucket].dtdh_lock++;
1819 1824
1820 1825 return (NULL);
1821 1826 next:
1822 1827 prev = dvar;
1823 1828 continue;
1824 1829 }
1825 1830
1826 1831 if (dvar == NULL) {
1827 1832 /*
1828 1833 * If dvar is NULL, it is because we went off the rails:
1829 1834 * one of the elements that we traversed in the hash chain
1830 1835 * was deleted while we were traversing it. In this case,
1831 1836 * we assert that we aren't doing a dealloc (deallocs lock
1832 1837 * the hash bucket to prevent themselves from racing with
1833 1838 * one another), and retry the hash chain traversal.
1834 1839 */
1835 1840 ASSERT(op != DTRACE_DYNVAR_DEALLOC);
1836 1841 goto top;
1837 1842 }
1838 1843
1839 1844 if (op != DTRACE_DYNVAR_ALLOC) {
1840 1845 /*
1841 1846 * If we are not to allocate a new variable, we want to
1842 1847 * return NULL now. Before we return, check that the value
1843 1848 * of the lock word hasn't changed. If it has, we may have
1844 1849 * seen an inconsistent snapshot.
1845 1850 */
1846 1851 if (op == DTRACE_DYNVAR_NOALLOC) {
1847 1852 if (hash[bucket].dtdh_lock != lock)
1848 1853 goto top;
1849 1854 } else {
1850 1855 ASSERT(op == DTRACE_DYNVAR_DEALLOC);
1851 1856 ASSERT(hash[bucket].dtdh_lock == lock);
1852 1857 ASSERT(lock & 1);
1853 1858 hash[bucket].dtdh_lock++;
1854 1859 }
1855 1860
1856 1861 return (NULL);
1857 1862 }
1858 1863
1859 1864 /*
1860 1865 * We need to allocate a new dynamic variable. The size we need is the
1861 1866 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
1862 1867 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
1863 1868 * the size of any referred-to data (dsize). We then round the final
1864 1869 * size up to the chunksize for allocation.
1865 1870 */
1866 1871 for (ksize = 0, i = 0; i < nkeys; i++)
1867 1872 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
1868 1873
1869 1874 /*
1870 1875 * This should be pretty much impossible, but could happen if, say,
1871 1876 * strange DIF specified the tuple. Ideally, this should be an
1872 1877 * assertion and not an error condition -- but that requires that the
1873 1878 * chunksize calculation in dtrace_difo_chunksize() be absolutely
1874 1879 * bullet-proof. (That is, it must not be able to be fooled by
1875 1880 * malicious DIF.) Given the lack of backwards branches in DIF,
1876 1881 * solving this would presumably not amount to solving the Halting
1877 1882 * Problem -- but it still seems awfully hard.
1878 1883 */
1879 1884 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
1880 1885 ksize + dsize > chunksize) {
1881 1886 dcpu->dtdsc_drops++;
1882 1887 return (NULL);
1883 1888 }
1884 1889
1885 1890 nstate = DTRACE_DSTATE_EMPTY;
1886 1891
1887 1892 do {
1888 1893 retry:
1889 1894 free = dcpu->dtdsc_free;
1890 1895
1891 1896 if (free == NULL) {
1892 1897 dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
1893 1898 void *rval;
1894 1899
1895 1900 if (clean == NULL) {
1896 1901 /*
1897 1902 * We're out of dynamic variable space on
1898 1903 * this CPU. Unless we have tried all CPUs,
1899 1904 * we'll try to allocate from a different
1900 1905 * CPU.
1901 1906 */
1902 1907 switch (dstate->dtds_state) {
1903 1908 case DTRACE_DSTATE_CLEAN: {
1904 1909 void *sp = &dstate->dtds_state;
1905 1910
1906 1911 if (++cpu >= NCPU)
1907 1912 cpu = 0;
1908 1913
1909 1914 if (dcpu->dtdsc_dirty != NULL &&
1910 1915 nstate == DTRACE_DSTATE_EMPTY)
1911 1916 nstate = DTRACE_DSTATE_DIRTY;
1912 1917
1913 1918 if (dcpu->dtdsc_rinsing != NULL)
1914 1919 nstate = DTRACE_DSTATE_RINSING;
1915 1920
1916 1921 dcpu = &dstate->dtds_percpu[cpu];
1917 1922
1918 1923 if (cpu != me)
1919 1924 goto retry;
1920 1925
1921 1926 (void) dtrace_cas32(sp,
1922 1927 DTRACE_DSTATE_CLEAN, nstate);
1923 1928
1924 1929 /*
1925 1930 * To increment the correct bean
1926 1931 * counter, take another lap.
1927 1932 */
1928 1933 goto retry;
1929 1934 }
1930 1935
1931 1936 case DTRACE_DSTATE_DIRTY:
1932 1937 dcpu->dtdsc_dirty_drops++;
1933 1938 break;
1934 1939
1935 1940 case DTRACE_DSTATE_RINSING:
1936 1941 dcpu->dtdsc_rinsing_drops++;
1937 1942 break;
1938 1943
1939 1944 case DTRACE_DSTATE_EMPTY:
1940 1945 dcpu->dtdsc_drops++;
1941 1946 break;
1942 1947 }
1943 1948
1944 1949 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
1945 1950 return (NULL);
1946 1951 }
1947 1952
1948 1953 /*
1949 1954 * The clean list appears to be non-empty. We want to
1950 1955 * move the clean list to the free list; we start by
1951 1956 * moving the clean pointer aside.
1952 1957 */
1953 1958 if (dtrace_casptr(&dcpu->dtdsc_clean,
1954 1959 clean, NULL) != clean) {
1955 1960 /*
1956 1961 * We are in one of two situations:
1957 1962 *
1958 1963 * (a) The clean list was switched to the
1959 1964 * free list by another CPU.
1960 1965 *
1961 1966 * (b) The clean list was added to by the
1962 1967 * cleansing cyclic.
1963 1968 *
1964 1969 * In either of these situations, we can
1965 1970 * just reattempt the free list allocation.
1966 1971 */
1967 1972 goto retry;
1968 1973 }
1969 1974
1970 1975 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
1971 1976
1972 1977 /*
1973 1978 * Now we'll move the clean list to our free list.
1974 1979 * It's impossible for this to fail: the only way
1975 1980 * the free list can be updated is through this
1976 1981 * code path, and only one CPU can own the clean list.
1977 1982 * Thus, it would only be possible for this to fail if
1978 1983 * this code were racing with dtrace_dynvar_clean().
1979 1984 * (That is, if dtrace_dynvar_clean() updated the clean
1980 1985 * list, and we ended up racing to update the free
1981 1986 * list.) This race is prevented by the dtrace_sync()
1982 1987 * in dtrace_dynvar_clean() -- which flushes the
1983 1988 * owners of the clean lists out before resetting
1984 1989 * the clean lists.
1985 1990 */
1986 1991 dcpu = &dstate->dtds_percpu[me];
1987 1992 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
1988 1993 ASSERT(rval == NULL);
1989 1994 goto retry;
1990 1995 }
1991 1996
1992 1997 dvar = free;
1993 1998 new_free = dvar->dtdv_next;
1994 1999 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
1995 2000
1996 2001 /*
1997 2002 * We have now allocated a new chunk. We copy the tuple keys into the
1998 2003 * tuple array and copy any referenced key data into the data space
1999 2004 * following the tuple array. As we do this, we relocate dttk_value
2000 2005 * in the final tuple to point to the key data address in the chunk.
2001 2006 */
2002 2007 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
2003 2008 dvar->dtdv_data = (void *)(kdata + ksize);
2004 2009 dvar->dtdv_tuple.dtt_nkeys = nkeys;
2005 2010
2006 2011 for (i = 0; i < nkeys; i++) {
2007 2012 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
2008 2013 size_t kesize = key[i].dttk_size;
2009 2014
2010 2015 if (kesize != 0) {
2011 2016 dtrace_bcopy(
2012 2017 (const void *)(uintptr_t)key[i].dttk_value,
2013 2018 (void *)kdata, kesize);
2014 2019 dkey->dttk_value = kdata;
2015 2020 kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
2016 2021 } else {
2017 2022 dkey->dttk_value = key[i].dttk_value;
2018 2023 }
2019 2024
2020 2025 dkey->dttk_size = kesize;
2021 2026 }
2022 2027
2023 2028 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
2024 2029 dvar->dtdv_hashval = hashval;
2025 2030 dvar->dtdv_next = start;
2026 2031
2027 2032 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
2028 2033 return (dvar);
2029 2034
2030 2035 /*
2031 2036 * The cas has failed. Either another CPU is adding an element to
2032 2037 * this hash chain, or another CPU is deleting an element from this
2033 2038 * hash chain. The simplest way to deal with both of these cases
2034 2039 * (though not necessarily the most efficient) is to free our
2035 2040 * allocated block and tail-call ourselves. Note that the free is
2036 2041 * to the dirty list and _not_ to the free list. This is to prevent
2037 2042 * races with allocators, above.
2038 2043 */
2039 2044 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2040 2045
2041 2046 dtrace_membar_producer();
2042 2047
2043 2048 do {
2044 2049 free = dcpu->dtdsc_dirty;
2045 2050 dvar->dtdv_next = free;
2046 2051 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
2047 2052
2048 2053 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate));
2049 2054 }
2050 2055
2051 2056 /*ARGSUSED*/
2052 2057 static void
2053 2058 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
2054 2059 {
2055 2060 if ((int64_t)nval < (int64_t)*oval)
2056 2061 *oval = nval;
2057 2062 }
2058 2063
2059 2064 /*ARGSUSED*/
2060 2065 static void
2061 2066 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
2062 2067 {
2063 2068 if ((int64_t)nval > (int64_t)*oval)
2064 2069 *oval = nval;
2065 2070 }
2066 2071
2067 2072 static void
2068 2073 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
2069 2074 {
2070 2075 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
2071 2076 int64_t val = (int64_t)nval;
2072 2077
2073 2078 if (val < 0) {
2074 2079 for (i = 0; i < zero; i++) {
2075 2080 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
2076 2081 quanta[i] += incr;
2077 2082 return;
2078 2083 }
2079 2084 }
2080 2085 } else {
2081 2086 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
2082 2087 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
2083 2088 quanta[i - 1] += incr;
2084 2089 return;
2085 2090 }
2086 2091 }
2087 2092
2088 2093 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
2089 2094 return;
2090 2095 }
2091 2096
2092 2097 ASSERT(0);
2093 2098 }
2094 2099
2095 2100 static void
2096 2101 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
2097 2102 {
2098 2103 uint64_t arg = *lquanta++;
2099 2104 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2100 2105 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2101 2106 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
2102 2107 int32_t val = (int32_t)nval, level;
2103 2108
2104 2109 ASSERT(step != 0);
2105 2110 ASSERT(levels != 0);
2106 2111
2107 2112 if (val < base) {
2108 2113 /*
2109 2114 * This is an underflow.
2110 2115 */
2111 2116 lquanta[0] += incr;
2112 2117 return;
2113 2118 }
2114 2119
2115 2120 level = (val - base) / step;
2116 2121
2117 2122 if (level < levels) {
2118 2123 lquanta[level + 1] += incr;
2119 2124 return;
2120 2125 }
2121 2126
2122 2127 /*
2123 2128 * This is an overflow.
2124 2129 */
2125 2130 lquanta[levels + 1] += incr;
2126 2131 }
2127 2132
2128 2133 static int
2129 2134 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low,
2130 2135 uint16_t high, uint16_t nsteps, int64_t value)
2131 2136 {
2132 2137 int64_t this = 1, last, next;
2133 2138 int base = 1, order;
2134 2139
2135 2140 ASSERT(factor <= nsteps);
2136 2141 ASSERT(nsteps % factor == 0);
2137 2142
2138 2143 for (order = 0; order < low; order++)
2139 2144 this *= factor;
2140 2145
2141 2146 /*
2142 2147 * If our value is less than our factor taken to the power of the
2143 2148 * low order of magnitude, it goes into the zeroth bucket.
2144 2149 */
2145 2150 if (value < (last = this))
2146 2151 return (0);
2147 2152
2148 2153 for (this *= factor; order <= high; order++) {
2149 2154 int nbuckets = this > nsteps ? nsteps : this;
2150 2155
2151 2156 if ((next = this * factor) < this) {
2152 2157 /*
2153 2158 * We should not generally get log/linear quantizations
2154 2159 * with a high magnitude that allows 64-bits to
2155 2160 * overflow, but we nonetheless protect against this
2156 2161 * by explicitly checking for overflow, and clamping
2157 2162 * our value accordingly.
2158 2163 */
2159 2164 value = this - 1;
2160 2165 }
2161 2166
2162 2167 if (value < this) {
2163 2168 /*
2164 2169 * If our value lies within this order of magnitude,
2165 2170 * determine its position by taking the offset within
2166 2171 * the order of magnitude, dividing by the bucket
2167 2172 * width, and adding to our (accumulated) base.
2168 2173 */
2169 2174 return (base + (value - last) / (this / nbuckets));
2170 2175 }
2171 2176
2172 2177 base += nbuckets - (nbuckets / factor);
2173 2178 last = this;
2174 2179 this = next;
2175 2180 }
2176 2181
2177 2182 /*
2178 2183 * Our value is greater than or equal to our factor taken to the
2179 2184 * power of one plus the high magnitude -- return the top bucket.
2180 2185 */
2181 2186 return (base);
2182 2187 }
2183 2188
2184 2189 static void
2185 2190 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
2186 2191 {
2187 2192 uint64_t arg = *llquanta++;
2188 2193 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
2189 2194 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
2190 2195 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
2191 2196 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
2192 2197
2193 2198 llquanta[dtrace_aggregate_llquantize_bucket(factor,
2194 2199 low, high, nsteps, nval)] += incr;
2195 2200 }
2196 2201
2197 2202 /*ARGSUSED*/
2198 2203 static void
2199 2204 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
2200 2205 {
2201 2206 data[0]++;
2202 2207 data[1] += nval;
2203 2208 }
2204 2209
2205 2210 /*ARGSUSED*/
2206 2211 static void
2207 2212 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2208 2213 {
2209 2214 int64_t snval = (int64_t)nval;
2210 2215 uint64_t tmp[2];
2211 2216
2212 2217 data[0]++;
2213 2218 data[1] += nval;
2214 2219
2215 2220 /*
2216 2221 * What we want to say here is:
2217 2222 *
2218 2223 * data[2] += nval * nval;
2219 2224 *
2220 2225 * But given that nval is 64-bit, we could easily overflow, so
2221 2226 * we do this as 128-bit arithmetic.
2222 2227 */
2223 2228 if (snval < 0)
2224 2229 snval = -snval;
2225 2230
2226 2231 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2227 2232 dtrace_add_128(data + 2, tmp, data + 2);
2228 2233 }
2229 2234
2230 2235 /*ARGSUSED*/
2231 2236 static void
2232 2237 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2233 2238 {
2234 2239 *oval = *oval + 1;
2235 2240 }
2236 2241
2237 2242 /*ARGSUSED*/
2238 2243 static void
2239 2244 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2240 2245 {
2241 2246 *oval += nval;
2242 2247 }
2243 2248
2244 2249 /*
2245 2250 * Aggregate given the tuple in the principal data buffer, and the aggregating
2246 2251 * action denoted by the specified dtrace_aggregation_t. The aggregation
2247 2252 * buffer is specified as the buf parameter. This routine does not return
2248 2253 * failure; if there is no space in the aggregation buffer, the data will be
2249 2254 * dropped, and a corresponding counter incremented.
2250 2255 */
2251 2256 static void
2252 2257 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2253 2258 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2254 2259 {
2255 2260 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2256 2261 uint32_t i, ndx, size, fsize;
2257 2262 uint32_t align = sizeof (uint64_t) - 1;
2258 2263 dtrace_aggbuffer_t *agb;
2259 2264 dtrace_aggkey_t *key;
2260 2265 uint32_t hashval = 0, limit, isstr;
2261 2266 caddr_t tomax, data, kdata;
2262 2267 dtrace_actkind_t action;
2263 2268 dtrace_action_t *act;
2264 2269 uintptr_t offs;
2265 2270
2266 2271 if (buf == NULL)
2267 2272 return;
2268 2273
2269 2274 if (!agg->dtag_hasarg) {
2270 2275 /*
2271 2276 * Currently, only quantize() and lquantize() take additional
2272 2277 * arguments, and they have the same semantics: an increment
2273 2278 * value that defaults to 1 when not present. If additional
2274 2279 * aggregating actions take arguments, the setting of the
2275 2280 * default argument value will presumably have to become more
2276 2281 * sophisticated...
2277 2282 */
2278 2283 arg = 1;
2279 2284 }
2280 2285
2281 2286 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2282 2287 size = rec->dtrd_offset - agg->dtag_base;
2283 2288 fsize = size + rec->dtrd_size;
2284 2289
2285 2290 ASSERT(dbuf->dtb_tomax != NULL);
2286 2291 data = dbuf->dtb_tomax + offset + agg->dtag_base;
2287 2292
2288 2293 if ((tomax = buf->dtb_tomax) == NULL) {
2289 2294 dtrace_buffer_drop(buf);
2290 2295 return;
2291 2296 }
2292 2297
2293 2298 /*
2294 2299 * The metastructure is always at the bottom of the buffer.
2295 2300 */
2296 2301 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2297 2302 sizeof (dtrace_aggbuffer_t));
2298 2303
2299 2304 if (buf->dtb_offset == 0) {
2300 2305 /*
2301 2306 * We just kludge up approximately 1/8th of the size to be
2302 2307 * buckets. If this guess ends up being routinely
2303 2308 * off-the-mark, we may need to dynamically readjust this
2304 2309 * based on past performance.
2305 2310 */
2306 2311 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2307 2312
2308 2313 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2309 2314 (uintptr_t)tomax || hashsize == 0) {
2310 2315 /*
2311 2316 * We've been given a ludicrously small buffer;
2312 2317 * increment our drop count and leave.
2313 2318 */
2314 2319 dtrace_buffer_drop(buf);
2315 2320 return;
2316 2321 }
2317 2322
2318 2323 /*
2319 2324 * And now, a pathetic attempt to try to get a an odd (or
2320 2325 * perchance, a prime) hash size for better hash distribution.
2321 2326 */
2322 2327 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2323 2328 hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2324 2329
2325 2330 agb->dtagb_hashsize = hashsize;
2326 2331 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2327 2332 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2328 2333 agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2329 2334
2330 2335 for (i = 0; i < agb->dtagb_hashsize; i++)
2331 2336 agb->dtagb_hash[i] = NULL;
2332 2337 }
2333 2338
2334 2339 ASSERT(agg->dtag_first != NULL);
2335 2340 ASSERT(agg->dtag_first->dta_intuple);
2336 2341
2337 2342 /*
2338 2343 * Calculate the hash value based on the key. Note that we _don't_
2339 2344 * include the aggid in the hashing (but we will store it as part of
2340 2345 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2341 2346 * algorithm: a simple, quick algorithm that has no known funnels, and
2342 2347 * gets good distribution in practice. The efficacy of the hashing
2343 2348 * algorithm (and a comparison with other algorithms) may be found by
2344 2349 * running the ::dtrace_aggstat MDB dcmd.
2345 2350 */
2346 2351 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2347 2352 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2348 2353 limit = i + act->dta_rec.dtrd_size;
2349 2354 ASSERT(limit <= size);
2350 2355 isstr = DTRACEACT_ISSTRING(act);
2351 2356
2352 2357 for (; i < limit; i++) {
2353 2358 hashval += data[i];
2354 2359 hashval += (hashval << 10);
2355 2360 hashval ^= (hashval >> 6);
2356 2361
2357 2362 if (isstr && data[i] == '\0')
2358 2363 break;
2359 2364 }
2360 2365 }
2361 2366
2362 2367 hashval += (hashval << 3);
2363 2368 hashval ^= (hashval >> 11);
2364 2369 hashval += (hashval << 15);
2365 2370
2366 2371 /*
2367 2372 * Yes, the divide here is expensive -- but it's generally the least
2368 2373 * of the performance issues given the amount of data that we iterate
2369 2374 * over to compute hash values, compare data, etc.
2370 2375 */
2371 2376 ndx = hashval % agb->dtagb_hashsize;
2372 2377
2373 2378 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2374 2379 ASSERT((caddr_t)key >= tomax);
2375 2380 ASSERT((caddr_t)key < tomax + buf->dtb_size);
2376 2381
2377 2382 if (hashval != key->dtak_hashval || key->dtak_size != size)
2378 2383 continue;
2379 2384
2380 2385 kdata = key->dtak_data;
2381 2386 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2382 2387
2383 2388 for (act = agg->dtag_first; act->dta_intuple;
2384 2389 act = act->dta_next) {
2385 2390 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2386 2391 limit = i + act->dta_rec.dtrd_size;
2387 2392 ASSERT(limit <= size);
2388 2393 isstr = DTRACEACT_ISSTRING(act);
2389 2394
2390 2395 for (; i < limit; i++) {
2391 2396 if (kdata[i] != data[i])
2392 2397 goto next;
2393 2398
2394 2399 if (isstr && data[i] == '\0')
2395 2400 break;
2396 2401 }
2397 2402 }
2398 2403
2399 2404 if (action != key->dtak_action) {
2400 2405 /*
2401 2406 * We are aggregating on the same value in the same
2402 2407 * aggregation with two different aggregating actions.
2403 2408 * (This should have been picked up in the compiler,
2404 2409 * so we may be dealing with errant or devious DIF.)
2405 2410 * This is an error condition; we indicate as much,
2406 2411 * and return.
2407 2412 */
2408 2413 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2409 2414 return;
2410 2415 }
2411 2416
2412 2417 /*
2413 2418 * This is a hit: we need to apply the aggregator to
2414 2419 * the value at this key.
2415 2420 */
2416 2421 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2417 2422 return;
2418 2423 next:
2419 2424 continue;
2420 2425 }
2421 2426
2422 2427 /*
2423 2428 * We didn't find it. We need to allocate some zero-filled space,
2424 2429 * link it into the hash table appropriately, and apply the aggregator
2425 2430 * to the (zero-filled) value.
2426 2431 */
2427 2432 offs = buf->dtb_offset;
2428 2433 while (offs & (align - 1))
2429 2434 offs += sizeof (uint32_t);
2430 2435
2431 2436 /*
2432 2437 * If we don't have enough room to both allocate a new key _and_
2433 2438 * its associated data, increment the drop count and return.
2434 2439 */
2435 2440 if ((uintptr_t)tomax + offs + fsize >
2436 2441 agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2437 2442 dtrace_buffer_drop(buf);
2438 2443 return;
2439 2444 }
2440 2445
2441 2446 /*CONSTCOND*/
2442 2447 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2443 2448 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2444 2449 agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2445 2450
2446 2451 key->dtak_data = kdata = tomax + offs;
2447 2452 buf->dtb_offset = offs + fsize;
2448 2453
2449 2454 /*
2450 2455 * Now copy the data across.
2451 2456 */
2452 2457 *((dtrace_aggid_t *)kdata) = agg->dtag_id;
2453 2458
2454 2459 for (i = sizeof (dtrace_aggid_t); i < size; i++)
2455 2460 kdata[i] = data[i];
2456 2461
2457 2462 /*
2458 2463 * Because strings are not zeroed out by default, we need to iterate
2459 2464 * looking for actions that store strings, and we need to explicitly
2460 2465 * pad these strings out with zeroes.
2461 2466 */
2462 2467 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2463 2468 int nul;
2464 2469
2465 2470 if (!DTRACEACT_ISSTRING(act))
2466 2471 continue;
2467 2472
2468 2473 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2469 2474 limit = i + act->dta_rec.dtrd_size;
2470 2475 ASSERT(limit <= size);
2471 2476
2472 2477 for (nul = 0; i < limit; i++) {
2473 2478 if (nul) {
2474 2479 kdata[i] = '\0';
2475 2480 continue;
2476 2481 }
2477 2482
2478 2483 if (data[i] != '\0')
2479 2484 continue;
2480 2485
2481 2486 nul = 1;
2482 2487 }
2483 2488 }
2484 2489
2485 2490 for (i = size; i < fsize; i++)
2486 2491 kdata[i] = 0;
2487 2492
2488 2493 key->dtak_hashval = hashval;
2489 2494 key->dtak_size = size;
2490 2495 key->dtak_action = action;
2491 2496 key->dtak_next = agb->dtagb_hash[ndx];
2492 2497 agb->dtagb_hash[ndx] = key;
2493 2498
2494 2499 /*
2495 2500 * Finally, apply the aggregator.
2496 2501 */
2497 2502 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2498 2503 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2499 2504 }
2500 2505
2501 2506 /*
2502 2507 * Given consumer state, this routine finds a speculation in the INACTIVE
2503 2508 * state and transitions it into the ACTIVE state. If there is no speculation
2504 2509 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2505 2510 * incremented -- it is up to the caller to take appropriate action.
2506 2511 */
2507 2512 static int
2508 2513 dtrace_speculation(dtrace_state_t *state)
2509 2514 {
2510 2515 int i = 0;
2511 2516 dtrace_speculation_state_t current;
2512 2517 uint32_t *stat = &state->dts_speculations_unavail, count;
2513 2518
2514 2519 while (i < state->dts_nspeculations) {
2515 2520 dtrace_speculation_t *spec = &state->dts_speculations[i];
2516 2521
2517 2522 current = spec->dtsp_state;
2518 2523
2519 2524 if (current != DTRACESPEC_INACTIVE) {
2520 2525 if (current == DTRACESPEC_COMMITTINGMANY ||
2521 2526 current == DTRACESPEC_COMMITTING ||
2522 2527 current == DTRACESPEC_DISCARDING)
2523 2528 stat = &state->dts_speculations_busy;
2524 2529 i++;
2525 2530 continue;
2526 2531 }
2527 2532
2528 2533 if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2529 2534 current, DTRACESPEC_ACTIVE) == current)
2530 2535 return (i + 1);
2531 2536 }
2532 2537
2533 2538 /*
2534 2539 * We couldn't find a speculation. If we found as much as a single
2535 2540 * busy speculation buffer, we'll attribute this failure as "busy"
2536 2541 * instead of "unavail".
2537 2542 */
2538 2543 do {
2539 2544 count = *stat;
2540 2545 } while (dtrace_cas32(stat, count, count + 1) != count);
2541 2546
2542 2547 return (0);
2543 2548 }
2544 2549
2545 2550 /*
2546 2551 * This routine commits an active speculation. If the specified speculation
2547 2552 * is not in a valid state to perform a commit(), this routine will silently do
2548 2553 * nothing. The state of the specified speculation is transitioned according
2549 2554 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2550 2555 */
2551 2556 static void
2552 2557 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2553 2558 dtrace_specid_t which)
2554 2559 {
2555 2560 dtrace_speculation_t *spec;
2556 2561 dtrace_buffer_t *src, *dest;
2557 2562 uintptr_t daddr, saddr, dlimit, slimit;
2558 2563 dtrace_speculation_state_t current, new;
2559 2564 intptr_t offs;
2560 2565 uint64_t timestamp;
2561 2566
2562 2567 if (which == 0)
2563 2568 return;
2564 2569
2565 2570 if (which > state->dts_nspeculations) {
2566 2571 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2567 2572 return;
2568 2573 }
2569 2574
2570 2575 spec = &state->dts_speculations[which - 1];
2571 2576 src = &spec->dtsp_buffer[cpu];
2572 2577 dest = &state->dts_buffer[cpu];
2573 2578
2574 2579 do {
2575 2580 current = spec->dtsp_state;
2576 2581
2577 2582 if (current == DTRACESPEC_COMMITTINGMANY)
2578 2583 break;
2579 2584
2580 2585 switch (current) {
2581 2586 case DTRACESPEC_INACTIVE:
2582 2587 case DTRACESPEC_DISCARDING:
2583 2588 return;
2584 2589
2585 2590 case DTRACESPEC_COMMITTING:
2586 2591 /*
2587 2592 * This is only possible if we are (a) commit()'ing
2588 2593 * without having done a prior speculate() on this CPU
2589 2594 * and (b) racing with another commit() on a different
2590 2595 * CPU. There's nothing to do -- we just assert that
2591 2596 * our offset is 0.
2592 2597 */
2593 2598 ASSERT(src->dtb_offset == 0);
2594 2599 return;
2595 2600
2596 2601 case DTRACESPEC_ACTIVE:
2597 2602 new = DTRACESPEC_COMMITTING;
2598 2603 break;
2599 2604
2600 2605 case DTRACESPEC_ACTIVEONE:
2601 2606 /*
2602 2607 * This speculation is active on one CPU. If our
2603 2608 * buffer offset is non-zero, we know that the one CPU
2604 2609 * must be us. Otherwise, we are committing on a
2605 2610 * different CPU from the speculate(), and we must
2606 2611 * rely on being asynchronously cleaned.
2607 2612 */
2608 2613 if (src->dtb_offset != 0) {
2609 2614 new = DTRACESPEC_COMMITTING;
2610 2615 break;
2611 2616 }
2612 2617 /*FALLTHROUGH*/
2613 2618
2614 2619 case DTRACESPEC_ACTIVEMANY:
2615 2620 new = DTRACESPEC_COMMITTINGMANY;
2616 2621 break;
2617 2622
2618 2623 default:
2619 2624 ASSERT(0);
2620 2625 }
2621 2626 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2622 2627 current, new) != current);
2623 2628
2624 2629 /*
2625 2630 * We have set the state to indicate that we are committing this
2626 2631 * speculation. Now reserve the necessary space in the destination
2627 2632 * buffer.
2628 2633 */
2629 2634 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2630 2635 sizeof (uint64_t), state, NULL)) < 0) {
2631 2636 dtrace_buffer_drop(dest);
2632 2637 goto out;
2633 2638 }
2634 2639
2635 2640 /*
2636 2641 * We have sufficient space to copy the speculative buffer into the
2637 2642 * primary buffer. First, modify the speculative buffer, filling
2638 2643 * in the timestamp of all entries with the current time. The data
2639 2644 * must have the commit() time rather than the time it was traced,
2640 2645 * so that all entries in the primary buffer are in timestamp order.
2641 2646 */
2642 2647 timestamp = dtrace_gethrtime();
2643 2648 saddr = (uintptr_t)src->dtb_tomax;
2644 2649 slimit = saddr + src->dtb_offset;
2645 2650 while (saddr < slimit) {
2646 2651 size_t size;
2647 2652 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
2648 2653
2649 2654 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2650 2655 saddr += sizeof (dtrace_epid_t);
2651 2656 continue;
2652 2657 }
2653 2658 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs);
2654 2659 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
2655 2660
2656 2661 ASSERT3U(saddr + size, <=, slimit);
2657 2662 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t));
2658 2663 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX);
2659 2664
2660 2665 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
2661 2666
2662 2667 saddr += size;
2663 2668 }
2664 2669
2665 2670 /*
2666 2671 * Copy the buffer across. (Note that this is a
2667 2672 * highly subobtimal bcopy(); in the unlikely event that this becomes
2668 2673 * a serious performance issue, a high-performance DTrace-specific
2669 2674 * bcopy() should obviously be invented.)
2670 2675 */
2671 2676 daddr = (uintptr_t)dest->dtb_tomax + offs;
2672 2677 dlimit = daddr + src->dtb_offset;
2673 2678 saddr = (uintptr_t)src->dtb_tomax;
2674 2679
2675 2680 /*
2676 2681 * First, the aligned portion.
2677 2682 */
2678 2683 while (dlimit - daddr >= sizeof (uint64_t)) {
2679 2684 *((uint64_t *)daddr) = *((uint64_t *)saddr);
2680 2685
2681 2686 daddr += sizeof (uint64_t);
2682 2687 saddr += sizeof (uint64_t);
2683 2688 }
2684 2689
2685 2690 /*
2686 2691 * Now any left-over bit...
2687 2692 */
2688 2693 while (dlimit - daddr)
2689 2694 *((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2690 2695
2691 2696 /*
2692 2697 * Finally, commit the reserved space in the destination buffer.
2693 2698 */
2694 2699 dest->dtb_offset = offs + src->dtb_offset;
2695 2700
2696 2701 out:
2697 2702 /*
2698 2703 * If we're lucky enough to be the only active CPU on this speculation
2699 2704 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2700 2705 */
2701 2706 if (current == DTRACESPEC_ACTIVE ||
2702 2707 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2703 2708 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2704 2709 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2705 2710
2706 2711 ASSERT(rval == DTRACESPEC_COMMITTING);
2707 2712 }
2708 2713
2709 2714 src->dtb_offset = 0;
2710 2715 src->dtb_xamot_drops += src->dtb_drops;
2711 2716 src->dtb_drops = 0;
2712 2717 }
2713 2718
2714 2719 /*
2715 2720 * This routine discards an active speculation. If the specified speculation
2716 2721 * is not in a valid state to perform a discard(), this routine will silently
2717 2722 * do nothing. The state of the specified speculation is transitioned
2718 2723 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2719 2724 */
2720 2725 static void
2721 2726 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
2722 2727 dtrace_specid_t which)
2723 2728 {
2724 2729 dtrace_speculation_t *spec;
2725 2730 dtrace_speculation_state_t current, new;
2726 2731 dtrace_buffer_t *buf;
2727 2732
2728 2733 if (which == 0)
2729 2734 return;
2730 2735
2731 2736 if (which > state->dts_nspeculations) {
2732 2737 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2733 2738 return;
2734 2739 }
2735 2740
2736 2741 spec = &state->dts_speculations[which - 1];
2737 2742 buf = &spec->dtsp_buffer[cpu];
2738 2743
2739 2744 do {
2740 2745 current = spec->dtsp_state;
2741 2746
2742 2747 switch (current) {
2743 2748 case DTRACESPEC_INACTIVE:
2744 2749 case DTRACESPEC_COMMITTINGMANY:
2745 2750 case DTRACESPEC_COMMITTING:
2746 2751 case DTRACESPEC_DISCARDING:
2747 2752 return;
2748 2753
2749 2754 case DTRACESPEC_ACTIVE:
2750 2755 case DTRACESPEC_ACTIVEMANY:
2751 2756 new = DTRACESPEC_DISCARDING;
2752 2757 break;
2753 2758
2754 2759 case DTRACESPEC_ACTIVEONE:
2755 2760 if (buf->dtb_offset != 0) {
2756 2761 new = DTRACESPEC_INACTIVE;
2757 2762 } else {
2758 2763 new = DTRACESPEC_DISCARDING;
2759 2764 }
2760 2765 break;
2761 2766
2762 2767 default:
2763 2768 ASSERT(0);
2764 2769 }
2765 2770 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2766 2771 current, new) != current);
2767 2772
2768 2773 buf->dtb_offset = 0;
2769 2774 buf->dtb_drops = 0;
2770 2775 }
2771 2776
2772 2777 /*
2773 2778 * Note: not called from probe context. This function is called
2774 2779 * asynchronously from cross call context to clean any speculations that are
2775 2780 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
2776 2781 * transitioned back to the INACTIVE state until all CPUs have cleaned the
2777 2782 * speculation.
2778 2783 */
2779 2784 static void
2780 2785 dtrace_speculation_clean_here(dtrace_state_t *state)
2781 2786 {
2782 2787 dtrace_icookie_t cookie;
2783 2788 processorid_t cpu = CPU->cpu_id;
2784 2789 dtrace_buffer_t *dest = &state->dts_buffer[cpu];
2785 2790 dtrace_specid_t i;
2786 2791
2787 2792 cookie = dtrace_interrupt_disable();
2788 2793
2789 2794 if (dest->dtb_tomax == NULL) {
2790 2795 dtrace_interrupt_enable(cookie);
2791 2796 return;
2792 2797 }
2793 2798
2794 2799 for (i = 0; i < state->dts_nspeculations; i++) {
2795 2800 dtrace_speculation_t *spec = &state->dts_speculations[i];
2796 2801 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
2797 2802
2798 2803 if (src->dtb_tomax == NULL)
2799 2804 continue;
2800 2805
2801 2806 if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
2802 2807 src->dtb_offset = 0;
2803 2808 continue;
2804 2809 }
2805 2810
2806 2811 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2807 2812 continue;
2808 2813
2809 2814 if (src->dtb_offset == 0)
2810 2815 continue;
2811 2816
2812 2817 dtrace_speculation_commit(state, cpu, i + 1);
2813 2818 }
2814 2819
2815 2820 dtrace_interrupt_enable(cookie);
2816 2821 }
2817 2822
2818 2823 /*
2819 2824 * Note: not called from probe context. This function is called
2820 2825 * asynchronously (and at a regular interval) to clean any speculations that
2821 2826 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
2822 2827 * is work to be done, it cross calls all CPUs to perform that work;
2823 2828 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2824 2829 * INACTIVE state until they have been cleaned by all CPUs.
2825 2830 */
2826 2831 static void
2827 2832 dtrace_speculation_clean(dtrace_state_t *state)
2828 2833 {
2829 2834 int work = 0, rv;
2830 2835 dtrace_specid_t i;
2831 2836
2832 2837 for (i = 0; i < state->dts_nspeculations; i++) {
2833 2838 dtrace_speculation_t *spec = &state->dts_speculations[i];
2834 2839
2835 2840 ASSERT(!spec->dtsp_cleaning);
2836 2841
2837 2842 if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
2838 2843 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2839 2844 continue;
2840 2845
2841 2846 work++;
2842 2847 spec->dtsp_cleaning = 1;
2843 2848 }
2844 2849
2845 2850 if (!work)
2846 2851 return;
2847 2852
2848 2853 dtrace_xcall(DTRACE_CPUALL,
2849 2854 (dtrace_xcall_t)dtrace_speculation_clean_here, state);
2850 2855
2851 2856 /*
2852 2857 * We now know that all CPUs have committed or discarded their
2853 2858 * speculation buffers, as appropriate. We can now set the state
2854 2859 * to inactive.
2855 2860 */
2856 2861 for (i = 0; i < state->dts_nspeculations; i++) {
2857 2862 dtrace_speculation_t *spec = &state->dts_speculations[i];
2858 2863 dtrace_speculation_state_t current, new;
2859 2864
2860 2865 if (!spec->dtsp_cleaning)
2861 2866 continue;
2862 2867
2863 2868 current = spec->dtsp_state;
2864 2869 ASSERT(current == DTRACESPEC_DISCARDING ||
2865 2870 current == DTRACESPEC_COMMITTINGMANY);
2866 2871
2867 2872 new = DTRACESPEC_INACTIVE;
2868 2873
2869 2874 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
2870 2875 ASSERT(rv == current);
2871 2876 spec->dtsp_cleaning = 0;
2872 2877 }
2873 2878 }
2874 2879
2875 2880 /*
2876 2881 * Called as part of a speculate() to get the speculative buffer associated
2877 2882 * with a given speculation. Returns NULL if the specified speculation is not
2878 2883 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
2879 2884 * the active CPU is not the specified CPU -- the speculation will be
2880 2885 * atomically transitioned into the ACTIVEMANY state.
2881 2886 */
2882 2887 static dtrace_buffer_t *
2883 2888 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
2884 2889 dtrace_specid_t which)
2885 2890 {
2886 2891 dtrace_speculation_t *spec;
2887 2892 dtrace_speculation_state_t current, new;
2888 2893 dtrace_buffer_t *buf;
2889 2894
2890 2895 if (which == 0)
2891 2896 return (NULL);
2892 2897
2893 2898 if (which > state->dts_nspeculations) {
2894 2899 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2895 2900 return (NULL);
2896 2901 }
2897 2902
2898 2903 spec = &state->dts_speculations[which - 1];
2899 2904 buf = &spec->dtsp_buffer[cpuid];
2900 2905
2901 2906 do {
2902 2907 current = spec->dtsp_state;
2903 2908
2904 2909 switch (current) {
2905 2910 case DTRACESPEC_INACTIVE:
2906 2911 case DTRACESPEC_COMMITTINGMANY:
2907 2912 case DTRACESPEC_DISCARDING:
2908 2913 return (NULL);
2909 2914
2910 2915 case DTRACESPEC_COMMITTING:
2911 2916 ASSERT(buf->dtb_offset == 0);
2912 2917 return (NULL);
2913 2918
2914 2919 case DTRACESPEC_ACTIVEONE:
2915 2920 /*
2916 2921 * This speculation is currently active on one CPU.
2917 2922 * Check the offset in the buffer; if it's non-zero,
2918 2923 * that CPU must be us (and we leave the state alone).
2919 2924 * If it's zero, assume that we're starting on a new
2920 2925 * CPU -- and change the state to indicate that the
2921 2926 * speculation is active on more than one CPU.
2922 2927 */
2923 2928 if (buf->dtb_offset != 0)
2924 2929 return (buf);
2925 2930
2926 2931 new = DTRACESPEC_ACTIVEMANY;
2927 2932 break;
2928 2933
2929 2934 case DTRACESPEC_ACTIVEMANY:
2930 2935 return (buf);
2931 2936
2932 2937 case DTRACESPEC_ACTIVE:
2933 2938 new = DTRACESPEC_ACTIVEONE;
2934 2939 break;
2935 2940
2936 2941 default:
2937 2942 ASSERT(0);
2938 2943 }
2939 2944 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2940 2945 current, new) != current);
2941 2946
2942 2947 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
2943 2948 return (buf);
2944 2949 }
2945 2950
2946 2951 /*
2947 2952 * Return a string. In the event that the user lacks the privilege to access
2948 2953 * arbitrary kernel memory, we copy the string out to scratch memory so that we
2949 2954 * don't fail access checking.
2950 2955 *
2951 2956 * dtrace_dif_variable() uses this routine as a helper for various
2952 2957 * builtin values such as 'execname' and 'probefunc.'
2953 2958 */
2954 2959 uintptr_t
2955 2960 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
2956 2961 dtrace_mstate_t *mstate)
2957 2962 {
2958 2963 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
2959 2964 uintptr_t ret;
2960 2965 size_t strsz;
2961 2966
2962 2967 /*
2963 2968 * The easy case: this probe is allowed to read all of memory, so
2964 2969 * we can just return this as a vanilla pointer.
2965 2970 */
2966 2971 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
2967 2972 return (addr);
2968 2973
2969 2974 /*
2970 2975 * This is the tougher case: we copy the string in question from
2971 2976 * kernel memory into scratch memory and return it that way: this
2972 2977 * ensures that we won't trip up when access checking tests the
2973 2978 * BYREF return value.
2974 2979 */
2975 2980 strsz = dtrace_strlen((char *)addr, size) + 1;
2976 2981
2977 2982 if (mstate->dtms_scratch_ptr + strsz >
2978 2983 mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
2979 2984 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
2980 2985 return (NULL);
2981 2986 }
2982 2987
2983 2988 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
2984 2989 strsz);
2985 2990 ret = mstate->dtms_scratch_ptr;
2986 2991 mstate->dtms_scratch_ptr += strsz;
2987 2992 return (ret);
2988 2993 }
2989 2994
2990 2995 /*
2991 2996 * This function implements the DIF emulator's variable lookups. The emulator
2992 2997 * passes a reserved variable identifier and optional built-in array index.
2993 2998 */
2994 2999 static uint64_t
2995 3000 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
2996 3001 uint64_t ndx)
2997 3002 {
2998 3003 /*
2999 3004 * If we're accessing one of the uncached arguments, we'll turn this
3000 3005 * into a reference in the args array.
3001 3006 */
3002 3007 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
3003 3008 ndx = v - DIF_VAR_ARG0;
3004 3009 v = DIF_VAR_ARGS;
3005 3010 }
3006 3011
3007 3012 switch (v) {
3008 3013 case DIF_VAR_ARGS:
3009 3014 if (!(mstate->dtms_access & DTRACE_ACCESS_ARGS)) {
3010 3015 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |=
3011 3016 CPU_DTRACE_KPRIV;
3012 3017 return (0);
3013 3018 }
3014 3019
3015 3020 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
3016 3021 if (ndx >= sizeof (mstate->dtms_arg) /
3017 3022 sizeof (mstate->dtms_arg[0])) {
3018 3023 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3019 3024 dtrace_provider_t *pv;
3020 3025 uint64_t val;
3021 3026
3022 3027 pv = mstate->dtms_probe->dtpr_provider;
3023 3028 if (pv->dtpv_pops.dtps_getargval != NULL)
3024 3029 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
3025 3030 mstate->dtms_probe->dtpr_id,
3026 3031 mstate->dtms_probe->dtpr_arg, ndx, aframes);
3027 3032 else
3028 3033 val = dtrace_getarg(ndx, aframes);
3029 3034
3030 3035 /*
3031 3036 * This is regrettably required to keep the compiler
3032 3037 * from tail-optimizing the call to dtrace_getarg().
3033 3038 * The condition always evaluates to true, but the
3034 3039 * compiler has no way of figuring that out a priori.
3035 3040 * (None of this would be necessary if the compiler
3036 3041 * could be relied upon to _always_ tail-optimize
3037 3042 * the call to dtrace_getarg() -- but it can't.)
3038 3043 */
3039 3044 if (mstate->dtms_probe != NULL)
3040 3045 return (val);
3041 3046
3042 3047 ASSERT(0);
3043 3048 }
3044 3049
3045 3050 return (mstate->dtms_arg[ndx]);
3046 3051
3047 3052 case DIF_VAR_UREGS: {
3048 3053 klwp_t *lwp;
3049 3054
3050 3055 if (!dtrace_priv_proc(state, mstate))
3051 3056 return (0);
3052 3057
3053 3058 if ((lwp = curthread->t_lwp) == NULL) {
3054 3059 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3055 3060 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL;
3056 3061 return (0);
3057 3062 }
3058 3063
3059 3064 return (dtrace_getreg(lwp->lwp_regs, ndx));
3060 3065 }
3061 3066
3062 3067 case DIF_VAR_VMREGS: {
3063 3068 uint64_t rval;
3064 3069
3065 3070 if (!dtrace_priv_kernel(state))
3066 3071 return (0);
3067 3072
3068 3073 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3069 3074
3070 3075 rval = dtrace_getvmreg(ndx,
3071 3076 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags);
3072 3077
3073 3078 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3074 3079
3075 3080 return (rval);
3076 3081 }
3077 3082
3078 3083 case DIF_VAR_CURTHREAD:
3079 3084 if (!dtrace_priv_proc(state, mstate))
3080 3085 return (0);
3081 3086 return ((uint64_t)(uintptr_t)curthread);
3082 3087
3083 3088 case DIF_VAR_TIMESTAMP:
3084 3089 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
3085 3090 mstate->dtms_timestamp = dtrace_gethrtime();
3086 3091 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
3087 3092 }
3088 3093 return (mstate->dtms_timestamp);
3089 3094
3090 3095 case DIF_VAR_VTIMESTAMP:
3091 3096 ASSERT(dtrace_vtime_references != 0);
3092 3097 return (curthread->t_dtrace_vtime);
3093 3098
3094 3099 case DIF_VAR_WALLTIMESTAMP:
3095 3100 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
3096 3101 mstate->dtms_walltimestamp = dtrace_gethrestime();
3097 3102 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
3098 3103 }
3099 3104 return (mstate->dtms_walltimestamp);
3100 3105
3101 3106 case DIF_VAR_IPL:
3102 3107 if (!dtrace_priv_kernel(state))
3103 3108 return (0);
3104 3109 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
3105 3110 mstate->dtms_ipl = dtrace_getipl();
3106 3111 mstate->dtms_present |= DTRACE_MSTATE_IPL;
3107 3112 }
3108 3113 return (mstate->dtms_ipl);
3109 3114
3110 3115 case DIF_VAR_EPID:
3111 3116 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
3112 3117 return (mstate->dtms_epid);
3113 3118
3114 3119 case DIF_VAR_ID:
3115 3120 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3116 3121 return (mstate->dtms_probe->dtpr_id);
3117 3122
3118 3123 case DIF_VAR_STACKDEPTH:
3119 3124 if (!dtrace_priv_kernel(state))
3120 3125 return (0);
3121 3126 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
3122 3127 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3123 3128
3124 3129 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
3125 3130 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
3126 3131 }
3127 3132 return (mstate->dtms_stackdepth);
3128 3133
3129 3134 case DIF_VAR_USTACKDEPTH:
3130 3135 if (!dtrace_priv_proc(state, mstate))
3131 3136 return (0);
3132 3137 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
3133 3138 /*
3134 3139 * See comment in DIF_VAR_PID.
3135 3140 */
3136 3141 if (DTRACE_ANCHORED(mstate->dtms_probe) &&
3137 3142 CPU_ON_INTR(CPU)) {
3138 3143 mstate->dtms_ustackdepth = 0;
3139 3144 } else {
3140 3145 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3141 3146 mstate->dtms_ustackdepth =
3142 3147 dtrace_getustackdepth();
3143 3148 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3144 3149 }
3145 3150 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
3146 3151 }
3147 3152 return (mstate->dtms_ustackdepth);
3148 3153
3149 3154 case DIF_VAR_CALLER:
3150 3155 if (!dtrace_priv_kernel(state))
3151 3156 return (0);
3152 3157 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
3153 3158 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3154 3159
3155 3160 if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
3156 3161 /*
3157 3162 * If this is an unanchored probe, we are
3158 3163 * required to go through the slow path:
3159 3164 * dtrace_caller() only guarantees correct
3160 3165 * results for anchored probes.
3161 3166 */
3162 3167 pc_t caller[2];
3163 3168
3164 3169 dtrace_getpcstack(caller, 2, aframes,
3165 3170 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
3166 3171 mstate->dtms_caller = caller[1];
3167 3172 } else if ((mstate->dtms_caller =
3168 3173 dtrace_caller(aframes)) == -1) {
3169 3174 /*
3170 3175 * We have failed to do this the quick way;
3171 3176 * we must resort to the slower approach of
3172 3177 * calling dtrace_getpcstack().
3173 3178 */
3174 3179 pc_t caller;
3175 3180
3176 3181 dtrace_getpcstack(&caller, 1, aframes, NULL);
3177 3182 mstate->dtms_caller = caller;
3178 3183 }
3179 3184
3180 3185 mstate->dtms_present |= DTRACE_MSTATE_CALLER;
3181 3186 }
3182 3187 return (mstate->dtms_caller);
3183 3188
3184 3189 case DIF_VAR_UCALLER:
3185 3190 if (!dtrace_priv_proc(state, mstate))
3186 3191 return (0);
3187 3192
3188 3193 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
3189 3194 uint64_t ustack[3];
3190 3195
3191 3196 /*
3192 3197 * dtrace_getupcstack() fills in the first uint64_t
3193 3198 * with the current PID. The second uint64_t will
3194 3199 * be the program counter at user-level. The third
3195 3200 * uint64_t will contain the caller, which is what
3196 3201 * we're after.
3197 3202 */
3198 3203 ustack[2] = NULL;
3199 3204 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3200 3205 dtrace_getupcstack(ustack, 3);
3201 3206 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3202 3207 mstate->dtms_ucaller = ustack[2];
3203 3208 mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3204 3209 }
3205 3210
3206 3211 return (mstate->dtms_ucaller);
3207 3212
3208 3213 case DIF_VAR_PROBEPROV:
3209 3214 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3210 3215 return (dtrace_dif_varstr(
3211 3216 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3212 3217 state, mstate));
3213 3218
3214 3219 case DIF_VAR_PROBEMOD:
3215 3220 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3216 3221 return (dtrace_dif_varstr(
3217 3222 (uintptr_t)mstate->dtms_probe->dtpr_mod,
3218 3223 state, mstate));
3219 3224
3220 3225 case DIF_VAR_PROBEFUNC:
3221 3226 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3222 3227 return (dtrace_dif_varstr(
3223 3228 (uintptr_t)mstate->dtms_probe->dtpr_func,
3224 3229 state, mstate));
3225 3230
3226 3231 case DIF_VAR_PROBENAME:
3227 3232 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3228 3233 return (dtrace_dif_varstr(
3229 3234 (uintptr_t)mstate->dtms_probe->dtpr_name,
3230 3235 state, mstate));
3231 3236
3232 3237 case DIF_VAR_PID:
3233 3238 if (!dtrace_priv_proc(state, mstate))
3234 3239 return (0);
3235 3240
3236 3241 /*
3237 3242 * Note that we are assuming that an unanchored probe is
3238 3243 * always due to a high-level interrupt. (And we're assuming
3239 3244 * that there is only a single high level interrupt.)
3240 3245 */
3241 3246 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3242 3247 return (pid0.pid_id);
3243 3248
3244 3249 /*
3245 3250 * It is always safe to dereference one's own t_procp pointer:
3246 3251 * it always points to a valid, allocated proc structure.
3247 3252 * Further, it is always safe to dereference the p_pidp member
3248 3253 * of one's own proc structure. (These are truisms becuase
3249 3254 * threads and processes don't clean up their own state --
3250 3255 * they leave that task to whomever reaps them.)
3251 3256 */
3252 3257 return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
3253 3258
3254 3259 case DIF_VAR_PPID:
3255 3260 if (!dtrace_priv_proc(state, mstate))
3256 3261 return (0);
3257 3262
3258 3263 /*
3259 3264 * See comment in DIF_VAR_PID.
3260 3265 */
3261 3266 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3262 3267 return (pid0.pid_id);
3263 3268
3264 3269 /*
3265 3270 * It is always safe to dereference one's own t_procp pointer:
3266 3271 * it always points to a valid, allocated proc structure.
3267 3272 * (This is true because threads don't clean up their own
3268 3273 * state -- they leave that task to whomever reaps them.)
3269 3274 */
3270 3275 return ((uint64_t)curthread->t_procp->p_ppid);
3271 3276
3272 3277 case DIF_VAR_TID:
3273 3278 /*
3274 3279 * See comment in DIF_VAR_PID.
3275 3280 */
3276 3281 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3277 3282 return (0);
3278 3283
3279 3284 return ((uint64_t)curthread->t_tid);
3280 3285
3281 3286 case DIF_VAR_EXECNAME:
3282 3287 if (!dtrace_priv_proc(state, mstate))
3283 3288 return (0);
3284 3289
3285 3290 /*
3286 3291 * See comment in DIF_VAR_PID.
3287 3292 */
3288 3293 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3289 3294 return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
3290 3295
3291 3296 /*
3292 3297 * It is always safe to dereference one's own t_procp pointer:
3293 3298 * it always points to a valid, allocated proc structure.
3294 3299 * (This is true because threads don't clean up their own
3295 3300 * state -- they leave that task to whomever reaps them.)
3296 3301 */
3297 3302 return (dtrace_dif_varstr(
3298 3303 (uintptr_t)curthread->t_procp->p_user.u_comm,
3299 3304 state, mstate));
3300 3305
3301 3306 case DIF_VAR_ZONENAME:
3302 3307 if (!dtrace_priv_proc(state, mstate))
3303 3308 return (0);
3304 3309
3305 3310 /*
3306 3311 * See comment in DIF_VAR_PID.
3307 3312 */
3308 3313 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3309 3314 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
3310 3315
3311 3316 /*
3312 3317 * It is always safe to dereference one's own t_procp pointer:
3313 3318 * it always points to a valid, allocated proc structure.
3314 3319 * (This is true because threads don't clean up their own
3315 3320 * state -- they leave that task to whomever reaps them.)
3316 3321 */
3317 3322 return (dtrace_dif_varstr(
3318 3323 (uintptr_t)curthread->t_procp->p_zone->zone_name,
3319 3324 state, mstate));
3320 3325
3321 3326 case DIF_VAR_UID:
3322 3327 if (!dtrace_priv_proc(state, mstate))
3323 3328 return (0);
3324 3329
3325 3330 /*
3326 3331 * See comment in DIF_VAR_PID.
3327 3332 */
3328 3333 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3329 3334 return ((uint64_t)p0.p_cred->cr_uid);
3330 3335
3331 3336 /*
3332 3337 * It is always safe to dereference one's own t_procp pointer:
3333 3338 * it always points to a valid, allocated proc structure.
3334 3339 * (This is true because threads don't clean up their own
3335 3340 * state -- they leave that task to whomever reaps them.)
3336 3341 *
3337 3342 * Additionally, it is safe to dereference one's own process
3338 3343 * credential, since this is never NULL after process birth.
3339 3344 */
3340 3345 return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
3341 3346
3342 3347 case DIF_VAR_GID:
3343 3348 if (!dtrace_priv_proc(state, mstate))
3344 3349 return (0);
3345 3350
3346 3351 /*
3347 3352 * See comment in DIF_VAR_PID.
3348 3353 */
3349 3354 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3350 3355 return ((uint64_t)p0.p_cred->cr_gid);
3351 3356
3352 3357 /*
3353 3358 * It is always safe to dereference one's own t_procp pointer:
3354 3359 * it always points to a valid, allocated proc structure.
3355 3360 * (This is true because threads don't clean up their own
3356 3361 * state -- they leave that task to whomever reaps them.)
3357 3362 *
3358 3363 * Additionally, it is safe to dereference one's own process
3359 3364 * credential, since this is never NULL after process birth.
3360 3365 */
3361 3366 return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
3362 3367
3363 3368 case DIF_VAR_ERRNO: {
3364 3369 klwp_t *lwp;
3365 3370 if (!dtrace_priv_proc(state, mstate))
3366 3371 return (0);
3367 3372
3368 3373 /*
3369 3374 * See comment in DIF_VAR_PID.
3370 3375 */
3371 3376 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3372 3377 return (0);
3373 3378
3374 3379 /*
3375 3380 * It is always safe to dereference one's own t_lwp pointer in
3376 3381 * the event that this pointer is non-NULL. (This is true
3377 3382 * because threads and lwps don't clean up their own state --
3378 3383 * they leave that task to whomever reaps them.)
3379 3384 */
3380 3385 if ((lwp = curthread->t_lwp) == NULL)
3381 3386 return (0);
3382 3387
3383 3388 return ((uint64_t)lwp->lwp_errno);
3384 3389 }
3385 3390 default:
3386 3391 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3387 3392 return (0);
3388 3393 }
3389 3394 }
3390 3395
3391 3396
3392 3397 typedef enum dtrace_json_state {
3393 3398 DTRACE_JSON_REST = 1,
3394 3399 DTRACE_JSON_OBJECT,
3395 3400 DTRACE_JSON_STRING,
3396 3401 DTRACE_JSON_STRING_ESCAPE,
3397 3402 DTRACE_JSON_STRING_ESCAPE_UNICODE,
3398 3403 DTRACE_JSON_COLON,
3399 3404 DTRACE_JSON_COMMA,
3400 3405 DTRACE_JSON_VALUE,
3401 3406 DTRACE_JSON_IDENTIFIER,
3402 3407 DTRACE_JSON_NUMBER,
3403 3408 DTRACE_JSON_NUMBER_FRAC,
3404 3409 DTRACE_JSON_NUMBER_EXP,
3405 3410 DTRACE_JSON_COLLECT_OBJECT
3406 3411 } dtrace_json_state_t;
3407 3412
3408 3413 /*
3409 3414 * This function possesses just enough knowledge about JSON to extract a single
3410 3415 * value from a JSON string and store it in the scratch buffer. It is able
3411 3416 * to extract nested object values, and members of arrays by index.
3412 3417 *
3413 3418 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
3414 3419 * be looked up as we descend into the object tree. e.g.
3415 3420 *
3416 3421 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
3417 3422 * with nelems = 5.
3418 3423 *
3419 3424 * The run time of this function must be bounded above by strsize to limit the
3420 3425 * amount of work done in probe context. As such, it is implemented as a
3421 3426 * simple state machine, reading one character at a time using safe loads
3422 3427 * until we find the requested element, hit a parsing error or run off the
3423 3428 * end of the object or string.
3424 3429 *
3425 3430 * As there is no way for a subroutine to return an error without interrupting
3426 3431 * clause execution, we simply return NULL in the event of a missing key or any
3427 3432 * other error condition. Each NULL return in this function is commented with
3428 3433 * the error condition it represents -- parsing or otherwise.
3429 3434 *
3430 3435 * The set of states for the state machine closely matches the JSON
3431 3436 * specification (http://json.org/). Briefly:
3432 3437 *
3433 3438 * DTRACE_JSON_REST:
3434 3439 * Skip whitespace until we find either a top-level Object, moving
3435 3440 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
3436 3441 *
3437 3442 * DTRACE_JSON_OBJECT:
3438 3443 * Locate the next key String in an Object. Sets a flag to denote
3439 3444 * the next String as a key string and moves to DTRACE_JSON_STRING.
3440 3445 *
3441 3446 * DTRACE_JSON_COLON:
3442 3447 * Skip whitespace until we find the colon that separates key Strings
3443 3448 * from their values. Once found, move to DTRACE_JSON_VALUE.
3444 3449 *
3445 3450 * DTRACE_JSON_VALUE:
3446 3451 * Detects the type of the next value (String, Number, Identifier, Object
3447 3452 * or Array) and routes to the states that process that type. Here we also
3448 3453 * deal with the element selector list if we are requested to traverse down
3449 3454 * into the object tree.
3450 3455 *
3451 3456 * DTRACE_JSON_COMMA:
3452 3457 * Skip whitespace until we find the comma that separates key-value pairs
3453 3458 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
3454 3459 * (similarly DTRACE_JSON_VALUE). All following literal value processing
3455 3460 * states return to this state at the end of their value, unless otherwise
3456 3461 * noted.
3457 3462 *
3458 3463 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
3459 3464 * Processes a Number literal from the JSON, including any exponent
3460 3465 * component that may be present. Numbers are returned as strings, which
3461 3466 * may be passed to strtoll() if an integer is required.
3462 3467 *
3463 3468 * DTRACE_JSON_IDENTIFIER:
3464 3469 * Processes a "true", "false" or "null" literal in the JSON.
3465 3470 *
3466 3471 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
3467 3472 * DTRACE_JSON_STRING_ESCAPE_UNICODE:
3468 3473 * Processes a String literal from the JSON, whether the String denotes
3469 3474 * a key, a value or part of a larger Object. Handles all escape sequences
3470 3475 * present in the specification, including four-digit unicode characters,
3471 3476 * but merely includes the escape sequence without converting it to the
3472 3477 * actual escaped character. If the String is flagged as a key, we
3473 3478 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
3474 3479 *
3475 3480 * DTRACE_JSON_COLLECT_OBJECT:
3476 3481 * This state collects an entire Object (or Array), correctly handling
3477 3482 * embedded strings. If the full element selector list matches this nested
3478 3483 * object, we return the Object in full as a string. If not, we use this
3479 3484 * state to skip to the next value at this level and continue processing.
3480 3485 *
3481 3486 * NOTE: This function uses various macros from strtolctype.h to manipulate
3482 3487 * digit values, etc -- these have all been checked to ensure they make
3483 3488 * no additional function calls.
3484 3489 */
3485 3490 static char *
3486 3491 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems,
3487 3492 char *dest)
3488 3493 {
3489 3494 dtrace_json_state_t state = DTRACE_JSON_REST;
3490 3495 int64_t array_elem = INT64_MIN;
3491 3496 int64_t array_pos = 0;
3492 3497 uint8_t escape_unicount = 0;
3493 3498 boolean_t string_is_key = B_FALSE;
3494 3499 boolean_t collect_object = B_FALSE;
3495 3500 boolean_t found_key = B_FALSE;
3496 3501 boolean_t in_array = B_FALSE;
3497 3502 uint32_t braces = 0, brackets = 0;
3498 3503 char *elem = elemlist;
3499 3504 char *dd = dest;
3500 3505 uintptr_t cur;
3501 3506
3502 3507 for (cur = json; cur < json + size; cur++) {
3503 3508 char cc = dtrace_load8(cur);
3504 3509 if (cc == '\0')
3505 3510 return (NULL);
3506 3511
3507 3512 switch (state) {
3508 3513 case DTRACE_JSON_REST:
3509 3514 if (isspace(cc))
3510 3515 break;
3511 3516
3512 3517 if (cc == '{') {
3513 3518 state = DTRACE_JSON_OBJECT;
3514 3519 break;
3515 3520 }
3516 3521
3517 3522 if (cc == '[') {
3518 3523 in_array = B_TRUE;
3519 3524 array_pos = 0;
3520 3525 array_elem = dtrace_strtoll(elem, 10, size);
3521 3526 found_key = array_elem == 0 ? B_TRUE : B_FALSE;
3522 3527 state = DTRACE_JSON_VALUE;
3523 3528 break;
3524 3529 }
3525 3530
3526 3531 /*
3527 3532 * ERROR: expected to find a top-level object or array.
3528 3533 */
3529 3534 return (NULL);
3530 3535 case DTRACE_JSON_OBJECT:
3531 3536 if (isspace(cc))
3532 3537 break;
3533 3538
3534 3539 if (cc == '"') {
3535 3540 state = DTRACE_JSON_STRING;
3536 3541 string_is_key = B_TRUE;
3537 3542 break;
3538 3543 }
3539 3544
3540 3545 /*
3541 3546 * ERROR: either the object did not start with a key
3542 3547 * string, or we've run off the end of the object
3543 3548 * without finding the requested key.
3544 3549 */
3545 3550 return (NULL);
3546 3551 case DTRACE_JSON_STRING:
3547 3552 if (cc == '\\') {
3548 3553 *dd++ = '\\';
3549 3554 state = DTRACE_JSON_STRING_ESCAPE;
3550 3555 break;
3551 3556 }
3552 3557
3553 3558 if (cc == '"') {
3554 3559 if (collect_object) {
3555 3560 /*
3556 3561 * We don't reset the dest here, as
3557 3562 * the string is part of a larger
3558 3563 * object being collected.
3559 3564 */
3560 3565 *dd++ = cc;
3561 3566 collect_object = B_FALSE;
3562 3567 state = DTRACE_JSON_COLLECT_OBJECT;
3563 3568 break;
3564 3569 }
3565 3570 *dd = '\0';
3566 3571 dd = dest; /* reset string buffer */
3567 3572 if (string_is_key) {
3568 3573 if (dtrace_strncmp(dest, elem,
3569 3574 size) == 0)
3570 3575 found_key = B_TRUE;
3571 3576 } else if (found_key) {
3572 3577 if (nelems > 1) {
3573 3578 /*
3574 3579 * We expected an object, not
3575 3580 * this string.
3576 3581 */
3577 3582 return (NULL);
3578 3583 }
3579 3584 return (dest);
3580 3585 }
3581 3586 state = string_is_key ? DTRACE_JSON_COLON :
3582 3587 DTRACE_JSON_COMMA;
3583 3588 string_is_key = B_FALSE;
3584 3589 break;
3585 3590 }
3586 3591
3587 3592 *dd++ = cc;
3588 3593 break;
3589 3594 case DTRACE_JSON_STRING_ESCAPE:
3590 3595 *dd++ = cc;
3591 3596 if (cc == 'u') {
3592 3597 escape_unicount = 0;
3593 3598 state = DTRACE_JSON_STRING_ESCAPE_UNICODE;
3594 3599 } else {
3595 3600 state = DTRACE_JSON_STRING;
3596 3601 }
3597 3602 break;
3598 3603 case DTRACE_JSON_STRING_ESCAPE_UNICODE:
3599 3604 if (!isxdigit(cc)) {
3600 3605 /*
3601 3606 * ERROR: invalid unicode escape, expected
3602 3607 * four valid hexidecimal digits.
3603 3608 */
3604 3609 return (NULL);
3605 3610 }
3606 3611
3607 3612 *dd++ = cc;
3608 3613 if (++escape_unicount == 4)
3609 3614 state = DTRACE_JSON_STRING;
3610 3615 break;
3611 3616 case DTRACE_JSON_COLON:
3612 3617 if (isspace(cc))
3613 3618 break;
3614 3619
3615 3620 if (cc == ':') {
3616 3621 state = DTRACE_JSON_VALUE;
3617 3622 break;
3618 3623 }
3619 3624
3620 3625 /*
3621 3626 * ERROR: expected a colon.
3622 3627 */
3623 3628 return (NULL);
3624 3629 case DTRACE_JSON_COMMA:
3625 3630 if (isspace(cc))
3626 3631 break;
3627 3632
3628 3633 if (cc == ',') {
3629 3634 if (in_array) {
3630 3635 state = DTRACE_JSON_VALUE;
3631 3636 if (++array_pos == array_elem)
3632 3637 found_key = B_TRUE;
3633 3638 } else {
3634 3639 state = DTRACE_JSON_OBJECT;
3635 3640 }
3636 3641 break;
3637 3642 }
3638 3643
3639 3644 /*
3640 3645 * ERROR: either we hit an unexpected character, or
3641 3646 * we reached the end of the object or array without
3642 3647 * finding the requested key.
3643 3648 */
3644 3649 return (NULL);
3645 3650 case DTRACE_JSON_IDENTIFIER:
3646 3651 if (islower(cc)) {
3647 3652 *dd++ = cc;
3648 3653 break;
3649 3654 }
3650 3655
3651 3656 *dd = '\0';
3652 3657 dd = dest; /* reset string buffer */
3653 3658
3654 3659 if (dtrace_strncmp(dest, "true", 5) == 0 ||
3655 3660 dtrace_strncmp(dest, "false", 6) == 0 ||
3656 3661 dtrace_strncmp(dest, "null", 5) == 0) {
3657 3662 if (found_key) {
3658 3663 if (nelems > 1) {
3659 3664 /*
3660 3665 * ERROR: We expected an object,
3661 3666 * not this identifier.
3662 3667 */
3663 3668 return (NULL);
3664 3669 }
3665 3670 return (dest);
3666 3671 } else {
3667 3672 cur--;
3668 3673 state = DTRACE_JSON_COMMA;
3669 3674 break;
3670 3675 }
3671 3676 }
3672 3677
3673 3678 /*
3674 3679 * ERROR: we did not recognise the identifier as one
3675 3680 * of those in the JSON specification.
3676 3681 */
3677 3682 return (NULL);
3678 3683 case DTRACE_JSON_NUMBER:
3679 3684 if (cc == '.') {
3680 3685 *dd++ = cc;
3681 3686 state = DTRACE_JSON_NUMBER_FRAC;
3682 3687 break;
3683 3688 }
3684 3689
3685 3690 if (cc == 'x' || cc == 'X') {
3686 3691 /*
3687 3692 * ERROR: specification explicitly excludes
3688 3693 * hexidecimal or octal numbers.
3689 3694 */
3690 3695 return (NULL);
3691 3696 }
3692 3697
3693 3698 /* FALLTHRU */
3694 3699 case DTRACE_JSON_NUMBER_FRAC:
3695 3700 if (cc == 'e' || cc == 'E') {
3696 3701 *dd++ = cc;
3697 3702 state = DTRACE_JSON_NUMBER_EXP;
3698 3703 break;
3699 3704 }
3700 3705
3701 3706 if (cc == '+' || cc == '-') {
3702 3707 /*
3703 3708 * ERROR: expect sign as part of exponent only.
3704 3709 */
3705 3710 return (NULL);
3706 3711 }
3707 3712 /* FALLTHRU */
3708 3713 case DTRACE_JSON_NUMBER_EXP:
3709 3714 if (isdigit(cc) || cc == '+' || cc == '-') {
3710 3715 *dd++ = cc;
3711 3716 break;
3712 3717 }
3713 3718
3714 3719 *dd = '\0';
3715 3720 dd = dest; /* reset string buffer */
3716 3721 if (found_key) {
3717 3722 if (nelems > 1) {
3718 3723 /*
3719 3724 * ERROR: We expected an object, not
3720 3725 * this number.
3721 3726 */
3722 3727 return (NULL);
3723 3728 }
3724 3729 return (dest);
3725 3730 }
3726 3731
3727 3732 cur--;
3728 3733 state = DTRACE_JSON_COMMA;
3729 3734 break;
3730 3735 case DTRACE_JSON_VALUE:
3731 3736 if (isspace(cc))
3732 3737 break;
3733 3738
3734 3739 if (cc == '{' || cc == '[') {
3735 3740 if (nelems > 1 && found_key) {
3736 3741 in_array = cc == '[' ? B_TRUE : B_FALSE;
3737 3742 /*
3738 3743 * If our element selector directs us
3739 3744 * to descend into this nested object,
3740 3745 * then move to the next selector
3741 3746 * element in the list and restart the
3742 3747 * state machine.
3743 3748 */
3744 3749 while (*elem != '\0')
3745 3750 elem++;
3746 3751 elem++; /* skip the inter-element NUL */
3747 3752 nelems--;
3748 3753 dd = dest;
3749 3754 if (in_array) {
3750 3755 state = DTRACE_JSON_VALUE;
3751 3756 array_pos = 0;
3752 3757 array_elem = dtrace_strtoll(
3753 3758 elem, 10, size);
3754 3759 found_key = array_elem == 0 ?
3755 3760 B_TRUE : B_FALSE;
3756 3761 } else {
3757 3762 found_key = B_FALSE;
3758 3763 state = DTRACE_JSON_OBJECT;
3759 3764 }
3760 3765 break;
3761 3766 }
3762 3767
3763 3768 /*
3764 3769 * Otherwise, we wish to either skip this
3765 3770 * nested object or return it in full.
3766 3771 */
3767 3772 if (cc == '[')
3768 3773 brackets = 1;
3769 3774 else
3770 3775 braces = 1;
3771 3776 *dd++ = cc;
3772 3777 state = DTRACE_JSON_COLLECT_OBJECT;
3773 3778 break;
3774 3779 }
3775 3780
3776 3781 if (cc == '"') {
3777 3782 state = DTRACE_JSON_STRING;
3778 3783 break;
3779 3784 }
3780 3785
3781 3786 if (islower(cc)) {
3782 3787 /*
3783 3788 * Here we deal with true, false and null.
3784 3789 */
3785 3790 *dd++ = cc;
3786 3791 state = DTRACE_JSON_IDENTIFIER;
3787 3792 break;
3788 3793 }
3789 3794
3790 3795 if (cc == '-' || isdigit(cc)) {
3791 3796 *dd++ = cc;
3792 3797 state = DTRACE_JSON_NUMBER;
3793 3798 break;
3794 3799 }
3795 3800
3796 3801 /*
3797 3802 * ERROR: unexpected character at start of value.
3798 3803 */
3799 3804 return (NULL);
3800 3805 case DTRACE_JSON_COLLECT_OBJECT:
3801 3806 if (cc == '\0')
3802 3807 /*
3803 3808 * ERROR: unexpected end of input.
3804 3809 */
3805 3810 return (NULL);
3806 3811
3807 3812 *dd++ = cc;
3808 3813 if (cc == '"') {
3809 3814 collect_object = B_TRUE;
3810 3815 state = DTRACE_JSON_STRING;
3811 3816 break;
3812 3817 }
3813 3818
3814 3819 if (cc == ']') {
3815 3820 if (brackets-- == 0) {
3816 3821 /*
3817 3822 * ERROR: unbalanced brackets.
3818 3823 */
3819 3824 return (NULL);
3820 3825 }
3821 3826 } else if (cc == '}') {
3822 3827 if (braces-- == 0) {
3823 3828 /*
3824 3829 * ERROR: unbalanced braces.
3825 3830 */
3826 3831 return (NULL);
3827 3832 }
3828 3833 } else if (cc == '{') {
3829 3834 braces++;
3830 3835 } else if (cc == '[') {
3831 3836 brackets++;
3832 3837 }
3833 3838
3834 3839 if (brackets == 0 && braces == 0) {
3835 3840 if (found_key) {
3836 3841 *dd = '\0';
3837 3842 return (dest);
3838 3843 }
3839 3844 dd = dest; /* reset string buffer */
3840 3845 state = DTRACE_JSON_COMMA;
3841 3846 }
3842 3847 break;
3843 3848 }
3844 3849 }
3845 3850 return (NULL);
3846 3851 }
3847 3852
3848 3853 /*
3849 3854 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
3850 3855 * Notice that we don't bother validating the proper number of arguments or
3851 3856 * their types in the tuple stack. This isn't needed because all argument
3852 3857 * interpretation is safe because of our load safety -- the worst that can
3853 3858 * happen is that a bogus program can obtain bogus results.
3854 3859 */
3855 3860 static void
3856 3861 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
3857 3862 dtrace_key_t *tupregs, int nargs,
3858 3863 dtrace_mstate_t *mstate, dtrace_state_t *state)
3859 3864 {
3860 3865 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
3861 3866 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
3862 3867 dtrace_vstate_t *vstate = &state->dts_vstate;
3863 3868
3864 3869 union {
3865 3870 mutex_impl_t mi;
3866 3871 uint64_t mx;
3867 3872 } m;
3868 3873
3869 3874 union {
3870 3875 krwlock_t ri;
3871 3876 uintptr_t rw;
3872 3877 } r;
3873 3878
3874 3879 switch (subr) {
3875 3880 case DIF_SUBR_RAND:
3876 3881 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
3877 3882 break;
3878 3883
3879 3884 case DIF_SUBR_MUTEX_OWNED:
3880 3885 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3881 3886 mstate, vstate)) {
3882 3887 regs[rd] = NULL;
3883 3888 break;
3884 3889 }
3885 3890
3886 3891 m.mx = dtrace_load64(tupregs[0].dttk_value);
3887 3892 if (MUTEX_TYPE_ADAPTIVE(&m.mi))
3888 3893 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
3889 3894 else
3890 3895 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
3891 3896 break;
3892 3897
3893 3898 case DIF_SUBR_MUTEX_OWNER:
3894 3899 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3895 3900 mstate, vstate)) {
3896 3901 regs[rd] = NULL;
3897 3902 break;
3898 3903 }
3899 3904
3900 3905 m.mx = dtrace_load64(tupregs[0].dttk_value);
3901 3906 if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
3902 3907 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
3903 3908 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
3904 3909 else
3905 3910 regs[rd] = 0;
3906 3911 break;
3907 3912
3908 3913 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
3909 3914 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3910 3915 mstate, vstate)) {
3911 3916 regs[rd] = NULL;
3912 3917 break;
3913 3918 }
3914 3919
3915 3920 m.mx = dtrace_load64(tupregs[0].dttk_value);
3916 3921 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
3917 3922 break;
3918 3923
3919 3924 case DIF_SUBR_MUTEX_TYPE_SPIN:
3920 3925 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3921 3926 mstate, vstate)) {
3922 3927 regs[rd] = NULL;
3923 3928 break;
3924 3929 }
3925 3930
3926 3931 m.mx = dtrace_load64(tupregs[0].dttk_value);
3927 3932 regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
3928 3933 break;
3929 3934
3930 3935 case DIF_SUBR_RW_READ_HELD: {
3931 3936 uintptr_t tmp;
3932 3937
3933 3938 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3934 3939 mstate, vstate)) {
3935 3940 regs[rd] = NULL;
3936 3941 break;
3937 3942 }
3938 3943
3939 3944 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3940 3945 regs[rd] = _RW_READ_HELD(&r.ri, tmp);
3941 3946 break;
3942 3947 }
3943 3948
3944 3949 case DIF_SUBR_RW_WRITE_HELD:
3945 3950 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3946 3951 mstate, vstate)) {
3947 3952 regs[rd] = NULL;
3948 3953 break;
3949 3954 }
3950 3955
3951 3956 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3952 3957 regs[rd] = _RW_WRITE_HELD(&r.ri);
3953 3958 break;
3954 3959
3955 3960 case DIF_SUBR_RW_ISWRITER:
3956 3961 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3957 3962 mstate, vstate)) {
3958 3963 regs[rd] = NULL;
3959 3964 break;
3960 3965 }
3961 3966
3962 3967 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3963 3968 regs[rd] = _RW_ISWRITER(&r.ri);
3964 3969 break;
3965 3970
3966 3971 case DIF_SUBR_BCOPY: {
3967 3972 /*
3968 3973 * We need to be sure that the destination is in the scratch
3969 3974 * region -- no other region is allowed.
3970 3975 */
3971 3976 uintptr_t src = tupregs[0].dttk_value;
3972 3977 uintptr_t dest = tupregs[1].dttk_value;
3973 3978 size_t size = tupregs[2].dttk_value;
3974 3979
3975 3980 if (!dtrace_inscratch(dest, size, mstate)) {
3976 3981 *flags |= CPU_DTRACE_BADADDR;
3977 3982 *illval = regs[rd];
3978 3983 break;
3979 3984 }
3980 3985
3981 3986 if (!dtrace_canload(src, size, mstate, vstate)) {
3982 3987 regs[rd] = NULL;
3983 3988 break;
3984 3989 }
3985 3990
3986 3991 dtrace_bcopy((void *)src, (void *)dest, size);
3987 3992 break;
3988 3993 }
3989 3994
3990 3995 case DIF_SUBR_ALLOCA:
3991 3996 case DIF_SUBR_COPYIN: {
3992 3997 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
3993 3998 uint64_t size =
3994 3999 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
3995 4000 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
3996 4001
3997 4002 /*
3998 4003 * This action doesn't require any credential checks since
3999 4004 * probes will not activate in user contexts to which the
4000 4005 * enabling user does not have permissions.
4001 4006 */
4002 4007
4003 4008 /*
4004 4009 * Rounding up the user allocation size could have overflowed
4005 4010 * a large, bogus allocation (like -1ULL) to 0.
4006 4011 */
4007 4012 if (scratch_size < size ||
4008 4013 !DTRACE_INSCRATCH(mstate, scratch_size)) {
4009 4014 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4010 4015 regs[rd] = NULL;
4011 4016 break;
4012 4017 }
4013 4018
4014 4019 if (subr == DIF_SUBR_COPYIN) {
4015 4020 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4016 4021 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4017 4022 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4018 4023 }
4019 4024
4020 4025 mstate->dtms_scratch_ptr += scratch_size;
4021 4026 regs[rd] = dest;
4022 4027 break;
4023 4028 }
4024 4029
4025 4030 case DIF_SUBR_COPYINTO: {
4026 4031 uint64_t size = tupregs[1].dttk_value;
4027 4032 uintptr_t dest = tupregs[2].dttk_value;
4028 4033
4029 4034 /*
4030 4035 * This action doesn't require any credential checks since
4031 4036 * probes will not activate in user contexts to which the
4032 4037 * enabling user does not have permissions.
4033 4038 */
4034 4039 if (!dtrace_inscratch(dest, size, mstate)) {
4035 4040 *flags |= CPU_DTRACE_BADADDR;
4036 4041 *illval = regs[rd];
4037 4042 break;
4038 4043 }
4039 4044
4040 4045 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4041 4046 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4042 4047 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4043 4048 break;
4044 4049 }
4045 4050
4046 4051 case DIF_SUBR_COPYINSTR: {
4047 4052 uintptr_t dest = mstate->dtms_scratch_ptr;
4048 4053 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4049 4054
4050 4055 if (nargs > 1 && tupregs[1].dttk_value < size)
4051 4056 size = tupregs[1].dttk_value + 1;
4052 4057
4053 4058 /*
4054 4059 * This action doesn't require any credential checks since
4055 4060 * probes will not activate in user contexts to which the
4056 4061 * enabling user does not have permissions.
4057 4062 */
4058 4063 if (!DTRACE_INSCRATCH(mstate, size)) {
4059 4064 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4060 4065 regs[rd] = NULL;
4061 4066 break;
4062 4067 }
4063 4068
4064 4069 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4065 4070 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
4066 4071 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4067 4072
4068 4073 ((char *)dest)[size - 1] = '\0';
4069 4074 mstate->dtms_scratch_ptr += size;
4070 4075 regs[rd] = dest;
4071 4076 break;
4072 4077 }
4073 4078
4074 4079 case DIF_SUBR_MSGSIZE:
4075 4080 case DIF_SUBR_MSGDSIZE: {
4076 4081 uintptr_t baddr = tupregs[0].dttk_value, daddr;
4077 4082 uintptr_t wptr, rptr;
4078 4083 size_t count = 0;
4079 4084 int cont = 0;
4080 4085
4081 4086 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4082 4087
4083 4088 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
4084 4089 vstate)) {
4085 4090 regs[rd] = NULL;
4086 4091 break;
4087 4092 }
4088 4093
4089 4094 wptr = dtrace_loadptr(baddr +
4090 4095 offsetof(mblk_t, b_wptr));
4091 4096
4092 4097 rptr = dtrace_loadptr(baddr +
4093 4098 offsetof(mblk_t, b_rptr));
4094 4099
4095 4100 if (wptr < rptr) {
4096 4101 *flags |= CPU_DTRACE_BADADDR;
4097 4102 *illval = tupregs[0].dttk_value;
4098 4103 break;
4099 4104 }
4100 4105
4101 4106 daddr = dtrace_loadptr(baddr +
4102 4107 offsetof(mblk_t, b_datap));
4103 4108
4104 4109 baddr = dtrace_loadptr(baddr +
4105 4110 offsetof(mblk_t, b_cont));
4106 4111
4107 4112 /*
4108 4113 * We want to prevent against denial-of-service here,
4109 4114 * so we're only going to search the list for
4110 4115 * dtrace_msgdsize_max mblks.
4111 4116 */
4112 4117 if (cont++ > dtrace_msgdsize_max) {
4113 4118 *flags |= CPU_DTRACE_ILLOP;
4114 4119 break;
4115 4120 }
4116 4121
4117 4122 if (subr == DIF_SUBR_MSGDSIZE) {
4118 4123 if (dtrace_load8(daddr +
4119 4124 offsetof(dblk_t, db_type)) != M_DATA)
4120 4125 continue;
4121 4126 }
4122 4127
4123 4128 count += wptr - rptr;
4124 4129 }
4125 4130
4126 4131 if (!(*flags & CPU_DTRACE_FAULT))
4127 4132 regs[rd] = count;
4128 4133
4129 4134 break;
4130 4135 }
4131 4136
4132 4137 case DIF_SUBR_PROGENYOF: {
4133 4138 pid_t pid = tupregs[0].dttk_value;
4134 4139 proc_t *p;
4135 4140 int rval = 0;
4136 4141
4137 4142 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4138 4143
4139 4144 for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
4140 4145 if (p->p_pidp->pid_id == pid) {
4141 4146 rval = 1;
4142 4147 break;
4143 4148 }
4144 4149 }
4145 4150
4146 4151 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4147 4152
4148 4153 regs[rd] = rval;
4149 4154 break;
4150 4155 }
4151 4156
4152 4157 case DIF_SUBR_SPECULATION:
4153 4158 regs[rd] = dtrace_speculation(state);
4154 4159 break;
4155 4160
4156 4161 case DIF_SUBR_COPYOUT: {
4157 4162 uintptr_t kaddr = tupregs[0].dttk_value;
4158 4163 uintptr_t uaddr = tupregs[1].dttk_value;
4159 4164 uint64_t size = tupregs[2].dttk_value;
4160 4165
4161 4166 if (!dtrace_destructive_disallow &&
4162 4167 dtrace_priv_proc_control(state, mstate) &&
4163 4168 !dtrace_istoxic(kaddr, size)) {
4164 4169 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4165 4170 dtrace_copyout(kaddr, uaddr, size, flags);
4166 4171 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4167 4172 }
4168 4173 break;
4169 4174 }
4170 4175
4171 4176 case DIF_SUBR_COPYOUTSTR: {
4172 4177 uintptr_t kaddr = tupregs[0].dttk_value;
4173 4178 uintptr_t uaddr = tupregs[1].dttk_value;
4174 4179 uint64_t size = tupregs[2].dttk_value;
4175 4180
4176 4181 if (!dtrace_destructive_disallow &&
4177 4182 dtrace_priv_proc_control(state, mstate) &&
4178 4183 !dtrace_istoxic(kaddr, size)) {
4179 4184 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4180 4185 dtrace_copyoutstr(kaddr, uaddr, size, flags);
4181 4186 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4182 4187 }
4183 4188 break;
4184 4189 }
4185 4190
4186 4191 case DIF_SUBR_STRLEN: {
4187 4192 size_t sz;
4188 4193 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
4189 4194 sz = dtrace_strlen((char *)addr,
4190 4195 state->dts_options[DTRACEOPT_STRSIZE]);
4191 4196
4192 4197 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) {
4193 4198 regs[rd] = NULL;
4194 4199 break;
4195 4200 }
4196 4201
4197 4202 regs[rd] = sz;
4198 4203
4199 4204 break;
4200 4205 }
4201 4206
4202 4207 case DIF_SUBR_STRCHR:
4203 4208 case DIF_SUBR_STRRCHR: {
4204 4209 /*
4205 4210 * We're going to iterate over the string looking for the
4206 4211 * specified character. We will iterate until we have reached
4207 4212 * the string length or we have found the character. If this
4208 4213 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
4209 4214 * of the specified character instead of the first.
4210 4215 */
4211 4216 uintptr_t saddr = tupregs[0].dttk_value;
4212 4217 uintptr_t addr = tupregs[0].dttk_value;
4213 4218 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE];
4214 4219 char c, target = (char)tupregs[1].dttk_value;
4215 4220
4216 4221 for (regs[rd] = NULL; addr < limit; addr++) {
4217 4222 if ((c = dtrace_load8(addr)) == target) {
4218 4223 regs[rd] = addr;
4219 4224
4220 4225 if (subr == DIF_SUBR_STRCHR)
4221 4226 break;
4222 4227 }
4223 4228
4224 4229 if (c == '\0')
4225 4230 break;
4226 4231 }
4227 4232
4228 4233 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) {
4229 4234 regs[rd] = NULL;
4230 4235 break;
4231 4236 }
4232 4237
4233 4238 break;
4234 4239 }
4235 4240
4236 4241 case DIF_SUBR_STRSTR:
4237 4242 case DIF_SUBR_INDEX:
4238 4243 case DIF_SUBR_RINDEX: {
4239 4244 /*
4240 4245 * We're going to iterate over the string looking for the
4241 4246 * specified string. We will iterate until we have reached
4242 4247 * the string length or we have found the string. (Yes, this
4243 4248 * is done in the most naive way possible -- but considering
4244 4249 * that the string we're searching for is likely to be
4245 4250 * relatively short, the complexity of Rabin-Karp or similar
4246 4251 * hardly seems merited.)
4247 4252 */
4248 4253 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
4249 4254 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
4250 4255 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4251 4256 size_t len = dtrace_strlen(addr, size);
4252 4257 size_t sublen = dtrace_strlen(substr, size);
4253 4258 char *limit = addr + len, *orig = addr;
4254 4259 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
4255 4260 int inc = 1;
4256 4261
4257 4262 regs[rd] = notfound;
4258 4263
4259 4264 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
4260 4265 regs[rd] = NULL;
4261 4266 break;
4262 4267 }
4263 4268
4264 4269 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
4265 4270 vstate)) {
4266 4271 regs[rd] = NULL;
4267 4272 break;
4268 4273 }
4269 4274
4270 4275 /*
4271 4276 * strstr() and index()/rindex() have similar semantics if
4272 4277 * both strings are the empty string: strstr() returns a
4273 4278 * pointer to the (empty) string, and index() and rindex()
4274 4279 * both return index 0 (regardless of any position argument).
4275 4280 */
4276 4281 if (sublen == 0 && len == 0) {
4277 4282 if (subr == DIF_SUBR_STRSTR)
4278 4283 regs[rd] = (uintptr_t)addr;
4279 4284 else
4280 4285 regs[rd] = 0;
4281 4286 break;
4282 4287 }
4283 4288
4284 4289 if (subr != DIF_SUBR_STRSTR) {
4285 4290 if (subr == DIF_SUBR_RINDEX) {
4286 4291 limit = orig - 1;
4287 4292 addr += len;
4288 4293 inc = -1;
4289 4294 }
4290 4295
4291 4296 /*
4292 4297 * Both index() and rindex() take an optional position
4293 4298 * argument that denotes the starting position.
4294 4299 */
4295 4300 if (nargs == 3) {
4296 4301 int64_t pos = (int64_t)tupregs[2].dttk_value;
4297 4302
4298 4303 /*
4299 4304 * If the position argument to index() is
4300 4305 * negative, Perl implicitly clamps it at
4301 4306 * zero. This semantic is a little surprising
4302 4307 * given the special meaning of negative
4303 4308 * positions to similar Perl functions like
4304 4309 * substr(), but it appears to reflect a
4305 4310 * notion that index() can start from a
4306 4311 * negative index and increment its way up to
4307 4312 * the string. Given this notion, Perl's
4308 4313 * rindex() is at least self-consistent in
4309 4314 * that it implicitly clamps positions greater
4310 4315 * than the string length to be the string
4311 4316 * length. Where Perl completely loses
4312 4317 * coherence, however, is when the specified
4313 4318 * substring is the empty string (""). In
4314 4319 * this case, even if the position is
4315 4320 * negative, rindex() returns 0 -- and even if
4316 4321 * the position is greater than the length,
4317 4322 * index() returns the string length. These
4318 4323 * semantics violate the notion that index()
4319 4324 * should never return a value less than the
4320 4325 * specified position and that rindex() should
4321 4326 * never return a value greater than the
4322 4327 * specified position. (One assumes that
4323 4328 * these semantics are artifacts of Perl's
4324 4329 * implementation and not the results of
4325 4330 * deliberate design -- it beggars belief that
4326 4331 * even Larry Wall could desire such oddness.)
4327 4332 * While in the abstract one would wish for
4328 4333 * consistent position semantics across
4329 4334 * substr(), index() and rindex() -- or at the
4330 4335 * very least self-consistent position
4331 4336 * semantics for index() and rindex() -- we
4332 4337 * instead opt to keep with the extant Perl
4333 4338 * semantics, in all their broken glory. (Do
4334 4339 * we have more desire to maintain Perl's
4335 4340 * semantics than Perl does? Probably.)
4336 4341 */
4337 4342 if (subr == DIF_SUBR_RINDEX) {
4338 4343 if (pos < 0) {
4339 4344 if (sublen == 0)
4340 4345 regs[rd] = 0;
4341 4346 break;
4342 4347 }
4343 4348
4344 4349 if (pos > len)
4345 4350 pos = len;
4346 4351 } else {
4347 4352 if (pos < 0)
4348 4353 pos = 0;
4349 4354
4350 4355 if (pos >= len) {
4351 4356 if (sublen == 0)
4352 4357 regs[rd] = len;
4353 4358 break;
4354 4359 }
4355 4360 }
4356 4361
4357 4362 addr = orig + pos;
4358 4363 }
4359 4364 }
4360 4365
4361 4366 for (regs[rd] = notfound; addr != limit; addr += inc) {
4362 4367 if (dtrace_strncmp(addr, substr, sublen) == 0) {
4363 4368 if (subr != DIF_SUBR_STRSTR) {
4364 4369 /*
4365 4370 * As D index() and rindex() are
4366 4371 * modeled on Perl (and not on awk),
4367 4372 * we return a zero-based (and not a
4368 4373 * one-based) index. (For you Perl
4369 4374 * weenies: no, we're not going to add
4370 4375 * $[ -- and shouldn't you be at a con
4371 4376 * or something?)
4372 4377 */
4373 4378 regs[rd] = (uintptr_t)(addr - orig);
4374 4379 break;
4375 4380 }
4376 4381
4377 4382 ASSERT(subr == DIF_SUBR_STRSTR);
4378 4383 regs[rd] = (uintptr_t)addr;
4379 4384 break;
4380 4385 }
4381 4386 }
4382 4387
4383 4388 break;
4384 4389 }
4385 4390
4386 4391 case DIF_SUBR_STRTOK: {
4387 4392 uintptr_t addr = tupregs[0].dttk_value;
4388 4393 uintptr_t tokaddr = tupregs[1].dttk_value;
4389 4394 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4390 4395 uintptr_t limit, toklimit = tokaddr + size;
4391 4396 uint8_t c, tokmap[32]; /* 256 / 8 */
4392 4397 char *dest = (char *)mstate->dtms_scratch_ptr;
4393 4398 int i;
4394 4399
4395 4400 /*
4396 4401 * Check both the token buffer and (later) the input buffer,
4397 4402 * since both could be non-scratch addresses.
4398 4403 */
4399 4404 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) {
4400 4405 regs[rd] = NULL;
4401 4406 break;
4402 4407 }
4403 4408
4404 4409 if (!DTRACE_INSCRATCH(mstate, size)) {
4405 4410 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4406 4411 regs[rd] = NULL;
4407 4412 break;
4408 4413 }
4409 4414
4410 4415 if (addr == NULL) {
4411 4416 /*
4412 4417 * If the address specified is NULL, we use our saved
4413 4418 * strtok pointer from the mstate. Note that this
4414 4419 * means that the saved strtok pointer is _only_
4415 4420 * valid within multiple enablings of the same probe --
4416 4421 * it behaves like an implicit clause-local variable.
4417 4422 */
4418 4423 addr = mstate->dtms_strtok;
4419 4424 } else {
4420 4425 /*
4421 4426 * If the user-specified address is non-NULL we must
4422 4427 * access check it. This is the only time we have
4423 4428 * a chance to do so, since this address may reside
4424 4429 * in the string table of this clause-- future calls
4425 4430 * (when we fetch addr from mstate->dtms_strtok)
4426 4431 * would fail this access check.
4427 4432 */
4428 4433 if (!dtrace_strcanload(addr, size, mstate, vstate)) {
4429 4434 regs[rd] = NULL;
4430 4435 break;
4431 4436 }
4432 4437 }
4433 4438
4434 4439 /*
4435 4440 * First, zero the token map, and then process the token
4436 4441 * string -- setting a bit in the map for every character
4437 4442 * found in the token string.
4438 4443 */
4439 4444 for (i = 0; i < sizeof (tokmap); i++)
4440 4445 tokmap[i] = 0;
4441 4446
4442 4447 for (; tokaddr < toklimit; tokaddr++) {
4443 4448 if ((c = dtrace_load8(tokaddr)) == '\0')
4444 4449 break;
4445 4450
4446 4451 ASSERT((c >> 3) < sizeof (tokmap));
4447 4452 tokmap[c >> 3] |= (1 << (c & 0x7));
4448 4453 }
4449 4454
4450 4455 for (limit = addr + size; addr < limit; addr++) {
4451 4456 /*
4452 4457 * We're looking for a character that is _not_ contained
4453 4458 * in the token string.
4454 4459 */
4455 4460 if ((c = dtrace_load8(addr)) == '\0')
4456 4461 break;
4457 4462
4458 4463 if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
4459 4464 break;
4460 4465 }
4461 4466
4462 4467 if (c == '\0') {
4463 4468 /*
4464 4469 * We reached the end of the string without finding
4465 4470 * any character that was not in the token string.
4466 4471 * We return NULL in this case, and we set the saved
4467 4472 * address to NULL as well.
4468 4473 */
4469 4474 regs[rd] = NULL;
4470 4475 mstate->dtms_strtok = NULL;
4471 4476 break;
4472 4477 }
4473 4478
4474 4479 /*
4475 4480 * From here on, we're copying into the destination string.
4476 4481 */
4477 4482 for (i = 0; addr < limit && i < size - 1; addr++) {
4478 4483 if ((c = dtrace_load8(addr)) == '\0')
4479 4484 break;
4480 4485
4481 4486 if (tokmap[c >> 3] & (1 << (c & 0x7)))
4482 4487 break;
4483 4488
4484 4489 ASSERT(i < size);
4485 4490 dest[i++] = c;
4486 4491 }
4487 4492
4488 4493 ASSERT(i < size);
4489 4494 dest[i] = '\0';
4490 4495 regs[rd] = (uintptr_t)dest;
4491 4496 mstate->dtms_scratch_ptr += size;
4492 4497 mstate->dtms_strtok = addr;
4493 4498 break;
4494 4499 }
4495 4500
4496 4501 case DIF_SUBR_SUBSTR: {
4497 4502 uintptr_t s = tupregs[0].dttk_value;
4498 4503 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4499 4504 char *d = (char *)mstate->dtms_scratch_ptr;
4500 4505 int64_t index = (int64_t)tupregs[1].dttk_value;
4501 4506 int64_t remaining = (int64_t)tupregs[2].dttk_value;
4502 4507 size_t len = dtrace_strlen((char *)s, size);
4503 4508 int64_t i;
4504 4509
4505 4510 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4506 4511 regs[rd] = NULL;
4507 4512 break;
4508 4513 }
4509 4514
4510 4515 if (!DTRACE_INSCRATCH(mstate, size)) {
4511 4516 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4512 4517 regs[rd] = NULL;
4513 4518 break;
4514 4519 }
4515 4520
4516 4521 if (nargs <= 2)
4517 4522 remaining = (int64_t)size;
4518 4523
4519 4524 if (index < 0) {
4520 4525 index += len;
4521 4526
4522 4527 if (index < 0 && index + remaining > 0) {
4523 4528 remaining += index;
4524 4529 index = 0;
4525 4530 }
4526 4531 }
4527 4532
4528 4533 if (index >= len || index < 0) {
4529 4534 remaining = 0;
4530 4535 } else if (remaining < 0) {
4531 4536 remaining += len - index;
4532 4537 } else if (index + remaining > size) {
4533 4538 remaining = size - index;
4534 4539 }
4535 4540
4536 4541 for (i = 0; i < remaining; i++) {
4537 4542 if ((d[i] = dtrace_load8(s + index + i)) == '\0')
4538 4543 break;
4539 4544 }
4540 4545
4541 4546 d[i] = '\0';
4542 4547
4543 4548 mstate->dtms_scratch_ptr += size;
4544 4549 regs[rd] = (uintptr_t)d;
4545 4550 break;
4546 4551 }
4547 4552
4548 4553 case DIF_SUBR_JSON: {
4549 4554 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4550 4555 uintptr_t json = tupregs[0].dttk_value;
4551 4556 size_t jsonlen = dtrace_strlen((char *)json, size);
4552 4557 uintptr_t elem = tupregs[1].dttk_value;
4553 4558 size_t elemlen = dtrace_strlen((char *)elem, size);
4554 4559
4555 4560 char *dest = (char *)mstate->dtms_scratch_ptr;
4556 4561 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1;
4557 4562 char *ee = elemlist;
4558 4563 int nelems = 1;
4559 4564 uintptr_t cur;
4560 4565
4561 4566 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) ||
4562 4567 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) {
4563 4568 regs[rd] = NULL;
4564 4569 break;
4565 4570 }
4566 4571
4567 4572 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) {
4568 4573 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4569 4574 regs[rd] = NULL;
4570 4575 break;
4571 4576 }
4572 4577
4573 4578 /*
4574 4579 * Read the element selector and split it up into a packed list
4575 4580 * of strings.
4576 4581 */
4577 4582 for (cur = elem; cur < elem + elemlen; cur++) {
4578 4583 char cc = dtrace_load8(cur);
4579 4584
4580 4585 if (cur == elem && cc == '[') {
4581 4586 /*
4582 4587 * If the first element selector key is
4583 4588 * actually an array index then ignore the
4584 4589 * bracket.
4585 4590 */
4586 4591 continue;
4587 4592 }
4588 4593
4589 4594 if (cc == ']')
4590 4595 continue;
4591 4596
4592 4597 if (cc == '.' || cc == '[') {
4593 4598 nelems++;
4594 4599 cc = '\0';
4595 4600 }
4596 4601
4597 4602 *ee++ = cc;
4598 4603 }
4599 4604 *ee++ = '\0';
4600 4605
4601 4606 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist,
4602 4607 nelems, dest)) != NULL)
4603 4608 mstate->dtms_scratch_ptr += jsonlen + 1;
4604 4609 break;
4605 4610 }
4606 4611
4607 4612 case DIF_SUBR_TOUPPER:
4608 4613 case DIF_SUBR_TOLOWER: {
4609 4614 uintptr_t s = tupregs[0].dttk_value;
4610 4615 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4611 4616 char *dest = (char *)mstate->dtms_scratch_ptr, c;
4612 4617 size_t len = dtrace_strlen((char *)s, size);
4613 4618 char lower, upper, convert;
4614 4619 int64_t i;
4615 4620
4616 4621 if (subr == DIF_SUBR_TOUPPER) {
4617 4622 lower = 'a';
4618 4623 upper = 'z';
4619 4624 convert = 'A';
4620 4625 } else {
4621 4626 lower = 'A';
4622 4627 upper = 'Z';
4623 4628 convert = 'a';
4624 4629 }
4625 4630
4626 4631 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4627 4632 regs[rd] = NULL;
4628 4633 break;
4629 4634 }
4630 4635
4631 4636 if (!DTRACE_INSCRATCH(mstate, size)) {
4632 4637 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4633 4638 regs[rd] = NULL;
4634 4639 break;
4635 4640 }
4636 4641
4637 4642 for (i = 0; i < size - 1; i++) {
4638 4643 if ((c = dtrace_load8(s + i)) == '\0')
4639 4644 break;
4640 4645
4641 4646 if (c >= lower && c <= upper)
4642 4647 c = convert + (c - lower);
4643 4648
4644 4649 dest[i] = c;
4645 4650 }
4646 4651
4647 4652 ASSERT(i < size);
4648 4653 dest[i] = '\0';
4649 4654 regs[rd] = (uintptr_t)dest;
4650 4655 mstate->dtms_scratch_ptr += size;
4651 4656 break;
4652 4657 }
4653 4658
4654 4659 case DIF_SUBR_GETMAJOR:
4655 4660 #ifdef _LP64
4656 4661 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
4657 4662 #else
4658 4663 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
4659 4664 #endif
4660 4665 break;
4661 4666
4662 4667 case DIF_SUBR_GETMINOR:
4663 4668 #ifdef _LP64
4664 4669 regs[rd] = tupregs[0].dttk_value & MAXMIN64;
4665 4670 #else
4666 4671 regs[rd] = tupregs[0].dttk_value & MAXMIN;
4667 4672 #endif
4668 4673 break;
4669 4674
4670 4675 case DIF_SUBR_DDI_PATHNAME: {
4671 4676 /*
4672 4677 * This one is a galactic mess. We are going to roughly
4673 4678 * emulate ddi_pathname(), but it's made more complicated
4674 4679 * by the fact that we (a) want to include the minor name and
4675 4680 * (b) must proceed iteratively instead of recursively.
4676 4681 */
4677 4682 uintptr_t dest = mstate->dtms_scratch_ptr;
4678 4683 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4679 4684 char *start = (char *)dest, *end = start + size - 1;
4680 4685 uintptr_t daddr = tupregs[0].dttk_value;
4681 4686 int64_t minor = (int64_t)tupregs[1].dttk_value;
4682 4687 char *s;
4683 4688 int i, len, depth = 0;
4684 4689
4685 4690 /*
4686 4691 * Due to all the pointer jumping we do and context we must
4687 4692 * rely upon, we just mandate that the user must have kernel
4688 4693 * read privileges to use this routine.
4689 4694 */
4690 4695 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
4691 4696 *flags |= CPU_DTRACE_KPRIV;
4692 4697 *illval = daddr;
4693 4698 regs[rd] = NULL;
4694 4699 }
4695 4700
4696 4701 if (!DTRACE_INSCRATCH(mstate, size)) {
4697 4702 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4698 4703 regs[rd] = NULL;
4699 4704 break;
4700 4705 }
4701 4706
4702 4707 *end = '\0';
4703 4708
4704 4709 /*
4705 4710 * We want to have a name for the minor. In order to do this,
4706 4711 * we need to walk the minor list from the devinfo. We want
4707 4712 * to be sure that we don't infinitely walk a circular list,
4708 4713 * so we check for circularity by sending a scout pointer
4709 4714 * ahead two elements for every element that we iterate over;
4710 4715 * if the list is circular, these will ultimately point to the
4711 4716 * same element. You may recognize this little trick as the
4712 4717 * answer to a stupid interview question -- one that always
4713 4718 * seems to be asked by those who had to have it laboriously
4714 4719 * explained to them, and who can't even concisely describe
4715 4720 * the conditions under which one would be forced to resort to
4716 4721 * this technique. Needless to say, those conditions are
4717 4722 * found here -- and probably only here. Is this the only use
4718 4723 * of this infamous trick in shipping, production code? If it
4719 4724 * isn't, it probably should be...
4720 4725 */
4721 4726 if (minor != -1) {
4722 4727 uintptr_t maddr = dtrace_loadptr(daddr +
4723 4728 offsetof(struct dev_info, devi_minor));
4724 4729
4725 4730 uintptr_t next = offsetof(struct ddi_minor_data, next);
4726 4731 uintptr_t name = offsetof(struct ddi_minor_data,
4727 4732 d_minor) + offsetof(struct ddi_minor, name);
4728 4733 uintptr_t dev = offsetof(struct ddi_minor_data,
4729 4734 d_minor) + offsetof(struct ddi_minor, dev);
4730 4735 uintptr_t scout;
4731 4736
4732 4737 if (maddr != NULL)
4733 4738 scout = dtrace_loadptr(maddr + next);
4734 4739
4735 4740 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4736 4741 uint64_t m;
4737 4742 #ifdef _LP64
4738 4743 m = dtrace_load64(maddr + dev) & MAXMIN64;
4739 4744 #else
4740 4745 m = dtrace_load32(maddr + dev) & MAXMIN;
4741 4746 #endif
4742 4747 if (m != minor) {
4743 4748 maddr = dtrace_loadptr(maddr + next);
4744 4749
4745 4750 if (scout == NULL)
4746 4751 continue;
4747 4752
4748 4753 scout = dtrace_loadptr(scout + next);
4749 4754
4750 4755 if (scout == NULL)
4751 4756 continue;
4752 4757
4753 4758 scout = dtrace_loadptr(scout + next);
4754 4759
4755 4760 if (scout == NULL)
4756 4761 continue;
4757 4762
4758 4763 if (scout == maddr) {
4759 4764 *flags |= CPU_DTRACE_ILLOP;
4760 4765 break;
4761 4766 }
4762 4767
4763 4768 continue;
4764 4769 }
4765 4770
4766 4771 /*
4767 4772 * We have the minor data. Now we need to
4768 4773 * copy the minor's name into the end of the
4769 4774 * pathname.
4770 4775 */
4771 4776 s = (char *)dtrace_loadptr(maddr + name);
4772 4777 len = dtrace_strlen(s, size);
4773 4778
4774 4779 if (*flags & CPU_DTRACE_FAULT)
4775 4780 break;
4776 4781
4777 4782 if (len != 0) {
4778 4783 if ((end -= (len + 1)) < start)
4779 4784 break;
4780 4785
4781 4786 *end = ':';
4782 4787 }
4783 4788
4784 4789 for (i = 1; i <= len; i++)
4785 4790 end[i] = dtrace_load8((uintptr_t)s++);
4786 4791 break;
4787 4792 }
4788 4793 }
4789 4794
4790 4795 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4791 4796 ddi_node_state_t devi_state;
4792 4797
4793 4798 devi_state = dtrace_load32(daddr +
4794 4799 offsetof(struct dev_info, devi_node_state));
4795 4800
4796 4801 if (*flags & CPU_DTRACE_FAULT)
4797 4802 break;
4798 4803
4799 4804 if (devi_state >= DS_INITIALIZED) {
4800 4805 s = (char *)dtrace_loadptr(daddr +
4801 4806 offsetof(struct dev_info, devi_addr));
4802 4807 len = dtrace_strlen(s, size);
4803 4808
4804 4809 if (*flags & CPU_DTRACE_FAULT)
4805 4810 break;
4806 4811
4807 4812 if (len != 0) {
4808 4813 if ((end -= (len + 1)) < start)
4809 4814 break;
4810 4815
4811 4816 *end = '@';
4812 4817 }
4813 4818
4814 4819 for (i = 1; i <= len; i++)
4815 4820 end[i] = dtrace_load8((uintptr_t)s++);
4816 4821 }
4817 4822
4818 4823 /*
4819 4824 * Now for the node name...
4820 4825 */
4821 4826 s = (char *)dtrace_loadptr(daddr +
4822 4827 offsetof(struct dev_info, devi_node_name));
4823 4828
4824 4829 daddr = dtrace_loadptr(daddr +
4825 4830 offsetof(struct dev_info, devi_parent));
4826 4831
4827 4832 /*
4828 4833 * If our parent is NULL (that is, if we're the root
4829 4834 * node), we're going to use the special path
4830 4835 * "devices".
4831 4836 */
4832 4837 if (daddr == NULL)
4833 4838 s = "devices";
4834 4839
4835 4840 len = dtrace_strlen(s, size);
4836 4841 if (*flags & CPU_DTRACE_FAULT)
4837 4842 break;
4838 4843
4839 4844 if ((end -= (len + 1)) < start)
4840 4845 break;
4841 4846
4842 4847 for (i = 1; i <= len; i++)
4843 4848 end[i] = dtrace_load8((uintptr_t)s++);
4844 4849 *end = '/';
4845 4850
4846 4851 if (depth++ > dtrace_devdepth_max) {
4847 4852 *flags |= CPU_DTRACE_ILLOP;
4848 4853 break;
4849 4854 }
4850 4855 }
4851 4856
4852 4857 if (end < start)
4853 4858 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4854 4859
4855 4860 if (daddr == NULL) {
4856 4861 regs[rd] = (uintptr_t)end;
4857 4862 mstate->dtms_scratch_ptr += size;
4858 4863 }
4859 4864
4860 4865 break;
4861 4866 }
4862 4867
4863 4868 case DIF_SUBR_STRJOIN: {
4864 4869 char *d = (char *)mstate->dtms_scratch_ptr;
4865 4870 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4866 4871 uintptr_t s1 = tupregs[0].dttk_value;
4867 4872 uintptr_t s2 = tupregs[1].dttk_value;
4868 4873 int i = 0;
4869 4874
4870 4875 if (!dtrace_strcanload(s1, size, mstate, vstate) ||
4871 4876 !dtrace_strcanload(s2, size, mstate, vstate)) {
4872 4877 regs[rd] = NULL;
4873 4878 break;
4874 4879 }
4875 4880
4876 4881 if (!DTRACE_INSCRATCH(mstate, size)) {
4877 4882 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4878 4883 regs[rd] = NULL;
4879 4884 break;
4880 4885 }
4881 4886
4882 4887 for (;;) {
4883 4888 if (i >= size) {
4884 4889 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4885 4890 regs[rd] = NULL;
4886 4891 break;
4887 4892 }
4888 4893
4889 4894 if ((d[i++] = dtrace_load8(s1++)) == '\0') {
4890 4895 i--;
4891 4896 break;
4892 4897 }
4893 4898 }
4894 4899
4895 4900 for (;;) {
4896 4901 if (i >= size) {
4897 4902 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4898 4903 regs[rd] = NULL;
4899 4904 break;
4900 4905 }
4901 4906
4902 4907 if ((d[i++] = dtrace_load8(s2++)) == '\0')
4903 4908 break;
4904 4909 }
4905 4910
4906 4911 if (i < size) {
4907 4912 mstate->dtms_scratch_ptr += i;
4908 4913 regs[rd] = (uintptr_t)d;
4909 4914 }
4910 4915
4911 4916 break;
4912 4917 }
4913 4918
4914 4919 case DIF_SUBR_STRTOLL: {
4915 4920 uintptr_t s = tupregs[0].dttk_value;
4916 4921 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4917 4922 int base = 10;
4918 4923
4919 4924 if (nargs > 1) {
4920 4925 if ((base = tupregs[1].dttk_value) <= 1 ||
4921 4926 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4922 4927 *flags |= CPU_DTRACE_ILLOP;
4923 4928 break;
4924 4929 }
4925 4930 }
4926 4931
4927 4932 if (!dtrace_strcanload(s, size, mstate, vstate)) {
4928 4933 regs[rd] = INT64_MIN;
4929 4934 break;
4930 4935 }
4931 4936
4932 4937 regs[rd] = dtrace_strtoll((char *)s, base, size);
4933 4938 break;
4934 4939 }
4935 4940
4936 4941 case DIF_SUBR_LLTOSTR: {
4937 4942 int64_t i = (int64_t)tupregs[0].dttk_value;
4938 4943 uint64_t val, digit;
4939 4944 uint64_t size = 65; /* enough room for 2^64 in binary */
4940 4945 char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
4941 4946 int base = 10;
4942 4947
4943 4948 if (nargs > 1) {
4944 4949 if ((base = tupregs[1].dttk_value) <= 1 ||
4945 4950 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4946 4951 *flags |= CPU_DTRACE_ILLOP;
4947 4952 break;
4948 4953 }
4949 4954 }
4950 4955
4951 4956 val = (base == 10 && i < 0) ? i * -1 : i;
4952 4957
4953 4958 if (!DTRACE_INSCRATCH(mstate, size)) {
4954 4959 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4955 4960 regs[rd] = NULL;
4956 4961 break;
4957 4962 }
4958 4963
4959 4964 for (*end-- = '\0'; val; val /= base) {
4960 4965 if ((digit = val % base) <= '9' - '0') {
4961 4966 *end-- = '0' + digit;
4962 4967 } else {
4963 4968 *end-- = 'a' + (digit - ('9' - '0') - 1);
4964 4969 }
4965 4970 }
4966 4971
4967 4972 if (i == 0 && base == 16)
4968 4973 *end-- = '0';
4969 4974
4970 4975 if (base == 16)
4971 4976 *end-- = 'x';
4972 4977
4973 4978 if (i == 0 || base == 8 || base == 16)
4974 4979 *end-- = '0';
4975 4980
4976 4981 if (i < 0 && base == 10)
4977 4982 *end-- = '-';
4978 4983
4979 4984 regs[rd] = (uintptr_t)end + 1;
4980 4985 mstate->dtms_scratch_ptr += size;
4981 4986 break;
4982 4987 }
4983 4988
4984 4989 case DIF_SUBR_HTONS:
4985 4990 case DIF_SUBR_NTOHS:
4986 4991 #ifdef _BIG_ENDIAN
4987 4992 regs[rd] = (uint16_t)tupregs[0].dttk_value;
4988 4993 #else
4989 4994 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
4990 4995 #endif
4991 4996 break;
4992 4997
4993 4998
4994 4999 case DIF_SUBR_HTONL:
4995 5000 case DIF_SUBR_NTOHL:
4996 5001 #ifdef _BIG_ENDIAN
4997 5002 regs[rd] = (uint32_t)tupregs[0].dttk_value;
4998 5003 #else
4999 5004 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
5000 5005 #endif
5001 5006 break;
5002 5007
5003 5008
5004 5009 case DIF_SUBR_HTONLL:
5005 5010 case DIF_SUBR_NTOHLL:
5006 5011 #ifdef _BIG_ENDIAN
5007 5012 regs[rd] = (uint64_t)tupregs[0].dttk_value;
5008 5013 #else
5009 5014 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
5010 5015 #endif
5011 5016 break;
5012 5017
5013 5018
5014 5019 case DIF_SUBR_DIRNAME:
5015 5020 case DIF_SUBR_BASENAME: {
5016 5021 char *dest = (char *)mstate->dtms_scratch_ptr;
5017 5022 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5018 5023 uintptr_t src = tupregs[0].dttk_value;
5019 5024 int i, j, len = dtrace_strlen((char *)src, size);
5020 5025 int lastbase = -1, firstbase = -1, lastdir = -1;
5021 5026 int start, end;
5022 5027
5023 5028 if (!dtrace_canload(src, len + 1, mstate, vstate)) {
5024 5029 regs[rd] = NULL;
5025 5030 break;
5026 5031 }
5027 5032
5028 5033 if (!DTRACE_INSCRATCH(mstate, size)) {
5029 5034 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5030 5035 regs[rd] = NULL;
5031 5036 break;
5032 5037 }
5033 5038
5034 5039 /*
5035 5040 * The basename and dirname for a zero-length string is
5036 5041 * defined to be "."
5037 5042 */
5038 5043 if (len == 0) {
5039 5044 len = 1;
5040 5045 src = (uintptr_t)".";
5041 5046 }
5042 5047
5043 5048 /*
5044 5049 * Start from the back of the string, moving back toward the
5045 5050 * front until we see a character that isn't a slash. That
5046 5051 * character is the last character in the basename.
5047 5052 */
5048 5053 for (i = len - 1; i >= 0; i--) {
5049 5054 if (dtrace_load8(src + i) != '/')
5050 5055 break;
5051 5056 }
5052 5057
5053 5058 if (i >= 0)
5054 5059 lastbase = i;
5055 5060
5056 5061 /*
5057 5062 * Starting from the last character in the basename, move
5058 5063 * towards the front until we find a slash. The character
5059 5064 * that we processed immediately before that is the first
5060 5065 * character in the basename.
5061 5066 */
5062 5067 for (; i >= 0; i--) {
5063 5068 if (dtrace_load8(src + i) == '/')
5064 5069 break;
5065 5070 }
5066 5071
5067 5072 if (i >= 0)
5068 5073 firstbase = i + 1;
5069 5074
5070 5075 /*
5071 5076 * Now keep going until we find a non-slash character. That
5072 5077 * character is the last character in the dirname.
5073 5078 */
5074 5079 for (; i >= 0; i--) {
5075 5080 if (dtrace_load8(src + i) != '/')
5076 5081 break;
5077 5082 }
5078 5083
5079 5084 if (i >= 0)
5080 5085 lastdir = i;
5081 5086
5082 5087 ASSERT(!(lastbase == -1 && firstbase != -1));
5083 5088 ASSERT(!(firstbase == -1 && lastdir != -1));
5084 5089
5085 5090 if (lastbase == -1) {
5086 5091 /*
5087 5092 * We didn't find a non-slash character. We know that
5088 5093 * the length is non-zero, so the whole string must be
5089 5094 * slashes. In either the dirname or the basename
5090 5095 * case, we return '/'.
5091 5096 */
5092 5097 ASSERT(firstbase == -1);
5093 5098 firstbase = lastbase = lastdir = 0;
5094 5099 }
5095 5100
5096 5101 if (firstbase == -1) {
5097 5102 /*
5098 5103 * The entire string consists only of a basename
5099 5104 * component. If we're looking for dirname, we need
5100 5105 * to change our string to be just "."; if we're
5101 5106 * looking for a basename, we'll just set the first
5102 5107 * character of the basename to be 0.
5103 5108 */
5104 5109 if (subr == DIF_SUBR_DIRNAME) {
5105 5110 ASSERT(lastdir == -1);
5106 5111 src = (uintptr_t)".";
5107 5112 lastdir = 0;
5108 5113 } else {
5109 5114 firstbase = 0;
5110 5115 }
5111 5116 }
5112 5117
5113 5118 if (subr == DIF_SUBR_DIRNAME) {
5114 5119 if (lastdir == -1) {
5115 5120 /*
5116 5121 * We know that we have a slash in the name --
5117 5122 * or lastdir would be set to 0, above. And
5118 5123 * because lastdir is -1, we know that this
5119 5124 * slash must be the first character. (That
5120 5125 * is, the full string must be of the form
5121 5126 * "/basename".) In this case, the last
5122 5127 * character of the directory name is 0.
5123 5128 */
5124 5129 lastdir = 0;
5125 5130 }
5126 5131
5127 5132 start = 0;
5128 5133 end = lastdir;
5129 5134 } else {
5130 5135 ASSERT(subr == DIF_SUBR_BASENAME);
5131 5136 ASSERT(firstbase != -1 && lastbase != -1);
5132 5137 start = firstbase;
5133 5138 end = lastbase;
5134 5139 }
5135 5140
5136 5141 for (i = start, j = 0; i <= end && j < size - 1; i++, j++)
5137 5142 dest[j] = dtrace_load8(src + i);
5138 5143
5139 5144 dest[j] = '\0';
5140 5145 regs[rd] = (uintptr_t)dest;
5141 5146 mstate->dtms_scratch_ptr += size;
5142 5147 break;
5143 5148 }
5144 5149
5145 5150 case DIF_SUBR_GETF: {
5146 5151 uintptr_t fd = tupregs[0].dttk_value;
5147 5152 uf_info_t *finfo = &curthread->t_procp->p_user.u_finfo;
5148 5153 file_t *fp;
5149 5154
5150 5155 if (!dtrace_priv_proc(state, mstate)) {
5151 5156 regs[rd] = NULL;
5152 5157 break;
5153 5158 }
5154 5159
5155 5160 /*
5156 5161 * This is safe because fi_nfiles only increases, and the
5157 5162 * fi_list array is not freed when the array size doubles.
5158 5163 * (See the comment in flist_grow() for details on the
5159 5164 * management of the u_finfo structure.)
5160 5165 */
5161 5166 fp = fd < finfo->fi_nfiles ? finfo->fi_list[fd].uf_file : NULL;
5162 5167
5163 5168 mstate->dtms_getf = fp;
5164 5169 regs[rd] = (uintptr_t)fp;
5165 5170 break;
5166 5171 }
5167 5172
5168 5173 case DIF_SUBR_CLEANPATH: {
5169 5174 char *dest = (char *)mstate->dtms_scratch_ptr, c;
5170 5175 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5171 5176 uintptr_t src = tupregs[0].dttk_value;
5172 5177 int i = 0, j = 0;
5173 5178 zone_t *z;
5174 5179
5175 5180 if (!dtrace_strcanload(src, size, mstate, vstate)) {
5176 5181 regs[rd] = NULL;
5177 5182 break;
5178 5183 }
5179 5184
5180 5185 if (!DTRACE_INSCRATCH(mstate, size)) {
5181 5186 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5182 5187 regs[rd] = NULL;
5183 5188 break;
5184 5189 }
5185 5190
5186 5191 /*
5187 5192 * Move forward, loading each character.
5188 5193 */
5189 5194 do {
5190 5195 c = dtrace_load8(src + i++);
5191 5196 next:
5192 5197 if (j + 5 >= size) /* 5 = strlen("/..c\0") */
5193 5198 break;
5194 5199
5195 5200 if (c != '/') {
5196 5201 dest[j++] = c;
5197 5202 continue;
5198 5203 }
5199 5204
5200 5205 c = dtrace_load8(src + i++);
5201 5206
5202 5207 if (c == '/') {
5203 5208 /*
5204 5209 * We have two slashes -- we can just advance
5205 5210 * to the next character.
5206 5211 */
5207 5212 goto next;
5208 5213 }
5209 5214
5210 5215 if (c != '.') {
5211 5216 /*
5212 5217 * This is not "." and it's not ".." -- we can
5213 5218 * just store the "/" and this character and
5214 5219 * drive on.
5215 5220 */
5216 5221 dest[j++] = '/';
5217 5222 dest[j++] = c;
5218 5223 continue;
5219 5224 }
5220 5225
5221 5226 c = dtrace_load8(src + i++);
5222 5227
5223 5228 if (c == '/') {
5224 5229 /*
5225 5230 * This is a "/./" component. We're not going
5226 5231 * to store anything in the destination buffer;
5227 5232 * we're just going to go to the next component.
5228 5233 */
5229 5234 goto next;
5230 5235 }
5231 5236
5232 5237 if (c != '.') {
5233 5238 /*
5234 5239 * This is not ".." -- we can just store the
5235 5240 * "/." and this character and continue
5236 5241 * processing.
5237 5242 */
5238 5243 dest[j++] = '/';
5239 5244 dest[j++] = '.';
5240 5245 dest[j++] = c;
5241 5246 continue;
5242 5247 }
5243 5248
5244 5249 c = dtrace_load8(src + i++);
5245 5250
5246 5251 if (c != '/' && c != '\0') {
5247 5252 /*
5248 5253 * This is not ".." -- it's "..[mumble]".
5249 5254 * We'll store the "/.." and this character
5250 5255 * and continue processing.
5251 5256 */
5252 5257 dest[j++] = '/';
5253 5258 dest[j++] = '.';
5254 5259 dest[j++] = '.';
5255 5260 dest[j++] = c;
5256 5261 continue;
5257 5262 }
5258 5263
5259 5264 /*
5260 5265 * This is "/../" or "/..\0". We need to back up
5261 5266 * our destination pointer until we find a "/".
5262 5267 */
5263 5268 i--;
5264 5269 while (j != 0 && dest[--j] != '/')
5265 5270 continue;
5266 5271
5267 5272 if (c == '\0')
5268 5273 dest[++j] = '/';
5269 5274 } while (c != '\0');
5270 5275
5271 5276 dest[j] = '\0';
5272 5277
5273 5278 if (mstate->dtms_getf != NULL &&
5274 5279 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) &&
5275 5280 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) {
5276 5281 /*
5277 5282 * If we've done a getf() as a part of this ECB and we
5278 5283 * don't have kernel access (and we're not in the global
5279 5284 * zone), check if the path we cleaned up begins with
5280 5285 * the zone's root path, and trim it off if so. Note
5281 5286 * that this is an output cleanliness issue, not a
5282 5287 * security issue: knowing one's zone root path does
5283 5288 * not enable privilege escalation.
5284 5289 */
5285 5290 if (strstr(dest, z->zone_rootpath) == dest)
5286 5291 dest += strlen(z->zone_rootpath) - 1;
5287 5292 }
5288 5293
5289 5294 regs[rd] = (uintptr_t)dest;
5290 5295 mstate->dtms_scratch_ptr += size;
5291 5296 break;
5292 5297 }
5293 5298
5294 5299 case DIF_SUBR_INET_NTOA:
5295 5300 case DIF_SUBR_INET_NTOA6:
5296 5301 case DIF_SUBR_INET_NTOP: {
5297 5302 size_t size;
5298 5303 int af, argi, i;
5299 5304 char *base, *end;
5300 5305
5301 5306 if (subr == DIF_SUBR_INET_NTOP) {
5302 5307 af = (int)tupregs[0].dttk_value;
5303 5308 argi = 1;
5304 5309 } else {
5305 5310 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
5306 5311 argi = 0;
5307 5312 }
5308 5313
5309 5314 if (af == AF_INET) {
5310 5315 ipaddr_t ip4;
5311 5316 uint8_t *ptr8, val;
5312 5317
5313 5318 /*
5314 5319 * Safely load the IPv4 address.
5315 5320 */
5316 5321 ip4 = dtrace_load32(tupregs[argi].dttk_value);
5317 5322
5318 5323 /*
5319 5324 * Check an IPv4 string will fit in scratch.
5320 5325 */
5321 5326 size = INET_ADDRSTRLEN;
5322 5327 if (!DTRACE_INSCRATCH(mstate, size)) {
5323 5328 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5324 5329 regs[rd] = NULL;
5325 5330 break;
5326 5331 }
5327 5332 base = (char *)mstate->dtms_scratch_ptr;
5328 5333 end = (char *)mstate->dtms_scratch_ptr + size - 1;
5329 5334
5330 5335 /*
5331 5336 * Stringify as a dotted decimal quad.
5332 5337 */
5333 5338 *end-- = '\0';
5334 5339 ptr8 = (uint8_t *)&ip4;
5335 5340 for (i = 3; i >= 0; i--) {
5336 5341 val = ptr8[i];
5337 5342
5338 5343 if (val == 0) {
5339 5344 *end-- = '0';
5340 5345 } else {
5341 5346 for (; val; val /= 10) {
5342 5347 *end-- = '0' + (val % 10);
5343 5348 }
5344 5349 }
5345 5350
5346 5351 if (i > 0)
5347 5352 *end-- = '.';
5348 5353 }
5349 5354 ASSERT(end + 1 >= base);
5350 5355
5351 5356 } else if (af == AF_INET6) {
5352 5357 struct in6_addr ip6;
5353 5358 int firstzero, tryzero, numzero, v6end;
5354 5359 uint16_t val;
5355 5360 const char digits[] = "0123456789abcdef";
5356 5361
5357 5362 /*
5358 5363 * Stringify using RFC 1884 convention 2 - 16 bit
5359 5364 * hexadecimal values with a zero-run compression.
5360 5365 * Lower case hexadecimal digits are used.
5361 5366 * eg, fe80::214:4fff:fe0b:76c8.
5362 5367 * The IPv4 embedded form is returned for inet_ntop,
5363 5368 * just the IPv4 string is returned for inet_ntoa6.
5364 5369 */
5365 5370
5366 5371 /*
5367 5372 * Safely load the IPv6 address.
5368 5373 */
5369 5374 dtrace_bcopy(
5370 5375 (void *)(uintptr_t)tupregs[argi].dttk_value,
5371 5376 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
5372 5377
5373 5378 /*
5374 5379 * Check an IPv6 string will fit in scratch.
5375 5380 */
5376 5381 size = INET6_ADDRSTRLEN;
5377 5382 if (!DTRACE_INSCRATCH(mstate, size)) {
5378 5383 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5379 5384 regs[rd] = NULL;
5380 5385 break;
5381 5386 }
5382 5387 base = (char *)mstate->dtms_scratch_ptr;
5383 5388 end = (char *)mstate->dtms_scratch_ptr + size - 1;
5384 5389 *end-- = '\0';
5385 5390
5386 5391 /*
5387 5392 * Find the longest run of 16 bit zero values
5388 5393 * for the single allowed zero compression - "::".
5389 5394 */
5390 5395 firstzero = -1;
5391 5396 tryzero = -1;
5392 5397 numzero = 1;
5393 5398 for (i = 0; i < sizeof (struct in6_addr); i++) {
5394 5399 if (ip6._S6_un._S6_u8[i] == 0 &&
5395 5400 tryzero == -1 && i % 2 == 0) {
5396 5401 tryzero = i;
5397 5402 continue;
5398 5403 }
5399 5404
5400 5405 if (tryzero != -1 &&
5401 5406 (ip6._S6_un._S6_u8[i] != 0 ||
5402 5407 i == sizeof (struct in6_addr) - 1)) {
5403 5408
5404 5409 if (i - tryzero <= numzero) {
5405 5410 tryzero = -1;
5406 5411 continue;
5407 5412 }
5408 5413
5409 5414 firstzero = tryzero;
5410 5415 numzero = i - i % 2 - tryzero;
5411 5416 tryzero = -1;
5412 5417
5413 5418 if (ip6._S6_un._S6_u8[i] == 0 &&
5414 5419 i == sizeof (struct in6_addr) - 1)
5415 5420 numzero += 2;
5416 5421 }
5417 5422 }
5418 5423 ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
5419 5424
5420 5425 /*
5421 5426 * Check for an IPv4 embedded address.
5422 5427 */
5423 5428 v6end = sizeof (struct in6_addr) - 2;
5424 5429 if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
5425 5430 IN6_IS_ADDR_V4COMPAT(&ip6)) {
5426 5431 for (i = sizeof (struct in6_addr) - 1;
5427 5432 i >= DTRACE_V4MAPPED_OFFSET; i--) {
5428 5433 ASSERT(end >= base);
5429 5434
5430 5435 val = ip6._S6_un._S6_u8[i];
5431 5436
5432 5437 if (val == 0) {
5433 5438 *end-- = '0';
5434 5439 } else {
5435 5440 for (; val; val /= 10) {
5436 5441 *end-- = '0' + val % 10;
5437 5442 }
5438 5443 }
5439 5444
5440 5445 if (i > DTRACE_V4MAPPED_OFFSET)
5441 5446 *end-- = '.';
5442 5447 }
5443 5448
5444 5449 if (subr == DIF_SUBR_INET_NTOA6)
5445 5450 goto inetout;
5446 5451
5447 5452 /*
5448 5453 * Set v6end to skip the IPv4 address that
5449 5454 * we have already stringified.
5450 5455 */
5451 5456 v6end = 10;
5452 5457 }
5453 5458
5454 5459 /*
5455 5460 * Build the IPv6 string by working through the
5456 5461 * address in reverse.
5457 5462 */
5458 5463 for (i = v6end; i >= 0; i -= 2) {
5459 5464 ASSERT(end >= base);
5460 5465
5461 5466 if (i == firstzero + numzero - 2) {
5462 5467 *end-- = ':';
5463 5468 *end-- = ':';
5464 5469 i -= numzero - 2;
5465 5470 continue;
5466 5471 }
5467 5472
5468 5473 if (i < 14 && i != firstzero - 2)
5469 5474 *end-- = ':';
5470 5475
5471 5476 val = (ip6._S6_un._S6_u8[i] << 8) +
5472 5477 ip6._S6_un._S6_u8[i + 1];
5473 5478
5474 5479 if (val == 0) {
5475 5480 *end-- = '0';
5476 5481 } else {
5477 5482 for (; val; val /= 16) {
5478 5483 *end-- = digits[val % 16];
5479 5484 }
5480 5485 }
5481 5486 }
5482 5487 ASSERT(end + 1 >= base);
5483 5488
5484 5489 } else {
5485 5490 /*
5486 5491 * The user didn't use AH_INET or AH_INET6.
5487 5492 */
5488 5493 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5489 5494 regs[rd] = NULL;
5490 5495 break;
5491 5496 }
5492 5497
5493 5498 inetout: regs[rd] = (uintptr_t)end + 1;
5494 5499 mstate->dtms_scratch_ptr += size;
5495 5500 break;
5496 5501 }
5497 5502
5498 5503 }
5499 5504 }
5500 5505
5501 5506 /*
5502 5507 * Emulate the execution of DTrace IR instructions specified by the given
5503 5508 * DIF object. This function is deliberately void of assertions as all of
5504 5509 * the necessary checks are handled by a call to dtrace_difo_validate().
5505 5510 */
5506 5511 static uint64_t
5507 5512 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
5508 5513 dtrace_vstate_t *vstate, dtrace_state_t *state)
5509 5514 {
5510 5515 const dif_instr_t *text = difo->dtdo_buf;
5511 5516 const uint_t textlen = difo->dtdo_len;
5512 5517 const char *strtab = difo->dtdo_strtab;
5513 5518 const uint64_t *inttab = difo->dtdo_inttab;
5514 5519
5515 5520 uint64_t rval = 0;
5516 5521 dtrace_statvar_t *svar;
5517 5522 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
5518 5523 dtrace_difv_t *v;
5519 5524 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
5520 5525 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
5521 5526
5522 5527 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
5523 5528 uint64_t regs[DIF_DIR_NREGS];
5524 5529 uint64_t *tmp;
5525 5530
5526 5531 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
5527 5532 int64_t cc_r;
5528 5533 uint_t pc = 0, id, opc;
5529 5534 uint8_t ttop = 0;
5530 5535 dif_instr_t instr;
5531 5536 uint_t r1, r2, rd;
5532 5537
5533 5538 /*
5534 5539 * We stash the current DIF object into the machine state: we need it
5535 5540 * for subsequent access checking.
5536 5541 */
5537 5542 mstate->dtms_difo = difo;
5538 5543
5539 5544 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */
5540 5545
5541 5546 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
5542 5547 opc = pc;
5543 5548
5544 5549 instr = text[pc++];
5545 5550 r1 = DIF_INSTR_R1(instr);
5546 5551 r2 = DIF_INSTR_R2(instr);
5547 5552 rd = DIF_INSTR_RD(instr);
5548 5553
5549 5554 switch (DIF_INSTR_OP(instr)) {
5550 5555 case DIF_OP_OR:
5551 5556 regs[rd] = regs[r1] | regs[r2];
5552 5557 break;
5553 5558 case DIF_OP_XOR:
5554 5559 regs[rd] = regs[r1] ^ regs[r2];
5555 5560 break;
5556 5561 case DIF_OP_AND:
5557 5562 regs[rd] = regs[r1] & regs[r2];
5558 5563 break;
5559 5564 case DIF_OP_SLL:
5560 5565 regs[rd] = regs[r1] << regs[r2];
5561 5566 break;
5562 5567 case DIF_OP_SRL:
5563 5568 regs[rd] = regs[r1] >> regs[r2];
5564 5569 break;
5565 5570 case DIF_OP_SUB:
5566 5571 regs[rd] = regs[r1] - regs[r2];
5567 5572 break;
5568 5573 case DIF_OP_ADD:
5569 5574 regs[rd] = regs[r1] + regs[r2];
5570 5575 break;
5571 5576 case DIF_OP_MUL:
5572 5577 regs[rd] = regs[r1] * regs[r2];
5573 5578 break;
5574 5579 case DIF_OP_SDIV:
5575 5580 if (regs[r2] == 0) {
5576 5581 regs[rd] = 0;
5577 5582 *flags |= CPU_DTRACE_DIVZERO;
5578 5583 } else {
5579 5584 regs[rd] = (int64_t)regs[r1] /
5580 5585 (int64_t)regs[r2];
5581 5586 }
5582 5587 break;
5583 5588
5584 5589 case DIF_OP_UDIV:
5585 5590 if (regs[r2] == 0) {
5586 5591 regs[rd] = 0;
5587 5592 *flags |= CPU_DTRACE_DIVZERO;
5588 5593 } else {
5589 5594 regs[rd] = regs[r1] / regs[r2];
5590 5595 }
5591 5596 break;
5592 5597
5593 5598 case DIF_OP_SREM:
5594 5599 if (regs[r2] == 0) {
5595 5600 regs[rd] = 0;
5596 5601 *flags |= CPU_DTRACE_DIVZERO;
5597 5602 } else {
5598 5603 regs[rd] = (int64_t)regs[r1] %
5599 5604 (int64_t)regs[r2];
5600 5605 }
5601 5606 break;
5602 5607
5603 5608 case DIF_OP_UREM:
5604 5609 if (regs[r2] == 0) {
5605 5610 regs[rd] = 0;
5606 5611 *flags |= CPU_DTRACE_DIVZERO;
5607 5612 } else {
5608 5613 regs[rd] = regs[r1] % regs[r2];
5609 5614 }
5610 5615 break;
5611 5616
5612 5617 case DIF_OP_NOT:
5613 5618 regs[rd] = ~regs[r1];
5614 5619 break;
5615 5620 case DIF_OP_MOV:
5616 5621 regs[rd] = regs[r1];
5617 5622 break;
5618 5623 case DIF_OP_CMP:
5619 5624 cc_r = regs[r1] - regs[r2];
5620 5625 cc_n = cc_r < 0;
5621 5626 cc_z = cc_r == 0;
5622 5627 cc_v = 0;
5623 5628 cc_c = regs[r1] < regs[r2];
5624 5629 break;
5625 5630 case DIF_OP_TST:
5626 5631 cc_n = cc_v = cc_c = 0;
5627 5632 cc_z = regs[r1] == 0;
5628 5633 break;
5629 5634 case DIF_OP_BA:
5630 5635 pc = DIF_INSTR_LABEL(instr);
5631 5636 break;
5632 5637 case DIF_OP_BE:
5633 5638 if (cc_z)
5634 5639 pc = DIF_INSTR_LABEL(instr);
5635 5640 break;
5636 5641 case DIF_OP_BNE:
5637 5642 if (cc_z == 0)
5638 5643 pc = DIF_INSTR_LABEL(instr);
5639 5644 break;
5640 5645 case DIF_OP_BG:
5641 5646 if ((cc_z | (cc_n ^ cc_v)) == 0)
5642 5647 pc = DIF_INSTR_LABEL(instr);
5643 5648 break;
5644 5649 case DIF_OP_BGU:
5645 5650 if ((cc_c | cc_z) == 0)
5646 5651 pc = DIF_INSTR_LABEL(instr);
5647 5652 break;
5648 5653 case DIF_OP_BGE:
5649 5654 if ((cc_n ^ cc_v) == 0)
5650 5655 pc = DIF_INSTR_LABEL(instr);
5651 5656 break;
5652 5657 case DIF_OP_BGEU:
5653 5658 if (cc_c == 0)
5654 5659 pc = DIF_INSTR_LABEL(instr);
5655 5660 break;
5656 5661 case DIF_OP_BL:
5657 5662 if (cc_n ^ cc_v)
5658 5663 pc = DIF_INSTR_LABEL(instr);
5659 5664 break;
5660 5665 case DIF_OP_BLU:
5661 5666 if (cc_c)
5662 5667 pc = DIF_INSTR_LABEL(instr);
5663 5668 break;
5664 5669 case DIF_OP_BLE:
5665 5670 if (cc_z | (cc_n ^ cc_v))
5666 5671 pc = DIF_INSTR_LABEL(instr);
5667 5672 break;
5668 5673 case DIF_OP_BLEU:
5669 5674 if (cc_c | cc_z)
5670 5675 pc = DIF_INSTR_LABEL(instr);
5671 5676 break;
5672 5677 case DIF_OP_RLDSB:
5673 5678 if (!dtrace_canload(regs[r1], 1, mstate, vstate))
5674 5679 break;
5675 5680 /*FALLTHROUGH*/
5676 5681 case DIF_OP_LDSB:
5677 5682 regs[rd] = (int8_t)dtrace_load8(regs[r1]);
5678 5683 break;
5679 5684 case DIF_OP_RLDSH:
5680 5685 if (!dtrace_canload(regs[r1], 2, mstate, vstate))
5681 5686 break;
5682 5687 /*FALLTHROUGH*/
5683 5688 case DIF_OP_LDSH:
5684 5689 regs[rd] = (int16_t)dtrace_load16(regs[r1]);
5685 5690 break;
5686 5691 case DIF_OP_RLDSW:
5687 5692 if (!dtrace_canload(regs[r1], 4, mstate, vstate))
5688 5693 break;
5689 5694 /*FALLTHROUGH*/
5690 5695 case DIF_OP_LDSW:
5691 5696 regs[rd] = (int32_t)dtrace_load32(regs[r1]);
5692 5697 break;
5693 5698 case DIF_OP_RLDUB:
5694 5699 if (!dtrace_canload(regs[r1], 1, mstate, vstate))
5695 5700 break;
5696 5701 /*FALLTHROUGH*/
5697 5702 case DIF_OP_LDUB:
5698 5703 regs[rd] = dtrace_load8(regs[r1]);
5699 5704 break;
5700 5705 case DIF_OP_RLDUH:
5701 5706 if (!dtrace_canload(regs[r1], 2, mstate, vstate))
5702 5707 break;
5703 5708 /*FALLTHROUGH*/
5704 5709 case DIF_OP_LDUH:
5705 5710 regs[rd] = dtrace_load16(regs[r1]);
5706 5711 break;
5707 5712 case DIF_OP_RLDUW:
5708 5713 if (!dtrace_canload(regs[r1], 4, mstate, vstate))
5709 5714 break;
5710 5715 /*FALLTHROUGH*/
5711 5716 case DIF_OP_LDUW:
5712 5717 regs[rd] = dtrace_load32(regs[r1]);
5713 5718 break;
5714 5719 case DIF_OP_RLDX:
5715 5720 if (!dtrace_canload(regs[r1], 8, mstate, vstate))
5716 5721 break;
5717 5722 /*FALLTHROUGH*/
5718 5723 case DIF_OP_LDX:
5719 5724 regs[rd] = dtrace_load64(regs[r1]);
5720 5725 break;
5721 5726 case DIF_OP_ULDSB:
5722 5727 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5723 5728 regs[rd] = (int8_t)
5724 5729 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5725 5730 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5726 5731 break;
5727 5732 case DIF_OP_ULDSH:
5728 5733 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5729 5734 regs[rd] = (int16_t)
5730 5735 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5731 5736 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5732 5737 break;
5733 5738 case DIF_OP_ULDSW:
5734 5739 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5735 5740 regs[rd] = (int32_t)
5736 5741 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5737 5742 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5738 5743 break;
5739 5744 case DIF_OP_ULDUB:
5740 5745 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5741 5746 regs[rd] =
5742 5747 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5743 5748 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5744 5749 break;
5745 5750 case DIF_OP_ULDUH:
5746 5751 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5747 5752 regs[rd] =
5748 5753 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5749 5754 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5750 5755 break;
5751 5756 case DIF_OP_ULDUW:
5752 5757 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5753 5758 regs[rd] =
5754 5759 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5755 5760 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5756 5761 break;
5757 5762 case DIF_OP_ULDX:
5758 5763 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
5759 5764 regs[rd] =
5760 5765 dtrace_fuword64((void *)(uintptr_t)regs[r1]);
5761 5766 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
5762 5767 break;
5763 5768 case DIF_OP_RET:
5764 5769 rval = regs[rd];
5765 5770 pc = textlen;
5766 5771 break;
5767 5772 case DIF_OP_NOP:
5768 5773 break;
5769 5774 case DIF_OP_SETX:
5770 5775 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
5771 5776 break;
5772 5777 case DIF_OP_SETS:
5773 5778 regs[rd] = (uint64_t)(uintptr_t)
5774 5779 (strtab + DIF_INSTR_STRING(instr));
5775 5780 break;
5776 5781 case DIF_OP_SCMP: {
5777 5782 size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
5778 5783 uintptr_t s1 = regs[r1];
5779 5784 uintptr_t s2 = regs[r2];
5780 5785
5781 5786 if (s1 != NULL &&
5782 5787 !dtrace_strcanload(s1, sz, mstate, vstate))
5783 5788 break;
5784 5789 if (s2 != NULL &&
5785 5790 !dtrace_strcanload(s2, sz, mstate, vstate))
5786 5791 break;
5787 5792
5788 5793 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz);
5789 5794
5790 5795 cc_n = cc_r < 0;
5791 5796 cc_z = cc_r == 0;
5792 5797 cc_v = cc_c = 0;
5793 5798 break;
5794 5799 }
5795 5800 case DIF_OP_LDGA:
5796 5801 regs[rd] = dtrace_dif_variable(mstate, state,
5797 5802 r1, regs[r2]);
5798 5803 break;
5799 5804 case DIF_OP_LDGS:
5800 5805 id = DIF_INSTR_VAR(instr);
5801 5806
5802 5807 if (id >= DIF_VAR_OTHER_UBASE) {
5803 5808 uintptr_t a;
5804 5809
5805 5810 id -= DIF_VAR_OTHER_UBASE;
5806 5811 svar = vstate->dtvs_globals[id];
5807 5812 ASSERT(svar != NULL);
5808 5813 v = &svar->dtsv_var;
5809 5814
5810 5815 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
5811 5816 regs[rd] = svar->dtsv_data;
5812 5817 break;
5813 5818 }
5814 5819
5815 5820 a = (uintptr_t)svar->dtsv_data;
5816 5821
5817 5822 if (*(uint8_t *)a == UINT8_MAX) {
5818 5823 /*
5819 5824 * If the 0th byte is set to UINT8_MAX
5820 5825 * then this is to be treated as a
5821 5826 * reference to a NULL variable.
5822 5827 */
5823 5828 regs[rd] = NULL;
5824 5829 } else {
5825 5830 regs[rd] = a + sizeof (uint64_t);
5826 5831 }
5827 5832
5828 5833 break;
5829 5834 }
5830 5835
5831 5836 regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
5832 5837 break;
5833 5838
5834 5839 case DIF_OP_STGS:
5835 5840 id = DIF_INSTR_VAR(instr);
5836 5841
5837 5842 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5838 5843 id -= DIF_VAR_OTHER_UBASE;
5839 5844
5840 5845 svar = vstate->dtvs_globals[id];
5841 5846 ASSERT(svar != NULL);
5842 5847 v = &svar->dtsv_var;
5843 5848
5844 5849 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5845 5850 uintptr_t a = (uintptr_t)svar->dtsv_data;
5846 5851
5847 5852 ASSERT(a != NULL);
5848 5853 ASSERT(svar->dtsv_size != 0);
5849 5854
5850 5855 if (regs[rd] == NULL) {
5851 5856 *(uint8_t *)a = UINT8_MAX;
5852 5857 break;
5853 5858 } else {
5854 5859 *(uint8_t *)a = 0;
5855 5860 a += sizeof (uint64_t);
5856 5861 }
5857 5862 if (!dtrace_vcanload(
5858 5863 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5859 5864 mstate, vstate))
5860 5865 break;
5861 5866
5862 5867 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5863 5868 (void *)a, &v->dtdv_type);
5864 5869 break;
5865 5870 }
5866 5871
5867 5872 svar->dtsv_data = regs[rd];
5868 5873 break;
5869 5874
5870 5875 case DIF_OP_LDTA:
5871 5876 /*
5872 5877 * There are no DTrace built-in thread-local arrays at
5873 5878 * present. This opcode is saved for future work.
5874 5879 */
5875 5880 *flags |= CPU_DTRACE_ILLOP;
5876 5881 regs[rd] = 0;
5877 5882 break;
5878 5883
5879 5884 case DIF_OP_LDLS:
5880 5885 id = DIF_INSTR_VAR(instr);
5881 5886
5882 5887 if (id < DIF_VAR_OTHER_UBASE) {
5883 5888 /*
5884 5889 * For now, this has no meaning.
5885 5890 */
5886 5891 regs[rd] = 0;
5887 5892 break;
5888 5893 }
5889 5894
5890 5895 id -= DIF_VAR_OTHER_UBASE;
5891 5896
5892 5897 ASSERT(id < vstate->dtvs_nlocals);
5893 5898 ASSERT(vstate->dtvs_locals != NULL);
5894 5899
5895 5900 svar = vstate->dtvs_locals[id];
5896 5901 ASSERT(svar != NULL);
5897 5902 v = &svar->dtsv_var;
5898 5903
5899 5904 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5900 5905 uintptr_t a = (uintptr_t)svar->dtsv_data;
5901 5906 size_t sz = v->dtdv_type.dtdt_size;
5902 5907
5903 5908 sz += sizeof (uint64_t);
5904 5909 ASSERT(svar->dtsv_size == NCPU * sz);
5905 5910 a += CPU->cpu_id * sz;
5906 5911
5907 5912 if (*(uint8_t *)a == UINT8_MAX) {
5908 5913 /*
5909 5914 * If the 0th byte is set to UINT8_MAX
5910 5915 * then this is to be treated as a
5911 5916 * reference to a NULL variable.
5912 5917 */
5913 5918 regs[rd] = NULL;
5914 5919 } else {
5915 5920 regs[rd] = a + sizeof (uint64_t);
5916 5921 }
5917 5922
5918 5923 break;
5919 5924 }
5920 5925
5921 5926 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5922 5927 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5923 5928 regs[rd] = tmp[CPU->cpu_id];
5924 5929 break;
5925 5930
5926 5931 case DIF_OP_STLS:
5927 5932 id = DIF_INSTR_VAR(instr);
5928 5933
5929 5934 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5930 5935 id -= DIF_VAR_OTHER_UBASE;
5931 5936 ASSERT(id < vstate->dtvs_nlocals);
5932 5937
5933 5938 ASSERT(vstate->dtvs_locals != NULL);
5934 5939 svar = vstate->dtvs_locals[id];
5935 5940 ASSERT(svar != NULL);
5936 5941 v = &svar->dtsv_var;
5937 5942
5938 5943 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5939 5944 uintptr_t a = (uintptr_t)svar->dtsv_data;
5940 5945 size_t sz = v->dtdv_type.dtdt_size;
5941 5946
5942 5947 sz += sizeof (uint64_t);
5943 5948 ASSERT(svar->dtsv_size == NCPU * sz);
5944 5949 a += CPU->cpu_id * sz;
5945 5950
5946 5951 if (regs[rd] == NULL) {
5947 5952 *(uint8_t *)a = UINT8_MAX;
5948 5953 break;
5949 5954 } else {
5950 5955 *(uint8_t *)a = 0;
5951 5956 a += sizeof (uint64_t);
5952 5957 }
5953 5958
5954 5959 if (!dtrace_vcanload(
5955 5960 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5956 5961 mstate, vstate))
5957 5962 break;
5958 5963
5959 5964 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5960 5965 (void *)a, &v->dtdv_type);
5961 5966 break;
5962 5967 }
5963 5968
5964 5969 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5965 5970 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5966 5971 tmp[CPU->cpu_id] = regs[rd];
5967 5972 break;
5968 5973
5969 5974 case DIF_OP_LDTS: {
5970 5975 dtrace_dynvar_t *dvar;
5971 5976 dtrace_key_t *key;
5972 5977
5973 5978 id = DIF_INSTR_VAR(instr);
5974 5979 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5975 5980 id -= DIF_VAR_OTHER_UBASE;
5976 5981 v = &vstate->dtvs_tlocals[id];
5977 5982
5978 5983 key = &tupregs[DIF_DTR_NREGS];
5979 5984 key[0].dttk_value = (uint64_t)id;
5980 5985 key[0].dttk_size = 0;
5981 5986 DTRACE_TLS_THRKEY(key[1].dttk_value);
5982 5987 key[1].dttk_size = 0;
5983 5988
5984 5989 dvar = dtrace_dynvar(dstate, 2, key,
5985 5990 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
5986 5991 mstate, vstate);
5987 5992
5988 5993 if (dvar == NULL) {
5989 5994 regs[rd] = 0;
5990 5995 break;
5991 5996 }
5992 5997
5993 5998 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5994 5999 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
5995 6000 } else {
5996 6001 regs[rd] = *((uint64_t *)dvar->dtdv_data);
5997 6002 }
5998 6003
5999 6004 break;
6000 6005 }
6001 6006
6002 6007 case DIF_OP_STTS: {
6003 6008 dtrace_dynvar_t *dvar;
6004 6009 dtrace_key_t *key;
6005 6010
6006 6011 id = DIF_INSTR_VAR(instr);
6007 6012 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6008 6013 id -= DIF_VAR_OTHER_UBASE;
6009 6014
6010 6015 key = &tupregs[DIF_DTR_NREGS];
6011 6016 key[0].dttk_value = (uint64_t)id;
6012 6017 key[0].dttk_size = 0;
6013 6018 DTRACE_TLS_THRKEY(key[1].dttk_value);
6014 6019 key[1].dttk_size = 0;
6015 6020 v = &vstate->dtvs_tlocals[id];
6016 6021
6017 6022 dvar = dtrace_dynvar(dstate, 2, key,
6018 6023 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6019 6024 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6020 6025 regs[rd] ? DTRACE_DYNVAR_ALLOC :
6021 6026 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6022 6027
6023 6028 /*
6024 6029 * Given that we're storing to thread-local data,
6025 6030 * we need to flush our predicate cache.
6026 6031 */
6027 6032 curthread->t_predcache = NULL;
6028 6033
6029 6034 if (dvar == NULL)
6030 6035 break;
6031 6036
6032 6037 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6033 6038 if (!dtrace_vcanload(
6034 6039 (void *)(uintptr_t)regs[rd],
6035 6040 &v->dtdv_type, mstate, vstate))
6036 6041 break;
6037 6042
6038 6043 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6039 6044 dvar->dtdv_data, &v->dtdv_type);
6040 6045 } else {
6041 6046 *((uint64_t *)dvar->dtdv_data) = regs[rd];
6042 6047 }
6043 6048
6044 6049 break;
6045 6050 }
6046 6051
6047 6052 case DIF_OP_SRA:
6048 6053 regs[rd] = (int64_t)regs[r1] >> regs[r2];
6049 6054 break;
6050 6055
6051 6056 case DIF_OP_CALL:
6052 6057 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
6053 6058 regs, tupregs, ttop, mstate, state);
6054 6059 break;
6055 6060
6056 6061 case DIF_OP_PUSHTR:
6057 6062 if (ttop == DIF_DTR_NREGS) {
6058 6063 *flags |= CPU_DTRACE_TUPOFLOW;
6059 6064 break;
6060 6065 }
6061 6066
6062 6067 if (r1 == DIF_TYPE_STRING) {
6063 6068 /*
6064 6069 * If this is a string type and the size is 0,
6065 6070 * we'll use the system-wide default string
6066 6071 * size. Note that we are _not_ looking at
6067 6072 * the value of the DTRACEOPT_STRSIZE option;
6068 6073 * had this been set, we would expect to have
6069 6074 * a non-zero size value in the "pushtr".
6070 6075 */
6071 6076 tupregs[ttop].dttk_size =
6072 6077 dtrace_strlen((char *)(uintptr_t)regs[rd],
6073 6078 regs[r2] ? regs[r2] :
6074 6079 dtrace_strsize_default) + 1;
6075 6080 } else {
6076 6081 tupregs[ttop].dttk_size = regs[r2];
6077 6082 }
6078 6083
6079 6084 tupregs[ttop++].dttk_value = regs[rd];
6080 6085 break;
6081 6086
6082 6087 case DIF_OP_PUSHTV:
6083 6088 if (ttop == DIF_DTR_NREGS) {
6084 6089 *flags |= CPU_DTRACE_TUPOFLOW;
6085 6090 break;
6086 6091 }
6087 6092
6088 6093 tupregs[ttop].dttk_value = regs[rd];
6089 6094 tupregs[ttop++].dttk_size = 0;
6090 6095 break;
6091 6096
6092 6097 case DIF_OP_POPTS:
6093 6098 if (ttop != 0)
6094 6099 ttop--;
6095 6100 break;
6096 6101
6097 6102 case DIF_OP_FLUSHTS:
6098 6103 ttop = 0;
6099 6104 break;
6100 6105
6101 6106 case DIF_OP_LDGAA:
6102 6107 case DIF_OP_LDTAA: {
6103 6108 dtrace_dynvar_t *dvar;
6104 6109 dtrace_key_t *key = tupregs;
6105 6110 uint_t nkeys = ttop;
6106 6111
6107 6112 id = DIF_INSTR_VAR(instr);
6108 6113 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6109 6114 id -= DIF_VAR_OTHER_UBASE;
6110 6115
6111 6116 key[nkeys].dttk_value = (uint64_t)id;
6112 6117 key[nkeys++].dttk_size = 0;
6113 6118
6114 6119 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
6115 6120 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6116 6121 key[nkeys++].dttk_size = 0;
6117 6122 v = &vstate->dtvs_tlocals[id];
6118 6123 } else {
6119 6124 v = &vstate->dtvs_globals[id]->dtsv_var;
6120 6125 }
6121 6126
6122 6127 dvar = dtrace_dynvar(dstate, nkeys, key,
6123 6128 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6124 6129 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6125 6130 DTRACE_DYNVAR_NOALLOC, mstate, vstate);
6126 6131
6127 6132 if (dvar == NULL) {
6128 6133 regs[rd] = 0;
6129 6134 break;
6130 6135 }
6131 6136
6132 6137 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6133 6138 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6134 6139 } else {
6135 6140 regs[rd] = *((uint64_t *)dvar->dtdv_data);
6136 6141 }
6137 6142
6138 6143 break;
6139 6144 }
6140 6145
6141 6146 case DIF_OP_STGAA:
6142 6147 case DIF_OP_STTAA: {
6143 6148 dtrace_dynvar_t *dvar;
6144 6149 dtrace_key_t *key = tupregs;
6145 6150 uint_t nkeys = ttop;
6146 6151
6147 6152 id = DIF_INSTR_VAR(instr);
6148 6153 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6149 6154 id -= DIF_VAR_OTHER_UBASE;
6150 6155
6151 6156 key[nkeys].dttk_value = (uint64_t)id;
6152 6157 key[nkeys++].dttk_size = 0;
6153 6158
6154 6159 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
6155 6160 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6156 6161 key[nkeys++].dttk_size = 0;
6157 6162 v = &vstate->dtvs_tlocals[id];
6158 6163 } else {
6159 6164 v = &vstate->dtvs_globals[id]->dtsv_var;
6160 6165 }
6161 6166
6162 6167 dvar = dtrace_dynvar(dstate, nkeys, key,
6163 6168 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6164 6169 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6165 6170 regs[rd] ? DTRACE_DYNVAR_ALLOC :
6166 6171 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6167 6172
6168 6173 if (dvar == NULL)
6169 6174 break;
6170 6175
6171 6176 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6172 6177 if (!dtrace_vcanload(
6173 6178 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6174 6179 mstate, vstate))
6175 6180 break;
6176 6181
6177 6182 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6178 6183 dvar->dtdv_data, &v->dtdv_type);
6179 6184 } else {
6180 6185 *((uint64_t *)dvar->dtdv_data) = regs[rd];
6181 6186 }
6182 6187
6183 6188 break;
6184 6189 }
6185 6190
6186 6191 case DIF_OP_ALLOCS: {
6187 6192 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6188 6193 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
6189 6194
6190 6195 /*
6191 6196 * Rounding up the user allocation size could have
6192 6197 * overflowed large, bogus allocations (like -1ULL) to
6193 6198 * 0.
6194 6199 */
6195 6200 if (size < regs[r1] ||
6196 6201 !DTRACE_INSCRATCH(mstate, size)) {
6197 6202 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6198 6203 regs[rd] = NULL;
6199 6204 break;
6200 6205 }
6201 6206
6202 6207 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
6203 6208 mstate->dtms_scratch_ptr += size;
6204 6209 regs[rd] = ptr;
6205 6210 break;
6206 6211 }
6207 6212
6208 6213 case DIF_OP_COPYS:
6209 6214 if (!dtrace_canstore(regs[rd], regs[r2],
6210 6215 mstate, vstate)) {
6211 6216 *flags |= CPU_DTRACE_BADADDR;
6212 6217 *illval = regs[rd];
6213 6218 break;
6214 6219 }
6215 6220
6216 6221 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
6217 6222 break;
6218 6223
6219 6224 dtrace_bcopy((void *)(uintptr_t)regs[r1],
6220 6225 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
6221 6226 break;
6222 6227
6223 6228 case DIF_OP_STB:
6224 6229 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
6225 6230 *flags |= CPU_DTRACE_BADADDR;
6226 6231 *illval = regs[rd];
6227 6232 break;
6228 6233 }
6229 6234 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
6230 6235 break;
6231 6236
6232 6237 case DIF_OP_STH:
6233 6238 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
6234 6239 *flags |= CPU_DTRACE_BADADDR;
6235 6240 *illval = regs[rd];
6236 6241 break;
6237 6242 }
6238 6243 if (regs[rd] & 1) {
6239 6244 *flags |= CPU_DTRACE_BADALIGN;
6240 6245 *illval = regs[rd];
6241 6246 break;
6242 6247 }
6243 6248 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
6244 6249 break;
6245 6250
6246 6251 case DIF_OP_STW:
6247 6252 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
6248 6253 *flags |= CPU_DTRACE_BADADDR;
6249 6254 *illval = regs[rd];
6250 6255 break;
6251 6256 }
6252 6257 if (regs[rd] & 3) {
6253 6258 *flags |= CPU_DTRACE_BADALIGN;
6254 6259 *illval = regs[rd];
6255 6260 break;
6256 6261 }
6257 6262 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
6258 6263 break;
6259 6264
6260 6265 case DIF_OP_STX:
6261 6266 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
6262 6267 *flags |= CPU_DTRACE_BADADDR;
6263 6268 *illval = regs[rd];
6264 6269 break;
6265 6270 }
6266 6271 if (regs[rd] & 7) {
6267 6272 *flags |= CPU_DTRACE_BADALIGN;
6268 6273 *illval = regs[rd];
6269 6274 break;
6270 6275 }
6271 6276 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
6272 6277 break;
6273 6278 }
6274 6279 }
6275 6280
6276 6281 if (!(*flags & CPU_DTRACE_FAULT))
6277 6282 return (rval);
6278 6283
6279 6284 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
6280 6285 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
6281 6286
6282 6287 return (0);
6283 6288 }
6284 6289
6285 6290 static void
6286 6291 dtrace_action_breakpoint(dtrace_ecb_t *ecb)
6287 6292 {
6288 6293 dtrace_probe_t *probe = ecb->dte_probe;
6289 6294 dtrace_provider_t *prov = probe->dtpr_provider;
6290 6295 char c[DTRACE_FULLNAMELEN + 80], *str;
6291 6296 char *msg = "dtrace: breakpoint action at probe ";
6292 6297 char *ecbmsg = " (ecb ";
6293 6298 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
6294 6299 uintptr_t val = (uintptr_t)ecb;
6295 6300 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
6296 6301
6297 6302 if (dtrace_destructive_disallow)
6298 6303 return;
6299 6304
6300 6305 /*
6301 6306 * It's impossible to be taking action on the NULL probe.
6302 6307 */
6303 6308 ASSERT(probe != NULL);
6304 6309
6305 6310 /*
6306 6311 * This is a poor man's (destitute man's?) sprintf(): we want to
6307 6312 * print the provider name, module name, function name and name of
6308 6313 * the probe, along with the hex address of the ECB with the breakpoint
6309 6314 * action -- all of which we must place in the character buffer by
6310 6315 * hand.
6311 6316 */
6312 6317 while (*msg != '\0')
6313 6318 c[i++] = *msg++;
6314 6319
6315 6320 for (str = prov->dtpv_name; *str != '\0'; str++)
6316 6321 c[i++] = *str;
6317 6322 c[i++] = ':';
6318 6323
6319 6324 for (str = probe->dtpr_mod; *str != '\0'; str++)
6320 6325 c[i++] = *str;
6321 6326 c[i++] = ':';
6322 6327
6323 6328 for (str = probe->dtpr_func; *str != '\0'; str++)
6324 6329 c[i++] = *str;
6325 6330 c[i++] = ':';
6326 6331
6327 6332 for (str = probe->dtpr_name; *str != '\0'; str++)
6328 6333 c[i++] = *str;
6329 6334
6330 6335 while (*ecbmsg != '\0')
6331 6336 c[i++] = *ecbmsg++;
6332 6337
6333 6338 while (shift >= 0) {
6334 6339 mask = (uintptr_t)0xf << shift;
6335 6340
6336 6341 if (val >= ((uintptr_t)1 << shift))
6337 6342 c[i++] = "0123456789abcdef"[(val & mask) >> shift];
6338 6343 shift -= 4;
6339 6344 }
6340 6345
6341 6346 c[i++] = ')';
6342 6347 c[i] = '\0';
6343 6348
6344 6349 debug_enter(c);
6345 6350 }
6346 6351
6347 6352 static void
6348 6353 dtrace_action_panic(dtrace_ecb_t *ecb)
6349 6354 {
6350 6355 dtrace_probe_t *probe = ecb->dte_probe;
6351 6356
6352 6357 /*
6353 6358 * It's impossible to be taking action on the NULL probe.
6354 6359 */
6355 6360 ASSERT(probe != NULL);
6356 6361
6357 6362 if (dtrace_destructive_disallow)
6358 6363 return;
6359 6364
6360 6365 if (dtrace_panicked != NULL)
6361 6366 return;
6362 6367
6363 6368 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
6364 6369 return;
6365 6370
6366 6371 /*
6367 6372 * We won the right to panic. (We want to be sure that only one
6368 6373 * thread calls panic() from dtrace_probe(), and that panic() is
6369 6374 * called exactly once.)
6370 6375 */
6371 6376 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6372 6377 probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
6373 6378 probe->dtpr_func, probe->dtpr_name, (void *)ecb);
6374 6379 }
6375 6380
6376 6381 static void
6377 6382 dtrace_action_raise(uint64_t sig)
6378 6383 {
6379 6384 if (dtrace_destructive_disallow)
6380 6385 return;
6381 6386
6382 6387 if (sig >= NSIG) {
6383 6388 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6384 6389 return;
6385 6390 }
6386 6391
6387 6392 /*
6388 6393 * raise() has a queue depth of 1 -- we ignore all subsequent
6389 6394 * invocations of the raise() action.
6390 6395 */
6391 6396 if (curthread->t_dtrace_sig == 0)
6392 6397 curthread->t_dtrace_sig = (uint8_t)sig;
6393 6398
6394 6399 curthread->t_sig_check = 1;
6395 6400 aston(curthread);
6396 6401 }
6397 6402
6398 6403 static void
6399 6404 dtrace_action_stop(void)
6400 6405 {
6401 6406 if (dtrace_destructive_disallow)
6402 6407 return;
6403 6408
6404 6409 if (!curthread->t_dtrace_stop) {
6405 6410 curthread->t_dtrace_stop = 1;
6406 6411 curthread->t_sig_check = 1;
6407 6412 aston(curthread);
6408 6413 }
6409 6414 }
6410 6415
6411 6416 static void
6412 6417 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
6413 6418 {
6414 6419 hrtime_t now;
6415 6420 volatile uint16_t *flags;
6416 6421 cpu_t *cpu = CPU;
6417 6422
6418 6423 if (dtrace_destructive_disallow)
6419 6424 return;
6420 6425
6421 6426 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags;
6422 6427
6423 6428 now = dtrace_gethrtime();
6424 6429
6425 6430 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
6426 6431 /*
6427 6432 * We need to advance the mark to the current time.
6428 6433 */
6429 6434 cpu->cpu_dtrace_chillmark = now;
6430 6435 cpu->cpu_dtrace_chilled = 0;
6431 6436 }
6432 6437
6433 6438 /*
6434 6439 * Now check to see if the requested chill time would take us over
6435 6440 * the maximum amount of time allowed in the chill interval. (Or
6436 6441 * worse, if the calculation itself induces overflow.)
6437 6442 */
6438 6443 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
6439 6444 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
6440 6445 *flags |= CPU_DTRACE_ILLOP;
6441 6446 return;
6442 6447 }
6443 6448
6444 6449 while (dtrace_gethrtime() - now < val)
6445 6450 continue;
6446 6451
6447 6452 /*
6448 6453 * Normally, we assure that the value of the variable "timestamp" does
6449 6454 * not change within an ECB. The presence of chill() represents an
6450 6455 * exception to this rule, however.
6451 6456 */
6452 6457 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
6453 6458 cpu->cpu_dtrace_chilled += val;
6454 6459 }
6455 6460
6456 6461 static void
6457 6462 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
6458 6463 uint64_t *buf, uint64_t arg)
6459 6464 {
6460 6465 int nframes = DTRACE_USTACK_NFRAMES(arg);
6461 6466 int strsize = DTRACE_USTACK_STRSIZE(arg);
6462 6467 uint64_t *pcs = &buf[1], *fps;
6463 6468 char *str = (char *)&pcs[nframes];
6464 6469 int size, offs = 0, i, j;
6465 6470 uintptr_t old = mstate->dtms_scratch_ptr, saved;
6466 6471 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
6467 6472 char *sym;
6468 6473
6469 6474 /*
6470 6475 * Should be taking a faster path if string space has not been
6471 6476 * allocated.
6472 6477 */
6473 6478 ASSERT(strsize != 0);
6474 6479
6475 6480 /*
6476 6481 * We will first allocate some temporary space for the frame pointers.
6477 6482 */
6478 6483 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6479 6484 size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
6480 6485 (nframes * sizeof (uint64_t));
6481 6486
6482 6487 if (!DTRACE_INSCRATCH(mstate, size)) {
6483 6488 /*
6484 6489 * Not enough room for our frame pointers -- need to indicate
6485 6490 * that we ran out of scratch space.
6486 6491 */
6487 6492 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6488 6493 return;
6489 6494 }
6490 6495
6491 6496 mstate->dtms_scratch_ptr += size;
6492 6497 saved = mstate->dtms_scratch_ptr;
6493 6498
6494 6499 /*
6495 6500 * Now get a stack with both program counters and frame pointers.
6496 6501 */
6497 6502 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6498 6503 dtrace_getufpstack(buf, fps, nframes + 1);
6499 6504 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6500 6505
6501 6506 /*
6502 6507 * If that faulted, we're cooked.
6503 6508 */
6504 6509 if (*flags & CPU_DTRACE_FAULT)
6505 6510 goto out;
6506 6511
6507 6512 /*
6508 6513 * Now we want to walk up the stack, calling the USTACK helper. For
6509 6514 * each iteration, we restore the scratch pointer.
6510 6515 */
6511 6516 for (i = 0; i < nframes; i++) {
6512 6517 mstate->dtms_scratch_ptr = saved;
6513 6518
6514 6519 if (offs >= strsize)
6515 6520 break;
6516 6521
6517 6522 sym = (char *)(uintptr_t)dtrace_helper(
6518 6523 DTRACE_HELPER_ACTION_USTACK,
6519 6524 mstate, state, pcs[i], fps[i]);
6520 6525
6521 6526 /*
6522 6527 * If we faulted while running the helper, we're going to
6523 6528 * clear the fault and null out the corresponding string.
6524 6529 */
6525 6530 if (*flags & CPU_DTRACE_FAULT) {
6526 6531 *flags &= ~CPU_DTRACE_FAULT;
6527 6532 str[offs++] = '\0';
6528 6533 continue;
6529 6534 }
6530 6535
6531 6536 if (sym == NULL) {
6532 6537 str[offs++] = '\0';
6533 6538 continue;
6534 6539 }
6535 6540
6536 6541 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6537 6542
6538 6543 /*
6539 6544 * Now copy in the string that the helper returned to us.
6540 6545 */
6541 6546 for (j = 0; offs + j < strsize; j++) {
6542 6547 if ((str[offs + j] = sym[j]) == '\0')
6543 6548 break;
6544 6549 }
6545 6550
6546 6551 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6547 6552
6548 6553 offs += j + 1;
6549 6554 }
6550 6555
6551 6556 if (offs >= strsize) {
6552 6557 /*
6553 6558 * If we didn't have room for all of the strings, we don't
6554 6559 * abort processing -- this needn't be a fatal error -- but we
6555 6560 * still want to increment a counter (dts_stkstroverflows) to
6556 6561 * allow this condition to be warned about. (If this is from
6557 6562 * a jstack() action, it is easily tuned via jstackstrsize.)
6558 6563 */
6559 6564 dtrace_error(&state->dts_stkstroverflows);
6560 6565 }
6561 6566
6562 6567 while (offs < strsize)
6563 6568 str[offs++] = '\0';
6564 6569
6565 6570 out:
6566 6571 mstate->dtms_scratch_ptr = old;
6567 6572 }
6568 6573
6569 6574 static void
6570 6575 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size,
6571 6576 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind)
6572 6577 {
6573 6578 volatile uint16_t *flags;
6574 6579 uint64_t val = *valp;
6575 6580 size_t valoffs = *valoffsp;
6576 6581
6577 6582 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
6578 6583 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF);
6579 6584
6580 6585 /*
6581 6586 * If this is a string, we're going to only load until we find the zero
6582 6587 * byte -- after which we'll store zero bytes.
6583 6588 */
6584 6589 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
6585 6590 char c = '\0' + 1;
6586 6591 size_t s;
6587 6592
6588 6593 for (s = 0; s < size; s++) {
6589 6594 if (c != '\0' && dtkind == DIF_TF_BYREF) {
6590 6595 c = dtrace_load8(val++);
6591 6596 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) {
6592 6597 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6593 6598 c = dtrace_fuword8((void *)(uintptr_t)val++);
6594 6599 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6595 6600 if (*flags & CPU_DTRACE_FAULT)
6596 6601 break;
6597 6602 }
6598 6603
6599 6604 DTRACE_STORE(uint8_t, tomax, valoffs++, c);
6600 6605
6601 6606 if (c == '\0' && intuple)
6602 6607 break;
6603 6608 }
6604 6609 } else {
6605 6610 uint8_t c;
6606 6611 while (valoffs < end) {
6607 6612 if (dtkind == DIF_TF_BYREF) {
6608 6613 c = dtrace_load8(val++);
6609 6614 } else if (dtkind == DIF_TF_BYUREF) {
6610 6615 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6611 6616 c = dtrace_fuword8((void *)(uintptr_t)val++);
6612 6617 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6613 6618 if (*flags & CPU_DTRACE_FAULT)
6614 6619 break;
6615 6620 }
6616 6621
6617 6622 DTRACE_STORE(uint8_t, tomax,
6618 6623 valoffs++, c);
6619 6624 }
6620 6625 }
6621 6626
6622 6627 *valp = val;
6623 6628 *valoffsp = valoffs;
6624 6629 }
6625 6630
6626 6631 /*
6627 6632 * If you're looking for the epicenter of DTrace, you just found it. This
6628 6633 * is the function called by the provider to fire a probe -- from which all
6629 6634 * subsequent probe-context DTrace activity emanates.
6630 6635 */
6631 6636 void
6632 6637 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
6633 6638 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
6634 6639 {
6635 6640 processorid_t cpuid;
6636 6641 dtrace_icookie_t cookie;
6637 6642 dtrace_probe_t *probe;
6638 6643 dtrace_mstate_t mstate;
6639 6644 dtrace_ecb_t *ecb;
6640 6645 dtrace_action_t *act;
6641 6646 intptr_t offs;
6642 6647 size_t size;
6643 6648 int vtime, onintr;
6644 6649 volatile uint16_t *flags;
6645 6650 hrtime_t now, end;
6646 6651
6647 6652 /*
6648 6653 * Kick out immediately if this CPU is still being born (in which case
6649 6654 * curthread will be set to -1) or the current thread can't allow
6650 6655 * probes in its current context.
6651 6656 */
6652 6657 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
6653 6658 return;
6654 6659
6655 6660 cookie = dtrace_interrupt_disable();
6656 6661 probe = dtrace_probes[id - 1];
6657 6662 cpuid = CPU->cpu_id;
6658 6663 onintr = CPU_ON_INTR(CPU);
6659 6664
6660 6665 CPU->cpu_dtrace_probes++;
6661 6666
6662 6667 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
6663 6668 probe->dtpr_predcache == curthread->t_predcache) {
6664 6669 /*
6665 6670 * We have hit in the predicate cache; we know that
6666 6671 * this predicate would evaluate to be false.
6667 6672 */
6668 6673 dtrace_interrupt_enable(cookie);
6669 6674 return;
6670 6675 }
6671 6676
6672 6677 if (panic_quiesce) {
6673 6678 /*
6674 6679 * We don't trace anything if we're panicking.
6675 6680 */
6676 6681 dtrace_interrupt_enable(cookie);
6677 6682 return;
6678 6683 }
6679 6684
6680 6685 now = dtrace_gethrtime();
6681 6686 vtime = dtrace_vtime_references != 0;
6682 6687
6683 6688 if (vtime && curthread->t_dtrace_start)
6684 6689 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
6685 6690
6686 6691 mstate.dtms_difo = NULL;
6687 6692 mstate.dtms_probe = probe;
6688 6693 mstate.dtms_strtok = NULL;
6689 6694 mstate.dtms_arg[0] = arg0;
6690 6695 mstate.dtms_arg[1] = arg1;
6691 6696 mstate.dtms_arg[2] = arg2;
6692 6697 mstate.dtms_arg[3] = arg3;
6693 6698 mstate.dtms_arg[4] = arg4;
6694 6699
6695 6700 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
6696 6701
6697 6702 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
6698 6703 dtrace_predicate_t *pred = ecb->dte_predicate;
6699 6704 dtrace_state_t *state = ecb->dte_state;
6700 6705 dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
6701 6706 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
6702 6707 dtrace_vstate_t *vstate = &state->dts_vstate;
6703 6708 dtrace_provider_t *prov = probe->dtpr_provider;
6704 6709 uint64_t tracememsize = 0;
6705 6710 int committed = 0;
6706 6711 caddr_t tomax;
6707 6712
6708 6713 /*
6709 6714 * A little subtlety with the following (seemingly innocuous)
6710 6715 * declaration of the automatic 'val': by looking at the
6711 6716 * code, you might think that it could be declared in the
6712 6717 * action processing loop, below. (That is, it's only used in
6713 6718 * the action processing loop.) However, it must be declared
6714 6719 * out of that scope because in the case of DIF expression
6715 6720 * arguments to aggregating actions, one iteration of the
6716 6721 * action loop will use the last iteration's value.
6717 6722 */
6718 6723 #ifdef lint
6719 6724 uint64_t val = 0;
6720 6725 #else
6721 6726 uint64_t val;
6722 6727 #endif
6723 6728
6724 6729 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
6725 6730 mstate.dtms_access = DTRACE_ACCESS_ARGS | DTRACE_ACCESS_PROC;
6726 6731 mstate.dtms_getf = NULL;
6727 6732
6728 6733 *flags &= ~CPU_DTRACE_ERROR;
6729 6734
6730 6735 if (prov == dtrace_provider) {
6731 6736 /*
6732 6737 * If dtrace itself is the provider of this probe,
6733 6738 * we're only going to continue processing the ECB if
6734 6739 * arg0 (the dtrace_state_t) is equal to the ECB's
6735 6740 * creating state. (This prevents disjoint consumers
6736 6741 * from seeing one another's metaprobes.)
6737 6742 */
6738 6743 if (arg0 != (uint64_t)(uintptr_t)state)
6739 6744 continue;
6740 6745 }
6741 6746
6742 6747 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
6743 6748 /*
6744 6749 * We're not currently active. If our provider isn't
6745 6750 * the dtrace pseudo provider, we're not interested.
6746 6751 */
6747 6752 if (prov != dtrace_provider)
6748 6753 continue;
6749 6754
6750 6755 /*
6751 6756 * Now we must further check if we are in the BEGIN
6752 6757 * probe. If we are, we will only continue processing
6753 6758 * if we're still in WARMUP -- if one BEGIN enabling
6754 6759 * has invoked the exit() action, we don't want to
6755 6760 * evaluate subsequent BEGIN enablings.
6756 6761 */
6757 6762 if (probe->dtpr_id == dtrace_probeid_begin &&
6758 6763 state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
6759 6764 ASSERT(state->dts_activity ==
6760 6765 DTRACE_ACTIVITY_DRAINING);
6761 6766 continue;
6762 6767 }
6763 6768 }
6764 6769
6765 6770 if (ecb->dte_cond && !dtrace_priv_probe(state, &mstate, ecb))
6766 6771 continue;
6767 6772
6768 6773 if (now - state->dts_alive > dtrace_deadman_timeout) {
6769 6774 /*
6770 6775 * We seem to be dead. Unless we (a) have kernel
6771 6776 * destructive permissions (b) have explicitly enabled
6772 6777 * destructive actions and (c) destructive actions have
6773 6778 * not been disabled, we're going to transition into
6774 6779 * the KILLED state, from which no further processing
6775 6780 * on this state will be performed.
6776 6781 */
6777 6782 if (!dtrace_priv_kernel_destructive(state) ||
6778 6783 !state->dts_cred.dcr_destructive ||
6779 6784 dtrace_destructive_disallow) {
6780 6785 void *activity = &state->dts_activity;
6781 6786 dtrace_activity_t current;
6782 6787
6783 6788 do {
6784 6789 current = state->dts_activity;
6785 6790 } while (dtrace_cas32(activity, current,
6786 6791 DTRACE_ACTIVITY_KILLED) != current);
6787 6792
6788 6793 continue;
6789 6794 }
6790 6795 }
6791 6796
6792 6797 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
6793 6798 ecb->dte_alignment, state, &mstate)) < 0)
6794 6799 continue;
6795 6800
6796 6801 tomax = buf->dtb_tomax;
6797 6802 ASSERT(tomax != NULL);
6798 6803
6799 6804 if (ecb->dte_size != 0) {
6800 6805 dtrace_rechdr_t dtrh;
6801 6806 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
6802 6807 mstate.dtms_timestamp = dtrace_gethrtime();
6803 6808 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
6804 6809 }
6805 6810 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t));
6806 6811 dtrh.dtrh_epid = ecb->dte_epid;
6807 6812 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh,
6808 6813 mstate.dtms_timestamp);
6809 6814 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh;
6810 6815 }
6811 6816
6812 6817 mstate.dtms_epid = ecb->dte_epid;
6813 6818 mstate.dtms_present |= DTRACE_MSTATE_EPID;
6814 6819
6815 6820 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
6816 6821 mstate.dtms_access |= DTRACE_ACCESS_KERNEL;
6817 6822
6818 6823 if (pred != NULL) {
6819 6824 dtrace_difo_t *dp = pred->dtp_difo;
6820 6825 int rval;
6821 6826
6822 6827 rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
6823 6828
6824 6829 if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
6825 6830 dtrace_cacheid_t cid = probe->dtpr_predcache;
6826 6831
6827 6832 if (cid != DTRACE_CACHEIDNONE && !onintr) {
6828 6833 /*
6829 6834 * Update the predicate cache...
6830 6835 */
6831 6836 ASSERT(cid == pred->dtp_cacheid);
6832 6837 curthread->t_predcache = cid;
6833 6838 }
6834 6839
6835 6840 continue;
6836 6841 }
6837 6842 }
6838 6843
6839 6844 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
6840 6845 act != NULL; act = act->dta_next) {
6841 6846 size_t valoffs;
6842 6847 dtrace_difo_t *dp;
6843 6848 dtrace_recdesc_t *rec = &act->dta_rec;
6844 6849
6845 6850 size = rec->dtrd_size;
6846 6851 valoffs = offs + rec->dtrd_offset;
6847 6852
6848 6853 if (DTRACEACT_ISAGG(act->dta_kind)) {
6849 6854 uint64_t v = 0xbad;
6850 6855 dtrace_aggregation_t *agg;
6851 6856
6852 6857 agg = (dtrace_aggregation_t *)act;
6853 6858
6854 6859 if ((dp = act->dta_difo) != NULL)
6855 6860 v = dtrace_dif_emulate(dp,
6856 6861 &mstate, vstate, state);
6857 6862
6858 6863 if (*flags & CPU_DTRACE_ERROR)
6859 6864 continue;
6860 6865
6861 6866 /*
6862 6867 * Note that we always pass the expression
6863 6868 * value from the previous iteration of the
6864 6869 * action loop. This value will only be used
6865 6870 * if there is an expression argument to the
6866 6871 * aggregating action, denoted by the
6867 6872 * dtag_hasarg field.
6868 6873 */
6869 6874 dtrace_aggregate(agg, buf,
6870 6875 offs, aggbuf, v, val);
6871 6876 continue;
6872 6877 }
6873 6878
6874 6879 switch (act->dta_kind) {
6875 6880 case DTRACEACT_STOP:
6876 6881 if (dtrace_priv_proc_destructive(state,
6877 6882 &mstate))
6878 6883 dtrace_action_stop();
6879 6884 continue;
6880 6885
6881 6886 case DTRACEACT_BREAKPOINT:
6882 6887 if (dtrace_priv_kernel_destructive(state))
6883 6888 dtrace_action_breakpoint(ecb);
6884 6889 continue;
6885 6890
6886 6891 case DTRACEACT_PANIC:
6887 6892 if (dtrace_priv_kernel_destructive(state))
6888 6893 dtrace_action_panic(ecb);
6889 6894 continue;
6890 6895
6891 6896 case DTRACEACT_STACK:
6892 6897 if (!dtrace_priv_kernel(state))
6893 6898 continue;
6894 6899
6895 6900 dtrace_getpcstack((pc_t *)(tomax + valoffs),
6896 6901 size / sizeof (pc_t), probe->dtpr_aframes,
6897 6902 DTRACE_ANCHORED(probe) ? NULL :
6898 6903 (uint32_t *)arg0);
6899 6904
6900 6905 continue;
6901 6906
6902 6907 case DTRACEACT_JSTACK:
6903 6908 case DTRACEACT_USTACK:
6904 6909 if (!dtrace_priv_proc(state, &mstate))
6905 6910 continue;
6906 6911
6907 6912 /*
6908 6913 * See comment in DIF_VAR_PID.
6909 6914 */
6910 6915 if (DTRACE_ANCHORED(mstate.dtms_probe) &&
6911 6916 CPU_ON_INTR(CPU)) {
6912 6917 int depth = DTRACE_USTACK_NFRAMES(
6913 6918 rec->dtrd_arg) + 1;
6914 6919
6915 6920 dtrace_bzero((void *)(tomax + valoffs),
6916 6921 DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
6917 6922 + depth * sizeof (uint64_t));
6918 6923
6919 6924 continue;
6920 6925 }
6921 6926
6922 6927 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
6923 6928 curproc->p_dtrace_helpers != NULL) {
6924 6929 /*
6925 6930 * This is the slow path -- we have
6926 6931 * allocated string space, and we're
6927 6932 * getting the stack of a process that
6928 6933 * has helpers. Call into a separate
6929 6934 * routine to perform this processing.
6930 6935 */
6931 6936 dtrace_action_ustack(&mstate, state,
6932 6937 (uint64_t *)(tomax + valoffs),
6933 6938 rec->dtrd_arg);
6934 6939 continue;
6935 6940 }
6936 6941
6937 6942 /*
6938 6943 * Clear the string space, since there's no
6939 6944 * helper to do it for us.
6940 6945 */
6941 6946 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0) {
6942 6947 int depth = DTRACE_USTACK_NFRAMES(
6943 6948 rec->dtrd_arg);
6944 6949 size_t strsize = DTRACE_USTACK_STRSIZE(
6945 6950 rec->dtrd_arg);
6946 6951 uint64_t *buf = (uint64_t *)(tomax +
6947 6952 valoffs);
6948 6953 void *strspace = &buf[depth + 1];
6949 6954
6950 6955 dtrace_bzero(strspace,
6951 6956 MIN(depth, strsize));
6952 6957 }
6953 6958
6954 6959 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6955 6960 dtrace_getupcstack((uint64_t *)
6956 6961 (tomax + valoffs),
6957 6962 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
6958 6963 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6959 6964 continue;
6960 6965
6961 6966 default:
6962 6967 break;
6963 6968 }
6964 6969
6965 6970 dp = act->dta_difo;
6966 6971 ASSERT(dp != NULL);
6967 6972
6968 6973 val = dtrace_dif_emulate(dp, &mstate, vstate, state);
6969 6974
6970 6975 if (*flags & CPU_DTRACE_ERROR)
6971 6976 continue;
6972 6977
6973 6978 switch (act->dta_kind) {
6974 6979 case DTRACEACT_SPECULATE: {
6975 6980 dtrace_rechdr_t *dtrh;
6976 6981
6977 6982 ASSERT(buf == &state->dts_buffer[cpuid]);
6978 6983 buf = dtrace_speculation_buffer(state,
6979 6984 cpuid, val);
6980 6985
6981 6986 if (buf == NULL) {
6982 6987 *flags |= CPU_DTRACE_DROP;
6983 6988 continue;
6984 6989 }
6985 6990
6986 6991 offs = dtrace_buffer_reserve(buf,
6987 6992 ecb->dte_needed, ecb->dte_alignment,
6988 6993 state, NULL);
6989 6994
6990 6995 if (offs < 0) {
6991 6996 *flags |= CPU_DTRACE_DROP;
6992 6997 continue;
6993 6998 }
6994 6999
6995 7000 tomax = buf->dtb_tomax;
6996 7001 ASSERT(tomax != NULL);
6997 7002
6998 7003 if (ecb->dte_size == 0)
6999 7004 continue;
7000 7005
7001 7006 ASSERT3U(ecb->dte_size, >=,
7002 7007 sizeof (dtrace_rechdr_t));
7003 7008 dtrh = ((void *)(tomax + offs));
7004 7009 dtrh->dtrh_epid = ecb->dte_epid;
7005 7010 /*
7006 7011 * When the speculation is committed, all of
7007 7012 * the records in the speculative buffer will
7008 7013 * have their timestamps set to the commit
7009 7014 * time. Until then, it is set to a sentinel
7010 7015 * value, for debugability.
7011 7016 */
7012 7017 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
7013 7018 continue;
7014 7019 }
7015 7020
7016 7021 case DTRACEACT_CHILL:
7017 7022 if (dtrace_priv_kernel_destructive(state))
7018 7023 dtrace_action_chill(&mstate, val);
7019 7024 continue;
7020 7025
7021 7026 case DTRACEACT_RAISE:
7022 7027 if (dtrace_priv_proc_destructive(state,
7023 7028 &mstate))
7024 7029 dtrace_action_raise(val);
7025 7030 continue;
7026 7031
7027 7032 case DTRACEACT_COMMIT:
7028 7033 ASSERT(!committed);
7029 7034
7030 7035 /*
7031 7036 * We need to commit our buffer state.
7032 7037 */
7033 7038 if (ecb->dte_size)
7034 7039 buf->dtb_offset = offs + ecb->dte_size;
7035 7040 buf = &state->dts_buffer[cpuid];
7036 7041 dtrace_speculation_commit(state, cpuid, val);
7037 7042 committed = 1;
7038 7043 continue;
7039 7044
7040 7045 case DTRACEACT_DISCARD:
7041 7046 dtrace_speculation_discard(state, cpuid, val);
7042 7047 continue;
7043 7048
7044 7049 case DTRACEACT_DIFEXPR:
7045 7050 case DTRACEACT_LIBACT:
7046 7051 case DTRACEACT_PRINTF:
7047 7052 case DTRACEACT_PRINTA:
7048 7053 case DTRACEACT_SYSTEM:
7049 7054 case DTRACEACT_FREOPEN:
7050 7055 case DTRACEACT_TRACEMEM:
7051 7056 break;
7052 7057
7053 7058 case DTRACEACT_TRACEMEM_DYNSIZE:
7054 7059 tracememsize = val;
7055 7060 break;
7056 7061
7057 7062 case DTRACEACT_SYM:
7058 7063 case DTRACEACT_MOD:
7059 7064 if (!dtrace_priv_kernel(state))
7060 7065 continue;
7061 7066 break;
7062 7067
7063 7068 case DTRACEACT_USYM:
7064 7069 case DTRACEACT_UMOD:
7065 7070 case DTRACEACT_UADDR: {
7066 7071 struct pid *pid = curthread->t_procp->p_pidp;
7067 7072
7068 7073 if (!dtrace_priv_proc(state, &mstate))
7069 7074 continue;
7070 7075
7071 7076 DTRACE_STORE(uint64_t, tomax,
7072 7077 valoffs, (uint64_t)pid->pid_id);
7073 7078 DTRACE_STORE(uint64_t, tomax,
7074 7079 valoffs + sizeof (uint64_t), val);
7075 7080
7076 7081 continue;
7077 7082 }
7078 7083
7079 7084 case DTRACEACT_EXIT: {
7080 7085 /*
7081 7086 * For the exit action, we are going to attempt
7082 7087 * to atomically set our activity to be
7083 7088 * draining. If this fails (either because
7084 7089 * another CPU has beat us to the exit action,
7085 7090 * or because our current activity is something
7086 7091 * other than ACTIVE or WARMUP), we will
7087 7092 * continue. This assures that the exit action
7088 7093 * can be successfully recorded at most once
7089 7094 * when we're in the ACTIVE state. If we're
7090 7095 * encountering the exit() action while in
7091 7096 * COOLDOWN, however, we want to honor the new
7092 7097 * status code. (We know that we're the only
7093 7098 * thread in COOLDOWN, so there is no race.)
7094 7099 */
7095 7100 void *activity = &state->dts_activity;
7096 7101 dtrace_activity_t current = state->dts_activity;
7097 7102
7098 7103 if (current == DTRACE_ACTIVITY_COOLDOWN)
7099 7104 break;
7100 7105
7101 7106 if (current != DTRACE_ACTIVITY_WARMUP)
7102 7107 current = DTRACE_ACTIVITY_ACTIVE;
7103 7108
7104 7109 if (dtrace_cas32(activity, current,
7105 7110 DTRACE_ACTIVITY_DRAINING) != current) {
7106 7111 *flags |= CPU_DTRACE_DROP;
7107 7112 continue;
7108 7113 }
7109 7114
7110 7115 break;
7111 7116 }
7112 7117
7113 7118 default:
7114 7119 ASSERT(0);
7115 7120 }
7116 7121
7117 7122 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ||
7118 7123 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) {
7119 7124 uintptr_t end = valoffs + size;
7120 7125
7121 7126 if (tracememsize != 0 &&
7122 7127 valoffs + tracememsize < end) {
7123 7128 end = valoffs + tracememsize;
7124 7129 tracememsize = 0;
7125 7130 }
7126 7131
7127 7132 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF &&
7128 7133 !dtrace_vcanload((void *)(uintptr_t)val,
7129 7134 &dp->dtdo_rtype, &mstate, vstate))
7130 7135 continue;
7131 7136
7132 7137 dtrace_store_by_ref(dp, tomax, size, &valoffs,
7133 7138 &val, end, act->dta_intuple,
7134 7139 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ?
7135 7140 DIF_TF_BYREF: DIF_TF_BYUREF);
7136 7141 continue;
7137 7142 }
7138 7143
7139 7144 switch (size) {
7140 7145 case 0:
7141 7146 break;
7142 7147
7143 7148 case sizeof (uint8_t):
7144 7149 DTRACE_STORE(uint8_t, tomax, valoffs, val);
7145 7150 break;
7146 7151 case sizeof (uint16_t):
7147 7152 DTRACE_STORE(uint16_t, tomax, valoffs, val);
7148 7153 break;
7149 7154 case sizeof (uint32_t):
7150 7155 DTRACE_STORE(uint32_t, tomax, valoffs, val);
7151 7156 break;
7152 7157 case sizeof (uint64_t):
7153 7158 DTRACE_STORE(uint64_t, tomax, valoffs, val);
7154 7159 break;
7155 7160 default:
7156 7161 /*
7157 7162 * Any other size should have been returned by
7158 7163 * reference, not by value.
7159 7164 */
7160 7165 ASSERT(0);
7161 7166 break;
7162 7167 }
7163 7168 }
7164 7169
7165 7170 if (*flags & CPU_DTRACE_DROP)
7166 7171 continue;
7167 7172
7168 7173 if (*flags & CPU_DTRACE_FAULT) {
7169 7174 int ndx;
7170 7175 dtrace_action_t *err;
7171 7176
7172 7177 buf->dtb_errors++;
7173 7178
7174 7179 if (probe->dtpr_id == dtrace_probeid_error) {
7175 7180 /*
7176 7181 * There's nothing we can do -- we had an
7177 7182 * error on the error probe. We bump an
7178 7183 * error counter to at least indicate that
7179 7184 * this condition happened.
7180 7185 */
7181 7186 dtrace_error(&state->dts_dblerrors);
7182 7187 continue;
7183 7188 }
7184 7189
7185 7190 if (vtime) {
7186 7191 /*
7187 7192 * Before recursing on dtrace_probe(), we
7188 7193 * need to explicitly clear out our start
7189 7194 * time to prevent it from being accumulated
7190 7195 * into t_dtrace_vtime.
7191 7196 */
7192 7197 curthread->t_dtrace_start = 0;
7193 7198 }
7194 7199
7195 7200 /*
7196 7201 * Iterate over the actions to figure out which action
7197 7202 * we were processing when we experienced the error.
7198 7203 * Note that act points _past_ the faulting action; if
7199 7204 * act is ecb->dte_action, the fault was in the
7200 7205 * predicate, if it's ecb->dte_action->dta_next it's
7201 7206 * in action #1, and so on.
7202 7207 */
7203 7208 for (err = ecb->dte_action, ndx = 0;
7204 7209 err != act; err = err->dta_next, ndx++)
7205 7210 continue;
7206 7211
7207 7212 dtrace_probe_error(state, ecb->dte_epid, ndx,
7208 7213 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
7209 7214 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
7210 7215 cpu_core[cpuid].cpuc_dtrace_illval);
7211 7216
7212 7217 continue;
7213 7218 }
7214 7219
7215 7220 if (!committed)
7216 7221 buf->dtb_offset = offs + ecb->dte_size;
7217 7222 }
7218 7223
7219 7224 end = dtrace_gethrtime();
7220 7225 if (vtime)
7221 7226 curthread->t_dtrace_start = end;
7222 7227
7223 7228 CPU->cpu_dtrace_nsec += end - now;
7224 7229
7225 7230 dtrace_interrupt_enable(cookie);
7226 7231 }
7227 7232
7228 7233 /*
7229 7234 * DTrace Probe Hashing Functions
7230 7235 *
7231 7236 * The functions in this section (and indeed, the functions in remaining
7232 7237 * sections) are not _called_ from probe context. (Any exceptions to this are
7233 7238 * marked with a "Note:".) Rather, they are called from elsewhere in the
7234 7239 * DTrace framework to look-up probes in, add probes to and remove probes from
7235 7240 * the DTrace probe hashes. (Each probe is hashed by each element of the
7236 7241 * probe tuple -- allowing for fast lookups, regardless of what was
7237 7242 * specified.)
7238 7243 */
7239 7244 static uint_t
7240 7245 dtrace_hash_str(char *p)
7241 7246 {
7242 7247 unsigned int g;
7243 7248 uint_t hval = 0;
7244 7249
7245 7250 while (*p) {
7246 7251 hval = (hval << 4) + *p++;
7247 7252 if ((g = (hval & 0xf0000000)) != 0)
7248 7253 hval ^= g >> 24;
7249 7254 hval &= ~g;
7250 7255 }
7251 7256 return (hval);
7252 7257 }
7253 7258
7254 7259 static dtrace_hash_t *
7255 7260 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
7256 7261 {
7257 7262 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
7258 7263
7259 7264 hash->dth_stroffs = stroffs;
7260 7265 hash->dth_nextoffs = nextoffs;
7261 7266 hash->dth_prevoffs = prevoffs;
7262 7267
7263 7268 hash->dth_size = 1;
7264 7269 hash->dth_mask = hash->dth_size - 1;
7265 7270
7266 7271 hash->dth_tab = kmem_zalloc(hash->dth_size *
7267 7272 sizeof (dtrace_hashbucket_t *), KM_SLEEP);
7268 7273
7269 7274 return (hash);
7270 7275 }
7271 7276
7272 7277 static void
7273 7278 dtrace_hash_destroy(dtrace_hash_t *hash)
7274 7279 {
7275 7280 #ifdef DEBUG
7276 7281 int i;
7277 7282
7278 7283 for (i = 0; i < hash->dth_size; i++)
7279 7284 ASSERT(hash->dth_tab[i] == NULL);
7280 7285 #endif
7281 7286
7282 7287 kmem_free(hash->dth_tab,
7283 7288 hash->dth_size * sizeof (dtrace_hashbucket_t *));
7284 7289 kmem_free(hash, sizeof (dtrace_hash_t));
7285 7290 }
7286 7291
7287 7292 static void
7288 7293 dtrace_hash_resize(dtrace_hash_t *hash)
7289 7294 {
7290 7295 int size = hash->dth_size, i, ndx;
7291 7296 int new_size = hash->dth_size << 1;
7292 7297 int new_mask = new_size - 1;
7293 7298 dtrace_hashbucket_t **new_tab, *bucket, *next;
7294 7299
7295 7300 ASSERT((new_size & new_mask) == 0);
7296 7301
7297 7302 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
7298 7303
7299 7304 for (i = 0; i < size; i++) {
7300 7305 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
7301 7306 dtrace_probe_t *probe = bucket->dthb_chain;
7302 7307
7303 7308 ASSERT(probe != NULL);
7304 7309 ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
7305 7310
7306 7311 next = bucket->dthb_next;
7307 7312 bucket->dthb_next = new_tab[ndx];
7308 7313 new_tab[ndx] = bucket;
7309 7314 }
7310 7315 }
7311 7316
7312 7317 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
7313 7318 hash->dth_tab = new_tab;
7314 7319 hash->dth_size = new_size;
7315 7320 hash->dth_mask = new_mask;
7316 7321 }
7317 7322
7318 7323 static void
7319 7324 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
7320 7325 {
7321 7326 int hashval = DTRACE_HASHSTR(hash, new);
7322 7327 int ndx = hashval & hash->dth_mask;
7323 7328 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7324 7329 dtrace_probe_t **nextp, **prevp;
7325 7330
7326 7331 for (; bucket != NULL; bucket = bucket->dthb_next) {
7327 7332 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
7328 7333 goto add;
7329 7334 }
7330 7335
7331 7336 if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
7332 7337 dtrace_hash_resize(hash);
7333 7338 dtrace_hash_add(hash, new);
7334 7339 return;
7335 7340 }
7336 7341
7337 7342 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
7338 7343 bucket->dthb_next = hash->dth_tab[ndx];
7339 7344 hash->dth_tab[ndx] = bucket;
7340 7345 hash->dth_nbuckets++;
7341 7346
7342 7347 add:
7343 7348 nextp = DTRACE_HASHNEXT(hash, new);
7344 7349 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
7345 7350 *nextp = bucket->dthb_chain;
7346 7351
7347 7352 if (bucket->dthb_chain != NULL) {
7348 7353 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
7349 7354 ASSERT(*prevp == NULL);
7350 7355 *prevp = new;
7351 7356 }
7352 7357
7353 7358 bucket->dthb_chain = new;
7354 7359 bucket->dthb_len++;
7355 7360 }
7356 7361
7357 7362 static dtrace_probe_t *
7358 7363 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
7359 7364 {
7360 7365 int hashval = DTRACE_HASHSTR(hash, template);
7361 7366 int ndx = hashval & hash->dth_mask;
7362 7367 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7363 7368
7364 7369 for (; bucket != NULL; bucket = bucket->dthb_next) {
7365 7370 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
7366 7371 return (bucket->dthb_chain);
7367 7372 }
7368 7373
7369 7374 return (NULL);
7370 7375 }
7371 7376
7372 7377 static int
7373 7378 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
7374 7379 {
7375 7380 int hashval = DTRACE_HASHSTR(hash, template);
7376 7381 int ndx = hashval & hash->dth_mask;
7377 7382 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7378 7383
7379 7384 for (; bucket != NULL; bucket = bucket->dthb_next) {
7380 7385 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
7381 7386 return (bucket->dthb_len);
7382 7387 }
7383 7388
7384 7389 return (NULL);
7385 7390 }
7386 7391
7387 7392 static void
7388 7393 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
7389 7394 {
7390 7395 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
7391 7396 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7392 7397
7393 7398 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
7394 7399 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
7395 7400
7396 7401 /*
7397 7402 * Find the bucket that we're removing this probe from.
7398 7403 */
7399 7404 for (; bucket != NULL; bucket = bucket->dthb_next) {
7400 7405 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
7401 7406 break;
7402 7407 }
7403 7408
7404 7409 ASSERT(bucket != NULL);
7405 7410
7406 7411 if (*prevp == NULL) {
7407 7412 if (*nextp == NULL) {
7408 7413 /*
7409 7414 * The removed probe was the only probe on this
7410 7415 * bucket; we need to remove the bucket.
7411 7416 */
7412 7417 dtrace_hashbucket_t *b = hash->dth_tab[ndx];
7413 7418
7414 7419 ASSERT(bucket->dthb_chain == probe);
7415 7420 ASSERT(b != NULL);
7416 7421
7417 7422 if (b == bucket) {
7418 7423 hash->dth_tab[ndx] = bucket->dthb_next;
7419 7424 } else {
7420 7425 while (b->dthb_next != bucket)
7421 7426 b = b->dthb_next;
7422 7427 b->dthb_next = bucket->dthb_next;
7423 7428 }
7424 7429
7425 7430 ASSERT(hash->dth_nbuckets > 0);
7426 7431 hash->dth_nbuckets--;
7427 7432 kmem_free(bucket, sizeof (dtrace_hashbucket_t));
7428 7433 return;
7429 7434 }
7430 7435
7431 7436 bucket->dthb_chain = *nextp;
7432 7437 } else {
7433 7438 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
7434 7439 }
7435 7440
7436 7441 if (*nextp != NULL)
7437 7442 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
7438 7443 }
7439 7444
7440 7445 /*
7441 7446 * DTrace Utility Functions
7442 7447 *
7443 7448 * These are random utility functions that are _not_ called from probe context.
7444 7449 */
7445 7450 static int
7446 7451 dtrace_badattr(const dtrace_attribute_t *a)
7447 7452 {
7448 7453 return (a->dtat_name > DTRACE_STABILITY_MAX ||
7449 7454 a->dtat_data > DTRACE_STABILITY_MAX ||
7450 7455 a->dtat_class > DTRACE_CLASS_MAX);
7451 7456 }
7452 7457
7453 7458 /*
7454 7459 * Return a duplicate copy of a string. If the specified string is NULL,
7455 7460 * this function returns a zero-length string.
7456 7461 */
7457 7462 static char *
7458 7463 dtrace_strdup(const char *str)
7459 7464 {
7460 7465 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
7461 7466
7462 7467 if (str != NULL)
7463 7468 (void) strcpy(new, str);
7464 7469
7465 7470 return (new);
7466 7471 }
7467 7472
7468 7473 #define DTRACE_ISALPHA(c) \
7469 7474 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
7470 7475
7471 7476 static int
7472 7477 dtrace_badname(const char *s)
7473 7478 {
7474 7479 char c;
7475 7480
7476 7481 if (s == NULL || (c = *s++) == '\0')
7477 7482 return (0);
7478 7483
7479 7484 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
7480 7485 return (1);
7481 7486
7482 7487 while ((c = *s++) != '\0') {
7483 7488 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
7484 7489 c != '-' && c != '_' && c != '.' && c != '`')
7485 7490 return (1);
7486 7491 }
7487 7492
7488 7493 return (0);
7489 7494 }
7490 7495
7491 7496 static void
7492 7497 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
7493 7498 {
7494 7499 uint32_t priv;
7495 7500
7496 7501 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
7497 7502 /*
7498 7503 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
7499 7504 */
7500 7505 priv = DTRACE_PRIV_ALL;
7501 7506 } else {
7502 7507 *uidp = crgetuid(cr);
7503 7508 *zoneidp = crgetzoneid(cr);
7504 7509
7505 7510 priv = 0;
7506 7511 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
7507 7512 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
7508 7513 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
7509 7514 priv |= DTRACE_PRIV_USER;
7510 7515 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
7511 7516 priv |= DTRACE_PRIV_PROC;
7512 7517 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
7513 7518 priv |= DTRACE_PRIV_OWNER;
7514 7519 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
7515 7520 priv |= DTRACE_PRIV_ZONEOWNER;
7516 7521 }
7517 7522
7518 7523 *privp = priv;
7519 7524 }
7520 7525
7521 7526 #ifdef DTRACE_ERRDEBUG
7522 7527 static void
7523 7528 dtrace_errdebug(const char *str)
7524 7529 {
7525 7530 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ;
7526 7531 int occupied = 0;
7527 7532
7528 7533 mutex_enter(&dtrace_errlock);
7529 7534 dtrace_errlast = str;
7530 7535 dtrace_errthread = curthread;
7531 7536
7532 7537 while (occupied++ < DTRACE_ERRHASHSZ) {
7533 7538 if (dtrace_errhash[hval].dter_msg == str) {
7534 7539 dtrace_errhash[hval].dter_count++;
7535 7540 goto out;
7536 7541 }
7537 7542
7538 7543 if (dtrace_errhash[hval].dter_msg != NULL) {
7539 7544 hval = (hval + 1) % DTRACE_ERRHASHSZ;
7540 7545 continue;
7541 7546 }
7542 7547
7543 7548 dtrace_errhash[hval].dter_msg = str;
7544 7549 dtrace_errhash[hval].dter_count = 1;
7545 7550 goto out;
7546 7551 }
7547 7552
7548 7553 panic("dtrace: undersized error hash");
7549 7554 out:
7550 7555 mutex_exit(&dtrace_errlock);
7551 7556 }
7552 7557 #endif
7553 7558
7554 7559 /*
7555 7560 * DTrace Matching Functions
7556 7561 *
7557 7562 * These functions are used to match groups of probes, given some elements of
7558 7563 * a probe tuple, or some globbed expressions for elements of a probe tuple.
7559 7564 */
7560 7565 static int
7561 7566 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
7562 7567 zoneid_t zoneid)
7563 7568 {
7564 7569 if (priv != DTRACE_PRIV_ALL) {
7565 7570 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
7566 7571 uint32_t match = priv & ppriv;
7567 7572
7568 7573 /*
7569 7574 * No PRIV_DTRACE_* privileges...
7570 7575 */
7571 7576 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
7572 7577 DTRACE_PRIV_KERNEL)) == 0)
7573 7578 return (0);
7574 7579
7575 7580 /*
7576 7581 * No matching bits, but there were bits to match...
7577 7582 */
7578 7583 if (match == 0 && ppriv != 0)
7579 7584 return (0);
7580 7585
7581 7586 /*
7582 7587 * Need to have permissions to the process, but don't...
7583 7588 */
7584 7589 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
7585 7590 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
7586 7591 return (0);
7587 7592 }
7588 7593
7589 7594 /*
7590 7595 * Need to be in the same zone unless we possess the
7591 7596 * privilege to examine all zones.
7592 7597 */
7593 7598 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
7594 7599 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
7595 7600 return (0);
7596 7601 }
7597 7602 }
7598 7603
7599 7604 return (1);
7600 7605 }
7601 7606
7602 7607 /*
7603 7608 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
7604 7609 * consists of input pattern strings and an ops-vector to evaluate them.
7605 7610 * This function returns >0 for match, 0 for no match, and <0 for error.
7606 7611 */
7607 7612 static int
7608 7613 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
7609 7614 uint32_t priv, uid_t uid, zoneid_t zoneid)
7610 7615 {
7611 7616 dtrace_provider_t *pvp = prp->dtpr_provider;
7612 7617 int rv;
7613 7618
7614 7619 if (pvp->dtpv_defunct)
7615 7620 return (0);
7616 7621
7617 7622 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
7618 7623 return (rv);
7619 7624
7620 7625 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
7621 7626 return (rv);
7622 7627
7623 7628 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
7624 7629 return (rv);
7625 7630
7626 7631 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
7627 7632 return (rv);
7628 7633
7629 7634 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
7630 7635 return (0);
7631 7636
7632 7637 return (rv);
7633 7638 }
7634 7639
7635 7640 /*
7636 7641 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
7637 7642 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
7638 7643 * libc's version, the kernel version only applies to 8-bit ASCII strings.
7639 7644 * In addition, all of the recursion cases except for '*' matching have been
7640 7645 * unwound. For '*', we still implement recursive evaluation, but a depth
7641 7646 * counter is maintained and matching is aborted if we recurse too deep.
7642 7647 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
7643 7648 */
7644 7649 static int
7645 7650 dtrace_match_glob(const char *s, const char *p, int depth)
7646 7651 {
7647 7652 const char *olds;
7648 7653 char s1, c;
7649 7654 int gs;
7650 7655
7651 7656 if (depth > DTRACE_PROBEKEY_MAXDEPTH)
7652 7657 return (-1);
7653 7658
7654 7659 if (s == NULL)
7655 7660 s = ""; /* treat NULL as empty string */
7656 7661
7657 7662 top:
7658 7663 olds = s;
7659 7664 s1 = *s++;
7660 7665
7661 7666 if (p == NULL)
7662 7667 return (0);
7663 7668
7664 7669 if ((c = *p++) == '\0')
7665 7670 return (s1 == '\0');
7666 7671
7667 7672 switch (c) {
7668 7673 case '[': {
7669 7674 int ok = 0, notflag = 0;
7670 7675 char lc = '\0';
7671 7676
7672 7677 if (s1 == '\0')
7673 7678 return (0);
7674 7679
7675 7680 if (*p == '!') {
7676 7681 notflag = 1;
7677 7682 p++;
7678 7683 }
7679 7684
7680 7685 if ((c = *p++) == '\0')
7681 7686 return (0);
7682 7687
7683 7688 do {
7684 7689 if (c == '-' && lc != '\0' && *p != ']') {
7685 7690 if ((c = *p++) == '\0')
7686 7691 return (0);
7687 7692 if (c == '\\' && (c = *p++) == '\0')
7688 7693 return (0);
7689 7694
7690 7695 if (notflag) {
7691 7696 if (s1 < lc || s1 > c)
7692 7697 ok++;
7693 7698 else
7694 7699 return (0);
7695 7700 } else if (lc <= s1 && s1 <= c)
7696 7701 ok++;
7697 7702
7698 7703 } else if (c == '\\' && (c = *p++) == '\0')
7699 7704 return (0);
7700 7705
7701 7706 lc = c; /* save left-hand 'c' for next iteration */
7702 7707
7703 7708 if (notflag) {
7704 7709 if (s1 != c)
7705 7710 ok++;
7706 7711 else
7707 7712 return (0);
7708 7713 } else if (s1 == c)
7709 7714 ok++;
7710 7715
7711 7716 if ((c = *p++) == '\0')
7712 7717 return (0);
7713 7718
7714 7719 } while (c != ']');
7715 7720
7716 7721 if (ok)
7717 7722 goto top;
7718 7723
7719 7724 return (0);
7720 7725 }
7721 7726
7722 7727 case '\\':
7723 7728 if ((c = *p++) == '\0')
7724 7729 return (0);
7725 7730 /*FALLTHRU*/
7726 7731
7727 7732 default:
7728 7733 if (c != s1)
7729 7734 return (0);
7730 7735 /*FALLTHRU*/
7731 7736
7732 7737 case '?':
7733 7738 if (s1 != '\0')
7734 7739 goto top;
7735 7740 return (0);
7736 7741
7737 7742 case '*':
7738 7743 while (*p == '*')
7739 7744 p++; /* consecutive *'s are identical to a single one */
7740 7745
7741 7746 if (*p == '\0')
7742 7747 return (1);
7743 7748
7744 7749 for (s = olds; *s != '\0'; s++) {
7745 7750 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
7746 7751 return (gs);
7747 7752 }
7748 7753
7749 7754 return (0);
7750 7755 }
7751 7756 }
7752 7757
7753 7758 /*ARGSUSED*/
7754 7759 static int
7755 7760 dtrace_match_string(const char *s, const char *p, int depth)
7756 7761 {
7757 7762 return (s != NULL && strcmp(s, p) == 0);
7758 7763 }
7759 7764
7760 7765 /*ARGSUSED*/
7761 7766 static int
7762 7767 dtrace_match_nul(const char *s, const char *p, int depth)
7763 7768 {
7764 7769 return (1); /* always match the empty pattern */
7765 7770 }
7766 7771
7767 7772 /*ARGSUSED*/
7768 7773 static int
7769 7774 dtrace_match_nonzero(const char *s, const char *p, int depth)
7770 7775 {
7771 7776 return (s != NULL && s[0] != '\0');
7772 7777 }
7773 7778
7774 7779 static int
7775 7780 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
7776 7781 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
7777 7782 {
7778 7783 dtrace_probe_t template, *probe;
7779 7784 dtrace_hash_t *hash = NULL;
7780 7785 int len, rc, best = INT_MAX, nmatched = 0;
7781 7786 dtrace_id_t i;
7782 7787
7783 7788 ASSERT(MUTEX_HELD(&dtrace_lock));
7784 7789
7785 7790 /*
7786 7791 * If the probe ID is specified in the key, just lookup by ID and
7787 7792 * invoke the match callback once if a matching probe is found.
7788 7793 */
7789 7794 if (pkp->dtpk_id != DTRACE_IDNONE) {
7790 7795 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
7791 7796 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
7792 7797 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL)
7793 7798 return (DTRACE_MATCH_FAIL);
7794 7799 nmatched++;
7795 7800 }
7796 7801 return (nmatched);
7797 7802 }
7798 7803
7799 7804 template.dtpr_mod = (char *)pkp->dtpk_mod;
7800 7805 template.dtpr_func = (char *)pkp->dtpk_func;
7801 7806 template.dtpr_name = (char *)pkp->dtpk_name;
7802 7807
7803 7808 /*
7804 7809 * We want to find the most distinct of the module name, function
7805 7810 * name, and name. So for each one that is not a glob pattern or
7806 7811 * empty string, we perform a lookup in the corresponding hash and
7807 7812 * use the hash table with the fewest collisions to do our search.
7808 7813 */
7809 7814 if (pkp->dtpk_mmatch == &dtrace_match_string &&
7810 7815 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
7811 7816 best = len;
7812 7817 hash = dtrace_bymod;
7813 7818 }
7814 7819
7815 7820 if (pkp->dtpk_fmatch == &dtrace_match_string &&
7816 7821 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
7817 7822 best = len;
7818 7823 hash = dtrace_byfunc;
7819 7824 }
7820 7825
7821 7826 if (pkp->dtpk_nmatch == &dtrace_match_string &&
7822 7827 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
7823 7828 best = len;
7824 7829 hash = dtrace_byname;
7825 7830 }
7826 7831
7827 7832 /*
7828 7833 * If we did not select a hash table, iterate over every probe and
7829 7834 * invoke our callback for each one that matches our input probe key.
7830 7835 */
7831 7836 if (hash == NULL) {
7832 7837 for (i = 0; i < dtrace_nprobes; i++) {
7833 7838 if ((probe = dtrace_probes[i]) == NULL ||
7834 7839 dtrace_match_probe(probe, pkp, priv, uid,
7835 7840 zoneid) <= 0)
7836 7841 continue;
7837 7842
7838 7843 nmatched++;
7839 7844
7840 7845 if ((rc = (*matched)(probe, arg)) !=
7841 7846 DTRACE_MATCH_NEXT) {
7842 7847 if (rc == DTRACE_MATCH_FAIL)
7843 7848 return (DTRACE_MATCH_FAIL);
7844 7849 break;
7845 7850 }
7846 7851 }
7847 7852
7848 7853 return (nmatched);
7849 7854 }
7850 7855
7851 7856 /*
7852 7857 * If we selected a hash table, iterate over each probe of the same key
7853 7858 * name and invoke the callback for every probe that matches the other
7854 7859 * attributes of our input probe key.
7855 7860 */
7856 7861 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
7857 7862 probe = *(DTRACE_HASHNEXT(hash, probe))) {
7858 7863
7859 7864 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
7860 7865 continue;
7861 7866
7862 7867 nmatched++;
7863 7868
7864 7869 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) {
7865 7870 if (rc == DTRACE_MATCH_FAIL)
7866 7871 return (DTRACE_MATCH_FAIL);
7867 7872 break;
7868 7873 }
7869 7874 }
7870 7875
7871 7876 return (nmatched);
7872 7877 }
7873 7878
7874 7879 /*
7875 7880 * Return the function pointer dtrace_probecmp() should use to compare the
7876 7881 * specified pattern with a string. For NULL or empty patterns, we select
7877 7882 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
7878 7883 * For non-empty non-glob strings, we use dtrace_match_string().
7879 7884 */
7880 7885 static dtrace_probekey_f *
7881 7886 dtrace_probekey_func(const char *p)
7882 7887 {
7883 7888 char c;
7884 7889
7885 7890 if (p == NULL || *p == '\0')
7886 7891 return (&dtrace_match_nul);
7887 7892
7888 7893 while ((c = *p++) != '\0') {
7889 7894 if (c == '[' || c == '?' || c == '*' || c == '\\')
7890 7895 return (&dtrace_match_glob);
7891 7896 }
7892 7897
7893 7898 return (&dtrace_match_string);
7894 7899 }
7895 7900
7896 7901 /*
7897 7902 * Build a probe comparison key for use with dtrace_match_probe() from the
7898 7903 * given probe description. By convention, a null key only matches anchored
7899 7904 * probes: if each field is the empty string, reset dtpk_fmatch to
7900 7905 * dtrace_match_nonzero().
7901 7906 */
7902 7907 static void
7903 7908 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
7904 7909 {
7905 7910 pkp->dtpk_prov = pdp->dtpd_provider;
7906 7911 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
7907 7912
7908 7913 pkp->dtpk_mod = pdp->dtpd_mod;
7909 7914 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
7910 7915
7911 7916 pkp->dtpk_func = pdp->dtpd_func;
7912 7917 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
7913 7918
7914 7919 pkp->dtpk_name = pdp->dtpd_name;
7915 7920 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
7916 7921
7917 7922 pkp->dtpk_id = pdp->dtpd_id;
7918 7923
7919 7924 if (pkp->dtpk_id == DTRACE_IDNONE &&
7920 7925 pkp->dtpk_pmatch == &dtrace_match_nul &&
7921 7926 pkp->dtpk_mmatch == &dtrace_match_nul &&
7922 7927 pkp->dtpk_fmatch == &dtrace_match_nul &&
7923 7928 pkp->dtpk_nmatch == &dtrace_match_nul)
7924 7929 pkp->dtpk_fmatch = &dtrace_match_nonzero;
7925 7930 }
7926 7931
7927 7932 /*
7928 7933 * DTrace Provider-to-Framework API Functions
7929 7934 *
7930 7935 * These functions implement much of the Provider-to-Framework API, as
7931 7936 * described in <sys/dtrace.h>. The parts of the API not in this section are
7932 7937 * the functions in the API for probe management (found below), and
7933 7938 * dtrace_probe() itself (found above).
7934 7939 */
7935 7940
7936 7941 /*
7937 7942 * Register the calling provider with the DTrace framework. This should
7938 7943 * generally be called by DTrace providers in their attach(9E) entry point.
7939 7944 */
7940 7945 int
7941 7946 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
7942 7947 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
7943 7948 {
7944 7949 dtrace_provider_t *provider;
7945 7950
7946 7951 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
7947 7952 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7948 7953 "arguments", name ? name : "<NULL>");
7949 7954 return (EINVAL);
7950 7955 }
7951 7956
7952 7957 if (name[0] == '\0' || dtrace_badname(name)) {
7953 7958 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7954 7959 "provider name", name);
7955 7960 return (EINVAL);
7956 7961 }
7957 7962
7958 7963 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
7959 7964 pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
7960 7965 pops->dtps_destroy == NULL ||
7961 7966 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
7962 7967 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7963 7968 "provider ops", name);
7964 7969 return (EINVAL);
7965 7970 }
7966 7971
7967 7972 if (dtrace_badattr(&pap->dtpa_provider) ||
7968 7973 dtrace_badattr(&pap->dtpa_mod) ||
7969 7974 dtrace_badattr(&pap->dtpa_func) ||
7970 7975 dtrace_badattr(&pap->dtpa_name) ||
7971 7976 dtrace_badattr(&pap->dtpa_args)) {
7972 7977 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7973 7978 "provider attributes", name);
7974 7979 return (EINVAL);
7975 7980 }
7976 7981
7977 7982 if (priv & ~DTRACE_PRIV_ALL) {
7978 7983 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7979 7984 "privilege attributes", name);
7980 7985 return (EINVAL);
7981 7986 }
7982 7987
7983 7988 if ((priv & DTRACE_PRIV_KERNEL) &&
7984 7989 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
7985 7990 pops->dtps_mode == NULL) {
7986 7991 cmn_err(CE_WARN, "failed to register provider '%s': need "
7987 7992 "dtps_mode() op for given privilege attributes", name);
7988 7993 return (EINVAL);
7989 7994 }
7990 7995
7991 7996 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
7992 7997 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7993 7998 (void) strcpy(provider->dtpv_name, name);
7994 7999
7995 8000 provider->dtpv_attr = *pap;
7996 8001 provider->dtpv_priv.dtpp_flags = priv;
7997 8002 if (cr != NULL) {
7998 8003 provider->dtpv_priv.dtpp_uid = crgetuid(cr);
7999 8004 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
8000 8005 }
8001 8006 provider->dtpv_pops = *pops;
8002 8007
8003 8008 if (pops->dtps_provide == NULL) {
8004 8009 ASSERT(pops->dtps_provide_module != NULL);
8005 8010 provider->dtpv_pops.dtps_provide =
8006 8011 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop;
8007 8012 }
8008 8013
8009 8014 if (pops->dtps_provide_module == NULL) {
8010 8015 ASSERT(pops->dtps_provide != NULL);
8011 8016 provider->dtpv_pops.dtps_provide_module =
8012 8017 (void (*)(void *, struct modctl *))dtrace_nullop;
8013 8018 }
8014 8019
8015 8020 if (pops->dtps_suspend == NULL) {
8016 8021 ASSERT(pops->dtps_resume == NULL);
8017 8022 provider->dtpv_pops.dtps_suspend =
8018 8023 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
8019 8024 provider->dtpv_pops.dtps_resume =
8020 8025 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
8021 8026 }
8022 8027
8023 8028 provider->dtpv_arg = arg;
8024 8029 *idp = (dtrace_provider_id_t)provider;
8025 8030
8026 8031 if (pops == &dtrace_provider_ops) {
8027 8032 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8028 8033 ASSERT(MUTEX_HELD(&dtrace_lock));
8029 8034 ASSERT(dtrace_anon.dta_enabling == NULL);
8030 8035
8031 8036 /*
8032 8037 * We make sure that the DTrace provider is at the head of
8033 8038 * the provider chain.
8034 8039 */
8035 8040 provider->dtpv_next = dtrace_provider;
8036 8041 dtrace_provider = provider;
8037 8042 return (0);
8038 8043 }
8039 8044
8040 8045 mutex_enter(&dtrace_provider_lock);
8041 8046 mutex_enter(&dtrace_lock);
8042 8047
8043 8048 /*
8044 8049 * If there is at least one provider registered, we'll add this
8045 8050 * provider after the first provider.
8046 8051 */
8047 8052 if (dtrace_provider != NULL) {
8048 8053 provider->dtpv_next = dtrace_provider->dtpv_next;
8049 8054 dtrace_provider->dtpv_next = provider;
8050 8055 } else {
8051 8056 dtrace_provider = provider;
8052 8057 }
8053 8058
8054 8059 if (dtrace_retained != NULL) {
8055 8060 dtrace_enabling_provide(provider);
8056 8061
8057 8062 /*
8058 8063 * Now we need to call dtrace_enabling_matchall() -- which
8059 8064 * will acquire cpu_lock and dtrace_lock. We therefore need
8060 8065 * to drop all of our locks before calling into it...
8061 8066 */
8062 8067 mutex_exit(&dtrace_lock);
8063 8068 mutex_exit(&dtrace_provider_lock);
8064 8069 dtrace_enabling_matchall();
8065 8070
8066 8071 return (0);
8067 8072 }
8068 8073
8069 8074 mutex_exit(&dtrace_lock);
8070 8075 mutex_exit(&dtrace_provider_lock);
8071 8076
8072 8077 return (0);
8073 8078 }
8074 8079
8075 8080 /*
8076 8081 * Unregister the specified provider from the DTrace framework. This should
8077 8082 * generally be called by DTrace providers in their detach(9E) entry point.
8078 8083 */
8079 8084 int
8080 8085 dtrace_unregister(dtrace_provider_id_t id)
8081 8086 {
8082 8087 dtrace_provider_t *old = (dtrace_provider_t *)id;
8083 8088 dtrace_provider_t *prev = NULL;
8084 8089 int i, self = 0, noreap = 0;
8085 8090 dtrace_probe_t *probe, *first = NULL;
8086 8091
8087 8092 if (old->dtpv_pops.dtps_enable ==
8088 8093 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) {
8089 8094 /*
8090 8095 * If DTrace itself is the provider, we're called with locks
8091 8096 * already held.
8092 8097 */
8093 8098 ASSERT(old == dtrace_provider);
8094 8099 ASSERT(dtrace_devi != NULL);
8095 8100 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8096 8101 ASSERT(MUTEX_HELD(&dtrace_lock));
8097 8102 self = 1;
8098 8103
8099 8104 if (dtrace_provider->dtpv_next != NULL) {
8100 8105 /*
8101 8106 * There's another provider here; return failure.
8102 8107 */
8103 8108 return (EBUSY);
8104 8109 }
8105 8110 } else {
8106 8111 mutex_enter(&dtrace_provider_lock);
8107 8112 mutex_enter(&mod_lock);
8108 8113 mutex_enter(&dtrace_lock);
8109 8114 }
8110 8115
8111 8116 /*
8112 8117 * If anyone has /dev/dtrace open, or if there are anonymous enabled
8113 8118 * probes, we refuse to let providers slither away, unless this
8114 8119 * provider has already been explicitly invalidated.
8115 8120 */
8116 8121 if (!old->dtpv_defunct &&
8117 8122 (dtrace_opens || (dtrace_anon.dta_state != NULL &&
8118 8123 dtrace_anon.dta_state->dts_necbs > 0))) {
8119 8124 if (!self) {
8120 8125 mutex_exit(&dtrace_lock);
8121 8126 mutex_exit(&mod_lock);
8122 8127 mutex_exit(&dtrace_provider_lock);
8123 8128 }
8124 8129 return (EBUSY);
8125 8130 }
8126 8131
8127 8132 /*
8128 8133 * Attempt to destroy the probes associated with this provider.
8129 8134 */
8130 8135 for (i = 0; i < dtrace_nprobes; i++) {
8131 8136 if ((probe = dtrace_probes[i]) == NULL)
8132 8137 continue;
8133 8138
8134 8139 if (probe->dtpr_provider != old)
8135 8140 continue;
8136 8141
8137 8142 if (probe->dtpr_ecb == NULL)
8138 8143 continue;
8139 8144
8140 8145 /*
8141 8146 * If we are trying to unregister a defunct provider, and the
8142 8147 * provider was made defunct within the interval dictated by
8143 8148 * dtrace_unregister_defunct_reap, we'll (asynchronously)
8144 8149 * attempt to reap our enablings. To denote that the provider
8145 8150 * should reattempt to unregister itself at some point in the
8146 8151 * future, we will return a differentiable error code (EAGAIN
8147 8152 * instead of EBUSY) in this case.
8148 8153 */
8149 8154 if (dtrace_gethrtime() - old->dtpv_defunct >
8150 8155 dtrace_unregister_defunct_reap)
8151 8156 noreap = 1;
8152 8157
8153 8158 if (!self) {
8154 8159 mutex_exit(&dtrace_lock);
8155 8160 mutex_exit(&mod_lock);
8156 8161 mutex_exit(&dtrace_provider_lock);
8157 8162 }
8158 8163
8159 8164 if (noreap)
8160 8165 return (EBUSY);
8161 8166
8162 8167 (void) taskq_dispatch(dtrace_taskq,
8163 8168 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP);
8164 8169
8165 8170 return (EAGAIN);
8166 8171 }
8167 8172
8168 8173 /*
8169 8174 * All of the probes for this provider are disabled; we can safely
8170 8175 * remove all of them from their hash chains and from the probe array.
8171 8176 */
8172 8177 for (i = 0; i < dtrace_nprobes; i++) {
8173 8178 if ((probe = dtrace_probes[i]) == NULL)
8174 8179 continue;
8175 8180
8176 8181 if (probe->dtpr_provider != old)
8177 8182 continue;
8178 8183
8179 8184 dtrace_probes[i] = NULL;
8180 8185
8181 8186 dtrace_hash_remove(dtrace_bymod, probe);
8182 8187 dtrace_hash_remove(dtrace_byfunc, probe);
8183 8188 dtrace_hash_remove(dtrace_byname, probe);
8184 8189
8185 8190 if (first == NULL) {
8186 8191 first = probe;
8187 8192 probe->dtpr_nextmod = NULL;
8188 8193 } else {
8189 8194 probe->dtpr_nextmod = first;
8190 8195 first = probe;
8191 8196 }
8192 8197 }
8193 8198
8194 8199 /*
8195 8200 * The provider's probes have been removed from the hash chains and
8196 8201 * from the probe array. Now issue a dtrace_sync() to be sure that
8197 8202 * everyone has cleared out from any probe array processing.
8198 8203 */
8199 8204 dtrace_sync();
8200 8205
8201 8206 for (probe = first; probe != NULL; probe = first) {
8202 8207 first = probe->dtpr_nextmod;
8203 8208
8204 8209 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
8205 8210 probe->dtpr_arg);
8206 8211 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
8207 8212 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
8208 8213 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
8209 8214 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
8210 8215 kmem_free(probe, sizeof (dtrace_probe_t));
8211 8216 }
8212 8217
8213 8218 if ((prev = dtrace_provider) == old) {
8214 8219 ASSERT(self || dtrace_devi == NULL);
8215 8220 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
8216 8221 dtrace_provider = old->dtpv_next;
8217 8222 } else {
8218 8223 while (prev != NULL && prev->dtpv_next != old)
8219 8224 prev = prev->dtpv_next;
8220 8225
8221 8226 if (prev == NULL) {
8222 8227 panic("attempt to unregister non-existent "
8223 8228 "dtrace provider %p\n", (void *)id);
8224 8229 }
8225 8230
8226 8231 prev->dtpv_next = old->dtpv_next;
8227 8232 }
8228 8233
8229 8234 if (!self) {
8230 8235 mutex_exit(&dtrace_lock);
8231 8236 mutex_exit(&mod_lock);
8232 8237 mutex_exit(&dtrace_provider_lock);
8233 8238 }
8234 8239
8235 8240 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
8236 8241 kmem_free(old, sizeof (dtrace_provider_t));
8237 8242
8238 8243 return (0);
8239 8244 }
8240 8245
8241 8246 /*
8242 8247 * Invalidate the specified provider. All subsequent probe lookups for the
8243 8248 * specified provider will fail, but its probes will not be removed.
8244 8249 */
8245 8250 void
8246 8251 dtrace_invalidate(dtrace_provider_id_t id)
8247 8252 {
8248 8253 dtrace_provider_t *pvp = (dtrace_provider_t *)id;
8249 8254
8250 8255 ASSERT(pvp->dtpv_pops.dtps_enable !=
8251 8256 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop);
8252 8257
8253 8258 mutex_enter(&dtrace_provider_lock);
8254 8259 mutex_enter(&dtrace_lock);
8255 8260
8256 8261 pvp->dtpv_defunct = dtrace_gethrtime();
8257 8262
8258 8263 mutex_exit(&dtrace_lock);
8259 8264 mutex_exit(&dtrace_provider_lock);
8260 8265 }
8261 8266
8262 8267 /*
8263 8268 * Indicate whether or not DTrace has attached.
8264 8269 */
8265 8270 int
8266 8271 dtrace_attached(void)
8267 8272 {
8268 8273 /*
8269 8274 * dtrace_provider will be non-NULL iff the DTrace driver has
8270 8275 * attached. (It's non-NULL because DTrace is always itself a
8271 8276 * provider.)
8272 8277 */
8273 8278 return (dtrace_provider != NULL);
8274 8279 }
8275 8280
8276 8281 /*
8277 8282 * Remove all the unenabled probes for the given provider. This function is
8278 8283 * not unlike dtrace_unregister(), except that it doesn't remove the provider
8279 8284 * -- just as many of its associated probes as it can.
8280 8285 */
8281 8286 int
8282 8287 dtrace_condense(dtrace_provider_id_t id)
8283 8288 {
8284 8289 dtrace_provider_t *prov = (dtrace_provider_t *)id;
8285 8290 int i;
8286 8291 dtrace_probe_t *probe;
8287 8292
8288 8293 /*
8289 8294 * Make sure this isn't the dtrace provider itself.
8290 8295 */
8291 8296 ASSERT(prov->dtpv_pops.dtps_enable !=
8292 8297 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop);
8293 8298
8294 8299 mutex_enter(&dtrace_provider_lock);
8295 8300 mutex_enter(&dtrace_lock);
8296 8301
8297 8302 /*
8298 8303 * Attempt to destroy the probes associated with this provider.
8299 8304 */
8300 8305 for (i = 0; i < dtrace_nprobes; i++) {
8301 8306 if ((probe = dtrace_probes[i]) == NULL)
8302 8307 continue;
8303 8308
8304 8309 if (probe->dtpr_provider != prov)
8305 8310 continue;
8306 8311
8307 8312 if (probe->dtpr_ecb != NULL)
8308 8313 continue;
8309 8314
8310 8315 dtrace_probes[i] = NULL;
8311 8316
8312 8317 dtrace_hash_remove(dtrace_bymod, probe);
8313 8318 dtrace_hash_remove(dtrace_byfunc, probe);
8314 8319 dtrace_hash_remove(dtrace_byname, probe);
8315 8320
8316 8321 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
8317 8322 probe->dtpr_arg);
8318 8323 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
8319 8324 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
8320 8325 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
8321 8326 kmem_free(probe, sizeof (dtrace_probe_t));
8322 8327 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
8323 8328 }
8324 8329
8325 8330 mutex_exit(&dtrace_lock);
8326 8331 mutex_exit(&dtrace_provider_lock);
8327 8332
8328 8333 return (0);
8329 8334 }
8330 8335
8331 8336 /*
8332 8337 * DTrace Probe Management Functions
8333 8338 *
8334 8339 * The functions in this section perform the DTrace probe management,
8335 8340 * including functions to create probes, look-up probes, and call into the
8336 8341 * providers to request that probes be provided. Some of these functions are
8337 8342 * in the Provider-to-Framework API; these functions can be identified by the
8338 8343 * fact that they are not declared "static".
8339 8344 */
8340 8345
8341 8346 /*
8342 8347 * Create a probe with the specified module name, function name, and name.
8343 8348 */
8344 8349 dtrace_id_t
8345 8350 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
8346 8351 const char *func, const char *name, int aframes, void *arg)
8347 8352 {
8348 8353 dtrace_probe_t *probe, **probes;
8349 8354 dtrace_provider_t *provider = (dtrace_provider_t *)prov;
8350 8355 dtrace_id_t id;
8351 8356
8352 8357 if (provider == dtrace_provider) {
8353 8358 ASSERT(MUTEX_HELD(&dtrace_lock));
8354 8359 } else {
8355 8360 mutex_enter(&dtrace_lock);
8356 8361 }
8357 8362
8358 8363 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
8359 8364 VM_BESTFIT | VM_SLEEP);
8360 8365 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
8361 8366
8362 8367 probe->dtpr_id = id;
8363 8368 probe->dtpr_gen = dtrace_probegen++;
8364 8369 probe->dtpr_mod = dtrace_strdup(mod);
8365 8370 probe->dtpr_func = dtrace_strdup(func);
8366 8371 probe->dtpr_name = dtrace_strdup(name);
8367 8372 probe->dtpr_arg = arg;
8368 8373 probe->dtpr_aframes = aframes;
8369 8374 probe->dtpr_provider = provider;
8370 8375
8371 8376 dtrace_hash_add(dtrace_bymod, probe);
8372 8377 dtrace_hash_add(dtrace_byfunc, probe);
8373 8378 dtrace_hash_add(dtrace_byname, probe);
8374 8379
8375 8380 if (id - 1 >= dtrace_nprobes) {
8376 8381 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
8377 8382 size_t nsize = osize << 1;
8378 8383
8379 8384 if (nsize == 0) {
8380 8385 ASSERT(osize == 0);
8381 8386 ASSERT(dtrace_probes == NULL);
8382 8387 nsize = sizeof (dtrace_probe_t *);
8383 8388 }
8384 8389
8385 8390 probes = kmem_zalloc(nsize, KM_SLEEP);
8386 8391
8387 8392 if (dtrace_probes == NULL) {
8388 8393 ASSERT(osize == 0);
8389 8394 dtrace_probes = probes;
8390 8395 dtrace_nprobes = 1;
8391 8396 } else {
8392 8397 dtrace_probe_t **oprobes = dtrace_probes;
8393 8398
8394 8399 bcopy(oprobes, probes, osize);
8395 8400 dtrace_membar_producer();
8396 8401 dtrace_probes = probes;
8397 8402
8398 8403 dtrace_sync();
8399 8404
8400 8405 /*
8401 8406 * All CPUs are now seeing the new probes array; we can
8402 8407 * safely free the old array.
8403 8408 */
8404 8409 kmem_free(oprobes, osize);
8405 8410 dtrace_nprobes <<= 1;
8406 8411 }
8407 8412
8408 8413 ASSERT(id - 1 < dtrace_nprobes);
8409 8414 }
8410 8415
8411 8416 ASSERT(dtrace_probes[id - 1] == NULL);
8412 8417 dtrace_probes[id - 1] = probe;
8413 8418
8414 8419 if (provider != dtrace_provider)
8415 8420 mutex_exit(&dtrace_lock);
8416 8421
8417 8422 return (id);
8418 8423 }
8419 8424
8420 8425 static dtrace_probe_t *
8421 8426 dtrace_probe_lookup_id(dtrace_id_t id)
8422 8427 {
8423 8428 ASSERT(MUTEX_HELD(&dtrace_lock));
8424 8429
8425 8430 if (id == 0 || id > dtrace_nprobes)
8426 8431 return (NULL);
8427 8432
8428 8433 return (dtrace_probes[id - 1]);
8429 8434 }
8430 8435
8431 8436 static int
8432 8437 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
8433 8438 {
8434 8439 *((dtrace_id_t *)arg) = probe->dtpr_id;
8435 8440
8436 8441 return (DTRACE_MATCH_DONE);
8437 8442 }
8438 8443
8439 8444 /*
8440 8445 * Look up a probe based on provider and one or more of module name, function
8441 8446 * name and probe name.
8442 8447 */
8443 8448 dtrace_id_t
8444 8449 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod,
8445 8450 const char *func, const char *name)
8446 8451 {
8447 8452 dtrace_probekey_t pkey;
8448 8453 dtrace_id_t id;
8449 8454 int match;
8450 8455
8451 8456 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
8452 8457 pkey.dtpk_pmatch = &dtrace_match_string;
8453 8458 pkey.dtpk_mod = mod;
8454 8459 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
8455 8460 pkey.dtpk_func = func;
8456 8461 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
8457 8462 pkey.dtpk_name = name;
8458 8463 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
8459 8464 pkey.dtpk_id = DTRACE_IDNONE;
8460 8465
8461 8466 mutex_enter(&dtrace_lock);
8462 8467 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
8463 8468 dtrace_probe_lookup_match, &id);
8464 8469 mutex_exit(&dtrace_lock);
8465 8470
8466 8471 ASSERT(match == 1 || match == 0);
8467 8472 return (match ? id : 0);
8468 8473 }
8469 8474
8470 8475 /*
8471 8476 * Returns the probe argument associated with the specified probe.
8472 8477 */
8473 8478 void *
8474 8479 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
8475 8480 {
8476 8481 dtrace_probe_t *probe;
8477 8482 void *rval = NULL;
8478 8483
8479 8484 mutex_enter(&dtrace_lock);
8480 8485
8481 8486 if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
8482 8487 probe->dtpr_provider == (dtrace_provider_t *)id)
8483 8488 rval = probe->dtpr_arg;
8484 8489
8485 8490 mutex_exit(&dtrace_lock);
8486 8491
8487 8492 return (rval);
8488 8493 }
8489 8494
8490 8495 /*
8491 8496 * Copy a probe into a probe description.
8492 8497 */
8493 8498 static void
8494 8499 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
8495 8500 {
8496 8501 bzero(pdp, sizeof (dtrace_probedesc_t));
8497 8502 pdp->dtpd_id = prp->dtpr_id;
8498 8503
8499 8504 (void) strncpy(pdp->dtpd_provider,
8500 8505 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
8501 8506
8502 8507 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
8503 8508 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
8504 8509 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
8505 8510 }
8506 8511
8507 8512 /*
8508 8513 * Called to indicate that a probe -- or probes -- should be provided by a
8509 8514 * specfied provider. If the specified description is NULL, the provider will
8510 8515 * be told to provide all of its probes. (This is done whenever a new
8511 8516 * consumer comes along, or whenever a retained enabling is to be matched.) If
8512 8517 * the specified description is non-NULL, the provider is given the
8513 8518 * opportunity to dynamically provide the specified probe, allowing providers
8514 8519 * to support the creation of probes on-the-fly. (So-called _autocreated_
8515 8520 * probes.) If the provider is NULL, the operations will be applied to all
8516 8521 * providers; if the provider is non-NULL the operations will only be applied
8517 8522 * to the specified provider. The dtrace_provider_lock must be held, and the
8518 8523 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
8519 8524 * will need to grab the dtrace_lock when it reenters the framework through
8520 8525 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
8521 8526 */
8522 8527 static void
8523 8528 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
8524 8529 {
8525 8530 struct modctl *ctl;
8526 8531 int all = 0;
8527 8532
8528 8533 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8529 8534
8530 8535 if (prv == NULL) {
8531 8536 all = 1;
8532 8537 prv = dtrace_provider;
8533 8538 }
8534 8539
8535 8540 do {
8536 8541 /*
8537 8542 * First, call the blanket provide operation.
8538 8543 */
8539 8544 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
8540 8545
8541 8546 /*
8542 8547 * Now call the per-module provide operation. We will grab
8543 8548 * mod_lock to prevent the list from being modified. Note
8544 8549 * that this also prevents the mod_busy bits from changing.
8545 8550 * (mod_busy can only be changed with mod_lock held.)
8546 8551 */
8547 8552 mutex_enter(&mod_lock);
8548 8553
8549 8554 ctl = &modules;
8550 8555 do {
8551 8556 if (ctl->mod_busy || ctl->mod_mp == NULL)
8552 8557 continue;
8553 8558
8554 8559 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
8555 8560
8556 8561 } while ((ctl = ctl->mod_next) != &modules);
8557 8562
8558 8563 mutex_exit(&mod_lock);
8559 8564 } while (all && (prv = prv->dtpv_next) != NULL);
8560 8565 }
8561 8566
8562 8567 /*
8563 8568 * Iterate over each probe, and call the Framework-to-Provider API function
8564 8569 * denoted by offs.
8565 8570 */
8566 8571 static void
8567 8572 dtrace_probe_foreach(uintptr_t offs)
8568 8573 {
8569 8574 dtrace_provider_t *prov;
8570 8575 void (*func)(void *, dtrace_id_t, void *);
8571 8576 dtrace_probe_t *probe;
8572 8577 dtrace_icookie_t cookie;
8573 8578 int i;
8574 8579
8575 8580 /*
8576 8581 * We disable interrupts to walk through the probe array. This is
8577 8582 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
8578 8583 * won't see stale data.
8579 8584 */
8580 8585 cookie = dtrace_interrupt_disable();
8581 8586
8582 8587 for (i = 0; i < dtrace_nprobes; i++) {
8583 8588 if ((probe = dtrace_probes[i]) == NULL)
8584 8589 continue;
8585 8590
8586 8591 if (probe->dtpr_ecb == NULL) {
8587 8592 /*
8588 8593 * This probe isn't enabled -- don't call the function.
8589 8594 */
8590 8595 continue;
8591 8596 }
8592 8597
8593 8598 prov = probe->dtpr_provider;
8594 8599 func = *((void(**)(void *, dtrace_id_t, void *))
8595 8600 ((uintptr_t)&prov->dtpv_pops + offs));
8596 8601
8597 8602 func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
8598 8603 }
8599 8604
8600 8605 dtrace_interrupt_enable(cookie);
8601 8606 }
8602 8607
8603 8608 static int
8604 8609 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
8605 8610 {
8606 8611 dtrace_probekey_t pkey;
8607 8612 uint32_t priv;
8608 8613 uid_t uid;
8609 8614 zoneid_t zoneid;
8610 8615
8611 8616 ASSERT(MUTEX_HELD(&dtrace_lock));
8612 8617 dtrace_ecb_create_cache = NULL;
8613 8618
8614 8619 if (desc == NULL) {
8615 8620 /*
8616 8621 * If we're passed a NULL description, we're being asked to
8617 8622 * create an ECB with a NULL probe.
8618 8623 */
8619 8624 (void) dtrace_ecb_create_enable(NULL, enab);
8620 8625 return (0);
8621 8626 }
8622 8627
8623 8628 dtrace_probekey(desc, &pkey);
8624 8629 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
8625 8630 &priv, &uid, &zoneid);
8626 8631
8627 8632 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
8628 8633 enab));
8629 8634 }
8630 8635
8631 8636 /*
8632 8637 * DTrace Helper Provider Functions
8633 8638 */
8634 8639 static void
8635 8640 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
8636 8641 {
8637 8642 attr->dtat_name = DOF_ATTR_NAME(dofattr);
8638 8643 attr->dtat_data = DOF_ATTR_DATA(dofattr);
8639 8644 attr->dtat_class = DOF_ATTR_CLASS(dofattr);
8640 8645 }
8641 8646
8642 8647 static void
8643 8648 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
8644 8649 const dof_provider_t *dofprov, char *strtab)
8645 8650 {
8646 8651 hprov->dthpv_provname = strtab + dofprov->dofpv_name;
8647 8652 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
8648 8653 dofprov->dofpv_provattr);
8649 8654 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
8650 8655 dofprov->dofpv_modattr);
8651 8656 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
8652 8657 dofprov->dofpv_funcattr);
8653 8658 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
8654 8659 dofprov->dofpv_nameattr);
8655 8660 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
8656 8661 dofprov->dofpv_argsattr);
8657 8662 }
8658 8663
8659 8664 static void
8660 8665 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8661 8666 {
8662 8667 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8663 8668 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8664 8669 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
8665 8670 dof_provider_t *provider;
8666 8671 dof_probe_t *probe;
8667 8672 uint32_t *off, *enoff;
8668 8673 uint8_t *arg;
8669 8674 char *strtab;
8670 8675 uint_t i, nprobes;
8671 8676 dtrace_helper_provdesc_t dhpv;
8672 8677 dtrace_helper_probedesc_t dhpb;
8673 8678 dtrace_meta_t *meta = dtrace_meta_pid;
8674 8679 dtrace_mops_t *mops = &meta->dtm_mops;
8675 8680 void *parg;
8676 8681
8677 8682 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8678 8683 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8679 8684 provider->dofpv_strtab * dof->dofh_secsize);
8680 8685 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8681 8686 provider->dofpv_probes * dof->dofh_secsize);
8682 8687 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8683 8688 provider->dofpv_prargs * dof->dofh_secsize);
8684 8689 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8685 8690 provider->dofpv_proffs * dof->dofh_secsize);
8686 8691
8687 8692 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8688 8693 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
8689 8694 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
8690 8695 enoff = NULL;
8691 8696
8692 8697 /*
8693 8698 * See dtrace_helper_provider_validate().
8694 8699 */
8695 8700 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
8696 8701 provider->dofpv_prenoffs != DOF_SECT_NONE) {
8697 8702 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8698 8703 provider->dofpv_prenoffs * dof->dofh_secsize);
8699 8704 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
8700 8705 }
8701 8706
8702 8707 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
8703 8708
8704 8709 /*
8705 8710 * Create the provider.
8706 8711 */
8707 8712 dtrace_dofprov2hprov(&dhpv, provider, strtab);
8708 8713
8709 8714 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
8710 8715 return;
8711 8716
8712 8717 meta->dtm_count++;
8713 8718
8714 8719 /*
8715 8720 * Create the probes.
8716 8721 */
8717 8722 for (i = 0; i < nprobes; i++) {
8718 8723 probe = (dof_probe_t *)(uintptr_t)(daddr +
8719 8724 prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
8720 8725
8721 8726 dhpb.dthpb_mod = dhp->dofhp_mod;
8722 8727 dhpb.dthpb_func = strtab + probe->dofpr_func;
8723 8728 dhpb.dthpb_name = strtab + probe->dofpr_name;
8724 8729 dhpb.dthpb_base = probe->dofpr_addr;
8725 8730 dhpb.dthpb_offs = off + probe->dofpr_offidx;
8726 8731 dhpb.dthpb_noffs = probe->dofpr_noffs;
8727 8732 if (enoff != NULL) {
8728 8733 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
8729 8734 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
8730 8735 } else {
8731 8736 dhpb.dthpb_enoffs = NULL;
8732 8737 dhpb.dthpb_nenoffs = 0;
8733 8738 }
8734 8739 dhpb.dthpb_args = arg + probe->dofpr_argidx;
8735 8740 dhpb.dthpb_nargc = probe->dofpr_nargc;
8736 8741 dhpb.dthpb_xargc = probe->dofpr_xargc;
8737 8742 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
8738 8743 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
8739 8744
8740 8745 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
8741 8746 }
8742 8747 }
8743 8748
8744 8749 static void
8745 8750 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
8746 8751 {
8747 8752 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8748 8753 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8749 8754 int i;
8750 8755
8751 8756 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8752 8757
8753 8758 for (i = 0; i < dof->dofh_secnum; i++) {
8754 8759 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8755 8760 dof->dofh_secoff + i * dof->dofh_secsize);
8756 8761
8757 8762 if (sec->dofs_type != DOF_SECT_PROVIDER)
8758 8763 continue;
8759 8764
8760 8765 dtrace_helper_provide_one(dhp, sec, pid);
8761 8766 }
8762 8767
8763 8768 /*
8764 8769 * We may have just created probes, so we must now rematch against
8765 8770 * any retained enablings. Note that this call will acquire both
8766 8771 * cpu_lock and dtrace_lock; the fact that we are holding
8767 8772 * dtrace_meta_lock now is what defines the ordering with respect to
8768 8773 * these three locks.
8769 8774 */
8770 8775 dtrace_enabling_matchall();
8771 8776 }
8772 8777
8773 8778 static void
8774 8779 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8775 8780 {
8776 8781 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8777 8782 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8778 8783 dof_sec_t *str_sec;
8779 8784 dof_provider_t *provider;
8780 8785 char *strtab;
8781 8786 dtrace_helper_provdesc_t dhpv;
8782 8787 dtrace_meta_t *meta = dtrace_meta_pid;
8783 8788 dtrace_mops_t *mops = &meta->dtm_mops;
8784 8789
8785 8790 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8786 8791 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8787 8792 provider->dofpv_strtab * dof->dofh_secsize);
8788 8793
8789 8794 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8790 8795
8791 8796 /*
8792 8797 * Create the provider.
8793 8798 */
8794 8799 dtrace_dofprov2hprov(&dhpv, provider, strtab);
8795 8800
8796 8801 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
8797 8802
8798 8803 meta->dtm_count--;
8799 8804 }
8800 8805
8801 8806 static void
8802 8807 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
8803 8808 {
8804 8809 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8805 8810 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8806 8811 int i;
8807 8812
8808 8813 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8809 8814
8810 8815 for (i = 0; i < dof->dofh_secnum; i++) {
8811 8816 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8812 8817 dof->dofh_secoff + i * dof->dofh_secsize);
8813 8818
8814 8819 if (sec->dofs_type != DOF_SECT_PROVIDER)
8815 8820 continue;
8816 8821
8817 8822 dtrace_helper_provider_remove_one(dhp, sec, pid);
8818 8823 }
8819 8824 }
8820 8825
8821 8826 /*
8822 8827 * DTrace Meta Provider-to-Framework API Functions
8823 8828 *
8824 8829 * These functions implement the Meta Provider-to-Framework API, as described
8825 8830 * in <sys/dtrace.h>.
8826 8831 */
8827 8832 int
8828 8833 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
8829 8834 dtrace_meta_provider_id_t *idp)
8830 8835 {
8831 8836 dtrace_meta_t *meta;
8832 8837 dtrace_helpers_t *help, *next;
8833 8838 int i;
8834 8839
8835 8840 *idp = DTRACE_METAPROVNONE;
8836 8841
8837 8842 /*
8838 8843 * We strictly don't need the name, but we hold onto it for
8839 8844 * debuggability. All hail error queues!
8840 8845 */
8841 8846 if (name == NULL) {
8842 8847 cmn_err(CE_WARN, "failed to register meta-provider: "
8843 8848 "invalid name");
8844 8849 return (EINVAL);
8845 8850 }
8846 8851
8847 8852 if (mops == NULL ||
8848 8853 mops->dtms_create_probe == NULL ||
8849 8854 mops->dtms_provide_pid == NULL ||
8850 8855 mops->dtms_remove_pid == NULL) {
8851 8856 cmn_err(CE_WARN, "failed to register meta-register %s: "
8852 8857 "invalid ops", name);
8853 8858 return (EINVAL);
8854 8859 }
8855 8860
8856 8861 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
8857 8862 meta->dtm_mops = *mops;
8858 8863 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8859 8864 (void) strcpy(meta->dtm_name, name);
8860 8865 meta->dtm_arg = arg;
8861 8866
8862 8867 mutex_enter(&dtrace_meta_lock);
8863 8868 mutex_enter(&dtrace_lock);
8864 8869
8865 8870 if (dtrace_meta_pid != NULL) {
8866 8871 mutex_exit(&dtrace_lock);
8867 8872 mutex_exit(&dtrace_meta_lock);
8868 8873 cmn_err(CE_WARN, "failed to register meta-register %s: "
8869 8874 "user-land meta-provider exists", name);
8870 8875 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
8871 8876 kmem_free(meta, sizeof (dtrace_meta_t));
8872 8877 return (EINVAL);
8873 8878 }
8874 8879
8875 8880 dtrace_meta_pid = meta;
8876 8881 *idp = (dtrace_meta_provider_id_t)meta;
8877 8882
8878 8883 /*
8879 8884 * If there are providers and probes ready to go, pass them
8880 8885 * off to the new meta provider now.
8881 8886 */
8882 8887
8883 8888 help = dtrace_deferred_pid;
8884 8889 dtrace_deferred_pid = NULL;
8885 8890
8886 8891 mutex_exit(&dtrace_lock);
8887 8892
8888 8893 while (help != NULL) {
8889 8894 for (i = 0; i < help->dthps_nprovs; i++) {
8890 8895 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
8891 8896 help->dthps_pid);
8892 8897 }
8893 8898
8894 8899 next = help->dthps_next;
8895 8900 help->dthps_next = NULL;
8896 8901 help->dthps_prev = NULL;
8897 8902 help->dthps_deferred = 0;
8898 8903 help = next;
8899 8904 }
8900 8905
8901 8906 mutex_exit(&dtrace_meta_lock);
8902 8907
8903 8908 return (0);
8904 8909 }
8905 8910
8906 8911 int
8907 8912 dtrace_meta_unregister(dtrace_meta_provider_id_t id)
8908 8913 {
8909 8914 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
8910 8915
8911 8916 mutex_enter(&dtrace_meta_lock);
8912 8917 mutex_enter(&dtrace_lock);
8913 8918
8914 8919 if (old == dtrace_meta_pid) {
8915 8920 pp = &dtrace_meta_pid;
8916 8921 } else {
8917 8922 panic("attempt to unregister non-existent "
8918 8923 "dtrace meta-provider %p\n", (void *)old);
8919 8924 }
8920 8925
8921 8926 if (old->dtm_count != 0) {
8922 8927 mutex_exit(&dtrace_lock);
8923 8928 mutex_exit(&dtrace_meta_lock);
8924 8929 return (EBUSY);
8925 8930 }
8926 8931
8927 8932 *pp = NULL;
8928 8933
8929 8934 mutex_exit(&dtrace_lock);
8930 8935 mutex_exit(&dtrace_meta_lock);
8931 8936
8932 8937 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
8933 8938 kmem_free(old, sizeof (dtrace_meta_t));
8934 8939
8935 8940 return (0);
8936 8941 }
8937 8942
8938 8943
8939 8944 /*
8940 8945 * DTrace DIF Object Functions
8941 8946 */
8942 8947 static int
8943 8948 dtrace_difo_err(uint_t pc, const char *format, ...)
8944 8949 {
8945 8950 if (dtrace_err_verbose) {
8946 8951 va_list alist;
8947 8952
8948 8953 (void) uprintf("dtrace DIF object error: [%u]: ", pc);
8949 8954 va_start(alist, format);
8950 8955 (void) vuprintf(format, alist);
8951 8956 va_end(alist);
8952 8957 }
8953 8958
8954 8959 #ifdef DTRACE_ERRDEBUG
8955 8960 dtrace_errdebug(format);
8956 8961 #endif
8957 8962 return (1);
8958 8963 }
8959 8964
8960 8965 /*
8961 8966 * Validate a DTrace DIF object by checking the IR instructions. The following
8962 8967 * rules are currently enforced by dtrace_difo_validate():
8963 8968 *
8964 8969 * 1. Each instruction must have a valid opcode
8965 8970 * 2. Each register, string, variable, or subroutine reference must be valid
8966 8971 * 3. No instruction can modify register %r0 (must be zero)
8967 8972 * 4. All instruction reserved bits must be set to zero
8968 8973 * 5. The last instruction must be a "ret" instruction
8969 8974 * 6. All branch targets must reference a valid instruction _after_ the branch
8970 8975 */
8971 8976 static int
8972 8977 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
8973 8978 cred_t *cr)
8974 8979 {
8975 8980 int err = 0, i;
8976 8981 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
8977 8982 int kcheckload;
8978 8983 uint_t pc;
8979 8984
8980 8985 kcheckload = cr == NULL ||
8981 8986 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
8982 8987
8983 8988 dp->dtdo_destructive = 0;
8984 8989
8985 8990 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
8986 8991 dif_instr_t instr = dp->dtdo_buf[pc];
8987 8992
8988 8993 uint_t r1 = DIF_INSTR_R1(instr);
8989 8994 uint_t r2 = DIF_INSTR_R2(instr);
8990 8995 uint_t rd = DIF_INSTR_RD(instr);
8991 8996 uint_t rs = DIF_INSTR_RS(instr);
8992 8997 uint_t label = DIF_INSTR_LABEL(instr);
8993 8998 uint_t v = DIF_INSTR_VAR(instr);
8994 8999 uint_t subr = DIF_INSTR_SUBR(instr);
8995 9000 uint_t type = DIF_INSTR_TYPE(instr);
8996 9001 uint_t op = DIF_INSTR_OP(instr);
8997 9002
8998 9003 switch (op) {
8999 9004 case DIF_OP_OR:
9000 9005 case DIF_OP_XOR:
9001 9006 case DIF_OP_AND:
9002 9007 case DIF_OP_SLL:
9003 9008 case DIF_OP_SRL:
9004 9009 case DIF_OP_SRA:
9005 9010 case DIF_OP_SUB:
9006 9011 case DIF_OP_ADD:
9007 9012 case DIF_OP_MUL:
9008 9013 case DIF_OP_SDIV:
9009 9014 case DIF_OP_UDIV:
9010 9015 case DIF_OP_SREM:
9011 9016 case DIF_OP_UREM:
9012 9017 case DIF_OP_COPYS:
9013 9018 if (r1 >= nregs)
9014 9019 err += efunc(pc, "invalid register %u\n", r1);
9015 9020 if (r2 >= nregs)
9016 9021 err += efunc(pc, "invalid register %u\n", r2);
9017 9022 if (rd >= nregs)
9018 9023 err += efunc(pc, "invalid register %u\n", rd);
9019 9024 if (rd == 0)
9020 9025 err += efunc(pc, "cannot write to %r0\n");
9021 9026 break;
9022 9027 case DIF_OP_NOT:
9023 9028 case DIF_OP_MOV:
9024 9029 case DIF_OP_ALLOCS:
9025 9030 if (r1 >= nregs)
9026 9031 err += efunc(pc, "invalid register %u\n", r1);
9027 9032 if (r2 != 0)
9028 9033 err += efunc(pc, "non-zero reserved bits\n");
9029 9034 if (rd >= nregs)
9030 9035 err += efunc(pc, "invalid register %u\n", rd);
9031 9036 if (rd == 0)
9032 9037 err += efunc(pc, "cannot write to %r0\n");
9033 9038 break;
9034 9039 case DIF_OP_LDSB:
9035 9040 case DIF_OP_LDSH:
9036 9041 case DIF_OP_LDSW:
9037 9042 case DIF_OP_LDUB:
9038 9043 case DIF_OP_LDUH:
9039 9044 case DIF_OP_LDUW:
9040 9045 case DIF_OP_LDX:
9041 9046 if (r1 >= nregs)
9042 9047 err += efunc(pc, "invalid register %u\n", r1);
9043 9048 if (r2 != 0)
9044 9049 err += efunc(pc, "non-zero reserved bits\n");
9045 9050 if (rd >= nregs)
9046 9051 err += efunc(pc, "invalid register %u\n", rd);
9047 9052 if (rd == 0)
9048 9053 err += efunc(pc, "cannot write to %r0\n");
9049 9054 if (kcheckload)
9050 9055 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
9051 9056 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
9052 9057 break;
9053 9058 case DIF_OP_RLDSB:
9054 9059 case DIF_OP_RLDSH:
9055 9060 case DIF_OP_RLDSW:
9056 9061 case DIF_OP_RLDUB:
9057 9062 case DIF_OP_RLDUH:
9058 9063 case DIF_OP_RLDUW:
9059 9064 case DIF_OP_RLDX:
9060 9065 if (r1 >= nregs)
9061 9066 err += efunc(pc, "invalid register %u\n", r1);
9062 9067 if (r2 != 0)
9063 9068 err += efunc(pc, "non-zero reserved bits\n");
9064 9069 if (rd >= nregs)
9065 9070 err += efunc(pc, "invalid register %u\n", rd);
9066 9071 if (rd == 0)
9067 9072 err += efunc(pc, "cannot write to %r0\n");
9068 9073 break;
9069 9074 case DIF_OP_ULDSB:
9070 9075 case DIF_OP_ULDSH:
9071 9076 case DIF_OP_ULDSW:
9072 9077 case DIF_OP_ULDUB:
9073 9078 case DIF_OP_ULDUH:
9074 9079 case DIF_OP_ULDUW:
9075 9080 case DIF_OP_ULDX:
9076 9081 if (r1 >= nregs)
9077 9082 err += efunc(pc, "invalid register %u\n", r1);
9078 9083 if (r2 != 0)
9079 9084 err += efunc(pc, "non-zero reserved bits\n");
9080 9085 if (rd >= nregs)
9081 9086 err += efunc(pc, "invalid register %u\n", rd);
9082 9087 if (rd == 0)
9083 9088 err += efunc(pc, "cannot write to %r0\n");
9084 9089 break;
9085 9090 case DIF_OP_STB:
9086 9091 case DIF_OP_STH:
9087 9092 case DIF_OP_STW:
9088 9093 case DIF_OP_STX:
9089 9094 if (r1 >= nregs)
9090 9095 err += efunc(pc, "invalid register %u\n", r1);
9091 9096 if (r2 != 0)
9092 9097 err += efunc(pc, "non-zero reserved bits\n");
9093 9098 if (rd >= nregs)
9094 9099 err += efunc(pc, "invalid register %u\n", rd);
9095 9100 if (rd == 0)
9096 9101 err += efunc(pc, "cannot write to 0 address\n");
9097 9102 break;
9098 9103 case DIF_OP_CMP:
9099 9104 case DIF_OP_SCMP:
9100 9105 if (r1 >= nregs)
9101 9106 err += efunc(pc, "invalid register %u\n", r1);
9102 9107 if (r2 >= nregs)
9103 9108 err += efunc(pc, "invalid register %u\n", r2);
9104 9109 if (rd != 0)
9105 9110 err += efunc(pc, "non-zero reserved bits\n");
9106 9111 break;
9107 9112 case DIF_OP_TST:
9108 9113 if (r1 >= nregs)
9109 9114 err += efunc(pc, "invalid register %u\n", r1);
9110 9115 if (r2 != 0 || rd != 0)
9111 9116 err += efunc(pc, "non-zero reserved bits\n");
9112 9117 break;
9113 9118 case DIF_OP_BA:
9114 9119 case DIF_OP_BE:
9115 9120 case DIF_OP_BNE:
9116 9121 case DIF_OP_BG:
9117 9122 case DIF_OP_BGU:
9118 9123 case DIF_OP_BGE:
9119 9124 case DIF_OP_BGEU:
9120 9125 case DIF_OP_BL:
9121 9126 case DIF_OP_BLU:
9122 9127 case DIF_OP_BLE:
9123 9128 case DIF_OP_BLEU:
9124 9129 if (label >= dp->dtdo_len) {
9125 9130 err += efunc(pc, "invalid branch target %u\n",
9126 9131 label);
9127 9132 }
9128 9133 if (label <= pc) {
9129 9134 err += efunc(pc, "backward branch to %u\n",
9130 9135 label);
9131 9136 }
9132 9137 break;
9133 9138 case DIF_OP_RET:
9134 9139 if (r1 != 0 || r2 != 0)
9135 9140 err += efunc(pc, "non-zero reserved bits\n");
9136 9141 if (rd >= nregs)
9137 9142 err += efunc(pc, "invalid register %u\n", rd);
9138 9143 break;
9139 9144 case DIF_OP_NOP:
9140 9145 case DIF_OP_POPTS:
9141 9146 case DIF_OP_FLUSHTS:
9142 9147 if (r1 != 0 || r2 != 0 || rd != 0)
9143 9148 err += efunc(pc, "non-zero reserved bits\n");
9144 9149 break;
9145 9150 case DIF_OP_SETX:
9146 9151 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
9147 9152 err += efunc(pc, "invalid integer ref %u\n",
9148 9153 DIF_INSTR_INTEGER(instr));
9149 9154 }
9150 9155 if (rd >= nregs)
9151 9156 err += efunc(pc, "invalid register %u\n", rd);
9152 9157 if (rd == 0)
9153 9158 err += efunc(pc, "cannot write to %r0\n");
9154 9159 break;
9155 9160 case DIF_OP_SETS:
9156 9161 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
9157 9162 err += efunc(pc, "invalid string ref %u\n",
9158 9163 DIF_INSTR_STRING(instr));
9159 9164 }
9160 9165 if (rd >= nregs)
9161 9166 err += efunc(pc, "invalid register %u\n", rd);
9162 9167 if (rd == 0)
9163 9168 err += efunc(pc, "cannot write to %r0\n");
9164 9169 break;
9165 9170 case DIF_OP_LDGA:
9166 9171 case DIF_OP_LDTA:
9167 9172 if (r1 > DIF_VAR_ARRAY_MAX)
9168 9173 err += efunc(pc, "invalid array %u\n", r1);
9169 9174 if (r2 >= nregs)
9170 9175 err += efunc(pc, "invalid register %u\n", r2);
9171 9176 if (rd >= nregs)
9172 9177 err += efunc(pc, "invalid register %u\n", rd);
9173 9178 if (rd == 0)
9174 9179 err += efunc(pc, "cannot write to %r0\n");
9175 9180 break;
9176 9181 case DIF_OP_LDGS:
9177 9182 case DIF_OP_LDTS:
9178 9183 case DIF_OP_LDLS:
9179 9184 case DIF_OP_LDGAA:
9180 9185 case DIF_OP_LDTAA:
9181 9186 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
9182 9187 err += efunc(pc, "invalid variable %u\n", v);
9183 9188 if (rd >= nregs)
9184 9189 err += efunc(pc, "invalid register %u\n", rd);
9185 9190 if (rd == 0)
9186 9191 err += efunc(pc, "cannot write to %r0\n");
9187 9192 break;
9188 9193 case DIF_OP_STGS:
9189 9194 case DIF_OP_STTS:
9190 9195 case DIF_OP_STLS:
9191 9196 case DIF_OP_STGAA:
9192 9197 case DIF_OP_STTAA:
9193 9198 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
9194 9199 err += efunc(pc, "invalid variable %u\n", v);
9195 9200 if (rs >= nregs)
9196 9201 err += efunc(pc, "invalid register %u\n", rd);
9197 9202 break;
9198 9203 case DIF_OP_CALL:
9199 9204 if (subr > DIF_SUBR_MAX)
9200 9205 err += efunc(pc, "invalid subr %u\n", subr);
9201 9206 if (rd >= nregs)
9202 9207 err += efunc(pc, "invalid register %u\n", rd);
9203 9208 if (rd == 0)
9204 9209 err += efunc(pc, "cannot write to %r0\n");
9205 9210
9206 9211 if (subr == DIF_SUBR_COPYOUT ||
9207 9212 subr == DIF_SUBR_COPYOUTSTR) {
9208 9213 dp->dtdo_destructive = 1;
9209 9214 }
9210 9215
9211 9216 if (subr == DIF_SUBR_GETF) {
9212 9217 /*
9213 9218 * If we have a getf() we need to record that
9214 9219 * in our state. Note that our state can be
9215 9220 * NULL if this is a helper -- but in that
9216 9221 * case, the call to getf() is itself illegal,
9217 9222 * and will be caught (slightly later) when
9218 9223 * the helper is validated.
9219 9224 */
9220 9225 if (vstate->dtvs_state != NULL)
9221 9226 vstate->dtvs_state->dts_getf++;
9222 9227 }
9223 9228
9224 9229 break;
9225 9230 case DIF_OP_PUSHTR:
9226 9231 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
9227 9232 err += efunc(pc, "invalid ref type %u\n", type);
9228 9233 if (r2 >= nregs)
9229 9234 err += efunc(pc, "invalid register %u\n", r2);
9230 9235 if (rs >= nregs)
9231 9236 err += efunc(pc, "invalid register %u\n", rs);
9232 9237 break;
9233 9238 case DIF_OP_PUSHTV:
9234 9239 if (type != DIF_TYPE_CTF)
9235 9240 err += efunc(pc, "invalid val type %u\n", type);
9236 9241 if (r2 >= nregs)
9237 9242 err += efunc(pc, "invalid register %u\n", r2);
9238 9243 if (rs >= nregs)
9239 9244 err += efunc(pc, "invalid register %u\n", rs);
9240 9245 break;
9241 9246 default:
9242 9247 err += efunc(pc, "invalid opcode %u\n",
9243 9248 DIF_INSTR_OP(instr));
9244 9249 }
9245 9250 }
9246 9251
9247 9252 if (dp->dtdo_len != 0 &&
9248 9253 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
9249 9254 err += efunc(dp->dtdo_len - 1,
9250 9255 "expected 'ret' as last DIF instruction\n");
9251 9256 }
9252 9257
9253 9258 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) {
9254 9259 /*
9255 9260 * If we're not returning by reference, the size must be either
9256 9261 * 0 or the size of one of the base types.
9257 9262 */
9258 9263 switch (dp->dtdo_rtype.dtdt_size) {
9259 9264 case 0:
9260 9265 case sizeof (uint8_t):
9261 9266 case sizeof (uint16_t):
9262 9267 case sizeof (uint32_t):
9263 9268 case sizeof (uint64_t):
9264 9269 break;
9265 9270
9266 9271 default:
9267 9272 err += efunc(dp->dtdo_len - 1, "bad return size\n");
9268 9273 }
9269 9274 }
9270 9275
9271 9276 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
9272 9277 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
9273 9278 dtrace_diftype_t *vt, *et;
9274 9279 uint_t id, ndx;
9275 9280
9276 9281 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
9277 9282 v->dtdv_scope != DIFV_SCOPE_THREAD &&
9278 9283 v->dtdv_scope != DIFV_SCOPE_LOCAL) {
9279 9284 err += efunc(i, "unrecognized variable scope %d\n",
9280 9285 v->dtdv_scope);
9281 9286 break;
9282 9287 }
9283 9288
9284 9289 if (v->dtdv_kind != DIFV_KIND_ARRAY &&
9285 9290 v->dtdv_kind != DIFV_KIND_SCALAR) {
9286 9291 err += efunc(i, "unrecognized variable type %d\n",
9287 9292 v->dtdv_kind);
9288 9293 break;
9289 9294 }
9290 9295
9291 9296 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
9292 9297 err += efunc(i, "%d exceeds variable id limit\n", id);
9293 9298 break;
9294 9299 }
9295 9300
9296 9301 if (id < DIF_VAR_OTHER_UBASE)
9297 9302 continue;
9298 9303
9299 9304 /*
9300 9305 * For user-defined variables, we need to check that this
9301 9306 * definition is identical to any previous definition that we
9302 9307 * encountered.
9303 9308 */
9304 9309 ndx = id - DIF_VAR_OTHER_UBASE;
9305 9310
9306 9311 switch (v->dtdv_scope) {
9307 9312 case DIFV_SCOPE_GLOBAL:
9308 9313 if (ndx < vstate->dtvs_nglobals) {
9309 9314 dtrace_statvar_t *svar;
9310 9315
9311 9316 if ((svar = vstate->dtvs_globals[ndx]) != NULL)
9312 9317 existing = &svar->dtsv_var;
9313 9318 }
9314 9319
9315 9320 break;
9316 9321
9317 9322 case DIFV_SCOPE_THREAD:
9318 9323 if (ndx < vstate->dtvs_ntlocals)
9319 9324 existing = &vstate->dtvs_tlocals[ndx];
9320 9325 break;
9321 9326
9322 9327 case DIFV_SCOPE_LOCAL:
9323 9328 if (ndx < vstate->dtvs_nlocals) {
9324 9329 dtrace_statvar_t *svar;
9325 9330
9326 9331 if ((svar = vstate->dtvs_locals[ndx]) != NULL)
9327 9332 existing = &svar->dtsv_var;
9328 9333 }
9329 9334
9330 9335 break;
9331 9336 }
9332 9337
9333 9338 vt = &v->dtdv_type;
9334 9339
9335 9340 if (vt->dtdt_flags & DIF_TF_BYREF) {
9336 9341 if (vt->dtdt_size == 0) {
9337 9342 err += efunc(i, "zero-sized variable\n");
9338 9343 break;
9339 9344 }
9340 9345
9341 9346 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL &&
9342 9347 vt->dtdt_size > dtrace_global_maxsize) {
9343 9348 err += efunc(i, "oversized by-ref global\n");
9344 9349 break;
9345 9350 }
9346 9351 }
9347 9352
9348 9353 if (existing == NULL || existing->dtdv_id == 0)
9349 9354 continue;
9350 9355
9351 9356 ASSERT(existing->dtdv_id == v->dtdv_id);
9352 9357 ASSERT(existing->dtdv_scope == v->dtdv_scope);
9353 9358
9354 9359 if (existing->dtdv_kind != v->dtdv_kind)
9355 9360 err += efunc(i, "%d changed variable kind\n", id);
9356 9361
9357 9362 et = &existing->dtdv_type;
9358 9363
9359 9364 if (vt->dtdt_flags != et->dtdt_flags) {
9360 9365 err += efunc(i, "%d changed variable type flags\n", id);
9361 9366 break;
9362 9367 }
9363 9368
9364 9369 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
9365 9370 err += efunc(i, "%d changed variable type size\n", id);
9366 9371 break;
9367 9372 }
9368 9373 }
9369 9374
9370 9375 return (err);
9371 9376 }
9372 9377
9373 9378 /*
9374 9379 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
9375 9380 * are much more constrained than normal DIFOs. Specifically, they may
9376 9381 * not:
9377 9382 *
9378 9383 * 1. Make calls to subroutines other than copyin(), copyinstr() or
9379 9384 * miscellaneous string routines
9380 9385 * 2. Access DTrace variables other than the args[] array, and the
9381 9386 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
9382 9387 * 3. Have thread-local variables.
9383 9388 * 4. Have dynamic variables.
9384 9389 */
9385 9390 static int
9386 9391 dtrace_difo_validate_helper(dtrace_difo_t *dp)
9387 9392 {
9388 9393 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
9389 9394 int err = 0;
9390 9395 uint_t pc;
9391 9396
9392 9397 for (pc = 0; pc < dp->dtdo_len; pc++) {
9393 9398 dif_instr_t instr = dp->dtdo_buf[pc];
9394 9399
9395 9400 uint_t v = DIF_INSTR_VAR(instr);
9396 9401 uint_t subr = DIF_INSTR_SUBR(instr);
9397 9402 uint_t op = DIF_INSTR_OP(instr);
9398 9403
9399 9404 switch (op) {
9400 9405 case DIF_OP_OR:
9401 9406 case DIF_OP_XOR:
9402 9407 case DIF_OP_AND:
9403 9408 case DIF_OP_SLL:
9404 9409 case DIF_OP_SRL:
9405 9410 case DIF_OP_SRA:
9406 9411 case DIF_OP_SUB:
9407 9412 case DIF_OP_ADD:
9408 9413 case DIF_OP_MUL:
9409 9414 case DIF_OP_SDIV:
9410 9415 case DIF_OP_UDIV:
9411 9416 case DIF_OP_SREM:
9412 9417 case DIF_OP_UREM:
9413 9418 case DIF_OP_COPYS:
9414 9419 case DIF_OP_NOT:
9415 9420 case DIF_OP_MOV:
9416 9421 case DIF_OP_RLDSB:
9417 9422 case DIF_OP_RLDSH:
9418 9423 case DIF_OP_RLDSW:
9419 9424 case DIF_OP_RLDUB:
9420 9425 case DIF_OP_RLDUH:
9421 9426 case DIF_OP_RLDUW:
9422 9427 case DIF_OP_RLDX:
9423 9428 case DIF_OP_ULDSB:
9424 9429 case DIF_OP_ULDSH:
9425 9430 case DIF_OP_ULDSW:
9426 9431 case DIF_OP_ULDUB:
9427 9432 case DIF_OP_ULDUH:
9428 9433 case DIF_OP_ULDUW:
9429 9434 case DIF_OP_ULDX:
9430 9435 case DIF_OP_STB:
9431 9436 case DIF_OP_STH:
9432 9437 case DIF_OP_STW:
9433 9438 case DIF_OP_STX:
9434 9439 case DIF_OP_ALLOCS:
9435 9440 case DIF_OP_CMP:
9436 9441 case DIF_OP_SCMP:
9437 9442 case DIF_OP_TST:
9438 9443 case DIF_OP_BA:
9439 9444 case DIF_OP_BE:
9440 9445 case DIF_OP_BNE:
9441 9446 case DIF_OP_BG:
9442 9447 case DIF_OP_BGU:
9443 9448 case DIF_OP_BGE:
9444 9449 case DIF_OP_BGEU:
9445 9450 case DIF_OP_BL:
9446 9451 case DIF_OP_BLU:
9447 9452 case DIF_OP_BLE:
9448 9453 case DIF_OP_BLEU:
9449 9454 case DIF_OP_RET:
9450 9455 case DIF_OP_NOP:
9451 9456 case DIF_OP_POPTS:
9452 9457 case DIF_OP_FLUSHTS:
9453 9458 case DIF_OP_SETX:
9454 9459 case DIF_OP_SETS:
9455 9460 case DIF_OP_LDGA:
9456 9461 case DIF_OP_LDLS:
9457 9462 case DIF_OP_STGS:
9458 9463 case DIF_OP_STLS:
9459 9464 case DIF_OP_PUSHTR:
9460 9465 case DIF_OP_PUSHTV:
9461 9466 break;
9462 9467
9463 9468 case DIF_OP_LDGS:
9464 9469 if (v >= DIF_VAR_OTHER_UBASE)
9465 9470 break;
9466 9471
9467 9472 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
9468 9473 break;
9469 9474
9470 9475 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
9471 9476 v == DIF_VAR_PPID || v == DIF_VAR_TID ||
9472 9477 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
9473 9478 v == DIF_VAR_UID || v == DIF_VAR_GID)
9474 9479 break;
9475 9480
9476 9481 err += efunc(pc, "illegal variable %u\n", v);
9477 9482 break;
9478 9483
9479 9484 case DIF_OP_LDTA:
9480 9485 case DIF_OP_LDTS:
9481 9486 case DIF_OP_LDGAA:
9482 9487 case DIF_OP_LDTAA:
9483 9488 err += efunc(pc, "illegal dynamic variable load\n");
9484 9489 break;
9485 9490
9486 9491 case DIF_OP_STTS:
9487 9492 case DIF_OP_STGAA:
9488 9493 case DIF_OP_STTAA:
9489 9494 err += efunc(pc, "illegal dynamic variable store\n");
9490 9495 break;
9491 9496
9492 9497 case DIF_OP_CALL:
9493 9498 if (subr == DIF_SUBR_ALLOCA ||
9494 9499 subr == DIF_SUBR_BCOPY ||
9495 9500 subr == DIF_SUBR_COPYIN ||
9496 9501 subr == DIF_SUBR_COPYINTO ||
9497 9502 subr == DIF_SUBR_COPYINSTR ||
9498 9503 subr == DIF_SUBR_INDEX ||
9499 9504 subr == DIF_SUBR_INET_NTOA ||
9500 9505 subr == DIF_SUBR_INET_NTOA6 ||
9501 9506 subr == DIF_SUBR_INET_NTOP ||
9502 9507 subr == DIF_SUBR_JSON ||
9503 9508 subr == DIF_SUBR_LLTOSTR ||
9504 9509 subr == DIF_SUBR_STRTOLL ||
9505 9510 subr == DIF_SUBR_RINDEX ||
9506 9511 subr == DIF_SUBR_STRCHR ||
9507 9512 subr == DIF_SUBR_STRJOIN ||
9508 9513 subr == DIF_SUBR_STRRCHR ||
9509 9514 subr == DIF_SUBR_STRSTR ||
9510 9515 subr == DIF_SUBR_HTONS ||
9511 9516 subr == DIF_SUBR_HTONL ||
9512 9517 subr == DIF_SUBR_HTONLL ||
9513 9518 subr == DIF_SUBR_NTOHS ||
9514 9519 subr == DIF_SUBR_NTOHL ||
9515 9520 subr == DIF_SUBR_NTOHLL)
9516 9521 break;
9517 9522
9518 9523 err += efunc(pc, "invalid subr %u\n", subr);
9519 9524 break;
9520 9525
9521 9526 default:
9522 9527 err += efunc(pc, "invalid opcode %u\n",
9523 9528 DIF_INSTR_OP(instr));
9524 9529 }
9525 9530 }
9526 9531
9527 9532 return (err);
9528 9533 }
9529 9534
9530 9535 /*
9531 9536 * Returns 1 if the expression in the DIF object can be cached on a per-thread
9532 9537 * basis; 0 if not.
9533 9538 */
9534 9539 static int
9535 9540 dtrace_difo_cacheable(dtrace_difo_t *dp)
9536 9541 {
9537 9542 int i;
9538 9543
9539 9544 if (dp == NULL)
9540 9545 return (0);
9541 9546
9542 9547 for (i = 0; i < dp->dtdo_varlen; i++) {
9543 9548 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9544 9549
9545 9550 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
9546 9551 continue;
9547 9552
9548 9553 switch (v->dtdv_id) {
9549 9554 case DIF_VAR_CURTHREAD:
9550 9555 case DIF_VAR_PID:
9551 9556 case DIF_VAR_TID:
9552 9557 case DIF_VAR_EXECNAME:
9553 9558 case DIF_VAR_ZONENAME:
9554 9559 break;
9555 9560
9556 9561 default:
9557 9562 return (0);
9558 9563 }
9559 9564 }
9560 9565
9561 9566 /*
9562 9567 * This DIF object may be cacheable. Now we need to look for any
9563 9568 * array loading instructions, any memory loading instructions, or
9564 9569 * any stores to thread-local variables.
9565 9570 */
9566 9571 for (i = 0; i < dp->dtdo_len; i++) {
9567 9572 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
9568 9573
9569 9574 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
9570 9575 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
9571 9576 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
9572 9577 op == DIF_OP_LDGA || op == DIF_OP_STTS)
9573 9578 return (0);
9574 9579 }
9575 9580
9576 9581 return (1);
9577 9582 }
9578 9583
9579 9584 static void
9580 9585 dtrace_difo_hold(dtrace_difo_t *dp)
9581 9586 {
9582 9587 int i;
9583 9588
9584 9589 ASSERT(MUTEX_HELD(&dtrace_lock));
9585 9590
9586 9591 dp->dtdo_refcnt++;
9587 9592 ASSERT(dp->dtdo_refcnt != 0);
9588 9593
9589 9594 /*
9590 9595 * We need to check this DIF object for references to the variable
9591 9596 * DIF_VAR_VTIMESTAMP.
9592 9597 */
9593 9598 for (i = 0; i < dp->dtdo_varlen; i++) {
9594 9599 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9595 9600
9596 9601 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9597 9602 continue;
9598 9603
9599 9604 if (dtrace_vtime_references++ == 0)
9600 9605 dtrace_vtime_enable();
9601 9606 }
9602 9607 }
9603 9608
9604 9609 /*
9605 9610 * This routine calculates the dynamic variable chunksize for a given DIF
9606 9611 * object. The calculation is not fool-proof, and can probably be tricked by
9607 9612 * malicious DIF -- but it works for all compiler-generated DIF. Because this
9608 9613 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
9609 9614 * if a dynamic variable size exceeds the chunksize.
9610 9615 */
9611 9616 static void
9612 9617 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9613 9618 {
9614 9619 uint64_t sval;
9615 9620 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
9616 9621 const dif_instr_t *text = dp->dtdo_buf;
9617 9622 uint_t pc, srd = 0;
9618 9623 uint_t ttop = 0;
9619 9624 size_t size, ksize;
9620 9625 uint_t id, i;
9621 9626
9622 9627 for (pc = 0; pc < dp->dtdo_len; pc++) {
9623 9628 dif_instr_t instr = text[pc];
9624 9629 uint_t op = DIF_INSTR_OP(instr);
9625 9630 uint_t rd = DIF_INSTR_RD(instr);
9626 9631 uint_t r1 = DIF_INSTR_R1(instr);
9627 9632 uint_t nkeys = 0;
9628 9633 uchar_t scope;
9629 9634
9630 9635 dtrace_key_t *key = tupregs;
9631 9636
9632 9637 switch (op) {
9633 9638 case DIF_OP_SETX:
9634 9639 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
9635 9640 srd = rd;
9636 9641 continue;
9637 9642
9638 9643 case DIF_OP_STTS:
9639 9644 key = &tupregs[DIF_DTR_NREGS];
9640 9645 key[0].dttk_size = 0;
9641 9646 key[1].dttk_size = 0;
9642 9647 nkeys = 2;
9643 9648 scope = DIFV_SCOPE_THREAD;
9644 9649 break;
9645 9650
9646 9651 case DIF_OP_STGAA:
9647 9652 case DIF_OP_STTAA:
9648 9653 nkeys = ttop;
9649 9654
9650 9655 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
9651 9656 key[nkeys++].dttk_size = 0;
9652 9657
9653 9658 key[nkeys++].dttk_size = 0;
9654 9659
9655 9660 if (op == DIF_OP_STTAA) {
9656 9661 scope = DIFV_SCOPE_THREAD;
9657 9662 } else {
9658 9663 scope = DIFV_SCOPE_GLOBAL;
9659 9664 }
9660 9665
9661 9666 break;
9662 9667
9663 9668 case DIF_OP_PUSHTR:
9664 9669 if (ttop == DIF_DTR_NREGS)
9665 9670 return;
9666 9671
9667 9672 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
9668 9673 /*
9669 9674 * If the register for the size of the "pushtr"
9670 9675 * is %r0 (or the value is 0) and the type is
9671 9676 * a string, we'll use the system-wide default
9672 9677 * string size.
9673 9678 */
9674 9679 tupregs[ttop++].dttk_size =
9675 9680 dtrace_strsize_default;
9676 9681 } else {
9677 9682 if (srd == 0)
9678 9683 return;
9679 9684
9680 9685 tupregs[ttop++].dttk_size = sval;
9681 9686 }
9682 9687
9683 9688 break;
9684 9689
9685 9690 case DIF_OP_PUSHTV:
9686 9691 if (ttop == DIF_DTR_NREGS)
9687 9692 return;
9688 9693
9689 9694 tupregs[ttop++].dttk_size = 0;
9690 9695 break;
9691 9696
9692 9697 case DIF_OP_FLUSHTS:
9693 9698 ttop = 0;
9694 9699 break;
9695 9700
9696 9701 case DIF_OP_POPTS:
9697 9702 if (ttop != 0)
9698 9703 ttop--;
9699 9704 break;
9700 9705 }
9701 9706
9702 9707 sval = 0;
9703 9708 srd = 0;
9704 9709
9705 9710 if (nkeys == 0)
9706 9711 continue;
9707 9712
9708 9713 /*
9709 9714 * We have a dynamic variable allocation; calculate its size.
9710 9715 */
9711 9716 for (ksize = 0, i = 0; i < nkeys; i++)
9712 9717 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
9713 9718
9714 9719 size = sizeof (dtrace_dynvar_t);
9715 9720 size += sizeof (dtrace_key_t) * (nkeys - 1);
9716 9721 size += ksize;
9717 9722
9718 9723 /*
9719 9724 * Now we need to determine the size of the stored data.
9720 9725 */
9721 9726 id = DIF_INSTR_VAR(instr);
9722 9727
9723 9728 for (i = 0; i < dp->dtdo_varlen; i++) {
9724 9729 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9725 9730
9726 9731 if (v->dtdv_id == id && v->dtdv_scope == scope) {
9727 9732 size += v->dtdv_type.dtdt_size;
9728 9733 break;
9729 9734 }
9730 9735 }
9731 9736
9732 9737 if (i == dp->dtdo_varlen)
9733 9738 return;
9734 9739
9735 9740 /*
9736 9741 * We have the size. If this is larger than the chunk size
9737 9742 * for our dynamic variable state, reset the chunk size.
9738 9743 */
9739 9744 size = P2ROUNDUP(size, sizeof (uint64_t));
9740 9745
9741 9746 if (size > vstate->dtvs_dynvars.dtds_chunksize)
9742 9747 vstate->dtvs_dynvars.dtds_chunksize = size;
9743 9748 }
9744 9749 }
9745 9750
9746 9751 static void
9747 9752 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9748 9753 {
9749 9754 int i, oldsvars, osz, nsz, otlocals, ntlocals;
9750 9755 uint_t id;
9751 9756
9752 9757 ASSERT(MUTEX_HELD(&dtrace_lock));
9753 9758 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
9754 9759
9755 9760 for (i = 0; i < dp->dtdo_varlen; i++) {
9756 9761 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9757 9762 dtrace_statvar_t *svar, ***svarp;
9758 9763 size_t dsize = 0;
9759 9764 uint8_t scope = v->dtdv_scope;
9760 9765 int *np;
9761 9766
9762 9767 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9763 9768 continue;
9764 9769
9765 9770 id -= DIF_VAR_OTHER_UBASE;
9766 9771
9767 9772 switch (scope) {
9768 9773 case DIFV_SCOPE_THREAD:
9769 9774 while (id >= (otlocals = vstate->dtvs_ntlocals)) {
9770 9775 dtrace_difv_t *tlocals;
9771 9776
9772 9777 if ((ntlocals = (otlocals << 1)) == 0)
9773 9778 ntlocals = 1;
9774 9779
9775 9780 osz = otlocals * sizeof (dtrace_difv_t);
9776 9781 nsz = ntlocals * sizeof (dtrace_difv_t);
9777 9782
9778 9783 tlocals = kmem_zalloc(nsz, KM_SLEEP);
9779 9784
9780 9785 if (osz != 0) {
9781 9786 bcopy(vstate->dtvs_tlocals,
9782 9787 tlocals, osz);
9783 9788 kmem_free(vstate->dtvs_tlocals, osz);
9784 9789 }
9785 9790
9786 9791 vstate->dtvs_tlocals = tlocals;
9787 9792 vstate->dtvs_ntlocals = ntlocals;
9788 9793 }
9789 9794
9790 9795 vstate->dtvs_tlocals[id] = *v;
9791 9796 continue;
9792 9797
9793 9798 case DIFV_SCOPE_LOCAL:
9794 9799 np = &vstate->dtvs_nlocals;
9795 9800 svarp = &vstate->dtvs_locals;
9796 9801
9797 9802 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9798 9803 dsize = NCPU * (v->dtdv_type.dtdt_size +
9799 9804 sizeof (uint64_t));
9800 9805 else
9801 9806 dsize = NCPU * sizeof (uint64_t);
9802 9807
9803 9808 break;
9804 9809
9805 9810 case DIFV_SCOPE_GLOBAL:
9806 9811 np = &vstate->dtvs_nglobals;
9807 9812 svarp = &vstate->dtvs_globals;
9808 9813
9809 9814 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9810 9815 dsize = v->dtdv_type.dtdt_size +
9811 9816 sizeof (uint64_t);
9812 9817
9813 9818 break;
9814 9819
9815 9820 default:
9816 9821 ASSERT(0);
9817 9822 }
9818 9823
9819 9824 while (id >= (oldsvars = *np)) {
9820 9825 dtrace_statvar_t **statics;
9821 9826 int newsvars, oldsize, newsize;
9822 9827
9823 9828 if ((newsvars = (oldsvars << 1)) == 0)
9824 9829 newsvars = 1;
9825 9830
9826 9831 oldsize = oldsvars * sizeof (dtrace_statvar_t *);
9827 9832 newsize = newsvars * sizeof (dtrace_statvar_t *);
9828 9833
9829 9834 statics = kmem_zalloc(newsize, KM_SLEEP);
9830 9835
9831 9836 if (oldsize != 0) {
9832 9837 bcopy(*svarp, statics, oldsize);
9833 9838 kmem_free(*svarp, oldsize);
9834 9839 }
9835 9840
9836 9841 *svarp = statics;
9837 9842 *np = newsvars;
9838 9843 }
9839 9844
9840 9845 if ((svar = (*svarp)[id]) == NULL) {
9841 9846 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
9842 9847 svar->dtsv_var = *v;
9843 9848
9844 9849 if ((svar->dtsv_size = dsize) != 0) {
9845 9850 svar->dtsv_data = (uint64_t)(uintptr_t)
9846 9851 kmem_zalloc(dsize, KM_SLEEP);
9847 9852 }
9848 9853
9849 9854 (*svarp)[id] = svar;
9850 9855 }
9851 9856
9852 9857 svar->dtsv_refcnt++;
9853 9858 }
9854 9859
9855 9860 dtrace_difo_chunksize(dp, vstate);
9856 9861 dtrace_difo_hold(dp);
9857 9862 }
9858 9863
9859 9864 static dtrace_difo_t *
9860 9865 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9861 9866 {
9862 9867 dtrace_difo_t *new;
9863 9868 size_t sz;
9864 9869
9865 9870 ASSERT(dp->dtdo_buf != NULL);
9866 9871 ASSERT(dp->dtdo_refcnt != 0);
9867 9872
9868 9873 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
9869 9874
9870 9875 ASSERT(dp->dtdo_buf != NULL);
9871 9876 sz = dp->dtdo_len * sizeof (dif_instr_t);
9872 9877 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
9873 9878 bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
9874 9879 new->dtdo_len = dp->dtdo_len;
9875 9880
9876 9881 if (dp->dtdo_strtab != NULL) {
9877 9882 ASSERT(dp->dtdo_strlen != 0);
9878 9883 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
9879 9884 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
9880 9885 new->dtdo_strlen = dp->dtdo_strlen;
9881 9886 }
9882 9887
9883 9888 if (dp->dtdo_inttab != NULL) {
9884 9889 ASSERT(dp->dtdo_intlen != 0);
9885 9890 sz = dp->dtdo_intlen * sizeof (uint64_t);
9886 9891 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
9887 9892 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
9888 9893 new->dtdo_intlen = dp->dtdo_intlen;
9889 9894 }
9890 9895
9891 9896 if (dp->dtdo_vartab != NULL) {
9892 9897 ASSERT(dp->dtdo_varlen != 0);
9893 9898 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
9894 9899 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
9895 9900 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
9896 9901 new->dtdo_varlen = dp->dtdo_varlen;
9897 9902 }
9898 9903
9899 9904 dtrace_difo_init(new, vstate);
9900 9905 return (new);
9901 9906 }
9902 9907
9903 9908 static void
9904 9909 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9905 9910 {
9906 9911 int i;
9907 9912
9908 9913 ASSERT(dp->dtdo_refcnt == 0);
9909 9914
9910 9915 for (i = 0; i < dp->dtdo_varlen; i++) {
9911 9916 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9912 9917 dtrace_statvar_t *svar, **svarp;
9913 9918 uint_t id;
9914 9919 uint8_t scope = v->dtdv_scope;
9915 9920 int *np;
9916 9921
9917 9922 switch (scope) {
9918 9923 case DIFV_SCOPE_THREAD:
9919 9924 continue;
9920 9925
9921 9926 case DIFV_SCOPE_LOCAL:
9922 9927 np = &vstate->dtvs_nlocals;
9923 9928 svarp = vstate->dtvs_locals;
9924 9929 break;
9925 9930
9926 9931 case DIFV_SCOPE_GLOBAL:
9927 9932 np = &vstate->dtvs_nglobals;
9928 9933 svarp = vstate->dtvs_globals;
9929 9934 break;
9930 9935
9931 9936 default:
9932 9937 ASSERT(0);
9933 9938 }
9934 9939
9935 9940 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9936 9941 continue;
9937 9942
9938 9943 id -= DIF_VAR_OTHER_UBASE;
9939 9944 ASSERT(id < *np);
9940 9945
9941 9946 svar = svarp[id];
9942 9947 ASSERT(svar != NULL);
9943 9948 ASSERT(svar->dtsv_refcnt > 0);
9944 9949
9945 9950 if (--svar->dtsv_refcnt > 0)
9946 9951 continue;
9947 9952
9948 9953 if (svar->dtsv_size != 0) {
9949 9954 ASSERT(svar->dtsv_data != NULL);
9950 9955 kmem_free((void *)(uintptr_t)svar->dtsv_data,
9951 9956 svar->dtsv_size);
9952 9957 }
9953 9958
9954 9959 kmem_free(svar, sizeof (dtrace_statvar_t));
9955 9960 svarp[id] = NULL;
9956 9961 }
9957 9962
9958 9963 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
9959 9964 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
9960 9965 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
9961 9966 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
9962 9967
9963 9968 kmem_free(dp, sizeof (dtrace_difo_t));
9964 9969 }
9965 9970
9966 9971 static void
9967 9972 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9968 9973 {
9969 9974 int i;
9970 9975
9971 9976 ASSERT(MUTEX_HELD(&dtrace_lock));
9972 9977 ASSERT(dp->dtdo_refcnt != 0);
9973 9978
9974 9979 for (i = 0; i < dp->dtdo_varlen; i++) {
9975 9980 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9976 9981
9977 9982 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9978 9983 continue;
9979 9984
9980 9985 ASSERT(dtrace_vtime_references > 0);
9981 9986 if (--dtrace_vtime_references == 0)
9982 9987 dtrace_vtime_disable();
9983 9988 }
9984 9989
9985 9990 if (--dp->dtdo_refcnt == 0)
9986 9991 dtrace_difo_destroy(dp, vstate);
9987 9992 }
9988 9993
9989 9994 /*
9990 9995 * DTrace Format Functions
9991 9996 */
9992 9997 static uint16_t
9993 9998 dtrace_format_add(dtrace_state_t *state, char *str)
9994 9999 {
9995 10000 char *fmt, **new;
9996 10001 uint16_t ndx, len = strlen(str) + 1;
9997 10002
9998 10003 fmt = kmem_zalloc(len, KM_SLEEP);
9999 10004 bcopy(str, fmt, len);
10000 10005
10001 10006 for (ndx = 0; ndx < state->dts_nformats; ndx++) {
10002 10007 if (state->dts_formats[ndx] == NULL) {
10003 10008 state->dts_formats[ndx] = fmt;
10004 10009 return (ndx + 1);
10005 10010 }
10006 10011 }
10007 10012
10008 10013 if (state->dts_nformats == USHRT_MAX) {
10009 10014 /*
10010 10015 * This is only likely if a denial-of-service attack is being
10011 10016 * attempted. As such, it's okay to fail silently here.
10012 10017 */
10013 10018 kmem_free(fmt, len);
10014 10019 return (0);
10015 10020 }
10016 10021
10017 10022 /*
10018 10023 * For simplicity, we always resize the formats array to be exactly the
10019 10024 * number of formats.
10020 10025 */
10021 10026 ndx = state->dts_nformats++;
10022 10027 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
10023 10028
10024 10029 if (state->dts_formats != NULL) {
10025 10030 ASSERT(ndx != 0);
10026 10031 bcopy(state->dts_formats, new, ndx * sizeof (char *));
10027 10032 kmem_free(state->dts_formats, ndx * sizeof (char *));
10028 10033 }
10029 10034
10030 10035 state->dts_formats = new;
10031 10036 state->dts_formats[ndx] = fmt;
10032 10037
10033 10038 return (ndx + 1);
10034 10039 }
10035 10040
10036 10041 static void
10037 10042 dtrace_format_remove(dtrace_state_t *state, uint16_t format)
10038 10043 {
10039 10044 char *fmt;
10040 10045
10041 10046 ASSERT(state->dts_formats != NULL);
10042 10047 ASSERT(format <= state->dts_nformats);
10043 10048 ASSERT(state->dts_formats[format - 1] != NULL);
10044 10049
10045 10050 fmt = state->dts_formats[format - 1];
10046 10051 kmem_free(fmt, strlen(fmt) + 1);
10047 10052 state->dts_formats[format - 1] = NULL;
10048 10053 }
10049 10054
10050 10055 static void
10051 10056 dtrace_format_destroy(dtrace_state_t *state)
10052 10057 {
10053 10058 int i;
10054 10059
10055 10060 if (state->dts_nformats == 0) {
10056 10061 ASSERT(state->dts_formats == NULL);
10057 10062 return;
10058 10063 }
10059 10064
10060 10065 ASSERT(state->dts_formats != NULL);
10061 10066
10062 10067 for (i = 0; i < state->dts_nformats; i++) {
10063 10068 char *fmt = state->dts_formats[i];
10064 10069
10065 10070 if (fmt == NULL)
10066 10071 continue;
10067 10072
10068 10073 kmem_free(fmt, strlen(fmt) + 1);
10069 10074 }
10070 10075
10071 10076 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
10072 10077 state->dts_nformats = 0;
10073 10078 state->dts_formats = NULL;
10074 10079 }
10075 10080
10076 10081 /*
10077 10082 * DTrace Predicate Functions
10078 10083 */
10079 10084 static dtrace_predicate_t *
10080 10085 dtrace_predicate_create(dtrace_difo_t *dp)
10081 10086 {
10082 10087 dtrace_predicate_t *pred;
10083 10088
10084 10089 ASSERT(MUTEX_HELD(&dtrace_lock));
10085 10090 ASSERT(dp->dtdo_refcnt != 0);
10086 10091
10087 10092 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
10088 10093 pred->dtp_difo = dp;
10089 10094 pred->dtp_refcnt = 1;
10090 10095
10091 10096 if (!dtrace_difo_cacheable(dp))
10092 10097 return (pred);
10093 10098
10094 10099 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
10095 10100 /*
10096 10101 * This is only theoretically possible -- we have had 2^32
10097 10102 * cacheable predicates on this machine. We cannot allow any
10098 10103 * more predicates to become cacheable: as unlikely as it is,
10099 10104 * there may be a thread caching a (now stale) predicate cache
10100 10105 * ID. (N.B.: the temptation is being successfully resisted to
10101 10106 * have this cmn_err() "Holy shit -- we executed this code!")
10102 10107 */
10103 10108 return (pred);
10104 10109 }
10105 10110
10106 10111 pred->dtp_cacheid = dtrace_predcache_id++;
10107 10112
10108 10113 return (pred);
10109 10114 }
10110 10115
10111 10116 static void
10112 10117 dtrace_predicate_hold(dtrace_predicate_t *pred)
10113 10118 {
10114 10119 ASSERT(MUTEX_HELD(&dtrace_lock));
10115 10120 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
10116 10121 ASSERT(pred->dtp_refcnt > 0);
10117 10122
10118 10123 pred->dtp_refcnt++;
10119 10124 }
10120 10125
10121 10126 static void
10122 10127 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
10123 10128 {
10124 10129 dtrace_difo_t *dp = pred->dtp_difo;
10125 10130
10126 10131 ASSERT(MUTEX_HELD(&dtrace_lock));
10127 10132 ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
10128 10133 ASSERT(pred->dtp_refcnt > 0);
10129 10134
10130 10135 if (--pred->dtp_refcnt == 0) {
10131 10136 dtrace_difo_release(pred->dtp_difo, vstate);
10132 10137 kmem_free(pred, sizeof (dtrace_predicate_t));
10133 10138 }
10134 10139 }
10135 10140
10136 10141 /*
10137 10142 * DTrace Action Description Functions
10138 10143 */
10139 10144 static dtrace_actdesc_t *
10140 10145 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
10141 10146 uint64_t uarg, uint64_t arg)
10142 10147 {
10143 10148 dtrace_actdesc_t *act;
10144 10149
10145 10150 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
10146 10151 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA));
10147 10152
10148 10153 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
10149 10154 act->dtad_kind = kind;
10150 10155 act->dtad_ntuple = ntuple;
10151 10156 act->dtad_uarg = uarg;
10152 10157 act->dtad_arg = arg;
10153 10158 act->dtad_refcnt = 1;
10154 10159
10155 10160 return (act);
10156 10161 }
10157 10162
10158 10163 static void
10159 10164 dtrace_actdesc_hold(dtrace_actdesc_t *act)
10160 10165 {
10161 10166 ASSERT(act->dtad_refcnt >= 1);
10162 10167 act->dtad_refcnt++;
10163 10168 }
10164 10169
10165 10170 static void
10166 10171 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
10167 10172 {
10168 10173 dtrace_actkind_t kind = act->dtad_kind;
10169 10174 dtrace_difo_t *dp;
10170 10175
10171 10176 ASSERT(act->dtad_refcnt >= 1);
10172 10177
10173 10178 if (--act->dtad_refcnt != 0)
10174 10179 return;
10175 10180
10176 10181 if ((dp = act->dtad_difo) != NULL)
10177 10182 dtrace_difo_release(dp, vstate);
10178 10183
10179 10184 if (DTRACEACT_ISPRINTFLIKE(kind)) {
10180 10185 char *str = (char *)(uintptr_t)act->dtad_arg;
10181 10186
10182 10187 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
10183 10188 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
10184 10189
10185 10190 if (str != NULL)
10186 10191 kmem_free(str, strlen(str) + 1);
10187 10192 }
10188 10193
10189 10194 kmem_free(act, sizeof (dtrace_actdesc_t));
10190 10195 }
10191 10196
10192 10197 /*
10193 10198 * DTrace ECB Functions
10194 10199 */
10195 10200 static dtrace_ecb_t *
10196 10201 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
10197 10202 {
10198 10203 dtrace_ecb_t *ecb;
10199 10204 dtrace_epid_t epid;
10200 10205
10201 10206 ASSERT(MUTEX_HELD(&dtrace_lock));
10202 10207
10203 10208 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
10204 10209 ecb->dte_predicate = NULL;
10205 10210 ecb->dte_probe = probe;
10206 10211
10207 10212 /*
10208 10213 * The default size is the size of the default action: recording
10209 10214 * the header.
10210 10215 */
10211 10216 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
10212 10217 ecb->dte_alignment = sizeof (dtrace_epid_t);
10213 10218
10214 10219 epid = state->dts_epid++;
10215 10220
10216 10221 if (epid - 1 >= state->dts_necbs) {
10217 10222 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
10218 10223 int necbs = state->dts_necbs << 1;
10219 10224
10220 10225 ASSERT(epid == state->dts_necbs + 1);
10221 10226
10222 10227 if (necbs == 0) {
10223 10228 ASSERT(oecbs == NULL);
10224 10229 necbs = 1;
10225 10230 }
10226 10231
10227 10232 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
10228 10233
10229 10234 if (oecbs != NULL)
10230 10235 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
10231 10236
10232 10237 dtrace_membar_producer();
10233 10238 state->dts_ecbs = ecbs;
10234 10239
10235 10240 if (oecbs != NULL) {
10236 10241 /*
10237 10242 * If this state is active, we must dtrace_sync()
10238 10243 * before we can free the old dts_ecbs array: we're
10239 10244 * coming in hot, and there may be active ring
10240 10245 * buffer processing (which indexes into the dts_ecbs
10241 10246 * array) on another CPU.
10242 10247 */
10243 10248 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
10244 10249 dtrace_sync();
10245 10250
10246 10251 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
10247 10252 }
10248 10253
10249 10254 dtrace_membar_producer();
10250 10255 state->dts_necbs = necbs;
10251 10256 }
10252 10257
10253 10258 ecb->dte_state = state;
10254 10259
10255 10260 ASSERT(state->dts_ecbs[epid - 1] == NULL);
10256 10261 dtrace_membar_producer();
10257 10262 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
10258 10263
10259 10264 return (ecb);
10260 10265 }
10261 10266
10262 10267 static int
10263 10268 dtrace_ecb_enable(dtrace_ecb_t *ecb)
10264 10269 {
10265 10270 dtrace_probe_t *probe = ecb->dte_probe;
10266 10271
10267 10272 ASSERT(MUTEX_HELD(&cpu_lock));
10268 10273 ASSERT(MUTEX_HELD(&dtrace_lock));
10269 10274 ASSERT(ecb->dte_next == NULL);
10270 10275
10271 10276 if (probe == NULL) {
10272 10277 /*
10273 10278 * This is the NULL probe -- there's nothing to do.
10274 10279 */
10275 10280 return (0);
10276 10281 }
10277 10282
10278 10283 if (probe->dtpr_ecb == NULL) {
10279 10284 dtrace_provider_t *prov = probe->dtpr_provider;
10280 10285
10281 10286 /*
10282 10287 * We're the first ECB on this probe.
10283 10288 */
10284 10289 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
10285 10290
10286 10291 if (ecb->dte_predicate != NULL)
10287 10292 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
10288 10293
10289 10294 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
10290 10295 probe->dtpr_id, probe->dtpr_arg));
10291 10296 } else {
10292 10297 /*
10293 10298 * This probe is already active. Swing the last pointer to
10294 10299 * point to the new ECB, and issue a dtrace_sync() to assure
10295 10300 * that all CPUs have seen the change.
10296 10301 */
10297 10302 ASSERT(probe->dtpr_ecb_last != NULL);
10298 10303 probe->dtpr_ecb_last->dte_next = ecb;
10299 10304 probe->dtpr_ecb_last = ecb;
10300 10305 probe->dtpr_predcache = 0;
10301 10306
10302 10307 dtrace_sync();
10303 10308 return (0);
10304 10309 }
10305 10310 }
10306 10311
10307 10312 static void
10308 10313 dtrace_ecb_resize(dtrace_ecb_t *ecb)
10309 10314 {
10310 10315 dtrace_action_t *act;
10311 10316 uint32_t curneeded = UINT32_MAX;
10312 10317 uint32_t aggbase = UINT32_MAX;
10313 10318
10314 10319 /*
10315 10320 * If we record anything, we always record the dtrace_rechdr_t. (And
10316 10321 * we always record it first.)
10317 10322 */
10318 10323 ecb->dte_size = sizeof (dtrace_rechdr_t);
10319 10324 ecb->dte_alignment = sizeof (dtrace_epid_t);
10320 10325
10321 10326 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
10322 10327 dtrace_recdesc_t *rec = &act->dta_rec;
10323 10328 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
10324 10329
10325 10330 ecb->dte_alignment = MAX(ecb->dte_alignment,
10326 10331 rec->dtrd_alignment);
10327 10332
10328 10333 if (DTRACEACT_ISAGG(act->dta_kind)) {
10329 10334 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
10330 10335
10331 10336 ASSERT(rec->dtrd_size != 0);
10332 10337 ASSERT(agg->dtag_first != NULL);
10333 10338 ASSERT(act->dta_prev->dta_intuple);
10334 10339 ASSERT(aggbase != UINT32_MAX);
10335 10340 ASSERT(curneeded != UINT32_MAX);
10336 10341
10337 10342 agg->dtag_base = aggbase;
10338 10343
10339 10344 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
10340 10345 rec->dtrd_offset = curneeded;
10341 10346 curneeded += rec->dtrd_size;
10342 10347 ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
10343 10348
10344 10349 aggbase = UINT32_MAX;
10345 10350 curneeded = UINT32_MAX;
10346 10351 } else if (act->dta_intuple) {
10347 10352 if (curneeded == UINT32_MAX) {
10348 10353 /*
10349 10354 * This is the first record in a tuple. Align
10350 10355 * curneeded to be at offset 4 in an 8-byte
10351 10356 * aligned block.
10352 10357 */
10353 10358 ASSERT(act->dta_prev == NULL ||
10354 10359 !act->dta_prev->dta_intuple);
10355 10360 ASSERT3U(aggbase, ==, UINT32_MAX);
10356 10361 curneeded = P2PHASEUP(ecb->dte_size,
10357 10362 sizeof (uint64_t), sizeof (dtrace_aggid_t));
10358 10363
10359 10364 aggbase = curneeded - sizeof (dtrace_aggid_t);
10360 10365 ASSERT(IS_P2ALIGNED(aggbase,
10361 10366 sizeof (uint64_t)));
10362 10367 }
10363 10368 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
10364 10369 rec->dtrd_offset = curneeded;
10365 10370 curneeded += rec->dtrd_size;
10366 10371 } else {
10367 10372 /* tuples must be followed by an aggregation */
10368 10373 ASSERT(act->dta_prev == NULL ||
10369 10374 !act->dta_prev->dta_intuple);
10370 10375
10371 10376 ecb->dte_size = P2ROUNDUP(ecb->dte_size,
10372 10377 rec->dtrd_alignment);
10373 10378 rec->dtrd_offset = ecb->dte_size;
10374 10379 ecb->dte_size += rec->dtrd_size;
10375 10380 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
10376 10381 }
10377 10382 }
10378 10383
10379 10384 if ((act = ecb->dte_action) != NULL &&
10380 10385 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
10381 10386 ecb->dte_size == sizeof (dtrace_rechdr_t)) {
10382 10387 /*
10383 10388 * If the size is still sizeof (dtrace_rechdr_t), then all
10384 10389 * actions store no data; set the size to 0.
10385 10390 */
10386 10391 ecb->dte_size = 0;
10387 10392 }
10388 10393
10389 10394 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
10390 10395 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
10391 10396 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed,
10392 10397 ecb->dte_needed);
10393 10398 }
10394 10399
10395 10400 static dtrace_action_t *
10396 10401 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
10397 10402 {
10398 10403 dtrace_aggregation_t *agg;
10399 10404 size_t size = sizeof (uint64_t);
10400 10405 int ntuple = desc->dtad_ntuple;
10401 10406 dtrace_action_t *act;
10402 10407 dtrace_recdesc_t *frec;
10403 10408 dtrace_aggid_t aggid;
10404 10409 dtrace_state_t *state = ecb->dte_state;
10405 10410
10406 10411 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
10407 10412 agg->dtag_ecb = ecb;
10408 10413
10409 10414 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
10410 10415
10411 10416 switch (desc->dtad_kind) {
10412 10417 case DTRACEAGG_MIN:
10413 10418 agg->dtag_initial = INT64_MAX;
10414 10419 agg->dtag_aggregate = dtrace_aggregate_min;
10415 10420 break;
10416 10421
10417 10422 case DTRACEAGG_MAX:
10418 10423 agg->dtag_initial = INT64_MIN;
10419 10424 agg->dtag_aggregate = dtrace_aggregate_max;
10420 10425 break;
10421 10426
10422 10427 case DTRACEAGG_COUNT:
10423 10428 agg->dtag_aggregate = dtrace_aggregate_count;
10424 10429 break;
10425 10430
10426 10431 case DTRACEAGG_QUANTIZE:
10427 10432 agg->dtag_aggregate = dtrace_aggregate_quantize;
10428 10433 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
10429 10434 sizeof (uint64_t);
10430 10435 break;
10431 10436
10432 10437 case DTRACEAGG_LQUANTIZE: {
10433 10438 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
10434 10439 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
10435 10440
10436 10441 agg->dtag_initial = desc->dtad_arg;
10437 10442 agg->dtag_aggregate = dtrace_aggregate_lquantize;
10438 10443
10439 10444 if (step == 0 || levels == 0)
10440 10445 goto err;
10441 10446
10442 10447 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
10443 10448 break;
10444 10449 }
10445 10450
10446 10451 case DTRACEAGG_LLQUANTIZE: {
10447 10452 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
10448 10453 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
10449 10454 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
10450 10455 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
10451 10456 int64_t v;
10452 10457
10453 10458 agg->dtag_initial = desc->dtad_arg;
10454 10459 agg->dtag_aggregate = dtrace_aggregate_llquantize;
10455 10460
10456 10461 if (factor < 2 || low >= high || nsteps < factor)
10457 10462 goto err;
10458 10463
10459 10464 /*
10460 10465 * Now check that the number of steps evenly divides a power
10461 10466 * of the factor. (This assures both integer bucket size and
10462 10467 * linearity within each magnitude.)
10463 10468 */
10464 10469 for (v = factor; v < nsteps; v *= factor)
10465 10470 continue;
10466 10471
10467 10472 if ((v % nsteps) || (nsteps % factor))
10468 10473 goto err;
10469 10474
10470 10475 size = (dtrace_aggregate_llquantize_bucket(factor,
10471 10476 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
10472 10477 break;
10473 10478 }
10474 10479
10475 10480 case DTRACEAGG_AVG:
10476 10481 agg->dtag_aggregate = dtrace_aggregate_avg;
10477 10482 size = sizeof (uint64_t) * 2;
10478 10483 break;
10479 10484
10480 10485 case DTRACEAGG_STDDEV:
10481 10486 agg->dtag_aggregate = dtrace_aggregate_stddev;
10482 10487 size = sizeof (uint64_t) * 4;
10483 10488 break;
10484 10489
10485 10490 case DTRACEAGG_SUM:
10486 10491 agg->dtag_aggregate = dtrace_aggregate_sum;
10487 10492 break;
10488 10493
10489 10494 default:
10490 10495 goto err;
10491 10496 }
10492 10497
10493 10498 agg->dtag_action.dta_rec.dtrd_size = size;
10494 10499
10495 10500 if (ntuple == 0)
10496 10501 goto err;
10497 10502
10498 10503 /*
10499 10504 * We must make sure that we have enough actions for the n-tuple.
10500 10505 */
10501 10506 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
10502 10507 if (DTRACEACT_ISAGG(act->dta_kind))
10503 10508 break;
10504 10509
10505 10510 if (--ntuple == 0) {
10506 10511 /*
10507 10512 * This is the action with which our n-tuple begins.
10508 10513 */
10509 10514 agg->dtag_first = act;
10510 10515 goto success;
10511 10516 }
10512 10517 }
10513 10518
10514 10519 /*
10515 10520 * This n-tuple is short by ntuple elements. Return failure.
10516 10521 */
10517 10522 ASSERT(ntuple != 0);
10518 10523 err:
10519 10524 kmem_free(agg, sizeof (dtrace_aggregation_t));
10520 10525 return (NULL);
10521 10526
10522 10527 success:
10523 10528 /*
10524 10529 * If the last action in the tuple has a size of zero, it's actually
10525 10530 * an expression argument for the aggregating action.
10526 10531 */
10527 10532 ASSERT(ecb->dte_action_last != NULL);
10528 10533 act = ecb->dte_action_last;
10529 10534
10530 10535 if (act->dta_kind == DTRACEACT_DIFEXPR) {
10531 10536 ASSERT(act->dta_difo != NULL);
10532 10537
10533 10538 if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
10534 10539 agg->dtag_hasarg = 1;
10535 10540 }
10536 10541
10537 10542 /*
10538 10543 * We need to allocate an id for this aggregation.
10539 10544 */
10540 10545 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
10541 10546 VM_BESTFIT | VM_SLEEP);
10542 10547
10543 10548 if (aggid - 1 >= state->dts_naggregations) {
10544 10549 dtrace_aggregation_t **oaggs = state->dts_aggregations;
10545 10550 dtrace_aggregation_t **aggs;
10546 10551 int naggs = state->dts_naggregations << 1;
10547 10552 int onaggs = state->dts_naggregations;
10548 10553
10549 10554 ASSERT(aggid == state->dts_naggregations + 1);
10550 10555
10551 10556 if (naggs == 0) {
10552 10557 ASSERT(oaggs == NULL);
10553 10558 naggs = 1;
10554 10559 }
10555 10560
10556 10561 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
10557 10562
10558 10563 if (oaggs != NULL) {
10559 10564 bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
10560 10565 kmem_free(oaggs, onaggs * sizeof (*aggs));
10561 10566 }
10562 10567
10563 10568 state->dts_aggregations = aggs;
10564 10569 state->dts_naggregations = naggs;
10565 10570 }
10566 10571
10567 10572 ASSERT(state->dts_aggregations[aggid - 1] == NULL);
10568 10573 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
10569 10574
10570 10575 frec = &agg->dtag_first->dta_rec;
10571 10576 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
10572 10577 frec->dtrd_alignment = sizeof (dtrace_aggid_t);
10573 10578
10574 10579 for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
10575 10580 ASSERT(!act->dta_intuple);
10576 10581 act->dta_intuple = 1;
10577 10582 }
10578 10583
10579 10584 return (&agg->dtag_action);
10580 10585 }
10581 10586
10582 10587 static void
10583 10588 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
10584 10589 {
10585 10590 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
10586 10591 dtrace_state_t *state = ecb->dte_state;
10587 10592 dtrace_aggid_t aggid = agg->dtag_id;
10588 10593
10589 10594 ASSERT(DTRACEACT_ISAGG(act->dta_kind));
10590 10595 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
10591 10596
10592 10597 ASSERT(state->dts_aggregations[aggid - 1] == agg);
10593 10598 state->dts_aggregations[aggid - 1] = NULL;
10594 10599
10595 10600 kmem_free(agg, sizeof (dtrace_aggregation_t));
10596 10601 }
10597 10602
10598 10603 static int
10599 10604 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
10600 10605 {
10601 10606 dtrace_action_t *action, *last;
10602 10607 dtrace_difo_t *dp = desc->dtad_difo;
10603 10608 uint32_t size = 0, align = sizeof (uint8_t), mask;
10604 10609 uint16_t format = 0;
10605 10610 dtrace_recdesc_t *rec;
10606 10611 dtrace_state_t *state = ecb->dte_state;
10607 10612 dtrace_optval_t *opt = state->dts_options, nframes, strsize;
10608 10613 uint64_t arg = desc->dtad_arg;
10609 10614
10610 10615 ASSERT(MUTEX_HELD(&dtrace_lock));
10611 10616 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
10612 10617
10613 10618 if (DTRACEACT_ISAGG(desc->dtad_kind)) {
10614 10619 /*
10615 10620 * If this is an aggregating action, there must be neither
10616 10621 * a speculate nor a commit on the action chain.
10617 10622 */
10618 10623 dtrace_action_t *act;
10619 10624
10620 10625 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
10621 10626 if (act->dta_kind == DTRACEACT_COMMIT)
10622 10627 return (EINVAL);
10623 10628
10624 10629 if (act->dta_kind == DTRACEACT_SPECULATE)
10625 10630 return (EINVAL);
10626 10631 }
10627 10632
10628 10633 action = dtrace_ecb_aggregation_create(ecb, desc);
10629 10634
10630 10635 if (action == NULL)
10631 10636 return (EINVAL);
10632 10637 } else {
10633 10638 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
10634 10639 (desc->dtad_kind == DTRACEACT_DIFEXPR &&
10635 10640 dp != NULL && dp->dtdo_destructive)) {
10636 10641 state->dts_destructive = 1;
10637 10642 }
10638 10643
10639 10644 switch (desc->dtad_kind) {
10640 10645 case DTRACEACT_PRINTF:
10641 10646 case DTRACEACT_PRINTA:
10642 10647 case DTRACEACT_SYSTEM:
10643 10648 case DTRACEACT_FREOPEN:
10644 10649 case DTRACEACT_DIFEXPR:
10645 10650 /*
10646 10651 * We know that our arg is a string -- turn it into a
10647 10652 * format.
10648 10653 */
10649 10654 if (arg == NULL) {
10650 10655 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
10651 10656 desc->dtad_kind == DTRACEACT_DIFEXPR);
10652 10657 format = 0;
10653 10658 } else {
10654 10659 ASSERT(arg != NULL);
10655 10660 ASSERT(arg > KERNELBASE);
10656 10661 format = dtrace_format_add(state,
10657 10662 (char *)(uintptr_t)arg);
10658 10663 }
10659 10664
10660 10665 /*FALLTHROUGH*/
10661 10666 case DTRACEACT_LIBACT:
10662 10667 case DTRACEACT_TRACEMEM:
10663 10668 case DTRACEACT_TRACEMEM_DYNSIZE:
10664 10669 if (dp == NULL)
10665 10670 return (EINVAL);
10666 10671
10667 10672 if ((size = dp->dtdo_rtype.dtdt_size) != 0)
10668 10673 break;
10669 10674
10670 10675 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
10671 10676 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10672 10677 return (EINVAL);
10673 10678
10674 10679 size = opt[DTRACEOPT_STRSIZE];
10675 10680 }
10676 10681
10677 10682 break;
10678 10683
10679 10684 case DTRACEACT_STACK:
10680 10685 if ((nframes = arg) == 0) {
10681 10686 nframes = opt[DTRACEOPT_STACKFRAMES];
10682 10687 ASSERT(nframes > 0);
10683 10688 arg = nframes;
10684 10689 }
10685 10690
10686 10691 size = nframes * sizeof (pc_t);
10687 10692 break;
10688 10693
10689 10694 case DTRACEACT_JSTACK:
10690 10695 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
10691 10696 strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
10692 10697
10693 10698 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
10694 10699 nframes = opt[DTRACEOPT_JSTACKFRAMES];
10695 10700
10696 10701 arg = DTRACE_USTACK_ARG(nframes, strsize);
10697 10702
10698 10703 /*FALLTHROUGH*/
10699 10704 case DTRACEACT_USTACK:
10700 10705 if (desc->dtad_kind != DTRACEACT_JSTACK &&
10701 10706 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
10702 10707 strsize = DTRACE_USTACK_STRSIZE(arg);
10703 10708 nframes = opt[DTRACEOPT_USTACKFRAMES];
10704 10709 ASSERT(nframes > 0);
10705 10710 arg = DTRACE_USTACK_ARG(nframes, strsize);
10706 10711 }
10707 10712
10708 10713 /*
10709 10714 * Save a slot for the pid.
10710 10715 */
10711 10716 size = (nframes + 1) * sizeof (uint64_t);
10712 10717 size += DTRACE_USTACK_STRSIZE(arg);
10713 10718 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
10714 10719
10715 10720 break;
10716 10721
10717 10722 case DTRACEACT_SYM:
10718 10723 case DTRACEACT_MOD:
10719 10724 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
10720 10725 sizeof (uint64_t)) ||
10721 10726 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10722 10727 return (EINVAL);
10723 10728 break;
10724 10729
10725 10730 case DTRACEACT_USYM:
10726 10731 case DTRACEACT_UMOD:
10727 10732 case DTRACEACT_UADDR:
10728 10733 if (dp == NULL ||
10729 10734 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
10730 10735 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10731 10736 return (EINVAL);
10732 10737
10733 10738 /*
10734 10739 * We have a slot for the pid, plus a slot for the
10735 10740 * argument. To keep things simple (aligned with
10736 10741 * bitness-neutral sizing), we store each as a 64-bit
10737 10742 * quantity.
10738 10743 */
10739 10744 size = 2 * sizeof (uint64_t);
10740 10745 break;
10741 10746
10742 10747 case DTRACEACT_STOP:
10743 10748 case DTRACEACT_BREAKPOINT:
10744 10749 case DTRACEACT_PANIC:
10745 10750 break;
10746 10751
10747 10752 case DTRACEACT_CHILL:
10748 10753 case DTRACEACT_DISCARD:
10749 10754 case DTRACEACT_RAISE:
10750 10755 if (dp == NULL)
10751 10756 return (EINVAL);
10752 10757 break;
10753 10758
10754 10759 case DTRACEACT_EXIT:
10755 10760 if (dp == NULL ||
10756 10761 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
10757 10762 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10758 10763 return (EINVAL);
10759 10764 break;
10760 10765
10761 10766 case DTRACEACT_SPECULATE:
10762 10767 if (ecb->dte_size > sizeof (dtrace_rechdr_t))
10763 10768 return (EINVAL);
10764 10769
10765 10770 if (dp == NULL)
10766 10771 return (EINVAL);
10767 10772
10768 10773 state->dts_speculates = 1;
10769 10774 break;
10770 10775
10771 10776 case DTRACEACT_COMMIT: {
10772 10777 dtrace_action_t *act = ecb->dte_action;
10773 10778
10774 10779 for (; act != NULL; act = act->dta_next) {
10775 10780 if (act->dta_kind == DTRACEACT_COMMIT)
10776 10781 return (EINVAL);
10777 10782 }
10778 10783
10779 10784 if (dp == NULL)
10780 10785 return (EINVAL);
10781 10786 break;
10782 10787 }
10783 10788
10784 10789 default:
10785 10790 return (EINVAL);
10786 10791 }
10787 10792
10788 10793 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
10789 10794 /*
10790 10795 * If this is a data-storing action or a speculate,
10791 10796 * we must be sure that there isn't a commit on the
10792 10797 * action chain.
10793 10798 */
10794 10799 dtrace_action_t *act = ecb->dte_action;
10795 10800
10796 10801 for (; act != NULL; act = act->dta_next) {
10797 10802 if (act->dta_kind == DTRACEACT_COMMIT)
10798 10803 return (EINVAL);
10799 10804 }
10800 10805 }
10801 10806
10802 10807 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
10803 10808 action->dta_rec.dtrd_size = size;
10804 10809 }
10805 10810
10806 10811 action->dta_refcnt = 1;
10807 10812 rec = &action->dta_rec;
10808 10813 size = rec->dtrd_size;
10809 10814
10810 10815 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
10811 10816 if (!(size & mask)) {
10812 10817 align = mask + 1;
10813 10818 break;
10814 10819 }
10815 10820 }
10816 10821
10817 10822 action->dta_kind = desc->dtad_kind;
10818 10823
10819 10824 if ((action->dta_difo = dp) != NULL)
10820 10825 dtrace_difo_hold(dp);
10821 10826
10822 10827 rec->dtrd_action = action->dta_kind;
10823 10828 rec->dtrd_arg = arg;
10824 10829 rec->dtrd_uarg = desc->dtad_uarg;
10825 10830 rec->dtrd_alignment = (uint16_t)align;
10826 10831 rec->dtrd_format = format;
10827 10832
10828 10833 if ((last = ecb->dte_action_last) != NULL) {
10829 10834 ASSERT(ecb->dte_action != NULL);
10830 10835 action->dta_prev = last;
10831 10836 last->dta_next = action;
10832 10837 } else {
10833 10838 ASSERT(ecb->dte_action == NULL);
10834 10839 ecb->dte_action = action;
10835 10840 }
10836 10841
10837 10842 ecb->dte_action_last = action;
10838 10843
10839 10844 return (0);
10840 10845 }
10841 10846
10842 10847 static void
10843 10848 dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
10844 10849 {
10845 10850 dtrace_action_t *act = ecb->dte_action, *next;
10846 10851 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
10847 10852 dtrace_difo_t *dp;
10848 10853 uint16_t format;
10849 10854
10850 10855 if (act != NULL && act->dta_refcnt > 1) {
10851 10856 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
10852 10857 act->dta_refcnt--;
10853 10858 } else {
10854 10859 for (; act != NULL; act = next) {
10855 10860 next = act->dta_next;
10856 10861 ASSERT(next != NULL || act == ecb->dte_action_last);
10857 10862 ASSERT(act->dta_refcnt == 1);
10858 10863
10859 10864 if ((format = act->dta_rec.dtrd_format) != 0)
10860 10865 dtrace_format_remove(ecb->dte_state, format);
10861 10866
10862 10867 if ((dp = act->dta_difo) != NULL)
10863 10868 dtrace_difo_release(dp, vstate);
10864 10869
10865 10870 if (DTRACEACT_ISAGG(act->dta_kind)) {
10866 10871 dtrace_ecb_aggregation_destroy(ecb, act);
10867 10872 } else {
10868 10873 kmem_free(act, sizeof (dtrace_action_t));
10869 10874 }
10870 10875 }
10871 10876 }
10872 10877
10873 10878 ecb->dte_action = NULL;
10874 10879 ecb->dte_action_last = NULL;
10875 10880 ecb->dte_size = 0;
10876 10881 }
10877 10882
10878 10883 static void
10879 10884 dtrace_ecb_disable(dtrace_ecb_t *ecb)
10880 10885 {
10881 10886 /*
10882 10887 * We disable the ECB by removing it from its probe.
10883 10888 */
10884 10889 dtrace_ecb_t *pecb, *prev = NULL;
10885 10890 dtrace_probe_t *probe = ecb->dte_probe;
10886 10891
10887 10892 ASSERT(MUTEX_HELD(&dtrace_lock));
10888 10893
10889 10894 if (probe == NULL) {
10890 10895 /*
10891 10896 * This is the NULL probe; there is nothing to disable.
10892 10897 */
10893 10898 return;
10894 10899 }
10895 10900
10896 10901 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
10897 10902 if (pecb == ecb)
10898 10903 break;
10899 10904 prev = pecb;
10900 10905 }
10901 10906
10902 10907 ASSERT(pecb != NULL);
10903 10908
10904 10909 if (prev == NULL) {
10905 10910 probe->dtpr_ecb = ecb->dte_next;
10906 10911 } else {
10907 10912 prev->dte_next = ecb->dte_next;
10908 10913 }
10909 10914
10910 10915 if (ecb == probe->dtpr_ecb_last) {
10911 10916 ASSERT(ecb->dte_next == NULL);
10912 10917 probe->dtpr_ecb_last = prev;
10913 10918 }
10914 10919
10915 10920 /*
10916 10921 * The ECB has been disconnected from the probe; now sync to assure
10917 10922 * that all CPUs have seen the change before returning.
10918 10923 */
10919 10924 dtrace_sync();
10920 10925
10921 10926 if (probe->dtpr_ecb == NULL) {
10922 10927 /*
10923 10928 * That was the last ECB on the probe; clear the predicate
10924 10929 * cache ID for the probe, disable it and sync one more time
10925 10930 * to assure that we'll never hit it again.
10926 10931 */
10927 10932 dtrace_provider_t *prov = probe->dtpr_provider;
10928 10933
10929 10934 ASSERT(ecb->dte_next == NULL);
10930 10935 ASSERT(probe->dtpr_ecb_last == NULL);
10931 10936 probe->dtpr_predcache = DTRACE_CACHEIDNONE;
10932 10937 prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
10933 10938 probe->dtpr_id, probe->dtpr_arg);
10934 10939 dtrace_sync();
10935 10940 } else {
10936 10941 /*
10937 10942 * There is at least one ECB remaining on the probe. If there
10938 10943 * is _exactly_ one, set the probe's predicate cache ID to be
10939 10944 * the predicate cache ID of the remaining ECB.
10940 10945 */
10941 10946 ASSERT(probe->dtpr_ecb_last != NULL);
10942 10947 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
10943 10948
10944 10949 if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
10945 10950 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
10946 10951
10947 10952 ASSERT(probe->dtpr_ecb->dte_next == NULL);
10948 10953
10949 10954 if (p != NULL)
10950 10955 probe->dtpr_predcache = p->dtp_cacheid;
10951 10956 }
10952 10957
10953 10958 ecb->dte_next = NULL;
10954 10959 }
10955 10960 }
10956 10961
10957 10962 static void
10958 10963 dtrace_ecb_destroy(dtrace_ecb_t *ecb)
10959 10964 {
10960 10965 dtrace_state_t *state = ecb->dte_state;
10961 10966 dtrace_vstate_t *vstate = &state->dts_vstate;
10962 10967 dtrace_predicate_t *pred;
10963 10968 dtrace_epid_t epid = ecb->dte_epid;
10964 10969
10965 10970 ASSERT(MUTEX_HELD(&dtrace_lock));
10966 10971 ASSERT(ecb->dte_next == NULL);
10967 10972 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
10968 10973
10969 10974 if ((pred = ecb->dte_predicate) != NULL)
10970 10975 dtrace_predicate_release(pred, vstate);
10971 10976
10972 10977 dtrace_ecb_action_remove(ecb);
10973 10978
10974 10979 ASSERT(state->dts_ecbs[epid - 1] == ecb);
10975 10980 state->dts_ecbs[epid - 1] = NULL;
10976 10981
10977 10982 kmem_free(ecb, sizeof (dtrace_ecb_t));
10978 10983 }
10979 10984
10980 10985 static dtrace_ecb_t *
10981 10986 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
10982 10987 dtrace_enabling_t *enab)
10983 10988 {
10984 10989 dtrace_ecb_t *ecb;
10985 10990 dtrace_predicate_t *pred;
10986 10991 dtrace_actdesc_t *act;
10987 10992 dtrace_provider_t *prov;
10988 10993 dtrace_ecbdesc_t *desc = enab->dten_current;
10989 10994
10990 10995 ASSERT(MUTEX_HELD(&dtrace_lock));
10991 10996 ASSERT(state != NULL);
10992 10997
10993 10998 ecb = dtrace_ecb_add(state, probe);
10994 10999 ecb->dte_uarg = desc->dted_uarg;
10995 11000
10996 11001 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
10997 11002 dtrace_predicate_hold(pred);
10998 11003 ecb->dte_predicate = pred;
10999 11004 }
11000 11005
11001 11006 if (probe != NULL) {
11002 11007 /*
11003 11008 * If the provider shows more leg than the consumer is old
11004 11009 * enough to see, we need to enable the appropriate implicit
11005 11010 * predicate bits to prevent the ecb from activating at
11006 11011 * revealing times.
11007 11012 *
11008 11013 * Providers specifying DTRACE_PRIV_USER at register time
11009 11014 * are stating that they need the /proc-style privilege
11010 11015 * model to be enforced, and this is what DTRACE_COND_OWNER
11011 11016 * and DTRACE_COND_ZONEOWNER will then do at probe time.
11012 11017 */
11013 11018 prov = probe->dtpr_provider;
11014 11019 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
11015 11020 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11016 11021 ecb->dte_cond |= DTRACE_COND_OWNER;
11017 11022
11018 11023 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
11019 11024 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11020 11025 ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
11021 11026
11022 11027 /*
11023 11028 * If the provider shows us kernel innards and the user
11024 11029 * is lacking sufficient privilege, enable the
11025 11030 * DTRACE_COND_USERMODE implicit predicate.
11026 11031 */
11027 11032 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
11028 11033 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
11029 11034 ecb->dte_cond |= DTRACE_COND_USERMODE;
11030 11035 }
11031 11036
11032 11037 if (dtrace_ecb_create_cache != NULL) {
11033 11038 /*
11034 11039 * If we have a cached ecb, we'll use its action list instead
11035 11040 * of creating our own (saving both time and space).
11036 11041 */
11037 11042 dtrace_ecb_t *cached = dtrace_ecb_create_cache;
11038 11043 dtrace_action_t *act = cached->dte_action;
11039 11044
11040 11045 if (act != NULL) {
11041 11046 ASSERT(act->dta_refcnt > 0);
11042 11047 act->dta_refcnt++;
11043 11048 ecb->dte_action = act;
11044 11049 ecb->dte_action_last = cached->dte_action_last;
11045 11050 ecb->dte_needed = cached->dte_needed;
11046 11051 ecb->dte_size = cached->dte_size;
11047 11052 ecb->dte_alignment = cached->dte_alignment;
11048 11053 }
11049 11054
11050 11055 return (ecb);
11051 11056 }
11052 11057
11053 11058 for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
11054 11059 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
11055 11060 dtrace_ecb_destroy(ecb);
11056 11061 return (NULL);
11057 11062 }
11058 11063 }
11059 11064
11060 11065 dtrace_ecb_resize(ecb);
11061 11066
11062 11067 return (dtrace_ecb_create_cache = ecb);
11063 11068 }
11064 11069
11065 11070 static int
11066 11071 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
11067 11072 {
11068 11073 dtrace_ecb_t *ecb;
11069 11074 dtrace_enabling_t *enab = arg;
11070 11075 dtrace_state_t *state = enab->dten_vstate->dtvs_state;
11071 11076
11072 11077 ASSERT(state != NULL);
11073 11078
11074 11079 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
11075 11080 /*
11076 11081 * This probe was created in a generation for which this
11077 11082 * enabling has previously created ECBs; we don't want to
11078 11083 * enable it again, so just kick out.
11079 11084 */
11080 11085 return (DTRACE_MATCH_NEXT);
11081 11086 }
11082 11087
11083 11088 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
11084 11089 return (DTRACE_MATCH_DONE);
11085 11090
11086 11091 if (dtrace_ecb_enable(ecb) < 0)
11087 11092 return (DTRACE_MATCH_FAIL);
11088 11093
11089 11094 return (DTRACE_MATCH_NEXT);
11090 11095 }
11091 11096
11092 11097 static dtrace_ecb_t *
11093 11098 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
11094 11099 {
11095 11100 dtrace_ecb_t *ecb;
11096 11101
11097 11102 ASSERT(MUTEX_HELD(&dtrace_lock));
11098 11103
11099 11104 if (id == 0 || id > state->dts_necbs)
11100 11105 return (NULL);
11101 11106
11102 11107 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
11103 11108 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
11104 11109
11105 11110 return (state->dts_ecbs[id - 1]);
11106 11111 }
11107 11112
11108 11113 static dtrace_aggregation_t *
11109 11114 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
11110 11115 {
11111 11116 dtrace_aggregation_t *agg;
11112 11117
11113 11118 ASSERT(MUTEX_HELD(&dtrace_lock));
11114 11119
11115 11120 if (id == 0 || id > state->dts_naggregations)
11116 11121 return (NULL);
11117 11122
11118 11123 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
11119 11124 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
11120 11125 agg->dtag_id == id);
11121 11126
11122 11127 return (state->dts_aggregations[id - 1]);
11123 11128 }
11124 11129
11125 11130 /*
11126 11131 * DTrace Buffer Functions
11127 11132 *
11128 11133 * The following functions manipulate DTrace buffers. Most of these functions
11129 11134 * are called in the context of establishing or processing consumer state;
11130 11135 * exceptions are explicitly noted.
11131 11136 */
11132 11137
11133 11138 /*
11134 11139 * Note: called from cross call context. This function switches the two
11135 11140 * buffers on a given CPU. The atomicity of this operation is assured by
11136 11141 * disabling interrupts while the actual switch takes place; the disabling of
11137 11142 * interrupts serializes the execution with any execution of dtrace_probe() on
11138 11143 * the same CPU.
11139 11144 */
11140 11145 static void
11141 11146 dtrace_buffer_switch(dtrace_buffer_t *buf)
11142 11147 {
11143 11148 caddr_t tomax = buf->dtb_tomax;
11144 11149 caddr_t xamot = buf->dtb_xamot;
11145 11150 dtrace_icookie_t cookie;
11146 11151 hrtime_t now;
11147 11152
11148 11153 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11149 11154 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
11150 11155
11151 11156 cookie = dtrace_interrupt_disable();
11152 11157 now = dtrace_gethrtime();
11153 11158 buf->dtb_tomax = xamot;
11154 11159 buf->dtb_xamot = tomax;
11155 11160 buf->dtb_xamot_drops = buf->dtb_drops;
11156 11161 buf->dtb_xamot_offset = buf->dtb_offset;
11157 11162 buf->dtb_xamot_errors = buf->dtb_errors;
11158 11163 buf->dtb_xamot_flags = buf->dtb_flags;
11159 11164 buf->dtb_offset = 0;
11160 11165 buf->dtb_drops = 0;
11161 11166 buf->dtb_errors = 0;
11162 11167 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
11163 11168 buf->dtb_interval = now - buf->dtb_switched;
11164 11169 buf->dtb_switched = now;
11165 11170 dtrace_interrupt_enable(cookie);
11166 11171 }
11167 11172
11168 11173 /*
11169 11174 * Note: called from cross call context. This function activates a buffer
11170 11175 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
11171 11176 * is guaranteed by the disabling of interrupts.
11172 11177 */
11173 11178 static void
11174 11179 dtrace_buffer_activate(dtrace_state_t *state)
11175 11180 {
11176 11181 dtrace_buffer_t *buf;
11177 11182 dtrace_icookie_t cookie = dtrace_interrupt_disable();
11178 11183
11179 11184 buf = &state->dts_buffer[CPU->cpu_id];
11180 11185
11181 11186 if (buf->dtb_tomax != NULL) {
11182 11187 /*
11183 11188 * We might like to assert that the buffer is marked inactive,
11184 11189 * but this isn't necessarily true: the buffer for the CPU
11185 11190 * that processes the BEGIN probe has its buffer activated
11186 11191 * manually. In this case, we take the (harmless) action
11187 11192 * re-clearing the bit INACTIVE bit.
11188 11193 */
11189 11194 buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
11190 11195 }
11191 11196
11192 11197 dtrace_interrupt_enable(cookie);
11193 11198 }
11194 11199
11195 11200 static int
11196 11201 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
11197 11202 processorid_t cpu, int *factor)
11198 11203 {
11199 11204 cpu_t *cp;
11200 11205 dtrace_buffer_t *buf;
11201 11206 int allocated = 0, desired = 0;
11202 11207
11203 11208 ASSERT(MUTEX_HELD(&cpu_lock));
11204 11209 ASSERT(MUTEX_HELD(&dtrace_lock));
11205 11210
11206 11211 *factor = 1;
11207 11212
11208 11213 if (size > dtrace_nonroot_maxsize &&
11209 11214 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
11210 11215 return (EFBIG);
11211 11216
11212 11217 cp = cpu_list;
11213 11218
11214 11219 do {
11215 11220 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
11216 11221 continue;
11217 11222
11218 11223 buf = &bufs[cp->cpu_id];
11219 11224
11220 11225 /*
11221 11226 * If there is already a buffer allocated for this CPU, it
11222 11227 * is only possible that this is a DR event. In this case,
11223 11228 * the buffer size must match our specified size.
11224 11229 */
11225 11230 if (buf->dtb_tomax != NULL) {
11226 11231 ASSERT(buf->dtb_size == size);
11227 11232 continue;
11228 11233 }
11229 11234
11230 11235 ASSERT(buf->dtb_xamot == NULL);
11231 11236
11232 11237 if ((buf->dtb_tomax = kmem_zalloc(size,
11233 11238 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
11234 11239 goto err;
11235 11240
11236 11241 buf->dtb_size = size;
11237 11242 buf->dtb_flags = flags;
11238 11243 buf->dtb_offset = 0;
11239 11244 buf->dtb_drops = 0;
11240 11245
11241 11246 if (flags & DTRACEBUF_NOSWITCH)
11242 11247 continue;
11243 11248
11244 11249 if ((buf->dtb_xamot = kmem_zalloc(size,
11245 11250 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
11246 11251 goto err;
11247 11252 } while ((cp = cp->cpu_next) != cpu_list);
11248 11253
11249 11254 return (0);
11250 11255
11251 11256 err:
11252 11257 cp = cpu_list;
11253 11258
11254 11259 do {
11255 11260 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
11256 11261 continue;
11257 11262
11258 11263 buf = &bufs[cp->cpu_id];
11259 11264 desired += 2;
11260 11265
11261 11266 if (buf->dtb_xamot != NULL) {
11262 11267 ASSERT(buf->dtb_tomax != NULL);
11263 11268 ASSERT(buf->dtb_size == size);
11264 11269 kmem_free(buf->dtb_xamot, size);
11265 11270 allocated++;
11266 11271 }
11267 11272
11268 11273 if (buf->dtb_tomax != NULL) {
11269 11274 ASSERT(buf->dtb_size == size);
11270 11275 kmem_free(buf->dtb_tomax, size);
11271 11276 allocated++;
11272 11277 }
11273 11278
11274 11279 buf->dtb_tomax = NULL;
11275 11280 buf->dtb_xamot = NULL;
11276 11281 buf->dtb_size = 0;
11277 11282 } while ((cp = cp->cpu_next) != cpu_list);
11278 11283
11279 11284 *factor = desired / (allocated > 0 ? allocated : 1);
11280 11285
11281 11286 return (ENOMEM);
11282 11287 }
11283 11288
11284 11289 /*
11285 11290 * Note: called from probe context. This function just increments the drop
11286 11291 * count on a buffer. It has been made a function to allow for the
11287 11292 * possibility of understanding the source of mysterious drop counts. (A
11288 11293 * problem for which one may be particularly disappointed that DTrace cannot
11289 11294 * be used to understand DTrace.)
11290 11295 */
11291 11296 static void
11292 11297 dtrace_buffer_drop(dtrace_buffer_t *buf)
11293 11298 {
11294 11299 buf->dtb_drops++;
11295 11300 }
11296 11301
11297 11302 /*
11298 11303 * Note: called from probe context. This function is called to reserve space
11299 11304 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
11300 11305 * mstate. Returns the new offset in the buffer, or a negative value if an
11301 11306 * error has occurred.
11302 11307 */
11303 11308 static intptr_t
11304 11309 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
11305 11310 dtrace_state_t *state, dtrace_mstate_t *mstate)
11306 11311 {
11307 11312 intptr_t offs = buf->dtb_offset, soffs;
11308 11313 intptr_t woffs;
11309 11314 caddr_t tomax;
11310 11315 size_t total;
11311 11316
11312 11317 if (buf->dtb_flags & DTRACEBUF_INACTIVE)
11313 11318 return (-1);
11314 11319
11315 11320 if ((tomax = buf->dtb_tomax) == NULL) {
11316 11321 dtrace_buffer_drop(buf);
11317 11322 return (-1);
11318 11323 }
11319 11324
11320 11325 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
11321 11326 while (offs & (align - 1)) {
11322 11327 /*
11323 11328 * Assert that our alignment is off by a number which
11324 11329 * is itself sizeof (uint32_t) aligned.
11325 11330 */
11326 11331 ASSERT(!((align - (offs & (align - 1))) &
11327 11332 (sizeof (uint32_t) - 1)));
11328 11333 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
11329 11334 offs += sizeof (uint32_t);
11330 11335 }
11331 11336
11332 11337 if ((soffs = offs + needed) > buf->dtb_size) {
11333 11338 dtrace_buffer_drop(buf);
11334 11339 return (-1);
11335 11340 }
11336 11341
11337 11342 if (mstate == NULL)
11338 11343 return (offs);
11339 11344
11340 11345 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
11341 11346 mstate->dtms_scratch_size = buf->dtb_size - soffs;
11342 11347 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
11343 11348
11344 11349 return (offs);
11345 11350 }
11346 11351
11347 11352 if (buf->dtb_flags & DTRACEBUF_FILL) {
11348 11353 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
11349 11354 (buf->dtb_flags & DTRACEBUF_FULL))
11350 11355 return (-1);
11351 11356 goto out;
11352 11357 }
11353 11358
11354 11359 total = needed + (offs & (align - 1));
11355 11360
11356 11361 /*
11357 11362 * For a ring buffer, life is quite a bit more complicated. Before
11358 11363 * we can store any padding, we need to adjust our wrapping offset.
11359 11364 * (If we've never before wrapped or we're not about to, no adjustment
11360 11365 * is required.)
11361 11366 */
11362 11367 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
11363 11368 offs + total > buf->dtb_size) {
11364 11369 woffs = buf->dtb_xamot_offset;
11365 11370
11366 11371 if (offs + total > buf->dtb_size) {
11367 11372 /*
11368 11373 * We can't fit in the end of the buffer. First, a
11369 11374 * sanity check that we can fit in the buffer at all.
11370 11375 */
11371 11376 if (total > buf->dtb_size) {
11372 11377 dtrace_buffer_drop(buf);
11373 11378 return (-1);
11374 11379 }
11375 11380
11376 11381 /*
11377 11382 * We're going to be storing at the top of the buffer,
11378 11383 * so now we need to deal with the wrapped offset. We
11379 11384 * only reset our wrapped offset to 0 if it is
11380 11385 * currently greater than the current offset. If it
11381 11386 * is less than the current offset, it is because a
11382 11387 * previous allocation induced a wrap -- but the
11383 11388 * allocation didn't subsequently take the space due
11384 11389 * to an error or false predicate evaluation. In this
11385 11390 * case, we'll just leave the wrapped offset alone: if
11386 11391 * the wrapped offset hasn't been advanced far enough
11387 11392 * for this allocation, it will be adjusted in the
11388 11393 * lower loop.
11389 11394 */
11390 11395 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
11391 11396 if (woffs >= offs)
11392 11397 woffs = 0;
11393 11398 } else {
11394 11399 woffs = 0;
11395 11400 }
11396 11401
11397 11402 /*
11398 11403 * Now we know that we're going to be storing to the
11399 11404 * top of the buffer and that there is room for us
11400 11405 * there. We need to clear the buffer from the current
11401 11406 * offset to the end (there may be old gunk there).
11402 11407 */
11403 11408 while (offs < buf->dtb_size)
11404 11409 tomax[offs++] = 0;
11405 11410
11406 11411 /*
11407 11412 * We need to set our offset to zero. And because we
11408 11413 * are wrapping, we need to set the bit indicating as
11409 11414 * much. We can also adjust our needed space back
11410 11415 * down to the space required by the ECB -- we know
11411 11416 * that the top of the buffer is aligned.
11412 11417 */
11413 11418 offs = 0;
11414 11419 total = needed;
11415 11420 buf->dtb_flags |= DTRACEBUF_WRAPPED;
11416 11421 } else {
11417 11422 /*
11418 11423 * There is room for us in the buffer, so we simply
11419 11424 * need to check the wrapped offset.
11420 11425 */
11421 11426 if (woffs < offs) {
11422 11427 /*
11423 11428 * The wrapped offset is less than the offset.
11424 11429 * This can happen if we allocated buffer space
11425 11430 * that induced a wrap, but then we didn't
11426 11431 * subsequently take the space due to an error
11427 11432 * or false predicate evaluation. This is
11428 11433 * okay; we know that _this_ allocation isn't
11429 11434 * going to induce a wrap. We still can't
11430 11435 * reset the wrapped offset to be zero,
11431 11436 * however: the space may have been trashed in
11432 11437 * the previous failed probe attempt. But at
11433 11438 * least the wrapped offset doesn't need to
11434 11439 * be adjusted at all...
11435 11440 */
11436 11441 goto out;
11437 11442 }
11438 11443 }
11439 11444
11440 11445 while (offs + total > woffs) {
11441 11446 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
11442 11447 size_t size;
11443 11448
11444 11449 if (epid == DTRACE_EPIDNONE) {
11445 11450 size = sizeof (uint32_t);
11446 11451 } else {
11447 11452 ASSERT3U(epid, <=, state->dts_necbs);
11448 11453 ASSERT(state->dts_ecbs[epid - 1] != NULL);
11449 11454
11450 11455 size = state->dts_ecbs[epid - 1]->dte_size;
11451 11456 }
11452 11457
11453 11458 ASSERT(woffs + size <= buf->dtb_size);
11454 11459 ASSERT(size != 0);
11455 11460
11456 11461 if (woffs + size == buf->dtb_size) {
11457 11462 /*
11458 11463 * We've reached the end of the buffer; we want
11459 11464 * to set the wrapped offset to 0 and break
11460 11465 * out. However, if the offs is 0, then we're
11461 11466 * in a strange edge-condition: the amount of
11462 11467 * space that we want to reserve plus the size
11463 11468 * of the record that we're overwriting is
11464 11469 * greater than the size of the buffer. This
11465 11470 * is problematic because if we reserve the
11466 11471 * space but subsequently don't consume it (due
11467 11472 * to a failed predicate or error) the wrapped
11468 11473 * offset will be 0 -- yet the EPID at offset 0
11469 11474 * will not be committed. This situation is
11470 11475 * relatively easy to deal with: if we're in
11471 11476 * this case, the buffer is indistinguishable
11472 11477 * from one that hasn't wrapped; we need only
11473 11478 * finish the job by clearing the wrapped bit,
11474 11479 * explicitly setting the offset to be 0, and
11475 11480 * zero'ing out the old data in the buffer.
11476 11481 */
11477 11482 if (offs == 0) {
11478 11483 buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
11479 11484 buf->dtb_offset = 0;
11480 11485 woffs = total;
11481 11486
11482 11487 while (woffs < buf->dtb_size)
11483 11488 tomax[woffs++] = 0;
11484 11489 }
11485 11490
11486 11491 woffs = 0;
11487 11492 break;
11488 11493 }
11489 11494
11490 11495 woffs += size;
11491 11496 }
11492 11497
11493 11498 /*
11494 11499 * We have a wrapped offset. It may be that the wrapped offset
11495 11500 * has become zero -- that's okay.
11496 11501 */
11497 11502 buf->dtb_xamot_offset = woffs;
11498 11503 }
11499 11504
11500 11505 out:
11501 11506 /*
11502 11507 * Now we can plow the buffer with any necessary padding.
11503 11508 */
11504 11509 while (offs & (align - 1)) {
11505 11510 /*
11506 11511 * Assert that our alignment is off by a number which
11507 11512 * is itself sizeof (uint32_t) aligned.
11508 11513 */
11509 11514 ASSERT(!((align - (offs & (align - 1))) &
11510 11515 (sizeof (uint32_t) - 1)));
11511 11516 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
11512 11517 offs += sizeof (uint32_t);
11513 11518 }
11514 11519
11515 11520 if (buf->dtb_flags & DTRACEBUF_FILL) {
11516 11521 if (offs + needed > buf->dtb_size - state->dts_reserve) {
11517 11522 buf->dtb_flags |= DTRACEBUF_FULL;
11518 11523 return (-1);
11519 11524 }
11520 11525 }
11521 11526
11522 11527 if (mstate == NULL)
11523 11528 return (offs);
11524 11529
11525 11530 /*
11526 11531 * For ring buffers and fill buffers, the scratch space is always
11527 11532 * the inactive buffer.
11528 11533 */
11529 11534 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
11530 11535 mstate->dtms_scratch_size = buf->dtb_size;
11531 11536 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
11532 11537
11533 11538 return (offs);
11534 11539 }
11535 11540
11536 11541 static void
11537 11542 dtrace_buffer_polish(dtrace_buffer_t *buf)
11538 11543 {
11539 11544 ASSERT(buf->dtb_flags & DTRACEBUF_RING);
11540 11545 ASSERT(MUTEX_HELD(&dtrace_lock));
11541 11546
11542 11547 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
11543 11548 return;
11544 11549
11545 11550 /*
11546 11551 * We need to polish the ring buffer. There are three cases:
11547 11552 *
11548 11553 * - The first (and presumably most common) is that there is no gap
11549 11554 * between the buffer offset and the wrapped offset. In this case,
11550 11555 * there is nothing in the buffer that isn't valid data; we can
11551 11556 * mark the buffer as polished and return.
11552 11557 *
11553 11558 * - The second (less common than the first but still more common
11554 11559 * than the third) is that there is a gap between the buffer offset
11555 11560 * and the wrapped offset, and the wrapped offset is larger than the
11556 11561 * buffer offset. This can happen because of an alignment issue, or
11557 11562 * can happen because of a call to dtrace_buffer_reserve() that
11558 11563 * didn't subsequently consume the buffer space. In this case,
11559 11564 * we need to zero the data from the buffer offset to the wrapped
11560 11565 * offset.
11561 11566 *
11562 11567 * - The third (and least common) is that there is a gap between the
11563 11568 * buffer offset and the wrapped offset, but the wrapped offset is
11564 11569 * _less_ than the buffer offset. This can only happen because a
11565 11570 * call to dtrace_buffer_reserve() induced a wrap, but the space
11566 11571 * was not subsequently consumed. In this case, we need to zero the
11567 11572 * space from the offset to the end of the buffer _and_ from the
11568 11573 * top of the buffer to the wrapped offset.
11569 11574 */
11570 11575 if (buf->dtb_offset < buf->dtb_xamot_offset) {
11571 11576 bzero(buf->dtb_tomax + buf->dtb_offset,
11572 11577 buf->dtb_xamot_offset - buf->dtb_offset);
11573 11578 }
11574 11579
11575 11580 if (buf->dtb_offset > buf->dtb_xamot_offset) {
11576 11581 bzero(buf->dtb_tomax + buf->dtb_offset,
11577 11582 buf->dtb_size - buf->dtb_offset);
11578 11583 bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
11579 11584 }
11580 11585 }
11581 11586
11582 11587 /*
11583 11588 * This routine determines if data generated at the specified time has likely
11584 11589 * been entirely consumed at user-level. This routine is called to determine
11585 11590 * if an ECB on a defunct probe (but for an active enabling) can be safely
11586 11591 * disabled and destroyed.
11587 11592 */
11588 11593 static int
11589 11594 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when)
11590 11595 {
11591 11596 int i;
11592 11597
11593 11598 for (i = 0; i < NCPU; i++) {
11594 11599 dtrace_buffer_t *buf = &bufs[i];
11595 11600
11596 11601 if (buf->dtb_size == 0)
11597 11602 continue;
11598 11603
11599 11604 if (buf->dtb_flags & DTRACEBUF_RING)
11600 11605 return (0);
11601 11606
11602 11607 if (!buf->dtb_switched && buf->dtb_offset != 0)
11603 11608 return (0);
11604 11609
11605 11610 if (buf->dtb_switched - buf->dtb_interval < when)
11606 11611 return (0);
11607 11612 }
11608 11613
11609 11614 return (1);
11610 11615 }
11611 11616
11612 11617 static void
11613 11618 dtrace_buffer_free(dtrace_buffer_t *bufs)
11614 11619 {
11615 11620 int i;
11616 11621
11617 11622 for (i = 0; i < NCPU; i++) {
11618 11623 dtrace_buffer_t *buf = &bufs[i];
11619 11624
11620 11625 if (buf->dtb_tomax == NULL) {
11621 11626 ASSERT(buf->dtb_xamot == NULL);
11622 11627 ASSERT(buf->dtb_size == 0);
11623 11628 continue;
11624 11629 }
11625 11630
11626 11631 if (buf->dtb_xamot != NULL) {
11627 11632 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11628 11633 kmem_free(buf->dtb_xamot, buf->dtb_size);
11629 11634 }
11630 11635
11631 11636 kmem_free(buf->dtb_tomax, buf->dtb_size);
11632 11637 buf->dtb_size = 0;
11633 11638 buf->dtb_tomax = NULL;
11634 11639 buf->dtb_xamot = NULL;
11635 11640 }
11636 11641 }
11637 11642
11638 11643 /*
11639 11644 * DTrace Enabling Functions
11640 11645 */
11641 11646 static dtrace_enabling_t *
11642 11647 dtrace_enabling_create(dtrace_vstate_t *vstate)
11643 11648 {
11644 11649 dtrace_enabling_t *enab;
11645 11650
11646 11651 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
11647 11652 enab->dten_vstate = vstate;
11648 11653
11649 11654 return (enab);
11650 11655 }
11651 11656
11652 11657 static void
11653 11658 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
11654 11659 {
11655 11660 dtrace_ecbdesc_t **ndesc;
11656 11661 size_t osize, nsize;
11657 11662
11658 11663 /*
11659 11664 * We can't add to enablings after we've enabled them, or after we've
11660 11665 * retained them.
11661 11666 */
11662 11667 ASSERT(enab->dten_probegen == 0);
11663 11668 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11664 11669
11665 11670 if (enab->dten_ndesc < enab->dten_maxdesc) {
11666 11671 enab->dten_desc[enab->dten_ndesc++] = ecb;
11667 11672 return;
11668 11673 }
11669 11674
11670 11675 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11671 11676
11672 11677 if (enab->dten_maxdesc == 0) {
11673 11678 enab->dten_maxdesc = 1;
11674 11679 } else {
11675 11680 enab->dten_maxdesc <<= 1;
11676 11681 }
11677 11682
11678 11683 ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
11679 11684
11680 11685 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11681 11686 ndesc = kmem_zalloc(nsize, KM_SLEEP);
11682 11687 bcopy(enab->dten_desc, ndesc, osize);
11683 11688 kmem_free(enab->dten_desc, osize);
11684 11689
11685 11690 enab->dten_desc = ndesc;
11686 11691 enab->dten_desc[enab->dten_ndesc++] = ecb;
11687 11692 }
11688 11693
11689 11694 static void
11690 11695 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
11691 11696 dtrace_probedesc_t *pd)
11692 11697 {
11693 11698 dtrace_ecbdesc_t *new;
11694 11699 dtrace_predicate_t *pred;
11695 11700 dtrace_actdesc_t *act;
11696 11701
11697 11702 /*
11698 11703 * We're going to create a new ECB description that matches the
11699 11704 * specified ECB in every way, but has the specified probe description.
11700 11705 */
11701 11706 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
11702 11707
11703 11708 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
11704 11709 dtrace_predicate_hold(pred);
11705 11710
11706 11711 for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
11707 11712 dtrace_actdesc_hold(act);
11708 11713
11709 11714 new->dted_action = ecb->dted_action;
11710 11715 new->dted_pred = ecb->dted_pred;
11711 11716 new->dted_probe = *pd;
11712 11717 new->dted_uarg = ecb->dted_uarg;
11713 11718
11714 11719 dtrace_enabling_add(enab, new);
11715 11720 }
11716 11721
11717 11722 static void
11718 11723 dtrace_enabling_dump(dtrace_enabling_t *enab)
11719 11724 {
11720 11725 int i;
11721 11726
11722 11727 for (i = 0; i < enab->dten_ndesc; i++) {
11723 11728 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
11724 11729
11725 11730 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
11726 11731 desc->dtpd_provider, desc->dtpd_mod,
11727 11732 desc->dtpd_func, desc->dtpd_name);
11728 11733 }
11729 11734 }
11730 11735
11731 11736 static void
11732 11737 dtrace_enabling_destroy(dtrace_enabling_t *enab)
11733 11738 {
11734 11739 int i;
11735 11740 dtrace_ecbdesc_t *ep;
11736 11741 dtrace_vstate_t *vstate = enab->dten_vstate;
11737 11742
11738 11743 ASSERT(MUTEX_HELD(&dtrace_lock));
11739 11744
11740 11745 for (i = 0; i < enab->dten_ndesc; i++) {
11741 11746 dtrace_actdesc_t *act, *next;
11742 11747 dtrace_predicate_t *pred;
11743 11748
11744 11749 ep = enab->dten_desc[i];
11745 11750
11746 11751 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
11747 11752 dtrace_predicate_release(pred, vstate);
11748 11753
11749 11754 for (act = ep->dted_action; act != NULL; act = next) {
11750 11755 next = act->dtad_next;
11751 11756 dtrace_actdesc_release(act, vstate);
11752 11757 }
11753 11758
11754 11759 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
11755 11760 }
11756 11761
11757 11762 kmem_free(enab->dten_desc,
11758 11763 enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
11759 11764
11760 11765 /*
11761 11766 * If this was a retained enabling, decrement the dts_nretained count
11762 11767 * and take it off of the dtrace_retained list.
11763 11768 */
11764 11769 if (enab->dten_prev != NULL || enab->dten_next != NULL ||
11765 11770 dtrace_retained == enab) {
11766 11771 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11767 11772 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
11768 11773 enab->dten_vstate->dtvs_state->dts_nretained--;
11769 11774 dtrace_retained_gen++;
11770 11775 }
11771 11776
11772 11777 if (enab->dten_prev == NULL) {
11773 11778 if (dtrace_retained == enab) {
11774 11779 dtrace_retained = enab->dten_next;
11775 11780
11776 11781 if (dtrace_retained != NULL)
11777 11782 dtrace_retained->dten_prev = NULL;
11778 11783 }
11779 11784 } else {
11780 11785 ASSERT(enab != dtrace_retained);
11781 11786 ASSERT(dtrace_retained != NULL);
11782 11787 enab->dten_prev->dten_next = enab->dten_next;
11783 11788 }
11784 11789
11785 11790 if (enab->dten_next != NULL) {
11786 11791 ASSERT(dtrace_retained != NULL);
11787 11792 enab->dten_next->dten_prev = enab->dten_prev;
11788 11793 }
11789 11794
11790 11795 kmem_free(enab, sizeof (dtrace_enabling_t));
11791 11796 }
11792 11797
11793 11798 static int
11794 11799 dtrace_enabling_retain(dtrace_enabling_t *enab)
11795 11800 {
11796 11801 dtrace_state_t *state;
11797 11802
11798 11803 ASSERT(MUTEX_HELD(&dtrace_lock));
11799 11804 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11800 11805 ASSERT(enab->dten_vstate != NULL);
11801 11806
11802 11807 state = enab->dten_vstate->dtvs_state;
11803 11808 ASSERT(state != NULL);
11804 11809
11805 11810 /*
11806 11811 * We only allow each state to retain dtrace_retain_max enablings.
11807 11812 */
11808 11813 if (state->dts_nretained >= dtrace_retain_max)
11809 11814 return (ENOSPC);
11810 11815
11811 11816 state->dts_nretained++;
11812 11817 dtrace_retained_gen++;
11813 11818
11814 11819 if (dtrace_retained == NULL) {
11815 11820 dtrace_retained = enab;
11816 11821 return (0);
11817 11822 }
11818 11823
11819 11824 enab->dten_next = dtrace_retained;
11820 11825 dtrace_retained->dten_prev = enab;
11821 11826 dtrace_retained = enab;
11822 11827
11823 11828 return (0);
11824 11829 }
11825 11830
11826 11831 static int
11827 11832 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
11828 11833 dtrace_probedesc_t *create)
11829 11834 {
11830 11835 dtrace_enabling_t *new, *enab;
11831 11836 int found = 0, err = ENOENT;
11832 11837
11833 11838 ASSERT(MUTEX_HELD(&dtrace_lock));
11834 11839 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
11835 11840 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
11836 11841 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
11837 11842 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
11838 11843
11839 11844 new = dtrace_enabling_create(&state->dts_vstate);
11840 11845
11841 11846 /*
11842 11847 * Iterate over all retained enablings, looking for enablings that
11843 11848 * match the specified state.
11844 11849 */
11845 11850 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11846 11851 int i;
11847 11852
11848 11853 /*
11849 11854 * dtvs_state can only be NULL for helper enablings -- and
11850 11855 * helper enablings can't be retained.
11851 11856 */
11852 11857 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11853 11858
11854 11859 if (enab->dten_vstate->dtvs_state != state)
11855 11860 continue;
11856 11861
11857 11862 /*
11858 11863 * Now iterate over each probe description; we're looking for
11859 11864 * an exact match to the specified probe description.
11860 11865 */
11861 11866 for (i = 0; i < enab->dten_ndesc; i++) {
11862 11867 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11863 11868 dtrace_probedesc_t *pd = &ep->dted_probe;
11864 11869
11865 11870 if (strcmp(pd->dtpd_provider, match->dtpd_provider))
11866 11871 continue;
11867 11872
11868 11873 if (strcmp(pd->dtpd_mod, match->dtpd_mod))
11869 11874 continue;
11870 11875
11871 11876 if (strcmp(pd->dtpd_func, match->dtpd_func))
11872 11877 continue;
11873 11878
11874 11879 if (strcmp(pd->dtpd_name, match->dtpd_name))
11875 11880 continue;
11876 11881
11877 11882 /*
11878 11883 * We have a winning probe! Add it to our growing
11879 11884 * enabling.
11880 11885 */
11881 11886 found = 1;
11882 11887 dtrace_enabling_addlike(new, ep, create);
11883 11888 }
11884 11889 }
11885 11890
11886 11891 if (!found || (err = dtrace_enabling_retain(new)) != 0) {
11887 11892 dtrace_enabling_destroy(new);
11888 11893 return (err);
11889 11894 }
11890 11895
11891 11896 return (0);
11892 11897 }
11893 11898
11894 11899 static void
11895 11900 dtrace_enabling_retract(dtrace_state_t *state)
11896 11901 {
11897 11902 dtrace_enabling_t *enab, *next;
11898 11903
11899 11904 ASSERT(MUTEX_HELD(&dtrace_lock));
11900 11905
11901 11906 /*
11902 11907 * Iterate over all retained enablings, destroy the enablings retained
11903 11908 * for the specified state.
11904 11909 */
11905 11910 for (enab = dtrace_retained; enab != NULL; enab = next) {
11906 11911 next = enab->dten_next;
11907 11912
11908 11913 /*
11909 11914 * dtvs_state can only be NULL for helper enablings -- and
11910 11915 * helper enablings can't be retained.
11911 11916 */
11912 11917 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11913 11918
11914 11919 if (enab->dten_vstate->dtvs_state == state) {
11915 11920 ASSERT(state->dts_nretained > 0);
11916 11921 dtrace_enabling_destroy(enab);
11917 11922 }
11918 11923 }
11919 11924
11920 11925 ASSERT(state->dts_nretained == 0);
11921 11926 }
11922 11927
11923 11928 static int
11924 11929 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
11925 11930 {
11926 11931 int i = 0;
11927 11932 int total_matched = 0, matched = 0;
11928 11933
11929 11934 ASSERT(MUTEX_HELD(&cpu_lock));
11930 11935 ASSERT(MUTEX_HELD(&dtrace_lock));
11931 11936
11932 11937 for (i = 0; i < enab->dten_ndesc; i++) {
11933 11938 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11934 11939
11935 11940 enab->dten_current = ep;
11936 11941 enab->dten_error = 0;
11937 11942
11938 11943 /*
11939 11944 * If a provider failed to enable a probe then get out and
11940 11945 * let the consumer know we failed.
11941 11946 */
11942 11947 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0)
11943 11948 return (EBUSY);
11944 11949
11945 11950 total_matched += matched;
11946 11951
11947 11952 if (enab->dten_error != 0) {
11948 11953 /*
11949 11954 * If we get an error half-way through enabling the
11950 11955 * probes, we kick out -- perhaps with some number of
11951 11956 * them enabled. Leaving enabled probes enabled may
11952 11957 * be slightly confusing for user-level, but we expect
11953 11958 * that no one will attempt to actually drive on in
11954 11959 * the face of such errors. If this is an anonymous
11955 11960 * enabling (indicated with a NULL nmatched pointer),
11956 11961 * we cmn_err() a message. We aren't expecting to
11957 11962 * get such an error -- such as it can exist at all,
11958 11963 * it would be a result of corrupted DOF in the driver
11959 11964 * properties.
11960 11965 */
11961 11966 if (nmatched == NULL) {
11962 11967 cmn_err(CE_WARN, "dtrace_enabling_match() "
11963 11968 "error on %p: %d", (void *)ep,
11964 11969 enab->dten_error);
11965 11970 }
11966 11971
11967 11972 return (enab->dten_error);
11968 11973 }
11969 11974 }
11970 11975
11971 11976 enab->dten_probegen = dtrace_probegen;
11972 11977 if (nmatched != NULL)
11973 11978 *nmatched = total_matched;
11974 11979
11975 11980 return (0);
11976 11981 }
11977 11982
11978 11983 static void
11979 11984 dtrace_enabling_matchall(void)
11980 11985 {
11981 11986 dtrace_enabling_t *enab;
11982 11987
11983 11988 mutex_enter(&cpu_lock);
11984 11989 mutex_enter(&dtrace_lock);
11985 11990
11986 11991 /*
11987 11992 * Iterate over all retained enablings to see if any probes match
11988 11993 * against them. We only perform this operation on enablings for which
11989 11994 * we have sufficient permissions by virtue of being in the global zone
11990 11995 * or in the same zone as the DTrace client. Because we can be called
11991 11996 * after dtrace_detach() has been called, we cannot assert that there
11992 11997 * are retained enablings. We can safely load from dtrace_retained,
11993 11998 * however: the taskq_destroy() at the end of dtrace_detach() will
11994 11999 * block pending our completion.
11995 12000 */
11996 12001 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11997 12002 dtrace_cred_t *dcr = &enab->dten_vstate->dtvs_state->dts_cred;
11998 12003 cred_t *cr = dcr->dcr_cred;
11999 12004 zoneid_t zone = cr != NULL ? crgetzoneid(cr) : 0;
12000 12005
12001 12006 if ((dcr->dcr_visible & DTRACE_CRV_ALLZONE) || (cr != NULL &&
12002 12007 (zone == GLOBAL_ZONEID || getzoneid() == zone)))
12003 12008 (void) dtrace_enabling_match(enab, NULL);
12004 12009 }
12005 12010
12006 12011 mutex_exit(&dtrace_lock);
12007 12012 mutex_exit(&cpu_lock);
12008 12013 }
12009 12014
12010 12015 /*
12011 12016 * If an enabling is to be enabled without having matched probes (that is, if
12012 12017 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
12013 12018 * enabling must be _primed_ by creating an ECB for every ECB description.
12014 12019 * This must be done to assure that we know the number of speculations, the
12015 12020 * number of aggregations, the minimum buffer size needed, etc. before we
12016 12021 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
12017 12022 * enabling any probes, we create ECBs for every ECB decription, but with a
12018 12023 * NULL probe -- which is exactly what this function does.
12019 12024 */
12020 12025 static void
12021 12026 dtrace_enabling_prime(dtrace_state_t *state)
12022 12027 {
12023 12028 dtrace_enabling_t *enab;
12024 12029 int i;
12025 12030
12026 12031 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12027 12032 ASSERT(enab->dten_vstate->dtvs_state != NULL);
12028 12033
12029 12034 if (enab->dten_vstate->dtvs_state != state)
12030 12035 continue;
12031 12036
12032 12037 /*
12033 12038 * We don't want to prime an enabling more than once, lest
12034 12039 * we allow a malicious user to induce resource exhaustion.
12035 12040 * (The ECBs that result from priming an enabling aren't
12036 12041 * leaked -- but they also aren't deallocated until the
12037 12042 * consumer state is destroyed.)
12038 12043 */
12039 12044 if (enab->dten_primed)
12040 12045 continue;
12041 12046
12042 12047 for (i = 0; i < enab->dten_ndesc; i++) {
12043 12048 enab->dten_current = enab->dten_desc[i];
12044 12049 (void) dtrace_probe_enable(NULL, enab);
12045 12050 }
12046 12051
12047 12052 enab->dten_primed = 1;
12048 12053 }
12049 12054 }
12050 12055
12051 12056 /*
12052 12057 * Called to indicate that probes should be provided due to retained
12053 12058 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
12054 12059 * must take an initial lap through the enabling calling the dtps_provide()
12055 12060 * entry point explicitly to allow for autocreated probes.
12056 12061 */
12057 12062 static void
12058 12063 dtrace_enabling_provide(dtrace_provider_t *prv)
12059 12064 {
12060 12065 int i, all = 0;
12061 12066 dtrace_probedesc_t desc;
12062 12067 dtrace_genid_t gen;
12063 12068
12064 12069 ASSERT(MUTEX_HELD(&dtrace_lock));
12065 12070 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
12066 12071
12067 12072 if (prv == NULL) {
12068 12073 all = 1;
12069 12074 prv = dtrace_provider;
12070 12075 }
12071 12076
12072 12077 do {
12073 12078 dtrace_enabling_t *enab;
12074 12079 void *parg = prv->dtpv_arg;
12075 12080
12076 12081 retry:
12077 12082 gen = dtrace_retained_gen;
12078 12083 for (enab = dtrace_retained; enab != NULL;
12079 12084 enab = enab->dten_next) {
12080 12085 for (i = 0; i < enab->dten_ndesc; i++) {
12081 12086 desc = enab->dten_desc[i]->dted_probe;
12082 12087 mutex_exit(&dtrace_lock);
12083 12088 prv->dtpv_pops.dtps_provide(parg, &desc);
12084 12089 mutex_enter(&dtrace_lock);
12085 12090 /*
12086 12091 * Process the retained enablings again if
12087 12092 * they have changed while we weren't holding
12088 12093 * dtrace_lock.
12089 12094 */
12090 12095 if (gen != dtrace_retained_gen)
12091 12096 goto retry;
12092 12097 }
12093 12098 }
12094 12099 } while (all && (prv = prv->dtpv_next) != NULL);
12095 12100
12096 12101 mutex_exit(&dtrace_lock);
12097 12102 dtrace_probe_provide(NULL, all ? NULL : prv);
12098 12103 mutex_enter(&dtrace_lock);
12099 12104 }
12100 12105
12101 12106 /*
12102 12107 * Called to reap ECBs that are attached to probes from defunct providers.
12103 12108 */
12104 12109 static void
12105 12110 dtrace_enabling_reap(void)
12106 12111 {
12107 12112 dtrace_provider_t *prov;
12108 12113 dtrace_probe_t *probe;
12109 12114 dtrace_ecb_t *ecb;
12110 12115 hrtime_t when;
12111 12116 int i;
12112 12117
12113 12118 mutex_enter(&cpu_lock);
12114 12119 mutex_enter(&dtrace_lock);
12115 12120
12116 12121 for (i = 0; i < dtrace_nprobes; i++) {
12117 12122 if ((probe = dtrace_probes[i]) == NULL)
12118 12123 continue;
12119 12124
12120 12125 if (probe->dtpr_ecb == NULL)
12121 12126 continue;
12122 12127
12123 12128 prov = probe->dtpr_provider;
12124 12129
12125 12130 if ((when = prov->dtpv_defunct) == 0)
12126 12131 continue;
12127 12132
12128 12133 /*
12129 12134 * We have ECBs on a defunct provider: we want to reap these
12130 12135 * ECBs to allow the provider to unregister. The destruction
12131 12136 * of these ECBs must be done carefully: if we destroy the ECB
12132 12137 * and the consumer later wishes to consume an EPID that
12133 12138 * corresponds to the destroyed ECB (and if the EPID metadata
12134 12139 * has not been previously consumed), the consumer will abort
12135 12140 * processing on the unknown EPID. To reduce (but not, sadly,
12136 12141 * eliminate) the possibility of this, we will only destroy an
12137 12142 * ECB for a defunct provider if, for the state that
12138 12143 * corresponds to the ECB:
12139 12144 *
12140 12145 * (a) There is no speculative tracing (which can effectively
12141 12146 * cache an EPID for an arbitrary amount of time).
12142 12147 *
12143 12148 * (b) The principal buffers have been switched twice since the
12144 12149 * provider became defunct.
12145 12150 *
12146 12151 * (c) The aggregation buffers are of zero size or have been
12147 12152 * switched twice since the provider became defunct.
12148 12153 *
12149 12154 * We use dts_speculates to determine (a) and call a function
12150 12155 * (dtrace_buffer_consumed()) to determine (b) and (c). Note
12151 12156 * that as soon as we've been unable to destroy one of the ECBs
12152 12157 * associated with the probe, we quit trying -- reaping is only
12153 12158 * fruitful in as much as we can destroy all ECBs associated
12154 12159 * with the defunct provider's probes.
12155 12160 */
12156 12161 while ((ecb = probe->dtpr_ecb) != NULL) {
12157 12162 dtrace_state_t *state = ecb->dte_state;
12158 12163 dtrace_buffer_t *buf = state->dts_buffer;
12159 12164 dtrace_buffer_t *aggbuf = state->dts_aggbuffer;
12160 12165
12161 12166 if (state->dts_speculates)
12162 12167 break;
12163 12168
12164 12169 if (!dtrace_buffer_consumed(buf, when))
12165 12170 break;
12166 12171
12167 12172 if (!dtrace_buffer_consumed(aggbuf, when))
12168 12173 break;
12169 12174
12170 12175 dtrace_ecb_disable(ecb);
12171 12176 ASSERT(probe->dtpr_ecb != ecb);
12172 12177 dtrace_ecb_destroy(ecb);
12173 12178 }
12174 12179 }
12175 12180
12176 12181 mutex_exit(&dtrace_lock);
12177 12182 mutex_exit(&cpu_lock);
12178 12183 }
12179 12184
12180 12185 /*
12181 12186 * DTrace DOF Functions
12182 12187 */
12183 12188 /*ARGSUSED*/
12184 12189 static void
12185 12190 dtrace_dof_error(dof_hdr_t *dof, const char *str)
12186 12191 {
12187 12192 if (dtrace_err_verbose)
12188 12193 cmn_err(CE_WARN, "failed to process DOF: %s", str);
12189 12194
12190 12195 #ifdef DTRACE_ERRDEBUG
12191 12196 dtrace_errdebug(str);
12192 12197 #endif
12193 12198 }
12194 12199
12195 12200 /*
12196 12201 * Create DOF out of a currently enabled state. Right now, we only create
12197 12202 * DOF containing the run-time options -- but this could be expanded to create
12198 12203 * complete DOF representing the enabled state.
12199 12204 */
12200 12205 static dof_hdr_t *
12201 12206 dtrace_dof_create(dtrace_state_t *state)
12202 12207 {
12203 12208 dof_hdr_t *dof;
12204 12209 dof_sec_t *sec;
12205 12210 dof_optdesc_t *opt;
12206 12211 int i, len = sizeof (dof_hdr_t) +
12207 12212 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
12208 12213 sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
12209 12214
12210 12215 ASSERT(MUTEX_HELD(&dtrace_lock));
12211 12216
12212 12217 dof = kmem_zalloc(len, KM_SLEEP);
12213 12218 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
12214 12219 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
12215 12220 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
12216 12221 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
12217 12222
12218 12223 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
12219 12224 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
12220 12225 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
12221 12226 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
12222 12227 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
12223 12228 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
12224 12229
12225 12230 dof->dofh_flags = 0;
12226 12231 dof->dofh_hdrsize = sizeof (dof_hdr_t);
12227 12232 dof->dofh_secsize = sizeof (dof_sec_t);
12228 12233 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
12229 12234 dof->dofh_secoff = sizeof (dof_hdr_t);
12230 12235 dof->dofh_loadsz = len;
12231 12236 dof->dofh_filesz = len;
12232 12237 dof->dofh_pad = 0;
12233 12238
12234 12239 /*
12235 12240 * Fill in the option section header...
12236 12241 */
12237 12242 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
12238 12243 sec->dofs_type = DOF_SECT_OPTDESC;
12239 12244 sec->dofs_align = sizeof (uint64_t);
12240 12245 sec->dofs_flags = DOF_SECF_LOAD;
12241 12246 sec->dofs_entsize = sizeof (dof_optdesc_t);
12242 12247
12243 12248 opt = (dof_optdesc_t *)((uintptr_t)sec +
12244 12249 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
12245 12250
12246 12251 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
12247 12252 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
12248 12253
12249 12254 for (i = 0; i < DTRACEOPT_MAX; i++) {
12250 12255 opt[i].dofo_option = i;
12251 12256 opt[i].dofo_strtab = DOF_SECIDX_NONE;
12252 12257 opt[i].dofo_value = state->dts_options[i];
12253 12258 }
12254 12259
12255 12260 return (dof);
12256 12261 }
12257 12262
12258 12263 static dof_hdr_t *
12259 12264 dtrace_dof_copyin(uintptr_t uarg, int *errp)
12260 12265 {
12261 12266 dof_hdr_t hdr, *dof;
12262 12267
12263 12268 ASSERT(!MUTEX_HELD(&dtrace_lock));
12264 12269
12265 12270 /*
12266 12271 * First, we're going to copyin() the sizeof (dof_hdr_t).
12267 12272 */
12268 12273 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
12269 12274 dtrace_dof_error(NULL, "failed to copyin DOF header");
12270 12275 *errp = EFAULT;
12271 12276 return (NULL);
12272 12277 }
12273 12278
12274 12279 /*
12275 12280 * Now we'll allocate the entire DOF and copy it in -- provided
12276 12281 * that the length isn't outrageous.
12277 12282 */
12278 12283 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
12279 12284 dtrace_dof_error(&hdr, "load size exceeds maximum");
12280 12285 *errp = E2BIG;
12281 12286 return (NULL);
12282 12287 }
12283 12288
12284 12289 if (hdr.dofh_loadsz < sizeof (hdr)) {
12285 12290 dtrace_dof_error(&hdr, "invalid load size");
12286 12291 *errp = EINVAL;
12287 12292 return (NULL);
12288 12293 }
12289 12294
12290 12295 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
12291 12296
12292 12297 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 ||
12293 12298 dof->dofh_loadsz != hdr.dofh_loadsz) {
12294 12299 kmem_free(dof, hdr.dofh_loadsz);
12295 12300 *errp = EFAULT;
12296 12301 return (NULL);
12297 12302 }
12298 12303
12299 12304 return (dof);
12300 12305 }
12301 12306
12302 12307 static dof_hdr_t *
12303 12308 dtrace_dof_property(const char *name)
12304 12309 {
12305 12310 uchar_t *buf;
12306 12311 uint64_t loadsz;
12307 12312 unsigned int len, i;
12308 12313 dof_hdr_t *dof;
12309 12314
12310 12315 /*
12311 12316 * Unfortunately, array of values in .conf files are always (and
12312 12317 * only) interpreted to be integer arrays. We must read our DOF
12313 12318 * as an integer array, and then squeeze it into a byte array.
12314 12319 */
12315 12320 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
12316 12321 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
12317 12322 return (NULL);
12318 12323
12319 12324 for (i = 0; i < len; i++)
12320 12325 buf[i] = (uchar_t)(((int *)buf)[i]);
12321 12326
12322 12327 if (len < sizeof (dof_hdr_t)) {
12323 12328 ddi_prop_free(buf);
12324 12329 dtrace_dof_error(NULL, "truncated header");
12325 12330 return (NULL);
12326 12331 }
12327 12332
12328 12333 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
12329 12334 ddi_prop_free(buf);
12330 12335 dtrace_dof_error(NULL, "truncated DOF");
12331 12336 return (NULL);
12332 12337 }
12333 12338
12334 12339 if (loadsz >= dtrace_dof_maxsize) {
12335 12340 ddi_prop_free(buf);
12336 12341 dtrace_dof_error(NULL, "oversized DOF");
12337 12342 return (NULL);
12338 12343 }
12339 12344
12340 12345 dof = kmem_alloc(loadsz, KM_SLEEP);
12341 12346 bcopy(buf, dof, loadsz);
12342 12347 ddi_prop_free(buf);
12343 12348
12344 12349 return (dof);
12345 12350 }
12346 12351
12347 12352 static void
12348 12353 dtrace_dof_destroy(dof_hdr_t *dof)
12349 12354 {
12350 12355 kmem_free(dof, dof->dofh_loadsz);
12351 12356 }
12352 12357
12353 12358 /*
12354 12359 * Return the dof_sec_t pointer corresponding to a given section index. If the
12355 12360 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
12356 12361 * a type other than DOF_SECT_NONE is specified, the header is checked against
12357 12362 * this type and NULL is returned if the types do not match.
12358 12363 */
12359 12364 static dof_sec_t *
12360 12365 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
12361 12366 {
12362 12367 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
12363 12368 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
12364 12369
12365 12370 if (i >= dof->dofh_secnum) {
12366 12371 dtrace_dof_error(dof, "referenced section index is invalid");
12367 12372 return (NULL);
12368 12373 }
12369 12374
12370 12375 if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
12371 12376 dtrace_dof_error(dof, "referenced section is not loadable");
12372 12377 return (NULL);
12373 12378 }
12374 12379
12375 12380 if (type != DOF_SECT_NONE && type != sec->dofs_type) {
12376 12381 dtrace_dof_error(dof, "referenced section is the wrong type");
12377 12382 return (NULL);
12378 12383 }
12379 12384
12380 12385 return (sec);
12381 12386 }
12382 12387
12383 12388 static dtrace_probedesc_t *
12384 12389 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
12385 12390 {
12386 12391 dof_probedesc_t *probe;
12387 12392 dof_sec_t *strtab;
12388 12393 uintptr_t daddr = (uintptr_t)dof;
12389 12394 uintptr_t str;
12390 12395 size_t size;
12391 12396
12392 12397 if (sec->dofs_type != DOF_SECT_PROBEDESC) {
12393 12398 dtrace_dof_error(dof, "invalid probe section");
12394 12399 return (NULL);
12395 12400 }
12396 12401
12397 12402 if (sec->dofs_align != sizeof (dof_secidx_t)) {
12398 12403 dtrace_dof_error(dof, "bad alignment in probe description");
12399 12404 return (NULL);
12400 12405 }
12401 12406
12402 12407 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
12403 12408 dtrace_dof_error(dof, "truncated probe description");
12404 12409 return (NULL);
12405 12410 }
12406 12411
12407 12412 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
12408 12413 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
12409 12414
12410 12415 if (strtab == NULL)
12411 12416 return (NULL);
12412 12417
12413 12418 str = daddr + strtab->dofs_offset;
12414 12419 size = strtab->dofs_size;
12415 12420
12416 12421 if (probe->dofp_provider >= strtab->dofs_size) {
12417 12422 dtrace_dof_error(dof, "corrupt probe provider");
12418 12423 return (NULL);
12419 12424 }
12420 12425
12421 12426 (void) strncpy(desc->dtpd_provider,
12422 12427 (char *)(str + probe->dofp_provider),
12423 12428 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
12424 12429
12425 12430 if (probe->dofp_mod >= strtab->dofs_size) {
12426 12431 dtrace_dof_error(dof, "corrupt probe module");
12427 12432 return (NULL);
12428 12433 }
12429 12434
12430 12435 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
12431 12436 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
12432 12437
12433 12438 if (probe->dofp_func >= strtab->dofs_size) {
12434 12439 dtrace_dof_error(dof, "corrupt probe function");
12435 12440 return (NULL);
12436 12441 }
12437 12442
12438 12443 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
12439 12444 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
12440 12445
12441 12446 if (probe->dofp_name >= strtab->dofs_size) {
12442 12447 dtrace_dof_error(dof, "corrupt probe name");
12443 12448 return (NULL);
12444 12449 }
12445 12450
12446 12451 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
12447 12452 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
12448 12453
12449 12454 return (desc);
12450 12455 }
12451 12456
12452 12457 static dtrace_difo_t *
12453 12458 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12454 12459 cred_t *cr)
12455 12460 {
12456 12461 dtrace_difo_t *dp;
12457 12462 size_t ttl = 0;
12458 12463 dof_difohdr_t *dofd;
12459 12464 uintptr_t daddr = (uintptr_t)dof;
12460 12465 size_t max = dtrace_difo_maxsize;
12461 12466 int i, l, n;
12462 12467
12463 12468 static const struct {
12464 12469 int section;
12465 12470 int bufoffs;
12466 12471 int lenoffs;
12467 12472 int entsize;
12468 12473 int align;
12469 12474 const char *msg;
12470 12475 } difo[] = {
12471 12476 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
12472 12477 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
12473 12478 sizeof (dif_instr_t), "multiple DIF sections" },
12474 12479
12475 12480 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
12476 12481 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
12477 12482 sizeof (uint64_t), "multiple integer tables" },
12478 12483
12479 12484 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
12480 12485 offsetof(dtrace_difo_t, dtdo_strlen), 0,
12481 12486 sizeof (char), "multiple string tables" },
12482 12487
12483 12488 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
12484 12489 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
12485 12490 sizeof (uint_t), "multiple variable tables" },
12486 12491
12487 12492 { DOF_SECT_NONE, 0, 0, 0, NULL }
12488 12493 };
12489 12494
12490 12495 if (sec->dofs_type != DOF_SECT_DIFOHDR) {
12491 12496 dtrace_dof_error(dof, "invalid DIFO header section");
12492 12497 return (NULL);
12493 12498 }
12494 12499
12495 12500 if (sec->dofs_align != sizeof (dof_secidx_t)) {
12496 12501 dtrace_dof_error(dof, "bad alignment in DIFO header");
12497 12502 return (NULL);
12498 12503 }
12499 12504
12500 12505 if (sec->dofs_size < sizeof (dof_difohdr_t) ||
12501 12506 sec->dofs_size % sizeof (dof_secidx_t)) {
12502 12507 dtrace_dof_error(dof, "bad size in DIFO header");
12503 12508 return (NULL);
12504 12509 }
12505 12510
12506 12511 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12507 12512 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
12508 12513
12509 12514 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
12510 12515 dp->dtdo_rtype = dofd->dofd_rtype;
12511 12516
12512 12517 for (l = 0; l < n; l++) {
12513 12518 dof_sec_t *subsec;
12514 12519 void **bufp;
12515 12520 uint32_t *lenp;
12516 12521
12517 12522 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
12518 12523 dofd->dofd_links[l])) == NULL)
12519 12524 goto err; /* invalid section link */
12520 12525
12521 12526 if (ttl + subsec->dofs_size > max) {
12522 12527 dtrace_dof_error(dof, "exceeds maximum size");
12523 12528 goto err;
12524 12529 }
12525 12530
12526 12531 ttl += subsec->dofs_size;
12527 12532
12528 12533 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
12529 12534 if (subsec->dofs_type != difo[i].section)
12530 12535 continue;
12531 12536
12532 12537 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
12533 12538 dtrace_dof_error(dof, "section not loaded");
12534 12539 goto err;
12535 12540 }
12536 12541
12537 12542 if (subsec->dofs_align != difo[i].align) {
12538 12543 dtrace_dof_error(dof, "bad alignment");
12539 12544 goto err;
12540 12545 }
12541 12546
12542 12547 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
12543 12548 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
12544 12549
12545 12550 if (*bufp != NULL) {
12546 12551 dtrace_dof_error(dof, difo[i].msg);
12547 12552 goto err;
12548 12553 }
12549 12554
12550 12555 if (difo[i].entsize != subsec->dofs_entsize) {
12551 12556 dtrace_dof_error(dof, "entry size mismatch");
12552 12557 goto err;
12553 12558 }
12554 12559
12555 12560 if (subsec->dofs_entsize != 0 &&
12556 12561 (subsec->dofs_size % subsec->dofs_entsize) != 0) {
12557 12562 dtrace_dof_error(dof, "corrupt entry size");
12558 12563 goto err;
12559 12564 }
12560 12565
12561 12566 *lenp = subsec->dofs_size;
12562 12567 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
12563 12568 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
12564 12569 *bufp, subsec->dofs_size);
12565 12570
12566 12571 if (subsec->dofs_entsize != 0)
12567 12572 *lenp /= subsec->dofs_entsize;
12568 12573
12569 12574 break;
12570 12575 }
12571 12576
12572 12577 /*
12573 12578 * If we encounter a loadable DIFO sub-section that is not
12574 12579 * known to us, assume this is a broken program and fail.
12575 12580 */
12576 12581 if (difo[i].section == DOF_SECT_NONE &&
12577 12582 (subsec->dofs_flags & DOF_SECF_LOAD)) {
12578 12583 dtrace_dof_error(dof, "unrecognized DIFO subsection");
12579 12584 goto err;
12580 12585 }
12581 12586 }
12582 12587
12583 12588 if (dp->dtdo_buf == NULL) {
12584 12589 /*
12585 12590 * We can't have a DIF object without DIF text.
12586 12591 */
12587 12592 dtrace_dof_error(dof, "missing DIF text");
12588 12593 goto err;
12589 12594 }
12590 12595
12591 12596 /*
12592 12597 * Before we validate the DIF object, run through the variable table
12593 12598 * looking for the strings -- if any of their size are under, we'll set
12594 12599 * their size to be the system-wide default string size. Note that
12595 12600 * this should _not_ happen if the "strsize" option has been set --
12596 12601 * in this case, the compiler should have set the size to reflect the
12597 12602 * setting of the option.
12598 12603 */
12599 12604 for (i = 0; i < dp->dtdo_varlen; i++) {
12600 12605 dtrace_difv_t *v = &dp->dtdo_vartab[i];
12601 12606 dtrace_diftype_t *t = &v->dtdv_type;
12602 12607
12603 12608 if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
12604 12609 continue;
12605 12610
12606 12611 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
12607 12612 t->dtdt_size = dtrace_strsize_default;
12608 12613 }
12609 12614
12610 12615 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
12611 12616 goto err;
12612 12617
12613 12618 dtrace_difo_init(dp, vstate);
12614 12619 return (dp);
12615 12620
12616 12621 err:
12617 12622 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
12618 12623 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
12619 12624 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
12620 12625 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
12621 12626
12622 12627 kmem_free(dp, sizeof (dtrace_difo_t));
12623 12628 return (NULL);
12624 12629 }
12625 12630
12626 12631 static dtrace_predicate_t *
12627 12632 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12628 12633 cred_t *cr)
12629 12634 {
12630 12635 dtrace_difo_t *dp;
12631 12636
12632 12637 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
12633 12638 return (NULL);
12634 12639
12635 12640 return (dtrace_predicate_create(dp));
12636 12641 }
12637 12642
12638 12643 static dtrace_actdesc_t *
12639 12644 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12640 12645 cred_t *cr)
12641 12646 {
12642 12647 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
12643 12648 dof_actdesc_t *desc;
12644 12649 dof_sec_t *difosec;
12645 12650 size_t offs;
12646 12651 uintptr_t daddr = (uintptr_t)dof;
12647 12652 uint64_t arg;
12648 12653 dtrace_actkind_t kind;
12649 12654
12650 12655 if (sec->dofs_type != DOF_SECT_ACTDESC) {
12651 12656 dtrace_dof_error(dof, "invalid action section");
12652 12657 return (NULL);
12653 12658 }
12654 12659
12655 12660 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
12656 12661 dtrace_dof_error(dof, "truncated action description");
12657 12662 return (NULL);
12658 12663 }
12659 12664
12660 12665 if (sec->dofs_align != sizeof (uint64_t)) {
12661 12666 dtrace_dof_error(dof, "bad alignment in action description");
12662 12667 return (NULL);
12663 12668 }
12664 12669
12665 12670 if (sec->dofs_size < sec->dofs_entsize) {
12666 12671 dtrace_dof_error(dof, "section entry size exceeds total size");
12667 12672 return (NULL);
12668 12673 }
12669 12674
12670 12675 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
12671 12676 dtrace_dof_error(dof, "bad entry size in action description");
12672 12677 return (NULL);
12673 12678 }
12674 12679
12675 12680 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
12676 12681 dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
12677 12682 return (NULL);
12678 12683 }
12679 12684
12680 12685 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
12681 12686 desc = (dof_actdesc_t *)(daddr +
12682 12687 (uintptr_t)sec->dofs_offset + offs);
12683 12688 kind = (dtrace_actkind_t)desc->dofa_kind;
12684 12689
12685 12690 if ((DTRACEACT_ISPRINTFLIKE(kind) &&
12686 12691 (kind != DTRACEACT_PRINTA ||
12687 12692 desc->dofa_strtab != DOF_SECIDX_NONE)) ||
12688 12693 (kind == DTRACEACT_DIFEXPR &&
12689 12694 desc->dofa_strtab != DOF_SECIDX_NONE)) {
12690 12695 dof_sec_t *strtab;
12691 12696 char *str, *fmt;
12692 12697 uint64_t i;
12693 12698
12694 12699 /*
12695 12700 * The argument to these actions is an index into the
12696 12701 * DOF string table. For printf()-like actions, this
12697 12702 * is the format string. For print(), this is the
12698 12703 * CTF type of the expression result.
12699 12704 */
12700 12705 if ((strtab = dtrace_dof_sect(dof,
12701 12706 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
12702 12707 goto err;
12703 12708
12704 12709 str = (char *)((uintptr_t)dof +
12705 12710 (uintptr_t)strtab->dofs_offset);
12706 12711
12707 12712 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
12708 12713 if (str[i] == '\0')
12709 12714 break;
12710 12715 }
12711 12716
12712 12717 if (i >= strtab->dofs_size) {
12713 12718 dtrace_dof_error(dof, "bogus format string");
12714 12719 goto err;
12715 12720 }
12716 12721
12717 12722 if (i == desc->dofa_arg) {
12718 12723 dtrace_dof_error(dof, "empty format string");
12719 12724 goto err;
12720 12725 }
12721 12726
12722 12727 i -= desc->dofa_arg;
12723 12728 fmt = kmem_alloc(i + 1, KM_SLEEP);
12724 12729 bcopy(&str[desc->dofa_arg], fmt, i + 1);
12725 12730 arg = (uint64_t)(uintptr_t)fmt;
12726 12731 } else {
12727 12732 if (kind == DTRACEACT_PRINTA) {
12728 12733 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
12729 12734 arg = 0;
12730 12735 } else {
12731 12736 arg = desc->dofa_arg;
12732 12737 }
12733 12738 }
12734 12739
12735 12740 act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
12736 12741 desc->dofa_uarg, arg);
12737 12742
12738 12743 if (last != NULL) {
12739 12744 last->dtad_next = act;
12740 12745 } else {
12741 12746 first = act;
12742 12747 }
12743 12748
12744 12749 last = act;
12745 12750
12746 12751 if (desc->dofa_difo == DOF_SECIDX_NONE)
12747 12752 continue;
12748 12753
12749 12754 if ((difosec = dtrace_dof_sect(dof,
12750 12755 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
12751 12756 goto err;
12752 12757
12753 12758 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
12754 12759
12755 12760 if (act->dtad_difo == NULL)
12756 12761 goto err;
12757 12762 }
12758 12763
12759 12764 ASSERT(first != NULL);
12760 12765 return (first);
12761 12766
12762 12767 err:
12763 12768 for (act = first; act != NULL; act = next) {
12764 12769 next = act->dtad_next;
12765 12770 dtrace_actdesc_release(act, vstate);
12766 12771 }
12767 12772
12768 12773 return (NULL);
12769 12774 }
12770 12775
12771 12776 static dtrace_ecbdesc_t *
12772 12777 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12773 12778 cred_t *cr)
12774 12779 {
12775 12780 dtrace_ecbdesc_t *ep;
12776 12781 dof_ecbdesc_t *ecb;
12777 12782 dtrace_probedesc_t *desc;
12778 12783 dtrace_predicate_t *pred = NULL;
12779 12784
12780 12785 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
12781 12786 dtrace_dof_error(dof, "truncated ECB description");
12782 12787 return (NULL);
12783 12788 }
12784 12789
12785 12790 if (sec->dofs_align != sizeof (uint64_t)) {
12786 12791 dtrace_dof_error(dof, "bad alignment in ECB description");
12787 12792 return (NULL);
12788 12793 }
12789 12794
12790 12795 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
12791 12796 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
12792 12797
12793 12798 if (sec == NULL)
12794 12799 return (NULL);
12795 12800
12796 12801 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12797 12802 ep->dted_uarg = ecb->dofe_uarg;
12798 12803 desc = &ep->dted_probe;
12799 12804
12800 12805 if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
12801 12806 goto err;
12802 12807
12803 12808 if (ecb->dofe_pred != DOF_SECIDX_NONE) {
12804 12809 if ((sec = dtrace_dof_sect(dof,
12805 12810 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
12806 12811 goto err;
12807 12812
12808 12813 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
12809 12814 goto err;
12810 12815
12811 12816 ep->dted_pred.dtpdd_predicate = pred;
12812 12817 }
12813 12818
12814 12819 if (ecb->dofe_actions != DOF_SECIDX_NONE) {
12815 12820 if ((sec = dtrace_dof_sect(dof,
12816 12821 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
12817 12822 goto err;
12818 12823
12819 12824 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
12820 12825
12821 12826 if (ep->dted_action == NULL)
12822 12827 goto err;
12823 12828 }
12824 12829
12825 12830 return (ep);
12826 12831
12827 12832 err:
12828 12833 if (pred != NULL)
12829 12834 dtrace_predicate_release(pred, vstate);
12830 12835 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12831 12836 return (NULL);
12832 12837 }
12833 12838
12834 12839 /*
12835 12840 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
12836 12841 * specified DOF. At present, this amounts to simply adding 'ubase' to the
12837 12842 * site of any user SETX relocations to account for load object base address.
12838 12843 * In the future, if we need other relocations, this function can be extended.
12839 12844 */
12840 12845 static int
12841 12846 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase)
12842 12847 {
12843 12848 uintptr_t daddr = (uintptr_t)dof;
12844 12849 dof_relohdr_t *dofr =
12845 12850 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12846 12851 dof_sec_t *ss, *rs, *ts;
12847 12852 dof_relodesc_t *r;
12848 12853 uint_t i, n;
12849 12854
12850 12855 if (sec->dofs_size < sizeof (dof_relohdr_t) ||
12851 12856 sec->dofs_align != sizeof (dof_secidx_t)) {
12852 12857 dtrace_dof_error(dof, "invalid relocation header");
12853 12858 return (-1);
12854 12859 }
12855 12860
12856 12861 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
12857 12862 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
12858 12863 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
12859 12864
12860 12865 if (ss == NULL || rs == NULL || ts == NULL)
12861 12866 return (-1); /* dtrace_dof_error() has been called already */
12862 12867
12863 12868 if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
12864 12869 rs->dofs_align != sizeof (uint64_t)) {
12865 12870 dtrace_dof_error(dof, "invalid relocation section");
12866 12871 return (-1);
12867 12872 }
12868 12873
12869 12874 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
12870 12875 n = rs->dofs_size / rs->dofs_entsize;
12871 12876
12872 12877 for (i = 0; i < n; i++) {
12873 12878 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
12874 12879
12875 12880 switch (r->dofr_type) {
12876 12881 case DOF_RELO_NONE:
12877 12882 break;
12878 12883 case DOF_RELO_SETX:
12879 12884 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
12880 12885 sizeof (uint64_t) > ts->dofs_size) {
12881 12886 dtrace_dof_error(dof, "bad relocation offset");
12882 12887 return (-1);
12883 12888 }
12884 12889
12885 12890 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
12886 12891 dtrace_dof_error(dof, "misaligned setx relo");
12887 12892 return (-1);
12888 12893 }
12889 12894
12890 12895 *(uint64_t *)taddr += ubase;
12891 12896 break;
12892 12897 default:
12893 12898 dtrace_dof_error(dof, "invalid relocation type");
12894 12899 return (-1);
12895 12900 }
12896 12901
12897 12902 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
12898 12903 }
12899 12904
12900 12905 return (0);
12901 12906 }
12902 12907
12903 12908 /*
12904 12909 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
12905 12910 * header: it should be at the front of a memory region that is at least
12906 12911 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
12907 12912 * size. It need not be validated in any other way.
12908 12913 */
12909 12914 static int
12910 12915 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
12911 12916 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes)
12912 12917 {
12913 12918 uint64_t len = dof->dofh_loadsz, seclen;
12914 12919 uintptr_t daddr = (uintptr_t)dof;
12915 12920 dtrace_ecbdesc_t *ep;
12916 12921 dtrace_enabling_t *enab;
12917 12922 uint_t i;
12918 12923
12919 12924 ASSERT(MUTEX_HELD(&dtrace_lock));
12920 12925 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
12921 12926
12922 12927 /*
12923 12928 * Check the DOF header identification bytes. In addition to checking
12924 12929 * valid settings, we also verify that unused bits/bytes are zeroed so
12925 12930 * we can use them later without fear of regressing existing binaries.
12926 12931 */
12927 12932 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
12928 12933 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
12929 12934 dtrace_dof_error(dof, "DOF magic string mismatch");
12930 12935 return (-1);
12931 12936 }
12932 12937
12933 12938 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
12934 12939 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
12935 12940 dtrace_dof_error(dof, "DOF has invalid data model");
12936 12941 return (-1);
12937 12942 }
12938 12943
12939 12944 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
12940 12945 dtrace_dof_error(dof, "DOF encoding mismatch");
12941 12946 return (-1);
12942 12947 }
12943 12948
12944 12949 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
12945 12950 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
12946 12951 dtrace_dof_error(dof, "DOF version mismatch");
12947 12952 return (-1);
12948 12953 }
12949 12954
12950 12955 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
12951 12956 dtrace_dof_error(dof, "DOF uses unsupported instruction set");
12952 12957 return (-1);
12953 12958 }
12954 12959
12955 12960 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
12956 12961 dtrace_dof_error(dof, "DOF uses too many integer registers");
12957 12962 return (-1);
12958 12963 }
12959 12964
12960 12965 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
12961 12966 dtrace_dof_error(dof, "DOF uses too many tuple registers");
12962 12967 return (-1);
12963 12968 }
12964 12969
12965 12970 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
12966 12971 if (dof->dofh_ident[i] != 0) {
12967 12972 dtrace_dof_error(dof, "DOF has invalid ident byte set");
12968 12973 return (-1);
12969 12974 }
12970 12975 }
12971 12976
12972 12977 if (dof->dofh_flags & ~DOF_FL_VALID) {
12973 12978 dtrace_dof_error(dof, "DOF has invalid flag bits set");
12974 12979 return (-1);
12975 12980 }
12976 12981
12977 12982 if (dof->dofh_secsize == 0) {
12978 12983 dtrace_dof_error(dof, "zero section header size");
12979 12984 return (-1);
12980 12985 }
12981 12986
12982 12987 /*
12983 12988 * Check that the section headers don't exceed the amount of DOF
12984 12989 * data. Note that we cast the section size and number of sections
12985 12990 * to uint64_t's to prevent possible overflow in the multiplication.
12986 12991 */
12987 12992 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
12988 12993
12989 12994 if (dof->dofh_secoff > len || seclen > len ||
12990 12995 dof->dofh_secoff + seclen > len) {
12991 12996 dtrace_dof_error(dof, "truncated section headers");
12992 12997 return (-1);
12993 12998 }
12994 12999
12995 13000 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
12996 13001 dtrace_dof_error(dof, "misaligned section headers");
12997 13002 return (-1);
12998 13003 }
12999 13004
13000 13005 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
13001 13006 dtrace_dof_error(dof, "misaligned section size");
13002 13007 return (-1);
13003 13008 }
13004 13009
13005 13010 /*
13006 13011 * Take an initial pass through the section headers to be sure that
13007 13012 * the headers don't have stray offsets. If the 'noprobes' flag is
13008 13013 * set, do not permit sections relating to providers, probes, or args.
13009 13014 */
13010 13015 for (i = 0; i < dof->dofh_secnum; i++) {
13011 13016 dof_sec_t *sec = (dof_sec_t *)(daddr +
13012 13017 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13013 13018
13014 13019 if (noprobes) {
13015 13020 switch (sec->dofs_type) {
13016 13021 case DOF_SECT_PROVIDER:
13017 13022 case DOF_SECT_PROBES:
13018 13023 case DOF_SECT_PRARGS:
13019 13024 case DOF_SECT_PROFFS:
13020 13025 dtrace_dof_error(dof, "illegal sections "
13021 13026 "for enabling");
13022 13027 return (-1);
13023 13028 }
13024 13029 }
13025 13030
13026 13031 if (DOF_SEC_ISLOADABLE(sec->dofs_type) &&
13027 13032 !(sec->dofs_flags & DOF_SECF_LOAD)) {
13028 13033 dtrace_dof_error(dof, "loadable section with load "
13029 13034 "flag unset");
13030 13035 return (-1);
13031 13036 }
13032 13037
13033 13038 if (!(sec->dofs_flags & DOF_SECF_LOAD))
13034 13039 continue; /* just ignore non-loadable sections */
13035 13040
13036 13041 if (sec->dofs_align & (sec->dofs_align - 1)) {
13037 13042 dtrace_dof_error(dof, "bad section alignment");
13038 13043 return (-1);
13039 13044 }
13040 13045
13041 13046 if (sec->dofs_offset & (sec->dofs_align - 1)) {
13042 13047 dtrace_dof_error(dof, "misaligned section");
13043 13048 return (-1);
13044 13049 }
13045 13050
13046 13051 if (sec->dofs_offset > len || sec->dofs_size > len ||
13047 13052 sec->dofs_offset + sec->dofs_size > len) {
13048 13053 dtrace_dof_error(dof, "corrupt section header");
13049 13054 return (-1);
13050 13055 }
13051 13056
13052 13057 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
13053 13058 sec->dofs_offset + sec->dofs_size - 1) != '\0') {
13054 13059 dtrace_dof_error(dof, "non-terminating string table");
13055 13060 return (-1);
13056 13061 }
13057 13062 }
13058 13063
13059 13064 /*
13060 13065 * Take a second pass through the sections and locate and perform any
13061 13066 * relocations that are present. We do this after the first pass to
13062 13067 * be sure that all sections have had their headers validated.
13063 13068 */
13064 13069 for (i = 0; i < dof->dofh_secnum; i++) {
13065 13070 dof_sec_t *sec = (dof_sec_t *)(daddr +
13066 13071 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13067 13072
13068 13073 if (!(sec->dofs_flags & DOF_SECF_LOAD))
13069 13074 continue; /* skip sections that are not loadable */
13070 13075
13071 13076 switch (sec->dofs_type) {
13072 13077 case DOF_SECT_URELHDR:
13073 13078 if (dtrace_dof_relocate(dof, sec, ubase) != 0)
13074 13079 return (-1);
13075 13080 break;
13076 13081 }
13077 13082 }
13078 13083
13079 13084 if ((enab = *enabp) == NULL)
13080 13085 enab = *enabp = dtrace_enabling_create(vstate);
13081 13086
13082 13087 for (i = 0; i < dof->dofh_secnum; i++) {
13083 13088 dof_sec_t *sec = (dof_sec_t *)(daddr +
13084 13089 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13085 13090
13086 13091 if (sec->dofs_type != DOF_SECT_ECBDESC)
13087 13092 continue;
13088 13093
13089 13094 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
13090 13095 dtrace_enabling_destroy(enab);
13091 13096 *enabp = NULL;
13092 13097 return (-1);
13093 13098 }
13094 13099
13095 13100 dtrace_enabling_add(enab, ep);
13096 13101 }
13097 13102
13098 13103 return (0);
13099 13104 }
13100 13105
13101 13106 /*
13102 13107 * Process DOF for any options. This routine assumes that the DOF has been
13103 13108 * at least processed by dtrace_dof_slurp().
13104 13109 */
13105 13110 static int
13106 13111 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
13107 13112 {
13108 13113 int i, rval;
13109 13114 uint32_t entsize;
13110 13115 size_t offs;
13111 13116 dof_optdesc_t *desc;
13112 13117
13113 13118 for (i = 0; i < dof->dofh_secnum; i++) {
13114 13119 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
13115 13120 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13116 13121
13117 13122 if (sec->dofs_type != DOF_SECT_OPTDESC)
13118 13123 continue;
13119 13124
13120 13125 if (sec->dofs_align != sizeof (uint64_t)) {
13121 13126 dtrace_dof_error(dof, "bad alignment in "
13122 13127 "option description");
13123 13128 return (EINVAL);
13124 13129 }
13125 13130
13126 13131 if ((entsize = sec->dofs_entsize) == 0) {
13127 13132 dtrace_dof_error(dof, "zeroed option entry size");
13128 13133 return (EINVAL);
13129 13134 }
13130 13135
13131 13136 if (entsize < sizeof (dof_optdesc_t)) {
13132 13137 dtrace_dof_error(dof, "bad option entry size");
13133 13138 return (EINVAL);
13134 13139 }
13135 13140
13136 13141 for (offs = 0; offs < sec->dofs_size; offs += entsize) {
13137 13142 desc = (dof_optdesc_t *)((uintptr_t)dof +
13138 13143 (uintptr_t)sec->dofs_offset + offs);
13139 13144
13140 13145 if (desc->dofo_strtab != DOF_SECIDX_NONE) {
13141 13146 dtrace_dof_error(dof, "non-zero option string");
13142 13147 return (EINVAL);
13143 13148 }
13144 13149
13145 13150 if (desc->dofo_value == DTRACEOPT_UNSET) {
13146 13151 dtrace_dof_error(dof, "unset option");
13147 13152 return (EINVAL);
13148 13153 }
13149 13154
13150 13155 if ((rval = dtrace_state_option(state,
13151 13156 desc->dofo_option, desc->dofo_value)) != 0) {
13152 13157 dtrace_dof_error(dof, "rejected option");
13153 13158 return (rval);
13154 13159 }
13155 13160 }
13156 13161 }
13157 13162
13158 13163 return (0);
13159 13164 }
13160 13165
13161 13166 /*
13162 13167 * DTrace Consumer State Functions
13163 13168 */
13164 13169 int
13165 13170 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
13166 13171 {
13167 13172 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
13168 13173 void *base;
13169 13174 uintptr_t limit;
13170 13175 dtrace_dynvar_t *dvar, *next, *start;
13171 13176 int i;
13172 13177
13173 13178 ASSERT(MUTEX_HELD(&dtrace_lock));
13174 13179 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
13175 13180
13176 13181 bzero(dstate, sizeof (dtrace_dstate_t));
13177 13182
13178 13183 if ((dstate->dtds_chunksize = chunksize) == 0)
13179 13184 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
13180 13185
13181 13186 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
13182 13187 size = min;
13183 13188
13184 13189 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL)
13185 13190 return (ENOMEM);
13186 13191
13187 13192 dstate->dtds_size = size;
13188 13193 dstate->dtds_base = base;
13189 13194 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
13190 13195 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
13191 13196
13192 13197 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
13193 13198
13194 13199 if (hashsize != 1 && (hashsize & 1))
13195 13200 hashsize--;
13196 13201
13197 13202 dstate->dtds_hashsize = hashsize;
13198 13203 dstate->dtds_hash = dstate->dtds_base;
13199 13204
13200 13205 /*
13201 13206 * Set all of our hash buckets to point to the single sink, and (if
13202 13207 * it hasn't already been set), set the sink's hash value to be the
13203 13208 * sink sentinel value. The sink is needed for dynamic variable
13204 13209 * lookups to know that they have iterated over an entire, valid hash
13205 13210 * chain.
13206 13211 */
13207 13212 for (i = 0; i < hashsize; i++)
13208 13213 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
13209 13214
13210 13215 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
13211 13216 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
13212 13217
13213 13218 /*
13214 13219 * Determine number of active CPUs. Divide free list evenly among
13215 13220 * active CPUs.
13216 13221 */
13217 13222 start = (dtrace_dynvar_t *)
13218 13223 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
13219 13224 limit = (uintptr_t)base + size;
13220 13225
13221 13226 maxper = (limit - (uintptr_t)start) / NCPU;
13222 13227 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
13223 13228
13224 13229 for (i = 0; i < NCPU; i++) {
13225 13230 dstate->dtds_percpu[i].dtdsc_free = dvar = start;
13226 13231
13227 13232 /*
13228 13233 * If we don't even have enough chunks to make it once through
13229 13234 * NCPUs, we're just going to allocate everything to the first
13230 13235 * CPU. And if we're on the last CPU, we're going to allocate
13231 13236 * whatever is left over. In either case, we set the limit to
13232 13237 * be the limit of the dynamic variable space.
13233 13238 */
13234 13239 if (maxper == 0 || i == NCPU - 1) {
13235 13240 limit = (uintptr_t)base + size;
13236 13241 start = NULL;
13237 13242 } else {
13238 13243 limit = (uintptr_t)start + maxper;
13239 13244 start = (dtrace_dynvar_t *)limit;
13240 13245 }
13241 13246
13242 13247 ASSERT(limit <= (uintptr_t)base + size);
13243 13248
13244 13249 for (;;) {
13245 13250 next = (dtrace_dynvar_t *)((uintptr_t)dvar +
13246 13251 dstate->dtds_chunksize);
13247 13252
13248 13253 if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
13249 13254 break;
13250 13255
13251 13256 dvar->dtdv_next = next;
13252 13257 dvar = next;
13253 13258 }
13254 13259
13255 13260 if (maxper == 0)
13256 13261 break;
13257 13262 }
13258 13263
13259 13264 return (0);
13260 13265 }
13261 13266
13262 13267 void
13263 13268 dtrace_dstate_fini(dtrace_dstate_t *dstate)
13264 13269 {
13265 13270 ASSERT(MUTEX_HELD(&cpu_lock));
13266 13271
13267 13272 if (dstate->dtds_base == NULL)
13268 13273 return;
13269 13274
13270 13275 kmem_free(dstate->dtds_base, dstate->dtds_size);
13271 13276 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
13272 13277 }
13273 13278
13274 13279 static void
13275 13280 dtrace_vstate_fini(dtrace_vstate_t *vstate)
13276 13281 {
13277 13282 /*
13278 13283 * Logical XOR, where are you?
13279 13284 */
13280 13285 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
13281 13286
13282 13287 if (vstate->dtvs_nglobals > 0) {
13283 13288 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
13284 13289 sizeof (dtrace_statvar_t *));
13285 13290 }
13286 13291
13287 13292 if (vstate->dtvs_ntlocals > 0) {
13288 13293 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
13289 13294 sizeof (dtrace_difv_t));
13290 13295 }
13291 13296
13292 13297 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
13293 13298
13294 13299 if (vstate->dtvs_nlocals > 0) {
13295 13300 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
13296 13301 sizeof (dtrace_statvar_t *));
13297 13302 }
13298 13303 }
13299 13304
13300 13305 static void
13301 13306 dtrace_state_clean(dtrace_state_t *state)
13302 13307 {
13303 13308 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
13304 13309 return;
13305 13310
13306 13311 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
13307 13312 dtrace_speculation_clean(state);
13308 13313 }
13309 13314
13310 13315 static void
13311 13316 dtrace_state_deadman(dtrace_state_t *state)
13312 13317 {
13313 13318 hrtime_t now;
13314 13319
13315 13320 dtrace_sync();
13316 13321
13317 13322 now = dtrace_gethrtime();
13318 13323
13319 13324 if (state != dtrace_anon.dta_state &&
13320 13325 now - state->dts_laststatus >= dtrace_deadman_user)
13321 13326 return;
13322 13327
13323 13328 /*
13324 13329 * We must be sure that dts_alive never appears to be less than the
13325 13330 * value upon entry to dtrace_state_deadman(), and because we lack a
13326 13331 * dtrace_cas64(), we cannot store to it atomically. We thus instead
13327 13332 * store INT64_MAX to it, followed by a memory barrier, followed by
13328 13333 * the new value. This assures that dts_alive never appears to be
13329 13334 * less than its true value, regardless of the order in which the
13330 13335 * stores to the underlying storage are issued.
13331 13336 */
13332 13337 state->dts_alive = INT64_MAX;
13333 13338 dtrace_membar_producer();
13334 13339 state->dts_alive = now;
13335 13340 }
13336 13341
13337 13342 dtrace_state_t *
13338 13343 dtrace_state_create(dev_t *devp, cred_t *cr)
13339 13344 {
13340 13345 minor_t minor;
13341 13346 major_t major;
13342 13347 char c[30];
13343 13348 dtrace_state_t *state;
13344 13349 dtrace_optval_t *opt;
13345 13350 int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
13346 13351
13347 13352 ASSERT(MUTEX_HELD(&dtrace_lock));
13348 13353 ASSERT(MUTEX_HELD(&cpu_lock));
13349 13354
13350 13355 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
13351 13356 VM_BESTFIT | VM_SLEEP);
13352 13357
13353 13358 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
13354 13359 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
13355 13360 return (NULL);
13356 13361 }
13357 13362
13358 13363 state = ddi_get_soft_state(dtrace_softstate, minor);
13359 13364 state->dts_epid = DTRACE_EPIDNONE + 1;
13360 13365
13361 13366 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor);
13362 13367 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
13363 13368 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
13364 13369
13365 13370 if (devp != NULL) {
13366 13371 major = getemajor(*devp);
13367 13372 } else {
13368 13373 major = ddi_driver_major(dtrace_devi);
13369 13374 }
13370 13375
13371 13376 state->dts_dev = makedevice(major, minor);
13372 13377
13373 13378 if (devp != NULL)
13374 13379 *devp = state->dts_dev;
13375 13380
13376 13381 /*
13377 13382 * We allocate NCPU buffers. On the one hand, this can be quite
13378 13383 * a bit of memory per instance (nearly 36K on a Starcat). On the
13379 13384 * other hand, it saves an additional memory reference in the probe
13380 13385 * path.
13381 13386 */
13382 13387 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
13383 13388 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
13384 13389 state->dts_cleaner = CYCLIC_NONE;
13385 13390 state->dts_deadman = CYCLIC_NONE;
13386 13391 state->dts_vstate.dtvs_state = state;
13387 13392
13388 13393 for (i = 0; i < DTRACEOPT_MAX; i++)
13389 13394 state->dts_options[i] = DTRACEOPT_UNSET;
13390 13395
13391 13396 /*
13392 13397 * Set the default options.
13393 13398 */
13394 13399 opt = state->dts_options;
13395 13400 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
13396 13401 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
13397 13402 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
13398 13403 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
13399 13404 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
13400 13405 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
13401 13406 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
13402 13407 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
13403 13408 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
13404 13409 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
13405 13410 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
13406 13411 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
13407 13412 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
13408 13413 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
13409 13414
13410 13415 state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
13411 13416
13412 13417 /*
13413 13418 * Depending on the user credentials, we set flag bits which alter probe
13414 13419 * visibility or the amount of destructiveness allowed. In the case of
13415 13420 * actual anonymous tracing, or the possession of all privileges, all of
13416 13421 * the normal checks are bypassed.
13417 13422 */
13418 13423 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
13419 13424 state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
13420 13425 state->dts_cred.dcr_action = DTRACE_CRA_ALL;
13421 13426 } else {
13422 13427 /*
13423 13428 * Set up the credentials for this instantiation. We take a
13424 13429 * hold on the credential to prevent it from disappearing on
13425 13430 * us; this in turn prevents the zone_t referenced by this
13426 13431 * credential from disappearing. This means that we can
13427 13432 * examine the credential and the zone from probe context.
13428 13433 */
13429 13434 crhold(cr);
13430 13435 state->dts_cred.dcr_cred = cr;
13431 13436
13432 13437 /*
13433 13438 * CRA_PROC means "we have *some* privilege for dtrace" and
13434 13439 * unlocks the use of variables like pid, zonename, etc.
13435 13440 */
13436 13441 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
13437 13442 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
13438 13443 state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
13439 13444 }
13440 13445
13441 13446 /*
13442 13447 * dtrace_user allows use of syscall and profile providers.
13443 13448 * If the user also has proc_owner and/or proc_zone, we
13444 13449 * extend the scope to include additional visibility and
13445 13450 * destructive power.
13446 13451 */
13447 13452 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
13448 13453 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
13449 13454 state->dts_cred.dcr_visible |=
13450 13455 DTRACE_CRV_ALLPROC;
13451 13456
13452 13457 state->dts_cred.dcr_action |=
13453 13458 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13454 13459 }
13455 13460
13456 13461 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
13457 13462 state->dts_cred.dcr_visible |=
13458 13463 DTRACE_CRV_ALLZONE;
13459 13464
13460 13465 state->dts_cred.dcr_action |=
13461 13466 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13462 13467 }
13463 13468
13464 13469 /*
13465 13470 * If we have all privs in whatever zone this is,
13466 13471 * we can do destructive things to processes which
13467 13472 * have altered credentials.
13468 13473 */
13469 13474 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
13470 13475 cr->cr_zone->zone_privset)) {
13471 13476 state->dts_cred.dcr_action |=
13472 13477 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
13473 13478 }
13474 13479 }
13475 13480
13476 13481 /*
13477 13482 * Holding the dtrace_kernel privilege also implies that
13478 13483 * the user has the dtrace_user privilege from a visibility
13479 13484 * perspective. But without further privileges, some
13480 13485 * destructive actions are not available.
13481 13486 */
13482 13487 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
13483 13488 /*
13484 13489 * Make all probes in all zones visible. However,
13485 13490 * this doesn't mean that all actions become available
13486 13491 * to all zones.
13487 13492 */
13488 13493 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
13489 13494 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
13490 13495
13491 13496 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
13492 13497 DTRACE_CRA_PROC;
13493 13498 /*
13494 13499 * Holding proc_owner means that destructive actions
13495 13500 * for *this* zone are allowed.
13496 13501 */
13497 13502 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
13498 13503 state->dts_cred.dcr_action |=
13499 13504 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13500 13505
13501 13506 /*
13502 13507 * Holding proc_zone means that destructive actions
13503 13508 * for this user/group ID in all zones is allowed.
13504 13509 */
13505 13510 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
13506 13511 state->dts_cred.dcr_action |=
13507 13512 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13508 13513
13509 13514 /*
13510 13515 * If we have all privs in whatever zone this is,
13511 13516 * we can do destructive things to processes which
13512 13517 * have altered credentials.
13513 13518 */
13514 13519 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
13515 13520 cr->cr_zone->zone_privset)) {
13516 13521 state->dts_cred.dcr_action |=
13517 13522 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
13518 13523 }
13519 13524 }
13520 13525
13521 13526 /*
13522 13527 * Holding the dtrace_proc privilege gives control over fasttrap
13523 13528 * and pid providers. We need to grant wider destructive
13524 13529 * privileges in the event that the user has proc_owner and/or
13525 13530 * proc_zone.
13526 13531 */
13527 13532 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
13528 13533 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
13529 13534 state->dts_cred.dcr_action |=
13530 13535 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13531 13536
13532 13537 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
13533 13538 state->dts_cred.dcr_action |=
13534 13539 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13535 13540 }
13536 13541 }
13537 13542
13538 13543 return (state);
13539 13544 }
13540 13545
13541 13546 static int
13542 13547 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
13543 13548 {
13544 13549 dtrace_optval_t *opt = state->dts_options, size;
13545 13550 processorid_t cpu;
13546 13551 int flags = 0, rval, factor, divisor = 1;
13547 13552
13548 13553 ASSERT(MUTEX_HELD(&dtrace_lock));
13549 13554 ASSERT(MUTEX_HELD(&cpu_lock));
13550 13555 ASSERT(which < DTRACEOPT_MAX);
13551 13556 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
13552 13557 (state == dtrace_anon.dta_state &&
13553 13558 state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
13554 13559
13555 13560 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
13556 13561 return (0);
13557 13562
13558 13563 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
13559 13564 cpu = opt[DTRACEOPT_CPU];
13560 13565
13561 13566 if (which == DTRACEOPT_SPECSIZE)
13562 13567 flags |= DTRACEBUF_NOSWITCH;
13563 13568
13564 13569 if (which == DTRACEOPT_BUFSIZE) {
13565 13570 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
13566 13571 flags |= DTRACEBUF_RING;
13567 13572
13568 13573 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
13569 13574 flags |= DTRACEBUF_FILL;
13570 13575
13571 13576 if (state != dtrace_anon.dta_state ||
13572 13577 state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
13573 13578 flags |= DTRACEBUF_INACTIVE;
13574 13579 }
13575 13580
13576 13581 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) {
13577 13582 /*
13578 13583 * The size must be 8-byte aligned. If the size is not 8-byte
13579 13584 * aligned, drop it down by the difference.
13580 13585 */
13581 13586 if (size & (sizeof (uint64_t) - 1))
13582 13587 size -= size & (sizeof (uint64_t) - 1);
13583 13588
13584 13589 if (size < state->dts_reserve) {
13585 13590 /*
13586 13591 * Buffers always must be large enough to accommodate
13587 13592 * their prereserved space. We return E2BIG instead
13588 13593 * of ENOMEM in this case to allow for user-level
13589 13594 * software to differentiate the cases.
13590 13595 */
13591 13596 return (E2BIG);
13592 13597 }
13593 13598
13594 13599 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor);
13595 13600
13596 13601 if (rval != ENOMEM) {
13597 13602 opt[which] = size;
13598 13603 return (rval);
13599 13604 }
13600 13605
13601 13606 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13602 13607 return (rval);
13603 13608
13604 13609 for (divisor = 2; divisor < factor; divisor <<= 1)
13605 13610 continue;
13606 13611 }
13607 13612
13608 13613 return (ENOMEM);
13609 13614 }
13610 13615
13611 13616 static int
13612 13617 dtrace_state_buffers(dtrace_state_t *state)
13613 13618 {
13614 13619 dtrace_speculation_t *spec = state->dts_speculations;
13615 13620 int rval, i;
13616 13621
13617 13622 if ((rval = dtrace_state_buffer(state, state->dts_buffer,
13618 13623 DTRACEOPT_BUFSIZE)) != 0)
13619 13624 return (rval);
13620 13625
13621 13626 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
13622 13627 DTRACEOPT_AGGSIZE)) != 0)
13623 13628 return (rval);
13624 13629
13625 13630 for (i = 0; i < state->dts_nspeculations; i++) {
13626 13631 if ((rval = dtrace_state_buffer(state,
13627 13632 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
13628 13633 return (rval);
13629 13634 }
13630 13635
13631 13636 return (0);
13632 13637 }
13633 13638
13634 13639 static void
13635 13640 dtrace_state_prereserve(dtrace_state_t *state)
13636 13641 {
13637 13642 dtrace_ecb_t *ecb;
13638 13643 dtrace_probe_t *probe;
13639 13644
13640 13645 state->dts_reserve = 0;
13641 13646
13642 13647 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
13643 13648 return;
13644 13649
13645 13650 /*
13646 13651 * If our buffer policy is a "fill" buffer policy, we need to set the
13647 13652 * prereserved space to be the space required by the END probes.
13648 13653 */
13649 13654 probe = dtrace_probes[dtrace_probeid_end - 1];
13650 13655 ASSERT(probe != NULL);
13651 13656
13652 13657 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
13653 13658 if (ecb->dte_state != state)
13654 13659 continue;
13655 13660
13656 13661 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
13657 13662 }
13658 13663 }
13659 13664
13660 13665 static int
13661 13666 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
13662 13667 {
13663 13668 dtrace_optval_t *opt = state->dts_options, sz, nspec;
13664 13669 dtrace_speculation_t *spec;
13665 13670 dtrace_buffer_t *buf;
13666 13671 cyc_handler_t hdlr;
13667 13672 cyc_time_t when;
13668 13673 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
13669 13674 dtrace_icookie_t cookie;
13670 13675
13671 13676 mutex_enter(&cpu_lock);
13672 13677 mutex_enter(&dtrace_lock);
13673 13678
13674 13679 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
13675 13680 rval = EBUSY;
13676 13681 goto out;
13677 13682 }
13678 13683
13679 13684 /*
13680 13685 * Before we can perform any checks, we must prime all of the
13681 13686 * retained enablings that correspond to this state.
13682 13687 */
13683 13688 dtrace_enabling_prime(state);
13684 13689
13685 13690 if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
13686 13691 rval = EACCES;
13687 13692 goto out;
13688 13693 }
13689 13694
13690 13695 dtrace_state_prereserve(state);
13691 13696
13692 13697 /*
13693 13698 * Now we want to do is try to allocate our speculations.
13694 13699 * We do not automatically resize the number of speculations; if
13695 13700 * this fails, we will fail the operation.
13696 13701 */
13697 13702 nspec = opt[DTRACEOPT_NSPEC];
13698 13703 ASSERT(nspec != DTRACEOPT_UNSET);
13699 13704
13700 13705 if (nspec > INT_MAX) {
13701 13706 rval = ENOMEM;
13702 13707 goto out;
13703 13708 }
13704 13709
13705 13710 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t),
13706 13711 KM_NOSLEEP | KM_NORMALPRI);
13707 13712
13708 13713 if (spec == NULL) {
13709 13714 rval = ENOMEM;
13710 13715 goto out;
13711 13716 }
13712 13717
13713 13718 state->dts_speculations = spec;
13714 13719 state->dts_nspeculations = (int)nspec;
13715 13720
13716 13721 for (i = 0; i < nspec; i++) {
13717 13722 if ((buf = kmem_zalloc(bufsize,
13718 13723 KM_NOSLEEP | KM_NORMALPRI)) == NULL) {
13719 13724 rval = ENOMEM;
13720 13725 goto err;
13721 13726 }
13722 13727
13723 13728 spec[i].dtsp_buffer = buf;
13724 13729 }
13725 13730
13726 13731 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
13727 13732 if (dtrace_anon.dta_state == NULL) {
13728 13733 rval = ENOENT;
13729 13734 goto out;
13730 13735 }
13731 13736
13732 13737 if (state->dts_necbs != 0) {
13733 13738 rval = EALREADY;
13734 13739 goto out;
13735 13740 }
13736 13741
13737 13742 state->dts_anon = dtrace_anon_grab();
13738 13743 ASSERT(state->dts_anon != NULL);
13739 13744 state = state->dts_anon;
13740 13745
13741 13746 /*
13742 13747 * We want "grabanon" to be set in the grabbed state, so we'll
13743 13748 * copy that option value from the grabbing state into the
13744 13749 * grabbed state.
13745 13750 */
13746 13751 state->dts_options[DTRACEOPT_GRABANON] =
13747 13752 opt[DTRACEOPT_GRABANON];
13748 13753
13749 13754 *cpu = dtrace_anon.dta_beganon;
13750 13755
13751 13756 /*
13752 13757 * If the anonymous state is active (as it almost certainly
13753 13758 * is if the anonymous enabling ultimately matched anything),
13754 13759 * we don't allow any further option processing -- but we
13755 13760 * don't return failure.
13756 13761 */
13757 13762 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13758 13763 goto out;
13759 13764 }
13760 13765
13761 13766 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
13762 13767 opt[DTRACEOPT_AGGSIZE] != 0) {
13763 13768 if (state->dts_aggregations == NULL) {
13764 13769 /*
13765 13770 * We're not going to create an aggregation buffer
13766 13771 * because we don't have any ECBs that contain
13767 13772 * aggregations -- set this option to 0.
13768 13773 */
13769 13774 opt[DTRACEOPT_AGGSIZE] = 0;
13770 13775 } else {
13771 13776 /*
13772 13777 * If we have an aggregation buffer, we must also have
13773 13778 * a buffer to use as scratch.
13774 13779 */
13775 13780 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
13776 13781 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
13777 13782 opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
13778 13783 }
13779 13784 }
13780 13785 }
13781 13786
13782 13787 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
13783 13788 opt[DTRACEOPT_SPECSIZE] != 0) {
13784 13789 if (!state->dts_speculates) {
13785 13790 /*
13786 13791 * We're not going to create speculation buffers
13787 13792 * because we don't have any ECBs that actually
13788 13793 * speculate -- set the speculation size to 0.
13789 13794 */
13790 13795 opt[DTRACEOPT_SPECSIZE] = 0;
13791 13796 }
13792 13797 }
13793 13798
13794 13799 /*
13795 13800 * The bare minimum size for any buffer that we're actually going to
13796 13801 * do anything to is sizeof (uint64_t).
13797 13802 */
13798 13803 sz = sizeof (uint64_t);
13799 13804
13800 13805 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
13801 13806 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
13802 13807 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
13803 13808 /*
13804 13809 * A buffer size has been explicitly set to 0 (or to a size
13805 13810 * that will be adjusted to 0) and we need the space -- we
13806 13811 * need to return failure. We return ENOSPC to differentiate
13807 13812 * it from failing to allocate a buffer due to failure to meet
13808 13813 * the reserve (for which we return E2BIG).
13809 13814 */
13810 13815 rval = ENOSPC;
13811 13816 goto out;
13812 13817 }
13813 13818
13814 13819 if ((rval = dtrace_state_buffers(state)) != 0)
13815 13820 goto err;
13816 13821
13817 13822 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
13818 13823 sz = dtrace_dstate_defsize;
13819 13824
13820 13825 do {
13821 13826 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
13822 13827
13823 13828 if (rval == 0)
13824 13829 break;
13825 13830
13826 13831 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13827 13832 goto err;
13828 13833 } while (sz >>= 1);
13829 13834
13830 13835 opt[DTRACEOPT_DYNVARSIZE] = sz;
13831 13836
13832 13837 if (rval != 0)
13833 13838 goto err;
13834 13839
13835 13840 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
13836 13841 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
13837 13842
13838 13843 if (opt[DTRACEOPT_CLEANRATE] == 0)
13839 13844 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13840 13845
13841 13846 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
13842 13847 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
13843 13848
13844 13849 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
13845 13850 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13846 13851
13847 13852 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
13848 13853 hdlr.cyh_arg = state;
13849 13854 hdlr.cyh_level = CY_LOW_LEVEL;
13850 13855
13851 13856 when.cyt_when = 0;
13852 13857 when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
13853 13858
13854 13859 state->dts_cleaner = cyclic_add(&hdlr, &when);
13855 13860
13856 13861 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
13857 13862 hdlr.cyh_arg = state;
13858 13863 hdlr.cyh_level = CY_LOW_LEVEL;
13859 13864
13860 13865 when.cyt_when = 0;
13861 13866 when.cyt_interval = dtrace_deadman_interval;
13862 13867
13863 13868 state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
13864 13869 state->dts_deadman = cyclic_add(&hdlr, &when);
13865 13870
13866 13871 state->dts_activity = DTRACE_ACTIVITY_WARMUP;
13867 13872
13868 13873 if (state->dts_getf != 0 &&
13869 13874 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
13870 13875 /*
13871 13876 * We don't have kernel privs but we have at least one call
13872 13877 * to getf(); we need to bump our zone's count, and (if
13873 13878 * this is the first enabling to have an unprivileged call
13874 13879 * to getf()) we need to hook into closef().
13875 13880 */
13876 13881 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++;
13877 13882
13878 13883 if (dtrace_getf++ == 0) {
13879 13884 ASSERT(dtrace_closef == NULL);
13880 13885 dtrace_closef = dtrace_getf_barrier;
13881 13886 }
13882 13887 }
13883 13888
13884 13889 /*
13885 13890 * Now it's time to actually fire the BEGIN probe. We need to disable
13886 13891 * interrupts here both to record the CPU on which we fired the BEGIN
13887 13892 * probe (the data from this CPU will be processed first at user
13888 13893 * level) and to manually activate the buffer for this CPU.
13889 13894 */
13890 13895 cookie = dtrace_interrupt_disable();
13891 13896 *cpu = CPU->cpu_id;
13892 13897 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
13893 13898 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
13894 13899
13895 13900 dtrace_probe(dtrace_probeid_begin,
13896 13901 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13897 13902 dtrace_interrupt_enable(cookie);
13898 13903 /*
13899 13904 * We may have had an exit action from a BEGIN probe; only change our
13900 13905 * state to ACTIVE if we're still in WARMUP.
13901 13906 */
13902 13907 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
13903 13908 state->dts_activity == DTRACE_ACTIVITY_DRAINING);
13904 13909
13905 13910 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
13906 13911 state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
13907 13912
13908 13913 /*
13909 13914 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
13910 13915 * want each CPU to transition its principal buffer out of the
13911 13916 * INACTIVE state. Doing this assures that no CPU will suddenly begin
13912 13917 * processing an ECB halfway down a probe's ECB chain; all CPUs will
13913 13918 * atomically transition from processing none of a state's ECBs to
13914 13919 * processing all of them.
13915 13920 */
13916 13921 dtrace_xcall(DTRACE_CPUALL,
13917 13922 (dtrace_xcall_t)dtrace_buffer_activate, state);
13918 13923 goto out;
13919 13924
13920 13925 err:
13921 13926 dtrace_buffer_free(state->dts_buffer);
13922 13927 dtrace_buffer_free(state->dts_aggbuffer);
13923 13928
13924 13929 if ((nspec = state->dts_nspeculations) == 0) {
13925 13930 ASSERT(state->dts_speculations == NULL);
13926 13931 goto out;
13927 13932 }
13928 13933
13929 13934 spec = state->dts_speculations;
13930 13935 ASSERT(spec != NULL);
13931 13936
13932 13937 for (i = 0; i < state->dts_nspeculations; i++) {
13933 13938 if ((buf = spec[i].dtsp_buffer) == NULL)
13934 13939 break;
13935 13940
13936 13941 dtrace_buffer_free(buf);
13937 13942 kmem_free(buf, bufsize);
13938 13943 }
13939 13944
13940 13945 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
13941 13946 state->dts_nspeculations = 0;
13942 13947 state->dts_speculations = NULL;
13943 13948
13944 13949 out:
13945 13950 mutex_exit(&dtrace_lock);
13946 13951 mutex_exit(&cpu_lock);
13947 13952
13948 13953 return (rval);
13949 13954 }
13950 13955
13951 13956 static int
13952 13957 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
13953 13958 {
13954 13959 dtrace_icookie_t cookie;
13955 13960
13956 13961 ASSERT(MUTEX_HELD(&dtrace_lock));
13957 13962
13958 13963 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
13959 13964 state->dts_activity != DTRACE_ACTIVITY_DRAINING)
13960 13965 return (EINVAL);
13961 13966
13962 13967 /*
13963 13968 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
13964 13969 * to be sure that every CPU has seen it. See below for the details
13965 13970 * on why this is done.
13966 13971 */
13967 13972 state->dts_activity = DTRACE_ACTIVITY_DRAINING;
13968 13973 dtrace_sync();
13969 13974
13970 13975 /*
13971 13976 * By this point, it is impossible for any CPU to be still processing
13972 13977 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
13973 13978 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
13974 13979 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
13975 13980 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
13976 13981 * iff we're in the END probe.
13977 13982 */
13978 13983 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
13979 13984 dtrace_sync();
13980 13985 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
13981 13986
13982 13987 /*
13983 13988 * Finally, we can release the reserve and call the END probe. We
13984 13989 * disable interrupts across calling the END probe to allow us to
13985 13990 * return the CPU on which we actually called the END probe. This
13986 13991 * allows user-land to be sure that this CPU's principal buffer is
13987 13992 * processed last.
13988 13993 */
13989 13994 state->dts_reserve = 0;
13990 13995
13991 13996 cookie = dtrace_interrupt_disable();
13992 13997 *cpu = CPU->cpu_id;
13993 13998 dtrace_probe(dtrace_probeid_end,
13994 13999 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13995 14000 dtrace_interrupt_enable(cookie);
13996 14001
13997 14002 state->dts_activity = DTRACE_ACTIVITY_STOPPED;
13998 14003 dtrace_sync();
13999 14004
14000 14005 if (state->dts_getf != 0 &&
14001 14006 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
14002 14007 /*
14003 14008 * We don't have kernel privs but we have at least one call
14004 14009 * to getf(); we need to lower our zone's count, and (if
14005 14010 * this is the last enabling to have an unprivileged call
14006 14011 * to getf()) we need to clear the closef() hook.
14007 14012 */
14008 14013 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0);
14009 14014 ASSERT(dtrace_closef == dtrace_getf_barrier);
14010 14015 ASSERT(dtrace_getf > 0);
14011 14016
14012 14017 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--;
14013 14018
14014 14019 if (--dtrace_getf == 0)
14015 14020 dtrace_closef = NULL;
14016 14021 }
14017 14022
14018 14023 return (0);
14019 14024 }
14020 14025
14021 14026 static int
14022 14027 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
14023 14028 dtrace_optval_t val)
14024 14029 {
14025 14030 ASSERT(MUTEX_HELD(&dtrace_lock));
14026 14031
14027 14032 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
14028 14033 return (EBUSY);
14029 14034
14030 14035 if (option >= DTRACEOPT_MAX)
14031 14036 return (EINVAL);
14032 14037
14033 14038 if (option != DTRACEOPT_CPU && val < 0)
14034 14039 return (EINVAL);
14035 14040
14036 14041 switch (option) {
14037 14042 case DTRACEOPT_DESTRUCTIVE:
14038 14043 if (dtrace_destructive_disallow)
14039 14044 return (EACCES);
14040 14045
14041 14046 state->dts_cred.dcr_destructive = 1;
14042 14047 break;
14043 14048
14044 14049 case DTRACEOPT_BUFSIZE:
14045 14050 case DTRACEOPT_DYNVARSIZE:
14046 14051 case DTRACEOPT_AGGSIZE:
14047 14052 case DTRACEOPT_SPECSIZE:
14048 14053 case DTRACEOPT_STRSIZE:
14049 14054 if (val < 0)
14050 14055 return (EINVAL);
14051 14056
14052 14057 if (val >= LONG_MAX) {
14053 14058 /*
14054 14059 * If this is an otherwise negative value, set it to
14055 14060 * the highest multiple of 128m less than LONG_MAX.
14056 14061 * Technically, we're adjusting the size without
14057 14062 * regard to the buffer resizing policy, but in fact,
14058 14063 * this has no effect -- if we set the buffer size to
14059 14064 * ~LONG_MAX and the buffer policy is ultimately set to
14060 14065 * be "manual", the buffer allocation is guaranteed to
14061 14066 * fail, if only because the allocation requires two
14062 14067 * buffers. (We set the the size to the highest
14063 14068 * multiple of 128m because it ensures that the size
14064 14069 * will remain a multiple of a megabyte when
14065 14070 * repeatedly halved -- all the way down to 15m.)
14066 14071 */
14067 14072 val = LONG_MAX - (1 << 27) + 1;
14068 14073 }
14069 14074 }
14070 14075
14071 14076 state->dts_options[option] = val;
14072 14077
14073 14078 return (0);
14074 14079 }
14075 14080
14076 14081 static void
14077 14082 dtrace_state_destroy(dtrace_state_t *state)
14078 14083 {
14079 14084 dtrace_ecb_t *ecb;
14080 14085 dtrace_vstate_t *vstate = &state->dts_vstate;
14081 14086 minor_t minor = getminor(state->dts_dev);
14082 14087 int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
14083 14088 dtrace_speculation_t *spec = state->dts_speculations;
14084 14089 int nspec = state->dts_nspeculations;
14085 14090 uint32_t match;
14086 14091
14087 14092 ASSERT(MUTEX_HELD(&dtrace_lock));
14088 14093 ASSERT(MUTEX_HELD(&cpu_lock));
14089 14094
14090 14095 /*
14091 14096 * First, retract any retained enablings for this state.
14092 14097 */
14093 14098 dtrace_enabling_retract(state);
14094 14099 ASSERT(state->dts_nretained == 0);
14095 14100
14096 14101 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
14097 14102 state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
14098 14103 /*
14099 14104 * We have managed to come into dtrace_state_destroy() on a
14100 14105 * hot enabling -- almost certainly because of a disorderly
14101 14106 * shutdown of a consumer. (That is, a consumer that is
14102 14107 * exiting without having called dtrace_stop().) In this case,
14103 14108 * we're going to set our activity to be KILLED, and then
14104 14109 * issue a sync to be sure that everyone is out of probe
14105 14110 * context before we start blowing away ECBs.
14106 14111 */
14107 14112 state->dts_activity = DTRACE_ACTIVITY_KILLED;
14108 14113 dtrace_sync();
14109 14114 }
14110 14115
14111 14116 /*
14112 14117 * Release the credential hold we took in dtrace_state_create().
14113 14118 */
14114 14119 if (state->dts_cred.dcr_cred != NULL)
14115 14120 crfree(state->dts_cred.dcr_cred);
14116 14121
14117 14122 /*
14118 14123 * Now we can safely disable and destroy any enabled probes. Because
14119 14124 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
14120 14125 * (especially if they're all enabled), we take two passes through the
14121 14126 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
14122 14127 * in the second we disable whatever is left over.
14123 14128 */
14124 14129 for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
14125 14130 for (i = 0; i < state->dts_necbs; i++) {
14126 14131 if ((ecb = state->dts_ecbs[i]) == NULL)
14127 14132 continue;
14128 14133
14129 14134 if (match && ecb->dte_probe != NULL) {
14130 14135 dtrace_probe_t *probe = ecb->dte_probe;
14131 14136 dtrace_provider_t *prov = probe->dtpr_provider;
14132 14137
14133 14138 if (!(prov->dtpv_priv.dtpp_flags & match))
14134 14139 continue;
14135 14140 }
14136 14141
14137 14142 dtrace_ecb_disable(ecb);
14138 14143 dtrace_ecb_destroy(ecb);
14139 14144 }
14140 14145
14141 14146 if (!match)
14142 14147 break;
14143 14148 }
14144 14149
14145 14150 /*
14146 14151 * Before we free the buffers, perform one more sync to assure that
14147 14152 * every CPU is out of probe context.
14148 14153 */
14149 14154 dtrace_sync();
14150 14155
14151 14156 dtrace_buffer_free(state->dts_buffer);
14152 14157 dtrace_buffer_free(state->dts_aggbuffer);
14153 14158
14154 14159 for (i = 0; i < nspec; i++)
14155 14160 dtrace_buffer_free(spec[i].dtsp_buffer);
14156 14161
14157 14162 if (state->dts_cleaner != CYCLIC_NONE)
14158 14163 cyclic_remove(state->dts_cleaner);
14159 14164
14160 14165 if (state->dts_deadman != CYCLIC_NONE)
14161 14166 cyclic_remove(state->dts_deadman);
14162 14167
14163 14168 dtrace_dstate_fini(&vstate->dtvs_dynvars);
14164 14169 dtrace_vstate_fini(vstate);
14165 14170 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
14166 14171
14167 14172 if (state->dts_aggregations != NULL) {
14168 14173 #ifdef DEBUG
14169 14174 for (i = 0; i < state->dts_naggregations; i++)
14170 14175 ASSERT(state->dts_aggregations[i] == NULL);
14171 14176 #endif
14172 14177 ASSERT(state->dts_naggregations > 0);
14173 14178 kmem_free(state->dts_aggregations,
14174 14179 state->dts_naggregations * sizeof (dtrace_aggregation_t *));
14175 14180 }
14176 14181
14177 14182 kmem_free(state->dts_buffer, bufsize);
14178 14183 kmem_free(state->dts_aggbuffer, bufsize);
14179 14184
14180 14185 for (i = 0; i < nspec; i++)
14181 14186 kmem_free(spec[i].dtsp_buffer, bufsize);
14182 14187
14183 14188 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
14184 14189
14185 14190 dtrace_format_destroy(state);
14186 14191
14187 14192 vmem_destroy(state->dts_aggid_arena);
14188 14193 ddi_soft_state_free(dtrace_softstate, minor);
14189 14194 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
14190 14195 }
14191 14196
14192 14197 /*
14193 14198 * DTrace Anonymous Enabling Functions
14194 14199 */
14195 14200 static dtrace_state_t *
14196 14201 dtrace_anon_grab(void)
14197 14202 {
14198 14203 dtrace_state_t *state;
14199 14204
14200 14205 ASSERT(MUTEX_HELD(&dtrace_lock));
14201 14206
14202 14207 if ((state = dtrace_anon.dta_state) == NULL) {
14203 14208 ASSERT(dtrace_anon.dta_enabling == NULL);
14204 14209 return (NULL);
14205 14210 }
14206 14211
14207 14212 ASSERT(dtrace_anon.dta_enabling != NULL);
14208 14213 ASSERT(dtrace_retained != NULL);
14209 14214
14210 14215 dtrace_enabling_destroy(dtrace_anon.dta_enabling);
14211 14216 dtrace_anon.dta_enabling = NULL;
14212 14217 dtrace_anon.dta_state = NULL;
14213 14218
14214 14219 return (state);
14215 14220 }
14216 14221
14217 14222 static void
14218 14223 dtrace_anon_property(void)
14219 14224 {
14220 14225 int i, rv;
14221 14226 dtrace_state_t *state;
14222 14227 dof_hdr_t *dof;
14223 14228 char c[32]; /* enough for "dof-data-" + digits */
14224 14229
14225 14230 ASSERT(MUTEX_HELD(&dtrace_lock));
14226 14231 ASSERT(MUTEX_HELD(&cpu_lock));
14227 14232
14228 14233 for (i = 0; ; i++) {
14229 14234 (void) snprintf(c, sizeof (c), "dof-data-%d", i);
14230 14235
14231 14236 dtrace_err_verbose = 1;
14232 14237
14233 14238 if ((dof = dtrace_dof_property(c)) == NULL) {
14234 14239 dtrace_err_verbose = 0;
14235 14240 break;
14236 14241 }
14237 14242
14238 14243 /*
14239 14244 * We want to create anonymous state, so we need to transition
14240 14245 * the kernel debugger to indicate that DTrace is active. If
14241 14246 * this fails (e.g. because the debugger has modified text in
14242 14247 * some way), we won't continue with the processing.
14243 14248 */
14244 14249 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
14245 14250 cmn_err(CE_NOTE, "kernel debugger active; anonymous "
14246 14251 "enabling ignored.");
14247 14252 dtrace_dof_destroy(dof);
14248 14253 break;
14249 14254 }
14250 14255
14251 14256 /*
14252 14257 * If we haven't allocated an anonymous state, we'll do so now.
14253 14258 */
14254 14259 if ((state = dtrace_anon.dta_state) == NULL) {
14255 14260 state = dtrace_state_create(NULL, NULL);
14256 14261 dtrace_anon.dta_state = state;
14257 14262
14258 14263 if (state == NULL) {
14259 14264 /*
14260 14265 * This basically shouldn't happen: the only
14261 14266 * failure mode from dtrace_state_create() is a
14262 14267 * failure of ddi_soft_state_zalloc() that
14263 14268 * itself should never happen. Still, the
14264 14269 * interface allows for a failure mode, and
14265 14270 * we want to fail as gracefully as possible:
14266 14271 * we'll emit an error message and cease
14267 14272 * processing anonymous state in this case.
14268 14273 */
14269 14274 cmn_err(CE_WARN, "failed to create "
14270 14275 "anonymous state");
14271 14276 dtrace_dof_destroy(dof);
14272 14277 break;
14273 14278 }
14274 14279 }
14275 14280
14276 14281 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
14277 14282 &dtrace_anon.dta_enabling, 0, B_TRUE);
14278 14283
14279 14284 if (rv == 0)
14280 14285 rv = dtrace_dof_options(dof, state);
14281 14286
14282 14287 dtrace_err_verbose = 0;
14283 14288 dtrace_dof_destroy(dof);
14284 14289
14285 14290 if (rv != 0) {
14286 14291 /*
14287 14292 * This is malformed DOF; chuck any anonymous state
14288 14293 * that we created.
14289 14294 */
14290 14295 ASSERT(dtrace_anon.dta_enabling == NULL);
14291 14296 dtrace_state_destroy(state);
14292 14297 dtrace_anon.dta_state = NULL;
14293 14298 break;
14294 14299 }
14295 14300
14296 14301 ASSERT(dtrace_anon.dta_enabling != NULL);
14297 14302 }
14298 14303
14299 14304 if (dtrace_anon.dta_enabling != NULL) {
14300 14305 int rval;
14301 14306
14302 14307 /*
14303 14308 * dtrace_enabling_retain() can only fail because we are
14304 14309 * trying to retain more enablings than are allowed -- but
14305 14310 * we only have one anonymous enabling, and we are guaranteed
14306 14311 * to be allowed at least one retained enabling; we assert
14307 14312 * that dtrace_enabling_retain() returns success.
14308 14313 */
14309 14314 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
14310 14315 ASSERT(rval == 0);
14311 14316
14312 14317 dtrace_enabling_dump(dtrace_anon.dta_enabling);
14313 14318 }
↓ open down ↓ |
14021 lines elided |
↑ open up ↑ |
14314 14319 }
14315 14320
14316 14321 /*
14317 14322 * DTrace Helper Functions
14318 14323 */
14319 14324 static void
14320 14325 dtrace_helper_trace(dtrace_helper_action_t *helper,
14321 14326 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
14322 14327 {
14323 14328 uint32_t size, next, nnext, i;
14324 - dtrace_helptrace_t *ent;
14329 + dtrace_helptrace_t *ent, *buffer;
14325 14330 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
14326 14331
14327 - if (!dtrace_helptrace_enabled)
14332 + if ((buffer = dtrace_helptrace_buffer) == NULL)
14328 14333 return;
14329 14334
14330 14335 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
14331 14336
14332 14337 /*
14333 14338 * What would a tracing framework be without its own tracing
14334 14339 * framework? (Well, a hell of a lot simpler, for starters...)
14335 14340 */
14336 14341 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
14337 14342 sizeof (uint64_t) - sizeof (uint64_t);
14338 14343
14339 14344 /*
14340 14345 * Iterate until we can allocate a slot in the trace buffer.
14341 14346 */
14342 14347 do {
14343 14348 next = dtrace_helptrace_next;
14344 14349
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
14345 14350 if (next + size < dtrace_helptrace_bufsize) {
14346 14351 nnext = next + size;
14347 14352 } else {
14348 14353 nnext = size;
14349 14354 }
14350 14355 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
14351 14356
14352 14357 /*
14353 14358 * We have our slot; fill it in.
14354 14359 */
14355 - if (nnext == size)
14360 + if (nnext == size) {
14361 + dtrace_helptrace_wrapped++;
14356 14362 next = 0;
14363 + }
14357 14364
14358 - ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next];
14365 + ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next);
14359 14366 ent->dtht_helper = helper;
14360 14367 ent->dtht_where = where;
14361 14368 ent->dtht_nlocals = vstate->dtvs_nlocals;
14362 14369
14363 14370 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
14364 14371 mstate->dtms_fltoffs : -1;
14365 14372 ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
14366 14373 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
14367 14374
14368 14375 for (i = 0; i < vstate->dtvs_nlocals; i++) {
14369 14376 dtrace_statvar_t *svar;
14370 14377
14371 14378 if ((svar = vstate->dtvs_locals[i]) == NULL)
14372 14379 continue;
14373 14380
14374 14381 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
14375 14382 ent->dtht_locals[i] =
14376 14383 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id];
14377 14384 }
14378 14385 }
14379 14386
14380 14387 static uint64_t
14381 14388 dtrace_helper(int which, dtrace_mstate_t *mstate,
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14382 14389 dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
14383 14390 {
14384 14391 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
14385 14392 uint64_t sarg0 = mstate->dtms_arg[0];
14386 14393 uint64_t sarg1 = mstate->dtms_arg[1];
14387 14394 uint64_t rval;
14388 14395 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
14389 14396 dtrace_helper_action_t *helper;
14390 14397 dtrace_vstate_t *vstate;
14391 14398 dtrace_difo_t *pred;
14392 - int i, trace = dtrace_helptrace_enabled;
14399 + int i, trace = dtrace_helptrace_buffer != NULL;
14393 14400
14394 14401 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
14395 14402
14396 14403 if (helpers == NULL)
14397 14404 return (0);
14398 14405
14399 14406 if ((helper = helpers->dthps_actions[which]) == NULL)
14400 14407 return (0);
14401 14408
14402 14409 vstate = &helpers->dthps_vstate;
14403 14410 mstate->dtms_arg[0] = arg0;
14404 14411 mstate->dtms_arg[1] = arg1;
14405 14412
14406 14413 /*
14407 14414 * Now iterate over each helper. If its predicate evaluates to 'true',
14408 14415 * we'll call the corresponding actions. Note that the below calls
14409 14416 * to dtrace_dif_emulate() may set faults in machine state. This is
14410 14417 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
14411 14418 * the stored DIF offset with its own (which is the desired behavior).
14412 14419 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
14413 14420 * from machine state; this is okay, too.
14414 14421 */
14415 14422 for (; helper != NULL; helper = helper->dtha_next) {
14416 14423 if ((pred = helper->dtha_predicate) != NULL) {
14417 14424 if (trace)
14418 14425 dtrace_helper_trace(helper, mstate, vstate, 0);
14419 14426
14420 14427 if (!dtrace_dif_emulate(pred, mstate, vstate, state))
14421 14428 goto next;
14422 14429
14423 14430 if (*flags & CPU_DTRACE_FAULT)
14424 14431 goto err;
14425 14432 }
14426 14433
14427 14434 for (i = 0; i < helper->dtha_nactions; i++) {
14428 14435 if (trace)
14429 14436 dtrace_helper_trace(helper,
14430 14437 mstate, vstate, i + 1);
14431 14438
14432 14439 rval = dtrace_dif_emulate(helper->dtha_actions[i],
14433 14440 mstate, vstate, state);
14434 14441
14435 14442 if (*flags & CPU_DTRACE_FAULT)
14436 14443 goto err;
14437 14444 }
14438 14445
14439 14446 next:
14440 14447 if (trace)
14441 14448 dtrace_helper_trace(helper, mstate, vstate,
14442 14449 DTRACE_HELPTRACE_NEXT);
14443 14450 }
14444 14451
14445 14452 if (trace)
14446 14453 dtrace_helper_trace(helper, mstate, vstate,
14447 14454 DTRACE_HELPTRACE_DONE);
14448 14455
14449 14456 /*
14450 14457 * Restore the arg0 that we saved upon entry.
14451 14458 */
14452 14459 mstate->dtms_arg[0] = sarg0;
14453 14460 mstate->dtms_arg[1] = sarg1;
14454 14461
14455 14462 return (rval);
14456 14463
14457 14464 err:
14458 14465 if (trace)
14459 14466 dtrace_helper_trace(helper, mstate, vstate,
14460 14467 DTRACE_HELPTRACE_ERR);
14461 14468
14462 14469 /*
14463 14470 * Restore the arg0 that we saved upon entry.
14464 14471 */
14465 14472 mstate->dtms_arg[0] = sarg0;
14466 14473 mstate->dtms_arg[1] = sarg1;
14467 14474
14468 14475 return (NULL);
14469 14476 }
14470 14477
14471 14478 static void
14472 14479 dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
14473 14480 dtrace_vstate_t *vstate)
14474 14481 {
14475 14482 int i;
14476 14483
14477 14484 if (helper->dtha_predicate != NULL)
14478 14485 dtrace_difo_release(helper->dtha_predicate, vstate);
14479 14486
14480 14487 for (i = 0; i < helper->dtha_nactions; i++) {
14481 14488 ASSERT(helper->dtha_actions[i] != NULL);
14482 14489 dtrace_difo_release(helper->dtha_actions[i], vstate);
14483 14490 }
14484 14491
14485 14492 kmem_free(helper->dtha_actions,
14486 14493 helper->dtha_nactions * sizeof (dtrace_difo_t *));
14487 14494 kmem_free(helper, sizeof (dtrace_helper_action_t));
14488 14495 }
14489 14496
14490 14497 static int
14491 14498 dtrace_helper_destroygen(int gen)
14492 14499 {
14493 14500 proc_t *p = curproc;
14494 14501 dtrace_helpers_t *help = p->p_dtrace_helpers;
14495 14502 dtrace_vstate_t *vstate;
14496 14503 int i;
14497 14504
14498 14505 ASSERT(MUTEX_HELD(&dtrace_lock));
14499 14506
14500 14507 if (help == NULL || gen > help->dthps_generation)
14501 14508 return (EINVAL);
14502 14509
14503 14510 vstate = &help->dthps_vstate;
14504 14511
14505 14512 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
14506 14513 dtrace_helper_action_t *last = NULL, *h, *next;
14507 14514
14508 14515 for (h = help->dthps_actions[i]; h != NULL; h = next) {
14509 14516 next = h->dtha_next;
14510 14517
14511 14518 if (h->dtha_generation == gen) {
14512 14519 if (last != NULL) {
14513 14520 last->dtha_next = next;
14514 14521 } else {
14515 14522 help->dthps_actions[i] = next;
14516 14523 }
14517 14524
14518 14525 dtrace_helper_action_destroy(h, vstate);
14519 14526 } else {
14520 14527 last = h;
14521 14528 }
14522 14529 }
14523 14530 }
14524 14531
14525 14532 /*
14526 14533 * Interate until we've cleared out all helper providers with the
14527 14534 * given generation number.
14528 14535 */
14529 14536 for (;;) {
14530 14537 dtrace_helper_provider_t *prov;
14531 14538
14532 14539 /*
14533 14540 * Look for a helper provider with the right generation. We
14534 14541 * have to start back at the beginning of the list each time
14535 14542 * because we drop dtrace_lock. It's unlikely that we'll make
14536 14543 * more than two passes.
14537 14544 */
14538 14545 for (i = 0; i < help->dthps_nprovs; i++) {
14539 14546 prov = help->dthps_provs[i];
14540 14547
14541 14548 if (prov->dthp_generation == gen)
14542 14549 break;
14543 14550 }
14544 14551
14545 14552 /*
14546 14553 * If there were no matches, we're done.
14547 14554 */
14548 14555 if (i == help->dthps_nprovs)
14549 14556 break;
14550 14557
14551 14558 /*
14552 14559 * Move the last helper provider into this slot.
14553 14560 */
14554 14561 help->dthps_nprovs--;
14555 14562 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
14556 14563 help->dthps_provs[help->dthps_nprovs] = NULL;
14557 14564
14558 14565 mutex_exit(&dtrace_lock);
14559 14566
14560 14567 /*
14561 14568 * If we have a meta provider, remove this helper provider.
14562 14569 */
14563 14570 mutex_enter(&dtrace_meta_lock);
14564 14571 if (dtrace_meta_pid != NULL) {
14565 14572 ASSERT(dtrace_deferred_pid == NULL);
14566 14573 dtrace_helper_provider_remove(&prov->dthp_prov,
14567 14574 p->p_pid);
14568 14575 }
14569 14576 mutex_exit(&dtrace_meta_lock);
14570 14577
14571 14578 dtrace_helper_provider_destroy(prov);
14572 14579
14573 14580 mutex_enter(&dtrace_lock);
14574 14581 }
14575 14582
14576 14583 return (0);
14577 14584 }
14578 14585
14579 14586 static int
14580 14587 dtrace_helper_validate(dtrace_helper_action_t *helper)
14581 14588 {
14582 14589 int err = 0, i;
14583 14590 dtrace_difo_t *dp;
14584 14591
14585 14592 if ((dp = helper->dtha_predicate) != NULL)
14586 14593 err += dtrace_difo_validate_helper(dp);
14587 14594
14588 14595 for (i = 0; i < helper->dtha_nactions; i++)
14589 14596 err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
14590 14597
14591 14598 return (err == 0);
14592 14599 }
14593 14600
14594 14601 static int
14595 14602 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep)
14596 14603 {
14597 14604 dtrace_helpers_t *help;
14598 14605 dtrace_helper_action_t *helper, *last;
14599 14606 dtrace_actdesc_t *act;
14600 14607 dtrace_vstate_t *vstate;
14601 14608 dtrace_predicate_t *pred;
14602 14609 int count = 0, nactions = 0, i;
14603 14610
14604 14611 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
14605 14612 return (EINVAL);
14606 14613
14607 14614 help = curproc->p_dtrace_helpers;
14608 14615 last = help->dthps_actions[which];
14609 14616 vstate = &help->dthps_vstate;
14610 14617
14611 14618 for (count = 0; last != NULL; last = last->dtha_next) {
14612 14619 count++;
14613 14620 if (last->dtha_next == NULL)
14614 14621 break;
14615 14622 }
14616 14623
14617 14624 /*
14618 14625 * If we already have dtrace_helper_actions_max helper actions for this
14619 14626 * helper action type, we'll refuse to add a new one.
14620 14627 */
14621 14628 if (count >= dtrace_helper_actions_max)
14622 14629 return (ENOSPC);
14623 14630
14624 14631 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
14625 14632 helper->dtha_generation = help->dthps_generation;
14626 14633
14627 14634 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
14628 14635 ASSERT(pred->dtp_difo != NULL);
14629 14636 dtrace_difo_hold(pred->dtp_difo);
14630 14637 helper->dtha_predicate = pred->dtp_difo;
14631 14638 }
14632 14639
14633 14640 for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
14634 14641 if (act->dtad_kind != DTRACEACT_DIFEXPR)
14635 14642 goto err;
14636 14643
14637 14644 if (act->dtad_difo == NULL)
14638 14645 goto err;
14639 14646
14640 14647 nactions++;
14641 14648 }
14642 14649
14643 14650 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
14644 14651 (helper->dtha_nactions = nactions), KM_SLEEP);
14645 14652
14646 14653 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
14647 14654 dtrace_difo_hold(act->dtad_difo);
14648 14655 helper->dtha_actions[i++] = act->dtad_difo;
14649 14656 }
14650 14657
14651 14658 if (!dtrace_helper_validate(helper))
14652 14659 goto err;
14653 14660
14654 14661 if (last == NULL) {
14655 14662 help->dthps_actions[which] = helper;
14656 14663 } else {
14657 14664 last->dtha_next = helper;
14658 14665 }
14659 14666
14660 14667 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
14661 14668 dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
14662 14669 dtrace_helptrace_next = 0;
14663 14670 }
14664 14671
14665 14672 return (0);
14666 14673 err:
14667 14674 dtrace_helper_action_destroy(helper, vstate);
14668 14675 return (EINVAL);
14669 14676 }
14670 14677
14671 14678 static void
14672 14679 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
14673 14680 dof_helper_t *dofhp)
14674 14681 {
14675 14682 ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
14676 14683
14677 14684 mutex_enter(&dtrace_meta_lock);
14678 14685 mutex_enter(&dtrace_lock);
14679 14686
14680 14687 if (!dtrace_attached() || dtrace_meta_pid == NULL) {
14681 14688 /*
14682 14689 * If the dtrace module is loaded but not attached, or if
14683 14690 * there aren't isn't a meta provider registered to deal with
14684 14691 * these provider descriptions, we need to postpone creating
14685 14692 * the actual providers until later.
14686 14693 */
14687 14694
14688 14695 if (help->dthps_next == NULL && help->dthps_prev == NULL &&
14689 14696 dtrace_deferred_pid != help) {
14690 14697 help->dthps_deferred = 1;
14691 14698 help->dthps_pid = p->p_pid;
14692 14699 help->dthps_next = dtrace_deferred_pid;
14693 14700 help->dthps_prev = NULL;
14694 14701 if (dtrace_deferred_pid != NULL)
14695 14702 dtrace_deferred_pid->dthps_prev = help;
14696 14703 dtrace_deferred_pid = help;
14697 14704 }
14698 14705
14699 14706 mutex_exit(&dtrace_lock);
14700 14707
14701 14708 } else if (dofhp != NULL) {
14702 14709 /*
14703 14710 * If the dtrace module is loaded and we have a particular
14704 14711 * helper provider description, pass that off to the
14705 14712 * meta provider.
14706 14713 */
14707 14714
14708 14715 mutex_exit(&dtrace_lock);
14709 14716
14710 14717 dtrace_helper_provide(dofhp, p->p_pid);
14711 14718
14712 14719 } else {
14713 14720 /*
14714 14721 * Otherwise, just pass all the helper provider descriptions
14715 14722 * off to the meta provider.
14716 14723 */
14717 14724
14718 14725 int i;
14719 14726 mutex_exit(&dtrace_lock);
14720 14727
14721 14728 for (i = 0; i < help->dthps_nprovs; i++) {
14722 14729 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
14723 14730 p->p_pid);
14724 14731 }
14725 14732 }
14726 14733
14727 14734 mutex_exit(&dtrace_meta_lock);
14728 14735 }
14729 14736
14730 14737 static int
14731 14738 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen)
14732 14739 {
14733 14740 dtrace_helpers_t *help;
14734 14741 dtrace_helper_provider_t *hprov, **tmp_provs;
14735 14742 uint_t tmp_maxprovs, i;
14736 14743
14737 14744 ASSERT(MUTEX_HELD(&dtrace_lock));
14738 14745
14739 14746 help = curproc->p_dtrace_helpers;
14740 14747 ASSERT(help != NULL);
14741 14748
14742 14749 /*
14743 14750 * If we already have dtrace_helper_providers_max helper providers,
14744 14751 * we're refuse to add a new one.
14745 14752 */
14746 14753 if (help->dthps_nprovs >= dtrace_helper_providers_max)
14747 14754 return (ENOSPC);
14748 14755
14749 14756 /*
14750 14757 * Check to make sure this isn't a duplicate.
14751 14758 */
14752 14759 for (i = 0; i < help->dthps_nprovs; i++) {
14753 14760 if (dofhp->dofhp_dof ==
14754 14761 help->dthps_provs[i]->dthp_prov.dofhp_dof)
14755 14762 return (EALREADY);
14756 14763 }
14757 14764
14758 14765 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
14759 14766 hprov->dthp_prov = *dofhp;
14760 14767 hprov->dthp_ref = 1;
14761 14768 hprov->dthp_generation = gen;
14762 14769
14763 14770 /*
14764 14771 * Allocate a bigger table for helper providers if it's already full.
14765 14772 */
14766 14773 if (help->dthps_maxprovs == help->dthps_nprovs) {
14767 14774 tmp_maxprovs = help->dthps_maxprovs;
14768 14775 tmp_provs = help->dthps_provs;
14769 14776
14770 14777 if (help->dthps_maxprovs == 0)
14771 14778 help->dthps_maxprovs = 2;
14772 14779 else
14773 14780 help->dthps_maxprovs *= 2;
14774 14781 if (help->dthps_maxprovs > dtrace_helper_providers_max)
14775 14782 help->dthps_maxprovs = dtrace_helper_providers_max;
14776 14783
14777 14784 ASSERT(tmp_maxprovs < help->dthps_maxprovs);
14778 14785
14779 14786 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
14780 14787 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
14781 14788
14782 14789 if (tmp_provs != NULL) {
14783 14790 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
14784 14791 sizeof (dtrace_helper_provider_t *));
14785 14792 kmem_free(tmp_provs, tmp_maxprovs *
14786 14793 sizeof (dtrace_helper_provider_t *));
14787 14794 }
14788 14795 }
14789 14796
14790 14797 help->dthps_provs[help->dthps_nprovs] = hprov;
14791 14798 help->dthps_nprovs++;
14792 14799
14793 14800 return (0);
14794 14801 }
14795 14802
14796 14803 static void
14797 14804 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
14798 14805 {
14799 14806 mutex_enter(&dtrace_lock);
14800 14807
14801 14808 if (--hprov->dthp_ref == 0) {
14802 14809 dof_hdr_t *dof;
14803 14810 mutex_exit(&dtrace_lock);
14804 14811 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
14805 14812 dtrace_dof_destroy(dof);
14806 14813 kmem_free(hprov, sizeof (dtrace_helper_provider_t));
14807 14814 } else {
14808 14815 mutex_exit(&dtrace_lock);
14809 14816 }
14810 14817 }
14811 14818
14812 14819 static int
14813 14820 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
14814 14821 {
14815 14822 uintptr_t daddr = (uintptr_t)dof;
14816 14823 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
14817 14824 dof_provider_t *provider;
14818 14825 dof_probe_t *probe;
14819 14826 uint8_t *arg;
14820 14827 char *strtab, *typestr;
14821 14828 dof_stridx_t typeidx;
14822 14829 size_t typesz;
14823 14830 uint_t nprobes, j, k;
14824 14831
14825 14832 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
14826 14833
14827 14834 if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
14828 14835 dtrace_dof_error(dof, "misaligned section offset");
14829 14836 return (-1);
14830 14837 }
14831 14838
14832 14839 /*
14833 14840 * The section needs to be large enough to contain the DOF provider
14834 14841 * structure appropriate for the given version.
14835 14842 */
14836 14843 if (sec->dofs_size <
14837 14844 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
14838 14845 offsetof(dof_provider_t, dofpv_prenoffs) :
14839 14846 sizeof (dof_provider_t))) {
14840 14847 dtrace_dof_error(dof, "provider section too small");
14841 14848 return (-1);
14842 14849 }
14843 14850
14844 14851 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
14845 14852 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
14846 14853 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
14847 14854 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
14848 14855 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
14849 14856
14850 14857 if (str_sec == NULL || prb_sec == NULL ||
14851 14858 arg_sec == NULL || off_sec == NULL)
14852 14859 return (-1);
14853 14860
14854 14861 enoff_sec = NULL;
14855 14862
14856 14863 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
14857 14864 provider->dofpv_prenoffs != DOF_SECT_NONE &&
14858 14865 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
14859 14866 provider->dofpv_prenoffs)) == NULL)
14860 14867 return (-1);
14861 14868
14862 14869 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
14863 14870
14864 14871 if (provider->dofpv_name >= str_sec->dofs_size ||
14865 14872 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
14866 14873 dtrace_dof_error(dof, "invalid provider name");
14867 14874 return (-1);
14868 14875 }
14869 14876
14870 14877 if (prb_sec->dofs_entsize == 0 ||
14871 14878 prb_sec->dofs_entsize > prb_sec->dofs_size) {
14872 14879 dtrace_dof_error(dof, "invalid entry size");
14873 14880 return (-1);
14874 14881 }
14875 14882
14876 14883 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
14877 14884 dtrace_dof_error(dof, "misaligned entry size");
14878 14885 return (-1);
14879 14886 }
14880 14887
14881 14888 if (off_sec->dofs_entsize != sizeof (uint32_t)) {
14882 14889 dtrace_dof_error(dof, "invalid entry size");
14883 14890 return (-1);
14884 14891 }
14885 14892
14886 14893 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
14887 14894 dtrace_dof_error(dof, "misaligned section offset");
14888 14895 return (-1);
14889 14896 }
14890 14897
14891 14898 if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
14892 14899 dtrace_dof_error(dof, "invalid entry size");
14893 14900 return (-1);
14894 14901 }
14895 14902
14896 14903 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
14897 14904
14898 14905 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
14899 14906
14900 14907 /*
14901 14908 * Take a pass through the probes to check for errors.
14902 14909 */
14903 14910 for (j = 0; j < nprobes; j++) {
14904 14911 probe = (dof_probe_t *)(uintptr_t)(daddr +
14905 14912 prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
14906 14913
14907 14914 if (probe->dofpr_func >= str_sec->dofs_size) {
14908 14915 dtrace_dof_error(dof, "invalid function name");
14909 14916 return (-1);
14910 14917 }
14911 14918
14912 14919 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
14913 14920 dtrace_dof_error(dof, "function name too long");
14914 14921 return (-1);
14915 14922 }
14916 14923
14917 14924 if (probe->dofpr_name >= str_sec->dofs_size ||
14918 14925 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
14919 14926 dtrace_dof_error(dof, "invalid probe name");
14920 14927 return (-1);
14921 14928 }
14922 14929
14923 14930 /*
14924 14931 * The offset count must not wrap the index, and the offsets
14925 14932 * must also not overflow the section's data.
14926 14933 */
14927 14934 if (probe->dofpr_offidx + probe->dofpr_noffs <
14928 14935 probe->dofpr_offidx ||
14929 14936 (probe->dofpr_offidx + probe->dofpr_noffs) *
14930 14937 off_sec->dofs_entsize > off_sec->dofs_size) {
14931 14938 dtrace_dof_error(dof, "invalid probe offset");
14932 14939 return (-1);
14933 14940 }
14934 14941
14935 14942 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
14936 14943 /*
14937 14944 * If there's no is-enabled offset section, make sure
14938 14945 * there aren't any is-enabled offsets. Otherwise
14939 14946 * perform the same checks as for probe offsets
14940 14947 * (immediately above).
14941 14948 */
14942 14949 if (enoff_sec == NULL) {
14943 14950 if (probe->dofpr_enoffidx != 0 ||
14944 14951 probe->dofpr_nenoffs != 0) {
14945 14952 dtrace_dof_error(dof, "is-enabled "
14946 14953 "offsets with null section");
14947 14954 return (-1);
14948 14955 }
14949 14956 } else if (probe->dofpr_enoffidx +
14950 14957 probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
14951 14958 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
14952 14959 enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
14953 14960 dtrace_dof_error(dof, "invalid is-enabled "
14954 14961 "offset");
14955 14962 return (-1);
14956 14963 }
14957 14964
14958 14965 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
14959 14966 dtrace_dof_error(dof, "zero probe and "
14960 14967 "is-enabled offsets");
14961 14968 return (-1);
14962 14969 }
14963 14970 } else if (probe->dofpr_noffs == 0) {
14964 14971 dtrace_dof_error(dof, "zero probe offsets");
14965 14972 return (-1);
14966 14973 }
14967 14974
14968 14975 if (probe->dofpr_argidx + probe->dofpr_xargc <
14969 14976 probe->dofpr_argidx ||
14970 14977 (probe->dofpr_argidx + probe->dofpr_xargc) *
14971 14978 arg_sec->dofs_entsize > arg_sec->dofs_size) {
14972 14979 dtrace_dof_error(dof, "invalid args");
14973 14980 return (-1);
14974 14981 }
14975 14982
14976 14983 typeidx = probe->dofpr_nargv;
14977 14984 typestr = strtab + probe->dofpr_nargv;
14978 14985 for (k = 0; k < probe->dofpr_nargc; k++) {
14979 14986 if (typeidx >= str_sec->dofs_size) {
14980 14987 dtrace_dof_error(dof, "bad "
14981 14988 "native argument type");
14982 14989 return (-1);
14983 14990 }
14984 14991
14985 14992 typesz = strlen(typestr) + 1;
14986 14993 if (typesz > DTRACE_ARGTYPELEN) {
14987 14994 dtrace_dof_error(dof, "native "
14988 14995 "argument type too long");
14989 14996 return (-1);
14990 14997 }
14991 14998 typeidx += typesz;
14992 14999 typestr += typesz;
14993 15000 }
14994 15001
14995 15002 typeidx = probe->dofpr_xargv;
14996 15003 typestr = strtab + probe->dofpr_xargv;
14997 15004 for (k = 0; k < probe->dofpr_xargc; k++) {
14998 15005 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
14999 15006 dtrace_dof_error(dof, "bad "
15000 15007 "native argument index");
15001 15008 return (-1);
15002 15009 }
15003 15010
15004 15011 if (typeidx >= str_sec->dofs_size) {
15005 15012 dtrace_dof_error(dof, "bad "
15006 15013 "translated argument type");
15007 15014 return (-1);
15008 15015 }
15009 15016
15010 15017 typesz = strlen(typestr) + 1;
15011 15018 if (typesz > DTRACE_ARGTYPELEN) {
15012 15019 dtrace_dof_error(dof, "translated argument "
15013 15020 "type too long");
15014 15021 return (-1);
15015 15022 }
15016 15023
15017 15024 typeidx += typesz;
15018 15025 typestr += typesz;
15019 15026 }
15020 15027 }
15021 15028
15022 15029 return (0);
15023 15030 }
15024 15031
15025 15032 static int
15026 15033 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp)
15027 15034 {
15028 15035 dtrace_helpers_t *help;
15029 15036 dtrace_vstate_t *vstate;
15030 15037 dtrace_enabling_t *enab = NULL;
15031 15038 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
15032 15039 uintptr_t daddr = (uintptr_t)dof;
15033 15040
15034 15041 ASSERT(MUTEX_HELD(&dtrace_lock));
15035 15042
15036 15043 if ((help = curproc->p_dtrace_helpers) == NULL)
15037 15044 help = dtrace_helpers_create(curproc);
15038 15045
15039 15046 vstate = &help->dthps_vstate;
15040 15047
15041 15048 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab,
15042 15049 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) {
15043 15050 dtrace_dof_destroy(dof);
15044 15051 return (rv);
15045 15052 }
15046 15053
15047 15054 /*
15048 15055 * Look for helper providers and validate their descriptions.
15049 15056 */
15050 15057 if (dhp != NULL) {
15051 15058 for (i = 0; i < dof->dofh_secnum; i++) {
15052 15059 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
15053 15060 dof->dofh_secoff + i * dof->dofh_secsize);
15054 15061
15055 15062 if (sec->dofs_type != DOF_SECT_PROVIDER)
15056 15063 continue;
15057 15064
15058 15065 if (dtrace_helper_provider_validate(dof, sec) != 0) {
15059 15066 dtrace_enabling_destroy(enab);
15060 15067 dtrace_dof_destroy(dof);
15061 15068 return (-1);
15062 15069 }
15063 15070
15064 15071 nprovs++;
15065 15072 }
15066 15073 }
15067 15074
15068 15075 /*
15069 15076 * Now we need to walk through the ECB descriptions in the enabling.
15070 15077 */
15071 15078 for (i = 0; i < enab->dten_ndesc; i++) {
15072 15079 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
15073 15080 dtrace_probedesc_t *desc = &ep->dted_probe;
15074 15081
15075 15082 if (strcmp(desc->dtpd_provider, "dtrace") != 0)
15076 15083 continue;
15077 15084
15078 15085 if (strcmp(desc->dtpd_mod, "helper") != 0)
15079 15086 continue;
15080 15087
15081 15088 if (strcmp(desc->dtpd_func, "ustack") != 0)
15082 15089 continue;
15083 15090
15084 15091 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
15085 15092 ep)) != 0) {
15086 15093 /*
15087 15094 * Adding this helper action failed -- we are now going
15088 15095 * to rip out the entire generation and return failure.
15089 15096 */
15090 15097 (void) dtrace_helper_destroygen(help->dthps_generation);
15091 15098 dtrace_enabling_destroy(enab);
15092 15099 dtrace_dof_destroy(dof);
15093 15100 return (-1);
15094 15101 }
15095 15102
15096 15103 nhelpers++;
15097 15104 }
15098 15105
15099 15106 if (nhelpers < enab->dten_ndesc)
15100 15107 dtrace_dof_error(dof, "unmatched helpers");
15101 15108
15102 15109 gen = help->dthps_generation++;
15103 15110 dtrace_enabling_destroy(enab);
15104 15111
15105 15112 if (dhp != NULL && nprovs > 0) {
15106 15113 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
15107 15114 if (dtrace_helper_provider_add(dhp, gen) == 0) {
15108 15115 mutex_exit(&dtrace_lock);
15109 15116 dtrace_helper_provider_register(curproc, help, dhp);
15110 15117 mutex_enter(&dtrace_lock);
15111 15118
15112 15119 destroy = 0;
15113 15120 }
15114 15121 }
15115 15122
15116 15123 if (destroy)
15117 15124 dtrace_dof_destroy(dof);
15118 15125
15119 15126 return (gen);
15120 15127 }
15121 15128
15122 15129 static dtrace_helpers_t *
15123 15130 dtrace_helpers_create(proc_t *p)
15124 15131 {
15125 15132 dtrace_helpers_t *help;
15126 15133
15127 15134 ASSERT(MUTEX_HELD(&dtrace_lock));
15128 15135 ASSERT(p->p_dtrace_helpers == NULL);
15129 15136
15130 15137 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
15131 15138 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
15132 15139 DTRACE_NHELPER_ACTIONS, KM_SLEEP);
15133 15140
15134 15141 p->p_dtrace_helpers = help;
15135 15142 dtrace_helpers++;
15136 15143
15137 15144 return (help);
15138 15145 }
15139 15146
15140 15147 static void
15141 15148 dtrace_helpers_destroy(void)
15142 15149 {
15143 15150 dtrace_helpers_t *help;
15144 15151 dtrace_vstate_t *vstate;
15145 15152 proc_t *p = curproc;
15146 15153 int i;
15147 15154
15148 15155 mutex_enter(&dtrace_lock);
15149 15156
15150 15157 ASSERT(p->p_dtrace_helpers != NULL);
15151 15158 ASSERT(dtrace_helpers > 0);
15152 15159
15153 15160 help = p->p_dtrace_helpers;
15154 15161 vstate = &help->dthps_vstate;
15155 15162
15156 15163 /*
15157 15164 * We're now going to lose the help from this process.
15158 15165 */
15159 15166 p->p_dtrace_helpers = NULL;
15160 15167 dtrace_sync();
15161 15168
15162 15169 /*
15163 15170 * Destory the helper actions.
15164 15171 */
15165 15172 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15166 15173 dtrace_helper_action_t *h, *next;
15167 15174
15168 15175 for (h = help->dthps_actions[i]; h != NULL; h = next) {
15169 15176 next = h->dtha_next;
15170 15177 dtrace_helper_action_destroy(h, vstate);
15171 15178 h = next;
15172 15179 }
15173 15180 }
15174 15181
15175 15182 mutex_exit(&dtrace_lock);
15176 15183
15177 15184 /*
15178 15185 * Destroy the helper providers.
15179 15186 */
15180 15187 if (help->dthps_maxprovs > 0) {
15181 15188 mutex_enter(&dtrace_meta_lock);
15182 15189 if (dtrace_meta_pid != NULL) {
15183 15190 ASSERT(dtrace_deferred_pid == NULL);
15184 15191
15185 15192 for (i = 0; i < help->dthps_nprovs; i++) {
15186 15193 dtrace_helper_provider_remove(
15187 15194 &help->dthps_provs[i]->dthp_prov, p->p_pid);
15188 15195 }
15189 15196 } else {
15190 15197 mutex_enter(&dtrace_lock);
15191 15198 ASSERT(help->dthps_deferred == 0 ||
15192 15199 help->dthps_next != NULL ||
15193 15200 help->dthps_prev != NULL ||
15194 15201 help == dtrace_deferred_pid);
15195 15202
15196 15203 /*
15197 15204 * Remove the helper from the deferred list.
15198 15205 */
15199 15206 if (help->dthps_next != NULL)
15200 15207 help->dthps_next->dthps_prev = help->dthps_prev;
15201 15208 if (help->dthps_prev != NULL)
15202 15209 help->dthps_prev->dthps_next = help->dthps_next;
15203 15210 if (dtrace_deferred_pid == help) {
15204 15211 dtrace_deferred_pid = help->dthps_next;
15205 15212 ASSERT(help->dthps_prev == NULL);
15206 15213 }
15207 15214
15208 15215 mutex_exit(&dtrace_lock);
15209 15216 }
15210 15217
15211 15218 mutex_exit(&dtrace_meta_lock);
15212 15219
15213 15220 for (i = 0; i < help->dthps_nprovs; i++) {
15214 15221 dtrace_helper_provider_destroy(help->dthps_provs[i]);
15215 15222 }
15216 15223
15217 15224 kmem_free(help->dthps_provs, help->dthps_maxprovs *
15218 15225 sizeof (dtrace_helper_provider_t *));
15219 15226 }
15220 15227
15221 15228 mutex_enter(&dtrace_lock);
15222 15229
15223 15230 dtrace_vstate_fini(&help->dthps_vstate);
15224 15231 kmem_free(help->dthps_actions,
15225 15232 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
15226 15233 kmem_free(help, sizeof (dtrace_helpers_t));
15227 15234
15228 15235 --dtrace_helpers;
15229 15236 mutex_exit(&dtrace_lock);
15230 15237 }
15231 15238
15232 15239 static void
15233 15240 dtrace_helpers_duplicate(proc_t *from, proc_t *to)
15234 15241 {
15235 15242 dtrace_helpers_t *help, *newhelp;
15236 15243 dtrace_helper_action_t *helper, *new, *last;
15237 15244 dtrace_difo_t *dp;
15238 15245 dtrace_vstate_t *vstate;
15239 15246 int i, j, sz, hasprovs = 0;
15240 15247
15241 15248 mutex_enter(&dtrace_lock);
15242 15249 ASSERT(from->p_dtrace_helpers != NULL);
15243 15250 ASSERT(dtrace_helpers > 0);
15244 15251
15245 15252 help = from->p_dtrace_helpers;
15246 15253 newhelp = dtrace_helpers_create(to);
15247 15254 ASSERT(to->p_dtrace_helpers != NULL);
15248 15255
15249 15256 newhelp->dthps_generation = help->dthps_generation;
15250 15257 vstate = &newhelp->dthps_vstate;
15251 15258
15252 15259 /*
15253 15260 * Duplicate the helper actions.
15254 15261 */
15255 15262 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15256 15263 if ((helper = help->dthps_actions[i]) == NULL)
15257 15264 continue;
15258 15265
15259 15266 for (last = NULL; helper != NULL; helper = helper->dtha_next) {
15260 15267 new = kmem_zalloc(sizeof (dtrace_helper_action_t),
15261 15268 KM_SLEEP);
15262 15269 new->dtha_generation = helper->dtha_generation;
15263 15270
15264 15271 if ((dp = helper->dtha_predicate) != NULL) {
15265 15272 dp = dtrace_difo_duplicate(dp, vstate);
15266 15273 new->dtha_predicate = dp;
15267 15274 }
15268 15275
15269 15276 new->dtha_nactions = helper->dtha_nactions;
15270 15277 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
15271 15278 new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
15272 15279
15273 15280 for (j = 0; j < new->dtha_nactions; j++) {
15274 15281 dtrace_difo_t *dp = helper->dtha_actions[j];
15275 15282
15276 15283 ASSERT(dp != NULL);
15277 15284 dp = dtrace_difo_duplicate(dp, vstate);
15278 15285 new->dtha_actions[j] = dp;
15279 15286 }
15280 15287
15281 15288 if (last != NULL) {
15282 15289 last->dtha_next = new;
15283 15290 } else {
15284 15291 newhelp->dthps_actions[i] = new;
15285 15292 }
15286 15293
15287 15294 last = new;
15288 15295 }
15289 15296 }
15290 15297
15291 15298 /*
15292 15299 * Duplicate the helper providers and register them with the
15293 15300 * DTrace framework.
15294 15301 */
15295 15302 if (help->dthps_nprovs > 0) {
15296 15303 newhelp->dthps_nprovs = help->dthps_nprovs;
15297 15304 newhelp->dthps_maxprovs = help->dthps_nprovs;
15298 15305 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
15299 15306 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
15300 15307 for (i = 0; i < newhelp->dthps_nprovs; i++) {
15301 15308 newhelp->dthps_provs[i] = help->dthps_provs[i];
15302 15309 newhelp->dthps_provs[i]->dthp_ref++;
15303 15310 }
15304 15311
15305 15312 hasprovs = 1;
15306 15313 }
15307 15314
15308 15315 mutex_exit(&dtrace_lock);
15309 15316
15310 15317 if (hasprovs)
15311 15318 dtrace_helper_provider_register(to, newhelp, NULL);
15312 15319 }
15313 15320
15314 15321 /*
15315 15322 * DTrace Hook Functions
15316 15323 */
15317 15324 static void
15318 15325 dtrace_module_loaded(struct modctl *ctl)
15319 15326 {
15320 15327 dtrace_provider_t *prv;
15321 15328
15322 15329 mutex_enter(&dtrace_provider_lock);
15323 15330 mutex_enter(&mod_lock);
15324 15331
15325 15332 ASSERT(ctl->mod_busy);
15326 15333
15327 15334 /*
15328 15335 * We're going to call each providers per-module provide operation
15329 15336 * specifying only this module.
15330 15337 */
15331 15338 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
15332 15339 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
15333 15340
15334 15341 mutex_exit(&mod_lock);
15335 15342 mutex_exit(&dtrace_provider_lock);
15336 15343
15337 15344 /*
15338 15345 * If we have any retained enablings, we need to match against them.
15339 15346 * Enabling probes requires that cpu_lock be held, and we cannot hold
15340 15347 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
15341 15348 * module. (In particular, this happens when loading scheduling
15342 15349 * classes.) So if we have any retained enablings, we need to dispatch
15343 15350 * our task queue to do the match for us.
15344 15351 */
15345 15352 mutex_enter(&dtrace_lock);
15346 15353
15347 15354 if (dtrace_retained == NULL) {
15348 15355 mutex_exit(&dtrace_lock);
15349 15356 return;
15350 15357 }
15351 15358
15352 15359 (void) taskq_dispatch(dtrace_taskq,
15353 15360 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
15354 15361
15355 15362 mutex_exit(&dtrace_lock);
15356 15363
15357 15364 /*
15358 15365 * And now, for a little heuristic sleaze: in general, we want to
15359 15366 * match modules as soon as they load. However, we cannot guarantee
15360 15367 * this, because it would lead us to the lock ordering violation
15361 15368 * outlined above. The common case, of course, is that cpu_lock is
15362 15369 * _not_ held -- so we delay here for a clock tick, hoping that that's
15363 15370 * long enough for the task queue to do its work. If it's not, it's
15364 15371 * not a serious problem -- it just means that the module that we
15365 15372 * just loaded may not be immediately instrumentable.
15366 15373 */
15367 15374 delay(1);
15368 15375 }
15369 15376
15370 15377 static void
15371 15378 dtrace_module_unloaded(struct modctl *ctl)
15372 15379 {
15373 15380 dtrace_probe_t template, *probe, *first, *next;
15374 15381 dtrace_provider_t *prov;
15375 15382
15376 15383 template.dtpr_mod = ctl->mod_modname;
15377 15384
15378 15385 mutex_enter(&dtrace_provider_lock);
15379 15386 mutex_enter(&mod_lock);
15380 15387 mutex_enter(&dtrace_lock);
15381 15388
15382 15389 if (dtrace_bymod == NULL) {
15383 15390 /*
15384 15391 * The DTrace module is loaded (obviously) but not attached;
15385 15392 * we don't have any work to do.
15386 15393 */
15387 15394 mutex_exit(&dtrace_provider_lock);
15388 15395 mutex_exit(&mod_lock);
15389 15396 mutex_exit(&dtrace_lock);
15390 15397 return;
15391 15398 }
15392 15399
15393 15400 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
15394 15401 probe != NULL; probe = probe->dtpr_nextmod) {
15395 15402 if (probe->dtpr_ecb != NULL) {
15396 15403 mutex_exit(&dtrace_provider_lock);
15397 15404 mutex_exit(&mod_lock);
15398 15405 mutex_exit(&dtrace_lock);
15399 15406
15400 15407 /*
15401 15408 * This shouldn't _actually_ be possible -- we're
15402 15409 * unloading a module that has an enabled probe in it.
15403 15410 * (It's normally up to the provider to make sure that
15404 15411 * this can't happen.) However, because dtps_enable()
15405 15412 * doesn't have a failure mode, there can be an
15406 15413 * enable/unload race. Upshot: we don't want to
15407 15414 * assert, but we're not going to disable the
15408 15415 * probe, either.
15409 15416 */
15410 15417 if (dtrace_err_verbose) {
15411 15418 cmn_err(CE_WARN, "unloaded module '%s' had "
15412 15419 "enabled probes", ctl->mod_modname);
15413 15420 }
15414 15421
15415 15422 return;
15416 15423 }
15417 15424 }
15418 15425
15419 15426 probe = first;
15420 15427
15421 15428 for (first = NULL; probe != NULL; probe = next) {
15422 15429 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
15423 15430
15424 15431 dtrace_probes[probe->dtpr_id - 1] = NULL;
15425 15432
15426 15433 next = probe->dtpr_nextmod;
15427 15434 dtrace_hash_remove(dtrace_bymod, probe);
15428 15435 dtrace_hash_remove(dtrace_byfunc, probe);
15429 15436 dtrace_hash_remove(dtrace_byname, probe);
15430 15437
15431 15438 if (first == NULL) {
15432 15439 first = probe;
15433 15440 probe->dtpr_nextmod = NULL;
15434 15441 } else {
15435 15442 probe->dtpr_nextmod = first;
15436 15443 first = probe;
15437 15444 }
15438 15445 }
15439 15446
15440 15447 /*
15441 15448 * We've removed all of the module's probes from the hash chains and
15442 15449 * from the probe array. Now issue a dtrace_sync() to be sure that
15443 15450 * everyone has cleared out from any probe array processing.
15444 15451 */
15445 15452 dtrace_sync();
15446 15453
15447 15454 for (probe = first; probe != NULL; probe = first) {
15448 15455 first = probe->dtpr_nextmod;
15449 15456 prov = probe->dtpr_provider;
15450 15457 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
15451 15458 probe->dtpr_arg);
15452 15459 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
15453 15460 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
15454 15461 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
15455 15462 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
15456 15463 kmem_free(probe, sizeof (dtrace_probe_t));
15457 15464 }
15458 15465
15459 15466 mutex_exit(&dtrace_lock);
15460 15467 mutex_exit(&mod_lock);
15461 15468 mutex_exit(&dtrace_provider_lock);
15462 15469 }
15463 15470
15464 15471 void
15465 15472 dtrace_suspend(void)
15466 15473 {
15467 15474 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
15468 15475 }
15469 15476
15470 15477 void
15471 15478 dtrace_resume(void)
15472 15479 {
15473 15480 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
15474 15481 }
15475 15482
15476 15483 static int
15477 15484 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
15478 15485 {
15479 15486 ASSERT(MUTEX_HELD(&cpu_lock));
15480 15487 mutex_enter(&dtrace_lock);
15481 15488
15482 15489 switch (what) {
15483 15490 case CPU_CONFIG: {
15484 15491 dtrace_state_t *state;
15485 15492 dtrace_optval_t *opt, rs, c;
15486 15493
15487 15494 /*
15488 15495 * For now, we only allocate a new buffer for anonymous state.
15489 15496 */
15490 15497 if ((state = dtrace_anon.dta_state) == NULL)
15491 15498 break;
15492 15499
15493 15500 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
15494 15501 break;
15495 15502
15496 15503 opt = state->dts_options;
15497 15504 c = opt[DTRACEOPT_CPU];
15498 15505
15499 15506 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
15500 15507 break;
15501 15508
15502 15509 /*
15503 15510 * Regardless of what the actual policy is, we're going to
15504 15511 * temporarily set our resize policy to be manual. We're
15505 15512 * also going to temporarily set our CPU option to denote
15506 15513 * the newly configured CPU.
15507 15514 */
15508 15515 rs = opt[DTRACEOPT_BUFRESIZE];
15509 15516 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
15510 15517 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
15511 15518
15512 15519 (void) dtrace_state_buffers(state);
15513 15520
15514 15521 opt[DTRACEOPT_BUFRESIZE] = rs;
15515 15522 opt[DTRACEOPT_CPU] = c;
15516 15523
15517 15524 break;
15518 15525 }
15519 15526
15520 15527 case CPU_UNCONFIG:
15521 15528 /*
15522 15529 * We don't free the buffer in the CPU_UNCONFIG case. (The
15523 15530 * buffer will be freed when the consumer exits.)
15524 15531 */
15525 15532 break;
15526 15533
15527 15534 default:
15528 15535 break;
15529 15536 }
15530 15537
15531 15538 mutex_exit(&dtrace_lock);
15532 15539 return (0);
15533 15540 }
15534 15541
15535 15542 static void
15536 15543 dtrace_cpu_setup_initial(processorid_t cpu)
15537 15544 {
15538 15545 (void) dtrace_cpu_setup(CPU_CONFIG, cpu);
15539 15546 }
15540 15547
15541 15548 static void
15542 15549 dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
15543 15550 {
15544 15551 if (dtrace_toxranges >= dtrace_toxranges_max) {
15545 15552 int osize, nsize;
15546 15553 dtrace_toxrange_t *range;
15547 15554
15548 15555 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
15549 15556
15550 15557 if (osize == 0) {
15551 15558 ASSERT(dtrace_toxrange == NULL);
15552 15559 ASSERT(dtrace_toxranges_max == 0);
15553 15560 dtrace_toxranges_max = 1;
15554 15561 } else {
15555 15562 dtrace_toxranges_max <<= 1;
15556 15563 }
15557 15564
15558 15565 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
15559 15566 range = kmem_zalloc(nsize, KM_SLEEP);
15560 15567
15561 15568 if (dtrace_toxrange != NULL) {
15562 15569 ASSERT(osize != 0);
15563 15570 bcopy(dtrace_toxrange, range, osize);
15564 15571 kmem_free(dtrace_toxrange, osize);
15565 15572 }
15566 15573
15567 15574 dtrace_toxrange = range;
15568 15575 }
15569 15576
15570 15577 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL);
15571 15578 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL);
15572 15579
15573 15580 dtrace_toxrange[dtrace_toxranges].dtt_base = base;
15574 15581 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
15575 15582 dtrace_toxranges++;
15576 15583 }
15577 15584
15578 15585 static void
15579 15586 dtrace_getf_barrier()
15580 15587 {
15581 15588 /*
15582 15589 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings
15583 15590 * that contain calls to getf(), this routine will be called on every
15584 15591 * closef() before either the underlying vnode is released or the
15585 15592 * file_t itself is freed. By the time we are here, it is essential
15586 15593 * that the file_t can no longer be accessed from a call to getf()
15587 15594 * in probe context -- that assures that a dtrace_sync() can be used
15588 15595 * to clear out any enablings referring to the old structures.
15589 15596 */
15590 15597 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 ||
15591 15598 kcred->cr_zone->zone_dtrace_getf != 0)
15592 15599 dtrace_sync();
15593 15600 }
15594 15601
15595 15602 /*
15596 15603 * DTrace Driver Cookbook Functions
15597 15604 */
15598 15605 /*ARGSUSED*/
15599 15606 static int
15600 15607 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
15601 15608 {
15602 15609 dtrace_provider_id_t id;
15603 15610 dtrace_state_t *state = NULL;
15604 15611 dtrace_enabling_t *enab;
15605 15612
15606 15613 mutex_enter(&cpu_lock);
15607 15614 mutex_enter(&dtrace_provider_lock);
15608 15615 mutex_enter(&dtrace_lock);
15609 15616
15610 15617 if (ddi_soft_state_init(&dtrace_softstate,
15611 15618 sizeof (dtrace_state_t), 0) != 0) {
15612 15619 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
15613 15620 mutex_exit(&cpu_lock);
15614 15621 mutex_exit(&dtrace_provider_lock);
15615 15622 mutex_exit(&dtrace_lock);
15616 15623 return (DDI_FAILURE);
15617 15624 }
15618 15625
15619 15626 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
15620 15627 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
15621 15628 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
15622 15629 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
15623 15630 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
15624 15631 ddi_remove_minor_node(devi, NULL);
15625 15632 ddi_soft_state_fini(&dtrace_softstate);
15626 15633 mutex_exit(&cpu_lock);
15627 15634 mutex_exit(&dtrace_provider_lock);
15628 15635 mutex_exit(&dtrace_lock);
15629 15636 return (DDI_FAILURE);
15630 15637 }
15631 15638
15632 15639 ddi_report_dev(devi);
15633 15640 dtrace_devi = devi;
15634 15641
15635 15642 dtrace_modload = dtrace_module_loaded;
15636 15643 dtrace_modunload = dtrace_module_unloaded;
15637 15644 dtrace_cpu_init = dtrace_cpu_setup_initial;
15638 15645 dtrace_helpers_cleanup = dtrace_helpers_destroy;
15639 15646 dtrace_helpers_fork = dtrace_helpers_duplicate;
15640 15647 dtrace_cpustart_init = dtrace_suspend;
15641 15648 dtrace_cpustart_fini = dtrace_resume;
15642 15649 dtrace_debugger_init = dtrace_suspend;
15643 15650 dtrace_debugger_fini = dtrace_resume;
15644 15651
15645 15652 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
15646 15653
15647 15654 ASSERT(MUTEX_HELD(&cpu_lock));
15648 15655
15649 15656 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
15650 15657 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
15651 15658 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
15652 15659 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
15653 15660 VM_SLEEP | VMC_IDENTIFIER);
15654 15661 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
15655 15662 1, INT_MAX, 0);
15656 15663
15657 15664 dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
15658 15665 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
15659 15666 NULL, NULL, NULL, NULL, NULL, 0);
15660 15667
15661 15668 ASSERT(MUTEX_HELD(&cpu_lock));
15662 15669 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
15663 15670 offsetof(dtrace_probe_t, dtpr_nextmod),
15664 15671 offsetof(dtrace_probe_t, dtpr_prevmod));
15665 15672
15666 15673 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
15667 15674 offsetof(dtrace_probe_t, dtpr_nextfunc),
15668 15675 offsetof(dtrace_probe_t, dtpr_prevfunc));
15669 15676
15670 15677 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
15671 15678 offsetof(dtrace_probe_t, dtpr_nextname),
15672 15679 offsetof(dtrace_probe_t, dtpr_prevname));
15673 15680
15674 15681 if (dtrace_retain_max < 1) {
15675 15682 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
15676 15683 "setting to 1", dtrace_retain_max);
15677 15684 dtrace_retain_max = 1;
15678 15685 }
15679 15686
15680 15687 /*
15681 15688 * Now discover our toxic ranges.
15682 15689 */
15683 15690 dtrace_toxic_ranges(dtrace_toxrange_add);
15684 15691
15685 15692 /*
15686 15693 * Before we register ourselves as a provider to our own framework,
15687 15694 * we would like to assert that dtrace_provider is NULL -- but that's
15688 15695 * not true if we were loaded as a dependency of a DTrace provider.
15689 15696 * Once we've registered, we can assert that dtrace_provider is our
15690 15697 * pseudo provider.
15691 15698 */
15692 15699 (void) dtrace_register("dtrace", &dtrace_provider_attr,
15693 15700 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
15694 15701
15695 15702 ASSERT(dtrace_provider != NULL);
15696 15703 ASSERT((dtrace_provider_id_t)dtrace_provider == id);
15697 15704
15698 15705 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
↓ open down ↓ |
1296 lines elided |
↑ open up ↑ |
15699 15706 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
15700 15707 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
15701 15708 dtrace_provider, NULL, NULL, "END", 0, NULL);
15702 15709 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
15703 15710 dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
15704 15711
15705 15712 dtrace_anon_property();
15706 15713 mutex_exit(&cpu_lock);
15707 15714
15708 15715 /*
15709 - * If DTrace helper tracing is enabled, we need to allocate the
15710 - * trace buffer and initialize the values.
15711 - */
15712 - if (dtrace_helptrace_enabled) {
15713 - ASSERT(dtrace_helptrace_buffer == NULL);
15714 - dtrace_helptrace_buffer =
15715 - kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
15716 - dtrace_helptrace_next = 0;
15717 - }
15718 -
15719 - /*
15720 15716 * If there are already providers, we must ask them to provide their
15721 15717 * probes, and then match any anonymous enabling against them. Note
15722 15718 * that there should be no other retained enablings at this time:
15723 15719 * the only retained enablings at this time should be the anonymous
15724 15720 * enabling.
15725 15721 */
15726 15722 if (dtrace_anon.dta_enabling != NULL) {
15727 15723 ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
15728 15724
15729 15725 dtrace_enabling_provide(NULL);
15730 15726 state = dtrace_anon.dta_state;
15731 15727
15732 15728 /*
15733 15729 * We couldn't hold cpu_lock across the above call to
15734 15730 * dtrace_enabling_provide(), but we must hold it to actually
15735 15731 * enable the probes. We have to drop all of our locks, pick
15736 15732 * up cpu_lock, and regain our locks before matching the
15737 15733 * retained anonymous enabling.
15738 15734 */
15739 15735 mutex_exit(&dtrace_lock);
15740 15736 mutex_exit(&dtrace_provider_lock);
15741 15737
15742 15738 mutex_enter(&cpu_lock);
15743 15739 mutex_enter(&dtrace_provider_lock);
15744 15740 mutex_enter(&dtrace_lock);
15745 15741
15746 15742 if ((enab = dtrace_anon.dta_enabling) != NULL)
15747 15743 (void) dtrace_enabling_match(enab, NULL);
15748 15744
15749 15745 mutex_exit(&cpu_lock);
15750 15746 }
15751 15747
15752 15748 mutex_exit(&dtrace_lock);
15753 15749 mutex_exit(&dtrace_provider_lock);
15754 15750
15755 15751 if (state != NULL) {
15756 15752 /*
15757 15753 * If we created any anonymous state, set it going now.
15758 15754 */
15759 15755 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
15760 15756 }
15761 15757
15762 15758 return (DDI_SUCCESS);
15763 15759 }
15764 15760
15765 15761 /*ARGSUSED*/
15766 15762 static int
15767 15763 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
15768 15764 {
15769 15765 dtrace_state_t *state;
15770 15766 uint32_t priv;
15771 15767 uid_t uid;
15772 15768 zoneid_t zoneid;
15773 15769
15774 15770 if (getminor(*devp) == DTRACEMNRN_HELPER)
15775 15771 return (0);
15776 15772
15777 15773 /*
15778 15774 * If this wasn't an open with the "helper" minor, then it must be
15779 15775 * the "dtrace" minor.
15780 15776 */
15781 15777 if (getminor(*devp) != DTRACEMNRN_DTRACE)
15782 15778 return (ENXIO);
15783 15779
15784 15780 /*
15785 15781 * If no DTRACE_PRIV_* bits are set in the credential, then the
15786 15782 * caller lacks sufficient permission to do anything with DTrace.
15787 15783 */
15788 15784 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
15789 15785 if (priv == DTRACE_PRIV_NONE)
15790 15786 return (EACCES);
15791 15787
15792 15788 /*
15793 15789 * Ask all providers to provide all their probes.
15794 15790 */
15795 15791 mutex_enter(&dtrace_provider_lock);
15796 15792 dtrace_probe_provide(NULL, NULL);
15797 15793 mutex_exit(&dtrace_provider_lock);
15798 15794
15799 15795 mutex_enter(&cpu_lock);
15800 15796 mutex_enter(&dtrace_lock);
15801 15797 dtrace_opens++;
15802 15798 dtrace_membar_producer();
15803 15799
15804 15800 /*
↓ open down ↓ |
75 lines elided |
↑ open up ↑ |
15805 15801 * If the kernel debugger is active (that is, if the kernel debugger
15806 15802 * modified text in some way), we won't allow the open.
15807 15803 */
15808 15804 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15809 15805 dtrace_opens--;
15810 15806 mutex_exit(&cpu_lock);
15811 15807 mutex_exit(&dtrace_lock);
15812 15808 return (EBUSY);
15813 15809 }
15814 15810
15811 + if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) {
15812 + /*
15813 + * If DTrace helper tracing is enabled, we need to allocate the
15814 + * trace buffer and initialize the values.
15815 + */
15816 + dtrace_helptrace_buffer =
15817 + kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
15818 + dtrace_helptrace_next = 0;
15819 + dtrace_helptrace_wrapped = 0;
15820 + dtrace_helptrace_enable = 0;
15821 + }
15822 +
15815 15823 state = dtrace_state_create(devp, cred_p);
15816 15824 mutex_exit(&cpu_lock);
15817 15825
15818 15826 if (state == NULL) {
15819 15827 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
15820 15828 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15821 15829 mutex_exit(&dtrace_lock);
15822 15830 return (EAGAIN);
15823 15831 }
15824 15832
15825 15833 mutex_exit(&dtrace_lock);
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
15826 15834
15827 15835 return (0);
15828 15836 }
15829 15837
15830 15838 /*ARGSUSED*/
15831 15839 static int
15832 15840 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
15833 15841 {
15834 15842 minor_t minor = getminor(dev);
15835 15843 dtrace_state_t *state;
15844 + dtrace_helptrace_t *buf = NULL;
15836 15845
15837 15846 if (minor == DTRACEMNRN_HELPER)
15838 15847 return (0);
15839 15848
15840 15849 state = ddi_get_soft_state(dtrace_softstate, minor);
15841 15850
15842 15851 mutex_enter(&cpu_lock);
15843 15852 mutex_enter(&dtrace_lock);
15844 15853
15845 15854 if (state->dts_anon) {
15846 15855 /*
15847 15856 * There is anonymous state. Destroy that first.
15848 15857 */
15849 15858 ASSERT(dtrace_anon.dta_state == NULL);
15850 15859 dtrace_state_destroy(state->dts_anon);
15851 15860 }
15852 15861
15862 + if (dtrace_helptrace_disable) {
15863 + /*
15864 + * If we have been told to disable helper tracing, set the
15865 + * buffer to NULL before calling into dtrace_state_destroy();
15866 + * we take advantage of its dtrace_sync() to know that no
15867 + * CPU is in probe context with enabled helper tracing
15868 + * after it returns.
15869 + */
15870 + buf = dtrace_helptrace_buffer;
15871 + dtrace_helptrace_buffer = NULL;
15872 + }
15873 +
15853 15874 dtrace_state_destroy(state);
15854 15875 ASSERT(dtrace_opens > 0);
15855 15876
15856 15877 /*
15857 15878 * Only relinquish control of the kernel debugger interface when there
15858 15879 * are no consumers and no anonymous enablings.
15859 15880 */
15860 15881 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
15861 15882 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15862 15883
15884 + if (buf != NULL) {
15885 + kmem_free(buf, dtrace_helptrace_bufsize);
15886 + dtrace_helptrace_disable = 0;
15887 + }
15888 +
15863 15889 mutex_exit(&dtrace_lock);
15864 15890 mutex_exit(&cpu_lock);
15865 15891
15866 15892 return (0);
15867 15893 }
15868 15894
15869 15895 /*ARGSUSED*/
15870 15896 static int
15871 15897 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
15872 15898 {
15873 15899 int rval;
15874 15900 dof_helper_t help, *dhp = NULL;
15875 15901
15876 15902 switch (cmd) {
15877 15903 case DTRACEHIOC_ADDDOF:
15878 15904 if (copyin((void *)arg, &help, sizeof (help)) != 0) {
15879 15905 dtrace_dof_error(NULL, "failed to copyin DOF helper");
15880 15906 return (EFAULT);
15881 15907 }
15882 15908
15883 15909 dhp = &help;
15884 15910 arg = (intptr_t)help.dofhp_dof;
15885 15911 /*FALLTHROUGH*/
15886 15912
15887 15913 case DTRACEHIOC_ADD: {
15888 15914 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
15889 15915
15890 15916 if (dof == NULL)
15891 15917 return (rval);
15892 15918
15893 15919 mutex_enter(&dtrace_lock);
15894 15920
15895 15921 /*
15896 15922 * dtrace_helper_slurp() takes responsibility for the dof --
15897 15923 * it may free it now or it may save it and free it later.
15898 15924 */
15899 15925 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
15900 15926 *rv = rval;
15901 15927 rval = 0;
15902 15928 } else {
15903 15929 rval = EINVAL;
15904 15930 }
15905 15931
15906 15932 mutex_exit(&dtrace_lock);
15907 15933 return (rval);
15908 15934 }
15909 15935
15910 15936 case DTRACEHIOC_REMOVE: {
15911 15937 mutex_enter(&dtrace_lock);
15912 15938 rval = dtrace_helper_destroygen(arg);
15913 15939 mutex_exit(&dtrace_lock);
15914 15940
15915 15941 return (rval);
15916 15942 }
15917 15943
15918 15944 default:
15919 15945 break;
15920 15946 }
15921 15947
15922 15948 return (ENOTTY);
15923 15949 }
15924 15950
15925 15951 /*ARGSUSED*/
15926 15952 static int
15927 15953 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
15928 15954 {
15929 15955 minor_t minor = getminor(dev);
15930 15956 dtrace_state_t *state;
15931 15957 int rval;
15932 15958
15933 15959 if (minor == DTRACEMNRN_HELPER)
15934 15960 return (dtrace_ioctl_helper(cmd, arg, rv));
15935 15961
15936 15962 state = ddi_get_soft_state(dtrace_softstate, minor);
15937 15963
15938 15964 if (state->dts_anon) {
15939 15965 ASSERT(dtrace_anon.dta_state == NULL);
15940 15966 state = state->dts_anon;
15941 15967 }
15942 15968
15943 15969 switch (cmd) {
15944 15970 case DTRACEIOC_PROVIDER: {
15945 15971 dtrace_providerdesc_t pvd;
15946 15972 dtrace_provider_t *pvp;
15947 15973
15948 15974 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
15949 15975 return (EFAULT);
15950 15976
15951 15977 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
15952 15978 mutex_enter(&dtrace_provider_lock);
15953 15979
15954 15980 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
15955 15981 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
15956 15982 break;
15957 15983 }
15958 15984
15959 15985 mutex_exit(&dtrace_provider_lock);
15960 15986
15961 15987 if (pvp == NULL)
15962 15988 return (ESRCH);
15963 15989
15964 15990 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
15965 15991 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
15966 15992 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
15967 15993 return (EFAULT);
15968 15994
15969 15995 return (0);
15970 15996 }
15971 15997
15972 15998 case DTRACEIOC_EPROBE: {
15973 15999 dtrace_eprobedesc_t epdesc;
15974 16000 dtrace_ecb_t *ecb;
15975 16001 dtrace_action_t *act;
15976 16002 void *buf;
15977 16003 size_t size;
15978 16004 uintptr_t dest;
15979 16005 int nrecs;
15980 16006
15981 16007 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
15982 16008 return (EFAULT);
15983 16009
15984 16010 mutex_enter(&dtrace_lock);
15985 16011
15986 16012 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
15987 16013 mutex_exit(&dtrace_lock);
15988 16014 return (EINVAL);
15989 16015 }
15990 16016
15991 16017 if (ecb->dte_probe == NULL) {
15992 16018 mutex_exit(&dtrace_lock);
15993 16019 return (EINVAL);
15994 16020 }
15995 16021
15996 16022 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
15997 16023 epdesc.dtepd_uarg = ecb->dte_uarg;
15998 16024 epdesc.dtepd_size = ecb->dte_size;
15999 16025
16000 16026 nrecs = epdesc.dtepd_nrecs;
16001 16027 epdesc.dtepd_nrecs = 0;
16002 16028 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
16003 16029 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
16004 16030 continue;
16005 16031
16006 16032 epdesc.dtepd_nrecs++;
16007 16033 }
16008 16034
16009 16035 /*
16010 16036 * Now that we have the size, we need to allocate a temporary
16011 16037 * buffer in which to store the complete description. We need
16012 16038 * the temporary buffer to be able to drop dtrace_lock()
16013 16039 * across the copyout(), below.
16014 16040 */
16015 16041 size = sizeof (dtrace_eprobedesc_t) +
16016 16042 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
16017 16043
16018 16044 buf = kmem_alloc(size, KM_SLEEP);
16019 16045 dest = (uintptr_t)buf;
16020 16046
16021 16047 bcopy(&epdesc, (void *)dest, sizeof (epdesc));
16022 16048 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
16023 16049
16024 16050 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
16025 16051 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
16026 16052 continue;
16027 16053
16028 16054 if (nrecs-- == 0)
16029 16055 break;
16030 16056
16031 16057 bcopy(&act->dta_rec, (void *)dest,
16032 16058 sizeof (dtrace_recdesc_t));
16033 16059 dest += sizeof (dtrace_recdesc_t);
16034 16060 }
16035 16061
16036 16062 mutex_exit(&dtrace_lock);
16037 16063
16038 16064 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
16039 16065 kmem_free(buf, size);
16040 16066 return (EFAULT);
16041 16067 }
16042 16068
16043 16069 kmem_free(buf, size);
16044 16070 return (0);
16045 16071 }
16046 16072
16047 16073 case DTRACEIOC_AGGDESC: {
16048 16074 dtrace_aggdesc_t aggdesc;
16049 16075 dtrace_action_t *act;
16050 16076 dtrace_aggregation_t *agg;
16051 16077 int nrecs;
16052 16078 uint32_t offs;
16053 16079 dtrace_recdesc_t *lrec;
16054 16080 void *buf;
16055 16081 size_t size;
16056 16082 uintptr_t dest;
16057 16083
16058 16084 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
16059 16085 return (EFAULT);
16060 16086
16061 16087 mutex_enter(&dtrace_lock);
16062 16088
16063 16089 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
16064 16090 mutex_exit(&dtrace_lock);
16065 16091 return (EINVAL);
16066 16092 }
16067 16093
16068 16094 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
16069 16095
16070 16096 nrecs = aggdesc.dtagd_nrecs;
16071 16097 aggdesc.dtagd_nrecs = 0;
16072 16098
16073 16099 offs = agg->dtag_base;
16074 16100 lrec = &agg->dtag_action.dta_rec;
16075 16101 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
16076 16102
16077 16103 for (act = agg->dtag_first; ; act = act->dta_next) {
16078 16104 ASSERT(act->dta_intuple ||
16079 16105 DTRACEACT_ISAGG(act->dta_kind));
16080 16106
16081 16107 /*
16082 16108 * If this action has a record size of zero, it
16083 16109 * denotes an argument to the aggregating action.
16084 16110 * Because the presence of this record doesn't (or
16085 16111 * shouldn't) affect the way the data is interpreted,
16086 16112 * we don't copy it out to save user-level the
16087 16113 * confusion of dealing with a zero-length record.
16088 16114 */
16089 16115 if (act->dta_rec.dtrd_size == 0) {
16090 16116 ASSERT(agg->dtag_hasarg);
16091 16117 continue;
16092 16118 }
16093 16119
16094 16120 aggdesc.dtagd_nrecs++;
16095 16121
16096 16122 if (act == &agg->dtag_action)
16097 16123 break;
16098 16124 }
16099 16125
16100 16126 /*
16101 16127 * Now that we have the size, we need to allocate a temporary
16102 16128 * buffer in which to store the complete description. We need
16103 16129 * the temporary buffer to be able to drop dtrace_lock()
16104 16130 * across the copyout(), below.
16105 16131 */
16106 16132 size = sizeof (dtrace_aggdesc_t) +
16107 16133 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
16108 16134
16109 16135 buf = kmem_alloc(size, KM_SLEEP);
16110 16136 dest = (uintptr_t)buf;
16111 16137
16112 16138 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
16113 16139 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
16114 16140
16115 16141 for (act = agg->dtag_first; ; act = act->dta_next) {
16116 16142 dtrace_recdesc_t rec = act->dta_rec;
16117 16143
16118 16144 /*
16119 16145 * See the comment in the above loop for why we pass
16120 16146 * over zero-length records.
16121 16147 */
16122 16148 if (rec.dtrd_size == 0) {
16123 16149 ASSERT(agg->dtag_hasarg);
16124 16150 continue;
16125 16151 }
16126 16152
16127 16153 if (nrecs-- == 0)
16128 16154 break;
16129 16155
16130 16156 rec.dtrd_offset -= offs;
16131 16157 bcopy(&rec, (void *)dest, sizeof (rec));
16132 16158 dest += sizeof (dtrace_recdesc_t);
16133 16159
16134 16160 if (act == &agg->dtag_action)
16135 16161 break;
16136 16162 }
16137 16163
16138 16164 mutex_exit(&dtrace_lock);
16139 16165
16140 16166 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
16141 16167 kmem_free(buf, size);
16142 16168 return (EFAULT);
16143 16169 }
16144 16170
16145 16171 kmem_free(buf, size);
16146 16172 return (0);
16147 16173 }
16148 16174
16149 16175 case DTRACEIOC_ENABLE: {
16150 16176 dof_hdr_t *dof;
16151 16177 dtrace_enabling_t *enab = NULL;
16152 16178 dtrace_vstate_t *vstate;
16153 16179 int err = 0;
16154 16180
16155 16181 *rv = 0;
16156 16182
16157 16183 /*
16158 16184 * If a NULL argument has been passed, we take this as our
16159 16185 * cue to reevaluate our enablings.
16160 16186 */
16161 16187 if (arg == NULL) {
16162 16188 dtrace_enabling_matchall();
16163 16189
16164 16190 return (0);
16165 16191 }
16166 16192
16167 16193 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
16168 16194 return (rval);
16169 16195
16170 16196 mutex_enter(&cpu_lock);
16171 16197 mutex_enter(&dtrace_lock);
16172 16198 vstate = &state->dts_vstate;
16173 16199
16174 16200 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
16175 16201 mutex_exit(&dtrace_lock);
16176 16202 mutex_exit(&cpu_lock);
16177 16203 dtrace_dof_destroy(dof);
16178 16204 return (EBUSY);
16179 16205 }
16180 16206
16181 16207 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
16182 16208 mutex_exit(&dtrace_lock);
16183 16209 mutex_exit(&cpu_lock);
16184 16210 dtrace_dof_destroy(dof);
16185 16211 return (EINVAL);
16186 16212 }
16187 16213
16188 16214 if ((rval = dtrace_dof_options(dof, state)) != 0) {
16189 16215 dtrace_enabling_destroy(enab);
16190 16216 mutex_exit(&dtrace_lock);
16191 16217 mutex_exit(&cpu_lock);
16192 16218 dtrace_dof_destroy(dof);
16193 16219 return (rval);
16194 16220 }
16195 16221
16196 16222 if ((err = dtrace_enabling_match(enab, rv)) == 0) {
16197 16223 err = dtrace_enabling_retain(enab);
16198 16224 } else {
16199 16225 dtrace_enabling_destroy(enab);
16200 16226 }
16201 16227
16202 16228 mutex_exit(&cpu_lock);
16203 16229 mutex_exit(&dtrace_lock);
16204 16230 dtrace_dof_destroy(dof);
16205 16231
16206 16232 return (err);
16207 16233 }
16208 16234
16209 16235 case DTRACEIOC_REPLICATE: {
16210 16236 dtrace_repldesc_t desc;
16211 16237 dtrace_probedesc_t *match = &desc.dtrpd_match;
16212 16238 dtrace_probedesc_t *create = &desc.dtrpd_create;
16213 16239 int err;
16214 16240
16215 16241 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16216 16242 return (EFAULT);
16217 16243
16218 16244 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16219 16245 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16220 16246 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16221 16247 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16222 16248
16223 16249 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16224 16250 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16225 16251 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16226 16252 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16227 16253
16228 16254 mutex_enter(&dtrace_lock);
16229 16255 err = dtrace_enabling_replicate(state, match, create);
16230 16256 mutex_exit(&dtrace_lock);
16231 16257
16232 16258 return (err);
16233 16259 }
16234 16260
16235 16261 case DTRACEIOC_PROBEMATCH:
16236 16262 case DTRACEIOC_PROBES: {
16237 16263 dtrace_probe_t *probe = NULL;
16238 16264 dtrace_probedesc_t desc;
16239 16265 dtrace_probekey_t pkey;
16240 16266 dtrace_id_t i;
16241 16267 int m = 0;
16242 16268 uint32_t priv;
16243 16269 uid_t uid;
16244 16270 zoneid_t zoneid;
16245 16271
16246 16272 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16247 16273 return (EFAULT);
16248 16274
16249 16275 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16250 16276 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16251 16277 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16252 16278 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16253 16279
16254 16280 /*
16255 16281 * Before we attempt to match this probe, we want to give
16256 16282 * all providers the opportunity to provide it.
16257 16283 */
16258 16284 if (desc.dtpd_id == DTRACE_IDNONE) {
16259 16285 mutex_enter(&dtrace_provider_lock);
16260 16286 dtrace_probe_provide(&desc, NULL);
16261 16287 mutex_exit(&dtrace_provider_lock);
16262 16288 desc.dtpd_id++;
16263 16289 }
16264 16290
16265 16291 if (cmd == DTRACEIOC_PROBEMATCH) {
16266 16292 dtrace_probekey(&desc, &pkey);
16267 16293 pkey.dtpk_id = DTRACE_IDNONE;
16268 16294 }
16269 16295
16270 16296 dtrace_cred2priv(cr, &priv, &uid, &zoneid);
16271 16297
16272 16298 mutex_enter(&dtrace_lock);
16273 16299
16274 16300 if (cmd == DTRACEIOC_PROBEMATCH) {
16275 16301 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
16276 16302 if ((probe = dtrace_probes[i - 1]) != NULL &&
16277 16303 (m = dtrace_match_probe(probe, &pkey,
16278 16304 priv, uid, zoneid)) != 0)
16279 16305 break;
16280 16306 }
16281 16307
16282 16308 if (m < 0) {
16283 16309 mutex_exit(&dtrace_lock);
16284 16310 return (EINVAL);
16285 16311 }
16286 16312
16287 16313 } else {
16288 16314 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
16289 16315 if ((probe = dtrace_probes[i - 1]) != NULL &&
16290 16316 dtrace_match_priv(probe, priv, uid, zoneid))
16291 16317 break;
16292 16318 }
16293 16319 }
16294 16320
16295 16321 if (probe == NULL) {
16296 16322 mutex_exit(&dtrace_lock);
16297 16323 return (ESRCH);
16298 16324 }
16299 16325
16300 16326 dtrace_probe_description(probe, &desc);
16301 16327 mutex_exit(&dtrace_lock);
16302 16328
16303 16329 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16304 16330 return (EFAULT);
16305 16331
16306 16332 return (0);
16307 16333 }
16308 16334
16309 16335 case DTRACEIOC_PROBEARG: {
16310 16336 dtrace_argdesc_t desc;
16311 16337 dtrace_probe_t *probe;
16312 16338 dtrace_provider_t *prov;
16313 16339
16314 16340 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16315 16341 return (EFAULT);
16316 16342
16317 16343 if (desc.dtargd_id == DTRACE_IDNONE)
16318 16344 return (EINVAL);
16319 16345
16320 16346 if (desc.dtargd_ndx == DTRACE_ARGNONE)
16321 16347 return (EINVAL);
16322 16348
16323 16349 mutex_enter(&dtrace_provider_lock);
16324 16350 mutex_enter(&mod_lock);
16325 16351 mutex_enter(&dtrace_lock);
16326 16352
16327 16353 if (desc.dtargd_id > dtrace_nprobes) {
16328 16354 mutex_exit(&dtrace_lock);
16329 16355 mutex_exit(&mod_lock);
16330 16356 mutex_exit(&dtrace_provider_lock);
16331 16357 return (EINVAL);
16332 16358 }
16333 16359
16334 16360 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
16335 16361 mutex_exit(&dtrace_lock);
16336 16362 mutex_exit(&mod_lock);
16337 16363 mutex_exit(&dtrace_provider_lock);
16338 16364 return (EINVAL);
16339 16365 }
16340 16366
16341 16367 mutex_exit(&dtrace_lock);
16342 16368
16343 16369 prov = probe->dtpr_provider;
16344 16370
16345 16371 if (prov->dtpv_pops.dtps_getargdesc == NULL) {
16346 16372 /*
16347 16373 * There isn't any typed information for this probe.
16348 16374 * Set the argument number to DTRACE_ARGNONE.
16349 16375 */
16350 16376 desc.dtargd_ndx = DTRACE_ARGNONE;
16351 16377 } else {
16352 16378 desc.dtargd_native[0] = '\0';
16353 16379 desc.dtargd_xlate[0] = '\0';
16354 16380 desc.dtargd_mapping = desc.dtargd_ndx;
16355 16381
16356 16382 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
16357 16383 probe->dtpr_id, probe->dtpr_arg, &desc);
16358 16384 }
16359 16385
16360 16386 mutex_exit(&mod_lock);
16361 16387 mutex_exit(&dtrace_provider_lock);
16362 16388
16363 16389 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16364 16390 return (EFAULT);
16365 16391
16366 16392 return (0);
16367 16393 }
16368 16394
16369 16395 case DTRACEIOC_GO: {
16370 16396 processorid_t cpuid;
16371 16397 rval = dtrace_state_go(state, &cpuid);
16372 16398
16373 16399 if (rval != 0)
16374 16400 return (rval);
16375 16401
16376 16402 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
16377 16403 return (EFAULT);
16378 16404
16379 16405 return (0);
16380 16406 }
16381 16407
16382 16408 case DTRACEIOC_STOP: {
16383 16409 processorid_t cpuid;
16384 16410
16385 16411 mutex_enter(&dtrace_lock);
16386 16412 rval = dtrace_state_stop(state, &cpuid);
16387 16413 mutex_exit(&dtrace_lock);
16388 16414
16389 16415 if (rval != 0)
16390 16416 return (rval);
16391 16417
16392 16418 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
16393 16419 return (EFAULT);
16394 16420
16395 16421 return (0);
16396 16422 }
16397 16423
16398 16424 case DTRACEIOC_DOFGET: {
16399 16425 dof_hdr_t hdr, *dof;
16400 16426 uint64_t len;
16401 16427
16402 16428 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
16403 16429 return (EFAULT);
16404 16430
16405 16431 mutex_enter(&dtrace_lock);
16406 16432 dof = dtrace_dof_create(state);
16407 16433 mutex_exit(&dtrace_lock);
16408 16434
16409 16435 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
16410 16436 rval = copyout(dof, (void *)arg, len);
16411 16437 dtrace_dof_destroy(dof);
16412 16438
16413 16439 return (rval == 0 ? 0 : EFAULT);
16414 16440 }
16415 16441
16416 16442 case DTRACEIOC_AGGSNAP:
16417 16443 case DTRACEIOC_BUFSNAP: {
16418 16444 dtrace_bufdesc_t desc;
16419 16445 caddr_t cached;
16420 16446 dtrace_buffer_t *buf;
16421 16447
16422 16448 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16423 16449 return (EFAULT);
16424 16450
16425 16451 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
16426 16452 return (EINVAL);
16427 16453
16428 16454 mutex_enter(&dtrace_lock);
16429 16455
16430 16456 if (cmd == DTRACEIOC_BUFSNAP) {
16431 16457 buf = &state->dts_buffer[desc.dtbd_cpu];
16432 16458 } else {
16433 16459 buf = &state->dts_aggbuffer[desc.dtbd_cpu];
16434 16460 }
16435 16461
16436 16462 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
16437 16463 size_t sz = buf->dtb_offset;
16438 16464
16439 16465 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
16440 16466 mutex_exit(&dtrace_lock);
16441 16467 return (EBUSY);
16442 16468 }
16443 16469
16444 16470 /*
16445 16471 * If this buffer has already been consumed, we're
16446 16472 * going to indicate that there's nothing left here
16447 16473 * to consume.
16448 16474 */
16449 16475 if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
16450 16476 mutex_exit(&dtrace_lock);
16451 16477
16452 16478 desc.dtbd_size = 0;
16453 16479 desc.dtbd_drops = 0;
16454 16480 desc.dtbd_errors = 0;
16455 16481 desc.dtbd_oldest = 0;
16456 16482 sz = sizeof (desc);
16457 16483
16458 16484 if (copyout(&desc, (void *)arg, sz) != 0)
16459 16485 return (EFAULT);
16460 16486
16461 16487 return (0);
16462 16488 }
16463 16489
16464 16490 /*
16465 16491 * If this is a ring buffer that has wrapped, we want
16466 16492 * to copy the whole thing out.
16467 16493 */
16468 16494 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
16469 16495 dtrace_buffer_polish(buf);
16470 16496 sz = buf->dtb_size;
16471 16497 }
16472 16498
16473 16499 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
16474 16500 mutex_exit(&dtrace_lock);
16475 16501 return (EFAULT);
16476 16502 }
16477 16503
16478 16504 desc.dtbd_size = sz;
16479 16505 desc.dtbd_drops = buf->dtb_drops;
16480 16506 desc.dtbd_errors = buf->dtb_errors;
16481 16507 desc.dtbd_oldest = buf->dtb_xamot_offset;
16482 16508 desc.dtbd_timestamp = dtrace_gethrtime();
16483 16509
16484 16510 mutex_exit(&dtrace_lock);
16485 16511
16486 16512 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16487 16513 return (EFAULT);
16488 16514
16489 16515 buf->dtb_flags |= DTRACEBUF_CONSUMED;
16490 16516
16491 16517 return (0);
16492 16518 }
16493 16519
16494 16520 if (buf->dtb_tomax == NULL) {
16495 16521 ASSERT(buf->dtb_xamot == NULL);
16496 16522 mutex_exit(&dtrace_lock);
16497 16523 return (ENOENT);
16498 16524 }
16499 16525
16500 16526 cached = buf->dtb_tomax;
16501 16527 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
16502 16528
16503 16529 dtrace_xcall(desc.dtbd_cpu,
16504 16530 (dtrace_xcall_t)dtrace_buffer_switch, buf);
16505 16531
16506 16532 state->dts_errors += buf->dtb_xamot_errors;
16507 16533
16508 16534 /*
16509 16535 * If the buffers did not actually switch, then the cross call
16510 16536 * did not take place -- presumably because the given CPU is
16511 16537 * not in the ready set. If this is the case, we'll return
16512 16538 * ENOENT.
16513 16539 */
16514 16540 if (buf->dtb_tomax == cached) {
16515 16541 ASSERT(buf->dtb_xamot != cached);
16516 16542 mutex_exit(&dtrace_lock);
16517 16543 return (ENOENT);
16518 16544 }
16519 16545
16520 16546 ASSERT(cached == buf->dtb_xamot);
16521 16547
16522 16548 /*
16523 16549 * We have our snapshot; now copy it out.
16524 16550 */
16525 16551 if (copyout(buf->dtb_xamot, desc.dtbd_data,
16526 16552 buf->dtb_xamot_offset) != 0) {
16527 16553 mutex_exit(&dtrace_lock);
16528 16554 return (EFAULT);
16529 16555 }
16530 16556
16531 16557 desc.dtbd_size = buf->dtb_xamot_offset;
16532 16558 desc.dtbd_drops = buf->dtb_xamot_drops;
16533 16559 desc.dtbd_errors = buf->dtb_xamot_errors;
16534 16560 desc.dtbd_oldest = 0;
16535 16561 desc.dtbd_timestamp = buf->dtb_switched;
16536 16562
16537 16563 mutex_exit(&dtrace_lock);
16538 16564
16539 16565 /*
16540 16566 * Finally, copy out the buffer description.
16541 16567 */
16542 16568 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16543 16569 return (EFAULT);
16544 16570
16545 16571 return (0);
16546 16572 }
16547 16573
16548 16574 case DTRACEIOC_CONF: {
16549 16575 dtrace_conf_t conf;
16550 16576
16551 16577 bzero(&conf, sizeof (conf));
16552 16578 conf.dtc_difversion = DIF_VERSION;
16553 16579 conf.dtc_difintregs = DIF_DIR_NREGS;
16554 16580 conf.dtc_diftupregs = DIF_DTR_NREGS;
16555 16581 conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
16556 16582
16557 16583 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
16558 16584 return (EFAULT);
16559 16585
16560 16586 return (0);
16561 16587 }
16562 16588
16563 16589 case DTRACEIOC_STATUS: {
16564 16590 dtrace_status_t stat;
16565 16591 dtrace_dstate_t *dstate;
16566 16592 int i, j;
16567 16593 uint64_t nerrs;
16568 16594
16569 16595 /*
16570 16596 * See the comment in dtrace_state_deadman() for the reason
16571 16597 * for setting dts_laststatus to INT64_MAX before setting
16572 16598 * it to the correct value.
16573 16599 */
16574 16600 state->dts_laststatus = INT64_MAX;
16575 16601 dtrace_membar_producer();
16576 16602 state->dts_laststatus = dtrace_gethrtime();
16577 16603
16578 16604 bzero(&stat, sizeof (stat));
16579 16605
16580 16606 mutex_enter(&dtrace_lock);
16581 16607
16582 16608 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
16583 16609 mutex_exit(&dtrace_lock);
16584 16610 return (ENOENT);
16585 16611 }
16586 16612
16587 16613 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
16588 16614 stat.dtst_exiting = 1;
16589 16615
16590 16616 nerrs = state->dts_errors;
16591 16617 dstate = &state->dts_vstate.dtvs_dynvars;
16592 16618
16593 16619 for (i = 0; i < NCPU; i++) {
16594 16620 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
16595 16621
16596 16622 stat.dtst_dyndrops += dcpu->dtdsc_drops;
16597 16623 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
16598 16624 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
16599 16625
16600 16626 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
16601 16627 stat.dtst_filled++;
16602 16628
16603 16629 nerrs += state->dts_buffer[i].dtb_errors;
16604 16630
16605 16631 for (j = 0; j < state->dts_nspeculations; j++) {
16606 16632 dtrace_speculation_t *spec;
16607 16633 dtrace_buffer_t *buf;
16608 16634
16609 16635 spec = &state->dts_speculations[j];
16610 16636 buf = &spec->dtsp_buffer[i];
16611 16637 stat.dtst_specdrops += buf->dtb_xamot_drops;
16612 16638 }
16613 16639 }
16614 16640
16615 16641 stat.dtst_specdrops_busy = state->dts_speculations_busy;
16616 16642 stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
16617 16643 stat.dtst_stkstroverflows = state->dts_stkstroverflows;
16618 16644 stat.dtst_dblerrors = state->dts_dblerrors;
16619 16645 stat.dtst_killed =
16620 16646 (state->dts_activity == DTRACE_ACTIVITY_KILLED);
16621 16647 stat.dtst_errors = nerrs;
16622 16648
16623 16649 mutex_exit(&dtrace_lock);
16624 16650
16625 16651 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
16626 16652 return (EFAULT);
16627 16653
16628 16654 return (0);
16629 16655 }
16630 16656
16631 16657 case DTRACEIOC_FORMAT: {
16632 16658 dtrace_fmtdesc_t fmt;
16633 16659 char *str;
16634 16660 int len;
16635 16661
16636 16662 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
16637 16663 return (EFAULT);
16638 16664
16639 16665 mutex_enter(&dtrace_lock);
16640 16666
16641 16667 if (fmt.dtfd_format == 0 ||
16642 16668 fmt.dtfd_format > state->dts_nformats) {
16643 16669 mutex_exit(&dtrace_lock);
16644 16670 return (EINVAL);
16645 16671 }
16646 16672
16647 16673 /*
16648 16674 * Format strings are allocated contiguously and they are
16649 16675 * never freed; if a format index is less than the number
16650 16676 * of formats, we can assert that the format map is non-NULL
16651 16677 * and that the format for the specified index is non-NULL.
16652 16678 */
16653 16679 ASSERT(state->dts_formats != NULL);
16654 16680 str = state->dts_formats[fmt.dtfd_format - 1];
16655 16681 ASSERT(str != NULL);
16656 16682
16657 16683 len = strlen(str) + 1;
16658 16684
16659 16685 if (len > fmt.dtfd_length) {
16660 16686 fmt.dtfd_length = len;
16661 16687
16662 16688 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
16663 16689 mutex_exit(&dtrace_lock);
16664 16690 return (EINVAL);
16665 16691 }
16666 16692 } else {
16667 16693 if (copyout(str, fmt.dtfd_string, len) != 0) {
16668 16694 mutex_exit(&dtrace_lock);
16669 16695 return (EINVAL);
16670 16696 }
16671 16697 }
16672 16698
16673 16699 mutex_exit(&dtrace_lock);
16674 16700 return (0);
16675 16701 }
16676 16702
16677 16703 default:
16678 16704 break;
16679 16705 }
16680 16706
16681 16707 return (ENOTTY);
16682 16708 }
16683 16709
16684 16710 /*ARGSUSED*/
16685 16711 static int
16686 16712 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
16687 16713 {
16688 16714 dtrace_state_t *state;
16689 16715
16690 16716 switch (cmd) {
16691 16717 case DDI_DETACH:
16692 16718 break;
16693 16719
16694 16720 case DDI_SUSPEND:
16695 16721 return (DDI_SUCCESS);
16696 16722
16697 16723 default:
16698 16724 return (DDI_FAILURE);
16699 16725 }
16700 16726
16701 16727 mutex_enter(&cpu_lock);
16702 16728 mutex_enter(&dtrace_provider_lock);
16703 16729 mutex_enter(&dtrace_lock);
16704 16730
16705 16731 ASSERT(dtrace_opens == 0);
16706 16732
16707 16733 if (dtrace_helpers > 0) {
16708 16734 mutex_exit(&dtrace_provider_lock);
16709 16735 mutex_exit(&dtrace_lock);
16710 16736 mutex_exit(&cpu_lock);
16711 16737 return (DDI_FAILURE);
16712 16738 }
16713 16739
16714 16740 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
16715 16741 mutex_exit(&dtrace_provider_lock);
16716 16742 mutex_exit(&dtrace_lock);
16717 16743 mutex_exit(&cpu_lock);
16718 16744 return (DDI_FAILURE);
16719 16745 }
16720 16746
16721 16747 dtrace_provider = NULL;
16722 16748
16723 16749 if ((state = dtrace_anon_grab()) != NULL) {
16724 16750 /*
16725 16751 * If there were ECBs on this state, the provider should
16726 16752 * have not been allowed to detach; assert that there is
16727 16753 * none.
16728 16754 */
16729 16755 ASSERT(state->dts_necbs == 0);
16730 16756 dtrace_state_destroy(state);
16731 16757
16732 16758 /*
16733 16759 * If we're being detached with anonymous state, we need to
16734 16760 * indicate to the kernel debugger that DTrace is now inactive.
16735 16761 */
16736 16762 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
16737 16763 }
16738 16764
16739 16765 bzero(&dtrace_anon, sizeof (dtrace_anon_t));
16740 16766 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
16741 16767 dtrace_cpu_init = NULL;
16742 16768 dtrace_helpers_cleanup = NULL;
16743 16769 dtrace_helpers_fork = NULL;
16744 16770 dtrace_cpustart_init = NULL;
16745 16771 dtrace_cpustart_fini = NULL;
↓ open down ↓ |
873 lines elided |
↑ open up ↑ |
16746 16772 dtrace_debugger_init = NULL;
16747 16773 dtrace_debugger_fini = NULL;
16748 16774 dtrace_modload = NULL;
16749 16775 dtrace_modunload = NULL;
16750 16776
16751 16777 ASSERT(dtrace_getf == 0);
16752 16778 ASSERT(dtrace_closef == NULL);
16753 16779
16754 16780 mutex_exit(&cpu_lock);
16755 16781
16756 - if (dtrace_helptrace_enabled) {
16757 - kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize);
16758 - dtrace_helptrace_buffer = NULL;
16759 - }
16760 -
16761 16782 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
16762 16783 dtrace_probes = NULL;
16763 16784 dtrace_nprobes = 0;
16764 16785
16765 16786 dtrace_hash_destroy(dtrace_bymod);
16766 16787 dtrace_hash_destroy(dtrace_byfunc);
16767 16788 dtrace_hash_destroy(dtrace_byname);
16768 16789 dtrace_bymod = NULL;
16769 16790 dtrace_byfunc = NULL;
16770 16791 dtrace_byname = NULL;
16771 16792
16772 16793 kmem_cache_destroy(dtrace_state_cache);
16773 16794 vmem_destroy(dtrace_minor);
16774 16795 vmem_destroy(dtrace_arena);
16775 16796
16776 16797 if (dtrace_toxrange != NULL) {
16777 16798 kmem_free(dtrace_toxrange,
16778 16799 dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
16779 16800 dtrace_toxrange = NULL;
16780 16801 dtrace_toxranges = 0;
16781 16802 dtrace_toxranges_max = 0;
16782 16803 }
16783 16804
16784 16805 ddi_remove_minor_node(dtrace_devi, NULL);
16785 16806 dtrace_devi = NULL;
16786 16807
16787 16808 ddi_soft_state_fini(&dtrace_softstate);
16788 16809
16789 16810 ASSERT(dtrace_vtime_references == 0);
16790 16811 ASSERT(dtrace_opens == 0);
16791 16812 ASSERT(dtrace_retained == NULL);
16792 16813
16793 16814 mutex_exit(&dtrace_lock);
16794 16815 mutex_exit(&dtrace_provider_lock);
16795 16816
16796 16817 /*
16797 16818 * We don't destroy the task queue until after we have dropped our
16798 16819 * locks (taskq_destroy() may block on running tasks). To prevent
16799 16820 * attempting to do work after we have effectively detached but before
16800 16821 * the task queue has been destroyed, all tasks dispatched via the
16801 16822 * task queue must check that DTrace is still attached before
16802 16823 * performing any operation.
16803 16824 */
16804 16825 taskq_destroy(dtrace_taskq);
16805 16826 dtrace_taskq = NULL;
16806 16827
16807 16828 return (DDI_SUCCESS);
16808 16829 }
16809 16830
16810 16831 /*ARGSUSED*/
16811 16832 static int
16812 16833 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
16813 16834 {
16814 16835 int error;
16815 16836
16816 16837 switch (infocmd) {
16817 16838 case DDI_INFO_DEVT2DEVINFO:
16818 16839 *result = (void *)dtrace_devi;
16819 16840 error = DDI_SUCCESS;
16820 16841 break;
16821 16842 case DDI_INFO_DEVT2INSTANCE:
16822 16843 *result = (void *)0;
16823 16844 error = DDI_SUCCESS;
16824 16845 break;
16825 16846 default:
16826 16847 error = DDI_FAILURE;
16827 16848 }
16828 16849 return (error);
16829 16850 }
16830 16851
16831 16852 static struct cb_ops dtrace_cb_ops = {
16832 16853 dtrace_open, /* open */
16833 16854 dtrace_close, /* close */
16834 16855 nulldev, /* strategy */
16835 16856 nulldev, /* print */
16836 16857 nodev, /* dump */
16837 16858 nodev, /* read */
16838 16859 nodev, /* write */
16839 16860 dtrace_ioctl, /* ioctl */
16840 16861 nodev, /* devmap */
16841 16862 nodev, /* mmap */
16842 16863 nodev, /* segmap */
16843 16864 nochpoll, /* poll */
16844 16865 ddi_prop_op, /* cb_prop_op */
16845 16866 0, /* streamtab */
16846 16867 D_NEW | D_MP /* Driver compatibility flag */
16847 16868 };
16848 16869
16849 16870 static struct dev_ops dtrace_ops = {
16850 16871 DEVO_REV, /* devo_rev */
16851 16872 0, /* refcnt */
16852 16873 dtrace_info, /* get_dev_info */
16853 16874 nulldev, /* identify */
16854 16875 nulldev, /* probe */
16855 16876 dtrace_attach, /* attach */
16856 16877 dtrace_detach, /* detach */
16857 16878 nodev, /* reset */
16858 16879 &dtrace_cb_ops, /* driver operations */
16859 16880 NULL, /* bus operations */
16860 16881 nodev, /* dev power */
16861 16882 ddi_quiesce_not_needed, /* quiesce */
16862 16883 };
16863 16884
16864 16885 static struct modldrv modldrv = {
16865 16886 &mod_driverops, /* module type (this is a pseudo driver) */
16866 16887 "Dynamic Tracing", /* name of module */
16867 16888 &dtrace_ops, /* driver ops */
16868 16889 };
16869 16890
16870 16891 static struct modlinkage modlinkage = {
16871 16892 MODREV_1,
16872 16893 (void *)&modldrv,
16873 16894 NULL
16874 16895 };
16875 16896
16876 16897 int
16877 16898 _init(void)
16878 16899 {
16879 16900 return (mod_install(&modlinkage));
16880 16901 }
16881 16902
16882 16903 int
16883 16904 _info(struct modinfo *modinfop)
16884 16905 {
16885 16906 return (mod_info(&modlinkage, modinfop));
16886 16907 }
16887 16908
16888 16909 int
16889 16910 _fini(void)
16890 16911 {
16891 16912 return (mod_remove(&modlinkage));
16892 16913 }
↓ open down ↓ |
122 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX