Print this page
OS-1723 DTrace should speak JSON (review fixes)
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) 2012, 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,
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 272 *
273 273 * These variables should be set dynamically to enable helper tracing. The
274 274 * only variables that should be set are dtrace_helptrace_enable (which should
275 275 * be set to a non-zero value to allocate helper tracing buffers on the next
276 276 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a
277 277 * non-zero value to deallocate helper tracing buffers on the next close of
278 278 * /dev/dtrace). When (and only when) helper tracing is disabled, the
279 279 * buffer size may also be set via dtrace_helptrace_bufsize.
280 280 */
281 281 int dtrace_helptrace_enable = 0;
282 282 int dtrace_helptrace_disable = 0;
283 283 int dtrace_helptrace_bufsize = 16 * 1024 * 1024;
284 284 uint32_t dtrace_helptrace_nlocals;
285 285 static dtrace_helptrace_t *dtrace_helptrace_buffer;
286 286 static uint32_t dtrace_helptrace_next = 0;
287 287 static int dtrace_helptrace_wrapped = 0;
288 288
289 289 /*
290 290 * DTrace Error Hashing
291 291 *
292 292 * On DEBUG kernels, DTrace will track the errors that has seen in a hash
293 293 * table. This is very useful for checking coverage of tests that are
294 294 * expected to induce DIF or DOF processing errors, and may be useful for
295 295 * debugging problems in the DIF code generator or in DOF generation . The
296 296 * error hash may be examined with the ::dtrace_errhash MDB dcmd.
297 297 */
298 298 #ifdef DEBUG
299 299 static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ];
300 300 static const char *dtrace_errlast;
301 301 static kthread_t *dtrace_errthread;
302 302 static kmutex_t dtrace_errlock;
303 303 #endif
304 304
305 305 /*
306 306 * DTrace Macros and Constants
307 307 *
308 308 * These are various macros that are useful in various spots in the
309 309 * implementation, along with a few random constants that have no meaning
310 310 * outside of the implementation. There is no real structure to this cpp
311 311 * mishmash -- but is there ever?
312 312 */
313 313 #define DTRACE_HASHSTR(hash, probe) \
314 314 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
315 315
316 316 #define DTRACE_HASHNEXT(hash, probe) \
317 317 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
318 318
319 319 #define DTRACE_HASHPREV(hash, probe) \
320 320 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
321 321
322 322 #define DTRACE_HASHEQ(hash, lhs, rhs) \
323 323 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
324 324 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
325 325
326 326 #define DTRACE_AGGHASHSIZE_SLEW 17
327 327
328 328 #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3)
329 329
330 330 /*
331 331 * The key for a thread-local variable consists of the lower 61 bits of the
332 332 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
333 333 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
334 334 * equal to a variable identifier. This is necessary (but not sufficient) to
335 335 * assure that global associative arrays never collide with thread-local
336 336 * variables. To guarantee that they cannot collide, we must also define the
337 337 * order for keying dynamic variables. That order is:
338 338 *
339 339 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
340 340 *
341 341 * Because the variable-key and the tls-key are in orthogonal spaces, there is
342 342 * no way for a global variable key signature to match a thread-local key
343 343 * signature.
344 344 */
345 345 #define DTRACE_TLS_THRKEY(where) { \
346 346 uint_t intr = 0; \
347 347 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
348 348 for (; actv; actv >>= 1) \
349 349 intr++; \
350 350 ASSERT(intr < (1 << 3)); \
351 351 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
352 352 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
353 353 }
354 354
355 355 #define DT_BSWAP_8(x) ((x) & 0xff)
356 356 #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
357 357 #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
358 358 #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
359 359
360 360 #define DT_MASK_LO 0x00000000FFFFFFFFULL
361 361
362 362 #define DTRACE_STORE(type, tomax, offset, what) \
363 363 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
364 364
365 365 #ifndef __i386
366 366 #define DTRACE_ALIGNCHECK(addr, size, flags) \
367 367 if (addr & (size - 1)) { \
368 368 *flags |= CPU_DTRACE_BADALIGN; \
369 369 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
370 370 return (0); \
371 371 }
372 372 #else
373 373 #define DTRACE_ALIGNCHECK(addr, size, flags)
374 374 #endif
375 375
376 376 /*
377 377 * Test whether a range of memory starting at testaddr of size testsz falls
378 378 * within the range of memory described by addr, sz. We take care to avoid
379 379 * problems with overflow and underflow of the unsigned quantities, and
380 380 * disallow all negative sizes. Ranges of size 0 are allowed.
381 381 */
382 382 #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
383 383 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \
384 384 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \
385 385 (testaddr) + (testsz) >= (testaddr))
386 386
387 387 /*
388 388 * Test whether alloc_sz bytes will fit in the scratch region. We isolate
389 389 * alloc_sz on the righthand side of the comparison in order to avoid overflow
390 390 * or underflow in the comparison with it. This is simpler than the INRANGE
391 391 * check above, because we know that the dtms_scratch_ptr is valid in the
392 392 * range. Allocations of size zero are allowed.
393 393 */
394 394 #define DTRACE_INSCRATCH(mstate, alloc_sz) \
395 395 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
396 396 (mstate)->dtms_scratch_ptr >= (alloc_sz))
397 397
398 398 #define DTRACE_LOADFUNC(bits) \
399 399 /*CSTYLED*/ \
400 400 uint##bits##_t \
401 401 dtrace_load##bits(uintptr_t addr) \
402 402 { \
403 403 size_t size = bits / NBBY; \
404 404 /*CSTYLED*/ \
405 405 uint##bits##_t rval; \
406 406 int i; \
407 407 volatile uint16_t *flags = (volatile uint16_t *) \
408 408 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \
409 409 \
410 410 DTRACE_ALIGNCHECK(addr, size, flags); \
411 411 \
412 412 for (i = 0; i < dtrace_toxranges; i++) { \
413 413 if (addr >= dtrace_toxrange[i].dtt_limit) \
414 414 continue; \
415 415 \
416 416 if (addr + size <= dtrace_toxrange[i].dtt_base) \
417 417 continue; \
418 418 \
419 419 /* \
420 420 * This address falls within a toxic region; return 0. \
421 421 */ \
422 422 *flags |= CPU_DTRACE_BADADDR; \
423 423 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \
424 424 return (0); \
425 425 } \
426 426 \
427 427 *flags |= CPU_DTRACE_NOFAULT; \
428 428 /*CSTYLED*/ \
429 429 rval = *((volatile uint##bits##_t *)addr); \
430 430 *flags &= ~CPU_DTRACE_NOFAULT; \
431 431 \
432 432 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \
433 433 }
434 434
435 435 #ifdef _LP64
436 436 #define dtrace_loadptr dtrace_load64
437 437 #else
438 438 #define dtrace_loadptr dtrace_load32
439 439 #endif
440 440
441 441 #define DTRACE_DYNHASH_FREE 0
442 442 #define DTRACE_DYNHASH_SINK 1
443 443 #define DTRACE_DYNHASH_VALID 2
444 444
445 445 #define DTRACE_MATCH_FAIL -1
446 446 #define DTRACE_MATCH_NEXT 0
447 447 #define DTRACE_MATCH_DONE 1
448 448 #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0')
449 449 #define DTRACE_STATE_ALIGN 64
450 450
451 451 #define DTRACE_FLAGS2FLT(flags) \
452 452 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \
453 453 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \
454 454 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \
455 455 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \
456 456 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \
457 457 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \
458 458 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \
459 459 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \
460 460 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \
461 461 DTRACEFLT_UNKNOWN)
462 462
463 463 #define DTRACEACT_ISSTRING(act) \
464 464 ((act)->dta_kind == DTRACEACT_DIFEXPR && \
465 465 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
466 466
467 467 static size_t dtrace_strlen(const char *, size_t);
468 468 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
469 469 static void dtrace_enabling_provide(dtrace_provider_t *);
470 470 static int dtrace_enabling_match(dtrace_enabling_t *, int *);
471 471 static void dtrace_enabling_matchall(void);
472 472 static void dtrace_enabling_reap(void);
473 473 static dtrace_state_t *dtrace_anon_grab(void);
474 474 static uint64_t dtrace_helper(int, dtrace_mstate_t *,
475 475 dtrace_state_t *, uint64_t, uint64_t);
476 476 static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
477 477 static void dtrace_buffer_drop(dtrace_buffer_t *);
478 478 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when);
479 479 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
480 480 dtrace_state_t *, dtrace_mstate_t *);
481 481 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
482 482 dtrace_optval_t);
483 483 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
484 484 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
485 485 static int dtrace_priv_proc(dtrace_state_t *, dtrace_mstate_t *);
486 486 static void dtrace_getf_barrier(void);
487 487
488 488 /*
489 489 * DTrace Probe Context Functions
490 490 *
491 491 * These functions are called from probe context. Because probe context is
492 492 * any context in which C may be called, arbitrarily locks may be held,
493 493 * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
494 494 * As a result, functions called from probe context may only call other DTrace
495 495 * support functions -- they may not interact at all with the system at large.
496 496 * (Note that the ASSERT macro is made probe-context safe by redefining it in
497 497 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
498 498 * loads are to be performed from probe context, they _must_ be in terms of
499 499 * the safe dtrace_load*() variants.
500 500 *
501 501 * Some functions in this block are not actually called from probe context;
502 502 * for these functions, there will be a comment above the function reading
503 503 * "Note: not called from probe context."
504 504 */
505 505 void
506 506 dtrace_panic(const char *format, ...)
507 507 {
508 508 va_list alist;
509 509
510 510 va_start(alist, format);
511 511 dtrace_vpanic(format, alist);
512 512 va_end(alist);
513 513 }
514 514
515 515 int
516 516 dtrace_assfail(const char *a, const char *f, int l)
517 517 {
518 518 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
519 519
520 520 /*
521 521 * We just need something here that even the most clever compiler
522 522 * cannot optimize away.
523 523 */
524 524 return (a[(uintptr_t)f]);
525 525 }
526 526
527 527 /*
528 528 * Atomically increment a specified error counter from probe context.
529 529 */
530 530 static void
531 531 dtrace_error(uint32_t *counter)
532 532 {
533 533 /*
534 534 * Most counters stored to in probe context are per-CPU counters.
535 535 * However, there are some error conditions that are sufficiently
536 536 * arcane that they don't merit per-CPU storage. If these counters
537 537 * are incremented concurrently on different CPUs, scalability will be
538 538 * adversely affected -- but we don't expect them to be white-hot in a
539 539 * correctly constructed enabling...
540 540 */
541 541 uint32_t oval, nval;
542 542
543 543 do {
544 544 oval = *counter;
545 545
546 546 if ((nval = oval + 1) == 0) {
547 547 /*
548 548 * If the counter would wrap, set it to 1 -- assuring
549 549 * that the counter is never zero when we have seen
550 550 * errors. (The counter must be 32-bits because we
551 551 * aren't guaranteed a 64-bit compare&swap operation.)
552 552 * To save this code both the infamy of being fingered
553 553 * by a priggish news story and the indignity of being
554 554 * the target of a neo-puritan witch trial, we're
555 555 * carefully avoiding any colorful description of the
556 556 * likelihood of this condition -- but suffice it to
557 557 * say that it is only slightly more likely than the
558 558 * overflow of predicate cache IDs, as discussed in
559 559 * dtrace_predicate_create().
560 560 */
561 561 nval = 1;
562 562 }
563 563 } while (dtrace_cas32(counter, oval, nval) != oval);
564 564 }
565 565
566 566 /*
567 567 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
568 568 * uint8_t, a uint16_t, a uint32_t and a uint64_t.
569 569 */
570 570 DTRACE_LOADFUNC(8)
571 571 DTRACE_LOADFUNC(16)
572 572 DTRACE_LOADFUNC(32)
573 573 DTRACE_LOADFUNC(64)
574 574
575 575 static int
576 576 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
577 577 {
578 578 if (dest < mstate->dtms_scratch_base)
579 579 return (0);
580 580
581 581 if (dest + size < dest)
582 582 return (0);
583 583
584 584 if (dest + size > mstate->dtms_scratch_ptr)
585 585 return (0);
586 586
587 587 return (1);
588 588 }
589 589
590 590 static int
591 591 dtrace_canstore_statvar(uint64_t addr, size_t sz,
592 592 dtrace_statvar_t **svars, int nsvars)
593 593 {
594 594 int i;
595 595
596 596 for (i = 0; i < nsvars; i++) {
597 597 dtrace_statvar_t *svar = svars[i];
598 598
599 599 if (svar == NULL || svar->dtsv_size == 0)
600 600 continue;
601 601
602 602 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size))
603 603 return (1);
604 604 }
605 605
606 606 return (0);
607 607 }
608 608
609 609 /*
610 610 * Check to see if the address is within a memory region to which a store may
611 611 * be issued. This includes the DTrace scratch areas, and any DTrace variable
612 612 * region. The caller of dtrace_canstore() is responsible for performing any
613 613 * alignment checks that are needed before stores are actually executed.
614 614 */
615 615 static int
616 616 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
617 617 dtrace_vstate_t *vstate)
618 618 {
619 619 /*
620 620 * First, check to see if the address is in scratch space...
621 621 */
622 622 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
623 623 mstate->dtms_scratch_size))
624 624 return (1);
625 625
626 626 /*
627 627 * Now check to see if it's a dynamic variable. This check will pick
628 628 * up both thread-local variables and any global dynamically-allocated
629 629 * variables.
630 630 */
631 631 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base,
632 632 vstate->dtvs_dynvars.dtds_size)) {
633 633 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
634 634 uintptr_t base = (uintptr_t)dstate->dtds_base +
635 635 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
636 636 uintptr_t chunkoffs;
637 637
638 638 /*
639 639 * Before we assume that we can store here, we need to make
640 640 * sure that it isn't in our metadata -- storing to our
641 641 * dynamic variable metadata would corrupt our state. For
642 642 * the range to not include any dynamic variable metadata,
643 643 * it must:
644 644 *
645 645 * (1) Start above the hash table that is at the base of
646 646 * the dynamic variable space
647 647 *
648 648 * (2) Have a starting chunk offset that is beyond the
649 649 * dtrace_dynvar_t that is at the base of every chunk
650 650 *
651 651 * (3) Not span a chunk boundary
652 652 *
653 653 */
654 654 if (addr < base)
655 655 return (0);
656 656
657 657 chunkoffs = (addr - base) % dstate->dtds_chunksize;
658 658
659 659 if (chunkoffs < sizeof (dtrace_dynvar_t))
660 660 return (0);
661 661
662 662 if (chunkoffs + sz > dstate->dtds_chunksize)
663 663 return (0);
664 664
665 665 return (1);
666 666 }
667 667
668 668 /*
669 669 * Finally, check the static local and global variables. These checks
670 670 * take the longest, so we perform them last.
671 671 */
672 672 if (dtrace_canstore_statvar(addr, sz,
673 673 vstate->dtvs_locals, vstate->dtvs_nlocals))
674 674 return (1);
675 675
676 676 if (dtrace_canstore_statvar(addr, sz,
677 677 vstate->dtvs_globals, vstate->dtvs_nglobals))
678 678 return (1);
679 679
680 680 return (0);
681 681 }
682 682
683 683
684 684 /*
685 685 * Convenience routine to check to see if the address is within a memory
686 686 * region in which a load may be issued given the user's privilege level;
687 687 * if not, it sets the appropriate error flags and loads 'addr' into the
688 688 * illegal value slot.
689 689 *
690 690 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
691 691 * appropriate memory access protection.
692 692 */
693 693 static int
694 694 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
695 695 dtrace_vstate_t *vstate)
696 696 {
697 697 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
698 698 file_t *fp;
699 699
700 700 /*
701 701 * If we hold the privilege to read from kernel memory, then
702 702 * everything is readable.
703 703 */
704 704 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
705 705 return (1);
706 706
707 707 /*
708 708 * You can obviously read that which you can store.
709 709 */
710 710 if (dtrace_canstore(addr, sz, mstate, vstate))
711 711 return (1);
712 712
713 713 /*
714 714 * We're allowed to read from our own string table.
715 715 */
716 716 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab,
717 717 mstate->dtms_difo->dtdo_strlen))
718 718 return (1);
719 719
720 720 if (vstate->dtvs_state != NULL &&
721 721 dtrace_priv_proc(vstate->dtvs_state, mstate)) {
722 722 proc_t *p;
723 723
724 724 /*
725 725 * When we have privileges to the current process, there are
726 726 * several context-related kernel structures that are safe to
727 727 * read, even absent the privilege to read from kernel memory.
728 728 * These reads are safe because these structures contain only
729 729 * state that (1) we're permitted to read, (2) is harmless or
730 730 * (3) contains pointers to additional kernel state that we're
731 731 * not permitted to read (and as such, do not present an
732 732 * opportunity for privilege escalation). Finally (and
733 733 * critically), because of the nature of their relation with
734 734 * the current thread context, the memory associated with these
735 735 * structures cannot change over the duration of probe context,
736 736 * and it is therefore impossible for this memory to be
737 737 * deallocated and reallocated as something else while it's
738 738 * being operated upon.
739 739 */
740 740 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t)))
741 741 return (1);
742 742
743 743 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr,
744 744 sz, curthread->t_procp, sizeof (proc_t))) {
745 745 return (1);
746 746 }
747 747
748 748 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz,
749 749 curthread->t_cred, sizeof (cred_t))) {
750 750 return (1);
751 751 }
752 752
753 753 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz,
754 754 &(p->p_pidp->pid_id), sizeof (pid_t))) {
755 755 return (1);
756 756 }
757 757
758 758 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz,
759 759 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) {
760 760 return (1);
761 761 }
762 762 }
763 763
764 764 if ((fp = mstate->dtms_getf) != NULL) {
765 765 uintptr_t psz = sizeof (void *);
766 766 vnode_t *vp;
767 767 vnodeops_t *op;
768 768
769 769 /*
770 770 * When getf() returns a file_t, the enabling is implicitly
771 771 * granted the (transient) right to read the returned file_t
772 772 * as well as the v_path and v_op->vnop_name of the underlying
773 773 * vnode. These accesses are allowed after a successful
774 774 * getf() because the members that they refer to cannot change
775 775 * once set -- and the barrier logic in the kernel's closef()
776 776 * path assures that the file_t and its referenced vode_t
777 777 * cannot themselves be stale (that is, it impossible for
778 778 * either dtms_getf itself or its f_vnode member to reference
779 779 * freed memory).
780 780 */
781 781 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t)))
782 782 return (1);
783 783
784 784 if ((vp = fp->f_vnode) != NULL) {
785 785 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz))
786 786 return (1);
787 787
788 788 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz,
789 789 vp->v_path, strlen(vp->v_path) + 1)) {
790 790 return (1);
791 791 }
792 792
793 793 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz))
794 794 return (1);
795 795
796 796 if ((op = vp->v_op) != NULL &&
797 797 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) {
798 798 return (1);
799 799 }
800 800
801 801 if (op != NULL && op->vnop_name != NULL &&
802 802 DTRACE_INRANGE(addr, sz, op->vnop_name,
803 803 strlen(op->vnop_name) + 1)) {
804 804 return (1);
805 805 }
806 806 }
807 807 }
808 808
809 809 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
810 810 *illval = addr;
811 811 return (0);
812 812 }
813 813
814 814 /*
815 815 * Convenience routine to check to see if a given string is within a memory
816 816 * region in which a load may be issued given the user's privilege level;
817 817 * this exists so that we don't need to issue unnecessary dtrace_strlen()
818 818 * calls in the event that the user has all privileges.
819 819 */
820 820 static int
821 821 dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
822 822 dtrace_vstate_t *vstate)
823 823 {
824 824 size_t strsz;
825 825
826 826 /*
827 827 * If we hold the privilege to read from kernel memory, then
828 828 * everything is readable.
829 829 */
830 830 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
831 831 return (1);
832 832
833 833 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz);
834 834 if (dtrace_canload(addr, strsz, mstate, vstate))
835 835 return (1);
836 836
837 837 return (0);
838 838 }
839 839
840 840 /*
841 841 * Convenience routine to check to see if a given variable is within a memory
842 842 * region in which a load may be issued given the user's privilege level.
843 843 */
844 844 static int
845 845 dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate,
846 846 dtrace_vstate_t *vstate)
847 847 {
848 848 size_t sz, strsize;
849 849 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
850 850
851 851 /*
852 852 * If we hold the privilege to read from kernel memory, then
853 853 * everything is readable.
854 854 */
855 855 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
856 856 return (1);
857 857
858 858 if (type->dtdt_kind == DIF_TYPE_STRING) {
859 859 dtrace_state_t *state = vstate->dtvs_state;
860 860
861 861 if (state != NULL) {
862 862 strsize = state->dts_options[DTRACEOPT_STRSIZE];
863 863 } else {
864 864 /*
865 865 * In helper context, we have a NULL state; fall back
866 866 * to using the system-wide default for the string size
867 867 * in this case.
868 868 */
869 869 strsize = dtrace_strsize_default;
870 870 }
871 871
↓ open down ↓ |
871 lines elided |
↑ open up ↑ |
872 872 sz = dtrace_strlen(src, strsize) + 1;
873 873 } else {
874 874 sz = type->dtdt_size;
875 875 }
876 876
877 877 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate));
878 878 }
879 879
880 880 /*
881 881 * Convert a string to a signed integer using safe loads.
882 + *
883 + * NOTE: This function uses various macros from strtolctype.h to manipulate
884 + * digit values, etc -- these have all been checked to ensure they make
885 + * no additional function calls.
882 886 */
883 887 static int64_t
884 888 dtrace_strtoll(char *input, int base, size_t limit)
885 889 {
886 890 uintptr_t pos = (uintptr_t)input;
887 891 int64_t val = 0;
888 892 int x;
889 893 boolean_t neg = B_FALSE;
890 894 char c, cc, ccc;
891 895 uintptr_t end = pos + limit;
892 896
893 - /* eat whitespace */
897 + /*
898 + * Consume any whitespace preceding digits.
899 + */
894 900 while ((c = dtrace_load8(pos)) == ' ' || c == '\t')
895 901 pos++;
896 902
897 - /* sign? */
903 + /*
904 + * Handle an explicit sign if one is present.
905 + */
898 906 if (c == '-' || c == '+') {
899 907 if (c == '-')
900 908 neg = B_TRUE;
901 909 c = dtrace_load8(++pos);
902 910 }
903 911
904 - /* hex prefix? */
912 + /*
913 + * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
914 + * if present.
915 + */
905 916 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' ||
906 917 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) {
907 - pos += 2; /* skip over leading "0x" or "0X" */
918 + pos += 2;
908 919 c = ccc;
909 920 }
910 921
911 - /* read in digits */
922 + /*
923 + * Read in contiguous digits until the first non-digit character.
924 + */
912 925 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base;
913 926 c = dtrace_load8(++pos))
914 927 val = val * base + x;
915 928
916 929 return (neg ? -val : val);
917 930 }
918 931
919 932 /*
920 933 * Compare two strings using safe loads.
921 934 */
922 935 static int
923 936 dtrace_strncmp(char *s1, char *s2, size_t limit)
924 937 {
925 938 uint8_t c1, c2;
926 939 volatile uint16_t *flags;
927 940
928 941 if (s1 == s2 || limit == 0)
929 942 return (0);
930 943
931 944 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
932 945
933 946 do {
934 947 if (s1 == NULL) {
935 948 c1 = '\0';
936 949 } else {
937 950 c1 = dtrace_load8((uintptr_t)s1++);
938 951 }
939 952
940 953 if (s2 == NULL) {
941 954 c2 = '\0';
942 955 } else {
943 956 c2 = dtrace_load8((uintptr_t)s2++);
944 957 }
945 958
946 959 if (c1 != c2)
947 960 return (c1 - c2);
948 961 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
949 962
950 963 return (0);
951 964 }
952 965
953 966 /*
954 967 * Compute strlen(s) for a string using safe memory accesses. The additional
955 968 * len parameter is used to specify a maximum length to ensure completion.
956 969 */
957 970 static size_t
958 971 dtrace_strlen(const char *s, size_t lim)
959 972 {
960 973 uint_t len;
961 974
962 975 for (len = 0; len != lim; len++) {
963 976 if (dtrace_load8((uintptr_t)s++) == '\0')
964 977 break;
965 978 }
966 979
967 980 return (len);
968 981 }
969 982
970 983 /*
971 984 * Check if an address falls within a toxic region.
972 985 */
973 986 static int
974 987 dtrace_istoxic(uintptr_t kaddr, size_t size)
975 988 {
976 989 uintptr_t taddr, tsize;
977 990 int i;
978 991
979 992 for (i = 0; i < dtrace_toxranges; i++) {
980 993 taddr = dtrace_toxrange[i].dtt_base;
981 994 tsize = dtrace_toxrange[i].dtt_limit - taddr;
982 995
983 996 if (kaddr - taddr < tsize) {
984 997 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
985 998 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr;
986 999 return (1);
987 1000 }
988 1001
989 1002 if (taddr - kaddr < size) {
990 1003 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
991 1004 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr;
992 1005 return (1);
993 1006 }
994 1007 }
995 1008
996 1009 return (0);
997 1010 }
998 1011
999 1012 /*
1000 1013 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe
1001 1014 * memory specified by the DIF program. The dst is assumed to be safe memory
1002 1015 * that we can store to directly because it is managed by DTrace. As with
1003 1016 * standard bcopy, overlapping copies are handled properly.
1004 1017 */
1005 1018 static void
1006 1019 dtrace_bcopy(const void *src, void *dst, size_t len)
1007 1020 {
1008 1021 if (len != 0) {
1009 1022 uint8_t *s1 = dst;
1010 1023 const uint8_t *s2 = src;
1011 1024
1012 1025 if (s1 <= s2) {
1013 1026 do {
1014 1027 *s1++ = dtrace_load8((uintptr_t)s2++);
1015 1028 } while (--len != 0);
1016 1029 } else {
1017 1030 s2 += len;
1018 1031 s1 += len;
1019 1032
1020 1033 do {
1021 1034 *--s1 = dtrace_load8((uintptr_t)--s2);
1022 1035 } while (--len != 0);
1023 1036 }
1024 1037 }
1025 1038 }
1026 1039
1027 1040 /*
1028 1041 * Copy src to dst using safe memory accesses, up to either the specified
1029 1042 * length, or the point that a nul byte is encountered. The src is assumed to
1030 1043 * be unsafe memory specified by the DIF program. The dst is assumed to be
1031 1044 * safe memory that we can store to directly because it is managed by DTrace.
1032 1045 * Unlike dtrace_bcopy(), overlapping regions are not handled.
1033 1046 */
1034 1047 static void
1035 1048 dtrace_strcpy(const void *src, void *dst, size_t len)
1036 1049 {
1037 1050 if (len != 0) {
1038 1051 uint8_t *s1 = dst, c;
1039 1052 const uint8_t *s2 = src;
1040 1053
1041 1054 do {
1042 1055 *s1++ = c = dtrace_load8((uintptr_t)s2++);
1043 1056 } while (--len != 0 && c != '\0');
1044 1057 }
1045 1058 }
1046 1059
1047 1060 /*
1048 1061 * Copy src to dst, deriving the size and type from the specified (BYREF)
1049 1062 * variable type. The src is assumed to be unsafe memory specified by the DIF
1050 1063 * program. The dst is assumed to be DTrace variable memory that is of the
1051 1064 * specified type; we assume that we can store to directly.
1052 1065 */
1053 1066 static void
1054 1067 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type)
1055 1068 {
1056 1069 ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1057 1070
1058 1071 if (type->dtdt_kind == DIF_TYPE_STRING) {
1059 1072 dtrace_strcpy(src, dst, type->dtdt_size);
1060 1073 } else {
1061 1074 dtrace_bcopy(src, dst, type->dtdt_size);
1062 1075 }
1063 1076 }
1064 1077
1065 1078 /*
1066 1079 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be
1067 1080 * unsafe memory specified by the DIF program. The s2 data is assumed to be
1068 1081 * safe memory that we can access directly because it is managed by DTrace.
1069 1082 */
1070 1083 static int
1071 1084 dtrace_bcmp(const void *s1, const void *s2, size_t len)
1072 1085 {
1073 1086 volatile uint16_t *flags;
1074 1087
1075 1088 flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
1076 1089
1077 1090 if (s1 == s2)
1078 1091 return (0);
1079 1092
1080 1093 if (s1 == NULL || s2 == NULL)
1081 1094 return (1);
1082 1095
1083 1096 if (s1 != s2 && len != 0) {
1084 1097 const uint8_t *ps1 = s1;
1085 1098 const uint8_t *ps2 = s2;
1086 1099
1087 1100 do {
1088 1101 if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1089 1102 return (1);
1090 1103 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1091 1104 }
1092 1105 return (0);
1093 1106 }
1094 1107
1095 1108 /*
1096 1109 * Zero the specified region using a simple byte-by-byte loop. Note that this
1097 1110 * is for safe DTrace-managed memory only.
1098 1111 */
1099 1112 static void
1100 1113 dtrace_bzero(void *dst, size_t len)
1101 1114 {
1102 1115 uchar_t *cp;
1103 1116
1104 1117 for (cp = dst; len != 0; len--)
1105 1118 *cp++ = 0;
1106 1119 }
1107 1120
1108 1121 static void
1109 1122 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1110 1123 {
1111 1124 uint64_t result[2];
1112 1125
1113 1126 result[0] = addend1[0] + addend2[0];
1114 1127 result[1] = addend1[1] + addend2[1] +
1115 1128 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1116 1129
1117 1130 sum[0] = result[0];
1118 1131 sum[1] = result[1];
1119 1132 }
1120 1133
1121 1134 /*
1122 1135 * Shift the 128-bit value in a by b. If b is positive, shift left.
1123 1136 * If b is negative, shift right.
1124 1137 */
1125 1138 static void
1126 1139 dtrace_shift_128(uint64_t *a, int b)
1127 1140 {
1128 1141 uint64_t mask;
1129 1142
1130 1143 if (b == 0)
1131 1144 return;
1132 1145
1133 1146 if (b < 0) {
1134 1147 b = -b;
1135 1148 if (b >= 64) {
1136 1149 a[0] = a[1] >> (b - 64);
1137 1150 a[1] = 0;
1138 1151 } else {
1139 1152 a[0] >>= b;
1140 1153 mask = 1LL << (64 - b);
1141 1154 mask -= 1;
1142 1155 a[0] |= ((a[1] & mask) << (64 - b));
1143 1156 a[1] >>= b;
1144 1157 }
1145 1158 } else {
1146 1159 if (b >= 64) {
1147 1160 a[1] = a[0] << (b - 64);
1148 1161 a[0] = 0;
1149 1162 } else {
1150 1163 a[1] <<= b;
1151 1164 mask = a[0] >> (64 - b);
1152 1165 a[1] |= mask;
1153 1166 a[0] <<= b;
1154 1167 }
1155 1168 }
1156 1169 }
1157 1170
1158 1171 /*
1159 1172 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1160 1173 * use native multiplication on those, and then re-combine into the
1161 1174 * resulting 128-bit value.
1162 1175 *
1163 1176 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1164 1177 * hi1 * hi2 << 64 +
1165 1178 * hi1 * lo2 << 32 +
1166 1179 * hi2 * lo1 << 32 +
1167 1180 * lo1 * lo2
1168 1181 */
1169 1182 static void
1170 1183 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1171 1184 {
1172 1185 uint64_t hi1, hi2, lo1, lo2;
1173 1186 uint64_t tmp[2];
1174 1187
1175 1188 hi1 = factor1 >> 32;
1176 1189 hi2 = factor2 >> 32;
1177 1190
1178 1191 lo1 = factor1 & DT_MASK_LO;
1179 1192 lo2 = factor2 & DT_MASK_LO;
1180 1193
1181 1194 product[0] = lo1 * lo2;
1182 1195 product[1] = hi1 * hi2;
1183 1196
1184 1197 tmp[0] = hi1 * lo2;
1185 1198 tmp[1] = 0;
1186 1199 dtrace_shift_128(tmp, 32);
1187 1200 dtrace_add_128(product, tmp, product);
1188 1201
1189 1202 tmp[0] = hi2 * lo1;
1190 1203 tmp[1] = 0;
1191 1204 dtrace_shift_128(tmp, 32);
1192 1205 dtrace_add_128(product, tmp, product);
1193 1206 }
1194 1207
1195 1208 /*
1196 1209 * This privilege check should be used by actions and subroutines to
1197 1210 * verify that the user credentials of the process that enabled the
1198 1211 * invoking ECB match the target credentials
1199 1212 */
1200 1213 static int
1201 1214 dtrace_priv_proc_common_user(dtrace_state_t *state)
1202 1215 {
1203 1216 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1204 1217
1205 1218 /*
1206 1219 * We should always have a non-NULL state cred here, since if cred
1207 1220 * is null (anonymous tracing), we fast-path bypass this routine.
1208 1221 */
1209 1222 ASSERT(s_cr != NULL);
1210 1223
1211 1224 if ((cr = CRED()) != NULL &&
1212 1225 s_cr->cr_uid == cr->cr_uid &&
1213 1226 s_cr->cr_uid == cr->cr_ruid &&
1214 1227 s_cr->cr_uid == cr->cr_suid &&
1215 1228 s_cr->cr_gid == cr->cr_gid &&
1216 1229 s_cr->cr_gid == cr->cr_rgid &&
1217 1230 s_cr->cr_gid == cr->cr_sgid)
1218 1231 return (1);
1219 1232
1220 1233 return (0);
1221 1234 }
1222 1235
1223 1236 /*
1224 1237 * This privilege check should be used by actions and subroutines to
1225 1238 * verify that the zone of the process that enabled the invoking ECB
1226 1239 * matches the target credentials
1227 1240 */
1228 1241 static int
1229 1242 dtrace_priv_proc_common_zone(dtrace_state_t *state)
1230 1243 {
1231 1244 cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1232 1245
1233 1246 /*
1234 1247 * We should always have a non-NULL state cred here, since if cred
1235 1248 * is null (anonymous tracing), we fast-path bypass this routine.
1236 1249 */
1237 1250 ASSERT(s_cr != NULL);
1238 1251
1239 1252 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone)
1240 1253 return (1);
1241 1254
1242 1255 return (0);
1243 1256 }
1244 1257
1245 1258 /*
1246 1259 * This privilege check should be used by actions and subroutines to
1247 1260 * verify that the process has not setuid or changed credentials.
1248 1261 */
1249 1262 static int
1250 1263 dtrace_priv_proc_common_nocd()
1251 1264 {
1252 1265 proc_t *proc;
1253 1266
1254 1267 if ((proc = ttoproc(curthread)) != NULL &&
1255 1268 !(proc->p_flag & SNOCD))
1256 1269 return (1);
1257 1270
1258 1271 return (0);
1259 1272 }
1260 1273
1261 1274 static int
1262 1275 dtrace_priv_proc_destructive(dtrace_state_t *state, dtrace_mstate_t *mstate)
1263 1276 {
1264 1277 int action = state->dts_cred.dcr_action;
1265 1278
1266 1279 if (!(mstate->dtms_access & DTRACE_ACCESS_PROC))
1267 1280 goto bad;
1268 1281
1269 1282 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1270 1283 dtrace_priv_proc_common_zone(state) == 0)
1271 1284 goto bad;
1272 1285
1273 1286 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1274 1287 dtrace_priv_proc_common_user(state) == 0)
1275 1288 goto bad;
1276 1289
1277 1290 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1278 1291 dtrace_priv_proc_common_nocd() == 0)
1279 1292 goto bad;
1280 1293
1281 1294 return (1);
1282 1295
1283 1296 bad:
1284 1297 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1285 1298
1286 1299 return (0);
1287 1300 }
1288 1301
1289 1302 static int
1290 1303 dtrace_priv_proc_control(dtrace_state_t *state, dtrace_mstate_t *mstate)
1291 1304 {
1292 1305 if (mstate->dtms_access & DTRACE_ACCESS_PROC) {
1293 1306 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1294 1307 return (1);
1295 1308
1296 1309 if (dtrace_priv_proc_common_zone(state) &&
1297 1310 dtrace_priv_proc_common_user(state) &&
1298 1311 dtrace_priv_proc_common_nocd())
1299 1312 return (1);
1300 1313 }
1301 1314
1302 1315 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1303 1316
1304 1317 return (0);
1305 1318 }
1306 1319
1307 1320 static int
1308 1321 dtrace_priv_proc(dtrace_state_t *state, dtrace_mstate_t *mstate)
1309 1322 {
1310 1323 if ((mstate->dtms_access & DTRACE_ACCESS_PROC) &&
1311 1324 (state->dts_cred.dcr_action & DTRACE_CRA_PROC))
1312 1325 return (1);
1313 1326
1314 1327 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1315 1328
1316 1329 return (0);
1317 1330 }
1318 1331
1319 1332 static int
1320 1333 dtrace_priv_kernel(dtrace_state_t *state)
1321 1334 {
1322 1335 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1323 1336 return (1);
1324 1337
1325 1338 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1326 1339
1327 1340 return (0);
1328 1341 }
1329 1342
1330 1343 static int
1331 1344 dtrace_priv_kernel_destructive(dtrace_state_t *state)
1332 1345 {
1333 1346 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1334 1347 return (1);
1335 1348
1336 1349 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1337 1350
1338 1351 return (0);
1339 1352 }
1340 1353
1341 1354 /*
1342 1355 * Determine if the dte_cond of the specified ECB allows for processing of
1343 1356 * the current probe to continue. Note that this routine may allow continued
1344 1357 * processing, but with access(es) stripped from the mstate's dtms_access
1345 1358 * field.
1346 1359 */
1347 1360 static int
1348 1361 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate,
1349 1362 dtrace_ecb_t *ecb)
1350 1363 {
1351 1364 dtrace_probe_t *probe = ecb->dte_probe;
1352 1365 dtrace_provider_t *prov = probe->dtpr_provider;
1353 1366 dtrace_pops_t *pops = &prov->dtpv_pops;
1354 1367 int mode = DTRACE_MODE_NOPRIV_DROP;
1355 1368
1356 1369 ASSERT(ecb->dte_cond);
1357 1370
1358 1371 if (pops->dtps_mode != NULL) {
1359 1372 mode = pops->dtps_mode(prov->dtpv_arg,
1360 1373 probe->dtpr_id, probe->dtpr_arg);
1361 1374
1362 1375 ASSERT(mode & (DTRACE_MODE_USER | DTRACE_MODE_KERNEL));
1363 1376 ASSERT(mode & (DTRACE_MODE_NOPRIV_RESTRICT |
1364 1377 DTRACE_MODE_NOPRIV_DROP));
1365 1378 }
1366 1379
1367 1380 /*
1368 1381 * If the dte_cond bits indicate that this consumer is only allowed to
1369 1382 * see user-mode firings of this probe, check that the probe was fired
1370 1383 * while in a user context. If that's not the case, use the policy
1371 1384 * specified by the provider to determine if we drop the probe or
1372 1385 * merely restrict operation.
1373 1386 */
1374 1387 if (ecb->dte_cond & DTRACE_COND_USERMODE) {
1375 1388 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP);
1376 1389
1377 1390 if (!(mode & DTRACE_MODE_USER)) {
1378 1391 if (mode & DTRACE_MODE_NOPRIV_DROP)
1379 1392 return (0);
1380 1393
1381 1394 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
1382 1395 }
1383 1396 }
1384 1397
1385 1398 /*
1386 1399 * This is more subtle than it looks. We have to be absolutely certain
1387 1400 * that CRED() isn't going to change out from under us so it's only
1388 1401 * legit to examine that structure if we're in constrained situations.
1389 1402 * Currently, the only times we'll this check is if a non-super-user
1390 1403 * has enabled the profile or syscall providers -- providers that
1391 1404 * allow visibility of all processes. For the profile case, the check
1392 1405 * above will ensure that we're examining a user context.
1393 1406 */
1394 1407 if (ecb->dte_cond & DTRACE_COND_OWNER) {
1395 1408 cred_t *cr;
1396 1409 cred_t *s_cr = state->dts_cred.dcr_cred;
1397 1410 proc_t *proc;
1398 1411
1399 1412 ASSERT(s_cr != NULL);
1400 1413
1401 1414 if ((cr = CRED()) == NULL ||
1402 1415 s_cr->cr_uid != cr->cr_uid ||
1403 1416 s_cr->cr_uid != cr->cr_ruid ||
1404 1417 s_cr->cr_uid != cr->cr_suid ||
1405 1418 s_cr->cr_gid != cr->cr_gid ||
1406 1419 s_cr->cr_gid != cr->cr_rgid ||
1407 1420 s_cr->cr_gid != cr->cr_sgid ||
1408 1421 (proc = ttoproc(curthread)) == NULL ||
1409 1422 (proc->p_flag & SNOCD)) {
1410 1423 if (mode & DTRACE_MODE_NOPRIV_DROP)
1411 1424 return (0);
1412 1425
1413 1426 mstate->dtms_access &= ~DTRACE_ACCESS_PROC;
1414 1427 }
1415 1428 }
1416 1429
1417 1430 /*
1418 1431 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not
1419 1432 * in our zone, check to see if our mode policy is to restrict rather
1420 1433 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC
1421 1434 * and DTRACE_ACCESS_ARGS
1422 1435 */
1423 1436 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
1424 1437 cred_t *cr;
1425 1438 cred_t *s_cr = state->dts_cred.dcr_cred;
1426 1439
1427 1440 ASSERT(s_cr != NULL);
1428 1441
1429 1442 if ((cr = CRED()) == NULL ||
1430 1443 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) {
1431 1444 if (mode & DTRACE_MODE_NOPRIV_DROP)
1432 1445 return (0);
1433 1446
1434 1447 mstate->dtms_access &=
1435 1448 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS);
1436 1449 }
1437 1450 }
1438 1451
1439 1452 /*
1440 1453 * By merits of being in this code path at all, we have limited
1441 1454 * privileges. If the provider has indicated that limited privileges
1442 1455 * are to denote restricted operation, strip off the ability to access
1443 1456 * arguments.
1444 1457 */
1445 1458 if (mode & DTRACE_MODE_LIMITEDPRIV_RESTRICT)
1446 1459 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
1447 1460
1448 1461 return (1);
1449 1462 }
1450 1463
1451 1464 /*
1452 1465 * Note: not called from probe context. This function is called
1453 1466 * asynchronously (and at a regular interval) from outside of probe context to
1454 1467 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable
1455 1468 * cleaning is explained in detail in <sys/dtrace_impl.h>.
1456 1469 */
1457 1470 void
1458 1471 dtrace_dynvar_clean(dtrace_dstate_t *dstate)
1459 1472 {
1460 1473 dtrace_dynvar_t *dirty;
1461 1474 dtrace_dstate_percpu_t *dcpu;
1462 1475 dtrace_dynvar_t **rinsep;
1463 1476 int i, j, work = 0;
1464 1477
1465 1478 for (i = 0; i < NCPU; i++) {
1466 1479 dcpu = &dstate->dtds_percpu[i];
1467 1480 rinsep = &dcpu->dtdsc_rinsing;
1468 1481
1469 1482 /*
1470 1483 * If the dirty list is NULL, there is no dirty work to do.
1471 1484 */
1472 1485 if (dcpu->dtdsc_dirty == NULL)
1473 1486 continue;
1474 1487
1475 1488 if (dcpu->dtdsc_rinsing != NULL) {
1476 1489 /*
1477 1490 * If the rinsing list is non-NULL, then it is because
1478 1491 * this CPU was selected to accept another CPU's
1479 1492 * dirty list -- and since that time, dirty buffers
1480 1493 * have accumulated. This is a highly unlikely
1481 1494 * condition, but we choose to ignore the dirty
1482 1495 * buffers -- they'll be picked up a future cleanse.
1483 1496 */
1484 1497 continue;
1485 1498 }
1486 1499
1487 1500 if (dcpu->dtdsc_clean != NULL) {
1488 1501 /*
1489 1502 * If the clean list is non-NULL, then we're in a
1490 1503 * situation where a CPU has done deallocations (we
1491 1504 * have a non-NULL dirty list) but no allocations (we
1492 1505 * also have a non-NULL clean list). We can't simply
1493 1506 * move the dirty list into the clean list on this
1494 1507 * CPU, yet we also don't want to allow this condition
1495 1508 * to persist, lest a short clean list prevent a
1496 1509 * massive dirty list from being cleaned (which in
1497 1510 * turn could lead to otherwise avoidable dynamic
1498 1511 * drops). To deal with this, we look for some CPU
1499 1512 * with a NULL clean list, NULL dirty list, and NULL
1500 1513 * rinsing list -- and then we borrow this CPU to
1501 1514 * rinse our dirty list.
1502 1515 */
1503 1516 for (j = 0; j < NCPU; j++) {
1504 1517 dtrace_dstate_percpu_t *rinser;
1505 1518
1506 1519 rinser = &dstate->dtds_percpu[j];
1507 1520
1508 1521 if (rinser->dtdsc_rinsing != NULL)
1509 1522 continue;
1510 1523
1511 1524 if (rinser->dtdsc_dirty != NULL)
1512 1525 continue;
1513 1526
1514 1527 if (rinser->dtdsc_clean != NULL)
1515 1528 continue;
1516 1529
1517 1530 rinsep = &rinser->dtdsc_rinsing;
1518 1531 break;
1519 1532 }
1520 1533
1521 1534 if (j == NCPU) {
1522 1535 /*
1523 1536 * We were unable to find another CPU that
1524 1537 * could accept this dirty list -- we are
1525 1538 * therefore unable to clean it now.
1526 1539 */
1527 1540 dtrace_dynvar_failclean++;
1528 1541 continue;
1529 1542 }
1530 1543 }
1531 1544
1532 1545 work = 1;
1533 1546
1534 1547 /*
1535 1548 * Atomically move the dirty list aside.
1536 1549 */
1537 1550 do {
1538 1551 dirty = dcpu->dtdsc_dirty;
1539 1552
1540 1553 /*
1541 1554 * Before we zap the dirty list, set the rinsing list.
1542 1555 * (This allows for a potential assertion in
1543 1556 * dtrace_dynvar(): if a free dynamic variable appears
1544 1557 * on a hash chain, either the dirty list or the
1545 1558 * rinsing list for some CPU must be non-NULL.)
1546 1559 */
1547 1560 *rinsep = dirty;
1548 1561 dtrace_membar_producer();
1549 1562 } while (dtrace_casptr(&dcpu->dtdsc_dirty,
1550 1563 dirty, NULL) != dirty);
1551 1564 }
1552 1565
1553 1566 if (!work) {
1554 1567 /*
1555 1568 * We have no work to do; we can simply return.
1556 1569 */
1557 1570 return;
1558 1571 }
1559 1572
1560 1573 dtrace_sync();
1561 1574
1562 1575 for (i = 0; i < NCPU; i++) {
1563 1576 dcpu = &dstate->dtds_percpu[i];
1564 1577
1565 1578 if (dcpu->dtdsc_rinsing == NULL)
1566 1579 continue;
1567 1580
1568 1581 /*
1569 1582 * We are now guaranteed that no hash chain contains a pointer
1570 1583 * into this dirty list; we can make it clean.
1571 1584 */
1572 1585 ASSERT(dcpu->dtdsc_clean == NULL);
1573 1586 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1574 1587 dcpu->dtdsc_rinsing = NULL;
1575 1588 }
1576 1589
1577 1590 /*
1578 1591 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1579 1592 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1580 1593 * This prevents a race whereby a CPU incorrectly decides that
1581 1594 * the state should be something other than DTRACE_DSTATE_CLEAN
1582 1595 * after dtrace_dynvar_clean() has completed.
1583 1596 */
1584 1597 dtrace_sync();
1585 1598
1586 1599 dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1587 1600 }
1588 1601
1589 1602 /*
1590 1603 * Depending on the value of the op parameter, this function looks-up,
1591 1604 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an
1592 1605 * allocation is requested, this function will return a pointer to a
1593 1606 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1594 1607 * variable can be allocated. If NULL is returned, the appropriate counter
1595 1608 * will be incremented.
1596 1609 */
1597 1610 dtrace_dynvar_t *
1598 1611 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1599 1612 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1600 1613 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1601 1614 {
1602 1615 uint64_t hashval = DTRACE_DYNHASH_VALID;
1603 1616 dtrace_dynhash_t *hash = dstate->dtds_hash;
1604 1617 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1605 1618 processorid_t me = CPU->cpu_id, cpu = me;
1606 1619 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
1607 1620 size_t bucket, ksize;
1608 1621 size_t chunksize = dstate->dtds_chunksize;
1609 1622 uintptr_t kdata, lock, nstate;
1610 1623 uint_t i;
1611 1624
1612 1625 ASSERT(nkeys != 0);
1613 1626
1614 1627 /*
1615 1628 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time"
1616 1629 * algorithm. For the by-value portions, we perform the algorithm in
1617 1630 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a
1618 1631 * bit, and seems to have only a minute effect on distribution. For
1619 1632 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1620 1633 * over each referenced byte. It's painful to do this, but it's much
1621 1634 * better than pathological hash distribution. The efficacy of the
1622 1635 * hashing algorithm (and a comparison with other algorithms) may be
1623 1636 * found by running the ::dtrace_dynstat MDB dcmd.
1624 1637 */
1625 1638 for (i = 0; i < nkeys; i++) {
1626 1639 if (key[i].dttk_size == 0) {
1627 1640 uint64_t val = key[i].dttk_value;
1628 1641
1629 1642 hashval += (val >> 48) & 0xffff;
1630 1643 hashval += (hashval << 10);
1631 1644 hashval ^= (hashval >> 6);
1632 1645
1633 1646 hashval += (val >> 32) & 0xffff;
1634 1647 hashval += (hashval << 10);
1635 1648 hashval ^= (hashval >> 6);
1636 1649
1637 1650 hashval += (val >> 16) & 0xffff;
1638 1651 hashval += (hashval << 10);
1639 1652 hashval ^= (hashval >> 6);
1640 1653
1641 1654 hashval += val & 0xffff;
1642 1655 hashval += (hashval << 10);
1643 1656 hashval ^= (hashval >> 6);
1644 1657 } else {
1645 1658 /*
1646 1659 * This is incredibly painful, but it beats the hell
1647 1660 * out of the alternative.
1648 1661 */
1649 1662 uint64_t j, size = key[i].dttk_size;
1650 1663 uintptr_t base = (uintptr_t)key[i].dttk_value;
1651 1664
1652 1665 if (!dtrace_canload(base, size, mstate, vstate))
1653 1666 break;
1654 1667
1655 1668 for (j = 0; j < size; j++) {
1656 1669 hashval += dtrace_load8(base + j);
1657 1670 hashval += (hashval << 10);
1658 1671 hashval ^= (hashval >> 6);
1659 1672 }
1660 1673 }
1661 1674 }
1662 1675
1663 1676 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1664 1677 return (NULL);
1665 1678
1666 1679 hashval += (hashval << 3);
1667 1680 hashval ^= (hashval >> 11);
1668 1681 hashval += (hashval << 15);
1669 1682
1670 1683 /*
1671 1684 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1672 1685 * comes out to be one of our two sentinel hash values. If this
1673 1686 * actually happens, we set the hashval to be a value known to be a
1674 1687 * non-sentinel value.
1675 1688 */
1676 1689 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1677 1690 hashval = DTRACE_DYNHASH_VALID;
1678 1691
1679 1692 /*
1680 1693 * Yes, it's painful to do a divide here. If the cycle count becomes
1681 1694 * important here, tricks can be pulled to reduce it. (However, it's
1682 1695 * critical that hash collisions be kept to an absolute minimum;
1683 1696 * they're much more painful than a divide.) It's better to have a
1684 1697 * solution that generates few collisions and still keeps things
1685 1698 * relatively simple.
1686 1699 */
1687 1700 bucket = hashval % dstate->dtds_hashsize;
1688 1701
1689 1702 if (op == DTRACE_DYNVAR_DEALLOC) {
1690 1703 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1691 1704
1692 1705 for (;;) {
1693 1706 while ((lock = *lockp) & 1)
1694 1707 continue;
1695 1708
1696 1709 if (dtrace_casptr((void *)lockp,
1697 1710 (void *)lock, (void *)(lock + 1)) == (void *)lock)
1698 1711 break;
1699 1712 }
1700 1713
1701 1714 dtrace_membar_producer();
1702 1715 }
1703 1716
1704 1717 top:
1705 1718 prev = NULL;
1706 1719 lock = hash[bucket].dtdh_lock;
1707 1720
1708 1721 dtrace_membar_consumer();
1709 1722
1710 1723 start = hash[bucket].dtdh_chain;
1711 1724 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1712 1725 start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1713 1726 op != DTRACE_DYNVAR_DEALLOC));
1714 1727
1715 1728 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1716 1729 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1717 1730 dtrace_key_t *dkey = &dtuple->dtt_key[0];
1718 1731
1719 1732 if (dvar->dtdv_hashval != hashval) {
1720 1733 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1721 1734 /*
1722 1735 * We've reached the sink, and therefore the
1723 1736 * end of the hash chain; we can kick out of
1724 1737 * the loop knowing that we have seen a valid
1725 1738 * snapshot of state.
1726 1739 */
1727 1740 ASSERT(dvar->dtdv_next == NULL);
1728 1741 ASSERT(dvar == &dtrace_dynhash_sink);
1729 1742 break;
1730 1743 }
1731 1744
1732 1745 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
1733 1746 /*
1734 1747 * We've gone off the rails: somewhere along
1735 1748 * the line, one of the members of this hash
1736 1749 * chain was deleted. Note that we could also
1737 1750 * detect this by simply letting this loop run
1738 1751 * to completion, as we would eventually hit
1739 1752 * the end of the dirty list. However, we
1740 1753 * want to avoid running the length of the
1741 1754 * dirty list unnecessarily (it might be quite
1742 1755 * long), so we catch this as early as
1743 1756 * possible by detecting the hash marker. In
1744 1757 * this case, we simply set dvar to NULL and
1745 1758 * break; the conditional after the loop will
1746 1759 * send us back to top.
1747 1760 */
1748 1761 dvar = NULL;
1749 1762 break;
1750 1763 }
1751 1764
1752 1765 goto next;
1753 1766 }
1754 1767
1755 1768 if (dtuple->dtt_nkeys != nkeys)
1756 1769 goto next;
1757 1770
1758 1771 for (i = 0; i < nkeys; i++, dkey++) {
1759 1772 if (dkey->dttk_size != key[i].dttk_size)
1760 1773 goto next; /* size or type mismatch */
1761 1774
1762 1775 if (dkey->dttk_size != 0) {
1763 1776 if (dtrace_bcmp(
1764 1777 (void *)(uintptr_t)key[i].dttk_value,
1765 1778 (void *)(uintptr_t)dkey->dttk_value,
1766 1779 dkey->dttk_size))
1767 1780 goto next;
1768 1781 } else {
1769 1782 if (dkey->dttk_value != key[i].dttk_value)
1770 1783 goto next;
1771 1784 }
1772 1785 }
1773 1786
1774 1787 if (op != DTRACE_DYNVAR_DEALLOC)
1775 1788 return (dvar);
1776 1789
1777 1790 ASSERT(dvar->dtdv_next == NULL ||
1778 1791 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
1779 1792
1780 1793 if (prev != NULL) {
1781 1794 ASSERT(hash[bucket].dtdh_chain != dvar);
1782 1795 ASSERT(start != dvar);
1783 1796 ASSERT(prev->dtdv_next == dvar);
1784 1797 prev->dtdv_next = dvar->dtdv_next;
1785 1798 } else {
1786 1799 if (dtrace_casptr(&hash[bucket].dtdh_chain,
1787 1800 start, dvar->dtdv_next) != start) {
1788 1801 /*
1789 1802 * We have failed to atomically swing the
1790 1803 * hash table head pointer, presumably because
1791 1804 * of a conflicting allocation on another CPU.
1792 1805 * We need to reread the hash chain and try
1793 1806 * again.
1794 1807 */
1795 1808 goto top;
1796 1809 }
1797 1810 }
1798 1811
1799 1812 dtrace_membar_producer();
1800 1813
1801 1814 /*
1802 1815 * Now set the hash value to indicate that it's free.
1803 1816 */
1804 1817 ASSERT(hash[bucket].dtdh_chain != dvar);
1805 1818 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1806 1819
1807 1820 dtrace_membar_producer();
1808 1821
1809 1822 /*
1810 1823 * Set the next pointer to point at the dirty list, and
1811 1824 * atomically swing the dirty pointer to the newly freed dvar.
1812 1825 */
1813 1826 do {
1814 1827 next = dcpu->dtdsc_dirty;
1815 1828 dvar->dtdv_next = next;
1816 1829 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
1817 1830
1818 1831 /*
1819 1832 * Finally, unlock this hash bucket.
1820 1833 */
1821 1834 ASSERT(hash[bucket].dtdh_lock == lock);
1822 1835 ASSERT(lock & 1);
1823 1836 hash[bucket].dtdh_lock++;
1824 1837
1825 1838 return (NULL);
1826 1839 next:
1827 1840 prev = dvar;
1828 1841 continue;
1829 1842 }
1830 1843
1831 1844 if (dvar == NULL) {
1832 1845 /*
1833 1846 * If dvar is NULL, it is because we went off the rails:
1834 1847 * one of the elements that we traversed in the hash chain
1835 1848 * was deleted while we were traversing it. In this case,
1836 1849 * we assert that we aren't doing a dealloc (deallocs lock
1837 1850 * the hash bucket to prevent themselves from racing with
1838 1851 * one another), and retry the hash chain traversal.
1839 1852 */
1840 1853 ASSERT(op != DTRACE_DYNVAR_DEALLOC);
1841 1854 goto top;
1842 1855 }
1843 1856
1844 1857 if (op != DTRACE_DYNVAR_ALLOC) {
1845 1858 /*
1846 1859 * If we are not to allocate a new variable, we want to
1847 1860 * return NULL now. Before we return, check that the value
1848 1861 * of the lock word hasn't changed. If it has, we may have
1849 1862 * seen an inconsistent snapshot.
1850 1863 */
1851 1864 if (op == DTRACE_DYNVAR_NOALLOC) {
1852 1865 if (hash[bucket].dtdh_lock != lock)
1853 1866 goto top;
1854 1867 } else {
1855 1868 ASSERT(op == DTRACE_DYNVAR_DEALLOC);
1856 1869 ASSERT(hash[bucket].dtdh_lock == lock);
1857 1870 ASSERT(lock & 1);
1858 1871 hash[bucket].dtdh_lock++;
1859 1872 }
1860 1873
1861 1874 return (NULL);
1862 1875 }
1863 1876
1864 1877 /*
1865 1878 * We need to allocate a new dynamic variable. The size we need is the
1866 1879 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
1867 1880 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
1868 1881 * the size of any referred-to data (dsize). We then round the final
1869 1882 * size up to the chunksize for allocation.
1870 1883 */
1871 1884 for (ksize = 0, i = 0; i < nkeys; i++)
1872 1885 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
1873 1886
1874 1887 /*
1875 1888 * This should be pretty much impossible, but could happen if, say,
1876 1889 * strange DIF specified the tuple. Ideally, this should be an
1877 1890 * assertion and not an error condition -- but that requires that the
1878 1891 * chunksize calculation in dtrace_difo_chunksize() be absolutely
1879 1892 * bullet-proof. (That is, it must not be able to be fooled by
1880 1893 * malicious DIF.) Given the lack of backwards branches in DIF,
1881 1894 * solving this would presumably not amount to solving the Halting
1882 1895 * Problem -- but it still seems awfully hard.
1883 1896 */
1884 1897 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
1885 1898 ksize + dsize > chunksize) {
1886 1899 dcpu->dtdsc_drops++;
1887 1900 return (NULL);
1888 1901 }
1889 1902
1890 1903 nstate = DTRACE_DSTATE_EMPTY;
1891 1904
1892 1905 do {
1893 1906 retry:
1894 1907 free = dcpu->dtdsc_free;
1895 1908
1896 1909 if (free == NULL) {
1897 1910 dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
1898 1911 void *rval;
1899 1912
1900 1913 if (clean == NULL) {
1901 1914 /*
1902 1915 * We're out of dynamic variable space on
1903 1916 * this CPU. Unless we have tried all CPUs,
1904 1917 * we'll try to allocate from a different
1905 1918 * CPU.
1906 1919 */
1907 1920 switch (dstate->dtds_state) {
1908 1921 case DTRACE_DSTATE_CLEAN: {
1909 1922 void *sp = &dstate->dtds_state;
1910 1923
1911 1924 if (++cpu >= NCPU)
1912 1925 cpu = 0;
1913 1926
1914 1927 if (dcpu->dtdsc_dirty != NULL &&
1915 1928 nstate == DTRACE_DSTATE_EMPTY)
1916 1929 nstate = DTRACE_DSTATE_DIRTY;
1917 1930
1918 1931 if (dcpu->dtdsc_rinsing != NULL)
1919 1932 nstate = DTRACE_DSTATE_RINSING;
1920 1933
1921 1934 dcpu = &dstate->dtds_percpu[cpu];
1922 1935
1923 1936 if (cpu != me)
1924 1937 goto retry;
1925 1938
1926 1939 (void) dtrace_cas32(sp,
1927 1940 DTRACE_DSTATE_CLEAN, nstate);
1928 1941
1929 1942 /*
1930 1943 * To increment the correct bean
1931 1944 * counter, take another lap.
1932 1945 */
1933 1946 goto retry;
1934 1947 }
1935 1948
1936 1949 case DTRACE_DSTATE_DIRTY:
1937 1950 dcpu->dtdsc_dirty_drops++;
1938 1951 break;
1939 1952
1940 1953 case DTRACE_DSTATE_RINSING:
1941 1954 dcpu->dtdsc_rinsing_drops++;
1942 1955 break;
1943 1956
1944 1957 case DTRACE_DSTATE_EMPTY:
1945 1958 dcpu->dtdsc_drops++;
1946 1959 break;
1947 1960 }
1948 1961
1949 1962 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
1950 1963 return (NULL);
1951 1964 }
1952 1965
1953 1966 /*
1954 1967 * The clean list appears to be non-empty. We want to
1955 1968 * move the clean list to the free list; we start by
1956 1969 * moving the clean pointer aside.
1957 1970 */
1958 1971 if (dtrace_casptr(&dcpu->dtdsc_clean,
1959 1972 clean, NULL) != clean) {
1960 1973 /*
1961 1974 * We are in one of two situations:
1962 1975 *
1963 1976 * (a) The clean list was switched to the
1964 1977 * free list by another CPU.
1965 1978 *
1966 1979 * (b) The clean list was added to by the
1967 1980 * cleansing cyclic.
1968 1981 *
1969 1982 * In either of these situations, we can
1970 1983 * just reattempt the free list allocation.
1971 1984 */
1972 1985 goto retry;
1973 1986 }
1974 1987
1975 1988 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
1976 1989
1977 1990 /*
1978 1991 * Now we'll move the clean list to our free list.
1979 1992 * It's impossible for this to fail: the only way
1980 1993 * the free list can be updated is through this
1981 1994 * code path, and only one CPU can own the clean list.
1982 1995 * Thus, it would only be possible for this to fail if
1983 1996 * this code were racing with dtrace_dynvar_clean().
1984 1997 * (That is, if dtrace_dynvar_clean() updated the clean
1985 1998 * list, and we ended up racing to update the free
1986 1999 * list.) This race is prevented by the dtrace_sync()
1987 2000 * in dtrace_dynvar_clean() -- which flushes the
1988 2001 * owners of the clean lists out before resetting
1989 2002 * the clean lists.
1990 2003 */
1991 2004 dcpu = &dstate->dtds_percpu[me];
1992 2005 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
1993 2006 ASSERT(rval == NULL);
1994 2007 goto retry;
1995 2008 }
1996 2009
1997 2010 dvar = free;
1998 2011 new_free = dvar->dtdv_next;
1999 2012 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
2000 2013
2001 2014 /*
2002 2015 * We have now allocated a new chunk. We copy the tuple keys into the
2003 2016 * tuple array and copy any referenced key data into the data space
2004 2017 * following the tuple array. As we do this, we relocate dttk_value
2005 2018 * in the final tuple to point to the key data address in the chunk.
2006 2019 */
2007 2020 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
2008 2021 dvar->dtdv_data = (void *)(kdata + ksize);
2009 2022 dvar->dtdv_tuple.dtt_nkeys = nkeys;
2010 2023
2011 2024 for (i = 0; i < nkeys; i++) {
2012 2025 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
2013 2026 size_t kesize = key[i].dttk_size;
2014 2027
2015 2028 if (kesize != 0) {
2016 2029 dtrace_bcopy(
2017 2030 (const void *)(uintptr_t)key[i].dttk_value,
2018 2031 (void *)kdata, kesize);
2019 2032 dkey->dttk_value = kdata;
2020 2033 kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
2021 2034 } else {
2022 2035 dkey->dttk_value = key[i].dttk_value;
2023 2036 }
2024 2037
2025 2038 dkey->dttk_size = kesize;
2026 2039 }
2027 2040
2028 2041 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
2029 2042 dvar->dtdv_hashval = hashval;
2030 2043 dvar->dtdv_next = start;
2031 2044
2032 2045 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
2033 2046 return (dvar);
2034 2047
2035 2048 /*
2036 2049 * The cas has failed. Either another CPU is adding an element to
2037 2050 * this hash chain, or another CPU is deleting an element from this
2038 2051 * hash chain. The simplest way to deal with both of these cases
2039 2052 * (though not necessarily the most efficient) is to free our
2040 2053 * allocated block and tail-call ourselves. Note that the free is
2041 2054 * to the dirty list and _not_ to the free list. This is to prevent
2042 2055 * races with allocators, above.
2043 2056 */
2044 2057 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2045 2058
2046 2059 dtrace_membar_producer();
2047 2060
2048 2061 do {
2049 2062 free = dcpu->dtdsc_dirty;
2050 2063 dvar->dtdv_next = free;
2051 2064 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
2052 2065
2053 2066 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate));
2054 2067 }
2055 2068
2056 2069 /*ARGSUSED*/
2057 2070 static void
2058 2071 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
2059 2072 {
2060 2073 if ((int64_t)nval < (int64_t)*oval)
2061 2074 *oval = nval;
2062 2075 }
2063 2076
2064 2077 /*ARGSUSED*/
2065 2078 static void
2066 2079 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
2067 2080 {
2068 2081 if ((int64_t)nval > (int64_t)*oval)
2069 2082 *oval = nval;
2070 2083 }
2071 2084
2072 2085 static void
2073 2086 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
2074 2087 {
2075 2088 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
2076 2089 int64_t val = (int64_t)nval;
2077 2090
2078 2091 if (val < 0) {
2079 2092 for (i = 0; i < zero; i++) {
2080 2093 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
2081 2094 quanta[i] += incr;
2082 2095 return;
2083 2096 }
2084 2097 }
2085 2098 } else {
2086 2099 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
2087 2100 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
2088 2101 quanta[i - 1] += incr;
2089 2102 return;
2090 2103 }
2091 2104 }
2092 2105
2093 2106 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
2094 2107 return;
2095 2108 }
2096 2109
2097 2110 ASSERT(0);
2098 2111 }
2099 2112
2100 2113 static void
2101 2114 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
2102 2115 {
2103 2116 uint64_t arg = *lquanta++;
2104 2117 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2105 2118 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2106 2119 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
2107 2120 int32_t val = (int32_t)nval, level;
2108 2121
2109 2122 ASSERT(step != 0);
2110 2123 ASSERT(levels != 0);
2111 2124
2112 2125 if (val < base) {
2113 2126 /*
2114 2127 * This is an underflow.
2115 2128 */
2116 2129 lquanta[0] += incr;
2117 2130 return;
2118 2131 }
2119 2132
2120 2133 level = (val - base) / step;
2121 2134
2122 2135 if (level < levels) {
2123 2136 lquanta[level + 1] += incr;
2124 2137 return;
2125 2138 }
2126 2139
2127 2140 /*
2128 2141 * This is an overflow.
2129 2142 */
2130 2143 lquanta[levels + 1] += incr;
2131 2144 }
2132 2145
2133 2146 static int
2134 2147 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low,
2135 2148 uint16_t high, uint16_t nsteps, int64_t value)
2136 2149 {
2137 2150 int64_t this = 1, last, next;
2138 2151 int base = 1, order;
2139 2152
2140 2153 ASSERT(factor <= nsteps);
2141 2154 ASSERT(nsteps % factor == 0);
2142 2155
2143 2156 for (order = 0; order < low; order++)
2144 2157 this *= factor;
2145 2158
2146 2159 /*
2147 2160 * If our value is less than our factor taken to the power of the
2148 2161 * low order of magnitude, it goes into the zeroth bucket.
2149 2162 */
2150 2163 if (value < (last = this))
2151 2164 return (0);
2152 2165
2153 2166 for (this *= factor; order <= high; order++) {
2154 2167 int nbuckets = this > nsteps ? nsteps : this;
2155 2168
2156 2169 if ((next = this * factor) < this) {
2157 2170 /*
2158 2171 * We should not generally get log/linear quantizations
2159 2172 * with a high magnitude that allows 64-bits to
2160 2173 * overflow, but we nonetheless protect against this
2161 2174 * by explicitly checking for overflow, and clamping
2162 2175 * our value accordingly.
2163 2176 */
2164 2177 value = this - 1;
2165 2178 }
2166 2179
2167 2180 if (value < this) {
2168 2181 /*
2169 2182 * If our value lies within this order of magnitude,
2170 2183 * determine its position by taking the offset within
2171 2184 * the order of magnitude, dividing by the bucket
2172 2185 * width, and adding to our (accumulated) base.
2173 2186 */
2174 2187 return (base + (value - last) / (this / nbuckets));
2175 2188 }
2176 2189
2177 2190 base += nbuckets - (nbuckets / factor);
2178 2191 last = this;
2179 2192 this = next;
2180 2193 }
2181 2194
2182 2195 /*
2183 2196 * Our value is greater than or equal to our factor taken to the
2184 2197 * power of one plus the high magnitude -- return the top bucket.
2185 2198 */
2186 2199 return (base);
2187 2200 }
2188 2201
2189 2202 static void
2190 2203 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
2191 2204 {
2192 2205 uint64_t arg = *llquanta++;
2193 2206 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
2194 2207 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
2195 2208 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
2196 2209 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
2197 2210
2198 2211 llquanta[dtrace_aggregate_llquantize_bucket(factor,
2199 2212 low, high, nsteps, nval)] += incr;
2200 2213 }
2201 2214
2202 2215 /*ARGSUSED*/
2203 2216 static void
2204 2217 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
2205 2218 {
2206 2219 data[0]++;
2207 2220 data[1] += nval;
2208 2221 }
2209 2222
2210 2223 /*ARGSUSED*/
2211 2224 static void
2212 2225 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2213 2226 {
2214 2227 int64_t snval = (int64_t)nval;
2215 2228 uint64_t tmp[2];
2216 2229
2217 2230 data[0]++;
2218 2231 data[1] += nval;
2219 2232
2220 2233 /*
2221 2234 * What we want to say here is:
2222 2235 *
2223 2236 * data[2] += nval * nval;
2224 2237 *
2225 2238 * But given that nval is 64-bit, we could easily overflow, so
2226 2239 * we do this as 128-bit arithmetic.
2227 2240 */
2228 2241 if (snval < 0)
2229 2242 snval = -snval;
2230 2243
2231 2244 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2232 2245 dtrace_add_128(data + 2, tmp, data + 2);
2233 2246 }
2234 2247
2235 2248 /*ARGSUSED*/
2236 2249 static void
2237 2250 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2238 2251 {
2239 2252 *oval = *oval + 1;
2240 2253 }
2241 2254
2242 2255 /*ARGSUSED*/
2243 2256 static void
2244 2257 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2245 2258 {
2246 2259 *oval += nval;
2247 2260 }
2248 2261
2249 2262 /*
2250 2263 * Aggregate given the tuple in the principal data buffer, and the aggregating
2251 2264 * action denoted by the specified dtrace_aggregation_t. The aggregation
2252 2265 * buffer is specified as the buf parameter. This routine does not return
2253 2266 * failure; if there is no space in the aggregation buffer, the data will be
2254 2267 * dropped, and a corresponding counter incremented.
2255 2268 */
2256 2269 static void
2257 2270 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2258 2271 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2259 2272 {
2260 2273 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2261 2274 uint32_t i, ndx, size, fsize;
2262 2275 uint32_t align = sizeof (uint64_t) - 1;
2263 2276 dtrace_aggbuffer_t *agb;
2264 2277 dtrace_aggkey_t *key;
2265 2278 uint32_t hashval = 0, limit, isstr;
2266 2279 caddr_t tomax, data, kdata;
2267 2280 dtrace_actkind_t action;
2268 2281 dtrace_action_t *act;
2269 2282 uintptr_t offs;
2270 2283
2271 2284 if (buf == NULL)
2272 2285 return;
2273 2286
2274 2287 if (!agg->dtag_hasarg) {
2275 2288 /*
2276 2289 * Currently, only quantize() and lquantize() take additional
2277 2290 * arguments, and they have the same semantics: an increment
2278 2291 * value that defaults to 1 when not present. If additional
2279 2292 * aggregating actions take arguments, the setting of the
2280 2293 * default argument value will presumably have to become more
2281 2294 * sophisticated...
2282 2295 */
2283 2296 arg = 1;
2284 2297 }
2285 2298
2286 2299 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2287 2300 size = rec->dtrd_offset - agg->dtag_base;
2288 2301 fsize = size + rec->dtrd_size;
2289 2302
2290 2303 ASSERT(dbuf->dtb_tomax != NULL);
2291 2304 data = dbuf->dtb_tomax + offset + agg->dtag_base;
2292 2305
2293 2306 if ((tomax = buf->dtb_tomax) == NULL) {
2294 2307 dtrace_buffer_drop(buf);
2295 2308 return;
2296 2309 }
2297 2310
2298 2311 /*
2299 2312 * The metastructure is always at the bottom of the buffer.
2300 2313 */
2301 2314 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2302 2315 sizeof (dtrace_aggbuffer_t));
2303 2316
2304 2317 if (buf->dtb_offset == 0) {
2305 2318 /*
2306 2319 * We just kludge up approximately 1/8th of the size to be
2307 2320 * buckets. If this guess ends up being routinely
2308 2321 * off-the-mark, we may need to dynamically readjust this
2309 2322 * based on past performance.
2310 2323 */
2311 2324 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2312 2325
2313 2326 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2314 2327 (uintptr_t)tomax || hashsize == 0) {
2315 2328 /*
2316 2329 * We've been given a ludicrously small buffer;
2317 2330 * increment our drop count and leave.
2318 2331 */
2319 2332 dtrace_buffer_drop(buf);
2320 2333 return;
2321 2334 }
2322 2335
2323 2336 /*
2324 2337 * And now, a pathetic attempt to try to get a an odd (or
2325 2338 * perchance, a prime) hash size for better hash distribution.
2326 2339 */
2327 2340 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2328 2341 hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2329 2342
2330 2343 agb->dtagb_hashsize = hashsize;
2331 2344 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2332 2345 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2333 2346 agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2334 2347
2335 2348 for (i = 0; i < agb->dtagb_hashsize; i++)
2336 2349 agb->dtagb_hash[i] = NULL;
2337 2350 }
2338 2351
2339 2352 ASSERT(agg->dtag_first != NULL);
2340 2353 ASSERT(agg->dtag_first->dta_intuple);
2341 2354
2342 2355 /*
2343 2356 * Calculate the hash value based on the key. Note that we _don't_
2344 2357 * include the aggid in the hashing (but we will store it as part of
2345 2358 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time"
2346 2359 * algorithm: a simple, quick algorithm that has no known funnels, and
2347 2360 * gets good distribution in practice. The efficacy of the hashing
2348 2361 * algorithm (and a comparison with other algorithms) may be found by
2349 2362 * running the ::dtrace_aggstat MDB dcmd.
2350 2363 */
2351 2364 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2352 2365 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2353 2366 limit = i + act->dta_rec.dtrd_size;
2354 2367 ASSERT(limit <= size);
2355 2368 isstr = DTRACEACT_ISSTRING(act);
2356 2369
2357 2370 for (; i < limit; i++) {
2358 2371 hashval += data[i];
2359 2372 hashval += (hashval << 10);
2360 2373 hashval ^= (hashval >> 6);
2361 2374
2362 2375 if (isstr && data[i] == '\0')
2363 2376 break;
2364 2377 }
2365 2378 }
2366 2379
2367 2380 hashval += (hashval << 3);
2368 2381 hashval ^= (hashval >> 11);
2369 2382 hashval += (hashval << 15);
2370 2383
2371 2384 /*
2372 2385 * Yes, the divide here is expensive -- but it's generally the least
2373 2386 * of the performance issues given the amount of data that we iterate
2374 2387 * over to compute hash values, compare data, etc.
2375 2388 */
2376 2389 ndx = hashval % agb->dtagb_hashsize;
2377 2390
2378 2391 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2379 2392 ASSERT((caddr_t)key >= tomax);
2380 2393 ASSERT((caddr_t)key < tomax + buf->dtb_size);
2381 2394
2382 2395 if (hashval != key->dtak_hashval || key->dtak_size != size)
2383 2396 continue;
2384 2397
2385 2398 kdata = key->dtak_data;
2386 2399 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2387 2400
2388 2401 for (act = agg->dtag_first; act->dta_intuple;
2389 2402 act = act->dta_next) {
2390 2403 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2391 2404 limit = i + act->dta_rec.dtrd_size;
2392 2405 ASSERT(limit <= size);
2393 2406 isstr = DTRACEACT_ISSTRING(act);
2394 2407
2395 2408 for (; i < limit; i++) {
2396 2409 if (kdata[i] != data[i])
2397 2410 goto next;
2398 2411
2399 2412 if (isstr && data[i] == '\0')
2400 2413 break;
2401 2414 }
2402 2415 }
2403 2416
2404 2417 if (action != key->dtak_action) {
2405 2418 /*
2406 2419 * We are aggregating on the same value in the same
2407 2420 * aggregation with two different aggregating actions.
2408 2421 * (This should have been picked up in the compiler,
2409 2422 * so we may be dealing with errant or devious DIF.)
2410 2423 * This is an error condition; we indicate as much,
2411 2424 * and return.
2412 2425 */
2413 2426 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2414 2427 return;
2415 2428 }
2416 2429
2417 2430 /*
2418 2431 * This is a hit: we need to apply the aggregator to
2419 2432 * the value at this key.
2420 2433 */
2421 2434 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2422 2435 return;
2423 2436 next:
2424 2437 continue;
2425 2438 }
2426 2439
2427 2440 /*
2428 2441 * We didn't find it. We need to allocate some zero-filled space,
2429 2442 * link it into the hash table appropriately, and apply the aggregator
2430 2443 * to the (zero-filled) value.
2431 2444 */
2432 2445 offs = buf->dtb_offset;
2433 2446 while (offs & (align - 1))
2434 2447 offs += sizeof (uint32_t);
2435 2448
2436 2449 /*
2437 2450 * If we don't have enough room to both allocate a new key _and_
2438 2451 * its associated data, increment the drop count and return.
2439 2452 */
2440 2453 if ((uintptr_t)tomax + offs + fsize >
2441 2454 agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2442 2455 dtrace_buffer_drop(buf);
2443 2456 return;
2444 2457 }
2445 2458
2446 2459 /*CONSTCOND*/
2447 2460 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2448 2461 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2449 2462 agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2450 2463
2451 2464 key->dtak_data = kdata = tomax + offs;
2452 2465 buf->dtb_offset = offs + fsize;
2453 2466
2454 2467 /*
2455 2468 * Now copy the data across.
2456 2469 */
2457 2470 *((dtrace_aggid_t *)kdata) = agg->dtag_id;
2458 2471
2459 2472 for (i = sizeof (dtrace_aggid_t); i < size; i++)
2460 2473 kdata[i] = data[i];
2461 2474
2462 2475 /*
2463 2476 * Because strings are not zeroed out by default, we need to iterate
2464 2477 * looking for actions that store strings, and we need to explicitly
2465 2478 * pad these strings out with zeroes.
2466 2479 */
2467 2480 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2468 2481 int nul;
2469 2482
2470 2483 if (!DTRACEACT_ISSTRING(act))
2471 2484 continue;
2472 2485
2473 2486 i = act->dta_rec.dtrd_offset - agg->dtag_base;
2474 2487 limit = i + act->dta_rec.dtrd_size;
2475 2488 ASSERT(limit <= size);
2476 2489
2477 2490 for (nul = 0; i < limit; i++) {
2478 2491 if (nul) {
2479 2492 kdata[i] = '\0';
2480 2493 continue;
2481 2494 }
2482 2495
2483 2496 if (data[i] != '\0')
2484 2497 continue;
2485 2498
2486 2499 nul = 1;
2487 2500 }
2488 2501 }
2489 2502
2490 2503 for (i = size; i < fsize; i++)
2491 2504 kdata[i] = 0;
2492 2505
2493 2506 key->dtak_hashval = hashval;
2494 2507 key->dtak_size = size;
2495 2508 key->dtak_action = action;
2496 2509 key->dtak_next = agb->dtagb_hash[ndx];
2497 2510 agb->dtagb_hash[ndx] = key;
2498 2511
2499 2512 /*
2500 2513 * Finally, apply the aggregator.
2501 2514 */
2502 2515 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2503 2516 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2504 2517 }
2505 2518
2506 2519 /*
2507 2520 * Given consumer state, this routine finds a speculation in the INACTIVE
2508 2521 * state and transitions it into the ACTIVE state. If there is no speculation
2509 2522 * in the INACTIVE state, 0 is returned. In this case, no error counter is
2510 2523 * incremented -- it is up to the caller to take appropriate action.
2511 2524 */
2512 2525 static int
2513 2526 dtrace_speculation(dtrace_state_t *state)
2514 2527 {
2515 2528 int i = 0;
2516 2529 dtrace_speculation_state_t current;
2517 2530 uint32_t *stat = &state->dts_speculations_unavail, count;
2518 2531
2519 2532 while (i < state->dts_nspeculations) {
2520 2533 dtrace_speculation_t *spec = &state->dts_speculations[i];
2521 2534
2522 2535 current = spec->dtsp_state;
2523 2536
2524 2537 if (current != DTRACESPEC_INACTIVE) {
2525 2538 if (current == DTRACESPEC_COMMITTINGMANY ||
2526 2539 current == DTRACESPEC_COMMITTING ||
2527 2540 current == DTRACESPEC_DISCARDING)
2528 2541 stat = &state->dts_speculations_busy;
2529 2542 i++;
2530 2543 continue;
2531 2544 }
2532 2545
2533 2546 if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2534 2547 current, DTRACESPEC_ACTIVE) == current)
2535 2548 return (i + 1);
2536 2549 }
2537 2550
2538 2551 /*
2539 2552 * We couldn't find a speculation. If we found as much as a single
2540 2553 * busy speculation buffer, we'll attribute this failure as "busy"
2541 2554 * instead of "unavail".
2542 2555 */
2543 2556 do {
2544 2557 count = *stat;
2545 2558 } while (dtrace_cas32(stat, count, count + 1) != count);
2546 2559
2547 2560 return (0);
2548 2561 }
2549 2562
2550 2563 /*
2551 2564 * This routine commits an active speculation. If the specified speculation
2552 2565 * is not in a valid state to perform a commit(), this routine will silently do
2553 2566 * nothing. The state of the specified speculation is transitioned according
2554 2567 * to the state transition diagram outlined in <sys/dtrace_impl.h>
2555 2568 */
2556 2569 static void
2557 2570 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2558 2571 dtrace_specid_t which)
2559 2572 {
2560 2573 dtrace_speculation_t *spec;
2561 2574 dtrace_buffer_t *src, *dest;
2562 2575 uintptr_t daddr, saddr, dlimit, slimit;
2563 2576 dtrace_speculation_state_t current, new;
2564 2577 intptr_t offs;
2565 2578 uint64_t timestamp;
2566 2579
2567 2580 if (which == 0)
2568 2581 return;
2569 2582
2570 2583 if (which > state->dts_nspeculations) {
2571 2584 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2572 2585 return;
2573 2586 }
2574 2587
2575 2588 spec = &state->dts_speculations[which - 1];
2576 2589 src = &spec->dtsp_buffer[cpu];
2577 2590 dest = &state->dts_buffer[cpu];
2578 2591
2579 2592 do {
2580 2593 current = spec->dtsp_state;
2581 2594
2582 2595 if (current == DTRACESPEC_COMMITTINGMANY)
2583 2596 break;
2584 2597
2585 2598 switch (current) {
2586 2599 case DTRACESPEC_INACTIVE:
2587 2600 case DTRACESPEC_DISCARDING:
2588 2601 return;
2589 2602
2590 2603 case DTRACESPEC_COMMITTING:
2591 2604 /*
2592 2605 * This is only possible if we are (a) commit()'ing
2593 2606 * without having done a prior speculate() on this CPU
2594 2607 * and (b) racing with another commit() on a different
2595 2608 * CPU. There's nothing to do -- we just assert that
2596 2609 * our offset is 0.
2597 2610 */
2598 2611 ASSERT(src->dtb_offset == 0);
2599 2612 return;
2600 2613
2601 2614 case DTRACESPEC_ACTIVE:
2602 2615 new = DTRACESPEC_COMMITTING;
2603 2616 break;
2604 2617
2605 2618 case DTRACESPEC_ACTIVEONE:
2606 2619 /*
2607 2620 * This speculation is active on one CPU. If our
2608 2621 * buffer offset is non-zero, we know that the one CPU
2609 2622 * must be us. Otherwise, we are committing on a
2610 2623 * different CPU from the speculate(), and we must
2611 2624 * rely on being asynchronously cleaned.
2612 2625 */
2613 2626 if (src->dtb_offset != 0) {
2614 2627 new = DTRACESPEC_COMMITTING;
2615 2628 break;
2616 2629 }
2617 2630 /*FALLTHROUGH*/
2618 2631
2619 2632 case DTRACESPEC_ACTIVEMANY:
2620 2633 new = DTRACESPEC_COMMITTINGMANY;
2621 2634 break;
2622 2635
2623 2636 default:
2624 2637 ASSERT(0);
2625 2638 }
2626 2639 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2627 2640 current, new) != current);
2628 2641
2629 2642 /*
2630 2643 * We have set the state to indicate that we are committing this
2631 2644 * speculation. Now reserve the necessary space in the destination
2632 2645 * buffer.
2633 2646 */
2634 2647 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2635 2648 sizeof (uint64_t), state, NULL)) < 0) {
2636 2649 dtrace_buffer_drop(dest);
2637 2650 goto out;
2638 2651 }
2639 2652
2640 2653 /*
2641 2654 * We have sufficient space to copy the speculative buffer into the
2642 2655 * primary buffer. First, modify the speculative buffer, filling
2643 2656 * in the timestamp of all entries with the current time. The data
2644 2657 * must have the commit() time rather than the time it was traced,
2645 2658 * so that all entries in the primary buffer are in timestamp order.
2646 2659 */
2647 2660 timestamp = dtrace_gethrtime();
2648 2661 saddr = (uintptr_t)src->dtb_tomax;
2649 2662 slimit = saddr + src->dtb_offset;
2650 2663 while (saddr < slimit) {
2651 2664 size_t size;
2652 2665 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
2653 2666
2654 2667 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2655 2668 saddr += sizeof (dtrace_epid_t);
2656 2669 continue;
2657 2670 }
2658 2671 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs);
2659 2672 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
2660 2673
2661 2674 ASSERT3U(saddr + size, <=, slimit);
2662 2675 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t));
2663 2676 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX);
2664 2677
2665 2678 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
2666 2679
2667 2680 saddr += size;
2668 2681 }
2669 2682
2670 2683 /*
2671 2684 * Copy the buffer across. (Note that this is a
2672 2685 * highly subobtimal bcopy(); in the unlikely event that this becomes
2673 2686 * a serious performance issue, a high-performance DTrace-specific
2674 2687 * bcopy() should obviously be invented.)
2675 2688 */
2676 2689 daddr = (uintptr_t)dest->dtb_tomax + offs;
2677 2690 dlimit = daddr + src->dtb_offset;
2678 2691 saddr = (uintptr_t)src->dtb_tomax;
2679 2692
2680 2693 /*
2681 2694 * First, the aligned portion.
2682 2695 */
2683 2696 while (dlimit - daddr >= sizeof (uint64_t)) {
2684 2697 *((uint64_t *)daddr) = *((uint64_t *)saddr);
2685 2698
2686 2699 daddr += sizeof (uint64_t);
2687 2700 saddr += sizeof (uint64_t);
2688 2701 }
2689 2702
2690 2703 /*
2691 2704 * Now any left-over bit...
2692 2705 */
2693 2706 while (dlimit - daddr)
2694 2707 *((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2695 2708
2696 2709 /*
2697 2710 * Finally, commit the reserved space in the destination buffer.
2698 2711 */
2699 2712 dest->dtb_offset = offs + src->dtb_offset;
2700 2713
2701 2714 out:
2702 2715 /*
2703 2716 * If we're lucky enough to be the only active CPU on this speculation
2704 2717 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2705 2718 */
2706 2719 if (current == DTRACESPEC_ACTIVE ||
2707 2720 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2708 2721 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2709 2722 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2710 2723
2711 2724 ASSERT(rval == DTRACESPEC_COMMITTING);
2712 2725 }
2713 2726
2714 2727 src->dtb_offset = 0;
2715 2728 src->dtb_xamot_drops += src->dtb_drops;
2716 2729 src->dtb_drops = 0;
2717 2730 }
2718 2731
2719 2732 /*
2720 2733 * This routine discards an active speculation. If the specified speculation
2721 2734 * is not in a valid state to perform a discard(), this routine will silently
2722 2735 * do nothing. The state of the specified speculation is transitioned
2723 2736 * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2724 2737 */
2725 2738 static void
2726 2739 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
2727 2740 dtrace_specid_t which)
2728 2741 {
2729 2742 dtrace_speculation_t *spec;
2730 2743 dtrace_speculation_state_t current, new;
2731 2744 dtrace_buffer_t *buf;
2732 2745
2733 2746 if (which == 0)
2734 2747 return;
2735 2748
2736 2749 if (which > state->dts_nspeculations) {
2737 2750 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2738 2751 return;
2739 2752 }
2740 2753
2741 2754 spec = &state->dts_speculations[which - 1];
2742 2755 buf = &spec->dtsp_buffer[cpu];
2743 2756
2744 2757 do {
2745 2758 current = spec->dtsp_state;
2746 2759
2747 2760 switch (current) {
2748 2761 case DTRACESPEC_INACTIVE:
2749 2762 case DTRACESPEC_COMMITTINGMANY:
2750 2763 case DTRACESPEC_COMMITTING:
2751 2764 case DTRACESPEC_DISCARDING:
2752 2765 return;
2753 2766
2754 2767 case DTRACESPEC_ACTIVE:
2755 2768 case DTRACESPEC_ACTIVEMANY:
2756 2769 new = DTRACESPEC_DISCARDING;
2757 2770 break;
2758 2771
2759 2772 case DTRACESPEC_ACTIVEONE:
2760 2773 if (buf->dtb_offset != 0) {
2761 2774 new = DTRACESPEC_INACTIVE;
2762 2775 } else {
2763 2776 new = DTRACESPEC_DISCARDING;
2764 2777 }
2765 2778 break;
2766 2779
2767 2780 default:
2768 2781 ASSERT(0);
2769 2782 }
2770 2783 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2771 2784 current, new) != current);
2772 2785
2773 2786 buf->dtb_offset = 0;
2774 2787 buf->dtb_drops = 0;
2775 2788 }
2776 2789
2777 2790 /*
2778 2791 * Note: not called from probe context. This function is called
2779 2792 * asynchronously from cross call context to clean any speculations that are
2780 2793 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be
2781 2794 * transitioned back to the INACTIVE state until all CPUs have cleaned the
2782 2795 * speculation.
2783 2796 */
2784 2797 static void
2785 2798 dtrace_speculation_clean_here(dtrace_state_t *state)
2786 2799 {
2787 2800 dtrace_icookie_t cookie;
2788 2801 processorid_t cpu = CPU->cpu_id;
2789 2802 dtrace_buffer_t *dest = &state->dts_buffer[cpu];
2790 2803 dtrace_specid_t i;
2791 2804
2792 2805 cookie = dtrace_interrupt_disable();
2793 2806
2794 2807 if (dest->dtb_tomax == NULL) {
2795 2808 dtrace_interrupt_enable(cookie);
2796 2809 return;
2797 2810 }
2798 2811
2799 2812 for (i = 0; i < state->dts_nspeculations; i++) {
2800 2813 dtrace_speculation_t *spec = &state->dts_speculations[i];
2801 2814 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
2802 2815
2803 2816 if (src->dtb_tomax == NULL)
2804 2817 continue;
2805 2818
2806 2819 if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
2807 2820 src->dtb_offset = 0;
2808 2821 continue;
2809 2822 }
2810 2823
2811 2824 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2812 2825 continue;
2813 2826
2814 2827 if (src->dtb_offset == 0)
2815 2828 continue;
2816 2829
2817 2830 dtrace_speculation_commit(state, cpu, i + 1);
2818 2831 }
2819 2832
2820 2833 dtrace_interrupt_enable(cookie);
2821 2834 }
2822 2835
2823 2836 /*
2824 2837 * Note: not called from probe context. This function is called
2825 2838 * asynchronously (and at a regular interval) to clean any speculations that
2826 2839 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there
2827 2840 * is work to be done, it cross calls all CPUs to perform that work;
2828 2841 * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2829 2842 * INACTIVE state until they have been cleaned by all CPUs.
2830 2843 */
2831 2844 static void
2832 2845 dtrace_speculation_clean(dtrace_state_t *state)
2833 2846 {
2834 2847 int work = 0, rv;
2835 2848 dtrace_specid_t i;
2836 2849
2837 2850 for (i = 0; i < state->dts_nspeculations; i++) {
2838 2851 dtrace_speculation_t *spec = &state->dts_speculations[i];
2839 2852
2840 2853 ASSERT(!spec->dtsp_cleaning);
2841 2854
2842 2855 if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
2843 2856 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2844 2857 continue;
2845 2858
2846 2859 work++;
2847 2860 spec->dtsp_cleaning = 1;
2848 2861 }
2849 2862
2850 2863 if (!work)
2851 2864 return;
2852 2865
2853 2866 dtrace_xcall(DTRACE_CPUALL,
2854 2867 (dtrace_xcall_t)dtrace_speculation_clean_here, state);
2855 2868
2856 2869 /*
2857 2870 * We now know that all CPUs have committed or discarded their
2858 2871 * speculation buffers, as appropriate. We can now set the state
2859 2872 * to inactive.
2860 2873 */
2861 2874 for (i = 0; i < state->dts_nspeculations; i++) {
2862 2875 dtrace_speculation_t *spec = &state->dts_speculations[i];
2863 2876 dtrace_speculation_state_t current, new;
2864 2877
2865 2878 if (!spec->dtsp_cleaning)
2866 2879 continue;
2867 2880
2868 2881 current = spec->dtsp_state;
2869 2882 ASSERT(current == DTRACESPEC_DISCARDING ||
2870 2883 current == DTRACESPEC_COMMITTINGMANY);
2871 2884
2872 2885 new = DTRACESPEC_INACTIVE;
2873 2886
2874 2887 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
2875 2888 ASSERT(rv == current);
2876 2889 spec->dtsp_cleaning = 0;
2877 2890 }
2878 2891 }
2879 2892
2880 2893 /*
2881 2894 * Called as part of a speculate() to get the speculative buffer associated
2882 2895 * with a given speculation. Returns NULL if the specified speculation is not
2883 2896 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and
2884 2897 * the active CPU is not the specified CPU -- the speculation will be
2885 2898 * atomically transitioned into the ACTIVEMANY state.
2886 2899 */
2887 2900 static dtrace_buffer_t *
2888 2901 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
2889 2902 dtrace_specid_t which)
2890 2903 {
2891 2904 dtrace_speculation_t *spec;
2892 2905 dtrace_speculation_state_t current, new;
2893 2906 dtrace_buffer_t *buf;
2894 2907
2895 2908 if (which == 0)
2896 2909 return (NULL);
2897 2910
2898 2911 if (which > state->dts_nspeculations) {
2899 2912 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2900 2913 return (NULL);
2901 2914 }
2902 2915
2903 2916 spec = &state->dts_speculations[which - 1];
2904 2917 buf = &spec->dtsp_buffer[cpuid];
2905 2918
2906 2919 do {
2907 2920 current = spec->dtsp_state;
2908 2921
2909 2922 switch (current) {
2910 2923 case DTRACESPEC_INACTIVE:
2911 2924 case DTRACESPEC_COMMITTINGMANY:
2912 2925 case DTRACESPEC_DISCARDING:
2913 2926 return (NULL);
2914 2927
2915 2928 case DTRACESPEC_COMMITTING:
2916 2929 ASSERT(buf->dtb_offset == 0);
2917 2930 return (NULL);
2918 2931
2919 2932 case DTRACESPEC_ACTIVEONE:
2920 2933 /*
2921 2934 * This speculation is currently active on one CPU.
2922 2935 * Check the offset in the buffer; if it's non-zero,
2923 2936 * that CPU must be us (and we leave the state alone).
2924 2937 * If it's zero, assume that we're starting on a new
2925 2938 * CPU -- and change the state to indicate that the
2926 2939 * speculation is active on more than one CPU.
2927 2940 */
2928 2941 if (buf->dtb_offset != 0)
2929 2942 return (buf);
2930 2943
2931 2944 new = DTRACESPEC_ACTIVEMANY;
2932 2945 break;
2933 2946
2934 2947 case DTRACESPEC_ACTIVEMANY:
2935 2948 return (buf);
2936 2949
2937 2950 case DTRACESPEC_ACTIVE:
2938 2951 new = DTRACESPEC_ACTIVEONE;
2939 2952 break;
2940 2953
2941 2954 default:
2942 2955 ASSERT(0);
2943 2956 }
2944 2957 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2945 2958 current, new) != current);
2946 2959
2947 2960 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
2948 2961 return (buf);
2949 2962 }
2950 2963
2951 2964 /*
2952 2965 * Return a string. In the event that the user lacks the privilege to access
2953 2966 * arbitrary kernel memory, we copy the string out to scratch memory so that we
2954 2967 * don't fail access checking.
2955 2968 *
2956 2969 * dtrace_dif_variable() uses this routine as a helper for various
2957 2970 * builtin values such as 'execname' and 'probefunc.'
2958 2971 */
2959 2972 uintptr_t
2960 2973 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
2961 2974 dtrace_mstate_t *mstate)
2962 2975 {
2963 2976 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
2964 2977 uintptr_t ret;
2965 2978 size_t strsz;
2966 2979
2967 2980 /*
2968 2981 * The easy case: this probe is allowed to read all of memory, so
2969 2982 * we can just return this as a vanilla pointer.
2970 2983 */
2971 2984 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
2972 2985 return (addr);
2973 2986
2974 2987 /*
2975 2988 * This is the tougher case: we copy the string in question from
2976 2989 * kernel memory into scratch memory and return it that way: this
2977 2990 * ensures that we won't trip up when access checking tests the
2978 2991 * BYREF return value.
2979 2992 */
2980 2993 strsz = dtrace_strlen((char *)addr, size) + 1;
2981 2994
2982 2995 if (mstate->dtms_scratch_ptr + strsz >
2983 2996 mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
2984 2997 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
2985 2998 return (NULL);
2986 2999 }
2987 3000
2988 3001 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
2989 3002 strsz);
2990 3003 ret = mstate->dtms_scratch_ptr;
2991 3004 mstate->dtms_scratch_ptr += strsz;
2992 3005 return (ret);
2993 3006 }
2994 3007
2995 3008 /*
2996 3009 * This function implements the DIF emulator's variable lookups. The emulator
2997 3010 * passes a reserved variable identifier and optional built-in array index.
2998 3011 */
2999 3012 static uint64_t
3000 3013 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
3001 3014 uint64_t ndx)
3002 3015 {
3003 3016 /*
3004 3017 * If we're accessing one of the uncached arguments, we'll turn this
3005 3018 * into a reference in the args array.
3006 3019 */
3007 3020 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
3008 3021 ndx = v - DIF_VAR_ARG0;
3009 3022 v = DIF_VAR_ARGS;
3010 3023 }
3011 3024
3012 3025 switch (v) {
3013 3026 case DIF_VAR_ARGS:
3014 3027 if (!(mstate->dtms_access & DTRACE_ACCESS_ARGS)) {
3015 3028 cpu_core[CPU->cpu_id].cpuc_dtrace_flags |=
3016 3029 CPU_DTRACE_KPRIV;
3017 3030 return (0);
3018 3031 }
3019 3032
3020 3033 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
3021 3034 if (ndx >= sizeof (mstate->dtms_arg) /
3022 3035 sizeof (mstate->dtms_arg[0])) {
3023 3036 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3024 3037 dtrace_provider_t *pv;
3025 3038 uint64_t val;
3026 3039
3027 3040 pv = mstate->dtms_probe->dtpr_provider;
3028 3041 if (pv->dtpv_pops.dtps_getargval != NULL)
3029 3042 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
3030 3043 mstate->dtms_probe->dtpr_id,
3031 3044 mstate->dtms_probe->dtpr_arg, ndx, aframes);
3032 3045 else
3033 3046 val = dtrace_getarg(ndx, aframes);
3034 3047
3035 3048 /*
3036 3049 * This is regrettably required to keep the compiler
3037 3050 * from tail-optimizing the call to dtrace_getarg().
3038 3051 * The condition always evaluates to true, but the
3039 3052 * compiler has no way of figuring that out a priori.
3040 3053 * (None of this would be necessary if the compiler
3041 3054 * could be relied upon to _always_ tail-optimize
3042 3055 * the call to dtrace_getarg() -- but it can't.)
3043 3056 */
3044 3057 if (mstate->dtms_probe != NULL)
3045 3058 return (val);
3046 3059
3047 3060 ASSERT(0);
3048 3061 }
3049 3062
3050 3063 return (mstate->dtms_arg[ndx]);
3051 3064
3052 3065 case DIF_VAR_UREGS: {
3053 3066 klwp_t *lwp;
3054 3067
3055 3068 if (!dtrace_priv_proc(state, mstate))
3056 3069 return (0);
3057 3070
3058 3071 if ((lwp = curthread->t_lwp) == NULL) {
3059 3072 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3060 3073 cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL;
3061 3074 return (0);
3062 3075 }
3063 3076
3064 3077 return (dtrace_getreg(lwp->lwp_regs, ndx));
3065 3078 }
3066 3079
3067 3080 case DIF_VAR_VMREGS: {
3068 3081 uint64_t rval;
3069 3082
3070 3083 if (!dtrace_priv_kernel(state))
3071 3084 return (0);
3072 3085
3073 3086 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3074 3087
3075 3088 rval = dtrace_getvmreg(ndx,
3076 3089 &cpu_core[CPU->cpu_id].cpuc_dtrace_flags);
3077 3090
3078 3091 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3079 3092
3080 3093 return (rval);
3081 3094 }
3082 3095
3083 3096 case DIF_VAR_CURTHREAD:
3084 3097 if (!dtrace_priv_proc(state, mstate))
3085 3098 return (0);
3086 3099 return ((uint64_t)(uintptr_t)curthread);
3087 3100
3088 3101 case DIF_VAR_TIMESTAMP:
3089 3102 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
3090 3103 mstate->dtms_timestamp = dtrace_gethrtime();
3091 3104 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
3092 3105 }
3093 3106 return (mstate->dtms_timestamp);
3094 3107
3095 3108 case DIF_VAR_VTIMESTAMP:
3096 3109 ASSERT(dtrace_vtime_references != 0);
3097 3110 return (curthread->t_dtrace_vtime);
3098 3111
3099 3112 case DIF_VAR_WALLTIMESTAMP:
3100 3113 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
3101 3114 mstate->dtms_walltimestamp = dtrace_gethrestime();
3102 3115 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
3103 3116 }
3104 3117 return (mstate->dtms_walltimestamp);
3105 3118
3106 3119 case DIF_VAR_IPL:
3107 3120 if (!dtrace_priv_kernel(state))
3108 3121 return (0);
3109 3122 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
3110 3123 mstate->dtms_ipl = dtrace_getipl();
3111 3124 mstate->dtms_present |= DTRACE_MSTATE_IPL;
3112 3125 }
3113 3126 return (mstate->dtms_ipl);
3114 3127
3115 3128 case DIF_VAR_EPID:
3116 3129 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
3117 3130 return (mstate->dtms_epid);
3118 3131
3119 3132 case DIF_VAR_ID:
3120 3133 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3121 3134 return (mstate->dtms_probe->dtpr_id);
3122 3135
3123 3136 case DIF_VAR_STACKDEPTH:
3124 3137 if (!dtrace_priv_kernel(state))
3125 3138 return (0);
3126 3139 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
3127 3140 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3128 3141
3129 3142 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
3130 3143 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
3131 3144 }
3132 3145 return (mstate->dtms_stackdepth);
3133 3146
3134 3147 case DIF_VAR_USTACKDEPTH:
3135 3148 if (!dtrace_priv_proc(state, mstate))
3136 3149 return (0);
3137 3150 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
3138 3151 /*
3139 3152 * See comment in DIF_VAR_PID.
3140 3153 */
3141 3154 if (DTRACE_ANCHORED(mstate->dtms_probe) &&
3142 3155 CPU_ON_INTR(CPU)) {
3143 3156 mstate->dtms_ustackdepth = 0;
3144 3157 } else {
3145 3158 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3146 3159 mstate->dtms_ustackdepth =
3147 3160 dtrace_getustackdepth();
3148 3161 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3149 3162 }
3150 3163 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
3151 3164 }
3152 3165 return (mstate->dtms_ustackdepth);
3153 3166
3154 3167 case DIF_VAR_CALLER:
3155 3168 if (!dtrace_priv_kernel(state))
3156 3169 return (0);
3157 3170 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
3158 3171 int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3159 3172
3160 3173 if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
3161 3174 /*
3162 3175 * If this is an unanchored probe, we are
3163 3176 * required to go through the slow path:
3164 3177 * dtrace_caller() only guarantees correct
3165 3178 * results for anchored probes.
3166 3179 */
3167 3180 pc_t caller[2];
3168 3181
3169 3182 dtrace_getpcstack(caller, 2, aframes,
3170 3183 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
3171 3184 mstate->dtms_caller = caller[1];
3172 3185 } else if ((mstate->dtms_caller =
3173 3186 dtrace_caller(aframes)) == -1) {
3174 3187 /*
3175 3188 * We have failed to do this the quick way;
3176 3189 * we must resort to the slower approach of
3177 3190 * calling dtrace_getpcstack().
3178 3191 */
3179 3192 pc_t caller;
3180 3193
3181 3194 dtrace_getpcstack(&caller, 1, aframes, NULL);
3182 3195 mstate->dtms_caller = caller;
3183 3196 }
3184 3197
3185 3198 mstate->dtms_present |= DTRACE_MSTATE_CALLER;
3186 3199 }
3187 3200 return (mstate->dtms_caller);
3188 3201
3189 3202 case DIF_VAR_UCALLER:
3190 3203 if (!dtrace_priv_proc(state, mstate))
3191 3204 return (0);
3192 3205
3193 3206 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
3194 3207 uint64_t ustack[3];
3195 3208
3196 3209 /*
3197 3210 * dtrace_getupcstack() fills in the first uint64_t
3198 3211 * with the current PID. The second uint64_t will
3199 3212 * be the program counter at user-level. The third
3200 3213 * uint64_t will contain the caller, which is what
3201 3214 * we're after.
3202 3215 */
3203 3216 ustack[2] = NULL;
3204 3217 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3205 3218 dtrace_getupcstack(ustack, 3);
3206 3219 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3207 3220 mstate->dtms_ucaller = ustack[2];
3208 3221 mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3209 3222 }
3210 3223
3211 3224 return (mstate->dtms_ucaller);
3212 3225
3213 3226 case DIF_VAR_PROBEPROV:
3214 3227 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3215 3228 return (dtrace_dif_varstr(
3216 3229 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3217 3230 state, mstate));
3218 3231
3219 3232 case DIF_VAR_PROBEMOD:
3220 3233 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3221 3234 return (dtrace_dif_varstr(
3222 3235 (uintptr_t)mstate->dtms_probe->dtpr_mod,
3223 3236 state, mstate));
3224 3237
3225 3238 case DIF_VAR_PROBEFUNC:
3226 3239 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3227 3240 return (dtrace_dif_varstr(
3228 3241 (uintptr_t)mstate->dtms_probe->dtpr_func,
3229 3242 state, mstate));
3230 3243
3231 3244 case DIF_VAR_PROBENAME:
3232 3245 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3233 3246 return (dtrace_dif_varstr(
3234 3247 (uintptr_t)mstate->dtms_probe->dtpr_name,
3235 3248 state, mstate));
3236 3249
3237 3250 case DIF_VAR_PID:
3238 3251 if (!dtrace_priv_proc(state, mstate))
3239 3252 return (0);
3240 3253
3241 3254 /*
3242 3255 * Note that we are assuming that an unanchored probe is
3243 3256 * always due to a high-level interrupt. (And we're assuming
3244 3257 * that there is only a single high level interrupt.)
3245 3258 */
3246 3259 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3247 3260 return (pid0.pid_id);
3248 3261
3249 3262 /*
3250 3263 * It is always safe to dereference one's own t_procp pointer:
3251 3264 * it always points to a valid, allocated proc structure.
3252 3265 * Further, it is always safe to dereference the p_pidp member
3253 3266 * of one's own proc structure. (These are truisms becuase
3254 3267 * threads and processes don't clean up their own state --
3255 3268 * they leave that task to whomever reaps them.)
3256 3269 */
3257 3270 return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
3258 3271
3259 3272 case DIF_VAR_PPID:
3260 3273 if (!dtrace_priv_proc(state, mstate))
3261 3274 return (0);
3262 3275
3263 3276 /*
3264 3277 * See comment in DIF_VAR_PID.
3265 3278 */
3266 3279 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3267 3280 return (pid0.pid_id);
3268 3281
3269 3282 /*
3270 3283 * It is always safe to dereference one's own t_procp pointer:
3271 3284 * it always points to a valid, allocated proc structure.
3272 3285 * (This is true because threads don't clean up their own
3273 3286 * state -- they leave that task to whomever reaps them.)
3274 3287 */
3275 3288 return ((uint64_t)curthread->t_procp->p_ppid);
3276 3289
3277 3290 case DIF_VAR_TID:
3278 3291 /*
3279 3292 * See comment in DIF_VAR_PID.
3280 3293 */
3281 3294 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3282 3295 return (0);
3283 3296
3284 3297 return ((uint64_t)curthread->t_tid);
3285 3298
3286 3299 case DIF_VAR_EXECNAME:
3287 3300 if (!dtrace_priv_proc(state, mstate))
3288 3301 return (0);
3289 3302
3290 3303 /*
3291 3304 * See comment in DIF_VAR_PID.
3292 3305 */
3293 3306 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3294 3307 return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
3295 3308
3296 3309 /*
3297 3310 * It is always safe to dereference one's own t_procp pointer:
3298 3311 * it always points to a valid, allocated proc structure.
3299 3312 * (This is true because threads don't clean up their own
3300 3313 * state -- they leave that task to whomever reaps them.)
3301 3314 */
3302 3315 return (dtrace_dif_varstr(
3303 3316 (uintptr_t)curthread->t_procp->p_user.u_comm,
3304 3317 state, mstate));
3305 3318
3306 3319 case DIF_VAR_ZONENAME:
3307 3320 if (!dtrace_priv_proc(state, mstate))
3308 3321 return (0);
3309 3322
3310 3323 /*
3311 3324 * See comment in DIF_VAR_PID.
3312 3325 */
3313 3326 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3314 3327 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
3315 3328
3316 3329 /*
3317 3330 * It is always safe to dereference one's own t_procp pointer:
3318 3331 * it always points to a valid, allocated proc structure.
3319 3332 * (This is true because threads don't clean up their own
3320 3333 * state -- they leave that task to whomever reaps them.)
3321 3334 */
3322 3335 return (dtrace_dif_varstr(
3323 3336 (uintptr_t)curthread->t_procp->p_zone->zone_name,
3324 3337 state, mstate));
3325 3338
3326 3339 case DIF_VAR_UID:
3327 3340 if (!dtrace_priv_proc(state, mstate))
3328 3341 return (0);
3329 3342
3330 3343 /*
3331 3344 * See comment in DIF_VAR_PID.
3332 3345 */
3333 3346 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3334 3347 return ((uint64_t)p0.p_cred->cr_uid);
3335 3348
3336 3349 /*
3337 3350 * It is always safe to dereference one's own t_procp pointer:
3338 3351 * it always points to a valid, allocated proc structure.
3339 3352 * (This is true because threads don't clean up their own
3340 3353 * state -- they leave that task to whomever reaps them.)
3341 3354 *
3342 3355 * Additionally, it is safe to dereference one's own process
3343 3356 * credential, since this is never NULL after process birth.
3344 3357 */
3345 3358 return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
3346 3359
3347 3360 case DIF_VAR_GID:
3348 3361 if (!dtrace_priv_proc(state, mstate))
3349 3362 return (0);
3350 3363
3351 3364 /*
3352 3365 * See comment in DIF_VAR_PID.
3353 3366 */
3354 3367 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3355 3368 return ((uint64_t)p0.p_cred->cr_gid);
3356 3369
3357 3370 /*
3358 3371 * It is always safe to dereference one's own t_procp pointer:
3359 3372 * it always points to a valid, allocated proc structure.
3360 3373 * (This is true because threads don't clean up their own
3361 3374 * state -- they leave that task to whomever reaps them.)
3362 3375 *
3363 3376 * Additionally, it is safe to dereference one's own process
3364 3377 * credential, since this is never NULL after process birth.
3365 3378 */
3366 3379 return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
3367 3380
3368 3381 case DIF_VAR_ERRNO: {
3369 3382 klwp_t *lwp;
3370 3383 if (!dtrace_priv_proc(state, mstate))
3371 3384 return (0);
3372 3385
3373 3386 /*
3374 3387 * See comment in DIF_VAR_PID.
3375 3388 */
3376 3389 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3377 3390 return (0);
3378 3391
3379 3392 /*
3380 3393 * It is always safe to dereference one's own t_lwp pointer in
3381 3394 * the event that this pointer is non-NULL. (This is true
3382 3395 * because threads and lwps don't clean up their own state --
3383 3396 * they leave that task to whomever reaps them.)
3384 3397 */
3385 3398 if ((lwp = curthread->t_lwp) == NULL)
3386 3399 return (0);
↓ open down ↓ |
2465 lines elided |
↑ open up ↑ |
3387 3400
3388 3401 return ((uint64_t)lwp->lwp_errno);
3389 3402 }
3390 3403 default:
3391 3404 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3392 3405 return (0);
3393 3406 }
3394 3407 }
3395 3408
3396 3409
3397 -typedef enum json_state {
3398 - JSON_REST = 1,
3399 - JSON_OBJECT,
3400 - JSON_STRING,
3401 - JSON_STRING_ESCAPE,
3402 - JSON_STRING_ESCAPE_UNICODE,
3403 - JSON_COLON,
3404 - JSON_COMMA,
3405 - JSON_VALUE,
3406 - JSON_IDENTIFIER,
3407 - JSON_NUMBER,
3408 - JSON_NUMBER_FRAC,
3409 - JSON_NUMBER_EXP,
3410 - JSON_COLLECT_OBJECT
3411 -} json_state_t;
3410 +typedef enum dtrace_json_state {
3411 + DTRACE_JSON_REST = 1,
3412 + DTRACE_JSON_OBJECT,
3413 + DTRACE_JSON_STRING,
3414 + DTRACE_JSON_STRING_ESCAPE,
3415 + DTRACE_JSON_STRING_ESCAPE_UNICODE,
3416 + DTRACE_JSON_COLON,
3417 + DTRACE_JSON_COMMA,
3418 + DTRACE_JSON_VALUE,
3419 + DTRACE_JSON_IDENTIFIER,
3420 + DTRACE_JSON_NUMBER,
3421 + DTRACE_JSON_NUMBER_FRAC,
3422 + DTRACE_JSON_NUMBER_EXP,
3423 + DTRACE_JSON_COLLECT_OBJECT
3424 +} dtrace_json_state_t;
3412 3425
3413 3426 /*
3414 3427 * This function possesses just enough knowledge about JSON to extract a single
3415 3428 * value from a JSON string and store it in the scratch buffer. It is able
3416 3429 * to extract nested object values, and members of arrays by index.
3417 3430 *
3418 3431 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
3419 3432 * be looked up as we descend into the object tree. e.g.
3420 3433 *
3421 3434 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
3422 3435 * with nelems = 5.
3436 + *
3437 + * The run time of this function must be bounded above by strsize to limit the
3438 + * amount of work done in probe context. As such, it is implemented as a
3439 + * simple state machine, reading one character at a time using safe loads
3440 + * until we find the requested element, hit a parsing error or run off the
3441 + * end of the object or string.
3442 + *
3443 + * As there is no way for a subroutine to return an error without interrupting
3444 + * clause execution, we simply return NULL in the event of a missing key or any
3445 + * other error condition. Each NULL return in this function is commented with
3446 + * the error condition it represents -- parsing or otherwise.
3447 + *
3448 + * The set of states for the state machine closely matches the JSON
3449 + * specification (http://json.org/). Briefly:
3450 + *
3451 + * DTRACE_JSON_REST:
3452 + * Skip whitespace until we find either a top-level Object, moving
3453 + * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
3454 + *
3455 + * DTRACE_JSON_OBJECT:
3456 + * Locate the next key String in an Object. Sets a flag to denote
3457 + * the next String as a key string and moves to DTRACE_JSON_STRING.
3458 + *
3459 + * DTRACE_JSON_COLON:
3460 + * Skip whitespace until we find the colon that separates key Strings
3461 + * from their values. Once found, move to DTRACE_JSON_VALUE.
3462 + *
3463 + * DTRACE_JSON_VALUE:
3464 + * Detects the type of the next value (String, Number, Identifier, Object
3465 + * or Array) and routes to the states that process that type. Here we also
3466 + * deal with the element selector list if we are requested to traverse down
3467 + * into the object tree.
3468 + *
3469 + * DTRACE_JSON_COMMA:
3470 + * Skip whitespace until we find the comma that separates key-value pairs
3471 + * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
3472 + * (similarly DTRACE_JSON_VALUE). All following literal value processing
3473 + * states return to this state at the end of their value, unless otherwise
3474 + * noted.
3475 + *
3476 + * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
3477 + * Processes a Number literal from the JSON, including any exponent
3478 + * component that may be present. Numbers are returned as strings, which
3479 + * may be passed to strtoll() if an integer is required.
3480 + *
3481 + * DTRACE_JSON_IDENTIFIER:
3482 + * Processes a "true", "false" or "null" literal in the JSON.
3483 + *
3484 + * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
3485 + * DTRACE_JSON_STRING_ESCAPE_UNICODE:
3486 + * Processes a String literal from the JSON, whether the String denotes
3487 + * a key, a value or part of a larger Object. Handles all escape sequences
3488 + * present in the specification, including four-digit unicode characters,
3489 + * but merely includes the escape sequence without converting it to the
3490 + * actual escaped character. If the String is flagged as a key, we
3491 + * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
3492 + *
3493 + * DTRACE_JSON_COLLECT_OBJECT:
3494 + * This state collects an entire Object (or Array), correctly handling
3495 + * embedded strings. If the full element selector list matches this nested
3496 + * object, we return the Object in full as a string. If not, we use this
3497 + * state to skip to the next value at this level and continue processing.
3498 + *
3499 + * NOTE: This function uses various macros from strtolctype.h to manipulate
3500 + * digit values, etc -- these have all been checked to ensure they make
3501 + * no additional function calls.
3423 3502 */
3424 3503 static char *
3425 3504 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems,
3426 3505 char *dest)
3427 3506 {
3428 - json_state_t state = JSON_REST;
3507 + dtrace_json_state_t state = DTRACE_JSON_REST;
3429 3508 uint64_t i;
3430 3509 int64_t array_elem = INT64_MIN;
3431 3510 int64_t array_pos = 0;
3432 3511 uint8_t escape_unicount = 0;
3433 3512 boolean_t string_is_key = B_FALSE;
3434 3513 boolean_t collect_object = B_FALSE;
3435 3514 boolean_t found_key = B_FALSE;
3436 3515 boolean_t in_array = B_FALSE;
3437 - uint8_t braces = 0, brackets = 0;
3516 + uint32_t braces = 0, brackets = 0;
3438 3517 char *elem = elemlist;
3439 3518 char *dd = dest;
3440 3519 uintptr_t cur;
3441 3520
3442 3521 for (cur = json; cur < json + size; cur++) {
3443 3522 char cc = dtrace_load8(cur);
3444 - if (cc == '\0' || braces > 250)
3523 + if (cc == '\0')
3445 3524 return (NULL);
3446 3525
3447 3526 switch (state) {
3448 - case JSON_REST:
3449 - if (cc == ' ' || cc == '\t' || cc == '\n' || cc == '\r')
3450 - break; /* eat whitespace */
3527 + case DTRACE_JSON_REST:
3528 + if (isspace(cc))
3529 + break;
3451 3530
3452 3531 if (cc == '{') {
3453 - state = JSON_OBJECT;
3532 + state = DTRACE_JSON_OBJECT;
3454 3533 break;
3455 3534 }
3456 3535
3457 3536 if (cc == '[') {
3458 3537 in_array = B_TRUE;
3459 3538 array_pos = 0;
3460 3539 array_elem = dtrace_strtoll(elem, 10, size);
3461 - found_key = !!(array_elem == 0);
3462 - state = JSON_VALUE;
3540 + found_key = array_elem == 0 ? B_TRUE : B_FALSE;
3541 + state = DTRACE_JSON_VALUE;
3463 3542 break;
3464 3543 }
3465 3544
3466 - /* ERROR: expected object or array */
3545 + /*
3546 + * ERROR: expected to find a top-level object or array.
3547 + */
3467 3548 return (NULL);
3468 - case JSON_OBJECT:
3469 - if (cc == ' ' || cc == '\t' || cc == '\n' || cc == '\r')
3470 - break; /* eat whitespace */
3549 + case DTRACE_JSON_OBJECT:
3550 + if (isspace(cc))
3551 + break;
3471 3552
3472 3553 if (cc == '"') {
3473 - state = JSON_STRING;
3554 + state = DTRACE_JSON_STRING;
3474 3555 string_is_key = B_TRUE;
3475 3556 break;
3476 3557 }
3477 3558
3478 - /* ERROR: key not found! */
3559 + /*
3560 + * ERROR: either the object did not start with a key
3561 + * string, or we've run off the end of the object
3562 + * without finding the requested key.
3563 + */
3479 3564 return (NULL);
3480 - case JSON_STRING:
3565 + case DTRACE_JSON_STRING:
3481 3566 if (cc == '\\') {
3482 3567 *dd++ = '\\';
3483 - state = JSON_STRING_ESCAPE;
3568 + state = DTRACE_JSON_STRING_ESCAPE;
3484 3569 break;
3485 3570 }
3486 3571
3487 3572 if (cc == '"') {
3488 3573 if (collect_object) {
3489 3574 /*
3490 3575 * We don't reset the dest here, as
3491 3576 * the string is part of a larger
3492 3577 * object being collected.
3493 3578 */
3494 3579 *dd++ = cc;
3495 3580 collect_object = B_FALSE;
3496 - state = JSON_COLLECT_OBJECT;
3581 + state = DTRACE_JSON_COLLECT_OBJECT;
3497 3582 break;
3498 3583 }
3499 3584 *dd = '\0';
3500 3585 dd = dest; /* reset string buffer */
3501 3586 if (string_is_key) {
3502 3587 if (dtrace_strncmp(dest, elem,
3503 3588 size) == 0)
3504 3589 found_key = B_TRUE;
3505 3590 } else if (found_key) {
3506 3591 if (nelems > 1) {
3507 3592 /*
3508 3593 * We expected an object, not
3509 3594 * this string.
3510 3595 */
3511 3596 return (NULL);
3512 3597 }
3513 3598 return (dest);
3514 3599 }
3515 - state = string_is_key ? JSON_COLON :
3516 - JSON_COMMA;
3600 + state = string_is_key ? DTRACE_JSON_COLON :
3601 + DTRACE_JSON_COMMA;
3517 3602 string_is_key = B_FALSE;
3518 3603 break;
3519 3604 }
3520 3605
3521 3606 *dd++ = cc;
3522 3607 break;
3523 - case JSON_STRING_ESCAPE:
3608 + case DTRACE_JSON_STRING_ESCAPE:
3524 3609 *dd++ = cc;
3525 3610 if (cc == 'u') {
3526 3611 escape_unicount = 0;
3527 - state = JSON_STRING_ESCAPE_UNICODE;
3612 + state = DTRACE_JSON_STRING_ESCAPE_UNICODE;
3528 3613 } else {
3529 - state = JSON_STRING;
3614 + state = DTRACE_JSON_STRING;
3530 3615 }
3531 3616 break;
3532 - case JSON_STRING_ESCAPE_UNICODE:
3533 - if (!isxdigit(cc))
3534 - /* ERROR: unvalid unicode escape */
3617 + case DTRACE_JSON_STRING_ESCAPE_UNICODE:
3618 + if (!isxdigit(cc)) {
3619 + /*
3620 + * ERROR: invalid unicode escape, expected
3621 + * four valid hexidecimal digits.
3622 + */
3535 3623 return (NULL);
3624 + }
3536 3625
3537 3626 *dd++ = cc;
3538 3627 if (++escape_unicount == 4)
3539 - state = JSON_STRING;
3628 + state = DTRACE_JSON_STRING;
3540 3629 break;
3541 - case JSON_COLON:
3542 - if (cc == ' ' || cc == '\t' || cc == '\n' || cc == '\r')
3543 - break; /* eat whitespace */
3630 + case DTRACE_JSON_COLON:
3631 + if (isspace(cc))
3632 + break;
3544 3633
3545 3634 if (cc == ':') {
3546 - state = JSON_VALUE;
3635 + state = DTRACE_JSON_VALUE;
3547 3636 break;
3548 3637 }
3549 3638
3550 - /* ERROR: expected colon */
3639 + /*
3640 + * ERROR: expected a colon.
3641 + */
3551 3642 return (NULL);
3552 - case JSON_COMMA:
3553 - if (cc == ' ' || cc == '\t' || cc == '\n' || cc == '\r')
3554 - break; /* eat whitespace */
3643 + case DTRACE_JSON_COMMA:
3644 + if (isspace(cc))
3645 + break;
3555 3646
3556 3647 if (cc == ',') {
3557 3648 if (in_array) {
3558 - state = JSON_VALUE;
3649 + state = DTRACE_JSON_VALUE;
3559 3650 if (++array_pos == array_elem)
3560 3651 found_key = B_TRUE;
3561 3652 } else {
3562 - state = JSON_OBJECT;
3653 + state = DTRACE_JSON_OBJECT;
3563 3654 }
3564 3655 break;
3565 3656 }
3566 3657
3567 - /* ERROR: key not found or expected comma */
3658 + /*
3659 + * ERROR: either we hit an unexpected character, or
3660 + * we reached the end of the object or array without
3661 + * finding the requested key.
3662 + */
3568 3663 return (NULL);
3569 - case JSON_IDENTIFIER:
3570 - if (cc >= 'a' && cc <= 'z') {
3664 + case DTRACE_JSON_IDENTIFIER:
3665 + if (islower(cc)) {
3571 3666 *dd++ = cc;
3572 3667 break;
3573 3668 }
3574 3669
3575 3670 *dd = '\0';
3576 3671 dd = dest; /* reset string buffer */
3577 3672
3578 3673 if (dtrace_strncmp(dest, "true", 5) == 0 ||
3579 3674 dtrace_strncmp(dest, "false", 6) == 0 ||
3580 3675 dtrace_strncmp(dest, "null", 5) == 0) {
3581 3676 if (found_key) {
3582 3677 if (nelems > 1) {
3583 3678 /*
3584 - * We expected an object, not
3585 - * this identifier.
3679 + * ERROR: We expected an object,
3680 + * not this identifier.
3586 3681 */
3587 3682 return (NULL);
3588 3683 }
3589 3684 return (dest);
3590 3685 } else {
3591 3686 cur--;
3592 - state = JSON_COMMA;
3687 + state = DTRACE_JSON_COMMA;
3593 3688 break;
3594 3689 }
3595 3690 }
3596 3691
3597 - /* ERROR: unexpected identifier */
3692 + /*
3693 + * ERROR: we did not recognise the identifier as one
3694 + * of those in the JSON specification.
3695 + */
3598 3696 return (NULL);
3599 - case JSON_NUMBER:
3697 + case DTRACE_JSON_NUMBER:
3600 3698 if (cc == '.') {
3601 3699 *dd++ = cc;
3602 - state = JSON_NUMBER_FRAC;
3700 + state = DTRACE_JSON_NUMBER_FRAC;
3603 3701 break;
3604 3702 }
3605 3703
3606 - if (cc == 'x' || cc == 'X')
3607 - /* ERROR: spec explicitly excludes hex */
3704 + if (cc == 'x' || cc == 'X') {
3705 + /*
3706 + * ERROR: specification explicitly excludes
3707 + * hexidecimal or octal numbers.
3708 + */
3608 3709 return (NULL);
3710 + }
3609 3711
3610 3712 /* FALLTHRU */
3611 - case JSON_NUMBER_FRAC:
3713 + case DTRACE_JSON_NUMBER_FRAC:
3612 3714 if (cc == 'e' || cc == 'E') {
3613 3715 *dd++ = cc;
3614 - state = JSON_NUMBER_EXP;
3716 + state = DTRACE_JSON_NUMBER_EXP;
3615 3717 break;
3616 3718 }
3617 3719
3618 3720 if (cc == '+' || cc == '-') {
3619 3721 /*
3620 - * ERROR: expect sign as part of exponent only
3722 + * ERROR: expect sign as part of exponent only.
3621 3723 */
3622 3724 return (NULL);
3623 3725 }
3624 3726 /* FALLTHRU */
3625 - case JSON_NUMBER_EXP:
3626 - if ((cc >= '0' && cc <= '9') || cc == '+' ||
3627 - cc == '-') {
3727 + case DTRACE_JSON_NUMBER_EXP:
3728 + if (isdigit(cc) || cc == '+' || cc == '-') {
3628 3729 *dd++ = cc;
3629 3730 break;
3630 3731 }
3631 3732
3632 3733 *dd = '\0';
3633 3734 dd = dest; /* reset string buffer */
3634 3735 if (found_key) {
3635 3736 if (nelems > 1) {
3636 3737 /*
3637 - * We expected an object, not this
3638 - * number.
3738 + * ERROR: We expected an object, not
3739 + * this number.
3639 3740 */
3640 3741 return (NULL);
3641 3742 }
3642 3743 return (dest);
3643 3744 }
3644 3745
3645 3746 cur--;
3646 - state = JSON_COMMA;
3747 + state = DTRACE_JSON_COMMA;
3647 3748 break;
3648 - case JSON_VALUE:
3649 - if (cc == ' ' || cc == '\t' || cc == '\n' || cc == '\r')
3650 - break; /* eat whitespace */
3749 + case DTRACE_JSON_VALUE:
3750 + if (isspace(cc))
3751 + break;
3651 3752
3652 3753 if (cc == '{' || cc == '[') {
3653 3754 if (nelems > 1 && found_key) {
3654 - in_array = !!(cc == '[');
3755 + in_array = cc == '[' ? B_TRUE : B_FALSE;
3655 3756 /*
3656 3757 * If our element selector directs us
3657 3758 * to descend into this nested object,
3658 3759 * then move to the next selector
3659 3760 * element in the list and restart the
3660 3761 * state machine.
3661 3762 */
3662 3763 while (*elem != '\0')
3663 3764 elem++;
3664 3765 elem++; /* skip the inter-element NUL */
3665 3766 nelems--;
3666 3767 dd = dest;
3667 3768 if (in_array) {
3668 - state = JSON_VALUE;
3769 + state = DTRACE_JSON_VALUE;
3669 3770 array_pos = 0;
3670 3771 array_elem = dtrace_strtoll(
3671 3772 elem, 10, size);
3672 - found_key = !!(array_elem == 0);
3773 + found_key = array_elem == 0 ?
3774 + B_TRUE : B_FALSE;
3673 3775 } else {
3674 3776 found_key = B_FALSE;
3675 - state = JSON_OBJECT;
3777 + state = DTRACE_JSON_OBJECT;
3676 3778 }
3677 3779 break;
3678 3780 }
3679 3781
3680 3782 /*
3681 3783 * Otherwise, we wish to either skip this
3682 3784 * nested object or return it in full.
3683 3785 */
3684 3786 if (cc == '[')
3685 3787 brackets = 1;
3686 3788 else
3687 3789 braces = 1;
3688 3790 *dd++ = cc;
3689 - state = JSON_COLLECT_OBJECT;
3791 + state = DTRACE_JSON_COLLECT_OBJECT;
3690 3792 break;
3691 3793 }
3692 3794
3693 3795 if (cc == '"') {
3694 - state = JSON_STRING;
3796 + state = DTRACE_JSON_STRING;
3695 3797 break;
3696 3798 }
3697 3799
3698 - if (cc >= 'a' && cc <= 'z') {
3699 - /* Here we deal with true, false and null */
3800 + if (islower(cc)) {
3801 + /*
3802 + * Here we deal with true, false and null.
3803 + */
3700 3804 *dd++ = cc;
3701 - state = JSON_IDENTIFIER;
3805 + state = DTRACE_JSON_IDENTIFIER;
3702 3806 break;
3703 3807 }
3704 3808
3705 - if (cc == '-' || (cc >= '0' && cc <= '9')) {
3809 + if (cc == '-' || isdigit(cc)) {
3706 3810 *dd++ = cc;
3707 - state = JSON_NUMBER;
3811 + state = DTRACE_JSON_NUMBER;
3708 3812 break;
3709 3813 }
3710 3814
3711 - /* ERROR: unexpected character */
3815 + /*
3816 + * ERROR: unexpected character at start of value.
3817 + */
3712 3818 return (NULL);
3713 - case JSON_COLLECT_OBJECT:
3819 + case DTRACE_JSON_COLLECT_OBJECT:
3714 3820 if (cc == '\0')
3715 - /* ERROR: unexpected end of input */
3821 + /*
3822 + * ERROR: unexpected end of input.
3823 + */
3716 3824 return (NULL);
3717 3825
3718 3826 *dd++ = cc;
3719 3827 if (cc == '"') {
3720 3828 collect_object = B_TRUE;
3721 - state = JSON_STRING;
3829 + state = DTRACE_JSON_STRING;
3722 3830 break;
3723 3831 }
3724 3832
3725 3833 if (cc == ']') {
3726 3834 if (brackets-- == 0) {
3727 - /* ERROR: unbalanced brackets */
3835 + /*
3836 + * ERROR: unbalanced brackets.
3837 + */
3728 3838 return (NULL);
3729 3839 }
3730 3840 } else if (cc == '}') {
3731 3841 if (braces-- == 0) {
3732 - /* ERROR: unbalanced braces */
3842 + /*
3843 + * ERROR: unbalanced braces.
3844 + */
3733 3845 return (NULL);
3734 3846 }
3735 3847 } else if (cc == '{') {
3736 3848 braces++;
3737 3849 } else if (cc == '[') {
3738 3850 brackets++;
3739 3851 }
3740 3852
3741 3853 if (brackets == 0 && braces == 0) {
3742 3854 if (found_key) {
3743 3855 *dd = '\0';
3744 3856 return (dest);
3745 3857 }
3746 3858 dd = dest; /* reset string buffer */
3747 - state = JSON_COMMA;
3859 + state = DTRACE_JSON_COMMA;
3748 3860 }
3749 3861 break;
3750 3862 }
3751 3863 }
3752 3864 return (NULL);
3753 3865 }
3754 3866
3755 3867 /*
3756 3868 * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
3757 3869 * Notice that we don't bother validating the proper number of arguments or
3758 3870 * their types in the tuple stack. This isn't needed because all argument
3759 3871 * interpretation is safe because of our load safety -- the worst that can
3760 3872 * happen is that a bogus program can obtain bogus results.
3761 3873 */
3762 3874 static void
3763 3875 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
3764 3876 dtrace_key_t *tupregs, int nargs,
3765 3877 dtrace_mstate_t *mstate, dtrace_state_t *state)
3766 3878 {
3767 3879 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
3768 3880 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
3769 3881 dtrace_vstate_t *vstate = &state->dts_vstate;
3770 3882
3771 3883 union {
3772 3884 mutex_impl_t mi;
3773 3885 uint64_t mx;
3774 3886 } m;
3775 3887
3776 3888 union {
3777 3889 krwlock_t ri;
3778 3890 uintptr_t rw;
3779 3891 } r;
3780 3892
3781 3893 switch (subr) {
3782 3894 case DIF_SUBR_RAND:
3783 3895 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875;
3784 3896 break;
3785 3897
3786 3898 case DIF_SUBR_MUTEX_OWNED:
3787 3899 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3788 3900 mstate, vstate)) {
3789 3901 regs[rd] = NULL;
3790 3902 break;
3791 3903 }
3792 3904
3793 3905 m.mx = dtrace_load64(tupregs[0].dttk_value);
3794 3906 if (MUTEX_TYPE_ADAPTIVE(&m.mi))
3795 3907 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
3796 3908 else
3797 3909 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
3798 3910 break;
3799 3911
3800 3912 case DIF_SUBR_MUTEX_OWNER:
3801 3913 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3802 3914 mstate, vstate)) {
3803 3915 regs[rd] = NULL;
3804 3916 break;
3805 3917 }
3806 3918
3807 3919 m.mx = dtrace_load64(tupregs[0].dttk_value);
3808 3920 if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
3809 3921 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
3810 3922 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
3811 3923 else
3812 3924 regs[rd] = 0;
3813 3925 break;
3814 3926
3815 3927 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
3816 3928 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3817 3929 mstate, vstate)) {
3818 3930 regs[rd] = NULL;
3819 3931 break;
3820 3932 }
3821 3933
3822 3934 m.mx = dtrace_load64(tupregs[0].dttk_value);
3823 3935 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
3824 3936 break;
3825 3937
3826 3938 case DIF_SUBR_MUTEX_TYPE_SPIN:
3827 3939 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
3828 3940 mstate, vstate)) {
3829 3941 regs[rd] = NULL;
3830 3942 break;
3831 3943 }
3832 3944
3833 3945 m.mx = dtrace_load64(tupregs[0].dttk_value);
3834 3946 regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
3835 3947 break;
3836 3948
3837 3949 case DIF_SUBR_RW_READ_HELD: {
3838 3950 uintptr_t tmp;
3839 3951
3840 3952 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
3841 3953 mstate, vstate)) {
3842 3954 regs[rd] = NULL;
3843 3955 break;
3844 3956 }
3845 3957
3846 3958 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3847 3959 regs[rd] = _RW_READ_HELD(&r.ri, tmp);
3848 3960 break;
3849 3961 }
3850 3962
3851 3963 case DIF_SUBR_RW_WRITE_HELD:
3852 3964 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3853 3965 mstate, vstate)) {
3854 3966 regs[rd] = NULL;
3855 3967 break;
3856 3968 }
3857 3969
3858 3970 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3859 3971 regs[rd] = _RW_WRITE_HELD(&r.ri);
3860 3972 break;
3861 3973
3862 3974 case DIF_SUBR_RW_ISWRITER:
3863 3975 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
3864 3976 mstate, vstate)) {
3865 3977 regs[rd] = NULL;
3866 3978 break;
3867 3979 }
3868 3980
3869 3981 r.rw = dtrace_loadptr(tupregs[0].dttk_value);
3870 3982 regs[rd] = _RW_ISWRITER(&r.ri);
3871 3983 break;
3872 3984
3873 3985 case DIF_SUBR_BCOPY: {
3874 3986 /*
3875 3987 * We need to be sure that the destination is in the scratch
3876 3988 * region -- no other region is allowed.
3877 3989 */
3878 3990 uintptr_t src = tupregs[0].dttk_value;
3879 3991 uintptr_t dest = tupregs[1].dttk_value;
3880 3992 size_t size = tupregs[2].dttk_value;
3881 3993
3882 3994 if (!dtrace_inscratch(dest, size, mstate)) {
3883 3995 *flags |= CPU_DTRACE_BADADDR;
3884 3996 *illval = regs[rd];
3885 3997 break;
3886 3998 }
3887 3999
3888 4000 if (!dtrace_canload(src, size, mstate, vstate)) {
3889 4001 regs[rd] = NULL;
3890 4002 break;
3891 4003 }
3892 4004
3893 4005 dtrace_bcopy((void *)src, (void *)dest, size);
3894 4006 break;
3895 4007 }
3896 4008
3897 4009 case DIF_SUBR_ALLOCA:
3898 4010 case DIF_SUBR_COPYIN: {
3899 4011 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
3900 4012 uint64_t size =
3901 4013 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
3902 4014 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
3903 4015
3904 4016 /*
3905 4017 * This action doesn't require any credential checks since
3906 4018 * probes will not activate in user contexts to which the
3907 4019 * enabling user does not have permissions.
3908 4020 */
3909 4021
3910 4022 /*
3911 4023 * Rounding up the user allocation size could have overflowed
3912 4024 * a large, bogus allocation (like -1ULL) to 0.
3913 4025 */
3914 4026 if (scratch_size < size ||
3915 4027 !DTRACE_INSCRATCH(mstate, scratch_size)) {
3916 4028 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3917 4029 regs[rd] = NULL;
3918 4030 break;
3919 4031 }
3920 4032
3921 4033 if (subr == DIF_SUBR_COPYIN) {
3922 4034 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3923 4035 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
3924 4036 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3925 4037 }
3926 4038
3927 4039 mstate->dtms_scratch_ptr += scratch_size;
3928 4040 regs[rd] = dest;
3929 4041 break;
3930 4042 }
3931 4043
3932 4044 case DIF_SUBR_COPYINTO: {
3933 4045 uint64_t size = tupregs[1].dttk_value;
3934 4046 uintptr_t dest = tupregs[2].dttk_value;
3935 4047
3936 4048 /*
3937 4049 * This action doesn't require any credential checks since
3938 4050 * probes will not activate in user contexts to which the
3939 4051 * enabling user does not have permissions.
3940 4052 */
3941 4053 if (!dtrace_inscratch(dest, size, mstate)) {
3942 4054 *flags |= CPU_DTRACE_BADADDR;
3943 4055 *illval = regs[rd];
3944 4056 break;
3945 4057 }
3946 4058
3947 4059 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3948 4060 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
3949 4061 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3950 4062 break;
3951 4063 }
3952 4064
3953 4065 case DIF_SUBR_COPYINSTR: {
3954 4066 uintptr_t dest = mstate->dtms_scratch_ptr;
3955 4067 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3956 4068
3957 4069 if (nargs > 1 && tupregs[1].dttk_value < size)
3958 4070 size = tupregs[1].dttk_value + 1;
3959 4071
3960 4072 /*
3961 4073 * This action doesn't require any credential checks since
3962 4074 * probes will not activate in user contexts to which the
3963 4075 * enabling user does not have permissions.
3964 4076 */
3965 4077 if (!DTRACE_INSCRATCH(mstate, size)) {
3966 4078 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3967 4079 regs[rd] = NULL;
3968 4080 break;
3969 4081 }
3970 4082
3971 4083 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3972 4084 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
3973 4085 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3974 4086
3975 4087 ((char *)dest)[size - 1] = '\0';
3976 4088 mstate->dtms_scratch_ptr += size;
3977 4089 regs[rd] = dest;
3978 4090 break;
3979 4091 }
3980 4092
3981 4093 case DIF_SUBR_MSGSIZE:
3982 4094 case DIF_SUBR_MSGDSIZE: {
3983 4095 uintptr_t baddr = tupregs[0].dttk_value, daddr;
3984 4096 uintptr_t wptr, rptr;
3985 4097 size_t count = 0;
3986 4098 int cont = 0;
3987 4099
3988 4100 while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
3989 4101
3990 4102 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
3991 4103 vstate)) {
3992 4104 regs[rd] = NULL;
3993 4105 break;
3994 4106 }
3995 4107
3996 4108 wptr = dtrace_loadptr(baddr +
3997 4109 offsetof(mblk_t, b_wptr));
3998 4110
3999 4111 rptr = dtrace_loadptr(baddr +
4000 4112 offsetof(mblk_t, b_rptr));
4001 4113
4002 4114 if (wptr < rptr) {
4003 4115 *flags |= CPU_DTRACE_BADADDR;
4004 4116 *illval = tupregs[0].dttk_value;
4005 4117 break;
4006 4118 }
4007 4119
4008 4120 daddr = dtrace_loadptr(baddr +
4009 4121 offsetof(mblk_t, b_datap));
4010 4122
4011 4123 baddr = dtrace_loadptr(baddr +
4012 4124 offsetof(mblk_t, b_cont));
4013 4125
4014 4126 /*
4015 4127 * We want to prevent against denial-of-service here,
4016 4128 * so we're only going to search the list for
4017 4129 * dtrace_msgdsize_max mblks.
4018 4130 */
4019 4131 if (cont++ > dtrace_msgdsize_max) {
4020 4132 *flags |= CPU_DTRACE_ILLOP;
4021 4133 break;
4022 4134 }
4023 4135
4024 4136 if (subr == DIF_SUBR_MSGDSIZE) {
4025 4137 if (dtrace_load8(daddr +
4026 4138 offsetof(dblk_t, db_type)) != M_DATA)
4027 4139 continue;
4028 4140 }
4029 4141
4030 4142 count += wptr - rptr;
4031 4143 }
4032 4144
4033 4145 if (!(*flags & CPU_DTRACE_FAULT))
4034 4146 regs[rd] = count;
4035 4147
4036 4148 break;
4037 4149 }
4038 4150
4039 4151 case DIF_SUBR_PROGENYOF: {
4040 4152 pid_t pid = tupregs[0].dttk_value;
4041 4153 proc_t *p;
4042 4154 int rval = 0;
4043 4155
4044 4156 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4045 4157
4046 4158 for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
4047 4159 if (p->p_pidp->pid_id == pid) {
4048 4160 rval = 1;
4049 4161 break;
4050 4162 }
4051 4163 }
4052 4164
4053 4165 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4054 4166
4055 4167 regs[rd] = rval;
4056 4168 break;
4057 4169 }
4058 4170
4059 4171 case DIF_SUBR_SPECULATION:
4060 4172 regs[rd] = dtrace_speculation(state);
4061 4173 break;
4062 4174
4063 4175 case DIF_SUBR_COPYOUT: {
4064 4176 uintptr_t kaddr = tupregs[0].dttk_value;
4065 4177 uintptr_t uaddr = tupregs[1].dttk_value;
4066 4178 uint64_t size = tupregs[2].dttk_value;
4067 4179
4068 4180 if (!dtrace_destructive_disallow &&
4069 4181 dtrace_priv_proc_control(state, mstate) &&
4070 4182 !dtrace_istoxic(kaddr, size)) {
4071 4183 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4072 4184 dtrace_copyout(kaddr, uaddr, size, flags);
4073 4185 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4074 4186 }
4075 4187 break;
4076 4188 }
4077 4189
4078 4190 case DIF_SUBR_COPYOUTSTR: {
4079 4191 uintptr_t kaddr = tupregs[0].dttk_value;
4080 4192 uintptr_t uaddr = tupregs[1].dttk_value;
4081 4193 uint64_t size = tupregs[2].dttk_value;
4082 4194
4083 4195 if (!dtrace_destructive_disallow &&
4084 4196 dtrace_priv_proc_control(state, mstate) &&
4085 4197 !dtrace_istoxic(kaddr, size)) {
4086 4198 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4087 4199 dtrace_copyoutstr(kaddr, uaddr, size, flags);
4088 4200 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4089 4201 }
4090 4202 break;
4091 4203 }
4092 4204
4093 4205 case DIF_SUBR_STRLEN: {
4094 4206 size_t sz;
4095 4207 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
4096 4208 sz = dtrace_strlen((char *)addr,
4097 4209 state->dts_options[DTRACEOPT_STRSIZE]);
4098 4210
4099 4211 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) {
4100 4212 regs[rd] = NULL;
4101 4213 break;
4102 4214 }
4103 4215
4104 4216 regs[rd] = sz;
4105 4217
4106 4218 break;
4107 4219 }
4108 4220
4109 4221 case DIF_SUBR_STRCHR:
4110 4222 case DIF_SUBR_STRRCHR: {
4111 4223 /*
4112 4224 * We're going to iterate over the string looking for the
4113 4225 * specified character. We will iterate until we have reached
4114 4226 * the string length or we have found the character. If this
4115 4227 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
4116 4228 * of the specified character instead of the first.
4117 4229 */
4118 4230 uintptr_t saddr = tupregs[0].dttk_value;
4119 4231 uintptr_t addr = tupregs[0].dttk_value;
4120 4232 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE];
4121 4233 char c, target = (char)tupregs[1].dttk_value;
4122 4234
4123 4235 for (regs[rd] = NULL; addr < limit; addr++) {
4124 4236 if ((c = dtrace_load8(addr)) == target) {
4125 4237 regs[rd] = addr;
4126 4238
4127 4239 if (subr == DIF_SUBR_STRCHR)
4128 4240 break;
4129 4241 }
4130 4242
4131 4243 if (c == '\0')
4132 4244 break;
4133 4245 }
4134 4246
4135 4247 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) {
4136 4248 regs[rd] = NULL;
4137 4249 break;
4138 4250 }
4139 4251
4140 4252 break;
4141 4253 }
4142 4254
4143 4255 case DIF_SUBR_STRSTR:
4144 4256 case DIF_SUBR_INDEX:
4145 4257 case DIF_SUBR_RINDEX: {
4146 4258 /*
4147 4259 * We're going to iterate over the string looking for the
4148 4260 * specified string. We will iterate until we have reached
4149 4261 * the string length or we have found the string. (Yes, this
4150 4262 * is done in the most naive way possible -- but considering
4151 4263 * that the string we're searching for is likely to be
4152 4264 * relatively short, the complexity of Rabin-Karp or similar
4153 4265 * hardly seems merited.)
4154 4266 */
4155 4267 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
4156 4268 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
4157 4269 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4158 4270 size_t len = dtrace_strlen(addr, size);
4159 4271 size_t sublen = dtrace_strlen(substr, size);
4160 4272 char *limit = addr + len, *orig = addr;
4161 4273 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
4162 4274 int inc = 1;
4163 4275
4164 4276 regs[rd] = notfound;
4165 4277
4166 4278 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
4167 4279 regs[rd] = NULL;
4168 4280 break;
4169 4281 }
4170 4282
4171 4283 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
4172 4284 vstate)) {
4173 4285 regs[rd] = NULL;
4174 4286 break;
4175 4287 }
4176 4288
4177 4289 /*
4178 4290 * strstr() and index()/rindex() have similar semantics if
4179 4291 * both strings are the empty string: strstr() returns a
4180 4292 * pointer to the (empty) string, and index() and rindex()
4181 4293 * both return index 0 (regardless of any position argument).
4182 4294 */
4183 4295 if (sublen == 0 && len == 0) {
4184 4296 if (subr == DIF_SUBR_STRSTR)
4185 4297 regs[rd] = (uintptr_t)addr;
4186 4298 else
4187 4299 regs[rd] = 0;
4188 4300 break;
4189 4301 }
4190 4302
4191 4303 if (subr != DIF_SUBR_STRSTR) {
4192 4304 if (subr == DIF_SUBR_RINDEX) {
4193 4305 limit = orig - 1;
4194 4306 addr += len;
4195 4307 inc = -1;
4196 4308 }
4197 4309
4198 4310 /*
4199 4311 * Both index() and rindex() take an optional position
4200 4312 * argument that denotes the starting position.
4201 4313 */
4202 4314 if (nargs == 3) {
4203 4315 int64_t pos = (int64_t)tupregs[2].dttk_value;
4204 4316
4205 4317 /*
4206 4318 * If the position argument to index() is
4207 4319 * negative, Perl implicitly clamps it at
4208 4320 * zero. This semantic is a little surprising
4209 4321 * given the special meaning of negative
4210 4322 * positions to similar Perl functions like
4211 4323 * substr(), but it appears to reflect a
4212 4324 * notion that index() can start from a
4213 4325 * negative index and increment its way up to
4214 4326 * the string. Given this notion, Perl's
4215 4327 * rindex() is at least self-consistent in
4216 4328 * that it implicitly clamps positions greater
4217 4329 * than the string length to be the string
4218 4330 * length. Where Perl completely loses
4219 4331 * coherence, however, is when the specified
4220 4332 * substring is the empty string (""). In
4221 4333 * this case, even if the position is
4222 4334 * negative, rindex() returns 0 -- and even if
4223 4335 * the position is greater than the length,
4224 4336 * index() returns the string length. These
4225 4337 * semantics violate the notion that index()
4226 4338 * should never return a value less than the
4227 4339 * specified position and that rindex() should
4228 4340 * never return a value greater than the
4229 4341 * specified position. (One assumes that
4230 4342 * these semantics are artifacts of Perl's
4231 4343 * implementation and not the results of
4232 4344 * deliberate design -- it beggars belief that
4233 4345 * even Larry Wall could desire such oddness.)
4234 4346 * While in the abstract one would wish for
4235 4347 * consistent position semantics across
4236 4348 * substr(), index() and rindex() -- or at the
4237 4349 * very least self-consistent position
4238 4350 * semantics for index() and rindex() -- we
4239 4351 * instead opt to keep with the extant Perl
4240 4352 * semantics, in all their broken glory. (Do
4241 4353 * we have more desire to maintain Perl's
4242 4354 * semantics than Perl does? Probably.)
4243 4355 */
4244 4356 if (subr == DIF_SUBR_RINDEX) {
4245 4357 if (pos < 0) {
4246 4358 if (sublen == 0)
4247 4359 regs[rd] = 0;
4248 4360 break;
4249 4361 }
4250 4362
4251 4363 if (pos > len)
4252 4364 pos = len;
4253 4365 } else {
4254 4366 if (pos < 0)
4255 4367 pos = 0;
4256 4368
4257 4369 if (pos >= len) {
4258 4370 if (sublen == 0)
4259 4371 regs[rd] = len;
4260 4372 break;
4261 4373 }
4262 4374 }
4263 4375
4264 4376 addr = orig + pos;
4265 4377 }
4266 4378 }
4267 4379
4268 4380 for (regs[rd] = notfound; addr != limit; addr += inc) {
4269 4381 if (dtrace_strncmp(addr, substr, sublen) == 0) {
4270 4382 if (subr != DIF_SUBR_STRSTR) {
4271 4383 /*
4272 4384 * As D index() and rindex() are
4273 4385 * modeled on Perl (and not on awk),
4274 4386 * we return a zero-based (and not a
4275 4387 * one-based) index. (For you Perl
4276 4388 * weenies: no, we're not going to add
4277 4389 * $[ -- and shouldn't you be at a con
4278 4390 * or something?)
4279 4391 */
4280 4392 regs[rd] = (uintptr_t)(addr - orig);
4281 4393 break;
4282 4394 }
4283 4395
4284 4396 ASSERT(subr == DIF_SUBR_STRSTR);
4285 4397 regs[rd] = (uintptr_t)addr;
4286 4398 break;
4287 4399 }
4288 4400 }
4289 4401
4290 4402 break;
4291 4403 }
4292 4404
4293 4405 case DIF_SUBR_STRTOK: {
4294 4406 uintptr_t addr = tupregs[0].dttk_value;
4295 4407 uintptr_t tokaddr = tupregs[1].dttk_value;
4296 4408 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4297 4409 uintptr_t limit, toklimit = tokaddr + size;
4298 4410 uint8_t c, tokmap[32]; /* 256 / 8 */
4299 4411 char *dest = (char *)mstate->dtms_scratch_ptr;
4300 4412 int i;
4301 4413
4302 4414 /*
4303 4415 * Check both the token buffer and (later) the input buffer,
4304 4416 * since both could be non-scratch addresses.
4305 4417 */
4306 4418 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) {
4307 4419 regs[rd] = NULL;
4308 4420 break;
4309 4421 }
4310 4422
4311 4423 if (!DTRACE_INSCRATCH(mstate, size)) {
4312 4424 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4313 4425 regs[rd] = NULL;
4314 4426 break;
4315 4427 }
4316 4428
4317 4429 if (addr == NULL) {
4318 4430 /*
4319 4431 * If the address specified is NULL, we use our saved
4320 4432 * strtok pointer from the mstate. Note that this
4321 4433 * means that the saved strtok pointer is _only_
4322 4434 * valid within multiple enablings of the same probe --
4323 4435 * it behaves like an implicit clause-local variable.
4324 4436 */
4325 4437 addr = mstate->dtms_strtok;
4326 4438 } else {
4327 4439 /*
4328 4440 * If the user-specified address is non-NULL we must
4329 4441 * access check it. This is the only time we have
4330 4442 * a chance to do so, since this address may reside
4331 4443 * in the string table of this clause-- future calls
4332 4444 * (when we fetch addr from mstate->dtms_strtok)
4333 4445 * would fail this access check.
4334 4446 */
4335 4447 if (!dtrace_strcanload(addr, size, mstate, vstate)) {
4336 4448 regs[rd] = NULL;
4337 4449 break;
4338 4450 }
4339 4451 }
4340 4452
4341 4453 /*
4342 4454 * First, zero the token map, and then process the token
4343 4455 * string -- setting a bit in the map for every character
4344 4456 * found in the token string.
4345 4457 */
4346 4458 for (i = 0; i < sizeof (tokmap); i++)
4347 4459 tokmap[i] = 0;
4348 4460
4349 4461 for (; tokaddr < toklimit; tokaddr++) {
4350 4462 if ((c = dtrace_load8(tokaddr)) == '\0')
4351 4463 break;
4352 4464
4353 4465 ASSERT((c >> 3) < sizeof (tokmap));
4354 4466 tokmap[c >> 3] |= (1 << (c & 0x7));
4355 4467 }
4356 4468
4357 4469 for (limit = addr + size; addr < limit; addr++) {
4358 4470 /*
4359 4471 * We're looking for a character that is _not_ contained
4360 4472 * in the token string.
4361 4473 */
4362 4474 if ((c = dtrace_load8(addr)) == '\0')
4363 4475 break;
4364 4476
4365 4477 if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
4366 4478 break;
4367 4479 }
4368 4480
4369 4481 if (c == '\0') {
4370 4482 /*
4371 4483 * We reached the end of the string without finding
4372 4484 * any character that was not in the token string.
4373 4485 * We return NULL in this case, and we set the saved
4374 4486 * address to NULL as well.
4375 4487 */
4376 4488 regs[rd] = NULL;
4377 4489 mstate->dtms_strtok = NULL;
4378 4490 break;
4379 4491 }
4380 4492
4381 4493 /*
4382 4494 * From here on, we're copying into the destination string.
4383 4495 */
4384 4496 for (i = 0; addr < limit && i < size - 1; addr++) {
4385 4497 if ((c = dtrace_load8(addr)) == '\0')
4386 4498 break;
4387 4499
4388 4500 if (tokmap[c >> 3] & (1 << (c & 0x7)))
4389 4501 break;
4390 4502
4391 4503 ASSERT(i < size);
4392 4504 dest[i++] = c;
4393 4505 }
4394 4506
4395 4507 ASSERT(i < size);
4396 4508 dest[i] = '\0';
4397 4509 regs[rd] = (uintptr_t)dest;
4398 4510 mstate->dtms_scratch_ptr += size;
4399 4511 mstate->dtms_strtok = addr;
4400 4512 break;
4401 4513 }
4402 4514
4403 4515 case DIF_SUBR_SUBSTR: {
4404 4516 uintptr_t s = tupregs[0].dttk_value;
4405 4517 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4406 4518 char *d = (char *)mstate->dtms_scratch_ptr;
4407 4519 int64_t index = (int64_t)tupregs[1].dttk_value;
4408 4520 int64_t remaining = (int64_t)tupregs[2].dttk_value;
4409 4521 size_t len = dtrace_strlen((char *)s, size);
4410 4522 int64_t i;
4411 4523
4412 4524 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4413 4525 regs[rd] = NULL;
4414 4526 break;
4415 4527 }
4416 4528
4417 4529 if (!DTRACE_INSCRATCH(mstate, size)) {
4418 4530 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4419 4531 regs[rd] = NULL;
4420 4532 break;
4421 4533 }
4422 4534
4423 4535 if (nargs <= 2)
4424 4536 remaining = (int64_t)size;
4425 4537
4426 4538 if (index < 0) {
4427 4539 index += len;
4428 4540
4429 4541 if (index < 0 && index + remaining > 0) {
4430 4542 remaining += index;
4431 4543 index = 0;
4432 4544 }
4433 4545 }
4434 4546
4435 4547 if (index >= len || index < 0) {
4436 4548 remaining = 0;
4437 4549 } else if (remaining < 0) {
4438 4550 remaining += len - index;
4439 4551 } else if (index + remaining > size) {
4440 4552 remaining = size - index;
4441 4553 }
4442 4554
4443 4555 for (i = 0; i < remaining; i++) {
4444 4556 if ((d[i] = dtrace_load8(s + index + i)) == '\0')
4445 4557 break;
4446 4558 }
4447 4559
4448 4560 d[i] = '\0';
4449 4561
4450 4562 mstate->dtms_scratch_ptr += size;
4451 4563 regs[rd] = (uintptr_t)d;
4452 4564 break;
4453 4565 }
4454 4566
4455 4567 case DIF_SUBR_JSON: {
4456 4568 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4457 4569 uintptr_t json = tupregs[0].dttk_value;
4458 4570 size_t jsonlen = dtrace_strlen((char *)json, size);
4459 4571 uintptr_t elem = tupregs[1].dttk_value;
4460 4572 size_t elemlen = dtrace_strlen((char *)elem, size);
4461 4573
4462 4574 char *dest = (char *)mstate->dtms_scratch_ptr;
4463 4575 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1;
4464 4576 char *ee = elemlist;
4465 4577 int nelems = 1;
4466 4578 uintptr_t cur;
4467 4579
4468 4580 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) ||
4469 4581 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) {
4470 4582 regs[rd] = NULL;
4471 4583 break;
4472 4584 }
4473 4585
4474 4586 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) {
4475 4587 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4476 4588 regs[rd] = NULL;
↓ open down ↓ |
719 lines elided |
↑ open up ↑ |
4477 4589 break;
4478 4590 }
4479 4591
4480 4592 /*
4481 4593 * Read the element selector and split it up into a packed list
4482 4594 * of strings.
4483 4595 */
4484 4596 for (cur = elem; cur < elem + elemlen; cur++) {
4485 4597 char cc = dtrace_load8(cur);
4486 4598
4487 - if (cur == elem && cc == '[')
4488 - /* first element selector may be an array */
4599 + if (cur == elem && cc == '[') {
4600 + /*
4601 + * If the first element selector key is
4602 + * actually an array index then ignore the
4603 + * bracket.
4604 + */
4489 4605 continue;
4606 + }
4490 4607
4491 4608 if (cc == ']')
4492 4609 continue;
4493 4610
4494 4611 if (cc == '.' || cc == '[') {
4495 4612 nelems++;
4496 4613 cc = '\0';
4497 4614 }
4498 4615
4499 4616 *ee++ = cc;
4500 4617 }
4501 4618 *ee++ = '\0';
4502 4619
4503 4620 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist,
4504 4621 nelems, dest)) != NULL)
4505 4622 mstate->dtms_scratch_ptr += jsonlen + 1;
4506 4623 break;
4507 4624 }
4508 4625
4509 4626 case DIF_SUBR_TOUPPER:
4510 4627 case DIF_SUBR_TOLOWER: {
4511 4628 uintptr_t s = tupregs[0].dttk_value;
4512 4629 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4513 4630 char *dest = (char *)mstate->dtms_scratch_ptr, c;
4514 4631 size_t len = dtrace_strlen((char *)s, size);
4515 4632 char lower, upper, convert;
4516 4633 int64_t i;
4517 4634
4518 4635 if (subr == DIF_SUBR_TOUPPER) {
4519 4636 lower = 'a';
4520 4637 upper = 'z';
4521 4638 convert = 'A';
4522 4639 } else {
4523 4640 lower = 'A';
4524 4641 upper = 'Z';
4525 4642 convert = 'a';
4526 4643 }
4527 4644
4528 4645 if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4529 4646 regs[rd] = NULL;
4530 4647 break;
4531 4648 }
4532 4649
4533 4650 if (!DTRACE_INSCRATCH(mstate, size)) {
4534 4651 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4535 4652 regs[rd] = NULL;
4536 4653 break;
4537 4654 }
4538 4655
4539 4656 for (i = 0; i < size - 1; i++) {
4540 4657 if ((c = dtrace_load8(s + i)) == '\0')
4541 4658 break;
4542 4659
4543 4660 if (c >= lower && c <= upper)
4544 4661 c = convert + (c - lower);
4545 4662
4546 4663 dest[i] = c;
4547 4664 }
4548 4665
4549 4666 ASSERT(i < size);
4550 4667 dest[i] = '\0';
4551 4668 regs[rd] = (uintptr_t)dest;
4552 4669 mstate->dtms_scratch_ptr += size;
4553 4670 break;
4554 4671 }
4555 4672
4556 4673 case DIF_SUBR_GETMAJOR:
4557 4674 #ifdef _LP64
4558 4675 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
4559 4676 #else
4560 4677 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
4561 4678 #endif
4562 4679 break;
4563 4680
4564 4681 case DIF_SUBR_GETMINOR:
4565 4682 #ifdef _LP64
4566 4683 regs[rd] = tupregs[0].dttk_value & MAXMIN64;
4567 4684 #else
4568 4685 regs[rd] = tupregs[0].dttk_value & MAXMIN;
4569 4686 #endif
4570 4687 break;
4571 4688
4572 4689 case DIF_SUBR_DDI_PATHNAME: {
4573 4690 /*
4574 4691 * This one is a galactic mess. We are going to roughly
4575 4692 * emulate ddi_pathname(), but it's made more complicated
4576 4693 * by the fact that we (a) want to include the minor name and
4577 4694 * (b) must proceed iteratively instead of recursively.
4578 4695 */
4579 4696 uintptr_t dest = mstate->dtms_scratch_ptr;
4580 4697 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4581 4698 char *start = (char *)dest, *end = start + size - 1;
4582 4699 uintptr_t daddr = tupregs[0].dttk_value;
4583 4700 int64_t minor = (int64_t)tupregs[1].dttk_value;
4584 4701 char *s;
4585 4702 int i, len, depth = 0;
4586 4703
4587 4704 /*
4588 4705 * Due to all the pointer jumping we do and context we must
4589 4706 * rely upon, we just mandate that the user must have kernel
4590 4707 * read privileges to use this routine.
4591 4708 */
4592 4709 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
4593 4710 *flags |= CPU_DTRACE_KPRIV;
4594 4711 *illval = daddr;
4595 4712 regs[rd] = NULL;
4596 4713 }
4597 4714
4598 4715 if (!DTRACE_INSCRATCH(mstate, size)) {
4599 4716 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4600 4717 regs[rd] = NULL;
4601 4718 break;
4602 4719 }
4603 4720
4604 4721 *end = '\0';
4605 4722
4606 4723 /*
4607 4724 * We want to have a name for the minor. In order to do this,
4608 4725 * we need to walk the minor list from the devinfo. We want
4609 4726 * to be sure that we don't infinitely walk a circular list,
4610 4727 * so we check for circularity by sending a scout pointer
4611 4728 * ahead two elements for every element that we iterate over;
4612 4729 * if the list is circular, these will ultimately point to the
4613 4730 * same element. You may recognize this little trick as the
4614 4731 * answer to a stupid interview question -- one that always
4615 4732 * seems to be asked by those who had to have it laboriously
4616 4733 * explained to them, and who can't even concisely describe
4617 4734 * the conditions under which one would be forced to resort to
4618 4735 * this technique. Needless to say, those conditions are
4619 4736 * found here -- and probably only here. Is this the only use
4620 4737 * of this infamous trick in shipping, production code? If it
4621 4738 * isn't, it probably should be...
4622 4739 */
4623 4740 if (minor != -1) {
4624 4741 uintptr_t maddr = dtrace_loadptr(daddr +
4625 4742 offsetof(struct dev_info, devi_minor));
4626 4743
4627 4744 uintptr_t next = offsetof(struct ddi_minor_data, next);
4628 4745 uintptr_t name = offsetof(struct ddi_minor_data,
4629 4746 d_minor) + offsetof(struct ddi_minor, name);
4630 4747 uintptr_t dev = offsetof(struct ddi_minor_data,
4631 4748 d_minor) + offsetof(struct ddi_minor, dev);
4632 4749 uintptr_t scout;
4633 4750
4634 4751 if (maddr != NULL)
4635 4752 scout = dtrace_loadptr(maddr + next);
4636 4753
4637 4754 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4638 4755 uint64_t m;
4639 4756 #ifdef _LP64
4640 4757 m = dtrace_load64(maddr + dev) & MAXMIN64;
4641 4758 #else
4642 4759 m = dtrace_load32(maddr + dev) & MAXMIN;
4643 4760 #endif
4644 4761 if (m != minor) {
4645 4762 maddr = dtrace_loadptr(maddr + next);
4646 4763
4647 4764 if (scout == NULL)
4648 4765 continue;
4649 4766
4650 4767 scout = dtrace_loadptr(scout + next);
4651 4768
4652 4769 if (scout == NULL)
4653 4770 continue;
4654 4771
4655 4772 scout = dtrace_loadptr(scout + next);
4656 4773
4657 4774 if (scout == NULL)
4658 4775 continue;
4659 4776
4660 4777 if (scout == maddr) {
4661 4778 *flags |= CPU_DTRACE_ILLOP;
4662 4779 break;
4663 4780 }
4664 4781
4665 4782 continue;
4666 4783 }
4667 4784
4668 4785 /*
4669 4786 * We have the minor data. Now we need to
4670 4787 * copy the minor's name into the end of the
4671 4788 * pathname.
4672 4789 */
4673 4790 s = (char *)dtrace_loadptr(maddr + name);
4674 4791 len = dtrace_strlen(s, size);
4675 4792
4676 4793 if (*flags & CPU_DTRACE_FAULT)
4677 4794 break;
4678 4795
4679 4796 if (len != 0) {
4680 4797 if ((end -= (len + 1)) < start)
4681 4798 break;
4682 4799
4683 4800 *end = ':';
4684 4801 }
4685 4802
4686 4803 for (i = 1; i <= len; i++)
4687 4804 end[i] = dtrace_load8((uintptr_t)s++);
4688 4805 break;
4689 4806 }
4690 4807 }
4691 4808
4692 4809 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
4693 4810 ddi_node_state_t devi_state;
4694 4811
4695 4812 devi_state = dtrace_load32(daddr +
4696 4813 offsetof(struct dev_info, devi_node_state));
4697 4814
4698 4815 if (*flags & CPU_DTRACE_FAULT)
4699 4816 break;
4700 4817
4701 4818 if (devi_state >= DS_INITIALIZED) {
4702 4819 s = (char *)dtrace_loadptr(daddr +
4703 4820 offsetof(struct dev_info, devi_addr));
4704 4821 len = dtrace_strlen(s, size);
4705 4822
4706 4823 if (*flags & CPU_DTRACE_FAULT)
4707 4824 break;
4708 4825
4709 4826 if (len != 0) {
4710 4827 if ((end -= (len + 1)) < start)
4711 4828 break;
4712 4829
4713 4830 *end = '@';
4714 4831 }
4715 4832
4716 4833 for (i = 1; i <= len; i++)
4717 4834 end[i] = dtrace_load8((uintptr_t)s++);
4718 4835 }
4719 4836
4720 4837 /*
4721 4838 * Now for the node name...
4722 4839 */
4723 4840 s = (char *)dtrace_loadptr(daddr +
4724 4841 offsetof(struct dev_info, devi_node_name));
4725 4842
4726 4843 daddr = dtrace_loadptr(daddr +
4727 4844 offsetof(struct dev_info, devi_parent));
4728 4845
4729 4846 /*
4730 4847 * If our parent is NULL (that is, if we're the root
4731 4848 * node), we're going to use the special path
4732 4849 * "devices".
4733 4850 */
4734 4851 if (daddr == NULL)
4735 4852 s = "devices";
4736 4853
4737 4854 len = dtrace_strlen(s, size);
4738 4855 if (*flags & CPU_DTRACE_FAULT)
4739 4856 break;
4740 4857
4741 4858 if ((end -= (len + 1)) < start)
4742 4859 break;
4743 4860
4744 4861 for (i = 1; i <= len; i++)
4745 4862 end[i] = dtrace_load8((uintptr_t)s++);
4746 4863 *end = '/';
4747 4864
4748 4865 if (depth++ > dtrace_devdepth_max) {
4749 4866 *flags |= CPU_DTRACE_ILLOP;
4750 4867 break;
4751 4868 }
4752 4869 }
4753 4870
4754 4871 if (end < start)
4755 4872 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4756 4873
4757 4874 if (daddr == NULL) {
4758 4875 regs[rd] = (uintptr_t)end;
4759 4876 mstate->dtms_scratch_ptr += size;
4760 4877 }
4761 4878
4762 4879 break;
4763 4880 }
4764 4881
4765 4882 case DIF_SUBR_STRJOIN: {
4766 4883 char *d = (char *)mstate->dtms_scratch_ptr;
4767 4884 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4768 4885 uintptr_t s1 = tupregs[0].dttk_value;
4769 4886 uintptr_t s2 = tupregs[1].dttk_value;
4770 4887 int i = 0;
4771 4888
4772 4889 if (!dtrace_strcanload(s1, size, mstate, vstate) ||
4773 4890 !dtrace_strcanload(s2, size, mstate, vstate)) {
4774 4891 regs[rd] = NULL;
4775 4892 break;
4776 4893 }
4777 4894
4778 4895 if (!DTRACE_INSCRATCH(mstate, size)) {
4779 4896 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4780 4897 regs[rd] = NULL;
4781 4898 break;
4782 4899 }
4783 4900
4784 4901 for (;;) {
4785 4902 if (i >= size) {
4786 4903 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4787 4904 regs[rd] = NULL;
4788 4905 break;
4789 4906 }
4790 4907
4791 4908 if ((d[i++] = dtrace_load8(s1++)) == '\0') {
4792 4909 i--;
4793 4910 break;
4794 4911 }
4795 4912 }
4796 4913
4797 4914 for (;;) {
4798 4915 if (i >= size) {
4799 4916 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4800 4917 regs[rd] = NULL;
4801 4918 break;
4802 4919 }
4803 4920
4804 4921 if ((d[i++] = dtrace_load8(s2++)) == '\0')
4805 4922 break;
4806 4923 }
4807 4924
4808 4925 if (i < size) {
4809 4926 mstate->dtms_scratch_ptr += i;
4810 4927 regs[rd] = (uintptr_t)d;
4811 4928 }
4812 4929
4813 4930 break;
4814 4931 }
4815 4932
4816 4933 case DIF_SUBR_STRTOLL: {
4817 4934 uintptr_t s = tupregs[0].dttk_value;
4818 4935 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4819 4936 int base = 10;
4820 4937
4821 4938 if (nargs > 1) {
4822 4939 if ((base = tupregs[1].dttk_value) <= 1 ||
4823 4940 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4824 4941 *flags |= CPU_DTRACE_ILLOP;
4825 4942 break;
4826 4943 }
4827 4944 }
4828 4945
4829 4946 if (!dtrace_strcanload(s, size, mstate, vstate)) {
4830 4947 regs[rd] = INT64_MIN;
4831 4948 break;
4832 4949 }
4833 4950
4834 4951 regs[rd] = dtrace_strtoll((char *)s, base, size);
4835 4952 break;
4836 4953 }
4837 4954
4838 4955 case DIF_SUBR_LLTOSTR: {
4839 4956 int64_t i = (int64_t)tupregs[0].dttk_value;
4840 4957 uint64_t val, digit;
4841 4958 uint64_t size = 65; /* enough room for 2^64 in binary */
4842 4959 char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
4843 4960 int base = 10;
4844 4961
4845 4962 if (nargs > 1) {
4846 4963 if ((base = tupregs[1].dttk_value) <= 1 ||
4847 4964 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4848 4965 *flags |= CPU_DTRACE_ILLOP;
4849 4966 break;
4850 4967 }
4851 4968 }
4852 4969
4853 4970 val = (base == 10 && i < 0) ? i * -1 : i;
4854 4971
4855 4972 if (!DTRACE_INSCRATCH(mstate, size)) {
4856 4973 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4857 4974 regs[rd] = NULL;
4858 4975 break;
4859 4976 }
4860 4977
4861 4978 for (*end-- = '\0'; val; val /= base) {
4862 4979 if ((digit = val % base) <= '9' - '0') {
4863 4980 *end-- = '0' + digit;
4864 4981 } else {
4865 4982 *end-- = 'a' + (digit - ('9' - '0') - 1);
4866 4983 }
4867 4984 }
4868 4985
4869 4986 if (i == 0 && base == 16)
4870 4987 *end-- = '0';
4871 4988
4872 4989 if (base == 16)
4873 4990 *end-- = 'x';
4874 4991
4875 4992 if (i == 0 || base == 8 || base == 16)
4876 4993 *end-- = '0';
4877 4994
4878 4995 if (i < 0 && base == 10)
4879 4996 *end-- = '-';
4880 4997
4881 4998 regs[rd] = (uintptr_t)end + 1;
4882 4999 mstate->dtms_scratch_ptr += size;
4883 5000 break;
4884 5001 }
4885 5002
4886 5003 case DIF_SUBR_HTONS:
4887 5004 case DIF_SUBR_NTOHS:
4888 5005 #ifdef _BIG_ENDIAN
4889 5006 regs[rd] = (uint16_t)tupregs[0].dttk_value;
4890 5007 #else
4891 5008 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
4892 5009 #endif
4893 5010 break;
4894 5011
4895 5012
4896 5013 case DIF_SUBR_HTONL:
4897 5014 case DIF_SUBR_NTOHL:
4898 5015 #ifdef _BIG_ENDIAN
4899 5016 regs[rd] = (uint32_t)tupregs[0].dttk_value;
4900 5017 #else
4901 5018 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
4902 5019 #endif
4903 5020 break;
4904 5021
4905 5022
4906 5023 case DIF_SUBR_HTONLL:
4907 5024 case DIF_SUBR_NTOHLL:
4908 5025 #ifdef _BIG_ENDIAN
4909 5026 regs[rd] = (uint64_t)tupregs[0].dttk_value;
4910 5027 #else
4911 5028 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
4912 5029 #endif
4913 5030 break;
4914 5031
4915 5032
4916 5033 case DIF_SUBR_DIRNAME:
4917 5034 case DIF_SUBR_BASENAME: {
4918 5035 char *dest = (char *)mstate->dtms_scratch_ptr;
4919 5036 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4920 5037 uintptr_t src = tupregs[0].dttk_value;
4921 5038 int i, j, len = dtrace_strlen((char *)src, size);
4922 5039 int lastbase = -1, firstbase = -1, lastdir = -1;
4923 5040 int start, end;
4924 5041
4925 5042 if (!dtrace_canload(src, len + 1, mstate, vstate)) {
4926 5043 regs[rd] = NULL;
4927 5044 break;
4928 5045 }
4929 5046
4930 5047 if (!DTRACE_INSCRATCH(mstate, size)) {
4931 5048 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4932 5049 regs[rd] = NULL;
4933 5050 break;
4934 5051 }
4935 5052
4936 5053 /*
4937 5054 * The basename and dirname for a zero-length string is
4938 5055 * defined to be "."
4939 5056 */
4940 5057 if (len == 0) {
4941 5058 len = 1;
4942 5059 src = (uintptr_t)".";
4943 5060 }
4944 5061
4945 5062 /*
4946 5063 * Start from the back of the string, moving back toward the
4947 5064 * front until we see a character that isn't a slash. That
4948 5065 * character is the last character in the basename.
4949 5066 */
4950 5067 for (i = len - 1; i >= 0; i--) {
4951 5068 if (dtrace_load8(src + i) != '/')
4952 5069 break;
4953 5070 }
4954 5071
4955 5072 if (i >= 0)
4956 5073 lastbase = i;
4957 5074
4958 5075 /*
4959 5076 * Starting from the last character in the basename, move
4960 5077 * towards the front until we find a slash. The character
4961 5078 * that we processed immediately before that is the first
4962 5079 * character in the basename.
4963 5080 */
4964 5081 for (; i >= 0; i--) {
4965 5082 if (dtrace_load8(src + i) == '/')
4966 5083 break;
4967 5084 }
4968 5085
4969 5086 if (i >= 0)
4970 5087 firstbase = i + 1;
4971 5088
4972 5089 /*
4973 5090 * Now keep going until we find a non-slash character. That
4974 5091 * character is the last character in the dirname.
4975 5092 */
4976 5093 for (; i >= 0; i--) {
4977 5094 if (dtrace_load8(src + i) != '/')
4978 5095 break;
4979 5096 }
4980 5097
4981 5098 if (i >= 0)
4982 5099 lastdir = i;
4983 5100
4984 5101 ASSERT(!(lastbase == -1 && firstbase != -1));
4985 5102 ASSERT(!(firstbase == -1 && lastdir != -1));
4986 5103
4987 5104 if (lastbase == -1) {
4988 5105 /*
4989 5106 * We didn't find a non-slash character. We know that
4990 5107 * the length is non-zero, so the whole string must be
4991 5108 * slashes. In either the dirname or the basename
4992 5109 * case, we return '/'.
4993 5110 */
4994 5111 ASSERT(firstbase == -1);
4995 5112 firstbase = lastbase = lastdir = 0;
4996 5113 }
4997 5114
4998 5115 if (firstbase == -1) {
4999 5116 /*
5000 5117 * The entire string consists only of a basename
5001 5118 * component. If we're looking for dirname, we need
5002 5119 * to change our string to be just "."; if we're
5003 5120 * looking for a basename, we'll just set the first
5004 5121 * character of the basename to be 0.
5005 5122 */
5006 5123 if (subr == DIF_SUBR_DIRNAME) {
5007 5124 ASSERT(lastdir == -1);
5008 5125 src = (uintptr_t)".";
5009 5126 lastdir = 0;
5010 5127 } else {
5011 5128 firstbase = 0;
5012 5129 }
5013 5130 }
5014 5131
5015 5132 if (subr == DIF_SUBR_DIRNAME) {
5016 5133 if (lastdir == -1) {
5017 5134 /*
5018 5135 * We know that we have a slash in the name --
5019 5136 * or lastdir would be set to 0, above. And
5020 5137 * because lastdir is -1, we know that this
5021 5138 * slash must be the first character. (That
5022 5139 * is, the full string must be of the form
5023 5140 * "/basename".) In this case, the last
5024 5141 * character of the directory name is 0.
5025 5142 */
5026 5143 lastdir = 0;
5027 5144 }
5028 5145
5029 5146 start = 0;
5030 5147 end = lastdir;
5031 5148 } else {
5032 5149 ASSERT(subr == DIF_SUBR_BASENAME);
5033 5150 ASSERT(firstbase != -1 && lastbase != -1);
5034 5151 start = firstbase;
5035 5152 end = lastbase;
5036 5153 }
5037 5154
5038 5155 for (i = start, j = 0; i <= end && j < size - 1; i++, j++)
5039 5156 dest[j] = dtrace_load8(src + i);
5040 5157
5041 5158 dest[j] = '\0';
5042 5159 regs[rd] = (uintptr_t)dest;
5043 5160 mstate->dtms_scratch_ptr += size;
5044 5161 break;
5045 5162 }
5046 5163
5047 5164 case DIF_SUBR_GETF: {
5048 5165 uintptr_t fd = tupregs[0].dttk_value;
5049 5166 uf_info_t *finfo = &curthread->t_procp->p_user.u_finfo;
5050 5167 file_t *fp;
5051 5168
5052 5169 if (!dtrace_priv_proc(state, mstate)) {
5053 5170 regs[rd] = NULL;
5054 5171 break;
5055 5172 }
5056 5173
5057 5174 /*
5058 5175 * This is safe because fi_nfiles only increases, and the
5059 5176 * fi_list array is not freed when the array size doubles.
5060 5177 * (See the comment in flist_grow() for details on the
5061 5178 * management of the u_finfo structure.)
5062 5179 */
5063 5180 fp = fd < finfo->fi_nfiles ? finfo->fi_list[fd].uf_file : NULL;
5064 5181
5065 5182 mstate->dtms_getf = fp;
5066 5183 regs[rd] = (uintptr_t)fp;
5067 5184 break;
5068 5185 }
5069 5186
5070 5187 case DIF_SUBR_CLEANPATH: {
5071 5188 char *dest = (char *)mstate->dtms_scratch_ptr, c;
5072 5189 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5073 5190 uintptr_t src = tupregs[0].dttk_value;
5074 5191 int i = 0, j = 0;
5075 5192 zone_t *z;
5076 5193
5077 5194 if (!dtrace_strcanload(src, size, mstate, vstate)) {
5078 5195 regs[rd] = NULL;
5079 5196 break;
5080 5197 }
5081 5198
5082 5199 if (!DTRACE_INSCRATCH(mstate, size)) {
5083 5200 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5084 5201 regs[rd] = NULL;
5085 5202 break;
5086 5203 }
5087 5204
5088 5205 /*
5089 5206 * Move forward, loading each character.
5090 5207 */
5091 5208 do {
5092 5209 c = dtrace_load8(src + i++);
5093 5210 next:
5094 5211 if (j + 5 >= size) /* 5 = strlen("/..c\0") */
5095 5212 break;
5096 5213
5097 5214 if (c != '/') {
5098 5215 dest[j++] = c;
5099 5216 continue;
5100 5217 }
5101 5218
5102 5219 c = dtrace_load8(src + i++);
5103 5220
5104 5221 if (c == '/') {
5105 5222 /*
5106 5223 * We have two slashes -- we can just advance
5107 5224 * to the next character.
5108 5225 */
5109 5226 goto next;
5110 5227 }
5111 5228
5112 5229 if (c != '.') {
5113 5230 /*
5114 5231 * This is not "." and it's not ".." -- we can
5115 5232 * just store the "/" and this character and
5116 5233 * drive on.
5117 5234 */
5118 5235 dest[j++] = '/';
5119 5236 dest[j++] = c;
5120 5237 continue;
5121 5238 }
5122 5239
5123 5240 c = dtrace_load8(src + i++);
5124 5241
5125 5242 if (c == '/') {
5126 5243 /*
5127 5244 * This is a "/./" component. We're not going
5128 5245 * to store anything in the destination buffer;
5129 5246 * we're just going to go to the next component.
5130 5247 */
5131 5248 goto next;
5132 5249 }
5133 5250
5134 5251 if (c != '.') {
5135 5252 /*
5136 5253 * This is not ".." -- we can just store the
5137 5254 * "/." and this character and continue
5138 5255 * processing.
5139 5256 */
5140 5257 dest[j++] = '/';
5141 5258 dest[j++] = '.';
5142 5259 dest[j++] = c;
5143 5260 continue;
5144 5261 }
5145 5262
5146 5263 c = dtrace_load8(src + i++);
5147 5264
5148 5265 if (c != '/' && c != '\0') {
5149 5266 /*
5150 5267 * This is not ".." -- it's "..[mumble]".
5151 5268 * We'll store the "/.." and this character
5152 5269 * and continue processing.
5153 5270 */
5154 5271 dest[j++] = '/';
5155 5272 dest[j++] = '.';
5156 5273 dest[j++] = '.';
5157 5274 dest[j++] = c;
5158 5275 continue;
5159 5276 }
5160 5277
5161 5278 /*
5162 5279 * This is "/../" or "/..\0". We need to back up
5163 5280 * our destination pointer until we find a "/".
5164 5281 */
5165 5282 i--;
5166 5283 while (j != 0 && dest[--j] != '/')
5167 5284 continue;
5168 5285
5169 5286 if (c == '\0')
5170 5287 dest[++j] = '/';
5171 5288 } while (c != '\0');
5172 5289
5173 5290 dest[j] = '\0';
5174 5291
5175 5292 if (mstate->dtms_getf != NULL &&
5176 5293 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) &&
5177 5294 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) {
5178 5295 /*
5179 5296 * If we've done a getf() as a part of this ECB and we
5180 5297 * don't have kernel access (and we're not in the global
5181 5298 * zone), check if the path we cleaned up begins with
5182 5299 * the zone's root path, and trim it off if so. Note
5183 5300 * that this is an output cleanliness issue, not a
5184 5301 * security issue: knowing one's zone root path does
5185 5302 * not enable privilege escalation.
5186 5303 */
5187 5304 if (strstr(dest, z->zone_rootpath) == dest)
5188 5305 dest += strlen(z->zone_rootpath) - 1;
5189 5306 }
5190 5307
5191 5308 regs[rd] = (uintptr_t)dest;
5192 5309 mstate->dtms_scratch_ptr += size;
5193 5310 break;
5194 5311 }
5195 5312
5196 5313 case DIF_SUBR_INET_NTOA:
5197 5314 case DIF_SUBR_INET_NTOA6:
5198 5315 case DIF_SUBR_INET_NTOP: {
5199 5316 size_t size;
5200 5317 int af, argi, i;
5201 5318 char *base, *end;
5202 5319
5203 5320 if (subr == DIF_SUBR_INET_NTOP) {
5204 5321 af = (int)tupregs[0].dttk_value;
5205 5322 argi = 1;
5206 5323 } else {
5207 5324 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
5208 5325 argi = 0;
5209 5326 }
5210 5327
5211 5328 if (af == AF_INET) {
5212 5329 ipaddr_t ip4;
5213 5330 uint8_t *ptr8, val;
5214 5331
5215 5332 /*
5216 5333 * Safely load the IPv4 address.
5217 5334 */
5218 5335 ip4 = dtrace_load32(tupregs[argi].dttk_value);
5219 5336
5220 5337 /*
5221 5338 * Check an IPv4 string will fit in scratch.
5222 5339 */
5223 5340 size = INET_ADDRSTRLEN;
5224 5341 if (!DTRACE_INSCRATCH(mstate, size)) {
5225 5342 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5226 5343 regs[rd] = NULL;
5227 5344 break;
5228 5345 }
5229 5346 base = (char *)mstate->dtms_scratch_ptr;
5230 5347 end = (char *)mstate->dtms_scratch_ptr + size - 1;
5231 5348
5232 5349 /*
5233 5350 * Stringify as a dotted decimal quad.
5234 5351 */
5235 5352 *end-- = '\0';
5236 5353 ptr8 = (uint8_t *)&ip4;
5237 5354 for (i = 3; i >= 0; i--) {
5238 5355 val = ptr8[i];
5239 5356
5240 5357 if (val == 0) {
5241 5358 *end-- = '0';
5242 5359 } else {
5243 5360 for (; val; val /= 10) {
5244 5361 *end-- = '0' + (val % 10);
5245 5362 }
5246 5363 }
5247 5364
5248 5365 if (i > 0)
5249 5366 *end-- = '.';
5250 5367 }
5251 5368 ASSERT(end + 1 >= base);
5252 5369
5253 5370 } else if (af == AF_INET6) {
5254 5371 struct in6_addr ip6;
5255 5372 int firstzero, tryzero, numzero, v6end;
5256 5373 uint16_t val;
5257 5374 const char digits[] = "0123456789abcdef";
5258 5375
5259 5376 /*
5260 5377 * Stringify using RFC 1884 convention 2 - 16 bit
5261 5378 * hexadecimal values with a zero-run compression.
5262 5379 * Lower case hexadecimal digits are used.
5263 5380 * eg, fe80::214:4fff:fe0b:76c8.
5264 5381 * The IPv4 embedded form is returned for inet_ntop,
5265 5382 * just the IPv4 string is returned for inet_ntoa6.
5266 5383 */
5267 5384
5268 5385 /*
5269 5386 * Safely load the IPv6 address.
5270 5387 */
5271 5388 dtrace_bcopy(
5272 5389 (void *)(uintptr_t)tupregs[argi].dttk_value,
5273 5390 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
5274 5391
5275 5392 /*
5276 5393 * Check an IPv6 string will fit in scratch.
5277 5394 */
5278 5395 size = INET6_ADDRSTRLEN;
5279 5396 if (!DTRACE_INSCRATCH(mstate, size)) {
5280 5397 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5281 5398 regs[rd] = NULL;
5282 5399 break;
5283 5400 }
5284 5401 base = (char *)mstate->dtms_scratch_ptr;
5285 5402 end = (char *)mstate->dtms_scratch_ptr + size - 1;
5286 5403 *end-- = '\0';
5287 5404
5288 5405 /*
5289 5406 * Find the longest run of 16 bit zero values
5290 5407 * for the single allowed zero compression - "::".
5291 5408 */
5292 5409 firstzero = -1;
5293 5410 tryzero = -1;
5294 5411 numzero = 1;
5295 5412 for (i = 0; i < sizeof (struct in6_addr); i++) {
5296 5413 if (ip6._S6_un._S6_u8[i] == 0 &&
5297 5414 tryzero == -1 && i % 2 == 0) {
5298 5415 tryzero = i;
5299 5416 continue;
5300 5417 }
5301 5418
5302 5419 if (tryzero != -1 &&
5303 5420 (ip6._S6_un._S6_u8[i] != 0 ||
5304 5421 i == sizeof (struct in6_addr) - 1)) {
5305 5422
5306 5423 if (i - tryzero <= numzero) {
5307 5424 tryzero = -1;
5308 5425 continue;
5309 5426 }
5310 5427
5311 5428 firstzero = tryzero;
5312 5429 numzero = i - i % 2 - tryzero;
5313 5430 tryzero = -1;
5314 5431
5315 5432 if (ip6._S6_un._S6_u8[i] == 0 &&
5316 5433 i == sizeof (struct in6_addr) - 1)
5317 5434 numzero += 2;
5318 5435 }
5319 5436 }
5320 5437 ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
5321 5438
5322 5439 /*
5323 5440 * Check for an IPv4 embedded address.
5324 5441 */
5325 5442 v6end = sizeof (struct in6_addr) - 2;
5326 5443 if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
5327 5444 IN6_IS_ADDR_V4COMPAT(&ip6)) {
5328 5445 for (i = sizeof (struct in6_addr) - 1;
5329 5446 i >= DTRACE_V4MAPPED_OFFSET; i--) {
5330 5447 ASSERT(end >= base);
5331 5448
5332 5449 val = ip6._S6_un._S6_u8[i];
5333 5450
5334 5451 if (val == 0) {
5335 5452 *end-- = '0';
5336 5453 } else {
5337 5454 for (; val; val /= 10) {
5338 5455 *end-- = '0' + val % 10;
5339 5456 }
5340 5457 }
5341 5458
5342 5459 if (i > DTRACE_V4MAPPED_OFFSET)
5343 5460 *end-- = '.';
5344 5461 }
5345 5462
5346 5463 if (subr == DIF_SUBR_INET_NTOA6)
5347 5464 goto inetout;
5348 5465
5349 5466 /*
5350 5467 * Set v6end to skip the IPv4 address that
5351 5468 * we have already stringified.
5352 5469 */
5353 5470 v6end = 10;
5354 5471 }
5355 5472
5356 5473 /*
5357 5474 * Build the IPv6 string by working through the
5358 5475 * address in reverse.
5359 5476 */
5360 5477 for (i = v6end; i >= 0; i -= 2) {
5361 5478 ASSERT(end >= base);
5362 5479
5363 5480 if (i == firstzero + numzero - 2) {
5364 5481 *end-- = ':';
5365 5482 *end-- = ':';
5366 5483 i -= numzero - 2;
5367 5484 continue;
5368 5485 }
5369 5486
5370 5487 if (i < 14 && i != firstzero - 2)
5371 5488 *end-- = ':';
5372 5489
5373 5490 val = (ip6._S6_un._S6_u8[i] << 8) +
5374 5491 ip6._S6_un._S6_u8[i + 1];
5375 5492
5376 5493 if (val == 0) {
5377 5494 *end-- = '0';
5378 5495 } else {
5379 5496 for (; val; val /= 16) {
5380 5497 *end-- = digits[val % 16];
5381 5498 }
5382 5499 }
5383 5500 }
5384 5501 ASSERT(end + 1 >= base);
5385 5502
5386 5503 } else {
5387 5504 /*
5388 5505 * The user didn't use AH_INET or AH_INET6.
5389 5506 */
5390 5507 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5391 5508 regs[rd] = NULL;
5392 5509 break;
5393 5510 }
5394 5511
5395 5512 inetout: regs[rd] = (uintptr_t)end + 1;
5396 5513 mstate->dtms_scratch_ptr += size;
5397 5514 break;
5398 5515 }
5399 5516
5400 5517 }
5401 5518 }
5402 5519
5403 5520 /*
5404 5521 * Emulate the execution of DTrace IR instructions specified by the given
5405 5522 * DIF object. This function is deliberately void of assertions as all of
5406 5523 * the necessary checks are handled by a call to dtrace_difo_validate().
5407 5524 */
5408 5525 static uint64_t
5409 5526 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
5410 5527 dtrace_vstate_t *vstate, dtrace_state_t *state)
5411 5528 {
5412 5529 const dif_instr_t *text = difo->dtdo_buf;
5413 5530 const uint_t textlen = difo->dtdo_len;
5414 5531 const char *strtab = difo->dtdo_strtab;
5415 5532 const uint64_t *inttab = difo->dtdo_inttab;
5416 5533
5417 5534 uint64_t rval = 0;
5418 5535 dtrace_statvar_t *svar;
5419 5536 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
5420 5537 dtrace_difv_t *v;
5421 5538 volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
5422 5539 volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
5423 5540
5424 5541 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
5425 5542 uint64_t regs[DIF_DIR_NREGS];
5426 5543 uint64_t *tmp;
5427 5544
5428 5545 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
5429 5546 int64_t cc_r;
5430 5547 uint_t pc = 0, id, opc;
5431 5548 uint8_t ttop = 0;
5432 5549 dif_instr_t instr;
5433 5550 uint_t r1, r2, rd;
5434 5551
5435 5552 /*
5436 5553 * We stash the current DIF object into the machine state: we need it
5437 5554 * for subsequent access checking.
5438 5555 */
5439 5556 mstate->dtms_difo = difo;
5440 5557
5441 5558 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */
5442 5559
5443 5560 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
5444 5561 opc = pc;
5445 5562
5446 5563 instr = text[pc++];
5447 5564 r1 = DIF_INSTR_R1(instr);
5448 5565 r2 = DIF_INSTR_R2(instr);
5449 5566 rd = DIF_INSTR_RD(instr);
5450 5567
5451 5568 switch (DIF_INSTR_OP(instr)) {
5452 5569 case DIF_OP_OR:
5453 5570 regs[rd] = regs[r1] | regs[r2];
5454 5571 break;
5455 5572 case DIF_OP_XOR:
5456 5573 regs[rd] = regs[r1] ^ regs[r2];
5457 5574 break;
5458 5575 case DIF_OP_AND:
5459 5576 regs[rd] = regs[r1] & regs[r2];
5460 5577 break;
5461 5578 case DIF_OP_SLL:
5462 5579 regs[rd] = regs[r1] << regs[r2];
5463 5580 break;
5464 5581 case DIF_OP_SRL:
5465 5582 regs[rd] = regs[r1] >> regs[r2];
5466 5583 break;
5467 5584 case DIF_OP_SUB:
5468 5585 regs[rd] = regs[r1] - regs[r2];
5469 5586 break;
5470 5587 case DIF_OP_ADD:
5471 5588 regs[rd] = regs[r1] + regs[r2];
5472 5589 break;
5473 5590 case DIF_OP_MUL:
5474 5591 regs[rd] = regs[r1] * regs[r2];
5475 5592 break;
5476 5593 case DIF_OP_SDIV:
5477 5594 if (regs[r2] == 0) {
5478 5595 regs[rd] = 0;
5479 5596 *flags |= CPU_DTRACE_DIVZERO;
5480 5597 } else {
5481 5598 regs[rd] = (int64_t)regs[r1] /
5482 5599 (int64_t)regs[r2];
5483 5600 }
5484 5601 break;
5485 5602
5486 5603 case DIF_OP_UDIV:
5487 5604 if (regs[r2] == 0) {
5488 5605 regs[rd] = 0;
5489 5606 *flags |= CPU_DTRACE_DIVZERO;
5490 5607 } else {
5491 5608 regs[rd] = regs[r1] / regs[r2];
5492 5609 }
5493 5610 break;
5494 5611
5495 5612 case DIF_OP_SREM:
5496 5613 if (regs[r2] == 0) {
5497 5614 regs[rd] = 0;
5498 5615 *flags |= CPU_DTRACE_DIVZERO;
5499 5616 } else {
5500 5617 regs[rd] = (int64_t)regs[r1] %
5501 5618 (int64_t)regs[r2];
5502 5619 }
5503 5620 break;
5504 5621
5505 5622 case DIF_OP_UREM:
5506 5623 if (regs[r2] == 0) {
5507 5624 regs[rd] = 0;
5508 5625 *flags |= CPU_DTRACE_DIVZERO;
5509 5626 } else {
5510 5627 regs[rd] = regs[r1] % regs[r2];
5511 5628 }
5512 5629 break;
5513 5630
5514 5631 case DIF_OP_NOT:
5515 5632 regs[rd] = ~regs[r1];
5516 5633 break;
5517 5634 case DIF_OP_MOV:
5518 5635 regs[rd] = regs[r1];
5519 5636 break;
5520 5637 case DIF_OP_CMP:
5521 5638 cc_r = regs[r1] - regs[r2];
5522 5639 cc_n = cc_r < 0;
5523 5640 cc_z = cc_r == 0;
5524 5641 cc_v = 0;
5525 5642 cc_c = regs[r1] < regs[r2];
5526 5643 break;
5527 5644 case DIF_OP_TST:
5528 5645 cc_n = cc_v = cc_c = 0;
5529 5646 cc_z = regs[r1] == 0;
5530 5647 break;
5531 5648 case DIF_OP_BA:
5532 5649 pc = DIF_INSTR_LABEL(instr);
5533 5650 break;
5534 5651 case DIF_OP_BE:
5535 5652 if (cc_z)
5536 5653 pc = DIF_INSTR_LABEL(instr);
5537 5654 break;
5538 5655 case DIF_OP_BNE:
5539 5656 if (cc_z == 0)
5540 5657 pc = DIF_INSTR_LABEL(instr);
5541 5658 break;
5542 5659 case DIF_OP_BG:
5543 5660 if ((cc_z | (cc_n ^ cc_v)) == 0)
5544 5661 pc = DIF_INSTR_LABEL(instr);
5545 5662 break;
5546 5663 case DIF_OP_BGU:
5547 5664 if ((cc_c | cc_z) == 0)
5548 5665 pc = DIF_INSTR_LABEL(instr);
5549 5666 break;
5550 5667 case DIF_OP_BGE:
5551 5668 if ((cc_n ^ cc_v) == 0)
5552 5669 pc = DIF_INSTR_LABEL(instr);
5553 5670 break;
5554 5671 case DIF_OP_BGEU:
5555 5672 if (cc_c == 0)
5556 5673 pc = DIF_INSTR_LABEL(instr);
5557 5674 break;
5558 5675 case DIF_OP_BL:
5559 5676 if (cc_n ^ cc_v)
5560 5677 pc = DIF_INSTR_LABEL(instr);
5561 5678 break;
5562 5679 case DIF_OP_BLU:
5563 5680 if (cc_c)
5564 5681 pc = DIF_INSTR_LABEL(instr);
5565 5682 break;
5566 5683 case DIF_OP_BLE:
5567 5684 if (cc_z | (cc_n ^ cc_v))
5568 5685 pc = DIF_INSTR_LABEL(instr);
5569 5686 break;
5570 5687 case DIF_OP_BLEU:
5571 5688 if (cc_c | cc_z)
5572 5689 pc = DIF_INSTR_LABEL(instr);
5573 5690 break;
5574 5691 case DIF_OP_RLDSB:
5575 5692 if (!dtrace_canload(regs[r1], 1, mstate, vstate))
5576 5693 break;
5577 5694 /*FALLTHROUGH*/
5578 5695 case DIF_OP_LDSB:
5579 5696 regs[rd] = (int8_t)dtrace_load8(regs[r1]);
5580 5697 break;
5581 5698 case DIF_OP_RLDSH:
5582 5699 if (!dtrace_canload(regs[r1], 2, mstate, vstate))
5583 5700 break;
5584 5701 /*FALLTHROUGH*/
5585 5702 case DIF_OP_LDSH:
5586 5703 regs[rd] = (int16_t)dtrace_load16(regs[r1]);
5587 5704 break;
5588 5705 case DIF_OP_RLDSW:
5589 5706 if (!dtrace_canload(regs[r1], 4, mstate, vstate))
5590 5707 break;
5591 5708 /*FALLTHROUGH*/
5592 5709 case DIF_OP_LDSW:
5593 5710 regs[rd] = (int32_t)dtrace_load32(regs[r1]);
5594 5711 break;
5595 5712 case DIF_OP_RLDUB:
5596 5713 if (!dtrace_canload(regs[r1], 1, mstate, vstate))
5597 5714 break;
5598 5715 /*FALLTHROUGH*/
5599 5716 case DIF_OP_LDUB:
5600 5717 regs[rd] = dtrace_load8(regs[r1]);
5601 5718 break;
5602 5719 case DIF_OP_RLDUH:
5603 5720 if (!dtrace_canload(regs[r1], 2, mstate, vstate))
5604 5721 break;
5605 5722 /*FALLTHROUGH*/
5606 5723 case DIF_OP_LDUH:
5607 5724 regs[rd] = dtrace_load16(regs[r1]);
5608 5725 break;
5609 5726 case DIF_OP_RLDUW:
5610 5727 if (!dtrace_canload(regs[r1], 4, mstate, vstate))
5611 5728 break;
5612 5729 /*FALLTHROUGH*/
5613 5730 case DIF_OP_LDUW:
5614 5731 regs[rd] = dtrace_load32(regs[r1]);
5615 5732 break;
5616 5733 case DIF_OP_RLDX:
5617 5734 if (!dtrace_canload(regs[r1], 8, mstate, vstate))
5618 5735 break;
5619 5736 /*FALLTHROUGH*/
5620 5737 case DIF_OP_LDX:
5621 5738 regs[rd] = dtrace_load64(regs[r1]);
5622 5739 break;
5623 5740 case DIF_OP_ULDSB:
5624 5741 regs[rd] = (int8_t)
5625 5742 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5626 5743 break;
5627 5744 case DIF_OP_ULDSH:
5628 5745 regs[rd] = (int16_t)
5629 5746 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5630 5747 break;
5631 5748 case DIF_OP_ULDSW:
5632 5749 regs[rd] = (int32_t)
5633 5750 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5634 5751 break;
5635 5752 case DIF_OP_ULDUB:
5636 5753 regs[rd] =
5637 5754 dtrace_fuword8((void *)(uintptr_t)regs[r1]);
5638 5755 break;
5639 5756 case DIF_OP_ULDUH:
5640 5757 regs[rd] =
5641 5758 dtrace_fuword16((void *)(uintptr_t)regs[r1]);
5642 5759 break;
5643 5760 case DIF_OP_ULDUW:
5644 5761 regs[rd] =
5645 5762 dtrace_fuword32((void *)(uintptr_t)regs[r1]);
5646 5763 break;
5647 5764 case DIF_OP_ULDX:
5648 5765 regs[rd] =
5649 5766 dtrace_fuword64((void *)(uintptr_t)regs[r1]);
5650 5767 break;
5651 5768 case DIF_OP_RET:
5652 5769 rval = regs[rd];
5653 5770 pc = textlen;
5654 5771 break;
5655 5772 case DIF_OP_NOP:
5656 5773 break;
5657 5774 case DIF_OP_SETX:
5658 5775 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
5659 5776 break;
5660 5777 case DIF_OP_SETS:
5661 5778 regs[rd] = (uint64_t)(uintptr_t)
5662 5779 (strtab + DIF_INSTR_STRING(instr));
5663 5780 break;
5664 5781 case DIF_OP_SCMP: {
5665 5782 size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
5666 5783 uintptr_t s1 = regs[r1];
5667 5784 uintptr_t s2 = regs[r2];
5668 5785
5669 5786 if (s1 != NULL &&
5670 5787 !dtrace_strcanload(s1, sz, mstate, vstate))
5671 5788 break;
5672 5789 if (s2 != NULL &&
5673 5790 !dtrace_strcanload(s2, sz, mstate, vstate))
5674 5791 break;
5675 5792
5676 5793 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz);
5677 5794
5678 5795 cc_n = cc_r < 0;
5679 5796 cc_z = cc_r == 0;
5680 5797 cc_v = cc_c = 0;
5681 5798 break;
5682 5799 }
5683 5800 case DIF_OP_LDGA:
5684 5801 regs[rd] = dtrace_dif_variable(mstate, state,
5685 5802 r1, regs[r2]);
5686 5803 break;
5687 5804 case DIF_OP_LDGS:
5688 5805 id = DIF_INSTR_VAR(instr);
5689 5806
5690 5807 if (id >= DIF_VAR_OTHER_UBASE) {
5691 5808 uintptr_t a;
5692 5809
5693 5810 id -= DIF_VAR_OTHER_UBASE;
5694 5811 svar = vstate->dtvs_globals[id];
5695 5812 ASSERT(svar != NULL);
5696 5813 v = &svar->dtsv_var;
5697 5814
5698 5815 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
5699 5816 regs[rd] = svar->dtsv_data;
5700 5817 break;
5701 5818 }
5702 5819
5703 5820 a = (uintptr_t)svar->dtsv_data;
5704 5821
5705 5822 if (*(uint8_t *)a == UINT8_MAX) {
5706 5823 /*
5707 5824 * If the 0th byte is set to UINT8_MAX
5708 5825 * then this is to be treated as a
5709 5826 * reference to a NULL variable.
5710 5827 */
5711 5828 regs[rd] = NULL;
5712 5829 } else {
5713 5830 regs[rd] = a + sizeof (uint64_t);
5714 5831 }
5715 5832
5716 5833 break;
5717 5834 }
5718 5835
5719 5836 regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
5720 5837 break;
5721 5838
5722 5839 case DIF_OP_STGS:
5723 5840 id = DIF_INSTR_VAR(instr);
5724 5841
5725 5842 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5726 5843 id -= DIF_VAR_OTHER_UBASE;
5727 5844
5728 5845 svar = vstate->dtvs_globals[id];
5729 5846 ASSERT(svar != NULL);
5730 5847 v = &svar->dtsv_var;
5731 5848
5732 5849 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5733 5850 uintptr_t a = (uintptr_t)svar->dtsv_data;
5734 5851
5735 5852 ASSERT(a != NULL);
5736 5853 ASSERT(svar->dtsv_size != 0);
5737 5854
5738 5855 if (regs[rd] == NULL) {
5739 5856 *(uint8_t *)a = UINT8_MAX;
5740 5857 break;
5741 5858 } else {
5742 5859 *(uint8_t *)a = 0;
5743 5860 a += sizeof (uint64_t);
5744 5861 }
5745 5862 if (!dtrace_vcanload(
5746 5863 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5747 5864 mstate, vstate))
5748 5865 break;
5749 5866
5750 5867 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5751 5868 (void *)a, &v->dtdv_type);
5752 5869 break;
5753 5870 }
5754 5871
5755 5872 svar->dtsv_data = regs[rd];
5756 5873 break;
5757 5874
5758 5875 case DIF_OP_LDTA:
5759 5876 /*
5760 5877 * There are no DTrace built-in thread-local arrays at
5761 5878 * present. This opcode is saved for future work.
5762 5879 */
5763 5880 *flags |= CPU_DTRACE_ILLOP;
5764 5881 regs[rd] = 0;
5765 5882 break;
5766 5883
5767 5884 case DIF_OP_LDLS:
5768 5885 id = DIF_INSTR_VAR(instr);
5769 5886
5770 5887 if (id < DIF_VAR_OTHER_UBASE) {
5771 5888 /*
5772 5889 * For now, this has no meaning.
5773 5890 */
5774 5891 regs[rd] = 0;
5775 5892 break;
5776 5893 }
5777 5894
5778 5895 id -= DIF_VAR_OTHER_UBASE;
5779 5896
5780 5897 ASSERT(id < vstate->dtvs_nlocals);
5781 5898 ASSERT(vstate->dtvs_locals != NULL);
5782 5899
5783 5900 svar = vstate->dtvs_locals[id];
5784 5901 ASSERT(svar != NULL);
5785 5902 v = &svar->dtsv_var;
5786 5903
5787 5904 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5788 5905 uintptr_t a = (uintptr_t)svar->dtsv_data;
5789 5906 size_t sz = v->dtdv_type.dtdt_size;
5790 5907
5791 5908 sz += sizeof (uint64_t);
5792 5909 ASSERT(svar->dtsv_size == NCPU * sz);
5793 5910 a += CPU->cpu_id * sz;
5794 5911
5795 5912 if (*(uint8_t *)a == UINT8_MAX) {
5796 5913 /*
5797 5914 * If the 0th byte is set to UINT8_MAX
5798 5915 * then this is to be treated as a
5799 5916 * reference to a NULL variable.
5800 5917 */
5801 5918 regs[rd] = NULL;
5802 5919 } else {
5803 5920 regs[rd] = a + sizeof (uint64_t);
5804 5921 }
5805 5922
5806 5923 break;
5807 5924 }
5808 5925
5809 5926 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5810 5927 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5811 5928 regs[rd] = tmp[CPU->cpu_id];
5812 5929 break;
5813 5930
5814 5931 case DIF_OP_STLS:
5815 5932 id = DIF_INSTR_VAR(instr);
5816 5933
5817 5934 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5818 5935 id -= DIF_VAR_OTHER_UBASE;
5819 5936 ASSERT(id < vstate->dtvs_nlocals);
5820 5937
5821 5938 ASSERT(vstate->dtvs_locals != NULL);
5822 5939 svar = vstate->dtvs_locals[id];
5823 5940 ASSERT(svar != NULL);
5824 5941 v = &svar->dtsv_var;
5825 5942
5826 5943 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5827 5944 uintptr_t a = (uintptr_t)svar->dtsv_data;
5828 5945 size_t sz = v->dtdv_type.dtdt_size;
5829 5946
5830 5947 sz += sizeof (uint64_t);
5831 5948 ASSERT(svar->dtsv_size == NCPU * sz);
5832 5949 a += CPU->cpu_id * sz;
5833 5950
5834 5951 if (regs[rd] == NULL) {
5835 5952 *(uint8_t *)a = UINT8_MAX;
5836 5953 break;
5837 5954 } else {
5838 5955 *(uint8_t *)a = 0;
5839 5956 a += sizeof (uint64_t);
5840 5957 }
5841 5958
5842 5959 if (!dtrace_vcanload(
5843 5960 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
5844 5961 mstate, vstate))
5845 5962 break;
5846 5963
5847 5964 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5848 5965 (void *)a, &v->dtdv_type);
5849 5966 break;
5850 5967 }
5851 5968
5852 5969 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
5853 5970 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
5854 5971 tmp[CPU->cpu_id] = regs[rd];
5855 5972 break;
5856 5973
5857 5974 case DIF_OP_LDTS: {
5858 5975 dtrace_dynvar_t *dvar;
5859 5976 dtrace_key_t *key;
5860 5977
5861 5978 id = DIF_INSTR_VAR(instr);
5862 5979 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5863 5980 id -= DIF_VAR_OTHER_UBASE;
5864 5981 v = &vstate->dtvs_tlocals[id];
5865 5982
5866 5983 key = &tupregs[DIF_DTR_NREGS];
5867 5984 key[0].dttk_value = (uint64_t)id;
5868 5985 key[0].dttk_size = 0;
5869 5986 DTRACE_TLS_THRKEY(key[1].dttk_value);
5870 5987 key[1].dttk_size = 0;
5871 5988
5872 5989 dvar = dtrace_dynvar(dstate, 2, key,
5873 5990 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
5874 5991 mstate, vstate);
5875 5992
5876 5993 if (dvar == NULL) {
5877 5994 regs[rd] = 0;
5878 5995 break;
5879 5996 }
5880 5997
5881 5998 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5882 5999 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
5883 6000 } else {
5884 6001 regs[rd] = *((uint64_t *)dvar->dtdv_data);
5885 6002 }
5886 6003
5887 6004 break;
5888 6005 }
5889 6006
5890 6007 case DIF_OP_STTS: {
5891 6008 dtrace_dynvar_t *dvar;
5892 6009 dtrace_key_t *key;
5893 6010
5894 6011 id = DIF_INSTR_VAR(instr);
5895 6012 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5896 6013 id -= DIF_VAR_OTHER_UBASE;
5897 6014
5898 6015 key = &tupregs[DIF_DTR_NREGS];
5899 6016 key[0].dttk_value = (uint64_t)id;
5900 6017 key[0].dttk_size = 0;
5901 6018 DTRACE_TLS_THRKEY(key[1].dttk_value);
5902 6019 key[1].dttk_size = 0;
5903 6020 v = &vstate->dtvs_tlocals[id];
5904 6021
5905 6022 dvar = dtrace_dynvar(dstate, 2, key,
5906 6023 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
5907 6024 v->dtdv_type.dtdt_size : sizeof (uint64_t),
5908 6025 regs[rd] ? DTRACE_DYNVAR_ALLOC :
5909 6026 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
5910 6027
5911 6028 /*
5912 6029 * Given that we're storing to thread-local data,
5913 6030 * we need to flush our predicate cache.
5914 6031 */
5915 6032 curthread->t_predcache = NULL;
5916 6033
5917 6034 if (dvar == NULL)
5918 6035 break;
5919 6036
5920 6037 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
5921 6038 if (!dtrace_vcanload(
5922 6039 (void *)(uintptr_t)regs[rd],
5923 6040 &v->dtdv_type, mstate, vstate))
5924 6041 break;
5925 6042
5926 6043 dtrace_vcopy((void *)(uintptr_t)regs[rd],
5927 6044 dvar->dtdv_data, &v->dtdv_type);
5928 6045 } else {
5929 6046 *((uint64_t *)dvar->dtdv_data) = regs[rd];
5930 6047 }
5931 6048
5932 6049 break;
5933 6050 }
5934 6051
5935 6052 case DIF_OP_SRA:
5936 6053 regs[rd] = (int64_t)regs[r1] >> regs[r2];
5937 6054 break;
5938 6055
5939 6056 case DIF_OP_CALL:
5940 6057 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
5941 6058 regs, tupregs, ttop, mstate, state);
5942 6059 break;
5943 6060
5944 6061 case DIF_OP_PUSHTR:
5945 6062 if (ttop == DIF_DTR_NREGS) {
5946 6063 *flags |= CPU_DTRACE_TUPOFLOW;
5947 6064 break;
5948 6065 }
5949 6066
5950 6067 if (r1 == DIF_TYPE_STRING) {
5951 6068 /*
5952 6069 * If this is a string type and the size is 0,
5953 6070 * we'll use the system-wide default string
5954 6071 * size. Note that we are _not_ looking at
5955 6072 * the value of the DTRACEOPT_STRSIZE option;
5956 6073 * had this been set, we would expect to have
5957 6074 * a non-zero size value in the "pushtr".
5958 6075 */
5959 6076 tupregs[ttop].dttk_size =
5960 6077 dtrace_strlen((char *)(uintptr_t)regs[rd],
5961 6078 regs[r2] ? regs[r2] :
5962 6079 dtrace_strsize_default) + 1;
5963 6080 } else {
5964 6081 tupregs[ttop].dttk_size = regs[r2];
5965 6082 }
5966 6083
5967 6084 tupregs[ttop++].dttk_value = regs[rd];
5968 6085 break;
5969 6086
5970 6087 case DIF_OP_PUSHTV:
5971 6088 if (ttop == DIF_DTR_NREGS) {
5972 6089 *flags |= CPU_DTRACE_TUPOFLOW;
5973 6090 break;
5974 6091 }
5975 6092
5976 6093 tupregs[ttop].dttk_value = regs[rd];
5977 6094 tupregs[ttop++].dttk_size = 0;
5978 6095 break;
5979 6096
5980 6097 case DIF_OP_POPTS:
5981 6098 if (ttop != 0)
5982 6099 ttop--;
5983 6100 break;
5984 6101
5985 6102 case DIF_OP_FLUSHTS:
5986 6103 ttop = 0;
5987 6104 break;
5988 6105
5989 6106 case DIF_OP_LDGAA:
5990 6107 case DIF_OP_LDTAA: {
5991 6108 dtrace_dynvar_t *dvar;
5992 6109 dtrace_key_t *key = tupregs;
5993 6110 uint_t nkeys = ttop;
5994 6111
5995 6112 id = DIF_INSTR_VAR(instr);
5996 6113 ASSERT(id >= DIF_VAR_OTHER_UBASE);
5997 6114 id -= DIF_VAR_OTHER_UBASE;
5998 6115
5999 6116 key[nkeys].dttk_value = (uint64_t)id;
6000 6117 key[nkeys++].dttk_size = 0;
6001 6118
6002 6119 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
6003 6120 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6004 6121 key[nkeys++].dttk_size = 0;
6005 6122 v = &vstate->dtvs_tlocals[id];
6006 6123 } else {
6007 6124 v = &vstate->dtvs_globals[id]->dtsv_var;
6008 6125 }
6009 6126
6010 6127 dvar = dtrace_dynvar(dstate, nkeys, key,
6011 6128 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6012 6129 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6013 6130 DTRACE_DYNVAR_NOALLOC, mstate, vstate);
6014 6131
6015 6132 if (dvar == NULL) {
6016 6133 regs[rd] = 0;
6017 6134 break;
6018 6135 }
6019 6136
6020 6137 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6021 6138 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6022 6139 } else {
6023 6140 regs[rd] = *((uint64_t *)dvar->dtdv_data);
6024 6141 }
6025 6142
6026 6143 break;
6027 6144 }
6028 6145
6029 6146 case DIF_OP_STGAA:
6030 6147 case DIF_OP_STTAA: {
6031 6148 dtrace_dynvar_t *dvar;
6032 6149 dtrace_key_t *key = tupregs;
6033 6150 uint_t nkeys = ttop;
6034 6151
6035 6152 id = DIF_INSTR_VAR(instr);
6036 6153 ASSERT(id >= DIF_VAR_OTHER_UBASE);
6037 6154 id -= DIF_VAR_OTHER_UBASE;
6038 6155
6039 6156 key[nkeys].dttk_value = (uint64_t)id;
6040 6157 key[nkeys++].dttk_size = 0;
6041 6158
6042 6159 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
6043 6160 DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6044 6161 key[nkeys++].dttk_size = 0;
6045 6162 v = &vstate->dtvs_tlocals[id];
6046 6163 } else {
6047 6164 v = &vstate->dtvs_globals[id]->dtsv_var;
6048 6165 }
6049 6166
6050 6167 dvar = dtrace_dynvar(dstate, nkeys, key,
6051 6168 v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6052 6169 v->dtdv_type.dtdt_size : sizeof (uint64_t),
6053 6170 regs[rd] ? DTRACE_DYNVAR_ALLOC :
6054 6171 DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6055 6172
6056 6173 if (dvar == NULL)
6057 6174 break;
6058 6175
6059 6176 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6060 6177 if (!dtrace_vcanload(
6061 6178 (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6062 6179 mstate, vstate))
6063 6180 break;
6064 6181
6065 6182 dtrace_vcopy((void *)(uintptr_t)regs[rd],
6066 6183 dvar->dtdv_data, &v->dtdv_type);
6067 6184 } else {
6068 6185 *((uint64_t *)dvar->dtdv_data) = regs[rd];
6069 6186 }
6070 6187
6071 6188 break;
6072 6189 }
6073 6190
6074 6191 case DIF_OP_ALLOCS: {
6075 6192 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6076 6193 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
6077 6194
6078 6195 /*
6079 6196 * Rounding up the user allocation size could have
6080 6197 * overflowed large, bogus allocations (like -1ULL) to
6081 6198 * 0.
6082 6199 */
6083 6200 if (size < regs[r1] ||
6084 6201 !DTRACE_INSCRATCH(mstate, size)) {
6085 6202 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6086 6203 regs[rd] = NULL;
6087 6204 break;
6088 6205 }
6089 6206
6090 6207 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
6091 6208 mstate->dtms_scratch_ptr += size;
6092 6209 regs[rd] = ptr;
6093 6210 break;
6094 6211 }
6095 6212
6096 6213 case DIF_OP_COPYS:
6097 6214 if (!dtrace_canstore(regs[rd], regs[r2],
6098 6215 mstate, vstate)) {
6099 6216 *flags |= CPU_DTRACE_BADADDR;
6100 6217 *illval = regs[rd];
6101 6218 break;
6102 6219 }
6103 6220
6104 6221 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
6105 6222 break;
6106 6223
6107 6224 dtrace_bcopy((void *)(uintptr_t)regs[r1],
6108 6225 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
6109 6226 break;
6110 6227
6111 6228 case DIF_OP_STB:
6112 6229 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
6113 6230 *flags |= CPU_DTRACE_BADADDR;
6114 6231 *illval = regs[rd];
6115 6232 break;
6116 6233 }
6117 6234 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
6118 6235 break;
6119 6236
6120 6237 case DIF_OP_STH:
6121 6238 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
6122 6239 *flags |= CPU_DTRACE_BADADDR;
6123 6240 *illval = regs[rd];
6124 6241 break;
6125 6242 }
6126 6243 if (regs[rd] & 1) {
6127 6244 *flags |= CPU_DTRACE_BADALIGN;
6128 6245 *illval = regs[rd];
6129 6246 break;
6130 6247 }
6131 6248 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
6132 6249 break;
6133 6250
6134 6251 case DIF_OP_STW:
6135 6252 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
6136 6253 *flags |= CPU_DTRACE_BADADDR;
6137 6254 *illval = regs[rd];
6138 6255 break;
6139 6256 }
6140 6257 if (regs[rd] & 3) {
6141 6258 *flags |= CPU_DTRACE_BADALIGN;
6142 6259 *illval = regs[rd];
6143 6260 break;
6144 6261 }
6145 6262 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
6146 6263 break;
6147 6264
6148 6265 case DIF_OP_STX:
6149 6266 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
6150 6267 *flags |= CPU_DTRACE_BADADDR;
6151 6268 *illval = regs[rd];
6152 6269 break;
6153 6270 }
6154 6271 if (regs[rd] & 7) {
6155 6272 *flags |= CPU_DTRACE_BADALIGN;
6156 6273 *illval = regs[rd];
6157 6274 break;
6158 6275 }
6159 6276 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
6160 6277 break;
6161 6278 }
6162 6279 }
6163 6280
6164 6281 if (!(*flags & CPU_DTRACE_FAULT))
6165 6282 return (rval);
6166 6283
6167 6284 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
6168 6285 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
6169 6286
6170 6287 return (0);
6171 6288 }
6172 6289
6173 6290 static void
6174 6291 dtrace_action_breakpoint(dtrace_ecb_t *ecb)
6175 6292 {
6176 6293 dtrace_probe_t *probe = ecb->dte_probe;
6177 6294 dtrace_provider_t *prov = probe->dtpr_provider;
6178 6295 char c[DTRACE_FULLNAMELEN + 80], *str;
6179 6296 char *msg = "dtrace: breakpoint action at probe ";
6180 6297 char *ecbmsg = " (ecb ";
6181 6298 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
6182 6299 uintptr_t val = (uintptr_t)ecb;
6183 6300 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
6184 6301
6185 6302 if (dtrace_destructive_disallow)
6186 6303 return;
6187 6304
6188 6305 /*
6189 6306 * It's impossible to be taking action on the NULL probe.
6190 6307 */
6191 6308 ASSERT(probe != NULL);
6192 6309
6193 6310 /*
6194 6311 * This is a poor man's (destitute man's?) sprintf(): we want to
6195 6312 * print the provider name, module name, function name and name of
6196 6313 * the probe, along with the hex address of the ECB with the breakpoint
6197 6314 * action -- all of which we must place in the character buffer by
6198 6315 * hand.
6199 6316 */
6200 6317 while (*msg != '\0')
6201 6318 c[i++] = *msg++;
6202 6319
6203 6320 for (str = prov->dtpv_name; *str != '\0'; str++)
6204 6321 c[i++] = *str;
6205 6322 c[i++] = ':';
6206 6323
6207 6324 for (str = probe->dtpr_mod; *str != '\0'; str++)
6208 6325 c[i++] = *str;
6209 6326 c[i++] = ':';
6210 6327
6211 6328 for (str = probe->dtpr_func; *str != '\0'; str++)
6212 6329 c[i++] = *str;
6213 6330 c[i++] = ':';
6214 6331
6215 6332 for (str = probe->dtpr_name; *str != '\0'; str++)
6216 6333 c[i++] = *str;
6217 6334
6218 6335 while (*ecbmsg != '\0')
6219 6336 c[i++] = *ecbmsg++;
6220 6337
6221 6338 while (shift >= 0) {
6222 6339 mask = (uintptr_t)0xf << shift;
6223 6340
6224 6341 if (val >= ((uintptr_t)1 << shift))
6225 6342 c[i++] = "0123456789abcdef"[(val & mask) >> shift];
6226 6343 shift -= 4;
6227 6344 }
6228 6345
6229 6346 c[i++] = ')';
6230 6347 c[i] = '\0';
6231 6348
6232 6349 debug_enter(c);
6233 6350 }
6234 6351
6235 6352 static void
6236 6353 dtrace_action_panic(dtrace_ecb_t *ecb)
6237 6354 {
6238 6355 dtrace_probe_t *probe = ecb->dte_probe;
6239 6356
6240 6357 /*
6241 6358 * It's impossible to be taking action on the NULL probe.
6242 6359 */
6243 6360 ASSERT(probe != NULL);
6244 6361
6245 6362 if (dtrace_destructive_disallow)
6246 6363 return;
6247 6364
6248 6365 if (dtrace_panicked != NULL)
6249 6366 return;
6250 6367
6251 6368 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
6252 6369 return;
6253 6370
6254 6371 /*
6255 6372 * We won the right to panic. (We want to be sure that only one
6256 6373 * thread calls panic() from dtrace_probe(), and that panic() is
6257 6374 * called exactly once.)
6258 6375 */
6259 6376 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6260 6377 probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
6261 6378 probe->dtpr_func, probe->dtpr_name, (void *)ecb);
6262 6379 }
6263 6380
6264 6381 static void
6265 6382 dtrace_action_raise(uint64_t sig)
6266 6383 {
6267 6384 if (dtrace_destructive_disallow)
6268 6385 return;
6269 6386
6270 6387 if (sig >= NSIG) {
6271 6388 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6272 6389 return;
6273 6390 }
6274 6391
6275 6392 /*
6276 6393 * raise() has a queue depth of 1 -- we ignore all subsequent
6277 6394 * invocations of the raise() action.
6278 6395 */
6279 6396 if (curthread->t_dtrace_sig == 0)
6280 6397 curthread->t_dtrace_sig = (uint8_t)sig;
6281 6398
6282 6399 curthread->t_sig_check = 1;
6283 6400 aston(curthread);
6284 6401 }
6285 6402
6286 6403 static void
6287 6404 dtrace_action_stop(void)
6288 6405 {
6289 6406 if (dtrace_destructive_disallow)
6290 6407 return;
6291 6408
6292 6409 if (!curthread->t_dtrace_stop) {
6293 6410 curthread->t_dtrace_stop = 1;
6294 6411 curthread->t_sig_check = 1;
6295 6412 aston(curthread);
6296 6413 }
6297 6414 }
6298 6415
6299 6416 static void
6300 6417 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
6301 6418 {
6302 6419 hrtime_t now;
6303 6420 volatile uint16_t *flags;
6304 6421 cpu_t *cpu = CPU;
6305 6422
6306 6423 if (dtrace_destructive_disallow)
6307 6424 return;
6308 6425
6309 6426 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags;
6310 6427
6311 6428 now = dtrace_gethrtime();
6312 6429
6313 6430 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
6314 6431 /*
6315 6432 * We need to advance the mark to the current time.
6316 6433 */
6317 6434 cpu->cpu_dtrace_chillmark = now;
6318 6435 cpu->cpu_dtrace_chilled = 0;
6319 6436 }
6320 6437
6321 6438 /*
6322 6439 * Now check to see if the requested chill time would take us over
6323 6440 * the maximum amount of time allowed in the chill interval. (Or
6324 6441 * worse, if the calculation itself induces overflow.)
6325 6442 */
6326 6443 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
6327 6444 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
6328 6445 *flags |= CPU_DTRACE_ILLOP;
6329 6446 return;
6330 6447 }
6331 6448
6332 6449 while (dtrace_gethrtime() - now < val)
6333 6450 continue;
6334 6451
6335 6452 /*
6336 6453 * Normally, we assure that the value of the variable "timestamp" does
6337 6454 * not change within an ECB. The presence of chill() represents an
6338 6455 * exception to this rule, however.
6339 6456 */
6340 6457 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
6341 6458 cpu->cpu_dtrace_chilled += val;
6342 6459 }
6343 6460
6344 6461 static void
6345 6462 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
6346 6463 uint64_t *buf, uint64_t arg)
6347 6464 {
6348 6465 int nframes = DTRACE_USTACK_NFRAMES(arg);
6349 6466 int strsize = DTRACE_USTACK_STRSIZE(arg);
6350 6467 uint64_t *pcs = &buf[1], *fps;
6351 6468 char *str = (char *)&pcs[nframes];
6352 6469 int size, offs = 0, i, j;
6353 6470 uintptr_t old = mstate->dtms_scratch_ptr, saved;
6354 6471 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
6355 6472 char *sym;
6356 6473
6357 6474 /*
6358 6475 * Should be taking a faster path if string space has not been
6359 6476 * allocated.
6360 6477 */
6361 6478 ASSERT(strsize != 0);
6362 6479
6363 6480 /*
6364 6481 * We will first allocate some temporary space for the frame pointers.
6365 6482 */
6366 6483 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6367 6484 size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
6368 6485 (nframes * sizeof (uint64_t));
6369 6486
6370 6487 if (!DTRACE_INSCRATCH(mstate, size)) {
6371 6488 /*
6372 6489 * Not enough room for our frame pointers -- need to indicate
6373 6490 * that we ran out of scratch space.
6374 6491 */
6375 6492 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6376 6493 return;
6377 6494 }
6378 6495
6379 6496 mstate->dtms_scratch_ptr += size;
6380 6497 saved = mstate->dtms_scratch_ptr;
6381 6498
6382 6499 /*
6383 6500 * Now get a stack with both program counters and frame pointers.
6384 6501 */
6385 6502 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6386 6503 dtrace_getufpstack(buf, fps, nframes + 1);
6387 6504 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6388 6505
6389 6506 /*
6390 6507 * If that faulted, we're cooked.
6391 6508 */
6392 6509 if (*flags & CPU_DTRACE_FAULT)
6393 6510 goto out;
6394 6511
6395 6512 /*
6396 6513 * Now we want to walk up the stack, calling the USTACK helper. For
6397 6514 * each iteration, we restore the scratch pointer.
6398 6515 */
6399 6516 for (i = 0; i < nframes; i++) {
6400 6517 mstate->dtms_scratch_ptr = saved;
6401 6518
6402 6519 if (offs >= strsize)
6403 6520 break;
6404 6521
6405 6522 sym = (char *)(uintptr_t)dtrace_helper(
6406 6523 DTRACE_HELPER_ACTION_USTACK,
6407 6524 mstate, state, pcs[i], fps[i]);
6408 6525
6409 6526 /*
6410 6527 * If we faulted while running the helper, we're going to
6411 6528 * clear the fault and null out the corresponding string.
6412 6529 */
6413 6530 if (*flags & CPU_DTRACE_FAULT) {
6414 6531 *flags &= ~CPU_DTRACE_FAULT;
6415 6532 str[offs++] = '\0';
6416 6533 continue;
6417 6534 }
6418 6535
6419 6536 if (sym == NULL) {
6420 6537 str[offs++] = '\0';
6421 6538 continue;
6422 6539 }
6423 6540
6424 6541 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6425 6542
6426 6543 /*
6427 6544 * Now copy in the string that the helper returned to us.
6428 6545 */
6429 6546 for (j = 0; offs + j < strsize; j++) {
6430 6547 if ((str[offs + j] = sym[j]) == '\0')
6431 6548 break;
6432 6549 }
6433 6550
6434 6551 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6435 6552
6436 6553 offs += j + 1;
6437 6554 }
6438 6555
6439 6556 if (offs >= strsize) {
6440 6557 /*
6441 6558 * If we didn't have room for all of the strings, we don't
6442 6559 * abort processing -- this needn't be a fatal error -- but we
6443 6560 * still want to increment a counter (dts_stkstroverflows) to
6444 6561 * allow this condition to be warned about. (If this is from
6445 6562 * a jstack() action, it is easily tuned via jstackstrsize.)
6446 6563 */
6447 6564 dtrace_error(&state->dts_stkstroverflows);
6448 6565 }
6449 6566
6450 6567 while (offs < strsize)
6451 6568 str[offs++] = '\0';
6452 6569
6453 6570 out:
6454 6571 mstate->dtms_scratch_ptr = old;
6455 6572 }
6456 6573
6457 6574 /*
6458 6575 * If you're looking for the epicenter of DTrace, you just found it. This
6459 6576 * is the function called by the provider to fire a probe -- from which all
6460 6577 * subsequent probe-context DTrace activity emanates.
6461 6578 */
6462 6579 void
6463 6580 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
6464 6581 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
6465 6582 {
6466 6583 processorid_t cpuid;
6467 6584 dtrace_icookie_t cookie;
6468 6585 dtrace_probe_t *probe;
6469 6586 dtrace_mstate_t mstate;
6470 6587 dtrace_ecb_t *ecb;
6471 6588 dtrace_action_t *act;
6472 6589 intptr_t offs;
6473 6590 size_t size;
6474 6591 int vtime, onintr;
6475 6592 volatile uint16_t *flags;
6476 6593 hrtime_t now;
6477 6594
6478 6595 /*
6479 6596 * Kick out immediately if this CPU is still being born (in which case
6480 6597 * curthread will be set to -1) or the current thread can't allow
6481 6598 * probes in its current context.
6482 6599 */
6483 6600 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
6484 6601 return;
6485 6602
6486 6603 cookie = dtrace_interrupt_disable();
6487 6604 probe = dtrace_probes[id - 1];
6488 6605 cpuid = CPU->cpu_id;
6489 6606 onintr = CPU_ON_INTR(CPU);
6490 6607
6491 6608 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
6492 6609 probe->dtpr_predcache == curthread->t_predcache) {
6493 6610 /*
6494 6611 * We have hit in the predicate cache; we know that
6495 6612 * this predicate would evaluate to be false.
6496 6613 */
6497 6614 dtrace_interrupt_enable(cookie);
6498 6615 return;
6499 6616 }
6500 6617
6501 6618 if (panic_quiesce) {
6502 6619 /*
6503 6620 * We don't trace anything if we're panicking.
6504 6621 */
6505 6622 dtrace_interrupt_enable(cookie);
6506 6623 return;
6507 6624 }
6508 6625
6509 6626 now = dtrace_gethrtime();
6510 6627 vtime = dtrace_vtime_references != 0;
6511 6628
6512 6629 if (vtime && curthread->t_dtrace_start)
6513 6630 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
6514 6631
6515 6632 mstate.dtms_difo = NULL;
6516 6633 mstate.dtms_probe = probe;
6517 6634 mstate.dtms_strtok = NULL;
6518 6635 mstate.dtms_arg[0] = arg0;
6519 6636 mstate.dtms_arg[1] = arg1;
6520 6637 mstate.dtms_arg[2] = arg2;
6521 6638 mstate.dtms_arg[3] = arg3;
6522 6639 mstate.dtms_arg[4] = arg4;
6523 6640
6524 6641 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
6525 6642
6526 6643 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
6527 6644 dtrace_predicate_t *pred = ecb->dte_predicate;
6528 6645 dtrace_state_t *state = ecb->dte_state;
6529 6646 dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
6530 6647 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
6531 6648 dtrace_vstate_t *vstate = &state->dts_vstate;
6532 6649 dtrace_provider_t *prov = probe->dtpr_provider;
6533 6650 uint64_t tracememsize = 0;
6534 6651 int committed = 0;
6535 6652 caddr_t tomax;
6536 6653
6537 6654 /*
6538 6655 * A little subtlety with the following (seemingly innocuous)
6539 6656 * declaration of the automatic 'val': by looking at the
6540 6657 * code, you might think that it could be declared in the
6541 6658 * action processing loop, below. (That is, it's only used in
6542 6659 * the action processing loop.) However, it must be declared
6543 6660 * out of that scope because in the case of DIF expression
6544 6661 * arguments to aggregating actions, one iteration of the
6545 6662 * action loop will use the last iteration's value.
6546 6663 */
6547 6664 #ifdef lint
6548 6665 uint64_t val = 0;
6549 6666 #else
6550 6667 uint64_t val;
6551 6668 #endif
6552 6669
6553 6670 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
6554 6671 mstate.dtms_access = DTRACE_ACCESS_ARGS | DTRACE_ACCESS_PROC;
6555 6672 mstate.dtms_getf = NULL;
6556 6673
6557 6674 *flags &= ~CPU_DTRACE_ERROR;
6558 6675
6559 6676 if (prov == dtrace_provider) {
6560 6677 /*
6561 6678 * If dtrace itself is the provider of this probe,
6562 6679 * we're only going to continue processing the ECB if
6563 6680 * arg0 (the dtrace_state_t) is equal to the ECB's
6564 6681 * creating state. (This prevents disjoint consumers
6565 6682 * from seeing one another's metaprobes.)
6566 6683 */
6567 6684 if (arg0 != (uint64_t)(uintptr_t)state)
6568 6685 continue;
6569 6686 }
6570 6687
6571 6688 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
6572 6689 /*
6573 6690 * We're not currently active. If our provider isn't
6574 6691 * the dtrace pseudo provider, we're not interested.
6575 6692 */
6576 6693 if (prov != dtrace_provider)
6577 6694 continue;
6578 6695
6579 6696 /*
6580 6697 * Now we must further check if we are in the BEGIN
6581 6698 * probe. If we are, we will only continue processing
6582 6699 * if we're still in WARMUP -- if one BEGIN enabling
6583 6700 * has invoked the exit() action, we don't want to
6584 6701 * evaluate subsequent BEGIN enablings.
6585 6702 */
6586 6703 if (probe->dtpr_id == dtrace_probeid_begin &&
6587 6704 state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
6588 6705 ASSERT(state->dts_activity ==
6589 6706 DTRACE_ACTIVITY_DRAINING);
6590 6707 continue;
6591 6708 }
6592 6709 }
6593 6710
6594 6711 if (ecb->dte_cond && !dtrace_priv_probe(state, &mstate, ecb))
6595 6712 continue;
6596 6713
6597 6714 if (now - state->dts_alive > dtrace_deadman_timeout) {
6598 6715 /*
6599 6716 * We seem to be dead. Unless we (a) have kernel
6600 6717 * destructive permissions (b) have explicitly enabled
6601 6718 * destructive actions and (c) destructive actions have
6602 6719 * not been disabled, we're going to transition into
6603 6720 * the KILLED state, from which no further processing
6604 6721 * on this state will be performed.
6605 6722 */
6606 6723 if (!dtrace_priv_kernel_destructive(state) ||
6607 6724 !state->dts_cred.dcr_destructive ||
6608 6725 dtrace_destructive_disallow) {
6609 6726 void *activity = &state->dts_activity;
6610 6727 dtrace_activity_t current;
6611 6728
6612 6729 do {
6613 6730 current = state->dts_activity;
6614 6731 } while (dtrace_cas32(activity, current,
6615 6732 DTRACE_ACTIVITY_KILLED) != current);
6616 6733
6617 6734 continue;
6618 6735 }
6619 6736 }
6620 6737
6621 6738 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
6622 6739 ecb->dte_alignment, state, &mstate)) < 0)
6623 6740 continue;
6624 6741
6625 6742 tomax = buf->dtb_tomax;
6626 6743 ASSERT(tomax != NULL);
6627 6744
6628 6745 if (ecb->dte_size != 0) {
6629 6746 dtrace_rechdr_t dtrh;
6630 6747 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
6631 6748 mstate.dtms_timestamp = dtrace_gethrtime();
6632 6749 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
6633 6750 }
6634 6751 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t));
6635 6752 dtrh.dtrh_epid = ecb->dte_epid;
6636 6753 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh,
6637 6754 mstate.dtms_timestamp);
6638 6755 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh;
6639 6756 }
6640 6757
6641 6758 mstate.dtms_epid = ecb->dte_epid;
6642 6759 mstate.dtms_present |= DTRACE_MSTATE_EPID;
6643 6760
6644 6761 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
6645 6762 mstate.dtms_access |= DTRACE_ACCESS_KERNEL;
6646 6763
6647 6764 if (pred != NULL) {
6648 6765 dtrace_difo_t *dp = pred->dtp_difo;
6649 6766 int rval;
6650 6767
6651 6768 rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
6652 6769
6653 6770 if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
6654 6771 dtrace_cacheid_t cid = probe->dtpr_predcache;
6655 6772
6656 6773 if (cid != DTRACE_CACHEIDNONE && !onintr) {
6657 6774 /*
6658 6775 * Update the predicate cache...
6659 6776 */
6660 6777 ASSERT(cid == pred->dtp_cacheid);
6661 6778 curthread->t_predcache = cid;
6662 6779 }
6663 6780
6664 6781 continue;
6665 6782 }
6666 6783 }
6667 6784
6668 6785 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
6669 6786 act != NULL; act = act->dta_next) {
6670 6787 size_t valoffs;
6671 6788 dtrace_difo_t *dp;
6672 6789 dtrace_recdesc_t *rec = &act->dta_rec;
6673 6790
6674 6791 size = rec->dtrd_size;
6675 6792 valoffs = offs + rec->dtrd_offset;
6676 6793
6677 6794 if (DTRACEACT_ISAGG(act->dta_kind)) {
6678 6795 uint64_t v = 0xbad;
6679 6796 dtrace_aggregation_t *agg;
6680 6797
6681 6798 agg = (dtrace_aggregation_t *)act;
6682 6799
6683 6800 if ((dp = act->dta_difo) != NULL)
6684 6801 v = dtrace_dif_emulate(dp,
6685 6802 &mstate, vstate, state);
6686 6803
6687 6804 if (*flags & CPU_DTRACE_ERROR)
6688 6805 continue;
6689 6806
6690 6807 /*
6691 6808 * Note that we always pass the expression
6692 6809 * value from the previous iteration of the
6693 6810 * action loop. This value will only be used
6694 6811 * if there is an expression argument to the
6695 6812 * aggregating action, denoted by the
6696 6813 * dtag_hasarg field.
6697 6814 */
6698 6815 dtrace_aggregate(agg, buf,
6699 6816 offs, aggbuf, v, val);
6700 6817 continue;
6701 6818 }
6702 6819
6703 6820 switch (act->dta_kind) {
6704 6821 case DTRACEACT_STOP:
6705 6822 if (dtrace_priv_proc_destructive(state,
6706 6823 &mstate))
6707 6824 dtrace_action_stop();
6708 6825 continue;
6709 6826
6710 6827 case DTRACEACT_BREAKPOINT:
6711 6828 if (dtrace_priv_kernel_destructive(state))
6712 6829 dtrace_action_breakpoint(ecb);
6713 6830 continue;
6714 6831
6715 6832 case DTRACEACT_PANIC:
6716 6833 if (dtrace_priv_kernel_destructive(state))
6717 6834 dtrace_action_panic(ecb);
6718 6835 continue;
6719 6836
6720 6837 case DTRACEACT_STACK:
6721 6838 if (!dtrace_priv_kernel(state))
6722 6839 continue;
6723 6840
6724 6841 dtrace_getpcstack((pc_t *)(tomax + valoffs),
6725 6842 size / sizeof (pc_t), probe->dtpr_aframes,
6726 6843 DTRACE_ANCHORED(probe) ? NULL :
6727 6844 (uint32_t *)arg0);
6728 6845
6729 6846 continue;
6730 6847
6731 6848 case DTRACEACT_JSTACK:
6732 6849 case DTRACEACT_USTACK:
6733 6850 if (!dtrace_priv_proc(state, &mstate))
6734 6851 continue;
6735 6852
6736 6853 /*
6737 6854 * See comment in DIF_VAR_PID.
6738 6855 */
6739 6856 if (DTRACE_ANCHORED(mstate.dtms_probe) &&
6740 6857 CPU_ON_INTR(CPU)) {
6741 6858 int depth = DTRACE_USTACK_NFRAMES(
6742 6859 rec->dtrd_arg) + 1;
6743 6860
6744 6861 dtrace_bzero((void *)(tomax + valoffs),
6745 6862 DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
6746 6863 + depth * sizeof (uint64_t));
6747 6864
6748 6865 continue;
6749 6866 }
6750 6867
6751 6868 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
6752 6869 curproc->p_dtrace_helpers != NULL) {
6753 6870 /*
6754 6871 * This is the slow path -- we have
6755 6872 * allocated string space, and we're
6756 6873 * getting the stack of a process that
6757 6874 * has helpers. Call into a separate
6758 6875 * routine to perform this processing.
6759 6876 */
6760 6877 dtrace_action_ustack(&mstate, state,
6761 6878 (uint64_t *)(tomax + valoffs),
6762 6879 rec->dtrd_arg);
6763 6880 continue;
6764 6881 }
6765 6882
6766 6883 /*
6767 6884 * Clear the string space, since there's no
6768 6885 * helper to do it for us.
6769 6886 */
6770 6887 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0) {
6771 6888 int depth = DTRACE_USTACK_NFRAMES(
6772 6889 rec->dtrd_arg);
6773 6890 size_t strsize = DTRACE_USTACK_STRSIZE(
6774 6891 rec->dtrd_arg);
6775 6892 uint64_t *buf = (uint64_t *)(tomax +
6776 6893 valoffs);
6777 6894 void *strspace = &buf[depth + 1];
6778 6895
6779 6896 dtrace_bzero(strspace,
6780 6897 MIN(depth, strsize));
6781 6898 }
6782 6899
6783 6900 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6784 6901 dtrace_getupcstack((uint64_t *)
6785 6902 (tomax + valoffs),
6786 6903 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
6787 6904 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6788 6905 continue;
6789 6906
6790 6907 default:
6791 6908 break;
6792 6909 }
6793 6910
6794 6911 dp = act->dta_difo;
6795 6912 ASSERT(dp != NULL);
6796 6913
6797 6914 val = dtrace_dif_emulate(dp, &mstate, vstate, state);
6798 6915
6799 6916 if (*flags & CPU_DTRACE_ERROR)
6800 6917 continue;
6801 6918
6802 6919 switch (act->dta_kind) {
6803 6920 case DTRACEACT_SPECULATE: {
6804 6921 dtrace_rechdr_t *dtrh;
6805 6922
6806 6923 ASSERT(buf == &state->dts_buffer[cpuid]);
6807 6924 buf = dtrace_speculation_buffer(state,
6808 6925 cpuid, val);
6809 6926
6810 6927 if (buf == NULL) {
6811 6928 *flags |= CPU_DTRACE_DROP;
6812 6929 continue;
6813 6930 }
6814 6931
6815 6932 offs = dtrace_buffer_reserve(buf,
6816 6933 ecb->dte_needed, ecb->dte_alignment,
6817 6934 state, NULL);
6818 6935
6819 6936 if (offs < 0) {
6820 6937 *flags |= CPU_DTRACE_DROP;
6821 6938 continue;
6822 6939 }
6823 6940
6824 6941 tomax = buf->dtb_tomax;
6825 6942 ASSERT(tomax != NULL);
6826 6943
6827 6944 if (ecb->dte_size == 0)
6828 6945 continue;
6829 6946
6830 6947 ASSERT3U(ecb->dte_size, >=,
6831 6948 sizeof (dtrace_rechdr_t));
6832 6949 dtrh = ((void *)(tomax + offs));
6833 6950 dtrh->dtrh_epid = ecb->dte_epid;
6834 6951 /*
6835 6952 * When the speculation is committed, all of
6836 6953 * the records in the speculative buffer will
6837 6954 * have their timestamps set to the commit
6838 6955 * time. Until then, it is set to a sentinel
6839 6956 * value, for debugability.
6840 6957 */
6841 6958 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
6842 6959 continue;
6843 6960 }
6844 6961
6845 6962 case DTRACEACT_CHILL:
6846 6963 if (dtrace_priv_kernel_destructive(state))
6847 6964 dtrace_action_chill(&mstate, val);
6848 6965 continue;
6849 6966
6850 6967 case DTRACEACT_RAISE:
6851 6968 if (dtrace_priv_proc_destructive(state,
6852 6969 &mstate))
6853 6970 dtrace_action_raise(val);
6854 6971 continue;
6855 6972
6856 6973 case DTRACEACT_COMMIT:
6857 6974 ASSERT(!committed);
6858 6975
6859 6976 /*
6860 6977 * We need to commit our buffer state.
6861 6978 */
6862 6979 if (ecb->dte_size)
6863 6980 buf->dtb_offset = offs + ecb->dte_size;
6864 6981 buf = &state->dts_buffer[cpuid];
6865 6982 dtrace_speculation_commit(state, cpuid, val);
6866 6983 committed = 1;
6867 6984 continue;
6868 6985
6869 6986 case DTRACEACT_DISCARD:
6870 6987 dtrace_speculation_discard(state, cpuid, val);
6871 6988 continue;
6872 6989
6873 6990 case DTRACEACT_DIFEXPR:
6874 6991 case DTRACEACT_LIBACT:
6875 6992 case DTRACEACT_PRINTF:
6876 6993 case DTRACEACT_PRINTA:
6877 6994 case DTRACEACT_SYSTEM:
6878 6995 case DTRACEACT_FREOPEN:
6879 6996 case DTRACEACT_TRACEMEM:
6880 6997 break;
6881 6998
6882 6999 case DTRACEACT_TRACEMEM_DYNSIZE:
6883 7000 tracememsize = val;
6884 7001 break;
6885 7002
6886 7003 case DTRACEACT_SYM:
6887 7004 case DTRACEACT_MOD:
6888 7005 if (!dtrace_priv_kernel(state))
6889 7006 continue;
6890 7007 break;
6891 7008
6892 7009 case DTRACEACT_USYM:
6893 7010 case DTRACEACT_UMOD:
6894 7011 case DTRACEACT_UADDR: {
6895 7012 struct pid *pid = curthread->t_procp->p_pidp;
6896 7013
6897 7014 if (!dtrace_priv_proc(state, &mstate))
6898 7015 continue;
6899 7016
6900 7017 DTRACE_STORE(uint64_t, tomax,
6901 7018 valoffs, (uint64_t)pid->pid_id);
6902 7019 DTRACE_STORE(uint64_t, tomax,
6903 7020 valoffs + sizeof (uint64_t), val);
6904 7021
6905 7022 continue;
6906 7023 }
6907 7024
6908 7025 case DTRACEACT_EXIT: {
6909 7026 /*
6910 7027 * For the exit action, we are going to attempt
6911 7028 * to atomically set our activity to be
6912 7029 * draining. If this fails (either because
6913 7030 * another CPU has beat us to the exit action,
6914 7031 * or because our current activity is something
6915 7032 * other than ACTIVE or WARMUP), we will
6916 7033 * continue. This assures that the exit action
6917 7034 * can be successfully recorded at most once
6918 7035 * when we're in the ACTIVE state. If we're
6919 7036 * encountering the exit() action while in
6920 7037 * COOLDOWN, however, we want to honor the new
6921 7038 * status code. (We know that we're the only
6922 7039 * thread in COOLDOWN, so there is no race.)
6923 7040 */
6924 7041 void *activity = &state->dts_activity;
6925 7042 dtrace_activity_t current = state->dts_activity;
6926 7043
6927 7044 if (current == DTRACE_ACTIVITY_COOLDOWN)
6928 7045 break;
6929 7046
6930 7047 if (current != DTRACE_ACTIVITY_WARMUP)
6931 7048 current = DTRACE_ACTIVITY_ACTIVE;
6932 7049
6933 7050 if (dtrace_cas32(activity, current,
6934 7051 DTRACE_ACTIVITY_DRAINING) != current) {
6935 7052 *flags |= CPU_DTRACE_DROP;
6936 7053 continue;
6937 7054 }
6938 7055
6939 7056 break;
6940 7057 }
6941 7058
6942 7059 default:
6943 7060 ASSERT(0);
6944 7061 }
6945 7062
6946 7063 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) {
6947 7064 uintptr_t end = valoffs + size;
6948 7065
6949 7066 if (tracememsize != 0 &&
6950 7067 valoffs + tracememsize < end) {
6951 7068 end = valoffs + tracememsize;
6952 7069 tracememsize = 0;
6953 7070 }
6954 7071
6955 7072 if (!dtrace_vcanload((void *)(uintptr_t)val,
6956 7073 &dp->dtdo_rtype, &mstate, vstate))
6957 7074 continue;
6958 7075
6959 7076 /*
6960 7077 * If this is a string, we're going to only
6961 7078 * load until we find the zero byte -- after
6962 7079 * which we'll store zero bytes.
6963 7080 */
6964 7081 if (dp->dtdo_rtype.dtdt_kind ==
6965 7082 DIF_TYPE_STRING) {
6966 7083 char c = '\0' + 1;
6967 7084 int intuple = act->dta_intuple;
6968 7085 size_t s;
6969 7086
6970 7087 for (s = 0; s < size; s++) {
6971 7088 if (c != '\0')
6972 7089 c = dtrace_load8(val++);
6973 7090
6974 7091 DTRACE_STORE(uint8_t, tomax,
6975 7092 valoffs++, c);
6976 7093
6977 7094 if (c == '\0' && intuple)
6978 7095 break;
6979 7096 }
6980 7097
6981 7098 continue;
6982 7099 }
6983 7100
6984 7101 while (valoffs < end) {
6985 7102 DTRACE_STORE(uint8_t, tomax, valoffs++,
6986 7103 dtrace_load8(val++));
6987 7104 }
6988 7105
6989 7106 continue;
6990 7107 }
6991 7108
6992 7109 switch (size) {
6993 7110 case 0:
6994 7111 break;
6995 7112
6996 7113 case sizeof (uint8_t):
6997 7114 DTRACE_STORE(uint8_t, tomax, valoffs, val);
6998 7115 break;
6999 7116 case sizeof (uint16_t):
7000 7117 DTRACE_STORE(uint16_t, tomax, valoffs, val);
7001 7118 break;
7002 7119 case sizeof (uint32_t):
7003 7120 DTRACE_STORE(uint32_t, tomax, valoffs, val);
7004 7121 break;
7005 7122 case sizeof (uint64_t):
7006 7123 DTRACE_STORE(uint64_t, tomax, valoffs, val);
7007 7124 break;
7008 7125 default:
7009 7126 /*
7010 7127 * Any other size should have been returned by
7011 7128 * reference, not by value.
7012 7129 */
7013 7130 ASSERT(0);
7014 7131 break;
7015 7132 }
7016 7133 }
7017 7134
7018 7135 if (*flags & CPU_DTRACE_DROP)
7019 7136 continue;
7020 7137
7021 7138 if (*flags & CPU_DTRACE_FAULT) {
7022 7139 int ndx;
7023 7140 dtrace_action_t *err;
7024 7141
7025 7142 buf->dtb_errors++;
7026 7143
7027 7144 if (probe->dtpr_id == dtrace_probeid_error) {
7028 7145 /*
7029 7146 * There's nothing we can do -- we had an
7030 7147 * error on the error probe. We bump an
7031 7148 * error counter to at least indicate that
7032 7149 * this condition happened.
7033 7150 */
7034 7151 dtrace_error(&state->dts_dblerrors);
7035 7152 continue;
7036 7153 }
7037 7154
7038 7155 if (vtime) {
7039 7156 /*
7040 7157 * Before recursing on dtrace_probe(), we
7041 7158 * need to explicitly clear out our start
7042 7159 * time to prevent it from being accumulated
7043 7160 * into t_dtrace_vtime.
7044 7161 */
7045 7162 curthread->t_dtrace_start = 0;
7046 7163 }
7047 7164
7048 7165 /*
7049 7166 * Iterate over the actions to figure out which action
7050 7167 * we were processing when we experienced the error.
7051 7168 * Note that act points _past_ the faulting action; if
7052 7169 * act is ecb->dte_action, the fault was in the
7053 7170 * predicate, if it's ecb->dte_action->dta_next it's
7054 7171 * in action #1, and so on.
7055 7172 */
7056 7173 for (err = ecb->dte_action, ndx = 0;
7057 7174 err != act; err = err->dta_next, ndx++)
7058 7175 continue;
7059 7176
7060 7177 dtrace_probe_error(state, ecb->dte_epid, ndx,
7061 7178 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
7062 7179 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
7063 7180 cpu_core[cpuid].cpuc_dtrace_illval);
7064 7181
7065 7182 continue;
7066 7183 }
7067 7184
7068 7185 if (!committed)
7069 7186 buf->dtb_offset = offs + ecb->dte_size;
7070 7187 }
7071 7188
7072 7189 if (vtime)
7073 7190 curthread->t_dtrace_start = dtrace_gethrtime();
7074 7191
7075 7192 dtrace_interrupt_enable(cookie);
7076 7193 }
7077 7194
7078 7195 /*
7079 7196 * DTrace Probe Hashing Functions
7080 7197 *
7081 7198 * The functions in this section (and indeed, the functions in remaining
7082 7199 * sections) are not _called_ from probe context. (Any exceptions to this are
7083 7200 * marked with a "Note:".) Rather, they are called from elsewhere in the
7084 7201 * DTrace framework to look-up probes in, add probes to and remove probes from
7085 7202 * the DTrace probe hashes. (Each probe is hashed by each element of the
7086 7203 * probe tuple -- allowing for fast lookups, regardless of what was
7087 7204 * specified.)
7088 7205 */
7089 7206 static uint_t
7090 7207 dtrace_hash_str(char *p)
7091 7208 {
7092 7209 unsigned int g;
7093 7210 uint_t hval = 0;
7094 7211
7095 7212 while (*p) {
7096 7213 hval = (hval << 4) + *p++;
7097 7214 if ((g = (hval & 0xf0000000)) != 0)
7098 7215 hval ^= g >> 24;
7099 7216 hval &= ~g;
7100 7217 }
7101 7218 return (hval);
7102 7219 }
7103 7220
7104 7221 static dtrace_hash_t *
7105 7222 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
7106 7223 {
7107 7224 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
7108 7225
7109 7226 hash->dth_stroffs = stroffs;
7110 7227 hash->dth_nextoffs = nextoffs;
7111 7228 hash->dth_prevoffs = prevoffs;
7112 7229
7113 7230 hash->dth_size = 1;
7114 7231 hash->dth_mask = hash->dth_size - 1;
7115 7232
7116 7233 hash->dth_tab = kmem_zalloc(hash->dth_size *
7117 7234 sizeof (dtrace_hashbucket_t *), KM_SLEEP);
7118 7235
7119 7236 return (hash);
7120 7237 }
7121 7238
7122 7239 static void
7123 7240 dtrace_hash_destroy(dtrace_hash_t *hash)
7124 7241 {
7125 7242 #ifdef DEBUG
7126 7243 int i;
7127 7244
7128 7245 for (i = 0; i < hash->dth_size; i++)
7129 7246 ASSERT(hash->dth_tab[i] == NULL);
7130 7247 #endif
7131 7248
7132 7249 kmem_free(hash->dth_tab,
7133 7250 hash->dth_size * sizeof (dtrace_hashbucket_t *));
7134 7251 kmem_free(hash, sizeof (dtrace_hash_t));
7135 7252 }
7136 7253
7137 7254 static void
7138 7255 dtrace_hash_resize(dtrace_hash_t *hash)
7139 7256 {
7140 7257 int size = hash->dth_size, i, ndx;
7141 7258 int new_size = hash->dth_size << 1;
7142 7259 int new_mask = new_size - 1;
7143 7260 dtrace_hashbucket_t **new_tab, *bucket, *next;
7144 7261
7145 7262 ASSERT((new_size & new_mask) == 0);
7146 7263
7147 7264 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
7148 7265
7149 7266 for (i = 0; i < size; i++) {
7150 7267 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
7151 7268 dtrace_probe_t *probe = bucket->dthb_chain;
7152 7269
7153 7270 ASSERT(probe != NULL);
7154 7271 ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
7155 7272
7156 7273 next = bucket->dthb_next;
7157 7274 bucket->dthb_next = new_tab[ndx];
7158 7275 new_tab[ndx] = bucket;
7159 7276 }
7160 7277 }
7161 7278
7162 7279 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
7163 7280 hash->dth_tab = new_tab;
7164 7281 hash->dth_size = new_size;
7165 7282 hash->dth_mask = new_mask;
7166 7283 }
7167 7284
7168 7285 static void
7169 7286 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
7170 7287 {
7171 7288 int hashval = DTRACE_HASHSTR(hash, new);
7172 7289 int ndx = hashval & hash->dth_mask;
7173 7290 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7174 7291 dtrace_probe_t **nextp, **prevp;
7175 7292
7176 7293 for (; bucket != NULL; bucket = bucket->dthb_next) {
7177 7294 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
7178 7295 goto add;
7179 7296 }
7180 7297
7181 7298 if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
7182 7299 dtrace_hash_resize(hash);
7183 7300 dtrace_hash_add(hash, new);
7184 7301 return;
7185 7302 }
7186 7303
7187 7304 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
7188 7305 bucket->dthb_next = hash->dth_tab[ndx];
7189 7306 hash->dth_tab[ndx] = bucket;
7190 7307 hash->dth_nbuckets++;
7191 7308
7192 7309 add:
7193 7310 nextp = DTRACE_HASHNEXT(hash, new);
7194 7311 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
7195 7312 *nextp = bucket->dthb_chain;
7196 7313
7197 7314 if (bucket->dthb_chain != NULL) {
7198 7315 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
7199 7316 ASSERT(*prevp == NULL);
7200 7317 *prevp = new;
7201 7318 }
7202 7319
7203 7320 bucket->dthb_chain = new;
7204 7321 bucket->dthb_len++;
7205 7322 }
7206 7323
7207 7324 static dtrace_probe_t *
7208 7325 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
7209 7326 {
7210 7327 int hashval = DTRACE_HASHSTR(hash, template);
7211 7328 int ndx = hashval & hash->dth_mask;
7212 7329 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7213 7330
7214 7331 for (; bucket != NULL; bucket = bucket->dthb_next) {
7215 7332 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
7216 7333 return (bucket->dthb_chain);
7217 7334 }
7218 7335
7219 7336 return (NULL);
7220 7337 }
7221 7338
7222 7339 static int
7223 7340 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
7224 7341 {
7225 7342 int hashval = DTRACE_HASHSTR(hash, template);
7226 7343 int ndx = hashval & hash->dth_mask;
7227 7344 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7228 7345
7229 7346 for (; bucket != NULL; bucket = bucket->dthb_next) {
7230 7347 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
7231 7348 return (bucket->dthb_len);
7232 7349 }
7233 7350
7234 7351 return (NULL);
7235 7352 }
7236 7353
7237 7354 static void
7238 7355 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
7239 7356 {
7240 7357 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
7241 7358 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7242 7359
7243 7360 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
7244 7361 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
7245 7362
7246 7363 /*
7247 7364 * Find the bucket that we're removing this probe from.
7248 7365 */
7249 7366 for (; bucket != NULL; bucket = bucket->dthb_next) {
7250 7367 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
7251 7368 break;
7252 7369 }
7253 7370
7254 7371 ASSERT(bucket != NULL);
7255 7372
7256 7373 if (*prevp == NULL) {
7257 7374 if (*nextp == NULL) {
7258 7375 /*
7259 7376 * The removed probe was the only probe on this
7260 7377 * bucket; we need to remove the bucket.
7261 7378 */
7262 7379 dtrace_hashbucket_t *b = hash->dth_tab[ndx];
7263 7380
7264 7381 ASSERT(bucket->dthb_chain == probe);
7265 7382 ASSERT(b != NULL);
7266 7383
7267 7384 if (b == bucket) {
7268 7385 hash->dth_tab[ndx] = bucket->dthb_next;
7269 7386 } else {
7270 7387 while (b->dthb_next != bucket)
7271 7388 b = b->dthb_next;
7272 7389 b->dthb_next = bucket->dthb_next;
7273 7390 }
7274 7391
7275 7392 ASSERT(hash->dth_nbuckets > 0);
7276 7393 hash->dth_nbuckets--;
7277 7394 kmem_free(bucket, sizeof (dtrace_hashbucket_t));
7278 7395 return;
7279 7396 }
7280 7397
7281 7398 bucket->dthb_chain = *nextp;
7282 7399 } else {
7283 7400 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
7284 7401 }
7285 7402
7286 7403 if (*nextp != NULL)
7287 7404 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
7288 7405 }
7289 7406
7290 7407 /*
7291 7408 * DTrace Utility Functions
7292 7409 *
7293 7410 * These are random utility functions that are _not_ called from probe context.
7294 7411 */
7295 7412 static int
7296 7413 dtrace_badattr(const dtrace_attribute_t *a)
7297 7414 {
7298 7415 return (a->dtat_name > DTRACE_STABILITY_MAX ||
7299 7416 a->dtat_data > DTRACE_STABILITY_MAX ||
7300 7417 a->dtat_class > DTRACE_CLASS_MAX);
7301 7418 }
7302 7419
7303 7420 /*
7304 7421 * Return a duplicate copy of a string. If the specified string is NULL,
7305 7422 * this function returns a zero-length string.
7306 7423 */
7307 7424 static char *
7308 7425 dtrace_strdup(const char *str)
7309 7426 {
7310 7427 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
7311 7428
7312 7429 if (str != NULL)
7313 7430 (void) strcpy(new, str);
7314 7431
7315 7432 return (new);
7316 7433 }
7317 7434
7318 7435 #define DTRACE_ISALPHA(c) \
7319 7436 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
7320 7437
7321 7438 static int
7322 7439 dtrace_badname(const char *s)
7323 7440 {
7324 7441 char c;
7325 7442
7326 7443 if (s == NULL || (c = *s++) == '\0')
7327 7444 return (0);
7328 7445
7329 7446 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
7330 7447 return (1);
7331 7448
7332 7449 while ((c = *s++) != '\0') {
7333 7450 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
7334 7451 c != '-' && c != '_' && c != '.' && c != '`')
7335 7452 return (1);
7336 7453 }
7337 7454
7338 7455 return (0);
7339 7456 }
7340 7457
7341 7458 static void
7342 7459 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
7343 7460 {
7344 7461 uint32_t priv;
7345 7462
7346 7463 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
7347 7464 /*
7348 7465 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
7349 7466 */
7350 7467 priv = DTRACE_PRIV_ALL;
7351 7468 } else {
7352 7469 *uidp = crgetuid(cr);
7353 7470 *zoneidp = crgetzonedid(cr);
7354 7471
7355 7472 priv = 0;
7356 7473 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
7357 7474 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
7358 7475 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
7359 7476 priv |= DTRACE_PRIV_USER;
7360 7477 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
7361 7478 priv |= DTRACE_PRIV_PROC;
7362 7479 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
7363 7480 priv |= DTRACE_PRIV_OWNER;
7364 7481 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
7365 7482 priv |= DTRACE_PRIV_ZONEOWNER;
7366 7483 }
7367 7484
7368 7485 *privp = priv;
7369 7486 }
7370 7487
7371 7488 #ifdef DTRACE_ERRDEBUG
7372 7489 static void
7373 7490 dtrace_errdebug(const char *str)
7374 7491 {
7375 7492 int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ;
7376 7493 int occupied = 0;
7377 7494
7378 7495 mutex_enter(&dtrace_errlock);
7379 7496 dtrace_errlast = str;
7380 7497 dtrace_errthread = curthread;
7381 7498
7382 7499 while (occupied++ < DTRACE_ERRHASHSZ) {
7383 7500 if (dtrace_errhash[hval].dter_msg == str) {
7384 7501 dtrace_errhash[hval].dter_count++;
7385 7502 goto out;
7386 7503 }
7387 7504
7388 7505 if (dtrace_errhash[hval].dter_msg != NULL) {
7389 7506 hval = (hval + 1) % DTRACE_ERRHASHSZ;
7390 7507 continue;
7391 7508 }
7392 7509
7393 7510 dtrace_errhash[hval].dter_msg = str;
7394 7511 dtrace_errhash[hval].dter_count = 1;
7395 7512 goto out;
7396 7513 }
7397 7514
7398 7515 panic("dtrace: undersized error hash");
7399 7516 out:
7400 7517 mutex_exit(&dtrace_errlock);
7401 7518 }
7402 7519 #endif
7403 7520
7404 7521 /*
7405 7522 * DTrace Matching Functions
7406 7523 *
7407 7524 * These functions are used to match groups of probes, given some elements of
7408 7525 * a probe tuple, or some globbed expressions for elements of a probe tuple.
7409 7526 */
7410 7527 static int
7411 7528 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
7412 7529 zoneid_t zoneid)
7413 7530 {
7414 7531 if (priv != DTRACE_PRIV_ALL) {
7415 7532 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
7416 7533 uint32_t match = priv & ppriv;
7417 7534
7418 7535 /*
7419 7536 * No PRIV_DTRACE_* privileges...
7420 7537 */
7421 7538 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
7422 7539 DTRACE_PRIV_KERNEL)) == 0)
7423 7540 return (0);
7424 7541
7425 7542 /*
7426 7543 * No matching bits, but there were bits to match...
7427 7544 */
7428 7545 if (match == 0 && ppriv != 0)
7429 7546 return (0);
7430 7547
7431 7548 /*
7432 7549 * Need to have permissions to the process, but don't...
7433 7550 */
7434 7551 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
7435 7552 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
7436 7553 return (0);
7437 7554 }
7438 7555
7439 7556 /*
7440 7557 * Need to be in the same zone unless we possess the
7441 7558 * privilege to examine all zones.
7442 7559 */
7443 7560 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
7444 7561 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
7445 7562 return (0);
7446 7563 }
7447 7564 }
7448 7565
7449 7566 return (1);
7450 7567 }
7451 7568
7452 7569 /*
7453 7570 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
7454 7571 * consists of input pattern strings and an ops-vector to evaluate them.
7455 7572 * This function returns >0 for match, 0 for no match, and <0 for error.
7456 7573 */
7457 7574 static int
7458 7575 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
7459 7576 uint32_t priv, uid_t uid, zoneid_t zoneid)
7460 7577 {
7461 7578 dtrace_provider_t *pvp = prp->dtpr_provider;
7462 7579 int rv;
7463 7580
7464 7581 if (pvp->dtpv_defunct)
7465 7582 return (0);
7466 7583
7467 7584 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
7468 7585 return (rv);
7469 7586
7470 7587 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
7471 7588 return (rv);
7472 7589
7473 7590 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
7474 7591 return (rv);
7475 7592
7476 7593 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
7477 7594 return (rv);
7478 7595
7479 7596 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
7480 7597 return (0);
7481 7598
7482 7599 return (rv);
7483 7600 }
7484 7601
7485 7602 /*
7486 7603 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
7487 7604 * interface for matching a glob pattern 'p' to an input string 's'. Unlike
7488 7605 * libc's version, the kernel version only applies to 8-bit ASCII strings.
7489 7606 * In addition, all of the recursion cases except for '*' matching have been
7490 7607 * unwound. For '*', we still implement recursive evaluation, but a depth
7491 7608 * counter is maintained and matching is aborted if we recurse too deep.
7492 7609 * The function returns 0 if no match, >0 if match, and <0 if recursion error.
7493 7610 */
7494 7611 static int
7495 7612 dtrace_match_glob(const char *s, const char *p, int depth)
7496 7613 {
7497 7614 const char *olds;
7498 7615 char s1, c;
7499 7616 int gs;
7500 7617
7501 7618 if (depth > DTRACE_PROBEKEY_MAXDEPTH)
7502 7619 return (-1);
7503 7620
7504 7621 if (s == NULL)
7505 7622 s = ""; /* treat NULL as empty string */
7506 7623
7507 7624 top:
7508 7625 olds = s;
7509 7626 s1 = *s++;
7510 7627
7511 7628 if (p == NULL)
7512 7629 return (0);
7513 7630
7514 7631 if ((c = *p++) == '\0')
7515 7632 return (s1 == '\0');
7516 7633
7517 7634 switch (c) {
7518 7635 case '[': {
7519 7636 int ok = 0, notflag = 0;
7520 7637 char lc = '\0';
7521 7638
7522 7639 if (s1 == '\0')
7523 7640 return (0);
7524 7641
7525 7642 if (*p == '!') {
7526 7643 notflag = 1;
7527 7644 p++;
7528 7645 }
7529 7646
7530 7647 if ((c = *p++) == '\0')
7531 7648 return (0);
7532 7649
7533 7650 do {
7534 7651 if (c == '-' && lc != '\0' && *p != ']') {
7535 7652 if ((c = *p++) == '\0')
7536 7653 return (0);
7537 7654 if (c == '\\' && (c = *p++) == '\0')
7538 7655 return (0);
7539 7656
7540 7657 if (notflag) {
7541 7658 if (s1 < lc || s1 > c)
7542 7659 ok++;
7543 7660 else
7544 7661 return (0);
7545 7662 } else if (lc <= s1 && s1 <= c)
7546 7663 ok++;
7547 7664
7548 7665 } else if (c == '\\' && (c = *p++) == '\0')
7549 7666 return (0);
7550 7667
7551 7668 lc = c; /* save left-hand 'c' for next iteration */
7552 7669
7553 7670 if (notflag) {
7554 7671 if (s1 != c)
7555 7672 ok++;
7556 7673 else
7557 7674 return (0);
7558 7675 } else if (s1 == c)
7559 7676 ok++;
7560 7677
7561 7678 if ((c = *p++) == '\0')
7562 7679 return (0);
7563 7680
7564 7681 } while (c != ']');
7565 7682
7566 7683 if (ok)
7567 7684 goto top;
7568 7685
7569 7686 return (0);
7570 7687 }
7571 7688
7572 7689 case '\\':
7573 7690 if ((c = *p++) == '\0')
7574 7691 return (0);
7575 7692 /*FALLTHRU*/
7576 7693
7577 7694 default:
7578 7695 if (c != s1)
7579 7696 return (0);
7580 7697 /*FALLTHRU*/
7581 7698
7582 7699 case '?':
7583 7700 if (s1 != '\0')
7584 7701 goto top;
7585 7702 return (0);
7586 7703
7587 7704 case '*':
7588 7705 while (*p == '*')
7589 7706 p++; /* consecutive *'s are identical to a single one */
7590 7707
7591 7708 if (*p == '\0')
7592 7709 return (1);
7593 7710
7594 7711 for (s = olds; *s != '\0'; s++) {
7595 7712 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
7596 7713 return (gs);
7597 7714 }
7598 7715
7599 7716 return (0);
7600 7717 }
7601 7718 }
7602 7719
7603 7720 /*ARGSUSED*/
7604 7721 static int
7605 7722 dtrace_match_string(const char *s, const char *p, int depth)
7606 7723 {
7607 7724 return (s != NULL && strcmp(s, p) == 0);
7608 7725 }
7609 7726
7610 7727 /*ARGSUSED*/
7611 7728 static int
7612 7729 dtrace_match_nul(const char *s, const char *p, int depth)
7613 7730 {
7614 7731 return (1); /* always match the empty pattern */
7615 7732 }
7616 7733
7617 7734 /*ARGSUSED*/
7618 7735 static int
7619 7736 dtrace_match_nonzero(const char *s, const char *p, int depth)
7620 7737 {
7621 7738 return (s != NULL && s[0] != '\0');
7622 7739 }
7623 7740
7624 7741 static int
7625 7742 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
7626 7743 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
7627 7744 {
7628 7745 dtrace_probe_t template, *probe;
7629 7746 dtrace_hash_t *hash = NULL;
7630 7747 int len, rc, best = INT_MAX, nmatched = 0;
7631 7748 dtrace_id_t i;
7632 7749
7633 7750 ASSERT(MUTEX_HELD(&dtrace_lock));
7634 7751
7635 7752 /*
7636 7753 * If the probe ID is specified in the key, just lookup by ID and
7637 7754 * invoke the match callback once if a matching probe is found.
7638 7755 */
7639 7756 if (pkp->dtpk_id != DTRACE_IDNONE) {
7640 7757 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
7641 7758 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
7642 7759 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL)
7643 7760 return (DTRACE_MATCH_FAIL);
7644 7761 nmatched++;
7645 7762 }
7646 7763 return (nmatched);
7647 7764 }
7648 7765
7649 7766 template.dtpr_mod = (char *)pkp->dtpk_mod;
7650 7767 template.dtpr_func = (char *)pkp->dtpk_func;
7651 7768 template.dtpr_name = (char *)pkp->dtpk_name;
7652 7769
7653 7770 /*
7654 7771 * We want to find the most distinct of the module name, function
7655 7772 * name, and name. So for each one that is not a glob pattern or
7656 7773 * empty string, we perform a lookup in the corresponding hash and
7657 7774 * use the hash table with the fewest collisions to do our search.
7658 7775 */
7659 7776 if (pkp->dtpk_mmatch == &dtrace_match_string &&
7660 7777 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
7661 7778 best = len;
7662 7779 hash = dtrace_bymod;
7663 7780 }
7664 7781
7665 7782 if (pkp->dtpk_fmatch == &dtrace_match_string &&
7666 7783 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
7667 7784 best = len;
7668 7785 hash = dtrace_byfunc;
7669 7786 }
7670 7787
7671 7788 if (pkp->dtpk_nmatch == &dtrace_match_string &&
7672 7789 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
7673 7790 best = len;
7674 7791 hash = dtrace_byname;
7675 7792 }
7676 7793
7677 7794 /*
7678 7795 * If we did not select a hash table, iterate over every probe and
7679 7796 * invoke our callback for each one that matches our input probe key.
7680 7797 */
7681 7798 if (hash == NULL) {
7682 7799 for (i = 0; i < dtrace_nprobes; i++) {
7683 7800 if ((probe = dtrace_probes[i]) == NULL ||
7684 7801 dtrace_match_probe(probe, pkp, priv, uid,
7685 7802 zoneid) <= 0)
7686 7803 continue;
7687 7804
7688 7805 nmatched++;
7689 7806
7690 7807 if ((rc = (*matched)(probe, arg)) !=
7691 7808 DTRACE_MATCH_NEXT) {
7692 7809 if (rc == DTRACE_MATCH_FAIL)
7693 7810 return (DTRACE_MATCH_FAIL);
7694 7811 break;
7695 7812 }
7696 7813 }
7697 7814
7698 7815 return (nmatched);
7699 7816 }
7700 7817
7701 7818 /*
7702 7819 * If we selected a hash table, iterate over each probe of the same key
7703 7820 * name and invoke the callback for every probe that matches the other
7704 7821 * attributes of our input probe key.
7705 7822 */
7706 7823 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
7707 7824 probe = *(DTRACE_HASHNEXT(hash, probe))) {
7708 7825
7709 7826 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
7710 7827 continue;
7711 7828
7712 7829 nmatched++;
7713 7830
7714 7831 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) {
7715 7832 if (rc == DTRACE_MATCH_FAIL)
7716 7833 return (DTRACE_MATCH_FAIL);
7717 7834 break;
7718 7835 }
7719 7836 }
7720 7837
7721 7838 return (nmatched);
7722 7839 }
7723 7840
7724 7841 /*
7725 7842 * Return the function pointer dtrace_probecmp() should use to compare the
7726 7843 * specified pattern with a string. For NULL or empty patterns, we select
7727 7844 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob().
7728 7845 * For non-empty non-glob strings, we use dtrace_match_string().
7729 7846 */
7730 7847 static dtrace_probekey_f *
7731 7848 dtrace_probekey_func(const char *p)
7732 7849 {
7733 7850 char c;
7734 7851
7735 7852 if (p == NULL || *p == '\0')
7736 7853 return (&dtrace_match_nul);
7737 7854
7738 7855 while ((c = *p++) != '\0') {
7739 7856 if (c == '[' || c == '?' || c == '*' || c == '\\')
7740 7857 return (&dtrace_match_glob);
7741 7858 }
7742 7859
7743 7860 return (&dtrace_match_string);
7744 7861 }
7745 7862
7746 7863 /*
7747 7864 * Build a probe comparison key for use with dtrace_match_probe() from the
7748 7865 * given probe description. By convention, a null key only matches anchored
7749 7866 * probes: if each field is the empty string, reset dtpk_fmatch to
7750 7867 * dtrace_match_nonzero().
7751 7868 */
7752 7869 static void
7753 7870 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
7754 7871 {
7755 7872 pkp->dtpk_prov = pdp->dtpd_provider;
7756 7873 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
7757 7874
7758 7875 pkp->dtpk_mod = pdp->dtpd_mod;
7759 7876 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
7760 7877
7761 7878 pkp->dtpk_func = pdp->dtpd_func;
7762 7879 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
7763 7880
7764 7881 pkp->dtpk_name = pdp->dtpd_name;
7765 7882 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
7766 7883
7767 7884 pkp->dtpk_id = pdp->dtpd_id;
7768 7885
7769 7886 if (pkp->dtpk_id == DTRACE_IDNONE &&
7770 7887 pkp->dtpk_pmatch == &dtrace_match_nul &&
7771 7888 pkp->dtpk_mmatch == &dtrace_match_nul &&
7772 7889 pkp->dtpk_fmatch == &dtrace_match_nul &&
7773 7890 pkp->dtpk_nmatch == &dtrace_match_nul)
7774 7891 pkp->dtpk_fmatch = &dtrace_match_nonzero;
7775 7892 }
7776 7893
7777 7894 /*
7778 7895 * DTrace Provider-to-Framework API Functions
7779 7896 *
7780 7897 * These functions implement much of the Provider-to-Framework API, as
7781 7898 * described in <sys/dtrace.h>. The parts of the API not in this section are
7782 7899 * the functions in the API for probe management (found below), and
7783 7900 * dtrace_probe() itself (found above).
7784 7901 */
7785 7902
7786 7903 /*
7787 7904 * Register the calling provider with the DTrace framework. This should
7788 7905 * generally be called by DTrace providers in their attach(9E) entry point.
7789 7906 */
7790 7907 int
7791 7908 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
7792 7909 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
7793 7910 {
7794 7911 dtrace_provider_t *provider;
7795 7912
7796 7913 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
7797 7914 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7798 7915 "arguments", name ? name : "<NULL>");
7799 7916 return (EINVAL);
7800 7917 }
7801 7918
7802 7919 if (name[0] == '\0' || dtrace_badname(name)) {
7803 7920 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7804 7921 "provider name", name);
7805 7922 return (EINVAL);
7806 7923 }
7807 7924
7808 7925 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
7809 7926 pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
7810 7927 pops->dtps_destroy == NULL ||
7811 7928 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
7812 7929 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7813 7930 "provider ops", name);
7814 7931 return (EINVAL);
7815 7932 }
7816 7933
7817 7934 if (dtrace_badattr(&pap->dtpa_provider) ||
7818 7935 dtrace_badattr(&pap->dtpa_mod) ||
7819 7936 dtrace_badattr(&pap->dtpa_func) ||
7820 7937 dtrace_badattr(&pap->dtpa_name) ||
7821 7938 dtrace_badattr(&pap->dtpa_args)) {
7822 7939 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7823 7940 "provider attributes", name);
7824 7941 return (EINVAL);
7825 7942 }
7826 7943
7827 7944 if (priv & ~DTRACE_PRIV_ALL) {
7828 7945 cmn_err(CE_WARN, "failed to register provider '%s': invalid "
7829 7946 "privilege attributes", name);
7830 7947 return (EINVAL);
7831 7948 }
7832 7949
7833 7950 if ((priv & DTRACE_PRIV_KERNEL) &&
7834 7951 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
7835 7952 pops->dtps_mode == NULL) {
7836 7953 cmn_err(CE_WARN, "failed to register provider '%s': need "
7837 7954 "dtps_mode() op for given privilege attributes", name);
7838 7955 return (EINVAL);
7839 7956 }
7840 7957
7841 7958 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
7842 7959 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7843 7960 (void) strcpy(provider->dtpv_name, name);
7844 7961
7845 7962 provider->dtpv_attr = *pap;
7846 7963 provider->dtpv_priv.dtpp_flags = priv;
7847 7964 if (cr != NULL) {
7848 7965 provider->dtpv_priv.dtpp_uid = crgetuid(cr);
7849 7966 provider->dtpv_priv.dtpp_zoneid = crgetzonedid(cr);
7850 7967 }
7851 7968 provider->dtpv_pops = *pops;
7852 7969
7853 7970 if (pops->dtps_provide == NULL) {
7854 7971 ASSERT(pops->dtps_provide_module != NULL);
7855 7972 provider->dtpv_pops.dtps_provide =
7856 7973 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop;
7857 7974 }
7858 7975
7859 7976 if (pops->dtps_provide_module == NULL) {
7860 7977 ASSERT(pops->dtps_provide != NULL);
7861 7978 provider->dtpv_pops.dtps_provide_module =
7862 7979 (void (*)(void *, struct modctl *))dtrace_nullop;
7863 7980 }
7864 7981
7865 7982 if (pops->dtps_suspend == NULL) {
7866 7983 ASSERT(pops->dtps_resume == NULL);
7867 7984 provider->dtpv_pops.dtps_suspend =
7868 7985 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
7869 7986 provider->dtpv_pops.dtps_resume =
7870 7987 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
7871 7988 }
7872 7989
7873 7990 provider->dtpv_arg = arg;
7874 7991 *idp = (dtrace_provider_id_t)provider;
7875 7992
7876 7993 if (pops == &dtrace_provider_ops) {
7877 7994 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7878 7995 ASSERT(MUTEX_HELD(&dtrace_lock));
7879 7996 ASSERT(dtrace_anon.dta_enabling == NULL);
7880 7997
7881 7998 /*
7882 7999 * We make sure that the DTrace provider is at the head of
7883 8000 * the provider chain.
7884 8001 */
7885 8002 provider->dtpv_next = dtrace_provider;
7886 8003 dtrace_provider = provider;
7887 8004 return (0);
7888 8005 }
7889 8006
7890 8007 mutex_enter(&dtrace_provider_lock);
7891 8008 mutex_enter(&dtrace_lock);
7892 8009
7893 8010 /*
7894 8011 * If there is at least one provider registered, we'll add this
7895 8012 * provider after the first provider.
7896 8013 */
7897 8014 if (dtrace_provider != NULL) {
7898 8015 provider->dtpv_next = dtrace_provider->dtpv_next;
7899 8016 dtrace_provider->dtpv_next = provider;
7900 8017 } else {
7901 8018 dtrace_provider = provider;
7902 8019 }
7903 8020
7904 8021 if (dtrace_retained != NULL) {
7905 8022 dtrace_enabling_provide(provider);
7906 8023
7907 8024 /*
7908 8025 * Now we need to call dtrace_enabling_matchall() -- which
7909 8026 * will acquire cpu_lock and dtrace_lock. We therefore need
7910 8027 * to drop all of our locks before calling into it...
7911 8028 */
7912 8029 mutex_exit(&dtrace_lock);
7913 8030 mutex_exit(&dtrace_provider_lock);
7914 8031 dtrace_enabling_matchall();
7915 8032
7916 8033 return (0);
7917 8034 }
7918 8035
7919 8036 mutex_exit(&dtrace_lock);
7920 8037 mutex_exit(&dtrace_provider_lock);
7921 8038
7922 8039 return (0);
7923 8040 }
7924 8041
7925 8042 /*
7926 8043 * Unregister the specified provider from the DTrace framework. This should
7927 8044 * generally be called by DTrace providers in their detach(9E) entry point.
7928 8045 */
7929 8046 int
7930 8047 dtrace_unregister(dtrace_provider_id_t id)
7931 8048 {
7932 8049 dtrace_provider_t *old = (dtrace_provider_t *)id;
7933 8050 dtrace_provider_t *prev = NULL;
7934 8051 int i, self = 0, noreap = 0;
7935 8052 dtrace_probe_t *probe, *first = NULL;
7936 8053
7937 8054 if (old->dtpv_pops.dtps_enable ==
7938 8055 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) {
7939 8056 /*
7940 8057 * If DTrace itself is the provider, we're called with locks
7941 8058 * already held.
7942 8059 */
7943 8060 ASSERT(old == dtrace_provider);
7944 8061 ASSERT(dtrace_devi != NULL);
7945 8062 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
7946 8063 ASSERT(MUTEX_HELD(&dtrace_lock));
7947 8064 self = 1;
7948 8065
7949 8066 if (dtrace_provider->dtpv_next != NULL) {
7950 8067 /*
7951 8068 * There's another provider here; return failure.
7952 8069 */
7953 8070 return (EBUSY);
7954 8071 }
7955 8072 } else {
7956 8073 mutex_enter(&dtrace_provider_lock);
7957 8074 mutex_enter(&mod_lock);
7958 8075 mutex_enter(&dtrace_lock);
7959 8076 }
7960 8077
7961 8078 /*
7962 8079 * If anyone has /dev/dtrace open, or if there are anonymous enabled
7963 8080 * probes, we refuse to let providers slither away, unless this
7964 8081 * provider has already been explicitly invalidated.
7965 8082 */
7966 8083 if (!old->dtpv_defunct &&
7967 8084 (dtrace_opens || (dtrace_anon.dta_state != NULL &&
7968 8085 dtrace_anon.dta_state->dts_necbs > 0))) {
7969 8086 if (!self) {
7970 8087 mutex_exit(&dtrace_lock);
7971 8088 mutex_exit(&mod_lock);
7972 8089 mutex_exit(&dtrace_provider_lock);
7973 8090 }
7974 8091 return (EBUSY);
7975 8092 }
7976 8093
7977 8094 /*
7978 8095 * Attempt to destroy the probes associated with this provider.
7979 8096 */
7980 8097 for (i = 0; i < dtrace_nprobes; i++) {
7981 8098 if ((probe = dtrace_probes[i]) == NULL)
7982 8099 continue;
7983 8100
7984 8101 if (probe->dtpr_provider != old)
7985 8102 continue;
7986 8103
7987 8104 if (probe->dtpr_ecb == NULL)
7988 8105 continue;
7989 8106
7990 8107 /*
7991 8108 * If we are trying to unregister a defunct provider, and the
7992 8109 * provider was made defunct within the interval dictated by
7993 8110 * dtrace_unregister_defunct_reap, we'll (asynchronously)
7994 8111 * attempt to reap our enablings. To denote that the provider
7995 8112 * should reattempt to unregister itself at some point in the
7996 8113 * future, we will return a differentiable error code (EAGAIN
7997 8114 * instead of EBUSY) in this case.
7998 8115 */
7999 8116 if (dtrace_gethrtime() - old->dtpv_defunct >
8000 8117 dtrace_unregister_defunct_reap)
8001 8118 noreap = 1;
8002 8119
8003 8120 if (!self) {
8004 8121 mutex_exit(&dtrace_lock);
8005 8122 mutex_exit(&mod_lock);
8006 8123 mutex_exit(&dtrace_provider_lock);
8007 8124 }
8008 8125
8009 8126 if (noreap)
8010 8127 return (EBUSY);
8011 8128
8012 8129 (void) taskq_dispatch(dtrace_taskq,
8013 8130 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP);
8014 8131
8015 8132 return (EAGAIN);
8016 8133 }
8017 8134
8018 8135 /*
8019 8136 * All of the probes for this provider are disabled; we can safely
8020 8137 * remove all of them from their hash chains and from the probe array.
8021 8138 */
8022 8139 for (i = 0; i < dtrace_nprobes; i++) {
8023 8140 if ((probe = dtrace_probes[i]) == NULL)
8024 8141 continue;
8025 8142
8026 8143 if (probe->dtpr_provider != old)
8027 8144 continue;
8028 8145
8029 8146 dtrace_probes[i] = NULL;
8030 8147
8031 8148 dtrace_hash_remove(dtrace_bymod, probe);
8032 8149 dtrace_hash_remove(dtrace_byfunc, probe);
8033 8150 dtrace_hash_remove(dtrace_byname, probe);
8034 8151
8035 8152 if (first == NULL) {
8036 8153 first = probe;
8037 8154 probe->dtpr_nextmod = NULL;
8038 8155 } else {
8039 8156 probe->dtpr_nextmod = first;
8040 8157 first = probe;
8041 8158 }
8042 8159 }
8043 8160
8044 8161 /*
8045 8162 * The provider's probes have been removed from the hash chains and
8046 8163 * from the probe array. Now issue a dtrace_sync() to be sure that
8047 8164 * everyone has cleared out from any probe array processing.
8048 8165 */
8049 8166 dtrace_sync();
8050 8167
8051 8168 for (probe = first; probe != NULL; probe = first) {
8052 8169 first = probe->dtpr_nextmod;
8053 8170
8054 8171 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
8055 8172 probe->dtpr_arg);
8056 8173 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
8057 8174 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
8058 8175 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
8059 8176 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
8060 8177 kmem_free(probe, sizeof (dtrace_probe_t));
8061 8178 }
8062 8179
8063 8180 if ((prev = dtrace_provider) == old) {
8064 8181 ASSERT(self || dtrace_devi == NULL);
8065 8182 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
8066 8183 dtrace_provider = old->dtpv_next;
8067 8184 } else {
8068 8185 while (prev != NULL && prev->dtpv_next != old)
8069 8186 prev = prev->dtpv_next;
8070 8187
8071 8188 if (prev == NULL) {
8072 8189 panic("attempt to unregister non-existent "
8073 8190 "dtrace provider %p\n", (void *)id);
8074 8191 }
8075 8192
8076 8193 prev->dtpv_next = old->dtpv_next;
8077 8194 }
8078 8195
8079 8196 if (!self) {
8080 8197 mutex_exit(&dtrace_lock);
8081 8198 mutex_exit(&mod_lock);
8082 8199 mutex_exit(&dtrace_provider_lock);
8083 8200 }
8084 8201
8085 8202 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
8086 8203 kmem_free(old, sizeof (dtrace_provider_t));
8087 8204
8088 8205 return (0);
8089 8206 }
8090 8207
8091 8208 /*
8092 8209 * Invalidate the specified provider. All subsequent probe lookups for the
8093 8210 * specified provider will fail, but its probes will not be removed.
8094 8211 */
8095 8212 void
8096 8213 dtrace_invalidate(dtrace_provider_id_t id)
8097 8214 {
8098 8215 dtrace_provider_t *pvp = (dtrace_provider_t *)id;
8099 8216
8100 8217 ASSERT(pvp->dtpv_pops.dtps_enable !=
8101 8218 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop);
8102 8219
8103 8220 mutex_enter(&dtrace_provider_lock);
8104 8221 mutex_enter(&dtrace_lock);
8105 8222
8106 8223 pvp->dtpv_defunct = dtrace_gethrtime();
8107 8224
8108 8225 mutex_exit(&dtrace_lock);
8109 8226 mutex_exit(&dtrace_provider_lock);
8110 8227 }
8111 8228
8112 8229 /*
8113 8230 * Indicate whether or not DTrace has attached.
8114 8231 */
8115 8232 int
8116 8233 dtrace_attached(void)
8117 8234 {
8118 8235 /*
8119 8236 * dtrace_provider will be non-NULL iff the DTrace driver has
8120 8237 * attached. (It's non-NULL because DTrace is always itself a
8121 8238 * provider.)
8122 8239 */
8123 8240 return (dtrace_provider != NULL);
8124 8241 }
8125 8242
8126 8243 /*
8127 8244 * Remove all the unenabled probes for the given provider. This function is
8128 8245 * not unlike dtrace_unregister(), except that it doesn't remove the provider
8129 8246 * -- just as many of its associated probes as it can.
8130 8247 */
8131 8248 int
8132 8249 dtrace_condense(dtrace_provider_id_t id)
8133 8250 {
8134 8251 dtrace_provider_t *prov = (dtrace_provider_t *)id;
8135 8252 int i;
8136 8253 dtrace_probe_t *probe;
8137 8254
8138 8255 /*
8139 8256 * Make sure this isn't the dtrace provider itself.
8140 8257 */
8141 8258 ASSERT(prov->dtpv_pops.dtps_enable !=
8142 8259 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop);
8143 8260
8144 8261 mutex_enter(&dtrace_provider_lock);
8145 8262 mutex_enter(&dtrace_lock);
8146 8263
8147 8264 /*
8148 8265 * Attempt to destroy the probes associated with this provider.
8149 8266 */
8150 8267 for (i = 0; i < dtrace_nprobes; i++) {
8151 8268 if ((probe = dtrace_probes[i]) == NULL)
8152 8269 continue;
8153 8270
8154 8271 if (probe->dtpr_provider != prov)
8155 8272 continue;
8156 8273
8157 8274 if (probe->dtpr_ecb != NULL)
8158 8275 continue;
8159 8276
8160 8277 dtrace_probes[i] = NULL;
8161 8278
8162 8279 dtrace_hash_remove(dtrace_bymod, probe);
8163 8280 dtrace_hash_remove(dtrace_byfunc, probe);
8164 8281 dtrace_hash_remove(dtrace_byname, probe);
8165 8282
8166 8283 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
8167 8284 probe->dtpr_arg);
8168 8285 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
8169 8286 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
8170 8287 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
8171 8288 kmem_free(probe, sizeof (dtrace_probe_t));
8172 8289 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
8173 8290 }
8174 8291
8175 8292 mutex_exit(&dtrace_lock);
8176 8293 mutex_exit(&dtrace_provider_lock);
8177 8294
8178 8295 return (0);
8179 8296 }
8180 8297
8181 8298 /*
8182 8299 * DTrace Probe Management Functions
8183 8300 *
8184 8301 * The functions in this section perform the DTrace probe management,
8185 8302 * including functions to create probes, look-up probes, and call into the
8186 8303 * providers to request that probes be provided. Some of these functions are
8187 8304 * in the Provider-to-Framework API; these functions can be identified by the
8188 8305 * fact that they are not declared "static".
8189 8306 */
8190 8307
8191 8308 /*
8192 8309 * Create a probe with the specified module name, function name, and name.
8193 8310 */
8194 8311 dtrace_id_t
8195 8312 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
8196 8313 const char *func, const char *name, int aframes, void *arg)
8197 8314 {
8198 8315 dtrace_probe_t *probe, **probes;
8199 8316 dtrace_provider_t *provider = (dtrace_provider_t *)prov;
8200 8317 dtrace_id_t id;
8201 8318
8202 8319 if (provider == dtrace_provider) {
8203 8320 ASSERT(MUTEX_HELD(&dtrace_lock));
8204 8321 } else {
8205 8322 mutex_enter(&dtrace_lock);
8206 8323 }
8207 8324
8208 8325 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
8209 8326 VM_BESTFIT | VM_SLEEP);
8210 8327 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
8211 8328
8212 8329 probe->dtpr_id = id;
8213 8330 probe->dtpr_gen = dtrace_probegen++;
8214 8331 probe->dtpr_mod = dtrace_strdup(mod);
8215 8332 probe->dtpr_func = dtrace_strdup(func);
8216 8333 probe->dtpr_name = dtrace_strdup(name);
8217 8334 probe->dtpr_arg = arg;
8218 8335 probe->dtpr_aframes = aframes;
8219 8336 probe->dtpr_provider = provider;
8220 8337
8221 8338 dtrace_hash_add(dtrace_bymod, probe);
8222 8339 dtrace_hash_add(dtrace_byfunc, probe);
8223 8340 dtrace_hash_add(dtrace_byname, probe);
8224 8341
8225 8342 if (id - 1 >= dtrace_nprobes) {
8226 8343 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
8227 8344 size_t nsize = osize << 1;
8228 8345
8229 8346 if (nsize == 0) {
8230 8347 ASSERT(osize == 0);
8231 8348 ASSERT(dtrace_probes == NULL);
8232 8349 nsize = sizeof (dtrace_probe_t *);
8233 8350 }
8234 8351
8235 8352 probes = kmem_zalloc(nsize, KM_SLEEP);
8236 8353
8237 8354 if (dtrace_probes == NULL) {
8238 8355 ASSERT(osize == 0);
8239 8356 dtrace_probes = probes;
8240 8357 dtrace_nprobes = 1;
8241 8358 } else {
8242 8359 dtrace_probe_t **oprobes = dtrace_probes;
8243 8360
8244 8361 bcopy(oprobes, probes, osize);
8245 8362 dtrace_membar_producer();
8246 8363 dtrace_probes = probes;
8247 8364
8248 8365 dtrace_sync();
8249 8366
8250 8367 /*
8251 8368 * All CPUs are now seeing the new probes array; we can
8252 8369 * safely free the old array.
8253 8370 */
8254 8371 kmem_free(oprobes, osize);
8255 8372 dtrace_nprobes <<= 1;
8256 8373 }
8257 8374
8258 8375 ASSERT(id - 1 < dtrace_nprobes);
8259 8376 }
8260 8377
8261 8378 ASSERT(dtrace_probes[id - 1] == NULL);
8262 8379 dtrace_probes[id - 1] = probe;
8263 8380
8264 8381 if (provider != dtrace_provider)
8265 8382 mutex_exit(&dtrace_lock);
8266 8383
8267 8384 return (id);
8268 8385 }
8269 8386
8270 8387 static dtrace_probe_t *
8271 8388 dtrace_probe_lookup_id(dtrace_id_t id)
8272 8389 {
8273 8390 ASSERT(MUTEX_HELD(&dtrace_lock));
8274 8391
8275 8392 if (id == 0 || id > dtrace_nprobes)
8276 8393 return (NULL);
8277 8394
8278 8395 return (dtrace_probes[id - 1]);
8279 8396 }
8280 8397
8281 8398 static int
8282 8399 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
8283 8400 {
8284 8401 *((dtrace_id_t *)arg) = probe->dtpr_id;
8285 8402
8286 8403 return (DTRACE_MATCH_DONE);
8287 8404 }
8288 8405
8289 8406 /*
8290 8407 * Look up a probe based on provider and one or more of module name, function
8291 8408 * name and probe name.
8292 8409 */
8293 8410 dtrace_id_t
8294 8411 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod,
8295 8412 const char *func, const char *name)
8296 8413 {
8297 8414 dtrace_probekey_t pkey;
8298 8415 dtrace_id_t id;
8299 8416 int match;
8300 8417
8301 8418 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
8302 8419 pkey.dtpk_pmatch = &dtrace_match_string;
8303 8420 pkey.dtpk_mod = mod;
8304 8421 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
8305 8422 pkey.dtpk_func = func;
8306 8423 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
8307 8424 pkey.dtpk_name = name;
8308 8425 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
8309 8426 pkey.dtpk_id = DTRACE_IDNONE;
8310 8427
8311 8428 mutex_enter(&dtrace_lock);
8312 8429 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
8313 8430 dtrace_probe_lookup_match, &id);
8314 8431 mutex_exit(&dtrace_lock);
8315 8432
8316 8433 ASSERT(match == 1 || match == 0);
8317 8434 return (match ? id : 0);
8318 8435 }
8319 8436
8320 8437 /*
8321 8438 * Returns the probe argument associated with the specified probe.
8322 8439 */
8323 8440 void *
8324 8441 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
8325 8442 {
8326 8443 dtrace_probe_t *probe;
8327 8444 void *rval = NULL;
8328 8445
8329 8446 mutex_enter(&dtrace_lock);
8330 8447
8331 8448 if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
8332 8449 probe->dtpr_provider == (dtrace_provider_t *)id)
8333 8450 rval = probe->dtpr_arg;
8334 8451
8335 8452 mutex_exit(&dtrace_lock);
8336 8453
8337 8454 return (rval);
8338 8455 }
8339 8456
8340 8457 /*
8341 8458 * Copy a probe into a probe description.
8342 8459 */
8343 8460 static void
8344 8461 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
8345 8462 {
8346 8463 bzero(pdp, sizeof (dtrace_probedesc_t));
8347 8464 pdp->dtpd_id = prp->dtpr_id;
8348 8465
8349 8466 (void) strncpy(pdp->dtpd_provider,
8350 8467 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
8351 8468
8352 8469 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
8353 8470 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
8354 8471 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
8355 8472 }
8356 8473
8357 8474 /*
8358 8475 * Called to indicate that a probe -- or probes -- should be provided by a
8359 8476 * specfied provider. If the specified description is NULL, the provider will
8360 8477 * be told to provide all of its probes. (This is done whenever a new
8361 8478 * consumer comes along, or whenever a retained enabling is to be matched.) If
8362 8479 * the specified description is non-NULL, the provider is given the
8363 8480 * opportunity to dynamically provide the specified probe, allowing providers
8364 8481 * to support the creation of probes on-the-fly. (So-called _autocreated_
8365 8482 * probes.) If the provider is NULL, the operations will be applied to all
8366 8483 * providers; if the provider is non-NULL the operations will only be applied
8367 8484 * to the specified provider. The dtrace_provider_lock must be held, and the
8368 8485 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
8369 8486 * will need to grab the dtrace_lock when it reenters the framework through
8370 8487 * dtrace_probe_lookup(), dtrace_probe_create(), etc.
8371 8488 */
8372 8489 static void
8373 8490 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
8374 8491 {
8375 8492 struct modctl *ctl;
8376 8493 int all = 0;
8377 8494
8378 8495 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8379 8496
8380 8497 if (prv == NULL) {
8381 8498 all = 1;
8382 8499 prv = dtrace_provider;
8383 8500 }
8384 8501
8385 8502 do {
8386 8503 /*
8387 8504 * First, call the blanket provide operation.
8388 8505 */
8389 8506 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
8390 8507
8391 8508 /*
8392 8509 * Now call the per-module provide operation. We will grab
8393 8510 * mod_lock to prevent the list from being modified. Note
8394 8511 * that this also prevents the mod_busy bits from changing.
8395 8512 * (mod_busy can only be changed with mod_lock held.)
8396 8513 */
8397 8514 mutex_enter(&mod_lock);
8398 8515
8399 8516 ctl = &modules;
8400 8517 do {
8401 8518 if (ctl->mod_busy || ctl->mod_mp == NULL)
8402 8519 continue;
8403 8520
8404 8521 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
8405 8522
8406 8523 } while ((ctl = ctl->mod_next) != &modules);
8407 8524
8408 8525 mutex_exit(&mod_lock);
8409 8526 } while (all && (prv = prv->dtpv_next) != NULL);
8410 8527 }
8411 8528
8412 8529 /*
8413 8530 * Iterate over each probe, and call the Framework-to-Provider API function
8414 8531 * denoted by offs.
8415 8532 */
8416 8533 static void
8417 8534 dtrace_probe_foreach(uintptr_t offs)
8418 8535 {
8419 8536 dtrace_provider_t *prov;
8420 8537 void (*func)(void *, dtrace_id_t, void *);
8421 8538 dtrace_probe_t *probe;
8422 8539 dtrace_icookie_t cookie;
8423 8540 int i;
8424 8541
8425 8542 /*
8426 8543 * We disable interrupts to walk through the probe array. This is
8427 8544 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
8428 8545 * won't see stale data.
8429 8546 */
8430 8547 cookie = dtrace_interrupt_disable();
8431 8548
8432 8549 for (i = 0; i < dtrace_nprobes; i++) {
8433 8550 if ((probe = dtrace_probes[i]) == NULL)
8434 8551 continue;
8435 8552
8436 8553 if (probe->dtpr_ecb == NULL) {
8437 8554 /*
8438 8555 * This probe isn't enabled -- don't call the function.
8439 8556 */
8440 8557 continue;
8441 8558 }
8442 8559
8443 8560 prov = probe->dtpr_provider;
8444 8561 func = *((void(**)(void *, dtrace_id_t, void *))
8445 8562 ((uintptr_t)&prov->dtpv_pops + offs));
8446 8563
8447 8564 func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
8448 8565 }
8449 8566
8450 8567 dtrace_interrupt_enable(cookie);
8451 8568 }
8452 8569
8453 8570 static int
8454 8571 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
8455 8572 {
8456 8573 dtrace_probekey_t pkey;
8457 8574 uint32_t priv;
8458 8575 uid_t uid;
8459 8576 zoneid_t zoneid;
8460 8577 dtrace_state_t *state = enab->dten_vstate->dtvs_state;
8461 8578
8462 8579 ASSERT(MUTEX_HELD(&dtrace_lock));
8463 8580 dtrace_ecb_create_cache = NULL;
8464 8581
8465 8582 if (desc == NULL) {
8466 8583 /*
8467 8584 * If we're passed a NULL description, we're being asked to
8468 8585 * create an ECB with a NULL probe.
8469 8586 */
8470 8587 (void) dtrace_ecb_create_enable(NULL, enab);
8471 8588 return (0);
8472 8589 }
8473 8590
8474 8591 dtrace_probekey(desc, &pkey);
8475 8592 dtrace_cred2priv(state->dts_cred.dcr_cred, &priv, &uid, &zoneid);
8476 8593
8477 8594 if ((priv & DTRACE_PRIV_ZONEOWNER) &&
8478 8595 state->dts_options[DTRACEOPT_ZONE] != DTRACEOPT_UNSET) {
8479 8596 /*
8480 8597 * If we have the privilege of instrumenting all zones but we
8481 8598 * have been told to instrument but one, we will spoof this up
8482 8599 * depriving ourselves of DTRACE_PRIV_ZONEOWNER for purposes
8483 8600 * of dtrace_match(). (Note that DTRACEOPT_ZONE is not for
8484 8601 * security but rather for performance: it allows the global
8485 8602 * zone to instrument USDT probes in a local zone without
8486 8603 * requiring all zones to be instrumented.)
8487 8604 */
8488 8605 priv &= ~DTRACE_PRIV_ZONEOWNER;
8489 8606 zoneid = state->dts_options[DTRACEOPT_ZONE];
8490 8607 }
8491 8608
8492 8609 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
8493 8610 enab));
8494 8611 }
8495 8612
8496 8613 /*
8497 8614 * DTrace Helper Provider Functions
8498 8615 */
8499 8616 static void
8500 8617 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
8501 8618 {
8502 8619 attr->dtat_name = DOF_ATTR_NAME(dofattr);
8503 8620 attr->dtat_data = DOF_ATTR_DATA(dofattr);
8504 8621 attr->dtat_class = DOF_ATTR_CLASS(dofattr);
8505 8622 }
8506 8623
8507 8624 static void
8508 8625 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
8509 8626 const dof_provider_t *dofprov, char *strtab)
8510 8627 {
8511 8628 hprov->dthpv_provname = strtab + dofprov->dofpv_name;
8512 8629 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
8513 8630 dofprov->dofpv_provattr);
8514 8631 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
8515 8632 dofprov->dofpv_modattr);
8516 8633 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
8517 8634 dofprov->dofpv_funcattr);
8518 8635 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
8519 8636 dofprov->dofpv_nameattr);
8520 8637 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
8521 8638 dofprov->dofpv_argsattr);
8522 8639 }
8523 8640
8524 8641 static void
8525 8642 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8526 8643 {
8527 8644 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8528 8645 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8529 8646 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
8530 8647 dof_provider_t *provider;
8531 8648 dof_probe_t *probe;
8532 8649 uint32_t *off, *enoff;
8533 8650 uint8_t *arg;
8534 8651 char *strtab;
8535 8652 uint_t i, nprobes;
8536 8653 dtrace_helper_provdesc_t dhpv;
8537 8654 dtrace_helper_probedesc_t dhpb;
8538 8655 dtrace_meta_t *meta = dtrace_meta_pid;
8539 8656 dtrace_mops_t *mops = &meta->dtm_mops;
8540 8657 void *parg;
8541 8658
8542 8659 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8543 8660 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8544 8661 provider->dofpv_strtab * dof->dofh_secsize);
8545 8662 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8546 8663 provider->dofpv_probes * dof->dofh_secsize);
8547 8664 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8548 8665 provider->dofpv_prargs * dof->dofh_secsize);
8549 8666 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8550 8667 provider->dofpv_proffs * dof->dofh_secsize);
8551 8668
8552 8669 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8553 8670 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
8554 8671 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
8555 8672 enoff = NULL;
8556 8673
8557 8674 /*
8558 8675 * See dtrace_helper_provider_validate().
8559 8676 */
8560 8677 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
8561 8678 provider->dofpv_prenoffs != DOF_SECT_NONE) {
8562 8679 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8563 8680 provider->dofpv_prenoffs * dof->dofh_secsize);
8564 8681 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
8565 8682 }
8566 8683
8567 8684 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
8568 8685
8569 8686 /*
8570 8687 * Create the provider.
8571 8688 */
8572 8689 dtrace_dofprov2hprov(&dhpv, provider, strtab);
8573 8690
8574 8691 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
8575 8692 return;
8576 8693
8577 8694 meta->dtm_count++;
8578 8695
8579 8696 /*
8580 8697 * Create the probes.
8581 8698 */
8582 8699 for (i = 0; i < nprobes; i++) {
8583 8700 probe = (dof_probe_t *)(uintptr_t)(daddr +
8584 8701 prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
8585 8702
8586 8703 dhpb.dthpb_mod = dhp->dofhp_mod;
8587 8704 dhpb.dthpb_func = strtab + probe->dofpr_func;
8588 8705 dhpb.dthpb_name = strtab + probe->dofpr_name;
8589 8706 dhpb.dthpb_base = probe->dofpr_addr;
8590 8707 dhpb.dthpb_offs = off + probe->dofpr_offidx;
8591 8708 dhpb.dthpb_noffs = probe->dofpr_noffs;
8592 8709 if (enoff != NULL) {
8593 8710 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
8594 8711 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
8595 8712 } else {
8596 8713 dhpb.dthpb_enoffs = NULL;
8597 8714 dhpb.dthpb_nenoffs = 0;
8598 8715 }
8599 8716 dhpb.dthpb_args = arg + probe->dofpr_argidx;
8600 8717 dhpb.dthpb_nargc = probe->dofpr_nargc;
8601 8718 dhpb.dthpb_xargc = probe->dofpr_xargc;
8602 8719 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
8603 8720 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
8604 8721
8605 8722 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
8606 8723 }
8607 8724 }
8608 8725
8609 8726 static void
8610 8727 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
8611 8728 {
8612 8729 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8613 8730 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8614 8731 int i;
8615 8732
8616 8733 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8617 8734
8618 8735 for (i = 0; i < dof->dofh_secnum; i++) {
8619 8736 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8620 8737 dof->dofh_secoff + i * dof->dofh_secsize);
8621 8738
8622 8739 if (sec->dofs_type != DOF_SECT_PROVIDER)
8623 8740 continue;
8624 8741
8625 8742 dtrace_helper_provide_one(dhp, sec, pid);
8626 8743 }
8627 8744
8628 8745 /*
8629 8746 * We may have just created probes, so we must now rematch against
8630 8747 * any retained enablings. Note that this call will acquire both
8631 8748 * cpu_lock and dtrace_lock; the fact that we are holding
8632 8749 * dtrace_meta_lock now is what defines the ordering with respect to
8633 8750 * these three locks.
8634 8751 */
8635 8752 dtrace_enabling_matchall();
8636 8753 }
8637 8754
8638 8755 static void
8639 8756 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
8640 8757 {
8641 8758 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8642 8759 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8643 8760 dof_sec_t *str_sec;
8644 8761 dof_provider_t *provider;
8645 8762 char *strtab;
8646 8763 dtrace_helper_provdesc_t dhpv;
8647 8764 dtrace_meta_t *meta = dtrace_meta_pid;
8648 8765 dtrace_mops_t *mops = &meta->dtm_mops;
8649 8766
8650 8767 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
8651 8768 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
8652 8769 provider->dofpv_strtab * dof->dofh_secsize);
8653 8770
8654 8771 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
8655 8772
8656 8773 /*
8657 8774 * Create the provider.
8658 8775 */
8659 8776 dtrace_dofprov2hprov(&dhpv, provider, strtab);
8660 8777
8661 8778 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
8662 8779
8663 8780 meta->dtm_count--;
8664 8781 }
8665 8782
8666 8783 static void
8667 8784 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
8668 8785 {
8669 8786 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
8670 8787 dof_hdr_t *dof = (dof_hdr_t *)daddr;
8671 8788 int i;
8672 8789
8673 8790 ASSERT(MUTEX_HELD(&dtrace_meta_lock));
8674 8791
8675 8792 for (i = 0; i < dof->dofh_secnum; i++) {
8676 8793 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
8677 8794 dof->dofh_secoff + i * dof->dofh_secsize);
8678 8795
8679 8796 if (sec->dofs_type != DOF_SECT_PROVIDER)
8680 8797 continue;
8681 8798
8682 8799 dtrace_helper_provider_remove_one(dhp, sec, pid);
8683 8800 }
8684 8801 }
8685 8802
8686 8803 /*
8687 8804 * DTrace Meta Provider-to-Framework API Functions
8688 8805 *
8689 8806 * These functions implement the Meta Provider-to-Framework API, as described
8690 8807 * in <sys/dtrace.h>.
8691 8808 */
8692 8809 int
8693 8810 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
8694 8811 dtrace_meta_provider_id_t *idp)
8695 8812 {
8696 8813 dtrace_meta_t *meta;
8697 8814 dtrace_helpers_t *help, *next;
8698 8815 int i;
8699 8816
8700 8817 *idp = DTRACE_METAPROVNONE;
8701 8818
8702 8819 /*
8703 8820 * We strictly don't need the name, but we hold onto it for
8704 8821 * debuggability. All hail error queues!
8705 8822 */
8706 8823 if (name == NULL) {
8707 8824 cmn_err(CE_WARN, "failed to register meta-provider: "
8708 8825 "invalid name");
8709 8826 return (EINVAL);
8710 8827 }
8711 8828
8712 8829 if (mops == NULL ||
8713 8830 mops->dtms_create_probe == NULL ||
8714 8831 mops->dtms_provide_pid == NULL ||
8715 8832 mops->dtms_remove_pid == NULL) {
8716 8833 cmn_err(CE_WARN, "failed to register meta-register %s: "
8717 8834 "invalid ops", name);
8718 8835 return (EINVAL);
8719 8836 }
8720 8837
8721 8838 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
8722 8839 meta->dtm_mops = *mops;
8723 8840 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8724 8841 (void) strcpy(meta->dtm_name, name);
8725 8842 meta->dtm_arg = arg;
8726 8843
8727 8844 mutex_enter(&dtrace_meta_lock);
8728 8845 mutex_enter(&dtrace_lock);
8729 8846
8730 8847 if (dtrace_meta_pid != NULL) {
8731 8848 mutex_exit(&dtrace_lock);
8732 8849 mutex_exit(&dtrace_meta_lock);
8733 8850 cmn_err(CE_WARN, "failed to register meta-register %s: "
8734 8851 "user-land meta-provider exists", name);
8735 8852 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
8736 8853 kmem_free(meta, sizeof (dtrace_meta_t));
8737 8854 return (EINVAL);
8738 8855 }
8739 8856
8740 8857 dtrace_meta_pid = meta;
8741 8858 *idp = (dtrace_meta_provider_id_t)meta;
8742 8859
8743 8860 /*
8744 8861 * If there are providers and probes ready to go, pass them
8745 8862 * off to the new meta provider now.
8746 8863 */
8747 8864
8748 8865 help = dtrace_deferred_pid;
8749 8866 dtrace_deferred_pid = NULL;
8750 8867
8751 8868 mutex_exit(&dtrace_lock);
8752 8869
8753 8870 while (help != NULL) {
8754 8871 for (i = 0; i < help->dthps_nprovs; i++) {
8755 8872 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
8756 8873 help->dthps_pid);
8757 8874 }
8758 8875
8759 8876 next = help->dthps_next;
8760 8877 help->dthps_next = NULL;
8761 8878 help->dthps_prev = NULL;
8762 8879 help->dthps_deferred = 0;
8763 8880 help = next;
8764 8881 }
8765 8882
8766 8883 mutex_exit(&dtrace_meta_lock);
8767 8884
8768 8885 return (0);
8769 8886 }
8770 8887
8771 8888 int
8772 8889 dtrace_meta_unregister(dtrace_meta_provider_id_t id)
8773 8890 {
8774 8891 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
8775 8892
8776 8893 mutex_enter(&dtrace_meta_lock);
8777 8894 mutex_enter(&dtrace_lock);
8778 8895
8779 8896 if (old == dtrace_meta_pid) {
8780 8897 pp = &dtrace_meta_pid;
8781 8898 } else {
8782 8899 panic("attempt to unregister non-existent "
8783 8900 "dtrace meta-provider %p\n", (void *)old);
8784 8901 }
8785 8902
8786 8903 if (old->dtm_count != 0) {
8787 8904 mutex_exit(&dtrace_lock);
8788 8905 mutex_exit(&dtrace_meta_lock);
8789 8906 return (EBUSY);
8790 8907 }
8791 8908
8792 8909 *pp = NULL;
8793 8910
8794 8911 mutex_exit(&dtrace_lock);
8795 8912 mutex_exit(&dtrace_meta_lock);
8796 8913
8797 8914 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
8798 8915 kmem_free(old, sizeof (dtrace_meta_t));
8799 8916
8800 8917 return (0);
8801 8918 }
8802 8919
8803 8920
8804 8921 /*
8805 8922 * DTrace DIF Object Functions
8806 8923 */
8807 8924 static int
8808 8925 dtrace_difo_err(uint_t pc, const char *format, ...)
8809 8926 {
8810 8927 if (dtrace_err_verbose) {
8811 8928 va_list alist;
8812 8929
8813 8930 (void) uprintf("dtrace DIF object error: [%u]: ", pc);
8814 8931 va_start(alist, format);
8815 8932 (void) vuprintf(format, alist);
8816 8933 va_end(alist);
8817 8934 }
8818 8935
8819 8936 #ifdef DTRACE_ERRDEBUG
8820 8937 dtrace_errdebug(format);
8821 8938 #endif
8822 8939 return (1);
8823 8940 }
8824 8941
8825 8942 /*
8826 8943 * Validate a DTrace DIF object by checking the IR instructions. The following
8827 8944 * rules are currently enforced by dtrace_difo_validate():
8828 8945 *
8829 8946 * 1. Each instruction must have a valid opcode
8830 8947 * 2. Each register, string, variable, or subroutine reference must be valid
8831 8948 * 3. No instruction can modify register %r0 (must be zero)
8832 8949 * 4. All instruction reserved bits must be set to zero
8833 8950 * 5. The last instruction must be a "ret" instruction
8834 8951 * 6. All branch targets must reference a valid instruction _after_ the branch
8835 8952 */
8836 8953 static int
8837 8954 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
8838 8955 cred_t *cr)
8839 8956 {
8840 8957 int err = 0, i;
8841 8958 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
8842 8959 int kcheckload;
8843 8960 uint_t pc;
8844 8961
8845 8962 kcheckload = cr == NULL ||
8846 8963 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
8847 8964
8848 8965 dp->dtdo_destructive = 0;
8849 8966
8850 8967 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
8851 8968 dif_instr_t instr = dp->dtdo_buf[pc];
8852 8969
8853 8970 uint_t r1 = DIF_INSTR_R1(instr);
8854 8971 uint_t r2 = DIF_INSTR_R2(instr);
8855 8972 uint_t rd = DIF_INSTR_RD(instr);
8856 8973 uint_t rs = DIF_INSTR_RS(instr);
8857 8974 uint_t label = DIF_INSTR_LABEL(instr);
8858 8975 uint_t v = DIF_INSTR_VAR(instr);
8859 8976 uint_t subr = DIF_INSTR_SUBR(instr);
8860 8977 uint_t type = DIF_INSTR_TYPE(instr);
8861 8978 uint_t op = DIF_INSTR_OP(instr);
8862 8979
8863 8980 switch (op) {
8864 8981 case DIF_OP_OR:
8865 8982 case DIF_OP_XOR:
8866 8983 case DIF_OP_AND:
8867 8984 case DIF_OP_SLL:
8868 8985 case DIF_OP_SRL:
8869 8986 case DIF_OP_SRA:
8870 8987 case DIF_OP_SUB:
8871 8988 case DIF_OP_ADD:
8872 8989 case DIF_OP_MUL:
8873 8990 case DIF_OP_SDIV:
8874 8991 case DIF_OP_UDIV:
8875 8992 case DIF_OP_SREM:
8876 8993 case DIF_OP_UREM:
8877 8994 case DIF_OP_COPYS:
8878 8995 if (r1 >= nregs)
8879 8996 err += efunc(pc, "invalid register %u\n", r1);
8880 8997 if (r2 >= nregs)
8881 8998 err += efunc(pc, "invalid register %u\n", r2);
8882 8999 if (rd >= nregs)
8883 9000 err += efunc(pc, "invalid register %u\n", rd);
8884 9001 if (rd == 0)
8885 9002 err += efunc(pc, "cannot write to %r0\n");
8886 9003 break;
8887 9004 case DIF_OP_NOT:
8888 9005 case DIF_OP_MOV:
8889 9006 case DIF_OP_ALLOCS:
8890 9007 if (r1 >= nregs)
8891 9008 err += efunc(pc, "invalid register %u\n", r1);
8892 9009 if (r2 != 0)
8893 9010 err += efunc(pc, "non-zero reserved bits\n");
8894 9011 if (rd >= nregs)
8895 9012 err += efunc(pc, "invalid register %u\n", rd);
8896 9013 if (rd == 0)
8897 9014 err += efunc(pc, "cannot write to %r0\n");
8898 9015 break;
8899 9016 case DIF_OP_LDSB:
8900 9017 case DIF_OP_LDSH:
8901 9018 case DIF_OP_LDSW:
8902 9019 case DIF_OP_LDUB:
8903 9020 case DIF_OP_LDUH:
8904 9021 case DIF_OP_LDUW:
8905 9022 case DIF_OP_LDX:
8906 9023 if (r1 >= nregs)
8907 9024 err += efunc(pc, "invalid register %u\n", r1);
8908 9025 if (r2 != 0)
8909 9026 err += efunc(pc, "non-zero reserved bits\n");
8910 9027 if (rd >= nregs)
8911 9028 err += efunc(pc, "invalid register %u\n", rd);
8912 9029 if (rd == 0)
8913 9030 err += efunc(pc, "cannot write to %r0\n");
8914 9031 if (kcheckload)
8915 9032 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
8916 9033 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
8917 9034 break;
8918 9035 case DIF_OP_RLDSB:
8919 9036 case DIF_OP_RLDSH:
8920 9037 case DIF_OP_RLDSW:
8921 9038 case DIF_OP_RLDUB:
8922 9039 case DIF_OP_RLDUH:
8923 9040 case DIF_OP_RLDUW:
8924 9041 case DIF_OP_RLDX:
8925 9042 if (r1 >= nregs)
8926 9043 err += efunc(pc, "invalid register %u\n", r1);
8927 9044 if (r2 != 0)
8928 9045 err += efunc(pc, "non-zero reserved bits\n");
8929 9046 if (rd >= nregs)
8930 9047 err += efunc(pc, "invalid register %u\n", rd);
8931 9048 if (rd == 0)
8932 9049 err += efunc(pc, "cannot write to %r0\n");
8933 9050 break;
8934 9051 case DIF_OP_ULDSB:
8935 9052 case DIF_OP_ULDSH:
8936 9053 case DIF_OP_ULDSW:
8937 9054 case DIF_OP_ULDUB:
8938 9055 case DIF_OP_ULDUH:
8939 9056 case DIF_OP_ULDUW:
8940 9057 case DIF_OP_ULDX:
8941 9058 if (r1 >= nregs)
8942 9059 err += efunc(pc, "invalid register %u\n", r1);
8943 9060 if (r2 != 0)
8944 9061 err += efunc(pc, "non-zero reserved bits\n");
8945 9062 if (rd >= nregs)
8946 9063 err += efunc(pc, "invalid register %u\n", rd);
8947 9064 if (rd == 0)
8948 9065 err += efunc(pc, "cannot write to %r0\n");
8949 9066 break;
8950 9067 case DIF_OP_STB:
8951 9068 case DIF_OP_STH:
8952 9069 case DIF_OP_STW:
8953 9070 case DIF_OP_STX:
8954 9071 if (r1 >= nregs)
8955 9072 err += efunc(pc, "invalid register %u\n", r1);
8956 9073 if (r2 != 0)
8957 9074 err += efunc(pc, "non-zero reserved bits\n");
8958 9075 if (rd >= nregs)
8959 9076 err += efunc(pc, "invalid register %u\n", rd);
8960 9077 if (rd == 0)
8961 9078 err += efunc(pc, "cannot write to 0 address\n");
8962 9079 break;
8963 9080 case DIF_OP_CMP:
8964 9081 case DIF_OP_SCMP:
8965 9082 if (r1 >= nregs)
8966 9083 err += efunc(pc, "invalid register %u\n", r1);
8967 9084 if (r2 >= nregs)
8968 9085 err += efunc(pc, "invalid register %u\n", r2);
8969 9086 if (rd != 0)
8970 9087 err += efunc(pc, "non-zero reserved bits\n");
8971 9088 break;
8972 9089 case DIF_OP_TST:
8973 9090 if (r1 >= nregs)
8974 9091 err += efunc(pc, "invalid register %u\n", r1);
8975 9092 if (r2 != 0 || rd != 0)
8976 9093 err += efunc(pc, "non-zero reserved bits\n");
8977 9094 break;
8978 9095 case DIF_OP_BA:
8979 9096 case DIF_OP_BE:
8980 9097 case DIF_OP_BNE:
8981 9098 case DIF_OP_BG:
8982 9099 case DIF_OP_BGU:
8983 9100 case DIF_OP_BGE:
8984 9101 case DIF_OP_BGEU:
8985 9102 case DIF_OP_BL:
8986 9103 case DIF_OP_BLU:
8987 9104 case DIF_OP_BLE:
8988 9105 case DIF_OP_BLEU:
8989 9106 if (label >= dp->dtdo_len) {
8990 9107 err += efunc(pc, "invalid branch target %u\n",
8991 9108 label);
8992 9109 }
8993 9110 if (label <= pc) {
8994 9111 err += efunc(pc, "backward branch to %u\n",
8995 9112 label);
8996 9113 }
8997 9114 break;
8998 9115 case DIF_OP_RET:
8999 9116 if (r1 != 0 || r2 != 0)
9000 9117 err += efunc(pc, "non-zero reserved bits\n");
9001 9118 if (rd >= nregs)
9002 9119 err += efunc(pc, "invalid register %u\n", rd);
9003 9120 break;
9004 9121 case DIF_OP_NOP:
9005 9122 case DIF_OP_POPTS:
9006 9123 case DIF_OP_FLUSHTS:
9007 9124 if (r1 != 0 || r2 != 0 || rd != 0)
9008 9125 err += efunc(pc, "non-zero reserved bits\n");
9009 9126 break;
9010 9127 case DIF_OP_SETX:
9011 9128 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
9012 9129 err += efunc(pc, "invalid integer ref %u\n",
9013 9130 DIF_INSTR_INTEGER(instr));
9014 9131 }
9015 9132 if (rd >= nregs)
9016 9133 err += efunc(pc, "invalid register %u\n", rd);
9017 9134 if (rd == 0)
9018 9135 err += efunc(pc, "cannot write to %r0\n");
9019 9136 break;
9020 9137 case DIF_OP_SETS:
9021 9138 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
9022 9139 err += efunc(pc, "invalid string ref %u\n",
9023 9140 DIF_INSTR_STRING(instr));
9024 9141 }
9025 9142 if (rd >= nregs)
9026 9143 err += efunc(pc, "invalid register %u\n", rd);
9027 9144 if (rd == 0)
9028 9145 err += efunc(pc, "cannot write to %r0\n");
9029 9146 break;
9030 9147 case DIF_OP_LDGA:
9031 9148 case DIF_OP_LDTA:
9032 9149 if (r1 > DIF_VAR_ARRAY_MAX)
9033 9150 err += efunc(pc, "invalid array %u\n", r1);
9034 9151 if (r2 >= nregs)
9035 9152 err += efunc(pc, "invalid register %u\n", r2);
9036 9153 if (rd >= nregs)
9037 9154 err += efunc(pc, "invalid register %u\n", rd);
9038 9155 if (rd == 0)
9039 9156 err += efunc(pc, "cannot write to %r0\n");
9040 9157 break;
9041 9158 case DIF_OP_LDGS:
9042 9159 case DIF_OP_LDTS:
9043 9160 case DIF_OP_LDLS:
9044 9161 case DIF_OP_LDGAA:
9045 9162 case DIF_OP_LDTAA:
9046 9163 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
9047 9164 err += efunc(pc, "invalid variable %u\n", v);
9048 9165 if (rd >= nregs)
9049 9166 err += efunc(pc, "invalid register %u\n", rd);
9050 9167 if (rd == 0)
9051 9168 err += efunc(pc, "cannot write to %r0\n");
9052 9169 break;
9053 9170 case DIF_OP_STGS:
9054 9171 case DIF_OP_STTS:
9055 9172 case DIF_OP_STLS:
9056 9173 case DIF_OP_STGAA:
9057 9174 case DIF_OP_STTAA:
9058 9175 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
9059 9176 err += efunc(pc, "invalid variable %u\n", v);
9060 9177 if (rs >= nregs)
9061 9178 err += efunc(pc, "invalid register %u\n", rd);
9062 9179 break;
9063 9180 case DIF_OP_CALL:
9064 9181 if (subr > DIF_SUBR_MAX)
9065 9182 err += efunc(pc, "invalid subr %u\n", subr);
9066 9183 if (rd >= nregs)
9067 9184 err += efunc(pc, "invalid register %u\n", rd);
9068 9185 if (rd == 0)
9069 9186 err += efunc(pc, "cannot write to %r0\n");
9070 9187
9071 9188 if (subr == DIF_SUBR_COPYOUT ||
9072 9189 subr == DIF_SUBR_COPYOUTSTR) {
9073 9190 dp->dtdo_destructive = 1;
9074 9191 }
9075 9192
9076 9193 if (subr == DIF_SUBR_GETF) {
9077 9194 /*
9078 9195 * If we have a getf() we need to record that
9079 9196 * in our state. Note that our state can be
9080 9197 * NULL if this is a helper -- but in that
9081 9198 * case, the call to getf() is itself illegal,
9082 9199 * and will be caught (slightly later) when
9083 9200 * the helper is validated.
9084 9201 */
9085 9202 if (vstate->dtvs_state != NULL)
9086 9203 vstate->dtvs_state->dts_getf++;
9087 9204 }
9088 9205
9089 9206 break;
9090 9207 case DIF_OP_PUSHTR:
9091 9208 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
9092 9209 err += efunc(pc, "invalid ref type %u\n", type);
9093 9210 if (r2 >= nregs)
9094 9211 err += efunc(pc, "invalid register %u\n", r2);
9095 9212 if (rs >= nregs)
9096 9213 err += efunc(pc, "invalid register %u\n", rs);
9097 9214 break;
9098 9215 case DIF_OP_PUSHTV:
9099 9216 if (type != DIF_TYPE_CTF)
9100 9217 err += efunc(pc, "invalid val type %u\n", type);
9101 9218 if (r2 >= nregs)
9102 9219 err += efunc(pc, "invalid register %u\n", r2);
9103 9220 if (rs >= nregs)
9104 9221 err += efunc(pc, "invalid register %u\n", rs);
9105 9222 break;
9106 9223 default:
9107 9224 err += efunc(pc, "invalid opcode %u\n",
9108 9225 DIF_INSTR_OP(instr));
9109 9226 }
9110 9227 }
9111 9228
9112 9229 if (dp->dtdo_len != 0 &&
9113 9230 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
9114 9231 err += efunc(dp->dtdo_len - 1,
9115 9232 "expected 'ret' as last DIF instruction\n");
9116 9233 }
9117 9234
9118 9235 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) {
9119 9236 /*
9120 9237 * If we're not returning by reference, the size must be either
9121 9238 * 0 or the size of one of the base types.
9122 9239 */
9123 9240 switch (dp->dtdo_rtype.dtdt_size) {
9124 9241 case 0:
9125 9242 case sizeof (uint8_t):
9126 9243 case sizeof (uint16_t):
9127 9244 case sizeof (uint32_t):
9128 9245 case sizeof (uint64_t):
9129 9246 break;
9130 9247
9131 9248 default:
9132 9249 err += efunc(dp->dtdo_len - 1, "bad return size\n");
9133 9250 }
9134 9251 }
9135 9252
9136 9253 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
9137 9254 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
9138 9255 dtrace_diftype_t *vt, *et;
9139 9256 uint_t id, ndx;
9140 9257
9141 9258 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
9142 9259 v->dtdv_scope != DIFV_SCOPE_THREAD &&
9143 9260 v->dtdv_scope != DIFV_SCOPE_LOCAL) {
9144 9261 err += efunc(i, "unrecognized variable scope %d\n",
9145 9262 v->dtdv_scope);
9146 9263 break;
9147 9264 }
9148 9265
9149 9266 if (v->dtdv_kind != DIFV_KIND_ARRAY &&
9150 9267 v->dtdv_kind != DIFV_KIND_SCALAR) {
9151 9268 err += efunc(i, "unrecognized variable type %d\n",
9152 9269 v->dtdv_kind);
9153 9270 break;
9154 9271 }
9155 9272
9156 9273 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
9157 9274 err += efunc(i, "%d exceeds variable id limit\n", id);
9158 9275 break;
9159 9276 }
9160 9277
9161 9278 if (id < DIF_VAR_OTHER_UBASE)
9162 9279 continue;
9163 9280
9164 9281 /*
9165 9282 * For user-defined variables, we need to check that this
9166 9283 * definition is identical to any previous definition that we
9167 9284 * encountered.
9168 9285 */
9169 9286 ndx = id - DIF_VAR_OTHER_UBASE;
9170 9287
9171 9288 switch (v->dtdv_scope) {
9172 9289 case DIFV_SCOPE_GLOBAL:
9173 9290 if (ndx < vstate->dtvs_nglobals) {
9174 9291 dtrace_statvar_t *svar;
9175 9292
9176 9293 if ((svar = vstate->dtvs_globals[ndx]) != NULL)
9177 9294 existing = &svar->dtsv_var;
9178 9295 }
9179 9296
9180 9297 break;
9181 9298
9182 9299 case DIFV_SCOPE_THREAD:
9183 9300 if (ndx < vstate->dtvs_ntlocals)
9184 9301 existing = &vstate->dtvs_tlocals[ndx];
9185 9302 break;
9186 9303
9187 9304 case DIFV_SCOPE_LOCAL:
9188 9305 if (ndx < vstate->dtvs_nlocals) {
9189 9306 dtrace_statvar_t *svar;
9190 9307
9191 9308 if ((svar = vstate->dtvs_locals[ndx]) != NULL)
9192 9309 existing = &svar->dtsv_var;
9193 9310 }
9194 9311
9195 9312 break;
9196 9313 }
9197 9314
9198 9315 vt = &v->dtdv_type;
9199 9316
9200 9317 if (vt->dtdt_flags & DIF_TF_BYREF) {
9201 9318 if (vt->dtdt_size == 0) {
9202 9319 err += efunc(i, "zero-sized variable\n");
9203 9320 break;
9204 9321 }
9205 9322
9206 9323 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL &&
9207 9324 vt->dtdt_size > dtrace_global_maxsize) {
9208 9325 err += efunc(i, "oversized by-ref global\n");
9209 9326 break;
9210 9327 }
9211 9328 }
9212 9329
9213 9330 if (existing == NULL || existing->dtdv_id == 0)
9214 9331 continue;
9215 9332
9216 9333 ASSERT(existing->dtdv_id == v->dtdv_id);
9217 9334 ASSERT(existing->dtdv_scope == v->dtdv_scope);
9218 9335
9219 9336 if (existing->dtdv_kind != v->dtdv_kind)
9220 9337 err += efunc(i, "%d changed variable kind\n", id);
9221 9338
9222 9339 et = &existing->dtdv_type;
9223 9340
9224 9341 if (vt->dtdt_flags != et->dtdt_flags) {
9225 9342 err += efunc(i, "%d changed variable type flags\n", id);
9226 9343 break;
9227 9344 }
9228 9345
9229 9346 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
9230 9347 err += efunc(i, "%d changed variable type size\n", id);
9231 9348 break;
9232 9349 }
9233 9350 }
9234 9351
9235 9352 return (err);
9236 9353 }
9237 9354
9238 9355 /*
9239 9356 * Validate a DTrace DIF object that it is to be used as a helper. Helpers
9240 9357 * are much more constrained than normal DIFOs. Specifically, they may
9241 9358 * not:
9242 9359 *
9243 9360 * 1. Make calls to subroutines other than copyin(), copyinstr() or
9244 9361 * miscellaneous string routines
9245 9362 * 2. Access DTrace variables other than the args[] array, and the
9246 9363 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
9247 9364 * 3. Have thread-local variables.
9248 9365 * 4. Have dynamic variables.
9249 9366 */
9250 9367 static int
9251 9368 dtrace_difo_validate_helper(dtrace_difo_t *dp)
9252 9369 {
9253 9370 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
9254 9371 int err = 0;
9255 9372 uint_t pc;
9256 9373
9257 9374 for (pc = 0; pc < dp->dtdo_len; pc++) {
9258 9375 dif_instr_t instr = dp->dtdo_buf[pc];
9259 9376
9260 9377 uint_t v = DIF_INSTR_VAR(instr);
9261 9378 uint_t subr = DIF_INSTR_SUBR(instr);
9262 9379 uint_t op = DIF_INSTR_OP(instr);
9263 9380
9264 9381 switch (op) {
9265 9382 case DIF_OP_OR:
9266 9383 case DIF_OP_XOR:
9267 9384 case DIF_OP_AND:
9268 9385 case DIF_OP_SLL:
9269 9386 case DIF_OP_SRL:
9270 9387 case DIF_OP_SRA:
9271 9388 case DIF_OP_SUB:
9272 9389 case DIF_OP_ADD:
9273 9390 case DIF_OP_MUL:
9274 9391 case DIF_OP_SDIV:
9275 9392 case DIF_OP_UDIV:
9276 9393 case DIF_OP_SREM:
9277 9394 case DIF_OP_UREM:
9278 9395 case DIF_OP_COPYS:
9279 9396 case DIF_OP_NOT:
9280 9397 case DIF_OP_MOV:
9281 9398 case DIF_OP_RLDSB:
9282 9399 case DIF_OP_RLDSH:
9283 9400 case DIF_OP_RLDSW:
9284 9401 case DIF_OP_RLDUB:
9285 9402 case DIF_OP_RLDUH:
9286 9403 case DIF_OP_RLDUW:
9287 9404 case DIF_OP_RLDX:
9288 9405 case DIF_OP_ULDSB:
9289 9406 case DIF_OP_ULDSH:
9290 9407 case DIF_OP_ULDSW:
9291 9408 case DIF_OP_ULDUB:
9292 9409 case DIF_OP_ULDUH:
9293 9410 case DIF_OP_ULDUW:
9294 9411 case DIF_OP_ULDX:
9295 9412 case DIF_OP_STB:
9296 9413 case DIF_OP_STH:
9297 9414 case DIF_OP_STW:
9298 9415 case DIF_OP_STX:
9299 9416 case DIF_OP_ALLOCS:
9300 9417 case DIF_OP_CMP:
9301 9418 case DIF_OP_SCMP:
9302 9419 case DIF_OP_TST:
9303 9420 case DIF_OP_BA:
9304 9421 case DIF_OP_BE:
9305 9422 case DIF_OP_BNE:
9306 9423 case DIF_OP_BG:
9307 9424 case DIF_OP_BGU:
9308 9425 case DIF_OP_BGE:
9309 9426 case DIF_OP_BGEU:
9310 9427 case DIF_OP_BL:
9311 9428 case DIF_OP_BLU:
9312 9429 case DIF_OP_BLE:
9313 9430 case DIF_OP_BLEU:
9314 9431 case DIF_OP_RET:
9315 9432 case DIF_OP_NOP:
9316 9433 case DIF_OP_POPTS:
9317 9434 case DIF_OP_FLUSHTS:
9318 9435 case DIF_OP_SETX:
9319 9436 case DIF_OP_SETS:
9320 9437 case DIF_OP_LDGA:
9321 9438 case DIF_OP_LDLS:
9322 9439 case DIF_OP_STGS:
9323 9440 case DIF_OP_STLS:
9324 9441 case DIF_OP_PUSHTR:
9325 9442 case DIF_OP_PUSHTV:
9326 9443 break;
9327 9444
9328 9445 case DIF_OP_LDGS:
9329 9446 if (v >= DIF_VAR_OTHER_UBASE)
9330 9447 break;
9331 9448
9332 9449 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
9333 9450 break;
9334 9451
9335 9452 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
9336 9453 v == DIF_VAR_PPID || v == DIF_VAR_TID ||
9337 9454 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
9338 9455 v == DIF_VAR_UID || v == DIF_VAR_GID)
9339 9456 break;
9340 9457
9341 9458 err += efunc(pc, "illegal variable %u\n", v);
9342 9459 break;
9343 9460
9344 9461 case DIF_OP_LDTA:
9345 9462 case DIF_OP_LDTS:
9346 9463 case DIF_OP_LDGAA:
9347 9464 case DIF_OP_LDTAA:
9348 9465 err += efunc(pc, "illegal dynamic variable load\n");
9349 9466 break;
9350 9467
9351 9468 case DIF_OP_STTS:
9352 9469 case DIF_OP_STGAA:
9353 9470 case DIF_OP_STTAA:
9354 9471 err += efunc(pc, "illegal dynamic variable store\n");
9355 9472 break;
9356 9473
9357 9474 case DIF_OP_CALL:
9358 9475 if (subr == DIF_SUBR_ALLOCA ||
9359 9476 subr == DIF_SUBR_BCOPY ||
9360 9477 subr == DIF_SUBR_COPYIN ||
9361 9478 subr == DIF_SUBR_COPYINTO ||
9362 9479 subr == DIF_SUBR_COPYINSTR ||
9363 9480 subr == DIF_SUBR_INDEX ||
9364 9481 subr == DIF_SUBR_INET_NTOA ||
9365 9482 subr == DIF_SUBR_INET_NTOA6 ||
9366 9483 subr == DIF_SUBR_INET_NTOP ||
9367 9484 subr == DIF_SUBR_LLTOSTR ||
9368 9485 subr == DIF_SUBR_STRTOLL ||
9369 9486 subr == DIF_SUBR_RINDEX ||
9370 9487 subr == DIF_SUBR_STRCHR ||
9371 9488 subr == DIF_SUBR_STRJOIN ||
9372 9489 subr == DIF_SUBR_STRRCHR ||
9373 9490 subr == DIF_SUBR_STRSTR ||
9374 9491 subr == DIF_SUBR_HTONS ||
9375 9492 subr == DIF_SUBR_HTONL ||
9376 9493 subr == DIF_SUBR_HTONLL ||
9377 9494 subr == DIF_SUBR_NTOHS ||
9378 9495 subr == DIF_SUBR_NTOHL ||
9379 9496 subr == DIF_SUBR_NTOHLL)
9380 9497 break;
9381 9498
9382 9499 err += efunc(pc, "invalid subr %u\n", subr);
9383 9500 break;
9384 9501
9385 9502 default:
9386 9503 err += efunc(pc, "invalid opcode %u\n",
9387 9504 DIF_INSTR_OP(instr));
9388 9505 }
9389 9506 }
9390 9507
9391 9508 return (err);
9392 9509 }
9393 9510
9394 9511 /*
9395 9512 * Returns 1 if the expression in the DIF object can be cached on a per-thread
9396 9513 * basis; 0 if not.
9397 9514 */
9398 9515 static int
9399 9516 dtrace_difo_cacheable(dtrace_difo_t *dp)
9400 9517 {
9401 9518 int i;
9402 9519
9403 9520 if (dp == NULL)
9404 9521 return (0);
9405 9522
9406 9523 for (i = 0; i < dp->dtdo_varlen; i++) {
9407 9524 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9408 9525
9409 9526 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
9410 9527 continue;
9411 9528
9412 9529 switch (v->dtdv_id) {
9413 9530 case DIF_VAR_CURTHREAD:
9414 9531 case DIF_VAR_PID:
9415 9532 case DIF_VAR_TID:
9416 9533 case DIF_VAR_EXECNAME:
9417 9534 case DIF_VAR_ZONENAME:
9418 9535 break;
9419 9536
9420 9537 default:
9421 9538 return (0);
9422 9539 }
9423 9540 }
9424 9541
9425 9542 /*
9426 9543 * This DIF object may be cacheable. Now we need to look for any
9427 9544 * array loading instructions, any memory loading instructions, or
9428 9545 * any stores to thread-local variables.
9429 9546 */
9430 9547 for (i = 0; i < dp->dtdo_len; i++) {
9431 9548 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
9432 9549
9433 9550 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
9434 9551 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
9435 9552 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
9436 9553 op == DIF_OP_LDGA || op == DIF_OP_STTS)
9437 9554 return (0);
9438 9555 }
9439 9556
9440 9557 return (1);
9441 9558 }
9442 9559
9443 9560 static void
9444 9561 dtrace_difo_hold(dtrace_difo_t *dp)
9445 9562 {
9446 9563 int i;
9447 9564
9448 9565 ASSERT(MUTEX_HELD(&dtrace_lock));
9449 9566
9450 9567 dp->dtdo_refcnt++;
9451 9568 ASSERT(dp->dtdo_refcnt != 0);
9452 9569
9453 9570 /*
9454 9571 * We need to check this DIF object for references to the variable
9455 9572 * DIF_VAR_VTIMESTAMP.
9456 9573 */
9457 9574 for (i = 0; i < dp->dtdo_varlen; i++) {
9458 9575 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9459 9576
9460 9577 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9461 9578 continue;
9462 9579
9463 9580 if (dtrace_vtime_references++ == 0)
9464 9581 dtrace_vtime_enable();
9465 9582 }
9466 9583 }
9467 9584
9468 9585 /*
9469 9586 * This routine calculates the dynamic variable chunksize for a given DIF
9470 9587 * object. The calculation is not fool-proof, and can probably be tricked by
9471 9588 * malicious DIF -- but it works for all compiler-generated DIF. Because this
9472 9589 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
9473 9590 * if a dynamic variable size exceeds the chunksize.
9474 9591 */
9475 9592 static void
9476 9593 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9477 9594 {
9478 9595 uint64_t sval;
9479 9596 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
9480 9597 const dif_instr_t *text = dp->dtdo_buf;
9481 9598 uint_t pc, srd = 0;
9482 9599 uint_t ttop = 0;
9483 9600 size_t size, ksize;
9484 9601 uint_t id, i;
9485 9602
9486 9603 for (pc = 0; pc < dp->dtdo_len; pc++) {
9487 9604 dif_instr_t instr = text[pc];
9488 9605 uint_t op = DIF_INSTR_OP(instr);
9489 9606 uint_t rd = DIF_INSTR_RD(instr);
9490 9607 uint_t r1 = DIF_INSTR_R1(instr);
9491 9608 uint_t nkeys = 0;
9492 9609 uchar_t scope;
9493 9610
9494 9611 dtrace_key_t *key = tupregs;
9495 9612
9496 9613 switch (op) {
9497 9614 case DIF_OP_SETX:
9498 9615 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
9499 9616 srd = rd;
9500 9617 continue;
9501 9618
9502 9619 case DIF_OP_STTS:
9503 9620 key = &tupregs[DIF_DTR_NREGS];
9504 9621 key[0].dttk_size = 0;
9505 9622 key[1].dttk_size = 0;
9506 9623 nkeys = 2;
9507 9624 scope = DIFV_SCOPE_THREAD;
9508 9625 break;
9509 9626
9510 9627 case DIF_OP_STGAA:
9511 9628 case DIF_OP_STTAA:
9512 9629 nkeys = ttop;
9513 9630
9514 9631 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
9515 9632 key[nkeys++].dttk_size = 0;
9516 9633
9517 9634 key[nkeys++].dttk_size = 0;
9518 9635
9519 9636 if (op == DIF_OP_STTAA) {
9520 9637 scope = DIFV_SCOPE_THREAD;
9521 9638 } else {
9522 9639 scope = DIFV_SCOPE_GLOBAL;
9523 9640 }
9524 9641
9525 9642 break;
9526 9643
9527 9644 case DIF_OP_PUSHTR:
9528 9645 if (ttop == DIF_DTR_NREGS)
9529 9646 return;
9530 9647
9531 9648 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
9532 9649 /*
9533 9650 * If the register for the size of the "pushtr"
9534 9651 * is %r0 (or the value is 0) and the type is
9535 9652 * a string, we'll use the system-wide default
9536 9653 * string size.
9537 9654 */
9538 9655 tupregs[ttop++].dttk_size =
9539 9656 dtrace_strsize_default;
9540 9657 } else {
9541 9658 if (srd == 0)
9542 9659 return;
9543 9660
9544 9661 tupregs[ttop++].dttk_size = sval;
9545 9662 }
9546 9663
9547 9664 break;
9548 9665
9549 9666 case DIF_OP_PUSHTV:
9550 9667 if (ttop == DIF_DTR_NREGS)
9551 9668 return;
9552 9669
9553 9670 tupregs[ttop++].dttk_size = 0;
9554 9671 break;
9555 9672
9556 9673 case DIF_OP_FLUSHTS:
9557 9674 ttop = 0;
9558 9675 break;
9559 9676
9560 9677 case DIF_OP_POPTS:
9561 9678 if (ttop != 0)
9562 9679 ttop--;
9563 9680 break;
9564 9681 }
9565 9682
9566 9683 sval = 0;
9567 9684 srd = 0;
9568 9685
9569 9686 if (nkeys == 0)
9570 9687 continue;
9571 9688
9572 9689 /*
9573 9690 * We have a dynamic variable allocation; calculate its size.
9574 9691 */
9575 9692 for (ksize = 0, i = 0; i < nkeys; i++)
9576 9693 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
9577 9694
9578 9695 size = sizeof (dtrace_dynvar_t);
9579 9696 size += sizeof (dtrace_key_t) * (nkeys - 1);
9580 9697 size += ksize;
9581 9698
9582 9699 /*
9583 9700 * Now we need to determine the size of the stored data.
9584 9701 */
9585 9702 id = DIF_INSTR_VAR(instr);
9586 9703
9587 9704 for (i = 0; i < dp->dtdo_varlen; i++) {
9588 9705 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9589 9706
9590 9707 if (v->dtdv_id == id && v->dtdv_scope == scope) {
9591 9708 size += v->dtdv_type.dtdt_size;
9592 9709 break;
9593 9710 }
9594 9711 }
9595 9712
9596 9713 if (i == dp->dtdo_varlen)
9597 9714 return;
9598 9715
9599 9716 /*
9600 9717 * We have the size. If this is larger than the chunk size
9601 9718 * for our dynamic variable state, reset the chunk size.
9602 9719 */
9603 9720 size = P2ROUNDUP(size, sizeof (uint64_t));
9604 9721
9605 9722 if (size > vstate->dtvs_dynvars.dtds_chunksize)
9606 9723 vstate->dtvs_dynvars.dtds_chunksize = size;
9607 9724 }
9608 9725 }
9609 9726
9610 9727 static void
9611 9728 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9612 9729 {
9613 9730 int i, oldsvars, osz, nsz, otlocals, ntlocals;
9614 9731 uint_t id;
9615 9732
9616 9733 ASSERT(MUTEX_HELD(&dtrace_lock));
9617 9734 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
9618 9735
9619 9736 for (i = 0; i < dp->dtdo_varlen; i++) {
9620 9737 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9621 9738 dtrace_statvar_t *svar, ***svarp;
9622 9739 size_t dsize = 0;
9623 9740 uint8_t scope = v->dtdv_scope;
9624 9741 int *np;
9625 9742
9626 9743 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9627 9744 continue;
9628 9745
9629 9746 id -= DIF_VAR_OTHER_UBASE;
9630 9747
9631 9748 switch (scope) {
9632 9749 case DIFV_SCOPE_THREAD:
9633 9750 while (id >= (otlocals = vstate->dtvs_ntlocals)) {
9634 9751 dtrace_difv_t *tlocals;
9635 9752
9636 9753 if ((ntlocals = (otlocals << 1)) == 0)
9637 9754 ntlocals = 1;
9638 9755
9639 9756 osz = otlocals * sizeof (dtrace_difv_t);
9640 9757 nsz = ntlocals * sizeof (dtrace_difv_t);
9641 9758
9642 9759 tlocals = kmem_zalloc(nsz, KM_SLEEP);
9643 9760
9644 9761 if (osz != 0) {
9645 9762 bcopy(vstate->dtvs_tlocals,
9646 9763 tlocals, osz);
9647 9764 kmem_free(vstate->dtvs_tlocals, osz);
9648 9765 }
9649 9766
9650 9767 vstate->dtvs_tlocals = tlocals;
9651 9768 vstate->dtvs_ntlocals = ntlocals;
9652 9769 }
9653 9770
9654 9771 vstate->dtvs_tlocals[id] = *v;
9655 9772 continue;
9656 9773
9657 9774 case DIFV_SCOPE_LOCAL:
9658 9775 np = &vstate->dtvs_nlocals;
9659 9776 svarp = &vstate->dtvs_locals;
9660 9777
9661 9778 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9662 9779 dsize = NCPU * (v->dtdv_type.dtdt_size +
9663 9780 sizeof (uint64_t));
9664 9781 else
9665 9782 dsize = NCPU * sizeof (uint64_t);
9666 9783
9667 9784 break;
9668 9785
9669 9786 case DIFV_SCOPE_GLOBAL:
9670 9787 np = &vstate->dtvs_nglobals;
9671 9788 svarp = &vstate->dtvs_globals;
9672 9789
9673 9790 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
9674 9791 dsize = v->dtdv_type.dtdt_size +
9675 9792 sizeof (uint64_t);
9676 9793
9677 9794 break;
9678 9795
9679 9796 default:
9680 9797 ASSERT(0);
9681 9798 }
9682 9799
9683 9800 while (id >= (oldsvars = *np)) {
9684 9801 dtrace_statvar_t **statics;
9685 9802 int newsvars, oldsize, newsize;
9686 9803
9687 9804 if ((newsvars = (oldsvars << 1)) == 0)
9688 9805 newsvars = 1;
9689 9806
9690 9807 oldsize = oldsvars * sizeof (dtrace_statvar_t *);
9691 9808 newsize = newsvars * sizeof (dtrace_statvar_t *);
9692 9809
9693 9810 statics = kmem_zalloc(newsize, KM_SLEEP);
9694 9811
9695 9812 if (oldsize != 0) {
9696 9813 bcopy(*svarp, statics, oldsize);
9697 9814 kmem_free(*svarp, oldsize);
9698 9815 }
9699 9816
9700 9817 *svarp = statics;
9701 9818 *np = newsvars;
9702 9819 }
9703 9820
9704 9821 if ((svar = (*svarp)[id]) == NULL) {
9705 9822 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
9706 9823 svar->dtsv_var = *v;
9707 9824
9708 9825 if ((svar->dtsv_size = dsize) != 0) {
9709 9826 svar->dtsv_data = (uint64_t)(uintptr_t)
9710 9827 kmem_zalloc(dsize, KM_SLEEP);
9711 9828 }
9712 9829
9713 9830 (*svarp)[id] = svar;
9714 9831 }
9715 9832
9716 9833 svar->dtsv_refcnt++;
9717 9834 }
9718 9835
9719 9836 dtrace_difo_chunksize(dp, vstate);
9720 9837 dtrace_difo_hold(dp);
9721 9838 }
9722 9839
9723 9840 static dtrace_difo_t *
9724 9841 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9725 9842 {
9726 9843 dtrace_difo_t *new;
9727 9844 size_t sz;
9728 9845
9729 9846 ASSERT(dp->dtdo_buf != NULL);
9730 9847 ASSERT(dp->dtdo_refcnt != 0);
9731 9848
9732 9849 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
9733 9850
9734 9851 ASSERT(dp->dtdo_buf != NULL);
9735 9852 sz = dp->dtdo_len * sizeof (dif_instr_t);
9736 9853 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
9737 9854 bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
9738 9855 new->dtdo_len = dp->dtdo_len;
9739 9856
9740 9857 if (dp->dtdo_strtab != NULL) {
9741 9858 ASSERT(dp->dtdo_strlen != 0);
9742 9859 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
9743 9860 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
9744 9861 new->dtdo_strlen = dp->dtdo_strlen;
9745 9862 }
9746 9863
9747 9864 if (dp->dtdo_inttab != NULL) {
9748 9865 ASSERT(dp->dtdo_intlen != 0);
9749 9866 sz = dp->dtdo_intlen * sizeof (uint64_t);
9750 9867 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
9751 9868 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
9752 9869 new->dtdo_intlen = dp->dtdo_intlen;
9753 9870 }
9754 9871
9755 9872 if (dp->dtdo_vartab != NULL) {
9756 9873 ASSERT(dp->dtdo_varlen != 0);
9757 9874 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
9758 9875 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
9759 9876 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
9760 9877 new->dtdo_varlen = dp->dtdo_varlen;
9761 9878 }
9762 9879
9763 9880 dtrace_difo_init(new, vstate);
9764 9881 return (new);
9765 9882 }
9766 9883
9767 9884 static void
9768 9885 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9769 9886 {
9770 9887 int i;
9771 9888
9772 9889 ASSERT(dp->dtdo_refcnt == 0);
9773 9890
9774 9891 for (i = 0; i < dp->dtdo_varlen; i++) {
9775 9892 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9776 9893 dtrace_statvar_t *svar, **svarp;
9777 9894 uint_t id;
9778 9895 uint8_t scope = v->dtdv_scope;
9779 9896 int *np;
9780 9897
9781 9898 switch (scope) {
9782 9899 case DIFV_SCOPE_THREAD:
9783 9900 continue;
9784 9901
9785 9902 case DIFV_SCOPE_LOCAL:
9786 9903 np = &vstate->dtvs_nlocals;
9787 9904 svarp = vstate->dtvs_locals;
9788 9905 break;
9789 9906
9790 9907 case DIFV_SCOPE_GLOBAL:
9791 9908 np = &vstate->dtvs_nglobals;
9792 9909 svarp = vstate->dtvs_globals;
9793 9910 break;
9794 9911
9795 9912 default:
9796 9913 ASSERT(0);
9797 9914 }
9798 9915
9799 9916 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
9800 9917 continue;
9801 9918
9802 9919 id -= DIF_VAR_OTHER_UBASE;
9803 9920 ASSERT(id < *np);
9804 9921
9805 9922 svar = svarp[id];
9806 9923 ASSERT(svar != NULL);
9807 9924 ASSERT(svar->dtsv_refcnt > 0);
9808 9925
9809 9926 if (--svar->dtsv_refcnt > 0)
9810 9927 continue;
9811 9928
9812 9929 if (svar->dtsv_size != 0) {
9813 9930 ASSERT(svar->dtsv_data != NULL);
9814 9931 kmem_free((void *)(uintptr_t)svar->dtsv_data,
9815 9932 svar->dtsv_size);
9816 9933 }
9817 9934
9818 9935 kmem_free(svar, sizeof (dtrace_statvar_t));
9819 9936 svarp[id] = NULL;
9820 9937 }
9821 9938
9822 9939 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
9823 9940 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
9824 9941 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
9825 9942 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
9826 9943
9827 9944 kmem_free(dp, sizeof (dtrace_difo_t));
9828 9945 }
9829 9946
9830 9947 static void
9831 9948 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
9832 9949 {
9833 9950 int i;
9834 9951
9835 9952 ASSERT(MUTEX_HELD(&dtrace_lock));
9836 9953 ASSERT(dp->dtdo_refcnt != 0);
9837 9954
9838 9955 for (i = 0; i < dp->dtdo_varlen; i++) {
9839 9956 dtrace_difv_t *v = &dp->dtdo_vartab[i];
9840 9957
9841 9958 if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
9842 9959 continue;
9843 9960
9844 9961 ASSERT(dtrace_vtime_references > 0);
9845 9962 if (--dtrace_vtime_references == 0)
9846 9963 dtrace_vtime_disable();
9847 9964 }
9848 9965
9849 9966 if (--dp->dtdo_refcnt == 0)
9850 9967 dtrace_difo_destroy(dp, vstate);
9851 9968 }
9852 9969
9853 9970 /*
9854 9971 * DTrace Format Functions
9855 9972 */
9856 9973 static uint16_t
9857 9974 dtrace_format_add(dtrace_state_t *state, char *str)
9858 9975 {
9859 9976 char *fmt, **new;
9860 9977 uint16_t ndx, len = strlen(str) + 1;
9861 9978
9862 9979 fmt = kmem_zalloc(len, KM_SLEEP);
9863 9980 bcopy(str, fmt, len);
9864 9981
9865 9982 for (ndx = 0; ndx < state->dts_nformats; ndx++) {
9866 9983 if (state->dts_formats[ndx] == NULL) {
9867 9984 state->dts_formats[ndx] = fmt;
9868 9985 return (ndx + 1);
9869 9986 }
9870 9987 }
9871 9988
9872 9989 if (state->dts_nformats == USHRT_MAX) {
9873 9990 /*
9874 9991 * This is only likely if a denial-of-service attack is being
9875 9992 * attempted. As such, it's okay to fail silently here.
9876 9993 */
9877 9994 kmem_free(fmt, len);
9878 9995 return (0);
9879 9996 }
9880 9997
9881 9998 /*
9882 9999 * For simplicity, we always resize the formats array to be exactly the
9883 10000 * number of formats.
9884 10001 */
9885 10002 ndx = state->dts_nformats++;
9886 10003 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
9887 10004
9888 10005 if (state->dts_formats != NULL) {
9889 10006 ASSERT(ndx != 0);
9890 10007 bcopy(state->dts_formats, new, ndx * sizeof (char *));
9891 10008 kmem_free(state->dts_formats, ndx * sizeof (char *));
9892 10009 }
9893 10010
9894 10011 state->dts_formats = new;
9895 10012 state->dts_formats[ndx] = fmt;
9896 10013
9897 10014 return (ndx + 1);
9898 10015 }
9899 10016
9900 10017 static void
9901 10018 dtrace_format_remove(dtrace_state_t *state, uint16_t format)
9902 10019 {
9903 10020 char *fmt;
9904 10021
9905 10022 ASSERT(state->dts_formats != NULL);
9906 10023 ASSERT(format <= state->dts_nformats);
9907 10024 ASSERT(state->dts_formats[format - 1] != NULL);
9908 10025
9909 10026 fmt = state->dts_formats[format - 1];
9910 10027 kmem_free(fmt, strlen(fmt) + 1);
9911 10028 state->dts_formats[format - 1] = NULL;
9912 10029 }
9913 10030
9914 10031 static void
9915 10032 dtrace_format_destroy(dtrace_state_t *state)
9916 10033 {
9917 10034 int i;
9918 10035
9919 10036 if (state->dts_nformats == 0) {
9920 10037 ASSERT(state->dts_formats == NULL);
9921 10038 return;
9922 10039 }
9923 10040
9924 10041 ASSERT(state->dts_formats != NULL);
9925 10042
9926 10043 for (i = 0; i < state->dts_nformats; i++) {
9927 10044 char *fmt = state->dts_formats[i];
9928 10045
9929 10046 if (fmt == NULL)
9930 10047 continue;
9931 10048
9932 10049 kmem_free(fmt, strlen(fmt) + 1);
9933 10050 }
9934 10051
9935 10052 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
9936 10053 state->dts_nformats = 0;
9937 10054 state->dts_formats = NULL;
9938 10055 }
9939 10056
9940 10057 /*
9941 10058 * DTrace Predicate Functions
9942 10059 */
9943 10060 static dtrace_predicate_t *
9944 10061 dtrace_predicate_create(dtrace_difo_t *dp)
9945 10062 {
9946 10063 dtrace_predicate_t *pred;
9947 10064
9948 10065 ASSERT(MUTEX_HELD(&dtrace_lock));
9949 10066 ASSERT(dp->dtdo_refcnt != 0);
9950 10067
9951 10068 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
9952 10069 pred->dtp_difo = dp;
9953 10070 pred->dtp_refcnt = 1;
9954 10071
9955 10072 if (!dtrace_difo_cacheable(dp))
9956 10073 return (pred);
9957 10074
9958 10075 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
9959 10076 /*
9960 10077 * This is only theoretically possible -- we have had 2^32
9961 10078 * cacheable predicates on this machine. We cannot allow any
9962 10079 * more predicates to become cacheable: as unlikely as it is,
9963 10080 * there may be a thread caching a (now stale) predicate cache
9964 10081 * ID. (N.B.: the temptation is being successfully resisted to
9965 10082 * have this cmn_err() "Holy shit -- we executed this code!")
9966 10083 */
9967 10084 return (pred);
9968 10085 }
9969 10086
9970 10087 pred->dtp_cacheid = dtrace_predcache_id++;
9971 10088
9972 10089 return (pred);
9973 10090 }
9974 10091
9975 10092 static void
9976 10093 dtrace_predicate_hold(dtrace_predicate_t *pred)
9977 10094 {
9978 10095 ASSERT(MUTEX_HELD(&dtrace_lock));
9979 10096 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
9980 10097 ASSERT(pred->dtp_refcnt > 0);
9981 10098
9982 10099 pred->dtp_refcnt++;
9983 10100 }
9984 10101
9985 10102 static void
9986 10103 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
9987 10104 {
9988 10105 dtrace_difo_t *dp = pred->dtp_difo;
9989 10106
9990 10107 ASSERT(MUTEX_HELD(&dtrace_lock));
9991 10108 ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
9992 10109 ASSERT(pred->dtp_refcnt > 0);
9993 10110
9994 10111 if (--pred->dtp_refcnt == 0) {
9995 10112 dtrace_difo_release(pred->dtp_difo, vstate);
9996 10113 kmem_free(pred, sizeof (dtrace_predicate_t));
9997 10114 }
9998 10115 }
9999 10116
10000 10117 /*
10001 10118 * DTrace Action Description Functions
10002 10119 */
10003 10120 static dtrace_actdesc_t *
10004 10121 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
10005 10122 uint64_t uarg, uint64_t arg)
10006 10123 {
10007 10124 dtrace_actdesc_t *act;
10008 10125
10009 10126 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
10010 10127 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA));
10011 10128
10012 10129 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
10013 10130 act->dtad_kind = kind;
10014 10131 act->dtad_ntuple = ntuple;
10015 10132 act->dtad_uarg = uarg;
10016 10133 act->dtad_arg = arg;
10017 10134 act->dtad_refcnt = 1;
10018 10135
10019 10136 return (act);
10020 10137 }
10021 10138
10022 10139 static void
10023 10140 dtrace_actdesc_hold(dtrace_actdesc_t *act)
10024 10141 {
10025 10142 ASSERT(act->dtad_refcnt >= 1);
10026 10143 act->dtad_refcnt++;
10027 10144 }
10028 10145
10029 10146 static void
10030 10147 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
10031 10148 {
10032 10149 dtrace_actkind_t kind = act->dtad_kind;
10033 10150 dtrace_difo_t *dp;
10034 10151
10035 10152 ASSERT(act->dtad_refcnt >= 1);
10036 10153
10037 10154 if (--act->dtad_refcnt != 0)
10038 10155 return;
10039 10156
10040 10157 if ((dp = act->dtad_difo) != NULL)
10041 10158 dtrace_difo_release(dp, vstate);
10042 10159
10043 10160 if (DTRACEACT_ISPRINTFLIKE(kind)) {
10044 10161 char *str = (char *)(uintptr_t)act->dtad_arg;
10045 10162
10046 10163 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
10047 10164 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
10048 10165
10049 10166 if (str != NULL)
10050 10167 kmem_free(str, strlen(str) + 1);
10051 10168 }
10052 10169
10053 10170 kmem_free(act, sizeof (dtrace_actdesc_t));
10054 10171 }
10055 10172
10056 10173 /*
10057 10174 * DTrace ECB Functions
10058 10175 */
10059 10176 static dtrace_ecb_t *
10060 10177 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
10061 10178 {
10062 10179 dtrace_ecb_t *ecb;
10063 10180 dtrace_epid_t epid;
10064 10181
10065 10182 ASSERT(MUTEX_HELD(&dtrace_lock));
10066 10183
10067 10184 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
10068 10185 ecb->dte_predicate = NULL;
10069 10186 ecb->dte_probe = probe;
10070 10187
10071 10188 /*
10072 10189 * The default size is the size of the default action: recording
10073 10190 * the header.
10074 10191 */
10075 10192 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
10076 10193 ecb->dte_alignment = sizeof (dtrace_epid_t);
10077 10194
10078 10195 epid = state->dts_epid++;
10079 10196
10080 10197 if (epid - 1 >= state->dts_necbs) {
10081 10198 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
10082 10199 int necbs = state->dts_necbs << 1;
10083 10200
10084 10201 ASSERT(epid == state->dts_necbs + 1);
10085 10202
10086 10203 if (necbs == 0) {
10087 10204 ASSERT(oecbs == NULL);
10088 10205 necbs = 1;
10089 10206 }
10090 10207
10091 10208 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
10092 10209
10093 10210 if (oecbs != NULL)
10094 10211 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
10095 10212
10096 10213 dtrace_membar_producer();
10097 10214 state->dts_ecbs = ecbs;
10098 10215
10099 10216 if (oecbs != NULL) {
10100 10217 /*
10101 10218 * If this state is active, we must dtrace_sync()
10102 10219 * before we can free the old dts_ecbs array: we're
10103 10220 * coming in hot, and there may be active ring
10104 10221 * buffer processing (which indexes into the dts_ecbs
10105 10222 * array) on another CPU.
10106 10223 */
10107 10224 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
10108 10225 dtrace_sync();
10109 10226
10110 10227 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
10111 10228 }
10112 10229
10113 10230 dtrace_membar_producer();
10114 10231 state->dts_necbs = necbs;
10115 10232 }
10116 10233
10117 10234 ecb->dte_state = state;
10118 10235
10119 10236 ASSERT(state->dts_ecbs[epid - 1] == NULL);
10120 10237 dtrace_membar_producer();
10121 10238 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
10122 10239
10123 10240 return (ecb);
10124 10241 }
10125 10242
10126 10243 static int
10127 10244 dtrace_ecb_enable(dtrace_ecb_t *ecb)
10128 10245 {
10129 10246 dtrace_probe_t *probe = ecb->dte_probe;
10130 10247
10131 10248 ASSERT(MUTEX_HELD(&cpu_lock));
10132 10249 ASSERT(MUTEX_HELD(&dtrace_lock));
10133 10250 ASSERT(ecb->dte_next == NULL);
10134 10251
10135 10252 if (probe == NULL) {
10136 10253 /*
10137 10254 * This is the NULL probe -- there's nothing to do.
10138 10255 */
10139 10256 return (0);
10140 10257 }
10141 10258
10142 10259 if (probe->dtpr_ecb == NULL) {
10143 10260 dtrace_provider_t *prov = probe->dtpr_provider;
10144 10261
10145 10262 /*
10146 10263 * We're the first ECB on this probe.
10147 10264 */
10148 10265 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
10149 10266
10150 10267 if (ecb->dte_predicate != NULL)
10151 10268 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
10152 10269
10153 10270 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
10154 10271 probe->dtpr_id, probe->dtpr_arg));
10155 10272 } else {
10156 10273 /*
10157 10274 * This probe is already active. Swing the last pointer to
10158 10275 * point to the new ECB, and issue a dtrace_sync() to assure
10159 10276 * that all CPUs have seen the change.
10160 10277 */
10161 10278 ASSERT(probe->dtpr_ecb_last != NULL);
10162 10279 probe->dtpr_ecb_last->dte_next = ecb;
10163 10280 probe->dtpr_ecb_last = ecb;
10164 10281 probe->dtpr_predcache = 0;
10165 10282
10166 10283 dtrace_sync();
10167 10284 return (0);
10168 10285 }
10169 10286 }
10170 10287
10171 10288 static void
10172 10289 dtrace_ecb_resize(dtrace_ecb_t *ecb)
10173 10290 {
10174 10291 dtrace_action_t *act;
10175 10292 uint32_t curneeded = UINT32_MAX;
10176 10293 uint32_t aggbase = UINT32_MAX;
10177 10294
10178 10295 /*
10179 10296 * If we record anything, we always record the dtrace_rechdr_t. (And
10180 10297 * we always record it first.)
10181 10298 */
10182 10299 ecb->dte_size = sizeof (dtrace_rechdr_t);
10183 10300 ecb->dte_alignment = sizeof (dtrace_epid_t);
10184 10301
10185 10302 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
10186 10303 dtrace_recdesc_t *rec = &act->dta_rec;
10187 10304 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
10188 10305
10189 10306 ecb->dte_alignment = MAX(ecb->dte_alignment,
10190 10307 rec->dtrd_alignment);
10191 10308
10192 10309 if (DTRACEACT_ISAGG(act->dta_kind)) {
10193 10310 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
10194 10311
10195 10312 ASSERT(rec->dtrd_size != 0);
10196 10313 ASSERT(agg->dtag_first != NULL);
10197 10314 ASSERT(act->dta_prev->dta_intuple);
10198 10315 ASSERT(aggbase != UINT32_MAX);
10199 10316 ASSERT(curneeded != UINT32_MAX);
10200 10317
10201 10318 agg->dtag_base = aggbase;
10202 10319
10203 10320 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
10204 10321 rec->dtrd_offset = curneeded;
10205 10322 curneeded += rec->dtrd_size;
10206 10323 ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
10207 10324
10208 10325 aggbase = UINT32_MAX;
10209 10326 curneeded = UINT32_MAX;
10210 10327 } else if (act->dta_intuple) {
10211 10328 if (curneeded == UINT32_MAX) {
10212 10329 /*
10213 10330 * This is the first record in a tuple. Align
10214 10331 * curneeded to be at offset 4 in an 8-byte
10215 10332 * aligned block.
10216 10333 */
10217 10334 ASSERT(act->dta_prev == NULL ||
10218 10335 !act->dta_prev->dta_intuple);
10219 10336 ASSERT3U(aggbase, ==, UINT32_MAX);
10220 10337 curneeded = P2PHASEUP(ecb->dte_size,
10221 10338 sizeof (uint64_t), sizeof (dtrace_aggid_t));
10222 10339
10223 10340 aggbase = curneeded - sizeof (dtrace_aggid_t);
10224 10341 ASSERT(IS_P2ALIGNED(aggbase,
10225 10342 sizeof (uint64_t)));
10226 10343 }
10227 10344 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
10228 10345 rec->dtrd_offset = curneeded;
10229 10346 curneeded += rec->dtrd_size;
10230 10347 } else {
10231 10348 /* tuples must be followed by an aggregation */
10232 10349 ASSERT(act->dta_prev == NULL ||
10233 10350 !act->dta_prev->dta_intuple);
10234 10351
10235 10352 ecb->dte_size = P2ROUNDUP(ecb->dte_size,
10236 10353 rec->dtrd_alignment);
10237 10354 rec->dtrd_offset = ecb->dte_size;
10238 10355 ecb->dte_size += rec->dtrd_size;
10239 10356 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
10240 10357 }
10241 10358 }
10242 10359
10243 10360 if ((act = ecb->dte_action) != NULL &&
10244 10361 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
10245 10362 ecb->dte_size == sizeof (dtrace_rechdr_t)) {
10246 10363 /*
10247 10364 * If the size is still sizeof (dtrace_rechdr_t), then all
10248 10365 * actions store no data; set the size to 0.
10249 10366 */
10250 10367 ecb->dte_size = 0;
10251 10368 }
10252 10369
10253 10370 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
10254 10371 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
10255 10372 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed,
10256 10373 ecb->dte_needed);
10257 10374 }
10258 10375
10259 10376 static dtrace_action_t *
10260 10377 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
10261 10378 {
10262 10379 dtrace_aggregation_t *agg;
10263 10380 size_t size = sizeof (uint64_t);
10264 10381 int ntuple = desc->dtad_ntuple;
10265 10382 dtrace_action_t *act;
10266 10383 dtrace_recdesc_t *frec;
10267 10384 dtrace_aggid_t aggid;
10268 10385 dtrace_state_t *state = ecb->dte_state;
10269 10386
10270 10387 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
10271 10388 agg->dtag_ecb = ecb;
10272 10389
10273 10390 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
10274 10391
10275 10392 switch (desc->dtad_kind) {
10276 10393 case DTRACEAGG_MIN:
10277 10394 agg->dtag_initial = INT64_MAX;
10278 10395 agg->dtag_aggregate = dtrace_aggregate_min;
10279 10396 break;
10280 10397
10281 10398 case DTRACEAGG_MAX:
10282 10399 agg->dtag_initial = INT64_MIN;
10283 10400 agg->dtag_aggregate = dtrace_aggregate_max;
10284 10401 break;
10285 10402
10286 10403 case DTRACEAGG_COUNT:
10287 10404 agg->dtag_aggregate = dtrace_aggregate_count;
10288 10405 break;
10289 10406
10290 10407 case DTRACEAGG_QUANTIZE:
10291 10408 agg->dtag_aggregate = dtrace_aggregate_quantize;
10292 10409 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
10293 10410 sizeof (uint64_t);
10294 10411 break;
10295 10412
10296 10413 case DTRACEAGG_LQUANTIZE: {
10297 10414 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
10298 10415 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
10299 10416
10300 10417 agg->dtag_initial = desc->dtad_arg;
10301 10418 agg->dtag_aggregate = dtrace_aggregate_lquantize;
10302 10419
10303 10420 if (step == 0 || levels == 0)
10304 10421 goto err;
10305 10422
10306 10423 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
10307 10424 break;
10308 10425 }
10309 10426
10310 10427 case DTRACEAGG_LLQUANTIZE: {
10311 10428 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
10312 10429 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
10313 10430 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
10314 10431 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
10315 10432 int64_t v;
10316 10433
10317 10434 agg->dtag_initial = desc->dtad_arg;
10318 10435 agg->dtag_aggregate = dtrace_aggregate_llquantize;
10319 10436
10320 10437 if (factor < 2 || low >= high || nsteps < factor)
10321 10438 goto err;
10322 10439
10323 10440 /*
10324 10441 * Now check that the number of steps evenly divides a power
10325 10442 * of the factor. (This assures both integer bucket size and
10326 10443 * linearity within each magnitude.)
10327 10444 */
10328 10445 for (v = factor; v < nsteps; v *= factor)
10329 10446 continue;
10330 10447
10331 10448 if ((v % nsteps) || (nsteps % factor))
10332 10449 goto err;
10333 10450
10334 10451 size = (dtrace_aggregate_llquantize_bucket(factor,
10335 10452 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
10336 10453 break;
10337 10454 }
10338 10455
10339 10456 case DTRACEAGG_AVG:
10340 10457 agg->dtag_aggregate = dtrace_aggregate_avg;
10341 10458 size = sizeof (uint64_t) * 2;
10342 10459 break;
10343 10460
10344 10461 case DTRACEAGG_STDDEV:
10345 10462 agg->dtag_aggregate = dtrace_aggregate_stddev;
10346 10463 size = sizeof (uint64_t) * 4;
10347 10464 break;
10348 10465
10349 10466 case DTRACEAGG_SUM:
10350 10467 agg->dtag_aggregate = dtrace_aggregate_sum;
10351 10468 break;
10352 10469
10353 10470 default:
10354 10471 goto err;
10355 10472 }
10356 10473
10357 10474 agg->dtag_action.dta_rec.dtrd_size = size;
10358 10475
10359 10476 if (ntuple == 0)
10360 10477 goto err;
10361 10478
10362 10479 /*
10363 10480 * We must make sure that we have enough actions for the n-tuple.
10364 10481 */
10365 10482 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
10366 10483 if (DTRACEACT_ISAGG(act->dta_kind))
10367 10484 break;
10368 10485
10369 10486 if (--ntuple == 0) {
10370 10487 /*
10371 10488 * This is the action with which our n-tuple begins.
10372 10489 */
10373 10490 agg->dtag_first = act;
10374 10491 goto success;
10375 10492 }
10376 10493 }
10377 10494
10378 10495 /*
10379 10496 * This n-tuple is short by ntuple elements. Return failure.
10380 10497 */
10381 10498 ASSERT(ntuple != 0);
10382 10499 err:
10383 10500 kmem_free(agg, sizeof (dtrace_aggregation_t));
10384 10501 return (NULL);
10385 10502
10386 10503 success:
10387 10504 /*
10388 10505 * If the last action in the tuple has a size of zero, it's actually
10389 10506 * an expression argument for the aggregating action.
10390 10507 */
10391 10508 ASSERT(ecb->dte_action_last != NULL);
10392 10509 act = ecb->dte_action_last;
10393 10510
10394 10511 if (act->dta_kind == DTRACEACT_DIFEXPR) {
10395 10512 ASSERT(act->dta_difo != NULL);
10396 10513
10397 10514 if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
10398 10515 agg->dtag_hasarg = 1;
10399 10516 }
10400 10517
10401 10518 /*
10402 10519 * We need to allocate an id for this aggregation.
10403 10520 */
10404 10521 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
10405 10522 VM_BESTFIT | VM_SLEEP);
10406 10523
10407 10524 if (aggid - 1 >= state->dts_naggregations) {
10408 10525 dtrace_aggregation_t **oaggs = state->dts_aggregations;
10409 10526 dtrace_aggregation_t **aggs;
10410 10527 int naggs = state->dts_naggregations << 1;
10411 10528 int onaggs = state->dts_naggregations;
10412 10529
10413 10530 ASSERT(aggid == state->dts_naggregations + 1);
10414 10531
10415 10532 if (naggs == 0) {
10416 10533 ASSERT(oaggs == NULL);
10417 10534 naggs = 1;
10418 10535 }
10419 10536
10420 10537 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
10421 10538
10422 10539 if (oaggs != NULL) {
10423 10540 bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
10424 10541 kmem_free(oaggs, onaggs * sizeof (*aggs));
10425 10542 }
10426 10543
10427 10544 state->dts_aggregations = aggs;
10428 10545 state->dts_naggregations = naggs;
10429 10546 }
10430 10547
10431 10548 ASSERT(state->dts_aggregations[aggid - 1] == NULL);
10432 10549 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
10433 10550
10434 10551 frec = &agg->dtag_first->dta_rec;
10435 10552 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
10436 10553 frec->dtrd_alignment = sizeof (dtrace_aggid_t);
10437 10554
10438 10555 for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
10439 10556 ASSERT(!act->dta_intuple);
10440 10557 act->dta_intuple = 1;
10441 10558 }
10442 10559
10443 10560 return (&agg->dtag_action);
10444 10561 }
10445 10562
10446 10563 static void
10447 10564 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
10448 10565 {
10449 10566 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
10450 10567 dtrace_state_t *state = ecb->dte_state;
10451 10568 dtrace_aggid_t aggid = agg->dtag_id;
10452 10569
10453 10570 ASSERT(DTRACEACT_ISAGG(act->dta_kind));
10454 10571 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
10455 10572
10456 10573 ASSERT(state->dts_aggregations[aggid - 1] == agg);
10457 10574 state->dts_aggregations[aggid - 1] = NULL;
10458 10575
10459 10576 kmem_free(agg, sizeof (dtrace_aggregation_t));
10460 10577 }
10461 10578
10462 10579 static int
10463 10580 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
10464 10581 {
10465 10582 dtrace_action_t *action, *last;
10466 10583 dtrace_difo_t *dp = desc->dtad_difo;
10467 10584 uint32_t size = 0, align = sizeof (uint8_t), mask;
10468 10585 uint16_t format = 0;
10469 10586 dtrace_recdesc_t *rec;
10470 10587 dtrace_state_t *state = ecb->dte_state;
10471 10588 dtrace_optval_t *opt = state->dts_options, nframes, strsize;
10472 10589 uint64_t arg = desc->dtad_arg;
10473 10590
10474 10591 ASSERT(MUTEX_HELD(&dtrace_lock));
10475 10592 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
10476 10593
10477 10594 if (DTRACEACT_ISAGG(desc->dtad_kind)) {
10478 10595 /*
10479 10596 * If this is an aggregating action, there must be neither
10480 10597 * a speculate nor a commit on the action chain.
10481 10598 */
10482 10599 dtrace_action_t *act;
10483 10600
10484 10601 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
10485 10602 if (act->dta_kind == DTRACEACT_COMMIT)
10486 10603 return (EINVAL);
10487 10604
10488 10605 if (act->dta_kind == DTRACEACT_SPECULATE)
10489 10606 return (EINVAL);
10490 10607 }
10491 10608
10492 10609 action = dtrace_ecb_aggregation_create(ecb, desc);
10493 10610
10494 10611 if (action == NULL)
10495 10612 return (EINVAL);
10496 10613 } else {
10497 10614 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
10498 10615 (desc->dtad_kind == DTRACEACT_DIFEXPR &&
10499 10616 dp != NULL && dp->dtdo_destructive)) {
10500 10617 state->dts_destructive = 1;
10501 10618 }
10502 10619
10503 10620 switch (desc->dtad_kind) {
10504 10621 case DTRACEACT_PRINTF:
10505 10622 case DTRACEACT_PRINTA:
10506 10623 case DTRACEACT_SYSTEM:
10507 10624 case DTRACEACT_FREOPEN:
10508 10625 case DTRACEACT_DIFEXPR:
10509 10626 /*
10510 10627 * We know that our arg is a string -- turn it into a
10511 10628 * format.
10512 10629 */
10513 10630 if (arg == NULL) {
10514 10631 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
10515 10632 desc->dtad_kind == DTRACEACT_DIFEXPR);
10516 10633 format = 0;
10517 10634 } else {
10518 10635 ASSERT(arg != NULL);
10519 10636 ASSERT(arg > KERNELBASE);
10520 10637 format = dtrace_format_add(state,
10521 10638 (char *)(uintptr_t)arg);
10522 10639 }
10523 10640
10524 10641 /*FALLTHROUGH*/
10525 10642 case DTRACEACT_LIBACT:
10526 10643 case DTRACEACT_TRACEMEM:
10527 10644 case DTRACEACT_TRACEMEM_DYNSIZE:
10528 10645 if (dp == NULL)
10529 10646 return (EINVAL);
10530 10647
10531 10648 if ((size = dp->dtdo_rtype.dtdt_size) != 0)
10532 10649 break;
10533 10650
10534 10651 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
10535 10652 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10536 10653 return (EINVAL);
10537 10654
10538 10655 size = opt[DTRACEOPT_STRSIZE];
10539 10656 }
10540 10657
10541 10658 break;
10542 10659
10543 10660 case DTRACEACT_STACK:
10544 10661 if ((nframes = arg) == 0) {
10545 10662 nframes = opt[DTRACEOPT_STACKFRAMES];
10546 10663 ASSERT(nframes > 0);
10547 10664 arg = nframes;
10548 10665 }
10549 10666
10550 10667 size = nframes * sizeof (pc_t);
10551 10668 break;
10552 10669
10553 10670 case DTRACEACT_JSTACK:
10554 10671 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
10555 10672 strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
10556 10673
10557 10674 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
10558 10675 nframes = opt[DTRACEOPT_JSTACKFRAMES];
10559 10676
10560 10677 arg = DTRACE_USTACK_ARG(nframes, strsize);
10561 10678
10562 10679 /*FALLTHROUGH*/
10563 10680 case DTRACEACT_USTACK:
10564 10681 if (desc->dtad_kind != DTRACEACT_JSTACK &&
10565 10682 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
10566 10683 strsize = DTRACE_USTACK_STRSIZE(arg);
10567 10684 nframes = opt[DTRACEOPT_USTACKFRAMES];
10568 10685 ASSERT(nframes > 0);
10569 10686 arg = DTRACE_USTACK_ARG(nframes, strsize);
10570 10687 }
10571 10688
10572 10689 /*
10573 10690 * Save a slot for the pid.
10574 10691 */
10575 10692 size = (nframes + 1) * sizeof (uint64_t);
10576 10693 size += DTRACE_USTACK_STRSIZE(arg);
10577 10694 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
10578 10695
10579 10696 break;
10580 10697
10581 10698 case DTRACEACT_SYM:
10582 10699 case DTRACEACT_MOD:
10583 10700 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
10584 10701 sizeof (uint64_t)) ||
10585 10702 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10586 10703 return (EINVAL);
10587 10704 break;
10588 10705
10589 10706 case DTRACEACT_USYM:
10590 10707 case DTRACEACT_UMOD:
10591 10708 case DTRACEACT_UADDR:
10592 10709 if (dp == NULL ||
10593 10710 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
10594 10711 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10595 10712 return (EINVAL);
10596 10713
10597 10714 /*
10598 10715 * We have a slot for the pid, plus a slot for the
10599 10716 * argument. To keep things simple (aligned with
10600 10717 * bitness-neutral sizing), we store each as a 64-bit
10601 10718 * quantity.
10602 10719 */
10603 10720 size = 2 * sizeof (uint64_t);
10604 10721 break;
10605 10722
10606 10723 case DTRACEACT_STOP:
10607 10724 case DTRACEACT_BREAKPOINT:
10608 10725 case DTRACEACT_PANIC:
10609 10726 break;
10610 10727
10611 10728 case DTRACEACT_CHILL:
10612 10729 case DTRACEACT_DISCARD:
10613 10730 case DTRACEACT_RAISE:
10614 10731 if (dp == NULL)
10615 10732 return (EINVAL);
10616 10733 break;
10617 10734
10618 10735 case DTRACEACT_EXIT:
10619 10736 if (dp == NULL ||
10620 10737 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
10621 10738 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
10622 10739 return (EINVAL);
10623 10740 break;
10624 10741
10625 10742 case DTRACEACT_SPECULATE:
10626 10743 if (ecb->dte_size > sizeof (dtrace_rechdr_t))
10627 10744 return (EINVAL);
10628 10745
10629 10746 if (dp == NULL)
10630 10747 return (EINVAL);
10631 10748
10632 10749 state->dts_speculates = 1;
10633 10750 break;
10634 10751
10635 10752 case DTRACEACT_COMMIT: {
10636 10753 dtrace_action_t *act = ecb->dte_action;
10637 10754
10638 10755 for (; act != NULL; act = act->dta_next) {
10639 10756 if (act->dta_kind == DTRACEACT_COMMIT)
10640 10757 return (EINVAL);
10641 10758 }
10642 10759
10643 10760 if (dp == NULL)
10644 10761 return (EINVAL);
10645 10762 break;
10646 10763 }
10647 10764
10648 10765 default:
10649 10766 return (EINVAL);
10650 10767 }
10651 10768
10652 10769 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
10653 10770 /*
10654 10771 * If this is a data-storing action or a speculate,
10655 10772 * we must be sure that there isn't a commit on the
10656 10773 * action chain.
10657 10774 */
10658 10775 dtrace_action_t *act = ecb->dte_action;
10659 10776
10660 10777 for (; act != NULL; act = act->dta_next) {
10661 10778 if (act->dta_kind == DTRACEACT_COMMIT)
10662 10779 return (EINVAL);
10663 10780 }
10664 10781 }
10665 10782
10666 10783 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
10667 10784 action->dta_rec.dtrd_size = size;
10668 10785 }
10669 10786
10670 10787 action->dta_refcnt = 1;
10671 10788 rec = &action->dta_rec;
10672 10789 size = rec->dtrd_size;
10673 10790
10674 10791 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
10675 10792 if (!(size & mask)) {
10676 10793 align = mask + 1;
10677 10794 break;
10678 10795 }
10679 10796 }
10680 10797
10681 10798 action->dta_kind = desc->dtad_kind;
10682 10799
10683 10800 if ((action->dta_difo = dp) != NULL)
10684 10801 dtrace_difo_hold(dp);
10685 10802
10686 10803 rec->dtrd_action = action->dta_kind;
10687 10804 rec->dtrd_arg = arg;
10688 10805 rec->dtrd_uarg = desc->dtad_uarg;
10689 10806 rec->dtrd_alignment = (uint16_t)align;
10690 10807 rec->dtrd_format = format;
10691 10808
10692 10809 if ((last = ecb->dte_action_last) != NULL) {
10693 10810 ASSERT(ecb->dte_action != NULL);
10694 10811 action->dta_prev = last;
10695 10812 last->dta_next = action;
10696 10813 } else {
10697 10814 ASSERT(ecb->dte_action == NULL);
10698 10815 ecb->dte_action = action;
10699 10816 }
10700 10817
10701 10818 ecb->dte_action_last = action;
10702 10819
10703 10820 return (0);
10704 10821 }
10705 10822
10706 10823 static void
10707 10824 dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
10708 10825 {
10709 10826 dtrace_action_t *act = ecb->dte_action, *next;
10710 10827 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
10711 10828 dtrace_difo_t *dp;
10712 10829 uint16_t format;
10713 10830
10714 10831 if (act != NULL && act->dta_refcnt > 1) {
10715 10832 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
10716 10833 act->dta_refcnt--;
10717 10834 } else {
10718 10835 for (; act != NULL; act = next) {
10719 10836 next = act->dta_next;
10720 10837 ASSERT(next != NULL || act == ecb->dte_action_last);
10721 10838 ASSERT(act->dta_refcnt == 1);
10722 10839
10723 10840 if ((format = act->dta_rec.dtrd_format) != 0)
10724 10841 dtrace_format_remove(ecb->dte_state, format);
10725 10842
10726 10843 if ((dp = act->dta_difo) != NULL)
10727 10844 dtrace_difo_release(dp, vstate);
10728 10845
10729 10846 if (DTRACEACT_ISAGG(act->dta_kind)) {
10730 10847 dtrace_ecb_aggregation_destroy(ecb, act);
10731 10848 } else {
10732 10849 kmem_free(act, sizeof (dtrace_action_t));
10733 10850 }
10734 10851 }
10735 10852 }
10736 10853
10737 10854 ecb->dte_action = NULL;
10738 10855 ecb->dte_action_last = NULL;
10739 10856 ecb->dte_size = 0;
10740 10857 }
10741 10858
10742 10859 static void
10743 10860 dtrace_ecb_disable(dtrace_ecb_t *ecb)
10744 10861 {
10745 10862 /*
10746 10863 * We disable the ECB by removing it from its probe.
10747 10864 */
10748 10865 dtrace_ecb_t *pecb, *prev = NULL;
10749 10866 dtrace_probe_t *probe = ecb->dte_probe;
10750 10867
10751 10868 ASSERT(MUTEX_HELD(&dtrace_lock));
10752 10869
10753 10870 if (probe == NULL) {
10754 10871 /*
10755 10872 * This is the NULL probe; there is nothing to disable.
10756 10873 */
10757 10874 return;
10758 10875 }
10759 10876
10760 10877 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
10761 10878 if (pecb == ecb)
10762 10879 break;
10763 10880 prev = pecb;
10764 10881 }
10765 10882
10766 10883 ASSERT(pecb != NULL);
10767 10884
10768 10885 if (prev == NULL) {
10769 10886 probe->dtpr_ecb = ecb->dte_next;
10770 10887 } else {
10771 10888 prev->dte_next = ecb->dte_next;
10772 10889 }
10773 10890
10774 10891 if (ecb == probe->dtpr_ecb_last) {
10775 10892 ASSERT(ecb->dte_next == NULL);
10776 10893 probe->dtpr_ecb_last = prev;
10777 10894 }
10778 10895
10779 10896 /*
10780 10897 * The ECB has been disconnected from the probe; now sync to assure
10781 10898 * that all CPUs have seen the change before returning.
10782 10899 */
10783 10900 dtrace_sync();
10784 10901
10785 10902 if (probe->dtpr_ecb == NULL) {
10786 10903 /*
10787 10904 * That was the last ECB on the probe; clear the predicate
10788 10905 * cache ID for the probe, disable it and sync one more time
10789 10906 * to assure that we'll never hit it again.
10790 10907 */
10791 10908 dtrace_provider_t *prov = probe->dtpr_provider;
10792 10909
10793 10910 ASSERT(ecb->dte_next == NULL);
10794 10911 ASSERT(probe->dtpr_ecb_last == NULL);
10795 10912 probe->dtpr_predcache = DTRACE_CACHEIDNONE;
10796 10913 prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
10797 10914 probe->dtpr_id, probe->dtpr_arg);
10798 10915 dtrace_sync();
10799 10916 } else {
10800 10917 /*
10801 10918 * There is at least one ECB remaining on the probe. If there
10802 10919 * is _exactly_ one, set the probe's predicate cache ID to be
10803 10920 * the predicate cache ID of the remaining ECB.
10804 10921 */
10805 10922 ASSERT(probe->dtpr_ecb_last != NULL);
10806 10923 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
10807 10924
10808 10925 if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
10809 10926 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
10810 10927
10811 10928 ASSERT(probe->dtpr_ecb->dte_next == NULL);
10812 10929
10813 10930 if (p != NULL)
10814 10931 probe->dtpr_predcache = p->dtp_cacheid;
10815 10932 }
10816 10933
10817 10934 ecb->dte_next = NULL;
10818 10935 }
10819 10936 }
10820 10937
10821 10938 static void
10822 10939 dtrace_ecb_destroy(dtrace_ecb_t *ecb)
10823 10940 {
10824 10941 dtrace_state_t *state = ecb->dte_state;
10825 10942 dtrace_vstate_t *vstate = &state->dts_vstate;
10826 10943 dtrace_predicate_t *pred;
10827 10944 dtrace_epid_t epid = ecb->dte_epid;
10828 10945
10829 10946 ASSERT(MUTEX_HELD(&dtrace_lock));
10830 10947 ASSERT(ecb->dte_next == NULL);
10831 10948 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
10832 10949
10833 10950 if ((pred = ecb->dte_predicate) != NULL)
10834 10951 dtrace_predicate_release(pred, vstate);
10835 10952
10836 10953 dtrace_ecb_action_remove(ecb);
10837 10954
10838 10955 ASSERT(state->dts_ecbs[epid - 1] == ecb);
10839 10956 state->dts_ecbs[epid - 1] = NULL;
10840 10957
10841 10958 kmem_free(ecb, sizeof (dtrace_ecb_t));
10842 10959 }
10843 10960
10844 10961 static dtrace_ecb_t *
10845 10962 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
10846 10963 dtrace_enabling_t *enab)
10847 10964 {
10848 10965 dtrace_ecb_t *ecb;
10849 10966 dtrace_predicate_t *pred;
10850 10967 dtrace_actdesc_t *act;
10851 10968 dtrace_provider_t *prov;
10852 10969 dtrace_ecbdesc_t *desc = enab->dten_current;
10853 10970
10854 10971 ASSERT(MUTEX_HELD(&dtrace_lock));
10855 10972 ASSERT(state != NULL);
10856 10973
10857 10974 ecb = dtrace_ecb_add(state, probe);
10858 10975 ecb->dte_uarg = desc->dted_uarg;
10859 10976
10860 10977 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
10861 10978 dtrace_predicate_hold(pred);
10862 10979 ecb->dte_predicate = pred;
10863 10980 }
10864 10981
10865 10982 if (probe != NULL) {
10866 10983 /*
10867 10984 * If the provider shows more leg than the consumer is old
10868 10985 * enough to see, we need to enable the appropriate implicit
10869 10986 * predicate bits to prevent the ecb from activating at
10870 10987 * revealing times.
10871 10988 *
10872 10989 * Providers specifying DTRACE_PRIV_USER at register time
10873 10990 * are stating that they need the /proc-style privilege
10874 10991 * model to be enforced, and this is what DTRACE_COND_OWNER
10875 10992 * and DTRACE_COND_ZONEOWNER will then do at probe time.
10876 10993 */
10877 10994 prov = probe->dtpr_provider;
10878 10995 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
10879 10996 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
10880 10997 ecb->dte_cond |= DTRACE_COND_OWNER;
10881 10998
10882 10999 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
10883 11000 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
10884 11001 ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
10885 11002
10886 11003 /*
10887 11004 * If the provider shows us kernel innards and the user
10888 11005 * is lacking sufficient privilege, enable the
10889 11006 * DTRACE_COND_USERMODE implicit predicate.
10890 11007 */
10891 11008 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
10892 11009 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
10893 11010 ecb->dte_cond |= DTRACE_COND_USERMODE;
10894 11011 }
10895 11012
10896 11013 if (dtrace_ecb_create_cache != NULL) {
10897 11014 /*
10898 11015 * If we have a cached ecb, we'll use its action list instead
10899 11016 * of creating our own (saving both time and space).
10900 11017 */
10901 11018 dtrace_ecb_t *cached = dtrace_ecb_create_cache;
10902 11019 dtrace_action_t *act = cached->dte_action;
10903 11020
10904 11021 if (act != NULL) {
10905 11022 ASSERT(act->dta_refcnt > 0);
10906 11023 act->dta_refcnt++;
10907 11024 ecb->dte_action = act;
10908 11025 ecb->dte_action_last = cached->dte_action_last;
10909 11026 ecb->dte_needed = cached->dte_needed;
10910 11027 ecb->dte_size = cached->dte_size;
10911 11028 ecb->dte_alignment = cached->dte_alignment;
10912 11029 }
10913 11030
10914 11031 return (ecb);
10915 11032 }
10916 11033
10917 11034 for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
10918 11035 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
10919 11036 dtrace_ecb_destroy(ecb);
10920 11037 return (NULL);
10921 11038 }
10922 11039 }
10923 11040
10924 11041 dtrace_ecb_resize(ecb);
10925 11042
10926 11043 return (dtrace_ecb_create_cache = ecb);
10927 11044 }
10928 11045
10929 11046 static int
10930 11047 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
10931 11048 {
10932 11049 dtrace_ecb_t *ecb;
10933 11050 dtrace_enabling_t *enab = arg;
10934 11051 dtrace_state_t *state = enab->dten_vstate->dtvs_state;
10935 11052
10936 11053 ASSERT(state != NULL);
10937 11054
10938 11055 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
10939 11056 /*
10940 11057 * This probe was created in a generation for which this
10941 11058 * enabling has previously created ECBs; we don't want to
10942 11059 * enable it again, so just kick out.
10943 11060 */
10944 11061 return (DTRACE_MATCH_NEXT);
10945 11062 }
10946 11063
10947 11064 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
10948 11065 return (DTRACE_MATCH_DONE);
10949 11066
10950 11067 if (dtrace_ecb_enable(ecb) < 0)
10951 11068 return (DTRACE_MATCH_FAIL);
10952 11069
10953 11070 return (DTRACE_MATCH_NEXT);
10954 11071 }
10955 11072
10956 11073 static dtrace_ecb_t *
10957 11074 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
10958 11075 {
10959 11076 dtrace_ecb_t *ecb;
10960 11077
10961 11078 ASSERT(MUTEX_HELD(&dtrace_lock));
10962 11079
10963 11080 if (id == 0 || id > state->dts_necbs)
10964 11081 return (NULL);
10965 11082
10966 11083 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
10967 11084 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
10968 11085
10969 11086 return (state->dts_ecbs[id - 1]);
10970 11087 }
10971 11088
10972 11089 static dtrace_aggregation_t *
10973 11090 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
10974 11091 {
10975 11092 dtrace_aggregation_t *agg;
10976 11093
10977 11094 ASSERT(MUTEX_HELD(&dtrace_lock));
10978 11095
10979 11096 if (id == 0 || id > state->dts_naggregations)
10980 11097 return (NULL);
10981 11098
10982 11099 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
10983 11100 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
10984 11101 agg->dtag_id == id);
10985 11102
10986 11103 return (state->dts_aggregations[id - 1]);
10987 11104 }
10988 11105
10989 11106 /*
10990 11107 * DTrace Buffer Functions
10991 11108 *
10992 11109 * The following functions manipulate DTrace buffers. Most of these functions
10993 11110 * are called in the context of establishing or processing consumer state;
10994 11111 * exceptions are explicitly noted.
10995 11112 */
10996 11113
10997 11114 /*
10998 11115 * Note: called from cross call context. This function switches the two
10999 11116 * buffers on a given CPU. The atomicity of this operation is assured by
11000 11117 * disabling interrupts while the actual switch takes place; the disabling of
11001 11118 * interrupts serializes the execution with any execution of dtrace_probe() on
11002 11119 * the same CPU.
11003 11120 */
11004 11121 static void
11005 11122 dtrace_buffer_switch(dtrace_buffer_t *buf)
11006 11123 {
11007 11124 caddr_t tomax = buf->dtb_tomax;
11008 11125 caddr_t xamot = buf->dtb_xamot;
11009 11126 dtrace_icookie_t cookie;
11010 11127 hrtime_t now;
11011 11128
11012 11129 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11013 11130 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
11014 11131
11015 11132 cookie = dtrace_interrupt_disable();
11016 11133 now = dtrace_gethrtime();
11017 11134 buf->dtb_tomax = xamot;
11018 11135 buf->dtb_xamot = tomax;
11019 11136 buf->dtb_xamot_drops = buf->dtb_drops;
11020 11137 buf->dtb_xamot_offset = buf->dtb_offset;
11021 11138 buf->dtb_xamot_errors = buf->dtb_errors;
11022 11139 buf->dtb_xamot_flags = buf->dtb_flags;
11023 11140 buf->dtb_offset = 0;
11024 11141 buf->dtb_drops = 0;
11025 11142 buf->dtb_errors = 0;
11026 11143 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
11027 11144 buf->dtb_interval = now - buf->dtb_switched;
11028 11145 buf->dtb_switched = now;
11029 11146 dtrace_interrupt_enable(cookie);
11030 11147 }
11031 11148
11032 11149 /*
11033 11150 * Note: called from cross call context. This function activates a buffer
11034 11151 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation
11035 11152 * is guaranteed by the disabling of interrupts.
11036 11153 */
11037 11154 static void
11038 11155 dtrace_buffer_activate(dtrace_state_t *state)
11039 11156 {
11040 11157 dtrace_buffer_t *buf;
11041 11158 dtrace_icookie_t cookie = dtrace_interrupt_disable();
11042 11159
11043 11160 buf = &state->dts_buffer[CPU->cpu_id];
11044 11161
11045 11162 if (buf->dtb_tomax != NULL) {
11046 11163 /*
11047 11164 * We might like to assert that the buffer is marked inactive,
11048 11165 * but this isn't necessarily true: the buffer for the CPU
11049 11166 * that processes the BEGIN probe has its buffer activated
11050 11167 * manually. In this case, we take the (harmless) action
11051 11168 * re-clearing the bit INACTIVE bit.
11052 11169 */
11053 11170 buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
11054 11171 }
11055 11172
11056 11173 dtrace_interrupt_enable(cookie);
11057 11174 }
11058 11175
11059 11176 static int
11060 11177 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
11061 11178 processorid_t cpu, int *factor)
11062 11179 {
11063 11180 cpu_t *cp;
11064 11181 dtrace_buffer_t *buf;
11065 11182 int allocated = 0, desired = 0;
11066 11183
11067 11184 ASSERT(MUTEX_HELD(&cpu_lock));
11068 11185 ASSERT(MUTEX_HELD(&dtrace_lock));
11069 11186
11070 11187 *factor = 1;
11071 11188
11072 11189 if (size > dtrace_nonroot_maxsize &&
11073 11190 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
11074 11191 return (EFBIG);
11075 11192
11076 11193 cp = cpu_list;
11077 11194
11078 11195 do {
11079 11196 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
11080 11197 continue;
11081 11198
11082 11199 buf = &bufs[cp->cpu_id];
11083 11200
11084 11201 /*
11085 11202 * If there is already a buffer allocated for this CPU, it
11086 11203 * is only possible that this is a DR event. In this case,
11087 11204 * the buffer size must match our specified size.
11088 11205 */
11089 11206 if (buf->dtb_tomax != NULL) {
11090 11207 ASSERT(buf->dtb_size == size);
11091 11208 continue;
11092 11209 }
11093 11210
11094 11211 ASSERT(buf->dtb_xamot == NULL);
11095 11212
11096 11213 if ((buf->dtb_tomax = kmem_zalloc(size,
11097 11214 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
11098 11215 goto err;
11099 11216
11100 11217 buf->dtb_size = size;
11101 11218 buf->dtb_flags = flags;
11102 11219 buf->dtb_offset = 0;
11103 11220 buf->dtb_drops = 0;
11104 11221
11105 11222 if (flags & DTRACEBUF_NOSWITCH)
11106 11223 continue;
11107 11224
11108 11225 if ((buf->dtb_xamot = kmem_zalloc(size,
11109 11226 KM_NOSLEEP | KM_NORMALPRI)) == NULL)
11110 11227 goto err;
11111 11228 } while ((cp = cp->cpu_next) != cpu_list);
11112 11229
11113 11230 return (0);
11114 11231
11115 11232 err:
11116 11233 cp = cpu_list;
11117 11234
11118 11235 do {
11119 11236 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
11120 11237 continue;
11121 11238
11122 11239 buf = &bufs[cp->cpu_id];
11123 11240 desired += 2;
11124 11241
11125 11242 if (buf->dtb_xamot != NULL) {
11126 11243 ASSERT(buf->dtb_tomax != NULL);
11127 11244 ASSERT(buf->dtb_size == size);
11128 11245 kmem_free(buf->dtb_xamot, size);
11129 11246 allocated++;
11130 11247 }
11131 11248
11132 11249 if (buf->dtb_tomax != NULL) {
11133 11250 ASSERT(buf->dtb_size == size);
11134 11251 kmem_free(buf->dtb_tomax, size);
11135 11252 allocated++;
11136 11253 }
11137 11254
11138 11255 buf->dtb_tomax = NULL;
11139 11256 buf->dtb_xamot = NULL;
11140 11257 buf->dtb_size = 0;
11141 11258 } while ((cp = cp->cpu_next) != cpu_list);
11142 11259
11143 11260 *factor = desired / (allocated > 0 ? allocated : 1);
11144 11261
11145 11262 return (ENOMEM);
11146 11263 }
11147 11264
11148 11265 /*
11149 11266 * Note: called from probe context. This function just increments the drop
11150 11267 * count on a buffer. It has been made a function to allow for the
11151 11268 * possibility of understanding the source of mysterious drop counts. (A
11152 11269 * problem for which one may be particularly disappointed that DTrace cannot
11153 11270 * be used to understand DTrace.)
11154 11271 */
11155 11272 static void
11156 11273 dtrace_buffer_drop(dtrace_buffer_t *buf)
11157 11274 {
11158 11275 buf->dtb_drops++;
11159 11276 }
11160 11277
11161 11278 /*
11162 11279 * Note: called from probe context. This function is called to reserve space
11163 11280 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the
11164 11281 * mstate. Returns the new offset in the buffer, or a negative value if an
11165 11282 * error has occurred.
11166 11283 */
11167 11284 static intptr_t
11168 11285 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
11169 11286 dtrace_state_t *state, dtrace_mstate_t *mstate)
11170 11287 {
11171 11288 intptr_t offs = buf->dtb_offset, soffs;
11172 11289 intptr_t woffs;
11173 11290 caddr_t tomax;
11174 11291 size_t total;
11175 11292
11176 11293 if (buf->dtb_flags & DTRACEBUF_INACTIVE)
11177 11294 return (-1);
11178 11295
11179 11296 if ((tomax = buf->dtb_tomax) == NULL) {
11180 11297 dtrace_buffer_drop(buf);
11181 11298 return (-1);
11182 11299 }
11183 11300
11184 11301 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
11185 11302 while (offs & (align - 1)) {
11186 11303 /*
11187 11304 * Assert that our alignment is off by a number which
11188 11305 * is itself sizeof (uint32_t) aligned.
11189 11306 */
11190 11307 ASSERT(!((align - (offs & (align - 1))) &
11191 11308 (sizeof (uint32_t) - 1)));
11192 11309 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
11193 11310 offs += sizeof (uint32_t);
11194 11311 }
11195 11312
11196 11313 if ((soffs = offs + needed) > buf->dtb_size) {
11197 11314 dtrace_buffer_drop(buf);
11198 11315 return (-1);
11199 11316 }
11200 11317
11201 11318 if (mstate == NULL)
11202 11319 return (offs);
11203 11320
11204 11321 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
11205 11322 mstate->dtms_scratch_size = buf->dtb_size - soffs;
11206 11323 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
11207 11324
11208 11325 return (offs);
11209 11326 }
11210 11327
11211 11328 if (buf->dtb_flags & DTRACEBUF_FILL) {
11212 11329 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
11213 11330 (buf->dtb_flags & DTRACEBUF_FULL))
11214 11331 return (-1);
11215 11332 goto out;
11216 11333 }
11217 11334
11218 11335 total = needed + (offs & (align - 1));
11219 11336
11220 11337 /*
11221 11338 * For a ring buffer, life is quite a bit more complicated. Before
11222 11339 * we can store any padding, we need to adjust our wrapping offset.
11223 11340 * (If we've never before wrapped or we're not about to, no adjustment
11224 11341 * is required.)
11225 11342 */
11226 11343 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
11227 11344 offs + total > buf->dtb_size) {
11228 11345 woffs = buf->dtb_xamot_offset;
11229 11346
11230 11347 if (offs + total > buf->dtb_size) {
11231 11348 /*
11232 11349 * We can't fit in the end of the buffer. First, a
11233 11350 * sanity check that we can fit in the buffer at all.
11234 11351 */
11235 11352 if (total > buf->dtb_size) {
11236 11353 dtrace_buffer_drop(buf);
11237 11354 return (-1);
11238 11355 }
11239 11356
11240 11357 /*
11241 11358 * We're going to be storing at the top of the buffer,
11242 11359 * so now we need to deal with the wrapped offset. We
11243 11360 * only reset our wrapped offset to 0 if it is
11244 11361 * currently greater than the current offset. If it
11245 11362 * is less than the current offset, it is because a
11246 11363 * previous allocation induced a wrap -- but the
11247 11364 * allocation didn't subsequently take the space due
11248 11365 * to an error or false predicate evaluation. In this
11249 11366 * case, we'll just leave the wrapped offset alone: if
11250 11367 * the wrapped offset hasn't been advanced far enough
11251 11368 * for this allocation, it will be adjusted in the
11252 11369 * lower loop.
11253 11370 */
11254 11371 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
11255 11372 if (woffs >= offs)
11256 11373 woffs = 0;
11257 11374 } else {
11258 11375 woffs = 0;
11259 11376 }
11260 11377
11261 11378 /*
11262 11379 * Now we know that we're going to be storing to the
11263 11380 * top of the buffer and that there is room for us
11264 11381 * there. We need to clear the buffer from the current
11265 11382 * offset to the end (there may be old gunk there).
11266 11383 */
11267 11384 while (offs < buf->dtb_size)
11268 11385 tomax[offs++] = 0;
11269 11386
11270 11387 /*
11271 11388 * We need to set our offset to zero. And because we
11272 11389 * are wrapping, we need to set the bit indicating as
11273 11390 * much. We can also adjust our needed space back
11274 11391 * down to the space required by the ECB -- we know
11275 11392 * that the top of the buffer is aligned.
11276 11393 */
11277 11394 offs = 0;
11278 11395 total = needed;
11279 11396 buf->dtb_flags |= DTRACEBUF_WRAPPED;
11280 11397 } else {
11281 11398 /*
11282 11399 * There is room for us in the buffer, so we simply
11283 11400 * need to check the wrapped offset.
11284 11401 */
11285 11402 if (woffs < offs) {
11286 11403 /*
11287 11404 * The wrapped offset is less than the offset.
11288 11405 * This can happen if we allocated buffer space
11289 11406 * that induced a wrap, but then we didn't
11290 11407 * subsequently take the space due to an error
11291 11408 * or false predicate evaluation. This is
11292 11409 * okay; we know that _this_ allocation isn't
11293 11410 * going to induce a wrap. We still can't
11294 11411 * reset the wrapped offset to be zero,
11295 11412 * however: the space may have been trashed in
11296 11413 * the previous failed probe attempt. But at
11297 11414 * least the wrapped offset doesn't need to
11298 11415 * be adjusted at all...
11299 11416 */
11300 11417 goto out;
11301 11418 }
11302 11419 }
11303 11420
11304 11421 while (offs + total > woffs) {
11305 11422 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
11306 11423 size_t size;
11307 11424
11308 11425 if (epid == DTRACE_EPIDNONE) {
11309 11426 size = sizeof (uint32_t);
11310 11427 } else {
11311 11428 ASSERT3U(epid, <=, state->dts_necbs);
11312 11429 ASSERT(state->dts_ecbs[epid - 1] != NULL);
11313 11430
11314 11431 size = state->dts_ecbs[epid - 1]->dte_size;
11315 11432 }
11316 11433
11317 11434 ASSERT(woffs + size <= buf->dtb_size);
11318 11435 ASSERT(size != 0);
11319 11436
11320 11437 if (woffs + size == buf->dtb_size) {
11321 11438 /*
11322 11439 * We've reached the end of the buffer; we want
11323 11440 * to set the wrapped offset to 0 and break
11324 11441 * out. However, if the offs is 0, then we're
11325 11442 * in a strange edge-condition: the amount of
11326 11443 * space that we want to reserve plus the size
11327 11444 * of the record that we're overwriting is
11328 11445 * greater than the size of the buffer. This
11329 11446 * is problematic because if we reserve the
11330 11447 * space but subsequently don't consume it (due
11331 11448 * to a failed predicate or error) the wrapped
11332 11449 * offset will be 0 -- yet the EPID at offset 0
11333 11450 * will not be committed. This situation is
11334 11451 * relatively easy to deal with: if we're in
11335 11452 * this case, the buffer is indistinguishable
11336 11453 * from one that hasn't wrapped; we need only
11337 11454 * finish the job by clearing the wrapped bit,
11338 11455 * explicitly setting the offset to be 0, and
11339 11456 * zero'ing out the old data in the buffer.
11340 11457 */
11341 11458 if (offs == 0) {
11342 11459 buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
11343 11460 buf->dtb_offset = 0;
11344 11461 woffs = total;
11345 11462
11346 11463 while (woffs < buf->dtb_size)
11347 11464 tomax[woffs++] = 0;
11348 11465 }
11349 11466
11350 11467 woffs = 0;
11351 11468 break;
11352 11469 }
11353 11470
11354 11471 woffs += size;
11355 11472 }
11356 11473
11357 11474 /*
11358 11475 * We have a wrapped offset. It may be that the wrapped offset
11359 11476 * has become zero -- that's okay.
11360 11477 */
11361 11478 buf->dtb_xamot_offset = woffs;
11362 11479 }
11363 11480
11364 11481 out:
11365 11482 /*
11366 11483 * Now we can plow the buffer with any necessary padding.
11367 11484 */
11368 11485 while (offs & (align - 1)) {
11369 11486 /*
11370 11487 * Assert that our alignment is off by a number which
11371 11488 * is itself sizeof (uint32_t) aligned.
11372 11489 */
11373 11490 ASSERT(!((align - (offs & (align - 1))) &
11374 11491 (sizeof (uint32_t) - 1)));
11375 11492 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
11376 11493 offs += sizeof (uint32_t);
11377 11494 }
11378 11495
11379 11496 if (buf->dtb_flags & DTRACEBUF_FILL) {
11380 11497 if (offs + needed > buf->dtb_size - state->dts_reserve) {
11381 11498 buf->dtb_flags |= DTRACEBUF_FULL;
11382 11499 return (-1);
11383 11500 }
11384 11501 }
11385 11502
11386 11503 if (mstate == NULL)
11387 11504 return (offs);
11388 11505
11389 11506 /*
11390 11507 * For ring buffers and fill buffers, the scratch space is always
11391 11508 * the inactive buffer.
11392 11509 */
11393 11510 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
11394 11511 mstate->dtms_scratch_size = buf->dtb_size;
11395 11512 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
11396 11513
11397 11514 return (offs);
11398 11515 }
11399 11516
11400 11517 static void
11401 11518 dtrace_buffer_polish(dtrace_buffer_t *buf)
11402 11519 {
11403 11520 ASSERT(buf->dtb_flags & DTRACEBUF_RING);
11404 11521 ASSERT(MUTEX_HELD(&dtrace_lock));
11405 11522
11406 11523 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
11407 11524 return;
11408 11525
11409 11526 /*
11410 11527 * We need to polish the ring buffer. There are three cases:
11411 11528 *
11412 11529 * - The first (and presumably most common) is that there is no gap
11413 11530 * between the buffer offset and the wrapped offset. In this case,
11414 11531 * there is nothing in the buffer that isn't valid data; we can
11415 11532 * mark the buffer as polished and return.
11416 11533 *
11417 11534 * - The second (less common than the first but still more common
11418 11535 * than the third) is that there is a gap between the buffer offset
11419 11536 * and the wrapped offset, and the wrapped offset is larger than the
11420 11537 * buffer offset. This can happen because of an alignment issue, or
11421 11538 * can happen because of a call to dtrace_buffer_reserve() that
11422 11539 * didn't subsequently consume the buffer space. In this case,
11423 11540 * we need to zero the data from the buffer offset to the wrapped
11424 11541 * offset.
11425 11542 *
11426 11543 * - The third (and least common) is that there is a gap between the
11427 11544 * buffer offset and the wrapped offset, but the wrapped offset is
11428 11545 * _less_ than the buffer offset. This can only happen because a
11429 11546 * call to dtrace_buffer_reserve() induced a wrap, but the space
11430 11547 * was not subsequently consumed. In this case, we need to zero the
11431 11548 * space from the offset to the end of the buffer _and_ from the
11432 11549 * top of the buffer to the wrapped offset.
11433 11550 */
11434 11551 if (buf->dtb_offset < buf->dtb_xamot_offset) {
11435 11552 bzero(buf->dtb_tomax + buf->dtb_offset,
11436 11553 buf->dtb_xamot_offset - buf->dtb_offset);
11437 11554 }
11438 11555
11439 11556 if (buf->dtb_offset > buf->dtb_xamot_offset) {
11440 11557 bzero(buf->dtb_tomax + buf->dtb_offset,
11441 11558 buf->dtb_size - buf->dtb_offset);
11442 11559 bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
11443 11560 }
11444 11561 }
11445 11562
11446 11563 /*
11447 11564 * This routine determines if data generated at the specified time has likely
11448 11565 * been entirely consumed at user-level. This routine is called to determine
11449 11566 * if an ECB on a defunct probe (but for an active enabling) can be safely
11450 11567 * disabled and destroyed.
11451 11568 */
11452 11569 static int
11453 11570 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when)
11454 11571 {
11455 11572 int i;
11456 11573
11457 11574 for (i = 0; i < NCPU; i++) {
11458 11575 dtrace_buffer_t *buf = &bufs[i];
11459 11576
11460 11577 if (buf->dtb_size == 0)
11461 11578 continue;
11462 11579
11463 11580 if (buf->dtb_flags & DTRACEBUF_RING)
11464 11581 return (0);
11465 11582
11466 11583 if (!buf->dtb_switched && buf->dtb_offset != 0)
11467 11584 return (0);
11468 11585
11469 11586 if (buf->dtb_switched - buf->dtb_interval < when)
11470 11587 return (0);
11471 11588 }
11472 11589
11473 11590 return (1);
11474 11591 }
11475 11592
11476 11593 static void
11477 11594 dtrace_buffer_free(dtrace_buffer_t *bufs)
11478 11595 {
11479 11596 int i;
11480 11597
11481 11598 for (i = 0; i < NCPU; i++) {
11482 11599 dtrace_buffer_t *buf = &bufs[i];
11483 11600
11484 11601 if (buf->dtb_tomax == NULL) {
11485 11602 ASSERT(buf->dtb_xamot == NULL);
11486 11603 ASSERT(buf->dtb_size == 0);
11487 11604 continue;
11488 11605 }
11489 11606
11490 11607 if (buf->dtb_xamot != NULL) {
11491 11608 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11492 11609 kmem_free(buf->dtb_xamot, buf->dtb_size);
11493 11610 }
11494 11611
11495 11612 kmem_free(buf->dtb_tomax, buf->dtb_size);
11496 11613 buf->dtb_size = 0;
11497 11614 buf->dtb_tomax = NULL;
11498 11615 buf->dtb_xamot = NULL;
11499 11616 }
11500 11617 }
11501 11618
11502 11619 /*
11503 11620 * DTrace Enabling Functions
11504 11621 */
11505 11622 static dtrace_enabling_t *
11506 11623 dtrace_enabling_create(dtrace_vstate_t *vstate)
11507 11624 {
11508 11625 dtrace_enabling_t *enab;
11509 11626
11510 11627 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
11511 11628 enab->dten_vstate = vstate;
11512 11629
11513 11630 return (enab);
11514 11631 }
11515 11632
11516 11633 static void
11517 11634 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
11518 11635 {
11519 11636 dtrace_ecbdesc_t **ndesc;
11520 11637 size_t osize, nsize;
11521 11638
11522 11639 /*
11523 11640 * We can't add to enablings after we've enabled them, or after we've
11524 11641 * retained them.
11525 11642 */
11526 11643 ASSERT(enab->dten_probegen == 0);
11527 11644 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11528 11645
11529 11646 if (enab->dten_ndesc < enab->dten_maxdesc) {
11530 11647 enab->dten_desc[enab->dten_ndesc++] = ecb;
11531 11648 return;
11532 11649 }
11533 11650
11534 11651 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11535 11652
11536 11653 if (enab->dten_maxdesc == 0) {
11537 11654 enab->dten_maxdesc = 1;
11538 11655 } else {
11539 11656 enab->dten_maxdesc <<= 1;
11540 11657 }
11541 11658
11542 11659 ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
11543 11660
11544 11661 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
11545 11662 ndesc = kmem_zalloc(nsize, KM_SLEEP);
11546 11663 bcopy(enab->dten_desc, ndesc, osize);
11547 11664 kmem_free(enab->dten_desc, osize);
11548 11665
11549 11666 enab->dten_desc = ndesc;
11550 11667 enab->dten_desc[enab->dten_ndesc++] = ecb;
11551 11668 }
11552 11669
11553 11670 static void
11554 11671 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
11555 11672 dtrace_probedesc_t *pd)
11556 11673 {
11557 11674 dtrace_ecbdesc_t *new;
11558 11675 dtrace_predicate_t *pred;
11559 11676 dtrace_actdesc_t *act;
11560 11677
11561 11678 /*
11562 11679 * We're going to create a new ECB description that matches the
11563 11680 * specified ECB in every way, but has the specified probe description.
11564 11681 */
11565 11682 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
11566 11683
11567 11684 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
11568 11685 dtrace_predicate_hold(pred);
11569 11686
11570 11687 for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
11571 11688 dtrace_actdesc_hold(act);
11572 11689
11573 11690 new->dted_action = ecb->dted_action;
11574 11691 new->dted_pred = ecb->dted_pred;
11575 11692 new->dted_probe = *pd;
11576 11693 new->dted_uarg = ecb->dted_uarg;
11577 11694
11578 11695 dtrace_enabling_add(enab, new);
11579 11696 }
11580 11697
11581 11698 static void
11582 11699 dtrace_enabling_dump(dtrace_enabling_t *enab)
11583 11700 {
11584 11701 int i;
11585 11702
11586 11703 for (i = 0; i < enab->dten_ndesc; i++) {
11587 11704 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
11588 11705
11589 11706 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
11590 11707 desc->dtpd_provider, desc->dtpd_mod,
11591 11708 desc->dtpd_func, desc->dtpd_name);
11592 11709 }
11593 11710 }
11594 11711
11595 11712 static void
11596 11713 dtrace_enabling_destroy(dtrace_enabling_t *enab)
11597 11714 {
11598 11715 int i;
11599 11716 dtrace_ecbdesc_t *ep;
11600 11717 dtrace_vstate_t *vstate = enab->dten_vstate;
11601 11718
11602 11719 ASSERT(MUTEX_HELD(&dtrace_lock));
11603 11720
11604 11721 for (i = 0; i < enab->dten_ndesc; i++) {
11605 11722 dtrace_actdesc_t *act, *next;
11606 11723 dtrace_predicate_t *pred;
11607 11724
11608 11725 ep = enab->dten_desc[i];
11609 11726
11610 11727 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
11611 11728 dtrace_predicate_release(pred, vstate);
11612 11729
11613 11730 for (act = ep->dted_action; act != NULL; act = next) {
11614 11731 next = act->dtad_next;
11615 11732 dtrace_actdesc_release(act, vstate);
11616 11733 }
11617 11734
11618 11735 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
11619 11736 }
11620 11737
11621 11738 kmem_free(enab->dten_desc,
11622 11739 enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
11623 11740
11624 11741 /*
11625 11742 * If this was a retained enabling, decrement the dts_nretained count
11626 11743 * and take it off of the dtrace_retained list.
11627 11744 */
11628 11745 if (enab->dten_prev != NULL || enab->dten_next != NULL ||
11629 11746 dtrace_retained == enab) {
11630 11747 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11631 11748 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
11632 11749 enab->dten_vstate->dtvs_state->dts_nretained--;
11633 11750 dtrace_retained_gen++;
11634 11751 }
11635 11752
11636 11753 if (enab->dten_prev == NULL) {
11637 11754 if (dtrace_retained == enab) {
11638 11755 dtrace_retained = enab->dten_next;
11639 11756
11640 11757 if (dtrace_retained != NULL)
11641 11758 dtrace_retained->dten_prev = NULL;
11642 11759 }
11643 11760 } else {
11644 11761 ASSERT(enab != dtrace_retained);
11645 11762 ASSERT(dtrace_retained != NULL);
11646 11763 enab->dten_prev->dten_next = enab->dten_next;
11647 11764 }
11648 11765
11649 11766 if (enab->dten_next != NULL) {
11650 11767 ASSERT(dtrace_retained != NULL);
11651 11768 enab->dten_next->dten_prev = enab->dten_prev;
11652 11769 }
11653 11770
11654 11771 kmem_free(enab, sizeof (dtrace_enabling_t));
11655 11772 }
11656 11773
11657 11774 static int
11658 11775 dtrace_enabling_retain(dtrace_enabling_t *enab)
11659 11776 {
11660 11777 dtrace_state_t *state;
11661 11778
11662 11779 ASSERT(MUTEX_HELD(&dtrace_lock));
11663 11780 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
11664 11781 ASSERT(enab->dten_vstate != NULL);
11665 11782
11666 11783 state = enab->dten_vstate->dtvs_state;
11667 11784 ASSERT(state != NULL);
11668 11785
11669 11786 /*
11670 11787 * We only allow each state to retain dtrace_retain_max enablings.
11671 11788 */
11672 11789 if (state->dts_nretained >= dtrace_retain_max)
11673 11790 return (ENOSPC);
11674 11791
11675 11792 state->dts_nretained++;
11676 11793 dtrace_retained_gen++;
11677 11794
11678 11795 if (dtrace_retained == NULL) {
11679 11796 dtrace_retained = enab;
11680 11797 return (0);
11681 11798 }
11682 11799
11683 11800 enab->dten_next = dtrace_retained;
11684 11801 dtrace_retained->dten_prev = enab;
11685 11802 dtrace_retained = enab;
11686 11803
11687 11804 return (0);
11688 11805 }
11689 11806
11690 11807 static int
11691 11808 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
11692 11809 dtrace_probedesc_t *create)
11693 11810 {
11694 11811 dtrace_enabling_t *new, *enab;
11695 11812 int found = 0, err = ENOENT;
11696 11813
11697 11814 ASSERT(MUTEX_HELD(&dtrace_lock));
11698 11815 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
11699 11816 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
11700 11817 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
11701 11818 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
11702 11819
11703 11820 new = dtrace_enabling_create(&state->dts_vstate);
11704 11821
11705 11822 /*
11706 11823 * Iterate over all retained enablings, looking for enablings that
11707 11824 * match the specified state.
11708 11825 */
11709 11826 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11710 11827 int i;
11711 11828
11712 11829 /*
11713 11830 * dtvs_state can only be NULL for helper enablings -- and
11714 11831 * helper enablings can't be retained.
11715 11832 */
11716 11833 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11717 11834
11718 11835 if (enab->dten_vstate->dtvs_state != state)
11719 11836 continue;
11720 11837
11721 11838 /*
11722 11839 * Now iterate over each probe description; we're looking for
11723 11840 * an exact match to the specified probe description.
11724 11841 */
11725 11842 for (i = 0; i < enab->dten_ndesc; i++) {
11726 11843 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11727 11844 dtrace_probedesc_t *pd = &ep->dted_probe;
11728 11845
11729 11846 if (strcmp(pd->dtpd_provider, match->dtpd_provider))
11730 11847 continue;
11731 11848
11732 11849 if (strcmp(pd->dtpd_mod, match->dtpd_mod))
11733 11850 continue;
11734 11851
11735 11852 if (strcmp(pd->dtpd_func, match->dtpd_func))
11736 11853 continue;
11737 11854
11738 11855 if (strcmp(pd->dtpd_name, match->dtpd_name))
11739 11856 continue;
11740 11857
11741 11858 /*
11742 11859 * We have a winning probe! Add it to our growing
11743 11860 * enabling.
11744 11861 */
11745 11862 found = 1;
11746 11863 dtrace_enabling_addlike(new, ep, create);
11747 11864 }
11748 11865 }
11749 11866
11750 11867 if (!found || (err = dtrace_enabling_retain(new)) != 0) {
11751 11868 dtrace_enabling_destroy(new);
11752 11869 return (err);
11753 11870 }
11754 11871
11755 11872 return (0);
11756 11873 }
11757 11874
11758 11875 static void
11759 11876 dtrace_enabling_retract(dtrace_state_t *state)
11760 11877 {
11761 11878 dtrace_enabling_t *enab, *next;
11762 11879
11763 11880 ASSERT(MUTEX_HELD(&dtrace_lock));
11764 11881
11765 11882 /*
11766 11883 * Iterate over all retained enablings, destroy the enablings retained
11767 11884 * for the specified state.
11768 11885 */
11769 11886 for (enab = dtrace_retained; enab != NULL; enab = next) {
11770 11887 next = enab->dten_next;
11771 11888
11772 11889 /*
11773 11890 * dtvs_state can only be NULL for helper enablings -- and
11774 11891 * helper enablings can't be retained.
11775 11892 */
11776 11893 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11777 11894
11778 11895 if (enab->dten_vstate->dtvs_state == state) {
11779 11896 ASSERT(state->dts_nretained > 0);
11780 11897 dtrace_enabling_destroy(enab);
11781 11898 }
11782 11899 }
11783 11900
11784 11901 ASSERT(state->dts_nretained == 0);
11785 11902 }
11786 11903
11787 11904 static int
11788 11905 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
11789 11906 {
11790 11907 int i = 0;
11791 11908 int total_matched = 0, matched = 0;
11792 11909
11793 11910 ASSERT(MUTEX_HELD(&cpu_lock));
11794 11911 ASSERT(MUTEX_HELD(&dtrace_lock));
11795 11912
11796 11913 for (i = 0; i < enab->dten_ndesc; i++) {
11797 11914 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
11798 11915
11799 11916 enab->dten_current = ep;
11800 11917 enab->dten_error = 0;
11801 11918
11802 11919 /*
11803 11920 * If a provider failed to enable a probe then get out and
11804 11921 * let the consumer know we failed.
11805 11922 */
11806 11923 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0)
11807 11924 return (EBUSY);
11808 11925
11809 11926 total_matched += matched;
11810 11927
11811 11928 if (enab->dten_error != 0) {
11812 11929 /*
11813 11930 * If we get an error half-way through enabling the
11814 11931 * probes, we kick out -- perhaps with some number of
11815 11932 * them enabled. Leaving enabled probes enabled may
11816 11933 * be slightly confusing for user-level, but we expect
11817 11934 * that no one will attempt to actually drive on in
11818 11935 * the face of such errors. If this is an anonymous
11819 11936 * enabling (indicated with a NULL nmatched pointer),
11820 11937 * we cmn_err() a message. We aren't expecting to
11821 11938 * get such an error -- such as it can exist at all,
11822 11939 * it would be a result of corrupted DOF in the driver
11823 11940 * properties.
11824 11941 */
11825 11942 if (nmatched == NULL) {
11826 11943 cmn_err(CE_WARN, "dtrace_enabling_match() "
11827 11944 "error on %p: %d", (void *)ep,
11828 11945 enab->dten_error);
11829 11946 }
11830 11947
11831 11948 return (enab->dten_error);
11832 11949 }
11833 11950 }
11834 11951
11835 11952 enab->dten_probegen = dtrace_probegen;
11836 11953 if (nmatched != NULL)
11837 11954 *nmatched = total_matched;
11838 11955
11839 11956 return (0);
11840 11957 }
11841 11958
11842 11959 static void
11843 11960 dtrace_enabling_matchall(void)
11844 11961 {
11845 11962 dtrace_enabling_t *enab;
11846 11963
11847 11964 mutex_enter(&cpu_lock);
11848 11965 mutex_enter(&dtrace_lock);
11849 11966
11850 11967 /*
11851 11968 * Iterate over all retained enablings to see if any probes match
11852 11969 * against them. We only perform this operation on enablings for which
11853 11970 * we have sufficient permissions by virtue of being in the global zone
11854 11971 * or in the same zone as the DTrace client. Because we can be called
11855 11972 * after dtrace_detach() has been called, we cannot assert that there
11856 11973 * are retained enablings. We can safely load from dtrace_retained,
11857 11974 * however: the taskq_destroy() at the end of dtrace_detach() will
11858 11975 * block pending our completion.
11859 11976 */
11860 11977 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11861 11978 dtrace_cred_t *dcr = &enab->dten_vstate->dtvs_state->dts_cred;
11862 11979 cred_t *cr = dcr->dcr_cred;
11863 11980 zoneid_t zone = cr != NULL ? crgetzoneid(cr) : 0;
11864 11981
11865 11982 if ((dcr->dcr_visible & DTRACE_CRV_ALLZONE) || (cr != NULL &&
11866 11983 (zone == GLOBAL_ZONEID || getzoneid() == zone)))
11867 11984 (void) dtrace_enabling_match(enab, NULL);
11868 11985 }
11869 11986
11870 11987 mutex_exit(&dtrace_lock);
11871 11988 mutex_exit(&cpu_lock);
11872 11989 }
11873 11990
11874 11991 /*
11875 11992 * If an enabling is to be enabled without having matched probes (that is, if
11876 11993 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
11877 11994 * enabling must be _primed_ by creating an ECB for every ECB description.
11878 11995 * This must be done to assure that we know the number of speculations, the
11879 11996 * number of aggregations, the minimum buffer size needed, etc. before we
11880 11997 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually
11881 11998 * enabling any probes, we create ECBs for every ECB decription, but with a
11882 11999 * NULL probe -- which is exactly what this function does.
11883 12000 */
11884 12001 static void
11885 12002 dtrace_enabling_prime(dtrace_state_t *state)
11886 12003 {
11887 12004 dtrace_enabling_t *enab;
11888 12005 int i;
11889 12006
11890 12007 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
11891 12008 ASSERT(enab->dten_vstate->dtvs_state != NULL);
11892 12009
11893 12010 if (enab->dten_vstate->dtvs_state != state)
11894 12011 continue;
11895 12012
11896 12013 /*
11897 12014 * We don't want to prime an enabling more than once, lest
11898 12015 * we allow a malicious user to induce resource exhaustion.
11899 12016 * (The ECBs that result from priming an enabling aren't
11900 12017 * leaked -- but they also aren't deallocated until the
11901 12018 * consumer state is destroyed.)
11902 12019 */
11903 12020 if (enab->dten_primed)
11904 12021 continue;
11905 12022
11906 12023 for (i = 0; i < enab->dten_ndesc; i++) {
11907 12024 enab->dten_current = enab->dten_desc[i];
11908 12025 (void) dtrace_probe_enable(NULL, enab);
11909 12026 }
11910 12027
11911 12028 enab->dten_primed = 1;
11912 12029 }
11913 12030 }
11914 12031
11915 12032 /*
11916 12033 * Called to indicate that probes should be provided due to retained
11917 12034 * enablings. This is implemented in terms of dtrace_probe_provide(), but it
11918 12035 * must take an initial lap through the enabling calling the dtps_provide()
11919 12036 * entry point explicitly to allow for autocreated probes.
11920 12037 */
11921 12038 static void
11922 12039 dtrace_enabling_provide(dtrace_provider_t *prv)
11923 12040 {
11924 12041 int i, all = 0;
11925 12042 dtrace_probedesc_t desc;
11926 12043 dtrace_genid_t gen;
11927 12044
11928 12045 ASSERT(MUTEX_HELD(&dtrace_lock));
11929 12046 ASSERT(MUTEX_HELD(&dtrace_provider_lock));
11930 12047
11931 12048 if (prv == NULL) {
11932 12049 all = 1;
11933 12050 prv = dtrace_provider;
11934 12051 }
11935 12052
11936 12053 do {
11937 12054 dtrace_enabling_t *enab;
11938 12055 void *parg = prv->dtpv_arg;
11939 12056
11940 12057 retry:
11941 12058 gen = dtrace_retained_gen;
11942 12059 for (enab = dtrace_retained; enab != NULL;
11943 12060 enab = enab->dten_next) {
11944 12061 for (i = 0; i < enab->dten_ndesc; i++) {
11945 12062 desc = enab->dten_desc[i]->dted_probe;
11946 12063 mutex_exit(&dtrace_lock);
11947 12064 prv->dtpv_pops.dtps_provide(parg, &desc);
11948 12065 mutex_enter(&dtrace_lock);
11949 12066 /*
11950 12067 * Process the retained enablings again if
11951 12068 * they have changed while we weren't holding
11952 12069 * dtrace_lock.
11953 12070 */
11954 12071 if (gen != dtrace_retained_gen)
11955 12072 goto retry;
11956 12073 }
11957 12074 }
11958 12075 } while (all && (prv = prv->dtpv_next) != NULL);
11959 12076
11960 12077 mutex_exit(&dtrace_lock);
11961 12078 dtrace_probe_provide(NULL, all ? NULL : prv);
11962 12079 mutex_enter(&dtrace_lock);
11963 12080 }
11964 12081
11965 12082 /*
11966 12083 * Called to reap ECBs that are attached to probes from defunct providers.
11967 12084 */
11968 12085 static void
11969 12086 dtrace_enabling_reap(void)
11970 12087 {
11971 12088 dtrace_provider_t *prov;
11972 12089 dtrace_probe_t *probe;
11973 12090 dtrace_ecb_t *ecb;
11974 12091 hrtime_t when;
11975 12092 int i;
11976 12093
11977 12094 mutex_enter(&cpu_lock);
11978 12095 mutex_enter(&dtrace_lock);
11979 12096
11980 12097 for (i = 0; i < dtrace_nprobes; i++) {
11981 12098 if ((probe = dtrace_probes[i]) == NULL)
11982 12099 continue;
11983 12100
11984 12101 if (probe->dtpr_ecb == NULL)
11985 12102 continue;
11986 12103
11987 12104 prov = probe->dtpr_provider;
11988 12105
11989 12106 if ((when = prov->dtpv_defunct) == 0)
11990 12107 continue;
11991 12108
11992 12109 /*
11993 12110 * We have ECBs on a defunct provider: we want to reap these
11994 12111 * ECBs to allow the provider to unregister. The destruction
11995 12112 * of these ECBs must be done carefully: if we destroy the ECB
11996 12113 * and the consumer later wishes to consume an EPID that
11997 12114 * corresponds to the destroyed ECB (and if the EPID metadata
11998 12115 * has not been previously consumed), the consumer will abort
11999 12116 * processing on the unknown EPID. To reduce (but not, sadly,
12000 12117 * eliminate) the possibility of this, we will only destroy an
12001 12118 * ECB for a defunct provider if, for the state that
12002 12119 * corresponds to the ECB:
12003 12120 *
12004 12121 * (a) There is no speculative tracing (which can effectively
12005 12122 * cache an EPID for an arbitrary amount of time).
12006 12123 *
12007 12124 * (b) The principal buffers have been switched twice since the
12008 12125 * provider became defunct.
12009 12126 *
12010 12127 * (c) The aggregation buffers are of zero size or have been
12011 12128 * switched twice since the provider became defunct.
12012 12129 *
12013 12130 * We use dts_speculates to determine (a) and call a function
12014 12131 * (dtrace_buffer_consumed()) to determine (b) and (c). Note
12015 12132 * that as soon as we've been unable to destroy one of the ECBs
12016 12133 * associated with the probe, we quit trying -- reaping is only
12017 12134 * fruitful in as much as we can destroy all ECBs associated
12018 12135 * with the defunct provider's probes.
12019 12136 */
12020 12137 while ((ecb = probe->dtpr_ecb) != NULL) {
12021 12138 dtrace_state_t *state = ecb->dte_state;
12022 12139 dtrace_buffer_t *buf = state->dts_buffer;
12023 12140 dtrace_buffer_t *aggbuf = state->dts_aggbuffer;
12024 12141
12025 12142 if (state->dts_speculates)
12026 12143 break;
12027 12144
12028 12145 if (!dtrace_buffer_consumed(buf, when))
12029 12146 break;
12030 12147
12031 12148 if (!dtrace_buffer_consumed(aggbuf, when))
12032 12149 break;
12033 12150
12034 12151 dtrace_ecb_disable(ecb);
12035 12152 ASSERT(probe->dtpr_ecb != ecb);
12036 12153 dtrace_ecb_destroy(ecb);
12037 12154 }
12038 12155 }
12039 12156
12040 12157 mutex_exit(&dtrace_lock);
12041 12158 mutex_exit(&cpu_lock);
12042 12159 }
12043 12160
12044 12161 /*
12045 12162 * DTrace DOF Functions
12046 12163 */
12047 12164 /*ARGSUSED*/
12048 12165 static void
12049 12166 dtrace_dof_error(dof_hdr_t *dof, const char *str)
12050 12167 {
12051 12168 if (dtrace_err_verbose)
12052 12169 cmn_err(CE_WARN, "failed to process DOF: %s", str);
12053 12170
12054 12171 #ifdef DTRACE_ERRDEBUG
12055 12172 dtrace_errdebug(str);
12056 12173 #endif
12057 12174 }
12058 12175
12059 12176 /*
12060 12177 * Create DOF out of a currently enabled state. Right now, we only create
12061 12178 * DOF containing the run-time options -- but this could be expanded to create
12062 12179 * complete DOF representing the enabled state.
12063 12180 */
12064 12181 static dof_hdr_t *
12065 12182 dtrace_dof_create(dtrace_state_t *state)
12066 12183 {
12067 12184 dof_hdr_t *dof;
12068 12185 dof_sec_t *sec;
12069 12186 dof_optdesc_t *opt;
12070 12187 int i, len = sizeof (dof_hdr_t) +
12071 12188 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
12072 12189 sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
12073 12190
12074 12191 ASSERT(MUTEX_HELD(&dtrace_lock));
12075 12192
12076 12193 dof = kmem_zalloc(len, KM_SLEEP);
12077 12194 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
12078 12195 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
12079 12196 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
12080 12197 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
12081 12198
12082 12199 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
12083 12200 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
12084 12201 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
12085 12202 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
12086 12203 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
12087 12204 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
12088 12205
12089 12206 dof->dofh_flags = 0;
12090 12207 dof->dofh_hdrsize = sizeof (dof_hdr_t);
12091 12208 dof->dofh_secsize = sizeof (dof_sec_t);
12092 12209 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
12093 12210 dof->dofh_secoff = sizeof (dof_hdr_t);
12094 12211 dof->dofh_loadsz = len;
12095 12212 dof->dofh_filesz = len;
12096 12213 dof->dofh_pad = 0;
12097 12214
12098 12215 /*
12099 12216 * Fill in the option section header...
12100 12217 */
12101 12218 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
12102 12219 sec->dofs_type = DOF_SECT_OPTDESC;
12103 12220 sec->dofs_align = sizeof (uint64_t);
12104 12221 sec->dofs_flags = DOF_SECF_LOAD;
12105 12222 sec->dofs_entsize = sizeof (dof_optdesc_t);
12106 12223
12107 12224 opt = (dof_optdesc_t *)((uintptr_t)sec +
12108 12225 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
12109 12226
12110 12227 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
12111 12228 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
12112 12229
12113 12230 for (i = 0; i < DTRACEOPT_MAX; i++) {
12114 12231 opt[i].dofo_option = i;
12115 12232 opt[i].dofo_strtab = DOF_SECIDX_NONE;
12116 12233 opt[i].dofo_value = state->dts_options[i];
12117 12234 }
12118 12235
12119 12236 return (dof);
12120 12237 }
12121 12238
12122 12239 static dof_hdr_t *
12123 12240 dtrace_dof_copyin(uintptr_t uarg, int *errp)
12124 12241 {
12125 12242 dof_hdr_t hdr, *dof;
12126 12243
12127 12244 ASSERT(!MUTEX_HELD(&dtrace_lock));
12128 12245
12129 12246 /*
12130 12247 * First, we're going to copyin() the sizeof (dof_hdr_t).
12131 12248 */
12132 12249 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
12133 12250 dtrace_dof_error(NULL, "failed to copyin DOF header");
12134 12251 *errp = EFAULT;
12135 12252 return (NULL);
12136 12253 }
12137 12254
12138 12255 /*
12139 12256 * Now we'll allocate the entire DOF and copy it in -- provided
12140 12257 * that the length isn't outrageous.
12141 12258 */
12142 12259 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
12143 12260 dtrace_dof_error(&hdr, "load size exceeds maximum");
12144 12261 *errp = E2BIG;
12145 12262 return (NULL);
12146 12263 }
12147 12264
12148 12265 if (hdr.dofh_loadsz < sizeof (hdr)) {
12149 12266 dtrace_dof_error(&hdr, "invalid load size");
12150 12267 *errp = EINVAL;
12151 12268 return (NULL);
12152 12269 }
12153 12270
12154 12271 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
12155 12272
12156 12273 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 ||
12157 12274 dof->dofh_loadsz != hdr.dofh_loadsz) {
12158 12275 kmem_free(dof, hdr.dofh_loadsz);
12159 12276 *errp = EFAULT;
12160 12277 return (NULL);
12161 12278 }
12162 12279
12163 12280 return (dof);
12164 12281 }
12165 12282
12166 12283 static dof_hdr_t *
12167 12284 dtrace_dof_property(const char *name)
12168 12285 {
12169 12286 uchar_t *buf;
12170 12287 uint64_t loadsz;
12171 12288 unsigned int len, i;
12172 12289 dof_hdr_t *dof;
12173 12290
12174 12291 /*
12175 12292 * Unfortunately, array of values in .conf files are always (and
12176 12293 * only) interpreted to be integer arrays. We must read our DOF
12177 12294 * as an integer array, and then squeeze it into a byte array.
12178 12295 */
12179 12296 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
12180 12297 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
12181 12298 return (NULL);
12182 12299
12183 12300 for (i = 0; i < len; i++)
12184 12301 buf[i] = (uchar_t)(((int *)buf)[i]);
12185 12302
12186 12303 if (len < sizeof (dof_hdr_t)) {
12187 12304 ddi_prop_free(buf);
12188 12305 dtrace_dof_error(NULL, "truncated header");
12189 12306 return (NULL);
12190 12307 }
12191 12308
12192 12309 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
12193 12310 ddi_prop_free(buf);
12194 12311 dtrace_dof_error(NULL, "truncated DOF");
12195 12312 return (NULL);
12196 12313 }
12197 12314
12198 12315 if (loadsz >= dtrace_dof_maxsize) {
12199 12316 ddi_prop_free(buf);
12200 12317 dtrace_dof_error(NULL, "oversized DOF");
12201 12318 return (NULL);
12202 12319 }
12203 12320
12204 12321 dof = kmem_alloc(loadsz, KM_SLEEP);
12205 12322 bcopy(buf, dof, loadsz);
12206 12323 ddi_prop_free(buf);
12207 12324
12208 12325 return (dof);
12209 12326 }
12210 12327
12211 12328 static void
12212 12329 dtrace_dof_destroy(dof_hdr_t *dof)
12213 12330 {
12214 12331 kmem_free(dof, dof->dofh_loadsz);
12215 12332 }
12216 12333
12217 12334 /*
12218 12335 * Return the dof_sec_t pointer corresponding to a given section index. If the
12219 12336 * index is not valid, dtrace_dof_error() is called and NULL is returned. If
12220 12337 * a type other than DOF_SECT_NONE is specified, the header is checked against
12221 12338 * this type and NULL is returned if the types do not match.
12222 12339 */
12223 12340 static dof_sec_t *
12224 12341 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
12225 12342 {
12226 12343 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
12227 12344 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
12228 12345
12229 12346 if (i >= dof->dofh_secnum) {
12230 12347 dtrace_dof_error(dof, "referenced section index is invalid");
12231 12348 return (NULL);
12232 12349 }
12233 12350
12234 12351 if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
12235 12352 dtrace_dof_error(dof, "referenced section is not loadable");
12236 12353 return (NULL);
12237 12354 }
12238 12355
12239 12356 if (type != DOF_SECT_NONE && type != sec->dofs_type) {
12240 12357 dtrace_dof_error(dof, "referenced section is the wrong type");
12241 12358 return (NULL);
12242 12359 }
12243 12360
12244 12361 return (sec);
12245 12362 }
12246 12363
12247 12364 static dtrace_probedesc_t *
12248 12365 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
12249 12366 {
12250 12367 dof_probedesc_t *probe;
12251 12368 dof_sec_t *strtab;
12252 12369 uintptr_t daddr = (uintptr_t)dof;
12253 12370 uintptr_t str;
12254 12371 size_t size;
12255 12372
12256 12373 if (sec->dofs_type != DOF_SECT_PROBEDESC) {
12257 12374 dtrace_dof_error(dof, "invalid probe section");
12258 12375 return (NULL);
12259 12376 }
12260 12377
12261 12378 if (sec->dofs_align != sizeof (dof_secidx_t)) {
12262 12379 dtrace_dof_error(dof, "bad alignment in probe description");
12263 12380 return (NULL);
12264 12381 }
12265 12382
12266 12383 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
12267 12384 dtrace_dof_error(dof, "truncated probe description");
12268 12385 return (NULL);
12269 12386 }
12270 12387
12271 12388 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
12272 12389 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
12273 12390
12274 12391 if (strtab == NULL)
12275 12392 return (NULL);
12276 12393
12277 12394 str = daddr + strtab->dofs_offset;
12278 12395 size = strtab->dofs_size;
12279 12396
12280 12397 if (probe->dofp_provider >= strtab->dofs_size) {
12281 12398 dtrace_dof_error(dof, "corrupt probe provider");
12282 12399 return (NULL);
12283 12400 }
12284 12401
12285 12402 (void) strncpy(desc->dtpd_provider,
12286 12403 (char *)(str + probe->dofp_provider),
12287 12404 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
12288 12405
12289 12406 if (probe->dofp_mod >= strtab->dofs_size) {
12290 12407 dtrace_dof_error(dof, "corrupt probe module");
12291 12408 return (NULL);
12292 12409 }
12293 12410
12294 12411 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
12295 12412 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
12296 12413
12297 12414 if (probe->dofp_func >= strtab->dofs_size) {
12298 12415 dtrace_dof_error(dof, "corrupt probe function");
12299 12416 return (NULL);
12300 12417 }
12301 12418
12302 12419 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
12303 12420 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
12304 12421
12305 12422 if (probe->dofp_name >= strtab->dofs_size) {
12306 12423 dtrace_dof_error(dof, "corrupt probe name");
12307 12424 return (NULL);
12308 12425 }
12309 12426
12310 12427 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
12311 12428 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
12312 12429
12313 12430 return (desc);
12314 12431 }
12315 12432
12316 12433 static dtrace_difo_t *
12317 12434 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12318 12435 cred_t *cr)
12319 12436 {
12320 12437 dtrace_difo_t *dp;
12321 12438 size_t ttl = 0;
12322 12439 dof_difohdr_t *dofd;
12323 12440 uintptr_t daddr = (uintptr_t)dof;
12324 12441 size_t max = dtrace_difo_maxsize;
12325 12442 int i, l, n;
12326 12443
12327 12444 static const struct {
12328 12445 int section;
12329 12446 int bufoffs;
12330 12447 int lenoffs;
12331 12448 int entsize;
12332 12449 int align;
12333 12450 const char *msg;
12334 12451 } difo[] = {
12335 12452 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
12336 12453 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
12337 12454 sizeof (dif_instr_t), "multiple DIF sections" },
12338 12455
12339 12456 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
12340 12457 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
12341 12458 sizeof (uint64_t), "multiple integer tables" },
12342 12459
12343 12460 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
12344 12461 offsetof(dtrace_difo_t, dtdo_strlen), 0,
12345 12462 sizeof (char), "multiple string tables" },
12346 12463
12347 12464 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
12348 12465 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
12349 12466 sizeof (uint_t), "multiple variable tables" },
12350 12467
12351 12468 { DOF_SECT_NONE, 0, 0, 0, NULL }
12352 12469 };
12353 12470
12354 12471 if (sec->dofs_type != DOF_SECT_DIFOHDR) {
12355 12472 dtrace_dof_error(dof, "invalid DIFO header section");
12356 12473 return (NULL);
12357 12474 }
12358 12475
12359 12476 if (sec->dofs_align != sizeof (dof_secidx_t)) {
12360 12477 dtrace_dof_error(dof, "bad alignment in DIFO header");
12361 12478 return (NULL);
12362 12479 }
12363 12480
12364 12481 if (sec->dofs_size < sizeof (dof_difohdr_t) ||
12365 12482 sec->dofs_size % sizeof (dof_secidx_t)) {
12366 12483 dtrace_dof_error(dof, "bad size in DIFO header");
12367 12484 return (NULL);
12368 12485 }
12369 12486
12370 12487 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12371 12488 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
12372 12489
12373 12490 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
12374 12491 dp->dtdo_rtype = dofd->dofd_rtype;
12375 12492
12376 12493 for (l = 0; l < n; l++) {
12377 12494 dof_sec_t *subsec;
12378 12495 void **bufp;
12379 12496 uint32_t *lenp;
12380 12497
12381 12498 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
12382 12499 dofd->dofd_links[l])) == NULL)
12383 12500 goto err; /* invalid section link */
12384 12501
12385 12502 if (ttl + subsec->dofs_size > max) {
12386 12503 dtrace_dof_error(dof, "exceeds maximum size");
12387 12504 goto err;
12388 12505 }
12389 12506
12390 12507 ttl += subsec->dofs_size;
12391 12508
12392 12509 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
12393 12510 if (subsec->dofs_type != difo[i].section)
12394 12511 continue;
12395 12512
12396 12513 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
12397 12514 dtrace_dof_error(dof, "section not loaded");
12398 12515 goto err;
12399 12516 }
12400 12517
12401 12518 if (subsec->dofs_align != difo[i].align) {
12402 12519 dtrace_dof_error(dof, "bad alignment");
12403 12520 goto err;
12404 12521 }
12405 12522
12406 12523 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
12407 12524 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
12408 12525
12409 12526 if (*bufp != NULL) {
12410 12527 dtrace_dof_error(dof, difo[i].msg);
12411 12528 goto err;
12412 12529 }
12413 12530
12414 12531 if (difo[i].entsize != subsec->dofs_entsize) {
12415 12532 dtrace_dof_error(dof, "entry size mismatch");
12416 12533 goto err;
12417 12534 }
12418 12535
12419 12536 if (subsec->dofs_entsize != 0 &&
12420 12537 (subsec->dofs_size % subsec->dofs_entsize) != 0) {
12421 12538 dtrace_dof_error(dof, "corrupt entry size");
12422 12539 goto err;
12423 12540 }
12424 12541
12425 12542 *lenp = subsec->dofs_size;
12426 12543 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
12427 12544 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
12428 12545 *bufp, subsec->dofs_size);
12429 12546
12430 12547 if (subsec->dofs_entsize != 0)
12431 12548 *lenp /= subsec->dofs_entsize;
12432 12549
12433 12550 break;
12434 12551 }
12435 12552
12436 12553 /*
12437 12554 * If we encounter a loadable DIFO sub-section that is not
12438 12555 * known to us, assume this is a broken program and fail.
12439 12556 */
12440 12557 if (difo[i].section == DOF_SECT_NONE &&
12441 12558 (subsec->dofs_flags & DOF_SECF_LOAD)) {
12442 12559 dtrace_dof_error(dof, "unrecognized DIFO subsection");
12443 12560 goto err;
12444 12561 }
12445 12562 }
12446 12563
12447 12564 if (dp->dtdo_buf == NULL) {
12448 12565 /*
12449 12566 * We can't have a DIF object without DIF text.
12450 12567 */
12451 12568 dtrace_dof_error(dof, "missing DIF text");
12452 12569 goto err;
12453 12570 }
12454 12571
12455 12572 /*
12456 12573 * Before we validate the DIF object, run through the variable table
12457 12574 * looking for the strings -- if any of their size are under, we'll set
12458 12575 * their size to be the system-wide default string size. Note that
12459 12576 * this should _not_ happen if the "strsize" option has been set --
12460 12577 * in this case, the compiler should have set the size to reflect the
12461 12578 * setting of the option.
12462 12579 */
12463 12580 for (i = 0; i < dp->dtdo_varlen; i++) {
12464 12581 dtrace_difv_t *v = &dp->dtdo_vartab[i];
12465 12582 dtrace_diftype_t *t = &v->dtdv_type;
12466 12583
12467 12584 if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
12468 12585 continue;
12469 12586
12470 12587 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
12471 12588 t->dtdt_size = dtrace_strsize_default;
12472 12589 }
12473 12590
12474 12591 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
12475 12592 goto err;
12476 12593
12477 12594 dtrace_difo_init(dp, vstate);
12478 12595 return (dp);
12479 12596
12480 12597 err:
12481 12598 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
12482 12599 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
12483 12600 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
12484 12601 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
12485 12602
12486 12603 kmem_free(dp, sizeof (dtrace_difo_t));
12487 12604 return (NULL);
12488 12605 }
12489 12606
12490 12607 static dtrace_predicate_t *
12491 12608 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12492 12609 cred_t *cr)
12493 12610 {
12494 12611 dtrace_difo_t *dp;
12495 12612
12496 12613 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
12497 12614 return (NULL);
12498 12615
12499 12616 return (dtrace_predicate_create(dp));
12500 12617 }
12501 12618
12502 12619 static dtrace_actdesc_t *
12503 12620 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12504 12621 cred_t *cr)
12505 12622 {
12506 12623 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
12507 12624 dof_actdesc_t *desc;
12508 12625 dof_sec_t *difosec;
12509 12626 size_t offs;
12510 12627 uintptr_t daddr = (uintptr_t)dof;
12511 12628 uint64_t arg;
12512 12629 dtrace_actkind_t kind;
12513 12630
12514 12631 if (sec->dofs_type != DOF_SECT_ACTDESC) {
12515 12632 dtrace_dof_error(dof, "invalid action section");
12516 12633 return (NULL);
12517 12634 }
12518 12635
12519 12636 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
12520 12637 dtrace_dof_error(dof, "truncated action description");
12521 12638 return (NULL);
12522 12639 }
12523 12640
12524 12641 if (sec->dofs_align != sizeof (uint64_t)) {
12525 12642 dtrace_dof_error(dof, "bad alignment in action description");
12526 12643 return (NULL);
12527 12644 }
12528 12645
12529 12646 if (sec->dofs_size < sec->dofs_entsize) {
12530 12647 dtrace_dof_error(dof, "section entry size exceeds total size");
12531 12648 return (NULL);
12532 12649 }
12533 12650
12534 12651 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
12535 12652 dtrace_dof_error(dof, "bad entry size in action description");
12536 12653 return (NULL);
12537 12654 }
12538 12655
12539 12656 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
12540 12657 dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
12541 12658 return (NULL);
12542 12659 }
12543 12660
12544 12661 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
12545 12662 desc = (dof_actdesc_t *)(daddr +
12546 12663 (uintptr_t)sec->dofs_offset + offs);
12547 12664 kind = (dtrace_actkind_t)desc->dofa_kind;
12548 12665
12549 12666 if ((DTRACEACT_ISPRINTFLIKE(kind) &&
12550 12667 (kind != DTRACEACT_PRINTA ||
12551 12668 desc->dofa_strtab != DOF_SECIDX_NONE)) ||
12552 12669 (kind == DTRACEACT_DIFEXPR &&
12553 12670 desc->dofa_strtab != DOF_SECIDX_NONE)) {
12554 12671 dof_sec_t *strtab;
12555 12672 char *str, *fmt;
12556 12673 uint64_t i;
12557 12674
12558 12675 /*
12559 12676 * The argument to these actions is an index into the
12560 12677 * DOF string table. For printf()-like actions, this
12561 12678 * is the format string. For print(), this is the
12562 12679 * CTF type of the expression result.
12563 12680 */
12564 12681 if ((strtab = dtrace_dof_sect(dof,
12565 12682 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
12566 12683 goto err;
12567 12684
12568 12685 str = (char *)((uintptr_t)dof +
12569 12686 (uintptr_t)strtab->dofs_offset);
12570 12687
12571 12688 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
12572 12689 if (str[i] == '\0')
12573 12690 break;
12574 12691 }
12575 12692
12576 12693 if (i >= strtab->dofs_size) {
12577 12694 dtrace_dof_error(dof, "bogus format string");
12578 12695 goto err;
12579 12696 }
12580 12697
12581 12698 if (i == desc->dofa_arg) {
12582 12699 dtrace_dof_error(dof, "empty format string");
12583 12700 goto err;
12584 12701 }
12585 12702
12586 12703 i -= desc->dofa_arg;
12587 12704 fmt = kmem_alloc(i + 1, KM_SLEEP);
12588 12705 bcopy(&str[desc->dofa_arg], fmt, i + 1);
12589 12706 arg = (uint64_t)(uintptr_t)fmt;
12590 12707 } else {
12591 12708 if (kind == DTRACEACT_PRINTA) {
12592 12709 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
12593 12710 arg = 0;
12594 12711 } else {
12595 12712 arg = desc->dofa_arg;
12596 12713 }
12597 12714 }
12598 12715
12599 12716 act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
12600 12717 desc->dofa_uarg, arg);
12601 12718
12602 12719 if (last != NULL) {
12603 12720 last->dtad_next = act;
12604 12721 } else {
12605 12722 first = act;
12606 12723 }
12607 12724
12608 12725 last = act;
12609 12726
12610 12727 if (desc->dofa_difo == DOF_SECIDX_NONE)
12611 12728 continue;
12612 12729
12613 12730 if ((difosec = dtrace_dof_sect(dof,
12614 12731 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
12615 12732 goto err;
12616 12733
12617 12734 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
12618 12735
12619 12736 if (act->dtad_difo == NULL)
12620 12737 goto err;
12621 12738 }
12622 12739
12623 12740 ASSERT(first != NULL);
12624 12741 return (first);
12625 12742
12626 12743 err:
12627 12744 for (act = first; act != NULL; act = next) {
12628 12745 next = act->dtad_next;
12629 12746 dtrace_actdesc_release(act, vstate);
12630 12747 }
12631 12748
12632 12749 return (NULL);
12633 12750 }
12634 12751
12635 12752 static dtrace_ecbdesc_t *
12636 12753 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
12637 12754 cred_t *cr)
12638 12755 {
12639 12756 dtrace_ecbdesc_t *ep;
12640 12757 dof_ecbdesc_t *ecb;
12641 12758 dtrace_probedesc_t *desc;
12642 12759 dtrace_predicate_t *pred = NULL;
12643 12760
12644 12761 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
12645 12762 dtrace_dof_error(dof, "truncated ECB description");
12646 12763 return (NULL);
12647 12764 }
12648 12765
12649 12766 if (sec->dofs_align != sizeof (uint64_t)) {
12650 12767 dtrace_dof_error(dof, "bad alignment in ECB description");
12651 12768 return (NULL);
12652 12769 }
12653 12770
12654 12771 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
12655 12772 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
12656 12773
12657 12774 if (sec == NULL)
12658 12775 return (NULL);
12659 12776
12660 12777 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12661 12778 ep->dted_uarg = ecb->dofe_uarg;
12662 12779 desc = &ep->dted_probe;
12663 12780
12664 12781 if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
12665 12782 goto err;
12666 12783
12667 12784 if (ecb->dofe_pred != DOF_SECIDX_NONE) {
12668 12785 if ((sec = dtrace_dof_sect(dof,
12669 12786 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
12670 12787 goto err;
12671 12788
12672 12789 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
12673 12790 goto err;
12674 12791
12675 12792 ep->dted_pred.dtpdd_predicate = pred;
12676 12793 }
12677 12794
12678 12795 if (ecb->dofe_actions != DOF_SECIDX_NONE) {
12679 12796 if ((sec = dtrace_dof_sect(dof,
12680 12797 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
12681 12798 goto err;
12682 12799
12683 12800 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
12684 12801
12685 12802 if (ep->dted_action == NULL)
12686 12803 goto err;
12687 12804 }
12688 12805
12689 12806 return (ep);
12690 12807
12691 12808 err:
12692 12809 if (pred != NULL)
12693 12810 dtrace_predicate_release(pred, vstate);
12694 12811 kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12695 12812 return (NULL);
12696 12813 }
12697 12814
12698 12815 /*
12699 12816 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
12700 12817 * specified DOF. At present, this amounts to simply adding 'ubase' to the
12701 12818 * site of any user SETX relocations to account for load object base address.
12702 12819 * In the future, if we need other relocations, this function can be extended.
12703 12820 */
12704 12821 static int
12705 12822 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase)
12706 12823 {
12707 12824 uintptr_t daddr = (uintptr_t)dof;
12708 12825 dof_relohdr_t *dofr =
12709 12826 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
12710 12827 dof_sec_t *ss, *rs, *ts;
12711 12828 dof_relodesc_t *r;
12712 12829 uint_t i, n;
12713 12830
12714 12831 if (sec->dofs_size < sizeof (dof_relohdr_t) ||
12715 12832 sec->dofs_align != sizeof (dof_secidx_t)) {
12716 12833 dtrace_dof_error(dof, "invalid relocation header");
12717 12834 return (-1);
12718 12835 }
12719 12836
12720 12837 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
12721 12838 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
12722 12839 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
12723 12840
12724 12841 if (ss == NULL || rs == NULL || ts == NULL)
12725 12842 return (-1); /* dtrace_dof_error() has been called already */
12726 12843
12727 12844 if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
12728 12845 rs->dofs_align != sizeof (uint64_t)) {
12729 12846 dtrace_dof_error(dof, "invalid relocation section");
12730 12847 return (-1);
12731 12848 }
12732 12849
12733 12850 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
12734 12851 n = rs->dofs_size / rs->dofs_entsize;
12735 12852
12736 12853 for (i = 0; i < n; i++) {
12737 12854 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
12738 12855
12739 12856 switch (r->dofr_type) {
12740 12857 case DOF_RELO_NONE:
12741 12858 break;
12742 12859 case DOF_RELO_SETX:
12743 12860 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
12744 12861 sizeof (uint64_t) > ts->dofs_size) {
12745 12862 dtrace_dof_error(dof, "bad relocation offset");
12746 12863 return (-1);
12747 12864 }
12748 12865
12749 12866 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
12750 12867 dtrace_dof_error(dof, "misaligned setx relo");
12751 12868 return (-1);
12752 12869 }
12753 12870
12754 12871 *(uint64_t *)taddr += ubase;
12755 12872 break;
12756 12873 default:
12757 12874 dtrace_dof_error(dof, "invalid relocation type");
12758 12875 return (-1);
12759 12876 }
12760 12877
12761 12878 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
12762 12879 }
12763 12880
12764 12881 return (0);
12765 12882 }
12766 12883
12767 12884 /*
12768 12885 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
12769 12886 * header: it should be at the front of a memory region that is at least
12770 12887 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
12771 12888 * size. It need not be validated in any other way.
12772 12889 */
12773 12890 static int
12774 12891 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
12775 12892 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes)
12776 12893 {
12777 12894 uint64_t len = dof->dofh_loadsz, seclen;
12778 12895 uintptr_t daddr = (uintptr_t)dof;
12779 12896 dtrace_ecbdesc_t *ep;
12780 12897 dtrace_enabling_t *enab;
12781 12898 uint_t i;
12782 12899
12783 12900 ASSERT(MUTEX_HELD(&dtrace_lock));
12784 12901 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
12785 12902
12786 12903 /*
12787 12904 * Check the DOF header identification bytes. In addition to checking
12788 12905 * valid settings, we also verify that unused bits/bytes are zeroed so
12789 12906 * we can use them later without fear of regressing existing binaries.
12790 12907 */
12791 12908 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
12792 12909 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
12793 12910 dtrace_dof_error(dof, "DOF magic string mismatch");
12794 12911 return (-1);
12795 12912 }
12796 12913
12797 12914 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
12798 12915 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
12799 12916 dtrace_dof_error(dof, "DOF has invalid data model");
12800 12917 return (-1);
12801 12918 }
12802 12919
12803 12920 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
12804 12921 dtrace_dof_error(dof, "DOF encoding mismatch");
12805 12922 return (-1);
12806 12923 }
12807 12924
12808 12925 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
12809 12926 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
12810 12927 dtrace_dof_error(dof, "DOF version mismatch");
12811 12928 return (-1);
12812 12929 }
12813 12930
12814 12931 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
12815 12932 dtrace_dof_error(dof, "DOF uses unsupported instruction set");
12816 12933 return (-1);
12817 12934 }
12818 12935
12819 12936 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
12820 12937 dtrace_dof_error(dof, "DOF uses too many integer registers");
12821 12938 return (-1);
12822 12939 }
12823 12940
12824 12941 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
12825 12942 dtrace_dof_error(dof, "DOF uses too many tuple registers");
12826 12943 return (-1);
12827 12944 }
12828 12945
12829 12946 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
12830 12947 if (dof->dofh_ident[i] != 0) {
12831 12948 dtrace_dof_error(dof, "DOF has invalid ident byte set");
12832 12949 return (-1);
12833 12950 }
12834 12951 }
12835 12952
12836 12953 if (dof->dofh_flags & ~DOF_FL_VALID) {
12837 12954 dtrace_dof_error(dof, "DOF has invalid flag bits set");
12838 12955 return (-1);
12839 12956 }
12840 12957
12841 12958 if (dof->dofh_secsize == 0) {
12842 12959 dtrace_dof_error(dof, "zero section header size");
12843 12960 return (-1);
12844 12961 }
12845 12962
12846 12963 /*
12847 12964 * Check that the section headers don't exceed the amount of DOF
12848 12965 * data. Note that we cast the section size and number of sections
12849 12966 * to uint64_t's to prevent possible overflow in the multiplication.
12850 12967 */
12851 12968 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
12852 12969
12853 12970 if (dof->dofh_secoff > len || seclen > len ||
12854 12971 dof->dofh_secoff + seclen > len) {
12855 12972 dtrace_dof_error(dof, "truncated section headers");
12856 12973 return (-1);
12857 12974 }
12858 12975
12859 12976 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
12860 12977 dtrace_dof_error(dof, "misaligned section headers");
12861 12978 return (-1);
12862 12979 }
12863 12980
12864 12981 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
12865 12982 dtrace_dof_error(dof, "misaligned section size");
12866 12983 return (-1);
12867 12984 }
12868 12985
12869 12986 /*
12870 12987 * Take an initial pass through the section headers to be sure that
12871 12988 * the headers don't have stray offsets. If the 'noprobes' flag is
12872 12989 * set, do not permit sections relating to providers, probes, or args.
12873 12990 */
12874 12991 for (i = 0; i < dof->dofh_secnum; i++) {
12875 12992 dof_sec_t *sec = (dof_sec_t *)(daddr +
12876 12993 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12877 12994
12878 12995 if (noprobes) {
12879 12996 switch (sec->dofs_type) {
12880 12997 case DOF_SECT_PROVIDER:
12881 12998 case DOF_SECT_PROBES:
12882 12999 case DOF_SECT_PRARGS:
12883 13000 case DOF_SECT_PROFFS:
12884 13001 dtrace_dof_error(dof, "illegal sections "
12885 13002 "for enabling");
12886 13003 return (-1);
12887 13004 }
12888 13005 }
12889 13006
12890 13007 if (DOF_SEC_ISLOADABLE(sec->dofs_type) &&
12891 13008 !(sec->dofs_flags & DOF_SECF_LOAD)) {
12892 13009 dtrace_dof_error(dof, "loadable section with load "
12893 13010 "flag unset");
12894 13011 return (-1);
12895 13012 }
12896 13013
12897 13014 if (!(sec->dofs_flags & DOF_SECF_LOAD))
12898 13015 continue; /* just ignore non-loadable sections */
12899 13016
12900 13017 if (sec->dofs_align & (sec->dofs_align - 1)) {
12901 13018 dtrace_dof_error(dof, "bad section alignment");
12902 13019 return (-1);
12903 13020 }
12904 13021
12905 13022 if (sec->dofs_offset & (sec->dofs_align - 1)) {
12906 13023 dtrace_dof_error(dof, "misaligned section");
12907 13024 return (-1);
12908 13025 }
12909 13026
12910 13027 if (sec->dofs_offset > len || sec->dofs_size > len ||
12911 13028 sec->dofs_offset + sec->dofs_size > len) {
12912 13029 dtrace_dof_error(dof, "corrupt section header");
12913 13030 return (-1);
12914 13031 }
12915 13032
12916 13033 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
12917 13034 sec->dofs_offset + sec->dofs_size - 1) != '\0') {
12918 13035 dtrace_dof_error(dof, "non-terminating string table");
12919 13036 return (-1);
12920 13037 }
12921 13038 }
12922 13039
12923 13040 /*
12924 13041 * Take a second pass through the sections and locate and perform any
12925 13042 * relocations that are present. We do this after the first pass to
12926 13043 * be sure that all sections have had their headers validated.
12927 13044 */
12928 13045 for (i = 0; i < dof->dofh_secnum; i++) {
12929 13046 dof_sec_t *sec = (dof_sec_t *)(daddr +
12930 13047 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12931 13048
12932 13049 if (!(sec->dofs_flags & DOF_SECF_LOAD))
12933 13050 continue; /* skip sections that are not loadable */
12934 13051
12935 13052 switch (sec->dofs_type) {
12936 13053 case DOF_SECT_URELHDR:
12937 13054 if (dtrace_dof_relocate(dof, sec, ubase) != 0)
12938 13055 return (-1);
12939 13056 break;
12940 13057 }
12941 13058 }
12942 13059
12943 13060 if ((enab = *enabp) == NULL)
12944 13061 enab = *enabp = dtrace_enabling_create(vstate);
12945 13062
12946 13063 for (i = 0; i < dof->dofh_secnum; i++) {
12947 13064 dof_sec_t *sec = (dof_sec_t *)(daddr +
12948 13065 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12949 13066
12950 13067 if (sec->dofs_type != DOF_SECT_ECBDESC)
12951 13068 continue;
12952 13069
12953 13070 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
12954 13071 dtrace_enabling_destroy(enab);
12955 13072 *enabp = NULL;
12956 13073 return (-1);
12957 13074 }
12958 13075
12959 13076 dtrace_enabling_add(enab, ep);
12960 13077 }
12961 13078
12962 13079 return (0);
12963 13080 }
12964 13081
12965 13082 /*
12966 13083 * Process DOF for any options. This routine assumes that the DOF has been
12967 13084 * at least processed by dtrace_dof_slurp().
12968 13085 */
12969 13086 static int
12970 13087 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
12971 13088 {
12972 13089 int i, rval;
12973 13090 uint32_t entsize;
12974 13091 size_t offs;
12975 13092 dof_optdesc_t *desc;
12976 13093
12977 13094 for (i = 0; i < dof->dofh_secnum; i++) {
12978 13095 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
12979 13096 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
12980 13097
12981 13098 if (sec->dofs_type != DOF_SECT_OPTDESC)
12982 13099 continue;
12983 13100
12984 13101 if (sec->dofs_align != sizeof (uint64_t)) {
12985 13102 dtrace_dof_error(dof, "bad alignment in "
12986 13103 "option description");
12987 13104 return (EINVAL);
12988 13105 }
12989 13106
12990 13107 if ((entsize = sec->dofs_entsize) == 0) {
12991 13108 dtrace_dof_error(dof, "zeroed option entry size");
12992 13109 return (EINVAL);
12993 13110 }
12994 13111
12995 13112 if (entsize < sizeof (dof_optdesc_t)) {
12996 13113 dtrace_dof_error(dof, "bad option entry size");
12997 13114 return (EINVAL);
12998 13115 }
12999 13116
13000 13117 for (offs = 0; offs < sec->dofs_size; offs += entsize) {
13001 13118 desc = (dof_optdesc_t *)((uintptr_t)dof +
13002 13119 (uintptr_t)sec->dofs_offset + offs);
13003 13120
13004 13121 if (desc->dofo_strtab != DOF_SECIDX_NONE) {
13005 13122 dtrace_dof_error(dof, "non-zero option string");
13006 13123 return (EINVAL);
13007 13124 }
13008 13125
13009 13126 if (desc->dofo_value == DTRACEOPT_UNSET) {
13010 13127 dtrace_dof_error(dof, "unset option");
13011 13128 return (EINVAL);
13012 13129 }
13013 13130
13014 13131 if ((rval = dtrace_state_option(state,
13015 13132 desc->dofo_option, desc->dofo_value)) != 0) {
13016 13133 dtrace_dof_error(dof, "rejected option");
13017 13134 return (rval);
13018 13135 }
13019 13136 }
13020 13137 }
13021 13138
13022 13139 return (0);
13023 13140 }
13024 13141
13025 13142 /*
13026 13143 * DTrace Consumer State Functions
13027 13144 */
13028 13145 int
13029 13146 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
13030 13147 {
13031 13148 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
13032 13149 void *base;
13033 13150 uintptr_t limit;
13034 13151 dtrace_dynvar_t *dvar, *next, *start;
13035 13152 int i;
13036 13153
13037 13154 ASSERT(MUTEX_HELD(&dtrace_lock));
13038 13155 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
13039 13156
13040 13157 bzero(dstate, sizeof (dtrace_dstate_t));
13041 13158
13042 13159 if ((dstate->dtds_chunksize = chunksize) == 0)
13043 13160 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
13044 13161
13045 13162 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
13046 13163 size = min;
13047 13164
13048 13165 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL)
13049 13166 return (ENOMEM);
13050 13167
13051 13168 dstate->dtds_size = size;
13052 13169 dstate->dtds_base = base;
13053 13170 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
13054 13171 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
13055 13172
13056 13173 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
13057 13174
13058 13175 if (hashsize != 1 && (hashsize & 1))
13059 13176 hashsize--;
13060 13177
13061 13178 dstate->dtds_hashsize = hashsize;
13062 13179 dstate->dtds_hash = dstate->dtds_base;
13063 13180
13064 13181 /*
13065 13182 * Set all of our hash buckets to point to the single sink, and (if
13066 13183 * it hasn't already been set), set the sink's hash value to be the
13067 13184 * sink sentinel value. The sink is needed for dynamic variable
13068 13185 * lookups to know that they have iterated over an entire, valid hash
13069 13186 * chain.
13070 13187 */
13071 13188 for (i = 0; i < hashsize; i++)
13072 13189 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
13073 13190
13074 13191 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
13075 13192 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
13076 13193
13077 13194 /*
13078 13195 * Determine number of active CPUs. Divide free list evenly among
13079 13196 * active CPUs.
13080 13197 */
13081 13198 start = (dtrace_dynvar_t *)
13082 13199 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
13083 13200 limit = (uintptr_t)base + size;
13084 13201
13085 13202 maxper = (limit - (uintptr_t)start) / NCPU;
13086 13203 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
13087 13204
13088 13205 for (i = 0; i < NCPU; i++) {
13089 13206 dstate->dtds_percpu[i].dtdsc_free = dvar = start;
13090 13207
13091 13208 /*
13092 13209 * If we don't even have enough chunks to make it once through
13093 13210 * NCPUs, we're just going to allocate everything to the first
13094 13211 * CPU. And if we're on the last CPU, we're going to allocate
13095 13212 * whatever is left over. In either case, we set the limit to
13096 13213 * be the limit of the dynamic variable space.
13097 13214 */
13098 13215 if (maxper == 0 || i == NCPU - 1) {
13099 13216 limit = (uintptr_t)base + size;
13100 13217 start = NULL;
13101 13218 } else {
13102 13219 limit = (uintptr_t)start + maxper;
13103 13220 start = (dtrace_dynvar_t *)limit;
13104 13221 }
13105 13222
13106 13223 ASSERT(limit <= (uintptr_t)base + size);
13107 13224
13108 13225 for (;;) {
13109 13226 next = (dtrace_dynvar_t *)((uintptr_t)dvar +
13110 13227 dstate->dtds_chunksize);
13111 13228
13112 13229 if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
13113 13230 break;
13114 13231
13115 13232 dvar->dtdv_next = next;
13116 13233 dvar = next;
13117 13234 }
13118 13235
13119 13236 if (maxper == 0)
13120 13237 break;
13121 13238 }
13122 13239
13123 13240 return (0);
13124 13241 }
13125 13242
13126 13243 void
13127 13244 dtrace_dstate_fini(dtrace_dstate_t *dstate)
13128 13245 {
13129 13246 ASSERT(MUTEX_HELD(&cpu_lock));
13130 13247
13131 13248 if (dstate->dtds_base == NULL)
13132 13249 return;
13133 13250
13134 13251 kmem_free(dstate->dtds_base, dstate->dtds_size);
13135 13252 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
13136 13253 }
13137 13254
13138 13255 static void
13139 13256 dtrace_vstate_fini(dtrace_vstate_t *vstate)
13140 13257 {
13141 13258 /*
13142 13259 * Logical XOR, where are you?
13143 13260 */
13144 13261 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
13145 13262
13146 13263 if (vstate->dtvs_nglobals > 0) {
13147 13264 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
13148 13265 sizeof (dtrace_statvar_t *));
13149 13266 }
13150 13267
13151 13268 if (vstate->dtvs_ntlocals > 0) {
13152 13269 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
13153 13270 sizeof (dtrace_difv_t));
13154 13271 }
13155 13272
13156 13273 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
13157 13274
13158 13275 if (vstate->dtvs_nlocals > 0) {
13159 13276 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
13160 13277 sizeof (dtrace_statvar_t *));
13161 13278 }
13162 13279 }
13163 13280
13164 13281 static void
13165 13282 dtrace_state_clean(dtrace_state_t *state)
13166 13283 {
13167 13284 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
13168 13285 return;
13169 13286
13170 13287 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
13171 13288 dtrace_speculation_clean(state);
13172 13289 }
13173 13290
13174 13291 static void
13175 13292 dtrace_state_deadman(dtrace_state_t *state)
13176 13293 {
13177 13294 hrtime_t now;
13178 13295
13179 13296 dtrace_sync();
13180 13297
13181 13298 now = dtrace_gethrtime();
13182 13299
13183 13300 if (state != dtrace_anon.dta_state &&
13184 13301 now - state->dts_laststatus >= dtrace_deadman_user)
13185 13302 return;
13186 13303
13187 13304 /*
13188 13305 * We must be sure that dts_alive never appears to be less than the
13189 13306 * value upon entry to dtrace_state_deadman(), and because we lack a
13190 13307 * dtrace_cas64(), we cannot store to it atomically. We thus instead
13191 13308 * store INT64_MAX to it, followed by a memory barrier, followed by
13192 13309 * the new value. This assures that dts_alive never appears to be
13193 13310 * less than its true value, regardless of the order in which the
13194 13311 * stores to the underlying storage are issued.
13195 13312 */
13196 13313 state->dts_alive = INT64_MAX;
13197 13314 dtrace_membar_producer();
13198 13315 state->dts_alive = now;
13199 13316 }
13200 13317
13201 13318 dtrace_state_t *
13202 13319 dtrace_state_create(dev_t *devp, cred_t *cr)
13203 13320 {
13204 13321 minor_t minor;
13205 13322 major_t major;
13206 13323 char c[30];
13207 13324 dtrace_state_t *state;
13208 13325 dtrace_optval_t *opt;
13209 13326 int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
13210 13327
13211 13328 ASSERT(MUTEX_HELD(&dtrace_lock));
13212 13329 ASSERT(MUTEX_HELD(&cpu_lock));
13213 13330
13214 13331 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
13215 13332 VM_BESTFIT | VM_SLEEP);
13216 13333
13217 13334 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
13218 13335 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
13219 13336 return (NULL);
13220 13337 }
13221 13338
13222 13339 state = ddi_get_soft_state(dtrace_softstate, minor);
13223 13340 state->dts_epid = DTRACE_EPIDNONE + 1;
13224 13341
13225 13342 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor);
13226 13343 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
13227 13344 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
13228 13345
13229 13346 if (devp != NULL) {
13230 13347 major = getemajor(*devp);
13231 13348 } else {
13232 13349 major = ddi_driver_major(dtrace_devi);
13233 13350 }
13234 13351
13235 13352 state->dts_dev = makedevice(major, minor);
13236 13353
13237 13354 if (devp != NULL)
13238 13355 *devp = state->dts_dev;
13239 13356
13240 13357 /*
13241 13358 * We allocate NCPU buffers. On the one hand, this can be quite
13242 13359 * a bit of memory per instance (nearly 36K on a Starcat). On the
13243 13360 * other hand, it saves an additional memory reference in the probe
13244 13361 * path.
13245 13362 */
13246 13363 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
13247 13364 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
13248 13365 state->dts_cleaner = CYCLIC_NONE;
13249 13366 state->dts_deadman = CYCLIC_NONE;
13250 13367 state->dts_vstate.dtvs_state = state;
13251 13368
13252 13369 for (i = 0; i < DTRACEOPT_MAX; i++)
13253 13370 state->dts_options[i] = DTRACEOPT_UNSET;
13254 13371
13255 13372 /*
13256 13373 * Set the default options.
13257 13374 */
13258 13375 opt = state->dts_options;
13259 13376 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
13260 13377 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
13261 13378 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
13262 13379 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
13263 13380 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
13264 13381 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
13265 13382 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
13266 13383 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
13267 13384 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
13268 13385 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
13269 13386 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
13270 13387 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
13271 13388 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
13272 13389 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
13273 13390
13274 13391 state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
13275 13392
13276 13393 /*
13277 13394 * Depending on the user credentials, we set flag bits which alter probe
13278 13395 * visibility or the amount of destructiveness allowed. In the case of
13279 13396 * actual anonymous tracing, or the possession of all privileges, all of
13280 13397 * the normal checks are bypassed.
13281 13398 */
13282 13399 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
13283 13400 state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
13284 13401 state->dts_cred.dcr_action = DTRACE_CRA_ALL;
13285 13402 } else {
13286 13403 /*
13287 13404 * Set up the credentials for this instantiation. We take a
13288 13405 * hold on the credential to prevent it from disappearing on
13289 13406 * us; this in turn prevents the zone_t referenced by this
13290 13407 * credential from disappearing. This means that we can
13291 13408 * examine the credential and the zone from probe context.
13292 13409 */
13293 13410 crhold(cr);
13294 13411 state->dts_cred.dcr_cred = cr;
13295 13412
13296 13413 /*
13297 13414 * CRA_PROC means "we have *some* privilege for dtrace" and
13298 13415 * unlocks the use of variables like pid, zonename, etc.
13299 13416 */
13300 13417 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
13301 13418 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
13302 13419 state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
13303 13420 }
13304 13421
13305 13422 /*
13306 13423 * dtrace_user allows use of syscall and profile providers.
13307 13424 * If the user also has proc_owner and/or proc_zone, we
13308 13425 * extend the scope to include additional visibility and
13309 13426 * destructive power.
13310 13427 */
13311 13428 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
13312 13429 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
13313 13430 state->dts_cred.dcr_visible |=
13314 13431 DTRACE_CRV_ALLPROC;
13315 13432
13316 13433 state->dts_cred.dcr_action |=
13317 13434 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13318 13435 }
13319 13436
13320 13437 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
13321 13438 state->dts_cred.dcr_visible |=
13322 13439 DTRACE_CRV_ALLZONE;
13323 13440
13324 13441 state->dts_cred.dcr_action |=
13325 13442 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13326 13443 }
13327 13444
13328 13445 /*
13329 13446 * If we have all privs in whatever zone this is,
13330 13447 * we can do destructive things to processes which
13331 13448 * have altered credentials.
13332 13449 */
13333 13450 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
13334 13451 cr->cr_zone->zone_privset)) {
13335 13452 state->dts_cred.dcr_action |=
13336 13453 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
13337 13454 }
13338 13455 }
13339 13456
13340 13457 /*
13341 13458 * Holding the dtrace_kernel privilege also implies that
13342 13459 * the user has the dtrace_user privilege from a visibility
13343 13460 * perspective. But without further privileges, some
13344 13461 * destructive actions are not available.
13345 13462 */
13346 13463 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
13347 13464 /*
13348 13465 * Make all probes in all zones visible. However,
13349 13466 * this doesn't mean that all actions become available
13350 13467 * to all zones.
13351 13468 */
13352 13469 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
13353 13470 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
13354 13471
13355 13472 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
13356 13473 DTRACE_CRA_PROC;
13357 13474 /*
13358 13475 * Holding proc_owner means that destructive actions
13359 13476 * for *this* zone are allowed.
13360 13477 */
13361 13478 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
13362 13479 state->dts_cred.dcr_action |=
13363 13480 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13364 13481
13365 13482 /*
13366 13483 * Holding proc_zone means that destructive actions
13367 13484 * for this user/group ID in all zones is allowed.
13368 13485 */
13369 13486 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
13370 13487 state->dts_cred.dcr_action |=
13371 13488 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13372 13489
13373 13490 /*
13374 13491 * If we have all privs in whatever zone this is,
13375 13492 * we can do destructive things to processes which
13376 13493 * have altered credentials.
13377 13494 */
13378 13495 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
13379 13496 cr->cr_zone->zone_privset)) {
13380 13497 state->dts_cred.dcr_action |=
13381 13498 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
13382 13499 }
13383 13500 }
13384 13501
13385 13502 /*
13386 13503 * Holding the dtrace_proc privilege gives control over fasttrap
13387 13504 * and pid providers. We need to grant wider destructive
13388 13505 * privileges in the event that the user has proc_owner and/or
13389 13506 * proc_zone.
13390 13507 */
13391 13508 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
13392 13509 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
13393 13510 state->dts_cred.dcr_action |=
13394 13511 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
13395 13512
13396 13513 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
13397 13514 state->dts_cred.dcr_action |=
13398 13515 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
13399 13516 }
13400 13517 }
13401 13518
13402 13519 return (state);
13403 13520 }
13404 13521
13405 13522 static int
13406 13523 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
13407 13524 {
13408 13525 dtrace_optval_t *opt = state->dts_options, size;
13409 13526 processorid_t cpu;
13410 13527 int flags = 0, rval, factor, divisor = 1;
13411 13528
13412 13529 ASSERT(MUTEX_HELD(&dtrace_lock));
13413 13530 ASSERT(MUTEX_HELD(&cpu_lock));
13414 13531 ASSERT(which < DTRACEOPT_MAX);
13415 13532 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
13416 13533 (state == dtrace_anon.dta_state &&
13417 13534 state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
13418 13535
13419 13536 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
13420 13537 return (0);
13421 13538
13422 13539 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
13423 13540 cpu = opt[DTRACEOPT_CPU];
13424 13541
13425 13542 if (which == DTRACEOPT_SPECSIZE)
13426 13543 flags |= DTRACEBUF_NOSWITCH;
13427 13544
13428 13545 if (which == DTRACEOPT_BUFSIZE) {
13429 13546 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
13430 13547 flags |= DTRACEBUF_RING;
13431 13548
13432 13549 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
13433 13550 flags |= DTRACEBUF_FILL;
13434 13551
13435 13552 if (state != dtrace_anon.dta_state ||
13436 13553 state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
13437 13554 flags |= DTRACEBUF_INACTIVE;
13438 13555 }
13439 13556
13440 13557 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) {
13441 13558 /*
13442 13559 * The size must be 8-byte aligned. If the size is not 8-byte
13443 13560 * aligned, drop it down by the difference.
13444 13561 */
13445 13562 if (size & (sizeof (uint64_t) - 1))
13446 13563 size -= size & (sizeof (uint64_t) - 1);
13447 13564
13448 13565 if (size < state->dts_reserve) {
13449 13566 /*
13450 13567 * Buffers always must be large enough to accommodate
13451 13568 * their prereserved space. We return E2BIG instead
13452 13569 * of ENOMEM in this case to allow for user-level
13453 13570 * software to differentiate the cases.
13454 13571 */
13455 13572 return (E2BIG);
13456 13573 }
13457 13574
13458 13575 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor);
13459 13576
13460 13577 if (rval != ENOMEM) {
13461 13578 opt[which] = size;
13462 13579 return (rval);
13463 13580 }
13464 13581
13465 13582 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13466 13583 return (rval);
13467 13584
13468 13585 for (divisor = 2; divisor < factor; divisor <<= 1)
13469 13586 continue;
13470 13587 }
13471 13588
13472 13589 return (ENOMEM);
13473 13590 }
13474 13591
13475 13592 static int
13476 13593 dtrace_state_buffers(dtrace_state_t *state)
13477 13594 {
13478 13595 dtrace_speculation_t *spec = state->dts_speculations;
13479 13596 int rval, i;
13480 13597
13481 13598 if ((rval = dtrace_state_buffer(state, state->dts_buffer,
13482 13599 DTRACEOPT_BUFSIZE)) != 0)
13483 13600 return (rval);
13484 13601
13485 13602 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
13486 13603 DTRACEOPT_AGGSIZE)) != 0)
13487 13604 return (rval);
13488 13605
13489 13606 for (i = 0; i < state->dts_nspeculations; i++) {
13490 13607 if ((rval = dtrace_state_buffer(state,
13491 13608 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
13492 13609 return (rval);
13493 13610 }
13494 13611
13495 13612 return (0);
13496 13613 }
13497 13614
13498 13615 static void
13499 13616 dtrace_state_prereserve(dtrace_state_t *state)
13500 13617 {
13501 13618 dtrace_ecb_t *ecb;
13502 13619 dtrace_probe_t *probe;
13503 13620
13504 13621 state->dts_reserve = 0;
13505 13622
13506 13623 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
13507 13624 return;
13508 13625
13509 13626 /*
13510 13627 * If our buffer policy is a "fill" buffer policy, we need to set the
13511 13628 * prereserved space to be the space required by the END probes.
13512 13629 */
13513 13630 probe = dtrace_probes[dtrace_probeid_end - 1];
13514 13631 ASSERT(probe != NULL);
13515 13632
13516 13633 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
13517 13634 if (ecb->dte_state != state)
13518 13635 continue;
13519 13636
13520 13637 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
13521 13638 }
13522 13639 }
13523 13640
13524 13641 static int
13525 13642 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
13526 13643 {
13527 13644 dtrace_optval_t *opt = state->dts_options, sz, nspec;
13528 13645 dtrace_speculation_t *spec;
13529 13646 dtrace_buffer_t *buf;
13530 13647 cyc_handler_t hdlr;
13531 13648 cyc_time_t when;
13532 13649 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
13533 13650 dtrace_icookie_t cookie;
13534 13651
13535 13652 mutex_enter(&cpu_lock);
13536 13653 mutex_enter(&dtrace_lock);
13537 13654
13538 13655 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
13539 13656 rval = EBUSY;
13540 13657 goto out;
13541 13658 }
13542 13659
13543 13660 /*
13544 13661 * Before we can perform any checks, we must prime all of the
13545 13662 * retained enablings that correspond to this state.
13546 13663 */
13547 13664 dtrace_enabling_prime(state);
13548 13665
13549 13666 if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
13550 13667 rval = EACCES;
13551 13668 goto out;
13552 13669 }
13553 13670
13554 13671 dtrace_state_prereserve(state);
13555 13672
13556 13673 /*
13557 13674 * Now we want to do is try to allocate our speculations.
13558 13675 * We do not automatically resize the number of speculations; if
13559 13676 * this fails, we will fail the operation.
13560 13677 */
13561 13678 nspec = opt[DTRACEOPT_NSPEC];
13562 13679 ASSERT(nspec != DTRACEOPT_UNSET);
13563 13680
13564 13681 if (nspec > INT_MAX) {
13565 13682 rval = ENOMEM;
13566 13683 goto out;
13567 13684 }
13568 13685
13569 13686 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t),
13570 13687 KM_NOSLEEP | KM_NORMALPRI);
13571 13688
13572 13689 if (spec == NULL) {
13573 13690 rval = ENOMEM;
13574 13691 goto out;
13575 13692 }
13576 13693
13577 13694 state->dts_speculations = spec;
13578 13695 state->dts_nspeculations = (int)nspec;
13579 13696
13580 13697 for (i = 0; i < nspec; i++) {
13581 13698 if ((buf = kmem_zalloc(bufsize,
13582 13699 KM_NOSLEEP | KM_NORMALPRI)) == NULL) {
13583 13700 rval = ENOMEM;
13584 13701 goto err;
13585 13702 }
13586 13703
13587 13704 spec[i].dtsp_buffer = buf;
13588 13705 }
13589 13706
13590 13707 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
13591 13708 if (dtrace_anon.dta_state == NULL) {
13592 13709 rval = ENOENT;
13593 13710 goto out;
13594 13711 }
13595 13712
13596 13713 if (state->dts_necbs != 0) {
13597 13714 rval = EALREADY;
13598 13715 goto out;
13599 13716 }
13600 13717
13601 13718 state->dts_anon = dtrace_anon_grab();
13602 13719 ASSERT(state->dts_anon != NULL);
13603 13720 state = state->dts_anon;
13604 13721
13605 13722 /*
13606 13723 * We want "grabanon" to be set in the grabbed state, so we'll
13607 13724 * copy that option value from the grabbing state into the
13608 13725 * grabbed state.
13609 13726 */
13610 13727 state->dts_options[DTRACEOPT_GRABANON] =
13611 13728 opt[DTRACEOPT_GRABANON];
13612 13729
13613 13730 *cpu = dtrace_anon.dta_beganon;
13614 13731
13615 13732 /*
13616 13733 * If the anonymous state is active (as it almost certainly
13617 13734 * is if the anonymous enabling ultimately matched anything),
13618 13735 * we don't allow any further option processing -- but we
13619 13736 * don't return failure.
13620 13737 */
13621 13738 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13622 13739 goto out;
13623 13740 }
13624 13741
13625 13742 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
13626 13743 opt[DTRACEOPT_AGGSIZE] != 0) {
13627 13744 if (state->dts_aggregations == NULL) {
13628 13745 /*
13629 13746 * We're not going to create an aggregation buffer
13630 13747 * because we don't have any ECBs that contain
13631 13748 * aggregations -- set this option to 0.
13632 13749 */
13633 13750 opt[DTRACEOPT_AGGSIZE] = 0;
13634 13751 } else {
13635 13752 /*
13636 13753 * If we have an aggregation buffer, we must also have
13637 13754 * a buffer to use as scratch.
13638 13755 */
13639 13756 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
13640 13757 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
13641 13758 opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
13642 13759 }
13643 13760 }
13644 13761 }
13645 13762
13646 13763 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
13647 13764 opt[DTRACEOPT_SPECSIZE] != 0) {
13648 13765 if (!state->dts_speculates) {
13649 13766 /*
13650 13767 * We're not going to create speculation buffers
13651 13768 * because we don't have any ECBs that actually
13652 13769 * speculate -- set the speculation size to 0.
13653 13770 */
13654 13771 opt[DTRACEOPT_SPECSIZE] = 0;
13655 13772 }
13656 13773 }
13657 13774
13658 13775 /*
13659 13776 * The bare minimum size for any buffer that we're actually going to
13660 13777 * do anything to is sizeof (uint64_t).
13661 13778 */
13662 13779 sz = sizeof (uint64_t);
13663 13780
13664 13781 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
13665 13782 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
13666 13783 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
13667 13784 /*
13668 13785 * A buffer size has been explicitly set to 0 (or to a size
13669 13786 * that will be adjusted to 0) and we need the space -- we
13670 13787 * need to return failure. We return ENOSPC to differentiate
13671 13788 * it from failing to allocate a buffer due to failure to meet
13672 13789 * the reserve (for which we return E2BIG).
13673 13790 */
13674 13791 rval = ENOSPC;
13675 13792 goto out;
13676 13793 }
13677 13794
13678 13795 if ((rval = dtrace_state_buffers(state)) != 0)
13679 13796 goto err;
13680 13797
13681 13798 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
13682 13799 sz = dtrace_dstate_defsize;
13683 13800
13684 13801 do {
13685 13802 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
13686 13803
13687 13804 if (rval == 0)
13688 13805 break;
13689 13806
13690 13807 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
13691 13808 goto err;
13692 13809 } while (sz >>= 1);
13693 13810
13694 13811 opt[DTRACEOPT_DYNVARSIZE] = sz;
13695 13812
13696 13813 if (rval != 0)
13697 13814 goto err;
13698 13815
13699 13816 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
13700 13817 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
13701 13818
13702 13819 if (opt[DTRACEOPT_CLEANRATE] == 0)
13703 13820 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13704 13821
13705 13822 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
13706 13823 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
13707 13824
13708 13825 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
13709 13826 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
13710 13827
13711 13828 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
13712 13829 hdlr.cyh_arg = state;
13713 13830 hdlr.cyh_level = CY_LOW_LEVEL;
13714 13831
13715 13832 when.cyt_when = 0;
13716 13833 when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
13717 13834
13718 13835 state->dts_cleaner = cyclic_add(&hdlr, &when);
13719 13836
13720 13837 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
13721 13838 hdlr.cyh_arg = state;
13722 13839 hdlr.cyh_level = CY_LOW_LEVEL;
13723 13840
13724 13841 when.cyt_when = 0;
13725 13842 when.cyt_interval = dtrace_deadman_interval;
13726 13843
13727 13844 state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
13728 13845 state->dts_deadman = cyclic_add(&hdlr, &when);
13729 13846
13730 13847 state->dts_activity = DTRACE_ACTIVITY_WARMUP;
13731 13848
13732 13849 if (state->dts_getf != 0 &&
13733 13850 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
13734 13851 /*
13735 13852 * We don't have kernel privs but we have at least one call
13736 13853 * to getf(); we need to bump our zone's count, and (if
13737 13854 * this is the first enabling to have an unprivileged call
13738 13855 * to getf()) we need to hook into closef().
13739 13856 */
13740 13857 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++;
13741 13858
13742 13859 if (dtrace_getf++ == 0) {
13743 13860 ASSERT(dtrace_closef == NULL);
13744 13861 dtrace_closef = dtrace_getf_barrier;
13745 13862 }
13746 13863 }
13747 13864
13748 13865 /*
13749 13866 * Now it's time to actually fire the BEGIN probe. We need to disable
13750 13867 * interrupts here both to record the CPU on which we fired the BEGIN
13751 13868 * probe (the data from this CPU will be processed first at user
13752 13869 * level) and to manually activate the buffer for this CPU.
13753 13870 */
13754 13871 cookie = dtrace_interrupt_disable();
13755 13872 *cpu = CPU->cpu_id;
13756 13873 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
13757 13874 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
13758 13875
13759 13876 dtrace_probe(dtrace_probeid_begin,
13760 13877 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13761 13878 dtrace_interrupt_enable(cookie);
13762 13879 /*
13763 13880 * We may have had an exit action from a BEGIN probe; only change our
13764 13881 * state to ACTIVE if we're still in WARMUP.
13765 13882 */
13766 13883 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
13767 13884 state->dts_activity == DTRACE_ACTIVITY_DRAINING);
13768 13885
13769 13886 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
13770 13887 state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
13771 13888
13772 13889 /*
13773 13890 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
13774 13891 * want each CPU to transition its principal buffer out of the
13775 13892 * INACTIVE state. Doing this assures that no CPU will suddenly begin
13776 13893 * processing an ECB halfway down a probe's ECB chain; all CPUs will
13777 13894 * atomically transition from processing none of a state's ECBs to
13778 13895 * processing all of them.
13779 13896 */
13780 13897 dtrace_xcall(DTRACE_CPUALL,
13781 13898 (dtrace_xcall_t)dtrace_buffer_activate, state);
13782 13899 goto out;
13783 13900
13784 13901 err:
13785 13902 dtrace_buffer_free(state->dts_buffer);
13786 13903 dtrace_buffer_free(state->dts_aggbuffer);
13787 13904
13788 13905 if ((nspec = state->dts_nspeculations) == 0) {
13789 13906 ASSERT(state->dts_speculations == NULL);
13790 13907 goto out;
13791 13908 }
13792 13909
13793 13910 spec = state->dts_speculations;
13794 13911 ASSERT(spec != NULL);
13795 13912
13796 13913 for (i = 0; i < state->dts_nspeculations; i++) {
13797 13914 if ((buf = spec[i].dtsp_buffer) == NULL)
13798 13915 break;
13799 13916
13800 13917 dtrace_buffer_free(buf);
13801 13918 kmem_free(buf, bufsize);
13802 13919 }
13803 13920
13804 13921 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
13805 13922 state->dts_nspeculations = 0;
13806 13923 state->dts_speculations = NULL;
13807 13924
13808 13925 out:
13809 13926 mutex_exit(&dtrace_lock);
13810 13927 mutex_exit(&cpu_lock);
13811 13928
13812 13929 return (rval);
13813 13930 }
13814 13931
13815 13932 static int
13816 13933 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
13817 13934 {
13818 13935 dtrace_icookie_t cookie;
13819 13936
13820 13937 ASSERT(MUTEX_HELD(&dtrace_lock));
13821 13938
13822 13939 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
13823 13940 state->dts_activity != DTRACE_ACTIVITY_DRAINING)
13824 13941 return (EINVAL);
13825 13942
13826 13943 /*
13827 13944 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
13828 13945 * to be sure that every CPU has seen it. See below for the details
13829 13946 * on why this is done.
13830 13947 */
13831 13948 state->dts_activity = DTRACE_ACTIVITY_DRAINING;
13832 13949 dtrace_sync();
13833 13950
13834 13951 /*
13835 13952 * By this point, it is impossible for any CPU to be still processing
13836 13953 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to
13837 13954 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
13838 13955 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe()
13839 13956 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
13840 13957 * iff we're in the END probe.
13841 13958 */
13842 13959 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
13843 13960 dtrace_sync();
13844 13961 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
13845 13962
13846 13963 /*
13847 13964 * Finally, we can release the reserve and call the END probe. We
13848 13965 * disable interrupts across calling the END probe to allow us to
13849 13966 * return the CPU on which we actually called the END probe. This
13850 13967 * allows user-land to be sure that this CPU's principal buffer is
13851 13968 * processed last.
13852 13969 */
13853 13970 state->dts_reserve = 0;
13854 13971
13855 13972 cookie = dtrace_interrupt_disable();
13856 13973 *cpu = CPU->cpu_id;
13857 13974 dtrace_probe(dtrace_probeid_end,
13858 13975 (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
13859 13976 dtrace_interrupt_enable(cookie);
13860 13977
13861 13978 state->dts_activity = DTRACE_ACTIVITY_STOPPED;
13862 13979 dtrace_sync();
13863 13980
13864 13981 if (state->dts_getf != 0 &&
13865 13982 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
13866 13983 /*
13867 13984 * We don't have kernel privs but we have at least one call
13868 13985 * to getf(); we need to lower our zone's count, and (if
13869 13986 * this is the last enabling to have an unprivileged call
13870 13987 * to getf()) we need to clear the closef() hook.
13871 13988 */
13872 13989 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0);
13873 13990 ASSERT(dtrace_closef == dtrace_getf_barrier);
13874 13991 ASSERT(dtrace_getf > 0);
13875 13992
13876 13993 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--;
13877 13994
13878 13995 if (--dtrace_getf == 0)
13879 13996 dtrace_closef = NULL;
13880 13997 }
13881 13998
13882 13999 return (0);
13883 14000 }
13884 14001
13885 14002 static int
13886 14003 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
13887 14004 dtrace_optval_t val)
13888 14005 {
13889 14006 ASSERT(MUTEX_HELD(&dtrace_lock));
13890 14007
13891 14008 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
13892 14009 return (EBUSY);
13893 14010
13894 14011 if (option >= DTRACEOPT_MAX)
13895 14012 return (EINVAL);
13896 14013
13897 14014 if (option != DTRACEOPT_CPU && val < 0)
13898 14015 return (EINVAL);
13899 14016
13900 14017 switch (option) {
13901 14018 case DTRACEOPT_DESTRUCTIVE:
13902 14019 if (dtrace_destructive_disallow)
13903 14020 return (EACCES);
13904 14021
13905 14022 state->dts_cred.dcr_destructive = 1;
13906 14023 break;
13907 14024
13908 14025 case DTRACEOPT_BUFSIZE:
13909 14026 case DTRACEOPT_DYNVARSIZE:
13910 14027 case DTRACEOPT_AGGSIZE:
13911 14028 case DTRACEOPT_SPECSIZE:
13912 14029 case DTRACEOPT_STRSIZE:
13913 14030 if (val < 0)
13914 14031 return (EINVAL);
13915 14032
13916 14033 if (val >= LONG_MAX) {
13917 14034 /*
13918 14035 * If this is an otherwise negative value, set it to
13919 14036 * the highest multiple of 128m less than LONG_MAX.
13920 14037 * Technically, we're adjusting the size without
13921 14038 * regard to the buffer resizing policy, but in fact,
13922 14039 * this has no effect -- if we set the buffer size to
13923 14040 * ~LONG_MAX and the buffer policy is ultimately set to
13924 14041 * be "manual", the buffer allocation is guaranteed to
13925 14042 * fail, if only because the allocation requires two
13926 14043 * buffers. (We set the the size to the highest
13927 14044 * multiple of 128m because it ensures that the size
13928 14045 * will remain a multiple of a megabyte when
13929 14046 * repeatedly halved -- all the way down to 15m.)
13930 14047 */
13931 14048 val = LONG_MAX - (1 << 27) + 1;
13932 14049 }
13933 14050 }
13934 14051
13935 14052 state->dts_options[option] = val;
13936 14053
13937 14054 return (0);
13938 14055 }
13939 14056
13940 14057 static void
13941 14058 dtrace_state_destroy(dtrace_state_t *state)
13942 14059 {
13943 14060 dtrace_ecb_t *ecb;
13944 14061 dtrace_vstate_t *vstate = &state->dts_vstate;
13945 14062 minor_t minor = getminor(state->dts_dev);
13946 14063 int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
13947 14064 dtrace_speculation_t *spec = state->dts_speculations;
13948 14065 int nspec = state->dts_nspeculations;
13949 14066 uint32_t match;
13950 14067
13951 14068 ASSERT(MUTEX_HELD(&dtrace_lock));
13952 14069 ASSERT(MUTEX_HELD(&cpu_lock));
13953 14070
13954 14071 /*
13955 14072 * First, retract any retained enablings for this state.
13956 14073 */
13957 14074 dtrace_enabling_retract(state);
13958 14075 ASSERT(state->dts_nretained == 0);
13959 14076
13960 14077 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
13961 14078 state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
13962 14079 /*
13963 14080 * We have managed to come into dtrace_state_destroy() on a
13964 14081 * hot enabling -- almost certainly because of a disorderly
13965 14082 * shutdown of a consumer. (That is, a consumer that is
13966 14083 * exiting without having called dtrace_stop().) In this case,
13967 14084 * we're going to set our activity to be KILLED, and then
13968 14085 * issue a sync to be sure that everyone is out of probe
13969 14086 * context before we start blowing away ECBs.
13970 14087 */
13971 14088 state->dts_activity = DTRACE_ACTIVITY_KILLED;
13972 14089 dtrace_sync();
13973 14090 }
13974 14091
13975 14092 /*
13976 14093 * Release the credential hold we took in dtrace_state_create().
13977 14094 */
13978 14095 if (state->dts_cred.dcr_cred != NULL)
13979 14096 crfree(state->dts_cred.dcr_cred);
13980 14097
13981 14098 /*
13982 14099 * Now we can safely disable and destroy any enabled probes. Because
13983 14100 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
13984 14101 * (especially if they're all enabled), we take two passes through the
13985 14102 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and
13986 14103 * in the second we disable whatever is left over.
13987 14104 */
13988 14105 for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
13989 14106 for (i = 0; i < state->dts_necbs; i++) {
13990 14107 if ((ecb = state->dts_ecbs[i]) == NULL)
13991 14108 continue;
13992 14109
13993 14110 if (match && ecb->dte_probe != NULL) {
13994 14111 dtrace_probe_t *probe = ecb->dte_probe;
13995 14112 dtrace_provider_t *prov = probe->dtpr_provider;
13996 14113
13997 14114 if (!(prov->dtpv_priv.dtpp_flags & match))
13998 14115 continue;
13999 14116 }
14000 14117
14001 14118 dtrace_ecb_disable(ecb);
14002 14119 dtrace_ecb_destroy(ecb);
14003 14120 }
14004 14121
14005 14122 if (!match)
14006 14123 break;
14007 14124 }
14008 14125
14009 14126 /*
14010 14127 * Before we free the buffers, perform one more sync to assure that
14011 14128 * every CPU is out of probe context.
14012 14129 */
14013 14130 dtrace_sync();
14014 14131
14015 14132 dtrace_buffer_free(state->dts_buffer);
14016 14133 dtrace_buffer_free(state->dts_aggbuffer);
14017 14134
14018 14135 for (i = 0; i < nspec; i++)
14019 14136 dtrace_buffer_free(spec[i].dtsp_buffer);
14020 14137
14021 14138 if (state->dts_cleaner != CYCLIC_NONE)
14022 14139 cyclic_remove(state->dts_cleaner);
14023 14140
14024 14141 if (state->dts_deadman != CYCLIC_NONE)
14025 14142 cyclic_remove(state->dts_deadman);
14026 14143
14027 14144 dtrace_dstate_fini(&vstate->dtvs_dynvars);
14028 14145 dtrace_vstate_fini(vstate);
14029 14146 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
14030 14147
14031 14148 if (state->dts_aggregations != NULL) {
14032 14149 #ifdef DEBUG
14033 14150 for (i = 0; i < state->dts_naggregations; i++)
14034 14151 ASSERT(state->dts_aggregations[i] == NULL);
14035 14152 #endif
14036 14153 ASSERT(state->dts_naggregations > 0);
14037 14154 kmem_free(state->dts_aggregations,
14038 14155 state->dts_naggregations * sizeof (dtrace_aggregation_t *));
14039 14156 }
14040 14157
14041 14158 kmem_free(state->dts_buffer, bufsize);
14042 14159 kmem_free(state->dts_aggbuffer, bufsize);
14043 14160
14044 14161 for (i = 0; i < nspec; i++)
14045 14162 kmem_free(spec[i].dtsp_buffer, bufsize);
14046 14163
14047 14164 kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
14048 14165
14049 14166 dtrace_format_destroy(state);
14050 14167
14051 14168 vmem_destroy(state->dts_aggid_arena);
14052 14169 ddi_soft_state_free(dtrace_softstate, minor);
14053 14170 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
14054 14171 }
14055 14172
14056 14173 /*
14057 14174 * DTrace Anonymous Enabling Functions
14058 14175 */
14059 14176 static dtrace_state_t *
14060 14177 dtrace_anon_grab(void)
14061 14178 {
14062 14179 dtrace_state_t *state;
14063 14180
14064 14181 ASSERT(MUTEX_HELD(&dtrace_lock));
14065 14182
14066 14183 if ((state = dtrace_anon.dta_state) == NULL) {
14067 14184 ASSERT(dtrace_anon.dta_enabling == NULL);
14068 14185 return (NULL);
14069 14186 }
14070 14187
14071 14188 ASSERT(dtrace_anon.dta_enabling != NULL);
14072 14189 ASSERT(dtrace_retained != NULL);
14073 14190
14074 14191 dtrace_enabling_destroy(dtrace_anon.dta_enabling);
14075 14192 dtrace_anon.dta_enabling = NULL;
14076 14193 dtrace_anon.dta_state = NULL;
14077 14194
14078 14195 return (state);
14079 14196 }
14080 14197
14081 14198 static void
14082 14199 dtrace_anon_property(void)
14083 14200 {
14084 14201 int i, rv;
14085 14202 dtrace_state_t *state;
14086 14203 dof_hdr_t *dof;
14087 14204 char c[32]; /* enough for "dof-data-" + digits */
14088 14205
14089 14206 ASSERT(MUTEX_HELD(&dtrace_lock));
14090 14207 ASSERT(MUTEX_HELD(&cpu_lock));
14091 14208
14092 14209 for (i = 0; ; i++) {
14093 14210 (void) snprintf(c, sizeof (c), "dof-data-%d", i);
14094 14211
14095 14212 dtrace_err_verbose = 1;
14096 14213
14097 14214 if ((dof = dtrace_dof_property(c)) == NULL) {
14098 14215 dtrace_err_verbose = 0;
14099 14216 break;
14100 14217 }
14101 14218
14102 14219 /*
14103 14220 * We want to create anonymous state, so we need to transition
14104 14221 * the kernel debugger to indicate that DTrace is active. If
14105 14222 * this fails (e.g. because the debugger has modified text in
14106 14223 * some way), we won't continue with the processing.
14107 14224 */
14108 14225 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
14109 14226 cmn_err(CE_NOTE, "kernel debugger active; anonymous "
14110 14227 "enabling ignored.");
14111 14228 dtrace_dof_destroy(dof);
14112 14229 break;
14113 14230 }
14114 14231
14115 14232 /*
14116 14233 * If we haven't allocated an anonymous state, we'll do so now.
14117 14234 */
14118 14235 if ((state = dtrace_anon.dta_state) == NULL) {
14119 14236 state = dtrace_state_create(NULL, NULL);
14120 14237 dtrace_anon.dta_state = state;
14121 14238
14122 14239 if (state == NULL) {
14123 14240 /*
14124 14241 * This basically shouldn't happen: the only
14125 14242 * failure mode from dtrace_state_create() is a
14126 14243 * failure of ddi_soft_state_zalloc() that
14127 14244 * itself should never happen. Still, the
14128 14245 * interface allows for a failure mode, and
14129 14246 * we want to fail as gracefully as possible:
14130 14247 * we'll emit an error message and cease
14131 14248 * processing anonymous state in this case.
14132 14249 */
14133 14250 cmn_err(CE_WARN, "failed to create "
14134 14251 "anonymous state");
14135 14252 dtrace_dof_destroy(dof);
14136 14253 break;
14137 14254 }
14138 14255 }
14139 14256
14140 14257 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
14141 14258 &dtrace_anon.dta_enabling, 0, B_TRUE);
14142 14259
14143 14260 if (rv == 0)
14144 14261 rv = dtrace_dof_options(dof, state);
14145 14262
14146 14263 dtrace_err_verbose = 0;
14147 14264 dtrace_dof_destroy(dof);
14148 14265
14149 14266 if (rv != 0) {
14150 14267 /*
14151 14268 * This is malformed DOF; chuck any anonymous state
14152 14269 * that we created.
14153 14270 */
14154 14271 ASSERT(dtrace_anon.dta_enabling == NULL);
14155 14272 dtrace_state_destroy(state);
14156 14273 dtrace_anon.dta_state = NULL;
14157 14274 break;
14158 14275 }
14159 14276
14160 14277 ASSERT(dtrace_anon.dta_enabling != NULL);
14161 14278 }
14162 14279
14163 14280 if (dtrace_anon.dta_enabling != NULL) {
14164 14281 int rval;
14165 14282
14166 14283 /*
14167 14284 * dtrace_enabling_retain() can only fail because we are
14168 14285 * trying to retain more enablings than are allowed -- but
14169 14286 * we only have one anonymous enabling, and we are guaranteed
14170 14287 * to be allowed at least one retained enabling; we assert
14171 14288 * that dtrace_enabling_retain() returns success.
14172 14289 */
14173 14290 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
14174 14291 ASSERT(rval == 0);
14175 14292
14176 14293 dtrace_enabling_dump(dtrace_anon.dta_enabling);
14177 14294 }
14178 14295 }
14179 14296
14180 14297 /*
14181 14298 * DTrace Helper Functions
14182 14299 */
14183 14300 static void
14184 14301 dtrace_helper_trace(dtrace_helper_action_t *helper,
14185 14302 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
14186 14303 {
14187 14304 uint32_t size, next, nnext, i;
14188 14305 dtrace_helptrace_t *ent, *buffer;
14189 14306 uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
14190 14307
14191 14308 if ((buffer = dtrace_helptrace_buffer) == NULL)
14192 14309 return;
14193 14310
14194 14311 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
14195 14312
14196 14313 /*
14197 14314 * What would a tracing framework be without its own tracing
14198 14315 * framework? (Well, a hell of a lot simpler, for starters...)
14199 14316 */
14200 14317 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
14201 14318 sizeof (uint64_t) - sizeof (uint64_t);
14202 14319
14203 14320 /*
14204 14321 * Iterate until we can allocate a slot in the trace buffer.
14205 14322 */
14206 14323 do {
14207 14324 next = dtrace_helptrace_next;
14208 14325
14209 14326 if (next + size < dtrace_helptrace_bufsize) {
14210 14327 nnext = next + size;
14211 14328 } else {
14212 14329 nnext = size;
14213 14330 }
14214 14331 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
14215 14332
14216 14333 /*
14217 14334 * We have our slot; fill it in.
14218 14335 */
14219 14336 if (nnext == size) {
14220 14337 dtrace_helptrace_wrapped++;
14221 14338 next = 0;
14222 14339 }
14223 14340
14224 14341 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next);
14225 14342 ent->dtht_helper = helper;
14226 14343 ent->dtht_where = where;
14227 14344 ent->dtht_nlocals = vstate->dtvs_nlocals;
14228 14345
14229 14346 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
14230 14347 mstate->dtms_fltoffs : -1;
14231 14348 ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
14232 14349 ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
14233 14350
14234 14351 for (i = 0; i < vstate->dtvs_nlocals; i++) {
14235 14352 dtrace_statvar_t *svar;
14236 14353
14237 14354 if ((svar = vstate->dtvs_locals[i]) == NULL)
14238 14355 continue;
14239 14356
14240 14357 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
14241 14358 ent->dtht_locals[i] =
14242 14359 ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id];
14243 14360 }
14244 14361 }
14245 14362
14246 14363 static uint64_t
14247 14364 dtrace_helper(int which, dtrace_mstate_t *mstate,
14248 14365 dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
14249 14366 {
14250 14367 uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
14251 14368 uint64_t sarg0 = mstate->dtms_arg[0];
14252 14369 uint64_t sarg1 = mstate->dtms_arg[1];
14253 14370 uint64_t rval;
14254 14371 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
14255 14372 dtrace_helper_action_t *helper;
14256 14373 dtrace_vstate_t *vstate;
14257 14374 dtrace_difo_t *pred;
14258 14375 int i, trace = dtrace_helptrace_buffer != NULL;
14259 14376
14260 14377 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
14261 14378
14262 14379 if (helpers == NULL)
14263 14380 return (0);
14264 14381
14265 14382 if ((helper = helpers->dthps_actions[which]) == NULL)
14266 14383 return (0);
14267 14384
14268 14385 vstate = &helpers->dthps_vstate;
14269 14386 mstate->dtms_arg[0] = arg0;
14270 14387 mstate->dtms_arg[1] = arg1;
14271 14388
14272 14389 /*
14273 14390 * Now iterate over each helper. If its predicate evaluates to 'true',
14274 14391 * we'll call the corresponding actions. Note that the below calls
14275 14392 * to dtrace_dif_emulate() may set faults in machine state. This is
14276 14393 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow
14277 14394 * the stored DIF offset with its own (which is the desired behavior).
14278 14395 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
14279 14396 * from machine state; this is okay, too.
14280 14397 */
14281 14398 for (; helper != NULL; helper = helper->dtha_next) {
14282 14399 if ((pred = helper->dtha_predicate) != NULL) {
14283 14400 if (trace)
14284 14401 dtrace_helper_trace(helper, mstate, vstate, 0);
14285 14402
14286 14403 if (!dtrace_dif_emulate(pred, mstate, vstate, state))
14287 14404 goto next;
14288 14405
14289 14406 if (*flags & CPU_DTRACE_FAULT)
14290 14407 goto err;
14291 14408 }
14292 14409
14293 14410 for (i = 0; i < helper->dtha_nactions; i++) {
14294 14411 if (trace)
14295 14412 dtrace_helper_trace(helper,
14296 14413 mstate, vstate, i + 1);
14297 14414
14298 14415 rval = dtrace_dif_emulate(helper->dtha_actions[i],
14299 14416 mstate, vstate, state);
14300 14417
14301 14418 if (*flags & CPU_DTRACE_FAULT)
14302 14419 goto err;
14303 14420 }
14304 14421
14305 14422 next:
14306 14423 if (trace)
14307 14424 dtrace_helper_trace(helper, mstate, vstate,
14308 14425 DTRACE_HELPTRACE_NEXT);
14309 14426 }
14310 14427
14311 14428 if (trace)
14312 14429 dtrace_helper_trace(helper, mstate, vstate,
14313 14430 DTRACE_HELPTRACE_DONE);
14314 14431
14315 14432 /*
14316 14433 * Restore the arg0 that we saved upon entry.
14317 14434 */
14318 14435 mstate->dtms_arg[0] = sarg0;
14319 14436 mstate->dtms_arg[1] = sarg1;
14320 14437
14321 14438 return (rval);
14322 14439
14323 14440 err:
14324 14441 if (trace)
14325 14442 dtrace_helper_trace(helper, mstate, vstate,
14326 14443 DTRACE_HELPTRACE_ERR);
14327 14444
14328 14445 /*
14329 14446 * Restore the arg0 that we saved upon entry.
14330 14447 */
14331 14448 mstate->dtms_arg[0] = sarg0;
14332 14449 mstate->dtms_arg[1] = sarg1;
14333 14450
14334 14451 return (NULL);
14335 14452 }
14336 14453
14337 14454 static void
14338 14455 dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
14339 14456 dtrace_vstate_t *vstate)
14340 14457 {
14341 14458 int i;
14342 14459
14343 14460 if (helper->dtha_predicate != NULL)
14344 14461 dtrace_difo_release(helper->dtha_predicate, vstate);
14345 14462
14346 14463 for (i = 0; i < helper->dtha_nactions; i++) {
14347 14464 ASSERT(helper->dtha_actions[i] != NULL);
14348 14465 dtrace_difo_release(helper->dtha_actions[i], vstate);
14349 14466 }
14350 14467
14351 14468 kmem_free(helper->dtha_actions,
14352 14469 helper->dtha_nactions * sizeof (dtrace_difo_t *));
14353 14470 kmem_free(helper, sizeof (dtrace_helper_action_t));
14354 14471 }
14355 14472
14356 14473 static int
14357 14474 dtrace_helper_destroygen(int gen)
14358 14475 {
14359 14476 proc_t *p = curproc;
14360 14477 dtrace_helpers_t *help = p->p_dtrace_helpers;
14361 14478 dtrace_vstate_t *vstate;
14362 14479 int i;
14363 14480
14364 14481 ASSERT(MUTEX_HELD(&dtrace_lock));
14365 14482
14366 14483 if (help == NULL || gen > help->dthps_generation)
14367 14484 return (EINVAL);
14368 14485
14369 14486 vstate = &help->dthps_vstate;
14370 14487
14371 14488 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
14372 14489 dtrace_helper_action_t *last = NULL, *h, *next;
14373 14490
14374 14491 for (h = help->dthps_actions[i]; h != NULL; h = next) {
14375 14492 next = h->dtha_next;
14376 14493
14377 14494 if (h->dtha_generation == gen) {
14378 14495 if (last != NULL) {
14379 14496 last->dtha_next = next;
14380 14497 } else {
14381 14498 help->dthps_actions[i] = next;
14382 14499 }
14383 14500
14384 14501 dtrace_helper_action_destroy(h, vstate);
14385 14502 } else {
14386 14503 last = h;
14387 14504 }
14388 14505 }
14389 14506 }
14390 14507
14391 14508 /*
14392 14509 * Interate until we've cleared out all helper providers with the
14393 14510 * given generation number.
14394 14511 */
14395 14512 for (;;) {
14396 14513 dtrace_helper_provider_t *prov;
14397 14514
14398 14515 /*
14399 14516 * Look for a helper provider with the right generation. We
14400 14517 * have to start back at the beginning of the list each time
14401 14518 * because we drop dtrace_lock. It's unlikely that we'll make
14402 14519 * more than two passes.
14403 14520 */
14404 14521 for (i = 0; i < help->dthps_nprovs; i++) {
14405 14522 prov = help->dthps_provs[i];
14406 14523
14407 14524 if (prov->dthp_generation == gen)
14408 14525 break;
14409 14526 }
14410 14527
14411 14528 /*
14412 14529 * If there were no matches, we're done.
14413 14530 */
14414 14531 if (i == help->dthps_nprovs)
14415 14532 break;
14416 14533
14417 14534 /*
14418 14535 * Move the last helper provider into this slot.
14419 14536 */
14420 14537 help->dthps_nprovs--;
14421 14538 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
14422 14539 help->dthps_provs[help->dthps_nprovs] = NULL;
14423 14540
14424 14541 mutex_exit(&dtrace_lock);
14425 14542
14426 14543 /*
14427 14544 * If we have a meta provider, remove this helper provider.
14428 14545 */
14429 14546 mutex_enter(&dtrace_meta_lock);
14430 14547 if (dtrace_meta_pid != NULL) {
14431 14548 ASSERT(dtrace_deferred_pid == NULL);
14432 14549 dtrace_helper_provider_remove(&prov->dthp_prov,
14433 14550 p->p_pid);
14434 14551 }
14435 14552 mutex_exit(&dtrace_meta_lock);
14436 14553
14437 14554 dtrace_helper_provider_destroy(prov);
14438 14555
14439 14556 mutex_enter(&dtrace_lock);
14440 14557 }
14441 14558
14442 14559 return (0);
14443 14560 }
14444 14561
14445 14562 static int
14446 14563 dtrace_helper_validate(dtrace_helper_action_t *helper)
14447 14564 {
14448 14565 int err = 0, i;
14449 14566 dtrace_difo_t *dp;
14450 14567
14451 14568 if ((dp = helper->dtha_predicate) != NULL)
14452 14569 err += dtrace_difo_validate_helper(dp);
14453 14570
14454 14571 for (i = 0; i < helper->dtha_nactions; i++)
14455 14572 err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
14456 14573
14457 14574 return (err == 0);
14458 14575 }
14459 14576
14460 14577 static int
14461 14578 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep)
14462 14579 {
14463 14580 dtrace_helpers_t *help;
14464 14581 dtrace_helper_action_t *helper, *last;
14465 14582 dtrace_actdesc_t *act;
14466 14583 dtrace_vstate_t *vstate;
14467 14584 dtrace_predicate_t *pred;
14468 14585 int count = 0, nactions = 0, i;
14469 14586
14470 14587 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
14471 14588 return (EINVAL);
14472 14589
14473 14590 help = curproc->p_dtrace_helpers;
14474 14591 last = help->dthps_actions[which];
14475 14592 vstate = &help->dthps_vstate;
14476 14593
14477 14594 for (count = 0; last != NULL; last = last->dtha_next) {
14478 14595 count++;
14479 14596 if (last->dtha_next == NULL)
14480 14597 break;
14481 14598 }
14482 14599
14483 14600 /*
14484 14601 * If we already have dtrace_helper_actions_max helper actions for this
14485 14602 * helper action type, we'll refuse to add a new one.
14486 14603 */
14487 14604 if (count >= dtrace_helper_actions_max)
14488 14605 return (ENOSPC);
14489 14606
14490 14607 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
14491 14608 helper->dtha_generation = help->dthps_generation;
14492 14609
14493 14610 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
14494 14611 ASSERT(pred->dtp_difo != NULL);
14495 14612 dtrace_difo_hold(pred->dtp_difo);
14496 14613 helper->dtha_predicate = pred->dtp_difo;
14497 14614 }
14498 14615
14499 14616 for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
14500 14617 if (act->dtad_kind != DTRACEACT_DIFEXPR)
14501 14618 goto err;
14502 14619
14503 14620 if (act->dtad_difo == NULL)
14504 14621 goto err;
14505 14622
14506 14623 nactions++;
14507 14624 }
14508 14625
14509 14626 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
14510 14627 (helper->dtha_nactions = nactions), KM_SLEEP);
14511 14628
14512 14629 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
14513 14630 dtrace_difo_hold(act->dtad_difo);
14514 14631 helper->dtha_actions[i++] = act->dtad_difo;
14515 14632 }
14516 14633
14517 14634 if (!dtrace_helper_validate(helper))
14518 14635 goto err;
14519 14636
14520 14637 if (last == NULL) {
14521 14638 help->dthps_actions[which] = helper;
14522 14639 } else {
14523 14640 last->dtha_next = helper;
14524 14641 }
14525 14642
14526 14643 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
14527 14644 dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
14528 14645 dtrace_helptrace_next = 0;
14529 14646 }
14530 14647
14531 14648 return (0);
14532 14649 err:
14533 14650 dtrace_helper_action_destroy(helper, vstate);
14534 14651 return (EINVAL);
14535 14652 }
14536 14653
14537 14654 static void
14538 14655 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
14539 14656 dof_helper_t *dofhp)
14540 14657 {
14541 14658 ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
14542 14659
14543 14660 mutex_enter(&dtrace_meta_lock);
14544 14661 mutex_enter(&dtrace_lock);
14545 14662
14546 14663 if (!dtrace_attached() || dtrace_meta_pid == NULL) {
14547 14664 /*
14548 14665 * If the dtrace module is loaded but not attached, or if
14549 14666 * there aren't isn't a meta provider registered to deal with
14550 14667 * these provider descriptions, we need to postpone creating
14551 14668 * the actual providers until later.
14552 14669 */
14553 14670
14554 14671 if (help->dthps_next == NULL && help->dthps_prev == NULL &&
14555 14672 dtrace_deferred_pid != help) {
14556 14673 help->dthps_deferred = 1;
14557 14674 help->dthps_pid = p->p_pid;
14558 14675 help->dthps_next = dtrace_deferred_pid;
14559 14676 help->dthps_prev = NULL;
14560 14677 if (dtrace_deferred_pid != NULL)
14561 14678 dtrace_deferred_pid->dthps_prev = help;
14562 14679 dtrace_deferred_pid = help;
14563 14680 }
14564 14681
14565 14682 mutex_exit(&dtrace_lock);
14566 14683
14567 14684 } else if (dofhp != NULL) {
14568 14685 /*
14569 14686 * If the dtrace module is loaded and we have a particular
14570 14687 * helper provider description, pass that off to the
14571 14688 * meta provider.
14572 14689 */
14573 14690
14574 14691 mutex_exit(&dtrace_lock);
14575 14692
14576 14693 dtrace_helper_provide(dofhp, p->p_pid);
14577 14694
14578 14695 } else {
14579 14696 /*
14580 14697 * Otherwise, just pass all the helper provider descriptions
14581 14698 * off to the meta provider.
14582 14699 */
14583 14700
14584 14701 int i;
14585 14702 mutex_exit(&dtrace_lock);
14586 14703
14587 14704 for (i = 0; i < help->dthps_nprovs; i++) {
14588 14705 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
14589 14706 p->p_pid);
14590 14707 }
14591 14708 }
14592 14709
14593 14710 mutex_exit(&dtrace_meta_lock);
14594 14711 }
14595 14712
14596 14713 static int
14597 14714 dtrace_helper_provider_add(dof_helper_t *dofhp, int gen)
14598 14715 {
14599 14716 dtrace_helpers_t *help;
14600 14717 dtrace_helper_provider_t *hprov, **tmp_provs;
14601 14718 uint_t tmp_maxprovs, i;
14602 14719
14603 14720 ASSERT(MUTEX_HELD(&dtrace_lock));
14604 14721
14605 14722 help = curproc->p_dtrace_helpers;
14606 14723 ASSERT(help != NULL);
14607 14724
14608 14725 /*
14609 14726 * If we already have dtrace_helper_providers_max helper providers,
14610 14727 * we're refuse to add a new one.
14611 14728 */
14612 14729 if (help->dthps_nprovs >= dtrace_helper_providers_max)
14613 14730 return (ENOSPC);
14614 14731
14615 14732 /*
14616 14733 * Check to make sure this isn't a duplicate.
14617 14734 */
14618 14735 for (i = 0; i < help->dthps_nprovs; i++) {
14619 14736 if (dofhp->dofhp_addr ==
14620 14737 help->dthps_provs[i]->dthp_prov.dofhp_addr)
14621 14738 return (EALREADY);
14622 14739 }
14623 14740
14624 14741 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
14625 14742 hprov->dthp_prov = *dofhp;
14626 14743 hprov->dthp_ref = 1;
14627 14744 hprov->dthp_generation = gen;
14628 14745
14629 14746 /*
14630 14747 * Allocate a bigger table for helper providers if it's already full.
14631 14748 */
14632 14749 if (help->dthps_maxprovs == help->dthps_nprovs) {
14633 14750 tmp_maxprovs = help->dthps_maxprovs;
14634 14751 tmp_provs = help->dthps_provs;
14635 14752
14636 14753 if (help->dthps_maxprovs == 0)
14637 14754 help->dthps_maxprovs = 2;
14638 14755 else
14639 14756 help->dthps_maxprovs *= 2;
14640 14757 if (help->dthps_maxprovs > dtrace_helper_providers_max)
14641 14758 help->dthps_maxprovs = dtrace_helper_providers_max;
14642 14759
14643 14760 ASSERT(tmp_maxprovs < help->dthps_maxprovs);
14644 14761
14645 14762 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
14646 14763 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
14647 14764
14648 14765 if (tmp_provs != NULL) {
14649 14766 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
14650 14767 sizeof (dtrace_helper_provider_t *));
14651 14768 kmem_free(tmp_provs, tmp_maxprovs *
14652 14769 sizeof (dtrace_helper_provider_t *));
14653 14770 }
14654 14771 }
14655 14772
14656 14773 help->dthps_provs[help->dthps_nprovs] = hprov;
14657 14774 help->dthps_nprovs++;
14658 14775
14659 14776 return (0);
14660 14777 }
14661 14778
14662 14779 static void
14663 14780 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
14664 14781 {
14665 14782 mutex_enter(&dtrace_lock);
14666 14783
14667 14784 if (--hprov->dthp_ref == 0) {
14668 14785 dof_hdr_t *dof;
14669 14786 mutex_exit(&dtrace_lock);
14670 14787 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
14671 14788 dtrace_dof_destroy(dof);
14672 14789 kmem_free(hprov, sizeof (dtrace_helper_provider_t));
14673 14790 } else {
14674 14791 mutex_exit(&dtrace_lock);
14675 14792 }
14676 14793 }
14677 14794
14678 14795 static int
14679 14796 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
14680 14797 {
14681 14798 uintptr_t daddr = (uintptr_t)dof;
14682 14799 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
14683 14800 dof_provider_t *provider;
14684 14801 dof_probe_t *probe;
14685 14802 uint8_t *arg;
14686 14803 char *strtab, *typestr;
14687 14804 dof_stridx_t typeidx;
14688 14805 size_t typesz;
14689 14806 uint_t nprobes, j, k;
14690 14807
14691 14808 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
14692 14809
14693 14810 if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
14694 14811 dtrace_dof_error(dof, "misaligned section offset");
14695 14812 return (-1);
14696 14813 }
14697 14814
14698 14815 /*
14699 14816 * The section needs to be large enough to contain the DOF provider
14700 14817 * structure appropriate for the given version.
14701 14818 */
14702 14819 if (sec->dofs_size <
14703 14820 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
14704 14821 offsetof(dof_provider_t, dofpv_prenoffs) :
14705 14822 sizeof (dof_provider_t))) {
14706 14823 dtrace_dof_error(dof, "provider section too small");
14707 14824 return (-1);
14708 14825 }
14709 14826
14710 14827 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
14711 14828 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
14712 14829 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
14713 14830 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
14714 14831 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
14715 14832
14716 14833 if (str_sec == NULL || prb_sec == NULL ||
14717 14834 arg_sec == NULL || off_sec == NULL)
14718 14835 return (-1);
14719 14836
14720 14837 enoff_sec = NULL;
14721 14838
14722 14839 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
14723 14840 provider->dofpv_prenoffs != DOF_SECT_NONE &&
14724 14841 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
14725 14842 provider->dofpv_prenoffs)) == NULL)
14726 14843 return (-1);
14727 14844
14728 14845 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
14729 14846
14730 14847 if (provider->dofpv_name >= str_sec->dofs_size ||
14731 14848 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
14732 14849 dtrace_dof_error(dof, "invalid provider name");
14733 14850 return (-1);
14734 14851 }
14735 14852
14736 14853 if (prb_sec->dofs_entsize == 0 ||
14737 14854 prb_sec->dofs_entsize > prb_sec->dofs_size) {
14738 14855 dtrace_dof_error(dof, "invalid entry size");
14739 14856 return (-1);
14740 14857 }
14741 14858
14742 14859 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
14743 14860 dtrace_dof_error(dof, "misaligned entry size");
14744 14861 return (-1);
14745 14862 }
14746 14863
14747 14864 if (off_sec->dofs_entsize != sizeof (uint32_t)) {
14748 14865 dtrace_dof_error(dof, "invalid entry size");
14749 14866 return (-1);
14750 14867 }
14751 14868
14752 14869 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
14753 14870 dtrace_dof_error(dof, "misaligned section offset");
14754 14871 return (-1);
14755 14872 }
14756 14873
14757 14874 if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
14758 14875 dtrace_dof_error(dof, "invalid entry size");
14759 14876 return (-1);
14760 14877 }
14761 14878
14762 14879 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
14763 14880
14764 14881 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
14765 14882
14766 14883 /*
14767 14884 * Take a pass through the probes to check for errors.
14768 14885 */
14769 14886 for (j = 0; j < nprobes; j++) {
14770 14887 probe = (dof_probe_t *)(uintptr_t)(daddr +
14771 14888 prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
14772 14889
14773 14890 if (probe->dofpr_func >= str_sec->dofs_size) {
14774 14891 dtrace_dof_error(dof, "invalid function name");
14775 14892 return (-1);
14776 14893 }
14777 14894
14778 14895 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
14779 14896 dtrace_dof_error(dof, "function name too long");
14780 14897 return (-1);
14781 14898 }
14782 14899
14783 14900 if (probe->dofpr_name >= str_sec->dofs_size ||
14784 14901 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
14785 14902 dtrace_dof_error(dof, "invalid probe name");
14786 14903 return (-1);
14787 14904 }
14788 14905
14789 14906 /*
14790 14907 * The offset count must not wrap the index, and the offsets
14791 14908 * must also not overflow the section's data.
14792 14909 */
14793 14910 if (probe->dofpr_offidx + probe->dofpr_noffs <
14794 14911 probe->dofpr_offidx ||
14795 14912 (probe->dofpr_offidx + probe->dofpr_noffs) *
14796 14913 off_sec->dofs_entsize > off_sec->dofs_size) {
14797 14914 dtrace_dof_error(dof, "invalid probe offset");
14798 14915 return (-1);
14799 14916 }
14800 14917
14801 14918 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
14802 14919 /*
14803 14920 * If there's no is-enabled offset section, make sure
14804 14921 * there aren't any is-enabled offsets. Otherwise
14805 14922 * perform the same checks as for probe offsets
14806 14923 * (immediately above).
14807 14924 */
14808 14925 if (enoff_sec == NULL) {
14809 14926 if (probe->dofpr_enoffidx != 0 ||
14810 14927 probe->dofpr_nenoffs != 0) {
14811 14928 dtrace_dof_error(dof, "is-enabled "
14812 14929 "offsets with null section");
14813 14930 return (-1);
14814 14931 }
14815 14932 } else if (probe->dofpr_enoffidx +
14816 14933 probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
14817 14934 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
14818 14935 enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
14819 14936 dtrace_dof_error(dof, "invalid is-enabled "
14820 14937 "offset");
14821 14938 return (-1);
14822 14939 }
14823 14940
14824 14941 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
14825 14942 dtrace_dof_error(dof, "zero probe and "
14826 14943 "is-enabled offsets");
14827 14944 return (-1);
14828 14945 }
14829 14946 } else if (probe->dofpr_noffs == 0) {
14830 14947 dtrace_dof_error(dof, "zero probe offsets");
14831 14948 return (-1);
14832 14949 }
14833 14950
14834 14951 if (probe->dofpr_argidx + probe->dofpr_xargc <
14835 14952 probe->dofpr_argidx ||
14836 14953 (probe->dofpr_argidx + probe->dofpr_xargc) *
14837 14954 arg_sec->dofs_entsize > arg_sec->dofs_size) {
14838 14955 dtrace_dof_error(dof, "invalid args");
14839 14956 return (-1);
14840 14957 }
14841 14958
14842 14959 typeidx = probe->dofpr_nargv;
14843 14960 typestr = strtab + probe->dofpr_nargv;
14844 14961 for (k = 0; k < probe->dofpr_nargc; k++) {
14845 14962 if (typeidx >= str_sec->dofs_size) {
14846 14963 dtrace_dof_error(dof, "bad "
14847 14964 "native argument type");
14848 14965 return (-1);
14849 14966 }
14850 14967
14851 14968 typesz = strlen(typestr) + 1;
14852 14969 if (typesz > DTRACE_ARGTYPELEN) {
14853 14970 dtrace_dof_error(dof, "native "
14854 14971 "argument type too long");
14855 14972 return (-1);
14856 14973 }
14857 14974 typeidx += typesz;
14858 14975 typestr += typesz;
14859 14976 }
14860 14977
14861 14978 typeidx = probe->dofpr_xargv;
14862 14979 typestr = strtab + probe->dofpr_xargv;
14863 14980 for (k = 0; k < probe->dofpr_xargc; k++) {
14864 14981 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
14865 14982 dtrace_dof_error(dof, "bad "
14866 14983 "native argument index");
14867 14984 return (-1);
14868 14985 }
14869 14986
14870 14987 if (typeidx >= str_sec->dofs_size) {
14871 14988 dtrace_dof_error(dof, "bad "
14872 14989 "translated argument type");
14873 14990 return (-1);
14874 14991 }
14875 14992
14876 14993 typesz = strlen(typestr) + 1;
14877 14994 if (typesz > DTRACE_ARGTYPELEN) {
14878 14995 dtrace_dof_error(dof, "translated argument "
14879 14996 "type too long");
14880 14997 return (-1);
14881 14998 }
14882 14999
14883 15000 typeidx += typesz;
14884 15001 typestr += typesz;
14885 15002 }
14886 15003 }
14887 15004
14888 15005 return (0);
14889 15006 }
14890 15007
14891 15008 static int
14892 15009 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp)
14893 15010 {
14894 15011 dtrace_helpers_t *help;
14895 15012 dtrace_vstate_t *vstate;
14896 15013 dtrace_enabling_t *enab = NULL;
14897 15014 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
14898 15015 uintptr_t daddr = (uintptr_t)dof;
14899 15016
14900 15017 ASSERT(MUTEX_HELD(&dtrace_lock));
14901 15018
14902 15019 if ((help = curproc->p_dtrace_helpers) == NULL)
14903 15020 help = dtrace_helpers_create(curproc);
14904 15021
14905 15022 vstate = &help->dthps_vstate;
14906 15023
14907 15024 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab,
14908 15025 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) {
14909 15026 dtrace_dof_destroy(dof);
14910 15027 return (rv);
14911 15028 }
14912 15029
14913 15030 /*
14914 15031 * Look for helper providers and validate their descriptions.
14915 15032 */
14916 15033 if (dhp != NULL) {
14917 15034 for (i = 0; i < dof->dofh_secnum; i++) {
14918 15035 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
14919 15036 dof->dofh_secoff + i * dof->dofh_secsize);
14920 15037
14921 15038 if (sec->dofs_type != DOF_SECT_PROVIDER)
14922 15039 continue;
14923 15040
14924 15041 if (dtrace_helper_provider_validate(dof, sec) != 0) {
14925 15042 dtrace_enabling_destroy(enab);
14926 15043 dtrace_dof_destroy(dof);
14927 15044 return (-1);
14928 15045 }
14929 15046
14930 15047 nprovs++;
14931 15048 }
14932 15049 }
14933 15050
14934 15051 /*
14935 15052 * Now we need to walk through the ECB descriptions in the enabling.
14936 15053 */
14937 15054 for (i = 0; i < enab->dten_ndesc; i++) {
14938 15055 dtrace_ecbdesc_t *ep = enab->dten_desc[i];
14939 15056 dtrace_probedesc_t *desc = &ep->dted_probe;
14940 15057
14941 15058 if (strcmp(desc->dtpd_provider, "dtrace") != 0)
14942 15059 continue;
14943 15060
14944 15061 if (strcmp(desc->dtpd_mod, "helper") != 0)
14945 15062 continue;
14946 15063
14947 15064 if (strcmp(desc->dtpd_func, "ustack") != 0)
14948 15065 continue;
14949 15066
14950 15067 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
14951 15068 ep)) != 0) {
14952 15069 /*
14953 15070 * Adding this helper action failed -- we are now going
14954 15071 * to rip out the entire generation and return failure.
14955 15072 */
14956 15073 (void) dtrace_helper_destroygen(help->dthps_generation);
14957 15074 dtrace_enabling_destroy(enab);
14958 15075 dtrace_dof_destroy(dof);
14959 15076 return (-1);
14960 15077 }
14961 15078
14962 15079 nhelpers++;
14963 15080 }
14964 15081
14965 15082 if (nhelpers < enab->dten_ndesc)
14966 15083 dtrace_dof_error(dof, "unmatched helpers");
14967 15084
14968 15085 gen = help->dthps_generation++;
14969 15086 dtrace_enabling_destroy(enab);
14970 15087
14971 15088 if (dhp != NULL && nprovs > 0) {
14972 15089 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
14973 15090 if (dtrace_helper_provider_add(dhp, gen) == 0) {
14974 15091 mutex_exit(&dtrace_lock);
14975 15092 dtrace_helper_provider_register(curproc, help, dhp);
14976 15093 mutex_enter(&dtrace_lock);
14977 15094
14978 15095 destroy = 0;
14979 15096 }
14980 15097 }
14981 15098
14982 15099 if (destroy)
14983 15100 dtrace_dof_destroy(dof);
14984 15101
14985 15102 return (gen);
14986 15103 }
14987 15104
14988 15105 static dtrace_helpers_t *
14989 15106 dtrace_helpers_create(proc_t *p)
14990 15107 {
14991 15108 dtrace_helpers_t *help;
14992 15109
14993 15110 ASSERT(MUTEX_HELD(&dtrace_lock));
14994 15111 ASSERT(p->p_dtrace_helpers == NULL);
14995 15112
14996 15113 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
14997 15114 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
14998 15115 DTRACE_NHELPER_ACTIONS, KM_SLEEP);
14999 15116
15000 15117 p->p_dtrace_helpers = help;
15001 15118 dtrace_helpers++;
15002 15119
15003 15120 return (help);
15004 15121 }
15005 15122
15006 15123 static void
15007 15124 dtrace_helpers_destroy(void)
15008 15125 {
15009 15126 dtrace_helpers_t *help;
15010 15127 dtrace_vstate_t *vstate;
15011 15128 proc_t *p = curproc;
15012 15129 int i;
15013 15130
15014 15131 mutex_enter(&dtrace_lock);
15015 15132
15016 15133 ASSERT(p->p_dtrace_helpers != NULL);
15017 15134 ASSERT(dtrace_helpers > 0);
15018 15135
15019 15136 help = p->p_dtrace_helpers;
15020 15137 vstate = &help->dthps_vstate;
15021 15138
15022 15139 /*
15023 15140 * We're now going to lose the help from this process.
15024 15141 */
15025 15142 p->p_dtrace_helpers = NULL;
15026 15143 dtrace_sync();
15027 15144
15028 15145 /*
15029 15146 * Destory the helper actions.
15030 15147 */
15031 15148 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15032 15149 dtrace_helper_action_t *h, *next;
15033 15150
15034 15151 for (h = help->dthps_actions[i]; h != NULL; h = next) {
15035 15152 next = h->dtha_next;
15036 15153 dtrace_helper_action_destroy(h, vstate);
15037 15154 h = next;
15038 15155 }
15039 15156 }
15040 15157
15041 15158 mutex_exit(&dtrace_lock);
15042 15159
15043 15160 /*
15044 15161 * Destroy the helper providers.
15045 15162 */
15046 15163 if (help->dthps_maxprovs > 0) {
15047 15164 mutex_enter(&dtrace_meta_lock);
15048 15165 if (dtrace_meta_pid != NULL) {
15049 15166 ASSERT(dtrace_deferred_pid == NULL);
15050 15167
15051 15168 for (i = 0; i < help->dthps_nprovs; i++) {
15052 15169 dtrace_helper_provider_remove(
15053 15170 &help->dthps_provs[i]->dthp_prov, p->p_pid);
15054 15171 }
15055 15172 } else {
15056 15173 mutex_enter(&dtrace_lock);
15057 15174 ASSERT(help->dthps_deferred == 0 ||
15058 15175 help->dthps_next != NULL ||
15059 15176 help->dthps_prev != NULL ||
15060 15177 help == dtrace_deferred_pid);
15061 15178
15062 15179 /*
15063 15180 * Remove the helper from the deferred list.
15064 15181 */
15065 15182 if (help->dthps_next != NULL)
15066 15183 help->dthps_next->dthps_prev = help->dthps_prev;
15067 15184 if (help->dthps_prev != NULL)
15068 15185 help->dthps_prev->dthps_next = help->dthps_next;
15069 15186 if (dtrace_deferred_pid == help) {
15070 15187 dtrace_deferred_pid = help->dthps_next;
15071 15188 ASSERT(help->dthps_prev == NULL);
15072 15189 }
15073 15190
15074 15191 mutex_exit(&dtrace_lock);
15075 15192 }
15076 15193
15077 15194 mutex_exit(&dtrace_meta_lock);
15078 15195
15079 15196 for (i = 0; i < help->dthps_nprovs; i++) {
15080 15197 dtrace_helper_provider_destroy(help->dthps_provs[i]);
15081 15198 }
15082 15199
15083 15200 kmem_free(help->dthps_provs, help->dthps_maxprovs *
15084 15201 sizeof (dtrace_helper_provider_t *));
15085 15202 }
15086 15203
15087 15204 mutex_enter(&dtrace_lock);
15088 15205
15089 15206 dtrace_vstate_fini(&help->dthps_vstate);
15090 15207 kmem_free(help->dthps_actions,
15091 15208 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
15092 15209 kmem_free(help, sizeof (dtrace_helpers_t));
15093 15210
15094 15211 --dtrace_helpers;
15095 15212 mutex_exit(&dtrace_lock);
15096 15213 }
15097 15214
15098 15215 static void
15099 15216 dtrace_helpers_duplicate(proc_t *from, proc_t *to)
15100 15217 {
15101 15218 dtrace_helpers_t *help, *newhelp;
15102 15219 dtrace_helper_action_t *helper, *new, *last;
15103 15220 dtrace_difo_t *dp;
15104 15221 dtrace_vstate_t *vstate;
15105 15222 int i, j, sz, hasprovs = 0;
15106 15223
15107 15224 mutex_enter(&dtrace_lock);
15108 15225 ASSERT(from->p_dtrace_helpers != NULL);
15109 15226 ASSERT(dtrace_helpers > 0);
15110 15227
15111 15228 help = from->p_dtrace_helpers;
15112 15229 newhelp = dtrace_helpers_create(to);
15113 15230 ASSERT(to->p_dtrace_helpers != NULL);
15114 15231
15115 15232 newhelp->dthps_generation = help->dthps_generation;
15116 15233 vstate = &newhelp->dthps_vstate;
15117 15234
15118 15235 /*
15119 15236 * Duplicate the helper actions.
15120 15237 */
15121 15238 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15122 15239 if ((helper = help->dthps_actions[i]) == NULL)
15123 15240 continue;
15124 15241
15125 15242 for (last = NULL; helper != NULL; helper = helper->dtha_next) {
15126 15243 new = kmem_zalloc(sizeof (dtrace_helper_action_t),
15127 15244 KM_SLEEP);
15128 15245 new->dtha_generation = helper->dtha_generation;
15129 15246
15130 15247 if ((dp = helper->dtha_predicate) != NULL) {
15131 15248 dp = dtrace_difo_duplicate(dp, vstate);
15132 15249 new->dtha_predicate = dp;
15133 15250 }
15134 15251
15135 15252 new->dtha_nactions = helper->dtha_nactions;
15136 15253 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
15137 15254 new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
15138 15255
15139 15256 for (j = 0; j < new->dtha_nactions; j++) {
15140 15257 dtrace_difo_t *dp = helper->dtha_actions[j];
15141 15258
15142 15259 ASSERT(dp != NULL);
15143 15260 dp = dtrace_difo_duplicate(dp, vstate);
15144 15261 new->dtha_actions[j] = dp;
15145 15262 }
15146 15263
15147 15264 if (last != NULL) {
15148 15265 last->dtha_next = new;
15149 15266 } else {
15150 15267 newhelp->dthps_actions[i] = new;
15151 15268 }
15152 15269
15153 15270 last = new;
15154 15271 }
15155 15272 }
15156 15273
15157 15274 /*
15158 15275 * Duplicate the helper providers and register them with the
15159 15276 * DTrace framework.
15160 15277 */
15161 15278 if (help->dthps_nprovs > 0) {
15162 15279 newhelp->dthps_nprovs = help->dthps_nprovs;
15163 15280 newhelp->dthps_maxprovs = help->dthps_nprovs;
15164 15281 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
15165 15282 sizeof (dtrace_helper_provider_t *), KM_SLEEP);
15166 15283 for (i = 0; i < newhelp->dthps_nprovs; i++) {
15167 15284 newhelp->dthps_provs[i] = help->dthps_provs[i];
15168 15285 newhelp->dthps_provs[i]->dthp_ref++;
15169 15286 }
15170 15287
15171 15288 hasprovs = 1;
15172 15289 }
15173 15290
15174 15291 mutex_exit(&dtrace_lock);
15175 15292
15176 15293 if (hasprovs)
15177 15294 dtrace_helper_provider_register(to, newhelp, NULL);
15178 15295 }
15179 15296
15180 15297 /*
15181 15298 * DTrace Hook Functions
15182 15299 */
15183 15300 static void
15184 15301 dtrace_module_loaded(struct modctl *ctl)
15185 15302 {
15186 15303 dtrace_provider_t *prv;
15187 15304
15188 15305 mutex_enter(&dtrace_provider_lock);
15189 15306 mutex_enter(&mod_lock);
15190 15307
15191 15308 ASSERT(ctl->mod_busy);
15192 15309
15193 15310 /*
15194 15311 * We're going to call each providers per-module provide operation
15195 15312 * specifying only this module.
15196 15313 */
15197 15314 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
15198 15315 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
15199 15316
15200 15317 mutex_exit(&mod_lock);
15201 15318 mutex_exit(&dtrace_provider_lock);
15202 15319
15203 15320 /*
15204 15321 * If we have any retained enablings, we need to match against them.
15205 15322 * Enabling probes requires that cpu_lock be held, and we cannot hold
15206 15323 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
15207 15324 * module. (In particular, this happens when loading scheduling
15208 15325 * classes.) So if we have any retained enablings, we need to dispatch
15209 15326 * our task queue to do the match for us.
15210 15327 */
15211 15328 mutex_enter(&dtrace_lock);
15212 15329
15213 15330 if (dtrace_retained == NULL) {
15214 15331 mutex_exit(&dtrace_lock);
15215 15332 return;
15216 15333 }
15217 15334
15218 15335 (void) taskq_dispatch(dtrace_taskq,
15219 15336 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
15220 15337
15221 15338 mutex_exit(&dtrace_lock);
15222 15339
15223 15340 /*
15224 15341 * And now, for a little heuristic sleaze: in general, we want to
15225 15342 * match modules as soon as they load. However, we cannot guarantee
15226 15343 * this, because it would lead us to the lock ordering violation
15227 15344 * outlined above. The common case, of course, is that cpu_lock is
15228 15345 * _not_ held -- so we delay here for a clock tick, hoping that that's
15229 15346 * long enough for the task queue to do its work. If it's not, it's
15230 15347 * not a serious problem -- it just means that the module that we
15231 15348 * just loaded may not be immediately instrumentable.
15232 15349 */
15233 15350 delay(1);
15234 15351 }
15235 15352
15236 15353 static void
15237 15354 dtrace_module_unloaded(struct modctl *ctl)
15238 15355 {
15239 15356 dtrace_probe_t template, *probe, *first, *next;
15240 15357 dtrace_provider_t *prov;
15241 15358
15242 15359 template.dtpr_mod = ctl->mod_modname;
15243 15360
15244 15361 mutex_enter(&dtrace_provider_lock);
15245 15362 mutex_enter(&mod_lock);
15246 15363 mutex_enter(&dtrace_lock);
15247 15364
15248 15365 if (dtrace_bymod == NULL) {
15249 15366 /*
15250 15367 * The DTrace module is loaded (obviously) but not attached;
15251 15368 * we don't have any work to do.
15252 15369 */
15253 15370 mutex_exit(&dtrace_provider_lock);
15254 15371 mutex_exit(&mod_lock);
15255 15372 mutex_exit(&dtrace_lock);
15256 15373 return;
15257 15374 }
15258 15375
15259 15376 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
15260 15377 probe != NULL; probe = probe->dtpr_nextmod) {
15261 15378 if (probe->dtpr_ecb != NULL) {
15262 15379 mutex_exit(&dtrace_provider_lock);
15263 15380 mutex_exit(&mod_lock);
15264 15381 mutex_exit(&dtrace_lock);
15265 15382
15266 15383 /*
15267 15384 * This shouldn't _actually_ be possible -- we're
15268 15385 * unloading a module that has an enabled probe in it.
15269 15386 * (It's normally up to the provider to make sure that
15270 15387 * this can't happen.) However, because dtps_enable()
15271 15388 * doesn't have a failure mode, there can be an
15272 15389 * enable/unload race. Upshot: we don't want to
15273 15390 * assert, but we're not going to disable the
15274 15391 * probe, either.
15275 15392 */
15276 15393 if (dtrace_err_verbose) {
15277 15394 cmn_err(CE_WARN, "unloaded module '%s' had "
15278 15395 "enabled probes", ctl->mod_modname);
15279 15396 }
15280 15397
15281 15398 return;
15282 15399 }
15283 15400 }
15284 15401
15285 15402 probe = first;
15286 15403
15287 15404 for (first = NULL; probe != NULL; probe = next) {
15288 15405 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
15289 15406
15290 15407 dtrace_probes[probe->dtpr_id - 1] = NULL;
15291 15408
15292 15409 next = probe->dtpr_nextmod;
15293 15410 dtrace_hash_remove(dtrace_bymod, probe);
15294 15411 dtrace_hash_remove(dtrace_byfunc, probe);
15295 15412 dtrace_hash_remove(dtrace_byname, probe);
15296 15413
15297 15414 if (first == NULL) {
15298 15415 first = probe;
15299 15416 probe->dtpr_nextmod = NULL;
15300 15417 } else {
15301 15418 probe->dtpr_nextmod = first;
15302 15419 first = probe;
15303 15420 }
15304 15421 }
15305 15422
15306 15423 /*
15307 15424 * We've removed all of the module's probes from the hash chains and
15308 15425 * from the probe array. Now issue a dtrace_sync() to be sure that
15309 15426 * everyone has cleared out from any probe array processing.
15310 15427 */
15311 15428 dtrace_sync();
15312 15429
15313 15430 for (probe = first; probe != NULL; probe = first) {
15314 15431 first = probe->dtpr_nextmod;
15315 15432 prov = probe->dtpr_provider;
15316 15433 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
15317 15434 probe->dtpr_arg);
15318 15435 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
15319 15436 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
15320 15437 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
15321 15438 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
15322 15439 kmem_free(probe, sizeof (dtrace_probe_t));
15323 15440 }
15324 15441
15325 15442 mutex_exit(&dtrace_lock);
15326 15443 mutex_exit(&mod_lock);
15327 15444 mutex_exit(&dtrace_provider_lock);
15328 15445 }
15329 15446
15330 15447 void
15331 15448 dtrace_suspend(void)
15332 15449 {
15333 15450 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
15334 15451 }
15335 15452
15336 15453 void
15337 15454 dtrace_resume(void)
15338 15455 {
15339 15456 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
15340 15457 }
15341 15458
15342 15459 static int
15343 15460 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
15344 15461 {
15345 15462 ASSERT(MUTEX_HELD(&cpu_lock));
15346 15463 mutex_enter(&dtrace_lock);
15347 15464
15348 15465 switch (what) {
15349 15466 case CPU_CONFIG: {
15350 15467 dtrace_state_t *state;
15351 15468 dtrace_optval_t *opt, rs, c;
15352 15469
15353 15470 /*
15354 15471 * For now, we only allocate a new buffer for anonymous state.
15355 15472 */
15356 15473 if ((state = dtrace_anon.dta_state) == NULL)
15357 15474 break;
15358 15475
15359 15476 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
15360 15477 break;
15361 15478
15362 15479 opt = state->dts_options;
15363 15480 c = opt[DTRACEOPT_CPU];
15364 15481
15365 15482 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
15366 15483 break;
15367 15484
15368 15485 /*
15369 15486 * Regardless of what the actual policy is, we're going to
15370 15487 * temporarily set our resize policy to be manual. We're
15371 15488 * also going to temporarily set our CPU option to denote
15372 15489 * the newly configured CPU.
15373 15490 */
15374 15491 rs = opt[DTRACEOPT_BUFRESIZE];
15375 15492 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
15376 15493 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
15377 15494
15378 15495 (void) dtrace_state_buffers(state);
15379 15496
15380 15497 opt[DTRACEOPT_BUFRESIZE] = rs;
15381 15498 opt[DTRACEOPT_CPU] = c;
15382 15499
15383 15500 break;
15384 15501 }
15385 15502
15386 15503 case CPU_UNCONFIG:
15387 15504 /*
15388 15505 * We don't free the buffer in the CPU_UNCONFIG case. (The
15389 15506 * buffer will be freed when the consumer exits.)
15390 15507 */
15391 15508 break;
15392 15509
15393 15510 default:
15394 15511 break;
15395 15512 }
15396 15513
15397 15514 mutex_exit(&dtrace_lock);
15398 15515 return (0);
15399 15516 }
15400 15517
15401 15518 static void
15402 15519 dtrace_cpu_setup_initial(processorid_t cpu)
15403 15520 {
15404 15521 (void) dtrace_cpu_setup(CPU_CONFIG, cpu);
15405 15522 }
15406 15523
15407 15524 static void
15408 15525 dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
15409 15526 {
15410 15527 if (dtrace_toxranges >= dtrace_toxranges_max) {
15411 15528 int osize, nsize;
15412 15529 dtrace_toxrange_t *range;
15413 15530
15414 15531 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
15415 15532
15416 15533 if (osize == 0) {
15417 15534 ASSERT(dtrace_toxrange == NULL);
15418 15535 ASSERT(dtrace_toxranges_max == 0);
15419 15536 dtrace_toxranges_max = 1;
15420 15537 } else {
15421 15538 dtrace_toxranges_max <<= 1;
15422 15539 }
15423 15540
15424 15541 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
15425 15542 range = kmem_zalloc(nsize, KM_SLEEP);
15426 15543
15427 15544 if (dtrace_toxrange != NULL) {
15428 15545 ASSERT(osize != 0);
15429 15546 bcopy(dtrace_toxrange, range, osize);
15430 15547 kmem_free(dtrace_toxrange, osize);
15431 15548 }
15432 15549
15433 15550 dtrace_toxrange = range;
15434 15551 }
15435 15552
15436 15553 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL);
15437 15554 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL);
15438 15555
15439 15556 dtrace_toxrange[dtrace_toxranges].dtt_base = base;
15440 15557 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
15441 15558 dtrace_toxranges++;
15442 15559 }
15443 15560
15444 15561 static void
15445 15562 dtrace_getf_barrier()
15446 15563 {
15447 15564 /*
15448 15565 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings
15449 15566 * that contain calls to getf(), this routine will be called on every
15450 15567 * closef() before either the underlying vnode is released or the
15451 15568 * file_t itself is freed. By the time we are here, it is essential
15452 15569 * that the file_t can no longer be accessed from a call to getf()
15453 15570 * in probe context -- that assures that a dtrace_sync() can be used
15454 15571 * to clear out any enablings referring to the old structures.
15455 15572 */
15456 15573 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 ||
15457 15574 kcred->cr_zone->zone_dtrace_getf != 0)
15458 15575 dtrace_sync();
15459 15576 }
15460 15577
15461 15578 /*
15462 15579 * DTrace Driver Cookbook Functions
15463 15580 */
15464 15581 /*ARGSUSED*/
15465 15582 static int
15466 15583 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
15467 15584 {
15468 15585 dtrace_provider_id_t id;
15469 15586 dtrace_state_t *state = NULL;
15470 15587 dtrace_enabling_t *enab;
15471 15588
15472 15589 mutex_enter(&cpu_lock);
15473 15590 mutex_enter(&dtrace_provider_lock);
15474 15591 mutex_enter(&dtrace_lock);
15475 15592
15476 15593 if (ddi_soft_state_init(&dtrace_softstate,
15477 15594 sizeof (dtrace_state_t), 0) != 0) {
15478 15595 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
15479 15596 mutex_exit(&cpu_lock);
15480 15597 mutex_exit(&dtrace_provider_lock);
15481 15598 mutex_exit(&dtrace_lock);
15482 15599 return (DDI_FAILURE);
15483 15600 }
15484 15601
15485 15602 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
15486 15603 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
15487 15604 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
15488 15605 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
15489 15606 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
15490 15607 ddi_remove_minor_node(devi, NULL);
15491 15608 ddi_soft_state_fini(&dtrace_softstate);
15492 15609 mutex_exit(&cpu_lock);
15493 15610 mutex_exit(&dtrace_provider_lock);
15494 15611 mutex_exit(&dtrace_lock);
15495 15612 return (DDI_FAILURE);
15496 15613 }
15497 15614
15498 15615 ddi_report_dev(devi);
15499 15616 dtrace_devi = devi;
15500 15617
15501 15618 dtrace_modload = dtrace_module_loaded;
15502 15619 dtrace_modunload = dtrace_module_unloaded;
15503 15620 dtrace_cpu_init = dtrace_cpu_setup_initial;
15504 15621 dtrace_helpers_cleanup = dtrace_helpers_destroy;
15505 15622 dtrace_helpers_fork = dtrace_helpers_duplicate;
15506 15623 dtrace_cpustart_init = dtrace_suspend;
15507 15624 dtrace_cpustart_fini = dtrace_resume;
15508 15625 dtrace_debugger_init = dtrace_suspend;
15509 15626 dtrace_debugger_fini = dtrace_resume;
15510 15627
15511 15628 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
15512 15629
15513 15630 ASSERT(MUTEX_HELD(&cpu_lock));
15514 15631
15515 15632 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
15516 15633 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
15517 15634 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
15518 15635 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
15519 15636 VM_SLEEP | VMC_IDENTIFIER);
15520 15637 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
15521 15638 1, INT_MAX, 0);
15522 15639
15523 15640 dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
15524 15641 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
15525 15642 NULL, NULL, NULL, NULL, NULL, 0);
15526 15643
15527 15644 ASSERT(MUTEX_HELD(&cpu_lock));
15528 15645 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
15529 15646 offsetof(dtrace_probe_t, dtpr_nextmod),
15530 15647 offsetof(dtrace_probe_t, dtpr_prevmod));
15531 15648
15532 15649 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
15533 15650 offsetof(dtrace_probe_t, dtpr_nextfunc),
15534 15651 offsetof(dtrace_probe_t, dtpr_prevfunc));
15535 15652
15536 15653 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
15537 15654 offsetof(dtrace_probe_t, dtpr_nextname),
15538 15655 offsetof(dtrace_probe_t, dtpr_prevname));
15539 15656
15540 15657 if (dtrace_retain_max < 1) {
15541 15658 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
15542 15659 "setting to 1", dtrace_retain_max);
15543 15660 dtrace_retain_max = 1;
15544 15661 }
15545 15662
15546 15663 /*
15547 15664 * Now discover our toxic ranges.
15548 15665 */
15549 15666 dtrace_toxic_ranges(dtrace_toxrange_add);
15550 15667
15551 15668 /*
15552 15669 * Before we register ourselves as a provider to our own framework,
15553 15670 * we would like to assert that dtrace_provider is NULL -- but that's
15554 15671 * not true if we were loaded as a dependency of a DTrace provider.
15555 15672 * Once we've registered, we can assert that dtrace_provider is our
15556 15673 * pseudo provider.
15557 15674 */
15558 15675 (void) dtrace_register("dtrace", &dtrace_provider_attr,
15559 15676 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
15560 15677
15561 15678 ASSERT(dtrace_provider != NULL);
15562 15679 ASSERT((dtrace_provider_id_t)dtrace_provider == id);
15563 15680
15564 15681 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
15565 15682 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
15566 15683 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
15567 15684 dtrace_provider, NULL, NULL, "END", 0, NULL);
15568 15685 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
15569 15686 dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
15570 15687
15571 15688 dtrace_anon_property();
15572 15689 mutex_exit(&cpu_lock);
15573 15690
15574 15691 /*
15575 15692 * If there are already providers, we must ask them to provide their
15576 15693 * probes, and then match any anonymous enabling against them. Note
15577 15694 * that there should be no other retained enablings at this time:
15578 15695 * the only retained enablings at this time should be the anonymous
15579 15696 * enabling.
15580 15697 */
15581 15698 if (dtrace_anon.dta_enabling != NULL) {
15582 15699 ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
15583 15700
15584 15701 dtrace_enabling_provide(NULL);
15585 15702 state = dtrace_anon.dta_state;
15586 15703
15587 15704 /*
15588 15705 * We couldn't hold cpu_lock across the above call to
15589 15706 * dtrace_enabling_provide(), but we must hold it to actually
15590 15707 * enable the probes. We have to drop all of our locks, pick
15591 15708 * up cpu_lock, and regain our locks before matching the
15592 15709 * retained anonymous enabling.
15593 15710 */
15594 15711 mutex_exit(&dtrace_lock);
15595 15712 mutex_exit(&dtrace_provider_lock);
15596 15713
15597 15714 mutex_enter(&cpu_lock);
15598 15715 mutex_enter(&dtrace_provider_lock);
15599 15716 mutex_enter(&dtrace_lock);
15600 15717
15601 15718 if ((enab = dtrace_anon.dta_enabling) != NULL)
15602 15719 (void) dtrace_enabling_match(enab, NULL);
15603 15720
15604 15721 mutex_exit(&cpu_lock);
15605 15722 }
15606 15723
15607 15724 mutex_exit(&dtrace_lock);
15608 15725 mutex_exit(&dtrace_provider_lock);
15609 15726
15610 15727 if (state != NULL) {
15611 15728 /*
15612 15729 * If we created any anonymous state, set it going now.
15613 15730 */
15614 15731 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
15615 15732 }
15616 15733
15617 15734 return (DDI_SUCCESS);
15618 15735 }
15619 15736
15620 15737 /*ARGSUSED*/
15621 15738 static int
15622 15739 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
15623 15740 {
15624 15741 dtrace_state_t *state;
15625 15742 uint32_t priv;
15626 15743 uid_t uid;
15627 15744 zoneid_t zoneid;
15628 15745
15629 15746 if (getminor(*devp) == DTRACEMNRN_HELPER)
15630 15747 return (0);
15631 15748
15632 15749 /*
15633 15750 * If this wasn't an open with the "helper" minor, then it must be
15634 15751 * the "dtrace" minor.
15635 15752 */
15636 15753 if (getminor(*devp) != DTRACEMNRN_DTRACE)
15637 15754 return (ENXIO);
15638 15755
15639 15756 /*
15640 15757 * If no DTRACE_PRIV_* bits are set in the credential, then the
15641 15758 * caller lacks sufficient permission to do anything with DTrace.
15642 15759 */
15643 15760 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
15644 15761 if (priv == DTRACE_PRIV_NONE)
15645 15762 return (EACCES);
15646 15763
15647 15764 /*
15648 15765 * Ask all providers to provide all their probes.
15649 15766 */
15650 15767 mutex_enter(&dtrace_provider_lock);
15651 15768 dtrace_probe_provide(NULL, NULL);
15652 15769 mutex_exit(&dtrace_provider_lock);
15653 15770
15654 15771 mutex_enter(&cpu_lock);
15655 15772 mutex_enter(&dtrace_lock);
15656 15773 dtrace_opens++;
15657 15774 dtrace_membar_producer();
15658 15775
15659 15776 /*
15660 15777 * If the kernel debugger is active (that is, if the kernel debugger
15661 15778 * modified text in some way), we won't allow the open.
15662 15779 */
15663 15780 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15664 15781 dtrace_opens--;
15665 15782 mutex_exit(&cpu_lock);
15666 15783 mutex_exit(&dtrace_lock);
15667 15784 return (EBUSY);
15668 15785 }
15669 15786
15670 15787 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) {
15671 15788 /*
15672 15789 * If DTrace helper tracing is enabled, we need to allocate the
15673 15790 * trace buffer and initialize the values.
15674 15791 */
15675 15792 dtrace_helptrace_buffer =
15676 15793 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
15677 15794 dtrace_helptrace_next = 0;
15678 15795 dtrace_helptrace_wrapped = 0;
15679 15796 dtrace_helptrace_enable = 0;
15680 15797 }
15681 15798
15682 15799 state = dtrace_state_create(devp, cred_p);
15683 15800 mutex_exit(&cpu_lock);
15684 15801
15685 15802 if (state == NULL) {
15686 15803 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
15687 15804 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15688 15805 mutex_exit(&dtrace_lock);
15689 15806 return (EAGAIN);
15690 15807 }
15691 15808
15692 15809 mutex_exit(&dtrace_lock);
15693 15810
15694 15811 return (0);
15695 15812 }
15696 15813
15697 15814 /*ARGSUSED*/
15698 15815 static int
15699 15816 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
15700 15817 {
15701 15818 minor_t minor = getminor(dev);
15702 15819 dtrace_state_t *state;
15703 15820 dtrace_helptrace_t *buf = NULL;
15704 15821
15705 15822 if (minor == DTRACEMNRN_HELPER)
15706 15823 return (0);
15707 15824
15708 15825 state = ddi_get_soft_state(dtrace_softstate, minor);
15709 15826
15710 15827 mutex_enter(&cpu_lock);
15711 15828 mutex_enter(&dtrace_lock);
15712 15829
15713 15830 if (state->dts_anon) {
15714 15831 /*
15715 15832 * There is anonymous state. Destroy that first.
15716 15833 */
15717 15834 ASSERT(dtrace_anon.dta_state == NULL);
15718 15835 dtrace_state_destroy(state->dts_anon);
15719 15836 }
15720 15837
15721 15838 if (dtrace_helptrace_disable) {
15722 15839 /*
15723 15840 * If we have been told to disable helper tracing, set the
15724 15841 * buffer to NULL before calling into dtrace_state_destroy();
15725 15842 * we take advantage of its dtrace_sync() to know that no
15726 15843 * CPU is in probe context with enabled helper tracing
15727 15844 * after it returns.
15728 15845 */
15729 15846 buf = dtrace_helptrace_buffer;
15730 15847 dtrace_helptrace_buffer = NULL;
15731 15848 }
15732 15849
15733 15850 dtrace_state_destroy(state);
15734 15851 ASSERT(dtrace_opens > 0);
15735 15852
15736 15853 /*
15737 15854 * Only relinquish control of the kernel debugger interface when there
15738 15855 * are no consumers and no anonymous enablings.
15739 15856 */
15740 15857 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
15741 15858 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
15742 15859
15743 15860 if (buf != NULL) {
15744 15861 kmem_free(buf, dtrace_helptrace_bufsize);
15745 15862 dtrace_helptrace_disable = 0;
15746 15863 }
15747 15864
15748 15865 mutex_exit(&dtrace_lock);
15749 15866 mutex_exit(&cpu_lock);
15750 15867
15751 15868 return (0);
15752 15869 }
15753 15870
15754 15871 /*ARGSUSED*/
15755 15872 static int
15756 15873 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
15757 15874 {
15758 15875 int rval;
15759 15876 dof_helper_t help, *dhp = NULL;
15760 15877
15761 15878 switch (cmd) {
15762 15879 case DTRACEHIOC_ADDDOF:
15763 15880 if (copyin((void *)arg, &help, sizeof (help)) != 0) {
15764 15881 dtrace_dof_error(NULL, "failed to copyin DOF helper");
15765 15882 return (EFAULT);
15766 15883 }
15767 15884
15768 15885 dhp = &help;
15769 15886 arg = (intptr_t)help.dofhp_dof;
15770 15887 /*FALLTHROUGH*/
15771 15888
15772 15889 case DTRACEHIOC_ADD: {
15773 15890 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
15774 15891
15775 15892 if (dof == NULL)
15776 15893 return (rval);
15777 15894
15778 15895 mutex_enter(&dtrace_lock);
15779 15896
15780 15897 /*
15781 15898 * dtrace_helper_slurp() takes responsibility for the dof --
15782 15899 * it may free it now or it may save it and free it later.
15783 15900 */
15784 15901 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
15785 15902 *rv = rval;
15786 15903 rval = 0;
15787 15904 } else {
15788 15905 rval = EINVAL;
15789 15906 }
15790 15907
15791 15908 mutex_exit(&dtrace_lock);
15792 15909 return (rval);
15793 15910 }
15794 15911
15795 15912 case DTRACEHIOC_REMOVE: {
15796 15913 mutex_enter(&dtrace_lock);
15797 15914 rval = dtrace_helper_destroygen(arg);
15798 15915 mutex_exit(&dtrace_lock);
15799 15916
15800 15917 return (rval);
15801 15918 }
15802 15919
15803 15920 default:
15804 15921 break;
15805 15922 }
15806 15923
15807 15924 return (ENOTTY);
15808 15925 }
15809 15926
15810 15927 /*ARGSUSED*/
15811 15928 static int
15812 15929 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
15813 15930 {
15814 15931 minor_t minor = getminor(dev);
15815 15932 dtrace_state_t *state;
15816 15933 int rval;
15817 15934
15818 15935 if (minor == DTRACEMNRN_HELPER)
15819 15936 return (dtrace_ioctl_helper(cmd, arg, rv));
15820 15937
15821 15938 state = ddi_get_soft_state(dtrace_softstate, minor);
15822 15939
15823 15940 if (state->dts_anon) {
15824 15941 ASSERT(dtrace_anon.dta_state == NULL);
15825 15942 state = state->dts_anon;
15826 15943 }
15827 15944
15828 15945 switch (cmd) {
15829 15946 case DTRACEIOC_PROVIDER: {
15830 15947 dtrace_providerdesc_t pvd;
15831 15948 dtrace_provider_t *pvp;
15832 15949
15833 15950 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
15834 15951 return (EFAULT);
15835 15952
15836 15953 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
15837 15954 mutex_enter(&dtrace_provider_lock);
15838 15955
15839 15956 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
15840 15957 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
15841 15958 break;
15842 15959 }
15843 15960
15844 15961 mutex_exit(&dtrace_provider_lock);
15845 15962
15846 15963 if (pvp == NULL)
15847 15964 return (ESRCH);
15848 15965
15849 15966 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
15850 15967 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
15851 15968 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
15852 15969 return (EFAULT);
15853 15970
15854 15971 return (0);
15855 15972 }
15856 15973
15857 15974 case DTRACEIOC_EPROBE: {
15858 15975 dtrace_eprobedesc_t epdesc;
15859 15976 dtrace_ecb_t *ecb;
15860 15977 dtrace_action_t *act;
15861 15978 void *buf;
15862 15979 size_t size;
15863 15980 uintptr_t dest;
15864 15981 int nrecs;
15865 15982
15866 15983 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
15867 15984 return (EFAULT);
15868 15985
15869 15986 mutex_enter(&dtrace_lock);
15870 15987
15871 15988 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
15872 15989 mutex_exit(&dtrace_lock);
15873 15990 return (EINVAL);
15874 15991 }
15875 15992
15876 15993 if (ecb->dte_probe == NULL) {
15877 15994 mutex_exit(&dtrace_lock);
15878 15995 return (EINVAL);
15879 15996 }
15880 15997
15881 15998 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
15882 15999 epdesc.dtepd_uarg = ecb->dte_uarg;
15883 16000 epdesc.dtepd_size = ecb->dte_size;
15884 16001
15885 16002 nrecs = epdesc.dtepd_nrecs;
15886 16003 epdesc.dtepd_nrecs = 0;
15887 16004 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
15888 16005 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
15889 16006 continue;
15890 16007
15891 16008 epdesc.dtepd_nrecs++;
15892 16009 }
15893 16010
15894 16011 /*
15895 16012 * Now that we have the size, we need to allocate a temporary
15896 16013 * buffer in which to store the complete description. We need
15897 16014 * the temporary buffer to be able to drop dtrace_lock()
15898 16015 * across the copyout(), below.
15899 16016 */
15900 16017 size = sizeof (dtrace_eprobedesc_t) +
15901 16018 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
15902 16019
15903 16020 buf = kmem_alloc(size, KM_SLEEP);
15904 16021 dest = (uintptr_t)buf;
15905 16022
15906 16023 bcopy(&epdesc, (void *)dest, sizeof (epdesc));
15907 16024 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
15908 16025
15909 16026 for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
15910 16027 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
15911 16028 continue;
15912 16029
15913 16030 if (nrecs-- == 0)
15914 16031 break;
15915 16032
15916 16033 bcopy(&act->dta_rec, (void *)dest,
15917 16034 sizeof (dtrace_recdesc_t));
15918 16035 dest += sizeof (dtrace_recdesc_t);
15919 16036 }
15920 16037
15921 16038 mutex_exit(&dtrace_lock);
15922 16039
15923 16040 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
15924 16041 kmem_free(buf, size);
15925 16042 return (EFAULT);
15926 16043 }
15927 16044
15928 16045 kmem_free(buf, size);
15929 16046 return (0);
15930 16047 }
15931 16048
15932 16049 case DTRACEIOC_AGGDESC: {
15933 16050 dtrace_aggdesc_t aggdesc;
15934 16051 dtrace_action_t *act;
15935 16052 dtrace_aggregation_t *agg;
15936 16053 int nrecs;
15937 16054 uint32_t offs;
15938 16055 dtrace_recdesc_t *lrec;
15939 16056 void *buf;
15940 16057 size_t size;
15941 16058 uintptr_t dest;
15942 16059
15943 16060 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
15944 16061 return (EFAULT);
15945 16062
15946 16063 mutex_enter(&dtrace_lock);
15947 16064
15948 16065 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
15949 16066 mutex_exit(&dtrace_lock);
15950 16067 return (EINVAL);
15951 16068 }
15952 16069
15953 16070 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
15954 16071
15955 16072 nrecs = aggdesc.dtagd_nrecs;
15956 16073 aggdesc.dtagd_nrecs = 0;
15957 16074
15958 16075 offs = agg->dtag_base;
15959 16076 lrec = &agg->dtag_action.dta_rec;
15960 16077 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
15961 16078
15962 16079 for (act = agg->dtag_first; ; act = act->dta_next) {
15963 16080 ASSERT(act->dta_intuple ||
15964 16081 DTRACEACT_ISAGG(act->dta_kind));
15965 16082
15966 16083 /*
15967 16084 * If this action has a record size of zero, it
15968 16085 * denotes an argument to the aggregating action.
15969 16086 * Because the presence of this record doesn't (or
15970 16087 * shouldn't) affect the way the data is interpreted,
15971 16088 * we don't copy it out to save user-level the
15972 16089 * confusion of dealing with a zero-length record.
15973 16090 */
15974 16091 if (act->dta_rec.dtrd_size == 0) {
15975 16092 ASSERT(agg->dtag_hasarg);
15976 16093 continue;
15977 16094 }
15978 16095
15979 16096 aggdesc.dtagd_nrecs++;
15980 16097
15981 16098 if (act == &agg->dtag_action)
15982 16099 break;
15983 16100 }
15984 16101
15985 16102 /*
15986 16103 * Now that we have the size, we need to allocate a temporary
15987 16104 * buffer in which to store the complete description. We need
15988 16105 * the temporary buffer to be able to drop dtrace_lock()
15989 16106 * across the copyout(), below.
15990 16107 */
15991 16108 size = sizeof (dtrace_aggdesc_t) +
15992 16109 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
15993 16110
15994 16111 buf = kmem_alloc(size, KM_SLEEP);
15995 16112 dest = (uintptr_t)buf;
15996 16113
15997 16114 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
15998 16115 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
15999 16116
16000 16117 for (act = agg->dtag_first; ; act = act->dta_next) {
16001 16118 dtrace_recdesc_t rec = act->dta_rec;
16002 16119
16003 16120 /*
16004 16121 * See the comment in the above loop for why we pass
16005 16122 * over zero-length records.
16006 16123 */
16007 16124 if (rec.dtrd_size == 0) {
16008 16125 ASSERT(agg->dtag_hasarg);
16009 16126 continue;
16010 16127 }
16011 16128
16012 16129 if (nrecs-- == 0)
16013 16130 break;
16014 16131
16015 16132 rec.dtrd_offset -= offs;
16016 16133 bcopy(&rec, (void *)dest, sizeof (rec));
16017 16134 dest += sizeof (dtrace_recdesc_t);
16018 16135
16019 16136 if (act == &agg->dtag_action)
16020 16137 break;
16021 16138 }
16022 16139
16023 16140 mutex_exit(&dtrace_lock);
16024 16141
16025 16142 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
16026 16143 kmem_free(buf, size);
16027 16144 return (EFAULT);
16028 16145 }
16029 16146
16030 16147 kmem_free(buf, size);
16031 16148 return (0);
16032 16149 }
16033 16150
16034 16151 case DTRACEIOC_ENABLE: {
16035 16152 dof_hdr_t *dof;
16036 16153 dtrace_enabling_t *enab = NULL;
16037 16154 dtrace_vstate_t *vstate;
16038 16155 int err = 0;
16039 16156
16040 16157 *rv = 0;
16041 16158
16042 16159 /*
16043 16160 * If a NULL argument has been passed, we take this as our
16044 16161 * cue to reevaluate our enablings.
16045 16162 */
16046 16163 if (arg == NULL) {
16047 16164 dtrace_enabling_matchall();
16048 16165
16049 16166 return (0);
16050 16167 }
16051 16168
16052 16169 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
16053 16170 return (rval);
16054 16171
16055 16172 mutex_enter(&cpu_lock);
16056 16173 mutex_enter(&dtrace_lock);
16057 16174 vstate = &state->dts_vstate;
16058 16175
16059 16176 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
16060 16177 mutex_exit(&dtrace_lock);
16061 16178 mutex_exit(&cpu_lock);
16062 16179 dtrace_dof_destroy(dof);
16063 16180 return (EBUSY);
16064 16181 }
16065 16182
16066 16183 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
16067 16184 mutex_exit(&dtrace_lock);
16068 16185 mutex_exit(&cpu_lock);
16069 16186 dtrace_dof_destroy(dof);
16070 16187 return (EINVAL);
16071 16188 }
16072 16189
16073 16190 if ((rval = dtrace_dof_options(dof, state)) != 0) {
16074 16191 dtrace_enabling_destroy(enab);
16075 16192 mutex_exit(&dtrace_lock);
16076 16193 mutex_exit(&cpu_lock);
16077 16194 dtrace_dof_destroy(dof);
16078 16195 return (rval);
16079 16196 }
16080 16197
16081 16198 if ((err = dtrace_enabling_match(enab, rv)) == 0) {
16082 16199 err = dtrace_enabling_retain(enab);
16083 16200 } else {
16084 16201 dtrace_enabling_destroy(enab);
16085 16202 }
16086 16203
16087 16204 mutex_exit(&cpu_lock);
16088 16205 mutex_exit(&dtrace_lock);
16089 16206 dtrace_dof_destroy(dof);
16090 16207
16091 16208 return (err);
16092 16209 }
16093 16210
16094 16211 case DTRACEIOC_REPLICATE: {
16095 16212 dtrace_repldesc_t desc;
16096 16213 dtrace_probedesc_t *match = &desc.dtrpd_match;
16097 16214 dtrace_probedesc_t *create = &desc.dtrpd_create;
16098 16215 int err;
16099 16216
16100 16217 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16101 16218 return (EFAULT);
16102 16219
16103 16220 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16104 16221 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16105 16222 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16106 16223 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16107 16224
16108 16225 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16109 16226 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16110 16227 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16111 16228 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16112 16229
16113 16230 mutex_enter(&dtrace_lock);
16114 16231 err = dtrace_enabling_replicate(state, match, create);
16115 16232 mutex_exit(&dtrace_lock);
16116 16233
16117 16234 return (err);
16118 16235 }
16119 16236
16120 16237 case DTRACEIOC_PROBEMATCH:
16121 16238 case DTRACEIOC_PROBES: {
16122 16239 dtrace_probe_t *probe = NULL;
16123 16240 dtrace_probedesc_t desc;
16124 16241 dtrace_probekey_t pkey;
16125 16242 dtrace_id_t i;
16126 16243 int m = 0;
16127 16244 uint32_t priv;
16128 16245 uid_t uid;
16129 16246 zoneid_t zoneid;
16130 16247
16131 16248 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16132 16249 return (EFAULT);
16133 16250
16134 16251 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
16135 16252 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
16136 16253 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
16137 16254 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
16138 16255
16139 16256 /*
16140 16257 * Before we attempt to match this probe, we want to give
16141 16258 * all providers the opportunity to provide it.
16142 16259 */
16143 16260 if (desc.dtpd_id == DTRACE_IDNONE) {
16144 16261 mutex_enter(&dtrace_provider_lock);
16145 16262 dtrace_probe_provide(&desc, NULL);
16146 16263 mutex_exit(&dtrace_provider_lock);
16147 16264 desc.dtpd_id++;
16148 16265 }
16149 16266
16150 16267 if (cmd == DTRACEIOC_PROBEMATCH) {
16151 16268 dtrace_probekey(&desc, &pkey);
16152 16269 pkey.dtpk_id = DTRACE_IDNONE;
16153 16270 }
16154 16271
16155 16272 dtrace_cred2priv(cr, &priv, &uid, &zoneid);
16156 16273
16157 16274 mutex_enter(&dtrace_lock);
16158 16275
16159 16276 if (cmd == DTRACEIOC_PROBEMATCH) {
16160 16277 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
16161 16278 if ((probe = dtrace_probes[i - 1]) != NULL &&
16162 16279 (m = dtrace_match_probe(probe, &pkey,
16163 16280 priv, uid, zoneid)) != 0)
16164 16281 break;
16165 16282 }
16166 16283
16167 16284 if (m < 0) {
16168 16285 mutex_exit(&dtrace_lock);
16169 16286 return (EINVAL);
16170 16287 }
16171 16288
16172 16289 } else {
16173 16290 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
16174 16291 if ((probe = dtrace_probes[i - 1]) != NULL &&
16175 16292 dtrace_match_priv(probe, priv, uid, zoneid))
16176 16293 break;
16177 16294 }
16178 16295 }
16179 16296
16180 16297 if (probe == NULL) {
16181 16298 mutex_exit(&dtrace_lock);
16182 16299 return (ESRCH);
16183 16300 }
16184 16301
16185 16302 dtrace_probe_description(probe, &desc);
16186 16303 mutex_exit(&dtrace_lock);
16187 16304
16188 16305 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16189 16306 return (EFAULT);
16190 16307
16191 16308 return (0);
16192 16309 }
16193 16310
16194 16311 case DTRACEIOC_PROBEARG: {
16195 16312 dtrace_argdesc_t desc;
16196 16313 dtrace_probe_t *probe;
16197 16314 dtrace_provider_t *prov;
16198 16315
16199 16316 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16200 16317 return (EFAULT);
16201 16318
16202 16319 if (desc.dtargd_id == DTRACE_IDNONE)
16203 16320 return (EINVAL);
16204 16321
16205 16322 if (desc.dtargd_ndx == DTRACE_ARGNONE)
16206 16323 return (EINVAL);
16207 16324
16208 16325 mutex_enter(&dtrace_provider_lock);
16209 16326 mutex_enter(&mod_lock);
16210 16327 mutex_enter(&dtrace_lock);
16211 16328
16212 16329 if (desc.dtargd_id > dtrace_nprobes) {
16213 16330 mutex_exit(&dtrace_lock);
16214 16331 mutex_exit(&mod_lock);
16215 16332 mutex_exit(&dtrace_provider_lock);
16216 16333 return (EINVAL);
16217 16334 }
16218 16335
16219 16336 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
16220 16337 mutex_exit(&dtrace_lock);
16221 16338 mutex_exit(&mod_lock);
16222 16339 mutex_exit(&dtrace_provider_lock);
16223 16340 return (EINVAL);
16224 16341 }
16225 16342
16226 16343 mutex_exit(&dtrace_lock);
16227 16344
16228 16345 prov = probe->dtpr_provider;
16229 16346
16230 16347 if (prov->dtpv_pops.dtps_getargdesc == NULL) {
16231 16348 /*
16232 16349 * There isn't any typed information for this probe.
16233 16350 * Set the argument number to DTRACE_ARGNONE.
16234 16351 */
16235 16352 desc.dtargd_ndx = DTRACE_ARGNONE;
16236 16353 } else {
16237 16354 desc.dtargd_native[0] = '\0';
16238 16355 desc.dtargd_xlate[0] = '\0';
16239 16356 desc.dtargd_mapping = desc.dtargd_ndx;
16240 16357
16241 16358 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
16242 16359 probe->dtpr_id, probe->dtpr_arg, &desc);
16243 16360 }
16244 16361
16245 16362 mutex_exit(&mod_lock);
16246 16363 mutex_exit(&dtrace_provider_lock);
16247 16364
16248 16365 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16249 16366 return (EFAULT);
16250 16367
16251 16368 return (0);
16252 16369 }
16253 16370
16254 16371 case DTRACEIOC_GO: {
16255 16372 processorid_t cpuid;
16256 16373 rval = dtrace_state_go(state, &cpuid);
16257 16374
16258 16375 if (rval != 0)
16259 16376 return (rval);
16260 16377
16261 16378 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
16262 16379 return (EFAULT);
16263 16380
16264 16381 return (0);
16265 16382 }
16266 16383
16267 16384 case DTRACEIOC_STOP: {
16268 16385 processorid_t cpuid;
16269 16386
16270 16387 mutex_enter(&dtrace_lock);
16271 16388 rval = dtrace_state_stop(state, &cpuid);
16272 16389 mutex_exit(&dtrace_lock);
16273 16390
16274 16391 if (rval != 0)
16275 16392 return (rval);
16276 16393
16277 16394 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
16278 16395 return (EFAULT);
16279 16396
16280 16397 return (0);
16281 16398 }
16282 16399
16283 16400 case DTRACEIOC_DOFGET: {
16284 16401 dof_hdr_t hdr, *dof;
16285 16402 uint64_t len;
16286 16403
16287 16404 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
16288 16405 return (EFAULT);
16289 16406
16290 16407 mutex_enter(&dtrace_lock);
16291 16408 dof = dtrace_dof_create(state);
16292 16409 mutex_exit(&dtrace_lock);
16293 16410
16294 16411 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
16295 16412 rval = copyout(dof, (void *)arg, len);
16296 16413 dtrace_dof_destroy(dof);
16297 16414
16298 16415 return (rval == 0 ? 0 : EFAULT);
16299 16416 }
16300 16417
16301 16418 case DTRACEIOC_AGGSNAP:
16302 16419 case DTRACEIOC_BUFSNAP: {
16303 16420 dtrace_bufdesc_t desc;
16304 16421 caddr_t cached;
16305 16422 dtrace_buffer_t *buf;
16306 16423
16307 16424 if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
16308 16425 return (EFAULT);
16309 16426
16310 16427 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
16311 16428 return (EINVAL);
16312 16429
16313 16430 mutex_enter(&dtrace_lock);
16314 16431
16315 16432 if (cmd == DTRACEIOC_BUFSNAP) {
16316 16433 buf = &state->dts_buffer[desc.dtbd_cpu];
16317 16434 } else {
16318 16435 buf = &state->dts_aggbuffer[desc.dtbd_cpu];
16319 16436 }
16320 16437
16321 16438 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
16322 16439 size_t sz = buf->dtb_offset;
16323 16440
16324 16441 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
16325 16442 mutex_exit(&dtrace_lock);
16326 16443 return (EBUSY);
16327 16444 }
16328 16445
16329 16446 /*
16330 16447 * If this buffer has already been consumed, we're
16331 16448 * going to indicate that there's nothing left here
16332 16449 * to consume.
16333 16450 */
16334 16451 if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
16335 16452 mutex_exit(&dtrace_lock);
16336 16453
16337 16454 desc.dtbd_size = 0;
16338 16455 desc.dtbd_drops = 0;
16339 16456 desc.dtbd_errors = 0;
16340 16457 desc.dtbd_oldest = 0;
16341 16458 sz = sizeof (desc);
16342 16459
16343 16460 if (copyout(&desc, (void *)arg, sz) != 0)
16344 16461 return (EFAULT);
16345 16462
16346 16463 return (0);
16347 16464 }
16348 16465
16349 16466 /*
16350 16467 * If this is a ring buffer that has wrapped, we want
16351 16468 * to copy the whole thing out.
16352 16469 */
16353 16470 if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
16354 16471 dtrace_buffer_polish(buf);
16355 16472 sz = buf->dtb_size;
16356 16473 }
16357 16474
16358 16475 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
16359 16476 mutex_exit(&dtrace_lock);
16360 16477 return (EFAULT);
16361 16478 }
16362 16479
16363 16480 desc.dtbd_size = sz;
16364 16481 desc.dtbd_drops = buf->dtb_drops;
16365 16482 desc.dtbd_errors = buf->dtb_errors;
16366 16483 desc.dtbd_oldest = buf->dtb_xamot_offset;
16367 16484 desc.dtbd_timestamp = dtrace_gethrtime();
16368 16485
16369 16486 mutex_exit(&dtrace_lock);
16370 16487
16371 16488 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16372 16489 return (EFAULT);
16373 16490
16374 16491 buf->dtb_flags |= DTRACEBUF_CONSUMED;
16375 16492
16376 16493 return (0);
16377 16494 }
16378 16495
16379 16496 if (buf->dtb_tomax == NULL) {
16380 16497 ASSERT(buf->dtb_xamot == NULL);
16381 16498 mutex_exit(&dtrace_lock);
16382 16499 return (ENOENT);
16383 16500 }
16384 16501
16385 16502 cached = buf->dtb_tomax;
16386 16503 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
16387 16504
16388 16505 dtrace_xcall(desc.dtbd_cpu,
16389 16506 (dtrace_xcall_t)dtrace_buffer_switch, buf);
16390 16507
16391 16508 state->dts_errors += buf->dtb_xamot_errors;
16392 16509
16393 16510 /*
16394 16511 * If the buffers did not actually switch, then the cross call
16395 16512 * did not take place -- presumably because the given CPU is
16396 16513 * not in the ready set. If this is the case, we'll return
16397 16514 * ENOENT.
16398 16515 */
16399 16516 if (buf->dtb_tomax == cached) {
16400 16517 ASSERT(buf->dtb_xamot != cached);
16401 16518 mutex_exit(&dtrace_lock);
16402 16519 return (ENOENT);
16403 16520 }
16404 16521
16405 16522 ASSERT(cached == buf->dtb_xamot);
16406 16523
16407 16524 /*
16408 16525 * We have our snapshot; now copy it out.
16409 16526 */
16410 16527 if (copyout(buf->dtb_xamot, desc.dtbd_data,
16411 16528 buf->dtb_xamot_offset) != 0) {
16412 16529 mutex_exit(&dtrace_lock);
16413 16530 return (EFAULT);
16414 16531 }
16415 16532
16416 16533 desc.dtbd_size = buf->dtb_xamot_offset;
16417 16534 desc.dtbd_drops = buf->dtb_xamot_drops;
16418 16535 desc.dtbd_errors = buf->dtb_xamot_errors;
16419 16536 desc.dtbd_oldest = 0;
16420 16537 desc.dtbd_timestamp = buf->dtb_switched;
16421 16538
16422 16539 mutex_exit(&dtrace_lock);
16423 16540
16424 16541 /*
16425 16542 * Finally, copy out the buffer description.
16426 16543 */
16427 16544 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
16428 16545 return (EFAULT);
16429 16546
16430 16547 return (0);
16431 16548 }
16432 16549
16433 16550 case DTRACEIOC_CONF: {
16434 16551 dtrace_conf_t conf;
16435 16552
16436 16553 bzero(&conf, sizeof (conf));
16437 16554 conf.dtc_difversion = DIF_VERSION;
16438 16555 conf.dtc_difintregs = DIF_DIR_NREGS;
16439 16556 conf.dtc_diftupregs = DIF_DTR_NREGS;
16440 16557 conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
16441 16558
16442 16559 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
16443 16560 return (EFAULT);
16444 16561
16445 16562 return (0);
16446 16563 }
16447 16564
16448 16565 case DTRACEIOC_STATUS: {
16449 16566 dtrace_status_t stat;
16450 16567 dtrace_dstate_t *dstate;
16451 16568 int i, j;
16452 16569 uint64_t nerrs;
16453 16570
16454 16571 /*
16455 16572 * See the comment in dtrace_state_deadman() for the reason
16456 16573 * for setting dts_laststatus to INT64_MAX before setting
16457 16574 * it to the correct value.
16458 16575 */
16459 16576 state->dts_laststatus = INT64_MAX;
16460 16577 dtrace_membar_producer();
16461 16578 state->dts_laststatus = dtrace_gethrtime();
16462 16579
16463 16580 bzero(&stat, sizeof (stat));
16464 16581
16465 16582 mutex_enter(&dtrace_lock);
16466 16583
16467 16584 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
16468 16585 mutex_exit(&dtrace_lock);
16469 16586 return (ENOENT);
16470 16587 }
16471 16588
16472 16589 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
16473 16590 stat.dtst_exiting = 1;
16474 16591
16475 16592 nerrs = state->dts_errors;
16476 16593 dstate = &state->dts_vstate.dtvs_dynvars;
16477 16594
16478 16595 for (i = 0; i < NCPU; i++) {
16479 16596 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
16480 16597
16481 16598 stat.dtst_dyndrops += dcpu->dtdsc_drops;
16482 16599 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
16483 16600 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
16484 16601
16485 16602 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
16486 16603 stat.dtst_filled++;
16487 16604
16488 16605 nerrs += state->dts_buffer[i].dtb_errors;
16489 16606
16490 16607 for (j = 0; j < state->dts_nspeculations; j++) {
16491 16608 dtrace_speculation_t *spec;
16492 16609 dtrace_buffer_t *buf;
16493 16610
16494 16611 spec = &state->dts_speculations[j];
16495 16612 buf = &spec->dtsp_buffer[i];
16496 16613 stat.dtst_specdrops += buf->dtb_xamot_drops;
16497 16614 }
16498 16615 }
16499 16616
16500 16617 stat.dtst_specdrops_busy = state->dts_speculations_busy;
16501 16618 stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
16502 16619 stat.dtst_stkstroverflows = state->dts_stkstroverflows;
16503 16620 stat.dtst_dblerrors = state->dts_dblerrors;
16504 16621 stat.dtst_killed =
16505 16622 (state->dts_activity == DTRACE_ACTIVITY_KILLED);
16506 16623 stat.dtst_errors = nerrs;
16507 16624
16508 16625 mutex_exit(&dtrace_lock);
16509 16626
16510 16627 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
16511 16628 return (EFAULT);
16512 16629
16513 16630 return (0);
16514 16631 }
16515 16632
16516 16633 case DTRACEIOC_FORMAT: {
16517 16634 dtrace_fmtdesc_t fmt;
16518 16635 char *str;
16519 16636 int len;
16520 16637
16521 16638 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
16522 16639 return (EFAULT);
16523 16640
16524 16641 mutex_enter(&dtrace_lock);
16525 16642
16526 16643 if (fmt.dtfd_format == 0 ||
16527 16644 fmt.dtfd_format > state->dts_nformats) {
16528 16645 mutex_exit(&dtrace_lock);
16529 16646 return (EINVAL);
16530 16647 }
16531 16648
16532 16649 /*
16533 16650 * Format strings are allocated contiguously and they are
16534 16651 * never freed; if a format index is less than the number
16535 16652 * of formats, we can assert that the format map is non-NULL
16536 16653 * and that the format for the specified index is non-NULL.
16537 16654 */
16538 16655 ASSERT(state->dts_formats != NULL);
16539 16656 str = state->dts_formats[fmt.dtfd_format - 1];
16540 16657 ASSERT(str != NULL);
16541 16658
16542 16659 len = strlen(str) + 1;
16543 16660
16544 16661 if (len > fmt.dtfd_length) {
16545 16662 fmt.dtfd_length = len;
16546 16663
16547 16664 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
16548 16665 mutex_exit(&dtrace_lock);
16549 16666 return (EINVAL);
16550 16667 }
16551 16668 } else {
16552 16669 if (copyout(str, fmt.dtfd_string, len) != 0) {
16553 16670 mutex_exit(&dtrace_lock);
16554 16671 return (EINVAL);
16555 16672 }
16556 16673 }
16557 16674
16558 16675 mutex_exit(&dtrace_lock);
16559 16676 return (0);
16560 16677 }
16561 16678
16562 16679 default:
16563 16680 break;
16564 16681 }
16565 16682
16566 16683 return (ENOTTY);
16567 16684 }
16568 16685
16569 16686 /*ARGSUSED*/
16570 16687 static int
16571 16688 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
16572 16689 {
16573 16690 dtrace_state_t *state;
16574 16691
16575 16692 switch (cmd) {
16576 16693 case DDI_DETACH:
16577 16694 break;
16578 16695
16579 16696 case DDI_SUSPEND:
16580 16697 return (DDI_SUCCESS);
16581 16698
16582 16699 default:
16583 16700 return (DDI_FAILURE);
16584 16701 }
16585 16702
16586 16703 mutex_enter(&cpu_lock);
16587 16704 mutex_enter(&dtrace_provider_lock);
16588 16705 mutex_enter(&dtrace_lock);
16589 16706
16590 16707 ASSERT(dtrace_opens == 0);
16591 16708
16592 16709 if (dtrace_helpers > 0) {
16593 16710 mutex_exit(&dtrace_provider_lock);
16594 16711 mutex_exit(&dtrace_lock);
16595 16712 mutex_exit(&cpu_lock);
16596 16713 return (DDI_FAILURE);
16597 16714 }
16598 16715
16599 16716 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
16600 16717 mutex_exit(&dtrace_provider_lock);
16601 16718 mutex_exit(&dtrace_lock);
16602 16719 mutex_exit(&cpu_lock);
16603 16720 return (DDI_FAILURE);
16604 16721 }
16605 16722
16606 16723 dtrace_provider = NULL;
16607 16724
16608 16725 if ((state = dtrace_anon_grab()) != NULL) {
16609 16726 /*
16610 16727 * If there were ECBs on this state, the provider should
16611 16728 * have not been allowed to detach; assert that there is
16612 16729 * none.
16613 16730 */
16614 16731 ASSERT(state->dts_necbs == 0);
16615 16732 dtrace_state_destroy(state);
16616 16733
16617 16734 /*
16618 16735 * If we're being detached with anonymous state, we need to
16619 16736 * indicate to the kernel debugger that DTrace is now inactive.
16620 16737 */
16621 16738 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
16622 16739 }
16623 16740
16624 16741 bzero(&dtrace_anon, sizeof (dtrace_anon_t));
16625 16742 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
16626 16743 dtrace_cpu_init = NULL;
16627 16744 dtrace_helpers_cleanup = NULL;
16628 16745 dtrace_helpers_fork = NULL;
16629 16746 dtrace_cpustart_init = NULL;
16630 16747 dtrace_cpustart_fini = NULL;
16631 16748 dtrace_debugger_init = NULL;
16632 16749 dtrace_debugger_fini = NULL;
16633 16750 dtrace_modload = NULL;
16634 16751 dtrace_modunload = NULL;
16635 16752
16636 16753 ASSERT(dtrace_getf == 0);
16637 16754 ASSERT(dtrace_closef == NULL);
16638 16755
16639 16756 mutex_exit(&cpu_lock);
16640 16757
16641 16758 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
16642 16759 dtrace_probes = NULL;
16643 16760 dtrace_nprobes = 0;
16644 16761
16645 16762 dtrace_hash_destroy(dtrace_bymod);
16646 16763 dtrace_hash_destroy(dtrace_byfunc);
16647 16764 dtrace_hash_destroy(dtrace_byname);
16648 16765 dtrace_bymod = NULL;
16649 16766 dtrace_byfunc = NULL;
16650 16767 dtrace_byname = NULL;
16651 16768
16652 16769 kmem_cache_destroy(dtrace_state_cache);
16653 16770 vmem_destroy(dtrace_minor);
16654 16771 vmem_destroy(dtrace_arena);
16655 16772
16656 16773 if (dtrace_toxrange != NULL) {
16657 16774 kmem_free(dtrace_toxrange,
16658 16775 dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
16659 16776 dtrace_toxrange = NULL;
16660 16777 dtrace_toxranges = 0;
16661 16778 dtrace_toxranges_max = 0;
16662 16779 }
16663 16780
16664 16781 ddi_remove_minor_node(dtrace_devi, NULL);
16665 16782 dtrace_devi = NULL;
16666 16783
16667 16784 ddi_soft_state_fini(&dtrace_softstate);
16668 16785
16669 16786 ASSERT(dtrace_vtime_references == 0);
16670 16787 ASSERT(dtrace_opens == 0);
16671 16788 ASSERT(dtrace_retained == NULL);
16672 16789
16673 16790 mutex_exit(&dtrace_lock);
16674 16791 mutex_exit(&dtrace_provider_lock);
16675 16792
16676 16793 /*
16677 16794 * We don't destroy the task queue until after we have dropped our
16678 16795 * locks (taskq_destroy() may block on running tasks). To prevent
16679 16796 * attempting to do work after we have effectively detached but before
16680 16797 * the task queue has been destroyed, all tasks dispatched via the
16681 16798 * task queue must check that DTrace is still attached before
16682 16799 * performing any operation.
16683 16800 */
16684 16801 taskq_destroy(dtrace_taskq);
16685 16802 dtrace_taskq = NULL;
16686 16803
16687 16804 return (DDI_SUCCESS);
16688 16805 }
16689 16806
16690 16807 /*ARGSUSED*/
16691 16808 static int
16692 16809 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
16693 16810 {
16694 16811 int error;
16695 16812
16696 16813 switch (infocmd) {
16697 16814 case DDI_INFO_DEVT2DEVINFO:
16698 16815 *result = (void *)dtrace_devi;
16699 16816 error = DDI_SUCCESS;
16700 16817 break;
16701 16818 case DDI_INFO_DEVT2INSTANCE:
16702 16819 *result = (void *)0;
16703 16820 error = DDI_SUCCESS;
16704 16821 break;
16705 16822 default:
16706 16823 error = DDI_FAILURE;
16707 16824 }
16708 16825 return (error);
16709 16826 }
16710 16827
16711 16828 static struct cb_ops dtrace_cb_ops = {
16712 16829 dtrace_open, /* open */
16713 16830 dtrace_close, /* close */
16714 16831 nulldev, /* strategy */
16715 16832 nulldev, /* print */
16716 16833 nodev, /* dump */
16717 16834 nodev, /* read */
16718 16835 nodev, /* write */
16719 16836 dtrace_ioctl, /* ioctl */
16720 16837 nodev, /* devmap */
16721 16838 nodev, /* mmap */
16722 16839 nodev, /* segmap */
16723 16840 nochpoll, /* poll */
16724 16841 ddi_prop_op, /* cb_prop_op */
16725 16842 0, /* streamtab */
16726 16843 D_NEW | D_MP /* Driver compatibility flag */
16727 16844 };
16728 16845
16729 16846 static struct dev_ops dtrace_ops = {
16730 16847 DEVO_REV, /* devo_rev */
16731 16848 0, /* refcnt */
16732 16849 dtrace_info, /* get_dev_info */
16733 16850 nulldev, /* identify */
16734 16851 nulldev, /* probe */
16735 16852 dtrace_attach, /* attach */
16736 16853 dtrace_detach, /* detach */
16737 16854 nodev, /* reset */
16738 16855 &dtrace_cb_ops, /* driver operations */
16739 16856 NULL, /* bus operations */
16740 16857 nodev, /* dev power */
16741 16858 ddi_quiesce_not_needed, /* quiesce */
16742 16859 };
16743 16860
16744 16861 static struct modldrv modldrv = {
16745 16862 &mod_driverops, /* module type (this is a pseudo driver) */
16746 16863 "Dynamic Tracing", /* name of module */
16747 16864 &dtrace_ops, /* driver ops */
16748 16865 };
16749 16866
16750 16867 static struct modlinkage modlinkage = {
16751 16868 MODREV_1,
16752 16869 (void *)&modldrv,
16753 16870 NULL
16754 16871 };
16755 16872
16756 16873 int
16757 16874 _init(void)
16758 16875 {
16759 16876 return (mod_install(&modlinkage));
16760 16877 }
16761 16878
16762 16879 int
16763 16880 _info(struct modinfo *modinfop)
16764 16881 {
16765 16882 return (mod_info(&modlinkage, modinfop));
16766 16883 }
16767 16884
16768 16885 int
16769 16886 _fini(void)
16770 16887 {
16771 16888 return (mod_remove(&modlinkage));
16772 16889 }
↓ open down ↓ |
12273 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX