]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
b0d623f7 | 23 | * Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
2d21ac55 A |
24 | * Use is subject to license terms. |
25 | */ | |
26 | ||
b0d623f7 | 27 | /* #pragma ident "@(#)dtrace.c 1.65 08/07/02 SMI" */ |
2d21ac55 A |
28 | |
29 | /* | |
30 | * DTrace - Dynamic Tracing for Solaris | |
31 | * | |
32 | * This is the implementation of the Solaris Dynamic Tracing framework | |
33 | * (DTrace). The user-visible interface to DTrace is described at length in | |
34 | * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace | |
35 | * library, the in-kernel DTrace framework, and the DTrace providers are | |
36 | * described in the block comments in the <sys/dtrace.h> header file. The | |
37 | * internal architecture of DTrace is described in the block comments in the | |
38 | * <sys/dtrace_impl.h> header file. The comments contained within the DTrace | |
39 | * implementation very much assume mastery of all of these sources; if one has | |
40 | * an unanswered question about the implementation, one should consult them | |
41 | * first. | |
42 | * | |
43 | * The functions here are ordered roughly as follows: | |
44 | * | |
45 | * - Probe context functions | |
46 | * - Probe hashing functions | |
47 | * - Non-probe context utility functions | |
48 | * - Matching functions | |
49 | * - Provider-to-Framework API functions | |
50 | * - Probe management functions | |
51 | * - DIF object functions | |
52 | * - Format functions | |
53 | * - Predicate functions | |
54 | * - ECB functions | |
55 | * - Buffer functions | |
56 | * - Enabling functions | |
57 | * - DOF functions | |
58 | * - Anonymous enabling functions | |
59 | * - Consumer state functions | |
60 | * - Helper functions | |
61 | * - Hook functions | |
62 | * - Driver cookbook functions | |
63 | * | |
64 | * Each group of functions begins with a block comment labelled the "DTrace | |
65 | * [Group] Functions", allowing one to find each block by searching forward | |
66 | * on capital-f functions. | |
67 | */ | |
b0d623f7 A |
68 | #if !defined(__APPLE__) |
69 | #include <sys/errno.h> | |
70 | #include <sys/stat.h> | |
71 | #include <sys/modctl.h> | |
72 | #include <sys/conf.h> | |
73 | #include <sys/systm.h> | |
74 | #include <sys/ddi.h> | |
75 | #include <sys/sunddi.h> | |
76 | #include <sys/cpuvar.h> | |
77 | #include <sys/kmem.h> | |
78 | #include <sys/strsubr.h> | |
79 | #include <sys/sysmacros.h> | |
80 | #include <sys/dtrace_impl.h> | |
81 | #include <sys/atomic.h> | |
82 | #include <sys/cmn_err.h> | |
83 | #include <sys/mutex_impl.h> | |
84 | #include <sys/rwlock_impl.h> | |
85 | #include <sys/ctf_api.h> | |
86 | #include <sys/panic.h> | |
87 | #include <sys/priv_impl.h> | |
88 | #include <sys/policy.h> | |
89 | #include <sys/cred_impl.h> | |
90 | #include <sys/procfs_isa.h> | |
91 | #include <sys/taskq.h> | |
92 | #include <sys/mkdev.h> | |
93 | #include <sys/kdi.h> | |
94 | #include <sys/zone.h> | |
95 | #else | |
2d21ac55 A |
96 | #include <sys/errno.h> |
97 | #include <sys/types.h> | |
98 | #include <sys/stat.h> | |
99 | #include <sys/conf.h> | |
100 | #include <sys/systm.h> | |
101 | #include <sys/dtrace_impl.h> | |
102 | #include <sys/param.h> | |
103 | #include <sys/ioctl.h> | |
104 | #include <sys/fcntl.h> | |
105 | #include <miscfs/devfs/devfs.h> | |
106 | #include <sys/malloc.h> | |
107 | #include <sys/kernel_types.h> | |
108 | #include <sys/proc_internal.h> | |
109 | #include <sys/uio_internal.h> | |
110 | #include <sys/kauth.h> | |
111 | #include <vm/pmap.h> | |
112 | #include <sys/user.h> | |
113 | #include <mach/exception_types.h> | |
114 | #include <sys/signalvar.h> | |
115 | #include <kern/zalloc.h> | |
b0d623f7 A |
116 | #include <kern/ast.h> |
117 | #include <netinet/in.h> | |
118 | ||
119 | #if defined(__APPLE__) | |
120 | extern uint32_t pmap_find_phys(void *, uint64_t); | |
121 | extern boolean_t pmap_valid_page(uint32_t); | |
122 | #endif /* __APPLE__ */ | |
123 | ||
124 | ||
125 | /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */ | |
126 | #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */ | |
2d21ac55 A |
127 | |
128 | #define t_predcache t_dtrace_predcache /* Cosmetic. Helps readability of thread.h */ | |
129 | ||
130 | extern void dtrace_suspend(void); | |
131 | extern void dtrace_resume(void); | |
132 | extern void dtrace_init(void); | |
133 | extern void helper_init(void); | |
b0d623f7 A |
134 | extern void fasttrap_init(void); |
135 | extern void dtrace_lazy_dofs_duplicate(proc_t *, proc_t *); | |
136 | extern void dtrace_lazy_dofs_destroy(proc_t *); | |
137 | extern void dtrace_postinit(void); | |
2d21ac55 A |
138 | |
139 | #include "../../../osfmk/chud/chud_dtrace.h" | |
140 | ||
141 | extern kern_return_t chudxnu_dtrace_callback | |
142 | (uint64_t selector, uint64_t *args, uint32_t count); | |
b0d623f7 | 143 | #endif /* __APPLE__ */ |
2d21ac55 A |
144 | |
145 | /* | |
146 | * DTrace Tunable Variables | |
147 | * | |
148 | * The following variables may be tuned by adding a line to /etc/system that | |
149 | * includes both the name of the DTrace module ("dtrace") and the name of the | |
150 | * variable. For example: | |
151 | * | |
152 | * set dtrace:dtrace_destructive_disallow = 1 | |
153 | * | |
154 | * In general, the only variables that one should be tuning this way are those | |
155 | * that affect system-wide DTrace behavior, and for which the default behavior | |
156 | * is undesirable. Most of these variables are tunable on a per-consumer | |
157 | * basis using DTrace options, and need not be tuned on a system-wide basis. | |
158 | * When tuning these variables, avoid pathological values; while some attempt | |
159 | * is made to verify the integrity of these variables, they are not considered | |
160 | * part of the supported interface to DTrace, and they are therefore not | |
161 | * checked comprehensively. Further, these variables should not be tuned | |
162 | * dynamically via "mdb -kw" or other means; they should only be tuned via | |
163 | * /etc/system. | |
164 | */ | |
165 | int dtrace_destructive_disallow = 0; | |
2d21ac55 A |
166 | dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); |
167 | size_t dtrace_difo_maxsize = (256 * 1024); | |
b0d623f7 | 168 | dtrace_optval_t dtrace_dof_maxsize = (384 * 1024); |
2d21ac55 A |
169 | size_t dtrace_global_maxsize = (16 * 1024); |
170 | size_t dtrace_actions_max = (16 * 1024); | |
171 | size_t dtrace_retain_max = 1024; | |
172 | dtrace_optval_t dtrace_helper_actions_max = 32; | |
173 | dtrace_optval_t dtrace_helper_providers_max = 32; | |
174 | dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); | |
175 | size_t dtrace_strsize_default = 256; | |
176 | dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ | |
177 | dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ | |
178 | dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ | |
179 | dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ | |
180 | dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ | |
181 | dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ | |
182 | dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ | |
183 | dtrace_optval_t dtrace_nspec_default = 1; | |
184 | dtrace_optval_t dtrace_specsize_default = 32 * 1024; | |
185 | dtrace_optval_t dtrace_stackframes_default = 20; | |
186 | dtrace_optval_t dtrace_ustackframes_default = 20; | |
187 | dtrace_optval_t dtrace_jstackframes_default = 50; | |
188 | dtrace_optval_t dtrace_jstackstrsize_default = 512; | |
189 | int dtrace_msgdsize_max = 128; | |
190 | hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ | |
191 | hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ | |
192 | int dtrace_devdepth_max = 32; | |
193 | int dtrace_err_verbose; | |
194 | hrtime_t dtrace_deadman_interval = NANOSEC; | |
195 | hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; | |
196 | hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; | |
197 | ||
198 | /* | |
199 | * DTrace External Variables | |
200 | * | |
201 | * As dtrace(7D) is a kernel module, any DTrace variables are obviously | |
202 | * available to DTrace consumers via the backtick (`) syntax. One of these, | |
203 | * dtrace_zero, is made deliberately so: it is provided as a source of | |
204 | * well-known, zero-filled memory. While this variable is not documented, | |
205 | * it is used by some translators as an implementation detail. | |
206 | */ | |
207 | const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ | |
208 | ||
209 | /* | |
210 | * DTrace Internal Variables | |
211 | */ | |
212 | static dev_info_t *dtrace_devi; /* device info */ | |
213 | static vmem_t *dtrace_arena; /* probe ID arena */ | |
214 | static vmem_t *dtrace_minor; /* minor number arena */ | |
215 | static taskq_t *dtrace_taskq; /* task queue */ | |
216 | static dtrace_probe_t **dtrace_probes; /* array of all probes */ | |
217 | static int dtrace_nprobes; /* number of probes */ | |
218 | static dtrace_provider_t *dtrace_provider; /* provider list */ | |
219 | static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ | |
220 | static int dtrace_opens; /* number of opens */ | |
221 | static int dtrace_helpers; /* number of helpers */ | |
222 | static void *dtrace_softstate; /* softstate pointer */ | |
223 | static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ | |
224 | static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ | |
225 | static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ | |
226 | static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ | |
227 | static int dtrace_toxranges; /* number of toxic ranges */ | |
228 | static int dtrace_toxranges_max; /* size of toxic range array */ | |
229 | static dtrace_anon_t dtrace_anon; /* anonymous enabling */ | |
230 | static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ | |
231 | static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ | |
232 | static kthread_t *dtrace_panicked; /* panicking thread */ | |
233 | static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ | |
234 | static dtrace_genid_t dtrace_probegen; /* current probe generation */ | |
235 | static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ | |
236 | static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ | |
b0d623f7 | 237 | static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ |
2d21ac55 A |
238 | static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ |
239 | #if defined(__APPLE__) | |
b0d623f7 | 240 | static int dtrace_dof_mode; /* See dtrace_impl.h for a description of Darwin's dof modes. */ |
2d21ac55 A |
241 | #endif |
242 | ||
243 | #if defined(__APPLE__) | |
2d21ac55 A |
244 | /* |
245 | * To save memory, some common memory allocations are given a | |
b0d623f7 | 246 | * unique zone. For example, dtrace_probe_t is 72 bytes in size, |
2d21ac55 A |
247 | * which means it would fall into the kalloc.128 bucket. With |
248 | * 20k elements allocated, the space saved is substantial. | |
249 | */ | |
250 | ||
251 | struct zone *dtrace_probe_t_zone; | |
b0d623f7 | 252 | #endif /* __APPLE__ */ |
2d21ac55 A |
253 | |
254 | /* | |
255 | * DTrace Locking | |
256 | * DTrace is protected by three (relatively coarse-grained) locks: | |
257 | * | |
258 | * (1) dtrace_lock is required to manipulate essentially any DTrace state, | |
259 | * including enabling state, probes, ECBs, consumer state, helper state, | |
260 | * etc. Importantly, dtrace_lock is _not_ required when in probe context; | |
261 | * probe context is lock-free -- synchronization is handled via the | |
262 | * dtrace_sync() cross call mechanism. | |
263 | * | |
264 | * (2) dtrace_provider_lock is required when manipulating provider state, or | |
265 | * when provider state must be held constant. | |
266 | * | |
267 | * (3) dtrace_meta_lock is required when manipulating meta provider state, or | |
268 | * when meta provider state must be held constant. | |
269 | * | |
270 | * The lock ordering between these three locks is dtrace_meta_lock before | |
271 | * dtrace_provider_lock before dtrace_lock. (In particular, there are | |
272 | * several places where dtrace_provider_lock is held by the framework as it | |
273 | * calls into the providers -- which then call back into the framework, | |
274 | * grabbing dtrace_lock.) | |
275 | * | |
276 | * There are two other locks in the mix: mod_lock and cpu_lock. With respect | |
277 | * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical | |
278 | * role as a coarse-grained lock; it is acquired before both of these locks. | |
279 | * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must | |
280 | * be acquired _between_ dtrace_meta_lock and any other DTrace locks. | |
281 | * mod_lock is similar with respect to dtrace_provider_lock in that it must be | |
282 | * acquired _between_ dtrace_provider_lock and dtrace_lock. | |
283 | */ | |
284 | ||
b0d623f7 A |
285 | #if !defined(__APPLE__) |
286 | static kmutex_t dtrace_lock; /* probe state lock */ | |
287 | static kmutex_t dtrace_provider_lock; /* provider state lock */ | |
288 | static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ | |
289 | #else | |
2d21ac55 A |
290 | /* |
291 | * APPLE NOTE: | |
292 | * | |
293 | * All kmutex_t vars have been changed to lck_mtx_t. | |
294 | * Note that lck_mtx_t's require explicit initialization. | |
295 | * | |
296 | * mutex_enter() becomes lck_mtx_lock() | |
297 | * mutex_exit() becomes lck_mtx_unlock() | |
298 | * | |
299 | * Lock asserts are changed like this: | |
300 | * | |
301 | * ASSERT(MUTEX_HELD(&cpu_lock)); | |
302 | * becomes: | |
303 | * lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
304 | * | |
305 | * Due to the number of these changes, they are not called out explicitly. | |
306 | */ | |
307 | static lck_mtx_t dtrace_lock; /* probe state lock */ | |
308 | static lck_mtx_t dtrace_provider_lock; /* provider state lock */ | |
309 | static lck_mtx_t dtrace_meta_lock; /* meta-provider state lock */ | |
2d21ac55 | 310 | static lck_rw_t dtrace_dof_mode_lock; /* dof mode lock */ |
b0d623f7 | 311 | #endif /* __APPLE__ */ |
2d21ac55 A |
312 | |
313 | /* | |
314 | * DTrace Provider Variables | |
315 | * | |
316 | * These are the variables relating to DTrace as a provider (that is, the | |
317 | * provider of the BEGIN, END, and ERROR probes). | |
318 | */ | |
319 | static dtrace_pattr_t dtrace_provider_attr = { | |
320 | { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, | |
321 | { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, | |
322 | { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, | |
323 | { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, | |
324 | { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, | |
325 | }; | |
326 | ||
327 | static void | |
328 | dtrace_nullop(void) | |
329 | {} | |
330 | ||
331 | static dtrace_pops_t dtrace_provider_ops = { | |
332 | (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, | |
333 | (void (*)(void *, struct modctl *))dtrace_nullop, | |
334 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, | |
335 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, | |
336 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, | |
337 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, | |
338 | NULL, | |
339 | NULL, | |
340 | NULL, | |
341 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop | |
342 | }; | |
343 | ||
344 | static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ | |
345 | static dtrace_id_t dtrace_probeid_end; /* special END probe */ | |
346 | dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ | |
347 | ||
348 | /* | |
349 | * DTrace Helper Tracing Variables | |
350 | */ | |
351 | uint32_t dtrace_helptrace_next = 0; | |
352 | uint32_t dtrace_helptrace_nlocals; | |
353 | char *dtrace_helptrace_buffer; | |
b0d623f7 | 354 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 355 | int dtrace_helptrace_bufsize = 512 * 1024; |
b0d623f7 A |
356 | #else |
357 | size_t dtrace_helptrace_bufsize = 512 * 1024; | |
358 | #endif /* __APPLE__ */ | |
2d21ac55 | 359 | |
b0d623f7 | 360 | #if DEBUG |
2d21ac55 A |
361 | int dtrace_helptrace_enabled = 1; |
362 | #else | |
363 | int dtrace_helptrace_enabled = 0; | |
364 | #endif | |
365 | ||
366 | /* | |
367 | * DTrace Error Hashing | |
368 | * | |
369 | * On DEBUG kernels, DTrace will track the errors that has seen in a hash | |
370 | * table. This is very useful for checking coverage of tests that are | |
371 | * expected to induce DIF or DOF processing errors, and may be useful for | |
372 | * debugging problems in the DIF code generator or in DOF generation . The | |
373 | * error hash may be examined with the ::dtrace_errhash MDB dcmd. | |
374 | */ | |
b0d623f7 | 375 | #if DEBUG |
2d21ac55 A |
376 | static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; |
377 | static const char *dtrace_errlast; | |
378 | static kthread_t *dtrace_errthread; | |
379 | static lck_mtx_t dtrace_errlock; | |
380 | #endif | |
381 | ||
382 | /* | |
383 | * DTrace Macros and Constants | |
384 | * | |
385 | * These are various macros that are useful in various spots in the | |
386 | * implementation, along with a few random constants that have no meaning | |
387 | * outside of the implementation. There is no real structure to this cpp | |
388 | * mishmash -- but is there ever? | |
389 | */ | |
390 | #define DTRACE_HASHSTR(hash, probe) \ | |
391 | dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) | |
392 | ||
393 | #define DTRACE_HASHNEXT(hash, probe) \ | |
394 | (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) | |
395 | ||
396 | #define DTRACE_HASHPREV(hash, probe) \ | |
397 | (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) | |
398 | ||
399 | #define DTRACE_HASHEQ(hash, lhs, rhs) \ | |
400 | (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ | |
401 | *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) | |
402 | ||
403 | #define DTRACE_AGGHASHSIZE_SLEW 17 | |
404 | ||
b0d623f7 A |
405 | #define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) |
406 | ||
2d21ac55 A |
407 | /* |
408 | * The key for a thread-local variable consists of the lower 61 bits of the | |
409 | * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. | |
410 | * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never | |
411 | * equal to a variable identifier. This is necessary (but not sufficient) to | |
412 | * assure that global associative arrays never collide with thread-local | |
413 | * variables. To guarantee that they cannot collide, we must also define the | |
414 | * order for keying dynamic variables. That order is: | |
415 | * | |
416 | * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] | |
417 | * | |
418 | * Because the variable-key and the tls-key are in orthogonal spaces, there is | |
419 | * no way for a global variable key signature to match a thread-local key | |
420 | * signature. | |
421 | */ | |
422 | #if !defined(__APPLE__) | |
423 | #define DTRACE_TLS_THRKEY(where) { \ | |
424 | uint_t intr = 0; \ | |
425 | uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ | |
426 | for (; actv; actv >>= 1) \ | |
427 | intr++; \ | |
428 | ASSERT(intr < (1 << 3)); \ | |
429 | (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ | |
430 | (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ | |
431 | } | |
b0d623f7 A |
432 | #else |
433 | #if (defined(__x86_64__) || defined(__ppc64__)) | |
434 | /* FIXME: two function calls!! */ | |
435 | #define DTRACE_TLS_THRKEY(where) { \ | |
436 | uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \ | |
437 | uint64_t thr = (uintptr_t)current_thread(); \ | |
438 | ASSERT(intr < (1 << 3)); \ | |
439 | (where) = ((thr + DIF_VARIABLE_MAX) & \ | |
440 | (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ | |
441 | } | |
2d21ac55 | 442 | #else |
b0d623f7 | 443 | /* FIXME: three function calls!!! */ |
2d21ac55 | 444 | #define DTRACE_TLS_THRKEY(where) { \ |
b0d623f7 A |
445 | uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \ |
446 | uint64_t thr = (uintptr_t)current_thread(); \ | |
2d21ac55 A |
447 | uint_t pid = (uint_t)proc_selfpid(); \ |
448 | ASSERT(intr < (1 << 3)); \ | |
b0d623f7 | 449 | (where) = (((thr << 32 | pid) + DIF_VARIABLE_MAX) & \ |
2d21ac55 A |
450 | (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ |
451 | } | |
b0d623f7 | 452 | #endif |
2d21ac55 A |
453 | #endif /* __APPLE__ */ |
454 | ||
b0d623f7 A |
455 | #define DT_BSWAP_8(x) ((x) & 0xff) |
456 | #define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) | |
457 | #define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) | |
458 | #define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) | |
459 | ||
460 | #define DT_MASK_LO 0x00000000FFFFFFFFULL | |
461 | ||
2d21ac55 A |
462 | #define DTRACE_STORE(type, tomax, offset, what) \ |
463 | *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); | |
464 | ||
465 | #if !defined(__APPLE__) | |
b0d623f7 | 466 | #ifndef __i386 |
2d21ac55 A |
467 | #define DTRACE_ALIGNCHECK(addr, size, flags) \ |
468 | if (addr & (size - 1)) { \ | |
469 | *flags |= CPU_DTRACE_BADALIGN; \ | |
470 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ | |
471 | return (0); \ | |
472 | } | |
473 | #else | |
474 | #define DTRACE_ALIGNCHECK(addr, size, flags) | |
475 | #endif | |
b0d623f7 A |
476 | #else /* __APPLE__ */ |
477 | #define DTRACE_ALIGNCHECK(addr, size, flags) \ | |
478 | if (addr & (MIN(size,4) - 1)) { \ | |
479 | *flags |= CPU_DTRACE_BADALIGN; \ | |
480 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ | |
481 | return (0); \ | |
482 | } | |
483 | #endif /* __APPLE__ */ | |
484 | ||
485 | /* | |
486 | * Test whether a range of memory starting at testaddr of size testsz falls | |
487 | * within the range of memory described by addr, sz. We take care to avoid | |
488 | * problems with overflow and underflow of the unsigned quantities, and | |
489 | * disallow all negative sizes. Ranges of size 0 are allowed. | |
490 | */ | |
491 | #define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ | |
492 | ((testaddr) - (baseaddr) < (basesz) && \ | |
493 | (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ | |
494 | (testaddr) + (testsz) >= (testaddr)) | |
495 | ||
496 | /* | |
497 | * Test whether alloc_sz bytes will fit in the scratch region. We isolate | |
498 | * alloc_sz on the righthand side of the comparison in order to avoid overflow | |
499 | * or underflow in the comparison with it. This is simpler than the INRANGE | |
500 | * check above, because we know that the dtms_scratch_ptr is valid in the | |
501 | * range. Allocations of size zero are allowed. | |
502 | */ | |
503 | #define DTRACE_INSCRATCH(mstate, alloc_sz) \ | |
504 | ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ | |
505 | (mstate)->dtms_scratch_ptr >= (alloc_sz)) | |
2d21ac55 | 506 | |
b0d623f7 | 507 | #if !defined(__APPLE__) |
2d21ac55 A |
508 | #define DTRACE_LOADFUNC(bits) \ |
509 | /*CSTYLED*/ \ | |
510 | uint##bits##_t \ | |
511 | dtrace_load##bits(uintptr_t addr) \ | |
512 | { \ | |
513 | size_t size = bits / NBBY; \ | |
514 | /*CSTYLED*/ \ | |
515 | uint##bits##_t rval; \ | |
516 | int i; \ | |
517 | volatile uint16_t *flags = (volatile uint16_t *) \ | |
518 | &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ | |
519 | \ | |
520 | DTRACE_ALIGNCHECK(addr, size, flags); \ | |
521 | \ | |
522 | for (i = 0; i < dtrace_toxranges; i++) { \ | |
523 | if (addr >= dtrace_toxrange[i].dtt_limit) \ | |
524 | continue; \ | |
525 | \ | |
526 | if (addr + size <= dtrace_toxrange[i].dtt_base) \ | |
527 | continue; \ | |
528 | \ | |
529 | /* \ | |
530 | * This address falls within a toxic region; return 0. \ | |
531 | */ \ | |
532 | *flags |= CPU_DTRACE_BADADDR; \ | |
533 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ | |
534 | return (0); \ | |
535 | } \ | |
536 | \ | |
537 | *flags |= CPU_DTRACE_NOFAULT; \ | |
538 | /*CSTYLED*/ \ | |
539 | rval = *((volatile uint##bits##_t *)addr); \ | |
540 | *flags &= ~CPU_DTRACE_NOFAULT; \ | |
541 | \ | |
b0d623f7 | 542 | return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ |
2d21ac55 | 543 | } |
b0d623f7 | 544 | #else /* __APPLE__ */ |
2d21ac55 A |
545 | #define RECOVER_LABEL(bits) __asm__ volatile("_dtraceLoadRecover" #bits ":" ); |
546 | ||
b0d623f7 | 547 | #if (defined(__i386__) || defined (__x86_64__)) |
2d21ac55 A |
548 | #define DTRACE_LOADFUNC(bits) \ |
549 | /*CSTYLED*/ \ | |
550 | extern vm_offset_t dtraceLoadRecover##bits; \ | |
551 | uint##bits##_t dtrace_load##bits(uintptr_t addr); \ | |
552 | \ | |
553 | uint##bits##_t \ | |
554 | dtrace_load##bits(uintptr_t addr) \ | |
555 | { \ | |
556 | size_t size = bits / NBBY; \ | |
557 | /*CSTYLED*/ \ | |
558 | uint##bits##_t rval = 0; \ | |
559 | int i; \ | |
2d21ac55 A |
560 | volatile uint16_t *flags = (volatile uint16_t *) \ |
561 | &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ | |
562 | \ | |
563 | DTRACE_ALIGNCHECK(addr, size, flags); \ | |
564 | \ | |
565 | for (i = 0; i < dtrace_toxranges; i++) { \ | |
566 | if (addr >= dtrace_toxrange[i].dtt_limit) \ | |
567 | continue; \ | |
568 | \ | |
569 | if (addr + size <= dtrace_toxrange[i].dtt_base) \ | |
570 | continue; \ | |
571 | \ | |
572 | /* \ | |
573 | * This address falls within a toxic region; return 0. \ | |
574 | */ \ | |
575 | *flags |= CPU_DTRACE_BADADDR; \ | |
576 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ | |
577 | return (0); \ | |
578 | } \ | |
579 | \ | |
b0d623f7 A |
580 | { \ |
581 | volatile vm_offset_t recover = (vm_offset_t)&dtraceLoadRecover##bits; \ | |
582 | *flags |= CPU_DTRACE_NOFAULT; \ | |
583 | recover = dtrace_set_thread_recover(current_thread(), recover); \ | |
584 | /*CSTYLED*/ \ | |
585 | /* \ | |
586 | * PR6394061 - avoid device memory that is unpredictably \ | |
587 | * mapped and unmapped \ | |
588 | */ \ | |
589 | if (pmap_valid_page(pmap_find_phys(kernel_pmap, addr))) \ | |
590 | rval = *((volatile uint##bits##_t *)addr); \ | |
591 | RECOVER_LABEL(bits); \ | |
592 | (void)dtrace_set_thread_recover(current_thread(), recover); \ | |
593 | *flags &= ~CPU_DTRACE_NOFAULT; \ | |
594 | } \ | |
595 | \ | |
596 | return (rval); \ | |
597 | } | |
598 | #else /* all other architectures */ | |
599 | #define DTRACE_LOADFUNC(bits) \ | |
600 | /*CSTYLED*/ \ | |
601 | extern vm_offset_t dtraceLoadRecover##bits; \ | |
602 | uint##bits##_t dtrace_load##bits(uintptr_t addr); \ | |
603 | \ | |
604 | uint##bits##_t \ | |
605 | dtrace_load##bits(uintptr_t addr) \ | |
606 | { \ | |
607 | size_t size = bits / NBBY; \ | |
608 | /*CSTYLED*/ \ | |
609 | uint##bits##_t rval = 0; \ | |
610 | int i; \ | |
611 | volatile uint16_t *flags = (volatile uint16_t *) \ | |
612 | &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; \ | |
613 | \ | |
614 | DTRACE_ALIGNCHECK(addr, size, flags); \ | |
615 | \ | |
616 | for (i = 0; i < dtrace_toxranges; i++) { \ | |
617 | if (addr >= dtrace_toxrange[i].dtt_limit) \ | |
618 | continue; \ | |
619 | \ | |
620 | if (addr + size <= dtrace_toxrange[i].dtt_base) \ | |
621 | continue; \ | |
2d21ac55 | 622 | \ |
b0d623f7 A |
623 | /* \ |
624 | * This address falls within a toxic region; return 0. \ | |
625 | */ \ | |
2d21ac55 A |
626 | *flags |= CPU_DTRACE_BADADDR; \ |
627 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr; \ | |
628 | return (0); \ | |
629 | } \ | |
630 | \ | |
631 | { \ | |
632 | volatile vm_offset_t recover = (vm_offset_t)&dtraceLoadRecover##bits; \ | |
633 | *flags |= CPU_DTRACE_NOFAULT; \ | |
634 | recover = dtrace_set_thread_recover(current_thread(), recover); \ | |
b0d623f7 | 635 | /*CSTYLED*/ \ |
2d21ac55 A |
636 | rval = *((volatile uint##bits##_t *)addr); \ |
637 | RECOVER_LABEL(bits); \ | |
638 | (void)dtrace_set_thread_recover(current_thread(), recover); \ | |
639 | *flags &= ~CPU_DTRACE_NOFAULT; \ | |
640 | } \ | |
641 | \ | |
642 | return (rval); \ | |
643 | } | |
b0d623f7 | 644 | #endif |
2d21ac55 A |
645 | #endif /* __APPLE__ */ |
646 | ||
2d21ac55 A |
647 | #ifdef __LP64__ |
648 | #define dtrace_loadptr dtrace_load64 | |
649 | #else | |
650 | #define dtrace_loadptr dtrace_load32 | |
651 | #endif | |
652 | ||
653 | #define DTRACE_DYNHASH_FREE 0 | |
654 | #define DTRACE_DYNHASH_SINK 1 | |
655 | #define DTRACE_DYNHASH_VALID 2 | |
656 | ||
657 | #define DTRACE_MATCH_NEXT 0 | |
658 | #define DTRACE_MATCH_DONE 1 | |
659 | #define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') | |
660 | #define DTRACE_STATE_ALIGN 64 | |
661 | ||
662 | #define DTRACE_FLAGS2FLT(flags) \ | |
663 | (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ | |
664 | ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ | |
665 | ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ | |
666 | ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ | |
667 | ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ | |
668 | ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ | |
669 | ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ | |
670 | ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ | |
b0d623f7 | 671 | ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ |
2d21ac55 A |
672 | DTRACEFLT_UNKNOWN) |
673 | ||
674 | #define DTRACEACT_ISSTRING(act) \ | |
675 | ((act)->dta_kind == DTRACEACT_DIFEXPR && \ | |
676 | (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) | |
677 | ||
b0d623f7 A |
678 | |
679 | #if defined (__APPLE__) | |
680 | /* Avoid compiler warnings when assigning regs[rd] = NULL */ | |
681 | #ifdef NULL | |
682 | #undef NULL | |
683 | #define NULL (uintptr_t)0 | |
684 | #endif | |
685 | #endif /* __APPLE__ */ | |
686 | ||
687 | static size_t dtrace_strlen(const char *, size_t); | |
2d21ac55 A |
688 | static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); |
689 | static void dtrace_enabling_provide(dtrace_provider_t *); | |
690 | static int dtrace_enabling_match(dtrace_enabling_t *, int *); | |
691 | static void dtrace_enabling_matchall(void); | |
692 | static dtrace_state_t *dtrace_anon_grab(void); | |
693 | static uint64_t dtrace_helper(int, dtrace_mstate_t *, | |
694 | dtrace_state_t *, uint64_t, uint64_t); | |
695 | static dtrace_helpers_t *dtrace_helpers_create(proc_t *); | |
696 | static void dtrace_buffer_drop(dtrace_buffer_t *); | |
697 | static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, | |
698 | dtrace_state_t *, dtrace_mstate_t *); | |
699 | static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, | |
700 | dtrace_optval_t); | |
701 | static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); | |
702 | static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); | |
703 | ||
704 | /* | |
705 | * DTrace Probe Context Functions | |
706 | * | |
707 | * These functions are called from probe context. Because probe context is | |
708 | * any context in which C may be called, arbitrarily locks may be held, | |
709 | * interrupts may be disabled, we may be in arbitrary dispatched state, etc. | |
710 | * As a result, functions called from probe context may only call other DTrace | |
711 | * support functions -- they may not interact at all with the system at large. | |
712 | * (Note that the ASSERT macro is made probe-context safe by redefining it in | |
713 | * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary | |
714 | * loads are to be performed from probe context, they _must_ be in terms of | |
715 | * the safe dtrace_load*() variants. | |
716 | * | |
717 | * Some functions in this block are not actually called from probe context; | |
718 | * for these functions, there will be a comment above the function reading | |
719 | * "Note: not called from probe context." | |
720 | */ | |
721 | void | |
722 | dtrace_panic(const char *format, ...) | |
723 | { | |
724 | va_list alist; | |
725 | ||
726 | va_start(alist, format); | |
727 | dtrace_vpanic(format, alist); | |
728 | va_end(alist); | |
729 | } | |
730 | ||
731 | int | |
732 | dtrace_assfail(const char *a, const char *f, int l) | |
733 | { | |
734 | dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); | |
735 | ||
736 | /* | |
737 | * We just need something here that even the most clever compiler | |
738 | * cannot optimize away. | |
739 | */ | |
740 | return (a[(uintptr_t)f]); | |
741 | } | |
742 | ||
743 | /* | |
744 | * Atomically increment a specified error counter from probe context. | |
745 | */ | |
746 | static void | |
747 | dtrace_error(uint32_t *counter) | |
748 | { | |
749 | /* | |
750 | * Most counters stored to in probe context are per-CPU counters. | |
751 | * However, there are some error conditions that are sufficiently | |
752 | * arcane that they don't merit per-CPU storage. If these counters | |
753 | * are incremented concurrently on different CPUs, scalability will be | |
754 | * adversely affected -- but we don't expect them to be white-hot in a | |
755 | * correctly constructed enabling... | |
756 | */ | |
757 | uint32_t oval, nval; | |
758 | ||
759 | do { | |
760 | oval = *counter; | |
761 | ||
762 | if ((nval = oval + 1) == 0) { | |
763 | /* | |
764 | * If the counter would wrap, set it to 1 -- assuring | |
765 | * that the counter is never zero when we have seen | |
766 | * errors. (The counter must be 32-bits because we | |
767 | * aren't guaranteed a 64-bit compare&swap operation.) | |
768 | * To save this code both the infamy of being fingered | |
769 | * by a priggish news story and the indignity of being | |
770 | * the target of a neo-puritan witch trial, we're | |
771 | * carefully avoiding any colorful description of the | |
772 | * likelihood of this condition -- but suffice it to | |
773 | * say that it is only slightly more likely than the | |
774 | * overflow of predicate cache IDs, as discussed in | |
775 | * dtrace_predicate_create(). | |
776 | */ | |
777 | nval = 1; | |
778 | } | |
779 | } while (dtrace_cas32(counter, oval, nval) != oval); | |
780 | } | |
781 | ||
782 | /* | |
783 | * Use the DTRACE_LOADFUNC macro to define functions for each of loading a | |
784 | * uint8_t, a uint16_t, a uint32_t and a uint64_t. | |
785 | */ | |
786 | DTRACE_LOADFUNC(8) | |
787 | DTRACE_LOADFUNC(16) | |
788 | DTRACE_LOADFUNC(32) | |
789 | DTRACE_LOADFUNC(64) | |
790 | ||
791 | static int | |
792 | dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) | |
793 | { | |
794 | if (dest < mstate->dtms_scratch_base) | |
795 | return (0); | |
796 | ||
797 | if (dest + size < dest) | |
798 | return (0); | |
799 | ||
800 | if (dest + size > mstate->dtms_scratch_ptr) | |
801 | return (0); | |
802 | ||
803 | return (1); | |
804 | } | |
805 | ||
806 | static int | |
807 | dtrace_canstore_statvar(uint64_t addr, size_t sz, | |
808 | dtrace_statvar_t **svars, int nsvars) | |
809 | { | |
810 | int i; | |
811 | ||
812 | for (i = 0; i < nsvars; i++) { | |
813 | dtrace_statvar_t *svar = svars[i]; | |
814 | ||
815 | if (svar == NULL || svar->dtsv_size == 0) | |
816 | continue; | |
817 | ||
b0d623f7 | 818 | if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) |
2d21ac55 A |
819 | return (1); |
820 | } | |
821 | ||
822 | return (0); | |
823 | } | |
824 | ||
825 | /* | |
826 | * Check to see if the address is within a memory region to which a store may | |
827 | * be issued. This includes the DTrace scratch areas, and any DTrace variable | |
828 | * region. The caller of dtrace_canstore() is responsible for performing any | |
829 | * alignment checks that are needed before stores are actually executed. | |
830 | */ | |
831 | static int | |
832 | dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, | |
833 | dtrace_vstate_t *vstate) | |
834 | { | |
2d21ac55 A |
835 | /* |
836 | * First, check to see if the address is in scratch space... | |
837 | */ | |
b0d623f7 A |
838 | if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, |
839 | mstate->dtms_scratch_size)) | |
2d21ac55 A |
840 | return (1); |
841 | ||
842 | /* | |
843 | * Now check to see if it's a dynamic variable. This check will pick | |
844 | * up both thread-local variables and any global dynamically-allocated | |
845 | * variables. | |
846 | */ | |
b0d623f7 A |
847 | if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, |
848 | vstate->dtvs_dynvars.dtds_size)) { | |
849 | dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; | |
850 | uintptr_t base = (uintptr_t)dstate->dtds_base + | |
851 | (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); | |
852 | uintptr_t chunkoffs; | |
853 | ||
854 | /* | |
855 | * Before we assume that we can store here, we need to make | |
856 | * sure that it isn't in our metadata -- storing to our | |
857 | * dynamic variable metadata would corrupt our state. For | |
858 | * the range to not include any dynamic variable metadata, | |
859 | * it must: | |
860 | * | |
861 | * (1) Start above the hash table that is at the base of | |
862 | * the dynamic variable space | |
863 | * | |
864 | * (2) Have a starting chunk offset that is beyond the | |
865 | * dtrace_dynvar_t that is at the base of every chunk | |
866 | * | |
867 | * (3) Not span a chunk boundary | |
868 | * | |
869 | */ | |
870 | if (addr < base) | |
871 | return (0); | |
872 | ||
873 | chunkoffs = (addr - base) % dstate->dtds_chunksize; | |
874 | ||
875 | if (chunkoffs < sizeof (dtrace_dynvar_t)) | |
876 | return (0); | |
877 | ||
878 | if (chunkoffs + sz > dstate->dtds_chunksize) | |
879 | return (0); | |
880 | ||
2d21ac55 | 881 | return (1); |
b0d623f7 | 882 | } |
2d21ac55 A |
883 | |
884 | /* | |
885 | * Finally, check the static local and global variables. These checks | |
886 | * take the longest, so we perform them last. | |
887 | */ | |
888 | if (dtrace_canstore_statvar(addr, sz, | |
889 | vstate->dtvs_locals, vstate->dtvs_nlocals)) | |
890 | return (1); | |
891 | ||
892 | if (dtrace_canstore_statvar(addr, sz, | |
893 | vstate->dtvs_globals, vstate->dtvs_nglobals)) | |
894 | return (1); | |
895 | ||
896 | return (0); | |
897 | } | |
898 | ||
b0d623f7 A |
899 | |
900 | /* | |
901 | * Convenience routine to check to see if the address is within a memory | |
902 | * region in which a load may be issued given the user's privilege level; | |
903 | * if not, it sets the appropriate error flags and loads 'addr' into the | |
904 | * illegal value slot. | |
905 | * | |
906 | * DTrace subroutines (DIF_SUBR_*) should use this helper to implement | |
907 | * appropriate memory access protection. | |
908 | */ | |
909 | static int | |
910 | dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, | |
911 | dtrace_vstate_t *vstate) | |
912 | { | |
913 | #if !defined(__APPLE__) /* Quiet compiler warning - matches dtrace_dif_emulate */ | |
914 | volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
915 | #else | |
916 | volatile uint64_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
917 | #endif /* __APPLE */ | |
918 | ||
919 | /* | |
920 | * If we hold the privilege to read from kernel memory, then | |
921 | * everything is readable. | |
922 | */ | |
923 | if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) | |
924 | return (1); | |
925 | ||
926 | /* | |
927 | * You can obviously read that which you can store. | |
928 | */ | |
929 | if (dtrace_canstore(addr, sz, mstate, vstate)) | |
930 | return (1); | |
931 | ||
932 | /* | |
933 | * We're allowed to read from our own string table. | |
934 | */ | |
935 | if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, | |
936 | mstate->dtms_difo->dtdo_strlen)) | |
937 | return (1); | |
938 | ||
939 | DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); | |
940 | *illval = addr; | |
941 | return (0); | |
942 | } | |
943 | ||
944 | /* | |
945 | * Convenience routine to check to see if a given string is within a memory | |
946 | * region in which a load may be issued given the user's privilege level; | |
947 | * this exists so that we don't need to issue unnecessary dtrace_strlen() | |
948 | * calls in the event that the user has all privileges. | |
949 | */ | |
950 | static int | |
951 | dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, | |
952 | dtrace_vstate_t *vstate) | |
953 | { | |
954 | size_t strsz; | |
955 | ||
956 | /* | |
957 | * If we hold the privilege to read from kernel memory, then | |
958 | * everything is readable. | |
959 | */ | |
960 | if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) | |
961 | return (1); | |
962 | ||
963 | strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); | |
964 | if (dtrace_canload(addr, strsz, mstate, vstate)) | |
965 | return (1); | |
966 | ||
967 | return (0); | |
968 | } | |
969 | ||
970 | /* | |
971 | * Convenience routine to check to see if a given variable is within a memory | |
972 | * region in which a load may be issued given the user's privilege level. | |
973 | */ | |
974 | static int | |
975 | dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, | |
976 | dtrace_vstate_t *vstate) | |
977 | { | |
978 | size_t sz; | |
979 | ASSERT(type->dtdt_flags & DIF_TF_BYREF); | |
980 | ||
981 | /* | |
982 | * If we hold the privilege to read from kernel memory, then | |
983 | * everything is readable. | |
984 | */ | |
985 | if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) | |
986 | return (1); | |
987 | ||
988 | if (type->dtdt_kind == DIF_TYPE_STRING) | |
989 | sz = dtrace_strlen(src, | |
990 | vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; | |
991 | else | |
992 | sz = type->dtdt_size; | |
993 | ||
994 | return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); | |
995 | } | |
996 | ||
2d21ac55 A |
997 | /* |
998 | * Compare two strings using safe loads. | |
999 | */ | |
1000 | static int | |
1001 | dtrace_strncmp(char *s1, char *s2, size_t limit) | |
1002 | { | |
1003 | uint8_t c1, c2; | |
1004 | volatile uint16_t *flags; | |
1005 | ||
1006 | if (s1 == s2 || limit == 0) | |
1007 | return (0); | |
1008 | ||
1009 | flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
1010 | ||
1011 | do { | |
b0d623f7 | 1012 | if (s1 == NULL) { |
2d21ac55 | 1013 | c1 = '\0'; |
b0d623f7 | 1014 | } else { |
2d21ac55 | 1015 | c1 = dtrace_load8((uintptr_t)s1++); |
b0d623f7 | 1016 | } |
2d21ac55 | 1017 | |
b0d623f7 | 1018 | if (s2 == NULL) { |
2d21ac55 | 1019 | c2 = '\0'; |
b0d623f7 | 1020 | } else { |
2d21ac55 | 1021 | c2 = dtrace_load8((uintptr_t)s2++); |
b0d623f7 | 1022 | } |
2d21ac55 A |
1023 | |
1024 | if (c1 != c2) | |
1025 | return (c1 - c2); | |
1026 | } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); | |
1027 | ||
1028 | return (0); | |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * Compute strlen(s) for a string using safe memory accesses. The additional | |
1033 | * len parameter is used to specify a maximum length to ensure completion. | |
1034 | */ | |
1035 | static size_t | |
1036 | dtrace_strlen(const char *s, size_t lim) | |
1037 | { | |
1038 | uint_t len; | |
1039 | ||
b0d623f7 | 1040 | for (len = 0; len != lim; len++) { |
2d21ac55 A |
1041 | if (dtrace_load8((uintptr_t)s++) == '\0') |
1042 | break; | |
b0d623f7 | 1043 | } |
2d21ac55 A |
1044 | |
1045 | return (len); | |
1046 | } | |
1047 | ||
1048 | /* | |
1049 | * Check if an address falls within a toxic region. | |
1050 | */ | |
1051 | static int | |
1052 | dtrace_istoxic(uintptr_t kaddr, size_t size) | |
1053 | { | |
1054 | uintptr_t taddr, tsize; | |
1055 | int i; | |
1056 | ||
1057 | for (i = 0; i < dtrace_toxranges; i++) { | |
1058 | taddr = dtrace_toxrange[i].dtt_base; | |
1059 | tsize = dtrace_toxrange[i].dtt_limit - taddr; | |
1060 | ||
1061 | if (kaddr - taddr < tsize) { | |
1062 | DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); | |
1063 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr; | |
1064 | return (1); | |
1065 | } | |
1066 | ||
1067 | if (taddr - kaddr < size) { | |
1068 | DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); | |
1069 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr; | |
1070 | return (1); | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | return (0); | |
1075 | } | |
1076 | ||
1077 | /* | |
1078 | * Copy src to dst using safe memory accesses. The src is assumed to be unsafe | |
1079 | * memory specified by the DIF program. The dst is assumed to be safe memory | |
1080 | * that we can store to directly because it is managed by DTrace. As with | |
1081 | * standard bcopy, overlapping copies are handled properly. | |
1082 | */ | |
1083 | static void | |
1084 | dtrace_bcopy(const void *src, void *dst, size_t len) | |
1085 | { | |
1086 | if (len != 0) { | |
1087 | uint8_t *s1 = dst; | |
1088 | const uint8_t *s2 = src; | |
1089 | ||
1090 | if (s1 <= s2) { | |
1091 | do { | |
1092 | *s1++ = dtrace_load8((uintptr_t)s2++); | |
1093 | } while (--len != 0); | |
1094 | } else { | |
1095 | s2 += len; | |
1096 | s1 += len; | |
1097 | ||
1098 | do { | |
1099 | *--s1 = dtrace_load8((uintptr_t)--s2); | |
1100 | } while (--len != 0); | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | /* | |
1106 | * Copy src to dst using safe memory accesses, up to either the specified | |
1107 | * length, or the point that a nul byte is encountered. The src is assumed to | |
1108 | * be unsafe memory specified by the DIF program. The dst is assumed to be | |
1109 | * safe memory that we can store to directly because it is managed by DTrace. | |
1110 | * Unlike dtrace_bcopy(), overlapping regions are not handled. | |
1111 | */ | |
1112 | static void | |
1113 | dtrace_strcpy(const void *src, void *dst, size_t len) | |
1114 | { | |
1115 | if (len != 0) { | |
1116 | uint8_t *s1 = dst, c; | |
1117 | const uint8_t *s2 = src; | |
1118 | ||
1119 | do { | |
1120 | *s1++ = c = dtrace_load8((uintptr_t)s2++); | |
1121 | } while (--len != 0 && c != '\0'); | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | /* | |
1126 | * Copy src to dst, deriving the size and type from the specified (BYREF) | |
1127 | * variable type. The src is assumed to be unsafe memory specified by the DIF | |
1128 | * program. The dst is assumed to be DTrace variable memory that is of the | |
1129 | * specified type; we assume that we can store to directly. | |
1130 | */ | |
1131 | static void | |
1132 | dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) | |
1133 | { | |
1134 | ASSERT(type->dtdt_flags & DIF_TF_BYREF); | |
1135 | ||
b0d623f7 | 1136 | if (type->dtdt_kind == DIF_TYPE_STRING) { |
2d21ac55 | 1137 | dtrace_strcpy(src, dst, type->dtdt_size); |
b0d623f7 | 1138 | } else { |
2d21ac55 A |
1139 | dtrace_bcopy(src, dst, type->dtdt_size); |
1140 | } | |
b0d623f7 | 1141 | } |
2d21ac55 A |
1142 | |
1143 | /* | |
1144 | * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be | |
1145 | * unsafe memory specified by the DIF program. The s2 data is assumed to be | |
1146 | * safe memory that we can access directly because it is managed by DTrace. | |
1147 | */ | |
1148 | static int | |
1149 | dtrace_bcmp(const void *s1, const void *s2, size_t len) | |
1150 | { | |
1151 | volatile uint16_t *flags; | |
1152 | ||
1153 | flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
1154 | ||
1155 | if (s1 == s2) | |
1156 | return (0); | |
1157 | ||
1158 | if (s1 == NULL || s2 == NULL) | |
1159 | return (1); | |
1160 | ||
1161 | if (s1 != s2 && len != 0) { | |
1162 | const uint8_t *ps1 = s1; | |
1163 | const uint8_t *ps2 = s2; | |
1164 | ||
1165 | do { | |
1166 | if (dtrace_load8((uintptr_t)ps1++) != *ps2++) | |
1167 | return (1); | |
1168 | } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); | |
1169 | } | |
1170 | return (0); | |
1171 | } | |
1172 | ||
1173 | /* | |
1174 | * Zero the specified region using a simple byte-by-byte loop. Note that this | |
1175 | * is for safe DTrace-managed memory only. | |
1176 | */ | |
1177 | static void | |
1178 | dtrace_bzero(void *dst, size_t len) | |
1179 | { | |
1180 | uchar_t *cp; | |
1181 | ||
1182 | for (cp = dst; len != 0; len--) | |
1183 | *cp++ = 0; | |
1184 | } | |
1185 | ||
b0d623f7 A |
1186 | static void |
1187 | dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) | |
1188 | { | |
1189 | uint64_t result[2]; | |
1190 | ||
1191 | result[0] = addend1[0] + addend2[0]; | |
1192 | result[1] = addend1[1] + addend2[1] + | |
1193 | (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); | |
1194 | ||
1195 | sum[0] = result[0]; | |
1196 | sum[1] = result[1]; | |
1197 | } | |
1198 | ||
1199 | /* | |
1200 | * Shift the 128-bit value in a by b. If b is positive, shift left. | |
1201 | * If b is negative, shift right. | |
1202 | */ | |
1203 | static void | |
1204 | dtrace_shift_128(uint64_t *a, int b) | |
1205 | { | |
1206 | uint64_t mask; | |
1207 | ||
1208 | if (b == 0) | |
1209 | return; | |
1210 | ||
1211 | if (b < 0) { | |
1212 | b = -b; | |
1213 | if (b >= 64) { | |
1214 | a[0] = a[1] >> (b - 64); | |
1215 | a[1] = 0; | |
1216 | } else { | |
1217 | a[0] >>= b; | |
1218 | mask = 1LL << (64 - b); | |
1219 | mask -= 1; | |
1220 | a[0] |= ((a[1] & mask) << (64 - b)); | |
1221 | a[1] >>= b; | |
1222 | } | |
1223 | } else { | |
1224 | if (b >= 64) { | |
1225 | a[1] = a[0] << (b - 64); | |
1226 | a[0] = 0; | |
1227 | } else { | |
1228 | a[1] <<= b; | |
1229 | mask = a[0] >> (64 - b); | |
1230 | a[1] |= mask; | |
1231 | a[0] <<= b; | |
1232 | } | |
1233 | } | |
1234 | } | |
1235 | ||
1236 | /* | |
1237 | * The basic idea is to break the 2 64-bit values into 4 32-bit values, | |
1238 | * use native multiplication on those, and then re-combine into the | |
1239 | * resulting 128-bit value. | |
1240 | * | |
1241 | * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = | |
1242 | * hi1 * hi2 << 64 + | |
1243 | * hi1 * lo2 << 32 + | |
1244 | * hi2 * lo1 << 32 + | |
1245 | * lo1 * lo2 | |
1246 | */ | |
1247 | static void | |
1248 | dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) | |
1249 | { | |
1250 | uint64_t hi1, hi2, lo1, lo2; | |
1251 | uint64_t tmp[2]; | |
1252 | ||
1253 | hi1 = factor1 >> 32; | |
1254 | hi2 = factor2 >> 32; | |
1255 | ||
1256 | lo1 = factor1 & DT_MASK_LO; | |
1257 | lo2 = factor2 & DT_MASK_LO; | |
1258 | ||
1259 | product[0] = lo1 * lo2; | |
1260 | product[1] = hi1 * hi2; | |
1261 | ||
1262 | tmp[0] = hi1 * lo2; | |
1263 | tmp[1] = 0; | |
1264 | dtrace_shift_128(tmp, 32); | |
1265 | dtrace_add_128(product, tmp, product); | |
1266 | ||
1267 | tmp[0] = hi2 * lo1; | |
1268 | tmp[1] = 0; | |
1269 | dtrace_shift_128(tmp, 32); | |
1270 | dtrace_add_128(product, tmp, product); | |
1271 | } | |
1272 | ||
2d21ac55 A |
1273 | /* |
1274 | * This privilege check should be used by actions and subroutines to | |
1275 | * verify that the user credentials of the process that enabled the | |
1276 | * invoking ECB match the target credentials | |
1277 | */ | |
1278 | static int | |
1279 | dtrace_priv_proc_common_user(dtrace_state_t *state) | |
1280 | { | |
1281 | cred_t *cr, *s_cr = state->dts_cred.dcr_cred; | |
1282 | ||
1283 | /* | |
1284 | * We should always have a non-NULL state cred here, since if cred | |
1285 | * is null (anonymous tracing), we fast-path bypass this routine. | |
1286 | */ | |
1287 | ASSERT(s_cr != NULL); | |
1288 | ||
1289 | #if !defined(__APPLE__) | |
1290 | if ((cr = CRED()) != NULL && | |
1291 | #else | |
1292 | if ((cr = dtrace_CRED()) != NULL && | |
1293 | #endif /* __APPLE__ */ | |
1294 | s_cr->cr_uid == cr->cr_uid && | |
1295 | s_cr->cr_uid == cr->cr_ruid && | |
1296 | s_cr->cr_uid == cr->cr_suid && | |
1297 | s_cr->cr_gid == cr->cr_gid && | |
1298 | s_cr->cr_gid == cr->cr_rgid && | |
1299 | s_cr->cr_gid == cr->cr_sgid) | |
1300 | return (1); | |
1301 | ||
1302 | return (0); | |
1303 | } | |
1304 | ||
1305 | /* | |
1306 | * This privilege check should be used by actions and subroutines to | |
1307 | * verify that the zone of the process that enabled the invoking ECB | |
1308 | * matches the target credentials | |
1309 | */ | |
1310 | static int | |
1311 | dtrace_priv_proc_common_zone(dtrace_state_t *state) | |
1312 | { | |
1313 | cred_t *cr, *s_cr = state->dts_cred.dcr_cred; | |
b0d623f7 | 1314 | #pragma unused(cr, s_cr) /* __APPLE__ */ |
2d21ac55 A |
1315 | |
1316 | /* | |
1317 | * We should always have a non-NULL state cred here, since if cred | |
1318 | * is null (anonymous tracing), we fast-path bypass this routine. | |
1319 | */ | |
1320 | ASSERT(s_cr != NULL); | |
1321 | ||
1322 | #if !defined(__APPLE__) | |
1323 | if ((cr = CRED()) != NULL && | |
1324 | s_cr->cr_zone == cr->cr_zone) | |
1325 | return (1); | |
1326 | ||
1327 | return (0); | |
1328 | #else | |
c910b4d9 A |
1329 | #pragma unused(state) |
1330 | ||
2d21ac55 A |
1331 | return 1; /* Darwin doesn't do zones. */ |
1332 | #endif /* __APPLE__ */ | |
1333 | } | |
1334 | ||
1335 | /* | |
1336 | * This privilege check should be used by actions and subroutines to | |
1337 | * verify that the process has not setuid or changed credentials. | |
1338 | */ | |
1339 | #if !defined(__APPLE__) | |
1340 | static int | |
1341 | dtrace_priv_proc_common_nocd() | |
1342 | { | |
1343 | proc_t *proc; | |
1344 | ||
1345 | if ((proc = ttoproc(curthread)) != NULL && | |
1346 | !(proc->p_flag & SNOCD)) | |
1347 | return (1); | |
1348 | ||
1349 | return (0); | |
1350 | } | |
1351 | #else | |
1352 | static int | |
1353 | dtrace_priv_proc_common_nocd(void) | |
1354 | { | |
1355 | return 1; /* Darwin omits "No Core Dump" flag. */ | |
1356 | } | |
1357 | #endif /* __APPLE__ */ | |
1358 | ||
1359 | static int | |
1360 | dtrace_priv_proc_destructive(dtrace_state_t *state) | |
1361 | { | |
1362 | int action = state->dts_cred.dcr_action; | |
1363 | ||
cf7d32b8 A |
1364 | #if defined(__APPLE__) |
1365 | if (ISSET(current_proc()->p_lflag, P_LNOATTACH)) | |
1366 | goto bad; | |
1367 | #endif /* __APPLE__ */ | |
1368 | ||
2d21ac55 A |
1369 | if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && |
1370 | dtrace_priv_proc_common_zone(state) == 0) | |
1371 | goto bad; | |
1372 | ||
1373 | if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && | |
1374 | dtrace_priv_proc_common_user(state) == 0) | |
1375 | goto bad; | |
1376 | ||
1377 | if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && | |
1378 | dtrace_priv_proc_common_nocd() == 0) | |
1379 | goto bad; | |
1380 | ||
1381 | return (1); | |
1382 | ||
1383 | bad: | |
1384 | cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; | |
1385 | ||
1386 | return (0); | |
1387 | } | |
1388 | ||
1389 | static int | |
1390 | dtrace_priv_proc_control(dtrace_state_t *state) | |
1391 | { | |
cf7d32b8 A |
1392 | #if defined(__APPLE__) |
1393 | if (ISSET(current_proc()->p_lflag, P_LNOATTACH)) | |
1394 | goto bad; | |
1395 | #endif /* __APPLE__ */ | |
1396 | ||
2d21ac55 A |
1397 | if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) |
1398 | return (1); | |
1399 | ||
1400 | if (dtrace_priv_proc_common_zone(state) && | |
1401 | dtrace_priv_proc_common_user(state) && | |
1402 | dtrace_priv_proc_common_nocd()) | |
1403 | return (1); | |
1404 | ||
cf7d32b8 A |
1405 | #if defined(__APPLE__) |
1406 | bad: | |
1407 | #endif /* __APPLE__ */ | |
2d21ac55 A |
1408 | cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; |
1409 | ||
1410 | return (0); | |
1411 | } | |
1412 | ||
1413 | static int | |
1414 | dtrace_priv_proc(dtrace_state_t *state) | |
1415 | { | |
cf7d32b8 A |
1416 | #if defined(__APPLE__) |
1417 | if (ISSET(current_proc()->p_lflag, P_LNOATTACH)) | |
1418 | goto bad; | |
1419 | #endif /* __APPLE__ */ | |
1420 | ||
2d21ac55 A |
1421 | if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) |
1422 | return (1); | |
1423 | ||
cf7d32b8 A |
1424 | #if defined(__APPLE__) |
1425 | bad: | |
1426 | #endif /* __APPLE__ */ | |
2d21ac55 A |
1427 | cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; |
1428 | ||
1429 | return (0); | |
1430 | } | |
1431 | ||
935ed37a A |
1432 | #if defined(__APPLE__) |
1433 | /* dtrace_priv_proc() omitting the P_LNOATTACH check. For PID and EXECNAME accesses. */ | |
1434 | static int | |
1435 | dtrace_priv_proc_relaxed(dtrace_state_t *state) | |
1436 | { | |
1437 | ||
1438 | if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) | |
1439 | return (1); | |
1440 | ||
1441 | cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; | |
1442 | ||
1443 | return (0); | |
1444 | } | |
1445 | #endif /* __APPLE__ */ | |
1446 | ||
2d21ac55 A |
1447 | static int |
1448 | dtrace_priv_kernel(dtrace_state_t *state) | |
1449 | { | |
1450 | if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) | |
1451 | return (1); | |
1452 | ||
1453 | cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; | |
1454 | ||
1455 | return (0); | |
1456 | } | |
1457 | ||
1458 | static int | |
1459 | dtrace_priv_kernel_destructive(dtrace_state_t *state) | |
1460 | { | |
1461 | if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) | |
1462 | return (1); | |
1463 | ||
1464 | cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; | |
1465 | ||
1466 | return (0); | |
1467 | } | |
1468 | ||
1469 | /* | |
1470 | * Note: not called from probe context. This function is called | |
1471 | * asynchronously (and at a regular interval) from outside of probe context to | |
1472 | * clean the dirty dynamic variable lists on all CPUs. Dynamic variable | |
1473 | * cleaning is explained in detail in <sys/dtrace_impl.h>. | |
1474 | */ | |
b0d623f7 | 1475 | #if defined(__APPLE__) /* Quiet compiler warning. */ |
2d21ac55 A |
1476 | static |
1477 | #endif /* __APPLE__ */ | |
1478 | void | |
1479 | dtrace_dynvar_clean(dtrace_dstate_t *dstate) | |
1480 | { | |
1481 | dtrace_dynvar_t *dirty; | |
1482 | dtrace_dstate_percpu_t *dcpu; | |
1483 | int i, work = 0; | |
1484 | ||
c910b4d9 | 1485 | for (i = 0; i < (int)NCPU; i++) { |
2d21ac55 A |
1486 | dcpu = &dstate->dtds_percpu[i]; |
1487 | ||
1488 | ASSERT(dcpu->dtdsc_rinsing == NULL); | |
1489 | ||
1490 | /* | |
1491 | * If the dirty list is NULL, there is no dirty work to do. | |
1492 | */ | |
1493 | if (dcpu->dtdsc_dirty == NULL) | |
1494 | continue; | |
1495 | ||
1496 | /* | |
1497 | * If the clean list is non-NULL, then we're not going to do | |
1498 | * any work for this CPU -- it means that there has not been | |
1499 | * a dtrace_dynvar() allocation on this CPU (or from this CPU) | |
1500 | * since the last time we cleaned house. | |
1501 | */ | |
1502 | if (dcpu->dtdsc_clean != NULL) | |
1503 | continue; | |
1504 | ||
1505 | work = 1; | |
1506 | ||
1507 | /* | |
1508 | * Atomically move the dirty list aside. | |
1509 | */ | |
1510 | do { | |
1511 | dirty = dcpu->dtdsc_dirty; | |
1512 | ||
1513 | /* | |
1514 | * Before we zap the dirty list, set the rinsing list. | |
1515 | * (This allows for a potential assertion in | |
1516 | * dtrace_dynvar(): if a free dynamic variable appears | |
1517 | * on a hash chain, either the dirty list or the | |
1518 | * rinsing list for some CPU must be non-NULL.) | |
1519 | */ | |
1520 | dcpu->dtdsc_rinsing = dirty; | |
1521 | dtrace_membar_producer(); | |
1522 | } while (dtrace_casptr(&dcpu->dtdsc_dirty, | |
1523 | dirty, NULL) != dirty); | |
1524 | } | |
1525 | ||
1526 | if (!work) { | |
1527 | /* | |
1528 | * We have no work to do; we can simply return. | |
1529 | */ | |
1530 | return; | |
1531 | } | |
1532 | ||
1533 | dtrace_sync(); | |
1534 | ||
c910b4d9 | 1535 | for (i = 0; i < (int)NCPU; i++) { |
2d21ac55 A |
1536 | dcpu = &dstate->dtds_percpu[i]; |
1537 | ||
1538 | if (dcpu->dtdsc_rinsing == NULL) | |
1539 | continue; | |
1540 | ||
1541 | /* | |
1542 | * We are now guaranteed that no hash chain contains a pointer | |
1543 | * into this dirty list; we can make it clean. | |
1544 | */ | |
1545 | ASSERT(dcpu->dtdsc_clean == NULL); | |
1546 | dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; | |
1547 | dcpu->dtdsc_rinsing = NULL; | |
1548 | } | |
1549 | ||
1550 | /* | |
1551 | * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make | |
1552 | * sure that all CPUs have seen all of the dtdsc_clean pointers. | |
1553 | * This prevents a race whereby a CPU incorrectly decides that | |
1554 | * the state should be something other than DTRACE_DSTATE_CLEAN | |
1555 | * after dtrace_dynvar_clean() has completed. | |
1556 | */ | |
1557 | dtrace_sync(); | |
1558 | ||
1559 | dstate->dtds_state = DTRACE_DSTATE_CLEAN; | |
1560 | } | |
1561 | ||
1562 | /* | |
1563 | * Depending on the value of the op parameter, this function looks-up, | |
1564 | * allocates or deallocates an arbitrarily-keyed dynamic variable. If an | |
1565 | * allocation is requested, this function will return a pointer to a | |
1566 | * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no | |
1567 | * variable can be allocated. If NULL is returned, the appropriate counter | |
1568 | * will be incremented. | |
1569 | */ | |
b0d623f7 | 1570 | #if defined(__APPLE__) /* Quiet compiler warning. */ |
2d21ac55 A |
1571 | static |
1572 | #endif /* __APPLE__ */ | |
1573 | dtrace_dynvar_t * | |
1574 | dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, | |
b0d623f7 A |
1575 | dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, |
1576 | dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) | |
2d21ac55 A |
1577 | { |
1578 | uint64_t hashval = DTRACE_DYNHASH_VALID; | |
1579 | dtrace_dynhash_t *hash = dstate->dtds_hash; | |
1580 | dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; | |
1581 | processorid_t me = CPU->cpu_id, cpu = me; | |
1582 | dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; | |
1583 | size_t bucket, ksize; | |
1584 | size_t chunksize = dstate->dtds_chunksize; | |
1585 | uintptr_t kdata, lock, nstate; | |
1586 | uint_t i; | |
1587 | ||
1588 | ASSERT(nkeys != 0); | |
1589 | ||
1590 | /* | |
1591 | * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" | |
1592 | * algorithm. For the by-value portions, we perform the algorithm in | |
1593 | * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a | |
1594 | * bit, and seems to have only a minute effect on distribution. For | |
1595 | * the by-reference data, we perform "One-at-a-time" iterating (safely) | |
1596 | * over each referenced byte. It's painful to do this, but it's much | |
1597 | * better than pathological hash distribution. The efficacy of the | |
1598 | * hashing algorithm (and a comparison with other algorithms) may be | |
1599 | * found by running the ::dtrace_dynstat MDB dcmd. | |
1600 | */ | |
1601 | for (i = 0; i < nkeys; i++) { | |
1602 | if (key[i].dttk_size == 0) { | |
1603 | uint64_t val = key[i].dttk_value; | |
1604 | ||
1605 | hashval += (val >> 48) & 0xffff; | |
1606 | hashval += (hashval << 10); | |
1607 | hashval ^= (hashval >> 6); | |
1608 | ||
1609 | hashval += (val >> 32) & 0xffff; | |
1610 | hashval += (hashval << 10); | |
1611 | hashval ^= (hashval >> 6); | |
1612 | ||
1613 | hashval += (val >> 16) & 0xffff; | |
1614 | hashval += (hashval << 10); | |
1615 | hashval ^= (hashval >> 6); | |
1616 | ||
1617 | hashval += val & 0xffff; | |
1618 | hashval += (hashval << 10); | |
1619 | hashval ^= (hashval >> 6); | |
1620 | } else { | |
1621 | /* | |
1622 | * This is incredibly painful, but it beats the hell | |
1623 | * out of the alternative. | |
1624 | */ | |
1625 | uint64_t j, size = key[i].dttk_size; | |
1626 | uintptr_t base = (uintptr_t)key[i].dttk_value; | |
1627 | ||
b0d623f7 A |
1628 | if (!dtrace_canload(base, size, mstate, vstate)) |
1629 | break; | |
1630 | ||
2d21ac55 A |
1631 | for (j = 0; j < size; j++) { |
1632 | hashval += dtrace_load8(base + j); | |
1633 | hashval += (hashval << 10); | |
1634 | hashval ^= (hashval >> 6); | |
1635 | } | |
1636 | } | |
1637 | } | |
1638 | ||
b0d623f7 A |
1639 | if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) |
1640 | return (NULL); | |
1641 | ||
2d21ac55 A |
1642 | hashval += (hashval << 3); |
1643 | hashval ^= (hashval >> 11); | |
1644 | hashval += (hashval << 15); | |
1645 | ||
1646 | /* | |
1647 | * There is a remote chance (ideally, 1 in 2^31) that our hashval | |
1648 | * comes out to be one of our two sentinel hash values. If this | |
1649 | * actually happens, we set the hashval to be a value known to be a | |
1650 | * non-sentinel value. | |
1651 | */ | |
1652 | if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) | |
1653 | hashval = DTRACE_DYNHASH_VALID; | |
1654 | ||
1655 | /* | |
1656 | * Yes, it's painful to do a divide here. If the cycle count becomes | |
1657 | * important here, tricks can be pulled to reduce it. (However, it's | |
1658 | * critical that hash collisions be kept to an absolute minimum; | |
1659 | * they're much more painful than a divide.) It's better to have a | |
1660 | * solution that generates few collisions and still keeps things | |
1661 | * relatively simple. | |
1662 | */ | |
1663 | bucket = hashval % dstate->dtds_hashsize; | |
1664 | ||
1665 | if (op == DTRACE_DYNVAR_DEALLOC) { | |
1666 | volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; | |
1667 | ||
1668 | for (;;) { | |
1669 | while ((lock = *lockp) & 1) | |
1670 | continue; | |
1671 | ||
b0d623f7 | 1672 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 A |
1673 | if (dtrace_casptr((void *)lockp, |
1674 | (void *)lock, (void *)(lock + 1)) == (void *)lock) | |
1675 | break; | |
b0d623f7 A |
1676 | #else |
1677 | if (dtrace_casptr((void *)(uintptr_t)lockp, | |
1678 | (void *)lock, (void *)(lock + 1)) == (void *)lock) | |
1679 | break; | |
1680 | #endif /* __APPLE__ */ | |
2d21ac55 A |
1681 | } |
1682 | ||
1683 | dtrace_membar_producer(); | |
1684 | } | |
1685 | ||
1686 | top: | |
1687 | prev = NULL; | |
1688 | lock = hash[bucket].dtdh_lock; | |
1689 | ||
1690 | dtrace_membar_consumer(); | |
1691 | ||
1692 | start = hash[bucket].dtdh_chain; | |
1693 | ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || | |
1694 | start->dtdv_hashval != DTRACE_DYNHASH_FREE || | |
1695 | op != DTRACE_DYNVAR_DEALLOC)); | |
1696 | ||
1697 | for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { | |
1698 | dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; | |
1699 | dtrace_key_t *dkey = &dtuple->dtt_key[0]; | |
1700 | ||
1701 | if (dvar->dtdv_hashval != hashval) { | |
1702 | if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { | |
1703 | /* | |
1704 | * We've reached the sink, and therefore the | |
1705 | * end of the hash chain; we can kick out of | |
1706 | * the loop knowing that we have seen a valid | |
1707 | * snapshot of state. | |
1708 | */ | |
1709 | ASSERT(dvar->dtdv_next == NULL); | |
1710 | ASSERT(dvar == &dtrace_dynhash_sink); | |
1711 | break; | |
1712 | } | |
1713 | ||
1714 | if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { | |
1715 | /* | |
1716 | * We've gone off the rails: somewhere along | |
1717 | * the line, one of the members of this hash | |
1718 | * chain was deleted. Note that we could also | |
1719 | * detect this by simply letting this loop run | |
1720 | * to completion, as we would eventually hit | |
1721 | * the end of the dirty list. However, we | |
1722 | * want to avoid running the length of the | |
1723 | * dirty list unnecessarily (it might be quite | |
1724 | * long), so we catch this as early as | |
1725 | * possible by detecting the hash marker. In | |
1726 | * this case, we simply set dvar to NULL and | |
1727 | * break; the conditional after the loop will | |
1728 | * send us back to top. | |
1729 | */ | |
1730 | dvar = NULL; | |
1731 | break; | |
1732 | } | |
1733 | ||
1734 | goto next; | |
1735 | } | |
1736 | ||
1737 | if (dtuple->dtt_nkeys != nkeys) | |
1738 | goto next; | |
1739 | ||
1740 | for (i = 0; i < nkeys; i++, dkey++) { | |
1741 | if (dkey->dttk_size != key[i].dttk_size) | |
1742 | goto next; /* size or type mismatch */ | |
1743 | ||
1744 | if (dkey->dttk_size != 0) { | |
1745 | if (dtrace_bcmp( | |
1746 | (void *)(uintptr_t)key[i].dttk_value, | |
1747 | (void *)(uintptr_t)dkey->dttk_value, | |
1748 | dkey->dttk_size)) | |
1749 | goto next; | |
1750 | } else { | |
1751 | if (dkey->dttk_value != key[i].dttk_value) | |
1752 | goto next; | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | if (op != DTRACE_DYNVAR_DEALLOC) | |
1757 | return (dvar); | |
1758 | ||
1759 | ASSERT(dvar->dtdv_next == NULL || | |
1760 | dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); | |
1761 | ||
1762 | if (prev != NULL) { | |
1763 | ASSERT(hash[bucket].dtdh_chain != dvar); | |
1764 | ASSERT(start != dvar); | |
1765 | ASSERT(prev->dtdv_next == dvar); | |
1766 | prev->dtdv_next = dvar->dtdv_next; | |
1767 | } else { | |
1768 | if (dtrace_casptr(&hash[bucket].dtdh_chain, | |
1769 | start, dvar->dtdv_next) != start) { | |
1770 | /* | |
1771 | * We have failed to atomically swing the | |
1772 | * hash table head pointer, presumably because | |
1773 | * of a conflicting allocation on another CPU. | |
1774 | * We need to reread the hash chain and try | |
1775 | * again. | |
1776 | */ | |
1777 | goto top; | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | dtrace_membar_producer(); | |
1782 | ||
1783 | /* | |
1784 | * Now set the hash value to indicate that it's free. | |
1785 | */ | |
1786 | ASSERT(hash[bucket].dtdh_chain != dvar); | |
1787 | dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; | |
1788 | ||
1789 | dtrace_membar_producer(); | |
1790 | ||
1791 | /* | |
1792 | * Set the next pointer to point at the dirty list, and | |
1793 | * atomically swing the dirty pointer to the newly freed dvar. | |
1794 | */ | |
1795 | do { | |
1796 | next = dcpu->dtdsc_dirty; | |
1797 | dvar->dtdv_next = next; | |
1798 | } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); | |
1799 | ||
1800 | /* | |
1801 | * Finally, unlock this hash bucket. | |
1802 | */ | |
1803 | ASSERT(hash[bucket].dtdh_lock == lock); | |
1804 | ASSERT(lock & 1); | |
1805 | hash[bucket].dtdh_lock++; | |
1806 | ||
1807 | return (NULL); | |
1808 | next: | |
1809 | prev = dvar; | |
1810 | continue; | |
1811 | } | |
1812 | ||
1813 | if (dvar == NULL) { | |
1814 | /* | |
1815 | * If dvar is NULL, it is because we went off the rails: | |
1816 | * one of the elements that we traversed in the hash chain | |
1817 | * was deleted while we were traversing it. In this case, | |
1818 | * we assert that we aren't doing a dealloc (deallocs lock | |
1819 | * the hash bucket to prevent themselves from racing with | |
1820 | * one another), and retry the hash chain traversal. | |
1821 | */ | |
1822 | ASSERT(op != DTRACE_DYNVAR_DEALLOC); | |
1823 | goto top; | |
1824 | } | |
1825 | ||
1826 | if (op != DTRACE_DYNVAR_ALLOC) { | |
1827 | /* | |
1828 | * If we are not to allocate a new variable, we want to | |
1829 | * return NULL now. Before we return, check that the value | |
1830 | * of the lock word hasn't changed. If it has, we may have | |
1831 | * seen an inconsistent snapshot. | |
1832 | */ | |
1833 | if (op == DTRACE_DYNVAR_NOALLOC) { | |
1834 | if (hash[bucket].dtdh_lock != lock) | |
1835 | goto top; | |
1836 | } else { | |
1837 | ASSERT(op == DTRACE_DYNVAR_DEALLOC); | |
1838 | ASSERT(hash[bucket].dtdh_lock == lock); | |
1839 | ASSERT(lock & 1); | |
1840 | hash[bucket].dtdh_lock++; | |
1841 | } | |
1842 | ||
1843 | return (NULL); | |
1844 | } | |
1845 | ||
1846 | /* | |
1847 | * We need to allocate a new dynamic variable. The size we need is the | |
1848 | * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the | |
1849 | * size of any auxiliary key data (rounded up to 8-byte alignment) plus | |
1850 | * the size of any referred-to data (dsize). We then round the final | |
1851 | * size up to the chunksize for allocation. | |
1852 | */ | |
1853 | for (ksize = 0, i = 0; i < nkeys; i++) | |
1854 | ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); | |
1855 | ||
1856 | /* | |
1857 | * This should be pretty much impossible, but could happen if, say, | |
1858 | * strange DIF specified the tuple. Ideally, this should be an | |
1859 | * assertion and not an error condition -- but that requires that the | |
1860 | * chunksize calculation in dtrace_difo_chunksize() be absolutely | |
1861 | * bullet-proof. (That is, it must not be able to be fooled by | |
1862 | * malicious DIF.) Given the lack of backwards branches in DIF, | |
1863 | * solving this would presumably not amount to solving the Halting | |
1864 | * Problem -- but it still seems awfully hard. | |
1865 | */ | |
1866 | if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + | |
1867 | ksize + dsize > chunksize) { | |
1868 | dcpu->dtdsc_drops++; | |
1869 | return (NULL); | |
1870 | } | |
1871 | ||
1872 | nstate = DTRACE_DSTATE_EMPTY; | |
1873 | ||
1874 | do { | |
1875 | retry: | |
1876 | free = dcpu->dtdsc_free; | |
1877 | ||
1878 | if (free == NULL) { | |
1879 | dtrace_dynvar_t *clean = dcpu->dtdsc_clean; | |
1880 | void *rval; | |
1881 | ||
1882 | if (clean == NULL) { | |
1883 | /* | |
1884 | * We're out of dynamic variable space on | |
1885 | * this CPU. Unless we have tried all CPUs, | |
1886 | * we'll try to allocate from a different | |
1887 | * CPU. | |
1888 | */ | |
1889 | switch (dstate->dtds_state) { | |
1890 | case DTRACE_DSTATE_CLEAN: { | |
1891 | void *sp = &dstate->dtds_state; | |
1892 | ||
c910b4d9 | 1893 | if (++cpu >= (int)NCPU) |
2d21ac55 A |
1894 | cpu = 0; |
1895 | ||
1896 | if (dcpu->dtdsc_dirty != NULL && | |
1897 | nstate == DTRACE_DSTATE_EMPTY) | |
1898 | nstate = DTRACE_DSTATE_DIRTY; | |
1899 | ||
1900 | if (dcpu->dtdsc_rinsing != NULL) | |
1901 | nstate = DTRACE_DSTATE_RINSING; | |
1902 | ||
1903 | dcpu = &dstate->dtds_percpu[cpu]; | |
1904 | ||
1905 | if (cpu != me) | |
1906 | goto retry; | |
1907 | ||
1908 | (void) dtrace_cas32(sp, | |
1909 | DTRACE_DSTATE_CLEAN, nstate); | |
1910 | ||
1911 | /* | |
1912 | * To increment the correct bean | |
1913 | * counter, take another lap. | |
1914 | */ | |
1915 | goto retry; | |
1916 | } | |
1917 | ||
1918 | case DTRACE_DSTATE_DIRTY: | |
1919 | dcpu->dtdsc_dirty_drops++; | |
1920 | break; | |
1921 | ||
1922 | case DTRACE_DSTATE_RINSING: | |
1923 | dcpu->dtdsc_rinsing_drops++; | |
1924 | break; | |
1925 | ||
1926 | case DTRACE_DSTATE_EMPTY: | |
1927 | dcpu->dtdsc_drops++; | |
1928 | break; | |
1929 | } | |
1930 | ||
1931 | DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); | |
1932 | return (NULL); | |
1933 | } | |
1934 | ||
1935 | /* | |
1936 | * The clean list appears to be non-empty. We want to | |
1937 | * move the clean list to the free list; we start by | |
1938 | * moving the clean pointer aside. | |
1939 | */ | |
1940 | if (dtrace_casptr(&dcpu->dtdsc_clean, | |
1941 | clean, NULL) != clean) { | |
1942 | /* | |
1943 | * We are in one of two situations: | |
1944 | * | |
1945 | * (a) The clean list was switched to the | |
1946 | * free list by another CPU. | |
1947 | * | |
1948 | * (b) The clean list was added to by the | |
1949 | * cleansing cyclic. | |
1950 | * | |
1951 | * In either of these situations, we can | |
1952 | * just reattempt the free list allocation. | |
1953 | */ | |
1954 | goto retry; | |
1955 | } | |
1956 | ||
1957 | ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); | |
1958 | ||
1959 | /* | |
1960 | * Now we'll move the clean list to the free list. | |
1961 | * It's impossible for this to fail: the only way | |
1962 | * the free list can be updated is through this | |
1963 | * code path, and only one CPU can own the clean list. | |
1964 | * Thus, it would only be possible for this to fail if | |
1965 | * this code were racing with dtrace_dynvar_clean(). | |
1966 | * (That is, if dtrace_dynvar_clean() updated the clean | |
1967 | * list, and we ended up racing to update the free | |
1968 | * list.) This race is prevented by the dtrace_sync() | |
1969 | * in dtrace_dynvar_clean() -- which flushes the | |
1970 | * owners of the clean lists out before resetting | |
1971 | * the clean lists. | |
1972 | */ | |
1973 | rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); | |
1974 | ASSERT(rval == NULL); | |
1975 | goto retry; | |
1976 | } | |
1977 | ||
1978 | dvar = free; | |
1979 | new_free = dvar->dtdv_next; | |
1980 | } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); | |
1981 | ||
1982 | /* | |
1983 | * We have now allocated a new chunk. We copy the tuple keys into the | |
1984 | * tuple array and copy any referenced key data into the data space | |
1985 | * following the tuple array. As we do this, we relocate dttk_value | |
1986 | * in the final tuple to point to the key data address in the chunk. | |
1987 | */ | |
1988 | kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; | |
1989 | dvar->dtdv_data = (void *)(kdata + ksize); | |
1990 | dvar->dtdv_tuple.dtt_nkeys = nkeys; | |
1991 | ||
1992 | for (i = 0; i < nkeys; i++) { | |
1993 | dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; | |
1994 | size_t kesize = key[i].dttk_size; | |
1995 | ||
1996 | if (kesize != 0) { | |
1997 | dtrace_bcopy( | |
1998 | (const void *)(uintptr_t)key[i].dttk_value, | |
1999 | (void *)kdata, kesize); | |
2000 | dkey->dttk_value = kdata; | |
2001 | kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); | |
2002 | } else { | |
2003 | dkey->dttk_value = key[i].dttk_value; | |
2004 | } | |
2005 | ||
2006 | dkey->dttk_size = kesize; | |
2007 | } | |
2008 | ||
2009 | ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); | |
2010 | dvar->dtdv_hashval = hashval; | |
2011 | dvar->dtdv_next = start; | |
2012 | ||
2013 | if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) | |
2014 | return (dvar); | |
2015 | ||
2016 | /* | |
2017 | * The cas has failed. Either another CPU is adding an element to | |
2018 | * this hash chain, or another CPU is deleting an element from this | |
2019 | * hash chain. The simplest way to deal with both of these cases | |
2020 | * (though not necessarily the most efficient) is to free our | |
2021 | * allocated block and tail-call ourselves. Note that the free is | |
2022 | * to the dirty list and _not_ to the free list. This is to prevent | |
2023 | * races with allocators, above. | |
2024 | */ | |
2025 | dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; | |
2026 | ||
2027 | dtrace_membar_producer(); | |
2028 | ||
2029 | do { | |
2030 | free = dcpu->dtdsc_dirty; | |
2031 | dvar->dtdv_next = free; | |
2032 | } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); | |
2033 | ||
b0d623f7 | 2034 | return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); |
2d21ac55 A |
2035 | } |
2036 | ||
2037 | /*ARGSUSED*/ | |
2038 | static void | |
2039 | dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) | |
2040 | { | |
b0d623f7 A |
2041 | #pragma unused(arg) /* __APPLE__ */ |
2042 | if ((int64_t)nval < (int64_t)*oval) | |
2d21ac55 A |
2043 | *oval = nval; |
2044 | } | |
2045 | ||
2046 | /*ARGSUSED*/ | |
2047 | static void | |
2048 | dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) | |
2049 | { | |
b0d623f7 A |
2050 | #pragma unused(arg) /* __APPLE__ */ |
2051 | if ((int64_t)nval > (int64_t)*oval) | |
2d21ac55 A |
2052 | *oval = nval; |
2053 | } | |
2054 | ||
2055 | static void | |
2056 | dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) | |
2057 | { | |
2058 | int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; | |
2059 | int64_t val = (int64_t)nval; | |
2060 | ||
2061 | if (val < 0) { | |
2062 | for (i = 0; i < zero; i++) { | |
2063 | if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { | |
2064 | quanta[i] += incr; | |
2065 | return; | |
2066 | } | |
2067 | } | |
2068 | } else { | |
2069 | for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { | |
2070 | if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { | |
2071 | quanta[i - 1] += incr; | |
2072 | return; | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; | |
2077 | return; | |
2078 | } | |
2079 | ||
2080 | ASSERT(0); | |
2081 | } | |
2082 | ||
2083 | static void | |
2084 | dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) | |
2085 | { | |
2086 | uint64_t arg = *lquanta++; | |
2087 | int32_t base = DTRACE_LQUANTIZE_BASE(arg); | |
2088 | uint16_t step = DTRACE_LQUANTIZE_STEP(arg); | |
2089 | uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); | |
2090 | int32_t val = (int32_t)nval, level; | |
2091 | ||
2092 | ASSERT(step != 0); | |
2093 | ASSERT(levels != 0); | |
2094 | ||
2095 | if (val < base) { | |
2096 | /* | |
2097 | * This is an underflow. | |
2098 | */ | |
2099 | lquanta[0] += incr; | |
2100 | return; | |
2101 | } | |
2102 | ||
2103 | level = (val - base) / step; | |
2104 | ||
2105 | if (level < levels) { | |
2106 | lquanta[level + 1] += incr; | |
2107 | return; | |
2108 | } | |
2109 | ||
2110 | /* | |
2111 | * This is an overflow. | |
2112 | */ | |
2113 | lquanta[levels + 1] += incr; | |
2114 | } | |
2115 | ||
2116 | /*ARGSUSED*/ | |
2117 | static void | |
2118 | dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) | |
2119 | { | |
b0d623f7 | 2120 | #pragma unused(arg) /* __APPLE__ */ |
2d21ac55 A |
2121 | data[0]++; |
2122 | data[1] += nval; | |
2123 | } | |
2124 | ||
2125 | /*ARGSUSED*/ | |
2126 | static void | |
b0d623f7 | 2127 | dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) |
2d21ac55 | 2128 | { |
b0d623f7 A |
2129 | #pragma unused(arg) /* __APPLE__ */ |
2130 | int64_t snval = (int64_t)nval; | |
2131 | uint64_t tmp[2]; | |
2132 | ||
2133 | data[0]++; | |
2134 | data[1] += nval; | |
2135 | ||
2136 | /* | |
2137 | * What we want to say here is: | |
2138 | * | |
2139 | * data[2] += nval * nval; | |
2140 | * | |
2141 | * But given that nval is 64-bit, we could easily overflow, so | |
2142 | * we do this as 128-bit arithmetic. | |
2143 | */ | |
2144 | if (snval < 0) | |
2145 | snval = -snval; | |
2146 | ||
2147 | dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); | |
2148 | dtrace_add_128(data + 2, tmp, data + 2); | |
2d21ac55 A |
2149 | } |
2150 | ||
2151 | /*ARGSUSED*/ | |
2152 | static void | |
b0d623f7 | 2153 | dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) |
2d21ac55 | 2154 | { |
b0d623f7 A |
2155 | #pragma unused(nval, arg) /* __APPLE__ */ |
2156 | *oval = *oval + 1; | |
2157 | } | |
2158 | ||
2159 | /*ARGSUSED*/ | |
2160 | static void | |
2161 | dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) | |
2162 | { | |
2163 | #pragma unused(arg) /* __APPLE__ */ | |
2d21ac55 A |
2164 | *oval += nval; |
2165 | } | |
2166 | ||
2167 | /* | |
2168 | * Aggregate given the tuple in the principal data buffer, and the aggregating | |
2169 | * action denoted by the specified dtrace_aggregation_t. The aggregation | |
2170 | * buffer is specified as the buf parameter. This routine does not return | |
2171 | * failure; if there is no space in the aggregation buffer, the data will be | |
2172 | * dropped, and a corresponding counter incremented. | |
2173 | */ | |
2174 | static void | |
2175 | dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, | |
2176 | intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) | |
2177 | { | |
c910b4d9 | 2178 | #pragma unused(arg) |
2d21ac55 A |
2179 | dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; |
2180 | uint32_t i, ndx, size, fsize; | |
2181 | uint32_t align = sizeof (uint64_t) - 1; | |
2182 | dtrace_aggbuffer_t *agb; | |
2183 | dtrace_aggkey_t *key; | |
2184 | uint32_t hashval = 0, limit, isstr; | |
2185 | caddr_t tomax, data, kdata; | |
2186 | dtrace_actkind_t action; | |
2187 | dtrace_action_t *act; | |
2188 | uintptr_t offs; | |
2189 | ||
2190 | if (buf == NULL) | |
2191 | return; | |
2192 | ||
2193 | if (!agg->dtag_hasarg) { | |
2194 | /* | |
2195 | * Currently, only quantize() and lquantize() take additional | |
2196 | * arguments, and they have the same semantics: an increment | |
2197 | * value that defaults to 1 when not present. If additional | |
2198 | * aggregating actions take arguments, the setting of the | |
2199 | * default argument value will presumably have to become more | |
2200 | * sophisticated... | |
2201 | */ | |
2202 | arg = 1; | |
2203 | } | |
2204 | ||
2205 | action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; | |
2206 | size = rec->dtrd_offset - agg->dtag_base; | |
2207 | fsize = size + rec->dtrd_size; | |
2208 | ||
2209 | ASSERT(dbuf->dtb_tomax != NULL); | |
2210 | data = dbuf->dtb_tomax + offset + agg->dtag_base; | |
2211 | ||
2212 | if ((tomax = buf->dtb_tomax) == NULL) { | |
2213 | dtrace_buffer_drop(buf); | |
2214 | return; | |
2215 | } | |
2216 | ||
2217 | /* | |
2218 | * The metastructure is always at the bottom of the buffer. | |
2219 | */ | |
2220 | agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - | |
2221 | sizeof (dtrace_aggbuffer_t)); | |
2222 | ||
2223 | if (buf->dtb_offset == 0) { | |
2224 | /* | |
2225 | * We just kludge up approximately 1/8th of the size to be | |
2226 | * buckets. If this guess ends up being routinely | |
2227 | * off-the-mark, we may need to dynamically readjust this | |
2228 | * based on past performance. | |
2229 | */ | |
2230 | uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); | |
2231 | ||
2232 | if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < | |
2233 | (uintptr_t)tomax || hashsize == 0) { | |
2234 | /* | |
2235 | * We've been given a ludicrously small buffer; | |
2236 | * increment our drop count and leave. | |
2237 | */ | |
2238 | dtrace_buffer_drop(buf); | |
2239 | return; | |
2240 | } | |
2241 | ||
2242 | /* | |
2243 | * And now, a pathetic attempt to try to get a an odd (or | |
2244 | * perchance, a prime) hash size for better hash distribution. | |
2245 | */ | |
2246 | if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) | |
2247 | hashsize -= DTRACE_AGGHASHSIZE_SLEW; | |
2248 | ||
2249 | agb->dtagb_hashsize = hashsize; | |
2250 | agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - | |
2251 | agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); | |
2252 | agb->dtagb_free = (uintptr_t)agb->dtagb_hash; | |
2253 | ||
2254 | for (i = 0; i < agb->dtagb_hashsize; i++) | |
2255 | agb->dtagb_hash[i] = NULL; | |
2256 | } | |
2257 | ||
2258 | ASSERT(agg->dtag_first != NULL); | |
2259 | ASSERT(agg->dtag_first->dta_intuple); | |
2260 | ||
2261 | /* | |
2262 | * Calculate the hash value based on the key. Note that we _don't_ | |
2263 | * include the aggid in the hashing (but we will store it as part of | |
2264 | * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" | |
2265 | * algorithm: a simple, quick algorithm that has no known funnels, and | |
2266 | * gets good distribution in practice. The efficacy of the hashing | |
2267 | * algorithm (and a comparison with other algorithms) may be found by | |
2268 | * running the ::dtrace_aggstat MDB dcmd. | |
2269 | */ | |
2270 | for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { | |
2271 | i = act->dta_rec.dtrd_offset - agg->dtag_base; | |
2272 | limit = i + act->dta_rec.dtrd_size; | |
2273 | ASSERT(limit <= size); | |
2274 | isstr = DTRACEACT_ISSTRING(act); | |
2275 | ||
2276 | for (; i < limit; i++) { | |
2277 | hashval += data[i]; | |
2278 | hashval += (hashval << 10); | |
2279 | hashval ^= (hashval >> 6); | |
2280 | ||
2281 | if (isstr && data[i] == '\0') | |
2282 | break; | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | hashval += (hashval << 3); | |
2287 | hashval ^= (hashval >> 11); | |
2288 | hashval += (hashval << 15); | |
2289 | ||
2290 | /* | |
2291 | * Yes, the divide here is expensive -- but it's generally the least | |
2292 | * of the performance issues given the amount of data that we iterate | |
2293 | * over to compute hash values, compare data, etc. | |
2294 | */ | |
2295 | ndx = hashval % agb->dtagb_hashsize; | |
2296 | ||
2297 | for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { | |
2298 | ASSERT((caddr_t)key >= tomax); | |
2299 | ASSERT((caddr_t)key < tomax + buf->dtb_size); | |
2300 | ||
2301 | if (hashval != key->dtak_hashval || key->dtak_size != size) | |
2302 | continue; | |
2303 | ||
2304 | kdata = key->dtak_data; | |
2305 | ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); | |
2306 | ||
2307 | for (act = agg->dtag_first; act->dta_intuple; | |
2308 | act = act->dta_next) { | |
2309 | i = act->dta_rec.dtrd_offset - agg->dtag_base; | |
2310 | limit = i + act->dta_rec.dtrd_size; | |
2311 | ASSERT(limit <= size); | |
2312 | isstr = DTRACEACT_ISSTRING(act); | |
2313 | ||
2314 | for (; i < limit; i++) { | |
2315 | if (kdata[i] != data[i]) | |
2316 | goto next; | |
2317 | ||
2318 | if (isstr && data[i] == '\0') | |
2319 | break; | |
2320 | } | |
2321 | } | |
2322 | ||
2323 | if (action != key->dtak_action) { | |
2324 | /* | |
2325 | * We are aggregating on the same value in the same | |
2326 | * aggregation with two different aggregating actions. | |
2327 | * (This should have been picked up in the compiler, | |
2328 | * so we may be dealing with errant or devious DIF.) | |
2329 | * This is an error condition; we indicate as much, | |
2330 | * and return. | |
2331 | */ | |
2332 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
2333 | return; | |
2334 | } | |
2335 | ||
2336 | /* | |
2337 | * This is a hit: we need to apply the aggregator to | |
2338 | * the value at this key. | |
2339 | */ | |
2340 | agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); | |
2341 | return; | |
2342 | next: | |
2343 | continue; | |
2344 | } | |
2345 | ||
2346 | /* | |
2347 | * We didn't find it. We need to allocate some zero-filled space, | |
2348 | * link it into the hash table appropriately, and apply the aggregator | |
2349 | * to the (zero-filled) value. | |
2350 | */ | |
2351 | offs = buf->dtb_offset; | |
2352 | while (offs & (align - 1)) | |
2353 | offs += sizeof (uint32_t); | |
2354 | ||
2355 | /* | |
2356 | * If we don't have enough room to both allocate a new key _and_ | |
2357 | * its associated data, increment the drop count and return. | |
2358 | */ | |
2359 | if ((uintptr_t)tomax + offs + fsize > | |
2360 | agb->dtagb_free - sizeof (dtrace_aggkey_t)) { | |
2361 | dtrace_buffer_drop(buf); | |
2362 | return; | |
2363 | } | |
2364 | ||
2365 | /*CONSTCOND*/ | |
2366 | ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); | |
2367 | key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); | |
2368 | agb->dtagb_free -= sizeof (dtrace_aggkey_t); | |
2369 | ||
2370 | key->dtak_data = kdata = tomax + offs; | |
2371 | buf->dtb_offset = offs + fsize; | |
2372 | ||
2373 | /* | |
2374 | * Now copy the data across. | |
2375 | */ | |
2376 | *((dtrace_aggid_t *)kdata) = agg->dtag_id; | |
2377 | ||
2378 | for (i = sizeof (dtrace_aggid_t); i < size; i++) | |
2379 | kdata[i] = data[i]; | |
2380 | ||
2381 | /* | |
2382 | * Because strings are not zeroed out by default, we need to iterate | |
2383 | * looking for actions that store strings, and we need to explicitly | |
2384 | * pad these strings out with zeroes. | |
2385 | */ | |
2386 | for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { | |
2387 | int nul; | |
2388 | ||
2389 | if (!DTRACEACT_ISSTRING(act)) | |
2390 | continue; | |
2391 | ||
2392 | i = act->dta_rec.dtrd_offset - agg->dtag_base; | |
2393 | limit = i + act->dta_rec.dtrd_size; | |
2394 | ASSERT(limit <= size); | |
2395 | ||
2396 | for (nul = 0; i < limit; i++) { | |
2397 | if (nul) { | |
2398 | kdata[i] = '\0'; | |
2399 | continue; | |
2400 | } | |
2401 | ||
2402 | if (data[i] != '\0') | |
2403 | continue; | |
2404 | ||
2405 | nul = 1; | |
2406 | } | |
2407 | } | |
2408 | ||
2409 | for (i = size; i < fsize; i++) | |
2410 | kdata[i] = 0; | |
2411 | ||
2412 | key->dtak_hashval = hashval; | |
2413 | key->dtak_size = size; | |
2414 | key->dtak_action = action; | |
2415 | key->dtak_next = agb->dtagb_hash[ndx]; | |
2416 | agb->dtagb_hash[ndx] = key; | |
2417 | ||
2418 | /* | |
2419 | * Finally, apply the aggregator. | |
2420 | */ | |
2421 | *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; | |
2422 | agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); | |
2423 | } | |
2424 | ||
2425 | /* | |
2426 | * Given consumer state, this routine finds a speculation in the INACTIVE | |
2427 | * state and transitions it into the ACTIVE state. If there is no speculation | |
2428 | * in the INACTIVE state, 0 is returned. In this case, no error counter is | |
2429 | * incremented -- it is up to the caller to take appropriate action. | |
2430 | */ | |
2431 | static int | |
2432 | dtrace_speculation(dtrace_state_t *state) | |
2433 | { | |
2434 | int i = 0; | |
2435 | dtrace_speculation_state_t current; | |
2436 | uint32_t *stat = &state->dts_speculations_unavail, count; | |
2437 | ||
2438 | while (i < state->dts_nspeculations) { | |
2439 | dtrace_speculation_t *spec = &state->dts_speculations[i]; | |
2440 | ||
2441 | current = spec->dtsp_state; | |
2442 | ||
2443 | if (current != DTRACESPEC_INACTIVE) { | |
2444 | if (current == DTRACESPEC_COMMITTINGMANY || | |
2445 | current == DTRACESPEC_COMMITTING || | |
2446 | current == DTRACESPEC_DISCARDING) | |
2447 | stat = &state->dts_speculations_busy; | |
2448 | i++; | |
2449 | continue; | |
2450 | } | |
2451 | ||
2452 | if (dtrace_cas32((uint32_t *)&spec->dtsp_state, | |
2453 | current, DTRACESPEC_ACTIVE) == current) | |
2454 | return (i + 1); | |
2455 | } | |
2456 | ||
2457 | /* | |
2458 | * We couldn't find a speculation. If we found as much as a single | |
2459 | * busy speculation buffer, we'll attribute this failure as "busy" | |
2460 | * instead of "unavail". | |
2461 | */ | |
2462 | do { | |
2463 | count = *stat; | |
2464 | } while (dtrace_cas32(stat, count, count + 1) != count); | |
2465 | ||
2466 | return (0); | |
2467 | } | |
2468 | ||
2469 | /* | |
2470 | * This routine commits an active speculation. If the specified speculation | |
2471 | * is not in a valid state to perform a commit(), this routine will silently do | |
2472 | * nothing. The state of the specified speculation is transitioned according | |
2473 | * to the state transition diagram outlined in <sys/dtrace_impl.h> | |
2474 | */ | |
2475 | static void | |
2476 | dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, | |
2477 | dtrace_specid_t which) | |
2478 | { | |
2479 | dtrace_speculation_t *spec; | |
2480 | dtrace_buffer_t *src, *dest; | |
2481 | uintptr_t daddr, saddr, dlimit; | |
b0d623f7 | 2482 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2483 | dtrace_speculation_state_t current, new; |
b0d623f7 A |
2484 | #else |
2485 | dtrace_speculation_state_t current, new = DTRACESPEC_INACTIVE; | |
2486 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2487 | intptr_t offs; |
2488 | ||
2489 | if (which == 0) | |
2490 | return; | |
2491 | ||
b0d623f7 | 2492 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 A |
2493 | if (which > state->dts_nspeculations) { |
2494 | cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; | |
2495 | return; | |
2496 | } | |
b0d623f7 A |
2497 | #else |
2498 | if (which > (dtrace_specid_t)state->dts_nspeculations) { | |
2499 | cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; | |
2500 | return; | |
2501 | } | |
2502 | #endif /* __APPLE__ */ | |
2503 | ||
2d21ac55 A |
2504 | spec = &state->dts_speculations[which - 1]; |
2505 | src = &spec->dtsp_buffer[cpu]; | |
2506 | dest = &state->dts_buffer[cpu]; | |
2507 | ||
2508 | do { | |
2509 | current = spec->dtsp_state; | |
2510 | ||
2511 | if (current == DTRACESPEC_COMMITTINGMANY) | |
2512 | break; | |
2513 | ||
2514 | switch (current) { | |
2515 | case DTRACESPEC_INACTIVE: | |
2516 | case DTRACESPEC_DISCARDING: | |
2517 | return; | |
2518 | ||
2519 | case DTRACESPEC_COMMITTING: | |
2520 | /* | |
2521 | * This is only possible if we are (a) commit()'ing | |
2522 | * without having done a prior speculate() on this CPU | |
2523 | * and (b) racing with another commit() on a different | |
2524 | * CPU. There's nothing to do -- we just assert that | |
2525 | * our offset is 0. | |
2526 | */ | |
2527 | ASSERT(src->dtb_offset == 0); | |
2528 | return; | |
2529 | ||
2530 | case DTRACESPEC_ACTIVE: | |
2531 | new = DTRACESPEC_COMMITTING; | |
2532 | break; | |
2533 | ||
2534 | case DTRACESPEC_ACTIVEONE: | |
2535 | /* | |
2536 | * This speculation is active on one CPU. If our | |
2537 | * buffer offset is non-zero, we know that the one CPU | |
2538 | * must be us. Otherwise, we are committing on a | |
2539 | * different CPU from the speculate(), and we must | |
2540 | * rely on being asynchronously cleaned. | |
2541 | */ | |
2542 | if (src->dtb_offset != 0) { | |
2543 | new = DTRACESPEC_COMMITTING; | |
2544 | break; | |
2545 | } | |
2546 | /*FALLTHROUGH*/ | |
2547 | ||
2548 | case DTRACESPEC_ACTIVEMANY: | |
2549 | new = DTRACESPEC_COMMITTINGMANY; | |
2550 | break; | |
2551 | ||
2552 | default: | |
2553 | ASSERT(0); | |
2554 | } | |
2555 | } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, | |
2556 | current, new) != current); | |
2557 | ||
2558 | /* | |
2559 | * We have set the state to indicate that we are committing this | |
2560 | * speculation. Now reserve the necessary space in the destination | |
2561 | * buffer. | |
2562 | */ | |
2563 | if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, | |
2564 | sizeof (uint64_t), state, NULL)) < 0) { | |
2565 | dtrace_buffer_drop(dest); | |
2566 | goto out; | |
2567 | } | |
2568 | ||
2569 | /* | |
2570 | * We have the space; copy the buffer across. (Note that this is a | |
2571 | * highly subobtimal bcopy(); in the unlikely event that this becomes | |
2572 | * a serious performance issue, a high-performance DTrace-specific | |
2573 | * bcopy() should obviously be invented.) | |
2574 | */ | |
2575 | daddr = (uintptr_t)dest->dtb_tomax + offs; | |
2576 | dlimit = daddr + src->dtb_offset; | |
2577 | saddr = (uintptr_t)src->dtb_tomax; | |
2578 | ||
2579 | /* | |
2580 | * First, the aligned portion. | |
2581 | */ | |
2582 | while (dlimit - daddr >= sizeof (uint64_t)) { | |
2583 | *((uint64_t *)daddr) = *((uint64_t *)saddr); | |
2584 | ||
2585 | daddr += sizeof (uint64_t); | |
2586 | saddr += sizeof (uint64_t); | |
2587 | } | |
2588 | ||
2589 | /* | |
2590 | * Now any left-over bit... | |
2591 | */ | |
2592 | while (dlimit - daddr) | |
2593 | *((uint8_t *)daddr++) = *((uint8_t *)saddr++); | |
2594 | ||
2595 | /* | |
2596 | * Finally, commit the reserved space in the destination buffer. | |
2597 | */ | |
2598 | dest->dtb_offset = offs + src->dtb_offset; | |
2599 | ||
2600 | out: | |
2601 | /* | |
2602 | * If we're lucky enough to be the only active CPU on this speculation | |
2603 | * buffer, we can just set the state back to DTRACESPEC_INACTIVE. | |
2604 | */ | |
2605 | if (current == DTRACESPEC_ACTIVE || | |
2606 | (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { | |
2607 | uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, | |
2608 | DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); | |
b0d623f7 | 2609 | #pragma unused(rval) /* __APPLE__ */ |
2d21ac55 A |
2610 | |
2611 | ASSERT(rval == DTRACESPEC_COMMITTING); | |
2612 | } | |
2613 | ||
2614 | src->dtb_offset = 0; | |
2615 | src->dtb_xamot_drops += src->dtb_drops; | |
2616 | src->dtb_drops = 0; | |
2617 | } | |
2618 | ||
2619 | /* | |
2620 | * This routine discards an active speculation. If the specified speculation | |
2621 | * is not in a valid state to perform a discard(), this routine will silently | |
2622 | * do nothing. The state of the specified speculation is transitioned | |
2623 | * according to the state transition diagram outlined in <sys/dtrace_impl.h> | |
2624 | */ | |
2625 | static void | |
2626 | dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, | |
2627 | dtrace_specid_t which) | |
2628 | { | |
2629 | dtrace_speculation_t *spec; | |
b0d623f7 | 2630 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2631 | dtrace_speculation_state_t current, new; |
b0d623f7 A |
2632 | #else |
2633 | dtrace_speculation_state_t current, new = DTRACESPEC_INACTIVE; | |
2634 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2635 | dtrace_buffer_t *buf; |
2636 | ||
2637 | if (which == 0) | |
2638 | return; | |
2639 | ||
b0d623f7 | 2640 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 A |
2641 | if (which > state->dts_nspeculations) { |
2642 | cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; | |
2643 | return; | |
2644 | } | |
b0d623f7 A |
2645 | #else |
2646 | if (which > (dtrace_specid_t)state->dts_nspeculations) { | |
2647 | cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; | |
2648 | return; | |
2649 | } | |
2650 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2651 | |
2652 | spec = &state->dts_speculations[which - 1]; | |
2653 | buf = &spec->dtsp_buffer[cpu]; | |
2654 | ||
2655 | do { | |
2656 | current = spec->dtsp_state; | |
2657 | ||
2658 | switch (current) { | |
2659 | case DTRACESPEC_INACTIVE: | |
2660 | case DTRACESPEC_COMMITTINGMANY: | |
2661 | case DTRACESPEC_COMMITTING: | |
2662 | case DTRACESPEC_DISCARDING: | |
2663 | return; | |
2664 | ||
2665 | case DTRACESPEC_ACTIVE: | |
2666 | case DTRACESPEC_ACTIVEMANY: | |
2667 | new = DTRACESPEC_DISCARDING; | |
2668 | break; | |
2669 | ||
2670 | case DTRACESPEC_ACTIVEONE: | |
2671 | if (buf->dtb_offset != 0) { | |
2672 | new = DTRACESPEC_INACTIVE; | |
2673 | } else { | |
2674 | new = DTRACESPEC_DISCARDING; | |
2675 | } | |
2676 | break; | |
2677 | ||
2678 | default: | |
2679 | ASSERT(0); | |
2680 | } | |
2681 | } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, | |
2682 | current, new) != current); | |
2683 | ||
2684 | buf->dtb_offset = 0; | |
2685 | buf->dtb_drops = 0; | |
2686 | } | |
2687 | ||
2688 | /* | |
2689 | * Note: not called from probe context. This function is called | |
2690 | * asynchronously from cross call context to clean any speculations that are | |
2691 | * in the COMMITTINGMANY or DISCARDING states. These speculations may not be | |
2692 | * transitioned back to the INACTIVE state until all CPUs have cleaned the | |
2693 | * speculation. | |
2694 | */ | |
2695 | static void | |
2696 | dtrace_speculation_clean_here(dtrace_state_t *state) | |
2697 | { | |
2698 | dtrace_icookie_t cookie; | |
2699 | processorid_t cpu = CPU->cpu_id; | |
2700 | dtrace_buffer_t *dest = &state->dts_buffer[cpu]; | |
2701 | dtrace_specid_t i; | |
2702 | ||
2703 | cookie = dtrace_interrupt_disable(); | |
2704 | ||
2705 | if (dest->dtb_tomax == NULL) { | |
2706 | dtrace_interrupt_enable(cookie); | |
2707 | return; | |
2708 | } | |
2709 | ||
b0d623f7 | 2710 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2711 | for (i = 0; i < state->dts_nspeculations; i++) { |
b0d623f7 A |
2712 | #else |
2713 | for (i = 0; i < (dtrace_specid_t)state->dts_nspeculations; i++) { | |
2714 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2715 | dtrace_speculation_t *spec = &state->dts_speculations[i]; |
2716 | dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; | |
2717 | ||
2718 | if (src->dtb_tomax == NULL) | |
2719 | continue; | |
2720 | ||
2721 | if (spec->dtsp_state == DTRACESPEC_DISCARDING) { | |
2722 | src->dtb_offset = 0; | |
2723 | continue; | |
2724 | } | |
2725 | ||
2726 | if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) | |
2727 | continue; | |
2728 | ||
2729 | if (src->dtb_offset == 0) | |
2730 | continue; | |
2731 | ||
2732 | dtrace_speculation_commit(state, cpu, i + 1); | |
2733 | } | |
2734 | ||
2735 | dtrace_interrupt_enable(cookie); | |
2736 | } | |
2737 | ||
2738 | /* | |
2739 | * Note: not called from probe context. This function is called | |
2740 | * asynchronously (and at a regular interval) to clean any speculations that | |
2741 | * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there | |
2742 | * is work to be done, it cross calls all CPUs to perform that work; | |
2743 | * COMMITMANY and DISCARDING speculations may not be transitioned back to the | |
2744 | * INACTIVE state until they have been cleaned by all CPUs. | |
2745 | */ | |
2746 | static void | |
2747 | dtrace_speculation_clean(dtrace_state_t *state) | |
2748 | { | |
b0d623f7 | 2749 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2750 | int work = 0, rv; |
b0d623f7 A |
2751 | #else |
2752 | int work = 0; | |
2753 | uint32_t rv; | |
2754 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2755 | dtrace_specid_t i; |
2756 | ||
b0d623f7 | 2757 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2758 | for (i = 0; i < state->dts_nspeculations; i++) { |
b0d623f7 A |
2759 | #else |
2760 | for (i = 0; i < (dtrace_specid_t)state->dts_nspeculations; i++) { | |
2761 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2762 | dtrace_speculation_t *spec = &state->dts_speculations[i]; |
2763 | ||
2764 | ASSERT(!spec->dtsp_cleaning); | |
2765 | ||
2766 | if (spec->dtsp_state != DTRACESPEC_DISCARDING && | |
2767 | spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) | |
2768 | continue; | |
2769 | ||
2770 | work++; | |
2771 | spec->dtsp_cleaning = 1; | |
2772 | } | |
2773 | ||
2774 | if (!work) | |
2775 | return; | |
2776 | ||
2777 | dtrace_xcall(DTRACE_CPUALL, | |
2778 | (dtrace_xcall_t)dtrace_speculation_clean_here, state); | |
2779 | ||
2780 | /* | |
2781 | * We now know that all CPUs have committed or discarded their | |
2782 | * speculation buffers, as appropriate. We can now set the state | |
2783 | * to inactive. | |
2784 | */ | |
b0d623f7 | 2785 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2786 | for (i = 0; i < state->dts_nspeculations; i++) { |
b0d623f7 A |
2787 | #else |
2788 | for (i = 0; i < (dtrace_specid_t)state->dts_nspeculations; i++) { | |
2789 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2790 | dtrace_speculation_t *spec = &state->dts_speculations[i]; |
2791 | dtrace_speculation_state_t current, new; | |
2792 | ||
2793 | if (!spec->dtsp_cleaning) | |
2794 | continue; | |
2795 | ||
2796 | current = spec->dtsp_state; | |
2797 | ASSERT(current == DTRACESPEC_DISCARDING || | |
2798 | current == DTRACESPEC_COMMITTINGMANY); | |
2799 | ||
2800 | new = DTRACESPEC_INACTIVE; | |
2801 | ||
2802 | rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); | |
2803 | ASSERT(rv == current); | |
2804 | spec->dtsp_cleaning = 0; | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | /* | |
2809 | * Called as part of a speculate() to get the speculative buffer associated | |
2810 | * with a given speculation. Returns NULL if the specified speculation is not | |
2811 | * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and | |
2812 | * the active CPU is not the specified CPU -- the speculation will be | |
2813 | * atomically transitioned into the ACTIVEMANY state. | |
2814 | */ | |
2815 | static dtrace_buffer_t * | |
2816 | dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, | |
2817 | dtrace_specid_t which) | |
2818 | { | |
2819 | dtrace_speculation_t *spec; | |
b0d623f7 | 2820 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2821 | dtrace_speculation_state_t current, new; |
b0d623f7 A |
2822 | #else |
2823 | dtrace_speculation_state_t current, new = DTRACESPEC_INACTIVE; | |
2824 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2825 | dtrace_buffer_t *buf; |
2826 | ||
2827 | if (which == 0) | |
2828 | return (NULL); | |
2829 | ||
b0d623f7 | 2830 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 2831 | if (which > state->dts_nspeculations) { |
b0d623f7 A |
2832 | #else |
2833 | if (which > (dtrace_specid_t)state->dts_nspeculations) { | |
2834 | #endif /* __APPLE__ */ | |
2d21ac55 A |
2835 | cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; |
2836 | return (NULL); | |
2837 | } | |
2838 | ||
2839 | spec = &state->dts_speculations[which - 1]; | |
2840 | buf = &spec->dtsp_buffer[cpuid]; | |
2841 | ||
2842 | do { | |
2843 | current = spec->dtsp_state; | |
2844 | ||
2845 | switch (current) { | |
2846 | case DTRACESPEC_INACTIVE: | |
2847 | case DTRACESPEC_COMMITTINGMANY: | |
2848 | case DTRACESPEC_DISCARDING: | |
2849 | return (NULL); | |
2850 | ||
2851 | case DTRACESPEC_COMMITTING: | |
2852 | ASSERT(buf->dtb_offset == 0); | |
2853 | return (NULL); | |
2854 | ||
2855 | case DTRACESPEC_ACTIVEONE: | |
2856 | /* | |
2857 | * This speculation is currently active on one CPU. | |
2858 | * Check the offset in the buffer; if it's non-zero, | |
2859 | * that CPU must be us (and we leave the state alone). | |
2860 | * If it's zero, assume that we're starting on a new | |
2861 | * CPU -- and change the state to indicate that the | |
2862 | * speculation is active on more than one CPU. | |
2863 | */ | |
2864 | if (buf->dtb_offset != 0) | |
2865 | return (buf); | |
2866 | ||
2867 | new = DTRACESPEC_ACTIVEMANY; | |
2868 | break; | |
2869 | ||
2870 | case DTRACESPEC_ACTIVEMANY: | |
2871 | return (buf); | |
2872 | ||
2873 | case DTRACESPEC_ACTIVE: | |
2874 | new = DTRACESPEC_ACTIVEONE; | |
2875 | break; | |
2876 | ||
2877 | default: | |
2878 | ASSERT(0); | |
2879 | } | |
2880 | } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, | |
2881 | current, new) != current); | |
2882 | ||
2883 | ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); | |
2884 | return (buf); | |
2885 | } | |
2886 | ||
b0d623f7 A |
2887 | /* |
2888 | * Return a string. In the event that the user lacks the privilege to access | |
2889 | * arbitrary kernel memory, we copy the string out to scratch memory so that we | |
2890 | * don't fail access checking. | |
2891 | * | |
2892 | * dtrace_dif_variable() uses this routine as a helper for various | |
2893 | * builtin values such as 'execname' and 'probefunc.' | |
2894 | */ | |
2895 | #if defined(__APPLE__) /* Quiet compiler warning. */ | |
2896 | static | |
2897 | #endif /* __APPLE__ */ | |
2898 | uintptr_t | |
2899 | dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, | |
2900 | dtrace_mstate_t *mstate) | |
2901 | { | |
2902 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
2903 | uintptr_t ret; | |
2904 | size_t strsz; | |
2905 | ||
2906 | /* | |
2907 | * The easy case: this probe is allowed to read all of memory, so | |
2908 | * we can just return this as a vanilla pointer. | |
2909 | */ | |
2910 | if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) | |
2911 | return (addr); | |
2912 | ||
2913 | /* | |
2914 | * This is the tougher case: we copy the string in question from | |
2915 | * kernel memory into scratch memory and return it that way: this | |
2916 | * ensures that we won't trip up when access checking tests the | |
2917 | * BYREF return value. | |
2918 | */ | |
2919 | strsz = dtrace_strlen((char *)addr, size) + 1; | |
2920 | ||
2921 | if (mstate->dtms_scratch_ptr + strsz > | |
2922 | mstate->dtms_scratch_base + mstate->dtms_scratch_size) { | |
2923 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
2924 | return (NULL); | |
2925 | } | |
2926 | ||
2927 | dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, | |
2928 | strsz); | |
2929 | ret = mstate->dtms_scratch_ptr; | |
2930 | mstate->dtms_scratch_ptr += strsz; | |
2931 | return (ret); | |
2932 | } | |
2933 | ||
2d21ac55 A |
2934 | /* |
2935 | * This function implements the DIF emulator's variable lookups. The emulator | |
2936 | * passes a reserved variable identifier and optional built-in array index. | |
2937 | */ | |
2938 | static uint64_t | |
2939 | dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, | |
2940 | uint64_t ndx) | |
2941 | { | |
2942 | /* | |
2943 | * If we're accessing one of the uncached arguments, we'll turn this | |
2944 | * into a reference in the args array. | |
2945 | */ | |
2946 | if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { | |
2947 | ndx = v - DIF_VAR_ARG0; | |
2948 | v = DIF_VAR_ARGS; | |
2949 | } | |
2950 | ||
2951 | switch (v) { | |
2952 | case DIF_VAR_ARGS: | |
2953 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); | |
2954 | if (ndx >= sizeof (mstate->dtms_arg) / | |
2955 | sizeof (mstate->dtms_arg[0])) { | |
2956 | #if !defined(__APPLE__) | |
2957 | int aframes = mstate->dtms_probe->dtpr_aframes + 2; | |
2958 | #else | |
2959 | /* Account for introduction of __dtrace_probe() on xnu. */ | |
2960 | int aframes = mstate->dtms_probe->dtpr_aframes + 3; | |
2961 | #endif /* __APPLE__ */ | |
2962 | dtrace_provider_t *pv; | |
2963 | uint64_t val; | |
2964 | ||
2965 | pv = mstate->dtms_probe->dtpr_provider; | |
2966 | if (pv->dtpv_pops.dtps_getargval != NULL) | |
2967 | val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, | |
2968 | mstate->dtms_probe->dtpr_id, | |
2969 | mstate->dtms_probe->dtpr_arg, ndx, aframes); | |
2970 | #if defined(__APPLE__) | |
b0d623f7 | 2971 | /* Special case access of arg5 as passed to dtrace_probe_error() (which see.) */ |
2d21ac55 | 2972 | else if (mstate->dtms_probe->dtpr_id == dtrace_probeid_error && ndx == 5) { |
b0d623f7 | 2973 | return ((dtrace_state_t *)(uintptr_t)(mstate->dtms_arg[0]))->dts_arg_error_illval; |
2d21ac55 A |
2974 | } |
2975 | #endif /* __APPLE__ */ | |
2976 | else | |
2977 | val = dtrace_getarg(ndx, aframes); | |
2978 | ||
2979 | /* | |
2980 | * This is regrettably required to keep the compiler | |
2981 | * from tail-optimizing the call to dtrace_getarg(). | |
2982 | * The condition always evaluates to true, but the | |
2983 | * compiler has no way of figuring that out a priori. | |
2984 | * (None of this would be necessary if the compiler | |
2985 | * could be relied upon to _always_ tail-optimize | |
2986 | * the call to dtrace_getarg() -- but it can't.) | |
2987 | */ | |
2988 | if (mstate->dtms_probe != NULL) | |
2989 | return (val); | |
2990 | ||
2991 | ASSERT(0); | |
2992 | } | |
2993 | ||
2994 | return (mstate->dtms_arg[ndx]); | |
2995 | ||
2996 | #if !defined(__APPLE__) | |
2997 | case DIF_VAR_UREGS: { | |
2998 | klwp_t *lwp; | |
2999 | ||
3000 | if (!dtrace_priv_proc(state)) | |
3001 | return (0); | |
3002 | ||
3003 | if ((lwp = curthread->t_lwp) == NULL) { | |
3004 | DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); | |
3005 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = NULL; | |
3006 | return (0); | |
3007 | } | |
3008 | ||
3009 | return (dtrace_getreg(lwp->lwp_regs, ndx)); | |
3010 | } | |
3011 | #else | |
3012 | case DIF_VAR_UREGS: { | |
3013 | thread_t thread; | |
3014 | ||
3015 | if (!dtrace_priv_proc(state)) | |
3016 | return (0); | |
3017 | ||
3018 | if ((thread = current_thread()) == NULL) { | |
3019 | DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); | |
3020 | cpu_core[CPU->cpu_id].cpuc_dtrace_illval = 0; | |
3021 | return (0); | |
3022 | } | |
3023 | ||
3024 | return (dtrace_getreg(find_user_regs(thread), ndx)); | |
3025 | } | |
3026 | #endif /* __APPLE__ */ | |
3027 | ||
3028 | #if !defined(__APPLE__) | |
3029 | case DIF_VAR_CURTHREAD: | |
3030 | if (!dtrace_priv_kernel(state)) | |
3031 | return (0); | |
3032 | return ((uint64_t)(uintptr_t)curthread); | |
3033 | #else | |
3034 | case DIF_VAR_CURTHREAD: | |
3035 | if (!dtrace_priv_kernel(state)) | |
3036 | return (0); | |
3037 | ||
3038 | return ((uint64_t)(uintptr_t)current_thread()); | |
3039 | #endif /* __APPLE__ */ | |
3040 | ||
3041 | case DIF_VAR_TIMESTAMP: | |
3042 | if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { | |
3043 | mstate->dtms_timestamp = dtrace_gethrtime(); | |
3044 | mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; | |
3045 | } | |
3046 | return (mstate->dtms_timestamp); | |
3047 | ||
3048 | #if !defined(__APPLE__) | |
3049 | case DIF_VAR_VTIMESTAMP: | |
3050 | ASSERT(dtrace_vtime_references != 0); | |
3051 | return (curthread->t_dtrace_vtime); | |
3052 | #else | |
3053 | case DIF_VAR_VTIMESTAMP: | |
3054 | ASSERT(dtrace_vtime_references != 0); | |
3055 | return (dtrace_get_thread_vtime(current_thread())); | |
3056 | #endif /* __APPLE__ */ | |
3057 | ||
3058 | case DIF_VAR_WALLTIMESTAMP: | |
3059 | if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { | |
3060 | mstate->dtms_walltimestamp = dtrace_gethrestime(); | |
3061 | mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; | |
3062 | } | |
3063 | return (mstate->dtms_walltimestamp); | |
3064 | ||
3065 | case DIF_VAR_IPL: | |
3066 | if (!dtrace_priv_kernel(state)) | |
3067 | return (0); | |
3068 | if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { | |
3069 | mstate->dtms_ipl = dtrace_getipl(); | |
3070 | mstate->dtms_present |= DTRACE_MSTATE_IPL; | |
3071 | } | |
3072 | return (mstate->dtms_ipl); | |
3073 | ||
3074 | case DIF_VAR_EPID: | |
3075 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); | |
3076 | return (mstate->dtms_epid); | |
3077 | ||
3078 | case DIF_VAR_ID: | |
3079 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); | |
3080 | return (mstate->dtms_probe->dtpr_id); | |
3081 | ||
3082 | case DIF_VAR_STACKDEPTH: | |
3083 | if (!dtrace_priv_kernel(state)) | |
3084 | return (0); | |
3085 | if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { | |
3086 | #if !defined(__APPLE__) | |
3087 | int aframes = mstate->dtms_probe->dtpr_aframes + 2; | |
3088 | #else | |
3089 | /* Account for introduction of __dtrace_probe() on xnu. */ | |
3090 | int aframes = mstate->dtms_probe->dtpr_aframes + 3; | |
3091 | #endif /* __APPLE__ */ | |
3092 | ||
3093 | mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); | |
3094 | mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; | |
3095 | } | |
3096 | return (mstate->dtms_stackdepth); | |
3097 | ||
3098 | case DIF_VAR_USTACKDEPTH: | |
3099 | if (!dtrace_priv_proc(state)) | |
3100 | return (0); | |
3101 | if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { | |
3102 | /* | |
3103 | * See comment in DIF_VAR_PID. | |
3104 | */ | |
3105 | if (DTRACE_ANCHORED(mstate->dtms_probe) && | |
3106 | CPU_ON_INTR(CPU)) { | |
3107 | mstate->dtms_ustackdepth = 0; | |
3108 | } else { | |
3109 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
3110 | mstate->dtms_ustackdepth = | |
3111 | dtrace_getustackdepth(); | |
3112 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); | |
3113 | } | |
3114 | mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; | |
3115 | } | |
3116 | return (mstate->dtms_ustackdepth); | |
3117 | ||
3118 | case DIF_VAR_CALLER: | |
3119 | if (!dtrace_priv_kernel(state)) | |
3120 | return (0); | |
3121 | if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { | |
3122 | #if !defined(__APPLE__) | |
3123 | int aframes = mstate->dtms_probe->dtpr_aframes + 2; | |
3124 | #else | |
3125 | /* Account for introduction of __dtrace_probe() on xnu. */ | |
3126 | int aframes = mstate->dtms_probe->dtpr_aframes + 3; | |
3127 | #endif /* __APPLE__ */ | |
3128 | ||
3129 | if (!DTRACE_ANCHORED(mstate->dtms_probe)) { | |
3130 | /* | |
3131 | * If this is an unanchored probe, we are | |
3132 | * required to go through the slow path: | |
3133 | * dtrace_caller() only guarantees correct | |
3134 | * results for anchored probes. | |
3135 | */ | |
3136 | pc_t caller[2]; | |
3137 | ||
3138 | dtrace_getpcstack(caller, 2, aframes, | |
3139 | (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); | |
3140 | mstate->dtms_caller = caller[1]; | |
3141 | } else if ((mstate->dtms_caller = | |
b0d623f7 | 3142 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 3143 | dtrace_caller(aframes)) == -1) { |
b0d623f7 A |
3144 | #else |
3145 | dtrace_caller(aframes)) == (uintptr_t)-1) { | |
3146 | #endif /* __APPLE__ */ | |
2d21ac55 A |
3147 | /* |
3148 | * We have failed to do this the quick way; | |
3149 | * we must resort to the slower approach of | |
3150 | * calling dtrace_getpcstack(). | |
3151 | */ | |
3152 | pc_t caller; | |
3153 | ||
3154 | dtrace_getpcstack(&caller, 1, aframes, NULL); | |
3155 | mstate->dtms_caller = caller; | |
3156 | } | |
3157 | ||
3158 | mstate->dtms_present |= DTRACE_MSTATE_CALLER; | |
3159 | } | |
3160 | return (mstate->dtms_caller); | |
3161 | ||
3162 | case DIF_VAR_UCALLER: | |
3163 | if (!dtrace_priv_proc(state)) | |
3164 | return (0); | |
3165 | ||
3166 | if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { | |
3167 | uint64_t ustack[3]; | |
3168 | ||
3169 | /* | |
3170 | * dtrace_getupcstack() fills in the first uint64_t | |
3171 | * with the current PID. The second uint64_t will | |
3172 | * be the program counter at user-level. The third | |
3173 | * uint64_t will contain the caller, which is what | |
3174 | * we're after. | |
3175 | */ | |
3176 | ustack[2] = NULL; | |
b0d623f7 | 3177 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); |
2d21ac55 | 3178 | dtrace_getupcstack(ustack, 3); |
b0d623f7 | 3179 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
2d21ac55 A |
3180 | mstate->dtms_ucaller = ustack[2]; |
3181 | mstate->dtms_present |= DTRACE_MSTATE_UCALLER; | |
3182 | } | |
3183 | ||
3184 | return (mstate->dtms_ucaller); | |
3185 | ||
3186 | case DIF_VAR_PROBEPROV: | |
3187 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); | |
b0d623f7 A |
3188 | return (dtrace_dif_varstr( |
3189 | (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, | |
3190 | state, mstate)); | |
2d21ac55 A |
3191 | |
3192 | case DIF_VAR_PROBEMOD: | |
3193 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); | |
b0d623f7 A |
3194 | return (dtrace_dif_varstr( |
3195 | (uintptr_t)mstate->dtms_probe->dtpr_mod, | |
3196 | state, mstate)); | |
2d21ac55 A |
3197 | |
3198 | case DIF_VAR_PROBEFUNC: | |
3199 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); | |
b0d623f7 A |
3200 | return (dtrace_dif_varstr( |
3201 | (uintptr_t)mstate->dtms_probe->dtpr_func, | |
3202 | state, mstate)); | |
2d21ac55 A |
3203 | |
3204 | case DIF_VAR_PROBENAME: | |
3205 | ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); | |
b0d623f7 A |
3206 | return (dtrace_dif_varstr( |
3207 | (uintptr_t)mstate->dtms_probe->dtpr_name, | |
3208 | state, mstate)); | |
2d21ac55 A |
3209 | |
3210 | #if !defined(__APPLE__) | |
3211 | case DIF_VAR_PID: | |
3212 | if (!dtrace_priv_proc(state)) | |
3213 | return (0); | |
3214 | ||
3215 | /* | |
3216 | * Note that we are assuming that an unanchored probe is | |
3217 | * always due to a high-level interrupt. (And we're assuming | |
3218 | * that there is only a single high level interrupt.) | |
3219 | */ | |
3220 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3221 | return (pid0.pid_id); | |
3222 | ||
3223 | /* | |
3224 | * It is always safe to dereference one's own t_procp pointer: | |
3225 | * it always points to a valid, allocated proc structure. | |
3226 | * Further, it is always safe to dereference the p_pidp member | |
3227 | * of one's own proc structure. (These are truisms becuase | |
3228 | * threads and processes don't clean up their own state -- | |
3229 | * they leave that task to whomever reaps them.) | |
3230 | */ | |
3231 | return ((uint64_t)curthread->t_procp->p_pidp->pid_id); | |
3232 | ||
3233 | #else | |
3234 | case DIF_VAR_PID: | |
935ed37a | 3235 | if (!dtrace_priv_proc_relaxed(state)) |
2d21ac55 A |
3236 | return (0); |
3237 | ||
3238 | /* | |
3239 | * Note that we are assuming that an unanchored probe is | |
3240 | * always due to a high-level interrupt. (And we're assuming | |
3241 | * that there is only a single high level interrupt.) | |
3242 | */ | |
3243 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3244 | /* Anchored probe that fires while on an interrupt accrues to process 0 */ | |
3245 | return 0; | |
3246 | ||
3247 | return ((uint64_t)proc_selfpid()); | |
3248 | #endif /* __APPLE__ */ | |
3249 | ||
3250 | #if !defined(__APPLE__) | |
3251 | case DIF_VAR_PPID: | |
3252 | if (!dtrace_priv_proc(state)) | |
3253 | return (0); | |
3254 | ||
3255 | /* | |
3256 | * See comment in DIF_VAR_PID. | |
3257 | */ | |
3258 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3259 | return (pid0.pid_id); | |
3260 | ||
b0d623f7 A |
3261 | /* |
3262 | * It is always safe to dereference one's own t_procp pointer: | |
3263 | * it always points to a valid, allocated proc structure. | |
3264 | * (This is true because threads don't clean up their own | |
3265 | * state -- they leave that task to whomever reaps them.) | |
3266 | */ | |
2d21ac55 A |
3267 | return ((uint64_t)curthread->t_procp->p_ppid); |
3268 | #else | |
3269 | case DIF_VAR_PPID: | |
935ed37a | 3270 | if (!dtrace_priv_proc_relaxed(state)) |
2d21ac55 A |
3271 | return (0); |
3272 | ||
3273 | /* | |
3274 | * See comment in DIF_VAR_PID. | |
3275 | */ | |
3276 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3277 | return (0); | |
3278 | ||
b0d623f7 | 3279 | return ((uint64_t)proc_selfppid()); |
2d21ac55 A |
3280 | #endif /* __APPLE__ */ |
3281 | ||
3282 | #if !defined(__APPLE__) | |
3283 | case DIF_VAR_TID: | |
3284 | /* | |
3285 | * See comment in DIF_VAR_PID. | |
3286 | */ | |
3287 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3288 | return (0); | |
3289 | ||
3290 | return ((uint64_t)curthread->t_tid); | |
3291 | #else | |
3292 | case DIF_VAR_TID: | |
b0d623f7 A |
3293 | /* We do not need to check for null current_thread() */ |
3294 | return thread_tid(current_thread()); /* globally unique */ | |
3295 | ||
3296 | case DIF_VAR_PTHREAD_SELF: | |
3297 | if (!dtrace_priv_proc(state)) | |
3298 | return (0); | |
3299 | ||
3300 | /* Not currently supported, but we should be able to delta the dispatchqaddr and dispatchqoffset to get pthread_self */ | |
3301 | return 0; | |
3302 | ||
3303 | case DIF_VAR_DISPATCHQADDR: | |
3304 | if (!dtrace_priv_proc(state)) | |
2d21ac55 A |
3305 | return (0); |
3306 | ||
b0d623f7 A |
3307 | /* We do not need to check for null current_thread() */ |
3308 | return thread_dispatchqaddr(current_thread()); | |
2d21ac55 A |
3309 | #endif /* __APPLE__ */ |
3310 | ||
3311 | #if !defined(__APPLE__) | |
3312 | case DIF_VAR_EXECNAME: | |
3313 | if (!dtrace_priv_proc(state)) | |
3314 | return (0); | |
3315 | ||
3316 | /* | |
3317 | * See comment in DIF_VAR_PID. | |
3318 | */ | |
3319 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3320 | return ((uint64_t)(uintptr_t)p0.p_user.u_comm); | |
3321 | ||
3322 | /* | |
3323 | * It is always safe to dereference one's own t_procp pointer: | |
3324 | * it always points to a valid, allocated proc structure. | |
3325 | * (This is true because threads don't clean up their own | |
3326 | * state -- they leave that task to whomever reaps them.) | |
3327 | */ | |
b0d623f7 A |
3328 | return (dtrace_dif_varstr( |
3329 | (uintptr_t)curthread->t_procp->p_user.u_comm, | |
3330 | state, mstate)); | |
2d21ac55 A |
3331 | #else |
3332 | case DIF_VAR_EXECNAME: | |
3333 | { | |
3334 | char *xname = (char *)mstate->dtms_scratch_ptr; | |
3335 | size_t scratch_size = MAXCOMLEN+1; | |
3336 | ||
3337 | /* The scratch allocation's lifetime is that of the clause. */ | |
b0d623f7 A |
3338 | if (!DTRACE_INSCRATCH(mstate, scratch_size)) { |
3339 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
2d21ac55 | 3340 | return 0; |
b0d623f7 | 3341 | } |
2d21ac55 | 3342 | |
935ed37a | 3343 | if (!dtrace_priv_proc_relaxed(state)) |
2d21ac55 A |
3344 | return (0); |
3345 | ||
3346 | mstate->dtms_scratch_ptr += scratch_size; | |
3347 | proc_selfname( xname, MAXCOMLEN ); | |
3348 | ||
3349 | return ((uint64_t)(uintptr_t)xname); | |
3350 | } | |
3351 | #endif /* __APPLE__ */ | |
3352 | #if !defined(__APPLE__) | |
3353 | case DIF_VAR_ZONENAME: | |
3354 | if (!dtrace_priv_proc(state)) | |
3355 | return (0); | |
3356 | ||
3357 | /* | |
3358 | * See comment in DIF_VAR_PID. | |
3359 | */ | |
3360 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3361 | return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); | |
3362 | ||
3363 | /* | |
3364 | * It is always safe to dereference one's own t_procp pointer: | |
3365 | * it always points to a valid, allocated proc structure. | |
3366 | * (This is true because threads don't clean up their own | |
3367 | * state -- they leave that task to whomever reaps them.) | |
3368 | */ | |
b0d623f7 A |
3369 | return (dtrace_dif_varstr( |
3370 | (uintptr_t)curthread->t_procp->p_zone->zone_name, | |
3371 | state, mstate)); | |
2d21ac55 A |
3372 | |
3373 | #else | |
3374 | case DIF_VAR_ZONENAME: | |
3375 | if (!dtrace_priv_proc(state)) | |
3376 | return (0); | |
3377 | ||
b0d623f7 | 3378 | /* FIXME: return e.g. "global" allocated from scratch a la execname. */ |
2d21ac55 A |
3379 | return ((uint64_t)(uintptr_t)NULL); /* Darwin doesn't do "zones" */ |
3380 | #endif /* __APPLE__ */ | |
3381 | ||
3382 | #if !defined(__APPLE__) | |
3383 | case DIF_VAR_UID: | |
3384 | if (!dtrace_priv_proc(state)) | |
3385 | return (0); | |
3386 | ||
3387 | /* | |
3388 | * See comment in DIF_VAR_PID. | |
3389 | */ | |
3390 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3391 | return ((uint64_t)p0.p_cred->cr_uid); | |
3392 | ||
b0d623f7 A |
3393 | /* |
3394 | * It is always safe to dereference one's own t_procp pointer: | |
3395 | * it always points to a valid, allocated proc structure. | |
3396 | * (This is true because threads don't clean up their own | |
3397 | * state -- they leave that task to whomever reaps them.) | |
3398 | * | |
3399 | * Additionally, it is safe to dereference one's own process | |
3400 | * credential, since this is never NULL after process birth. | |
3401 | */ | |
3402 | return ((uint64_t)curthread->t_procp->p_cred->cr_uid); | |
2d21ac55 A |
3403 | #else |
3404 | case DIF_VAR_UID: | |
3405 | if (!dtrace_priv_proc(state)) | |
3406 | return (0); | |
3407 | ||
3408 | /* | |
3409 | * See comment in DIF_VAR_PID. | |
3410 | */ | |
3411 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3412 | return (0); | |
3413 | ||
3414 | if (dtrace_CRED() != NULL) | |
b0d623f7 | 3415 | /* Credential does not require lazy initialization. */ |
2d21ac55 | 3416 | return ((uint64_t)kauth_getuid()); |
b0d623f7 A |
3417 | else { |
3418 | /* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */ | |
3419 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
3420 | return -1ULL; | |
3421 | } | |
2d21ac55 A |
3422 | #endif /* __APPLE__ */ |
3423 | ||
3424 | #if !defined(__APPLE__) | |
3425 | case DIF_VAR_GID: | |
3426 | if (!dtrace_priv_proc(state)) | |
3427 | return (0); | |
3428 | ||
3429 | /* | |
3430 | * See comment in DIF_VAR_PID. | |
3431 | */ | |
3432 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3433 | return ((uint64_t)p0.p_cred->cr_gid); | |
3434 | ||
b0d623f7 A |
3435 | /* |
3436 | * It is always safe to dereference one's own t_procp pointer: | |
3437 | * it always points to a valid, allocated proc structure. | |
3438 | * (This is true because threads don't clean up their own | |
3439 | * state -- they leave that task to whomever reaps them.) | |
3440 | * | |
3441 | * Additionally, it is safe to dereference one's own process | |
3442 | * credential, since this is never NULL after process birth. | |
3443 | */ | |
3444 | return ((uint64_t)curthread->t_procp->p_cred->cr_gid); | |
2d21ac55 A |
3445 | #else |
3446 | case DIF_VAR_GID: | |
3447 | if (!dtrace_priv_proc(state)) | |
3448 | return (0); | |
3449 | ||
3450 | /* | |
3451 | * See comment in DIF_VAR_PID. | |
3452 | */ | |
3453 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3454 | return (0); | |
3455 | ||
3456 | if (dtrace_CRED() != NULL) | |
b0d623f7 | 3457 | /* Credential does not require lazy initialization. */ |
2d21ac55 | 3458 | return ((uint64_t)kauth_getgid()); |
b0d623f7 A |
3459 | else { |
3460 | /* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */ | |
3461 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
3462 | return -1ULL; | |
3463 | } | |
2d21ac55 A |
3464 | #endif /* __APPLE__ */ |
3465 | ||
3466 | #if !defined(__APPLE__) | |
3467 | case DIF_VAR_ERRNO: { | |
3468 | klwp_t *lwp; | |
3469 | if (!dtrace_priv_proc(state)) | |
3470 | return (0); | |
3471 | ||
3472 | /* | |
3473 | * See comment in DIF_VAR_PID. | |
3474 | */ | |
3475 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3476 | return (0); | |
3477 | ||
b0d623f7 A |
3478 | /* |
3479 | * It is always safe to dereference one's own t_lwp pointer in | |
3480 | * the event that this pointer is non-NULL. (This is true | |
3481 | * because threads and lwps don't clean up their own state -- | |
3482 | * they leave that task to whomever reaps them.) | |
3483 | */ | |
2d21ac55 A |
3484 | if ((lwp = curthread->t_lwp) == NULL) |
3485 | return (0); | |
3486 | ||
3487 | return ((uint64_t)lwp->lwp_errno); | |
3488 | } | |
3489 | #else | |
3490 | case DIF_VAR_ERRNO: { | |
3491 | uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread()); | |
3492 | if (!dtrace_priv_proc(state)) | |
3493 | return (0); | |
3494 | ||
3495 | /* | |
3496 | * See comment in DIF_VAR_PID. | |
3497 | */ | |
3498 | if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) | |
3499 | return (0); | |
3500 | ||
b0d623f7 A |
3501 | if (uthread) |
3502 | return (uint64_t)uthread->t_dtrace_errno; | |
3503 | else { | |
3504 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
3505 | return -1ULL; | |
3506 | } | |
2d21ac55 A |
3507 | } |
3508 | #endif /* __APPLE__ */ | |
3509 | ||
3510 | default: | |
3511 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
3512 | return (0); | |
3513 | } | |
3514 | } | |
3515 | ||
3516 | /* | |
3517 | * Emulate the execution of DTrace ID subroutines invoked by the call opcode. | |
3518 | * Notice that we don't bother validating the proper number of arguments or | |
3519 | * their types in the tuple stack. This isn't needed because all argument | |
3520 | * interpretation is safe because of our load safety -- the worst that can | |
3521 | * happen is that a bogus program can obtain bogus results. | |
3522 | */ | |
3523 | static void | |
3524 | dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, | |
3525 | dtrace_key_t *tupregs, int nargs, | |
3526 | dtrace_mstate_t *mstate, dtrace_state_t *state) | |
3527 | { | |
3528 | volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
3529 | #if !defined(__APPLE__) | |
3530 | volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
3531 | #else | |
3532 | volatile uint64_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
3533 | #endif /* __APPLE__ */ | |
b0d623f7 | 3534 | dtrace_vstate_t *vstate = &state->dts_vstate; |
2d21ac55 A |
3535 | |
3536 | #if !defined(__APPLE__) | |
3537 | union { | |
3538 | mutex_impl_t mi; | |
3539 | uint64_t mx; | |
3540 | } m; | |
3541 | ||
3542 | union { | |
3543 | krwlock_t ri; | |
3544 | uintptr_t rw; | |
3545 | } r; | |
3546 | #else | |
b0d623f7 | 3547 | /* FIXME: awaits lock/mutex work */ |
2d21ac55 A |
3548 | #endif /* __APPLE__ */ |
3549 | ||
3550 | switch (subr) { | |
3551 | case DIF_SUBR_RAND: | |
3552 | regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; | |
3553 | break; | |
3554 | ||
3555 | #if !defined(__APPLE__) | |
3556 | case DIF_SUBR_MUTEX_OWNED: | |
b0d623f7 A |
3557 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), |
3558 | mstate, vstate)) { | |
3559 | regs[rd] = NULL; | |
3560 | break; | |
3561 | } | |
3562 | ||
2d21ac55 A |
3563 | m.mx = dtrace_load64(tupregs[0].dttk_value); |
3564 | if (MUTEX_TYPE_ADAPTIVE(&m.mi)) | |
3565 | regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; | |
3566 | else | |
3567 | regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); | |
3568 | break; | |
3569 | ||
3570 | case DIF_SUBR_MUTEX_OWNER: | |
b0d623f7 A |
3571 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), |
3572 | mstate, vstate)) { | |
3573 | regs[rd] = NULL; | |
3574 | break; | |
3575 | } | |
3576 | ||
2d21ac55 A |
3577 | m.mx = dtrace_load64(tupregs[0].dttk_value); |
3578 | if (MUTEX_TYPE_ADAPTIVE(&m.mi) && | |
3579 | MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) | |
3580 | regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); | |
3581 | else | |
3582 | regs[rd] = 0; | |
3583 | break; | |
3584 | ||
3585 | case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: | |
b0d623f7 A |
3586 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), |
3587 | mstate, vstate)) { | |
3588 | regs[rd] = NULL; | |
3589 | break; | |
3590 | } | |
3591 | ||
2d21ac55 A |
3592 | m.mx = dtrace_load64(tupregs[0].dttk_value); |
3593 | regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); | |
3594 | break; | |
3595 | ||
3596 | case DIF_SUBR_MUTEX_TYPE_SPIN: | |
b0d623f7 A |
3597 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), |
3598 | mstate, vstate)) { | |
3599 | regs[rd] = NULL; | |
3600 | break; | |
3601 | } | |
3602 | ||
2d21ac55 A |
3603 | m.mx = dtrace_load64(tupregs[0].dttk_value); |
3604 | regs[rd] = MUTEX_TYPE_SPIN(&m.mi); | |
3605 | break; | |
3606 | ||
3607 | case DIF_SUBR_RW_READ_HELD: { | |
3608 | uintptr_t tmp; | |
3609 | ||
b0d623f7 A |
3610 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), |
3611 | mstate, vstate)) { | |
3612 | regs[rd] = NULL; | |
3613 | break; | |
3614 | } | |
3615 | ||
2d21ac55 A |
3616 | r.rw = dtrace_loadptr(tupregs[0].dttk_value); |
3617 | regs[rd] = _RW_READ_HELD(&r.ri, tmp); | |
3618 | break; | |
3619 | } | |
3620 | ||
3621 | case DIF_SUBR_RW_WRITE_HELD: | |
b0d623f7 A |
3622 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), |
3623 | mstate, vstate)) { | |
3624 | regs[rd] = NULL; | |
3625 | break; | |
3626 | } | |
3627 | ||
2d21ac55 A |
3628 | r.rw = dtrace_loadptr(tupregs[0].dttk_value); |
3629 | regs[rd] = _RW_WRITE_HELD(&r.ri); | |
3630 | break; | |
3631 | ||
3632 | case DIF_SUBR_RW_ISWRITER: | |
b0d623f7 A |
3633 | if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), |
3634 | mstate, vstate)) { | |
3635 | regs[rd] = NULL; | |
3636 | break; | |
3637 | } | |
3638 | ||
2d21ac55 A |
3639 | r.rw = dtrace_loadptr(tupregs[0].dttk_value); |
3640 | regs[rd] = _RW_ISWRITER(&r.ri); | |
3641 | break; | |
3642 | #else | |
b0d623f7 | 3643 | /* FIXME: awaits lock/mutex work */ |
2d21ac55 A |
3644 | #endif /* __APPLE__ */ |
3645 | ||
3646 | case DIF_SUBR_BCOPY: { | |
3647 | /* | |
3648 | * We need to be sure that the destination is in the scratch | |
3649 | * region -- no other region is allowed. | |
3650 | */ | |
3651 | uintptr_t src = tupregs[0].dttk_value; | |
3652 | uintptr_t dest = tupregs[1].dttk_value; | |
3653 | size_t size = tupregs[2].dttk_value; | |
3654 | ||
3655 | if (!dtrace_inscratch(dest, size, mstate)) { | |
3656 | *flags |= CPU_DTRACE_BADADDR; | |
3657 | *illval = regs[rd]; | |
3658 | break; | |
3659 | } | |
3660 | ||
b0d623f7 A |
3661 | if (!dtrace_canload(src, size, mstate, vstate)) { |
3662 | regs[rd] = NULL; | |
3663 | break; | |
3664 | } | |
3665 | ||
2d21ac55 A |
3666 | dtrace_bcopy((void *)src, (void *)dest, size); |
3667 | break; | |
3668 | } | |
3669 | ||
3670 | case DIF_SUBR_ALLOCA: | |
3671 | case DIF_SUBR_COPYIN: { | |
3672 | uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); | |
3673 | uint64_t size = | |
3674 | tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; | |
3675 | size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; | |
3676 | ||
3677 | /* | |
3678 | * This action doesn't require any credential checks since | |
3679 | * probes will not activate in user contexts to which the | |
3680 | * enabling user does not have permissions. | |
3681 | */ | |
b0d623f7 A |
3682 | |
3683 | /* | |
3684 | * Rounding up the user allocation size could have overflowed | |
3685 | * a large, bogus allocation (like -1ULL) to 0. | |
3686 | */ | |
3687 | if (scratch_size < size || | |
3688 | !DTRACE_INSCRATCH(mstate, scratch_size)) { | |
2d21ac55 A |
3689 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
3690 | regs[rd] = NULL; | |
3691 | break; | |
3692 | } | |
3693 | ||
3694 | if (subr == DIF_SUBR_COPYIN) { | |
3695 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
cf7d32b8 | 3696 | #if !defined(__APPLE__) |
b0d623f7 | 3697 | dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); |
cf7d32b8 A |
3698 | #else |
3699 | if (dtrace_priv_proc(state)) | |
b0d623f7 | 3700 | dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); |
cf7d32b8 | 3701 | #endif /* __APPLE__ */ |
2d21ac55 A |
3702 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3703 | } | |
3704 | ||
3705 | mstate->dtms_scratch_ptr += scratch_size; | |
3706 | regs[rd] = dest; | |
3707 | break; | |
3708 | } | |
3709 | ||
3710 | case DIF_SUBR_COPYINTO: { | |
3711 | uint64_t size = tupregs[1].dttk_value; | |
3712 | uintptr_t dest = tupregs[2].dttk_value; | |
3713 | ||
3714 | /* | |
3715 | * This action doesn't require any credential checks since | |
3716 | * probes will not activate in user contexts to which the | |
3717 | * enabling user does not have permissions. | |
3718 | */ | |
3719 | if (!dtrace_inscratch(dest, size, mstate)) { | |
3720 | *flags |= CPU_DTRACE_BADADDR; | |
3721 | *illval = regs[rd]; | |
3722 | break; | |
3723 | } | |
3724 | ||
3725 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
cf7d32b8 | 3726 | #if !defined(__APPLE__) |
b0d623f7 | 3727 | dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); |
cf7d32b8 A |
3728 | #else |
3729 | if (dtrace_priv_proc(state)) | |
b0d623f7 | 3730 | dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); |
cf7d32b8 | 3731 | #endif /* __APPLE__ */ |
2d21ac55 A |
3732 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3733 | break; | |
3734 | } | |
3735 | ||
3736 | case DIF_SUBR_COPYINSTR: { | |
3737 | uintptr_t dest = mstate->dtms_scratch_ptr; | |
3738 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
3739 | ||
3740 | if (nargs > 1 && tupregs[1].dttk_value < size) | |
3741 | size = tupregs[1].dttk_value + 1; | |
3742 | ||
3743 | /* | |
3744 | * This action doesn't require any credential checks since | |
3745 | * probes will not activate in user contexts to which the | |
3746 | * enabling user does not have permissions. | |
3747 | */ | |
b0d623f7 | 3748 | if (!DTRACE_INSCRATCH(mstate, size)) { |
2d21ac55 A |
3749 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
3750 | regs[rd] = NULL; | |
3751 | break; | |
3752 | } | |
3753 | ||
3754 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
cf7d32b8 | 3755 | #if !defined(__APPLE__) |
b0d623f7 | 3756 | dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); |
cf7d32b8 A |
3757 | #else |
3758 | if (dtrace_priv_proc(state)) | |
b0d623f7 | 3759 | dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); |
cf7d32b8 | 3760 | #endif /* __APPLE__ */ |
2d21ac55 A |
3761 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3762 | ||
3763 | ((char *)dest)[size - 1] = '\0'; | |
3764 | mstate->dtms_scratch_ptr += size; | |
3765 | regs[rd] = dest; | |
3766 | break; | |
3767 | } | |
3768 | ||
3769 | #if !defined(__APPLE__) | |
3770 | case DIF_SUBR_MSGSIZE: | |
3771 | case DIF_SUBR_MSGDSIZE: { | |
3772 | uintptr_t baddr = tupregs[0].dttk_value, daddr; | |
3773 | uintptr_t wptr, rptr; | |
3774 | size_t count = 0; | |
3775 | int cont = 0; | |
3776 | ||
3777 | while (baddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { | |
b0d623f7 A |
3778 | |
3779 | if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, | |
3780 | vstate)) { | |
3781 | regs[rd] = NULL; | |
3782 | break; | |
3783 | } | |
3784 | ||
2d21ac55 A |
3785 | wptr = dtrace_loadptr(baddr + |
3786 | offsetof(mblk_t, b_wptr)); | |
3787 | ||
3788 | rptr = dtrace_loadptr(baddr + | |
3789 | offsetof(mblk_t, b_rptr)); | |
3790 | ||
3791 | if (wptr < rptr) { | |
3792 | *flags |= CPU_DTRACE_BADADDR; | |
3793 | *illval = tupregs[0].dttk_value; | |
3794 | break; | |
3795 | } | |
3796 | ||
3797 | daddr = dtrace_loadptr(baddr + | |
3798 | offsetof(mblk_t, b_datap)); | |
3799 | ||
3800 | baddr = dtrace_loadptr(baddr + | |
3801 | offsetof(mblk_t, b_cont)); | |
3802 | ||
3803 | /* | |
3804 | * We want to prevent against denial-of-service here, | |
3805 | * so we're only going to search the list for | |
3806 | * dtrace_msgdsize_max mblks. | |
3807 | */ | |
3808 | if (cont++ > dtrace_msgdsize_max) { | |
3809 | *flags |= CPU_DTRACE_ILLOP; | |
3810 | break; | |
3811 | } | |
3812 | ||
3813 | if (subr == DIF_SUBR_MSGDSIZE) { | |
3814 | if (dtrace_load8(daddr + | |
3815 | offsetof(dblk_t, db_type)) != M_DATA) | |
3816 | continue; | |
3817 | } | |
3818 | ||
3819 | count += wptr - rptr; | |
3820 | } | |
3821 | ||
3822 | if (!(*flags & CPU_DTRACE_FAULT)) | |
3823 | regs[rd] = count; | |
3824 | ||
3825 | break; | |
3826 | } | |
3827 | #else | |
3828 | case DIF_SUBR_MSGSIZE: | |
3829 | case DIF_SUBR_MSGDSIZE: { | |
3830 | /* Darwin does not implement SysV streams messages */ | |
b0d623f7 | 3831 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); |
2d21ac55 A |
3832 | regs[rd] = 0; |
3833 | break; | |
3834 | } | |
3835 | #endif /* __APPLE__ */ | |
3836 | ||
3837 | #if !defined(__APPLE__) | |
3838 | case DIF_SUBR_PROGENYOF: { | |
3839 | pid_t pid = tupregs[0].dttk_value; | |
3840 | proc_t *p; | |
3841 | int rval = 0; | |
3842 | ||
3843 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
3844 | ||
3845 | for (p = curthread->t_procp; p != NULL; p = p->p_parent) { | |
3846 | if (p->p_pidp->pid_id == pid) { | |
3847 | rval = 1; | |
3848 | break; | |
3849 | } | |
3850 | } | |
3851 | ||
3852 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); | |
3853 | ||
3854 | regs[rd] = rval; | |
3855 | break; | |
3856 | } | |
3857 | #else | |
3858 | case DIF_SUBR_PROGENYOF: { | |
3859 | pid_t pid = tupregs[0].dttk_value; | |
3860 | struct proc *p = current_proc(); | |
3861 | int rval = 0, lim = nprocs; | |
3862 | ||
3863 | while(p && (lim-- > 0)) { | |
3864 | pid_t ppid; | |
3865 | ||
3866 | ppid = (pid_t)dtrace_load32((uintptr_t)&(p->p_pid)); | |
3867 | if (*flags & CPU_DTRACE_FAULT) | |
3868 | break; | |
3869 | ||
3870 | if (ppid == pid) { | |
3871 | rval = 1; | |
3872 | break; | |
3873 | } | |
3874 | ||
3875 | if (ppid == 0) | |
3876 | break; /* Can't climb process tree any further. */ | |
3877 | ||
3878 | p = (struct proc *)dtrace_loadptr((uintptr_t)&(p->p_pptr)); | |
3879 | if (*flags & CPU_DTRACE_FAULT) | |
3880 | break; | |
3881 | } | |
3882 | ||
3883 | regs[rd] = rval; | |
3884 | break; | |
3885 | } | |
3886 | #endif /* __APPLE__ */ | |
3887 | ||
3888 | case DIF_SUBR_SPECULATION: | |
3889 | regs[rd] = dtrace_speculation(state); | |
3890 | break; | |
3891 | ||
3892 | #if !defined(__APPLE__) | |
3893 | case DIF_SUBR_COPYOUT: { | |
3894 | uintptr_t kaddr = tupregs[0].dttk_value; | |
3895 | uintptr_t uaddr = tupregs[1].dttk_value; | |
3896 | uint64_t size = tupregs[2].dttk_value; | |
3897 | ||
3898 | if (!dtrace_destructive_disallow && | |
3899 | dtrace_priv_proc_control(state) && | |
3900 | !dtrace_istoxic(kaddr, size)) { | |
3901 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
b0d623f7 | 3902 | dtrace_copyout(kaddr, uaddr, size, flags); |
2d21ac55 A |
3903 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3904 | } | |
3905 | break; | |
3906 | } | |
3907 | ||
3908 | case DIF_SUBR_COPYOUTSTR: { | |
3909 | uintptr_t kaddr = tupregs[0].dttk_value; | |
3910 | uintptr_t uaddr = tupregs[1].dttk_value; | |
3911 | uint64_t size = tupregs[2].dttk_value; | |
3912 | ||
3913 | if (!dtrace_destructive_disallow && | |
3914 | dtrace_priv_proc_control(state) && | |
3915 | !dtrace_istoxic(kaddr, size)) { | |
3916 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
b0d623f7 | 3917 | dtrace_copyoutstr(kaddr, uaddr, size, flags); |
2d21ac55 A |
3918 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3919 | } | |
3920 | break; | |
3921 | } | |
3922 | #else | |
3923 | case DIF_SUBR_COPYOUT: { | |
3924 | uintptr_t kaddr = tupregs[0].dttk_value; | |
3925 | user_addr_t uaddr = tupregs[1].dttk_value; | |
3926 | uint64_t size = tupregs[2].dttk_value; | |
3927 | ||
3928 | if (!dtrace_destructive_disallow && | |
3929 | dtrace_priv_proc_control(state) && | |
3930 | !dtrace_istoxic(kaddr, size)) { | |
3931 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
b0d623f7 | 3932 | dtrace_copyout(kaddr, uaddr, size, flags); |
2d21ac55 A |
3933 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3934 | } | |
3935 | break; | |
3936 | } | |
3937 | ||
3938 | case DIF_SUBR_COPYOUTSTR: { | |
3939 | uintptr_t kaddr = tupregs[0].dttk_value; | |
3940 | user_addr_t uaddr = tupregs[1].dttk_value; | |
3941 | uint64_t size = tupregs[2].dttk_value; | |
3942 | ||
3943 | if (!dtrace_destructive_disallow && | |
3944 | dtrace_priv_proc_control(state) && | |
3945 | !dtrace_istoxic(kaddr, size)) { | |
3946 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
b0d623f7 | 3947 | dtrace_copyoutstr(kaddr, uaddr, size, flags); |
2d21ac55 A |
3948 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); |
3949 | } | |
3950 | break; | |
3951 | } | |
3952 | #endif /* __APPLE__ */ | |
3953 | ||
b0d623f7 A |
3954 | case DIF_SUBR_STRLEN: { |
3955 | size_t sz; | |
3956 | uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; | |
3957 | sz = dtrace_strlen((char *)addr, | |
2d21ac55 | 3958 | state->dts_options[DTRACEOPT_STRSIZE]); |
b0d623f7 A |
3959 | |
3960 | if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { | |
3961 | regs[rd] = NULL; | |
3962 | break; | |
3963 | } | |
3964 | ||
3965 | regs[rd] = sz; | |
3966 | ||
2d21ac55 | 3967 | break; |
b0d623f7 | 3968 | } |
2d21ac55 A |
3969 | |
3970 | case DIF_SUBR_STRCHR: | |
3971 | case DIF_SUBR_STRRCHR: { | |
3972 | /* | |
3973 | * We're going to iterate over the string looking for the | |
3974 | * specified character. We will iterate until we have reached | |
3975 | * the string length or we have found the character. If this | |
3976 | * is DIF_SUBR_STRRCHR, we will look for the last occurrence | |
3977 | * of the specified character instead of the first. | |
3978 | */ | |
b0d623f7 | 3979 | uintptr_t saddr = tupregs[0].dttk_value; |
2d21ac55 A |
3980 | uintptr_t addr = tupregs[0].dttk_value; |
3981 | uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; | |
3982 | char c, target = (char)tupregs[1].dttk_value; | |
3983 | ||
3984 | for (regs[rd] = NULL; addr < limit; addr++) { | |
3985 | if ((c = dtrace_load8(addr)) == target) { | |
3986 | regs[rd] = addr; | |
3987 | ||
3988 | if (subr == DIF_SUBR_STRCHR) | |
3989 | break; | |
3990 | } | |
3991 | ||
3992 | if (c == '\0') | |
3993 | break; | |
3994 | } | |
3995 | ||
b0d623f7 A |
3996 | if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { |
3997 | regs[rd] = NULL; | |
3998 | break; | |
3999 | } | |
4000 | ||
2d21ac55 A |
4001 | break; |
4002 | } | |
4003 | ||
4004 | case DIF_SUBR_STRSTR: | |
4005 | case DIF_SUBR_INDEX: | |
4006 | case DIF_SUBR_RINDEX: { | |
4007 | /* | |
4008 | * We're going to iterate over the string looking for the | |
4009 | * specified string. We will iterate until we have reached | |
4010 | * the string length or we have found the string. (Yes, this | |
4011 | * is done in the most naive way possible -- but considering | |
4012 | * that the string we're searching for is likely to be | |
4013 | * relatively short, the complexity of Rabin-Karp or similar | |
4014 | * hardly seems merited.) | |
4015 | */ | |
4016 | char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; | |
4017 | char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; | |
4018 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4019 | size_t len = dtrace_strlen(addr, size); | |
4020 | size_t sublen = dtrace_strlen(substr, size); | |
4021 | char *limit = addr + len, *orig = addr; | |
4022 | int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; | |
4023 | int inc = 1; | |
4024 | ||
4025 | regs[rd] = notfound; | |
4026 | ||
b0d623f7 A |
4027 | if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { |
4028 | regs[rd] = NULL; | |
4029 | break; | |
4030 | } | |
4031 | ||
4032 | if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, | |
4033 | vstate)) { | |
4034 | regs[rd] = NULL; | |
4035 | break; | |
4036 | } | |
4037 | ||
2d21ac55 A |
4038 | /* |
4039 | * strstr() and index()/rindex() have similar semantics if | |
4040 | * both strings are the empty string: strstr() returns a | |
4041 | * pointer to the (empty) string, and index() and rindex() | |
4042 | * both return index 0 (regardless of any position argument). | |
4043 | */ | |
4044 | if (sublen == 0 && len == 0) { | |
4045 | if (subr == DIF_SUBR_STRSTR) | |
4046 | regs[rd] = (uintptr_t)addr; | |
4047 | else | |
4048 | regs[rd] = 0; | |
4049 | break; | |
4050 | } | |
4051 | ||
4052 | if (subr != DIF_SUBR_STRSTR) { | |
4053 | if (subr == DIF_SUBR_RINDEX) { | |
4054 | limit = orig - 1; | |
4055 | addr += len; | |
4056 | inc = -1; | |
4057 | } | |
4058 | ||
4059 | /* | |
4060 | * Both index() and rindex() take an optional position | |
4061 | * argument that denotes the starting position. | |
4062 | */ | |
4063 | if (nargs == 3) { | |
4064 | int64_t pos = (int64_t)tupregs[2].dttk_value; | |
4065 | ||
4066 | /* | |
4067 | * If the position argument to index() is | |
4068 | * negative, Perl implicitly clamps it at | |
4069 | * zero. This semantic is a little surprising | |
4070 | * given the special meaning of negative | |
4071 | * positions to similar Perl functions like | |
4072 | * substr(), but it appears to reflect a | |
4073 | * notion that index() can start from a | |
4074 | * negative index and increment its way up to | |
4075 | * the string. Given this notion, Perl's | |
4076 | * rindex() is at least self-consistent in | |
4077 | * that it implicitly clamps positions greater | |
4078 | * than the string length to be the string | |
4079 | * length. Where Perl completely loses | |
4080 | * coherence, however, is when the specified | |
4081 | * substring is the empty string (""). In | |
4082 | * this case, even if the position is | |
4083 | * negative, rindex() returns 0 -- and even if | |
4084 | * the position is greater than the length, | |
4085 | * index() returns the string length. These | |
4086 | * semantics violate the notion that index() | |
4087 | * should never return a value less than the | |
4088 | * specified position and that rindex() should | |
4089 | * never return a value greater than the | |
4090 | * specified position. (One assumes that | |
4091 | * these semantics are artifacts of Perl's | |
4092 | * implementation and not the results of | |
4093 | * deliberate design -- it beggars belief that | |
4094 | * even Larry Wall could desire such oddness.) | |
4095 | * While in the abstract one would wish for | |
4096 | * consistent position semantics across | |
4097 | * substr(), index() and rindex() -- or at the | |
4098 | * very least self-consistent position | |
4099 | * semantics for index() and rindex() -- we | |
4100 | * instead opt to keep with the extant Perl | |
4101 | * semantics, in all their broken glory. (Do | |
4102 | * we have more desire to maintain Perl's | |
4103 | * semantics than Perl does? Probably.) | |
4104 | */ | |
4105 | if (subr == DIF_SUBR_RINDEX) { | |
4106 | if (pos < 0) { | |
4107 | if (sublen == 0) | |
4108 | regs[rd] = 0; | |
4109 | break; | |
4110 | } | |
4111 | ||
b0d623f7 | 4112 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 4113 | if (pos > len) |
b0d623f7 A |
4114 | #else |
4115 | if ((size_t)pos > len) | |
4116 | #endif /* __APPLE__ */ | |
2d21ac55 A |
4117 | pos = len; |
4118 | } else { | |
4119 | if (pos < 0) | |
4120 | pos = 0; | |
4121 | ||
b0d623f7 | 4122 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 4123 | if (pos >= len) { |
b0d623f7 A |
4124 | #else |
4125 | if ((size_t)pos >= len) { | |
4126 | #endif /* __APPLE__ */ | |
2d21ac55 A |
4127 | if (sublen == 0) |
4128 | regs[rd] = len; | |
4129 | break; | |
4130 | } | |
4131 | } | |
4132 | ||
4133 | addr = orig + pos; | |
4134 | } | |
4135 | } | |
4136 | ||
4137 | for (regs[rd] = notfound; addr != limit; addr += inc) { | |
4138 | if (dtrace_strncmp(addr, substr, sublen) == 0) { | |
4139 | if (subr != DIF_SUBR_STRSTR) { | |
4140 | /* | |
4141 | * As D index() and rindex() are | |
4142 | * modeled on Perl (and not on awk), | |
4143 | * we return a zero-based (and not a | |
4144 | * one-based) index. (For you Perl | |
4145 | * weenies: no, we're not going to add | |
4146 | * $[ -- and shouldn't you be at a con | |
4147 | * or something?) | |
4148 | */ | |
4149 | regs[rd] = (uintptr_t)(addr - orig); | |
4150 | break; | |
4151 | } | |
4152 | ||
4153 | ASSERT(subr == DIF_SUBR_STRSTR); | |
4154 | regs[rd] = (uintptr_t)addr; | |
4155 | break; | |
4156 | } | |
4157 | } | |
4158 | ||
4159 | break; | |
4160 | } | |
4161 | ||
4162 | case DIF_SUBR_STRTOK: { | |
4163 | uintptr_t addr = tupregs[0].dttk_value; | |
4164 | uintptr_t tokaddr = tupregs[1].dttk_value; | |
4165 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4166 | uintptr_t limit, toklimit = tokaddr + size; | |
2d21ac55 | 4167 | char *dest = (char *)mstate->dtms_scratch_ptr; |
b0d623f7 A |
4168 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
4169 | uint8_t c, tokmap[32]; /* 256 / 8 */ | |
2d21ac55 | 4170 | int i; |
b0d623f7 A |
4171 | #else |
4172 | uint8_t c='\0', tokmap[32]; /* 256 / 8 */ | |
4173 | uint64_t i = 0; | |
4174 | #endif /* __APPLE__ */ | |
4175 | ||
4176 | /* | |
4177 | * Check both the token buffer and (later) the input buffer, | |
4178 | * since both could be non-scratch addresses. | |
4179 | */ | |
4180 | if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { | |
4181 | regs[rd] = NULL; | |
4182 | break; | |
4183 | } | |
2d21ac55 | 4184 | |
b0d623f7 | 4185 | if (!DTRACE_INSCRATCH(mstate, size)) { |
2d21ac55 A |
4186 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4187 | regs[rd] = NULL; | |
4188 | break; | |
4189 | } | |
4190 | ||
4191 | if (addr == NULL) { | |
4192 | /* | |
4193 | * If the address specified is NULL, we use our saved | |
4194 | * strtok pointer from the mstate. Note that this | |
4195 | * means that the saved strtok pointer is _only_ | |
4196 | * valid within multiple enablings of the same probe -- | |
4197 | * it behaves like an implicit clause-local variable. | |
4198 | */ | |
4199 | addr = mstate->dtms_strtok; | |
b0d623f7 A |
4200 | } else { |
4201 | /* | |
4202 | * If the user-specified address is non-NULL we must | |
4203 | * access check it. This is the only time we have | |
4204 | * a chance to do so, since this address may reside | |
4205 | * in the string table of this clause-- future calls | |
4206 | * (when we fetch addr from mstate->dtms_strtok) | |
4207 | * would fail this access check. | |
4208 | */ | |
4209 | if (!dtrace_strcanload(addr, size, mstate, vstate)) { | |
4210 | regs[rd] = NULL; | |
4211 | break; | |
4212 | } | |
2d21ac55 A |
4213 | } |
4214 | ||
4215 | /* | |
4216 | * First, zero the token map, and then process the token | |
4217 | * string -- setting a bit in the map for every character | |
4218 | * found in the token string. | |
4219 | */ | |
c910b4d9 | 4220 | for (i = 0; i < (int)sizeof (tokmap); i++) |
2d21ac55 A |
4221 | tokmap[i] = 0; |
4222 | ||
4223 | for (; tokaddr < toklimit; tokaddr++) { | |
4224 | if ((c = dtrace_load8(tokaddr)) == '\0') | |
4225 | break; | |
4226 | ||
4227 | ASSERT((c >> 3) < sizeof (tokmap)); | |
4228 | tokmap[c >> 3] |= (1 << (c & 0x7)); | |
4229 | } | |
4230 | ||
4231 | for (limit = addr + size; addr < limit; addr++) { | |
4232 | /* | |
4233 | * We're looking for a character that is _not_ contained | |
4234 | * in the token string. | |
4235 | */ | |
4236 | if ((c = dtrace_load8(addr)) == '\0') | |
4237 | break; | |
4238 | ||
4239 | if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) | |
4240 | break; | |
4241 | } | |
4242 | ||
4243 | if (c == '\0') { | |
4244 | /* | |
4245 | * We reached the end of the string without finding | |
4246 | * any character that was not in the token string. | |
4247 | * We return NULL in this case, and we set the saved | |
4248 | * address to NULL as well. | |
4249 | */ | |
4250 | regs[rd] = NULL; | |
4251 | mstate->dtms_strtok = NULL; | |
4252 | break; | |
4253 | } | |
4254 | ||
4255 | /* | |
4256 | * From here on, we're copying into the destination string. | |
4257 | */ | |
4258 | for (i = 0; addr < limit && i < size - 1; addr++) { | |
4259 | if ((c = dtrace_load8(addr)) == '\0') | |
4260 | break; | |
4261 | ||
4262 | if (tokmap[c >> 3] & (1 << (c & 0x7))) | |
4263 | break; | |
4264 | ||
4265 | ASSERT(i < size); | |
4266 | dest[i++] = c; | |
4267 | } | |
4268 | ||
4269 | ASSERT(i < size); | |
4270 | dest[i] = '\0'; | |
4271 | regs[rd] = (uintptr_t)dest; | |
4272 | mstate->dtms_scratch_ptr += size; | |
4273 | mstate->dtms_strtok = addr; | |
4274 | break; | |
4275 | } | |
4276 | ||
4277 | case DIF_SUBR_SUBSTR: { | |
4278 | uintptr_t s = tupregs[0].dttk_value; | |
4279 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4280 | char *d = (char *)mstate->dtms_scratch_ptr; | |
4281 | int64_t index = (int64_t)tupregs[1].dttk_value; | |
4282 | int64_t remaining = (int64_t)tupregs[2].dttk_value; | |
4283 | size_t len = dtrace_strlen((char *)s, size); | |
4284 | int64_t i = 0; | |
4285 | ||
b0d623f7 A |
4286 | if (!dtrace_canload(s, len + 1, mstate, vstate)) { |
4287 | regs[rd] = NULL; | |
4288 | break; | |
4289 | } | |
2d21ac55 | 4290 | |
b0d623f7 | 4291 | if (!DTRACE_INSCRATCH(mstate, size)) { |
2d21ac55 A |
4292 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4293 | regs[rd] = NULL; | |
4294 | break; | |
4295 | } | |
4296 | ||
b0d623f7 A |
4297 | if (nargs <= 2) |
4298 | remaining = (int64_t)size; | |
4299 | ||
2d21ac55 A |
4300 | if (index < 0) { |
4301 | index += len; | |
4302 | ||
4303 | if (index < 0 && index + remaining > 0) { | |
4304 | remaining += index; | |
4305 | index = 0; | |
4306 | } | |
4307 | } | |
4308 | ||
b0d623f7 A |
4309 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
4310 | if (index >= len || index < 0) { | |
4311 | remaining = 0; | |
4312 | } else if (remaining < 0) { | |
4313 | remaining += len - index; | |
4314 | } else if (index + remaining > size) { | |
4315 | remaining = size - index; | |
4316 | } | |
4317 | #else | |
4318 | if ((size_t)index >= len || index < 0) { | |
4319 | remaining = 0; | |
4320 | } else if (remaining < 0) { | |
4321 | remaining += len - index; | |
4322 | } else if ((uint64_t)index + (uint64_t)remaining > size) { | |
4323 | remaining = size - index; | |
4324 | } | |
4325 | #endif /* __APPLE__ */ | |
4326 | for (i = 0; i < remaining; i++) { | |
4327 | if ((d[i] = dtrace_load8(s + index + i)) == '\0') | |
2d21ac55 A |
4328 | break; |
4329 | } | |
b0d623f7 A |
4330 | |
4331 | d[i] = '\0'; | |
2d21ac55 A |
4332 | |
4333 | mstate->dtms_scratch_ptr += size; | |
4334 | regs[rd] = (uintptr_t)d; | |
4335 | break; | |
4336 | } | |
4337 | ||
4338 | #if !defined(__APPLE__) | |
4339 | case DIF_SUBR_GETMAJOR: | |
b0d623f7 | 4340 | #ifdef _LP64 |
2d21ac55 A |
4341 | regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; |
4342 | #else | |
4343 | regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; | |
4344 | #endif | |
4345 | break; | |
4346 | ||
4347 | #else /* __APPLE__ */ | |
4348 | case DIF_SUBR_GETMAJOR: | |
4349 | regs[rd] = (uintptr_t)major( (dev_t)tupregs[0].dttk_value ); | |
4350 | break; | |
4351 | #endif /* __APPLE__ */ | |
4352 | ||
4353 | #if !defined(__APPLE__) | |
4354 | case DIF_SUBR_GETMINOR: | |
b0d623f7 | 4355 | #ifdef _LP64 |
2d21ac55 A |
4356 | regs[rd] = tupregs[0].dttk_value & MAXMIN64; |
4357 | #else | |
4358 | regs[rd] = tupregs[0].dttk_value & MAXMIN; | |
4359 | #endif | |
4360 | break; | |
4361 | ||
4362 | #else /* __APPLE__ */ | |
4363 | case DIF_SUBR_GETMINOR: | |
4364 | regs[rd] = (uintptr_t)minor( (dev_t)tupregs[0].dttk_value ); | |
4365 | break; | |
4366 | #endif /* __APPLE__ */ | |
4367 | ||
4368 | #if !defined(__APPLE__) | |
4369 | case DIF_SUBR_DDI_PATHNAME: { | |
4370 | /* | |
4371 | * This one is a galactic mess. We are going to roughly | |
4372 | * emulate ddi_pathname(), but it's made more complicated | |
4373 | * by the fact that we (a) want to include the minor name and | |
4374 | * (b) must proceed iteratively instead of recursively. | |
4375 | */ | |
4376 | uintptr_t dest = mstate->dtms_scratch_ptr; | |
4377 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4378 | char *start = (char *)dest, *end = start + size - 1; | |
4379 | uintptr_t daddr = tupregs[0].dttk_value; | |
4380 | int64_t minor = (int64_t)tupregs[1].dttk_value; | |
4381 | char *s; | |
4382 | int i, len, depth = 0; | |
4383 | ||
b0d623f7 A |
4384 | /* |
4385 | * Due to all the pointer jumping we do and context we must | |
4386 | * rely upon, we just mandate that the user must have kernel | |
4387 | * read privileges to use this routine. | |
4388 | */ | |
4389 | if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { | |
4390 | *flags |= CPU_DTRACE_KPRIV; | |
4391 | *illval = daddr; | |
4392 | regs[rd] = NULL; | |
4393 | } | |
4394 | ||
4395 | if (!DTRACE_INSCRATCH(mstate, size)) { | |
2d21ac55 A |
4396 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4397 | regs[rd] = NULL; | |
4398 | break; | |
4399 | } | |
4400 | ||
4401 | *end = '\0'; | |
4402 | ||
4403 | /* | |
4404 | * We want to have a name for the minor. In order to do this, | |
4405 | * we need to walk the minor list from the devinfo. We want | |
4406 | * to be sure that we don't infinitely walk a circular list, | |
4407 | * so we check for circularity by sending a scout pointer | |
4408 | * ahead two elements for every element that we iterate over; | |
4409 | * if the list is circular, these will ultimately point to the | |
4410 | * same element. You may recognize this little trick as the | |
4411 | * answer to a stupid interview question -- one that always | |
4412 | * seems to be asked by those who had to have it laboriously | |
4413 | * explained to them, and who can't even concisely describe | |
4414 | * the conditions under which one would be forced to resort to | |
4415 | * this technique. Needless to say, those conditions are | |
b0d623f7 A |
4416 | * found here -- and probably only here. Is this the only use |
4417 | * of this infamous trick in shipping, production code? If it | |
4418 | * isn't, it probably should be... | |
2d21ac55 A |
4419 | */ |
4420 | if (minor != -1) { | |
4421 | uintptr_t maddr = dtrace_loadptr(daddr + | |
4422 | offsetof(struct dev_info, devi_minor)); | |
4423 | ||
4424 | uintptr_t next = offsetof(struct ddi_minor_data, next); | |
4425 | uintptr_t name = offsetof(struct ddi_minor_data, | |
4426 | d_minor) + offsetof(struct ddi_minor, name); | |
4427 | uintptr_t dev = offsetof(struct ddi_minor_data, | |
4428 | d_minor) + offsetof(struct ddi_minor, dev); | |
4429 | uintptr_t scout; | |
4430 | ||
4431 | if (maddr != NULL) | |
4432 | scout = dtrace_loadptr(maddr + next); | |
4433 | ||
4434 | while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { | |
4435 | uint64_t m; | |
b0d623f7 | 4436 | #ifdef _LP64 |
2d21ac55 A |
4437 | m = dtrace_load64(maddr + dev) & MAXMIN64; |
4438 | #else | |
4439 | m = dtrace_load32(maddr + dev) & MAXMIN; | |
4440 | #endif | |
4441 | if (m != minor) { | |
4442 | maddr = dtrace_loadptr(maddr + next); | |
4443 | ||
4444 | if (scout == NULL) | |
4445 | continue; | |
4446 | ||
4447 | scout = dtrace_loadptr(scout + next); | |
4448 | ||
4449 | if (scout == NULL) | |
4450 | continue; | |
4451 | ||
4452 | scout = dtrace_loadptr(scout + next); | |
4453 | ||
4454 | if (scout == NULL) | |
4455 | continue; | |
4456 | ||
4457 | if (scout == maddr) { | |
4458 | *flags |= CPU_DTRACE_ILLOP; | |
4459 | break; | |
4460 | } | |
4461 | ||
4462 | continue; | |
4463 | } | |
4464 | ||
4465 | /* | |
4466 | * We have the minor data. Now we need to | |
4467 | * copy the minor's name into the end of the | |
4468 | * pathname. | |
4469 | */ | |
4470 | s = (char *)dtrace_loadptr(maddr + name); | |
4471 | len = dtrace_strlen(s, size); | |
4472 | ||
4473 | if (*flags & CPU_DTRACE_FAULT) | |
4474 | break; | |
4475 | ||
4476 | if (len != 0) { | |
4477 | if ((end -= (len + 1)) < start) | |
4478 | break; | |
4479 | ||
4480 | *end = ':'; | |
4481 | } | |
4482 | ||
4483 | for (i = 1; i <= len; i++) | |
4484 | end[i] = dtrace_load8((uintptr_t)s++); | |
4485 | break; | |
4486 | } | |
4487 | } | |
4488 | ||
4489 | while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { | |
4490 | ddi_node_state_t devi_state; | |
4491 | ||
4492 | devi_state = dtrace_load32(daddr + | |
4493 | offsetof(struct dev_info, devi_node_state)); | |
4494 | ||
4495 | if (*flags & CPU_DTRACE_FAULT) | |
4496 | break; | |
4497 | ||
4498 | if (devi_state >= DS_INITIALIZED) { | |
4499 | s = (char *)dtrace_loadptr(daddr + | |
4500 | offsetof(struct dev_info, devi_addr)); | |
4501 | len = dtrace_strlen(s, size); | |
4502 | ||
4503 | if (*flags & CPU_DTRACE_FAULT) | |
4504 | break; | |
4505 | ||
4506 | if (len != 0) { | |
4507 | if ((end -= (len + 1)) < start) | |
4508 | break; | |
4509 | ||
4510 | *end = '@'; | |
4511 | } | |
4512 | ||
4513 | for (i = 1; i <= len; i++) | |
4514 | end[i] = dtrace_load8((uintptr_t)s++); | |
4515 | } | |
4516 | ||
4517 | /* | |
4518 | * Now for the node name... | |
4519 | */ | |
4520 | s = (char *)dtrace_loadptr(daddr + | |
4521 | offsetof(struct dev_info, devi_node_name)); | |
4522 | ||
4523 | daddr = dtrace_loadptr(daddr + | |
4524 | offsetof(struct dev_info, devi_parent)); | |
4525 | ||
4526 | /* | |
4527 | * If our parent is NULL (that is, if we're the root | |
4528 | * node), we're going to use the special path | |
4529 | * "devices". | |
4530 | */ | |
4531 | if (daddr == NULL) | |
4532 | s = "devices"; | |
4533 | ||
4534 | len = dtrace_strlen(s, size); | |
4535 | if (*flags & CPU_DTRACE_FAULT) | |
4536 | break; | |
4537 | ||
4538 | if ((end -= (len + 1)) < start) | |
4539 | break; | |
4540 | ||
4541 | for (i = 1; i <= len; i++) | |
4542 | end[i] = dtrace_load8((uintptr_t)s++); | |
4543 | *end = '/'; | |
4544 | ||
4545 | if (depth++ > dtrace_devdepth_max) { | |
4546 | *flags |= CPU_DTRACE_ILLOP; | |
4547 | break; | |
4548 | } | |
4549 | } | |
4550 | ||
4551 | if (end < start) | |
4552 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
4553 | ||
4554 | if (daddr == NULL) { | |
4555 | regs[rd] = (uintptr_t)end; | |
4556 | mstate->dtms_scratch_ptr += size; | |
4557 | } | |
4558 | ||
4559 | break; | |
4560 | } | |
4561 | #else | |
4562 | case DIF_SUBR_DDI_PATHNAME: { | |
b0d623f7 A |
4563 | /* FIXME: awaits galactic disentanglement ;-} */ |
4564 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
2d21ac55 A |
4565 | regs[rd] = NULL; |
4566 | break; | |
4567 | } | |
4568 | #endif /* __APPLE__ */ | |
4569 | ||
4570 | case DIF_SUBR_STRJOIN: { | |
4571 | char *d = (char *)mstate->dtms_scratch_ptr; | |
4572 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4573 | uintptr_t s1 = tupregs[0].dttk_value; | |
4574 | uintptr_t s2 = tupregs[1].dttk_value; | |
b0d623f7 | 4575 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 4576 | int i = 0; |
b0d623f7 A |
4577 | #else |
4578 | uint64_t i = 0; | |
4579 | #endif /* __APPLE__ */ | |
4580 | ||
4581 | if (!dtrace_strcanload(s1, size, mstate, vstate) || | |
4582 | !dtrace_strcanload(s2, size, mstate, vstate)) { | |
4583 | regs[rd] = NULL; | |
4584 | break; | |
4585 | } | |
2d21ac55 | 4586 | |
b0d623f7 | 4587 | if (!DTRACE_INSCRATCH(mstate, size)) { |
2d21ac55 A |
4588 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4589 | regs[rd] = NULL; | |
4590 | break; | |
4591 | } | |
4592 | ||
4593 | for (;;) { | |
4594 | if (i >= size) { | |
4595 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
4596 | regs[rd] = NULL; | |
4597 | break; | |
4598 | } | |
4599 | ||
4600 | if ((d[i++] = dtrace_load8(s1++)) == '\0') { | |
4601 | i--; | |
4602 | break; | |
4603 | } | |
4604 | } | |
4605 | ||
4606 | for (;;) { | |
4607 | if (i >= size) { | |
4608 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
4609 | regs[rd] = NULL; | |
4610 | break; | |
4611 | } | |
4612 | ||
4613 | if ((d[i++] = dtrace_load8(s2++)) == '\0') | |
4614 | break; | |
4615 | } | |
4616 | ||
4617 | if (i < size) { | |
4618 | mstate->dtms_scratch_ptr += i; | |
4619 | regs[rd] = (uintptr_t)d; | |
4620 | } | |
4621 | ||
4622 | break; | |
4623 | } | |
4624 | ||
4625 | case DIF_SUBR_LLTOSTR: { | |
4626 | int64_t i = (int64_t)tupregs[0].dttk_value; | |
4627 | int64_t val = i < 0 ? i * -1 : i; | |
4628 | uint64_t size = 22; /* enough room for 2^64 in decimal */ | |
4629 | char *end = (char *)mstate->dtms_scratch_ptr + size - 1; | |
4630 | ||
b0d623f7 | 4631 | if (!DTRACE_INSCRATCH(mstate, size)) { |
2d21ac55 A |
4632 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4633 | regs[rd] = NULL; | |
4634 | break; | |
4635 | } | |
4636 | ||
4637 | for (*end-- = '\0'; val; val /= 10) | |
4638 | *end-- = '0' + (val % 10); | |
4639 | ||
4640 | if (i == 0) | |
4641 | *end-- = '0'; | |
4642 | ||
4643 | if (i < 0) | |
4644 | *end-- = '-'; | |
4645 | ||
4646 | regs[rd] = (uintptr_t)end + 1; | |
4647 | mstate->dtms_scratch_ptr += size; | |
4648 | break; | |
4649 | } | |
4650 | ||
b0d623f7 A |
4651 | case DIF_SUBR_HTONS: |
4652 | case DIF_SUBR_NTOHS: | |
4653 | #ifdef _BIG_ENDIAN | |
4654 | regs[rd] = (uint16_t)tupregs[0].dttk_value; | |
4655 | #else | |
4656 | regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); | |
4657 | #endif | |
4658 | break; | |
4659 | ||
4660 | ||
4661 | case DIF_SUBR_HTONL: | |
4662 | case DIF_SUBR_NTOHL: | |
4663 | #ifdef _BIG_ENDIAN | |
4664 | regs[rd] = (uint32_t)tupregs[0].dttk_value; | |
4665 | #else | |
4666 | regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); | |
4667 | #endif | |
4668 | break; | |
4669 | ||
4670 | ||
4671 | case DIF_SUBR_HTONLL: | |
4672 | case DIF_SUBR_NTOHLL: | |
4673 | #ifdef _BIG_ENDIAN | |
4674 | regs[rd] = (uint64_t)tupregs[0].dttk_value; | |
4675 | #else | |
4676 | regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); | |
4677 | #endif | |
4678 | break; | |
4679 | ||
4680 | ||
2d21ac55 A |
4681 | case DIF_SUBR_DIRNAME: |
4682 | case DIF_SUBR_BASENAME: { | |
4683 | char *dest = (char *)mstate->dtms_scratch_ptr; | |
4684 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4685 | uintptr_t src = tupregs[0].dttk_value; | |
4686 | int i, j, len = dtrace_strlen((char *)src, size); | |
4687 | int lastbase = -1, firstbase = -1, lastdir = -1; | |
4688 | int start, end; | |
4689 | ||
b0d623f7 A |
4690 | if (!dtrace_canload(src, len + 1, mstate, vstate)) { |
4691 | regs[rd] = NULL; | |
4692 | break; | |
4693 | } | |
4694 | ||
4695 | if (!DTRACE_INSCRATCH(mstate, size)) { | |
2d21ac55 A |
4696 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4697 | regs[rd] = NULL; | |
4698 | break; | |
4699 | } | |
4700 | ||
4701 | /* | |
4702 | * The basename and dirname for a zero-length string is | |
4703 | * defined to be "." | |
4704 | */ | |
4705 | if (len == 0) { | |
4706 | len = 1; | |
4707 | src = (uintptr_t)"."; | |
4708 | } | |
4709 | ||
4710 | /* | |
4711 | * Start from the back of the string, moving back toward the | |
4712 | * front until we see a character that isn't a slash. That | |
4713 | * character is the last character in the basename. | |
4714 | */ | |
4715 | for (i = len - 1; i >= 0; i--) { | |
4716 | if (dtrace_load8(src + i) != '/') | |
4717 | break; | |
4718 | } | |
4719 | ||
4720 | if (i >= 0) | |
4721 | lastbase = i; | |
4722 | ||
4723 | /* | |
4724 | * Starting from the last character in the basename, move | |
4725 | * towards the front until we find a slash. The character | |
4726 | * that we processed immediately before that is the first | |
4727 | * character in the basename. | |
4728 | */ | |
4729 | for (; i >= 0; i--) { | |
4730 | if (dtrace_load8(src + i) == '/') | |
4731 | break; | |
4732 | } | |
4733 | ||
4734 | if (i >= 0) | |
4735 | firstbase = i + 1; | |
4736 | ||
4737 | /* | |
4738 | * Now keep going until we find a non-slash character. That | |
4739 | * character is the last character in the dirname. | |
4740 | */ | |
4741 | for (; i >= 0; i--) { | |
4742 | if (dtrace_load8(src + i) != '/') | |
4743 | break; | |
4744 | } | |
4745 | ||
4746 | if (i >= 0) | |
4747 | lastdir = i; | |
4748 | ||
4749 | ASSERT(!(lastbase == -1 && firstbase != -1)); | |
4750 | ASSERT(!(firstbase == -1 && lastdir != -1)); | |
4751 | ||
4752 | if (lastbase == -1) { | |
4753 | /* | |
4754 | * We didn't find a non-slash character. We know that | |
4755 | * the length is non-zero, so the whole string must be | |
4756 | * slashes. In either the dirname or the basename | |
4757 | * case, we return '/'. | |
4758 | */ | |
4759 | ASSERT(firstbase == -1); | |
4760 | firstbase = lastbase = lastdir = 0; | |
4761 | } | |
4762 | ||
4763 | if (firstbase == -1) { | |
4764 | /* | |
4765 | * The entire string consists only of a basename | |
4766 | * component. If we're looking for dirname, we need | |
4767 | * to change our string to be just "."; if we're | |
4768 | * looking for a basename, we'll just set the first | |
4769 | * character of the basename to be 0. | |
4770 | */ | |
4771 | if (subr == DIF_SUBR_DIRNAME) { | |
4772 | ASSERT(lastdir == -1); | |
4773 | src = (uintptr_t)"."; | |
4774 | lastdir = 0; | |
4775 | } else { | |
4776 | firstbase = 0; | |
4777 | } | |
4778 | } | |
4779 | ||
4780 | if (subr == DIF_SUBR_DIRNAME) { | |
4781 | if (lastdir == -1) { | |
4782 | /* | |
4783 | * We know that we have a slash in the name -- | |
4784 | * or lastdir would be set to 0, above. And | |
4785 | * because lastdir is -1, we know that this | |
4786 | * slash must be the first character. (That | |
4787 | * is, the full string must be of the form | |
4788 | * "/basename".) In this case, the last | |
4789 | * character of the directory name is 0. | |
4790 | */ | |
4791 | lastdir = 0; | |
4792 | } | |
4793 | ||
4794 | start = 0; | |
4795 | end = lastdir; | |
4796 | } else { | |
4797 | ASSERT(subr == DIF_SUBR_BASENAME); | |
4798 | ASSERT(firstbase != -1 && lastbase != -1); | |
4799 | start = firstbase; | |
4800 | end = lastbase; | |
4801 | } | |
4802 | ||
b0d623f7 | 4803 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
4804 | for (i = start, j = 0; i <= end && j < size - 1; i++, j++) |
4805 | dest[j] = dtrace_load8(src + i); | |
b0d623f7 A |
4806 | #else |
4807 | for (i = start, j = 0; i <= end && (uint64_t)j < size - 1; i++, j++) | |
4808 | dest[j] = dtrace_load8(src + i); | |
4809 | #endif /* __APPLE__ */ | |
2d21ac55 A |
4810 | |
4811 | dest[j] = '\0'; | |
4812 | regs[rd] = (uintptr_t)dest; | |
4813 | mstate->dtms_scratch_ptr += size; | |
4814 | break; | |
4815 | } | |
4816 | ||
4817 | case DIF_SUBR_CLEANPATH: { | |
4818 | char *dest = (char *)mstate->dtms_scratch_ptr, c; | |
4819 | uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; | |
4820 | uintptr_t src = tupregs[0].dttk_value; | |
4821 | int i = 0, j = 0; | |
4822 | ||
b0d623f7 A |
4823 | if (!dtrace_strcanload(src, size, mstate, vstate)) { |
4824 | regs[rd] = NULL; | |
4825 | break; | |
4826 | } | |
4827 | ||
4828 | if (!DTRACE_INSCRATCH(mstate, size)) { | |
2d21ac55 A |
4829 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
4830 | regs[rd] = NULL; | |
4831 | break; | |
4832 | } | |
4833 | ||
4834 | /* | |
4835 | * Move forward, loading each character. | |
4836 | */ | |
4837 | do { | |
4838 | c = dtrace_load8(src + i++); | |
4839 | next: | |
b0d623f7 | 4840 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
4841 | if (j + 5 >= size) /* 5 = strlen("/..c\0") */ |
4842 | break; | |
b0d623f7 A |
4843 | #else |
4844 | if ((uint64_t)(j + 5) >= size) /* 5 = strlen("/..c\0") */ | |
4845 | break; | |
4846 | #endif /* __APPLE__ */ | |
2d21ac55 A |
4847 | |
4848 | if (c != '/') { | |
4849 | dest[j++] = c; | |
4850 | continue; | |
4851 | } | |
4852 | ||
4853 | c = dtrace_load8(src + i++); | |
4854 | ||
4855 | if (c == '/') { | |
4856 | /* | |
4857 | * We have two slashes -- we can just advance | |
4858 | * to the next character. | |
4859 | */ | |
4860 | goto next; | |
4861 | } | |
4862 | ||
4863 | if (c != '.') { | |
4864 | /* | |
4865 | * This is not "." and it's not ".." -- we can | |
4866 | * just store the "/" and this character and | |
4867 | * drive on. | |
4868 | */ | |
4869 | dest[j++] = '/'; | |
4870 | dest[j++] = c; | |
4871 | continue; | |
4872 | } | |
4873 | ||
4874 | c = dtrace_load8(src + i++); | |
4875 | ||
4876 | if (c == '/') { | |
4877 | /* | |
4878 | * This is a "/./" component. We're not going | |
4879 | * to store anything in the destination buffer; | |
4880 | * we're just going to go to the next component. | |
4881 | */ | |
4882 | goto next; | |
4883 | } | |
4884 | ||
4885 | if (c != '.') { | |
4886 | /* | |
4887 | * This is not ".." -- we can just store the | |
4888 | * "/." and this character and continue | |
4889 | * processing. | |
4890 | */ | |
4891 | dest[j++] = '/'; | |
4892 | dest[j++] = '.'; | |
4893 | dest[j++] = c; | |
4894 | continue; | |
4895 | } | |
4896 | ||
4897 | c = dtrace_load8(src + i++); | |
4898 | ||
4899 | if (c != '/' && c != '\0') { | |
4900 | /* | |
4901 | * This is not ".." -- it's "..[mumble]". | |
4902 | * We'll store the "/.." and this character | |
4903 | * and continue processing. | |
4904 | */ | |
4905 | dest[j++] = '/'; | |
4906 | dest[j++] = '.'; | |
4907 | dest[j++] = '.'; | |
4908 | dest[j++] = c; | |
4909 | continue; | |
4910 | } | |
4911 | ||
4912 | /* | |
4913 | * This is "/../" or "/..\0". We need to back up | |
4914 | * our destination pointer until we find a "/". | |
4915 | */ | |
4916 | i--; | |
4917 | while (j != 0 && dest[--j] != '/') | |
4918 | continue; | |
4919 | ||
4920 | if (c == '\0') | |
4921 | dest[++j] = '/'; | |
4922 | } while (c != '\0'); | |
4923 | ||
4924 | dest[j] = '\0'; | |
4925 | regs[rd] = (uintptr_t)dest; | |
4926 | mstate->dtms_scratch_ptr += size; | |
4927 | break; | |
4928 | } | |
2d21ac55 | 4929 | |
b0d623f7 A |
4930 | case DIF_SUBR_INET_NTOA: |
4931 | case DIF_SUBR_INET_NTOA6: | |
4932 | case DIF_SUBR_INET_NTOP: { | |
4933 | size_t size; | |
4934 | int af, argi, i; | |
4935 | char *base, *end; | |
2d21ac55 | 4936 | |
b0d623f7 A |
4937 | if (subr == DIF_SUBR_INET_NTOP) { |
4938 | af = (int)tupregs[0].dttk_value; | |
4939 | argi = 1; | |
4940 | } else { | |
4941 | af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; | |
4942 | argi = 0; | |
2d21ac55 A |
4943 | } |
4944 | ||
b0d623f7 A |
4945 | if (af == AF_INET) { |
4946 | #if !defined(__APPLE__) | |
4947 | ipaddr_t ip4; | |
4948 | #else | |
4949 | in_addr_t ip4; | |
4950 | #endif /* __APPLE__ */ | |
4951 | uint8_t *ptr8, val; | |
4952 | ||
4953 | /* | |
4954 | * Safely load the IPv4 address. | |
4955 | */ | |
4956 | ip4 = dtrace_load32(tupregs[argi].dttk_value); | |
4957 | ||
4958 | /* | |
4959 | * Check an IPv4 string will fit in scratch. | |
4960 | */ | |
4961 | #if !defined(__APPLE__) | |
4962 | size = INET_ADDRSTRLEN; | |
4963 | #else | |
4964 | size = MAX_IPv4_STR_LEN; | |
4965 | #endif /* __APPLE__ */ | |
4966 | if (!DTRACE_INSCRATCH(mstate, size)) { | |
4967 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
4968 | regs[rd] = NULL; | |
4969 | break; | |
4970 | } | |
4971 | base = (char *)mstate->dtms_scratch_ptr; | |
4972 | end = (char *)mstate->dtms_scratch_ptr + size - 1; | |
4973 | ||
4974 | /* | |
4975 | * Stringify as a dotted decimal quad. | |
4976 | */ | |
4977 | *end-- = '\0'; | |
4978 | ptr8 = (uint8_t *)&ip4; | |
4979 | for (i = 3; i >= 0; i--) { | |
4980 | val = ptr8[i]; | |
4981 | ||
4982 | if (val == 0) { | |
4983 | *end-- = '0'; | |
4984 | } else { | |
4985 | for (; val; val /= 10) { | |
4986 | *end-- = '0' + (val % 10); | |
4987 | } | |
4988 | } | |
4989 | ||
4990 | if (i > 0) | |
4991 | *end-- = '.'; | |
4992 | } | |
4993 | ASSERT(end + 1 >= base); | |
4994 | ||
4995 | } else if (af == AF_INET6) { | |
4996 | #if defined(__APPLE__) | |
4997 | #define _S6_un __u6_addr | |
4998 | #define _S6_u8 __u6_addr8 | |
4999 | #endif /* __APPLE__ */ | |
5000 | struct in6_addr ip6; | |
5001 | int firstzero, tryzero, numzero, v6end; | |
5002 | uint16_t val; | |
5003 | const char digits[] = "0123456789abcdef"; | |
5004 | ||
5005 | /* | |
5006 | * Stringify using RFC 1884 convention 2 - 16 bit | |
5007 | * hexadecimal values with a zero-run compression. | |
5008 | * Lower case hexadecimal digits are used. | |
5009 | * eg, fe80::214:4fff:fe0b:76c8. | |
5010 | * The IPv4 embedded form is returned for inet_ntop, | |
5011 | * just the IPv4 string is returned for inet_ntoa6. | |
5012 | */ | |
5013 | ||
5014 | /* | |
5015 | * Safely load the IPv6 address. | |
5016 | */ | |
5017 | dtrace_bcopy( | |
5018 | (void *)(uintptr_t)tupregs[argi].dttk_value, | |
5019 | (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); | |
5020 | ||
5021 | /* | |
5022 | * Check an IPv6 string will fit in scratch. | |
5023 | */ | |
5024 | size = INET6_ADDRSTRLEN; | |
5025 | if (!DTRACE_INSCRATCH(mstate, size)) { | |
5026 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
5027 | regs[rd] = NULL; | |
5028 | break; | |
5029 | } | |
5030 | base = (char *)mstate->dtms_scratch_ptr; | |
5031 | end = (char *)mstate->dtms_scratch_ptr + size - 1; | |
5032 | *end-- = '\0'; | |
5033 | ||
5034 | /* | |
5035 | * Find the longest run of 16 bit zero values | |
5036 | * for the single allowed zero compression - "::". | |
5037 | */ | |
5038 | firstzero = -1; | |
5039 | tryzero = -1; | |
5040 | numzero = 1; | |
5041 | #if !defined(__APPLE__) /* Quiet compiler warnings */ | |
5042 | for (i = 0; i < sizeof (struct in6_addr); i++) { | |
5043 | #else | |
5044 | for (i = 0; i < (int)sizeof (struct in6_addr); i++) { | |
5045 | #endif /* __APPLE__ */ | |
5046 | if (ip6._S6_un._S6_u8[i] == 0 && | |
5047 | tryzero == -1 && i % 2 == 0) { | |
5048 | tryzero = i; | |
5049 | continue; | |
5050 | } | |
5051 | ||
5052 | if (tryzero != -1 && | |
5053 | (ip6._S6_un._S6_u8[i] != 0 || | |
5054 | i == sizeof (struct in6_addr) - 1)) { | |
5055 | ||
5056 | if (i - tryzero <= numzero) { | |
5057 | tryzero = -1; | |
5058 | continue; | |
5059 | } | |
5060 | ||
5061 | firstzero = tryzero; | |
5062 | numzero = i - i % 2 - tryzero; | |
5063 | tryzero = -1; | |
5064 | ||
5065 | if (ip6._S6_un._S6_u8[i] == 0 && | |
5066 | i == sizeof (struct in6_addr) - 1) | |
5067 | numzero += 2; | |
5068 | } | |
5069 | } | |
5070 | #if !defined(__APPLE__) /* Quiet compiler warnings */ | |
5071 | ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); | |
5072 | #else | |
5073 | ASSERT(firstzero + numzero <= (int)sizeof (struct in6_addr)); | |
5074 | #endif /* __APPLE__ */ | |
5075 | ||
5076 | /* | |
5077 | * Check for an IPv4 embedded address. | |
5078 | */ | |
5079 | v6end = sizeof (struct in6_addr) - 2; | |
5080 | if (IN6_IS_ADDR_V4MAPPED(&ip6) || | |
5081 | IN6_IS_ADDR_V4COMPAT(&ip6)) { | |
5082 | #if !defined(__APPLE__) /* Quiet compiler warnings */ | |
5083 | for (i = sizeof (struct in6_addr) - 1; | |
5084 | i >= DTRACE_V4MAPPED_OFFSET; i--) { | |
5085 | #else | |
5086 | for (i = sizeof (struct in6_addr) - 1; | |
5087 | i >= (int)DTRACE_V4MAPPED_OFFSET; i--) { | |
5088 | #endif /* __APPLE__ */ | |
5089 | ASSERT(end >= base); | |
5090 | ||
5091 | val = ip6._S6_un._S6_u8[i]; | |
5092 | ||
5093 | if (val == 0) { | |
5094 | *end-- = '0'; | |
5095 | } else { | |
5096 | for (; val; val /= 10) { | |
5097 | *end-- = '0' + val % 10; | |
5098 | } | |
5099 | } | |
5100 | ||
5101 | #if !defined(__APPLE__) /* Quiet compiler warnings */ | |
5102 | if (i > DTRACE_V4MAPPED_OFFSET) | |
5103 | *end-- = '.'; | |
5104 | #else | |
5105 | if (i > (int)DTRACE_V4MAPPED_OFFSET) | |
5106 | *end-- = '.'; | |
5107 | #endif /* __APPLE__ */ | |
5108 | } | |
5109 | ||
5110 | if (subr == DIF_SUBR_INET_NTOA6) | |
5111 | goto inetout; | |
5112 | ||
5113 | /* | |
5114 | * Set v6end to skip the IPv4 address that | |
5115 | * we have already stringified. | |
5116 | */ | |
5117 | v6end = 10; | |
5118 | } | |
5119 | ||
5120 | /* | |
5121 | * Build the IPv6 string by working through the | |
5122 | * address in reverse. | |
5123 | */ | |
5124 | for (i = v6end; i >= 0; i -= 2) { | |
5125 | ASSERT(end >= base); | |
5126 | ||
5127 | if (i == firstzero + numzero - 2) { | |
5128 | *end-- = ':'; | |
5129 | *end-- = ':'; | |
5130 | i -= numzero - 2; | |
5131 | continue; | |
5132 | } | |
5133 | ||
5134 | if (i < 14 && i != firstzero - 2) | |
5135 | *end-- = ':'; | |
5136 | ||
5137 | val = (ip6._S6_un._S6_u8[i] << 8) + | |
5138 | ip6._S6_un._S6_u8[i + 1]; | |
5139 | ||
5140 | if (val == 0) { | |
5141 | *end-- = '0'; | |
5142 | } else { | |
5143 | for (; val; val /= 16) { | |
5144 | *end-- = digits[val % 16]; | |
5145 | } | |
5146 | } | |
5147 | } | |
5148 | ASSERT(end + 1 >= base); | |
5149 | ||
5150 | #if defined(__APPLE__) | |
5151 | #undef _S6_un | |
5152 | #undef _S6_u8 | |
5153 | #endif /* __APPLE__ */ | |
5154 | } else { | |
5155 | /* | |
5156 | * The user didn't use AH_INET or AH_INET6. | |
5157 | */ | |
5158 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
5159 | regs[rd] = NULL; | |
5160 | break; | |
5161 | } | |
5162 | ||
5163 | inetout: regs[rd] = (uintptr_t)end + 1; | |
5164 | mstate->dtms_scratch_ptr += size; | |
5165 | break; | |
5166 | } | |
5167 | ||
5168 | #ifdef __APPLE__ | |
5169 | ||
5170 | /* CoreProfile callback ('core_profile(uint64_t, [uint64_t], [uint64_t] ...)') */ | |
5171 | case DIF_SUBR_COREPROFILE: { | |
5172 | uint64_t selector = tupregs[0].dttk_value; | |
5173 | uint64_t args[DIF_DTR_NREGS-1] = {0ULL}; | |
5174 | uint32_t ii; | |
5175 | uint32_t count = (uint32_t)nargs; | |
5176 | ||
5177 | if (count < 1) { | |
5178 | regs[rd] = KERN_FAILURE; | |
5179 | break; | |
5180 | } | |
5181 | ||
5182 | if(count > DIF_DTR_NREGS) | |
5183 | count = DIF_DTR_NREGS; | |
5184 | ||
5185 | /* copy in any variadic argument list, bounded by DIF_DTR_NREGS */ | |
5186 | for(ii = 0; ii < count-1; ii++) { | |
5187 | args[ii] = tupregs[ii+1].dttk_value; | |
5188 | } | |
5189 | ||
5190 | kern_return_t ret = | |
5191 | chudxnu_dtrace_callback(selector, args, count-1); | |
2d21ac55 A |
5192 | if(KERN_SUCCESS != ret) { |
5193 | /* error */ | |
5194 | } | |
b0d623f7 A |
5195 | |
5196 | regs[rd] = ret; | |
2d21ac55 A |
5197 | break; |
5198 | } | |
5199 | ||
5200 | #endif /* __APPLE__ */ | |
5201 | ||
5202 | } | |
5203 | } | |
5204 | ||
5205 | /* | |
5206 | * Emulate the execution of DTrace IR instructions specified by the given | |
5207 | * DIF object. This function is deliberately void of assertions as all of | |
5208 | * the necessary checks are handled by a call to dtrace_difo_validate(). | |
5209 | */ | |
5210 | static uint64_t | |
5211 | dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, | |
5212 | dtrace_vstate_t *vstate, dtrace_state_t *state) | |
5213 | { | |
5214 | const dif_instr_t *text = difo->dtdo_buf; | |
5215 | const uint_t textlen = difo->dtdo_len; | |
5216 | const char *strtab = difo->dtdo_strtab; | |
5217 | const uint64_t *inttab = difo->dtdo_inttab; | |
5218 | ||
5219 | uint64_t rval = 0; | |
5220 | dtrace_statvar_t *svar; | |
5221 | dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; | |
5222 | dtrace_difv_t *v; | |
5223 | volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
5224 | #if !defined(__APPLE__) | |
5225 | volatile uintptr_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
5226 | #else | |
5227 | volatile uint64_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
5228 | #endif /* __APPLE__ */ | |
5229 | ||
5230 | dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ | |
5231 | uint64_t regs[DIF_DIR_NREGS]; | |
5232 | uint64_t *tmp; | |
5233 | ||
5234 | uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; | |
5235 | int64_t cc_r; | |
b0d623f7 | 5236 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 5237 | uint_t pc = 0, id, opc; |
b0d623f7 A |
5238 | #else |
5239 | uint_t pc = 0, id, opc = 0; | |
5240 | #endif /* __APPLE__ */ | |
2d21ac55 A |
5241 | uint8_t ttop = 0; |
5242 | dif_instr_t instr; | |
5243 | uint_t r1, r2, rd; | |
5244 | ||
b0d623f7 A |
5245 | /* |
5246 | * We stash the current DIF object into the machine state: we need it | |
5247 | * for subsequent access checking. | |
5248 | */ | |
5249 | mstate->dtms_difo = difo; | |
5250 | ||
2d21ac55 A |
5251 | regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ |
5252 | ||
5253 | while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { | |
5254 | opc = pc; | |
5255 | ||
5256 | instr = text[pc++]; | |
5257 | r1 = DIF_INSTR_R1(instr); | |
5258 | r2 = DIF_INSTR_R2(instr); | |
5259 | rd = DIF_INSTR_RD(instr); | |
5260 | ||
5261 | switch (DIF_INSTR_OP(instr)) { | |
5262 | case DIF_OP_OR: | |
5263 | regs[rd] = regs[r1] | regs[r2]; | |
5264 | break; | |
5265 | case DIF_OP_XOR: | |
5266 | regs[rd] = regs[r1] ^ regs[r2]; | |
5267 | break; | |
5268 | case DIF_OP_AND: | |
5269 | regs[rd] = regs[r1] & regs[r2]; | |
5270 | break; | |
5271 | case DIF_OP_SLL: | |
5272 | regs[rd] = regs[r1] << regs[r2]; | |
5273 | break; | |
5274 | case DIF_OP_SRL: | |
5275 | regs[rd] = regs[r1] >> regs[r2]; | |
5276 | break; | |
5277 | case DIF_OP_SUB: | |
5278 | regs[rd] = regs[r1] - regs[r2]; | |
5279 | break; | |
5280 | case DIF_OP_ADD: | |
5281 | regs[rd] = regs[r1] + regs[r2]; | |
5282 | break; | |
5283 | case DIF_OP_MUL: | |
5284 | regs[rd] = regs[r1] * regs[r2]; | |
5285 | break; | |
5286 | case DIF_OP_SDIV: | |
5287 | if (regs[r2] == 0) { | |
5288 | regs[rd] = 0; | |
5289 | *flags |= CPU_DTRACE_DIVZERO; | |
5290 | } else { | |
5291 | regs[rd] = (int64_t)regs[r1] / | |
5292 | (int64_t)regs[r2]; | |
5293 | } | |
5294 | break; | |
5295 | ||
5296 | case DIF_OP_UDIV: | |
5297 | if (regs[r2] == 0) { | |
5298 | regs[rd] = 0; | |
5299 | *flags |= CPU_DTRACE_DIVZERO; | |
5300 | } else { | |
5301 | regs[rd] = regs[r1] / regs[r2]; | |
5302 | } | |
5303 | break; | |
5304 | ||
5305 | case DIF_OP_SREM: | |
5306 | if (regs[r2] == 0) { | |
5307 | regs[rd] = 0; | |
5308 | *flags |= CPU_DTRACE_DIVZERO; | |
5309 | } else { | |
5310 | regs[rd] = (int64_t)regs[r1] % | |
5311 | (int64_t)regs[r2]; | |
5312 | } | |
5313 | break; | |
5314 | ||
5315 | case DIF_OP_UREM: | |
5316 | if (regs[r2] == 0) { | |
5317 | regs[rd] = 0; | |
5318 | *flags |= CPU_DTRACE_DIVZERO; | |
5319 | } else { | |
5320 | regs[rd] = regs[r1] % regs[r2]; | |
5321 | } | |
5322 | break; | |
5323 | ||
5324 | case DIF_OP_NOT: | |
5325 | regs[rd] = ~regs[r1]; | |
5326 | break; | |
5327 | case DIF_OP_MOV: | |
5328 | regs[rd] = regs[r1]; | |
5329 | break; | |
5330 | case DIF_OP_CMP: | |
5331 | cc_r = regs[r1] - regs[r2]; | |
5332 | cc_n = cc_r < 0; | |
5333 | cc_z = cc_r == 0; | |
5334 | cc_v = 0; | |
5335 | cc_c = regs[r1] < regs[r2]; | |
5336 | break; | |
5337 | case DIF_OP_TST: | |
5338 | cc_n = cc_v = cc_c = 0; | |
5339 | cc_z = regs[r1] == 0; | |
5340 | break; | |
5341 | case DIF_OP_BA: | |
5342 | pc = DIF_INSTR_LABEL(instr); | |
5343 | break; | |
5344 | case DIF_OP_BE: | |
5345 | if (cc_z) | |
5346 | pc = DIF_INSTR_LABEL(instr); | |
5347 | break; | |
5348 | case DIF_OP_BNE: | |
5349 | if (cc_z == 0) | |
5350 | pc = DIF_INSTR_LABEL(instr); | |
5351 | break; | |
5352 | case DIF_OP_BG: | |
5353 | if ((cc_z | (cc_n ^ cc_v)) == 0) | |
5354 | pc = DIF_INSTR_LABEL(instr); | |
5355 | break; | |
5356 | case DIF_OP_BGU: | |
5357 | if ((cc_c | cc_z) == 0) | |
5358 | pc = DIF_INSTR_LABEL(instr); | |
5359 | break; | |
5360 | case DIF_OP_BGE: | |
5361 | if ((cc_n ^ cc_v) == 0) | |
5362 | pc = DIF_INSTR_LABEL(instr); | |
5363 | break; | |
5364 | case DIF_OP_BGEU: | |
5365 | if (cc_c == 0) | |
5366 | pc = DIF_INSTR_LABEL(instr); | |
5367 | break; | |
5368 | case DIF_OP_BL: | |
5369 | if (cc_n ^ cc_v) | |
5370 | pc = DIF_INSTR_LABEL(instr); | |
5371 | break; | |
5372 | case DIF_OP_BLU: | |
5373 | if (cc_c) | |
5374 | pc = DIF_INSTR_LABEL(instr); | |
5375 | break; | |
5376 | case DIF_OP_BLE: | |
5377 | if (cc_z | (cc_n ^ cc_v)) | |
5378 | pc = DIF_INSTR_LABEL(instr); | |
5379 | break; | |
5380 | case DIF_OP_BLEU: | |
5381 | if (cc_c | cc_z) | |
5382 | pc = DIF_INSTR_LABEL(instr); | |
5383 | break; | |
5384 | case DIF_OP_RLDSB: | |
5385 | if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { | |
5386 | *flags |= CPU_DTRACE_KPRIV; | |
5387 | *illval = regs[r1]; | |
5388 | break; | |
5389 | } | |
5390 | /*FALLTHROUGH*/ | |
5391 | case DIF_OP_LDSB: | |
5392 | regs[rd] = (int8_t)dtrace_load8(regs[r1]); | |
5393 | break; | |
5394 | case DIF_OP_RLDSH: | |
5395 | if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { | |
5396 | *flags |= CPU_DTRACE_KPRIV; | |
5397 | *illval = regs[r1]; | |
5398 | break; | |
5399 | } | |
5400 | /*FALLTHROUGH*/ | |
5401 | case DIF_OP_LDSH: | |
5402 | regs[rd] = (int16_t)dtrace_load16(regs[r1]); | |
5403 | break; | |
5404 | case DIF_OP_RLDSW: | |
5405 | if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { | |
5406 | *flags |= CPU_DTRACE_KPRIV; | |
5407 | *illval = regs[r1]; | |
5408 | break; | |
5409 | } | |
5410 | /*FALLTHROUGH*/ | |
5411 | case DIF_OP_LDSW: | |
5412 | regs[rd] = (int32_t)dtrace_load32(regs[r1]); | |
5413 | break; | |
5414 | case DIF_OP_RLDUB: | |
5415 | if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { | |
5416 | *flags |= CPU_DTRACE_KPRIV; | |
5417 | *illval = regs[r1]; | |
5418 | break; | |
5419 | } | |
5420 | /*FALLTHROUGH*/ | |
5421 | case DIF_OP_LDUB: | |
5422 | regs[rd] = dtrace_load8(regs[r1]); | |
5423 | break; | |
5424 | case DIF_OP_RLDUH: | |
5425 | if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { | |
5426 | *flags |= CPU_DTRACE_KPRIV; | |
5427 | *illval = regs[r1]; | |
5428 | break; | |
5429 | } | |
5430 | /*FALLTHROUGH*/ | |
5431 | case DIF_OP_LDUH: | |
5432 | regs[rd] = dtrace_load16(regs[r1]); | |
5433 | break; | |
5434 | case DIF_OP_RLDUW: | |
5435 | if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { | |
5436 | *flags |= CPU_DTRACE_KPRIV; | |
5437 | *illval = regs[r1]; | |
5438 | break; | |
5439 | } | |
5440 | /*FALLTHROUGH*/ | |
5441 | case DIF_OP_LDUW: | |
5442 | regs[rd] = dtrace_load32(regs[r1]); | |
5443 | break; | |
5444 | case DIF_OP_RLDX: | |
5445 | if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { | |
5446 | *flags |= CPU_DTRACE_KPRIV; | |
5447 | *illval = regs[r1]; | |
5448 | break; | |
5449 | } | |
5450 | /*FALLTHROUGH*/ | |
5451 | case DIF_OP_LDX: | |
5452 | regs[rd] = dtrace_load64(regs[r1]); | |
5453 | break; | |
b0d623f7 A |
5454 | #if !defined(__APPLE__) |
5455 | case DIF_OP_ULDSB: | |
5456 | regs[rd] = (int8_t) | |
5457 | dtrace_fuword8((void *)(uintptr_t)regs[r1]); | |
5458 | break; | |
5459 | case DIF_OP_ULDSH: | |
5460 | regs[rd] = (int16_t) | |
5461 | dtrace_fuword16((void *)(uintptr_t)regs[r1]); | |
5462 | break; | |
5463 | case DIF_OP_ULDSW: | |
5464 | regs[rd] = (int32_t) | |
5465 | dtrace_fuword32((void *)(uintptr_t)regs[r1]); | |
5466 | break; | |
5467 | case DIF_OP_ULDUB: | |
5468 | regs[rd] = | |
5469 | dtrace_fuword8((void *)(uintptr_t)regs[r1]); | |
5470 | break; | |
5471 | case DIF_OP_ULDUH: | |
5472 | regs[rd] = | |
5473 | dtrace_fuword16((void *)(uintptr_t)regs[r1]); | |
5474 | break; | |
5475 | case DIF_OP_ULDUW: | |
5476 | regs[rd] = | |
5477 | dtrace_fuword32((void *)(uintptr_t)regs[r1]); | |
5478 | break; | |
5479 | case DIF_OP_ULDX: | |
5480 | regs[rd] = | |
5481 | dtrace_fuword64((void *)(uintptr_t)regs[r1]); | |
5482 | break; | |
5483 | #else /* Darwin 32-bit kernel may fetch from 64-bit user. Don't want uintptr_t cast. */ | |
2d21ac55 A |
5484 | case DIF_OP_ULDSB: |
5485 | regs[rd] = (int8_t) | |
5486 | dtrace_fuword8(regs[r1]); | |
5487 | break; | |
5488 | case DIF_OP_ULDSH: | |
5489 | regs[rd] = (int16_t) | |
5490 | dtrace_fuword16(regs[r1]); | |
5491 | break; | |
5492 | case DIF_OP_ULDSW: | |
5493 | regs[rd] = (int32_t) | |
5494 | dtrace_fuword32(regs[r1]); | |
5495 | break; | |
5496 | case DIF_OP_ULDUB: | |
5497 | regs[rd] = | |
5498 | dtrace_fuword8(regs[r1]); | |
5499 | break; | |
5500 | case DIF_OP_ULDUH: | |
5501 | regs[rd] = | |
5502 | dtrace_fuword16(regs[r1]); | |
5503 | break; | |
5504 | case DIF_OP_ULDUW: | |
5505 | regs[rd] = | |
5506 | dtrace_fuword32(regs[r1]); | |
5507 | break; | |
5508 | case DIF_OP_ULDX: | |
5509 | regs[rd] = | |
5510 | dtrace_fuword64(regs[r1]); | |
b0d623f7 | 5511 | #endif /* __APPLE__ */ |
2d21ac55 A |
5512 | break; |
5513 | case DIF_OP_RET: | |
5514 | rval = regs[rd]; | |
b0d623f7 | 5515 | pc = textlen; |
2d21ac55 A |
5516 | break; |
5517 | case DIF_OP_NOP: | |
5518 | break; | |
5519 | case DIF_OP_SETX: | |
5520 | regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; | |
5521 | break; | |
5522 | case DIF_OP_SETS: | |
5523 | regs[rd] = (uint64_t)(uintptr_t) | |
5524 | (strtab + DIF_INSTR_STRING(instr)); | |
5525 | break; | |
b0d623f7 A |
5526 | case DIF_OP_SCMP: { |
5527 | size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; | |
5528 | uintptr_t s1 = regs[r1]; | |
5529 | uintptr_t s2 = regs[r2]; | |
5530 | ||
5531 | if (s1 != NULL && | |
5532 | !dtrace_strcanload(s1, sz, mstate, vstate)) | |
5533 | break; | |
5534 | if (s2 != NULL && | |
5535 | !dtrace_strcanload(s2, sz, mstate, vstate)) | |
5536 | break; | |
5537 | ||
5538 | cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); | |
2d21ac55 A |
5539 | |
5540 | cc_n = cc_r < 0; | |
5541 | cc_z = cc_r == 0; | |
5542 | cc_v = cc_c = 0; | |
5543 | break; | |
b0d623f7 | 5544 | } |
2d21ac55 A |
5545 | case DIF_OP_LDGA: |
5546 | regs[rd] = dtrace_dif_variable(mstate, state, | |
5547 | r1, regs[r2]); | |
5548 | break; | |
5549 | case DIF_OP_LDGS: | |
5550 | id = DIF_INSTR_VAR(instr); | |
5551 | ||
5552 | if (id >= DIF_VAR_OTHER_UBASE) { | |
5553 | uintptr_t a; | |
5554 | ||
5555 | id -= DIF_VAR_OTHER_UBASE; | |
5556 | svar = vstate->dtvs_globals[id]; | |
5557 | ASSERT(svar != NULL); | |
5558 | v = &svar->dtsv_var; | |
5559 | ||
5560 | if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { | |
5561 | regs[rd] = svar->dtsv_data; | |
5562 | break; | |
5563 | } | |
5564 | ||
5565 | a = (uintptr_t)svar->dtsv_data; | |
5566 | ||
5567 | if (*(uint8_t *)a == UINT8_MAX) { | |
5568 | /* | |
5569 | * If the 0th byte is set to UINT8_MAX | |
5570 | * then this is to be treated as a | |
5571 | * reference to a NULL variable. | |
5572 | */ | |
5573 | regs[rd] = NULL; | |
5574 | } else { | |
5575 | regs[rd] = a + sizeof (uint64_t); | |
5576 | } | |
5577 | ||
5578 | break; | |
5579 | } | |
5580 | ||
5581 | regs[rd] = dtrace_dif_variable(mstate, state, id, 0); | |
5582 | break; | |
5583 | ||
5584 | case DIF_OP_STGS: | |
5585 | id = DIF_INSTR_VAR(instr); | |
5586 | ||
5587 | ASSERT(id >= DIF_VAR_OTHER_UBASE); | |
5588 | id -= DIF_VAR_OTHER_UBASE; | |
5589 | ||
5590 | svar = vstate->dtvs_globals[id]; | |
5591 | ASSERT(svar != NULL); | |
5592 | v = &svar->dtsv_var; | |
5593 | ||
5594 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
5595 | uintptr_t a = (uintptr_t)svar->dtsv_data; | |
5596 | ||
5597 | ASSERT(a != NULL); | |
5598 | ASSERT(svar->dtsv_size != 0); | |
5599 | ||
5600 | if (regs[rd] == NULL) { | |
5601 | *(uint8_t *)a = UINT8_MAX; | |
5602 | break; | |
5603 | } else { | |
5604 | *(uint8_t *)a = 0; | |
5605 | a += sizeof (uint64_t); | |
5606 | } | |
b0d623f7 A |
5607 | if (!dtrace_vcanload( |
5608 | (void *)(uintptr_t)regs[rd], &v->dtdv_type, | |
5609 | mstate, vstate)) | |
5610 | break; | |
2d21ac55 A |
5611 | |
5612 | dtrace_vcopy((void *)(uintptr_t)regs[rd], | |
5613 | (void *)a, &v->dtdv_type); | |
5614 | break; | |
5615 | } | |
5616 | ||
5617 | svar->dtsv_data = regs[rd]; | |
5618 | break; | |
5619 | ||
5620 | case DIF_OP_LDTA: | |
5621 | /* | |
5622 | * There are no DTrace built-in thread-local arrays at | |
5623 | * present. This opcode is saved for future work. | |
5624 | */ | |
5625 | *flags |= CPU_DTRACE_ILLOP; | |
5626 | regs[rd] = 0; | |
5627 | break; | |
5628 | ||
5629 | case DIF_OP_LDLS: | |
5630 | id = DIF_INSTR_VAR(instr); | |
5631 | ||
5632 | if (id < DIF_VAR_OTHER_UBASE) { | |
5633 | /* | |
5634 | * For now, this has no meaning. | |
5635 | */ | |
5636 | regs[rd] = 0; | |
5637 | break; | |
5638 | } | |
5639 | ||
5640 | id -= DIF_VAR_OTHER_UBASE; | |
5641 | ||
b0d623f7 | 5642 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 5643 | ASSERT(id < vstate->dtvs_nlocals); |
b0d623f7 A |
5644 | #else |
5645 | ASSERT(id < (uint_t)vstate->dtvs_nlocals); | |
5646 | #endif /* __APPLE__ */ | |
2d21ac55 A |
5647 | ASSERT(vstate->dtvs_locals != NULL); |
5648 | ||
5649 | svar = vstate->dtvs_locals[id]; | |
5650 | ASSERT(svar != NULL); | |
5651 | v = &svar->dtsv_var; | |
5652 | ||
5653 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
5654 | uintptr_t a = (uintptr_t)svar->dtsv_data; | |
5655 | size_t sz = v->dtdv_type.dtdt_size; | |
5656 | ||
5657 | sz += sizeof (uint64_t); | |
c910b4d9 | 5658 | ASSERT(svar->dtsv_size == (int)NCPU * sz); |
2d21ac55 A |
5659 | a += CPU->cpu_id * sz; |
5660 | ||
5661 | if (*(uint8_t *)a == UINT8_MAX) { | |
5662 | /* | |
5663 | * If the 0th byte is set to UINT8_MAX | |
5664 | * then this is to be treated as a | |
5665 | * reference to a NULL variable. | |
5666 | */ | |
5667 | regs[rd] = NULL; | |
5668 | } else { | |
5669 | regs[rd] = a + sizeof (uint64_t); | |
5670 | } | |
5671 | ||
5672 | break; | |
5673 | } | |
5674 | ||
c910b4d9 | 5675 | ASSERT(svar->dtsv_size == (int)NCPU * sizeof (uint64_t)); |
2d21ac55 A |
5676 | tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; |
5677 | regs[rd] = tmp[CPU->cpu_id]; | |
5678 | break; | |
5679 | ||
5680 | case DIF_OP_STLS: | |
5681 | id = DIF_INSTR_VAR(instr); | |
5682 | ||
5683 | ASSERT(id >= DIF_VAR_OTHER_UBASE); | |
5684 | id -= DIF_VAR_OTHER_UBASE; | |
b0d623f7 | 5685 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 5686 | ASSERT(id < vstate->dtvs_nlocals); |
b0d623f7 A |
5687 | #else |
5688 | ASSERT(id < (uint_t)vstate->dtvs_nlocals); | |
5689 | #endif /* __APPLE__ */ | |
2d21ac55 A |
5690 | |
5691 | ASSERT(vstate->dtvs_locals != NULL); | |
5692 | svar = vstate->dtvs_locals[id]; | |
5693 | ASSERT(svar != NULL); | |
5694 | v = &svar->dtsv_var; | |
5695 | ||
5696 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
5697 | uintptr_t a = (uintptr_t)svar->dtsv_data; | |
5698 | size_t sz = v->dtdv_type.dtdt_size; | |
5699 | ||
5700 | sz += sizeof (uint64_t); | |
c910b4d9 | 5701 | ASSERT(svar->dtsv_size == (int)NCPU * sz); |
2d21ac55 A |
5702 | a += CPU->cpu_id * sz; |
5703 | ||
5704 | if (regs[rd] == NULL) { | |
5705 | *(uint8_t *)a = UINT8_MAX; | |
5706 | break; | |
5707 | } else { | |
5708 | *(uint8_t *)a = 0; | |
5709 | a += sizeof (uint64_t); | |
5710 | } | |
5711 | ||
b0d623f7 A |
5712 | if (!dtrace_vcanload( |
5713 | (void *)(uintptr_t)regs[rd], &v->dtdv_type, | |
5714 | mstate, vstate)) | |
5715 | break; | |
5716 | ||
2d21ac55 A |
5717 | dtrace_vcopy((void *)(uintptr_t)regs[rd], |
5718 | (void *)a, &v->dtdv_type); | |
5719 | break; | |
5720 | } | |
5721 | ||
c910b4d9 | 5722 | ASSERT(svar->dtsv_size == (int)NCPU * sizeof (uint64_t)); |
2d21ac55 A |
5723 | tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; |
5724 | tmp[CPU->cpu_id] = regs[rd]; | |
5725 | break; | |
5726 | ||
5727 | case DIF_OP_LDTS: { | |
5728 | dtrace_dynvar_t *dvar; | |
5729 | dtrace_key_t *key; | |
5730 | ||
5731 | id = DIF_INSTR_VAR(instr); | |
5732 | ASSERT(id >= DIF_VAR_OTHER_UBASE); | |
5733 | id -= DIF_VAR_OTHER_UBASE; | |
5734 | v = &vstate->dtvs_tlocals[id]; | |
5735 | ||
5736 | key = &tupregs[DIF_DTR_NREGS]; | |
5737 | key[0].dttk_value = (uint64_t)id; | |
5738 | key[0].dttk_size = 0; | |
5739 | DTRACE_TLS_THRKEY(key[1].dttk_value); | |
5740 | key[1].dttk_size = 0; | |
5741 | ||
5742 | dvar = dtrace_dynvar(dstate, 2, key, | |
b0d623f7 A |
5743 | sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, |
5744 | mstate, vstate); | |
2d21ac55 A |
5745 | |
5746 | if (dvar == NULL) { | |
5747 | regs[rd] = 0; | |
5748 | break; | |
5749 | } | |
5750 | ||
5751 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
5752 | regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; | |
5753 | } else { | |
5754 | regs[rd] = *((uint64_t *)dvar->dtdv_data); | |
5755 | } | |
5756 | ||
5757 | break; | |
5758 | } | |
5759 | ||
5760 | case DIF_OP_STTS: { | |
5761 | dtrace_dynvar_t *dvar; | |
5762 | dtrace_key_t *key; | |
5763 | ||
5764 | id = DIF_INSTR_VAR(instr); | |
5765 | ASSERT(id >= DIF_VAR_OTHER_UBASE); | |
5766 | id -= DIF_VAR_OTHER_UBASE; | |
5767 | ||
5768 | key = &tupregs[DIF_DTR_NREGS]; | |
5769 | key[0].dttk_value = (uint64_t)id; | |
5770 | key[0].dttk_size = 0; | |
5771 | DTRACE_TLS_THRKEY(key[1].dttk_value); | |
5772 | key[1].dttk_size = 0; | |
5773 | v = &vstate->dtvs_tlocals[id]; | |
5774 | ||
5775 | dvar = dtrace_dynvar(dstate, 2, key, | |
5776 | v->dtdv_type.dtdt_size > sizeof (uint64_t) ? | |
5777 | v->dtdv_type.dtdt_size : sizeof (uint64_t), | |
5778 | regs[rd] ? DTRACE_DYNVAR_ALLOC : | |
b0d623f7 | 5779 | DTRACE_DYNVAR_DEALLOC, mstate, vstate); |
2d21ac55 A |
5780 | |
5781 | /* | |
5782 | * Given that we're storing to thread-local data, | |
5783 | * we need to flush our predicate cache. | |
5784 | */ | |
5785 | #if !defined(__APPLE__) | |
5786 | curthread->t_predcache = NULL; | |
5787 | #else | |
5788 | dtrace_set_thread_predcache(current_thread(), 0); | |
5789 | #endif /* __APPLE__ */ | |
5790 | ||
2d21ac55 A |
5791 | if (dvar == NULL) |
5792 | break; | |
5793 | ||
5794 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
b0d623f7 A |
5795 | if (!dtrace_vcanload( |
5796 | (void *)(uintptr_t)regs[rd], | |
5797 | &v->dtdv_type, mstate, vstate)) | |
5798 | break; | |
5799 | ||
2d21ac55 A |
5800 | dtrace_vcopy((void *)(uintptr_t)regs[rd], |
5801 | dvar->dtdv_data, &v->dtdv_type); | |
5802 | } else { | |
5803 | *((uint64_t *)dvar->dtdv_data) = regs[rd]; | |
5804 | } | |
5805 | ||
5806 | break; | |
5807 | } | |
5808 | ||
5809 | case DIF_OP_SRA: | |
5810 | regs[rd] = (int64_t)regs[r1] >> regs[r2]; | |
5811 | break; | |
5812 | ||
5813 | case DIF_OP_CALL: | |
5814 | dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, | |
5815 | regs, tupregs, ttop, mstate, state); | |
5816 | break; | |
5817 | ||
5818 | case DIF_OP_PUSHTR: | |
5819 | if (ttop == DIF_DTR_NREGS) { | |
5820 | *flags |= CPU_DTRACE_TUPOFLOW; | |
5821 | break; | |
5822 | } | |
5823 | ||
5824 | if (r1 == DIF_TYPE_STRING) { | |
5825 | /* | |
5826 | * If this is a string type and the size is 0, | |
5827 | * we'll use the system-wide default string | |
5828 | * size. Note that we are _not_ looking at | |
5829 | * the value of the DTRACEOPT_STRSIZE option; | |
5830 | * had this been set, we would expect to have | |
5831 | * a non-zero size value in the "pushtr". | |
5832 | */ | |
5833 | tupregs[ttop].dttk_size = | |
5834 | dtrace_strlen((char *)(uintptr_t)regs[rd], | |
5835 | regs[r2] ? regs[r2] : | |
5836 | dtrace_strsize_default) + 1; | |
5837 | } else { | |
5838 | tupregs[ttop].dttk_size = regs[r2]; | |
5839 | } | |
5840 | ||
5841 | tupregs[ttop++].dttk_value = regs[rd]; | |
5842 | break; | |
5843 | ||
5844 | case DIF_OP_PUSHTV: | |
5845 | if (ttop == DIF_DTR_NREGS) { | |
5846 | *flags |= CPU_DTRACE_TUPOFLOW; | |
5847 | break; | |
5848 | } | |
5849 | ||
5850 | tupregs[ttop].dttk_value = regs[rd]; | |
5851 | tupregs[ttop++].dttk_size = 0; | |
5852 | break; | |
5853 | ||
5854 | case DIF_OP_POPTS: | |
5855 | if (ttop != 0) | |
5856 | ttop--; | |
5857 | break; | |
5858 | ||
5859 | case DIF_OP_FLUSHTS: | |
5860 | ttop = 0; | |
5861 | break; | |
5862 | ||
5863 | case DIF_OP_LDGAA: | |
5864 | case DIF_OP_LDTAA: { | |
5865 | dtrace_dynvar_t *dvar; | |
5866 | dtrace_key_t *key = tupregs; | |
5867 | uint_t nkeys = ttop; | |
5868 | ||
5869 | id = DIF_INSTR_VAR(instr); | |
5870 | ASSERT(id >= DIF_VAR_OTHER_UBASE); | |
5871 | id -= DIF_VAR_OTHER_UBASE; | |
5872 | ||
5873 | key[nkeys].dttk_value = (uint64_t)id; | |
5874 | key[nkeys++].dttk_size = 0; | |
5875 | ||
5876 | if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { | |
5877 | DTRACE_TLS_THRKEY(key[nkeys].dttk_value); | |
5878 | key[nkeys++].dttk_size = 0; | |
5879 | v = &vstate->dtvs_tlocals[id]; | |
5880 | } else { | |
5881 | v = &vstate->dtvs_globals[id]->dtsv_var; | |
5882 | } | |
5883 | ||
5884 | dvar = dtrace_dynvar(dstate, nkeys, key, | |
5885 | v->dtdv_type.dtdt_size > sizeof (uint64_t) ? | |
5886 | v->dtdv_type.dtdt_size : sizeof (uint64_t), | |
b0d623f7 | 5887 | DTRACE_DYNVAR_NOALLOC, mstate, vstate); |
2d21ac55 A |
5888 | |
5889 | if (dvar == NULL) { | |
5890 | regs[rd] = 0; | |
5891 | break; | |
5892 | } | |
5893 | ||
5894 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
5895 | regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; | |
5896 | } else { | |
5897 | regs[rd] = *((uint64_t *)dvar->dtdv_data); | |
5898 | } | |
5899 | ||
5900 | break; | |
5901 | } | |
5902 | ||
5903 | case DIF_OP_STGAA: | |
5904 | case DIF_OP_STTAA: { | |
5905 | dtrace_dynvar_t *dvar; | |
5906 | dtrace_key_t *key = tupregs; | |
5907 | uint_t nkeys = ttop; | |
5908 | ||
5909 | id = DIF_INSTR_VAR(instr); | |
5910 | ASSERT(id >= DIF_VAR_OTHER_UBASE); | |
5911 | id -= DIF_VAR_OTHER_UBASE; | |
5912 | ||
5913 | key[nkeys].dttk_value = (uint64_t)id; | |
5914 | key[nkeys++].dttk_size = 0; | |
5915 | ||
5916 | if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { | |
5917 | DTRACE_TLS_THRKEY(key[nkeys].dttk_value); | |
5918 | key[nkeys++].dttk_size = 0; | |
5919 | v = &vstate->dtvs_tlocals[id]; | |
5920 | } else { | |
5921 | v = &vstate->dtvs_globals[id]->dtsv_var; | |
5922 | } | |
5923 | ||
5924 | dvar = dtrace_dynvar(dstate, nkeys, key, | |
5925 | v->dtdv_type.dtdt_size > sizeof (uint64_t) ? | |
5926 | v->dtdv_type.dtdt_size : sizeof (uint64_t), | |
5927 | regs[rd] ? DTRACE_DYNVAR_ALLOC : | |
b0d623f7 | 5928 | DTRACE_DYNVAR_DEALLOC, mstate, vstate); |
2d21ac55 A |
5929 | |
5930 | if (dvar == NULL) | |
5931 | break; | |
5932 | ||
5933 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { | |
b0d623f7 A |
5934 | if (!dtrace_vcanload( |
5935 | (void *)(uintptr_t)regs[rd], &v->dtdv_type, | |
5936 | mstate, vstate)) | |
5937 | break; | |
5938 | ||
2d21ac55 A |
5939 | dtrace_vcopy((void *)(uintptr_t)regs[rd], |
5940 | dvar->dtdv_data, &v->dtdv_type); | |
5941 | } else { | |
5942 | *((uint64_t *)dvar->dtdv_data) = regs[rd]; | |
5943 | } | |
5944 | ||
5945 | break; | |
5946 | } | |
5947 | ||
5948 | case DIF_OP_ALLOCS: { | |
5949 | uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); | |
5950 | size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; | |
5951 | ||
b0d623f7 A |
5952 | /* |
5953 | * Rounding up the user allocation size could have | |
5954 | * overflowed large, bogus allocations (like -1ULL) to | |
5955 | * 0. | |
5956 | */ | |
5957 | if (size < regs[r1] || | |
5958 | !DTRACE_INSCRATCH(mstate, size)) { | |
2d21ac55 A |
5959 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); |
5960 | regs[rd] = NULL; | |
b0d623f7 A |
5961 | break; |
5962 | } | |
5963 | ||
5964 | dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); | |
2d21ac55 A |
5965 | mstate->dtms_scratch_ptr += size; |
5966 | regs[rd] = ptr; | |
2d21ac55 A |
5967 | break; |
5968 | } | |
5969 | ||
5970 | case DIF_OP_COPYS: | |
5971 | if (!dtrace_canstore(regs[rd], regs[r2], | |
5972 | mstate, vstate)) { | |
5973 | *flags |= CPU_DTRACE_BADADDR; | |
5974 | *illval = regs[rd]; | |
5975 | break; | |
5976 | } | |
5977 | ||
b0d623f7 A |
5978 | if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) |
5979 | break; | |
5980 | ||
2d21ac55 A |
5981 | dtrace_bcopy((void *)(uintptr_t)regs[r1], |
5982 | (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); | |
5983 | break; | |
5984 | ||
5985 | case DIF_OP_STB: | |
5986 | if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { | |
5987 | *flags |= CPU_DTRACE_BADADDR; | |
5988 | *illval = regs[rd]; | |
5989 | break; | |
5990 | } | |
5991 | *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; | |
5992 | break; | |
5993 | ||
5994 | case DIF_OP_STH: | |
5995 | if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { | |
5996 | *flags |= CPU_DTRACE_BADADDR; | |
5997 | *illval = regs[rd]; | |
5998 | break; | |
5999 | } | |
6000 | if (regs[rd] & 1) { | |
6001 | *flags |= CPU_DTRACE_BADALIGN; | |
6002 | *illval = regs[rd]; | |
6003 | break; | |
6004 | } | |
6005 | *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; | |
6006 | break; | |
6007 | ||
6008 | case DIF_OP_STW: | |
6009 | if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { | |
6010 | *flags |= CPU_DTRACE_BADADDR; | |
6011 | *illval = regs[rd]; | |
6012 | break; | |
6013 | } | |
6014 | if (regs[rd] & 3) { | |
6015 | *flags |= CPU_DTRACE_BADALIGN; | |
6016 | *illval = regs[rd]; | |
6017 | break; | |
6018 | } | |
6019 | *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; | |
6020 | break; | |
6021 | ||
6022 | case DIF_OP_STX: | |
6023 | if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { | |
6024 | *flags |= CPU_DTRACE_BADADDR; | |
6025 | *illval = regs[rd]; | |
6026 | break; | |
6027 | } | |
6028 | #if !defined(__APPLE__) | |
6029 | if (regs[rd] & 7) { | |
6030 | #else | |
6031 | if (regs[rd] & 3) { /* Darwin kmem_zalloc() called from dtrace_difo_init() is 4-byte aligned. */ | |
6032 | #endif /* __APPLE__ */ | |
6033 | *flags |= CPU_DTRACE_BADALIGN; | |
6034 | *illval = regs[rd]; | |
6035 | break; | |
6036 | } | |
6037 | *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; | |
6038 | break; | |
6039 | } | |
6040 | } | |
6041 | ||
6042 | if (!(*flags & CPU_DTRACE_FAULT)) | |
6043 | return (rval); | |
6044 | ||
6045 | mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); | |
6046 | mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; | |
6047 | ||
6048 | return (0); | |
6049 | } | |
6050 | ||
6051 | static void | |
6052 | dtrace_action_breakpoint(dtrace_ecb_t *ecb) | |
6053 | { | |
6054 | dtrace_probe_t *probe = ecb->dte_probe; | |
6055 | dtrace_provider_t *prov = probe->dtpr_provider; | |
6056 | char c[DTRACE_FULLNAMELEN + 80], *str; | |
b0d623f7 | 6057 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
6058 | char *msg = "dtrace: breakpoint action at probe "; |
6059 | char *ecbmsg = " (ecb "; | |
b0d623f7 A |
6060 | #else |
6061 | const char *msg = "dtrace: breakpoint action at probe "; | |
6062 | const char *ecbmsg = " (ecb "; | |
6063 | #endif /* __APPLE__ */ | |
2d21ac55 A |
6064 | uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); |
6065 | uintptr_t val = (uintptr_t)ecb; | |
6066 | int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; | |
6067 | ||
6068 | if (dtrace_destructive_disallow) | |
6069 | return; | |
6070 | ||
6071 | /* | |
6072 | * It's impossible to be taking action on the NULL probe. | |
6073 | */ | |
6074 | ASSERT(probe != NULL); | |
6075 | ||
6076 | /* | |
6077 | * This is a poor man's (destitute man's?) sprintf(): we want to | |
6078 | * print the provider name, module name, function name and name of | |
6079 | * the probe, along with the hex address of the ECB with the breakpoint | |
6080 | * action -- all of which we must place in the character buffer by | |
6081 | * hand. | |
6082 | */ | |
6083 | while (*msg != '\0') | |
6084 | c[i++] = *msg++; | |
6085 | ||
6086 | for (str = prov->dtpv_name; *str != '\0'; str++) | |
6087 | c[i++] = *str; | |
6088 | c[i++] = ':'; | |
6089 | ||
6090 | for (str = probe->dtpr_mod; *str != '\0'; str++) | |
6091 | c[i++] = *str; | |
6092 | c[i++] = ':'; | |
6093 | ||
6094 | for (str = probe->dtpr_func; *str != '\0'; str++) | |
6095 | c[i++] = *str; | |
6096 | c[i++] = ':'; | |
6097 | ||
6098 | for (str = probe->dtpr_name; *str != '\0'; str++) | |
6099 | c[i++] = *str; | |
6100 | ||
6101 | while (*ecbmsg != '\0') | |
6102 | c[i++] = *ecbmsg++; | |
6103 | ||
6104 | while (shift >= 0) { | |
6105 | mask = (uintptr_t)0xf << shift; | |
6106 | ||
6107 | if (val >= ((uintptr_t)1 << shift)) | |
6108 | c[i++] = "0123456789abcdef"[(val & mask) >> shift]; | |
6109 | shift -= 4; | |
6110 | } | |
6111 | ||
6112 | c[i++] = ')'; | |
6113 | c[i] = '\0'; | |
6114 | ||
6115 | debug_enter(c); | |
6116 | } | |
6117 | ||
6118 | static void | |
6119 | dtrace_action_panic(dtrace_ecb_t *ecb) | |
6120 | { | |
6121 | dtrace_probe_t *probe = ecb->dte_probe; | |
6122 | ||
6123 | /* | |
6124 | * It's impossible to be taking action on the NULL probe. | |
6125 | */ | |
6126 | ASSERT(probe != NULL); | |
6127 | ||
6128 | if (dtrace_destructive_disallow) | |
6129 | return; | |
6130 | ||
6131 | if (dtrace_panicked != NULL) | |
6132 | return; | |
6133 | ||
6134 | #if !defined(__APPLE__) | |
6135 | if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) | |
6136 | return; | |
6137 | #else | |
6138 | if (dtrace_casptr(&dtrace_panicked, NULL, current_thread()) != NULL) | |
6139 | return; | |
6140 | #endif /* __APPLE__ */ | |
6141 | ||
6142 | /* | |
6143 | * We won the right to panic. (We want to be sure that only one | |
6144 | * thread calls panic() from dtrace_probe(), and that panic() is | |
6145 | * called exactly once.) | |
6146 | */ | |
6147 | dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", | |
6148 | probe->dtpr_provider->dtpv_name, probe->dtpr_mod, | |
6149 | probe->dtpr_func, probe->dtpr_name, (void *)ecb); | |
6150 | ||
6151 | #if defined(__APPLE__) | |
6152 | /* Mac OS X debug feature -- can return from panic() */ | |
6153 | dtrace_panicked = NULL; | |
6154 | #endif /* __APPLE__ */ | |
6155 | } | |
6156 | ||
6157 | static void | |
6158 | dtrace_action_raise(uint64_t sig) | |
6159 | { | |
6160 | if (dtrace_destructive_disallow) | |
6161 | return; | |
6162 | ||
6163 | if (sig >= NSIG) { | |
6164 | DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); | |
6165 | return; | |
6166 | } | |
6167 | ||
6168 | #if !defined(__APPLE__) | |
6169 | /* | |
6170 | * raise() has a queue depth of 1 -- we ignore all subsequent | |
6171 | * invocations of the raise() action. | |
6172 | */ | |
6173 | if (curthread->t_dtrace_sig == 0) | |
6174 | curthread->t_dtrace_sig = (uint8_t)sig; | |
6175 | ||
6176 | curthread->t_sig_check = 1; | |
6177 | aston(curthread); | |
6178 | #else | |
6179 | uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread()); | |
6180 | ||
6181 | if (uthread && uthread->t_dtrace_sig == 0) { | |
6182 | uthread->t_dtrace_sig = sig; | |
b0d623f7 | 6183 | astbsd_on(); |
2d21ac55 A |
6184 | } |
6185 | #endif /* __APPLE__ */ | |
6186 | } | |
6187 | ||
6188 | static void | |
6189 | dtrace_action_stop(void) | |
6190 | { | |
6191 | if (dtrace_destructive_disallow) | |
6192 | return; | |
6193 | ||
6194 | #if !defined(__APPLE__) | |
6195 | if (!curthread->t_dtrace_stop) { | |
6196 | curthread->t_dtrace_stop = 1; | |
6197 | curthread->t_sig_check = 1; | |
6198 | aston(curthread); | |
6199 | } | |
6200 | #else | |
b0d623f7 A |
6201 | uthread_t uthread = (uthread_t)get_bsdthread_info(current_thread()); |
6202 | ||
6203 | if (uthread && uthread->t_dtrace_stop == 0) { | |
6204 | uthread->t_dtrace_stop = 1; | |
6205 | astbsd_on(); | |
6206 | } | |
2d21ac55 A |
6207 | #endif /* __APPLE__ */ |
6208 | } | |
6209 | ||
6210 | static void | |
6211 | dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) | |
6212 | { | |
6213 | hrtime_t now; | |
6214 | volatile uint16_t *flags; | |
6215 | cpu_t *cpu = CPU; | |
6216 | ||
6217 | if (dtrace_destructive_disallow) | |
6218 | return; | |
6219 | ||
6220 | flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; | |
6221 | ||
6222 | now = dtrace_gethrtime(); | |
6223 | ||
6224 | if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { | |
6225 | /* | |
6226 | * We need to advance the mark to the current time. | |
6227 | */ | |
6228 | cpu->cpu_dtrace_chillmark = now; | |
6229 | cpu->cpu_dtrace_chilled = 0; | |
6230 | } | |
6231 | ||
6232 | /* | |
6233 | * Now check to see if the requested chill time would take us over | |
6234 | * the maximum amount of time allowed in the chill interval. (Or | |
6235 | * worse, if the calculation itself induces overflow.) | |
6236 | */ | |
6237 | if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || | |
6238 | cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { | |
6239 | *flags |= CPU_DTRACE_ILLOP; | |
6240 | return; | |
6241 | } | |
6242 | ||
6243 | while (dtrace_gethrtime() - now < val) | |
6244 | continue; | |
6245 | ||
6246 | /* | |
6247 | * Normally, we assure that the value of the variable "timestamp" does | |
6248 | * not change within an ECB. The presence of chill() represents an | |
6249 | * exception to this rule, however. | |
6250 | */ | |
6251 | mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; | |
6252 | cpu->cpu_dtrace_chilled += val; | |
6253 | } | |
6254 | ||
6255 | static void | |
6256 | dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, | |
6257 | uint64_t *buf, uint64_t arg) | |
6258 | { | |
6259 | int nframes = DTRACE_USTACK_NFRAMES(arg); | |
6260 | int strsize = DTRACE_USTACK_STRSIZE(arg); | |
6261 | uint64_t *pcs = &buf[1], *fps; | |
6262 | char *str = (char *)&pcs[nframes]; | |
6263 | int size, offs = 0, i, j; | |
6264 | uintptr_t old = mstate->dtms_scratch_ptr, saved; | |
6265 | uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
6266 | char *sym; | |
6267 | ||
6268 | /* | |
6269 | * Should be taking a faster path if string space has not been | |
6270 | * allocated. | |
6271 | */ | |
6272 | ASSERT(strsize != 0); | |
6273 | ||
6274 | /* | |
6275 | * We will first allocate some temporary space for the frame pointers. | |
6276 | */ | |
6277 | fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); | |
6278 | size = (uintptr_t)fps - mstate->dtms_scratch_ptr + | |
6279 | (nframes * sizeof (uint64_t)); | |
6280 | ||
b0d623f7 A |
6281 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
6282 | if (!DTRACE_INSCRATCH(mstate, size)) { | |
6283 | #else | |
6284 | if (!DTRACE_INSCRATCH(mstate, (uintptr_t)size)) { | |
6285 | #endif /* __APPLE__ */ | |
2d21ac55 A |
6286 | /* |
6287 | * Not enough room for our frame pointers -- need to indicate | |
6288 | * that we ran out of scratch space. | |
6289 | */ | |
6290 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); | |
6291 | return; | |
6292 | } | |
6293 | ||
6294 | mstate->dtms_scratch_ptr += size; | |
6295 | saved = mstate->dtms_scratch_ptr; | |
6296 | ||
6297 | /* | |
6298 | * Now get a stack with both program counters and frame pointers. | |
6299 | */ | |
6300 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
6301 | dtrace_getufpstack(buf, fps, nframes + 1); | |
6302 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); | |
6303 | ||
6304 | /* | |
6305 | * If that faulted, we're cooked. | |
6306 | */ | |
6307 | if (*flags & CPU_DTRACE_FAULT) | |
6308 | goto out; | |
6309 | ||
6310 | /* | |
6311 | * Now we want to walk up the stack, calling the USTACK helper. For | |
6312 | * each iteration, we restore the scratch pointer. | |
6313 | */ | |
6314 | for (i = 0; i < nframes; i++) { | |
6315 | mstate->dtms_scratch_ptr = saved; | |
6316 | ||
6317 | if (offs >= strsize) | |
6318 | break; | |
6319 | ||
6320 | sym = (char *)(uintptr_t)dtrace_helper( | |
6321 | DTRACE_HELPER_ACTION_USTACK, | |
6322 | mstate, state, pcs[i], fps[i]); | |
6323 | ||
6324 | /* | |
6325 | * If we faulted while running the helper, we're going to | |
6326 | * clear the fault and null out the corresponding string. | |
6327 | */ | |
6328 | if (*flags & CPU_DTRACE_FAULT) { | |
6329 | *flags &= ~CPU_DTRACE_FAULT; | |
6330 | str[offs++] = '\0'; | |
6331 | continue; | |
6332 | } | |
6333 | ||
6334 | if (sym == NULL) { | |
6335 | str[offs++] = '\0'; | |
6336 | continue; | |
6337 | } | |
6338 | ||
6339 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
6340 | ||
6341 | /* | |
6342 | * Now copy in the string that the helper returned to us. | |
6343 | */ | |
6344 | for (j = 0; offs + j < strsize; j++) { | |
6345 | if ((str[offs + j] = sym[j]) == '\0') | |
6346 | break; | |
6347 | } | |
6348 | ||
6349 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); | |
6350 | ||
6351 | offs += j + 1; | |
6352 | } | |
6353 | ||
6354 | if (offs >= strsize) { | |
6355 | /* | |
6356 | * If we didn't have room for all of the strings, we don't | |
6357 | * abort processing -- this needn't be a fatal error -- but we | |
6358 | * still want to increment a counter (dts_stkstroverflows) to | |
6359 | * allow this condition to be warned about. (If this is from | |
6360 | * a jstack() action, it is easily tuned via jstackstrsize.) | |
6361 | */ | |
6362 | dtrace_error(&state->dts_stkstroverflows); | |
6363 | } | |
6364 | ||
6365 | while (offs < strsize) | |
6366 | str[offs++] = '\0'; | |
6367 | ||
6368 | out: | |
6369 | mstate->dtms_scratch_ptr = old; | |
6370 | } | |
6371 | ||
6372 | /* | |
6373 | * If you're looking for the epicenter of DTrace, you just found it. This | |
6374 | * is the function called by the provider to fire a probe -- from which all | |
6375 | * subsequent probe-context DTrace activity emanates. | |
6376 | */ | |
6377 | #if !defined(__APPLE__) | |
6378 | void | |
6379 | dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, | |
6380 | uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) | |
6381 | #else | |
6382 | static void | |
6383 | __dtrace_probe(dtrace_id_t id, uint64_t arg0, uint64_t arg1, | |
6384 | uint64_t arg2, uint64_t arg3, uint64_t arg4) | |
6385 | #endif /* __APPLE__ */ | |
6386 | { | |
6387 | processorid_t cpuid; | |
6388 | dtrace_icookie_t cookie; | |
6389 | dtrace_probe_t *probe; | |
6390 | dtrace_mstate_t mstate; | |
6391 | dtrace_ecb_t *ecb; | |
6392 | dtrace_action_t *act; | |
6393 | intptr_t offs; | |
6394 | size_t size; | |
6395 | int vtime, onintr; | |
6396 | volatile uint16_t *flags; | |
6397 | hrtime_t now; | |
6398 | ||
6399 | #if !defined(__APPLE__) | |
6400 | /* | |
6401 | * Kick out immediately if this CPU is still being born (in which case | |
b0d623f7 A |
6402 | * curthread will be set to -1) or the current thread can't allow |
6403 | * probes in its current context. | |
2d21ac55 | 6404 | */ |
b0d623f7 | 6405 | if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) |
2d21ac55 A |
6406 | return; |
6407 | #else | |
b0d623f7 | 6408 | /* Not a concern for Darwin */ |
2d21ac55 A |
6409 | #endif /* __APPLE__ */ |
6410 | ||
6411 | cookie = dtrace_interrupt_disable(); | |
6412 | probe = dtrace_probes[id - 1]; | |
6413 | cpuid = CPU->cpu_id; | |
6414 | onintr = CPU_ON_INTR(CPU); | |
6415 | ||
6416 | #if !defined(__APPLE__) | |
6417 | if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && | |
6418 | probe->dtpr_predcache == curthread->t_predcache) { | |
6419 | #else | |
6420 | if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && | |
6421 | probe->dtpr_predcache == dtrace_get_thread_predcache(current_thread())) { | |
6422 | #endif /* __APPLE__ */ | |
6423 | /* | |
6424 | * We have hit in the predicate cache; we know that | |
6425 | * this predicate would evaluate to be false. | |
6426 | */ | |
6427 | dtrace_interrupt_enable(cookie); | |
6428 | return; | |
6429 | } | |
6430 | ||
6431 | if (panic_quiesce) { | |
6432 | /* | |
6433 | * We don't trace anything if we're panicking. | |
6434 | */ | |
6435 | dtrace_interrupt_enable(cookie); | |
6436 | return; | |
6437 | } | |
6438 | ||
6439 | #if !defined(__APPLE__) | |
6440 | now = dtrace_gethrtime(); | |
6441 | vtime = dtrace_vtime_references != 0; | |
6442 | ||
6443 | if (vtime && curthread->t_dtrace_start) | |
6444 | curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; | |
6445 | #else | |
b0d623f7 A |
6446 | /* FIXME: the time spent entering DTrace and arriving to this point is attributed |
6447 | to the current thread. Instead it should accrue to DTrace. */ | |
2d21ac55 A |
6448 | vtime = dtrace_vtime_references != 0; |
6449 | ||
6450 | if (vtime) | |
6451 | { | |
6452 | int64_t dtrace_accum_time, recent_vtime; | |
6453 | thread_t thread = current_thread(); | |
6454 | ||
6455 | dtrace_accum_time = dtrace_get_thread_tracing(thread); /* Time spent inside DTrace so far (nanoseconds) */ | |
6456 | ||
6457 | if (dtrace_accum_time >= 0) { | |
6458 | recent_vtime = dtrace_abs_to_nano(dtrace_calc_thread_recent_vtime(thread)); /* up to the moment thread vtime */ | |
6459 | ||
6460 | recent_vtime = recent_vtime - dtrace_accum_time; /* Time without DTrace contribution */ | |
6461 | ||
6462 | dtrace_set_thread_vtime(thread, recent_vtime); | |
6463 | } | |
6464 | } | |
6465 | ||
6466 | now = dtrace_gethrtime(); /* must not precede dtrace_calc_thread_recent_vtime() call! */ | |
6467 | #endif /* __APPLE__ */ | |
6468 | ||
cf7d32b8 A |
6469 | #if defined(__APPLE__) |
6470 | /* | |
6471 | * A provider may call dtrace_probe_error() in lieu of dtrace_probe() in some circumstances. | |
6472 | * See, e.g. fasttrap_isa.c. However the provider has no access to ECB context, so passes | |
b0d623f7 | 6473 | * 0 through "arg0" and the probe_id of the overridden probe as arg1. Detect that here |
cf7d32b8 A |
6474 | * and cons up a viable state (from the probe_id). |
6475 | */ | |
b0d623f7 | 6476 | if (dtrace_probeid_error == id && 0 == arg0) { |
cf7d32b8 A |
6477 | dtrace_id_t ftp_id = (dtrace_id_t)arg1; |
6478 | dtrace_probe_t *ftp_probe = dtrace_probes[ftp_id - 1]; | |
6479 | dtrace_ecb_t *ftp_ecb = ftp_probe->dtpr_ecb; | |
6480 | ||
6481 | if (NULL != ftp_ecb) { | |
6482 | dtrace_state_t *ftp_state = ftp_ecb->dte_state; | |
6483 | ||
6484 | arg0 = (uint64_t)(uintptr_t)ftp_state; | |
6485 | arg1 = ftp_ecb->dte_epid; | |
6486 | /* | |
6487 | * args[2-4] established by caller. | |
6488 | */ | |
6489 | ftp_state->dts_arg_error_illval = -1; /* arg5 */ | |
6490 | } | |
6491 | } | |
6492 | #endif /* __APPLE__ */ | |
6493 | ||
b0d623f7 | 6494 | mstate.dtms_difo = NULL; |
2d21ac55 | 6495 | mstate.dtms_probe = probe; |
b0d623f7 | 6496 | mstate.dtms_strtok = NULL; |
2d21ac55 A |
6497 | mstate.dtms_arg[0] = arg0; |
6498 | mstate.dtms_arg[1] = arg1; | |
6499 | mstate.dtms_arg[2] = arg2; | |
6500 | mstate.dtms_arg[3] = arg3; | |
6501 | mstate.dtms_arg[4] = arg4; | |
6502 | ||
6503 | flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; | |
6504 | ||
6505 | for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { | |
6506 | dtrace_predicate_t *pred = ecb->dte_predicate; | |
6507 | dtrace_state_t *state = ecb->dte_state; | |
6508 | dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; | |
6509 | dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; | |
6510 | dtrace_vstate_t *vstate = &state->dts_vstate; | |
6511 | dtrace_provider_t *prov = probe->dtpr_provider; | |
6512 | int committed = 0; | |
6513 | caddr_t tomax; | |
6514 | ||
6515 | /* | |
6516 | * A little subtlety with the following (seemingly innocuous) | |
6517 | * declaration of the automatic 'val': by looking at the | |
6518 | * code, you might think that it could be declared in the | |
6519 | * action processing loop, below. (That is, it's only used in | |
6520 | * the action processing loop.) However, it must be declared | |
6521 | * out of that scope because in the case of DIF expression | |
6522 | * arguments to aggregating actions, one iteration of the | |
6523 | * action loop will use the last iteration's value. | |
6524 | */ | |
6525 | #ifdef lint | |
6526 | uint64_t val = 0; | |
6527 | #else | |
c910b4d9 | 6528 | uint64_t val = 0; |
2d21ac55 A |
6529 | #endif |
6530 | ||
6531 | mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; | |
6532 | *flags &= ~CPU_DTRACE_ERROR; | |
6533 | ||
6534 | if (prov == dtrace_provider) { | |
6535 | /* | |
6536 | * If dtrace itself is the provider of this probe, | |
6537 | * we're only going to continue processing the ECB if | |
6538 | * arg0 (the dtrace_state_t) is equal to the ECB's | |
6539 | * creating state. (This prevents disjoint consumers | |
6540 | * from seeing one another's metaprobes.) | |
6541 | */ | |
6542 | if (arg0 != (uint64_t)(uintptr_t)state) | |
6543 | continue; | |
6544 | } | |
6545 | ||
6546 | if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { | |
6547 | /* | |
6548 | * We're not currently active. If our provider isn't | |
6549 | * the dtrace pseudo provider, we're not interested. | |
6550 | */ | |
6551 | if (prov != dtrace_provider) | |
6552 | continue; | |
6553 | ||
6554 | /* | |
6555 | * Now we must further check if we are in the BEGIN | |
6556 | * probe. If we are, we will only continue processing | |
6557 | * if we're still in WARMUP -- if one BEGIN enabling | |
6558 | * has invoked the exit() action, we don't want to | |
6559 | * evaluate subsequent BEGIN enablings. | |
6560 | */ | |
6561 | if (probe->dtpr_id == dtrace_probeid_begin && | |
6562 | state->dts_activity != DTRACE_ACTIVITY_WARMUP) { | |
6563 | ASSERT(state->dts_activity == | |
6564 | DTRACE_ACTIVITY_DRAINING); | |
6565 | continue; | |
6566 | } | |
6567 | } | |
6568 | ||
2d21ac55 A |
6569 | if (ecb->dte_cond) { |
6570 | /* | |
6571 | * If the dte_cond bits indicate that this | |
6572 | * consumer is only allowed to see user-mode firings | |
6573 | * of this probe, call the provider's dtps_usermode() | |
6574 | * entry point to check that the probe was fired | |
6575 | * while in a user context. Skip this ECB if that's | |
6576 | * not the case. | |
6577 | */ | |
6578 | if ((ecb->dte_cond & DTRACE_COND_USERMODE) && | |
6579 | prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, | |
6580 | probe->dtpr_id, probe->dtpr_arg) == 0) | |
6581 | continue; | |
6582 | ||
6583 | /* | |
6584 | * This is more subtle than it looks. We have to be | |
6585 | * absolutely certain that CRED() isn't going to | |
6586 | * change out from under us so it's only legit to | |
6587 | * examine that structure if we're in constrained | |
6588 | * situations. Currently, the only times we'll this | |
6589 | * check is if a non-super-user has enabled the | |
6590 | * profile or syscall providers -- providers that | |
6591 | * allow visibility of all processes. For the | |
6592 | * profile case, the check above will ensure that | |
6593 | * we're examining a user context. | |
6594 | */ | |
6595 | if (ecb->dte_cond & DTRACE_COND_OWNER) { | |
6596 | cred_t *cr; | |
6597 | cred_t *s_cr = | |
6598 | ecb->dte_state->dts_cred.dcr_cred; | |
6599 | proc_t *proc; | |
b0d623f7 | 6600 | #pragma unused(proc) /* __APPLE__ */ |
2d21ac55 A |
6601 | |
6602 | ASSERT(s_cr != NULL); | |
6603 | ||
6604 | #if !defined(__APPLE__) | |
6605 | if ((cr = CRED()) == NULL || | |
6606 | #else | |
6607 | if ((cr = dtrace_CRED()) == NULL || | |
6608 | #endif /* __APPLE__ */ | |
6609 | s_cr->cr_uid != cr->cr_uid || | |
6610 | s_cr->cr_uid != cr->cr_ruid || | |
6611 | s_cr->cr_uid != cr->cr_suid || | |
6612 | s_cr->cr_gid != cr->cr_gid || | |
6613 | s_cr->cr_gid != cr->cr_rgid || | |
6614 | s_cr->cr_gid != cr->cr_sgid || | |
6615 | #if !defined(__APPLE__) | |
6616 | (proc = ttoproc(curthread)) == NULL || | |
6617 | (proc->p_flag & SNOCD)) | |
6618 | #else | |
6619 | 1) /* Darwin omits "No Core Dump" flag. */ | |
6620 | #endif /* __APPLE__ */ | |
6621 | continue; | |
6622 | } | |
6623 | ||
6624 | if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { | |
6625 | cred_t *cr; | |
6626 | cred_t *s_cr = | |
6627 | ecb->dte_state->dts_cred.dcr_cred; | |
b0d623f7 | 6628 | #pragma unused(cr, s_cr) /* __APPLE__ */ |
2d21ac55 A |
6629 | |
6630 | ASSERT(s_cr != NULL); | |
6631 | ||
b0d623f7 | 6632 | #if !defined(__APPLE__) |
2d21ac55 A |
6633 | if ((cr = CRED()) == NULL || |
6634 | s_cr->cr_zone->zone_id != | |
6635 | cr->cr_zone->zone_id) | |
6636 | continue; | |
b0d623f7 A |
6637 | #else |
6638 | /* Darwin doesn't do zones. */ | |
2d21ac55 A |
6639 | #endif /* __APPLE__ */ |
6640 | } | |
6641 | } | |
6642 | ||
6643 | if (now - state->dts_alive > dtrace_deadman_timeout) { | |
6644 | /* | |
6645 | * We seem to be dead. Unless we (a) have kernel | |
6646 | * destructive permissions (b) have expicitly enabled | |
6647 | * destructive actions and (c) destructive actions have | |
6648 | * not been disabled, we're going to transition into | |
6649 | * the KILLED state, from which no further processing | |
6650 | * on this state will be performed. | |
6651 | */ | |
6652 | if (!dtrace_priv_kernel_destructive(state) || | |
6653 | !state->dts_cred.dcr_destructive || | |
6654 | dtrace_destructive_disallow) { | |
6655 | void *activity = &state->dts_activity; | |
6656 | dtrace_activity_t current; | |
6657 | ||
6658 | do { | |
6659 | current = state->dts_activity; | |
6660 | } while (dtrace_cas32(activity, current, | |
6661 | DTRACE_ACTIVITY_KILLED) != current); | |
6662 | ||
6663 | continue; | |
6664 | } | |
6665 | } | |
6666 | ||
6667 | if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, | |
6668 | ecb->dte_alignment, state, &mstate)) < 0) | |
6669 | continue; | |
6670 | ||
6671 | tomax = buf->dtb_tomax; | |
6672 | ASSERT(tomax != NULL); | |
6673 | ||
6674 | if (ecb->dte_size != 0) | |
6675 | DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); | |
6676 | ||
6677 | mstate.dtms_epid = ecb->dte_epid; | |
6678 | mstate.dtms_present |= DTRACE_MSTATE_EPID; | |
6679 | ||
b0d623f7 A |
6680 | if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) |
6681 | mstate.dtms_access = DTRACE_ACCESS_KERNEL; | |
6682 | else | |
6683 | mstate.dtms_access = 0; | |
6684 | ||
2d21ac55 A |
6685 | if (pred != NULL) { |
6686 | dtrace_difo_t *dp = pred->dtp_difo; | |
6687 | int rval; | |
6688 | ||
6689 | rval = dtrace_dif_emulate(dp, &mstate, vstate, state); | |
6690 | ||
6691 | if (!(*flags & CPU_DTRACE_ERROR) && !rval) { | |
6692 | dtrace_cacheid_t cid = probe->dtpr_predcache; | |
6693 | ||
6694 | if (cid != DTRACE_CACHEIDNONE && !onintr) { | |
6695 | /* | |
6696 | * Update the predicate cache... | |
6697 | */ | |
6698 | ASSERT(cid == pred->dtp_cacheid); | |
6699 | #if !defined(__APPLE__) | |
6700 | curthread->t_predcache = cid; | |
6701 | #else | |
6702 | dtrace_set_thread_predcache(current_thread(), cid); | |
6703 | #endif /* __APPLE__ */ | |
6704 | } | |
6705 | ||
6706 | continue; | |
6707 | } | |
6708 | } | |
6709 | ||
6710 | for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && | |
6711 | act != NULL; act = act->dta_next) { | |
6712 | size_t valoffs; | |
6713 | dtrace_difo_t *dp; | |
6714 | dtrace_recdesc_t *rec = &act->dta_rec; | |
6715 | ||
6716 | size = rec->dtrd_size; | |
6717 | valoffs = offs + rec->dtrd_offset; | |
6718 | ||
6719 | if (DTRACEACT_ISAGG(act->dta_kind)) { | |
6720 | uint64_t v = 0xbad; | |
6721 | dtrace_aggregation_t *agg; | |
6722 | ||
6723 | agg = (dtrace_aggregation_t *)act; | |
6724 | ||
6725 | if ((dp = act->dta_difo) != NULL) | |
6726 | v = dtrace_dif_emulate(dp, | |
6727 | &mstate, vstate, state); | |
6728 | ||
6729 | if (*flags & CPU_DTRACE_ERROR) | |
6730 | continue; | |
6731 | ||
6732 | /* | |
6733 | * Note that we always pass the expression | |
6734 | * value from the previous iteration of the | |
6735 | * action loop. This value will only be used | |
6736 | * if there is an expression argument to the | |
6737 | * aggregating action, denoted by the | |
6738 | * dtag_hasarg field. | |
6739 | */ | |
6740 | dtrace_aggregate(agg, buf, | |
6741 | offs, aggbuf, v, val); | |
6742 | continue; | |
6743 | } | |
6744 | ||
6745 | switch (act->dta_kind) { | |
6746 | case DTRACEACT_STOP: | |
6747 | if (dtrace_priv_proc_destructive(state)) | |
6748 | dtrace_action_stop(); | |
6749 | continue; | |
6750 | ||
6751 | case DTRACEACT_BREAKPOINT: | |
6752 | if (dtrace_priv_kernel_destructive(state)) | |
6753 | dtrace_action_breakpoint(ecb); | |
6754 | continue; | |
6755 | ||
6756 | case DTRACEACT_PANIC: | |
6757 | if (dtrace_priv_kernel_destructive(state)) | |
6758 | dtrace_action_panic(ecb); | |
6759 | continue; | |
6760 | ||
6761 | case DTRACEACT_STACK: | |
6762 | if (!dtrace_priv_kernel(state)) | |
6763 | continue; | |
6764 | ||
b0d623f7 | 6765 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
6766 | dtrace_getpcstack((pc_t *)(tomax + valoffs), |
6767 | size / sizeof (pc_t), probe->dtpr_aframes, | |
6768 | DTRACE_ANCHORED(probe) ? NULL : | |
6769 | (uint32_t *)arg0); | |
b0d623f7 A |
6770 | #else |
6771 | dtrace_getpcstack((pc_t *)(tomax + valoffs), | |
6772 | size / sizeof (pc_t), probe->dtpr_aframes, | |
6773 | DTRACE_ANCHORED(probe) ? NULL : | |
6774 | (uint32_t *)(uintptr_t)arg0); | |
6775 | #endif /* __APPLE__ */ | |
2d21ac55 A |
6776 | |
6777 | continue; | |
6778 | ||
6779 | case DTRACEACT_JSTACK: | |
6780 | case DTRACEACT_USTACK: | |
6781 | if (!dtrace_priv_proc(state)) | |
6782 | continue; | |
6783 | ||
6784 | /* | |
6785 | * See comment in DIF_VAR_PID. | |
6786 | */ | |
6787 | if (DTRACE_ANCHORED(mstate.dtms_probe) && | |
6788 | CPU_ON_INTR(CPU)) { | |
6789 | int depth = DTRACE_USTACK_NFRAMES( | |
6790 | rec->dtrd_arg) + 1; | |
6791 | ||
6792 | dtrace_bzero((void *)(tomax + valoffs), | |
6793 | DTRACE_USTACK_STRSIZE(rec->dtrd_arg) | |
6794 | + depth * sizeof (uint64_t)); | |
6795 | ||
6796 | continue; | |
6797 | } | |
6798 | ||
6799 | if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && | |
6800 | curproc->p_dtrace_helpers != NULL) { | |
6801 | /* | |
6802 | * This is the slow path -- we have | |
6803 | * allocated string space, and we're | |
6804 | * getting the stack of a process that | |
6805 | * has helpers. Call into a separate | |
6806 | * routine to perform this processing. | |
6807 | */ | |
6808 | dtrace_action_ustack(&mstate, state, | |
6809 | (uint64_t *)(tomax + valoffs), | |
6810 | rec->dtrd_arg); | |
6811 | continue; | |
6812 | } | |
6813 | ||
6814 | DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); | |
6815 | dtrace_getupcstack((uint64_t *) | |
6816 | (tomax + valoffs), | |
6817 | DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); | |
6818 | DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); | |
6819 | continue; | |
6820 | ||
6821 | default: | |
6822 | break; | |
6823 | } | |
6824 | ||
6825 | dp = act->dta_difo; | |
6826 | ASSERT(dp != NULL); | |
6827 | ||
6828 | val = dtrace_dif_emulate(dp, &mstate, vstate, state); | |
6829 | ||
6830 | if (*flags & CPU_DTRACE_ERROR) | |
6831 | continue; | |
6832 | ||
6833 | switch (act->dta_kind) { | |
6834 | case DTRACEACT_SPECULATE: | |
6835 | ASSERT(buf == &state->dts_buffer[cpuid]); | |
6836 | buf = dtrace_speculation_buffer(state, | |
6837 | cpuid, val); | |
6838 | ||
6839 | if (buf == NULL) { | |
6840 | *flags |= CPU_DTRACE_DROP; | |
6841 | continue; | |
6842 | } | |
6843 | ||
6844 | offs = dtrace_buffer_reserve(buf, | |
6845 | ecb->dte_needed, ecb->dte_alignment, | |
6846 | state, NULL); | |
6847 | ||
6848 | if (offs < 0) { | |
6849 | *flags |= CPU_DTRACE_DROP; | |
6850 | continue; | |
6851 | } | |
6852 | ||
6853 | tomax = buf->dtb_tomax; | |
6854 | ASSERT(tomax != NULL); | |
6855 | ||
6856 | if (ecb->dte_size != 0) | |
6857 | DTRACE_STORE(uint32_t, tomax, offs, | |
6858 | ecb->dte_epid); | |
6859 | continue; | |
6860 | ||
6861 | case DTRACEACT_CHILL: | |
6862 | if (dtrace_priv_kernel_destructive(state)) | |
6863 | dtrace_action_chill(&mstate, val); | |
6864 | continue; | |
6865 | ||
6866 | case DTRACEACT_RAISE: | |
6867 | if (dtrace_priv_proc_destructive(state)) | |
6868 | dtrace_action_raise(val); | |
6869 | continue; | |
6870 | ||
6871 | case DTRACEACT_COMMIT: | |
6872 | ASSERT(!committed); | |
6873 | ||
6874 | /* | |
6875 | * We need to commit our buffer state. | |
6876 | */ | |
6877 | if (ecb->dte_size) | |
6878 | buf->dtb_offset = offs + ecb->dte_size; | |
6879 | buf = &state->dts_buffer[cpuid]; | |
6880 | dtrace_speculation_commit(state, cpuid, val); | |
6881 | committed = 1; | |
6882 | continue; | |
6883 | ||
6884 | case DTRACEACT_DISCARD: | |
6885 | dtrace_speculation_discard(state, cpuid, val); | |
6886 | continue; | |
6887 | ||
6888 | case DTRACEACT_DIFEXPR: | |
6889 | case DTRACEACT_LIBACT: | |
6890 | case DTRACEACT_PRINTF: | |
6891 | case DTRACEACT_PRINTA: | |
6892 | case DTRACEACT_SYSTEM: | |
6893 | case DTRACEACT_FREOPEN: | |
b0d623f7 A |
6894 | #if defined(__APPLE__) |
6895 | case DTRACEACT_APPLEBINARY: | |
6896 | #endif /* __APPLE__ */ | |
2d21ac55 A |
6897 | break; |
6898 | ||
6899 | case DTRACEACT_SYM: | |
6900 | case DTRACEACT_MOD: | |
6901 | if (!dtrace_priv_kernel(state)) | |
6902 | continue; | |
6903 | break; | |
6904 | ||
6905 | #if !defined(__APPLE__) | |
6906 | case DTRACEACT_USYM: | |
6907 | case DTRACEACT_UMOD: | |
6908 | case DTRACEACT_UADDR: { | |
6909 | struct pid *pid = curthread->t_procp->p_pidp; | |
6910 | ||
6911 | if (!dtrace_priv_proc(state)) | |
6912 | continue; | |
6913 | ||
6914 | DTRACE_STORE(uint64_t, tomax, | |
6915 | valoffs, (uint64_t)pid->pid_id); | |
6916 | DTRACE_STORE(uint64_t, tomax, | |
6917 | valoffs + sizeof (uint64_t), val); | |
6918 | ||
6919 | continue; | |
6920 | } | |
6921 | #else | |
6922 | case DTRACEACT_USYM: | |
6923 | case DTRACEACT_UMOD: | |
6924 | case DTRACEACT_UADDR: { | |
6925 | if (!dtrace_priv_proc(state)) | |
6926 | continue; | |
6927 | ||
6928 | DTRACE_STORE(uint64_t, tomax, | |
6929 | valoffs, (uint64_t)proc_selfpid()); | |
6930 | DTRACE_STORE(uint64_t, tomax, | |
6931 | valoffs + sizeof (uint64_t), val); | |
6932 | ||
6933 | continue; | |
6934 | } | |
6935 | #endif /* __APPLE__ */ | |
6936 | ||
6937 | case DTRACEACT_EXIT: { | |
6938 | /* | |
6939 | * For the exit action, we are going to attempt | |
6940 | * to atomically set our activity to be | |
6941 | * draining. If this fails (either because | |
6942 | * another CPU has beat us to the exit action, | |
6943 | * or because our current activity is something | |
6944 | * other than ACTIVE or WARMUP), we will | |
6945 | * continue. This assures that the exit action | |
6946 | * can be successfully recorded at most once | |
6947 | * when we're in the ACTIVE state. If we're | |
6948 | * encountering the exit() action while in | |
6949 | * COOLDOWN, however, we want to honor the new | |
6950 | * status code. (We know that we're the only | |
6951 | * thread in COOLDOWN, so there is no race.) | |
6952 | */ | |
6953 | void *activity = &state->dts_activity; | |
6954 | dtrace_activity_t current = state->dts_activity; | |
6955 | ||
6956 | if (current == DTRACE_ACTIVITY_COOLDOWN) | |
6957 | break; | |
6958 | ||
6959 | if (current != DTRACE_ACTIVITY_WARMUP) | |
6960 | current = DTRACE_ACTIVITY_ACTIVE; | |
6961 | ||
6962 | if (dtrace_cas32(activity, current, | |
6963 | DTRACE_ACTIVITY_DRAINING) != current) { | |
6964 | *flags |= CPU_DTRACE_DROP; | |
6965 | continue; | |
6966 | } | |
6967 | ||
6968 | break; | |
6969 | } | |
6970 | ||
6971 | default: | |
6972 | ASSERT(0); | |
6973 | } | |
6974 | ||
6975 | if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { | |
6976 | uintptr_t end = valoffs + size; | |
6977 | ||
b0d623f7 A |
6978 | if (!dtrace_vcanload((void *)(uintptr_t)val, |
6979 | &dp->dtdo_rtype, &mstate, vstate)) | |
6980 | continue; | |
6981 | ||
2d21ac55 A |
6982 | /* |
6983 | * If this is a string, we're going to only | |
6984 | * load until we find the zero byte -- after | |
6985 | * which we'll store zero bytes. | |
6986 | */ | |
6987 | if (dp->dtdo_rtype.dtdt_kind == | |
6988 | DIF_TYPE_STRING) { | |
6989 | char c = '\0' + 1; | |
6990 | int intuple = act->dta_intuple; | |
6991 | size_t s; | |
6992 | ||
6993 | for (s = 0; s < size; s++) { | |
6994 | if (c != '\0') | |
6995 | c = dtrace_load8(val++); | |
6996 | ||
6997 | DTRACE_STORE(uint8_t, tomax, | |
6998 | valoffs++, c); | |
6999 | ||
7000 | if (c == '\0' && intuple) | |
7001 | break; | |
7002 | } | |
7003 | ||
7004 | continue; | |
7005 | } | |
7006 | ||
7007 | while (valoffs < end) { | |
7008 | DTRACE_STORE(uint8_t, tomax, valoffs++, | |
7009 | dtrace_load8(val++)); | |
7010 | } | |
7011 | ||
7012 | continue; | |
7013 | } | |
7014 | ||
7015 | switch (size) { | |
7016 | case 0: | |
7017 | break; | |
7018 | ||
7019 | case sizeof (uint8_t): | |
7020 | DTRACE_STORE(uint8_t, tomax, valoffs, val); | |
7021 | break; | |
7022 | case sizeof (uint16_t): | |
7023 | DTRACE_STORE(uint16_t, tomax, valoffs, val); | |
7024 | break; | |
7025 | case sizeof (uint32_t): | |
7026 | DTRACE_STORE(uint32_t, tomax, valoffs, val); | |
7027 | break; | |
7028 | case sizeof (uint64_t): | |
7029 | DTRACE_STORE(uint64_t, tomax, valoffs, val); | |
7030 | break; | |
7031 | default: | |
7032 | /* | |
7033 | * Any other size should have been returned by | |
7034 | * reference, not by value. | |
7035 | */ | |
7036 | ASSERT(0); | |
7037 | break; | |
7038 | } | |
7039 | } | |
7040 | ||
7041 | if (*flags & CPU_DTRACE_DROP) | |
7042 | continue; | |
7043 | ||
7044 | if (*flags & CPU_DTRACE_FAULT) { | |
7045 | int ndx; | |
7046 | dtrace_action_t *err; | |
7047 | ||
7048 | buf->dtb_errors++; | |
7049 | ||
7050 | if (probe->dtpr_id == dtrace_probeid_error) { | |
7051 | /* | |
7052 | * There's nothing we can do -- we had an | |
7053 | * error on the error probe. We bump an | |
7054 | * error counter to at least indicate that | |
7055 | * this condition happened. | |
7056 | */ | |
7057 | dtrace_error(&state->dts_dblerrors); | |
7058 | continue; | |
7059 | } | |
7060 | ||
7061 | if (vtime) { | |
7062 | /* | |
7063 | * Before recursing on dtrace_probe(), we | |
7064 | * need to explicitly clear out our start | |
7065 | * time to prevent it from being accumulated | |
7066 | * into t_dtrace_vtime. | |
7067 | */ | |
7068 | #if !defined(__APPLE__) | |
7069 | curthread->t_dtrace_start = 0; | |
7070 | #else | |
7071 | /* Set the sign bit on t_dtrace_tracing to suspend accumulation to it. */ | |
7072 | dtrace_set_thread_tracing(current_thread(), | |
7073 | (1ULL<<63) | dtrace_get_thread_tracing(current_thread())); | |
7074 | #endif /* __APPLE__ */ | |
7075 | } | |
7076 | ||
7077 | /* | |
7078 | * Iterate over the actions to figure out which action | |
7079 | * we were processing when we experienced the error. | |
7080 | * Note that act points _past_ the faulting action; if | |
7081 | * act is ecb->dte_action, the fault was in the | |
7082 | * predicate, if it's ecb->dte_action->dta_next it's | |
7083 | * in action #1, and so on. | |
7084 | */ | |
7085 | for (err = ecb->dte_action, ndx = 0; | |
7086 | err != act; err = err->dta_next, ndx++) | |
7087 | continue; | |
7088 | ||
7089 | dtrace_probe_error(state, ecb->dte_epid, ndx, | |
7090 | (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? | |
7091 | mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), | |
7092 | cpu_core[cpuid].cpuc_dtrace_illval); | |
7093 | ||
7094 | continue; | |
7095 | } | |
7096 | ||
7097 | if (!committed) | |
7098 | buf->dtb_offset = offs + ecb->dte_size; | |
7099 | } | |
7100 | ||
7101 | #if !defined(__APPLE__) | |
7102 | if (vtime) | |
7103 | curthread->t_dtrace_start = dtrace_gethrtime(); | |
7104 | #else | |
b0d623f7 A |
7105 | /* FIXME: the time spent leaving DTrace from this point to the rti is attributed |
7106 | to the current thread. Instead it should accrue to DTrace. */ | |
2d21ac55 A |
7107 | if (vtime) { |
7108 | thread_t thread = current_thread(); | |
7109 | int64_t t = dtrace_get_thread_tracing(thread); | |
7110 | ||
7111 | if (t >= 0) { | |
7112 | /* Usual case, accumulate time spent here into t_dtrace_tracing */ | |
7113 | dtrace_set_thread_tracing(thread, t + (dtrace_gethrtime() - now)); | |
7114 | } else { | |
7115 | /* Return from error recursion. No accumulation, just clear the sign bit on t_dtrace_tracing. */ | |
7116 | dtrace_set_thread_tracing(thread, (~(1ULL<<63)) & t); | |
7117 | } | |
7118 | } | |
7119 | #endif /* __APPLE__ */ | |
7120 | ||
7121 | dtrace_interrupt_enable(cookie); | |
7122 | } | |
7123 | ||
7124 | #if defined(__APPLE__) | |
b0d623f7 A |
7125 | /* Don't allow a thread to re-enter dtrace_probe(). This could occur if a probe is encountered |
7126 | on some function in the transitive closure of the call to dtrace_probe(). Solaris has some | |
7127 | strong guarantees that this won't happen, the Darwin implementation is not so mature as to | |
7128 | make those guarantees. */ | |
2d21ac55 A |
7129 | void |
7130 | dtrace_probe(dtrace_id_t id, uint64_t arg0, uint64_t arg1, | |
7131 | uint64_t arg2, uint64_t arg3, uint64_t arg4) | |
7132 | { | |
7133 | thread_t thread = current_thread(); | |
7134 | ||
7135 | if (id == dtrace_probeid_error) { | |
7136 | __dtrace_probe(id, arg0, arg1, arg2, arg3, arg4); | |
b0d623f7 | 7137 | dtrace_getipl(); /* Defeat tail-call optimization of __dtrace_probe() */ |
2d21ac55 A |
7138 | } else if (!dtrace_get_thread_reentering(thread)) { |
7139 | dtrace_set_thread_reentering(thread, TRUE); | |
7140 | __dtrace_probe(id, arg0, arg1, arg2, arg3, arg4); | |
7141 | dtrace_set_thread_reentering(thread, FALSE); | |
7142 | } | |
b0d623f7 A |
7143 | #if DEBUG |
7144 | else __dtrace_probe(dtrace_probeid_error, 0, id, 1, -1, DTRACEFLT_UNKNOWN); | |
7145 | #endif | |
2d21ac55 A |
7146 | } |
7147 | #endif /* __APPLE__ */ | |
7148 | ||
7149 | /* | |
7150 | * DTrace Probe Hashing Functions | |
7151 | * | |
7152 | * The functions in this section (and indeed, the functions in remaining | |
7153 | * sections) are not _called_ from probe context. (Any exceptions to this are | |
7154 | * marked with a "Note:".) Rather, they are called from elsewhere in the | |
7155 | * DTrace framework to look-up probes in, add probes to and remove probes from | |
7156 | * the DTrace probe hashes. (Each probe is hashed by each element of the | |
7157 | * probe tuple -- allowing for fast lookups, regardless of what was | |
7158 | * specified.) | |
7159 | */ | |
7160 | static uint_t | |
b0d623f7 | 7161 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 7162 | dtrace_hash_str(char *p) |
b0d623f7 A |
7163 | #else |
7164 | dtrace_hash_str(const char *p) | |
7165 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7166 | { |
7167 | unsigned int g; | |
7168 | uint_t hval = 0; | |
7169 | ||
7170 | while (*p) { | |
7171 | hval = (hval << 4) + *p++; | |
7172 | if ((g = (hval & 0xf0000000)) != 0) | |
7173 | hval ^= g >> 24; | |
7174 | hval &= ~g; | |
7175 | } | |
7176 | return (hval); | |
7177 | } | |
7178 | ||
7179 | static dtrace_hash_t * | |
7180 | dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) | |
7181 | { | |
7182 | dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); | |
7183 | ||
7184 | hash->dth_stroffs = stroffs; | |
7185 | hash->dth_nextoffs = nextoffs; | |
7186 | hash->dth_prevoffs = prevoffs; | |
7187 | ||
7188 | hash->dth_size = 1; | |
7189 | hash->dth_mask = hash->dth_size - 1; | |
7190 | ||
7191 | hash->dth_tab = kmem_zalloc(hash->dth_size * | |
7192 | sizeof (dtrace_hashbucket_t *), KM_SLEEP); | |
7193 | ||
7194 | return (hash); | |
7195 | } | |
7196 | ||
b0d623f7 | 7197 | #if !defined(__APPLE__) /* Unused. Quiet compiler warning. */ |
2d21ac55 A |
7198 | static void |
7199 | dtrace_hash_destroy(dtrace_hash_t *hash) | |
7200 | { | |
b0d623f7 | 7201 | #if DEBUG |
2d21ac55 A |
7202 | int i; |
7203 | ||
7204 | for (i = 0; i < hash->dth_size; i++) | |
7205 | ASSERT(hash->dth_tab[i] == NULL); | |
7206 | #endif | |
7207 | ||
7208 | kmem_free(hash->dth_tab, | |
7209 | hash->dth_size * sizeof (dtrace_hashbucket_t *)); | |
7210 | kmem_free(hash, sizeof (dtrace_hash_t)); | |
7211 | } | |
7212 | #endif /* __APPLE__ */ | |
7213 | ||
7214 | static void | |
7215 | dtrace_hash_resize(dtrace_hash_t *hash) | |
7216 | { | |
7217 | int size = hash->dth_size, i, ndx; | |
7218 | int new_size = hash->dth_size << 1; | |
7219 | int new_mask = new_size - 1; | |
7220 | dtrace_hashbucket_t **new_tab, *bucket, *next; | |
7221 | ||
7222 | ASSERT((new_size & new_mask) == 0); | |
7223 | ||
7224 | new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); | |
7225 | ||
7226 | for (i = 0; i < size; i++) { | |
7227 | for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { | |
7228 | dtrace_probe_t *probe = bucket->dthb_chain; | |
7229 | ||
7230 | ASSERT(probe != NULL); | |
7231 | ndx = DTRACE_HASHSTR(hash, probe) & new_mask; | |
7232 | ||
7233 | next = bucket->dthb_next; | |
7234 | bucket->dthb_next = new_tab[ndx]; | |
7235 | new_tab[ndx] = bucket; | |
7236 | } | |
7237 | } | |
7238 | ||
7239 | kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); | |
7240 | hash->dth_tab = new_tab; | |
7241 | hash->dth_size = new_size; | |
7242 | hash->dth_mask = new_mask; | |
7243 | } | |
7244 | ||
7245 | static void | |
7246 | dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) | |
7247 | { | |
7248 | int hashval = DTRACE_HASHSTR(hash, new); | |
7249 | int ndx = hashval & hash->dth_mask; | |
7250 | dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; | |
7251 | dtrace_probe_t **nextp, **prevp; | |
7252 | ||
7253 | for (; bucket != NULL; bucket = bucket->dthb_next) { | |
7254 | if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) | |
7255 | goto add; | |
7256 | } | |
7257 | ||
7258 | if ((hash->dth_nbuckets >> 1) > hash->dth_size) { | |
7259 | dtrace_hash_resize(hash); | |
7260 | dtrace_hash_add(hash, new); | |
7261 | return; | |
7262 | } | |
7263 | ||
7264 | bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); | |
7265 | bucket->dthb_next = hash->dth_tab[ndx]; | |
7266 | hash->dth_tab[ndx] = bucket; | |
7267 | hash->dth_nbuckets++; | |
7268 | ||
7269 | add: | |
7270 | nextp = DTRACE_HASHNEXT(hash, new); | |
7271 | ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); | |
7272 | *nextp = bucket->dthb_chain; | |
7273 | ||
7274 | if (bucket->dthb_chain != NULL) { | |
7275 | prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); | |
7276 | ASSERT(*prevp == NULL); | |
7277 | *prevp = new; | |
7278 | } | |
7279 | ||
7280 | bucket->dthb_chain = new; | |
7281 | bucket->dthb_len++; | |
7282 | } | |
7283 | ||
7284 | static dtrace_probe_t * | |
7285 | dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) | |
7286 | { | |
7287 | int hashval = DTRACE_HASHSTR(hash, template); | |
7288 | int ndx = hashval & hash->dth_mask; | |
7289 | dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; | |
7290 | ||
7291 | for (; bucket != NULL; bucket = bucket->dthb_next) { | |
7292 | if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) | |
7293 | return (bucket->dthb_chain); | |
7294 | } | |
7295 | ||
7296 | return (NULL); | |
7297 | } | |
7298 | ||
7299 | static int | |
7300 | dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) | |
7301 | { | |
7302 | int hashval = DTRACE_HASHSTR(hash, template); | |
7303 | int ndx = hashval & hash->dth_mask; | |
7304 | dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; | |
7305 | ||
7306 | for (; bucket != NULL; bucket = bucket->dthb_next) { | |
7307 | if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) | |
7308 | return (bucket->dthb_len); | |
7309 | } | |
7310 | ||
7311 | return (NULL); | |
7312 | } | |
7313 | ||
7314 | static void | |
7315 | dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) | |
7316 | { | |
7317 | int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; | |
7318 | dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; | |
7319 | ||
7320 | dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); | |
7321 | dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); | |
7322 | ||
7323 | /* | |
7324 | * Find the bucket that we're removing this probe from. | |
7325 | */ | |
7326 | for (; bucket != NULL; bucket = bucket->dthb_next) { | |
7327 | if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) | |
7328 | break; | |
7329 | } | |
7330 | ||
7331 | ASSERT(bucket != NULL); | |
7332 | ||
7333 | if (*prevp == NULL) { | |
7334 | if (*nextp == NULL) { | |
7335 | /* | |
7336 | * The removed probe was the only probe on this | |
7337 | * bucket; we need to remove the bucket. | |
7338 | */ | |
7339 | dtrace_hashbucket_t *b = hash->dth_tab[ndx]; | |
7340 | ||
7341 | ASSERT(bucket->dthb_chain == probe); | |
7342 | ASSERT(b != NULL); | |
7343 | ||
7344 | if (b == bucket) { | |
7345 | hash->dth_tab[ndx] = bucket->dthb_next; | |
7346 | } else { | |
7347 | while (b->dthb_next != bucket) | |
7348 | b = b->dthb_next; | |
7349 | b->dthb_next = bucket->dthb_next; | |
7350 | } | |
7351 | ||
7352 | ASSERT(hash->dth_nbuckets > 0); | |
7353 | hash->dth_nbuckets--; | |
7354 | kmem_free(bucket, sizeof (dtrace_hashbucket_t)); | |
7355 | return; | |
7356 | } | |
7357 | ||
7358 | bucket->dthb_chain = *nextp; | |
7359 | } else { | |
7360 | *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; | |
7361 | } | |
7362 | ||
7363 | if (*nextp != NULL) | |
7364 | *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; | |
7365 | } | |
7366 | ||
7367 | /* | |
7368 | * DTrace Utility Functions | |
7369 | * | |
7370 | * These are random utility functions that are _not_ called from probe context. | |
7371 | */ | |
7372 | static int | |
7373 | dtrace_badattr(const dtrace_attribute_t *a) | |
7374 | { | |
7375 | return (a->dtat_name > DTRACE_STABILITY_MAX || | |
7376 | a->dtat_data > DTRACE_STABILITY_MAX || | |
7377 | a->dtat_class > DTRACE_CLASS_MAX); | |
7378 | } | |
7379 | ||
7380 | /* | |
7381 | * Return a duplicate copy of a string. If the specified string is NULL, | |
7382 | * this function returns a zero-length string. | |
7383 | */ | |
b0d623f7 | 7384 | #if !defined(__APPLE__) |
2d21ac55 A |
7385 | static char * |
7386 | dtrace_strdup(const char *str) | |
7387 | { | |
7388 | char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); | |
7389 | ||
7390 | if (str != NULL) | |
7391 | (void) strcpy(new, str); | |
7392 | ||
7393 | return (new); | |
7394 | } | |
b0d623f7 A |
7395 | #else /* Employ size bounded string operation. */ |
7396 | static char * | |
7397 | dtrace_strdup(const char *str) | |
7398 | { | |
7399 | size_t bufsize = (str != NULL ? strlen(str) : 0) + 1; | |
7400 | char *new = kmem_zalloc(bufsize, KM_SLEEP); | |
7401 | ||
7402 | if (str != NULL) | |
7403 | (void) strlcpy(new, str, bufsize); | |
7404 | ||
7405 | return (new); | |
7406 | } | |
7407 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7408 | |
7409 | #define DTRACE_ISALPHA(c) \ | |
7410 | (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) | |
7411 | ||
7412 | static int | |
7413 | dtrace_badname(const char *s) | |
7414 | { | |
7415 | char c; | |
7416 | ||
7417 | if (s == NULL || (c = *s++) == '\0') | |
7418 | return (0); | |
7419 | ||
7420 | if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') | |
7421 | return (1); | |
7422 | ||
7423 | while ((c = *s++) != '\0') { | |
7424 | if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && | |
7425 | c != '-' && c != '_' && c != '.' && c != '`') | |
7426 | return (1); | |
7427 | } | |
7428 | ||
7429 | return (0); | |
7430 | } | |
7431 | ||
7432 | static void | |
7433 | dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) | |
7434 | { | |
7435 | uint32_t priv; | |
7436 | ||
7437 | if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { | |
7438 | /* | |
7439 | * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. | |
7440 | */ | |
7441 | priv = DTRACE_PRIV_ALL; | |
7442 | } else { | |
7443 | *uidp = crgetuid(cr); | |
7444 | *zoneidp = crgetzoneid(cr); | |
7445 | ||
7446 | priv = 0; | |
7447 | if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) | |
7448 | priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; | |
7449 | else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) | |
7450 | priv |= DTRACE_PRIV_USER; | |
7451 | if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) | |
7452 | priv |= DTRACE_PRIV_PROC; | |
7453 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) | |
7454 | priv |= DTRACE_PRIV_OWNER; | |
7455 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) | |
7456 | priv |= DTRACE_PRIV_ZONEOWNER; | |
7457 | } | |
7458 | ||
7459 | *privp = priv; | |
7460 | } | |
7461 | ||
7462 | #ifdef DTRACE_ERRDEBUG | |
7463 | static void | |
7464 | dtrace_errdebug(const char *str) | |
7465 | { | |
b0d623f7 | 7466 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 7467 | int hval = dtrace_hash_str((char *)str) % DTRACE_ERRHASHSZ; |
b0d623f7 A |
7468 | #else |
7469 | int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; | |
7470 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7471 | int occupied = 0; |
7472 | ||
7473 | lck_mtx_lock(&dtrace_errlock); | |
7474 | dtrace_errlast = str; | |
7475 | #if !defined(__APPLE__) | |
7476 | dtrace_errthread = curthread; | |
7477 | #else | |
b0d623f7 | 7478 | dtrace_errthread = (kthread_t *)current_thread(); |
2d21ac55 A |
7479 | #endif /* __APPLE__ */ |
7480 | ||
7481 | while (occupied++ < DTRACE_ERRHASHSZ) { | |
7482 | if (dtrace_errhash[hval].dter_msg == str) { | |
7483 | dtrace_errhash[hval].dter_count++; | |
7484 | goto out; | |
7485 | } | |
7486 | ||
7487 | if (dtrace_errhash[hval].dter_msg != NULL) { | |
7488 | hval = (hval + 1) % DTRACE_ERRHASHSZ; | |
7489 | continue; | |
7490 | } | |
7491 | ||
7492 | dtrace_errhash[hval].dter_msg = str; | |
7493 | dtrace_errhash[hval].dter_count = 1; | |
7494 | goto out; | |
7495 | } | |
7496 | ||
7497 | panic("dtrace: undersized error hash"); | |
7498 | out: | |
7499 | lck_mtx_unlock(&dtrace_errlock); | |
7500 | } | |
7501 | #endif | |
7502 | ||
7503 | /* | |
7504 | * DTrace Matching Functions | |
7505 | * | |
7506 | * These functions are used to match groups of probes, given some elements of | |
7507 | * a probe tuple, or some globbed expressions for elements of a probe tuple. | |
7508 | */ | |
7509 | static int | |
7510 | dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, | |
7511 | zoneid_t zoneid) | |
7512 | { | |
7513 | if (priv != DTRACE_PRIV_ALL) { | |
7514 | uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; | |
7515 | uint32_t match = priv & ppriv; | |
7516 | ||
7517 | /* | |
7518 | * No PRIV_DTRACE_* privileges... | |
7519 | */ | |
7520 | if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | | |
7521 | DTRACE_PRIV_KERNEL)) == 0) | |
7522 | return (0); | |
7523 | ||
7524 | /* | |
7525 | * No matching bits, but there were bits to match... | |
7526 | */ | |
7527 | if (match == 0 && ppriv != 0) | |
7528 | return (0); | |
7529 | ||
7530 | /* | |
7531 | * Need to have permissions to the process, but don't... | |
7532 | */ | |
7533 | if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && | |
7534 | uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { | |
7535 | return (0); | |
7536 | } | |
7537 | ||
7538 | /* | |
7539 | * Need to be in the same zone unless we possess the | |
7540 | * privilege to examine all zones. | |
7541 | */ | |
7542 | if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && | |
7543 | zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { | |
7544 | return (0); | |
7545 | } | |
7546 | } | |
7547 | ||
7548 | return (1); | |
7549 | } | |
7550 | ||
7551 | /* | |
7552 | * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which | |
7553 | * consists of input pattern strings and an ops-vector to evaluate them. | |
7554 | * This function returns >0 for match, 0 for no match, and <0 for error. | |
7555 | */ | |
7556 | static int | |
7557 | dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, | |
7558 | uint32_t priv, uid_t uid, zoneid_t zoneid) | |
7559 | { | |
7560 | dtrace_provider_t *pvp = prp->dtpr_provider; | |
7561 | int rv; | |
7562 | ||
7563 | if (pvp->dtpv_defunct) | |
7564 | return (0); | |
7565 | ||
7566 | if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) | |
7567 | return (rv); | |
7568 | ||
7569 | if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) | |
7570 | return (rv); | |
7571 | ||
7572 | if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) | |
7573 | return (rv); | |
7574 | ||
7575 | if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) | |
7576 | return (rv); | |
7577 | ||
7578 | if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) | |
7579 | return (0); | |
7580 | ||
7581 | return (rv); | |
7582 | } | |
7583 | ||
7584 | /* | |
7585 | * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) | |
7586 | * interface for matching a glob pattern 'p' to an input string 's'. Unlike | |
7587 | * libc's version, the kernel version only applies to 8-bit ASCII strings. | |
7588 | * In addition, all of the recursion cases except for '*' matching have been | |
7589 | * unwound. For '*', we still implement recursive evaluation, but a depth | |
7590 | * counter is maintained and matching is aborted if we recurse too deep. | |
7591 | * The function returns 0 if no match, >0 if match, and <0 if recursion error. | |
7592 | */ | |
7593 | static int | |
7594 | dtrace_match_glob(const char *s, const char *p, int depth) | |
7595 | { | |
7596 | const char *olds; | |
7597 | char s1, c; | |
7598 | int gs; | |
7599 | ||
7600 | if (depth > DTRACE_PROBEKEY_MAXDEPTH) | |
7601 | return (-1); | |
7602 | ||
7603 | if (s == NULL) | |
7604 | s = ""; /* treat NULL as empty string */ | |
7605 | ||
7606 | top: | |
7607 | olds = s; | |
7608 | s1 = *s++; | |
7609 | ||
7610 | if (p == NULL) | |
7611 | return (0); | |
7612 | ||
7613 | if ((c = *p++) == '\0') | |
7614 | return (s1 == '\0'); | |
7615 | ||
7616 | switch (c) { | |
7617 | case '[': { | |
7618 | int ok = 0, notflag = 0; | |
7619 | char lc = '\0'; | |
7620 | ||
7621 | if (s1 == '\0') | |
7622 | return (0); | |
7623 | ||
7624 | if (*p == '!') { | |
7625 | notflag = 1; | |
7626 | p++; | |
7627 | } | |
7628 | ||
7629 | if ((c = *p++) == '\0') | |
7630 | return (0); | |
7631 | ||
7632 | do { | |
7633 | if (c == '-' && lc != '\0' && *p != ']') { | |
7634 | if ((c = *p++) == '\0') | |
7635 | return (0); | |
7636 | if (c == '\\' && (c = *p++) == '\0') | |
7637 | return (0); | |
7638 | ||
7639 | if (notflag) { | |
7640 | if (s1 < lc || s1 > c) | |
7641 | ok++; | |
7642 | else | |
7643 | return (0); | |
7644 | } else if (lc <= s1 && s1 <= c) | |
7645 | ok++; | |
7646 | ||
7647 | } else if (c == '\\' && (c = *p++) == '\0') | |
7648 | return (0); | |
7649 | ||
7650 | lc = c; /* save left-hand 'c' for next iteration */ | |
7651 | ||
7652 | if (notflag) { | |
7653 | if (s1 != c) | |
7654 | ok++; | |
7655 | else | |
7656 | return (0); | |
7657 | } else if (s1 == c) | |
7658 | ok++; | |
7659 | ||
7660 | if ((c = *p++) == '\0') | |
7661 | return (0); | |
7662 | ||
7663 | } while (c != ']'); | |
7664 | ||
7665 | if (ok) | |
7666 | goto top; | |
7667 | ||
7668 | return (0); | |
7669 | } | |
7670 | ||
7671 | case '\\': | |
7672 | if ((c = *p++) == '\0') | |
7673 | return (0); | |
7674 | /*FALLTHRU*/ | |
7675 | ||
7676 | default: | |
7677 | if (c != s1) | |
7678 | return (0); | |
7679 | /*FALLTHRU*/ | |
7680 | ||
7681 | case '?': | |
7682 | if (s1 != '\0') | |
7683 | goto top; | |
7684 | return (0); | |
7685 | ||
7686 | case '*': | |
7687 | while (*p == '*') | |
7688 | p++; /* consecutive *'s are identical to a single one */ | |
7689 | ||
7690 | if (*p == '\0') | |
7691 | return (1); | |
7692 | ||
7693 | for (s = olds; *s != '\0'; s++) { | |
7694 | if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) | |
7695 | return (gs); | |
7696 | } | |
7697 | ||
7698 | return (0); | |
7699 | } | |
7700 | } | |
7701 | ||
7702 | /*ARGSUSED*/ | |
7703 | static int | |
7704 | dtrace_match_string(const char *s, const char *p, int depth) | |
7705 | { | |
b0d623f7 A |
7706 | #pragma unused(depth) /* __APPLE__ */ |
7707 | #if !defined(__APPLE__) | |
2d21ac55 | 7708 | return (s != NULL && strcmp(s, p) == 0); |
b0d623f7 A |
7709 | #else /* Employ size bounded string operation. */ |
7710 | return (s != NULL && strncmp(s, p, strlen(s) + 1) == 0); | |
7711 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7712 | } |
7713 | ||
7714 | /*ARGSUSED*/ | |
7715 | static int | |
7716 | dtrace_match_nul(const char *s, const char *p, int depth) | |
7717 | { | |
b0d623f7 | 7718 | #pragma unused(s, p, depth) /* __APPLE__ */ |
2d21ac55 A |
7719 | return (1); /* always match the empty pattern */ |
7720 | } | |
7721 | ||
7722 | /*ARGSUSED*/ | |
7723 | static int | |
7724 | dtrace_match_nonzero(const char *s, const char *p, int depth) | |
7725 | { | |
b0d623f7 | 7726 | #pragma unused(p, depth) /* __APPLE__ */ |
2d21ac55 A |
7727 | return (s != NULL && s[0] != '\0'); |
7728 | } | |
7729 | ||
7730 | static int | |
7731 | dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, | |
7732 | zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) | |
7733 | { | |
7734 | dtrace_probe_t template, *probe; | |
7735 | dtrace_hash_t *hash = NULL; | |
7736 | int len, best = INT_MAX, nmatched = 0; | |
7737 | dtrace_id_t i; | |
7738 | ||
7739 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
7740 | ||
7741 | /* | |
7742 | * If the probe ID is specified in the key, just lookup by ID and | |
7743 | * invoke the match callback once if a matching probe is found. | |
7744 | */ | |
7745 | if (pkp->dtpk_id != DTRACE_IDNONE) { | |
7746 | if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && | |
7747 | dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { | |
7748 | (void) (*matched)(probe, arg); | |
7749 | nmatched++; | |
7750 | } | |
7751 | return (nmatched); | |
7752 | } | |
7753 | ||
b0d623f7 | 7754 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
7755 | template.dtpr_mod = (char *)pkp->dtpk_mod; |
7756 | template.dtpr_func = (char *)pkp->dtpk_func; | |
7757 | template.dtpr_name = (char *)pkp->dtpk_name; | |
b0d623f7 A |
7758 | #else |
7759 | template.dtpr_mod = (char *)(uintptr_t)pkp->dtpk_mod; | |
7760 | template.dtpr_func = (char *)(uintptr_t)pkp->dtpk_func; | |
7761 | template.dtpr_name = (char *)(uintptr_t)pkp->dtpk_name; | |
7762 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7763 | |
7764 | /* | |
7765 | * We want to find the most distinct of the module name, function | |
7766 | * name, and name. So for each one that is not a glob pattern or | |
7767 | * empty string, we perform a lookup in the corresponding hash and | |
7768 | * use the hash table with the fewest collisions to do our search. | |
7769 | */ | |
7770 | if (pkp->dtpk_mmatch == &dtrace_match_string && | |
7771 | (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { | |
7772 | best = len; | |
7773 | hash = dtrace_bymod; | |
7774 | } | |
7775 | ||
7776 | if (pkp->dtpk_fmatch == &dtrace_match_string && | |
7777 | (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { | |
7778 | best = len; | |
7779 | hash = dtrace_byfunc; | |
7780 | } | |
7781 | ||
7782 | if (pkp->dtpk_nmatch == &dtrace_match_string && | |
7783 | (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { | |
7784 | best = len; | |
7785 | hash = dtrace_byname; | |
7786 | } | |
7787 | ||
7788 | /* | |
7789 | * If we did not select a hash table, iterate over every probe and | |
7790 | * invoke our callback for each one that matches our input probe key. | |
7791 | */ | |
7792 | if (hash == NULL) { | |
b0d623f7 | 7793 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 7794 | for (i = 0; i < dtrace_nprobes; i++) { |
b0d623f7 A |
7795 | #else |
7796 | for (i = 0; i < (dtrace_id_t)dtrace_nprobes; i++) { | |
7797 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7798 | if ((probe = dtrace_probes[i]) == NULL || |
7799 | dtrace_match_probe(probe, pkp, priv, uid, | |
7800 | zoneid) <= 0) | |
7801 | continue; | |
7802 | ||
7803 | nmatched++; | |
7804 | ||
7805 | if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) | |
7806 | break; | |
7807 | } | |
7808 | ||
7809 | return (nmatched); | |
7810 | } | |
7811 | ||
7812 | /* | |
7813 | * If we selected a hash table, iterate over each probe of the same key | |
7814 | * name and invoke the callback for every probe that matches the other | |
7815 | * attributes of our input probe key. | |
7816 | */ | |
7817 | for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; | |
7818 | probe = *(DTRACE_HASHNEXT(hash, probe))) { | |
7819 | ||
7820 | if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) | |
7821 | continue; | |
7822 | ||
7823 | nmatched++; | |
7824 | ||
7825 | if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) | |
7826 | break; | |
7827 | } | |
7828 | ||
7829 | return (nmatched); | |
7830 | } | |
7831 | ||
7832 | /* | |
7833 | * Return the function pointer dtrace_probecmp() should use to compare the | |
7834 | * specified pattern with a string. For NULL or empty patterns, we select | |
7835 | * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). | |
7836 | * For non-empty non-glob strings, we use dtrace_match_string(). | |
7837 | */ | |
7838 | static dtrace_probekey_f * | |
7839 | dtrace_probekey_func(const char *p) | |
7840 | { | |
7841 | char c; | |
7842 | ||
7843 | if (p == NULL || *p == '\0') | |
7844 | return (&dtrace_match_nul); | |
7845 | ||
7846 | while ((c = *p++) != '\0') { | |
7847 | if (c == '[' || c == '?' || c == '*' || c == '\\') | |
7848 | return (&dtrace_match_glob); | |
7849 | } | |
7850 | ||
7851 | return (&dtrace_match_string); | |
7852 | } | |
7853 | ||
7854 | /* | |
7855 | * Build a probe comparison key for use with dtrace_match_probe() from the | |
7856 | * given probe description. By convention, a null key only matches anchored | |
7857 | * probes: if each field is the empty string, reset dtpk_fmatch to | |
7858 | * dtrace_match_nonzero(). | |
7859 | */ | |
7860 | static void | |
7861 | dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) | |
7862 | { | |
7863 | pkp->dtpk_prov = pdp->dtpd_provider; | |
7864 | pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); | |
7865 | ||
7866 | pkp->dtpk_mod = pdp->dtpd_mod; | |
7867 | pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); | |
7868 | ||
7869 | pkp->dtpk_func = pdp->dtpd_func; | |
7870 | pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); | |
7871 | ||
7872 | pkp->dtpk_name = pdp->dtpd_name; | |
7873 | pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); | |
7874 | ||
7875 | pkp->dtpk_id = pdp->dtpd_id; | |
7876 | ||
7877 | if (pkp->dtpk_id == DTRACE_IDNONE && | |
7878 | pkp->dtpk_pmatch == &dtrace_match_nul && | |
7879 | pkp->dtpk_mmatch == &dtrace_match_nul && | |
7880 | pkp->dtpk_fmatch == &dtrace_match_nul && | |
7881 | pkp->dtpk_nmatch == &dtrace_match_nul) | |
7882 | pkp->dtpk_fmatch = &dtrace_match_nonzero; | |
7883 | } | |
7884 | ||
7885 | /* | |
7886 | * DTrace Provider-to-Framework API Functions | |
7887 | * | |
7888 | * These functions implement much of the Provider-to-Framework API, as | |
7889 | * described in <sys/dtrace.h>. The parts of the API not in this section are | |
7890 | * the functions in the API for probe management (found below), and | |
7891 | * dtrace_probe() itself (found above). | |
7892 | */ | |
7893 | ||
7894 | /* | |
7895 | * Register the calling provider with the DTrace framework. This should | |
7896 | * generally be called by DTrace providers in their attach(9E) entry point. | |
7897 | */ | |
7898 | int | |
7899 | dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, | |
7900 | cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) | |
7901 | { | |
7902 | dtrace_provider_t *provider; | |
7903 | ||
7904 | if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { | |
7905 | cmn_err(CE_WARN, "failed to register provider '%s': invalid " | |
7906 | "arguments", name ? name : "<NULL>"); | |
7907 | return (EINVAL); | |
7908 | } | |
7909 | ||
7910 | if (name[0] == '\0' || dtrace_badname(name)) { | |
7911 | cmn_err(CE_WARN, "failed to register provider '%s': invalid " | |
7912 | "provider name", name); | |
7913 | return (EINVAL); | |
7914 | } | |
7915 | ||
7916 | if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || | |
7917 | pops->dtps_enable == NULL || pops->dtps_disable == NULL || | |
7918 | pops->dtps_destroy == NULL || | |
7919 | ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { | |
7920 | cmn_err(CE_WARN, "failed to register provider '%s': invalid " | |
7921 | "provider ops", name); | |
7922 | return (EINVAL); | |
7923 | } | |
7924 | ||
7925 | if (dtrace_badattr(&pap->dtpa_provider) || | |
7926 | dtrace_badattr(&pap->dtpa_mod) || | |
7927 | dtrace_badattr(&pap->dtpa_func) || | |
7928 | dtrace_badattr(&pap->dtpa_name) || | |
7929 | dtrace_badattr(&pap->dtpa_args)) { | |
7930 | cmn_err(CE_WARN, "failed to register provider '%s': invalid " | |
7931 | "provider attributes", name); | |
7932 | return (EINVAL); | |
7933 | } | |
7934 | ||
7935 | if (priv & ~DTRACE_PRIV_ALL) { | |
7936 | cmn_err(CE_WARN, "failed to register provider '%s': invalid " | |
7937 | "privilege attributes", name); | |
7938 | return (EINVAL); | |
7939 | } | |
7940 | ||
7941 | if ((priv & DTRACE_PRIV_KERNEL) && | |
7942 | (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && | |
7943 | pops->dtps_usermode == NULL) { | |
7944 | cmn_err(CE_WARN, "failed to register provider '%s': need " | |
7945 | "dtps_usermode() op for given privilege attributes", name); | |
7946 | return (EINVAL); | |
7947 | } | |
7948 | ||
7949 | provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); | |
b0d623f7 | 7950 | #if !defined(__APPLE__) |
2d21ac55 A |
7951 | provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); |
7952 | (void) strcpy(provider->dtpv_name, name); | |
b0d623f7 A |
7953 | #else /* Employ size bounded string operation. */ |
7954 | { | |
7955 | size_t bufsize = strlen(name) + 1; | |
7956 | provider->dtpv_name = kmem_alloc(bufsize, KM_SLEEP); | |
7957 | (void) strlcpy(provider->dtpv_name, name, bufsize); | |
7958 | } | |
7959 | #endif /* __APPLE__ */ | |
2d21ac55 A |
7960 | |
7961 | provider->dtpv_attr = *pap; | |
7962 | provider->dtpv_priv.dtpp_flags = priv; | |
7963 | if (cr != NULL) { | |
7964 | provider->dtpv_priv.dtpp_uid = crgetuid(cr); | |
7965 | provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); | |
7966 | } | |
7967 | provider->dtpv_pops = *pops; | |
7968 | ||
7969 | if (pops->dtps_provide == NULL) { | |
7970 | ASSERT(pops->dtps_provide_module != NULL); | |
7971 | provider->dtpv_pops.dtps_provide = | |
7972 | (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; | |
7973 | } | |
7974 | ||
7975 | if (pops->dtps_provide_module == NULL) { | |
7976 | ASSERT(pops->dtps_provide != NULL); | |
7977 | provider->dtpv_pops.dtps_provide_module = | |
7978 | (void (*)(void *, struct modctl *))dtrace_nullop; | |
7979 | } | |
7980 | ||
7981 | if (pops->dtps_suspend == NULL) { | |
7982 | ASSERT(pops->dtps_resume == NULL); | |
7983 | provider->dtpv_pops.dtps_suspend = | |
7984 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; | |
7985 | provider->dtpv_pops.dtps_resume = | |
7986 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; | |
7987 | } | |
7988 | ||
7989 | provider->dtpv_arg = arg; | |
7990 | *idp = (dtrace_provider_id_t)provider; | |
7991 | ||
7992 | if (pops == &dtrace_provider_ops) { | |
7993 | lck_mtx_assert(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED); | |
7994 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
7995 | ASSERT(dtrace_anon.dta_enabling == NULL); | |
7996 | ||
7997 | /* | |
7998 | * We make sure that the DTrace provider is at the head of | |
7999 | * the provider chain. | |
8000 | */ | |
8001 | provider->dtpv_next = dtrace_provider; | |
8002 | dtrace_provider = provider; | |
8003 | return (0); | |
8004 | } | |
8005 | ||
8006 | lck_mtx_lock(&dtrace_provider_lock); | |
8007 | lck_mtx_lock(&dtrace_lock); | |
8008 | ||
8009 | /* | |
8010 | * If there is at least one provider registered, we'll add this | |
8011 | * provider after the first provider. | |
8012 | */ | |
8013 | if (dtrace_provider != NULL) { | |
8014 | provider->dtpv_next = dtrace_provider->dtpv_next; | |
8015 | dtrace_provider->dtpv_next = provider; | |
8016 | } else { | |
8017 | dtrace_provider = provider; | |
8018 | } | |
8019 | ||
8020 | if (dtrace_retained != NULL) { | |
8021 | dtrace_enabling_provide(provider); | |
8022 | ||
8023 | /* | |
8024 | * Now we need to call dtrace_enabling_matchall() -- which | |
8025 | * will acquire cpu_lock and dtrace_lock. We therefore need | |
8026 | * to drop all of our locks before calling into it... | |
8027 | */ | |
8028 | lck_mtx_unlock(&dtrace_lock); | |
8029 | lck_mtx_unlock(&dtrace_provider_lock); | |
8030 | dtrace_enabling_matchall(); | |
8031 | ||
8032 | return (0); | |
8033 | } | |
8034 | ||
8035 | lck_mtx_unlock(&dtrace_lock); | |
8036 | lck_mtx_unlock(&dtrace_provider_lock); | |
8037 | ||
8038 | return (0); | |
8039 | } | |
8040 | ||
8041 | /* | |
8042 | * Unregister the specified provider from the DTrace framework. This should | |
8043 | * generally be called by DTrace providers in their detach(9E) entry point. | |
8044 | */ | |
8045 | int | |
8046 | dtrace_unregister(dtrace_provider_id_t id) | |
8047 | { | |
8048 | dtrace_provider_t *old = (dtrace_provider_t *)id; | |
8049 | dtrace_provider_t *prev = NULL; | |
8050 | int i, self = 0; | |
8051 | dtrace_probe_t *probe, *first = NULL; | |
8052 | ||
8053 | if (old->dtpv_pops.dtps_enable == | |
8054 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { | |
8055 | /* | |
8056 | * If DTrace itself is the provider, we're called with locks | |
8057 | * already held. | |
8058 | */ | |
8059 | ASSERT(old == dtrace_provider); | |
8060 | ASSERT(dtrace_devi != NULL); | |
8061 | lck_mtx_assert(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED); | |
8062 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
2d21ac55 A |
8063 | self = 1; |
8064 | ||
8065 | if (dtrace_provider->dtpv_next != NULL) { | |
8066 | /* | |
8067 | * There's another provider here; return failure. | |
8068 | */ | |
8069 | return (EBUSY); | |
8070 | } | |
8071 | } else { | |
8072 | lck_mtx_lock(&dtrace_provider_lock); | |
8073 | lck_mtx_lock(&mod_lock); | |
8074 | lck_mtx_lock(&dtrace_lock); | |
8075 | } | |
8076 | ||
8077 | /* | |
8078 | * If anyone has /dev/dtrace open, or if there are anonymous enabled | |
8079 | * probes, we refuse to let providers slither away, unless this | |
8080 | * provider has already been explicitly invalidated. | |
8081 | */ | |
8082 | if (!old->dtpv_defunct && | |
8083 | (dtrace_opens || (dtrace_anon.dta_state != NULL && | |
8084 | dtrace_anon.dta_state->dts_necbs > 0))) { | |
8085 | if (!self) { | |
8086 | lck_mtx_unlock(&dtrace_lock); | |
8087 | lck_mtx_unlock(&mod_lock); | |
8088 | lck_mtx_unlock(&dtrace_provider_lock); | |
8089 | } | |
8090 | return (EBUSY); | |
8091 | } | |
8092 | ||
8093 | /* | |
8094 | * Attempt to destroy the probes associated with this provider. | |
8095 | */ | |
8096 | for (i = 0; i < dtrace_nprobes; i++) { | |
8097 | if ((probe = dtrace_probes[i]) == NULL) | |
8098 | continue; | |
8099 | ||
8100 | if (probe->dtpr_provider != old) | |
8101 | continue; | |
8102 | ||
8103 | if (probe->dtpr_ecb == NULL) | |
8104 | continue; | |
8105 | ||
8106 | /* | |
8107 | * We have at least one ECB; we can't remove this provider. | |
8108 | */ | |
8109 | if (!self) { | |
8110 | lck_mtx_unlock(&dtrace_lock); | |
8111 | lck_mtx_unlock(&mod_lock); | |
8112 | lck_mtx_unlock(&dtrace_provider_lock); | |
8113 | } | |
8114 | return (EBUSY); | |
8115 | } | |
8116 | ||
8117 | /* | |
8118 | * All of the probes for this provider are disabled; we can safely | |
8119 | * remove all of them from their hash chains and from the probe array. | |
8120 | */ | |
8121 | for (i = 0; i < dtrace_nprobes; i++) { | |
8122 | if ((probe = dtrace_probes[i]) == NULL) | |
8123 | continue; | |
8124 | ||
8125 | if (probe->dtpr_provider != old) | |
8126 | continue; | |
8127 | ||
8128 | dtrace_probes[i] = NULL; | |
8129 | ||
8130 | dtrace_hash_remove(dtrace_bymod, probe); | |
8131 | dtrace_hash_remove(dtrace_byfunc, probe); | |
8132 | dtrace_hash_remove(dtrace_byname, probe); | |
8133 | ||
8134 | if (first == NULL) { | |
8135 | first = probe; | |
8136 | probe->dtpr_nextmod = NULL; | |
8137 | } else { | |
8138 | probe->dtpr_nextmod = first; | |
8139 | first = probe; | |
8140 | } | |
8141 | } | |
8142 | ||
8143 | /* | |
8144 | * The provider's probes have been removed from the hash chains and | |
8145 | * from the probe array. Now issue a dtrace_sync() to be sure that | |
8146 | * everyone has cleared out from any probe array processing. | |
8147 | */ | |
8148 | dtrace_sync(); | |
8149 | ||
8150 | for (probe = first; probe != NULL; probe = first) { | |
8151 | first = probe->dtpr_nextmod; | |
8152 | ||
8153 | old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, | |
8154 | probe->dtpr_arg); | |
8155 | kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); | |
8156 | kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); | |
8157 | kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); | |
8158 | vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); | |
8159 | #if !defined(__APPLE__) | |
8160 | kmem_free(probe, sizeof (dtrace_probe_t)); | |
8161 | #else | |
8162 | zfree(dtrace_probe_t_zone, probe); | |
8163 | #endif | |
8164 | } | |
8165 | ||
8166 | if ((prev = dtrace_provider) == old) { | |
8167 | ASSERT(self || dtrace_devi == NULL); | |
8168 | ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); | |
8169 | dtrace_provider = old->dtpv_next; | |
8170 | } else { | |
8171 | while (prev != NULL && prev->dtpv_next != old) | |
8172 | prev = prev->dtpv_next; | |
8173 | ||
8174 | if (prev == NULL) { | |
8175 | panic("attempt to unregister non-existent " | |
8176 | "dtrace provider %p\n", (void *)id); | |
8177 | } | |
8178 | ||
8179 | prev->dtpv_next = old->dtpv_next; | |
8180 | } | |
8181 | ||
8182 | if (!self) { | |
8183 | lck_mtx_unlock(&dtrace_lock); | |
8184 | lck_mtx_unlock(&mod_lock); | |
8185 | lck_mtx_unlock(&dtrace_provider_lock); | |
8186 | } | |
8187 | ||
8188 | kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); | |
8189 | kmem_free(old, sizeof (dtrace_provider_t)); | |
8190 | ||
8191 | return (0); | |
8192 | } | |
8193 | ||
8194 | /* | |
8195 | * Invalidate the specified provider. All subsequent probe lookups for the | |
8196 | * specified provider will fail, but its probes will not be removed. | |
8197 | */ | |
8198 | void | |
8199 | dtrace_invalidate(dtrace_provider_id_t id) | |
8200 | { | |
8201 | dtrace_provider_t *pvp = (dtrace_provider_t *)id; | |
8202 | ||
8203 | ASSERT(pvp->dtpv_pops.dtps_enable != | |
8204 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); | |
8205 | ||
8206 | lck_mtx_lock(&dtrace_provider_lock); | |
8207 | lck_mtx_lock(&dtrace_lock); | |
8208 | ||
8209 | pvp->dtpv_defunct = 1; | |
8210 | ||
8211 | lck_mtx_unlock(&dtrace_lock); | |
8212 | lck_mtx_unlock(&dtrace_provider_lock); | |
8213 | } | |
8214 | ||
8215 | /* | |
8216 | * Indicate whether or not DTrace has attached. | |
8217 | */ | |
8218 | int | |
8219 | dtrace_attached(void) | |
8220 | { | |
8221 | /* | |
8222 | * dtrace_provider will be non-NULL iff the DTrace driver has | |
8223 | * attached. (It's non-NULL because DTrace is always itself a | |
8224 | * provider.) | |
8225 | */ | |
8226 | return (dtrace_provider != NULL); | |
8227 | } | |
8228 | ||
8229 | /* | |
8230 | * Remove all the unenabled probes for the given provider. This function is | |
8231 | * not unlike dtrace_unregister(), except that it doesn't remove the provider | |
8232 | * -- just as many of its associated probes as it can. | |
8233 | */ | |
8234 | int | |
8235 | dtrace_condense(dtrace_provider_id_t id) | |
8236 | { | |
8237 | dtrace_provider_t *prov = (dtrace_provider_t *)id; | |
8238 | int i; | |
8239 | dtrace_probe_t *probe; | |
8240 | ||
8241 | /* | |
8242 | * Make sure this isn't the dtrace provider itself. | |
8243 | */ | |
8244 | ASSERT(prov->dtpv_pops.dtps_enable != | |
8245 | (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); | |
8246 | ||
8247 | lck_mtx_lock(&dtrace_provider_lock); | |
8248 | lck_mtx_lock(&dtrace_lock); | |
8249 | ||
8250 | /* | |
8251 | * Attempt to destroy the probes associated with this provider. | |
8252 | */ | |
8253 | for (i = 0; i < dtrace_nprobes; i++) { | |
8254 | if ((probe = dtrace_probes[i]) == NULL) | |
8255 | continue; | |
8256 | ||
8257 | if (probe->dtpr_provider != prov) | |
8258 | continue; | |
8259 | ||
8260 | if (probe->dtpr_ecb != NULL) | |
8261 | continue; | |
8262 | ||
8263 | dtrace_probes[i] = NULL; | |
8264 | ||
8265 | dtrace_hash_remove(dtrace_bymod, probe); | |
8266 | dtrace_hash_remove(dtrace_byfunc, probe); | |
8267 | dtrace_hash_remove(dtrace_byname, probe); | |
8268 | ||
8269 | prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, | |
8270 | probe->dtpr_arg); | |
8271 | kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); | |
8272 | kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); | |
8273 | kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); | |
8274 | #if !defined(__APPLE__) | |
8275 | kmem_free(probe, sizeof (dtrace_probe_t)); | |
8276 | #else | |
8277 | zfree(dtrace_probe_t_zone, probe); | |
8278 | #endif | |
8279 | vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); | |
8280 | } | |
8281 | ||
8282 | lck_mtx_unlock(&dtrace_lock); | |
8283 | lck_mtx_unlock(&dtrace_provider_lock); | |
8284 | ||
8285 | return (0); | |
8286 | } | |
8287 | ||
8288 | /* | |
8289 | * DTrace Probe Management Functions | |
8290 | * | |
8291 | * The functions in this section perform the DTrace probe management, | |
8292 | * including functions to create probes, look-up probes, and call into the | |
8293 | * providers to request that probes be provided. Some of these functions are | |
8294 | * in the Provider-to-Framework API; these functions can be identified by the | |
8295 | * fact that they are not declared "static". | |
8296 | */ | |
8297 | ||
8298 | /* | |
8299 | * Create a probe with the specified module name, function name, and name. | |
8300 | */ | |
8301 | dtrace_id_t | |
8302 | dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, | |
8303 | const char *func, const char *name, int aframes, void *arg) | |
8304 | { | |
8305 | dtrace_probe_t *probe, **probes; | |
8306 | dtrace_provider_t *provider = (dtrace_provider_t *)prov; | |
8307 | dtrace_id_t id; | |
8308 | ||
8309 | if (provider == dtrace_provider) { | |
8310 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
8311 | } else { | |
8312 | lck_mtx_lock(&dtrace_lock); | |
8313 | } | |
8314 | ||
8315 | id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, | |
8316 | VM_BESTFIT | VM_SLEEP); | |
8317 | #if !defined(__APPLE__) | |
8318 | probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); | |
8319 | #else | |
8320 | probe = zalloc(dtrace_probe_t_zone); | |
8321 | bzero(probe, sizeof (dtrace_probe_t)); | |
8322 | #endif | |
8323 | ||
8324 | probe->dtpr_id = id; | |
8325 | probe->dtpr_gen = dtrace_probegen++; | |
8326 | probe->dtpr_mod = dtrace_strdup(mod); | |
8327 | probe->dtpr_func = dtrace_strdup(func); | |
8328 | probe->dtpr_name = dtrace_strdup(name); | |
8329 | probe->dtpr_arg = arg; | |
8330 | probe->dtpr_aframes = aframes; | |
8331 | probe->dtpr_provider = provider; | |
8332 | ||
8333 | dtrace_hash_add(dtrace_bymod, probe); | |
8334 | dtrace_hash_add(dtrace_byfunc, probe); | |
8335 | dtrace_hash_add(dtrace_byname, probe); | |
8336 | ||
b0d623f7 | 8337 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8338 | if (id - 1 >= dtrace_nprobes) { |
b0d623f7 A |
8339 | #else |
8340 | if (id - 1 >= (dtrace_id_t)dtrace_nprobes) { | |
8341 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8342 | size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); |
8343 | size_t nsize = osize << 1; | |
8344 | ||
8345 | if (nsize == 0) { | |
8346 | ASSERT(osize == 0); | |
8347 | ASSERT(dtrace_probes == NULL); | |
8348 | nsize = sizeof (dtrace_probe_t *); | |
8349 | } | |
8350 | ||
8351 | probes = kmem_zalloc(nsize, KM_SLEEP); | |
8352 | ||
8353 | if (dtrace_probes == NULL) { | |
8354 | ASSERT(osize == 0); | |
8355 | dtrace_probes = probes; | |
8356 | dtrace_nprobes = 1; | |
8357 | } else { | |
8358 | dtrace_probe_t **oprobes = dtrace_probes; | |
8359 | ||
8360 | bcopy(oprobes, probes, osize); | |
8361 | dtrace_membar_producer(); | |
8362 | dtrace_probes = probes; | |
8363 | ||
8364 | dtrace_sync(); | |
8365 | ||
8366 | /* | |
8367 | * All CPUs are now seeing the new probes array; we can | |
8368 | * safely free the old array. | |
8369 | */ | |
8370 | kmem_free(oprobes, osize); | |
8371 | dtrace_nprobes <<= 1; | |
8372 | } | |
8373 | ||
b0d623f7 | 8374 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8375 | ASSERT(id - 1 < dtrace_nprobes); |
b0d623f7 A |
8376 | #else |
8377 | ASSERT(id - 1 < (dtrace_id_t)dtrace_nprobes); | |
8378 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8379 | } |
8380 | ||
8381 | ASSERT(dtrace_probes[id - 1] == NULL); | |
8382 | dtrace_probes[id - 1] = probe; | |
8383 | ||
8384 | if (provider != dtrace_provider) | |
8385 | lck_mtx_unlock(&dtrace_lock); | |
8386 | ||
8387 | return (id); | |
8388 | } | |
8389 | ||
8390 | static dtrace_probe_t * | |
8391 | dtrace_probe_lookup_id(dtrace_id_t id) | |
8392 | { | |
8393 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
8394 | ||
b0d623f7 | 8395 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 A |
8396 | if (id == 0 || id > dtrace_nprobes) |
8397 | return (NULL); | |
b0d623f7 A |
8398 | #else |
8399 | if (id == 0 || id > (dtrace_id_t)dtrace_nprobes) | |
8400 | return (NULL); | |
8401 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8402 | |
8403 | return (dtrace_probes[id - 1]); | |
8404 | } | |
8405 | ||
8406 | static int | |
8407 | dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) | |
8408 | { | |
8409 | *((dtrace_id_t *)arg) = probe->dtpr_id; | |
8410 | ||
8411 | return (DTRACE_MATCH_DONE); | |
8412 | } | |
8413 | ||
8414 | /* | |
8415 | * Look up a probe based on provider and one or more of module name, function | |
8416 | * name and probe name. | |
8417 | */ | |
8418 | dtrace_id_t | |
8419 | dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod, | |
8420 | const char *func, const char *name) | |
8421 | { | |
8422 | dtrace_probekey_t pkey; | |
8423 | dtrace_id_t id; | |
8424 | int match; | |
8425 | ||
8426 | pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; | |
8427 | pkey.dtpk_pmatch = &dtrace_match_string; | |
8428 | pkey.dtpk_mod = mod; | |
8429 | pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; | |
8430 | pkey.dtpk_func = func; | |
8431 | pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; | |
8432 | pkey.dtpk_name = name; | |
8433 | pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; | |
8434 | pkey.dtpk_id = DTRACE_IDNONE; | |
8435 | ||
8436 | lck_mtx_lock(&dtrace_lock); | |
8437 | match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, | |
8438 | dtrace_probe_lookup_match, &id); | |
8439 | lck_mtx_unlock(&dtrace_lock); | |
8440 | ||
8441 | ASSERT(match == 1 || match == 0); | |
8442 | return (match ? id : 0); | |
8443 | } | |
8444 | ||
8445 | /* | |
8446 | * Returns the probe argument associated with the specified probe. | |
8447 | */ | |
8448 | void * | |
8449 | dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) | |
8450 | { | |
8451 | dtrace_probe_t *probe; | |
8452 | void *rval = NULL; | |
8453 | ||
8454 | lck_mtx_lock(&dtrace_lock); | |
8455 | ||
8456 | if ((probe = dtrace_probe_lookup_id(pid)) != NULL && | |
8457 | probe->dtpr_provider == (dtrace_provider_t *)id) | |
8458 | rval = probe->dtpr_arg; | |
8459 | ||
8460 | lck_mtx_unlock(&dtrace_lock); | |
8461 | ||
8462 | return (rval); | |
8463 | } | |
8464 | ||
8465 | /* | |
8466 | * Copy a probe into a probe description. | |
8467 | */ | |
8468 | static void | |
8469 | dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) | |
8470 | { | |
8471 | bzero(pdp, sizeof (dtrace_probedesc_t)); | |
8472 | pdp->dtpd_id = prp->dtpr_id; | |
8473 | ||
b0d623f7 A |
8474 | #if !defined(__APPLE__) |
8475 | (void) strncpy(pdp->dtpd_provider, | |
8476 | prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); | |
8477 | ||
8478 | (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); | |
8479 | (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); | |
8480 | (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); | |
8481 | #else /* Employ size bounded string operation. */ | |
2d21ac55 A |
8482 | (void) strlcpy(pdp->dtpd_provider, |
8483 | prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN); | |
8484 | ||
8485 | (void) strlcpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN); | |
8486 | (void) strlcpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN); | |
8487 | (void) strlcpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN); | |
b0d623f7 | 8488 | #endif /* __APPLE__ */ |
2d21ac55 A |
8489 | } |
8490 | ||
8491 | /* | |
8492 | * Called to indicate that a probe -- or probes -- should be provided by a | |
8493 | * specfied provider. If the specified description is NULL, the provider will | |
8494 | * be told to provide all of its probes. (This is done whenever a new | |
8495 | * consumer comes along, or whenever a retained enabling is to be matched.) If | |
8496 | * the specified description is non-NULL, the provider is given the | |
8497 | * opportunity to dynamically provide the specified probe, allowing providers | |
8498 | * to support the creation of probes on-the-fly. (So-called _autocreated_ | |
8499 | * probes.) If the provider is NULL, the operations will be applied to all | |
8500 | * providers; if the provider is non-NULL the operations will only be applied | |
8501 | * to the specified provider. The dtrace_provider_lock must be held, and the | |
8502 | * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation | |
8503 | * will need to grab the dtrace_lock when it reenters the framework through | |
8504 | * dtrace_probe_lookup(), dtrace_probe_create(), etc. | |
8505 | */ | |
8506 | static void | |
8507 | dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) | |
8508 | { | |
8509 | struct modctl *ctl; | |
8510 | int all = 0; | |
b0d623f7 | 8511 | #pragma unused(ctl) /* __APPLE__ */ |
2d21ac55 A |
8512 | |
8513 | lck_mtx_assert(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED); | |
8514 | ||
8515 | if (prv == NULL) { | |
8516 | all = 1; | |
8517 | prv = dtrace_provider; | |
8518 | } | |
8519 | ||
8520 | do { | |
2d21ac55 A |
8521 | /* |
8522 | * First, call the blanket provide operation. | |
8523 | */ | |
8524 | prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); | |
8525 | ||
8526 | #if !defined(__APPLE__) | |
8527 | /* | |
8528 | * Now call the per-module provide operation. We will grab | |
8529 | * mod_lock to prevent the list from being modified. Note | |
8530 | * that this also prevents the mod_busy bits from changing. | |
8531 | * (mod_busy can only be changed with mod_lock held.) | |
8532 | */ | |
b0d623f7 | 8533 | mutex_enter(&mod_lock); |
2d21ac55 A |
8534 | |
8535 | ctl = &modules; | |
8536 | do { | |
8537 | if (ctl->mod_busy || ctl->mod_mp == NULL) | |
8538 | continue; | |
8539 | ||
8540 | prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); | |
8541 | ||
8542 | } while ((ctl = ctl->mod_next) != &modules); | |
8543 | ||
b0d623f7 | 8544 | mutex_exit(&mod_lock); |
2d21ac55 | 8545 | #else |
c910b4d9 | 8546 | #if 0 /* FIXME: Workaround for PR_4643546 */ |
b0d623f7 | 8547 | /* NOTE: kmod_lock has been removed. */ |
2d21ac55 A |
8548 | simple_lock(&kmod_lock); |
8549 | ||
c910b4d9 | 8550 | kmod_info_t *ktl = kmod; |
2d21ac55 A |
8551 | while (ktl) { |
8552 | prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ktl); | |
8553 | ktl = ktl->next; | |
8554 | } | |
8555 | ||
8556 | simple_unlock(&kmod_lock); | |
8557 | #else | |
8558 | /* | |
8559 | * Don't bother to iterate over the kmod list. At present only fbt | |
8560 | * offers a provide_module in its dtpv_pops, and then it ignores the | |
8561 | * module anyway. | |
8562 | */ | |
8563 | prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, NULL); | |
8564 | #endif | |
8565 | #endif /* __APPLE__ */ | |
8566 | } while (all && (prv = prv->dtpv_next) != NULL); | |
8567 | } | |
8568 | ||
8569 | /* | |
8570 | * Iterate over each probe, and call the Framework-to-Provider API function | |
8571 | * denoted by offs. | |
8572 | */ | |
8573 | static void | |
8574 | dtrace_probe_foreach(uintptr_t offs) | |
8575 | { | |
8576 | dtrace_provider_t *prov; | |
8577 | void (*func)(void *, dtrace_id_t, void *); | |
8578 | dtrace_probe_t *probe; | |
8579 | dtrace_icookie_t cookie; | |
8580 | int i; | |
8581 | ||
8582 | /* | |
8583 | * We disable interrupts to walk through the probe array. This is | |
8584 | * safe -- the dtrace_sync() in dtrace_unregister() assures that we | |
8585 | * won't see stale data. | |
8586 | */ | |
8587 | cookie = dtrace_interrupt_disable(); | |
8588 | ||
8589 | for (i = 0; i < dtrace_nprobes; i++) { | |
8590 | if ((probe = dtrace_probes[i]) == NULL) | |
8591 | continue; | |
8592 | ||
8593 | if (probe->dtpr_ecb == NULL) { | |
8594 | /* | |
8595 | * This probe isn't enabled -- don't call the function. | |
8596 | */ | |
8597 | continue; | |
8598 | } | |
8599 | ||
8600 | prov = probe->dtpr_provider; | |
8601 | func = *((void(**)(void *, dtrace_id_t, void *)) | |
8602 | ((uintptr_t)&prov->dtpv_pops + offs)); | |
8603 | ||
8604 | func(prov->dtpv_arg, i + 1, probe->dtpr_arg); | |
8605 | } | |
8606 | ||
8607 | dtrace_interrupt_enable(cookie); | |
8608 | } | |
8609 | ||
8610 | static int | |
8611 | dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab) | |
8612 | { | |
8613 | dtrace_probekey_t pkey; | |
8614 | uint32_t priv; | |
8615 | uid_t uid; | |
8616 | zoneid_t zoneid; | |
8617 | ||
8618 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
8619 | ||
8620 | dtrace_ecb_create_cache = NULL; | |
8621 | ||
8622 | if (desc == NULL) { | |
8623 | /* | |
8624 | * If we're passed a NULL description, we're being asked to | |
8625 | * create an ECB with a NULL probe. | |
8626 | */ | |
8627 | (void) dtrace_ecb_create_enable(NULL, enab); | |
8628 | return (0); | |
8629 | } | |
8630 | ||
8631 | dtrace_probekey(desc, &pkey); | |
8632 | dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, | |
8633 | &priv, &uid, &zoneid); | |
8634 | ||
8635 | return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, | |
8636 | enab)); | |
8637 | } | |
8638 | ||
8639 | /* | |
8640 | * DTrace Helper Provider Functions | |
8641 | */ | |
8642 | static void | |
8643 | dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) | |
8644 | { | |
8645 | attr->dtat_name = DOF_ATTR_NAME(dofattr); | |
8646 | attr->dtat_data = DOF_ATTR_DATA(dofattr); | |
8647 | attr->dtat_class = DOF_ATTR_CLASS(dofattr); | |
8648 | } | |
8649 | ||
8650 | static void | |
8651 | dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, | |
8652 | const dof_provider_t *dofprov, char *strtab) | |
8653 | { | |
8654 | hprov->dthpv_provname = strtab + dofprov->dofpv_name; | |
8655 | dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, | |
8656 | dofprov->dofpv_provattr); | |
8657 | dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, | |
8658 | dofprov->dofpv_modattr); | |
8659 | dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, | |
8660 | dofprov->dofpv_funcattr); | |
8661 | dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, | |
8662 | dofprov->dofpv_nameattr); | |
8663 | dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, | |
8664 | dofprov->dofpv_argsattr); | |
8665 | } | |
8666 | ||
8667 | static void | |
8668 | dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) | |
8669 | { | |
8670 | uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; | |
8671 | dof_hdr_t *dof = (dof_hdr_t *)daddr; | |
8672 | dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; | |
8673 | dof_provider_t *provider; | |
8674 | dof_probe_t *probe; | |
8675 | uint32_t *off, *enoff; | |
8676 | uint8_t *arg; | |
8677 | char *strtab; | |
8678 | uint_t i, nprobes; | |
8679 | dtrace_helper_provdesc_t dhpv; | |
8680 | dtrace_helper_probedesc_t dhpb; | |
8681 | dtrace_meta_t *meta = dtrace_meta_pid; | |
8682 | dtrace_mops_t *mops = &meta->dtm_mops; | |
8683 | void *parg; | |
8684 | ||
8685 | provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); | |
8686 | str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + | |
8687 | provider->dofpv_strtab * dof->dofh_secsize); | |
8688 | prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + | |
8689 | provider->dofpv_probes * dof->dofh_secsize); | |
8690 | arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + | |
8691 | provider->dofpv_prargs * dof->dofh_secsize); | |
8692 | off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + | |
8693 | provider->dofpv_proffs * dof->dofh_secsize); | |
8694 | ||
8695 | strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); | |
8696 | off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); | |
8697 | arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); | |
8698 | enoff = NULL; | |
8699 | ||
8700 | /* | |
8701 | * See dtrace_helper_provider_validate(). | |
8702 | */ | |
8703 | if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && | |
8704 | provider->dofpv_prenoffs != DOF_SECT_NONE) { | |
8705 | enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + | |
8706 | provider->dofpv_prenoffs * dof->dofh_secsize); | |
8707 | enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); | |
8708 | } | |
8709 | ||
8710 | nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; | |
8711 | ||
8712 | /* | |
8713 | * Create the provider. | |
8714 | */ | |
8715 | dtrace_dofprov2hprov(&dhpv, provider, strtab); | |
8716 | ||
8717 | if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) | |
8718 | return; | |
8719 | ||
8720 | meta->dtm_count++; | |
8721 | ||
8722 | /* | |
8723 | * Create the probes. | |
8724 | */ | |
8725 | for (i = 0; i < nprobes; i++) { | |
8726 | probe = (dof_probe_t *)(uintptr_t)(daddr + | |
8727 | prb_sec->dofs_offset + i * prb_sec->dofs_entsize); | |
8728 | ||
8729 | dhpb.dthpb_mod = dhp->dofhp_mod; | |
8730 | dhpb.dthpb_func = strtab + probe->dofpr_func; | |
8731 | dhpb.dthpb_name = strtab + probe->dofpr_name; | |
b0d623f7 | 8732 | #if !defined(__APPLE__) |
2d21ac55 | 8733 | dhpb.dthpb_base = probe->dofpr_addr; |
b0d623f7 A |
8734 | #else |
8735 | dhpb.dthpb_base = dhp->dofhp_addr; /* FIXME: James, why? */ | |
2d21ac55 | 8736 | #endif |
b0d623f7 | 8737 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8738 | dhpb.dthpb_offs = off + probe->dofpr_offidx; |
b0d623f7 A |
8739 | #else |
8740 | dhpb.dthpb_offs = (int32_t *)(off + probe->dofpr_offidx); | |
8741 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8742 | dhpb.dthpb_noffs = probe->dofpr_noffs; |
8743 | if (enoff != NULL) { | |
b0d623f7 | 8744 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8745 | dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; |
b0d623f7 A |
8746 | #else |
8747 | dhpb.dthpb_enoffs = (int32_t *)(enoff + probe->dofpr_enoffidx); | |
8748 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8749 | dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; |
8750 | } else { | |
8751 | dhpb.dthpb_enoffs = NULL; | |
8752 | dhpb.dthpb_nenoffs = 0; | |
8753 | } | |
8754 | dhpb.dthpb_args = arg + probe->dofpr_argidx; | |
8755 | dhpb.dthpb_nargc = probe->dofpr_nargc; | |
8756 | dhpb.dthpb_xargc = probe->dofpr_xargc; | |
8757 | dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; | |
8758 | dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; | |
8759 | ||
8760 | mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); | |
8761 | } | |
8762 | } | |
8763 | ||
8764 | static void | |
8765 | dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) | |
8766 | { | |
8767 | uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; | |
8768 | dof_hdr_t *dof = (dof_hdr_t *)daddr; | |
b0d623f7 | 8769 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8770 | int i; |
b0d623f7 A |
8771 | #else |
8772 | uint32_t i; | |
8773 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8774 | |
8775 | lck_mtx_assert(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED); | |
8776 | ||
8777 | for (i = 0; i < dof->dofh_secnum; i++) { | |
8778 | dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + | |
8779 | dof->dofh_secoff + i * dof->dofh_secsize); | |
8780 | ||
8781 | if (sec->dofs_type != DOF_SECT_PROVIDER) | |
8782 | continue; | |
8783 | ||
8784 | dtrace_helper_provide_one(dhp, sec, pid); | |
8785 | } | |
8786 | ||
8787 | /* | |
8788 | * We may have just created probes, so we must now rematch against | |
8789 | * any retained enablings. Note that this call will acquire both | |
8790 | * cpu_lock and dtrace_lock; the fact that we are holding | |
8791 | * dtrace_meta_lock now is what defines the ordering with respect to | |
8792 | * these three locks. | |
8793 | */ | |
8794 | dtrace_enabling_matchall(); | |
8795 | } | |
8796 | ||
8797 | static void | |
8798 | dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) | |
8799 | { | |
8800 | uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; | |
8801 | dof_hdr_t *dof = (dof_hdr_t *)daddr; | |
8802 | dof_sec_t *str_sec; | |
8803 | dof_provider_t *provider; | |
8804 | char *strtab; | |
8805 | dtrace_helper_provdesc_t dhpv; | |
8806 | dtrace_meta_t *meta = dtrace_meta_pid; | |
8807 | dtrace_mops_t *mops = &meta->dtm_mops; | |
8808 | ||
8809 | provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); | |
8810 | str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + | |
8811 | provider->dofpv_strtab * dof->dofh_secsize); | |
8812 | ||
8813 | strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); | |
8814 | ||
8815 | /* | |
8816 | * Create the provider. | |
8817 | */ | |
8818 | dtrace_dofprov2hprov(&dhpv, provider, strtab); | |
8819 | ||
8820 | mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); | |
8821 | ||
8822 | meta->dtm_count--; | |
8823 | } | |
8824 | ||
8825 | static void | |
8826 | dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) | |
8827 | { | |
8828 | uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; | |
8829 | dof_hdr_t *dof = (dof_hdr_t *)daddr; | |
b0d623f7 | 8830 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8831 | int i; |
b0d623f7 A |
8832 | #else |
8833 | uint32_t i; | |
8834 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8835 | |
8836 | lck_mtx_assert(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED); | |
8837 | ||
8838 | for (i = 0; i < dof->dofh_secnum; i++) { | |
8839 | dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + | |
8840 | dof->dofh_secoff + i * dof->dofh_secsize); | |
8841 | ||
8842 | if (sec->dofs_type != DOF_SECT_PROVIDER) | |
8843 | continue; | |
8844 | ||
8845 | dtrace_helper_provider_remove_one(dhp, sec, pid); | |
8846 | } | |
8847 | } | |
8848 | ||
8849 | /* | |
8850 | * DTrace Meta Provider-to-Framework API Functions | |
8851 | * | |
8852 | * These functions implement the Meta Provider-to-Framework API, as described | |
8853 | * in <sys/dtrace.h>. | |
8854 | */ | |
8855 | int | |
8856 | dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, | |
8857 | dtrace_meta_provider_id_t *idp) | |
8858 | { | |
8859 | dtrace_meta_t *meta; | |
8860 | dtrace_helpers_t *help, *next; | |
b0d623f7 | 8861 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 8862 | int i; |
b0d623f7 A |
8863 | #else |
8864 | uint_t i; | |
8865 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8866 | |
8867 | *idp = DTRACE_METAPROVNONE; | |
8868 | ||
8869 | /* | |
8870 | * We strictly don't need the name, but we hold onto it for | |
8871 | * debuggability. All hail error queues! | |
8872 | */ | |
8873 | if (name == NULL) { | |
8874 | cmn_err(CE_WARN, "failed to register meta-provider: " | |
8875 | "invalid name"); | |
8876 | return (EINVAL); | |
8877 | } | |
8878 | ||
8879 | if (mops == NULL || | |
8880 | mops->dtms_create_probe == NULL || | |
8881 | mops->dtms_provide_pid == NULL || | |
8882 | mops->dtms_remove_pid == NULL) { | |
8883 | cmn_err(CE_WARN, "failed to register meta-register %s: " | |
8884 | "invalid ops", name); | |
8885 | return (EINVAL); | |
8886 | } | |
8887 | ||
8888 | meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); | |
8889 | meta->dtm_mops = *mops; | |
b0d623f7 | 8890 | #if !defined(__APPLE__) |
2d21ac55 A |
8891 | meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); |
8892 | (void) strcpy(meta->dtm_name, name); | |
b0d623f7 A |
8893 | #else /* Employ size bounded string operation. */ |
8894 | { | |
8895 | size_t bufsize = strlen(name) + 1; | |
8896 | meta->dtm_name = kmem_alloc(bufsize, KM_SLEEP); | |
8897 | (void) strlcpy(meta->dtm_name, name, bufsize); | |
8898 | } | |
8899 | #endif /* __APPLE__ */ | |
2d21ac55 A |
8900 | meta->dtm_arg = arg; |
8901 | ||
8902 | lck_mtx_lock(&dtrace_meta_lock); | |
8903 | lck_mtx_lock(&dtrace_lock); | |
8904 | ||
8905 | if (dtrace_meta_pid != NULL) { | |
8906 | lck_mtx_unlock(&dtrace_lock); | |
8907 | lck_mtx_unlock(&dtrace_meta_lock); | |
8908 | cmn_err(CE_WARN, "failed to register meta-register %s: " | |
8909 | "user-land meta-provider exists", name); | |
8910 | kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); | |
8911 | kmem_free(meta, sizeof (dtrace_meta_t)); | |
8912 | return (EINVAL); | |
8913 | } | |
8914 | ||
8915 | dtrace_meta_pid = meta; | |
8916 | *idp = (dtrace_meta_provider_id_t)meta; | |
8917 | ||
8918 | /* | |
8919 | * If there are providers and probes ready to go, pass them | |
8920 | * off to the new meta provider now. | |
8921 | */ | |
8922 | ||
8923 | help = dtrace_deferred_pid; | |
8924 | dtrace_deferred_pid = NULL; | |
8925 | ||
8926 | lck_mtx_unlock(&dtrace_lock); | |
8927 | ||
8928 | while (help != NULL) { | |
8929 | for (i = 0; i < help->dthps_nprovs; i++) { | |
8930 | dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, | |
8931 | help->dthps_pid); | |
8932 | } | |
8933 | ||
8934 | next = help->dthps_next; | |
8935 | help->dthps_next = NULL; | |
8936 | help->dthps_prev = NULL; | |
8937 | help->dthps_deferred = 0; | |
8938 | help = next; | |
8939 | } | |
8940 | ||
8941 | lck_mtx_unlock(&dtrace_meta_lock); | |
8942 | ||
8943 | return (0); | |
8944 | } | |
8945 | ||
8946 | int | |
8947 | dtrace_meta_unregister(dtrace_meta_provider_id_t id) | |
8948 | { | |
8949 | dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; | |
8950 | ||
8951 | lck_mtx_lock(&dtrace_meta_lock); | |
8952 | lck_mtx_lock(&dtrace_lock); | |
8953 | ||
8954 | if (old == dtrace_meta_pid) { | |
8955 | pp = &dtrace_meta_pid; | |
8956 | } else { | |
8957 | panic("attempt to unregister non-existent " | |
8958 | "dtrace meta-provider %p\n", (void *)old); | |
8959 | } | |
8960 | ||
8961 | if (old->dtm_count != 0) { | |
8962 | lck_mtx_unlock(&dtrace_lock); | |
8963 | lck_mtx_unlock(&dtrace_meta_lock); | |
8964 | return (EBUSY); | |
8965 | } | |
8966 | ||
8967 | *pp = NULL; | |
8968 | ||
8969 | lck_mtx_unlock(&dtrace_lock); | |
8970 | lck_mtx_unlock(&dtrace_meta_lock); | |
8971 | ||
8972 | kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); | |
8973 | kmem_free(old, sizeof (dtrace_meta_t)); | |
8974 | ||
8975 | return (0); | |
8976 | } | |
8977 | ||
8978 | ||
8979 | /* | |
8980 | * DTrace DIF Object Functions | |
8981 | */ | |
8982 | static int | |
8983 | dtrace_difo_err(uint_t pc, const char *format, ...) | |
8984 | { | |
8985 | if (dtrace_err_verbose) { | |
8986 | va_list alist; | |
8987 | ||
8988 | (void) uprintf("dtrace DIF object error: [%u]: ", pc); | |
8989 | va_start(alist, format); | |
8990 | (void) vuprintf(format, alist); | |
8991 | va_end(alist); | |
8992 | } | |
8993 | ||
8994 | #ifdef DTRACE_ERRDEBUG | |
8995 | dtrace_errdebug(format); | |
8996 | #endif | |
8997 | return (1); | |
8998 | } | |
8999 | ||
9000 | /* | |
9001 | * Validate a DTrace DIF object by checking the IR instructions. The following | |
9002 | * rules are currently enforced by dtrace_difo_validate(): | |
9003 | * | |
9004 | * 1. Each instruction must have a valid opcode | |
9005 | * 2. Each register, string, variable, or subroutine reference must be valid | |
9006 | * 3. No instruction can modify register %r0 (must be zero) | |
9007 | * 4. All instruction reserved bits must be set to zero | |
9008 | * 5. The last instruction must be a "ret" instruction | |
9009 | * 6. All branch targets must reference a valid instruction _after_ the branch | |
9010 | */ | |
9011 | static int | |
9012 | dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, | |
9013 | cred_t *cr) | |
9014 | { | |
b0d623f7 | 9015 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9016 | int err = 0, i; |
b0d623f7 A |
9017 | #else |
9018 | int err = 0; | |
9019 | uint_t i; | |
9020 | #endif /* __APPLE__ */ | |
9021 | int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; | |
9022 | int kcheckload; | |
9023 | uint_t pc; | |
9024 | ||
9025 | kcheckload = cr == NULL || | |
9026 | (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; | |
2d21ac55 A |
9027 | |
9028 | dp->dtdo_destructive = 0; | |
9029 | ||
9030 | for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { | |
9031 | dif_instr_t instr = dp->dtdo_buf[pc]; | |
9032 | ||
9033 | uint_t r1 = DIF_INSTR_R1(instr); | |
9034 | uint_t r2 = DIF_INSTR_R2(instr); | |
9035 | uint_t rd = DIF_INSTR_RD(instr); | |
9036 | uint_t rs = DIF_INSTR_RS(instr); | |
9037 | uint_t label = DIF_INSTR_LABEL(instr); | |
9038 | uint_t v = DIF_INSTR_VAR(instr); | |
9039 | uint_t subr = DIF_INSTR_SUBR(instr); | |
9040 | uint_t type = DIF_INSTR_TYPE(instr); | |
9041 | uint_t op = DIF_INSTR_OP(instr); | |
9042 | ||
9043 | switch (op) { | |
9044 | case DIF_OP_OR: | |
9045 | case DIF_OP_XOR: | |
9046 | case DIF_OP_AND: | |
9047 | case DIF_OP_SLL: | |
9048 | case DIF_OP_SRL: | |
9049 | case DIF_OP_SRA: | |
9050 | case DIF_OP_SUB: | |
9051 | case DIF_OP_ADD: | |
9052 | case DIF_OP_MUL: | |
9053 | case DIF_OP_SDIV: | |
9054 | case DIF_OP_UDIV: | |
9055 | case DIF_OP_SREM: | |
9056 | case DIF_OP_UREM: | |
9057 | case DIF_OP_COPYS: | |
9058 | if (r1 >= nregs) | |
9059 | err += efunc(pc, "invalid register %u\n", r1); | |
9060 | if (r2 >= nregs) | |
9061 | err += efunc(pc, "invalid register %u\n", r2); | |
9062 | if (rd >= nregs) | |
9063 | err += efunc(pc, "invalid register %u\n", rd); | |
9064 | if (rd == 0) | |
9065 | err += efunc(pc, "cannot write to %r0\n"); | |
9066 | break; | |
9067 | case DIF_OP_NOT: | |
9068 | case DIF_OP_MOV: | |
9069 | case DIF_OP_ALLOCS: | |
9070 | if (r1 >= nregs) | |
9071 | err += efunc(pc, "invalid register %u\n", r1); | |
9072 | if (r2 != 0) | |
9073 | err += efunc(pc, "non-zero reserved bits\n"); | |
9074 | if (rd >= nregs) | |
9075 | err += efunc(pc, "invalid register %u\n", rd); | |
9076 | if (rd == 0) | |
9077 | err += efunc(pc, "cannot write to %r0\n"); | |
9078 | break; | |
9079 | case DIF_OP_LDSB: | |
9080 | case DIF_OP_LDSH: | |
9081 | case DIF_OP_LDSW: | |
9082 | case DIF_OP_LDUB: | |
9083 | case DIF_OP_LDUH: | |
9084 | case DIF_OP_LDUW: | |
9085 | case DIF_OP_LDX: | |
9086 | if (r1 >= nregs) | |
9087 | err += efunc(pc, "invalid register %u\n", r1); | |
9088 | if (r2 != 0) | |
9089 | err += efunc(pc, "non-zero reserved bits\n"); | |
9090 | if (rd >= nregs) | |
9091 | err += efunc(pc, "invalid register %u\n", rd); | |
9092 | if (rd == 0) | |
9093 | err += efunc(pc, "cannot write to %r0\n"); | |
b0d623f7 | 9094 | if (kcheckload) |
2d21ac55 A |
9095 | dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + |
9096 | DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); | |
9097 | break; | |
9098 | case DIF_OP_RLDSB: | |
9099 | case DIF_OP_RLDSH: | |
9100 | case DIF_OP_RLDSW: | |
9101 | case DIF_OP_RLDUB: | |
9102 | case DIF_OP_RLDUH: | |
9103 | case DIF_OP_RLDUW: | |
9104 | case DIF_OP_RLDX: | |
9105 | if (r1 >= nregs) | |
9106 | err += efunc(pc, "invalid register %u\n", r1); | |
9107 | if (r2 != 0) | |
9108 | err += efunc(pc, "non-zero reserved bits\n"); | |
9109 | if (rd >= nregs) | |
9110 | err += efunc(pc, "invalid register %u\n", rd); | |
9111 | if (rd == 0) | |
9112 | err += efunc(pc, "cannot write to %r0\n"); | |
9113 | break; | |
9114 | case DIF_OP_ULDSB: | |
9115 | case DIF_OP_ULDSH: | |
9116 | case DIF_OP_ULDSW: | |
9117 | case DIF_OP_ULDUB: | |
9118 | case DIF_OP_ULDUH: | |
9119 | case DIF_OP_ULDUW: | |
9120 | case DIF_OP_ULDX: | |
9121 | if (r1 >= nregs) | |
9122 | err += efunc(pc, "invalid register %u\n", r1); | |
9123 | if (r2 != 0) | |
9124 | err += efunc(pc, "non-zero reserved bits\n"); | |
9125 | if (rd >= nregs) | |
9126 | err += efunc(pc, "invalid register %u\n", rd); | |
9127 | if (rd == 0) | |
9128 | err += efunc(pc, "cannot write to %r0\n"); | |
9129 | break; | |
9130 | case DIF_OP_STB: | |
9131 | case DIF_OP_STH: | |
9132 | case DIF_OP_STW: | |
9133 | case DIF_OP_STX: | |
9134 | if (r1 >= nregs) | |
9135 | err += efunc(pc, "invalid register %u\n", r1); | |
9136 | if (r2 != 0) | |
9137 | err += efunc(pc, "non-zero reserved bits\n"); | |
9138 | if (rd >= nregs) | |
9139 | err += efunc(pc, "invalid register %u\n", rd); | |
9140 | if (rd == 0) | |
9141 | err += efunc(pc, "cannot write to 0 address\n"); | |
9142 | break; | |
9143 | case DIF_OP_CMP: | |
9144 | case DIF_OP_SCMP: | |
9145 | if (r1 >= nregs) | |
9146 | err += efunc(pc, "invalid register %u\n", r1); | |
9147 | if (r2 >= nregs) | |
9148 | err += efunc(pc, "invalid register %u\n", r2); | |
9149 | if (rd != 0) | |
9150 | err += efunc(pc, "non-zero reserved bits\n"); | |
9151 | break; | |
9152 | case DIF_OP_TST: | |
9153 | if (r1 >= nregs) | |
9154 | err += efunc(pc, "invalid register %u\n", r1); | |
9155 | if (r2 != 0 || rd != 0) | |
9156 | err += efunc(pc, "non-zero reserved bits\n"); | |
9157 | break; | |
9158 | case DIF_OP_BA: | |
9159 | case DIF_OP_BE: | |
9160 | case DIF_OP_BNE: | |
9161 | case DIF_OP_BG: | |
9162 | case DIF_OP_BGU: | |
9163 | case DIF_OP_BGE: | |
9164 | case DIF_OP_BGEU: | |
9165 | case DIF_OP_BL: | |
9166 | case DIF_OP_BLU: | |
9167 | case DIF_OP_BLE: | |
9168 | case DIF_OP_BLEU: | |
9169 | if (label >= dp->dtdo_len) { | |
9170 | err += efunc(pc, "invalid branch target %u\n", | |
9171 | label); | |
9172 | } | |
9173 | if (label <= pc) { | |
9174 | err += efunc(pc, "backward branch to %u\n", | |
9175 | label); | |
9176 | } | |
9177 | break; | |
9178 | case DIF_OP_RET: | |
9179 | if (r1 != 0 || r2 != 0) | |
9180 | err += efunc(pc, "non-zero reserved bits\n"); | |
9181 | if (rd >= nregs) | |
9182 | err += efunc(pc, "invalid register %u\n", rd); | |
9183 | break; | |
9184 | case DIF_OP_NOP: | |
9185 | case DIF_OP_POPTS: | |
9186 | case DIF_OP_FLUSHTS: | |
9187 | if (r1 != 0 || r2 != 0 || rd != 0) | |
9188 | err += efunc(pc, "non-zero reserved bits\n"); | |
9189 | break; | |
9190 | case DIF_OP_SETX: | |
9191 | if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { | |
9192 | err += efunc(pc, "invalid integer ref %u\n", | |
9193 | DIF_INSTR_INTEGER(instr)); | |
9194 | } | |
9195 | if (rd >= nregs) | |
9196 | err += efunc(pc, "invalid register %u\n", rd); | |
9197 | if (rd == 0) | |
9198 | err += efunc(pc, "cannot write to %r0\n"); | |
9199 | break; | |
9200 | case DIF_OP_SETS: | |
9201 | if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { | |
9202 | err += efunc(pc, "invalid string ref %u\n", | |
9203 | DIF_INSTR_STRING(instr)); | |
9204 | } | |
9205 | if (rd >= nregs) | |
9206 | err += efunc(pc, "invalid register %u\n", rd); | |
9207 | if (rd == 0) | |
9208 | err += efunc(pc, "cannot write to %r0\n"); | |
9209 | break; | |
9210 | case DIF_OP_LDGA: | |
9211 | case DIF_OP_LDTA: | |
9212 | if (r1 > DIF_VAR_ARRAY_MAX) | |
9213 | err += efunc(pc, "invalid array %u\n", r1); | |
9214 | if (r2 >= nregs) | |
9215 | err += efunc(pc, "invalid register %u\n", r2); | |
9216 | if (rd >= nregs) | |
9217 | err += efunc(pc, "invalid register %u\n", rd); | |
9218 | if (rd == 0) | |
9219 | err += efunc(pc, "cannot write to %r0\n"); | |
9220 | break; | |
9221 | case DIF_OP_LDGS: | |
9222 | case DIF_OP_LDTS: | |
9223 | case DIF_OP_LDLS: | |
9224 | case DIF_OP_LDGAA: | |
9225 | case DIF_OP_LDTAA: | |
9226 | if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) | |
9227 | err += efunc(pc, "invalid variable %u\n", v); | |
9228 | if (rd >= nregs) | |
9229 | err += efunc(pc, "invalid register %u\n", rd); | |
9230 | if (rd == 0) | |
9231 | err += efunc(pc, "cannot write to %r0\n"); | |
9232 | break; | |
9233 | case DIF_OP_STGS: | |
9234 | case DIF_OP_STTS: | |
9235 | case DIF_OP_STLS: | |
9236 | case DIF_OP_STGAA: | |
9237 | case DIF_OP_STTAA: | |
9238 | if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) | |
9239 | err += efunc(pc, "invalid variable %u\n", v); | |
9240 | if (rs >= nregs) | |
9241 | err += efunc(pc, "invalid register %u\n", rd); | |
9242 | break; | |
9243 | case DIF_OP_CALL: | |
9244 | if (subr > DIF_SUBR_MAX) | |
9245 | err += efunc(pc, "invalid subr %u\n", subr); | |
9246 | if (rd >= nregs) | |
9247 | err += efunc(pc, "invalid register %u\n", rd); | |
9248 | if (rd == 0) | |
9249 | err += efunc(pc, "cannot write to %r0\n"); | |
9250 | ||
9251 | if (subr == DIF_SUBR_COPYOUT || | |
9252 | subr == DIF_SUBR_COPYOUTSTR) { | |
9253 | dp->dtdo_destructive = 1; | |
9254 | } | |
9255 | break; | |
9256 | case DIF_OP_PUSHTR: | |
9257 | if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) | |
9258 | err += efunc(pc, "invalid ref type %u\n", type); | |
9259 | if (r2 >= nregs) | |
9260 | err += efunc(pc, "invalid register %u\n", r2); | |
9261 | if (rs >= nregs) | |
9262 | err += efunc(pc, "invalid register %u\n", rs); | |
9263 | break; | |
9264 | case DIF_OP_PUSHTV: | |
9265 | if (type != DIF_TYPE_CTF) | |
9266 | err += efunc(pc, "invalid val type %u\n", type); | |
9267 | if (r2 >= nregs) | |
9268 | err += efunc(pc, "invalid register %u\n", r2); | |
9269 | if (rs >= nregs) | |
9270 | err += efunc(pc, "invalid register %u\n", rs); | |
9271 | break; | |
9272 | default: | |
9273 | err += efunc(pc, "invalid opcode %u\n", | |
9274 | DIF_INSTR_OP(instr)); | |
9275 | } | |
9276 | } | |
9277 | ||
9278 | if (dp->dtdo_len != 0 && | |
9279 | DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { | |
9280 | err += efunc(dp->dtdo_len - 1, | |
9281 | "expected 'ret' as last DIF instruction\n"); | |
9282 | } | |
9283 | ||
9284 | if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { | |
9285 | /* | |
9286 | * If we're not returning by reference, the size must be either | |
9287 | * 0 or the size of one of the base types. | |
9288 | */ | |
9289 | switch (dp->dtdo_rtype.dtdt_size) { | |
9290 | case 0: | |
9291 | case sizeof (uint8_t): | |
9292 | case sizeof (uint16_t): | |
9293 | case sizeof (uint32_t): | |
9294 | case sizeof (uint64_t): | |
9295 | break; | |
9296 | ||
9297 | default: | |
9298 | err += efunc(dp->dtdo_len - 1, "bad return size"); | |
9299 | } | |
9300 | } | |
9301 | ||
9302 | for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { | |
9303 | dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; | |
9304 | dtrace_diftype_t *vt, *et; | |
b0d623f7 | 9305 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9306 | uint_t id, ndx; |
b0d623f7 A |
9307 | #else |
9308 | uint_t id; | |
9309 | int ndx; | |
9310 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9311 | |
9312 | if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && | |
9313 | v->dtdv_scope != DIFV_SCOPE_THREAD && | |
9314 | v->dtdv_scope != DIFV_SCOPE_LOCAL) { | |
9315 | err += efunc(i, "unrecognized variable scope %d\n", | |
9316 | v->dtdv_scope); | |
9317 | break; | |
9318 | } | |
9319 | ||
9320 | if (v->dtdv_kind != DIFV_KIND_ARRAY && | |
9321 | v->dtdv_kind != DIFV_KIND_SCALAR) { | |
9322 | err += efunc(i, "unrecognized variable type %d\n", | |
9323 | v->dtdv_kind); | |
9324 | break; | |
9325 | } | |
9326 | ||
9327 | if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { | |
9328 | err += efunc(i, "%d exceeds variable id limit\n", id); | |
9329 | break; | |
9330 | } | |
9331 | ||
9332 | if (id < DIF_VAR_OTHER_UBASE) | |
9333 | continue; | |
9334 | ||
9335 | /* | |
9336 | * For user-defined variables, we need to check that this | |
9337 | * definition is identical to any previous definition that we | |
9338 | * encountered. | |
9339 | */ | |
9340 | ndx = id - DIF_VAR_OTHER_UBASE; | |
9341 | ||
9342 | switch (v->dtdv_scope) { | |
9343 | case DIFV_SCOPE_GLOBAL: | |
9344 | if (ndx < vstate->dtvs_nglobals) { | |
9345 | dtrace_statvar_t *svar; | |
9346 | ||
9347 | if ((svar = vstate->dtvs_globals[ndx]) != NULL) | |
9348 | existing = &svar->dtsv_var; | |
9349 | } | |
9350 | ||
9351 | break; | |
9352 | ||
9353 | case DIFV_SCOPE_THREAD: | |
9354 | if (ndx < vstate->dtvs_ntlocals) | |
9355 | existing = &vstate->dtvs_tlocals[ndx]; | |
9356 | break; | |
9357 | ||
9358 | case DIFV_SCOPE_LOCAL: | |
9359 | if (ndx < vstate->dtvs_nlocals) { | |
9360 | dtrace_statvar_t *svar; | |
9361 | ||
9362 | if ((svar = vstate->dtvs_locals[ndx]) != NULL) | |
9363 | existing = &svar->dtsv_var; | |
9364 | } | |
9365 | ||
9366 | break; | |
9367 | } | |
9368 | ||
9369 | vt = &v->dtdv_type; | |
9370 | ||
9371 | if (vt->dtdt_flags & DIF_TF_BYREF) { | |
9372 | if (vt->dtdt_size == 0) { | |
9373 | err += efunc(i, "zero-sized variable\n"); | |
9374 | break; | |
9375 | } | |
9376 | ||
9377 | if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && | |
9378 | vt->dtdt_size > dtrace_global_maxsize) { | |
9379 | err += efunc(i, "oversized by-ref global\n"); | |
9380 | break; | |
9381 | } | |
9382 | } | |
9383 | ||
9384 | if (existing == NULL || existing->dtdv_id == 0) | |
9385 | continue; | |
9386 | ||
9387 | ASSERT(existing->dtdv_id == v->dtdv_id); | |
9388 | ASSERT(existing->dtdv_scope == v->dtdv_scope); | |
9389 | ||
9390 | if (existing->dtdv_kind != v->dtdv_kind) | |
9391 | err += efunc(i, "%d changed variable kind\n", id); | |
9392 | ||
9393 | et = &existing->dtdv_type; | |
9394 | ||
9395 | if (vt->dtdt_flags != et->dtdt_flags) { | |
9396 | err += efunc(i, "%d changed variable type flags\n", id); | |
9397 | break; | |
9398 | } | |
9399 | ||
9400 | if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { | |
9401 | err += efunc(i, "%d changed variable type size\n", id); | |
9402 | break; | |
9403 | } | |
9404 | } | |
9405 | ||
9406 | return (err); | |
9407 | } | |
9408 | ||
9409 | /* | |
9410 | * Validate a DTrace DIF object that it is to be used as a helper. Helpers | |
9411 | * are much more constrained than normal DIFOs. Specifically, they may | |
9412 | * not: | |
9413 | * | |
9414 | * 1. Make calls to subroutines other than copyin(), copyinstr() or | |
9415 | * miscellaneous string routines | |
9416 | * 2. Access DTrace variables other than the args[] array, and the | |
9417 | * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. | |
9418 | * 3. Have thread-local variables. | |
9419 | * 4. Have dynamic variables. | |
9420 | */ | |
9421 | static int | |
9422 | dtrace_difo_validate_helper(dtrace_difo_t *dp) | |
9423 | { | |
9424 | int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; | |
9425 | int err = 0; | |
9426 | uint_t pc; | |
9427 | ||
9428 | for (pc = 0; pc < dp->dtdo_len; pc++) { | |
9429 | dif_instr_t instr = dp->dtdo_buf[pc]; | |
9430 | ||
9431 | uint_t v = DIF_INSTR_VAR(instr); | |
9432 | uint_t subr = DIF_INSTR_SUBR(instr); | |
9433 | uint_t op = DIF_INSTR_OP(instr); | |
9434 | ||
9435 | switch (op) { | |
9436 | case DIF_OP_OR: | |
9437 | case DIF_OP_XOR: | |
9438 | case DIF_OP_AND: | |
9439 | case DIF_OP_SLL: | |
9440 | case DIF_OP_SRL: | |
9441 | case DIF_OP_SRA: | |
9442 | case DIF_OP_SUB: | |
9443 | case DIF_OP_ADD: | |
9444 | case DIF_OP_MUL: | |
9445 | case DIF_OP_SDIV: | |
9446 | case DIF_OP_UDIV: | |
9447 | case DIF_OP_SREM: | |
9448 | case DIF_OP_UREM: | |
9449 | case DIF_OP_COPYS: | |
9450 | case DIF_OP_NOT: | |
9451 | case DIF_OP_MOV: | |
9452 | case DIF_OP_RLDSB: | |
9453 | case DIF_OP_RLDSH: | |
9454 | case DIF_OP_RLDSW: | |
9455 | case DIF_OP_RLDUB: | |
9456 | case DIF_OP_RLDUH: | |
9457 | case DIF_OP_RLDUW: | |
9458 | case DIF_OP_RLDX: | |
9459 | case DIF_OP_ULDSB: | |
9460 | case DIF_OP_ULDSH: | |
9461 | case DIF_OP_ULDSW: | |
9462 | case DIF_OP_ULDUB: | |
9463 | case DIF_OP_ULDUH: | |
9464 | case DIF_OP_ULDUW: | |
9465 | case DIF_OP_ULDX: | |
9466 | case DIF_OP_STB: | |
9467 | case DIF_OP_STH: | |
9468 | case DIF_OP_STW: | |
9469 | case DIF_OP_STX: | |
9470 | case DIF_OP_ALLOCS: | |
9471 | case DIF_OP_CMP: | |
9472 | case DIF_OP_SCMP: | |
9473 | case DIF_OP_TST: | |
9474 | case DIF_OP_BA: | |
9475 | case DIF_OP_BE: | |
9476 | case DIF_OP_BNE: | |
9477 | case DIF_OP_BG: | |
9478 | case DIF_OP_BGU: | |
9479 | case DIF_OP_BGE: | |
9480 | case DIF_OP_BGEU: | |
9481 | case DIF_OP_BL: | |
9482 | case DIF_OP_BLU: | |
9483 | case DIF_OP_BLE: | |
9484 | case DIF_OP_BLEU: | |
9485 | case DIF_OP_RET: | |
9486 | case DIF_OP_NOP: | |
9487 | case DIF_OP_POPTS: | |
9488 | case DIF_OP_FLUSHTS: | |
9489 | case DIF_OP_SETX: | |
9490 | case DIF_OP_SETS: | |
9491 | case DIF_OP_LDGA: | |
9492 | case DIF_OP_LDLS: | |
9493 | case DIF_OP_STGS: | |
9494 | case DIF_OP_STLS: | |
9495 | case DIF_OP_PUSHTR: | |
9496 | case DIF_OP_PUSHTV: | |
9497 | break; | |
9498 | ||
9499 | case DIF_OP_LDGS: | |
9500 | if (v >= DIF_VAR_OTHER_UBASE) | |
9501 | break; | |
9502 | ||
9503 | if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) | |
9504 | break; | |
9505 | ||
9506 | if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || | |
9507 | v == DIF_VAR_PPID || v == DIF_VAR_TID || | |
9508 | v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || | |
9509 | v == DIF_VAR_UID || v == DIF_VAR_GID) | |
9510 | break; | |
9511 | ||
9512 | err += efunc(pc, "illegal variable %u\n", v); | |
9513 | break; | |
9514 | ||
9515 | case DIF_OP_LDTA: | |
9516 | case DIF_OP_LDTS: | |
9517 | case DIF_OP_LDGAA: | |
9518 | case DIF_OP_LDTAA: | |
9519 | err += efunc(pc, "illegal dynamic variable load\n"); | |
9520 | break; | |
9521 | ||
9522 | case DIF_OP_STTS: | |
9523 | case DIF_OP_STGAA: | |
9524 | case DIF_OP_STTAA: | |
9525 | err += efunc(pc, "illegal dynamic variable store\n"); | |
9526 | break; | |
9527 | ||
9528 | case DIF_OP_CALL: | |
9529 | if (subr == DIF_SUBR_ALLOCA || | |
9530 | subr == DIF_SUBR_BCOPY || | |
9531 | subr == DIF_SUBR_COPYIN || | |
9532 | subr == DIF_SUBR_COPYINTO || | |
9533 | subr == DIF_SUBR_COPYINSTR || | |
9534 | subr == DIF_SUBR_INDEX || | |
b0d623f7 A |
9535 | subr == DIF_SUBR_INET_NTOA || |
9536 | subr == DIF_SUBR_INET_NTOA6 || | |
9537 | subr == DIF_SUBR_INET_NTOP || | |
2d21ac55 A |
9538 | subr == DIF_SUBR_LLTOSTR || |
9539 | subr == DIF_SUBR_RINDEX || | |
9540 | subr == DIF_SUBR_STRCHR || | |
9541 | subr == DIF_SUBR_STRJOIN || | |
9542 | subr == DIF_SUBR_STRRCHR || | |
9543 | subr == DIF_SUBR_STRSTR || | |
b0d623f7 A |
9544 | #if defined(__APPLE__) |
9545 | subr == DIF_SUBR_COREPROFILE || | |
9546 | #endif /* __APPLE__ */ | |
9547 | subr == DIF_SUBR_HTONS || | |
9548 | subr == DIF_SUBR_HTONL || | |
9549 | subr == DIF_SUBR_HTONLL || | |
9550 | subr == DIF_SUBR_NTOHS || | |
9551 | subr == DIF_SUBR_NTOHL || | |
9552 | subr == DIF_SUBR_NTOHLL) | |
2d21ac55 A |
9553 | break; |
9554 | ||
9555 | err += efunc(pc, "invalid subr %u\n", subr); | |
9556 | break; | |
9557 | ||
9558 | default: | |
9559 | err += efunc(pc, "invalid opcode %u\n", | |
9560 | DIF_INSTR_OP(instr)); | |
9561 | } | |
9562 | } | |
9563 | ||
9564 | return (err); | |
9565 | } | |
9566 | ||
9567 | /* | |
9568 | * Returns 1 if the expression in the DIF object can be cached on a per-thread | |
9569 | * basis; 0 if not. | |
9570 | */ | |
9571 | static int | |
9572 | dtrace_difo_cacheable(dtrace_difo_t *dp) | |
9573 | { | |
b0d623f7 | 9574 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9575 | int i; |
b0d623f7 A |
9576 | #else |
9577 | uint_t i; | |
9578 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9579 | |
9580 | if (dp == NULL) | |
9581 | return (0); | |
9582 | ||
9583 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
9584 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
9585 | ||
9586 | if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) | |
9587 | continue; | |
9588 | ||
9589 | switch (v->dtdv_id) { | |
9590 | case DIF_VAR_CURTHREAD: | |
9591 | case DIF_VAR_PID: | |
9592 | case DIF_VAR_TID: | |
9593 | case DIF_VAR_EXECNAME: | |
9594 | case DIF_VAR_ZONENAME: | |
9595 | break; | |
9596 | ||
9597 | default: | |
9598 | return (0); | |
9599 | } | |
9600 | } | |
9601 | ||
9602 | /* | |
9603 | * This DIF object may be cacheable. Now we need to look for any | |
9604 | * array loading instructions, any memory loading instructions, or | |
9605 | * any stores to thread-local variables. | |
9606 | */ | |
9607 | for (i = 0; i < dp->dtdo_len; i++) { | |
9608 | uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); | |
9609 | ||
9610 | if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || | |
9611 | (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || | |
9612 | (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || | |
9613 | op == DIF_OP_LDGA || op == DIF_OP_STTS) | |
9614 | return (0); | |
9615 | } | |
9616 | ||
9617 | return (1); | |
9618 | } | |
9619 | ||
9620 | static void | |
9621 | dtrace_difo_hold(dtrace_difo_t *dp) | |
9622 | { | |
b0d623f7 | 9623 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9624 | int i; |
b0d623f7 A |
9625 | #else |
9626 | uint_t i; | |
9627 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9628 | |
9629 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
9630 | ||
9631 | dp->dtdo_refcnt++; | |
9632 | ASSERT(dp->dtdo_refcnt != 0); | |
9633 | ||
9634 | /* | |
9635 | * We need to check this DIF object for references to the variable | |
9636 | * DIF_VAR_VTIMESTAMP. | |
9637 | */ | |
9638 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
9639 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
9640 | ||
9641 | if (v->dtdv_id != DIF_VAR_VTIMESTAMP) | |
9642 | continue; | |
9643 | ||
9644 | if (dtrace_vtime_references++ == 0) | |
9645 | dtrace_vtime_enable(); | |
9646 | } | |
9647 | } | |
9648 | ||
9649 | /* | |
9650 | * This routine calculates the dynamic variable chunksize for a given DIF | |
9651 | * object. The calculation is not fool-proof, and can probably be tricked by | |
9652 | * malicious DIF -- but it works for all compiler-generated DIF. Because this | |
9653 | * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail | |
9654 | * if a dynamic variable size exceeds the chunksize. | |
9655 | */ | |
9656 | static void | |
9657 | dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) | |
9658 | { | |
b0d623f7 | 9659 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9660 | uint64_t sval; |
b0d623f7 A |
9661 | #else |
9662 | uint64_t sval = 0; | |
9663 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9664 | dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ |
9665 | const dif_instr_t *text = dp->dtdo_buf; | |
9666 | uint_t pc, srd = 0; | |
9667 | uint_t ttop = 0; | |
9668 | size_t size, ksize; | |
9669 | uint_t id, i; | |
9670 | ||
9671 | for (pc = 0; pc < dp->dtdo_len; pc++) { | |
9672 | dif_instr_t instr = text[pc]; | |
9673 | uint_t op = DIF_INSTR_OP(instr); | |
9674 | uint_t rd = DIF_INSTR_RD(instr); | |
9675 | uint_t r1 = DIF_INSTR_R1(instr); | |
9676 | uint_t nkeys = 0; | |
9677 | uchar_t scope; | |
9678 | ||
9679 | dtrace_key_t *key = tupregs; | |
9680 | ||
9681 | switch (op) { | |
9682 | case DIF_OP_SETX: | |
9683 | sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; | |
9684 | srd = rd; | |
9685 | continue; | |
9686 | ||
9687 | case DIF_OP_STTS: | |
9688 | key = &tupregs[DIF_DTR_NREGS]; | |
9689 | key[0].dttk_size = 0; | |
9690 | key[1].dttk_size = 0; | |
9691 | nkeys = 2; | |
9692 | scope = DIFV_SCOPE_THREAD; | |
9693 | break; | |
9694 | ||
9695 | case DIF_OP_STGAA: | |
9696 | case DIF_OP_STTAA: | |
9697 | nkeys = ttop; | |
9698 | ||
9699 | if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) | |
9700 | key[nkeys++].dttk_size = 0; | |
9701 | ||
9702 | key[nkeys++].dttk_size = 0; | |
9703 | ||
9704 | if (op == DIF_OP_STTAA) { | |
9705 | scope = DIFV_SCOPE_THREAD; | |
9706 | } else { | |
9707 | scope = DIFV_SCOPE_GLOBAL; | |
9708 | } | |
9709 | ||
9710 | break; | |
9711 | ||
9712 | case DIF_OP_PUSHTR: | |
9713 | if (ttop == DIF_DTR_NREGS) | |
9714 | return; | |
9715 | ||
9716 | if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { | |
9717 | /* | |
9718 | * If the register for the size of the "pushtr" | |
9719 | * is %r0 (or the value is 0) and the type is | |
9720 | * a string, we'll use the system-wide default | |
9721 | * string size. | |
9722 | */ | |
9723 | tupregs[ttop++].dttk_size = | |
9724 | dtrace_strsize_default; | |
9725 | } else { | |
9726 | if (srd == 0) | |
9727 | return; | |
9728 | ||
9729 | tupregs[ttop++].dttk_size = sval; | |
9730 | } | |
9731 | ||
9732 | break; | |
9733 | ||
9734 | case DIF_OP_PUSHTV: | |
9735 | if (ttop == DIF_DTR_NREGS) | |
9736 | return; | |
9737 | ||
9738 | tupregs[ttop++].dttk_size = 0; | |
9739 | break; | |
9740 | ||
9741 | case DIF_OP_FLUSHTS: | |
9742 | ttop = 0; | |
9743 | break; | |
9744 | ||
9745 | case DIF_OP_POPTS: | |
9746 | if (ttop != 0) | |
9747 | ttop--; | |
9748 | break; | |
9749 | } | |
9750 | ||
9751 | sval = 0; | |
9752 | srd = 0; | |
9753 | ||
9754 | if (nkeys == 0) | |
9755 | continue; | |
9756 | ||
9757 | /* | |
9758 | * We have a dynamic variable allocation; calculate its size. | |
9759 | */ | |
9760 | for (ksize = 0, i = 0; i < nkeys; i++) | |
9761 | ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); | |
9762 | ||
9763 | size = sizeof (dtrace_dynvar_t); | |
9764 | size += sizeof (dtrace_key_t) * (nkeys - 1); | |
9765 | size += ksize; | |
9766 | ||
9767 | /* | |
9768 | * Now we need to determine the size of the stored data. | |
9769 | */ | |
9770 | id = DIF_INSTR_VAR(instr); | |
9771 | ||
9772 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
9773 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
9774 | ||
9775 | if (v->dtdv_id == id && v->dtdv_scope == scope) { | |
9776 | size += v->dtdv_type.dtdt_size; | |
9777 | break; | |
9778 | } | |
9779 | } | |
9780 | ||
9781 | if (i == dp->dtdo_varlen) | |
9782 | return; | |
9783 | ||
9784 | /* | |
9785 | * We have the size. If this is larger than the chunk size | |
9786 | * for our dynamic variable state, reset the chunk size. | |
9787 | */ | |
9788 | size = P2ROUNDUP(size, sizeof (uint64_t)); | |
9789 | ||
9790 | if (size > vstate->dtvs_dynvars.dtds_chunksize) | |
9791 | vstate->dtvs_dynvars.dtds_chunksize = size; | |
9792 | } | |
9793 | } | |
9794 | ||
9795 | static void | |
9796 | dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) | |
9797 | { | |
b0d623f7 | 9798 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
9799 | int i, oldsvars, osz, nsz, otlocals, ntlocals; |
9800 | uint_t id; | |
b0d623f7 A |
9801 | #else |
9802 | int oldsvars, osz, nsz, otlocals, ntlocals; | |
9803 | uint_t i, id; | |
9804 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9805 | |
9806 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
9807 | ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); | |
9808 | ||
9809 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
9810 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
b0d623f7 | 9811 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9812 | dtrace_statvar_t *svar, ***svarp; |
b0d623f7 A |
9813 | #else |
9814 | dtrace_statvar_t *svar; | |
9815 | dtrace_statvar_t ***svarp = NULL; | |
9816 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9817 | size_t dsize = 0; |
9818 | uint8_t scope = v->dtdv_scope; | |
b0d623f7 | 9819 | int *np = (int *)NULL; |
2d21ac55 A |
9820 | |
9821 | if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) | |
9822 | continue; | |
9823 | ||
9824 | id -= DIF_VAR_OTHER_UBASE; | |
9825 | ||
9826 | switch (scope) { | |
9827 | case DIFV_SCOPE_THREAD: | |
b0d623f7 | 9828 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9829 | while (id >= (otlocals = vstate->dtvs_ntlocals)) { |
b0d623f7 A |
9830 | #else |
9831 | while (id >= (uint_t)(otlocals = vstate->dtvs_ntlocals)) { | |
9832 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9833 | dtrace_difv_t *tlocals; |
9834 | ||
9835 | if ((ntlocals = (otlocals << 1)) == 0) | |
9836 | ntlocals = 1; | |
9837 | ||
9838 | osz = otlocals * sizeof (dtrace_difv_t); | |
9839 | nsz = ntlocals * sizeof (dtrace_difv_t); | |
9840 | ||
9841 | tlocals = kmem_zalloc(nsz, KM_SLEEP); | |
9842 | ||
9843 | if (osz != 0) { | |
9844 | bcopy(vstate->dtvs_tlocals, | |
9845 | tlocals, osz); | |
9846 | kmem_free(vstate->dtvs_tlocals, osz); | |
9847 | } | |
9848 | ||
9849 | vstate->dtvs_tlocals = tlocals; | |
9850 | vstate->dtvs_ntlocals = ntlocals; | |
9851 | } | |
9852 | ||
9853 | vstate->dtvs_tlocals[id] = *v; | |
9854 | continue; | |
9855 | ||
9856 | case DIFV_SCOPE_LOCAL: | |
9857 | np = &vstate->dtvs_nlocals; | |
9858 | svarp = &vstate->dtvs_locals; | |
9859 | ||
9860 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) | |
c910b4d9 | 9861 | dsize = (int)NCPU * (v->dtdv_type.dtdt_size + |
2d21ac55 A |
9862 | sizeof (uint64_t)); |
9863 | else | |
c910b4d9 | 9864 | dsize = (int)NCPU * sizeof (uint64_t); |
2d21ac55 A |
9865 | |
9866 | break; | |
9867 | ||
9868 | case DIFV_SCOPE_GLOBAL: | |
9869 | np = &vstate->dtvs_nglobals; | |
9870 | svarp = &vstate->dtvs_globals; | |
9871 | ||
9872 | if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) | |
9873 | dsize = v->dtdv_type.dtdt_size + | |
9874 | sizeof (uint64_t); | |
9875 | ||
9876 | break; | |
9877 | ||
9878 | default: | |
9879 | ASSERT(0); | |
9880 | } | |
9881 | ||
b0d623f7 | 9882 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9883 | while (id >= (oldsvars = *np)) { |
b0d623f7 A |
9884 | #else |
9885 | while (id >= (uint_t)(oldsvars = *np)) { | |
9886 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9887 | dtrace_statvar_t **statics; |
9888 | int newsvars, oldsize, newsize; | |
9889 | ||
9890 | if ((newsvars = (oldsvars << 1)) == 0) | |
9891 | newsvars = 1; | |
9892 | ||
9893 | oldsize = oldsvars * sizeof (dtrace_statvar_t *); | |
9894 | newsize = newsvars * sizeof (dtrace_statvar_t *); | |
9895 | ||
9896 | statics = kmem_zalloc(newsize, KM_SLEEP); | |
9897 | ||
9898 | if (oldsize != 0) { | |
9899 | bcopy(*svarp, statics, oldsize); | |
9900 | kmem_free(*svarp, oldsize); | |
9901 | } | |
9902 | ||
9903 | *svarp = statics; | |
9904 | *np = newsvars; | |
9905 | } | |
9906 | ||
9907 | if ((svar = (*svarp)[id]) == NULL) { | |
9908 | svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); | |
9909 | svar->dtsv_var = *v; | |
9910 | ||
9911 | if ((svar->dtsv_size = dsize) != 0) { | |
9912 | svar->dtsv_data = (uint64_t)(uintptr_t) | |
9913 | kmem_zalloc(dsize, KM_SLEEP); | |
9914 | } | |
9915 | ||
9916 | (*svarp)[id] = svar; | |
9917 | } | |
9918 | ||
9919 | svar->dtsv_refcnt++; | |
9920 | } | |
9921 | ||
9922 | dtrace_difo_chunksize(dp, vstate); | |
9923 | dtrace_difo_hold(dp); | |
9924 | } | |
9925 | ||
9926 | static dtrace_difo_t * | |
9927 | dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) | |
9928 | { | |
9929 | dtrace_difo_t *new; | |
9930 | size_t sz; | |
9931 | ||
9932 | ASSERT(dp->dtdo_buf != NULL); | |
9933 | ASSERT(dp->dtdo_refcnt != 0); | |
9934 | ||
9935 | new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); | |
9936 | ||
9937 | ASSERT(dp->dtdo_buf != NULL); | |
9938 | sz = dp->dtdo_len * sizeof (dif_instr_t); | |
9939 | new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); | |
9940 | bcopy(dp->dtdo_buf, new->dtdo_buf, sz); | |
9941 | new->dtdo_len = dp->dtdo_len; | |
9942 | ||
9943 | if (dp->dtdo_strtab != NULL) { | |
9944 | ASSERT(dp->dtdo_strlen != 0); | |
9945 | new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); | |
9946 | bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); | |
9947 | new->dtdo_strlen = dp->dtdo_strlen; | |
9948 | } | |
9949 | ||
9950 | if (dp->dtdo_inttab != NULL) { | |
9951 | ASSERT(dp->dtdo_intlen != 0); | |
9952 | sz = dp->dtdo_intlen * sizeof (uint64_t); | |
9953 | new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); | |
9954 | bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); | |
9955 | new->dtdo_intlen = dp->dtdo_intlen; | |
9956 | } | |
9957 | ||
9958 | if (dp->dtdo_vartab != NULL) { | |
9959 | ASSERT(dp->dtdo_varlen != 0); | |
9960 | sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); | |
9961 | new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); | |
9962 | bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); | |
9963 | new->dtdo_varlen = dp->dtdo_varlen; | |
9964 | } | |
9965 | ||
9966 | dtrace_difo_init(new, vstate); | |
9967 | return (new); | |
9968 | } | |
9969 | ||
9970 | static void | |
9971 | dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) | |
9972 | { | |
b0d623f7 | 9973 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 9974 | int i; |
b0d623f7 A |
9975 | #else |
9976 | uint_t i; | |
9977 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9978 | |
9979 | ASSERT(dp->dtdo_refcnt == 0); | |
9980 | ||
9981 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
9982 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
b0d623f7 | 9983 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
9984 | dtrace_statvar_t *svar, **svarp; |
9985 | uint_t id; | |
9986 | uint8_t scope = v->dtdv_scope; | |
b0d623f7 A |
9987 | int *np; |
9988 | #else | |
9989 | dtrace_statvar_t *svar; | |
9990 | dtrace_statvar_t **svarp = NULL; | |
9991 | uint_t id; | |
9992 | uint8_t scope = v->dtdv_scope; | |
9993 | int *np = NULL; | |
9994 | #endif /* __APPLE__ */ | |
2d21ac55 A |
9995 | |
9996 | switch (scope) { | |
9997 | case DIFV_SCOPE_THREAD: | |
9998 | continue; | |
9999 | ||
10000 | case DIFV_SCOPE_LOCAL: | |
10001 | np = &vstate->dtvs_nlocals; | |
10002 | svarp = vstate->dtvs_locals; | |
10003 | break; | |
10004 | ||
10005 | case DIFV_SCOPE_GLOBAL: | |
10006 | np = &vstate->dtvs_nglobals; | |
10007 | svarp = vstate->dtvs_globals; | |
10008 | break; | |
10009 | ||
10010 | default: | |
10011 | ASSERT(0); | |
10012 | } | |
10013 | ||
10014 | if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) | |
10015 | continue; | |
10016 | ||
10017 | id -= DIF_VAR_OTHER_UBASE; | |
b0d623f7 A |
10018 | |
10019 | #if !defined(__APPLE__) /* Quiet compiler warnings */ | |
2d21ac55 | 10020 | ASSERT(id < *np); |
b0d623f7 A |
10021 | #else |
10022 | ASSERT(id < (uint_t)*np); | |
10023 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10024 | |
10025 | svar = svarp[id]; | |
10026 | ASSERT(svar != NULL); | |
10027 | ASSERT(svar->dtsv_refcnt > 0); | |
10028 | ||
10029 | if (--svar->dtsv_refcnt > 0) | |
10030 | continue; | |
10031 | ||
10032 | if (svar->dtsv_size != 0) { | |
10033 | ASSERT(svar->dtsv_data != NULL); | |
10034 | kmem_free((void *)(uintptr_t)svar->dtsv_data, | |
10035 | svar->dtsv_size); | |
10036 | } | |
10037 | ||
10038 | kmem_free(svar, sizeof (dtrace_statvar_t)); | |
10039 | svarp[id] = NULL; | |
10040 | } | |
10041 | ||
10042 | kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); | |
10043 | kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); | |
10044 | kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); | |
10045 | kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); | |
10046 | ||
10047 | kmem_free(dp, sizeof (dtrace_difo_t)); | |
10048 | } | |
10049 | ||
10050 | static void | |
10051 | dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) | |
10052 | { | |
b0d623f7 | 10053 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 10054 | int i; |
b0d623f7 A |
10055 | #else |
10056 | uint_t i; | |
10057 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10058 | |
10059 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10060 | ASSERT(dp->dtdo_refcnt != 0); | |
10061 | ||
10062 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
10063 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
10064 | ||
10065 | if (v->dtdv_id != DIF_VAR_VTIMESTAMP) | |
10066 | continue; | |
10067 | ||
10068 | ASSERT(dtrace_vtime_references > 0); | |
10069 | if (--dtrace_vtime_references == 0) | |
10070 | dtrace_vtime_disable(); | |
10071 | } | |
10072 | ||
10073 | if (--dp->dtdo_refcnt == 0) | |
10074 | dtrace_difo_destroy(dp, vstate); | |
10075 | } | |
10076 | ||
10077 | /* | |
10078 | * DTrace Format Functions | |
10079 | */ | |
10080 | static uint16_t | |
10081 | dtrace_format_add(dtrace_state_t *state, char *str) | |
10082 | { | |
10083 | char *fmt, **new; | |
10084 | uint16_t ndx, len = strlen(str) + 1; | |
10085 | ||
10086 | fmt = kmem_zalloc(len, KM_SLEEP); | |
10087 | bcopy(str, fmt, len); | |
10088 | ||
10089 | for (ndx = 0; ndx < state->dts_nformats; ndx++) { | |
10090 | if (state->dts_formats[ndx] == NULL) { | |
10091 | state->dts_formats[ndx] = fmt; | |
10092 | return (ndx + 1); | |
10093 | } | |
10094 | } | |
10095 | ||
10096 | if (state->dts_nformats == USHRT_MAX) { | |
10097 | /* | |
10098 | * This is only likely if a denial-of-service attack is being | |
10099 | * attempted. As such, it's okay to fail silently here. | |
10100 | */ | |
10101 | kmem_free(fmt, len); | |
10102 | return (0); | |
10103 | } | |
10104 | ||
10105 | /* | |
10106 | * For simplicity, we always resize the formats array to be exactly the | |
10107 | * number of formats. | |
10108 | */ | |
10109 | ndx = state->dts_nformats++; | |
10110 | new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); | |
10111 | ||
10112 | if (state->dts_formats != NULL) { | |
10113 | ASSERT(ndx != 0); | |
10114 | bcopy(state->dts_formats, new, ndx * sizeof (char *)); | |
10115 | kmem_free(state->dts_formats, ndx * sizeof (char *)); | |
10116 | } | |
10117 | ||
10118 | state->dts_formats = new; | |
10119 | state->dts_formats[ndx] = fmt; | |
10120 | ||
10121 | return (ndx + 1); | |
10122 | } | |
10123 | ||
10124 | static void | |
10125 | dtrace_format_remove(dtrace_state_t *state, uint16_t format) | |
10126 | { | |
10127 | char *fmt; | |
10128 | ||
10129 | ASSERT(state->dts_formats != NULL); | |
10130 | ASSERT(format <= state->dts_nformats); | |
10131 | ASSERT(state->dts_formats[format - 1] != NULL); | |
10132 | ||
10133 | fmt = state->dts_formats[format - 1]; | |
10134 | kmem_free(fmt, strlen(fmt) + 1); | |
10135 | state->dts_formats[format - 1] = NULL; | |
10136 | } | |
10137 | ||
10138 | static void | |
10139 | dtrace_format_destroy(dtrace_state_t *state) | |
10140 | { | |
10141 | int i; | |
10142 | ||
10143 | if (state->dts_nformats == 0) { | |
10144 | ASSERT(state->dts_formats == NULL); | |
10145 | return; | |
10146 | } | |
10147 | ||
10148 | ASSERT(state->dts_formats != NULL); | |
10149 | ||
10150 | for (i = 0; i < state->dts_nformats; i++) { | |
10151 | char *fmt = state->dts_formats[i]; | |
10152 | ||
10153 | if (fmt == NULL) | |
10154 | continue; | |
10155 | ||
10156 | kmem_free(fmt, strlen(fmt) + 1); | |
10157 | } | |
10158 | ||
10159 | kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); | |
10160 | state->dts_nformats = 0; | |
10161 | state->dts_formats = NULL; | |
10162 | } | |
10163 | ||
10164 | /* | |
10165 | * DTrace Predicate Functions | |
10166 | */ | |
10167 | static dtrace_predicate_t * | |
10168 | dtrace_predicate_create(dtrace_difo_t *dp) | |
10169 | { | |
10170 | dtrace_predicate_t *pred; | |
10171 | ||
10172 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10173 | ASSERT(dp->dtdo_refcnt != 0); | |
10174 | ||
10175 | pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); | |
10176 | pred->dtp_difo = dp; | |
10177 | pred->dtp_refcnt = 1; | |
10178 | ||
10179 | if (!dtrace_difo_cacheable(dp)) | |
10180 | return (pred); | |
10181 | ||
10182 | if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { | |
10183 | /* | |
10184 | * This is only theoretically possible -- we have had 2^32 | |
10185 | * cacheable predicates on this machine. We cannot allow any | |
10186 | * more predicates to become cacheable: as unlikely as it is, | |
10187 | * there may be a thread caching a (now stale) predicate cache | |
10188 | * ID. (N.B.: the temptation is being successfully resisted to | |
10189 | * have this cmn_err() "Holy shit -- we executed this code!") | |
10190 | */ | |
10191 | return (pred); | |
10192 | } | |
10193 | ||
10194 | pred->dtp_cacheid = dtrace_predcache_id++; | |
10195 | ||
10196 | return (pred); | |
10197 | } | |
10198 | ||
10199 | static void | |
10200 | dtrace_predicate_hold(dtrace_predicate_t *pred) | |
10201 | { | |
10202 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10203 | ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); | |
10204 | ASSERT(pred->dtp_refcnt > 0); | |
10205 | ||
10206 | pred->dtp_refcnt++; | |
10207 | } | |
10208 | ||
10209 | static void | |
10210 | dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) | |
10211 | { | |
10212 | dtrace_difo_t *dp = pred->dtp_difo; | |
b0d623f7 | 10213 | #pragma unused(dp) /* __APPLE__ */ |
2d21ac55 A |
10214 | |
10215 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10216 | ASSERT(dp != NULL && dp->dtdo_refcnt != 0); | |
10217 | ASSERT(pred->dtp_refcnt > 0); | |
10218 | ||
10219 | if (--pred->dtp_refcnt == 0) { | |
10220 | dtrace_difo_release(pred->dtp_difo, vstate); | |
10221 | kmem_free(pred, sizeof (dtrace_predicate_t)); | |
10222 | } | |
10223 | } | |
10224 | ||
10225 | /* | |
10226 | * DTrace Action Description Functions | |
10227 | */ | |
10228 | static dtrace_actdesc_t * | |
10229 | dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, | |
10230 | uint64_t uarg, uint64_t arg) | |
10231 | { | |
10232 | dtrace_actdesc_t *act; | |
10233 | ||
b0d623f7 A |
10234 | ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && |
10235 | arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); | |
2d21ac55 A |
10236 | |
10237 | act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); | |
10238 | act->dtad_kind = kind; | |
10239 | act->dtad_ntuple = ntuple; | |
10240 | act->dtad_uarg = uarg; | |
10241 | act->dtad_arg = arg; | |
10242 | act->dtad_refcnt = 1; | |
10243 | ||
10244 | return (act); | |
10245 | } | |
10246 | ||
10247 | static void | |
10248 | dtrace_actdesc_hold(dtrace_actdesc_t *act) | |
10249 | { | |
10250 | ASSERT(act->dtad_refcnt >= 1); | |
10251 | act->dtad_refcnt++; | |
10252 | } | |
10253 | ||
10254 | static void | |
10255 | dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) | |
10256 | { | |
10257 | dtrace_actkind_t kind = act->dtad_kind; | |
10258 | dtrace_difo_t *dp; | |
10259 | ||
10260 | ASSERT(act->dtad_refcnt >= 1); | |
10261 | ||
10262 | if (--act->dtad_refcnt != 0) | |
10263 | return; | |
10264 | ||
10265 | if ((dp = act->dtad_difo) != NULL) | |
10266 | dtrace_difo_release(dp, vstate); | |
10267 | ||
10268 | if (DTRACEACT_ISPRINTFLIKE(kind)) { | |
10269 | char *str = (char *)(uintptr_t)act->dtad_arg; | |
10270 | ||
b0d623f7 A |
10271 | ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || |
10272 | (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); | |
2d21ac55 A |
10273 | |
10274 | if (str != NULL) | |
10275 | kmem_free(str, strlen(str) + 1); | |
10276 | } | |
10277 | ||
10278 | kmem_free(act, sizeof (dtrace_actdesc_t)); | |
10279 | } | |
10280 | ||
10281 | /* | |
10282 | * DTrace ECB Functions | |
10283 | */ | |
10284 | static dtrace_ecb_t * | |
10285 | dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) | |
10286 | { | |
10287 | dtrace_ecb_t *ecb; | |
10288 | dtrace_epid_t epid; | |
10289 | ||
10290 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10291 | ||
10292 | ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); | |
10293 | ecb->dte_predicate = NULL; | |
10294 | ecb->dte_probe = probe; | |
10295 | ||
10296 | /* | |
10297 | * The default size is the size of the default action: recording | |
10298 | * the epid. | |
10299 | */ | |
10300 | ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); | |
10301 | ecb->dte_alignment = sizeof (dtrace_epid_t); | |
10302 | ||
10303 | epid = state->dts_epid++; | |
10304 | ||
b0d623f7 | 10305 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 10306 | if (epid - 1 >= state->dts_necbs) { |
b0d623f7 A |
10307 | #else |
10308 | if (epid - 1 >= (dtrace_epid_t)state->dts_necbs) { | |
10309 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10310 | dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; |
10311 | int necbs = state->dts_necbs << 1; | |
10312 | ||
b0d623f7 | 10313 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 10314 | ASSERT(epid == state->dts_necbs + 1); |
b0d623f7 A |
10315 | #else |
10316 | ASSERT(epid == (dtrace_epid_t)state->dts_necbs + 1); | |
10317 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10318 | |
10319 | if (necbs == 0) { | |
10320 | ASSERT(oecbs == NULL); | |
10321 | necbs = 1; | |
10322 | } | |
10323 | ||
10324 | ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); | |
10325 | ||
10326 | if (oecbs != NULL) | |
10327 | bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); | |
10328 | ||
10329 | dtrace_membar_producer(); | |
10330 | state->dts_ecbs = ecbs; | |
10331 | ||
10332 | if (oecbs != NULL) { | |
10333 | /* | |
10334 | * If this state is active, we must dtrace_sync() | |
10335 | * before we can free the old dts_ecbs array: we're | |
10336 | * coming in hot, and there may be active ring | |
10337 | * buffer processing (which indexes into the dts_ecbs | |
10338 | * array) on another CPU. | |
10339 | */ | |
10340 | if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) | |
10341 | dtrace_sync(); | |
10342 | ||
10343 | kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); | |
10344 | } | |
10345 | ||
10346 | dtrace_membar_producer(); | |
10347 | state->dts_necbs = necbs; | |
10348 | } | |
10349 | ||
10350 | ecb->dte_state = state; | |
10351 | ||
10352 | ASSERT(state->dts_ecbs[epid - 1] == NULL); | |
10353 | dtrace_membar_producer(); | |
10354 | state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; | |
10355 | ||
10356 | return (ecb); | |
10357 | } | |
10358 | ||
10359 | static void | |
10360 | dtrace_ecb_enable(dtrace_ecb_t *ecb) | |
10361 | { | |
10362 | dtrace_probe_t *probe = ecb->dte_probe; | |
10363 | ||
10364 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
10365 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10366 | ASSERT(ecb->dte_next == NULL); | |
10367 | ||
10368 | if (probe == NULL) { | |
10369 | /* | |
10370 | * This is the NULL probe -- there's nothing to do. | |
10371 | */ | |
10372 | return; | |
10373 | } | |
10374 | ||
10375 | if (probe->dtpr_ecb == NULL) { | |
10376 | dtrace_provider_t *prov = probe->dtpr_provider; | |
10377 | ||
10378 | /* | |
10379 | * We're the first ECB on this probe. | |
10380 | */ | |
10381 | probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; | |
10382 | ||
10383 | if (ecb->dte_predicate != NULL) | |
10384 | probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; | |
10385 | ||
10386 | prov->dtpv_pops.dtps_enable(prov->dtpv_arg, | |
10387 | probe->dtpr_id, probe->dtpr_arg); | |
10388 | } else { | |
10389 | /* | |
10390 | * This probe is already active. Swing the last pointer to | |
10391 | * point to the new ECB, and issue a dtrace_sync() to assure | |
10392 | * that all CPUs have seen the change. | |
10393 | */ | |
10394 | ASSERT(probe->dtpr_ecb_last != NULL); | |
10395 | probe->dtpr_ecb_last->dte_next = ecb; | |
10396 | probe->dtpr_ecb_last = ecb; | |
10397 | probe->dtpr_predcache = 0; | |
10398 | ||
10399 | dtrace_sync(); | |
10400 | } | |
10401 | } | |
10402 | ||
10403 | static void | |
10404 | dtrace_ecb_resize(dtrace_ecb_t *ecb) | |
10405 | { | |
10406 | uint32_t maxalign = sizeof (dtrace_epid_t); | |
10407 | uint32_t align = sizeof (uint8_t), offs, diff; | |
10408 | dtrace_action_t *act; | |
10409 | int wastuple = 0; | |
10410 | uint32_t aggbase = UINT32_MAX; | |
10411 | dtrace_state_t *state = ecb->dte_state; | |
10412 | ||
10413 | /* | |
10414 | * If we record anything, we always record the epid. (And we always | |
10415 | * record it first.) | |
10416 | */ | |
10417 | offs = sizeof (dtrace_epid_t); | |
10418 | ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); | |
10419 | ||
10420 | for (act = ecb->dte_action; act != NULL; act = act->dta_next) { | |
10421 | dtrace_recdesc_t *rec = &act->dta_rec; | |
10422 | ||
10423 | if ((align = rec->dtrd_alignment) > maxalign) | |
10424 | maxalign = align; | |
10425 | ||
10426 | if (!wastuple && act->dta_intuple) { | |
10427 | /* | |
10428 | * This is the first record in a tuple. Align the | |
10429 | * offset to be at offset 4 in an 8-byte aligned | |
10430 | * block. | |
10431 | */ | |
10432 | diff = offs + sizeof (dtrace_aggid_t); | |
10433 | ||
c910b4d9 | 10434 | if ((diff = (diff & (sizeof (uint64_t) - 1)))) |
2d21ac55 A |
10435 | offs += sizeof (uint64_t) - diff; |
10436 | ||
10437 | aggbase = offs - sizeof (dtrace_aggid_t); | |
10438 | ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); | |
10439 | } | |
10440 | ||
10441 | /*LINTED*/ | |
10442 | if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { | |
10443 | /* | |
10444 | * The current offset is not properly aligned; align it. | |
10445 | */ | |
10446 | offs += align - diff; | |
10447 | } | |
10448 | ||
10449 | rec->dtrd_offset = offs; | |
10450 | ||
10451 | if (offs + rec->dtrd_size > ecb->dte_needed) { | |
10452 | ecb->dte_needed = offs + rec->dtrd_size; | |
10453 | ||
10454 | if (ecb->dte_needed > state->dts_needed) | |
10455 | state->dts_needed = ecb->dte_needed; | |
10456 | } | |
10457 | ||
10458 | if (DTRACEACT_ISAGG(act->dta_kind)) { | |
10459 | dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; | |
10460 | dtrace_action_t *first = agg->dtag_first, *prev; | |
10461 | ||
10462 | ASSERT(rec->dtrd_size != 0 && first != NULL); | |
10463 | ASSERT(wastuple); | |
10464 | ASSERT(aggbase != UINT32_MAX); | |
10465 | ||
10466 | agg->dtag_base = aggbase; | |
10467 | ||
10468 | while ((prev = first->dta_prev) != NULL && | |
10469 | DTRACEACT_ISAGG(prev->dta_kind)) { | |
10470 | agg = (dtrace_aggregation_t *)prev; | |
10471 | first = agg->dtag_first; | |
10472 | } | |
10473 | ||
10474 | if (prev != NULL) { | |
10475 | offs = prev->dta_rec.dtrd_offset + | |
10476 | prev->dta_rec.dtrd_size; | |
10477 | } else { | |
10478 | offs = sizeof (dtrace_epid_t); | |
10479 | } | |
10480 | wastuple = 0; | |
10481 | } else { | |
10482 | if (!act->dta_intuple) | |
10483 | ecb->dte_size = offs + rec->dtrd_size; | |
10484 | ||
10485 | offs += rec->dtrd_size; | |
10486 | } | |
10487 | ||
10488 | wastuple = act->dta_intuple; | |
10489 | } | |
10490 | ||
10491 | if ((act = ecb->dte_action) != NULL && | |
10492 | !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && | |
10493 | ecb->dte_size == sizeof (dtrace_epid_t)) { | |
10494 | /* | |
10495 | * If the size is still sizeof (dtrace_epid_t), then all | |
10496 | * actions store no data; set the size to 0. | |
10497 | */ | |
10498 | ecb->dte_alignment = maxalign; | |
10499 | ecb->dte_size = 0; | |
10500 | ||
10501 | /* | |
10502 | * If the needed space is still sizeof (dtrace_epid_t), then | |
10503 | * all actions need no additional space; set the needed | |
10504 | * size to 0. | |
10505 | */ | |
10506 | if (ecb->dte_needed == sizeof (dtrace_epid_t)) | |
10507 | ecb->dte_needed = 0; | |
10508 | ||
10509 | return; | |
10510 | } | |
10511 | ||
10512 | /* | |
10513 | * Set our alignment, and make sure that the dte_size and dte_needed | |
10514 | * are aligned to the size of an EPID. | |
10515 | */ | |
10516 | ecb->dte_alignment = maxalign; | |
10517 | ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & | |
10518 | ~(sizeof (dtrace_epid_t) - 1); | |
10519 | ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & | |
10520 | ~(sizeof (dtrace_epid_t) - 1); | |
10521 | ASSERT(ecb->dte_size <= ecb->dte_needed); | |
10522 | } | |
10523 | ||
10524 | static dtrace_action_t * | |
10525 | dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) | |
10526 | { | |
10527 | dtrace_aggregation_t *agg; | |
10528 | size_t size = sizeof (uint64_t); | |
10529 | int ntuple = desc->dtad_ntuple; | |
10530 | dtrace_action_t *act; | |
10531 | dtrace_recdesc_t *frec; | |
10532 | dtrace_aggid_t aggid; | |
10533 | dtrace_state_t *state = ecb->dte_state; | |
10534 | ||
10535 | agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); | |
10536 | agg->dtag_ecb = ecb; | |
10537 | ||
10538 | ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); | |
10539 | ||
10540 | switch (desc->dtad_kind) { | |
10541 | case DTRACEAGG_MIN: | |
b0d623f7 | 10542 | agg->dtag_initial = INT64_MAX; |
2d21ac55 A |
10543 | agg->dtag_aggregate = dtrace_aggregate_min; |
10544 | break; | |
10545 | ||
10546 | case DTRACEAGG_MAX: | |
b0d623f7 | 10547 | agg->dtag_initial = INT64_MIN; |
2d21ac55 A |
10548 | agg->dtag_aggregate = dtrace_aggregate_max; |
10549 | break; | |
10550 | ||
10551 | case DTRACEAGG_COUNT: | |
10552 | agg->dtag_aggregate = dtrace_aggregate_count; | |
10553 | break; | |
10554 | ||
10555 | case DTRACEAGG_QUANTIZE: | |
10556 | agg->dtag_aggregate = dtrace_aggregate_quantize; | |
10557 | size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * | |
10558 | sizeof (uint64_t); | |
10559 | break; | |
10560 | ||
10561 | case DTRACEAGG_LQUANTIZE: { | |
10562 | uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); | |
10563 | uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); | |
10564 | ||
10565 | agg->dtag_initial = desc->dtad_arg; | |
10566 | agg->dtag_aggregate = dtrace_aggregate_lquantize; | |
10567 | ||
10568 | if (step == 0 || levels == 0) | |
10569 | goto err; | |
10570 | ||
10571 | size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); | |
10572 | break; | |
10573 | } | |
10574 | ||
10575 | case DTRACEAGG_AVG: | |
10576 | agg->dtag_aggregate = dtrace_aggregate_avg; | |
10577 | size = sizeof (uint64_t) * 2; | |
10578 | break; | |
10579 | ||
b0d623f7 A |
10580 | case DTRACEAGG_STDDEV: |
10581 | agg->dtag_aggregate = dtrace_aggregate_stddev; | |
10582 | size = sizeof (uint64_t) * 4; | |
10583 | break; | |
10584 | ||
2d21ac55 A |
10585 | case DTRACEAGG_SUM: |
10586 | agg->dtag_aggregate = dtrace_aggregate_sum; | |
10587 | break; | |
10588 | ||
10589 | default: | |
10590 | goto err; | |
10591 | } | |
10592 | ||
10593 | agg->dtag_action.dta_rec.dtrd_size = size; | |
10594 | ||
10595 | if (ntuple == 0) | |
10596 | goto err; | |
10597 | ||
10598 | /* | |
10599 | * We must make sure that we have enough actions for the n-tuple. | |
10600 | */ | |
10601 | for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { | |
10602 | if (DTRACEACT_ISAGG(act->dta_kind)) | |
10603 | break; | |
10604 | ||
10605 | if (--ntuple == 0) { | |
10606 | /* | |
10607 | * This is the action with which our n-tuple begins. | |
10608 | */ | |
10609 | agg->dtag_first = act; | |
10610 | goto success; | |
10611 | } | |
10612 | } | |
10613 | ||
10614 | /* | |
10615 | * This n-tuple is short by ntuple elements. Return failure. | |
10616 | */ | |
10617 | ASSERT(ntuple != 0); | |
10618 | err: | |
10619 | kmem_free(agg, sizeof (dtrace_aggregation_t)); | |
10620 | return (NULL); | |
10621 | ||
10622 | success: | |
10623 | /* | |
10624 | * If the last action in the tuple has a size of zero, it's actually | |
10625 | * an expression argument for the aggregating action. | |
10626 | */ | |
10627 | ASSERT(ecb->dte_action_last != NULL); | |
10628 | act = ecb->dte_action_last; | |
10629 | ||
10630 | if (act->dta_kind == DTRACEACT_DIFEXPR) { | |
10631 | ASSERT(act->dta_difo != NULL); | |
10632 | ||
10633 | if (act->dta_difo->dtdo_rtype.dtdt_size == 0) | |
10634 | agg->dtag_hasarg = 1; | |
10635 | } | |
10636 | ||
10637 | /* | |
10638 | * We need to allocate an id for this aggregation. | |
10639 | */ | |
10640 | aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, | |
10641 | VM_BESTFIT | VM_SLEEP); | |
10642 | ||
b0d623f7 | 10643 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 10644 | if (aggid - 1 >= state->dts_naggregations) { |
b0d623f7 A |
10645 | #else |
10646 | if (aggid - 1 >= (dtrace_aggid_t)state->dts_naggregations) { | |
10647 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10648 | dtrace_aggregation_t **oaggs = state->dts_aggregations; |
10649 | dtrace_aggregation_t **aggs; | |
10650 | int naggs = state->dts_naggregations << 1; | |
10651 | int onaggs = state->dts_naggregations; | |
10652 | ||
b0d623f7 | 10653 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 10654 | ASSERT(aggid == state->dts_naggregations + 1); |
b0d623f7 A |
10655 | #else |
10656 | ASSERT(aggid == (dtrace_aggid_t)state->dts_naggregations + 1); | |
10657 | #endif /* __APPLE */ | |
2d21ac55 A |
10658 | |
10659 | if (naggs == 0) { | |
10660 | ASSERT(oaggs == NULL); | |
10661 | naggs = 1; | |
10662 | } | |
10663 | ||
10664 | aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); | |
10665 | ||
10666 | if (oaggs != NULL) { | |
10667 | bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); | |
10668 | kmem_free(oaggs, onaggs * sizeof (*aggs)); | |
10669 | } | |
10670 | ||
10671 | state->dts_aggregations = aggs; | |
10672 | state->dts_naggregations = naggs; | |
10673 | } | |
10674 | ||
10675 | ASSERT(state->dts_aggregations[aggid - 1] == NULL); | |
10676 | state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; | |
10677 | ||
10678 | frec = &agg->dtag_first->dta_rec; | |
10679 | if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) | |
10680 | frec->dtrd_alignment = sizeof (dtrace_aggid_t); | |
10681 | ||
10682 | for (act = agg->dtag_first; act != NULL; act = act->dta_next) { | |
10683 | ASSERT(!act->dta_intuple); | |
10684 | act->dta_intuple = 1; | |
10685 | } | |
10686 | ||
10687 | return (&agg->dtag_action); | |
10688 | } | |
10689 | ||
10690 | static void | |
10691 | dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) | |
10692 | { | |
10693 | dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; | |
10694 | dtrace_state_t *state = ecb->dte_state; | |
10695 | dtrace_aggid_t aggid = agg->dtag_id; | |
10696 | ||
10697 | ASSERT(DTRACEACT_ISAGG(act->dta_kind)); | |
10698 | vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); | |
10699 | ||
10700 | ASSERT(state->dts_aggregations[aggid - 1] == agg); | |
10701 | state->dts_aggregations[aggid - 1] = NULL; | |
10702 | ||
10703 | kmem_free(agg, sizeof (dtrace_aggregation_t)); | |
10704 | } | |
10705 | ||
10706 | static int | |
10707 | dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) | |
10708 | { | |
10709 | dtrace_action_t *action, *last; | |
10710 | dtrace_difo_t *dp = desc->dtad_difo; | |
10711 | uint32_t size = 0, align = sizeof (uint8_t), mask; | |
10712 | uint16_t format = 0; | |
10713 | dtrace_recdesc_t *rec; | |
10714 | dtrace_state_t *state = ecb->dte_state; | |
b0d623f7 | 10715 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 10716 | dtrace_optval_t *opt = state->dts_options, nframes, strsize; |
b0d623f7 A |
10717 | #else |
10718 | dtrace_optval_t *opt = state->dts_options; | |
10719 | dtrace_optval_t nframes=0, strsize; | |
10720 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10721 | uint64_t arg = desc->dtad_arg; |
10722 | ||
10723 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
10724 | ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); | |
10725 | ||
10726 | if (DTRACEACT_ISAGG(desc->dtad_kind)) { | |
10727 | /* | |
10728 | * If this is an aggregating action, there must be neither | |
10729 | * a speculate nor a commit on the action chain. | |
10730 | */ | |
10731 | dtrace_action_t *act; | |
10732 | ||
10733 | for (act = ecb->dte_action; act != NULL; act = act->dta_next) { | |
10734 | if (act->dta_kind == DTRACEACT_COMMIT) | |
10735 | return (EINVAL); | |
10736 | ||
10737 | if (act->dta_kind == DTRACEACT_SPECULATE) | |
10738 | return (EINVAL); | |
10739 | } | |
10740 | ||
10741 | action = dtrace_ecb_aggregation_create(ecb, desc); | |
10742 | ||
10743 | if (action == NULL) | |
10744 | return (EINVAL); | |
10745 | } else { | |
10746 | if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || | |
10747 | (desc->dtad_kind == DTRACEACT_DIFEXPR && | |
10748 | dp != NULL && dp->dtdo_destructive)) { | |
10749 | state->dts_destructive = 1; | |
10750 | } | |
10751 | ||
10752 | switch (desc->dtad_kind) { | |
10753 | case DTRACEACT_PRINTF: | |
10754 | case DTRACEACT_PRINTA: | |
10755 | case DTRACEACT_SYSTEM: | |
10756 | case DTRACEACT_FREOPEN: | |
10757 | /* | |
10758 | * We know that our arg is a string -- turn it into a | |
10759 | * format. | |
10760 | */ | |
10761 | if (arg == NULL) { | |
10762 | ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); | |
10763 | format = 0; | |
10764 | } else { | |
10765 | ASSERT(arg != NULL); | |
b0d623f7 | 10766 | ASSERT(arg > KERNELBASE); |
2d21ac55 A |
10767 | format = dtrace_format_add(state, |
10768 | (char *)(uintptr_t)arg); | |
10769 | } | |
10770 | ||
10771 | /*FALLTHROUGH*/ | |
10772 | case DTRACEACT_LIBACT: | |
10773 | case DTRACEACT_DIFEXPR: | |
b0d623f7 A |
10774 | #if defined(__APPLE__) |
10775 | case DTRACEACT_APPLEBINARY: | |
10776 | #endif /* __APPLE__ */ | |
2d21ac55 A |
10777 | if (dp == NULL) |
10778 | return (EINVAL); | |
10779 | ||
10780 | if ((size = dp->dtdo_rtype.dtdt_size) != 0) | |
10781 | break; | |
10782 | ||
10783 | if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { | |
10784 | if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) | |
10785 | return (EINVAL); | |
10786 | ||
10787 | size = opt[DTRACEOPT_STRSIZE]; | |
10788 | } | |
10789 | ||
10790 | break; | |
10791 | ||
10792 | case DTRACEACT_STACK: | |
10793 | if ((nframes = arg) == 0) { | |
10794 | nframes = opt[DTRACEOPT_STACKFRAMES]; | |
10795 | ASSERT(nframes > 0); | |
10796 | arg = nframes; | |
10797 | } | |
10798 | ||
10799 | size = nframes * sizeof (pc_t); | |
10800 | break; | |
10801 | ||
10802 | case DTRACEACT_JSTACK: | |
10803 | if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) | |
10804 | strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; | |
10805 | ||
10806 | if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) | |
10807 | nframes = opt[DTRACEOPT_JSTACKFRAMES]; | |
10808 | ||
10809 | arg = DTRACE_USTACK_ARG(nframes, strsize); | |
10810 | ||
10811 | /*FALLTHROUGH*/ | |
10812 | case DTRACEACT_USTACK: | |
10813 | if (desc->dtad_kind != DTRACEACT_JSTACK && | |
10814 | (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { | |
10815 | strsize = DTRACE_USTACK_STRSIZE(arg); | |
10816 | nframes = opt[DTRACEOPT_USTACKFRAMES]; | |
10817 | ASSERT(nframes > 0); | |
10818 | arg = DTRACE_USTACK_ARG(nframes, strsize); | |
10819 | } | |
10820 | ||
10821 | /* | |
10822 | * Save a slot for the pid. | |
10823 | */ | |
10824 | size = (nframes + 1) * sizeof (uint64_t); | |
10825 | size += DTRACE_USTACK_STRSIZE(arg); | |
10826 | size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); | |
10827 | ||
10828 | break; | |
10829 | ||
10830 | case DTRACEACT_SYM: | |
10831 | case DTRACEACT_MOD: | |
10832 | if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != | |
10833 | sizeof (uint64_t)) || | |
10834 | (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) | |
10835 | return (EINVAL); | |
10836 | break; | |
10837 | ||
10838 | case DTRACEACT_USYM: | |
10839 | case DTRACEACT_UMOD: | |
10840 | case DTRACEACT_UADDR: | |
10841 | if (dp == NULL || | |
10842 | (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || | |
10843 | (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) | |
10844 | return (EINVAL); | |
10845 | ||
10846 | /* | |
10847 | * We have a slot for the pid, plus a slot for the | |
10848 | * argument. To keep things simple (aligned with | |
10849 | * bitness-neutral sizing), we store each as a 64-bit | |
10850 | * quantity. | |
10851 | */ | |
10852 | size = 2 * sizeof (uint64_t); | |
10853 | break; | |
10854 | ||
10855 | case DTRACEACT_STOP: | |
10856 | case DTRACEACT_BREAKPOINT: | |
10857 | case DTRACEACT_PANIC: | |
10858 | break; | |
10859 | ||
10860 | case DTRACEACT_CHILL: | |
10861 | case DTRACEACT_DISCARD: | |
10862 | case DTRACEACT_RAISE: | |
10863 | if (dp == NULL) | |
10864 | return (EINVAL); | |
10865 | break; | |
10866 | ||
10867 | case DTRACEACT_EXIT: | |
10868 | if (dp == NULL || | |
10869 | (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || | |
10870 | (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) | |
10871 | return (EINVAL); | |
10872 | break; | |
10873 | ||
10874 | case DTRACEACT_SPECULATE: | |
10875 | if (ecb->dte_size > sizeof (dtrace_epid_t)) | |
10876 | return (EINVAL); | |
10877 | ||
10878 | if (dp == NULL) | |
10879 | return (EINVAL); | |
10880 | ||
10881 | state->dts_speculates = 1; | |
10882 | break; | |
10883 | ||
10884 | case DTRACEACT_COMMIT: { | |
10885 | dtrace_action_t *act = ecb->dte_action; | |
10886 | ||
10887 | for (; act != NULL; act = act->dta_next) { | |
10888 | if (act->dta_kind == DTRACEACT_COMMIT) | |
10889 | return (EINVAL); | |
10890 | } | |
10891 | ||
10892 | if (dp == NULL) | |
10893 | return (EINVAL); | |
10894 | break; | |
10895 | } | |
10896 | ||
10897 | default: | |
10898 | return (EINVAL); | |
10899 | } | |
10900 | ||
10901 | if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { | |
10902 | /* | |
10903 | * If this is a data-storing action or a speculate, | |
10904 | * we must be sure that there isn't a commit on the | |
10905 | * action chain. | |
10906 | */ | |
10907 | dtrace_action_t *act = ecb->dte_action; | |
10908 | ||
10909 | for (; act != NULL; act = act->dta_next) { | |
10910 | if (act->dta_kind == DTRACEACT_COMMIT) | |
10911 | return (EINVAL); | |
10912 | } | |
10913 | } | |
10914 | ||
10915 | action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); | |
10916 | action->dta_rec.dtrd_size = size; | |
10917 | } | |
10918 | ||
10919 | action->dta_refcnt = 1; | |
10920 | rec = &action->dta_rec; | |
10921 | size = rec->dtrd_size; | |
10922 | ||
10923 | for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { | |
10924 | if (!(size & mask)) { | |
10925 | align = mask + 1; | |
10926 | break; | |
10927 | } | |
10928 | } | |
10929 | ||
10930 | action->dta_kind = desc->dtad_kind; | |
10931 | ||
10932 | if ((action->dta_difo = dp) != NULL) | |
10933 | dtrace_difo_hold(dp); | |
10934 | ||
10935 | rec->dtrd_action = action->dta_kind; | |
10936 | rec->dtrd_arg = arg; | |
10937 | rec->dtrd_uarg = desc->dtad_uarg; | |
10938 | rec->dtrd_alignment = (uint16_t)align; | |
10939 | rec->dtrd_format = format; | |
10940 | ||
10941 | if ((last = ecb->dte_action_last) != NULL) { | |
10942 | ASSERT(ecb->dte_action != NULL); | |
10943 | action->dta_prev = last; | |
10944 | last->dta_next = action; | |
10945 | } else { | |
10946 | ASSERT(ecb->dte_action == NULL); | |
10947 | ecb->dte_action = action; | |
10948 | } | |
10949 | ||
10950 | ecb->dte_action_last = action; | |
10951 | ||
10952 | return (0); | |
10953 | } | |
10954 | ||
10955 | static void | |
10956 | dtrace_ecb_action_remove(dtrace_ecb_t *ecb) | |
10957 | { | |
10958 | dtrace_action_t *act = ecb->dte_action, *next; | |
10959 | dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; | |
10960 | dtrace_difo_t *dp; | |
10961 | uint16_t format; | |
10962 | ||
10963 | if (act != NULL && act->dta_refcnt > 1) { | |
10964 | ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); | |
10965 | act->dta_refcnt--; | |
10966 | } else { | |
10967 | for (; act != NULL; act = next) { | |
10968 | next = act->dta_next; | |
10969 | ASSERT(next != NULL || act == ecb->dte_action_last); | |
10970 | ASSERT(act->dta_refcnt == 1); | |
10971 | ||
10972 | if ((format = act->dta_rec.dtrd_format) != 0) | |
10973 | dtrace_format_remove(ecb->dte_state, format); | |
10974 | ||
10975 | if ((dp = act->dta_difo) != NULL) | |
10976 | dtrace_difo_release(dp, vstate); | |
10977 | ||
10978 | if (DTRACEACT_ISAGG(act->dta_kind)) { | |
10979 | dtrace_ecb_aggregation_destroy(ecb, act); | |
10980 | } else { | |
10981 | kmem_free(act, sizeof (dtrace_action_t)); | |
10982 | } | |
10983 | } | |
10984 | } | |
10985 | ||
10986 | ecb->dte_action = NULL; | |
10987 | ecb->dte_action_last = NULL; | |
10988 | ecb->dte_size = sizeof (dtrace_epid_t); | |
10989 | } | |
10990 | ||
10991 | static void | |
10992 | dtrace_ecb_disable(dtrace_ecb_t *ecb) | |
10993 | { | |
10994 | /* | |
10995 | * We disable the ECB by removing it from its probe. | |
10996 | */ | |
10997 | dtrace_ecb_t *pecb, *prev = NULL; | |
10998 | dtrace_probe_t *probe = ecb->dte_probe; | |
10999 | ||
11000 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11001 | ||
11002 | if (probe == NULL) { | |
11003 | /* | |
11004 | * This is the NULL probe; there is nothing to disable. | |
11005 | */ | |
11006 | return; | |
11007 | } | |
11008 | ||
11009 | for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { | |
11010 | if (pecb == ecb) | |
11011 | break; | |
11012 | prev = pecb; | |
11013 | } | |
11014 | ||
11015 | ASSERT(pecb != NULL); | |
11016 | ||
11017 | if (prev == NULL) { | |
11018 | probe->dtpr_ecb = ecb->dte_next; | |
11019 | } else { | |
11020 | prev->dte_next = ecb->dte_next; | |
11021 | } | |
11022 | ||
11023 | if (ecb == probe->dtpr_ecb_last) { | |
11024 | ASSERT(ecb->dte_next == NULL); | |
11025 | probe->dtpr_ecb_last = prev; | |
11026 | } | |
11027 | ||
11028 | /* | |
11029 | * The ECB has been disconnected from the probe; now sync to assure | |
11030 | * that all CPUs have seen the change before returning. | |
11031 | */ | |
11032 | dtrace_sync(); | |
11033 | ||
11034 | if (probe->dtpr_ecb == NULL) { | |
11035 | /* | |
11036 | * That was the last ECB on the probe; clear the predicate | |
11037 | * cache ID for the probe, disable it and sync one more time | |
11038 | * to assure that we'll never hit it again. | |
11039 | */ | |
11040 | dtrace_provider_t *prov = probe->dtpr_provider; | |
11041 | ||
11042 | ASSERT(ecb->dte_next == NULL); | |
11043 | ASSERT(probe->dtpr_ecb_last == NULL); | |
11044 | probe->dtpr_predcache = DTRACE_CACHEIDNONE; | |
11045 | prov->dtpv_pops.dtps_disable(prov->dtpv_arg, | |
11046 | probe->dtpr_id, probe->dtpr_arg); | |
11047 | dtrace_sync(); | |
11048 | } else { | |
11049 | /* | |
11050 | * There is at least one ECB remaining on the probe. If there | |
11051 | * is _exactly_ one, set the probe's predicate cache ID to be | |
11052 | * the predicate cache ID of the remaining ECB. | |
11053 | */ | |
11054 | ASSERT(probe->dtpr_ecb_last != NULL); | |
11055 | ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); | |
11056 | ||
11057 | if (probe->dtpr_ecb == probe->dtpr_ecb_last) { | |
11058 | dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; | |
11059 | ||
11060 | ASSERT(probe->dtpr_ecb->dte_next == NULL); | |
11061 | ||
11062 | if (p != NULL) | |
11063 | probe->dtpr_predcache = p->dtp_cacheid; | |
11064 | } | |
11065 | ||
11066 | ecb->dte_next = NULL; | |
11067 | } | |
11068 | } | |
11069 | ||
11070 | static void | |
11071 | dtrace_ecb_destroy(dtrace_ecb_t *ecb) | |
11072 | { | |
11073 | dtrace_state_t *state = ecb->dte_state; | |
11074 | dtrace_vstate_t *vstate = &state->dts_vstate; | |
11075 | dtrace_predicate_t *pred; | |
11076 | dtrace_epid_t epid = ecb->dte_epid; | |
11077 | ||
11078 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11079 | ASSERT(ecb->dte_next == NULL); | |
11080 | ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); | |
11081 | ||
11082 | if ((pred = ecb->dte_predicate) != NULL) | |
11083 | dtrace_predicate_release(pred, vstate); | |
11084 | ||
11085 | dtrace_ecb_action_remove(ecb); | |
11086 | ||
11087 | ASSERT(state->dts_ecbs[epid - 1] == ecb); | |
11088 | state->dts_ecbs[epid - 1] = NULL; | |
11089 | ||
11090 | kmem_free(ecb, sizeof (dtrace_ecb_t)); | |
11091 | } | |
11092 | ||
11093 | static dtrace_ecb_t * | |
11094 | dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, | |
11095 | dtrace_enabling_t *enab) | |
11096 | { | |
11097 | dtrace_ecb_t *ecb; | |
11098 | dtrace_predicate_t *pred; | |
11099 | dtrace_actdesc_t *act; | |
11100 | dtrace_provider_t *prov; | |
11101 | dtrace_ecbdesc_t *desc = enab->dten_current; | |
11102 | ||
11103 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11104 | ASSERT(state != NULL); | |
11105 | ||
11106 | ecb = dtrace_ecb_add(state, probe); | |
11107 | ecb->dte_uarg = desc->dted_uarg; | |
11108 | ||
11109 | if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { | |
11110 | dtrace_predicate_hold(pred); | |
11111 | ecb->dte_predicate = pred; | |
11112 | } | |
11113 | ||
11114 | if (probe != NULL) { | |
11115 | /* | |
11116 | * If the provider shows more leg than the consumer is old | |
11117 | * enough to see, we need to enable the appropriate implicit | |
11118 | * predicate bits to prevent the ecb from activating at | |
11119 | * revealing times. | |
11120 | * | |
11121 | * Providers specifying DTRACE_PRIV_USER at register time | |
11122 | * are stating that they need the /proc-style privilege | |
11123 | * model to be enforced, and this is what DTRACE_COND_OWNER | |
11124 | * and DTRACE_COND_ZONEOWNER will then do at probe time. | |
11125 | */ | |
11126 | prov = probe->dtpr_provider; | |
11127 | if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && | |
11128 | (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) | |
11129 | ecb->dte_cond |= DTRACE_COND_OWNER; | |
11130 | ||
11131 | if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && | |
11132 | (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) | |
11133 | ecb->dte_cond |= DTRACE_COND_ZONEOWNER; | |
11134 | ||
11135 | /* | |
11136 | * If the provider shows us kernel innards and the user | |
11137 | * is lacking sufficient privilege, enable the | |
11138 | * DTRACE_COND_USERMODE implicit predicate. | |
11139 | */ | |
11140 | if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && | |
11141 | (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) | |
11142 | ecb->dte_cond |= DTRACE_COND_USERMODE; | |
11143 | } | |
11144 | ||
11145 | if (dtrace_ecb_create_cache != NULL) { | |
11146 | /* | |
11147 | * If we have a cached ecb, we'll use its action list instead | |
11148 | * of creating our own (saving both time and space). | |
11149 | */ | |
11150 | dtrace_ecb_t *cached = dtrace_ecb_create_cache; | |
c910b4d9 | 11151 | dtrace_action_t *act_if = cached->dte_action; |
2d21ac55 | 11152 | |
c910b4d9 A |
11153 | if (act_if != NULL) { |
11154 | ASSERT(act_if->dta_refcnt > 0); | |
11155 | act_if->dta_refcnt++; | |
11156 | ecb->dte_action = act_if; | |
2d21ac55 A |
11157 | ecb->dte_action_last = cached->dte_action_last; |
11158 | ecb->dte_needed = cached->dte_needed; | |
11159 | ecb->dte_size = cached->dte_size; | |
11160 | ecb->dte_alignment = cached->dte_alignment; | |
11161 | } | |
11162 | ||
11163 | return (ecb); | |
11164 | } | |
11165 | ||
11166 | for (act = desc->dted_action; act != NULL; act = act->dtad_next) { | |
11167 | if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { | |
11168 | dtrace_ecb_destroy(ecb); | |
11169 | return (NULL); | |
11170 | } | |
11171 | } | |
11172 | ||
11173 | dtrace_ecb_resize(ecb); | |
11174 | ||
11175 | return (dtrace_ecb_create_cache = ecb); | |
11176 | } | |
11177 | ||
11178 | static int | |
11179 | dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) | |
11180 | { | |
11181 | dtrace_ecb_t *ecb; | |
11182 | dtrace_enabling_t *enab = arg; | |
11183 | dtrace_state_t *state = enab->dten_vstate->dtvs_state; | |
11184 | ||
11185 | ASSERT(state != NULL); | |
11186 | ||
11187 | if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { | |
11188 | /* | |
11189 | * This probe was created in a generation for which this | |
11190 | * enabling has previously created ECBs; we don't want to | |
11191 | * enable it again, so just kick out. | |
11192 | */ | |
11193 | return (DTRACE_MATCH_NEXT); | |
11194 | } | |
11195 | ||
11196 | if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) | |
11197 | return (DTRACE_MATCH_DONE); | |
11198 | ||
11199 | dtrace_ecb_enable(ecb); | |
11200 | return (DTRACE_MATCH_NEXT); | |
11201 | } | |
11202 | ||
11203 | static dtrace_ecb_t * | |
11204 | dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) | |
11205 | { | |
11206 | dtrace_ecb_t *ecb; | |
b0d623f7 | 11207 | #pragma unused(ecb) /* __APPLE__ */ |
2d21ac55 A |
11208 | |
11209 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11210 | ||
b0d623f7 | 11211 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 11212 | if (id == 0 || id > state->dts_necbs) |
b0d623f7 A |
11213 | #else |
11214 | if (id == 0 || id > (dtrace_epid_t)state->dts_necbs) | |
11215 | #endif /* __APPLE__ */ | |
2d21ac55 A |
11216 | return (NULL); |
11217 | ||
11218 | ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); | |
11219 | ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); | |
11220 | ||
11221 | return (state->dts_ecbs[id - 1]); | |
11222 | } | |
11223 | ||
11224 | static dtrace_aggregation_t * | |
11225 | dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) | |
11226 | { | |
11227 | dtrace_aggregation_t *agg; | |
b0d623f7 | 11228 | #pragma unused(agg) /* __APPLE__ */ |
2d21ac55 A |
11229 | |
11230 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11231 | ||
b0d623f7 | 11232 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 11233 | if (id == 0 || id > state->dts_naggregations) |
b0d623f7 A |
11234 | #else |
11235 | if (id == 0 || id > (dtrace_aggid_t)state->dts_naggregations) | |
11236 | #endif /* __APPLE__ */ | |
2d21ac55 A |
11237 | return (NULL); |
11238 | ||
11239 | ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); | |
11240 | ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || | |
11241 | agg->dtag_id == id); | |
11242 | ||
11243 | return (state->dts_aggregations[id - 1]); | |
11244 | } | |
11245 | ||
11246 | /* | |
11247 | * DTrace Buffer Functions | |
11248 | * | |
11249 | * The following functions manipulate DTrace buffers. Most of these functions | |
11250 | * are called in the context of establishing or processing consumer state; | |
11251 | * exceptions are explicitly noted. | |
11252 | */ | |
11253 | ||
11254 | /* | |
11255 | * Note: called from cross call context. This function switches the two | |
11256 | * buffers on a given CPU. The atomicity of this operation is assured by | |
11257 | * disabling interrupts while the actual switch takes place; the disabling of | |
11258 | * interrupts serializes the execution with any execution of dtrace_probe() on | |
11259 | * the same CPU. | |
11260 | */ | |
11261 | static void | |
11262 | dtrace_buffer_switch(dtrace_buffer_t *buf) | |
11263 | { | |
11264 | caddr_t tomax = buf->dtb_tomax; | |
11265 | caddr_t xamot = buf->dtb_xamot; | |
11266 | dtrace_icookie_t cookie; | |
11267 | ||
11268 | ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); | |
11269 | ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); | |
11270 | ||
11271 | cookie = dtrace_interrupt_disable(); | |
11272 | buf->dtb_tomax = xamot; | |
11273 | buf->dtb_xamot = tomax; | |
11274 | buf->dtb_xamot_drops = buf->dtb_drops; | |
11275 | buf->dtb_xamot_offset = buf->dtb_offset; | |
11276 | buf->dtb_xamot_errors = buf->dtb_errors; | |
11277 | buf->dtb_xamot_flags = buf->dtb_flags; | |
11278 | buf->dtb_offset = 0; | |
11279 | buf->dtb_drops = 0; | |
11280 | buf->dtb_errors = 0; | |
11281 | buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); | |
11282 | dtrace_interrupt_enable(cookie); | |
11283 | } | |
11284 | ||
11285 | /* | |
11286 | * Note: called from cross call context. This function activates a buffer | |
11287 | * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation | |
11288 | * is guaranteed by the disabling of interrupts. | |
11289 | */ | |
11290 | static void | |
11291 | dtrace_buffer_activate(dtrace_state_t *state) | |
11292 | { | |
11293 | dtrace_buffer_t *buf; | |
11294 | dtrace_icookie_t cookie = dtrace_interrupt_disable(); | |
11295 | ||
11296 | buf = &state->dts_buffer[CPU->cpu_id]; | |
11297 | ||
11298 | if (buf->dtb_tomax != NULL) { | |
11299 | /* | |
11300 | * We might like to assert that the buffer is marked inactive, | |
11301 | * but this isn't necessarily true: the buffer for the CPU | |
11302 | * that processes the BEGIN probe has its buffer activated | |
11303 | * manually. In this case, we take the (harmless) action | |
11304 | * re-clearing the bit INACTIVE bit. | |
11305 | */ | |
11306 | buf->dtb_flags &= ~DTRACEBUF_INACTIVE; | |
11307 | } | |
11308 | ||
11309 | dtrace_interrupt_enable(cookie); | |
11310 | } | |
11311 | ||
11312 | static int | |
11313 | dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, | |
11314 | processorid_t cpu) | |
11315 | { | |
11316 | cpu_t *cp; | |
11317 | dtrace_buffer_t *buf; | |
11318 | ||
11319 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
11320 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11321 | ||
b0d623f7 | 11322 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
11323 | if (size > dtrace_nonroot_maxsize && |
11324 | !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) | |
11325 | return (EFBIG); | |
b0d623f7 A |
11326 | #else |
11327 | if (size > (size_t)dtrace_nonroot_maxsize && | |
11328 | !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) | |
11329 | return (EFBIG); | |
11330 | #endif /* __APPLE__ */ | |
11331 | ||
2d21ac55 A |
11332 | |
11333 | #if defined(__APPLE__) | |
c910b4d9 | 11334 | if (size > (sane_size / 8) / (int)NCPU) /* As in kdbg_set_nkdbufs(), roughly. */ |
2d21ac55 A |
11335 | return (ENOMEM); |
11336 | #endif /* __APPLE__ */ | |
11337 | ||
11338 | cp = cpu_list; | |
11339 | ||
11340 | do { | |
11341 | if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) | |
11342 | continue; | |
11343 | ||
11344 | buf = &bufs[cp->cpu_id]; | |
11345 | ||
11346 | /* | |
11347 | * If there is already a buffer allocated for this CPU, it | |
11348 | * is only possible that this is a DR event. In this case, | |
11349 | * the buffer size must match our specified size. | |
11350 | */ | |
11351 | if (buf->dtb_tomax != NULL) { | |
11352 | ASSERT(buf->dtb_size == size); | |
11353 | continue; | |
11354 | } | |
11355 | ||
11356 | ASSERT(buf->dtb_xamot == NULL); | |
11357 | ||
11358 | if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) | |
11359 | goto err; | |
11360 | ||
11361 | buf->dtb_size = size; | |
11362 | buf->dtb_flags = flags; | |
11363 | buf->dtb_offset = 0; | |
11364 | buf->dtb_drops = 0; | |
11365 | ||
11366 | if (flags & DTRACEBUF_NOSWITCH) | |
11367 | continue; | |
11368 | ||
11369 | if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) | |
11370 | goto err; | |
11371 | } while ((cp = cp->cpu_next) != cpu_list); | |
11372 | ||
11373 | return (0); | |
11374 | ||
11375 | err: | |
11376 | cp = cpu_list; | |
11377 | ||
11378 | do { | |
11379 | if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) | |
11380 | continue; | |
11381 | ||
11382 | buf = &bufs[cp->cpu_id]; | |
11383 | ||
11384 | if (buf->dtb_xamot != NULL) { | |
11385 | ASSERT(buf->dtb_tomax != NULL); | |
11386 | ASSERT(buf->dtb_size == size); | |
11387 | kmem_free(buf->dtb_xamot, size); | |
11388 | } | |
11389 | ||
11390 | if (buf->dtb_tomax != NULL) { | |
11391 | ASSERT(buf->dtb_size == size); | |
11392 | kmem_free(buf->dtb_tomax, size); | |
11393 | } | |
11394 | ||
11395 | buf->dtb_tomax = NULL; | |
11396 | buf->dtb_xamot = NULL; | |
11397 | buf->dtb_size = 0; | |
11398 | } while ((cp = cp->cpu_next) != cpu_list); | |
11399 | ||
11400 | return (ENOMEM); | |
11401 | } | |
11402 | ||
11403 | /* | |
11404 | * Note: called from probe context. This function just increments the drop | |
11405 | * count on a buffer. It has been made a function to allow for the | |
11406 | * possibility of understanding the source of mysterious drop counts. (A | |
11407 | * problem for which one may be particularly disappointed that DTrace cannot | |
11408 | * be used to understand DTrace.) | |
11409 | */ | |
11410 | static void | |
11411 | dtrace_buffer_drop(dtrace_buffer_t *buf) | |
11412 | { | |
11413 | buf->dtb_drops++; | |
11414 | } | |
11415 | ||
11416 | /* | |
11417 | * Note: called from probe context. This function is called to reserve space | |
11418 | * in a buffer. If mstate is non-NULL, sets the scratch base and size in the | |
11419 | * mstate. Returns the new offset in the buffer, or a negative value if an | |
11420 | * error has occurred. | |
11421 | */ | |
11422 | static intptr_t | |
11423 | dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, | |
11424 | dtrace_state_t *state, dtrace_mstate_t *mstate) | |
11425 | { | |
11426 | intptr_t offs = buf->dtb_offset, soffs; | |
11427 | intptr_t woffs; | |
11428 | caddr_t tomax; | |
c910b4d9 | 11429 | size_t total_off; |
2d21ac55 A |
11430 | |
11431 | if (buf->dtb_flags & DTRACEBUF_INACTIVE) | |
11432 | return (-1); | |
11433 | ||
11434 | if ((tomax = buf->dtb_tomax) == NULL) { | |
11435 | dtrace_buffer_drop(buf); | |
11436 | return (-1); | |
11437 | } | |
11438 | ||
11439 | if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { | |
11440 | while (offs & (align - 1)) { | |
11441 | /* | |
11442 | * Assert that our alignment is off by a number which | |
11443 | * is itself sizeof (uint32_t) aligned. | |
11444 | */ | |
11445 | ASSERT(!((align - (offs & (align - 1))) & | |
11446 | (sizeof (uint32_t) - 1))); | |
11447 | DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); | |
11448 | offs += sizeof (uint32_t); | |
11449 | } | |
11450 | ||
b0d623f7 | 11451 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 11452 | if ((soffs = offs + needed) > buf->dtb_size) { |
b0d623f7 A |
11453 | #else |
11454 | if ((uint64_t)(soffs = offs + needed) > buf->dtb_size) { | |
11455 | #endif /* __APPLE__ */ | |
2d21ac55 A |
11456 | dtrace_buffer_drop(buf); |
11457 | return (-1); | |
11458 | } | |
11459 | ||
11460 | if (mstate == NULL) | |
11461 | return (offs); | |
11462 | ||
11463 | mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; | |
11464 | mstate->dtms_scratch_size = buf->dtb_size - soffs; | |
11465 | mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; | |
11466 | ||
11467 | return (offs); | |
11468 | } | |
11469 | ||
11470 | if (buf->dtb_flags & DTRACEBUF_FILL) { | |
11471 | if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && | |
11472 | (buf->dtb_flags & DTRACEBUF_FULL)) | |
11473 | return (-1); | |
11474 | goto out; | |
11475 | } | |
11476 | ||
c910b4d9 | 11477 | total_off = needed + (offs & (align - 1)); |
2d21ac55 A |
11478 | |
11479 | /* | |
11480 | * For a ring buffer, life is quite a bit more complicated. Before | |
11481 | * we can store any padding, we need to adjust our wrapping offset. | |
11482 | * (If we've never before wrapped or we're not about to, no adjustment | |
11483 | * is required.) | |
11484 | */ | |
11485 | if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || | |
c910b4d9 | 11486 | offs + total_off > buf->dtb_size) { |
2d21ac55 A |
11487 | woffs = buf->dtb_xamot_offset; |
11488 | ||
c910b4d9 | 11489 | if (offs + total_off > buf->dtb_size) { |
2d21ac55 A |
11490 | /* |
11491 | * We can't fit in the end of the buffer. First, a | |
11492 | * sanity check that we can fit in the buffer at all. | |
11493 | */ | |
c910b4d9 | 11494 | if (total_off > buf->dtb_size) { |
2d21ac55 A |
11495 | dtrace_buffer_drop(buf); |
11496 | return (-1); | |
11497 | } | |
11498 | ||
11499 | /* | |
11500 | * We're going to be storing at the top of the buffer, | |
11501 | * so now we need to deal with the wrapped offset. We | |
11502 | * only reset our wrapped offset to 0 if it is | |
11503 | * currently greater than the current offset. If it | |
11504 | * is less than the current offset, it is because a | |
11505 | * previous allocation induced a wrap -- but the | |
11506 | * allocation didn't subsequently take the space due | |
11507 | * to an error or false predicate evaluation. In this | |
11508 | * case, we'll just leave the wrapped offset alone: if | |
11509 | * the wrapped offset hasn't been advanced far enough | |
11510 | * for this allocation, it will be adjusted in the | |
11511 | * lower loop. | |
11512 | */ | |
11513 | if (buf->dtb_flags & DTRACEBUF_WRAPPED) { | |
11514 | if (woffs >= offs) | |
11515 | woffs = 0; | |
11516 | } else { | |
11517 | woffs = 0; | |
11518 | } | |
11519 | ||
11520 | /* | |
11521 | * Now we know that we're going to be storing to the | |
11522 | * top of the buffer and that there is room for us | |
11523 | * there. We need to clear the buffer from the current | |
11524 | * offset to the end (there may be old gunk there). | |
11525 | */ | |
b0d623f7 | 11526 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 11527 | while (offs < buf->dtb_size) |
b0d623f7 A |
11528 | #else |
11529 | while ((uint64_t)offs < buf->dtb_size) | |
11530 | #endif /* __APPLE__ */ | |
2d21ac55 A |
11531 | tomax[offs++] = 0; |
11532 | ||
11533 | /* | |
11534 | * We need to set our offset to zero. And because we | |
11535 | * are wrapping, we need to set the bit indicating as | |
11536 | * much. We can also adjust our needed space back | |
11537 | * down to the space required by the ECB -- we know | |
11538 | * that the top of the buffer is aligned. | |
11539 | */ | |
11540 | offs = 0; | |
c910b4d9 | 11541 | total_off = needed; |
2d21ac55 A |
11542 | buf->dtb_flags |= DTRACEBUF_WRAPPED; |
11543 | } else { | |
11544 | /* | |
11545 | * There is room for us in the buffer, so we simply | |
11546 | * need to check the wrapped offset. | |
11547 | */ | |
11548 | if (woffs < offs) { | |
11549 | /* | |
11550 | * The wrapped offset is less than the offset. | |
11551 | * This can happen if we allocated buffer space | |
11552 | * that induced a wrap, but then we didn't | |
11553 | * subsequently take the space due to an error | |
11554 | * or false predicate evaluation. This is | |
11555 | * okay; we know that _this_ allocation isn't | |
11556 | * going to induce a wrap. We still can't | |
11557 | * reset the wrapped offset to be zero, | |
11558 | * however: the space may have been trashed in | |
11559 | * the previous failed probe attempt. But at | |
11560 | * least the wrapped offset doesn't need to | |
11561 | * be adjusted at all... | |
11562 | */ | |
11563 | goto out; | |
11564 | } | |
11565 | } | |
11566 | ||
b0d623f7 | 11567 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
c910b4d9 | 11568 | while (offs + total_off > woffs) { |
b0d623f7 A |
11569 | #else |
11570 | while (offs + total_off > (size_t)woffs) { | |
11571 | #endif /* __APPLE__ */ | |
2d21ac55 A |
11572 | dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); |
11573 | size_t size; | |
11574 | ||
11575 | if (epid == DTRACE_EPIDNONE) { | |
11576 | size = sizeof (uint32_t); | |
11577 | } else { | |
b0d623f7 | 11578 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 11579 | ASSERT(epid <= state->dts_necbs); |
b0d623f7 A |
11580 | #else |
11581 | ASSERT(epid <= (dtrace_epid_t)state->dts_necbs); | |
11582 | #endif /* __APPLE__ */ | |
2d21ac55 A |
11583 | ASSERT(state->dts_ecbs[epid - 1] != NULL); |
11584 | ||
11585 | size = state->dts_ecbs[epid - 1]->dte_size; | |
11586 | } | |
11587 | ||
11588 | ASSERT(woffs + size <= buf->dtb_size); | |
11589 | ASSERT(size != 0); | |
11590 | ||
11591 | if (woffs + size == buf->dtb_size) { | |
11592 | /* | |
11593 | * We've reached the end of the buffer; we want | |
11594 | * to set the wrapped offset to 0 and break | |
11595 | * out. However, if the offs is 0, then we're | |
11596 | * in a strange edge-condition: the amount of | |
11597 | * space that we want to reserve plus the size | |
11598 | * of the record that we're overwriting is | |
11599 | * greater than the size of the buffer. This | |
11600 | * is problematic because if we reserve the | |
11601 | * space but subsequently don't consume it (due | |
11602 | * to a failed predicate or error) the wrapped | |
11603 | * offset will be 0 -- yet the EPID at offset 0 | |
11604 | * will not be committed. This situation is | |
11605 | * relatively easy to deal with: if we're in | |
11606 | * this case, the buffer is indistinguishable | |
11607 | * from one that hasn't wrapped; we need only | |
11608 | * finish the job by clearing the wrapped bit, | |
11609 | * explicitly setting the offset to be 0, and | |
11610 | * zero'ing out the old data in the buffer. | |
11611 | */ | |
11612 | if (offs == 0) { | |
11613 | buf->dtb_flags &= ~DTRACEBUF_WRAPPED; | |
11614 | buf->dtb_offset = 0; | |
c910b4d9 | 11615 | woffs = total_off; |
2d21ac55 | 11616 | |
b0d623f7 | 11617 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 11618 | while (woffs < buf->dtb_size) |
b0d623f7 A |
11619 | #else |
11620 | while ((uint64_t)woffs < buf->dtb_size) | |
11621 | #endif /* __APPLE__ */ | |
11622 | ||
2d21ac55 A |
11623 | tomax[woffs++] = 0; |
11624 | } | |
11625 | ||
11626 | woffs = 0; | |
11627 | break; | |
11628 | } | |
11629 | ||
11630 | woffs += size; | |
11631 | } | |
11632 | ||
11633 | /* | |
11634 | * We have a wrapped offset. It may be that the wrapped offset | |
11635 | * has become zero -- that's okay. | |
11636 | */ | |
11637 | buf->dtb_xamot_offset = woffs; | |
11638 | } | |
11639 | ||
11640 | out: | |
11641 | /* | |
11642 | * Now we can plow the buffer with any necessary padding. | |
11643 | */ | |
11644 | while (offs & (align - 1)) { | |
11645 | /* | |
11646 | * Assert that our alignment is off by a number which | |
11647 | * is itself sizeof (uint32_t) aligned. | |
11648 | */ | |
11649 | ASSERT(!((align - (offs & (align - 1))) & | |
11650 | (sizeof (uint32_t) - 1))); | |
11651 | DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); | |
11652 | offs += sizeof (uint32_t); | |
11653 | } | |
11654 | ||
11655 | if (buf->dtb_flags & DTRACEBUF_FILL) { | |
11656 | if (offs + needed > buf->dtb_size - state->dts_reserve) { | |
11657 | buf->dtb_flags |= DTRACEBUF_FULL; | |
11658 | return (-1); | |
11659 | } | |
11660 | } | |
11661 | ||
11662 | if (mstate == NULL) | |
11663 | return (offs); | |
11664 | ||
11665 | /* | |
11666 | * For ring buffers and fill buffers, the scratch space is always | |
11667 | * the inactive buffer. | |
11668 | */ | |
11669 | mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; | |
11670 | mstate->dtms_scratch_size = buf->dtb_size; | |
11671 | mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; | |
11672 | ||
11673 | return (offs); | |
11674 | } | |
11675 | ||
11676 | static void | |
11677 | dtrace_buffer_polish(dtrace_buffer_t *buf) | |
11678 | { | |
11679 | ASSERT(buf->dtb_flags & DTRACEBUF_RING); | |
11680 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11681 | ||
11682 | if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) | |
11683 | return; | |
11684 | ||
11685 | /* | |
11686 | * We need to polish the ring buffer. There are three cases: | |
11687 | * | |
11688 | * - The first (and presumably most common) is that there is no gap | |
11689 | * between the buffer offset and the wrapped offset. In this case, | |
11690 | * there is nothing in the buffer that isn't valid data; we can | |
11691 | * mark the buffer as polished and return. | |
11692 | * | |
11693 | * - The second (less common than the first but still more common | |
11694 | * than the third) is that there is a gap between the buffer offset | |
11695 | * and the wrapped offset, and the wrapped offset is larger than the | |
11696 | * buffer offset. This can happen because of an alignment issue, or | |
11697 | * can happen because of a call to dtrace_buffer_reserve() that | |
11698 | * didn't subsequently consume the buffer space. In this case, | |
11699 | * we need to zero the data from the buffer offset to the wrapped | |
11700 | * offset. | |
11701 | * | |
11702 | * - The third (and least common) is that there is a gap between the | |
11703 | * buffer offset and the wrapped offset, but the wrapped offset is | |
11704 | * _less_ than the buffer offset. This can only happen because a | |
11705 | * call to dtrace_buffer_reserve() induced a wrap, but the space | |
11706 | * was not subsequently consumed. In this case, we need to zero the | |
11707 | * space from the offset to the end of the buffer _and_ from the | |
11708 | * top of the buffer to the wrapped offset. | |
11709 | */ | |
11710 | if (buf->dtb_offset < buf->dtb_xamot_offset) { | |
11711 | bzero(buf->dtb_tomax + buf->dtb_offset, | |
11712 | buf->dtb_xamot_offset - buf->dtb_offset); | |
11713 | } | |
11714 | ||
11715 | if (buf->dtb_offset > buf->dtb_xamot_offset) { | |
11716 | bzero(buf->dtb_tomax + buf->dtb_offset, | |
11717 | buf->dtb_size - buf->dtb_offset); | |
11718 | bzero(buf->dtb_tomax, buf->dtb_xamot_offset); | |
11719 | } | |
11720 | } | |
11721 | ||
11722 | static void | |
11723 | dtrace_buffer_free(dtrace_buffer_t *bufs) | |
11724 | { | |
11725 | int i; | |
11726 | ||
c910b4d9 | 11727 | for (i = 0; i < (int)NCPU; i++) { |
2d21ac55 A |
11728 | dtrace_buffer_t *buf = &bufs[i]; |
11729 | ||
11730 | if (buf->dtb_tomax == NULL) { | |
11731 | ASSERT(buf->dtb_xamot == NULL); | |
11732 | ASSERT(buf->dtb_size == 0); | |
11733 | continue; | |
11734 | } | |
11735 | ||
11736 | if (buf->dtb_xamot != NULL) { | |
11737 | ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); | |
11738 | kmem_free(buf->dtb_xamot, buf->dtb_size); | |
11739 | } | |
11740 | ||
11741 | kmem_free(buf->dtb_tomax, buf->dtb_size); | |
11742 | buf->dtb_size = 0; | |
11743 | buf->dtb_tomax = NULL; | |
11744 | buf->dtb_xamot = NULL; | |
11745 | } | |
11746 | } | |
11747 | ||
11748 | /* | |
11749 | * DTrace Enabling Functions | |
11750 | */ | |
11751 | static dtrace_enabling_t * | |
11752 | dtrace_enabling_create(dtrace_vstate_t *vstate) | |
11753 | { | |
11754 | dtrace_enabling_t *enab; | |
11755 | ||
11756 | enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); | |
11757 | enab->dten_vstate = vstate; | |
11758 | ||
11759 | return (enab); | |
11760 | } | |
11761 | ||
11762 | static void | |
11763 | dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) | |
11764 | { | |
11765 | dtrace_ecbdesc_t **ndesc; | |
11766 | size_t osize, nsize; | |
11767 | ||
11768 | /* | |
11769 | * We can't add to enablings after we've enabled them, or after we've | |
11770 | * retained them. | |
11771 | */ | |
11772 | ASSERT(enab->dten_probegen == 0); | |
11773 | ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); | |
11774 | ||
11775 | #if defined(__APPLE__) | |
b0d623f7 | 11776 | if (ecb == NULL) return; /* Note: protection against gcc 4.0 botch on x86 */ |
2d21ac55 A |
11777 | #endif /* __APPLE__ */ |
11778 | ||
11779 | if (enab->dten_ndesc < enab->dten_maxdesc) { | |
11780 | enab->dten_desc[enab->dten_ndesc++] = ecb; | |
11781 | return; | |
11782 | } | |
11783 | ||
11784 | osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); | |
11785 | ||
11786 | if (enab->dten_maxdesc == 0) { | |
11787 | enab->dten_maxdesc = 1; | |
11788 | } else { | |
11789 | enab->dten_maxdesc <<= 1; | |
11790 | } | |
11791 | ||
11792 | ASSERT(enab->dten_ndesc < enab->dten_maxdesc); | |
11793 | ||
11794 | nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); | |
11795 | ndesc = kmem_zalloc(nsize, KM_SLEEP); | |
11796 | bcopy(enab->dten_desc, ndesc, osize); | |
11797 | kmem_free(enab->dten_desc, osize); | |
11798 | ||
11799 | enab->dten_desc = ndesc; | |
11800 | enab->dten_desc[enab->dten_ndesc++] = ecb; | |
11801 | } | |
11802 | ||
11803 | static void | |
11804 | dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, | |
11805 | dtrace_probedesc_t *pd) | |
11806 | { | |
11807 | dtrace_ecbdesc_t *new; | |
11808 | dtrace_predicate_t *pred; | |
11809 | dtrace_actdesc_t *act; | |
11810 | ||
11811 | /* | |
11812 | * We're going to create a new ECB description that matches the | |
11813 | * specified ECB in every way, but has the specified probe description. | |
11814 | */ | |
11815 | new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); | |
11816 | ||
11817 | if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) | |
11818 | dtrace_predicate_hold(pred); | |
11819 | ||
11820 | for (act = ecb->dted_action; act != NULL; act = act->dtad_next) | |
11821 | dtrace_actdesc_hold(act); | |
11822 | ||
11823 | new->dted_action = ecb->dted_action; | |
11824 | new->dted_pred = ecb->dted_pred; | |
11825 | new->dted_probe = *pd; | |
11826 | new->dted_uarg = ecb->dted_uarg; | |
11827 | ||
11828 | dtrace_enabling_add(enab, new); | |
11829 | } | |
11830 | ||
11831 | static void | |
11832 | dtrace_enabling_dump(dtrace_enabling_t *enab) | |
11833 | { | |
11834 | int i; | |
11835 | ||
11836 | for (i = 0; i < enab->dten_ndesc; i++) { | |
11837 | dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; | |
11838 | ||
11839 | cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, | |
11840 | desc->dtpd_provider, desc->dtpd_mod, | |
11841 | desc->dtpd_func, desc->dtpd_name); | |
11842 | } | |
11843 | } | |
11844 | ||
11845 | static void | |
11846 | dtrace_enabling_destroy(dtrace_enabling_t *enab) | |
11847 | { | |
11848 | int i; | |
11849 | dtrace_ecbdesc_t *ep; | |
11850 | dtrace_vstate_t *vstate = enab->dten_vstate; | |
11851 | ||
11852 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11853 | ||
11854 | for (i = 0; i < enab->dten_ndesc; i++) { | |
11855 | dtrace_actdesc_t *act, *next; | |
11856 | dtrace_predicate_t *pred; | |
11857 | ||
11858 | ep = enab->dten_desc[i]; | |
11859 | ||
11860 | if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) | |
11861 | dtrace_predicate_release(pred, vstate); | |
11862 | ||
11863 | for (act = ep->dted_action; act != NULL; act = next) { | |
11864 | next = act->dtad_next; | |
11865 | dtrace_actdesc_release(act, vstate); | |
11866 | } | |
11867 | ||
11868 | kmem_free(ep, sizeof (dtrace_ecbdesc_t)); | |
11869 | } | |
11870 | ||
11871 | kmem_free(enab->dten_desc, | |
11872 | enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); | |
11873 | ||
11874 | /* | |
11875 | * If this was a retained enabling, decrement the dts_nretained count | |
11876 | * and take it off of the dtrace_retained list. | |
11877 | */ | |
11878 | if (enab->dten_prev != NULL || enab->dten_next != NULL || | |
11879 | dtrace_retained == enab) { | |
11880 | ASSERT(enab->dten_vstate->dtvs_state != NULL); | |
11881 | ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); | |
11882 | enab->dten_vstate->dtvs_state->dts_nretained--; | |
b0d623f7 | 11883 | dtrace_retained_gen++; |
2d21ac55 A |
11884 | } |
11885 | ||
11886 | if (enab->dten_prev == NULL) { | |
11887 | if (dtrace_retained == enab) { | |
11888 | dtrace_retained = enab->dten_next; | |
11889 | ||
11890 | if (dtrace_retained != NULL) | |
11891 | dtrace_retained->dten_prev = NULL; | |
11892 | } | |
11893 | } else { | |
11894 | ASSERT(enab != dtrace_retained); | |
11895 | ASSERT(dtrace_retained != NULL); | |
11896 | enab->dten_prev->dten_next = enab->dten_next; | |
11897 | } | |
11898 | ||
11899 | if (enab->dten_next != NULL) { | |
11900 | ASSERT(dtrace_retained != NULL); | |
11901 | enab->dten_next->dten_prev = enab->dten_prev; | |
11902 | } | |
11903 | ||
11904 | kmem_free(enab, sizeof (dtrace_enabling_t)); | |
11905 | } | |
11906 | ||
11907 | static int | |
11908 | dtrace_enabling_retain(dtrace_enabling_t *enab) | |
11909 | { | |
11910 | dtrace_state_t *state; | |
11911 | ||
11912 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11913 | ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); | |
11914 | ASSERT(enab->dten_vstate != NULL); | |
11915 | ||
11916 | state = enab->dten_vstate->dtvs_state; | |
11917 | ASSERT(state != NULL); | |
11918 | ||
11919 | /* | |
11920 | * We only allow each state to retain dtrace_retain_max enablings. | |
11921 | */ | |
11922 | if (state->dts_nretained >= dtrace_retain_max) | |
11923 | return (ENOSPC); | |
11924 | ||
11925 | state->dts_nretained++; | |
b0d623f7 | 11926 | dtrace_retained_gen++; |
2d21ac55 A |
11927 | |
11928 | if (dtrace_retained == NULL) { | |
11929 | dtrace_retained = enab; | |
11930 | return (0); | |
11931 | } | |
11932 | ||
11933 | enab->dten_next = dtrace_retained; | |
11934 | dtrace_retained->dten_prev = enab; | |
11935 | dtrace_retained = enab; | |
11936 | ||
11937 | return (0); | |
11938 | } | |
11939 | ||
11940 | static int | |
11941 | dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, | |
11942 | dtrace_probedesc_t *create) | |
11943 | { | |
11944 | dtrace_enabling_t *new, *enab; | |
11945 | int found = 0, err = ENOENT; | |
11946 | ||
11947 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
11948 | ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); | |
11949 | ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); | |
11950 | ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); | |
11951 | ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); | |
11952 | ||
11953 | new = dtrace_enabling_create(&state->dts_vstate); | |
11954 | ||
11955 | /* | |
11956 | * Iterate over all retained enablings, looking for enablings that | |
11957 | * match the specified state. | |
11958 | */ | |
11959 | for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { | |
11960 | int i; | |
11961 | ||
11962 | /* | |
11963 | * dtvs_state can only be NULL for helper enablings -- and | |
11964 | * helper enablings can't be retained. | |
11965 | */ | |
11966 | ASSERT(enab->dten_vstate->dtvs_state != NULL); | |
11967 | ||
11968 | if (enab->dten_vstate->dtvs_state != state) | |
11969 | continue; | |
11970 | ||
11971 | /* | |
11972 | * Now iterate over each probe description; we're looking for | |
11973 | * an exact match to the specified probe description. | |
11974 | */ | |
11975 | for (i = 0; i < enab->dten_ndesc; i++) { | |
11976 | dtrace_ecbdesc_t *ep = enab->dten_desc[i]; | |
11977 | dtrace_probedesc_t *pd = &ep->dted_probe; | |
11978 | ||
b0d623f7 | 11979 | #if !defined(__APPLE__) |
2d21ac55 A |
11980 | if (strcmp(pd->dtpd_provider, match->dtpd_provider)) |
11981 | continue; | |
11982 | ||
11983 | if (strcmp(pd->dtpd_mod, match->dtpd_mod)) | |
11984 | continue; | |
11985 | ||
11986 | if (strcmp(pd->dtpd_func, match->dtpd_func)) | |
11987 | continue; | |
11988 | ||
11989 | if (strcmp(pd->dtpd_name, match->dtpd_name)) | |
11990 | continue; | |
b0d623f7 A |
11991 | #else /* Employ size bounded string operation. */ |
11992 | if (strncmp(pd->dtpd_provider, match->dtpd_provider, DTRACE_PROVNAMELEN)) | |
11993 | continue; | |
11994 | ||
11995 | if (strncmp(pd->dtpd_mod, match->dtpd_mod, DTRACE_MODNAMELEN)) | |
11996 | continue; | |
11997 | ||
11998 | if (strncmp(pd->dtpd_func, match->dtpd_func, DTRACE_FUNCNAMELEN)) | |
11999 | continue; | |
12000 | ||
12001 | if (strncmp(pd->dtpd_name, match->dtpd_name, DTRACE_NAMELEN)) | |
12002 | continue; | |
12003 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12004 | |
12005 | /* | |
12006 | * We have a winning probe! Add it to our growing | |
12007 | * enabling. | |
12008 | */ | |
12009 | found = 1; | |
12010 | dtrace_enabling_addlike(new, ep, create); | |
12011 | } | |
12012 | } | |
12013 | ||
12014 | if (!found || (err = dtrace_enabling_retain(new)) != 0) { | |
12015 | dtrace_enabling_destroy(new); | |
12016 | return (err); | |
12017 | } | |
12018 | ||
12019 | return (0); | |
12020 | } | |
12021 | ||
12022 | static void | |
12023 | dtrace_enabling_retract(dtrace_state_t *state) | |
12024 | { | |
12025 | dtrace_enabling_t *enab, *next; | |
12026 | ||
12027 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
12028 | ||
12029 | /* | |
12030 | * Iterate over all retained enablings, destroy the enablings retained | |
12031 | * for the specified state. | |
12032 | */ | |
12033 | for (enab = dtrace_retained; enab != NULL; enab = next) { | |
12034 | next = enab->dten_next; | |
12035 | ||
12036 | /* | |
12037 | * dtvs_state can only be NULL for helper enablings -- and | |
12038 | * helper enablings can't be retained. | |
12039 | */ | |
12040 | ASSERT(enab->dten_vstate->dtvs_state != NULL); | |
12041 | ||
12042 | if (enab->dten_vstate->dtvs_state == state) { | |
12043 | ASSERT(state->dts_nretained > 0); | |
12044 | dtrace_enabling_destroy(enab); | |
12045 | } | |
12046 | } | |
12047 | ||
12048 | ASSERT(state->dts_nretained == 0); | |
12049 | } | |
12050 | ||
12051 | static int | |
12052 | dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) | |
12053 | { | |
12054 | int i = 0; | |
12055 | int matched = 0; | |
12056 | ||
12057 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
12058 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
12059 | ||
12060 | for (i = 0; i < enab->dten_ndesc; i++) { | |
12061 | dtrace_ecbdesc_t *ep = enab->dten_desc[i]; | |
12062 | ||
12063 | enab->dten_current = ep; | |
12064 | enab->dten_error = 0; | |
12065 | ||
12066 | matched += dtrace_probe_enable(&ep->dted_probe, enab); | |
12067 | ||
12068 | if (enab->dten_error != 0) { | |
12069 | /* | |
12070 | * If we get an error half-way through enabling the | |
12071 | * probes, we kick out -- perhaps with some number of | |
12072 | * them enabled. Leaving enabled probes enabled may | |
12073 | * be slightly confusing for user-level, but we expect | |
12074 | * that no one will attempt to actually drive on in | |
12075 | * the face of such errors. If this is an anonymous | |
12076 | * enabling (indicated with a NULL nmatched pointer), | |
12077 | * we cmn_err() a message. We aren't expecting to | |
12078 | * get such an error -- such as it can exist at all, | |
12079 | * it would be a result of corrupted DOF in the driver | |
12080 | * properties. | |
12081 | */ | |
12082 | if (nmatched == NULL) { | |
12083 | cmn_err(CE_WARN, "dtrace_enabling_match() " | |
12084 | "error on %p: %d", (void *)ep, | |
12085 | enab->dten_error); | |
12086 | } | |
12087 | ||
12088 | return (enab->dten_error); | |
12089 | } | |
12090 | } | |
12091 | ||
12092 | enab->dten_probegen = dtrace_probegen; | |
12093 | if (nmatched != NULL) | |
12094 | *nmatched = matched; | |
12095 | ||
12096 | return (0); | |
12097 | } | |
12098 | ||
12099 | static void | |
12100 | dtrace_enabling_matchall(void) | |
12101 | { | |
12102 | dtrace_enabling_t *enab; | |
12103 | ||
12104 | lck_mtx_lock(&cpu_lock); | |
12105 | lck_mtx_lock(&dtrace_lock); | |
12106 | ||
12107 | /* | |
b0d623f7 A |
12108 | * Iterate over all retained enablings to see if any probes match |
12109 | * against them. We only perform this operation on enablings for which | |
12110 | * we have sufficient permissions by virtue of being in the global zone | |
12111 | * or in the same zone as the DTrace client. Because we can be called | |
12112 | * after dtrace_detach() has been called, we cannot assert that there | |
12113 | * are retained enablings. We can safely load from dtrace_retained, | |
12114 | * however: the taskq_destroy() at the end of dtrace_detach() will | |
12115 | * block pending our completion. | |
2d21ac55 | 12116 | */ |
2d21ac55 | 12117 | for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { |
b0d623f7 A |
12118 | #if !defined(__APPLE__) |
12119 | cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; | |
2d21ac55 | 12120 | |
b0d623f7 A |
12121 | if (INGLOBALZONE(curproc) || |
12122 | cr != NULL && getzoneid() == crgetzoneid(cr)) | |
12123 | (void) dtrace_enabling_match(enab, NULL); | |
12124 | #else | |
12125 | (void) dtrace_enabling_match(enab, NULL); /* As if always in "global" zone." */ | |
12126 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12127 | } |
12128 | ||
b0d623f7 A |
12129 | lck_mtx_unlock(&dtrace_lock); |
12130 | lck_mtx_unlock(&cpu_lock); | |
2d21ac55 A |
12131 | } |
12132 | ||
12133 | /* | |
12134 | * If an enabling is to be enabled without having matched probes (that is, if | |
12135 | * dtrace_state_go() is to be called on the underlying dtrace_state_t), the | |
12136 | * enabling must be _primed_ by creating an ECB for every ECB description. | |
12137 | * This must be done to assure that we know the number of speculations, the | |
12138 | * number of aggregations, the minimum buffer size needed, etc. before we | |
12139 | * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually | |
12140 | * enabling any probes, we create ECBs for every ECB decription, but with a | |
12141 | * NULL probe -- which is exactly what this function does. | |
12142 | */ | |
12143 | static void | |
12144 | dtrace_enabling_prime(dtrace_state_t *state) | |
12145 | { | |
12146 | dtrace_enabling_t *enab; | |
12147 | int i; | |
12148 | ||
12149 | for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { | |
12150 | ASSERT(enab->dten_vstate->dtvs_state != NULL); | |
12151 | ||
12152 | if (enab->dten_vstate->dtvs_state != state) | |
12153 | continue; | |
12154 | ||
12155 | /* | |
12156 | * We don't want to prime an enabling more than once, lest | |
12157 | * we allow a malicious user to induce resource exhaustion. | |
12158 | * (The ECBs that result from priming an enabling aren't | |
12159 | * leaked -- but they also aren't deallocated until the | |
12160 | * consumer state is destroyed.) | |
12161 | */ | |
12162 | if (enab->dten_primed) | |
12163 | continue; | |
12164 | ||
12165 | for (i = 0; i < enab->dten_ndesc; i++) { | |
12166 | enab->dten_current = enab->dten_desc[i]; | |
12167 | (void) dtrace_probe_enable(NULL, enab); | |
12168 | } | |
12169 | ||
12170 | enab->dten_primed = 1; | |
12171 | } | |
12172 | } | |
12173 | ||
12174 | /* | |
12175 | * Called to indicate that probes should be provided due to retained | |
12176 | * enablings. This is implemented in terms of dtrace_probe_provide(), but it | |
12177 | * must take an initial lap through the enabling calling the dtps_provide() | |
12178 | * entry point explicitly to allow for autocreated probes. | |
12179 | */ | |
12180 | static void | |
12181 | dtrace_enabling_provide(dtrace_provider_t *prv) | |
12182 | { | |
12183 | int i, all = 0; | |
12184 | dtrace_probedesc_t desc; | |
b0d623f7 | 12185 | dtrace_genid_t gen; |
2d21ac55 A |
12186 | |
12187 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
12188 | lck_mtx_assert(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED); | |
12189 | ||
12190 | if (prv == NULL) { | |
12191 | all = 1; | |
12192 | prv = dtrace_provider; | |
12193 | } | |
12194 | ||
12195 | do { | |
b0d623f7 | 12196 | dtrace_enabling_t *enab; |
2d21ac55 A |
12197 | void *parg = prv->dtpv_arg; |
12198 | ||
b0d623f7 A |
12199 | retry: |
12200 | gen = dtrace_retained_gen; | |
12201 | for (enab = dtrace_retained; enab != NULL; | |
12202 | enab = enab->dten_next) { | |
2d21ac55 A |
12203 | for (i = 0; i < enab->dten_ndesc; i++) { |
12204 | desc = enab->dten_desc[i]->dted_probe; | |
12205 | lck_mtx_unlock(&dtrace_lock); | |
12206 | prv->dtpv_pops.dtps_provide(parg, &desc); | |
12207 | lck_mtx_lock(&dtrace_lock); | |
b0d623f7 A |
12208 | /* |
12209 | * Process the retained enablings again if | |
12210 | * they have changed while we weren't holding | |
12211 | * dtrace_lock. | |
12212 | */ | |
12213 | if (gen != dtrace_retained_gen) | |
12214 | goto retry; | |
2d21ac55 A |
12215 | } |
12216 | } | |
12217 | } while (all && (prv = prv->dtpv_next) != NULL); | |
12218 | ||
12219 | lck_mtx_unlock(&dtrace_lock); | |
12220 | dtrace_probe_provide(NULL, all ? NULL : prv); | |
12221 | lck_mtx_lock(&dtrace_lock); | |
12222 | } | |
12223 | ||
12224 | /* | |
12225 | * DTrace DOF Functions | |
12226 | */ | |
12227 | /*ARGSUSED*/ | |
12228 | static void | |
12229 | dtrace_dof_error(dof_hdr_t *dof, const char *str) | |
12230 | { | |
b0d623f7 | 12231 | #pragma unused(dof) /* __APPLE__ */ |
2d21ac55 A |
12232 | if (dtrace_err_verbose) |
12233 | cmn_err(CE_WARN, "failed to process DOF: %s", str); | |
12234 | ||
12235 | #ifdef DTRACE_ERRDEBUG | |
12236 | dtrace_errdebug(str); | |
12237 | #endif | |
12238 | } | |
12239 | ||
12240 | /* | |
12241 | * Create DOF out of a currently enabled state. Right now, we only create | |
12242 | * DOF containing the run-time options -- but this could be expanded to create | |
12243 | * complete DOF representing the enabled state. | |
12244 | */ | |
12245 | static dof_hdr_t * | |
12246 | dtrace_dof_create(dtrace_state_t *state) | |
12247 | { | |
12248 | dof_hdr_t *dof; | |
12249 | dof_sec_t *sec; | |
12250 | dof_optdesc_t *opt; | |
12251 | int i, len = sizeof (dof_hdr_t) + | |
12252 | roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + | |
12253 | sizeof (dof_optdesc_t) * DTRACEOPT_MAX; | |
12254 | ||
12255 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
12256 | ||
b0d623f7 A |
12257 | #if !defined(__APPLE__) |
12258 | dof = kmem_zalloc(len, KM_SLEEP); | |
12259 | #else | |
2d21ac55 | 12260 | dof = dt_kmem_zalloc_aligned(len, 8, KM_SLEEP); |
b0d623f7 | 12261 | #endif /* __APPLE__ */ |
2d21ac55 A |
12262 | dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; |
12263 | dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; | |
12264 | dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; | |
12265 | dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; | |
12266 | ||
12267 | dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; | |
12268 | dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; | |
12269 | dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; | |
12270 | dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; | |
12271 | dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; | |
12272 | dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; | |
12273 | ||
12274 | dof->dofh_flags = 0; | |
12275 | dof->dofh_hdrsize = sizeof (dof_hdr_t); | |
12276 | dof->dofh_secsize = sizeof (dof_sec_t); | |
12277 | dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ | |
12278 | dof->dofh_secoff = sizeof (dof_hdr_t); | |
12279 | dof->dofh_loadsz = len; | |
12280 | dof->dofh_filesz = len; | |
12281 | dof->dofh_pad = 0; | |
12282 | ||
12283 | /* | |
12284 | * Fill in the option section header... | |
12285 | */ | |
12286 | sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); | |
12287 | sec->dofs_type = DOF_SECT_OPTDESC; | |
12288 | sec->dofs_align = sizeof (uint64_t); | |
12289 | sec->dofs_flags = DOF_SECF_LOAD; | |
12290 | sec->dofs_entsize = sizeof (dof_optdesc_t); | |
12291 | ||
12292 | opt = (dof_optdesc_t *)((uintptr_t)sec + | |
12293 | roundup(sizeof (dof_sec_t), sizeof (uint64_t))); | |
12294 | ||
12295 | sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; | |
12296 | sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; | |
12297 | ||
12298 | for (i = 0; i < DTRACEOPT_MAX; i++) { | |
12299 | opt[i].dofo_option = i; | |
12300 | opt[i].dofo_strtab = DOF_SECIDX_NONE; | |
12301 | opt[i].dofo_value = state->dts_options[i]; | |
12302 | } | |
12303 | ||
12304 | return (dof); | |
12305 | } | |
12306 | ||
12307 | static dof_hdr_t * | |
b0d623f7 | 12308 | #if !defined(__APPLE__) |
2d21ac55 | 12309 | dtrace_dof_copyin(uintptr_t uarg, int *errp) |
b0d623f7 A |
12310 | #else |
12311 | dtrace_dof_copyin(user_addr_t uarg, int *errp) | |
2d21ac55 A |
12312 | #endif |
12313 | { | |
12314 | dof_hdr_t hdr, *dof; | |
12315 | ||
12316 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED); | |
12317 | ||
12318 | /* | |
12319 | * First, we're going to copyin() the sizeof (dof_hdr_t). | |
12320 | */ | |
b0d623f7 | 12321 | #if !defined(__APPLE__) |
2d21ac55 | 12322 | if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { |
b0d623f7 A |
12323 | #else |
12324 | if (copyin(uarg, &hdr, sizeof (hdr)) != 0) { | |
2d21ac55 A |
12325 | #endif |
12326 | dtrace_dof_error(NULL, "failed to copyin DOF header"); | |
12327 | *errp = EFAULT; | |
12328 | return (NULL); | |
12329 | } | |
12330 | ||
12331 | /* | |
12332 | * Now we'll allocate the entire DOF and copy it in -- provided | |
12333 | * that the length isn't outrageous. | |
12334 | */ | |
b0d623f7 | 12335 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 12336 | if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { |
b0d623f7 A |
12337 | #else |
12338 | if (hdr.dofh_loadsz >= (uint64_t)dtrace_dof_maxsize) { | |
12339 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12340 | dtrace_dof_error(&hdr, "load size exceeds maximum"); |
12341 | *errp = E2BIG; | |
12342 | return (NULL); | |
12343 | } | |
12344 | ||
12345 | if (hdr.dofh_loadsz < sizeof (hdr)) { | |
12346 | dtrace_dof_error(&hdr, "invalid load size"); | |
12347 | *errp = EINVAL; | |
12348 | return (NULL); | |
12349 | } | |
12350 | ||
b0d623f7 A |
12351 | #if !defined(__APPLE__) |
12352 | dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); | |
12353 | ||
12354 | if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { | |
12355 | #else | |
2d21ac55 A |
12356 | dof = dt_kmem_alloc_aligned(hdr.dofh_loadsz, 8, KM_SLEEP); |
12357 | ||
2d21ac55 | 12358 | if (copyin(uarg, dof, hdr.dofh_loadsz) != 0) { |
2d21ac55 A |
12359 | #endif |
12360 | dt_kmem_free_aligned(dof, hdr.dofh_loadsz); | |
12361 | *errp = EFAULT; | |
12362 | return (NULL); | |
12363 | } | |
12364 | ||
12365 | return (dof); | |
12366 | } | |
12367 | ||
12368 | #if defined(__APPLE__) | |
12369 | ||
12370 | static dof_hdr_t * | |
12371 | dtrace_dof_copyin_from_proc(proc_t* p, user_addr_t uarg, int *errp) | |
12372 | { | |
12373 | dof_hdr_t hdr, *dof; | |
12374 | ||
12375 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED); | |
12376 | ||
12377 | /* | |
12378 | * First, we're going to copyin() the sizeof (dof_hdr_t). | |
12379 | */ | |
12380 | if (uread(p, &hdr, sizeof(hdr), uarg) != KERN_SUCCESS) { | |
12381 | dtrace_dof_error(NULL, "failed to copyin DOF header"); | |
12382 | *errp = EFAULT; | |
12383 | return (NULL); | |
12384 | } | |
12385 | ||
12386 | /* | |
12387 | * Now we'll allocate the entire DOF and copy it in -- provided | |
12388 | * that the length isn't outrageous. | |
12389 | */ | |
b0d623f7 | 12390 | if (hdr.dofh_loadsz >= (uint64_t)dtrace_dof_maxsize) { |
2d21ac55 A |
12391 | dtrace_dof_error(&hdr, "load size exceeds maximum"); |
12392 | *errp = E2BIG; | |
12393 | return (NULL); | |
12394 | } | |
12395 | ||
12396 | if (hdr.dofh_loadsz < sizeof (hdr)) { | |
12397 | dtrace_dof_error(&hdr, "invalid load size"); | |
12398 | *errp = EINVAL; | |
12399 | return (NULL); | |
12400 | } | |
12401 | ||
12402 | dof = dt_kmem_alloc_aligned(hdr.dofh_loadsz, 8, KM_SLEEP); | |
12403 | ||
12404 | if (uread(p, dof, hdr.dofh_loadsz, uarg) != KERN_SUCCESS) { | |
12405 | dt_kmem_free_aligned(dof, hdr.dofh_loadsz); | |
12406 | *errp = EFAULT; | |
12407 | return (NULL); | |
12408 | } | |
12409 | ||
12410 | return (dof); | |
12411 | } | |
12412 | ||
12413 | #endif /* __APPLE__ */ | |
12414 | ||
12415 | static dof_hdr_t * | |
12416 | dtrace_dof_property(const char *name) | |
12417 | { | |
12418 | uchar_t *buf; | |
12419 | uint64_t loadsz; | |
12420 | unsigned int len, i; | |
12421 | dof_hdr_t *dof; | |
12422 | ||
12423 | /* | |
12424 | * Unfortunately, array of values in .conf files are always (and | |
12425 | * only) interpreted to be integer arrays. We must read our DOF | |
12426 | * as an integer array, and then squeeze it into a byte array. | |
12427 | */ | |
b0d623f7 | 12428 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
12429 | if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, |
12430 | (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) | |
12431 | return (NULL); | |
b0d623f7 A |
12432 | #else |
12433 | if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, | |
12434 | name, (int **)&buf, &len) != DDI_PROP_SUCCESS) | |
12435 | return (NULL); | |
12436 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12437 | |
12438 | for (i = 0; i < len; i++) | |
12439 | buf[i] = (uchar_t)(((int *)buf)[i]); | |
12440 | ||
12441 | if (len < sizeof (dof_hdr_t)) { | |
12442 | ddi_prop_free(buf); | |
12443 | dtrace_dof_error(NULL, "truncated header"); | |
12444 | return (NULL); | |
12445 | } | |
12446 | ||
12447 | if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { | |
12448 | ddi_prop_free(buf); | |
12449 | dtrace_dof_error(NULL, "truncated DOF"); | |
12450 | return (NULL); | |
12451 | } | |
12452 | ||
b0d623f7 | 12453 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 12454 | if (loadsz >= dtrace_dof_maxsize) { |
b0d623f7 A |
12455 | #else |
12456 | if (loadsz >= (uint64_t)dtrace_dof_maxsize) { | |
12457 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12458 | ddi_prop_free(buf); |
12459 | dtrace_dof_error(NULL, "oversized DOF"); | |
12460 | return (NULL); | |
12461 | } | |
12462 | ||
b0d623f7 A |
12463 | #if !defined(__APPLE__) |
12464 | dof = kmem_alloc(loadsz, KM_SLEEP); | |
12465 | #else | |
2d21ac55 | 12466 | dof = dt_kmem_alloc_aligned(loadsz, 8, KM_SLEEP); |
b0d623f7 | 12467 | #endif /* __APPLE__ */ |
2d21ac55 A |
12468 | bcopy(buf, dof, loadsz); |
12469 | ddi_prop_free(buf); | |
12470 | ||
12471 | return (dof); | |
12472 | } | |
12473 | ||
12474 | static void | |
12475 | dtrace_dof_destroy(dof_hdr_t *dof) | |
12476 | { | |
b0d623f7 A |
12477 | #if !defined(__APPLE__) |
12478 | kmem_free(dof, dof->dofh_loadsz); | |
12479 | #else | |
2d21ac55 | 12480 | dt_kmem_free_aligned(dof, dof->dofh_loadsz); |
b0d623f7 | 12481 | #endif /* __APPLE__ */ |
2d21ac55 A |
12482 | } |
12483 | ||
12484 | /* | |
12485 | * Return the dof_sec_t pointer corresponding to a given section index. If the | |
12486 | * index is not valid, dtrace_dof_error() is called and NULL is returned. If | |
12487 | * a type other than DOF_SECT_NONE is specified, the header is checked against | |
12488 | * this type and NULL is returned if the types do not match. | |
12489 | */ | |
12490 | static dof_sec_t * | |
12491 | dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) | |
12492 | { | |
12493 | dof_sec_t *sec = (dof_sec_t *)(uintptr_t) | |
12494 | ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); | |
12495 | ||
12496 | if (i >= dof->dofh_secnum) { | |
12497 | dtrace_dof_error(dof, "referenced section index is invalid"); | |
12498 | return (NULL); | |
12499 | } | |
12500 | ||
12501 | if (!(sec->dofs_flags & DOF_SECF_LOAD)) { | |
12502 | dtrace_dof_error(dof, "referenced section is not loadable"); | |
12503 | return (NULL); | |
12504 | } | |
12505 | ||
12506 | if (type != DOF_SECT_NONE && type != sec->dofs_type) { | |
12507 | dtrace_dof_error(dof, "referenced section is the wrong type"); | |
12508 | return (NULL); | |
12509 | } | |
12510 | ||
12511 | return (sec); | |
12512 | } | |
12513 | ||
12514 | static dtrace_probedesc_t * | |
12515 | dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) | |
12516 | { | |
12517 | dof_probedesc_t *probe; | |
12518 | dof_sec_t *strtab; | |
12519 | uintptr_t daddr = (uintptr_t)dof; | |
12520 | uintptr_t str; | |
12521 | size_t size; | |
12522 | ||
12523 | if (sec->dofs_type != DOF_SECT_PROBEDESC) { | |
12524 | dtrace_dof_error(dof, "invalid probe section"); | |
12525 | return (NULL); | |
12526 | } | |
12527 | ||
12528 | if (sec->dofs_align != sizeof (dof_secidx_t)) { | |
12529 | dtrace_dof_error(dof, "bad alignment in probe description"); | |
12530 | return (NULL); | |
12531 | } | |
12532 | ||
12533 | if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { | |
12534 | dtrace_dof_error(dof, "truncated probe description"); | |
12535 | return (NULL); | |
12536 | } | |
12537 | ||
12538 | probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); | |
12539 | strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); | |
12540 | ||
12541 | if (strtab == NULL) | |
12542 | return (NULL); | |
12543 | ||
12544 | str = daddr + strtab->dofs_offset; | |
12545 | size = strtab->dofs_size; | |
12546 | ||
12547 | if (probe->dofp_provider >= strtab->dofs_size) { | |
12548 | dtrace_dof_error(dof, "corrupt probe provider"); | |
12549 | return (NULL); | |
12550 | } | |
12551 | ||
12552 | (void) strncpy(desc->dtpd_provider, | |
12553 | (char *)(str + probe->dofp_provider), | |
12554 | MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); | |
b0d623f7 A |
12555 | #if defined(__APPLE__) /* Employ size bounded string operation. */ |
12556 | desc->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
12557 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12558 | |
12559 | if (probe->dofp_mod >= strtab->dofs_size) { | |
12560 | dtrace_dof_error(dof, "corrupt probe module"); | |
12561 | return (NULL); | |
12562 | } | |
12563 | ||
12564 | (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), | |
12565 | MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); | |
b0d623f7 A |
12566 | #if defined(__APPLE__) /* Employ size bounded string operation. */ |
12567 | desc->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
12568 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12569 | |
12570 | if (probe->dofp_func >= strtab->dofs_size) { | |
12571 | dtrace_dof_error(dof, "corrupt probe function"); | |
12572 | return (NULL); | |
12573 | } | |
12574 | ||
12575 | (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), | |
12576 | MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); | |
b0d623f7 A |
12577 | #if defined(__APPLE__) /* Employ size bounded string operation. */ |
12578 | desc->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
12579 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12580 | |
12581 | if (probe->dofp_name >= strtab->dofs_size) { | |
12582 | dtrace_dof_error(dof, "corrupt probe name"); | |
12583 | return (NULL); | |
12584 | } | |
12585 | ||
12586 | (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), | |
12587 | MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); | |
b0d623f7 A |
12588 | #if defined(__APPLE__) /* Employ size bounded string operation. */ |
12589 | desc->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
12590 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12591 | |
12592 | return (desc); | |
12593 | } | |
12594 | ||
12595 | static dtrace_difo_t * | |
12596 | dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, | |
12597 | cred_t *cr) | |
12598 | { | |
12599 | dtrace_difo_t *dp; | |
12600 | size_t ttl = 0; | |
12601 | dof_difohdr_t *dofd; | |
12602 | uintptr_t daddr = (uintptr_t)dof; | |
c910b4d9 | 12603 | size_t max_size = dtrace_difo_maxsize; |
b0d623f7 | 12604 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 12605 | int i, l, n; |
b0d623f7 A |
12606 | #else |
12607 | uint_t i; | |
12608 | int l, n; | |
12609 | #endif /* __APPLE__ */ | |
12610 | ||
2d21ac55 A |
12611 | |
12612 | static const struct { | |
12613 | int section; | |
12614 | int bufoffs; | |
12615 | int lenoffs; | |
12616 | int entsize; | |
12617 | int align; | |
12618 | const char *msg; | |
12619 | } difo[] = { | |
12620 | { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), | |
12621 | offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), | |
12622 | sizeof (dif_instr_t), "multiple DIF sections" }, | |
12623 | ||
12624 | { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), | |
12625 | offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), | |
12626 | sizeof (uint64_t), "multiple integer tables" }, | |
12627 | ||
12628 | { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), | |
12629 | offsetof(dtrace_difo_t, dtdo_strlen), 0, | |
12630 | sizeof (char), "multiple string tables" }, | |
12631 | ||
12632 | { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), | |
12633 | offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), | |
12634 | sizeof (uint_t), "multiple variable tables" }, | |
12635 | ||
12636 | #if !defined(__APPLE__) | |
12637 | { DOF_SECT_NONE, 0, 0, 0, NULL } | |
12638 | #else | |
12639 | { DOF_SECT_NONE, 0, 0, 0, 0, NULL } | |
12640 | #endif /* __APPLE__ */ | |
12641 | }; | |
12642 | ||
12643 | if (sec->dofs_type != DOF_SECT_DIFOHDR) { | |
12644 | dtrace_dof_error(dof, "invalid DIFO header section"); | |
12645 | return (NULL); | |
12646 | } | |
12647 | ||
12648 | if (sec->dofs_align != sizeof (dof_secidx_t)) { | |
12649 | dtrace_dof_error(dof, "bad alignment in DIFO header"); | |
12650 | return (NULL); | |
12651 | } | |
12652 | ||
12653 | if (sec->dofs_size < sizeof (dof_difohdr_t) || | |
12654 | sec->dofs_size % sizeof (dof_secidx_t)) { | |
12655 | dtrace_dof_error(dof, "bad size in DIFO header"); | |
12656 | return (NULL); | |
12657 | } | |
12658 | ||
12659 | dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); | |
12660 | n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; | |
12661 | ||
12662 | dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); | |
12663 | dp->dtdo_rtype = dofd->dofd_rtype; | |
12664 | ||
12665 | for (l = 0; l < n; l++) { | |
12666 | dof_sec_t *subsec; | |
12667 | void **bufp; | |
12668 | uint32_t *lenp; | |
12669 | ||
12670 | if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, | |
12671 | dofd->dofd_links[l])) == NULL) | |
12672 | goto err; /* invalid section link */ | |
12673 | ||
c910b4d9 | 12674 | if (ttl + subsec->dofs_size > max_size) { |
2d21ac55 A |
12675 | dtrace_dof_error(dof, "exceeds maximum size"); |
12676 | goto err; | |
12677 | } | |
12678 | ||
12679 | ttl += subsec->dofs_size; | |
12680 | ||
12681 | for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { | |
b0d623f7 A |
12682 | |
12683 | #if !defined(__APPLE__) /* Quiet compiler warnings */ | |
2d21ac55 A |
12684 | if (subsec->dofs_type != difo[i].section) |
12685 | continue; | |
b0d623f7 A |
12686 | #else |
12687 | if (subsec->dofs_type != (uint32_t)difo[i].section) | |
12688 | continue; | |
12689 | #endif /* __APPLE __ */ | |
2d21ac55 A |
12690 | |
12691 | if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { | |
12692 | dtrace_dof_error(dof, "section not loaded"); | |
12693 | goto err; | |
12694 | } | |
12695 | ||
b0d623f7 | 12696 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
12697 | if (subsec->dofs_align != difo[i].align) { |
12698 | dtrace_dof_error(dof, "bad alignment"); | |
12699 | goto err; | |
12700 | } | |
b0d623f7 A |
12701 | #else |
12702 | if (subsec->dofs_align != (uint32_t)difo[i].align) { | |
12703 | dtrace_dof_error(dof, "bad alignment"); | |
12704 | goto err; | |
12705 | } | |
12706 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12707 | |
12708 | bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); | |
12709 | lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); | |
12710 | ||
12711 | if (*bufp != NULL) { | |
12712 | dtrace_dof_error(dof, difo[i].msg); | |
12713 | goto err; | |
12714 | } | |
12715 | ||
b0d623f7 | 12716 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 A |
12717 | if (difo[i].entsize != subsec->dofs_entsize) { |
12718 | dtrace_dof_error(dof, "entry size mismatch"); | |
12719 | goto err; | |
12720 | } | |
b0d623f7 A |
12721 | #else |
12722 | if ((uint32_t)difo[i].entsize != subsec->dofs_entsize) { | |
12723 | dtrace_dof_error(dof, "entry size mismatch"); | |
12724 | goto err; | |
12725 | } | |
12726 | #endif /* __APPLE__ */ | |
2d21ac55 A |
12727 | |
12728 | if (subsec->dofs_entsize != 0 && | |
12729 | (subsec->dofs_size % subsec->dofs_entsize) != 0) { | |
12730 | dtrace_dof_error(dof, "corrupt entry size"); | |
12731 | goto err; | |
12732 | } | |
12733 | ||
12734 | *lenp = subsec->dofs_size; | |
12735 | *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); | |
12736 | bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), | |
12737 | *bufp, subsec->dofs_size); | |
12738 | ||
12739 | if (subsec->dofs_entsize != 0) | |
12740 | *lenp /= subsec->dofs_entsize; | |
12741 | ||
12742 | break; | |
12743 | } | |
12744 | ||
12745 | /* | |
12746 | * If we encounter a loadable DIFO sub-section that is not | |
12747 | * known to us, assume this is a broken program and fail. | |
12748 | */ | |
12749 | if (difo[i].section == DOF_SECT_NONE && | |
12750 | (subsec->dofs_flags & DOF_SECF_LOAD)) { | |
12751 | dtrace_dof_error(dof, "unrecognized DIFO subsection"); | |
12752 | goto err; | |
12753 | } | |
12754 | } | |
b0d623f7 | 12755 | |
2d21ac55 A |
12756 | if (dp->dtdo_buf == NULL) { |
12757 | /* | |
12758 | * We can't have a DIF object without DIF text. | |
12759 | */ | |
12760 | dtrace_dof_error(dof, "missing DIF text"); | |
12761 | goto err; | |
12762 | } | |
12763 | ||
12764 | /* | |
12765 | * Before we validate the DIF object, run through the variable table | |
12766 | * looking for the strings -- if any of their size are under, we'll set | |
12767 | * their size to be the system-wide default string size. Note that | |
12768 | * this should _not_ happen if the "strsize" option has been set -- | |
12769 | * in this case, the compiler should have set the size to reflect the | |
12770 | * setting of the option. | |
12771 | */ | |
12772 | for (i = 0; i < dp->dtdo_varlen; i++) { | |
12773 | dtrace_difv_t *v = &dp->dtdo_vartab[i]; | |
12774 | dtrace_diftype_t *t = &v->dtdv_type; | |
12775 | ||
12776 | if (v->dtdv_id < DIF_VAR_OTHER_UBASE) | |
12777 | continue; | |
12778 | ||
12779 | if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) | |
12780 | t->dtdt_size = dtrace_strsize_default; | |
12781 | } | |
12782 | ||
12783 | if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) | |
12784 | goto err; | |
12785 | ||
12786 | dtrace_difo_init(dp, vstate); | |
12787 | return (dp); | |
12788 | ||
12789 | err: | |
12790 | kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); | |
12791 | kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); | |
12792 | kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); | |
12793 | kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); | |
12794 | ||
12795 | kmem_free(dp, sizeof (dtrace_difo_t)); | |
12796 | return (NULL); | |
12797 | } | |
12798 | ||
12799 | static dtrace_predicate_t * | |
12800 | dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, | |
12801 | cred_t *cr) | |
12802 | { | |
12803 | dtrace_difo_t *dp; | |
12804 | ||
12805 | if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) | |
12806 | return (NULL); | |
12807 | ||
12808 | return (dtrace_predicate_create(dp)); | |
12809 | } | |
12810 | ||
12811 | static dtrace_actdesc_t * | |
12812 | dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, | |
12813 | cred_t *cr) | |
12814 | { | |
12815 | dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; | |
12816 | dof_actdesc_t *desc; | |
12817 | dof_sec_t *difosec; | |
12818 | size_t offs; | |
12819 | uintptr_t daddr = (uintptr_t)dof; | |
12820 | uint64_t arg; | |
12821 | dtrace_actkind_t kind; | |
12822 | ||
12823 | if (sec->dofs_type != DOF_SECT_ACTDESC) { | |
12824 | dtrace_dof_error(dof, "invalid action section"); | |
12825 | return (NULL); | |
12826 | } | |
12827 | ||
12828 | if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { | |
12829 | dtrace_dof_error(dof, "truncated action description"); | |
12830 | return (NULL); | |
12831 | } | |
12832 | ||
12833 | if (sec->dofs_align != sizeof (uint64_t)) { | |
12834 | dtrace_dof_error(dof, "bad alignment in action description"); | |
12835 | return (NULL); | |
12836 | } | |
12837 | ||
12838 | if (sec->dofs_size < sec->dofs_entsize) { | |
12839 | dtrace_dof_error(dof, "section entry size exceeds total size"); | |
12840 | return (NULL); | |
12841 | } | |
12842 | ||
12843 | if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { | |
12844 | dtrace_dof_error(dof, "bad entry size in action description"); | |
12845 | return (NULL); | |
12846 | } | |
12847 | ||
12848 | if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { | |
12849 | dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); | |
12850 | return (NULL); | |
12851 | } | |
12852 | ||
12853 | for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { | |
12854 | desc = (dof_actdesc_t *)(daddr + | |
12855 | (uintptr_t)sec->dofs_offset + offs); | |
12856 | kind = (dtrace_actkind_t)desc->dofa_kind; | |
12857 | ||
12858 | if (DTRACEACT_ISPRINTFLIKE(kind) && | |
12859 | (kind != DTRACEACT_PRINTA || | |
12860 | desc->dofa_strtab != DOF_SECIDX_NONE)) { | |
12861 | dof_sec_t *strtab; | |
12862 | char *str, *fmt; | |
12863 | uint64_t i; | |
12864 | ||
12865 | /* | |
12866 | * printf()-like actions must have a format string. | |
12867 | */ | |
12868 | if ((strtab = dtrace_dof_sect(dof, | |
12869 | DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) | |
12870 | goto err; | |
12871 | ||
12872 | str = (char *)((uintptr_t)dof + | |
12873 | (uintptr_t)strtab->dofs_offset); | |
12874 | ||
12875 | for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { | |
12876 | if (str[i] == '\0') | |
12877 | break; | |
12878 | } | |
12879 | ||
12880 | if (i >= strtab->dofs_size) { | |
12881 | dtrace_dof_error(dof, "bogus format string"); | |
12882 | goto err; | |
12883 | } | |
12884 | ||
12885 | if (i == desc->dofa_arg) { | |
12886 | dtrace_dof_error(dof, "empty format string"); | |
12887 | goto err; | |
12888 | } | |
12889 | ||
12890 | i -= desc->dofa_arg; | |
12891 | fmt = kmem_alloc(i + 1, KM_SLEEP); | |
12892 | bcopy(&str[desc->dofa_arg], fmt, i + 1); | |
12893 | arg = (uint64_t)(uintptr_t)fmt; | |
12894 | } else { | |
12895 | if (kind == DTRACEACT_PRINTA) { | |
12896 | ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); | |
12897 | arg = 0; | |
12898 | } else { | |
12899 | arg = desc->dofa_arg; | |
12900 | } | |
12901 | } | |
12902 | ||
12903 | act = dtrace_actdesc_create(kind, desc->dofa_ntuple, | |
12904 | desc->dofa_uarg, arg); | |
12905 | ||
12906 | if (last != NULL) { | |
12907 | last->dtad_next = act; | |
12908 | } else { | |
12909 | first = act; | |
12910 | } | |
12911 | ||
12912 | last = act; | |
12913 | ||
12914 | if (desc->dofa_difo == DOF_SECIDX_NONE) | |
12915 | continue; | |
12916 | ||
12917 | if ((difosec = dtrace_dof_sect(dof, | |
12918 | DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) | |
12919 | goto err; | |
12920 | ||
12921 | act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); | |
12922 | ||
12923 | if (act->dtad_difo == NULL) | |
12924 | goto err; | |
12925 | } | |
12926 | ||
12927 | ASSERT(first != NULL); | |
12928 | return (first); | |
12929 | ||
12930 | err: | |
12931 | for (act = first; act != NULL; act = next) { | |
12932 | next = act->dtad_next; | |
12933 | dtrace_actdesc_release(act, vstate); | |
12934 | } | |
12935 | ||
12936 | return (NULL); | |
12937 | } | |
12938 | ||
12939 | static dtrace_ecbdesc_t * | |
12940 | dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, | |
12941 | cred_t *cr) | |
12942 | { | |
12943 | dtrace_ecbdesc_t *ep; | |
12944 | dof_ecbdesc_t *ecb; | |
12945 | dtrace_probedesc_t *desc; | |
12946 | dtrace_predicate_t *pred = NULL; | |
12947 | ||
12948 | if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { | |
12949 | dtrace_dof_error(dof, "truncated ECB description"); | |
12950 | return (NULL); | |
12951 | } | |
12952 | ||
12953 | if (sec->dofs_align != sizeof (uint64_t)) { | |
12954 | dtrace_dof_error(dof, "bad alignment in ECB description"); | |
12955 | return (NULL); | |
12956 | } | |
12957 | ||
12958 | ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); | |
12959 | sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); | |
12960 | ||
12961 | if (sec == NULL) | |
12962 | return (NULL); | |
12963 | ||
12964 | ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); | |
12965 | ep->dted_uarg = ecb->dofe_uarg; | |
12966 | desc = &ep->dted_probe; | |
12967 | ||
12968 | if (dtrace_dof_probedesc(dof, sec, desc) == NULL) | |
12969 | goto err; | |
12970 | ||
12971 | if (ecb->dofe_pred != DOF_SECIDX_NONE) { | |
12972 | if ((sec = dtrace_dof_sect(dof, | |
12973 | DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) | |
12974 | goto err; | |
12975 | ||
12976 | if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) | |
12977 | goto err; | |
12978 | ||
12979 | ep->dted_pred.dtpdd_predicate = pred; | |
12980 | } | |
12981 | ||
12982 | if (ecb->dofe_actions != DOF_SECIDX_NONE) { | |
12983 | if ((sec = dtrace_dof_sect(dof, | |
12984 | DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) | |
12985 | goto err; | |
12986 | ||
12987 | ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); | |
12988 | ||
12989 | if (ep->dted_action == NULL) | |
12990 | goto err; | |
12991 | } | |
12992 | ||
12993 | return (ep); | |
12994 | ||
12995 | err: | |
12996 | if (pred != NULL) | |
12997 | dtrace_predicate_release(pred, vstate); | |
12998 | kmem_free(ep, sizeof (dtrace_ecbdesc_t)); | |
12999 | return (NULL); | |
13000 | } | |
13001 | ||
13002 | #if !defined(__APPLE__) /* APPLE dyld has already done this for us */ | |
13003 | /* | |
13004 | * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the | |
13005 | * specified DOF. At present, this amounts to simply adding 'ubase' to the | |
13006 | * site of any user SETX relocations to account for load object base address. | |
13007 | * In the future, if we need other relocations, this function can be extended. | |
13008 | */ | |
13009 | static int | |
13010 | dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) | |
13011 | { | |
13012 | uintptr_t daddr = (uintptr_t)dof; | |
13013 | dof_relohdr_t *dofr = | |
13014 | (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); | |
13015 | dof_sec_t *ss, *rs, *ts; | |
13016 | dof_relodesc_t *r; | |
13017 | uint_t i, n; | |
13018 | ||
13019 | if (sec->dofs_size < sizeof (dof_relohdr_t) || | |
13020 | sec->dofs_align != sizeof (dof_secidx_t)) { | |
13021 | dtrace_dof_error(dof, "invalid relocation header"); | |
13022 | return (-1); | |
13023 | } | |
13024 | ||
13025 | ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); | |
13026 | rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); | |
13027 | ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); | |
13028 | ||
13029 | if (ss == NULL || rs == NULL || ts == NULL) | |
13030 | return (-1); /* dtrace_dof_error() has been called already */ | |
13031 | ||
13032 | if (rs->dofs_entsize < sizeof (dof_relodesc_t) || | |
13033 | rs->dofs_align != sizeof (uint64_t)) { | |
13034 | dtrace_dof_error(dof, "invalid relocation section"); | |
13035 | return (-1); | |
13036 | } | |
13037 | ||
13038 | r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); | |
13039 | n = rs->dofs_size / rs->dofs_entsize; | |
13040 | ||
13041 | for (i = 0; i < n; i++) { | |
13042 | uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; | |
13043 | ||
13044 | switch (r->dofr_type) { | |
13045 | case DOF_RELO_NONE: | |
13046 | break; | |
13047 | case DOF_RELO_SETX: | |
13048 | if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + | |
13049 | sizeof (uint64_t) > ts->dofs_size) { | |
13050 | dtrace_dof_error(dof, "bad relocation offset"); | |
13051 | return (-1); | |
13052 | } | |
13053 | ||
13054 | if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { | |
13055 | dtrace_dof_error(dof, "misaligned setx relo"); | |
13056 | return (-1); | |
13057 | } | |
13058 | ||
13059 | *(uint64_t *)taddr += ubase; | |
13060 | break; | |
13061 | default: | |
13062 | dtrace_dof_error(dof, "invalid relocation type"); | |
13063 | return (-1); | |
13064 | } | |
13065 | ||
13066 | r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); | |
13067 | } | |
13068 | ||
13069 | return (0); | |
13070 | } | |
13071 | #endif /* __APPLE__ */ | |
13072 | ||
13073 | /* | |
13074 | * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated | |
13075 | * header: it should be at the front of a memory region that is at least | |
13076 | * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in | |
13077 | * size. It need not be validated in any other way. | |
13078 | */ | |
13079 | static int | |
13080 | dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, | |
13081 | dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) | |
13082 | { | |
b0d623f7 | 13083 | #pragma unused(ubase) /* __APPLE__ */ |
2d21ac55 A |
13084 | uint64_t len = dof->dofh_loadsz, seclen; |
13085 | uintptr_t daddr = (uintptr_t)dof; | |
13086 | dtrace_ecbdesc_t *ep; | |
13087 | dtrace_enabling_t *enab; | |
13088 | uint_t i; | |
13089 | ||
13090 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
13091 | ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); | |
13092 | ||
13093 | /* | |
13094 | * Check the DOF header identification bytes. In addition to checking | |
13095 | * valid settings, we also verify that unused bits/bytes are zeroed so | |
13096 | * we can use them later without fear of regressing existing binaries. | |
13097 | */ | |
13098 | if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], | |
13099 | DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { | |
13100 | dtrace_dof_error(dof, "DOF magic string mismatch"); | |
13101 | return (-1); | |
13102 | } | |
13103 | ||
13104 | if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && | |
13105 | dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { | |
13106 | dtrace_dof_error(dof, "DOF has invalid data model"); | |
13107 | return (-1); | |
13108 | } | |
13109 | ||
13110 | if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { | |
13111 | dtrace_dof_error(dof, "DOF encoding mismatch"); | |
13112 | return (-1); | |
13113 | } | |
13114 | ||
13115 | #if !defined(__APPLE__) | |
13116 | if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && | |
13117 | dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { | |
13118 | dtrace_dof_error(dof, "DOF version mismatch"); | |
13119 | return (-1); | |
13120 | } | |
13121 | #else | |
13122 | /* | |
13123 | * We only support DOF_VERSION_3 for now. | |
13124 | */ | |
13125 | if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_3) { | |
13126 | dtrace_dof_error(dof, "DOF version mismatch"); | |
13127 | return (-1); | |
13128 | } | |
13129 | #endif | |
13130 | ||
13131 | if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { | |
13132 | dtrace_dof_error(dof, "DOF uses unsupported instruction set"); | |
13133 | return (-1); | |
13134 | } | |
13135 | ||
13136 | if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { | |
13137 | dtrace_dof_error(dof, "DOF uses too many integer registers"); | |
13138 | return (-1); | |
13139 | } | |
13140 | ||
13141 | if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { | |
13142 | dtrace_dof_error(dof, "DOF uses too many tuple registers"); | |
13143 | return (-1); | |
13144 | } | |
13145 | ||
13146 | for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { | |
13147 | if (dof->dofh_ident[i] != 0) { | |
13148 | dtrace_dof_error(dof, "DOF has invalid ident byte set"); | |
13149 | return (-1); | |
13150 | } | |
13151 | } | |
13152 | ||
13153 | if (dof->dofh_flags & ~DOF_FL_VALID) { | |
13154 | dtrace_dof_error(dof, "DOF has invalid flag bits set"); | |
13155 | return (-1); | |
13156 | } | |
13157 | ||
13158 | if (dof->dofh_secsize == 0) { | |
13159 | dtrace_dof_error(dof, "zero section header size"); | |
13160 | return (-1); | |
13161 | } | |
13162 | ||
13163 | /* | |
13164 | * Check that the section headers don't exceed the amount of DOF | |
13165 | * data. Note that we cast the section size and number of sections | |
13166 | * to uint64_t's to prevent possible overflow in the multiplication. | |
13167 | */ | |
13168 | seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; | |
13169 | ||
13170 | if (dof->dofh_secoff > len || seclen > len || | |
13171 | dof->dofh_secoff + seclen > len) { | |
13172 | dtrace_dof_error(dof, "truncated section headers"); | |
13173 | return (-1); | |
13174 | } | |
13175 | ||
13176 | if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { | |
13177 | dtrace_dof_error(dof, "misaligned section headers"); | |
13178 | return (-1); | |
13179 | } | |
13180 | ||
13181 | if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { | |
13182 | dtrace_dof_error(dof, "misaligned section size"); | |
13183 | return (-1); | |
13184 | } | |
13185 | ||
13186 | /* | |
13187 | * Take an initial pass through the section headers to be sure that | |
13188 | * the headers don't have stray offsets. If the 'noprobes' flag is | |
13189 | * set, do not permit sections relating to providers, probes, or args. | |
13190 | */ | |
13191 | for (i = 0; i < dof->dofh_secnum; i++) { | |
13192 | dof_sec_t *sec = (dof_sec_t *)(daddr + | |
13193 | (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); | |
13194 | ||
13195 | if (noprobes) { | |
13196 | switch (sec->dofs_type) { | |
13197 | case DOF_SECT_PROVIDER: | |
13198 | case DOF_SECT_PROBES: | |
13199 | case DOF_SECT_PRARGS: | |
13200 | case DOF_SECT_PROFFS: | |
13201 | dtrace_dof_error(dof, "illegal sections " | |
13202 | "for enabling"); | |
13203 | return (-1); | |
13204 | } | |
13205 | } | |
13206 | ||
13207 | if (!(sec->dofs_flags & DOF_SECF_LOAD)) | |
13208 | continue; /* just ignore non-loadable sections */ | |
13209 | ||
13210 | if (sec->dofs_align & (sec->dofs_align - 1)) { | |
13211 | dtrace_dof_error(dof, "bad section alignment"); | |
13212 | return (-1); | |
13213 | } | |
13214 | ||
13215 | if (sec->dofs_offset & (sec->dofs_align - 1)) { | |
13216 | dtrace_dof_error(dof, "misaligned section"); | |
13217 | return (-1); | |
13218 | } | |
13219 | ||
13220 | if (sec->dofs_offset > len || sec->dofs_size > len || | |
13221 | sec->dofs_offset + sec->dofs_size > len) { | |
13222 | dtrace_dof_error(dof, "corrupt section header"); | |
13223 | return (-1); | |
13224 | } | |
13225 | ||
13226 | if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + | |
13227 | sec->dofs_offset + sec->dofs_size - 1) != '\0') { | |
13228 | dtrace_dof_error(dof, "non-terminating string table"); | |
13229 | return (-1); | |
13230 | } | |
13231 | } | |
13232 | ||
13233 | #if !defined(__APPLE__) | |
2d21ac55 A |
13234 | /* |
13235 | * Take a second pass through the sections and locate and perform any | |
13236 | * relocations that are present. We do this after the first pass to | |
13237 | * be sure that all sections have had their headers validated. | |
13238 | */ | |
13239 | for (i = 0; i < dof->dofh_secnum; i++) { | |
13240 | dof_sec_t *sec = (dof_sec_t *)(daddr + | |
13241 | (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); | |
13242 | ||
13243 | if (!(sec->dofs_flags & DOF_SECF_LOAD)) | |
13244 | continue; /* skip sections that are not loadable */ | |
13245 | ||
13246 | switch (sec->dofs_type) { | |
13247 | case DOF_SECT_URELHDR: | |
13248 | if (dtrace_dof_relocate(dof, sec, ubase) != 0) | |
13249 | return (-1); | |
13250 | break; | |
13251 | } | |
13252 | } | |
b0d623f7 A |
13253 | #else |
13254 | /* | |
13255 | * APPLE NOTE: We have no relocation to perform. All dof values are | |
13256 | * relative offsets. | |
13257 | */ | |
2d21ac55 A |
13258 | #endif /* __APPLE__ */ |
13259 | ||
13260 | if ((enab = *enabp) == NULL) | |
13261 | enab = *enabp = dtrace_enabling_create(vstate); | |
13262 | ||
13263 | for (i = 0; i < dof->dofh_secnum; i++) { | |
13264 | dof_sec_t *sec = (dof_sec_t *)(daddr + | |
13265 | (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); | |
13266 | ||
13267 | if (sec->dofs_type != DOF_SECT_ECBDESC) | |
13268 | continue; | |
13269 | ||
13270 | #if !defined(__APPLE__) | |
13271 | if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { | |
13272 | dtrace_enabling_destroy(enab); | |
13273 | *enabp = NULL; | |
13274 | return (-1); | |
13275 | } | |
13276 | #else | |
b0d623f7 | 13277 | /* Note: Defend against gcc 4.0 botch on x86 (not all paths out of inlined dtrace_dof_ecbdesc |
2d21ac55 A |
13278 | are checked for the NULL return value.) */ |
13279 | ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr); | |
13280 | if (ep == NULL) { | |
13281 | dtrace_enabling_destroy(enab); | |
13282 | *enabp = NULL; | |
13283 | return (-1); | |
13284 | } | |
13285 | #endif /* __APPLE__ */ | |
13286 | ||
13287 | dtrace_enabling_add(enab, ep); | |
13288 | } | |
13289 | ||
13290 | return (0); | |
13291 | } | |
13292 | ||
13293 | /* | |
13294 | * Process DOF for any options. This routine assumes that the DOF has been | |
13295 | * at least processed by dtrace_dof_slurp(). | |
13296 | */ | |
13297 | static int | |
13298 | dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) | |
13299 | { | |
b0d623f7 | 13300 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 13301 | int i, rval; |
b0d623f7 A |
13302 | #else |
13303 | uint_t i; | |
13304 | int rval; | |
13305 | #endif /* __APPLE__ */ | |
2d21ac55 A |
13306 | uint32_t entsize; |
13307 | size_t offs; | |
13308 | dof_optdesc_t *desc; | |
13309 | ||
13310 | for (i = 0; i < dof->dofh_secnum; i++) { | |
13311 | dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + | |
13312 | (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); | |
13313 | ||
13314 | if (sec->dofs_type != DOF_SECT_OPTDESC) | |
13315 | continue; | |
13316 | ||
13317 | if (sec->dofs_align != sizeof (uint64_t)) { | |
13318 | dtrace_dof_error(dof, "bad alignment in " | |
13319 | "option description"); | |
13320 | return (EINVAL); | |
13321 | } | |
13322 | ||
13323 | if ((entsize = sec->dofs_entsize) == 0) { | |
13324 | dtrace_dof_error(dof, "zeroed option entry size"); | |
13325 | return (EINVAL); | |
13326 | } | |
13327 | ||
13328 | if (entsize < sizeof (dof_optdesc_t)) { | |
13329 | dtrace_dof_error(dof, "bad option entry size"); | |
13330 | return (EINVAL); | |
13331 | } | |
13332 | ||
13333 | for (offs = 0; offs < sec->dofs_size; offs += entsize) { | |
13334 | desc = (dof_optdesc_t *)((uintptr_t)dof + | |
13335 | (uintptr_t)sec->dofs_offset + offs); | |
13336 | ||
13337 | if (desc->dofo_strtab != DOF_SECIDX_NONE) { | |
13338 | dtrace_dof_error(dof, "non-zero option string"); | |
13339 | return (EINVAL); | |
13340 | } | |
13341 | ||
b0d623f7 | 13342 | #if !defined(__APPLE__) /* Quiet compiler warnings */ |
2d21ac55 | 13343 | if (desc->dofo_value == DTRACEOPT_UNSET) { |
b0d623f7 A |
13344 | #else |
13345 | if (desc->dofo_value == (uint64_t)DTRACEOPT_UNSET) { | |
13346 | #endif /* __APPLE __ */ | |
2d21ac55 A |
13347 | dtrace_dof_error(dof, "unset option"); |
13348 | return (EINVAL); | |
13349 | } | |
13350 | ||
13351 | if ((rval = dtrace_state_option(state, | |
13352 | desc->dofo_option, desc->dofo_value)) != 0) { | |
13353 | dtrace_dof_error(dof, "rejected option"); | |
13354 | return (rval); | |
13355 | } | |
13356 | } | |
13357 | } | |
13358 | ||
13359 | return (0); | |
13360 | } | |
13361 | ||
13362 | /* | |
13363 | * DTrace Consumer State Functions | |
13364 | */ | |
b0d623f7 | 13365 | #if defined(__APPLE__) /* Quiet compiler warning. */ |
2d21ac55 A |
13366 | static |
13367 | #endif /* __APPLE__ */ | |
13368 | int | |
13369 | dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) | |
13370 | { | |
c910b4d9 | 13371 | size_t hashsize, maxper, min_size, chunksize = dstate->dtds_chunksize; |
2d21ac55 A |
13372 | void *base; |
13373 | uintptr_t limit; | |
13374 | dtrace_dynvar_t *dvar, *next, *start; | |
b0d623f7 | 13375 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 13376 | int i; |
b0d623f7 A |
13377 | #else |
13378 | size_t i; | |
13379 | #endif /* __APPLE__ */ | |
2d21ac55 A |
13380 | |
13381 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
13382 | ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); | |
13383 | ||
13384 | bzero(dstate, sizeof (dtrace_dstate_t)); | |
13385 | ||
13386 | if ((dstate->dtds_chunksize = chunksize) == 0) | |
13387 | dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; | |
13388 | ||
c910b4d9 A |
13389 | if (size < (min_size = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) |
13390 | size = min_size; | |
2d21ac55 A |
13391 | |
13392 | if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) | |
13393 | return (ENOMEM); | |
13394 | ||
13395 | dstate->dtds_size = size; | |
13396 | dstate->dtds_base = base; | |
13397 | dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); | |
c910b4d9 | 13398 | bzero(dstate->dtds_percpu, (int)NCPU * sizeof (dtrace_dstate_percpu_t)); |
2d21ac55 A |
13399 | |
13400 | hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); | |
13401 | ||
13402 | if (hashsize != 1 && (hashsize & 1)) | |
13403 | hashsize--; | |
13404 | ||
13405 | dstate->dtds_hashsize = hashsize; | |
13406 | dstate->dtds_hash = dstate->dtds_base; | |
13407 | ||
13408 | /* | |
13409 | * Set all of our hash buckets to point to the single sink, and (if | |
13410 | * it hasn't already been set), set the sink's hash value to be the | |
13411 | * sink sentinel value. The sink is needed for dynamic variable | |
13412 | * lookups to know that they have iterated over an entire, valid hash | |
13413 | * chain. | |
13414 | */ | |
13415 | for (i = 0; i < hashsize; i++) | |
13416 | dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; | |
13417 | ||
13418 | if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) | |
13419 | dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; | |
13420 | ||
13421 | /* | |
13422 | * Determine number of active CPUs. Divide free list evenly among | |
13423 | * active CPUs. | |
13424 | */ | |
13425 | start = (dtrace_dynvar_t *) | |
13426 | ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); | |
13427 | limit = (uintptr_t)base + size; | |
13428 | ||
c910b4d9 | 13429 | maxper = (limit - (uintptr_t)start) / (int)NCPU; |
2d21ac55 A |
13430 | maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; |
13431 | ||
b0d623f7 | 13432 | for (i = 0; i < NCPU; i++) { |
2d21ac55 A |
13433 | dstate->dtds_percpu[i].dtdsc_free = dvar = start; |
13434 | ||
13435 | /* | |
13436 | * If we don't even have enough chunks to make it once through | |
13437 | * NCPUs, we're just going to allocate everything to the first | |
13438 | * CPU. And if we're on the last CPU, we're going to allocate | |
13439 | * whatever is left over. In either case, we set the limit to | |
13440 | * be the limit of the dynamic variable space. | |
13441 | */ | |
b0d623f7 | 13442 | if (maxper == 0 || i == NCPU - 1) { |
2d21ac55 A |
13443 | limit = (uintptr_t)base + size; |
13444 | start = NULL; | |
13445 | } else { | |
13446 | limit = (uintptr_t)start + maxper; | |
13447 | start = (dtrace_dynvar_t *)limit; | |
13448 | } | |
13449 | ||
13450 | ASSERT(limit <= (uintptr_t)base + size); | |
13451 | ||
13452 | for (;;) { | |
13453 | next = (dtrace_dynvar_t *)((uintptr_t)dvar + | |
13454 | dstate->dtds_chunksize); | |
13455 | ||
13456 | if ((uintptr_t)next + dstate->dtds_chunksize >= limit) | |
13457 | break; | |
13458 | ||
13459 | dvar->dtdv_next = next; | |
13460 | dvar = next; | |
13461 | } | |
13462 | ||
13463 | if (maxper == 0) | |
13464 | break; | |
13465 | } | |
13466 | ||
13467 | return (0); | |
13468 | } | |
13469 | ||
b0d623f7 | 13470 | #if defined(__APPLE__) /* Quiet compiler warning. */ |
2d21ac55 A |
13471 | static |
13472 | #endif /* __APPLE__ */ | |
13473 | void | |
13474 | dtrace_dstate_fini(dtrace_dstate_t *dstate) | |
13475 | { | |
13476 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
13477 | ||
13478 | if (dstate->dtds_base == NULL) | |
13479 | return; | |
13480 | ||
13481 | kmem_free(dstate->dtds_base, dstate->dtds_size); | |
13482 | kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); | |
13483 | } | |
13484 | ||
13485 | static void | |
13486 | dtrace_vstate_fini(dtrace_vstate_t *vstate) | |
13487 | { | |
13488 | /* | |
13489 | * Logical XOR, where are you? | |
13490 | */ | |
13491 | ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); | |
13492 | ||
13493 | if (vstate->dtvs_nglobals > 0) { | |
13494 | kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * | |
13495 | sizeof (dtrace_statvar_t *)); | |
13496 | } | |
13497 | ||
13498 | if (vstate->dtvs_ntlocals > 0) { | |
13499 | kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * | |
13500 | sizeof (dtrace_difv_t)); | |
13501 | } | |
13502 | ||
13503 | ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); | |
13504 | ||
13505 | if (vstate->dtvs_nlocals > 0) { | |
13506 | kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * | |
13507 | sizeof (dtrace_statvar_t *)); | |
13508 | } | |
13509 | } | |
13510 | ||
13511 | static void | |
13512 | dtrace_state_clean(dtrace_state_t *state) | |
13513 | { | |
13514 | if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) | |
13515 | return; | |
13516 | ||
13517 | dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); | |
13518 | dtrace_speculation_clean(state); | |
13519 | } | |
13520 | ||
13521 | static void | |
13522 | dtrace_state_deadman(dtrace_state_t *state) | |
13523 | { | |
13524 | hrtime_t now; | |
13525 | ||
13526 | dtrace_sync(); | |
13527 | ||
13528 | now = dtrace_gethrtime(); | |
13529 | ||
13530 | if (state != dtrace_anon.dta_state && | |
13531 | now - state->dts_laststatus >= dtrace_deadman_user) | |
13532 | return; | |
13533 | ||
13534 | /* | |
13535 | * We must be sure that dts_alive never appears to be less than the | |
13536 | * value upon entry to dtrace_state_deadman(), and because we lack a | |
13537 | * dtrace_cas64(), we cannot store to it atomically. We thus instead | |
13538 | * store INT64_MAX to it, followed by a memory barrier, followed by | |
13539 | * the new value. This assures that dts_alive never appears to be | |
13540 | * less than its true value, regardless of the order in which the | |
13541 | * stores to the underlying storage are issued. | |
13542 | */ | |
13543 | state->dts_alive = INT64_MAX; | |
13544 | dtrace_membar_producer(); | |
13545 | state->dts_alive = now; | |
13546 | } | |
13547 | ||
b0d623f7 | 13548 | #if !defined(__APPLE__) |
2d21ac55 A |
13549 | dtrace_state_t * |
13550 | dtrace_state_create(dev_t *devp, cred_t *cr) | |
b0d623f7 A |
13551 | #else |
13552 | static int | |
13553 | dtrace_state_create(dev_t *devp, cred_t *cr, dtrace_state_t **new_state) | |
13554 | #endif /* __APPLE__ */ | |
2d21ac55 A |
13555 | { |
13556 | minor_t minor; | |
13557 | major_t major; | |
13558 | char c[30]; | |
13559 | dtrace_state_t *state; | |
13560 | dtrace_optval_t *opt; | |
c910b4d9 | 13561 | int bufsize = (int)NCPU * sizeof (dtrace_buffer_t), i; |
2d21ac55 A |
13562 | |
13563 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
13564 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
13565 | ||
13566 | #if !defined(__APPLE__) | |
13567 | minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, | |
13568 | VM_BESTFIT | VM_SLEEP); | |
b0d623f7 A |
13569 | |
13570 | if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { | |
13571 | vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); | |
13572 | return (NULL); | |
13573 | } | |
2d21ac55 | 13574 | #else |
b0d623f7 A |
13575 | /* Cause restart */ |
13576 | *new_state = NULL; | |
13577 | ||
2d21ac55 A |
13578 | /* |
13579 | * Darwin's DEVFS layer acquired the minor number for this "device" when it called | |
13580 | * dtrace_devfs_clone_func(). At that time, dtrace_devfs_clone_func() proposed a minor number | |
13581 | * (next unused according to vmem_alloc()) and then immediately put the number back in play | |
13582 | * (by calling vmem_free()). Now that minor number is being used for an open, so committing it | |
b0d623f7 | 13583 | * to use. The following vmem_alloc() must deliver that same minor number. FIXME. |
2d21ac55 A |
13584 | */ |
13585 | ||
13586 | minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, | |
13587 | VM_BESTFIT | VM_SLEEP); | |
13588 | ||
13589 | if (NULL != devp) { | |
13590 | ASSERT(getminor(*devp) == minor); | |
13591 | if (getminor(*devp) != minor) { | |
13592 | printf("dtrace_open: couldn't re-acquire vended minor number %d. Instead got %d\n", | |
13593 | getminor(*devp), minor); | |
13594 | vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); | |
b0d623f7 | 13595 | return (ERESTART); /* can't reacquire */ |
2d21ac55 A |
13596 | } |
13597 | } else { | |
13598 | /* NULL==devp iff "Anonymous state" (see dtrace_anon_property), | |
13599 | * so just vend the minor device number here de novo since no "open" has occurred. */ | |
13600 | } | |
13601 | ||
2d21ac55 A |
13602 | if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { |
13603 | vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); | |
b0d623f7 | 13604 | return (EAGAIN); /* temporary resource shortage */ |
2d21ac55 A |
13605 | } |
13606 | ||
b0d623f7 A |
13607 | #endif /* __APPLE__ */ |
13608 | ||
2d21ac55 A |
13609 | state = ddi_get_soft_state(dtrace_softstate, minor); |
13610 | state->dts_epid = DTRACE_EPIDNONE + 1; | |
13611 | ||
13612 | (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor); | |
13613 | state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, | |
13614 | NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); | |
13615 | ||
13616 | if (devp != NULL) { | |
13617 | major = getemajor(*devp); | |
13618 | } else { | |
13619 | major = ddi_driver_major(dtrace_devi); | |
13620 | } | |
13621 | ||
13622 | state->dts_dev = makedevice(major, minor); | |
13623 | ||
13624 | if (devp != NULL) | |
13625 | *devp = state->dts_dev; | |
13626 | ||
13627 | /* | |
13628 | * We allocate NCPU buffers. On the one hand, this can be quite | |
13629 | * a bit of memory per instance (nearly 36K on a Starcat). On the | |
13630 | * other hand, it saves an additional memory reference in the probe | |
13631 | * path. | |
13632 | */ | |
13633 | state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); | |
13634 | state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); | |
13635 | state->dts_cleaner = CYCLIC_NONE; | |
13636 | state->dts_deadman = CYCLIC_NONE; | |
13637 | state->dts_vstate.dtvs_state = state; | |
13638 | ||
13639 | for (i = 0; i < DTRACEOPT_MAX; i++) | |
13640 | state->dts_options[i] = DTRACEOPT_UNSET; | |
13641 | ||
13642 | /* | |
13643 | * Set the default options. | |
13644 | */ | |
13645 | opt = state->dts_options; | |
13646 | opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; | |
13647 | opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; | |
13648 | opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; | |
13649 | opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; | |
13650 | opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; | |
13651 | opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; | |
13652 | opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; | |
13653 | opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; | |
13654 | opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; | |
13655 | opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; | |
13656 | opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; | |
13657 | opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; | |
13658 | opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; | |
13659 | opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; | |
13660 | ||
13661 | state->dts_activity = DTRACE_ACTIVITY_INACTIVE; | |
13662 | ||
13663 | /* | |
13664 | * Depending on the user credentials, we set flag bits which alter probe | |
13665 | * visibility or the amount of destructiveness allowed. In the case of | |
13666 | * actual anonymous tracing, or the possession of all privileges, all of | |
13667 | * the normal checks are bypassed. | |
13668 | */ | |
13669 | if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { | |
13670 | state->dts_cred.dcr_visible = DTRACE_CRV_ALL; | |
13671 | state->dts_cred.dcr_action = DTRACE_CRA_ALL; | |
13672 | } else { | |
13673 | /* | |
13674 | * Set up the credentials for this instantiation. We take a | |
13675 | * hold on the credential to prevent it from disappearing on | |
13676 | * us; this in turn prevents the zone_t referenced by this | |
13677 | * credential from disappearing. This means that we can | |
13678 | * examine the credential and the zone from probe context. | |
13679 | */ | |
13680 | crhold(cr); | |
13681 | state->dts_cred.dcr_cred = cr; | |
13682 | ||
13683 | /* | |
13684 | * CRA_PROC means "we have *some* privilege for dtrace" and | |
13685 | * unlocks the use of variables like pid, zonename, etc. | |
13686 | */ | |
13687 | if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || | |
13688 | PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { | |
13689 | state->dts_cred.dcr_action |= DTRACE_CRA_PROC; | |
13690 | } | |
13691 | ||
13692 | /* | |
13693 | * dtrace_user allows use of syscall and profile providers. | |
13694 | * If the user also has proc_owner and/or proc_zone, we | |
13695 | * extend the scope to include additional visibility and | |
13696 | * destructive power. | |
13697 | */ | |
13698 | if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { | |
13699 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { | |
13700 | state->dts_cred.dcr_visible |= | |
13701 | DTRACE_CRV_ALLPROC; | |
13702 | ||
13703 | state->dts_cred.dcr_action |= | |
13704 | DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; | |
13705 | } | |
13706 | ||
13707 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { | |
13708 | state->dts_cred.dcr_visible |= | |
13709 | DTRACE_CRV_ALLZONE; | |
13710 | ||
13711 | state->dts_cred.dcr_action |= | |
13712 | DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; | |
13713 | } | |
13714 | ||
13715 | /* | |
13716 | * If we have all privs in whatever zone this is, | |
13717 | * we can do destructive things to processes which | |
13718 | * have altered credentials. | |
13719 | */ | |
13720 | #if !defined(__APPLE__) | |
13721 | if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), | |
13722 | cr->cr_zone->zone_privset)) { | |
13723 | state->dts_cred.dcr_action |= | |
13724 | DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; | |
13725 | } | |
13726 | #else | |
13727 | /* Darwin doesn't do zones. */ | |
13728 | state->dts_cred.dcr_action |= | |
13729 | DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; | |
13730 | #endif /* __APPLE__ */ | |
13731 | } | |
13732 | ||
13733 | /* | |
13734 | * Holding the dtrace_kernel privilege also implies that | |
13735 | * the user has the dtrace_user privilege from a visibility | |
13736 | * perspective. But without further privileges, some | |
13737 | * destructive actions are not available. | |
13738 | */ | |
13739 | if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { | |
13740 | /* | |
13741 | * Make all probes in all zones visible. However, | |
13742 | * this doesn't mean that all actions become available | |
13743 | * to all zones. | |
13744 | */ | |
13745 | state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | | |
13746 | DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; | |
13747 | ||
13748 | state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | | |
13749 | DTRACE_CRA_PROC; | |
13750 | /* | |
13751 | * Holding proc_owner means that destructive actions | |
13752 | * for *this* zone are allowed. | |
13753 | */ | |
13754 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) | |
13755 | state->dts_cred.dcr_action |= | |
13756 | DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; | |
13757 | ||
13758 | /* | |
13759 | * Holding proc_zone means that destructive actions | |
13760 | * for this user/group ID in all zones is allowed. | |
13761 | */ | |
13762 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) | |
13763 | state->dts_cred.dcr_action |= | |
13764 | DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; | |
13765 | ||
13766 | /* | |
13767 | * If we have all privs in whatever zone this is, | |
13768 | * we can do destructive things to processes which | |
13769 | * have altered credentials. | |
13770 | */ | |
13771 | #if !defined(__APPLE__) | |
13772 | if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), | |
13773 | cr->cr_zone->zone_privset)) { | |
13774 | state->dts_cred.dcr_action |= | |
13775 | DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; | |
13776 | } | |
13777 | #else | |
13778 | /* Darwin doesn't do zones. */ | |
13779 | state->dts_cred.dcr_action |= | |
13780 | DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; | |
13781 | #endif /* __APPLE__ */ | |
13782 | } | |
13783 | ||
13784 | /* | |
13785 | * Holding the dtrace_proc privilege gives control over fasttrap | |
13786 | * and pid providers. We need to grant wider destructive | |
13787 | * privileges in the event that the user has proc_owner and/or | |
13788 | * proc_zone. | |
13789 | */ | |
13790 | if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { | |
13791 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) | |
13792 | state->dts_cred.dcr_action |= | |
13793 | DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; | |
13794 | ||
13795 | if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) | |
13796 | state->dts_cred.dcr_action |= | |
13797 | DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; | |
13798 | } | |
13799 | } | |
13800 | ||
b0d623f7 | 13801 | #if !defined(__APPLE__) |
2d21ac55 | 13802 | return (state); |
b0d623f7 A |
13803 | #else |
13804 | *new_state = state; | |
13805 | return(0); /* Success */ | |
13806 | #endif /* __APPLE__ */ | |
2d21ac55 A |
13807 | } |
13808 | ||
13809 | static int | |
13810 | dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) | |
13811 | { | |
13812 | dtrace_optval_t *opt = state->dts_options, size; | |
c910b4d9 | 13813 | processorid_t cpu = 0; |
2d21ac55 A |
13814 | int flags = 0, rval; |
13815 | ||
13816 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
13817 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
13818 | ASSERT(which < DTRACEOPT_MAX); | |
13819 | ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || | |
13820 | (state == dtrace_anon.dta_state && | |
13821 | state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); | |
13822 | ||
13823 | if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) | |
13824 | return (0); | |
13825 | ||
13826 | if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) | |
13827 | cpu = opt[DTRACEOPT_CPU]; | |
13828 | ||
13829 | if (which == DTRACEOPT_SPECSIZE) | |
13830 | flags |= DTRACEBUF_NOSWITCH; | |
13831 | ||
13832 | if (which == DTRACEOPT_BUFSIZE) { | |
13833 | if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) | |
13834 | flags |= DTRACEBUF_RING; | |
13835 | ||
13836 | if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) | |
13837 | flags |= DTRACEBUF_FILL; | |
13838 | ||
13839 | if (state != dtrace_anon.dta_state || | |
13840 | state->dts_activity != DTRACE_ACTIVITY_ACTIVE) | |
13841 | flags |= DTRACEBUF_INACTIVE; | |
13842 | } | |
13843 | ||
b0d623f7 | 13844 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 13845 | for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { |
b0d623f7 A |
13846 | #else |
13847 | for (size = opt[which]; (size_t)size >= sizeof (uint64_t); size >>= 1) { | |
13848 | #endif /* __APPLE__ */ | |
2d21ac55 A |
13849 | /* |
13850 | * The size must be 8-byte aligned. If the size is not 8-byte | |
13851 | * aligned, drop it down by the difference. | |
13852 | */ | |
13853 | if (size & (sizeof (uint64_t) - 1)) | |
13854 | size -= size & (sizeof (uint64_t) - 1); | |
13855 | ||
13856 | if (size < state->dts_reserve) { | |
13857 | /* | |
13858 | * Buffers always must be large enough to accommodate | |
13859 | * their prereserved space. We return E2BIG instead | |
13860 | * of ENOMEM in this case to allow for user-level | |
13861 | * software to differentiate the cases. | |
13862 | */ | |
13863 | return (E2BIG); | |
13864 | } | |
13865 | ||
13866 | rval = dtrace_buffer_alloc(buf, size, flags, cpu); | |
13867 | ||
13868 | if (rval != ENOMEM) { | |
13869 | opt[which] = size; | |
13870 | return (rval); | |
13871 | } | |
13872 | ||
13873 | if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) | |
13874 | return (rval); | |
13875 | } | |
13876 | ||
13877 | return (ENOMEM); | |
13878 | } | |
13879 | ||
13880 | static int | |
13881 | dtrace_state_buffers(dtrace_state_t *state) | |
13882 | { | |
13883 | dtrace_speculation_t *spec = state->dts_speculations; | |
13884 | int rval, i; | |
13885 | ||
13886 | if ((rval = dtrace_state_buffer(state, state->dts_buffer, | |
13887 | DTRACEOPT_BUFSIZE)) != 0) | |
13888 | return (rval); | |
13889 | ||
13890 | if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, | |
13891 | DTRACEOPT_AGGSIZE)) != 0) | |
13892 | return (rval); | |
13893 | ||
13894 | for (i = 0; i < state->dts_nspeculations; i++) { | |
13895 | if ((rval = dtrace_state_buffer(state, | |
13896 | spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) | |
13897 | return (rval); | |
13898 | } | |
13899 | ||
13900 | return (0); | |
13901 | } | |
13902 | ||
13903 | static void | |
13904 | dtrace_state_prereserve(dtrace_state_t *state) | |
13905 | { | |
13906 | dtrace_ecb_t *ecb; | |
13907 | dtrace_probe_t *probe; | |
13908 | ||
13909 | state->dts_reserve = 0; | |
13910 | ||
13911 | if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) | |
13912 | return; | |
13913 | ||
13914 | /* | |
13915 | * If our buffer policy is a "fill" buffer policy, we need to set the | |
13916 | * prereserved space to be the space required by the END probes. | |
13917 | */ | |
13918 | probe = dtrace_probes[dtrace_probeid_end - 1]; | |
13919 | ASSERT(probe != NULL); | |
13920 | ||
13921 | for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { | |
13922 | if (ecb->dte_state != state) | |
13923 | continue; | |
13924 | ||
13925 | state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; | |
13926 | } | |
13927 | } | |
13928 | ||
13929 | static int | |
13930 | dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) | |
13931 | { | |
13932 | dtrace_optval_t *opt = state->dts_options, sz, nspec; | |
13933 | dtrace_speculation_t *spec; | |
13934 | dtrace_buffer_t *buf; | |
13935 | cyc_handler_t hdlr; | |
13936 | cyc_time_t when; | |
c910b4d9 | 13937 | int rval = 0, i, bufsize = (int)NCPU * sizeof (dtrace_buffer_t); |
2d21ac55 A |
13938 | dtrace_icookie_t cookie; |
13939 | ||
13940 | lck_mtx_lock(&cpu_lock); | |
13941 | lck_mtx_lock(&dtrace_lock); | |
13942 | ||
13943 | if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { | |
13944 | rval = EBUSY; | |
13945 | goto out; | |
13946 | } | |
13947 | ||
13948 | /* | |
13949 | * Before we can perform any checks, we must prime all of the | |
13950 | * retained enablings that correspond to this state. | |
13951 | */ | |
13952 | dtrace_enabling_prime(state); | |
13953 | ||
13954 | if (state->dts_destructive && !state->dts_cred.dcr_destructive) { | |
13955 | rval = EACCES; | |
13956 | goto out; | |
13957 | } | |
13958 | ||
13959 | dtrace_state_prereserve(state); | |
13960 | ||
13961 | /* | |
13962 | * Now we want to do is try to allocate our speculations. | |
13963 | * We do not automatically resize the number of speculations; if | |
13964 | * this fails, we will fail the operation. | |
13965 | */ | |
13966 | nspec = opt[DTRACEOPT_NSPEC]; | |
13967 | ASSERT(nspec != DTRACEOPT_UNSET); | |
13968 | ||
13969 | if (nspec > INT_MAX) { | |
13970 | rval = ENOMEM; | |
13971 | goto out; | |
13972 | } | |
13973 | ||
13974 | spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); | |
13975 | ||
13976 | if (spec == NULL) { | |
13977 | rval = ENOMEM; | |
13978 | goto out; | |
13979 | } | |
13980 | ||
13981 | state->dts_speculations = spec; | |
13982 | state->dts_nspeculations = (int)nspec; | |
13983 | ||
13984 | for (i = 0; i < nspec; i++) { | |
13985 | if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { | |
13986 | rval = ENOMEM; | |
13987 | goto err; | |
13988 | } | |
13989 | ||
13990 | spec[i].dtsp_buffer = buf; | |
13991 | } | |
13992 | ||
13993 | if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { | |
13994 | if (dtrace_anon.dta_state == NULL) { | |
13995 | rval = ENOENT; | |
13996 | goto out; | |
13997 | } | |
13998 | ||
13999 | if (state->dts_necbs != 0) { | |
14000 | rval = EALREADY; | |
14001 | goto out; | |
14002 | } | |
14003 | ||
14004 | state->dts_anon = dtrace_anon_grab(); | |
14005 | ASSERT(state->dts_anon != NULL); | |
14006 | state = state->dts_anon; | |
14007 | ||
14008 | /* | |
14009 | * We want "grabanon" to be set in the grabbed state, so we'll | |
14010 | * copy that option value from the grabbing state into the | |
14011 | * grabbed state. | |
14012 | */ | |
14013 | state->dts_options[DTRACEOPT_GRABANON] = | |
14014 | opt[DTRACEOPT_GRABANON]; | |
14015 | ||
14016 | *cpu = dtrace_anon.dta_beganon; | |
14017 | ||
14018 | /* | |
14019 | * If the anonymous state is active (as it almost certainly | |
14020 | * is if the anonymous enabling ultimately matched anything), | |
14021 | * we don't allow any further option processing -- but we | |
14022 | * don't return failure. | |
14023 | */ | |
14024 | if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) | |
14025 | goto out; | |
14026 | } | |
14027 | ||
14028 | if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && | |
14029 | opt[DTRACEOPT_AGGSIZE] != 0) { | |
14030 | if (state->dts_aggregations == NULL) { | |
14031 | /* | |
14032 | * We're not going to create an aggregation buffer | |
14033 | * because we don't have any ECBs that contain | |
14034 | * aggregations -- set this option to 0. | |
14035 | */ | |
14036 | opt[DTRACEOPT_AGGSIZE] = 0; | |
14037 | } else { | |
14038 | /* | |
14039 | * If we have an aggregation buffer, we must also have | |
14040 | * a buffer to use as scratch. | |
14041 | */ | |
b0d623f7 | 14042 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 A |
14043 | if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || |
14044 | opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { | |
14045 | opt[DTRACEOPT_BUFSIZE] = state->dts_needed; | |
14046 | } | |
b0d623f7 A |
14047 | #else |
14048 | if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || | |
14049 | (size_t)opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { | |
14050 | opt[DTRACEOPT_BUFSIZE] = state->dts_needed; | |
14051 | } | |
14052 | #endif /* __APPLE__ */ | |
2d21ac55 A |
14053 | } |
14054 | } | |
14055 | ||
14056 | if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && | |
14057 | opt[DTRACEOPT_SPECSIZE] != 0) { | |
14058 | if (!state->dts_speculates) { | |
14059 | /* | |
14060 | * We're not going to create speculation buffers | |
14061 | * because we don't have any ECBs that actually | |
14062 | * speculate -- set the speculation size to 0. | |
14063 | */ | |
14064 | opt[DTRACEOPT_SPECSIZE] = 0; | |
14065 | } | |
14066 | } | |
14067 | ||
14068 | /* | |
14069 | * The bare minimum size for any buffer that we're actually going to | |
14070 | * do anything to is sizeof (uint64_t). | |
14071 | */ | |
14072 | sz = sizeof (uint64_t); | |
14073 | ||
14074 | if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || | |
14075 | (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || | |
14076 | (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { | |
14077 | /* | |
14078 | * A buffer size has been explicitly set to 0 (or to a size | |
14079 | * that will be adjusted to 0) and we need the space -- we | |
14080 | * need to return failure. We return ENOSPC to differentiate | |
14081 | * it from failing to allocate a buffer due to failure to meet | |
14082 | * the reserve (for which we return E2BIG). | |
14083 | */ | |
14084 | rval = ENOSPC; | |
14085 | goto out; | |
14086 | } | |
14087 | ||
14088 | if ((rval = dtrace_state_buffers(state)) != 0) | |
14089 | goto err; | |
14090 | ||
14091 | if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) | |
14092 | sz = dtrace_dstate_defsize; | |
14093 | ||
14094 | do { | |
14095 | rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); | |
14096 | ||
14097 | if (rval == 0) | |
14098 | break; | |
14099 | ||
14100 | if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) | |
14101 | goto err; | |
14102 | } while (sz >>= 1); | |
14103 | ||
14104 | opt[DTRACEOPT_DYNVARSIZE] = sz; | |
14105 | ||
14106 | if (rval != 0) | |
14107 | goto err; | |
14108 | ||
14109 | if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) | |
14110 | opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; | |
14111 | ||
14112 | if (opt[DTRACEOPT_CLEANRATE] == 0) | |
14113 | opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; | |
14114 | ||
14115 | if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) | |
14116 | opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; | |
14117 | ||
14118 | if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) | |
14119 | opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; | |
14120 | ||
14121 | hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; | |
14122 | hdlr.cyh_arg = state; | |
14123 | hdlr.cyh_level = CY_LOW_LEVEL; | |
14124 | ||
14125 | when.cyt_when = 0; | |
14126 | when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; | |
14127 | ||
14128 | state->dts_cleaner = cyclic_add(&hdlr, &when); | |
14129 | ||
14130 | hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; | |
14131 | hdlr.cyh_arg = state; | |
14132 | hdlr.cyh_level = CY_LOW_LEVEL; | |
14133 | ||
14134 | when.cyt_when = 0; | |
14135 | when.cyt_interval = dtrace_deadman_interval; | |
14136 | ||
14137 | state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); | |
14138 | state->dts_deadman = cyclic_add(&hdlr, &when); | |
14139 | ||
14140 | state->dts_activity = DTRACE_ACTIVITY_WARMUP; | |
14141 | ||
14142 | /* | |
14143 | * Now it's time to actually fire the BEGIN probe. We need to disable | |
14144 | * interrupts here both to record the CPU on which we fired the BEGIN | |
14145 | * probe (the data from this CPU will be processed first at user | |
14146 | * level) and to manually activate the buffer for this CPU. | |
14147 | */ | |
14148 | cookie = dtrace_interrupt_disable(); | |
14149 | *cpu = CPU->cpu_id; | |
14150 | ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); | |
14151 | state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; | |
14152 | ||
14153 | dtrace_probe(dtrace_probeid_begin, | |
14154 | (uint64_t)(uintptr_t)state, 0, 0, 0, 0); | |
14155 | dtrace_interrupt_enable(cookie); | |
14156 | /* | |
14157 | * We may have had an exit action from a BEGIN probe; only change our | |
14158 | * state to ACTIVE if we're still in WARMUP. | |
14159 | */ | |
14160 | ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || | |
14161 | state->dts_activity == DTRACE_ACTIVITY_DRAINING); | |
14162 | ||
14163 | if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) | |
14164 | state->dts_activity = DTRACE_ACTIVITY_ACTIVE; | |
14165 | ||
14166 | /* | |
14167 | * Regardless of whether or not now we're in ACTIVE or DRAINING, we | |
14168 | * want each CPU to transition its principal buffer out of the | |
14169 | * INACTIVE state. Doing this assures that no CPU will suddenly begin | |
14170 | * processing an ECB halfway down a probe's ECB chain; all CPUs will | |
14171 | * atomically transition from processing none of a state's ECBs to | |
14172 | * processing all of them. | |
14173 | */ | |
14174 | dtrace_xcall(DTRACE_CPUALL, | |
14175 | (dtrace_xcall_t)dtrace_buffer_activate, state); | |
14176 | goto out; | |
14177 | ||
14178 | err: | |
14179 | dtrace_buffer_free(state->dts_buffer); | |
14180 | dtrace_buffer_free(state->dts_aggbuffer); | |
14181 | ||
14182 | if ((nspec = state->dts_nspeculations) == 0) { | |
14183 | ASSERT(state->dts_speculations == NULL); | |
14184 | goto out; | |
14185 | } | |
14186 | ||
14187 | spec = state->dts_speculations; | |
14188 | ASSERT(spec != NULL); | |
14189 | ||
14190 | for (i = 0; i < state->dts_nspeculations; i++) { | |
14191 | if ((buf = spec[i].dtsp_buffer) == NULL) | |
14192 | break; | |
14193 | ||
14194 | dtrace_buffer_free(buf); | |
14195 | kmem_free(buf, bufsize); | |
14196 | } | |
14197 | ||
14198 | kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); | |
14199 | state->dts_nspeculations = 0; | |
14200 | state->dts_speculations = NULL; | |
14201 | ||
14202 | out: | |
14203 | lck_mtx_unlock(&dtrace_lock); | |
14204 | lck_mtx_unlock(&cpu_lock); | |
14205 | ||
14206 | return (rval); | |
14207 | } | |
14208 | ||
14209 | static int | |
14210 | dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) | |
14211 | { | |
14212 | dtrace_icookie_t cookie; | |
14213 | ||
14214 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
14215 | ||
14216 | if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && | |
14217 | state->dts_activity != DTRACE_ACTIVITY_DRAINING) | |
14218 | return (EINVAL); | |
14219 | ||
14220 | /* | |
14221 | * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync | |
14222 | * to be sure that every CPU has seen it. See below for the details | |
14223 | * on why this is done. | |
14224 | */ | |
14225 | state->dts_activity = DTRACE_ACTIVITY_DRAINING; | |
14226 | dtrace_sync(); | |
14227 | ||
14228 | /* | |
14229 | * By this point, it is impossible for any CPU to be still processing | |
14230 | * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to | |
14231 | * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any | |
14232 | * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() | |
14233 | * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN | |
14234 | * iff we're in the END probe. | |
14235 | */ | |
14236 | state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; | |
14237 | dtrace_sync(); | |
14238 | ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); | |
14239 | ||
14240 | /* | |
14241 | * Finally, we can release the reserve and call the END probe. We | |
14242 | * disable interrupts across calling the END probe to allow us to | |
14243 | * return the CPU on which we actually called the END probe. This | |
14244 | * allows user-land to be sure that this CPU's principal buffer is | |
14245 | * processed last. | |
14246 | */ | |
14247 | state->dts_reserve = 0; | |
14248 | ||
14249 | cookie = dtrace_interrupt_disable(); | |
14250 | *cpu = CPU->cpu_id; | |
14251 | dtrace_probe(dtrace_probeid_end, | |
14252 | (uint64_t)(uintptr_t)state, 0, 0, 0, 0); | |
14253 | dtrace_interrupt_enable(cookie); | |
14254 | ||
14255 | state->dts_activity = DTRACE_ACTIVITY_STOPPED; | |
14256 | dtrace_sync(); | |
14257 | ||
14258 | return (0); | |
14259 | } | |
14260 | ||
14261 | static int | |
14262 | dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, | |
14263 | dtrace_optval_t val) | |
14264 | { | |
14265 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
14266 | ||
14267 | if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) | |
14268 | return (EBUSY); | |
14269 | ||
14270 | if (option >= DTRACEOPT_MAX) | |
14271 | return (EINVAL); | |
14272 | ||
14273 | if (option != DTRACEOPT_CPU && val < 0) | |
14274 | return (EINVAL); | |
14275 | ||
14276 | switch (option) { | |
14277 | case DTRACEOPT_DESTRUCTIVE: | |
14278 | if (dtrace_destructive_disallow) | |
14279 | return (EACCES); | |
14280 | ||
14281 | state->dts_cred.dcr_destructive = 1; | |
14282 | break; | |
14283 | ||
14284 | case DTRACEOPT_BUFSIZE: | |
14285 | case DTRACEOPT_DYNVARSIZE: | |
14286 | case DTRACEOPT_AGGSIZE: | |
14287 | case DTRACEOPT_SPECSIZE: | |
14288 | case DTRACEOPT_STRSIZE: | |
14289 | if (val < 0) | |
14290 | return (EINVAL); | |
14291 | ||
14292 | if (val >= LONG_MAX) { | |
14293 | /* | |
14294 | * If this is an otherwise negative value, set it to | |
14295 | * the highest multiple of 128m less than LONG_MAX. | |
14296 | * Technically, we're adjusting the size without | |
14297 | * regard to the buffer resizing policy, but in fact, | |
14298 | * this has no effect -- if we set the buffer size to | |
14299 | * ~LONG_MAX and the buffer policy is ultimately set to | |
14300 | * be "manual", the buffer allocation is guaranteed to | |
14301 | * fail, if only because the allocation requires two | |
14302 | * buffers. (We set the the size to the highest | |
14303 | * multiple of 128m because it ensures that the size | |
14304 | * will remain a multiple of a megabyte when | |
14305 | * repeatedly halved -- all the way down to 15m.) | |
14306 | */ | |
14307 | val = LONG_MAX - (1 << 27) + 1; | |
14308 | } | |
14309 | } | |
14310 | ||
14311 | state->dts_options[option] = val; | |
14312 | ||
14313 | return (0); | |
14314 | } | |
14315 | ||
14316 | static void | |
14317 | dtrace_state_destroy(dtrace_state_t *state) | |
14318 | { | |
14319 | dtrace_ecb_t *ecb; | |
14320 | dtrace_vstate_t *vstate = &state->dts_vstate; | |
14321 | minor_t minor = getminor(state->dts_dev); | |
c910b4d9 | 14322 | int i, bufsize = (int)NCPU * sizeof (dtrace_buffer_t); |
2d21ac55 A |
14323 | dtrace_speculation_t *spec = state->dts_speculations; |
14324 | int nspec = state->dts_nspeculations; | |
14325 | uint32_t match; | |
14326 | ||
14327 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
14328 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
14329 | ||
14330 | /* | |
14331 | * First, retract any retained enablings for this state. | |
14332 | */ | |
14333 | dtrace_enabling_retract(state); | |
14334 | ASSERT(state->dts_nretained == 0); | |
14335 | ||
14336 | if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || | |
14337 | state->dts_activity == DTRACE_ACTIVITY_DRAINING) { | |
14338 | /* | |
14339 | * We have managed to come into dtrace_state_destroy() on a | |
14340 | * hot enabling -- almost certainly because of a disorderly | |
14341 | * shutdown of a consumer. (That is, a consumer that is | |
14342 | * exiting without having called dtrace_stop().) In this case, | |
14343 | * we're going to set our activity to be KILLED, and then | |
14344 | * issue a sync to be sure that everyone is out of probe | |
14345 | * context before we start blowing away ECBs. | |
14346 | */ | |
14347 | state->dts_activity = DTRACE_ACTIVITY_KILLED; | |
14348 | dtrace_sync(); | |
14349 | } | |
14350 | ||
14351 | /* | |
14352 | * Release the credential hold we took in dtrace_state_create(). | |
14353 | */ | |
14354 | if (state->dts_cred.dcr_cred != NULL) | |
14355 | crfree(state->dts_cred.dcr_cred); | |
14356 | ||
14357 | /* | |
14358 | * Now we can safely disable and destroy any enabled probes. Because | |
14359 | * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress | |
14360 | * (especially if they're all enabled), we take two passes through the | |
14361 | * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and | |
14362 | * in the second we disable whatever is left over. | |
14363 | */ | |
14364 | for (match = DTRACE_PRIV_KERNEL; ; match = 0) { | |
14365 | for (i = 0; i < state->dts_necbs; i++) { | |
14366 | if ((ecb = state->dts_ecbs[i]) == NULL) | |
14367 | continue; | |
14368 | ||
14369 | if (match && ecb->dte_probe != NULL) { | |
14370 | dtrace_probe_t *probe = ecb->dte_probe; | |
14371 | dtrace_provider_t *prov = probe->dtpr_provider; | |
14372 | ||
14373 | if (!(prov->dtpv_priv.dtpp_flags & match)) | |
14374 | continue; | |
14375 | } | |
14376 | ||
14377 | dtrace_ecb_disable(ecb); | |
14378 | dtrace_ecb_destroy(ecb); | |
14379 | } | |
14380 | ||
14381 | if (!match) | |
14382 | break; | |
14383 | } | |
14384 | ||
14385 | /* | |
14386 | * Before we free the buffers, perform one more sync to assure that | |
14387 | * every CPU is out of probe context. | |
14388 | */ | |
14389 | dtrace_sync(); | |
14390 | ||
14391 | dtrace_buffer_free(state->dts_buffer); | |
14392 | dtrace_buffer_free(state->dts_aggbuffer); | |
14393 | ||
14394 | for (i = 0; i < nspec; i++) | |
14395 | dtrace_buffer_free(spec[i].dtsp_buffer); | |
14396 | ||
14397 | if (state->dts_cleaner != CYCLIC_NONE) | |
14398 | cyclic_remove(state->dts_cleaner); | |
14399 | ||
14400 | if (state->dts_deadman != CYCLIC_NONE) | |
14401 | cyclic_remove(state->dts_deadman); | |
14402 | ||
14403 | dtrace_dstate_fini(&vstate->dtvs_dynvars); | |
14404 | dtrace_vstate_fini(vstate); | |
14405 | kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); | |
14406 | ||
14407 | if (state->dts_aggregations != NULL) { | |
b0d623f7 | 14408 | #if DEBUG |
2d21ac55 A |
14409 | for (i = 0; i < state->dts_naggregations; i++) |
14410 | ASSERT(state->dts_aggregations[i] == NULL); | |
14411 | #endif | |
14412 | ASSERT(state->dts_naggregations > 0); | |
14413 | kmem_free(state->dts_aggregations, | |
14414 | state->dts_naggregations * sizeof (dtrace_aggregation_t *)); | |
14415 | } | |
14416 | ||
14417 | kmem_free(state->dts_buffer, bufsize); | |
14418 | kmem_free(state->dts_aggbuffer, bufsize); | |
14419 | ||
14420 | for (i = 0; i < nspec; i++) | |
14421 | kmem_free(spec[i].dtsp_buffer, bufsize); | |
14422 | ||
14423 | kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); | |
14424 | ||
14425 | dtrace_format_destroy(state); | |
14426 | ||
14427 | vmem_destroy(state->dts_aggid_arena); | |
14428 | ddi_soft_state_free(dtrace_softstate, minor); | |
14429 | vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); | |
14430 | } | |
14431 | ||
14432 | /* | |
14433 | * DTrace Anonymous Enabling Functions | |
14434 | */ | |
14435 | static dtrace_state_t * | |
14436 | dtrace_anon_grab(void) | |
14437 | { | |
14438 | dtrace_state_t *state; | |
14439 | ||
14440 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
14441 | ||
14442 | if ((state = dtrace_anon.dta_state) == NULL) { | |
14443 | ASSERT(dtrace_anon.dta_enabling == NULL); | |
14444 | return (NULL); | |
14445 | } | |
14446 | ||
14447 | ASSERT(dtrace_anon.dta_enabling != NULL); | |
14448 | ASSERT(dtrace_retained != NULL); | |
14449 | ||
14450 | dtrace_enabling_destroy(dtrace_anon.dta_enabling); | |
14451 | dtrace_anon.dta_enabling = NULL; | |
14452 | dtrace_anon.dta_state = NULL; | |
14453 | ||
14454 | return (state); | |
14455 | } | |
14456 | ||
14457 | static void | |
14458 | dtrace_anon_property(void) | |
14459 | { | |
14460 | int i, rv; | |
14461 | dtrace_state_t *state; | |
14462 | dof_hdr_t *dof; | |
14463 | char c[32]; /* enough for "dof-data-" + digits */ | |
14464 | ||
14465 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
14466 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
14467 | ||
14468 | for (i = 0; ; i++) { | |
14469 | (void) snprintf(c, sizeof (c), "dof-data-%d", i); | |
14470 | ||
14471 | dtrace_err_verbose = 1; | |
14472 | ||
14473 | if ((dof = dtrace_dof_property(c)) == NULL) { | |
14474 | dtrace_err_verbose = 0; | |
14475 | break; | |
14476 | } | |
14477 | ||
14478 | /* | |
14479 | * We want to create anonymous state, so we need to transition | |
14480 | * the kernel debugger to indicate that DTrace is active. If | |
14481 | * this fails (e.g. because the debugger has modified text in | |
14482 | * some way), we won't continue with the processing. | |
14483 | */ | |
14484 | if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { | |
14485 | cmn_err(CE_NOTE, "kernel debugger active; anonymous " | |
14486 | "enabling ignored."); | |
14487 | dtrace_dof_destroy(dof); | |
14488 | break; | |
14489 | } | |
14490 | ||
14491 | /* | |
14492 | * If we haven't allocated an anonymous state, we'll do so now. | |
14493 | */ | |
14494 | if ((state = dtrace_anon.dta_state) == NULL) { | |
b0d623f7 | 14495 | #if !defined(__APPLE__) |
2d21ac55 A |
14496 | state = dtrace_state_create(NULL, NULL); |
14497 | dtrace_anon.dta_state = state; | |
2d21ac55 | 14498 | if (state == NULL) { |
b0d623f7 A |
14499 | #else |
14500 | rv = dtrace_state_create(NULL, NULL, &state); | |
14501 | dtrace_anon.dta_state = state; | |
14502 | if (rv != 0 || state == NULL) { | |
14503 | #endif /* __APPLE__ */ | |
2d21ac55 A |
14504 | /* |
14505 | * This basically shouldn't happen: the only | |
14506 | * failure mode from dtrace_state_create() is a | |
14507 | * failure of ddi_soft_state_zalloc() that | |
14508 | * itself should never happen. Still, the | |
14509 | * interface allows for a failure mode, and | |
14510 | * we want to fail as gracefully as possible: | |
14511 | * we'll emit an error message and cease | |
14512 | * processing anonymous state in this case. | |
14513 | */ | |
14514 | cmn_err(CE_WARN, "failed to create " | |
14515 | "anonymous state"); | |
14516 | dtrace_dof_destroy(dof); | |
14517 | break; | |
14518 | } | |
14519 | } | |
14520 | ||
14521 | rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), | |
14522 | &dtrace_anon.dta_enabling, 0, B_TRUE); | |
14523 | ||
14524 | if (rv == 0) | |
14525 | rv = dtrace_dof_options(dof, state); | |
14526 | ||
14527 | dtrace_err_verbose = 0; | |
14528 | dtrace_dof_destroy(dof); | |
14529 | ||
14530 | if (rv != 0) { | |
14531 | /* | |
14532 | * This is malformed DOF; chuck any anonymous state | |
14533 | * that we created. | |
14534 | */ | |
14535 | ASSERT(dtrace_anon.dta_enabling == NULL); | |
14536 | dtrace_state_destroy(state); | |
14537 | dtrace_anon.dta_state = NULL; | |
14538 | break; | |
14539 | } | |
14540 | ||
14541 | ASSERT(dtrace_anon.dta_enabling != NULL); | |
14542 | } | |
14543 | ||
14544 | if (dtrace_anon.dta_enabling != NULL) { | |
14545 | int rval; | |
14546 | ||
14547 | /* | |
14548 | * dtrace_enabling_retain() can only fail because we are | |
14549 | * trying to retain more enablings than are allowed -- but | |
14550 | * we only have one anonymous enabling, and we are guaranteed | |
14551 | * to be allowed at least one retained enabling; we assert | |
14552 | * that dtrace_enabling_retain() returns success. | |
14553 | */ | |
14554 | rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); | |
14555 | ASSERT(rval == 0); | |
14556 | ||
14557 | dtrace_enabling_dump(dtrace_anon.dta_enabling); | |
14558 | } | |
14559 | } | |
14560 | ||
14561 | /* | |
14562 | * DTrace Helper Functions | |
14563 | */ | |
14564 | static void | |
14565 | dtrace_helper_trace(dtrace_helper_action_t *helper, | |
14566 | dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) | |
14567 | { | |
b0d623f7 | 14568 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 14569 | uint32_t size, next, nnext, i; |
b0d623f7 A |
14570 | #else |
14571 | uint32_t size, next, nnext; | |
14572 | int i; | |
14573 | #endif /* __APPLE__ */ | |
2d21ac55 A |
14574 | dtrace_helptrace_t *ent; |
14575 | uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
14576 | ||
14577 | if (!dtrace_helptrace_enabled) | |
14578 | return; | |
14579 | ||
b0d623f7 | 14580 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 14581 | ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); |
b0d623f7 A |
14582 | #else |
14583 | ASSERT((uint32_t)vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); | |
14584 | #endif /* __APPLE__ */ | |
2d21ac55 A |
14585 | |
14586 | /* | |
14587 | * What would a tracing framework be without its own tracing | |
14588 | * framework? (Well, a hell of a lot simpler, for starters...) | |
14589 | */ | |
14590 | size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * | |
14591 | sizeof (uint64_t) - sizeof (uint64_t); | |
14592 | ||
14593 | /* | |
14594 | * Iterate until we can allocate a slot in the trace buffer. | |
14595 | */ | |
14596 | do { | |
14597 | next = dtrace_helptrace_next; | |
14598 | ||
14599 | if (next + size < dtrace_helptrace_bufsize) { | |
14600 | nnext = next + size; | |
14601 | } else { | |
14602 | nnext = size; | |
14603 | } | |
14604 | } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); | |
14605 | ||
14606 | /* | |
14607 | * We have our slot; fill it in. | |
14608 | */ | |
14609 | if (nnext == size) | |
14610 | next = 0; | |
14611 | ||
14612 | ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; | |
14613 | ent->dtht_helper = helper; | |
14614 | ent->dtht_where = where; | |
14615 | ent->dtht_nlocals = vstate->dtvs_nlocals; | |
14616 | ||
14617 | ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? | |
14618 | mstate->dtms_fltoffs : -1; | |
14619 | ent->dtht_fault = DTRACE_FLAGS2FLT(flags); | |
14620 | ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval; | |
14621 | ||
14622 | for (i = 0; i < vstate->dtvs_nlocals; i++) { | |
14623 | dtrace_statvar_t *svar; | |
14624 | ||
14625 | if ((svar = vstate->dtvs_locals[i]) == NULL) | |
14626 | continue; | |
14627 | ||
c910b4d9 | 14628 | ASSERT(svar->dtsv_size >= (int)NCPU * sizeof (uint64_t)); |
2d21ac55 A |
14629 | ent->dtht_locals[i] = |
14630 | ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id]; | |
14631 | } | |
14632 | } | |
14633 | ||
14634 | static uint64_t | |
14635 | dtrace_helper(int which, dtrace_mstate_t *mstate, | |
14636 | dtrace_state_t *state, uint64_t arg0, uint64_t arg1) | |
14637 | { | |
14638 | uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags; | |
14639 | uint64_t sarg0 = mstate->dtms_arg[0]; | |
14640 | uint64_t sarg1 = mstate->dtms_arg[1]; | |
c910b4d9 | 14641 | uint64_t rval = 0; |
2d21ac55 A |
14642 | dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; |
14643 | dtrace_helper_action_t *helper; | |
14644 | dtrace_vstate_t *vstate; | |
14645 | dtrace_difo_t *pred; | |
14646 | int i, trace = dtrace_helptrace_enabled; | |
14647 | ||
14648 | ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); | |
14649 | ||
14650 | if (helpers == NULL) | |
14651 | return (0); | |
14652 | ||
14653 | if ((helper = helpers->dthps_actions[which]) == NULL) | |
14654 | return (0); | |
14655 | ||
14656 | vstate = &helpers->dthps_vstate; | |
14657 | mstate->dtms_arg[0] = arg0; | |
14658 | mstate->dtms_arg[1] = arg1; | |
14659 | ||
14660 | /* | |
14661 | * Now iterate over each helper. If its predicate evaluates to 'true', | |
14662 | * we'll call the corresponding actions. Note that the below calls | |
14663 | * to dtrace_dif_emulate() may set faults in machine state. This is | |
14664 | * okay: our caller (the outer dtrace_dif_emulate()) will simply plow | |
14665 | * the stored DIF offset with its own (which is the desired behavior). | |
14666 | * Also, note the calls to dtrace_dif_emulate() may allocate scratch | |
14667 | * from machine state; this is okay, too. | |
14668 | */ | |
14669 | for (; helper != NULL; helper = helper->dtha_next) { | |
14670 | if ((pred = helper->dtha_predicate) != NULL) { | |
14671 | if (trace) | |
14672 | dtrace_helper_trace(helper, mstate, vstate, 0); | |
14673 | ||
14674 | if (!dtrace_dif_emulate(pred, mstate, vstate, state)) | |
14675 | goto next; | |
14676 | ||
14677 | if (*flags & CPU_DTRACE_FAULT) | |
14678 | goto err; | |
14679 | } | |
14680 | ||
14681 | for (i = 0; i < helper->dtha_nactions; i++) { | |
14682 | if (trace) | |
14683 | dtrace_helper_trace(helper, | |
14684 | mstate, vstate, i + 1); | |
14685 | ||
14686 | rval = dtrace_dif_emulate(helper->dtha_actions[i], | |
14687 | mstate, vstate, state); | |
14688 | ||
14689 | if (*flags & CPU_DTRACE_FAULT) | |
14690 | goto err; | |
14691 | } | |
14692 | ||
14693 | next: | |
14694 | if (trace) | |
14695 | dtrace_helper_trace(helper, mstate, vstate, | |
14696 | DTRACE_HELPTRACE_NEXT); | |
14697 | } | |
14698 | ||
14699 | if (trace) | |
14700 | dtrace_helper_trace(helper, mstate, vstate, | |
14701 | DTRACE_HELPTRACE_DONE); | |
14702 | ||
14703 | /* | |
14704 | * Restore the arg0 that we saved upon entry. | |
14705 | */ | |
14706 | mstate->dtms_arg[0] = sarg0; | |
14707 | mstate->dtms_arg[1] = sarg1; | |
14708 | ||
14709 | return (rval); | |
14710 | ||
14711 | err: | |
14712 | if (trace) | |
14713 | dtrace_helper_trace(helper, mstate, vstate, | |
14714 | DTRACE_HELPTRACE_ERR); | |
14715 | ||
14716 | /* | |
14717 | * Restore the arg0 that we saved upon entry. | |
14718 | */ | |
14719 | mstate->dtms_arg[0] = sarg0; | |
14720 | mstate->dtms_arg[1] = sarg1; | |
14721 | ||
14722 | return (NULL); | |
14723 | } | |
14724 | ||
14725 | static void | |
14726 | dtrace_helper_action_destroy(dtrace_helper_action_t *helper, | |
14727 | dtrace_vstate_t *vstate) | |
14728 | { | |
14729 | int i; | |
14730 | ||
14731 | if (helper->dtha_predicate != NULL) | |
14732 | dtrace_difo_release(helper->dtha_predicate, vstate); | |
14733 | ||
14734 | for (i = 0; i < helper->dtha_nactions; i++) { | |
14735 | ASSERT(helper->dtha_actions[i] != NULL); | |
14736 | dtrace_difo_release(helper->dtha_actions[i], vstate); | |
14737 | } | |
14738 | ||
14739 | kmem_free(helper->dtha_actions, | |
14740 | helper->dtha_nactions * sizeof (dtrace_difo_t *)); | |
14741 | kmem_free(helper, sizeof (dtrace_helper_action_t)); | |
14742 | } | |
14743 | ||
14744 | #if !defined(__APPLE__) | |
14745 | static int | |
14746 | dtrace_helper_destroygen(int gen) | |
14747 | { | |
14748 | proc_t *p = curproc; | |
14749 | #else | |
14750 | static int | |
14751 | dtrace_helper_destroygen(proc_t* p, int gen) | |
14752 | { | |
14753 | #endif | |
14754 | dtrace_helpers_t *help = p->p_dtrace_helpers; | |
14755 | dtrace_vstate_t *vstate; | |
b0d623f7 | 14756 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 14757 | int i; |
b0d623f7 A |
14758 | #else |
14759 | uint_t i; | |
14760 | #endif /* __APPLE__ */ | |
2d21ac55 A |
14761 | |
14762 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
14763 | ||
14764 | if (help == NULL || gen > help->dthps_generation) | |
14765 | return (EINVAL); | |
14766 | ||
14767 | vstate = &help->dthps_vstate; | |
14768 | ||
14769 | for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { | |
14770 | dtrace_helper_action_t *last = NULL, *h, *next; | |
14771 | ||
14772 | for (h = help->dthps_actions[i]; h != NULL; h = next) { | |
14773 | next = h->dtha_next; | |
14774 | ||
14775 | if (h->dtha_generation == gen) { | |
14776 | if (last != NULL) { | |
14777 | last->dtha_next = next; | |
14778 | } else { | |
14779 | help->dthps_actions[i] = next; | |
14780 | } | |
14781 | ||
14782 | dtrace_helper_action_destroy(h, vstate); | |
14783 | } else { | |
14784 | last = h; | |
14785 | } | |
14786 | } | |
14787 | } | |
14788 | ||
14789 | /* | |
14790 | * Interate until we've cleared out all helper providers with the | |
14791 | * given generation number. | |
14792 | */ | |
14793 | for (;;) { | |
c910b4d9 | 14794 | dtrace_helper_provider_t *prov = NULL; |
2d21ac55 A |
14795 | |
14796 | /* | |
14797 | * Look for a helper provider with the right generation. We | |
14798 | * have to start back at the beginning of the list each time | |
14799 | * because we drop dtrace_lock. It's unlikely that we'll make | |
14800 | * more than two passes. | |
14801 | */ | |
14802 | for (i = 0; i < help->dthps_nprovs; i++) { | |
14803 | prov = help->dthps_provs[i]; | |
14804 | ||
14805 | if (prov->dthp_generation == gen) | |
14806 | break; | |
14807 | } | |
14808 | ||
14809 | /* | |
14810 | * If there were no matches, we're done. | |
14811 | */ | |
14812 | if (i == help->dthps_nprovs) | |
14813 | break; | |
14814 | ||
14815 | /* | |
14816 | * Move the last helper provider into this slot. | |
14817 | */ | |
14818 | help->dthps_nprovs--; | |
14819 | help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; | |
14820 | help->dthps_provs[help->dthps_nprovs] = NULL; | |
14821 | ||
14822 | lck_mtx_unlock(&dtrace_lock); | |
14823 | ||
14824 | /* | |
14825 | * If we have a meta provider, remove this helper provider. | |
14826 | */ | |
14827 | lck_mtx_lock(&dtrace_meta_lock); | |
14828 | if (dtrace_meta_pid != NULL) { | |
14829 | ASSERT(dtrace_deferred_pid == NULL); | |
14830 | dtrace_helper_provider_remove(&prov->dthp_prov, | |
14831 | p->p_pid); | |
14832 | } | |
14833 | lck_mtx_unlock(&dtrace_meta_lock); | |
14834 | ||
14835 | dtrace_helper_provider_destroy(prov); | |
14836 | ||
14837 | lck_mtx_lock(&dtrace_lock); | |
14838 | } | |
14839 | ||
14840 | return (0); | |
14841 | } | |
14842 | ||
14843 | static int | |
14844 | dtrace_helper_validate(dtrace_helper_action_t *helper) | |
14845 | { | |
14846 | int err = 0, i; | |
14847 | dtrace_difo_t *dp; | |
14848 | ||
14849 | if ((dp = helper->dtha_predicate) != NULL) | |
14850 | err += dtrace_difo_validate_helper(dp); | |
14851 | ||
14852 | for (i = 0; i < helper->dtha_nactions; i++) | |
14853 | err += dtrace_difo_validate_helper(helper->dtha_actions[i]); | |
14854 | ||
14855 | return (err == 0); | |
14856 | } | |
14857 | ||
14858 | #if !defined(__APPLE__) | |
14859 | static int | |
14860 | dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) | |
14861 | #else | |
14862 | static int | |
14863 | dtrace_helper_action_add(proc_t* p, int which, dtrace_ecbdesc_t *ep) | |
14864 | #endif | |
14865 | { | |
14866 | dtrace_helpers_t *help; | |
14867 | dtrace_helper_action_t *helper, *last; | |
14868 | dtrace_actdesc_t *act; | |
14869 | dtrace_vstate_t *vstate; | |
14870 | dtrace_predicate_t *pred; | |
14871 | int count = 0, nactions = 0, i; | |
14872 | ||
14873 | if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) | |
14874 | return (EINVAL); | |
14875 | ||
14876 | #if !defined(__APPLE__) | |
14877 | help = curproc->p_dtrace_helpers; | |
14878 | #else | |
14879 | help = p->p_dtrace_helpers; | |
14880 | #endif | |
14881 | last = help->dthps_actions[which]; | |
14882 | vstate = &help->dthps_vstate; | |
14883 | ||
14884 | for (count = 0; last != NULL; last = last->dtha_next) { | |
14885 | count++; | |
14886 | if (last->dtha_next == NULL) | |
14887 | break; | |
14888 | } | |
14889 | ||
14890 | /* | |
14891 | * If we already have dtrace_helper_actions_max helper actions for this | |
14892 | * helper action type, we'll refuse to add a new one. | |
14893 | */ | |
14894 | if (count >= dtrace_helper_actions_max) | |
14895 | return (ENOSPC); | |
14896 | ||
14897 | helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); | |
14898 | helper->dtha_generation = help->dthps_generation; | |
14899 | ||
14900 | if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { | |
14901 | ASSERT(pred->dtp_difo != NULL); | |
14902 | dtrace_difo_hold(pred->dtp_difo); | |
14903 | helper->dtha_predicate = pred->dtp_difo; | |
14904 | } | |
14905 | ||
14906 | for (act = ep->dted_action; act != NULL; act = act->dtad_next) { | |
14907 | if (act->dtad_kind != DTRACEACT_DIFEXPR) | |
14908 | goto err; | |
14909 | ||
14910 | if (act->dtad_difo == NULL) | |
14911 | goto err; | |
14912 | ||
14913 | nactions++; | |
14914 | } | |
14915 | ||
14916 | helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * | |
14917 | (helper->dtha_nactions = nactions), KM_SLEEP); | |
14918 | ||
14919 | for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { | |
14920 | dtrace_difo_hold(act->dtad_difo); | |
14921 | helper->dtha_actions[i++] = act->dtad_difo; | |
14922 | } | |
14923 | ||
14924 | if (!dtrace_helper_validate(helper)) | |
14925 | goto err; | |
14926 | ||
14927 | if (last == NULL) { | |
14928 | help->dthps_actions[which] = helper; | |
14929 | } else { | |
14930 | last->dtha_next = helper; | |
14931 | } | |
14932 | ||
b0d623f7 | 14933 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 14934 | if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { |
b0d623f7 A |
14935 | #else |
14936 | if ((uint32_t)vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { | |
14937 | #endif /* __APPLE__ */ | |
2d21ac55 A |
14938 | dtrace_helptrace_nlocals = vstate->dtvs_nlocals; |
14939 | dtrace_helptrace_next = 0; | |
14940 | } | |
14941 | ||
14942 | return (0); | |
14943 | err: | |
14944 | dtrace_helper_action_destroy(helper, vstate); | |
14945 | return (EINVAL); | |
14946 | } | |
14947 | ||
14948 | static void | |
14949 | dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, | |
14950 | dof_helper_t *dofhp) | |
14951 | { | |
14952 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED); | |
14953 | ||
14954 | lck_mtx_lock(&dtrace_meta_lock); | |
14955 | lck_mtx_lock(&dtrace_lock); | |
14956 | ||
14957 | if (!dtrace_attached() || dtrace_meta_pid == NULL) { | |
14958 | /* | |
14959 | * If the dtrace module is loaded but not attached, or if | |
14960 | * there aren't isn't a meta provider registered to deal with | |
14961 | * these provider descriptions, we need to postpone creating | |
14962 | * the actual providers until later. | |
14963 | */ | |
14964 | ||
14965 | if (help->dthps_next == NULL && help->dthps_prev == NULL && | |
14966 | dtrace_deferred_pid != help) { | |
14967 | help->dthps_deferred = 1; | |
14968 | help->dthps_pid = p->p_pid; | |
14969 | help->dthps_next = dtrace_deferred_pid; | |
14970 | help->dthps_prev = NULL; | |
14971 | if (dtrace_deferred_pid != NULL) | |
14972 | dtrace_deferred_pid->dthps_prev = help; | |
14973 | dtrace_deferred_pid = help; | |
14974 | } | |
14975 | ||
14976 | lck_mtx_unlock(&dtrace_lock); | |
14977 | ||
14978 | } else if (dofhp != NULL) { | |
14979 | /* | |
14980 | * If the dtrace module is loaded and we have a particular | |
14981 | * helper provider description, pass that off to the | |
14982 | * meta provider. | |
14983 | */ | |
14984 | ||
14985 | lck_mtx_unlock(&dtrace_lock); | |
14986 | ||
14987 | dtrace_helper_provide(dofhp, p->p_pid); | |
14988 | ||
14989 | } else { | |
14990 | /* | |
14991 | * Otherwise, just pass all the helper provider descriptions | |
14992 | * off to the meta provider. | |
14993 | */ | |
14994 | ||
b0d623f7 | 14995 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 14996 | int i; |
b0d623f7 A |
14997 | #else |
14998 | uint_t i; | |
14999 | #endif /* __APPLE__ */ | |
2d21ac55 A |
15000 | lck_mtx_unlock(&dtrace_lock); |
15001 | ||
15002 | for (i = 0; i < help->dthps_nprovs; i++) { | |
15003 | dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, | |
15004 | p->p_pid); | |
15005 | } | |
15006 | } | |
15007 | ||
15008 | lck_mtx_unlock(&dtrace_meta_lock); | |
15009 | } | |
15010 | ||
15011 | #if !defined(__APPLE__) | |
15012 | static int | |
15013 | dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) | |
15014 | #else | |
15015 | static int | |
15016 | dtrace_helper_provider_add(proc_t* p, dof_helper_t *dofhp, int gen) | |
15017 | #endif | |
15018 | { | |
15019 | dtrace_helpers_t *help; | |
15020 | dtrace_helper_provider_t *hprov, **tmp_provs; | |
15021 | uint_t tmp_maxprovs, i; | |
15022 | ||
15023 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
15024 | ||
15025 | #if !defined(__APPLE__) | |
15026 | help = curproc->p_dtrace_helpers; | |
15027 | #else | |
15028 | help = p->p_dtrace_helpers; | |
15029 | #endif | |
15030 | ASSERT(help != NULL); | |
15031 | ||
15032 | /* | |
15033 | * If we already have dtrace_helper_providers_max helper providers, | |
15034 | * we're refuse to add a new one. | |
15035 | */ | |
15036 | if (help->dthps_nprovs >= dtrace_helper_providers_max) | |
15037 | return (ENOSPC); | |
15038 | ||
15039 | /* | |
15040 | * Check to make sure this isn't a duplicate. | |
15041 | */ | |
15042 | for (i = 0; i < help->dthps_nprovs; i++) { | |
15043 | if (dofhp->dofhp_addr == | |
15044 | help->dthps_provs[i]->dthp_prov.dofhp_addr) | |
15045 | return (EALREADY); | |
15046 | } | |
15047 | ||
15048 | hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); | |
15049 | hprov->dthp_prov = *dofhp; | |
15050 | hprov->dthp_ref = 1; | |
15051 | hprov->dthp_generation = gen; | |
15052 | ||
15053 | /* | |
15054 | * Allocate a bigger table for helper providers if it's already full. | |
15055 | */ | |
15056 | if (help->dthps_maxprovs == help->dthps_nprovs) { | |
15057 | tmp_maxprovs = help->dthps_maxprovs; | |
15058 | tmp_provs = help->dthps_provs; | |
15059 | ||
15060 | if (help->dthps_maxprovs == 0) | |
15061 | help->dthps_maxprovs = 2; | |
15062 | else | |
15063 | help->dthps_maxprovs *= 2; | |
15064 | if (help->dthps_maxprovs > dtrace_helper_providers_max) | |
15065 | help->dthps_maxprovs = dtrace_helper_providers_max; | |
15066 | ||
15067 | ASSERT(tmp_maxprovs < help->dthps_maxprovs); | |
15068 | ||
15069 | help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * | |
15070 | sizeof (dtrace_helper_provider_t *), KM_SLEEP); | |
15071 | ||
15072 | if (tmp_provs != NULL) { | |
15073 | bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * | |
15074 | sizeof (dtrace_helper_provider_t *)); | |
15075 | kmem_free(tmp_provs, tmp_maxprovs * | |
15076 | sizeof (dtrace_helper_provider_t *)); | |
15077 | } | |
15078 | } | |
15079 | ||
15080 | help->dthps_provs[help->dthps_nprovs] = hprov; | |
15081 | help->dthps_nprovs++; | |
15082 | ||
15083 | return (0); | |
15084 | } | |
15085 | ||
15086 | static void | |
15087 | dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) | |
15088 | { | |
15089 | lck_mtx_lock(&dtrace_lock); | |
15090 | ||
15091 | if (--hprov->dthp_ref == 0) { | |
15092 | dof_hdr_t *dof; | |
15093 | lck_mtx_unlock(&dtrace_lock); | |
15094 | dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; | |
15095 | dtrace_dof_destroy(dof); | |
15096 | kmem_free(hprov, sizeof (dtrace_helper_provider_t)); | |
15097 | } else { | |
15098 | lck_mtx_unlock(&dtrace_lock); | |
15099 | } | |
15100 | } | |
15101 | ||
15102 | static int | |
15103 | dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) | |
15104 | { | |
15105 | uintptr_t daddr = (uintptr_t)dof; | |
15106 | dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; | |
15107 | dof_provider_t *provider; | |
15108 | dof_probe_t *probe; | |
15109 | uint8_t *arg; | |
15110 | char *strtab, *typestr; | |
15111 | dof_stridx_t typeidx; | |
15112 | size_t typesz; | |
15113 | uint_t nprobes, j, k; | |
15114 | ||
15115 | ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); | |
15116 | ||
15117 | if (sec->dofs_offset & (sizeof (uint_t) - 1)) { | |
15118 | dtrace_dof_error(dof, "misaligned section offset"); | |
15119 | return (-1); | |
15120 | } | |
15121 | ||
15122 | /* | |
15123 | * The section needs to be large enough to contain the DOF provider | |
15124 | * structure appropriate for the given version. | |
15125 | */ | |
15126 | if (sec->dofs_size < | |
15127 | ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? | |
15128 | offsetof(dof_provider_t, dofpv_prenoffs) : | |
15129 | sizeof (dof_provider_t))) { | |
15130 | dtrace_dof_error(dof, "provider section too small"); | |
15131 | return (-1); | |
15132 | } | |
15133 | ||
15134 | provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); | |
15135 | str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); | |
15136 | prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); | |
15137 | arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); | |
15138 | off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); | |
15139 | ||
15140 | if (str_sec == NULL || prb_sec == NULL || | |
15141 | arg_sec == NULL || off_sec == NULL) | |
15142 | return (-1); | |
15143 | ||
15144 | enoff_sec = NULL; | |
15145 | ||
15146 | if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && | |
15147 | provider->dofpv_prenoffs != DOF_SECT_NONE && | |
15148 | (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, | |
15149 | provider->dofpv_prenoffs)) == NULL) | |
15150 | return (-1); | |
15151 | ||
15152 | strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); | |
15153 | ||
15154 | if (provider->dofpv_name >= str_sec->dofs_size || | |
15155 | strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { | |
15156 | dtrace_dof_error(dof, "invalid provider name"); | |
15157 | return (-1); | |
15158 | } | |
15159 | ||
15160 | if (prb_sec->dofs_entsize == 0 || | |
15161 | prb_sec->dofs_entsize > prb_sec->dofs_size) { | |
15162 | dtrace_dof_error(dof, "invalid entry size"); | |
15163 | return (-1); | |
15164 | } | |
15165 | ||
15166 | if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { | |
15167 | dtrace_dof_error(dof, "misaligned entry size"); | |
15168 | return (-1); | |
15169 | } | |
15170 | ||
15171 | if (off_sec->dofs_entsize != sizeof (uint32_t)) { | |
15172 | dtrace_dof_error(dof, "invalid entry size"); | |
15173 | return (-1); | |
15174 | } | |
15175 | ||
15176 | if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { | |
15177 | dtrace_dof_error(dof, "misaligned section offset"); | |
15178 | return (-1); | |
15179 | } | |
15180 | ||
15181 | if (arg_sec->dofs_entsize != sizeof (uint8_t)) { | |
15182 | dtrace_dof_error(dof, "invalid entry size"); | |
15183 | return (-1); | |
15184 | } | |
15185 | ||
15186 | arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); | |
15187 | ||
15188 | nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; | |
15189 | ||
15190 | /* | |
15191 | * Take a pass through the probes to check for errors. | |
15192 | */ | |
15193 | for (j = 0; j < nprobes; j++) { | |
15194 | probe = (dof_probe_t *)(uintptr_t)(daddr + | |
15195 | prb_sec->dofs_offset + j * prb_sec->dofs_entsize); | |
15196 | ||
15197 | if (probe->dofpr_func >= str_sec->dofs_size) { | |
15198 | dtrace_dof_error(dof, "invalid function name"); | |
15199 | return (-1); | |
15200 | } | |
15201 | ||
15202 | if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { | |
15203 | dtrace_dof_error(dof, "function name too long"); | |
15204 | return (-1); | |
15205 | } | |
15206 | ||
15207 | if (probe->dofpr_name >= str_sec->dofs_size || | |
15208 | strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { | |
15209 | dtrace_dof_error(dof, "invalid probe name"); | |
15210 | return (-1); | |
15211 | } | |
15212 | ||
15213 | /* | |
15214 | * The offset count must not wrap the index, and the offsets | |
15215 | * must also not overflow the section's data. | |
15216 | */ | |
15217 | if (probe->dofpr_offidx + probe->dofpr_noffs < | |
15218 | probe->dofpr_offidx || | |
15219 | (probe->dofpr_offidx + probe->dofpr_noffs) * | |
15220 | off_sec->dofs_entsize > off_sec->dofs_size) { | |
15221 | dtrace_dof_error(dof, "invalid probe offset"); | |
15222 | return (-1); | |
15223 | } | |
15224 | ||
15225 | if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { | |
15226 | /* | |
15227 | * If there's no is-enabled offset section, make sure | |
15228 | * there aren't any is-enabled offsets. Otherwise | |
15229 | * perform the same checks as for probe offsets | |
15230 | * (immediately above). | |
15231 | */ | |
15232 | if (enoff_sec == NULL) { | |
15233 | if (probe->dofpr_enoffidx != 0 || | |
15234 | probe->dofpr_nenoffs != 0) { | |
15235 | dtrace_dof_error(dof, "is-enabled " | |
15236 | "offsets with null section"); | |
15237 | return (-1); | |
15238 | } | |
15239 | } else if (probe->dofpr_enoffidx + | |
15240 | probe->dofpr_nenoffs < probe->dofpr_enoffidx || | |
15241 | (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * | |
15242 | enoff_sec->dofs_entsize > enoff_sec->dofs_size) { | |
15243 | dtrace_dof_error(dof, "invalid is-enabled " | |
15244 | "offset"); | |
15245 | return (-1); | |
15246 | } | |
15247 | ||
15248 | if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { | |
15249 | dtrace_dof_error(dof, "zero probe and " | |
15250 | "is-enabled offsets"); | |
15251 | return (-1); | |
15252 | } | |
15253 | } else if (probe->dofpr_noffs == 0) { | |
15254 | dtrace_dof_error(dof, "zero probe offsets"); | |
15255 | return (-1); | |
15256 | } | |
15257 | ||
15258 | if (probe->dofpr_argidx + probe->dofpr_xargc < | |
15259 | probe->dofpr_argidx || | |
15260 | (probe->dofpr_argidx + probe->dofpr_xargc) * | |
15261 | arg_sec->dofs_entsize > arg_sec->dofs_size) { | |
15262 | dtrace_dof_error(dof, "invalid args"); | |
15263 | return (-1); | |
15264 | } | |
15265 | ||
15266 | typeidx = probe->dofpr_nargv; | |
15267 | typestr = strtab + probe->dofpr_nargv; | |
15268 | for (k = 0; k < probe->dofpr_nargc; k++) { | |
15269 | if (typeidx >= str_sec->dofs_size) { | |
15270 | dtrace_dof_error(dof, "bad " | |
15271 | "native argument type"); | |
15272 | return (-1); | |
15273 | } | |
15274 | ||
15275 | typesz = strlen(typestr) + 1; | |
15276 | if (typesz > DTRACE_ARGTYPELEN) { | |
15277 | dtrace_dof_error(dof, "native " | |
15278 | "argument type too long"); | |
15279 | return (-1); | |
15280 | } | |
15281 | typeidx += typesz; | |
15282 | typestr += typesz; | |
15283 | } | |
15284 | ||
15285 | typeidx = probe->dofpr_xargv; | |
15286 | typestr = strtab + probe->dofpr_xargv; | |
15287 | for (k = 0; k < probe->dofpr_xargc; k++) { | |
15288 | if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { | |
15289 | dtrace_dof_error(dof, "bad " | |
15290 | "native argument index"); | |
15291 | return (-1); | |
15292 | } | |
15293 | ||
15294 | if (typeidx >= str_sec->dofs_size) { | |
15295 | dtrace_dof_error(dof, "bad " | |
15296 | "translated argument type"); | |
15297 | return (-1); | |
15298 | } | |
15299 | ||
15300 | typesz = strlen(typestr) + 1; | |
15301 | if (typesz > DTRACE_ARGTYPELEN) { | |
15302 | dtrace_dof_error(dof, "translated argument " | |
15303 | "type too long"); | |
15304 | return (-1); | |
15305 | } | |
15306 | ||
15307 | typeidx += typesz; | |
15308 | typestr += typesz; | |
15309 | } | |
15310 | } | |
15311 | ||
15312 | return (0); | |
15313 | } | |
15314 | ||
15315 | #if !defined(__APPLE__) | |
15316 | static int | |
15317 | dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) | |
15318 | #else | |
15319 | static int | |
15320 | dtrace_helper_slurp(proc_t* p, dof_hdr_t *dof, dof_helper_t *dhp) | |
15321 | #endif | |
15322 | { | |
15323 | dtrace_helpers_t *help; | |
15324 | dtrace_vstate_t *vstate; | |
15325 | dtrace_enabling_t *enab = NULL; | |
15326 | int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; | |
15327 | uintptr_t daddr = (uintptr_t)dof; | |
15328 | ||
15329 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
15330 | ||
15331 | #if !defined(__APPLE__) | |
15332 | if ((help = curproc->p_dtrace_helpers) == NULL) | |
15333 | help = dtrace_helpers_create(curproc); | |
15334 | #else | |
15335 | if ((help = p->p_dtrace_helpers) == NULL) | |
15336 | help = dtrace_helpers_create(p); | |
15337 | #endif | |
15338 | ||
15339 | vstate = &help->dthps_vstate; | |
15340 | ||
15341 | if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, | |
15342 | dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { | |
15343 | dtrace_dof_destroy(dof); | |
15344 | return (rv); | |
15345 | } | |
15346 | ||
15347 | /* | |
15348 | * Look for helper providers and validate their descriptions. | |
15349 | */ | |
15350 | if (dhp != NULL) { | |
b0d623f7 | 15351 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 15352 | for (i = 0; i < dof->dofh_secnum; i++) { |
b0d623f7 A |
15353 | #else |
15354 | for (i = 0; (uint32_t)i < dof->dofh_secnum; i++) { | |
15355 | #endif /* __APPLE__ */ | |
2d21ac55 A |
15356 | dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + |
15357 | dof->dofh_secoff + i * dof->dofh_secsize); | |
15358 | ||
15359 | if (sec->dofs_type != DOF_SECT_PROVIDER) | |
15360 | continue; | |
15361 | ||
15362 | if (dtrace_helper_provider_validate(dof, sec) != 0) { | |
15363 | dtrace_enabling_destroy(enab); | |
15364 | dtrace_dof_destroy(dof); | |
15365 | return (-1); | |
15366 | } | |
15367 | ||
15368 | nprovs++; | |
15369 | } | |
15370 | } | |
15371 | ||
15372 | /* | |
15373 | * Now we need to walk through the ECB descriptions in the enabling. | |
15374 | */ | |
15375 | for (i = 0; i < enab->dten_ndesc; i++) { | |
15376 | dtrace_ecbdesc_t *ep = enab->dten_desc[i]; | |
15377 | dtrace_probedesc_t *desc = &ep->dted_probe; | |
15378 | ||
b0d623f7 | 15379 | #if !defined(__APPLE__) |
2d21ac55 A |
15380 | if (strcmp(desc->dtpd_provider, "dtrace") != 0) |
15381 | continue; | |
15382 | ||
15383 | if (strcmp(desc->dtpd_mod, "helper") != 0) | |
15384 | continue; | |
15385 | ||
15386 | if (strcmp(desc->dtpd_func, "ustack") != 0) | |
15387 | continue; | |
b0d623f7 A |
15388 | #else /* Employ size bounded string operation. */ |
15389 | if (!LIT_STRNEQL(desc->dtpd_provider, "dtrace")) | |
15390 | continue; | |
2d21ac55 | 15391 | |
b0d623f7 A |
15392 | if (!LIT_STRNEQL(desc->dtpd_mod, "helper")) |
15393 | continue; | |
15394 | ||
15395 | if (!LIT_STRNEQL(desc->dtpd_func, "ustack")) | |
15396 | continue; | |
15397 | #endif /* __APPLE__ */ | |
15398 | ||
15399 | #if !defined(__APPLE__) | |
15400 | if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, | |
15401 | ep)) != 0) { | |
15402 | #else | |
15403 | if ((rv = dtrace_helper_action_add(p, DTRACE_HELPER_ACTION_USTACK, | |
15404 | ep)) != 0) { | |
15405 | #endif | |
15406 | /* | |
2d21ac55 A |
15407 | * Adding this helper action failed -- we are now going |
15408 | * to rip out the entire generation and return failure. | |
15409 | */ | |
15410 | #if !defined(__APPLE__) | |
15411 | (void) dtrace_helper_destroygen(help->dthps_generation); | |
15412 | #else | |
15413 | (void) dtrace_helper_destroygen(p, help->dthps_generation); | |
15414 | #endif | |
15415 | dtrace_enabling_destroy(enab); | |
15416 | dtrace_dof_destroy(dof); | |
15417 | return (-1); | |
15418 | } | |
15419 | ||
15420 | nhelpers++; | |
15421 | } | |
15422 | ||
15423 | if (nhelpers < enab->dten_ndesc) | |
15424 | dtrace_dof_error(dof, "unmatched helpers"); | |
15425 | ||
15426 | gen = help->dthps_generation++; | |
15427 | dtrace_enabling_destroy(enab); | |
15428 | ||
15429 | if (dhp != NULL && nprovs > 0) { | |
15430 | dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; | |
15431 | #if !defined(__APPLE__) | |
15432 | if (dtrace_helper_provider_add(dhp, gen) == 0) { | |
15433 | #else | |
15434 | if (dtrace_helper_provider_add(p, dhp, gen) == 0) { | |
15435 | #endif | |
15436 | lck_mtx_unlock(&dtrace_lock); | |
15437 | #if !defined(__APPLE__) | |
15438 | dtrace_helper_provider_register(curproc, help, dhp); | |
15439 | #else | |
15440 | dtrace_helper_provider_register(p, help, dhp); | |
15441 | #endif | |
15442 | lck_mtx_lock(&dtrace_lock); | |
15443 | ||
15444 | destroy = 0; | |
15445 | } | |
15446 | } | |
15447 | ||
15448 | if (destroy) | |
15449 | dtrace_dof_destroy(dof); | |
15450 | ||
15451 | return (gen); | |
15452 | } | |
15453 | ||
15454 | #if defined(__APPLE__) | |
15455 | ||
15456 | /* | |
15457 | * DTrace lazy dof | |
15458 | * | |
15459 | * DTrace user static probes (USDT probes) and helper actions are loaded | |
15460 | * in a process by proccessing dof sections. The dof sections are passed | |
15461 | * into the kernel by dyld, in a dof_ioctl_data_t block. It is rather | |
15462 | * expensive to process dof for a process that will never use it. There | |
15463 | * is a memory cost (allocating the providers/probes), and a cpu cost | |
15464 | * (creating the providers/probes). | |
15465 | * | |
15466 | * To reduce this cost, we use "lazy dof". The normal proceedure for | |
15467 | * dof processing is to copyin the dof(s) pointed to by the dof_ioctl_data_t | |
15468 | * block, and invoke dof_slurp_helper() on them. When "lazy dof" is | |
15469 | * used, each process retains the dof_ioctl_data_t block, instead of | |
15470 | * copying in the data it points to. | |
15471 | * | |
15472 | * The dof_ioctl_data_t blocks are managed as if they were the actual | |
15473 | * processed dof; on fork the block is copied to the child, on exec and | |
15474 | * exit the block is freed. | |
15475 | * | |
15476 | * If the process loads library(s) containing additional dof, the | |
15477 | * new dof_ioctl_data_t is merged with the existing block. | |
15478 | * | |
15479 | * There are a few catches that make this slightly more difficult. | |
15480 | * When dyld registers dof_ioctl_data_t blocks, it expects a unique | |
15481 | * identifier value for each dof in the block. In non-lazy dof terms, | |
15482 | * this is the generation that dof was loaded in. If we hand back | |
15483 | * a UID for a lazy dof, that same UID must be able to unload the | |
15484 | * dof once it has become non-lazy. To meet this requirement, the | |
15485 | * code that loads lazy dof requires that the UID's for dof(s) in | |
15486 | * the lazy dof be sorted, and in ascending order. It is okay to skip | |
15487 | * UID's, I.E., 1 -> 5 -> 6 is legal. | |
15488 | * | |
15489 | * Once a process has become non-lazy, it will stay non-lazy. All | |
15490 | * future dof operations for that process will be non-lazy, even | |
15491 | * if the dof mode transitions back to lazy. | |
15492 | * | |
15493 | * Always do lazy dof checks before non-lazy (I.E. In fork, exit, exec.). | |
15494 | * That way if the lazy check fails due to transitioning to non-lazy, the | |
15495 | * right thing is done with the newly faulted in dof. | |
15496 | */ | |
15497 | ||
15498 | /* | |
15499 | * This method is a bit squicky. It must handle: | |
15500 | * | |
15501 | * dof should not be lazy. | |
15502 | * dof should have been handled lazily, but there was an error | |
15503 | * dof was handled lazily, and needs to be freed. | |
15504 | * dof was handled lazily, and must not be freed. | |
15505 | * | |
15506 | * | |
15507 | * Returns EACCESS if dof should be handled non-lazily. | |
15508 | * | |
15509 | * KERN_SUCCESS and all other return codes indicate lazy handling of dof. | |
15510 | * | |
15511 | * If the dofs data is claimed by this method, dofs_claimed will be set. | |
15512 | * Callers should not free claimed dofs. | |
15513 | */ | |
b0d623f7 | 15514 | static int |
2d21ac55 A |
15515 | dtrace_lazy_dofs_add(proc_t *p, dof_ioctl_data_t* incoming_dofs, int *dofs_claimed) |
15516 | { | |
15517 | ASSERT(p); | |
15518 | ASSERT(incoming_dofs && incoming_dofs->dofiod_count > 0); | |
15519 | ||
15520 | int rval = 0; | |
15521 | *dofs_claimed = 0; | |
15522 | ||
15523 | lck_rw_lock_shared(&dtrace_dof_mode_lock); | |
15524 | ||
15525 | /* | |
15526 | * If we have lazy dof, dof mode better be LAZY_ON. | |
15527 | */ | |
15528 | ASSERT(p->p_dtrace_lazy_dofs == NULL || dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON); | |
15529 | ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL); | |
15530 | ASSERT(dtrace_dof_mode != DTRACE_DOF_MODE_NEVER); | |
15531 | ||
15532 | /* | |
15533 | * Any existing helpers force non-lazy behavior. | |
15534 | */ | |
15535 | if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON && (p->p_dtrace_helpers == NULL)) { | |
15536 | lck_mtx_lock(&p->p_dtrace_sprlock); | |
15537 | ||
15538 | dof_ioctl_data_t* existing_dofs = p->p_dtrace_lazy_dofs; | |
15539 | unsigned int existing_dofs_count = (existing_dofs) ? existing_dofs->dofiod_count : 0; | |
15540 | unsigned int i, merged_dofs_count = incoming_dofs->dofiod_count + existing_dofs_count; | |
15541 | ||
15542 | /* | |
15543 | * Range check... | |
15544 | */ | |
15545 | if (merged_dofs_count == 0 || merged_dofs_count > 1024) { | |
15546 | dtrace_dof_error(NULL, "lazy_dofs_add merged_dofs_count out of range"); | |
15547 | rval = EINVAL; | |
15548 | goto unlock; | |
15549 | } | |
15550 | ||
15551 | /* | |
15552 | * Each dof being added must be assigned a unique generation. | |
15553 | */ | |
15554 | uint64_t generation = (existing_dofs) ? existing_dofs->dofiod_helpers[existing_dofs_count - 1].dofhp_dof + 1 : 1; | |
15555 | for (i=0; i<incoming_dofs->dofiod_count; i++) { | |
15556 | /* | |
15557 | * We rely on these being the same so we can overwrite dofhp_dof and not lose info. | |
15558 | */ | |
15559 | ASSERT(incoming_dofs->dofiod_helpers[i].dofhp_dof == incoming_dofs->dofiod_helpers[i].dofhp_addr); | |
15560 | incoming_dofs->dofiod_helpers[i].dofhp_dof = generation++; | |
15561 | } | |
15562 | ||
15563 | ||
15564 | if (existing_dofs) { | |
15565 | /* | |
15566 | * Merge the existing and incoming dofs | |
15567 | */ | |
15568 | size_t merged_dofs_size = DOF_IOCTL_DATA_T_SIZE(merged_dofs_count); | |
15569 | dof_ioctl_data_t* merged_dofs = kmem_alloc(merged_dofs_size, KM_SLEEP); | |
15570 | ||
15571 | bcopy(&existing_dofs->dofiod_helpers[0], | |
15572 | &merged_dofs->dofiod_helpers[0], | |
15573 | sizeof(dof_helper_t) * existing_dofs_count); | |
15574 | bcopy(&incoming_dofs->dofiod_helpers[0], | |
15575 | &merged_dofs->dofiod_helpers[existing_dofs_count], | |
15576 | sizeof(dof_helper_t) * incoming_dofs->dofiod_count); | |
15577 | ||
15578 | merged_dofs->dofiod_count = merged_dofs_count; | |
15579 | ||
15580 | kmem_free(existing_dofs, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count)); | |
15581 | ||
15582 | p->p_dtrace_lazy_dofs = merged_dofs; | |
15583 | } else { | |
15584 | /* | |
15585 | * Claim the incoming dofs | |
15586 | */ | |
15587 | *dofs_claimed = 1; | |
15588 | p->p_dtrace_lazy_dofs = incoming_dofs; | |
15589 | } | |
15590 | ||
15591 | #if DEBUG | |
15592 | dof_ioctl_data_t* all_dofs = p->p_dtrace_lazy_dofs; | |
15593 | for (i=0; i<all_dofs->dofiod_count-1; i++) { | |
15594 | ASSERT(all_dofs->dofiod_helpers[i].dofhp_dof < all_dofs->dofiod_helpers[i+1].dofhp_dof); | |
15595 | } | |
b0d623f7 | 15596 | #endif /* DEBUG */ |
2d21ac55 A |
15597 | |
15598 | unlock: | |
15599 | lck_mtx_unlock(&p->p_dtrace_sprlock); | |
15600 | } else { | |
15601 | rval = EACCES; | |
15602 | } | |
15603 | ||
15604 | lck_rw_unlock_shared(&dtrace_dof_mode_lock); | |
15605 | ||
15606 | return rval; | |
15607 | } | |
15608 | ||
15609 | /* | |
15610 | * Returns: | |
15611 | * | |
15612 | * EINVAL: lazy dof is enabled, but the requested generation was not found. | |
15613 | * EACCES: This removal needs to be handled non-lazily. | |
15614 | */ | |
b0d623f7 | 15615 | static int |
2d21ac55 A |
15616 | dtrace_lazy_dofs_remove(proc_t *p, int generation) |
15617 | { | |
15618 | int rval = EINVAL; | |
15619 | ||
15620 | lck_rw_lock_shared(&dtrace_dof_mode_lock); | |
15621 | ||
15622 | /* | |
15623 | * If we have lazy dof, dof mode better be LAZY_ON. | |
15624 | */ | |
15625 | ASSERT(p->p_dtrace_lazy_dofs == NULL || dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON); | |
15626 | ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL); | |
15627 | ASSERT(dtrace_dof_mode != DTRACE_DOF_MODE_NEVER); | |
15628 | ||
15629 | /* | |
15630 | * Any existing helpers force non-lazy behavior. | |
15631 | */ | |
15632 | if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON && (p->p_dtrace_helpers == NULL)) { | |
15633 | lck_mtx_lock(&p->p_dtrace_sprlock); | |
15634 | ||
15635 | dof_ioctl_data_t* existing_dofs = p->p_dtrace_lazy_dofs; | |
15636 | ||
15637 | if (existing_dofs) { | |
15638 | int index, existing_dofs_count = existing_dofs->dofiod_count; | |
15639 | for (index=0; index<existing_dofs_count; index++) { | |
15640 | if ((int)existing_dofs->dofiod_helpers[index].dofhp_dof == generation) { | |
15641 | dof_ioctl_data_t* removed_dofs = NULL; | |
15642 | ||
15643 | /* | |
15644 | * If there is only 1 dof, we'll delete it and swap in NULL. | |
15645 | */ | |
15646 | if (existing_dofs_count > 1) { | |
15647 | int removed_dofs_count = existing_dofs_count - 1; | |
15648 | size_t removed_dofs_size = DOF_IOCTL_DATA_T_SIZE(removed_dofs_count); | |
15649 | ||
15650 | removed_dofs = kmem_alloc(removed_dofs_size, KM_SLEEP); | |
15651 | removed_dofs->dofiod_count = removed_dofs_count; | |
15652 | ||
15653 | /* | |
15654 | * copy the remaining data. | |
15655 | */ | |
15656 | if (index > 0) { | |
15657 | bcopy(&existing_dofs->dofiod_helpers[0], | |
15658 | &removed_dofs->dofiod_helpers[0], | |
15659 | index * sizeof(dof_helper_t)); | |
15660 | } | |
15661 | ||
15662 | if (index < existing_dofs_count-1) { | |
15663 | bcopy(&existing_dofs->dofiod_helpers[index+1], | |
15664 | &removed_dofs->dofiod_helpers[index], | |
15665 | (existing_dofs_count - index - 1) * sizeof(dof_helper_t)); | |
15666 | } | |
15667 | } | |
15668 | ||
15669 | kmem_free(existing_dofs, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count)); | |
15670 | ||
15671 | p->p_dtrace_lazy_dofs = removed_dofs; | |
15672 | ||
15673 | rval = KERN_SUCCESS; | |
15674 | ||
15675 | break; | |
15676 | } | |
15677 | } | |
15678 | ||
15679 | #if DEBUG | |
15680 | dof_ioctl_data_t* all_dofs = p->p_dtrace_lazy_dofs; | |
15681 | if (all_dofs) { | |
15682 | unsigned int i; | |
15683 | for (i=0; i<all_dofs->dofiod_count-1; i++) { | |
15684 | ASSERT(all_dofs->dofiod_helpers[i].dofhp_dof < all_dofs->dofiod_helpers[i+1].dofhp_dof); | |
15685 | } | |
15686 | } | |
15687 | #endif | |
15688 | ||
15689 | } | |
15690 | ||
15691 | lck_mtx_unlock(&p->p_dtrace_sprlock); | |
15692 | } else { | |
15693 | rval = EACCES; | |
15694 | } | |
15695 | ||
15696 | lck_rw_unlock_shared(&dtrace_dof_mode_lock); | |
15697 | ||
15698 | return rval; | |
15699 | } | |
15700 | ||
15701 | void | |
15702 | dtrace_lazy_dofs_destroy(proc_t *p) | |
15703 | { | |
15704 | lck_rw_lock_shared(&dtrace_dof_mode_lock); | |
15705 | lck_mtx_lock(&p->p_dtrace_sprlock); | |
15706 | ||
15707 | /* | |
15708 | * If we have lazy dof, dof mode better be LAZY_ON, or we must be exiting. | |
15709 | * We cannot assert against DTRACE_DOF_MODE_NEVER here, because we are called from | |
15710 | * kern_exit.c and kern_exec.c. | |
15711 | */ | |
15712 | ASSERT(p->p_dtrace_lazy_dofs == NULL || dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON || p->p_lflag & P_LEXIT); | |
15713 | ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL); | |
15714 | ||
15715 | dof_ioctl_data_t* lazy_dofs = p->p_dtrace_lazy_dofs; | |
15716 | p->p_dtrace_lazy_dofs = NULL; | |
15717 | ||
15718 | lck_mtx_unlock(&p->p_dtrace_sprlock); | |
15719 | lck_rw_unlock_shared(&dtrace_dof_mode_lock); | |
15720 | ||
15721 | if (lazy_dofs) { | |
15722 | kmem_free(lazy_dofs, DOF_IOCTL_DATA_T_SIZE(lazy_dofs->dofiod_count)); | |
15723 | } | |
15724 | } | |
15725 | ||
15726 | void | |
15727 | dtrace_lazy_dofs_duplicate(proc_t *parent, proc_t *child) | |
15728 | { | |
15729 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED); | |
15730 | lck_mtx_assert(&parent->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED); | |
15731 | lck_mtx_assert(&child->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED); | |
15732 | ||
15733 | lck_rw_lock_shared(&dtrace_dof_mode_lock); | |
15734 | lck_mtx_lock(&parent->p_dtrace_sprlock); | |
15735 | ||
15736 | /* | |
15737 | * If we have lazy dof, dof mode better be LAZY_ON, or we must be exiting. | |
15738 | * We cannot assert against DTRACE_DOF_MODE_NEVER here, because we are called from | |
15739 | * kern_fork.c | |
15740 | */ | |
15741 | ASSERT(parent->p_dtrace_lazy_dofs == NULL || dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON); | |
15742 | ASSERT(parent->p_dtrace_lazy_dofs == NULL || parent->p_dtrace_helpers == NULL); | |
15743 | /* | |
15744 | * In theory we should hold the child sprlock, but this is safe... | |
15745 | */ | |
15746 | ASSERT(child->p_dtrace_lazy_dofs == NULL && child->p_dtrace_helpers == NULL); | |
15747 | ||
15748 | dof_ioctl_data_t* parent_dofs = parent->p_dtrace_lazy_dofs; | |
15749 | dof_ioctl_data_t* child_dofs = NULL; | |
15750 | if (parent_dofs) { | |
15751 | size_t parent_dofs_size = DOF_IOCTL_DATA_T_SIZE(parent_dofs->dofiod_count); | |
15752 | child_dofs = kmem_alloc(parent_dofs_size, KM_SLEEP); | |
15753 | bcopy(parent_dofs, child_dofs, parent_dofs_size); | |
15754 | } | |
15755 | ||
15756 | lck_mtx_unlock(&parent->p_dtrace_sprlock); | |
15757 | ||
15758 | if (child_dofs) { | |
15759 | lck_mtx_lock(&child->p_dtrace_sprlock); | |
15760 | child->p_dtrace_lazy_dofs = child_dofs; | |
15761 | lck_mtx_unlock(&child->p_dtrace_sprlock); | |
15762 | } | |
15763 | ||
15764 | lck_rw_unlock_shared(&dtrace_dof_mode_lock); | |
15765 | } | |
15766 | ||
15767 | static int | |
15768 | dtrace_lazy_dofs_proc_iterate_filter(proc_t *p, void* ignored) | |
15769 | { | |
15770 | #pragma unused(ignored) | |
15771 | /* | |
15772 | * Okay to NULL test without taking the sprlock. | |
15773 | */ | |
15774 | return p->p_dtrace_lazy_dofs != NULL; | |
15775 | } | |
15776 | ||
15777 | static int | |
15778 | dtrace_lazy_dofs_proc_iterate_doit(proc_t *p, void* ignored) | |
15779 | { | |
15780 | #pragma unused(ignored) | |
15781 | /* | |
15782 | * It is possible this process may exit during our attempt to | |
15783 | * fault in the dof. We could fix this by holding locks longer, | |
15784 | * but the errors are benign. | |
15785 | */ | |
15786 | lck_mtx_lock(&p->p_dtrace_sprlock); | |
15787 | ||
15788 | /* | |
15789 | * In this case only, it is okay to have lazy dof when dof mode is DTRACE_DOF_MODE_LAZY_OFF | |
15790 | */ | |
15791 | ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL); | |
15792 | ASSERT(dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_OFF); | |
15793 | ||
15794 | ||
15795 | dof_ioctl_data_t* lazy_dofs = p->p_dtrace_lazy_dofs; | |
15796 | p->p_dtrace_lazy_dofs = NULL; | |
15797 | ||
15798 | lck_mtx_unlock(&p->p_dtrace_sprlock); | |
15799 | ||
15800 | /* | |
15801 | * Process each dof_helper_t | |
15802 | */ | |
15803 | if (lazy_dofs != NULL) { | |
15804 | unsigned int i; | |
15805 | int rval; | |
15806 | ||
15807 | for (i=0; i<lazy_dofs->dofiod_count; i++) { | |
15808 | /* | |
15809 | * When loading lazy dof, we depend on the generations being sorted in ascending order. | |
15810 | */ | |
15811 | ASSERT(i >= (lazy_dofs->dofiod_count - 1) || lazy_dofs->dofiod_helpers[i].dofhp_dof < lazy_dofs->dofiod_helpers[i+1].dofhp_dof); | |
15812 | ||
15813 | dof_helper_t *dhp = &lazy_dofs->dofiod_helpers[i]; | |
15814 | ||
15815 | /* | |
15816 | * We stored the generation in dofhp_dof. Save it, and restore the original value. | |
15817 | */ | |
15818 | int generation = dhp->dofhp_dof; | |
15819 | dhp->dofhp_dof = dhp->dofhp_addr; | |
15820 | ||
15821 | dof_hdr_t *dof = dtrace_dof_copyin_from_proc(p, dhp->dofhp_dof, &rval); | |
15822 | ||
15823 | if (dof != NULL) { | |
15824 | dtrace_helpers_t *help; | |
15825 | ||
15826 | lck_mtx_lock(&dtrace_lock); | |
15827 | ||
15828 | /* | |
15829 | * This must be done with the dtrace_lock held | |
15830 | */ | |
15831 | if ((help = p->p_dtrace_helpers) == NULL) | |
15832 | help = dtrace_helpers_create(p); | |
15833 | ||
15834 | /* | |
15835 | * If the generation value has been bumped, someone snuck in | |
15836 | * when we released the dtrace lock. We have to dump this generation, | |
15837 | * there is no safe way to load it. | |
15838 | */ | |
15839 | if (help->dthps_generation <= generation) { | |
15840 | help->dthps_generation = generation; | |
15841 | ||
15842 | /* | |
15843 | * dtrace_helper_slurp() takes responsibility for the dof -- | |
15844 | * it may free it now or it may save it and free it later. | |
15845 | */ | |
15846 | if ((rval = dtrace_helper_slurp(p, dof, dhp)) != generation) { | |
15847 | dtrace_dof_error(NULL, "returned value did not match expected generation"); | |
15848 | } | |
15849 | } | |
15850 | ||
15851 | lck_mtx_unlock(&dtrace_lock); | |
15852 | } | |
15853 | } | |
15854 | ||
15855 | kmem_free(lazy_dofs, DOF_IOCTL_DATA_T_SIZE(lazy_dofs->dofiod_count)); | |
15856 | } | |
15857 | ||
15858 | return PROC_RETURNED; | |
15859 | } | |
15860 | ||
15861 | #endif /* __APPLE__ */ | |
15862 | ||
15863 | static dtrace_helpers_t * | |
15864 | dtrace_helpers_create(proc_t *p) | |
15865 | { | |
15866 | dtrace_helpers_t *help; | |
15867 | ||
15868 | lck_mtx_assert(&dtrace_lock, LCK_MTX_ASSERT_OWNED); | |
15869 | ASSERT(p->p_dtrace_helpers == NULL); | |
15870 | ||
15871 | help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); | |
15872 | help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * | |
15873 | DTRACE_NHELPER_ACTIONS, KM_SLEEP); | |
15874 | ||
15875 | p->p_dtrace_helpers = help; | |
15876 | dtrace_helpers++; | |
15877 | ||
15878 | return (help); | |
15879 | } | |
15880 | ||
15881 | #if !defined(__APPLE__) | |
15882 | static void | |
15883 | dtrace_helpers_destroy(void) | |
15884 | { | |
b0d623f7 A |
15885 | dtrace_helpers_t *help; |
15886 | dtrace_vstate_t *vstate; | |
2d21ac55 | 15887 | proc_t *p = curproc; |
b0d623f7 | 15888 | int i; |
2d21ac55 A |
15889 | #else |
15890 | static void | |
15891 | dtrace_helpers_destroy(proc_t* p) | |
15892 | { | |
2d21ac55 A |
15893 | dtrace_helpers_t *help; |
15894 | dtrace_vstate_t *vstate; | |
b0d623f7 A |
15895 | uint_t i; |
15896 | #endif | |
2d21ac55 A |
15897 | |
15898 | lck_mtx_lock(&dtrace_lock); | |
15899 | ||
15900 | ASSERT(p->p_dtrace_helpers != NULL); | |
15901 | ASSERT(dtrace_helpers > 0); | |
15902 | ||
15903 | help = p->p_dtrace_helpers; | |
15904 | vstate = &help->dthps_vstate; | |
15905 | ||
15906 | /* | |
15907 | * We're now going to lose the help from this process. | |
15908 | */ | |
15909 | p->p_dtrace_helpers = NULL; | |
15910 | dtrace_sync(); | |
15911 | ||
15912 | /* | |
15913 | * Destory the helper actions. | |
15914 | */ | |
15915 | for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { | |
15916 | dtrace_helper_action_t *h, *next; | |
15917 | ||
15918 | for (h = help->dthps_actions[i]; h != NULL; h = next) { | |
15919 | next = h->dtha_next; | |
15920 | dtrace_helper_action_destroy(h, vstate); | |
15921 | h = next; | |
15922 | } | |
15923 | } | |
15924 | ||
15925 | lck_mtx_unlock(&dtrace_lock); | |
15926 | ||
15927 | /* | |
15928 | * Destroy the helper providers. | |
15929 | */ | |
15930 | if (help->dthps_maxprovs > 0) { | |
15931 | lck_mtx_lock(&dtrace_meta_lock); | |
15932 | if (dtrace_meta_pid != NULL) { | |
15933 | ASSERT(dtrace_deferred_pid == NULL); | |
15934 | ||
15935 | for (i = 0; i < help->dthps_nprovs; i++) { | |
15936 | dtrace_helper_provider_remove( | |
15937 | &help->dthps_provs[i]->dthp_prov, p->p_pid); | |
15938 | } | |
15939 | } else { | |
15940 | lck_mtx_lock(&dtrace_lock); | |
15941 | ASSERT(help->dthps_deferred == 0 || | |
15942 | help->dthps_next != NULL || | |
15943 | help->dthps_prev != NULL || | |
15944 | help == dtrace_deferred_pid); | |
15945 | ||
15946 | /* | |
15947 | * Remove the helper from the deferred list. | |
15948 | */ | |
15949 | if (help->dthps_next != NULL) | |
15950 | help->dthps_next->dthps_prev = help->dthps_prev; | |
15951 | if (help->dthps_prev != NULL) | |
15952 | help->dthps_prev->dthps_next = help->dthps_next; | |
15953 | if (dtrace_deferred_pid == help) { | |
15954 | dtrace_deferred_pid = help->dthps_next; | |
15955 | ASSERT(help->dthps_prev == NULL); | |
15956 | } | |
15957 | ||
15958 | lck_mtx_unlock(&dtrace_lock); | |
15959 | } | |
15960 | ||
15961 | lck_mtx_unlock(&dtrace_meta_lock); | |
15962 | ||
15963 | for (i = 0; i < help->dthps_nprovs; i++) { | |
15964 | dtrace_helper_provider_destroy(help->dthps_provs[i]); | |
15965 | } | |
15966 | ||
15967 | kmem_free(help->dthps_provs, help->dthps_maxprovs * | |
15968 | sizeof (dtrace_helper_provider_t *)); | |
15969 | } | |
15970 | ||
15971 | lck_mtx_lock(&dtrace_lock); | |
15972 | ||
15973 | dtrace_vstate_fini(&help->dthps_vstate); | |
15974 | kmem_free(help->dthps_actions, | |
15975 | sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); | |
15976 | kmem_free(help, sizeof (dtrace_helpers_t)); | |
15977 | ||
15978 | --dtrace_helpers; | |
15979 | lck_mtx_unlock(&dtrace_lock); | |
15980 | } | |
15981 | ||
15982 | static void | |
15983 | dtrace_helpers_duplicate(proc_t *from, proc_t *to) | |
15984 | { | |
15985 | dtrace_helpers_t *help, *newhelp; | |
15986 | dtrace_helper_action_t *helper, *new, *last; | |
15987 | dtrace_difo_t *dp; | |
15988 | dtrace_vstate_t *vstate; | |
b0d623f7 | 15989 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 | 15990 | int i, j, sz, hasprovs = 0; |
b0d623f7 A |
15991 | #else |
15992 | uint_t i; | |
15993 | int j, sz, hasprovs = 0; | |
15994 | #endif /* __APPLE__ */ | |
2d21ac55 A |
15995 | |
15996 | lck_mtx_lock(&dtrace_lock); | |
15997 | ASSERT(from->p_dtrace_helpers != NULL); | |
15998 | ASSERT(dtrace_helpers > 0); | |
15999 | ||
16000 | help = from->p_dtrace_helpers; | |
16001 | newhelp = dtrace_helpers_create(to); | |
16002 | ASSERT(to->p_dtrace_helpers != NULL); | |
16003 | ||
16004 | newhelp->dthps_generation = help->dthps_generation; | |
16005 | vstate = &newhelp->dthps_vstate; | |
16006 | ||
16007 | /* | |
16008 | * Duplicate the helper actions. | |
16009 | */ | |
16010 | for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { | |
16011 | if ((helper = help->dthps_actions[i]) == NULL) | |
16012 | continue; | |
16013 | ||
16014 | for (last = NULL; helper != NULL; helper = helper->dtha_next) { | |
16015 | new = kmem_zalloc(sizeof (dtrace_helper_action_t), | |
16016 | KM_SLEEP); | |
16017 | new->dtha_generation = helper->dtha_generation; | |
16018 | ||
16019 | if ((dp = helper->dtha_predicate) != NULL) { | |
16020 | dp = dtrace_difo_duplicate(dp, vstate); | |
16021 | new->dtha_predicate = dp; | |
16022 | } | |
16023 | ||
16024 | new->dtha_nactions = helper->dtha_nactions; | |
16025 | sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; | |
16026 | new->dtha_actions = kmem_alloc(sz, KM_SLEEP); | |
16027 | ||
b0d623f7 | 16028 | #if !defined(__APPLE__) /* Quiet compiler warning */ |
2d21ac55 A |
16029 | for (j = 0; j < new->dtha_nactions; j++) { |
16030 | dtrace_difo_t *dp = helper->dtha_actions[j]; | |
16031 | ||
16032 | ASSERT(dp != NULL); | |
16033 | dp = dtrace_difo_duplicate(dp, vstate); | |
16034 | new->dtha_actions[j] = dp; | |
16035 | } | |
b0d623f7 A |
16036 | #else |
16037 | for (j = 0; j < new->dtha_nactions; j++) { | |
16038 | dtrace_difo_t *dpj = helper->dtha_actions[j]; | |
16039 | ||
16040 | ASSERT(dpj != NULL); | |
16041 | dpj = dtrace_difo_duplicate(dpj, vstate); | |
16042 | new->dtha_actions[j] = dpj; | |
16043 | } | |
16044 | #endif /* __APPLE__ */ | |
2d21ac55 A |
16045 | |
16046 | if (last != NULL) { | |
16047 | last->dtha_next = new; | |
16048 | } else { | |
16049 | newhelp->dthps_actions[i] = new; | |
16050 | } | |
16051 | ||
16052 | last = new; | |
16053 | } | |
16054 | } | |
16055 | ||
16056 | /* | |
16057 | * Duplicate the helper providers and register them with the | |
16058 | * DTrace framework. | |
16059 | */ | |
16060 | if (help->dthps_nprovs > 0) { | |
16061 | newhelp->dthps_nprovs = help->dthps_nprovs; | |
16062 | newhelp->dthps_maxprovs = help->dthps_nprovs; | |
16063 | newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * | |
16064 | sizeof (dtrace_helper_provider_t *), KM_SLEEP); | |
16065 | for (i = 0; i < newhelp->dthps_nprovs; i++) { | |
16066 | newhelp->dthps_provs[i] = help->dthps_provs[i]; | |
16067 | newhelp->dthps_provs[i]->dthp_ref++; | |
16068 | } | |
16069 | ||
16070 | hasprovs = 1; | |
16071 | } | |
16072 | ||
16073 | lck_mtx_unlock(&dtrace_lock); | |
16074 | ||
16075 | if (hasprovs) | |
16076 | dtrace_helper_provider_register(to, newhelp, NULL); | |
16077 | } | |
16078 | ||
16079 | /* | |
16080 | * DTrace Hook Functions | |
16081 | */ | |
16082 | static void | |
16083 | dtrace_module_loaded(struct modctl *ctl) | |
16084 | { | |
16085 | dtrace_provider_t *prv; | |
16086 | ||
16087 | lck_mtx_lock(&dtrace_provider_lock); | |
16088 | lck_mtx_lock(&mod_lock); | |
16089 | ||
b0d623f7 A |
16090 | #if !defined(__APPLE__) |
16091 | ASSERT(ctl->mod_busy); | |
16092 | #else | |
16093 | /* FIXME: awaits kmod awareness PR_4648477. */ | |
16094 | #endif /* __APPLE__ */ | |
2d21ac55 A |
16095 | |
16096 | /* | |
16097 | * We're going to call each providers per-module provide operation | |
16098 | * specifying only this module. | |
16099 | */ | |
16100 | for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) | |
16101 | prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); | |
16102 | ||
16103 | lck_mtx_unlock(&mod_lock); | |
16104 | lck_mtx_unlock(&dtrace_provider_lock); | |
16105 | ||
16106 | /* | |
16107 | * If we have any retained enablings, we need to match against them. | |
16108 | * Enabling probes requires that cpu_lock be held, and we cannot hold | |
16109 | * cpu_lock here -- it is legal for cpu_lock to be held when loading a | |
16110 | * module. (In particular, this happens when loading scheduling | |
16111 | * classes.) So if we have any retained enablings, we need to dispatch | |
16112 | * our task queue to do the match for us. | |
16113 | */ | |
16114 | lck_mtx_lock(&dtrace_lock); | |
16115 | ||
16116 | if (dtrace_retained == NULL) { | |
16117 | lck_mtx_unlock(&dtrace_lock); | |
16118 | return; | |
16119 | } | |
16120 | ||
16121 | (void) taskq_dispatch(dtrace_taskq, | |
16122 | (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); | |
16123 | ||
16124 | lck_mtx_unlock(&dtrace_lock); | |
16125 | ||
16126 | /* | |
16127 | * And now, for a little heuristic sleaze: in general, we want to | |
16128 | * match modules as soon as they load. However, we cannot guarantee | |
16129 | * this, because it would lead us to the lock ordering violation | |
16130 | * outlined above. The common case, of course, is that cpu_lock is | |
16131 | * _not_ held -- so we delay here for a clock tick, hoping that that's | |
16132 | * long enough for the task queue to do its work. If it's not, it's | |
16133 | * not a serious problem -- it just means that the module that we | |
16134 | * just loaded may not be immediately instrumentable. | |
16135 | */ | |
16136 | delay(1); | |
16137 | } | |
16138 | ||
16139 | static void | |
16140 | dtrace_module_unloaded(struct modctl *ctl) | |
16141 | { | |
16142 | dtrace_probe_t template, *probe, *first, *next; | |
16143 | dtrace_provider_t *prov; | |
16144 | ||
16145 | template.dtpr_mod = ctl->mod_modname; | |
16146 | ||
16147 | lck_mtx_lock(&dtrace_provider_lock); | |
16148 | lck_mtx_lock(&mod_lock); | |
16149 | lck_mtx_lock(&dtrace_lock); | |
16150 | ||
16151 | if (dtrace_bymod == NULL) { | |
16152 | /* | |
16153 | * The DTrace module is loaded (obviously) but not attached; | |
16154 | * we don't have any work to do. | |
16155 | */ | |
16156 | lck_mtx_unlock(&dtrace_provider_lock); | |
16157 | lck_mtx_unlock(&mod_lock); | |
16158 | lck_mtx_unlock(&dtrace_lock); | |
16159 | return; | |
16160 | } | |
16161 | ||
16162 | for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); | |
16163 | probe != NULL; probe = probe->dtpr_nextmod) { | |
16164 | if (probe->dtpr_ecb != NULL) { | |
16165 | lck_mtx_unlock(&dtrace_provider_lock); | |
16166 | lck_mtx_unlock(&mod_lock); | |
16167 | lck_mtx_unlock(&dtrace_lock); | |
16168 | ||
16169 | /* | |
16170 | * This shouldn't _actually_ be possible -- we're | |
16171 | * unloading a module that has an enabled probe in it. | |
16172 | * (It's normally up to the provider to make sure that | |
16173 | * this can't happen.) However, because dtps_enable() | |
16174 | * doesn't have a failure mode, there can be an | |
16175 | * enable/unload race. Upshot: we don't want to | |
16176 | * assert, but we're not going to disable the | |
16177 | * probe, either. | |
16178 | */ | |
16179 | if (dtrace_err_verbose) { | |
16180 | cmn_err(CE_WARN, "unloaded module '%s' had " | |
16181 | "enabled probes", ctl->mod_modname); | |
16182 | } | |
16183 | ||
16184 | return; | |
16185 | } | |
16186 | } | |
16187 | ||
16188 | probe = first; | |
16189 | ||
16190 | for (first = NULL; probe != NULL; probe = next) { | |
16191 | ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); | |
16192 | ||
16193 | dtrace_probes[probe->dtpr_id - 1] = NULL; | |
16194 | ||
16195 | next = probe->dtpr_nextmod; | |
16196 | dtrace_hash_remove(dtrace_bymod, probe); | |
16197 | dtrace_hash_remove(dtrace_byfunc, probe); | |
16198 | dtrace_hash_remove(dtrace_byname, probe); | |
16199 | ||
16200 | if (first == NULL) { | |
16201 | first = probe; | |
16202 | probe->dtpr_nextmod = NULL; | |
16203 | } else { | |
16204 | probe->dtpr_nextmod = first; | |
16205 | first = probe; | |
16206 | } | |
16207 | } | |
16208 | ||
16209 | /* | |
16210 | * We've removed all of the module's probes from the hash chains and | |
16211 | * from the probe array. Now issue a dtrace_sync() to be sure that | |
16212 | * everyone has cleared out from any probe array processing. | |
16213 | */ | |
16214 | dtrace_sync(); | |
16215 | ||
16216 | for (probe = first; probe != NULL; probe = first) { | |
16217 | first = probe->dtpr_nextmod; | |
16218 | prov = probe->dtpr_provider; | |
16219 | prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, | |
16220 | probe->dtpr_arg); | |
16221 | kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); | |
16222 | kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); | |
16223 | kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); | |
16224 | vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); | |
16225 | #if !defined(__APPLE__) | |
16226 | kmem_free(probe, sizeof (dtrace_probe_t)); | |
16227 | #else | |
16228 | zfree(dtrace_probe_t_zone, probe); | |
b0d623f7 | 16229 | #endif /* __APPLE__ */ |
2d21ac55 A |
16230 | } |
16231 | ||
16232 | lck_mtx_unlock(&dtrace_lock); | |
16233 | lck_mtx_unlock(&mod_lock); | |
16234 | lck_mtx_unlock(&dtrace_provider_lock); | |
16235 | } | |
16236 | ||
16237 | void | |
16238 | dtrace_suspend(void) | |
16239 | { | |
16240 | dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); | |
16241 | } | |
16242 | ||
16243 | void | |
16244 | dtrace_resume(void) | |
16245 | { | |
16246 | dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); | |
16247 | } | |
16248 | ||
16249 | static int | |
16250 | dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) | |
16251 | { | |
16252 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
16253 | lck_mtx_lock(&dtrace_lock); | |
16254 | ||
16255 | switch (what) { | |
16256 | case CPU_CONFIG: { | |
16257 | dtrace_state_t *state; | |
16258 | dtrace_optval_t *opt, rs, c; | |
16259 | ||
16260 | /* | |
16261 | * For now, we only allocate a new buffer for anonymous state. | |
16262 | */ | |
16263 | if ((state = dtrace_anon.dta_state) == NULL) | |
16264 | break; | |
16265 | ||
16266 | if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) | |
16267 | break; | |
16268 | ||
16269 | opt = state->dts_options; | |
16270 | c = opt[DTRACEOPT_CPU]; | |
16271 | ||
16272 | if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) | |
16273 | break; | |
16274 | ||
16275 | /* | |
16276 | * Regardless of what the actual policy is, we're going to | |
16277 | * temporarily set our resize policy to be manual. We're | |
16278 | * also going to temporarily set our CPU option to denote | |
16279 | * the newly configured CPU. | |
16280 | */ | |
16281 | rs = opt[DTRACEOPT_BUFRESIZE]; | |
16282 | opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; | |
16283 | opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; | |
16284 | ||
16285 | (void) dtrace_state_buffers(state); | |
16286 | ||
16287 | opt[DTRACEOPT_BUFRESIZE] = rs; | |
16288 | opt[DTRACEOPT_CPU] = c; | |
16289 | ||
16290 | break; | |
16291 | } | |
16292 | ||
16293 | case CPU_UNCONFIG: | |
16294 | /* | |
16295 | * We don't free the buffer in the CPU_UNCONFIG case. (The | |
16296 | * buffer will be freed when the consumer exits.) | |
16297 | */ | |
16298 | break; | |
16299 | ||
16300 | default: | |
16301 | break; | |
16302 | } | |
16303 | ||
16304 | lck_mtx_unlock(&dtrace_lock); | |
16305 | return (0); | |
16306 | } | |
16307 | ||
16308 | static void | |
16309 | dtrace_cpu_setup_initial(processorid_t cpu) | |
16310 | { | |
16311 | (void) dtrace_cpu_setup(CPU_CONFIG, cpu); | |
16312 | } | |
16313 | ||
16314 | static void | |
16315 | dtrace_toxrange_add(uintptr_t base, uintptr_t limit) | |
16316 | { | |
16317 | if (dtrace_toxranges >= dtrace_toxranges_max) { | |
16318 | int osize, nsize; | |
16319 | dtrace_toxrange_t *range; | |
16320 | ||
16321 | osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); | |
16322 | ||
16323 | if (osize == 0) { | |
16324 | ASSERT(dtrace_toxrange == NULL); | |
16325 | ASSERT(dtrace_toxranges_max == 0); | |
16326 | dtrace_toxranges_max = 1; | |
16327 | } else { | |
16328 | dtrace_toxranges_max <<= 1; | |
16329 | } | |
16330 | ||
16331 | nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); | |
16332 | range = kmem_zalloc(nsize, KM_SLEEP); | |
16333 | ||
16334 | if (dtrace_toxrange != NULL) { | |
16335 | ASSERT(osize != 0); | |
16336 | bcopy(dtrace_toxrange, range, osize); | |
16337 | kmem_free(dtrace_toxrange, osize); | |
16338 | } | |
16339 | ||
16340 | dtrace_toxrange = range; | |
16341 | } | |
16342 | ||
16343 | ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == NULL); | |
16344 | ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == NULL); | |
16345 | ||
16346 | dtrace_toxrange[dtrace_toxranges].dtt_base = base; | |
16347 | dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; | |
16348 | dtrace_toxranges++; | |
16349 | } | |
16350 | ||
16351 | /* | |
16352 | * DTrace Driver Cookbook Functions | |
16353 | */ | |
16354 | /*ARGSUSED*/ | |
16355 | static int | |
16356 | dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) | |
16357 | { | |
b0d623f7 | 16358 | #pragma unused(cmd) /* __APPLE__ */ |
2d21ac55 A |
16359 | dtrace_provider_id_t id; |
16360 | dtrace_state_t *state = NULL; | |
16361 | dtrace_enabling_t *enab; | |
16362 | ||
16363 | lck_mtx_lock(&cpu_lock); | |
16364 | lck_mtx_lock(&dtrace_provider_lock); | |
16365 | lck_mtx_lock(&dtrace_lock); | |
16366 | ||
16367 | if (ddi_soft_state_init(&dtrace_softstate, | |
16368 | sizeof (dtrace_state_t), 0) != 0) { | |
16369 | cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); | |
16370 | lck_mtx_unlock(&cpu_lock); | |
16371 | lck_mtx_unlock(&dtrace_provider_lock); | |
16372 | lck_mtx_unlock(&dtrace_lock); | |
16373 | return (DDI_FAILURE); | |
16374 | } | |
16375 | ||
16376 | #if !defined(__APPLE__) | |
16377 | if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, | |
16378 | DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || | |
16379 | ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, | |
16380 | DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { | |
16381 | cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); | |
16382 | ddi_remove_minor_node(devi, NULL); | |
16383 | ddi_soft_state_fini(&dtrace_softstate); | |
16384 | lck_mtx_unlock(&cpu_lock); | |
16385 | lck_mtx_unlock(&dtrace_provider_lock); | |
16386 | lck_mtx_unlock(&dtrace_lock); | |
16387 | return (DDI_FAILURE); | |
16388 | } | |
b0d623f7 A |
16389 | #else |
16390 | /* Darwin uses BSD cloning device driver to automagically obtain minor device number. */ | |
2d21ac55 A |
16391 | #endif /* __APPLE__ */ |
16392 | ||
16393 | ddi_report_dev(devi); | |
16394 | dtrace_devi = devi; | |
16395 | ||
16396 | dtrace_modload = dtrace_module_loaded; | |
16397 | dtrace_modunload = dtrace_module_unloaded; | |
16398 | dtrace_cpu_init = dtrace_cpu_setup_initial; | |
16399 | dtrace_helpers_cleanup = dtrace_helpers_destroy; | |
16400 | dtrace_helpers_fork = dtrace_helpers_duplicate; | |
16401 | dtrace_cpustart_init = dtrace_suspend; | |
16402 | dtrace_cpustart_fini = dtrace_resume; | |
16403 | dtrace_debugger_init = dtrace_suspend; | |
16404 | dtrace_debugger_fini = dtrace_resume; | |
2d21ac55 A |
16405 | |
16406 | register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); | |
16407 | ||
16408 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
16409 | ||
16410 | dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, | |
16411 | NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); | |
16412 | dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, | |
16413 | UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, | |
16414 | VM_SLEEP | VMC_IDENTIFIER); | |
16415 | dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, | |
16416 | 1, INT_MAX, 0); | |
16417 | ||
16418 | dtrace_state_cache = kmem_cache_create("dtrace_state_cache", | |
c910b4d9 | 16419 | sizeof (dtrace_dstate_percpu_t) * (int)NCPU, DTRACE_STATE_ALIGN, |
2d21ac55 A |
16420 | NULL, NULL, NULL, NULL, NULL, 0); |
16421 | ||
16422 | lck_mtx_assert(&cpu_lock, LCK_MTX_ASSERT_OWNED); | |
2d21ac55 A |
16423 | dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), |
16424 | offsetof(dtrace_probe_t, dtpr_nextmod), | |
16425 | offsetof(dtrace_probe_t, dtpr_prevmod)); | |
16426 | ||
16427 | dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), | |
16428 | offsetof(dtrace_probe_t, dtpr_nextfunc), | |
16429 | offsetof(dtrace_probe_t, dtpr_prevfunc)); | |
16430 | ||
16431 | dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), | |
16432 | offsetof(dtrace_probe_t, dtpr_nextname), | |
16433 | offsetof(dtrace_probe_t, dtpr_prevname)); | |
16434 | ||
16435 | if (dtrace_retain_max < 1) { | |
16436 | cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " | |
16437 | "setting to 1", dtrace_retain_max); | |
16438 | dtrace_retain_max = 1; | |
16439 | } | |
16440 | ||
16441 | /* | |
16442 | * Now discover our toxic ranges. | |
16443 | */ | |
16444 | dtrace_toxic_ranges(dtrace_toxrange_add); | |
16445 | ||
16446 | /* | |
16447 | * Before we register ourselves as a provider to our own framework, | |
16448 | * we would like to assert that dtrace_provider is NULL -- but that's | |
16449 | * not true if we were loaded as a dependency of a DTrace provider. | |
16450 | * Once we've registered, we can assert that dtrace_provider is our | |
16451 | * pseudo provider. | |
16452 | */ | |
16453 | (void) dtrace_register("dtrace", &dtrace_provider_attr, | |
16454 | DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); | |
16455 | ||
16456 | ASSERT(dtrace_provider != NULL); | |
16457 | ASSERT((dtrace_provider_id_t)dtrace_provider == id); | |
16458 | ||
16459 | #if !defined(__APPLE__) | |
16460 | dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) | |
16461 | dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); | |
16462 | dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) | |
16463 | dtrace_provider, NULL, NULL, "END", 0, NULL); | |
16464 | dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) | |
16465 | dtrace_provider, NULL, NULL, "ERROR", 1, NULL); | |
16466 | #elif defined(__ppc__) || defined(__ppc64__) | |
16467 | dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) | |
16468 | dtrace_provider, NULL, NULL, "BEGIN", 2, NULL); | |
16469 | dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) | |
16470 | dtrace_provider, NULL, NULL, "END", 1, NULL); | |
16471 | dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) | |
16472 | dtrace_provider, NULL, NULL, "ERROR", 4, NULL); | |
16473 | #elif (defined(__i386__) || defined (__x86_64__)) | |
16474 | dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) | |
16475 | dtrace_provider, NULL, NULL, "BEGIN", 1, NULL); | |
16476 | dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) | |
16477 | dtrace_provider, NULL, NULL, "END", 0, NULL); | |
16478 | dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) | |
16479 | dtrace_provider, NULL, NULL, "ERROR", 3, NULL); | |
2d21ac55 A |
16480 | #else |
16481 | #error Unknown Architecture | |
16482 | #endif /* __APPLE__ */ | |
16483 | ||
16484 | dtrace_anon_property(); | |
16485 | lck_mtx_unlock(&cpu_lock); | |
16486 | ||
16487 | /* | |
16488 | * If DTrace helper tracing is enabled, we need to allocate the | |
16489 | * trace buffer and initialize the values. | |
16490 | */ | |
16491 | if (dtrace_helptrace_enabled) { | |
16492 | ASSERT(dtrace_helptrace_buffer == NULL); | |
16493 | dtrace_helptrace_buffer = | |
16494 | kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); | |
16495 | dtrace_helptrace_next = 0; | |
16496 | } | |
16497 | ||
16498 | /* | |
16499 | * If there are already providers, we must ask them to provide their | |
16500 | * probes, and then match any anonymous enabling against them. Note | |
16501 | * that there should be no other retained enablings at this time: | |
16502 | * the only retained enablings at this time should be the anonymous | |
16503 | * enabling. | |
16504 | */ | |
16505 | if (dtrace_anon.dta_enabling != NULL) { | |
16506 | ASSERT(dtrace_retained == dtrace_anon.dta_enabling); | |
16507 | ||
16508 | dtrace_enabling_provide(NULL); | |
16509 | state = dtrace_anon.dta_state; | |
16510 | ||
16511 | /* | |
16512 | * We couldn't hold cpu_lock across the above call to | |
16513 | * dtrace_enabling_provide(), but we must hold it to actually | |
16514 | * enable the probes. We have to drop all of our locks, pick | |
16515 | * up cpu_lock, and regain our locks before matching the | |
16516 | * retained anonymous enabling. | |
16517 | */ | |
16518 | lck_mtx_unlock(&dtrace_lock); | |
16519 | lck_mtx_unlock(&dtrace_provider_lock); | |
16520 | ||
16521 | lck_mtx_lock(&cpu_lock); | |
16522 | lck_mtx_lock(&dtrace_provider_lock); | |
16523 | lck_mtx_lock(&dtrace_lock); | |
16524 | ||
16525 | if ((enab = dtrace_anon.dta_enabling) != NULL) | |
16526 | (void) dtrace_enabling_match(enab, NULL); | |
16527 | ||
16528 | lck_mtx_unlock(&cpu_lock); | |
16529 | } | |
16530 | ||
16531 | lck_mtx_unlock(&dtrace_lock); | |
16532 | lck_mtx_unlock(&dtrace_provider_lock); | |
16533 | ||
16534 | if (state != NULL) { | |
16535 | /* | |
16536 | * If we created any anonymous state, set it going now. | |
16537 | */ | |
16538 | (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); | |
16539 | } | |
16540 | ||
16541 | return (DDI_SUCCESS); | |
16542 | } | |
16543 | ||
2d21ac55 A |
16544 | /*ARGSUSED*/ |
16545 | static int | |
16546 | dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) | |
16547 | { | |
16548 | #pragma unused(flag, otyp) | |
16549 | dtrace_state_t *state; | |
16550 | uint32_t priv; | |
16551 | uid_t uid; | |
16552 | zoneid_t zoneid; | |
b0d623f7 A |
16553 | #if defined (__APPLE__) |
16554 | int rv; | |
16555 | #endif /* __APPLE__ */ | |
2d21ac55 A |
16556 | |
16557 | #if !defined(__APPLE__) | |
16558 | if (getminor(*devp) == DTRACEMNRN_HELPER) | |
16559 | return (0); | |
16560 | ||
16561 | /* | |
16562 | * If this wasn't an open with the "helper" minor, then it must be | |
16563 | * the "dtrace" minor. | |
16564 | */ | |
b0d623f7 A |
16565 | if (getminor(*devp) != DTRACEMNRN_DTRACE) |
16566 | return (ENXIO); | |
2d21ac55 A |
16567 | #else |
16568 | /* Darwin puts Helper on its own major device. */ | |
16569 | #endif /* __APPLE__ */ | |
16570 | ||
16571 | /* | |
16572 | * If no DTRACE_PRIV_* bits are set in the credential, then the | |
16573 | * caller lacks sufficient permission to do anything with DTrace. | |
16574 | */ | |
16575 | dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); | |
16576 | if (priv == DTRACE_PRIV_NONE) | |
16577 | return (EACCES); | |
16578 | ||
16579 | #if defined(__APPLE__) | |
16580 | /* | |
16581 | * We delay the initialization of fasttrap as late as possible. | |
16582 | * It certainly can't be later than now! | |
16583 | */ | |
16584 | fasttrap_init(); | |
16585 | #endif /* __APPLE__ */ | |
16586 | ||
16587 | /* | |
16588 | * Ask all providers to provide all their probes. | |
16589 | */ | |
16590 | lck_mtx_lock(&dtrace_provider_lock); | |
16591 | dtrace_probe_provide(NULL, NULL); | |
16592 | lck_mtx_unlock(&dtrace_provider_lock); | |
16593 | ||
16594 | lck_mtx_lock(&cpu_lock); | |
16595 | lck_mtx_lock(&dtrace_lock); | |
16596 | dtrace_opens++; | |
16597 | dtrace_membar_producer(); | |
16598 | ||
16599 | /* | |
16600 | * If the kernel debugger is active (that is, if the kernel debugger | |
16601 | * modified text in some way), we won't allow the open. | |
16602 | */ | |
16603 | if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { | |
16604 | dtrace_opens--; | |
16605 | lck_mtx_unlock(&cpu_lock); | |
16606 | lck_mtx_unlock(&dtrace_lock); | |
16607 | return (EBUSY); | |
16608 | } | |
16609 | ||
b0d623f7 | 16610 | #if !defined(__APPLE__) |
2d21ac55 A |
16611 | state = dtrace_state_create(devp, cred_p); |
16612 | lck_mtx_unlock(&cpu_lock); | |
16613 | ||
16614 | if (state == NULL) { | |
16615 | if (--dtrace_opens == 0) | |
16616 | (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); | |
16617 | lck_mtx_unlock(&dtrace_lock); | |
16618 | return (EAGAIN); | |
16619 | } | |
b0d623f7 A |
16620 | |
16621 | lck_mtx_unlock(&dtrace_lock); | |
16622 | #else | |
16623 | rv = dtrace_state_create(devp, cred_p, &state); | |
16624 | lck_mtx_unlock(&cpu_lock); | |
2d21ac55 | 16625 | |
b0d623f7 A |
16626 | if (rv != 0 || state == NULL) { |
16627 | if (--dtrace_opens == 0) | |
16628 | (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); | |
16629 | lck_mtx_unlock(&dtrace_lock); | |
16630 | /* propagate EAGAIN or ERESTART */ | |
16631 | return (rv); | |
16632 | } | |
16633 | ||
2d21ac55 A |
16634 | lck_mtx_unlock(&dtrace_lock); |
16635 | ||
2d21ac55 A |
16636 | lck_rw_lock_exclusive(&dtrace_dof_mode_lock); |
16637 | ||
16638 | /* | |
16639 | * If we are currently lazy, transition states. | |
16640 | * | |
16641 | * Unlike dtrace_close, we do not need to check the | |
16642 | * value of dtrace_opens, as any positive value (and | |
16643 | * we count as 1) means we transition states. | |
16644 | */ | |
16645 | if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON) { | |
16646 | dtrace_dof_mode = DTRACE_DOF_MODE_LAZY_OFF; | |
16647 | ||
16648 | /* | |
16649 | * Iterate all existing processes and load lazy dofs. | |
16650 | */ | |
16651 | proc_iterate(PROC_ALLPROCLIST | PROC_NOWAITTRANS, | |
16652 | dtrace_lazy_dofs_proc_iterate_doit, | |
16653 | NULL, | |
16654 | dtrace_lazy_dofs_proc_iterate_filter, | |
16655 | NULL); | |
16656 | } | |
16657 | ||
16658 | lck_rw_unlock_exclusive(&dtrace_dof_mode_lock); | |
b0d623f7 | 16659 | #endif /* __APPLE__ */ |
2d21ac55 A |
16660 | |
16661 | return (0); | |
16662 | } | |
16663 | ||
16664 | /*ARGSUSED*/ | |
16665 | static int | |
16666 | dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) | |
16667 | { | |
b0d623f7 | 16668 | #pragma unused(flag, otyp, cred_p) /* __APPLE__ */ |
2d21ac55 A |
16669 | minor_t minor = getminor(dev); |
16670 | dtrace_state_t *state; | |
16671 | ||
16672 | #if !defined(__APPLE__) | |
16673 | if (minor == DTRACEMNRN_HELPER) | |
16674 | return (0); | |
16675 | #else | |
16676 | /* Darwin puts Helper on its own major device. */ | |
16677 | #endif /* __APPLE__ */ | |
16678 | ||
16679 | state = ddi_get_soft_state(dtrace_softstate, minor); | |
16680 | ||
16681 | lck_mtx_lock(&cpu_lock); | |
16682 | lck_mtx_lock(&dtrace_lock); | |
16683 | ||
16684 | if (state->dts_anon) { | |
16685 | /* | |
16686 | * There is anonymous state. Destroy that first. | |
16687 | */ | |
16688 | ASSERT(dtrace_anon.dta_state == NULL); | |
16689 | dtrace_state_destroy(state->dts_anon); | |
16690 | } | |
16691 | ||
16692 | dtrace_state_destroy(state); | |
16693 | ASSERT(dtrace_opens > 0); | |
16694 | if (--dtrace_opens == 0) | |
16695 | (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); | |
16696 | ||
16697 | lck_mtx_unlock(&dtrace_lock); | |
16698 | lck_mtx_unlock(&cpu_lock); | |
16699 | ||
16700 | #if defined(__APPLE__) | |
16701 | ||
16702 | /* | |
16703 | * Lock ordering requires the dof mode lock be taken before | |
16704 | * the dtrace_lock. | |
16705 | */ | |
16706 | lck_rw_lock_exclusive(&dtrace_dof_mode_lock); | |
16707 | lck_mtx_lock(&dtrace_lock); | |
16708 | ||
16709 | /* | |
16710 | * If we are currently lazy-off, and this is the last close, transition to | |
16711 | * lazy state. | |
16712 | */ | |
16713 | if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_OFF && dtrace_opens == 0) { | |
16714 | dtrace_dof_mode = DTRACE_DOF_MODE_LAZY_ON; | |
16715 | } | |
16716 | ||
16717 | lck_mtx_unlock(&dtrace_lock); | |
16718 | lck_rw_unlock_exclusive(&dtrace_dof_mode_lock); | |
b0d623f7 | 16719 | #endif /* __APPLE__ */ |
2d21ac55 A |
16720 | |
16721 | return (0); | |
16722 | } | |
16723 | ||
b0d623f7 | 16724 | #if !defined(__APPLE__) |
2d21ac55 A |
16725 | /*ARGSUSED*/ |
16726 | static int | |
b0d623f7 | 16727 | dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) |
2d21ac55 | 16728 | { |
b0d623f7 A |
16729 | int rval; |
16730 | dof_helper_t help, *dhp = NULL; | |
2d21ac55 A |
16731 | |
16732 | switch (cmd) { | |
b0d623f7 A |
16733 | case DTRACEHIOC_ADDDOF: |
16734 | if (copyin((void *)arg, &help, sizeof (help)) != 0) { | |
16735 | dtrace_dof_error(NULL, "failed to copyin DOF helper"); | |
16736 | return (EFAULT); | |
16737 | } | |
2d21ac55 | 16738 | |
b0d623f7 A |
16739 | dhp = &help; |
16740 | arg = (intptr_t)help.dofhp_dof; | |
16741 | /*FALLTHROUGH*/ | |
2d21ac55 | 16742 | |
b0d623f7 A |
16743 | case DTRACEHIOC_ADD: { |
16744 | dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); | |
2d21ac55 | 16745 | |
b0d623f7 A |
16746 | if (dof == NULL) |
16747 | return (rval); | |
2d21ac55 | 16748 | |
b0d623f7 A |
16749 | mutex_enter(&dtrace_lock); |
16750 | ||
16751 | /* | |
16752 | * dtrace_helper_slurp() takes responsibility for the dof -- | |
16753 | * it may free it now or it may save it and free it later. | |
16754 | */ | |
16755 | if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { | |
16756 | *rv = rval; | |
16757 | rval = 0; | |
16758 | } else { | |
16759 | rval = EINVAL; | |
2d21ac55 A |
16760 | } |
16761 | ||
b0d623f7 A |
16762 | mutex_exit(&dtrace_lock); |
16763 | return (rval); | |
16764 | } | |
2d21ac55 | 16765 | |
b0d623f7 A |
16766 | case DTRACEHIOC_REMOVE: { |
16767 | mutex_enter(&dtrace_lock); | |
16768 | rval = dtrace_helper_destroygen(arg); | |
16769 | mutex_exit(&dtrace_lock); | |
2d21ac55 | 16770 | |
b0d623f7 A |
16771 | return (rval); |
16772 | } | |
2d21ac55 | 16773 | |
b0d623f7 A |
16774 | default: |
16775 | break; | |
2d21ac55 A |
16776 | } |
16777 | ||
b0d623f7 | 16778 | return (ENOTTY); |
2d21ac55 | 16779 | } |
2d21ac55 A |
16780 | |
16781 | /*ARGSUSED*/ | |
16782 | static int | |
b0d623f7 | 16783 | dtrace_ioctl(dev_t dev, u_long cmd, intptr_t arg, int md, cred_t *cr, int *rv) |
2d21ac55 A |
16784 | { |
16785 | minor_t minor = getminor(dev); | |
16786 | dtrace_state_t *state; | |
16787 | int rval; | |
16788 | ||
2d21ac55 A |
16789 | if (minor == DTRACEMNRN_HELPER) |
16790 | return (dtrace_ioctl_helper(cmd, arg, rv)); | |
2d21ac55 A |
16791 | |
16792 | state = ddi_get_soft_state(dtrace_softstate, minor); | |
16793 | ||
16794 | if (state->dts_anon) { | |
16795 | ASSERT(dtrace_anon.dta_state == NULL); | |
16796 | state = state->dts_anon; | |
16797 | } | |
16798 | ||
16799 | switch (cmd) { | |
16800 | case DTRACEIOC_PROVIDER: { | |
16801 | dtrace_providerdesc_t pvd; | |
16802 | dtrace_provider_t *pvp; | |
16803 | ||
16804 | if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) | |
16805 | return (EFAULT); | |
16806 | ||
16807 | pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; | |
16808 | lck_mtx_lock(&dtrace_provider_lock); | |
16809 | ||
16810 | for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { | |
16811 | if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) | |
16812 | break; | |
16813 | } | |
16814 | ||
16815 | lck_mtx_unlock(&dtrace_provider_lock); | |
16816 | ||
16817 | if (pvp == NULL) | |
16818 | return (ESRCH); | |
16819 | ||
16820 | bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); | |
16821 | bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); | |
16822 | if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) | |
16823 | return (EFAULT); | |
16824 | ||
16825 | return (0); | |
16826 | } | |
16827 | ||
16828 | case DTRACEIOC_EPROBE: { | |
16829 | dtrace_eprobedesc_t epdesc; | |
16830 | dtrace_ecb_t *ecb; | |
16831 | dtrace_action_t *act; | |
16832 | void *buf; | |
16833 | size_t size; | |
16834 | uintptr_t dest; | |
16835 | int nrecs; | |
16836 | ||
16837 | if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) | |
16838 | return (EFAULT); | |
16839 | ||
16840 | lck_mtx_lock(&dtrace_lock); | |
16841 | ||
16842 | if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { | |
16843 | lck_mtx_unlock(&dtrace_lock); | |
16844 | return (EINVAL); | |
16845 | } | |
16846 | ||
16847 | if (ecb->dte_probe == NULL) { | |
16848 | lck_mtx_unlock(&dtrace_lock); | |
16849 | return (EINVAL); | |
16850 | } | |
16851 | ||
16852 | epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; | |
16853 | epdesc.dtepd_uarg = ecb->dte_uarg; | |
16854 | epdesc.dtepd_size = ecb->dte_size; | |
16855 | ||
16856 | nrecs = epdesc.dtepd_nrecs; | |
16857 | epdesc.dtepd_nrecs = 0; | |
16858 | for (act = ecb->dte_action; act != NULL; act = act->dta_next) { | |
16859 | if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) | |
16860 | continue; | |
16861 | ||
16862 | epdesc.dtepd_nrecs++; | |
16863 | } | |
16864 | ||
16865 | /* | |
16866 | * Now that we have the size, we need to allocate a temporary | |
16867 | * buffer in which to store the complete description. We need | |
16868 | * the temporary buffer to be able to drop dtrace_lock() | |
16869 | * across the copyout(), below. | |
16870 | */ | |
16871 | size = sizeof (dtrace_eprobedesc_t) + | |
16872 | (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); | |
16873 | ||
16874 | buf = kmem_alloc(size, KM_SLEEP); | |
16875 | dest = (uintptr_t)buf; | |
16876 | ||
16877 | bcopy(&epdesc, (void *)dest, sizeof (epdesc)); | |
16878 | dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); | |
16879 | ||
16880 | for (act = ecb->dte_action; act != NULL; act = act->dta_next) { | |
16881 | if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) | |
16882 | continue; | |
16883 | ||
16884 | if (nrecs-- == 0) | |
16885 | break; | |
16886 | ||
16887 | bcopy(&act->dta_rec, (void *)dest, | |
16888 | sizeof (dtrace_recdesc_t)); | |
16889 | dest += sizeof (dtrace_recdesc_t); | |
16890 | } | |
16891 | ||
16892 | lck_mtx_unlock(&dtrace_lock); | |
16893 | ||
16894 | if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { | |
16895 | kmem_free(buf, size); | |
16896 | return (EFAULT); | |
16897 | } | |
16898 | ||
16899 | kmem_free(buf, size); | |
16900 | return (0); | |
16901 | } | |
16902 | ||
16903 | case DTRACEIOC_AGGDESC: { | |
16904 | dtrace_aggdesc_t aggdesc; | |
16905 | dtrace_action_t *act; | |
16906 | dtrace_aggregation_t *agg; | |
16907 | int nrecs; | |
16908 | uint32_t offs; | |
16909 | dtrace_recdesc_t *lrec; | |
16910 | void *buf; | |
16911 | size_t size; | |
16912 | uintptr_t dest; | |
16913 | ||
16914 | if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) | |
16915 | return (EFAULT); | |
16916 | ||
16917 | lck_mtx_lock(&dtrace_lock); | |
16918 | ||
16919 | if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { | |
16920 | lck_mtx_unlock(&dtrace_lock); | |
16921 | return (EINVAL); | |
16922 | } | |
16923 | ||
16924 | aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; | |
16925 | ||
16926 | nrecs = aggdesc.dtagd_nrecs; | |
16927 | aggdesc.dtagd_nrecs = 0; | |
16928 | ||
16929 | offs = agg->dtag_base; | |
16930 | lrec = &agg->dtag_action.dta_rec; | |
16931 | aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; | |
16932 | ||
16933 | for (act = agg->dtag_first; ; act = act->dta_next) { | |
16934 | ASSERT(act->dta_intuple || | |
16935 | DTRACEACT_ISAGG(act->dta_kind)); | |
16936 | ||
16937 | /* | |
16938 | * If this action has a record size of zero, it | |
16939 | * denotes an argument to the aggregating action. | |
16940 | * Because the presence of this record doesn't (or | |
16941 | * shouldn't) affect the way the data is interpreted, | |
16942 | * we don't copy it out to save user-level the | |
16943 | * confusion of dealing with a zero-length record. | |
16944 | */ | |
16945 | if (act->dta_rec.dtrd_size == 0) { | |
16946 | ASSERT(agg->dtag_hasarg); | |
16947 | continue; | |
16948 | } | |
16949 | ||
16950 | aggdesc.dtagd_nrecs++; | |
16951 | ||
16952 | if (act == &agg->dtag_action) | |
16953 | break; | |
16954 | } | |
16955 | ||
16956 | /* | |
16957 | * Now that we have the size, we need to allocate a temporary | |
16958 | * buffer in which to store the complete description. We need | |
16959 | * the temporary buffer to be able to drop dtrace_lock() | |
16960 | * across the copyout(), below. | |
16961 | */ | |
16962 | size = sizeof (dtrace_aggdesc_t) + | |
16963 | (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); | |
16964 | ||
16965 | buf = kmem_alloc(size, KM_SLEEP); | |
16966 | dest = (uintptr_t)buf; | |
16967 | ||
16968 | bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); | |
16969 | dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); | |
16970 | ||
16971 | for (act = agg->dtag_first; ; act = act->dta_next) { | |
16972 | dtrace_recdesc_t rec = act->dta_rec; | |
16973 | ||
16974 | /* | |
16975 | * See the comment in the above loop for why we pass | |
16976 | * over zero-length records. | |
16977 | */ | |
16978 | if (rec.dtrd_size == 0) { | |
16979 | ASSERT(agg->dtag_hasarg); | |
16980 | continue; | |
16981 | } | |
16982 | ||
16983 | if (nrecs-- == 0) | |
16984 | break; | |
16985 | ||
16986 | rec.dtrd_offset -= offs; | |
16987 | bcopy(&rec, (void *)dest, sizeof (rec)); | |
16988 | dest += sizeof (dtrace_recdesc_t); | |
16989 | ||
16990 | if (act == &agg->dtag_action) | |
16991 | break; | |
16992 | } | |
16993 | ||
16994 | lck_mtx_unlock(&dtrace_lock); | |
16995 | ||
16996 | if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { | |
16997 | kmem_free(buf, size); | |
16998 | return (EFAULT); | |
16999 | } | |
17000 | ||
17001 | kmem_free(buf, size); | |
17002 | return (0); | |
17003 | } | |
17004 | ||
17005 | case DTRACEIOC_ENABLE: { | |
17006 | dof_hdr_t *dof; | |
17007 | dtrace_enabling_t *enab = NULL; | |
17008 | dtrace_vstate_t *vstate; | |
17009 | int err = 0; | |
17010 | ||
17011 | *rv = 0; | |
17012 | ||
17013 | /* | |
17014 | * If a NULL argument has been passed, we take this as our | |
17015 | * cue to reevaluate our enablings. | |
17016 | */ | |
17017 | if (arg == NULL) { | |
b0d623f7 | 17018 | dtrace_enabling_matchall(); |
2d21ac55 | 17019 | |
b0d623f7 | 17020 | return (0); |
2d21ac55 A |
17021 | } |
17022 | ||
17023 | if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) | |
17024 | return (rval); | |
17025 | ||
17026 | lck_mtx_lock(&cpu_lock); | |
17027 | lck_mtx_lock(&dtrace_lock); | |
17028 | vstate = &state->dts_vstate; | |
17029 | ||
17030 | if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { | |
17031 | lck_mtx_unlock(&dtrace_lock); | |
17032 | lck_mtx_unlock(&cpu_lock); | |
17033 | dtrace_dof_destroy(dof); | |
17034 | return (EBUSY); | |
17035 | } | |
17036 | ||
17037 | if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { | |
17038 | lck_mtx_unlock(&dtrace_lock); | |
17039 | lck_mtx_unlock(&cpu_lock); | |
17040 | dtrace_dof_destroy(dof); | |
17041 | return (EINVAL); | |
17042 | } | |
17043 | ||
17044 | if ((rval = dtrace_dof_options(dof, state)) != 0) { | |
17045 | dtrace_enabling_destroy(enab); | |
17046 | lck_mtx_unlock(&dtrace_lock); | |
17047 | lck_mtx_unlock(&cpu_lock); | |
17048 | dtrace_dof_destroy(dof); | |
17049 | return (rval); | |
17050 | } | |
17051 | ||
17052 | if ((err = dtrace_enabling_match(enab, rv)) == 0) { | |
17053 | err = dtrace_enabling_retain(enab); | |
17054 | } else { | |
17055 | dtrace_enabling_destroy(enab); | |
17056 | } | |
17057 | ||
17058 | lck_mtx_unlock(&cpu_lock); | |
17059 | lck_mtx_unlock(&dtrace_lock); | |
17060 | dtrace_dof_destroy(dof); | |
17061 | ||
17062 | return (err); | |
17063 | } | |
17064 | ||
17065 | case DTRACEIOC_REPLICATE: { | |
17066 | dtrace_repldesc_t desc; | |
17067 | dtrace_probedesc_t *match = &desc.dtrpd_match; | |
17068 | dtrace_probedesc_t *create = &desc.dtrpd_create; | |
17069 | int err; | |
17070 | ||
17071 | if (copyin((void *)arg, &desc, sizeof (desc)) != 0) | |
17072 | return (EFAULT); | |
17073 | ||
17074 | match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
17075 | match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
17076 | match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
17077 | match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
17078 | ||
17079 | create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
17080 | create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
17081 | create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
17082 | create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
17083 | ||
17084 | lck_mtx_lock(&dtrace_lock); | |
17085 | err = dtrace_enabling_replicate(state, match, create); | |
17086 | lck_mtx_unlock(&dtrace_lock); | |
17087 | ||
17088 | return (err); | |
17089 | } | |
17090 | ||
17091 | case DTRACEIOC_PROBEMATCH: | |
17092 | case DTRACEIOC_PROBES: { | |
17093 | dtrace_probe_t *probe = NULL; | |
17094 | dtrace_probedesc_t desc; | |
17095 | dtrace_probekey_t pkey; | |
17096 | dtrace_id_t i; | |
17097 | int m = 0; | |
17098 | uint32_t priv; | |
17099 | uid_t uid; | |
17100 | zoneid_t zoneid; | |
17101 | ||
17102 | if (copyin((void *)arg, &desc, sizeof (desc)) != 0) | |
17103 | return (EFAULT); | |
17104 | ||
17105 | desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
17106 | desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
17107 | desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
17108 | desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
17109 | ||
17110 | /* | |
17111 | * Before we attempt to match this probe, we want to give | |
17112 | * all providers the opportunity to provide it. | |
17113 | */ | |
17114 | if (desc.dtpd_id == DTRACE_IDNONE) { | |
17115 | lck_mtx_lock(&dtrace_provider_lock); | |
17116 | dtrace_probe_provide(&desc, NULL); | |
17117 | lck_mtx_unlock(&dtrace_provider_lock); | |
17118 | desc.dtpd_id++; | |
17119 | } | |
17120 | ||
17121 | if (cmd == DTRACEIOC_PROBEMATCH) { | |
17122 | dtrace_probekey(&desc, &pkey); | |
17123 | pkey.dtpk_id = DTRACE_IDNONE; | |
17124 | } | |
17125 | ||
17126 | dtrace_cred2priv(cr, &priv, &uid, &zoneid); | |
17127 | ||
17128 | lck_mtx_lock(&dtrace_lock); | |
17129 | ||
17130 | if (cmd == DTRACEIOC_PROBEMATCH) { | |
17131 | for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { | |
17132 | if ((probe = dtrace_probes[i - 1]) != NULL && | |
17133 | (m = dtrace_match_probe(probe, &pkey, | |
17134 | priv, uid, zoneid)) != 0) | |
17135 | break; | |
17136 | } | |
17137 | ||
17138 | if (m < 0) { | |
17139 | lck_mtx_unlock(&dtrace_lock); | |
17140 | return (EINVAL); | |
17141 | } | |
17142 | ||
17143 | } else { | |
17144 | for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { | |
17145 | if ((probe = dtrace_probes[i - 1]) != NULL && | |
17146 | dtrace_match_priv(probe, priv, uid, zoneid)) | |
17147 | break; | |
17148 | } | |
17149 | } | |
17150 | ||
17151 | if (probe == NULL) { | |
17152 | lck_mtx_unlock(&dtrace_lock); | |
17153 | return (ESRCH); | |
17154 | } | |
17155 | ||
17156 | dtrace_probe_description(probe, &desc); | |
17157 | lck_mtx_unlock(&dtrace_lock); | |
17158 | ||
17159 | if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) | |
17160 | return (EFAULT); | |
17161 | ||
17162 | return (0); | |
17163 | } | |
17164 | ||
17165 | case DTRACEIOC_PROBEARG: { | |
17166 | dtrace_argdesc_t desc; | |
17167 | dtrace_probe_t *probe; | |
17168 | dtrace_provider_t *prov; | |
17169 | ||
17170 | if (copyin((void *)arg, &desc, sizeof (desc)) != 0) | |
17171 | return (EFAULT); | |
17172 | ||
17173 | if (desc.dtargd_id == DTRACE_IDNONE) | |
17174 | return (EINVAL); | |
17175 | ||
17176 | if (desc.dtargd_ndx == DTRACE_ARGNONE) | |
17177 | return (EINVAL); | |
17178 | ||
17179 | lck_mtx_lock(&dtrace_provider_lock); | |
17180 | lck_mtx_lock(&mod_lock); | |
17181 | lck_mtx_lock(&dtrace_lock); | |
17182 | ||
17183 | if (desc.dtargd_id > dtrace_nprobes) { | |
17184 | lck_mtx_unlock(&dtrace_lock); | |
17185 | lck_mtx_unlock(&mod_lock); | |
17186 | lck_mtx_unlock(&dtrace_provider_lock); | |
17187 | return (EINVAL); | |
17188 | } | |
17189 | ||
17190 | if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { | |
17191 | lck_mtx_unlock(&dtrace_lock); | |
17192 | lck_mtx_unlock(&mod_lock); | |
17193 | lck_mtx_unlock(&dtrace_provider_lock); | |
17194 | return (EINVAL); | |
17195 | } | |
17196 | ||
17197 | lck_mtx_unlock(&dtrace_lock); | |
17198 | ||
17199 | prov = probe->dtpr_provider; | |
17200 | ||
17201 | if (prov->dtpv_pops.dtps_getargdesc == NULL) { | |
17202 | /* | |
17203 | * There isn't any typed information for this probe. | |
17204 | * Set the argument number to DTRACE_ARGNONE. | |
17205 | */ | |
17206 | desc.dtargd_ndx = DTRACE_ARGNONE; | |
17207 | } else { | |
17208 | desc.dtargd_native[0] = '\0'; | |
17209 | desc.dtargd_xlate[0] = '\0'; | |
17210 | desc.dtargd_mapping = desc.dtargd_ndx; | |
17211 | ||
17212 | prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, | |
17213 | probe->dtpr_id, probe->dtpr_arg, &desc); | |
17214 | } | |
17215 | ||
17216 | lck_mtx_unlock(&mod_lock); | |
17217 | lck_mtx_unlock(&dtrace_provider_lock); | |
17218 | ||
17219 | if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) | |
17220 | return (EFAULT); | |
17221 | ||
17222 | return (0); | |
17223 | } | |
17224 | ||
17225 | case DTRACEIOC_GO: { | |
17226 | processorid_t cpuid; | |
17227 | rval = dtrace_state_go(state, &cpuid); | |
17228 | ||
17229 | if (rval != 0) | |
17230 | return (rval); | |
17231 | ||
17232 | if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) | |
17233 | return (EFAULT); | |
17234 | ||
17235 | return (0); | |
17236 | } | |
17237 | ||
17238 | case DTRACEIOC_STOP: { | |
17239 | processorid_t cpuid; | |
17240 | ||
17241 | lck_mtx_lock(&dtrace_lock); | |
17242 | rval = dtrace_state_stop(state, &cpuid); | |
17243 | lck_mtx_unlock(&dtrace_lock); | |
17244 | ||
17245 | if (rval != 0) | |
17246 | return (rval); | |
17247 | ||
17248 | if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) | |
17249 | return (EFAULT); | |
17250 | ||
17251 | return (0); | |
17252 | } | |
17253 | ||
17254 | case DTRACEIOC_DOFGET: { | |
17255 | dof_hdr_t hdr, *dof; | |
17256 | uint64_t len; | |
17257 | ||
17258 | if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) | |
17259 | return (EFAULT); | |
17260 | ||
17261 | lck_mtx_lock(&dtrace_lock); | |
17262 | dof = dtrace_dof_create(state); | |
17263 | lck_mtx_unlock(&dtrace_lock); | |
17264 | ||
17265 | len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); | |
17266 | rval = copyout(dof, (void *)arg, len); | |
17267 | dtrace_dof_destroy(dof); | |
17268 | ||
17269 | return (rval == 0 ? 0 : EFAULT); | |
17270 | } | |
17271 | ||
17272 | case DTRACEIOC_AGGSNAP: | |
17273 | case DTRACEIOC_BUFSNAP: { | |
17274 | dtrace_bufdesc_t desc; | |
17275 | caddr_t cached; | |
17276 | dtrace_buffer_t *buf; | |
17277 | ||
17278 | if (copyin((void *)arg, &desc, sizeof (desc)) != 0) | |
17279 | return (EFAULT); | |
17280 | ||
b0d623f7 | 17281 | if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) |
2d21ac55 A |
17282 | return (EINVAL); |
17283 | ||
17284 | lck_mtx_lock(&dtrace_lock); | |
17285 | ||
17286 | if (cmd == DTRACEIOC_BUFSNAP) { | |
17287 | buf = &state->dts_buffer[desc.dtbd_cpu]; | |
17288 | } else { | |
17289 | buf = &state->dts_aggbuffer[desc.dtbd_cpu]; | |
17290 | } | |
17291 | ||
17292 | if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { | |
17293 | size_t sz = buf->dtb_offset; | |
17294 | ||
17295 | if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { | |
17296 | lck_mtx_unlock(&dtrace_lock); | |
17297 | return (EBUSY); | |
17298 | } | |
17299 | ||
17300 | /* | |
17301 | * If this buffer has already been consumed, we're | |
17302 | * going to indicate that there's nothing left here | |
17303 | * to consume. | |
17304 | */ | |
17305 | if (buf->dtb_flags & DTRACEBUF_CONSUMED) { | |
17306 | lck_mtx_unlock(&dtrace_lock); | |
17307 | ||
17308 | desc.dtbd_size = 0; | |
17309 | desc.dtbd_drops = 0; | |
17310 | desc.dtbd_errors = 0; | |
17311 | desc.dtbd_oldest = 0; | |
17312 | sz = sizeof (desc); | |
17313 | ||
17314 | if (copyout(&desc, (void *)arg, sz) != 0) | |
17315 | return (EFAULT); | |
17316 | ||
17317 | return (0); | |
17318 | } | |
17319 | ||
17320 | /* | |
17321 | * If this is a ring buffer that has wrapped, we want | |
17322 | * to copy the whole thing out. | |
17323 | */ | |
17324 | if (buf->dtb_flags & DTRACEBUF_WRAPPED) { | |
17325 | dtrace_buffer_polish(buf); | |
17326 | sz = buf->dtb_size; | |
17327 | } | |
17328 | ||
17329 | if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { | |
17330 | lck_mtx_unlock(&dtrace_lock); | |
17331 | return (EFAULT); | |
17332 | } | |
17333 | ||
17334 | desc.dtbd_size = sz; | |
17335 | desc.dtbd_drops = buf->dtb_drops; | |
17336 | desc.dtbd_errors = buf->dtb_errors; | |
17337 | desc.dtbd_oldest = buf->dtb_xamot_offset; | |
17338 | ||
17339 | lck_mtx_unlock(&dtrace_lock); | |
17340 | ||
17341 | if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) | |
17342 | return (EFAULT); | |
17343 | ||
17344 | buf->dtb_flags |= DTRACEBUF_CONSUMED; | |
17345 | ||
17346 | return (0); | |
17347 | } | |
17348 | ||
17349 | if (buf->dtb_tomax == NULL) { | |
17350 | ASSERT(buf->dtb_xamot == NULL); | |
17351 | lck_mtx_unlock(&dtrace_lock); | |
17352 | return (ENOENT); | |
17353 | } | |
17354 | ||
17355 | cached = buf->dtb_tomax; | |
17356 | ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); | |
17357 | ||
17358 | dtrace_xcall(desc.dtbd_cpu, | |
17359 | (dtrace_xcall_t)dtrace_buffer_switch, buf); | |
17360 | ||
17361 | state->dts_errors += buf->dtb_xamot_errors; | |
17362 | ||
17363 | /* | |
17364 | * If the buffers did not actually switch, then the cross call | |
17365 | * did not take place -- presumably because the given CPU is | |
17366 | * not in the ready set. If this is the case, we'll return | |
17367 | * ENOENT. | |
17368 | */ | |
17369 | if (buf->dtb_tomax == cached) { | |
17370 | ASSERT(buf->dtb_xamot != cached); | |
17371 | lck_mtx_unlock(&dtrace_lock); | |
17372 | return (ENOENT); | |
17373 | } | |
17374 | ||
17375 | ASSERT(cached == buf->dtb_xamot); | |
17376 | ||
17377 | /* | |
17378 | * We have our snapshot; now copy it out. | |
17379 | */ | |
17380 | if (copyout(buf->dtb_xamot, desc.dtbd_data, | |
17381 | buf->dtb_xamot_offset) != 0) { | |
17382 | lck_mtx_unlock(&dtrace_lock); | |
17383 | return (EFAULT); | |
17384 | } | |
17385 | ||
17386 | desc.dtbd_size = buf->dtb_xamot_offset; | |
17387 | desc.dtbd_drops = buf->dtb_xamot_drops; | |
17388 | desc.dtbd_errors = buf->dtb_xamot_errors; | |
17389 | desc.dtbd_oldest = 0; | |
17390 | ||
17391 | lck_mtx_unlock(&dtrace_lock); | |
17392 | ||
17393 | /* | |
17394 | * Finally, copy out the buffer description. | |
17395 | */ | |
17396 | if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) | |
17397 | return (EFAULT); | |
17398 | ||
17399 | return (0); | |
17400 | } | |
17401 | ||
17402 | case DTRACEIOC_CONF: { | |
17403 | dtrace_conf_t conf; | |
17404 | ||
17405 | bzero(&conf, sizeof (conf)); | |
17406 | conf.dtc_difversion = DIF_VERSION; | |
17407 | conf.dtc_difintregs = DIF_DIR_NREGS; | |
17408 | conf.dtc_diftupregs = DIF_DTR_NREGS; | |
17409 | conf.dtc_ctfmodel = CTF_MODEL_NATIVE; | |
17410 | ||
17411 | if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) | |
17412 | return (EFAULT); | |
17413 | ||
17414 | return (0); | |
17415 | } | |
17416 | ||
17417 | case DTRACEIOC_STATUS: { | |
17418 | dtrace_status_t stat; | |
17419 | dtrace_dstate_t *dstate; | |
17420 | int i, j; | |
17421 | uint64_t nerrs; | |
17422 | ||
17423 | /* | |
17424 | * See the comment in dtrace_state_deadman() for the reason | |
17425 | * for setting dts_laststatus to INT64_MAX before setting | |
17426 | * it to the correct value. | |
17427 | */ | |
17428 | state->dts_laststatus = INT64_MAX; | |
17429 | dtrace_membar_producer(); | |
17430 | state->dts_laststatus = dtrace_gethrtime(); | |
17431 | ||
17432 | bzero(&stat, sizeof (stat)); | |
17433 | ||
17434 | lck_mtx_lock(&dtrace_lock); | |
17435 | ||
17436 | if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { | |
17437 | lck_mtx_unlock(&dtrace_lock); | |
17438 | return (ENOENT); | |
17439 | } | |
17440 | ||
17441 | if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) | |
17442 | stat.dtst_exiting = 1; | |
17443 | ||
17444 | nerrs = state->dts_errors; | |
17445 | dstate = &state->dts_vstate.dtvs_dynvars; | |
17446 | ||
b0d623f7 | 17447 | for (i = 0; i < NCPU; i++) { |
2d21ac55 A |
17448 | dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; |
17449 | ||
17450 | stat.dtst_dyndrops += dcpu->dtdsc_drops; | |
17451 | stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; | |
17452 | stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; | |
17453 | ||
17454 | if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) | |
17455 | stat.dtst_filled++; | |
17456 | ||
17457 | nerrs += state->dts_buffer[i].dtb_errors; | |
17458 | ||
17459 | for (j = 0; j < state->dts_nspeculations; j++) { | |
17460 | dtrace_speculation_t *spec; | |
17461 | dtrace_buffer_t *buf; | |
17462 | ||
17463 | spec = &state->dts_speculations[j]; | |
17464 | buf = &spec->dtsp_buffer[i]; | |
17465 | stat.dtst_specdrops += buf->dtb_xamot_drops; | |
17466 | } | |
17467 | } | |
17468 | ||
17469 | stat.dtst_specdrops_busy = state->dts_speculations_busy; | |
17470 | stat.dtst_specdrops_unavail = state->dts_speculations_unavail; | |
17471 | stat.dtst_stkstroverflows = state->dts_stkstroverflows; | |
17472 | stat.dtst_dblerrors = state->dts_dblerrors; | |
17473 | stat.dtst_killed = | |
17474 | (state->dts_activity == DTRACE_ACTIVITY_KILLED); | |
17475 | stat.dtst_errors = nerrs; | |
17476 | ||
17477 | lck_mtx_unlock(&dtrace_lock); | |
17478 | ||
17479 | if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) | |
17480 | return (EFAULT); | |
17481 | ||
17482 | return (0); | |
17483 | } | |
17484 | ||
17485 | case DTRACEIOC_FORMAT: { | |
17486 | dtrace_fmtdesc_t fmt; | |
17487 | char *str; | |
17488 | int len; | |
17489 | ||
17490 | if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) | |
17491 | return (EFAULT); | |
17492 | ||
17493 | lck_mtx_lock(&dtrace_lock); | |
17494 | ||
17495 | if (fmt.dtfd_format == 0 || | |
17496 | fmt.dtfd_format > state->dts_nformats) { | |
17497 | lck_mtx_unlock(&dtrace_lock); | |
17498 | return (EINVAL); | |
17499 | } | |
17500 | ||
17501 | /* | |
17502 | * Format strings are allocated contiguously and they are | |
17503 | * never freed; if a format index is less than the number | |
17504 | * of formats, we can assert that the format map is non-NULL | |
17505 | * and that the format for the specified index is non-NULL. | |
17506 | */ | |
17507 | ASSERT(state->dts_formats != NULL); | |
17508 | str = state->dts_formats[fmt.dtfd_format - 1]; | |
17509 | ASSERT(str != NULL); | |
17510 | ||
17511 | len = strlen(str) + 1; | |
17512 | ||
17513 | if (len > fmt.dtfd_length) { | |
17514 | fmt.dtfd_length = len; | |
17515 | ||
17516 | if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { | |
17517 | lck_mtx_unlock(&dtrace_lock); | |
17518 | return (EINVAL); | |
17519 | } | |
17520 | } else { | |
17521 | if (copyout(str, fmt.dtfd_string, len) != 0) { | |
17522 | lck_mtx_unlock(&dtrace_lock); | |
17523 | return (EINVAL); | |
17524 | } | |
17525 | } | |
17526 | ||
17527 | lck_mtx_unlock(&dtrace_lock); | |
17528 | return (0); | |
17529 | } | |
17530 | ||
17531 | default: | |
17532 | break; | |
17533 | } | |
17534 | ||
17535 | return (ENOTTY); | |
17536 | } | |
b0d623f7 | 17537 | #else |
2d21ac55 A |
17538 | /*ARGSUSED*/ |
17539 | static int | |
b0d623f7 | 17540 | dtrace_ioctl_helper(u_long cmd, caddr_t arg, int *rv) |
2d21ac55 | 17541 | { |
b0d623f7 A |
17542 | #pragma unused(rv) |
17543 | /* | |
17544 | * Safe to check this outside the dof mode lock | |
17545 | */ | |
17546 | if (dtrace_dof_mode == DTRACE_DOF_MODE_NEVER) | |
17547 | return KERN_SUCCESS; | |
2d21ac55 A |
17548 | |
17549 | switch (cmd) { | |
b0d623f7 A |
17550 | case DTRACEHIOC_ADDDOF: { |
17551 | dof_helper_t *dhp = NULL; | |
17552 | size_t dof_ioctl_data_size; | |
17553 | dof_ioctl_data_t* multi_dof; | |
17554 | unsigned int i; | |
17555 | int rval = 0; | |
17556 | user_addr_t user_address = *(user_addr_t*)arg; | |
17557 | uint64_t dof_count; | |
17558 | int multi_dof_claimed = 0; | |
17559 | proc_t* p = current_proc(); | |
2d21ac55 | 17560 | |
b0d623f7 A |
17561 | /* |
17562 | * Read the number of DOF sections being passed in. | |
17563 | */ | |
17564 | if (copyin(user_address + offsetof(dof_ioctl_data_t, dofiod_count), | |
17565 | &dof_count, | |
17566 | sizeof(dof_count))) { | |
17567 | dtrace_dof_error(NULL, "failed to copyin dofiod_count"); | |
17568 | return (EFAULT); | |
17569 | } | |
17570 | ||
17571 | /* | |
17572 | * Range check the count. | |
17573 | */ | |
17574 | if (dof_count == 0 || dof_count > 1024) { | |
17575 | dtrace_dof_error(NULL, "dofiod_count is not valid"); | |
17576 | return (EINVAL); | |
17577 | } | |
17578 | ||
17579 | /* | |
17580 | * Allocate a correctly sized structure and copyin the data. | |
17581 | */ | |
17582 | dof_ioctl_data_size = DOF_IOCTL_DATA_T_SIZE(dof_count); | |
17583 | if ((multi_dof = kmem_alloc(dof_ioctl_data_size, KM_SLEEP)) == NULL) | |
17584 | return (ENOMEM); | |
17585 | ||
17586 | /* NOTE! We can no longer exit this method via return */ | |
17587 | if (copyin(user_address, multi_dof, dof_ioctl_data_size) != 0) { | |
17588 | dtrace_dof_error(NULL, "failed copyin of dof_ioctl_data_t"); | |
17589 | rval = EFAULT; | |
17590 | goto cleanup; | |
17591 | } | |
17592 | ||
17593 | /* | |
17594 | * Check that the count didn't change between the first copyin and the second. | |
17595 | */ | |
17596 | if (multi_dof->dofiod_count != dof_count) { | |
17597 | rval = EINVAL; | |
17598 | goto cleanup; | |
17599 | } | |
17600 | ||
17601 | /* | |
17602 | * Try to process lazily first. | |
17603 | */ | |
17604 | rval = dtrace_lazy_dofs_add(p, multi_dof, &multi_dof_claimed); | |
17605 | ||
17606 | /* | |
17607 | * If rval is EACCES, we must be non-lazy. | |
17608 | */ | |
17609 | if (rval == EACCES) { | |
17610 | rval = 0; | |
17611 | /* | |
17612 | * Process each dof_helper_t | |
17613 | */ | |
17614 | i = 0; | |
17615 | do { | |
17616 | dhp = &multi_dof->dofiod_helpers[i]; | |
17617 | ||
17618 | dof_hdr_t *dof = dtrace_dof_copyin(dhp->dofhp_dof, &rval); | |
17619 | ||
17620 | if (dof != NULL) { | |
17621 | lck_mtx_lock(&dtrace_lock); | |
17622 | ||
17623 | /* | |
17624 | * dtrace_helper_slurp() takes responsibility for the dof -- | |
17625 | * it may free it now or it may save it and free it later. | |
17626 | */ | |
17627 | if ((dhp->dofhp_dof = (uint64_t)dtrace_helper_slurp(p, dof, dhp)) == -1ULL) { | |
17628 | rval = EINVAL; | |
17629 | } | |
17630 | ||
17631 | lck_mtx_unlock(&dtrace_lock); | |
17632 | } | |
17633 | } while (++i < multi_dof->dofiod_count && rval == 0); | |
17634 | } | |
17635 | ||
17636 | /* | |
17637 | * We need to copyout the multi_dof struct, because it contains | |
17638 | * the generation (unique id) values needed to call DTRACEHIOC_REMOVE | |
17639 | * | |
17640 | * This could certainly be better optimized. | |
17641 | */ | |
17642 | if (copyout(multi_dof, user_address, dof_ioctl_data_size) != 0) { | |
17643 | dtrace_dof_error(NULL, "failed copyout of dof_ioctl_data_t"); | |
17644 | /* Don't overwrite pre-existing error code */ | |
17645 | if (rval == 0) rval = EFAULT; | |
17646 | } | |
17647 | ||
17648 | cleanup: | |
17649 | /* | |
17650 | * If we had to allocate struct memory, free it. | |
17651 | */ | |
17652 | if (multi_dof != NULL && !multi_dof_claimed) { | |
17653 | kmem_free(multi_dof, dof_ioctl_data_size); | |
17654 | } | |
17655 | ||
17656 | return rval; | |
17657 | } | |
17658 | ||
17659 | case DTRACEHIOC_REMOVE: { | |
17660 | int generation = *(int*)arg; | |
17661 | proc_t* p = current_proc(); | |
17662 | ||
17663 | /* | |
17664 | * Try lazy first. | |
17665 | */ | |
17666 | int rval = dtrace_lazy_dofs_remove(p, generation); | |
17667 | ||
17668 | /* | |
17669 | * EACCES means non-lazy | |
17670 | */ | |
17671 | if (rval == EACCES) { | |
17672 | lck_mtx_lock(&dtrace_lock); | |
17673 | rval = dtrace_helper_destroygen(p, generation); | |
17674 | lck_mtx_unlock(&dtrace_lock); | |
17675 | } | |
17676 | ||
17677 | return (rval); | |
17678 | } | |
17679 | ||
17680 | default: | |
17681 | break; | |
17682 | } | |
17683 | ||
17684 | return ENOTTY; | |
17685 | } | |
17686 | ||
17687 | /*ARGSUSED*/ | |
17688 | static int | |
17689 | dtrace_ioctl(dev_t dev, u_long cmd, user_addr_t arg, int md, cred_t *cr, int *rv) | |
17690 | { | |
17691 | #pragma unused(md) | |
17692 | minor_t minor = getminor(dev); | |
17693 | dtrace_state_t *state; | |
17694 | int rval; | |
17695 | ||
17696 | /* Darwin puts Helper on its own major device. */ | |
17697 | ||
17698 | state = ddi_get_soft_state(dtrace_softstate, minor); | |
17699 | ||
17700 | if (state->dts_anon) { | |
17701 | ASSERT(dtrace_anon.dta_state == NULL); | |
17702 | state = state->dts_anon; | |
17703 | } | |
17704 | ||
17705 | switch (cmd) { | |
17706 | case DTRACEIOC_PROVIDER: { | |
17707 | dtrace_providerdesc_t pvd; | |
17708 | dtrace_provider_t *pvp; | |
17709 | ||
17710 | if (copyin(arg, &pvd, sizeof (pvd)) != 0) | |
17711 | return (EFAULT); | |
17712 | ||
17713 | pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; | |
17714 | lck_mtx_lock(&dtrace_provider_lock); | |
17715 | ||
17716 | for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { | |
17717 | if (strncmp(pvp->dtpv_name, pvd.dtvd_name, DTRACE_PROVNAMELEN) == 0) | |
17718 | break; | |
17719 | } | |
17720 | ||
17721 | lck_mtx_unlock(&dtrace_provider_lock); | |
17722 | ||
17723 | if (pvp == NULL) | |
17724 | return (ESRCH); | |
17725 | ||
17726 | bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); | |
17727 | bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); | |
17728 | if (copyout(&pvd, arg, sizeof (pvd)) != 0) | |
17729 | return (EFAULT); | |
17730 | ||
17731 | return (0); | |
17732 | } | |
17733 | ||
17734 | case DTRACEIOC_EPROBE: { | |
17735 | dtrace_eprobedesc_t epdesc; | |
17736 | dtrace_ecb_t *ecb; | |
17737 | dtrace_action_t *act; | |
17738 | void *buf; | |
17739 | size_t size; | |
17740 | uintptr_t dest; | |
17741 | int nrecs; | |
17742 | ||
17743 | if (copyin(arg, &epdesc, sizeof (epdesc)) != 0) | |
17744 | return (EFAULT); | |
17745 | ||
17746 | lck_mtx_lock(&dtrace_lock); | |
17747 | ||
17748 | if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { | |
17749 | lck_mtx_unlock(&dtrace_lock); | |
17750 | return (EINVAL); | |
17751 | } | |
17752 | ||
17753 | if (ecb->dte_probe == NULL) { | |
17754 | lck_mtx_unlock(&dtrace_lock); | |
17755 | return (EINVAL); | |
17756 | } | |
17757 | ||
17758 | epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; | |
17759 | epdesc.dtepd_uarg = ecb->dte_uarg; | |
17760 | epdesc.dtepd_size = ecb->dte_size; | |
17761 | ||
17762 | nrecs = epdesc.dtepd_nrecs; | |
17763 | epdesc.dtepd_nrecs = 0; | |
17764 | for (act = ecb->dte_action; act != NULL; act = act->dta_next) { | |
17765 | if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) | |
17766 | continue; | |
17767 | ||
17768 | epdesc.dtepd_nrecs++; | |
17769 | } | |
17770 | ||
17771 | /* | |
17772 | * Now that we have the size, we need to allocate a temporary | |
17773 | * buffer in which to store the complete description. We need | |
17774 | * the temporary buffer to be able to drop dtrace_lock() | |
17775 | * across the copyout(), below. | |
17776 | */ | |
17777 | size = sizeof (dtrace_eprobedesc_t) + | |
17778 | (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); | |
17779 | ||
17780 | buf = kmem_alloc(size, KM_SLEEP); | |
17781 | dest = (uintptr_t)buf; | |
17782 | ||
17783 | bcopy(&epdesc, (void *)dest, sizeof (epdesc)); | |
17784 | dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); | |
17785 | ||
17786 | for (act = ecb->dte_action; act != NULL; act = act->dta_next) { | |
17787 | if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) | |
17788 | continue; | |
17789 | ||
17790 | if (nrecs-- == 0) | |
17791 | break; | |
17792 | ||
17793 | bcopy(&act->dta_rec, (void *)dest, | |
17794 | sizeof (dtrace_recdesc_t)); | |
17795 | dest += sizeof (dtrace_recdesc_t); | |
17796 | } | |
17797 | ||
17798 | lck_mtx_unlock(&dtrace_lock); | |
17799 | ||
17800 | if (copyout(buf, arg, dest - (uintptr_t)buf) != 0) { | |
17801 | kmem_free(buf, size); | |
17802 | return (EFAULT); | |
17803 | } | |
17804 | ||
17805 | kmem_free(buf, size); | |
17806 | return (0); | |
17807 | } | |
17808 | ||
17809 | case DTRACEIOC_AGGDESC: { | |
17810 | dtrace_aggdesc_t aggdesc; | |
17811 | dtrace_action_t *act; | |
17812 | dtrace_aggregation_t *agg; | |
17813 | int nrecs; | |
17814 | uint32_t offs; | |
17815 | dtrace_recdesc_t *lrec; | |
17816 | void *buf; | |
17817 | size_t size; | |
17818 | uintptr_t dest; | |
17819 | ||
17820 | if (copyin(arg, &aggdesc, sizeof (aggdesc)) != 0) | |
17821 | return (EFAULT); | |
17822 | ||
17823 | lck_mtx_lock(&dtrace_lock); | |
17824 | ||
17825 | if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { | |
17826 | lck_mtx_unlock(&dtrace_lock); | |
17827 | return (EINVAL); | |
17828 | } | |
17829 | ||
17830 | aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; | |
17831 | ||
17832 | nrecs = aggdesc.dtagd_nrecs; | |
17833 | aggdesc.dtagd_nrecs = 0; | |
17834 | ||
17835 | offs = agg->dtag_base; | |
17836 | lrec = &agg->dtag_action.dta_rec; | |
17837 | aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; | |
17838 | ||
17839 | for (act = agg->dtag_first; ; act = act->dta_next) { | |
17840 | ASSERT(act->dta_intuple || | |
17841 | DTRACEACT_ISAGG(act->dta_kind)); | |
17842 | ||
17843 | /* | |
17844 | * If this action has a record size of zero, it | |
17845 | * denotes an argument to the aggregating action. | |
17846 | * Because the presence of this record doesn't (or | |
17847 | * shouldn't) affect the way the data is interpreted, | |
17848 | * we don't copy it out to save user-level the | |
17849 | * confusion of dealing with a zero-length record. | |
17850 | */ | |
17851 | if (act->dta_rec.dtrd_size == 0) { | |
17852 | ASSERT(agg->dtag_hasarg); | |
17853 | continue; | |
17854 | } | |
17855 | ||
17856 | aggdesc.dtagd_nrecs++; | |
17857 | ||
17858 | if (act == &agg->dtag_action) | |
17859 | break; | |
17860 | } | |
17861 | ||
17862 | /* | |
17863 | * Now that we have the size, we need to allocate a temporary | |
17864 | * buffer in which to store the complete description. We need | |
17865 | * the temporary buffer to be able to drop dtrace_lock() | |
17866 | * across the copyout(), below. | |
17867 | */ | |
17868 | size = sizeof (dtrace_aggdesc_t) + | |
17869 | (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); | |
17870 | ||
17871 | buf = kmem_alloc(size, KM_SLEEP); | |
17872 | dest = (uintptr_t)buf; | |
17873 | ||
17874 | bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); | |
17875 | dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); | |
17876 | ||
17877 | for (act = agg->dtag_first; ; act = act->dta_next) { | |
17878 | dtrace_recdesc_t rec = act->dta_rec; | |
17879 | ||
17880 | /* | |
17881 | * See the comment in the above loop for why we pass | |
17882 | * over zero-length records. | |
17883 | */ | |
17884 | if (rec.dtrd_size == 0) { | |
17885 | ASSERT(agg->dtag_hasarg); | |
17886 | continue; | |
17887 | } | |
17888 | ||
17889 | if (nrecs-- == 0) | |
17890 | break; | |
17891 | ||
17892 | rec.dtrd_offset -= offs; | |
17893 | bcopy(&rec, (void *)dest, sizeof (rec)); | |
17894 | dest += sizeof (dtrace_recdesc_t); | |
17895 | ||
17896 | if (act == &agg->dtag_action) | |
17897 | break; | |
17898 | } | |
17899 | ||
17900 | lck_mtx_unlock(&dtrace_lock); | |
17901 | ||
17902 | if (copyout(buf, arg, dest - (uintptr_t)buf) != 0) { | |
17903 | kmem_free(buf, size); | |
17904 | return (EFAULT); | |
17905 | } | |
17906 | ||
17907 | kmem_free(buf, size); | |
17908 | return (0); | |
17909 | } | |
17910 | ||
17911 | case DTRACEIOC_ENABLE: { | |
17912 | dof_hdr_t *dof; | |
17913 | dtrace_enabling_t *enab = NULL; | |
17914 | dtrace_vstate_t *vstate; | |
17915 | int err = 0; | |
17916 | ||
17917 | *rv = 0; | |
17918 | ||
17919 | /* | |
17920 | * If a NULL argument has been passed, we take this as our | |
17921 | * cue to reevaluate our enablings. | |
17922 | */ | |
17923 | if (arg == NULL) { | |
17924 | dtrace_enabling_matchall(); | |
17925 | ||
17926 | return (0); | |
17927 | } | |
17928 | ||
17929 | if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) | |
17930 | return (rval); | |
17931 | ||
17932 | lck_mtx_lock(&cpu_lock); | |
17933 | lck_mtx_lock(&dtrace_lock); | |
17934 | vstate = &state->dts_vstate; | |
17935 | ||
17936 | if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { | |
17937 | lck_mtx_unlock(&dtrace_lock); | |
17938 | lck_mtx_unlock(&cpu_lock); | |
17939 | dtrace_dof_destroy(dof); | |
17940 | return (EBUSY); | |
17941 | } | |
17942 | ||
17943 | if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { | |
17944 | lck_mtx_unlock(&dtrace_lock); | |
17945 | lck_mtx_unlock(&cpu_lock); | |
17946 | dtrace_dof_destroy(dof); | |
17947 | return (EINVAL); | |
17948 | } | |
17949 | ||
17950 | if ((rval = dtrace_dof_options(dof, state)) != 0) { | |
17951 | dtrace_enabling_destroy(enab); | |
17952 | lck_mtx_unlock(&dtrace_lock); | |
17953 | lck_mtx_unlock(&cpu_lock); | |
17954 | dtrace_dof_destroy(dof); | |
17955 | return (rval); | |
17956 | } | |
17957 | ||
17958 | if ((err = dtrace_enabling_match(enab, rv)) == 0) { | |
17959 | err = dtrace_enabling_retain(enab); | |
17960 | } else { | |
17961 | dtrace_enabling_destroy(enab); | |
17962 | } | |
17963 | ||
17964 | lck_mtx_unlock(&cpu_lock); | |
17965 | lck_mtx_unlock(&dtrace_lock); | |
17966 | dtrace_dof_destroy(dof); | |
17967 | ||
17968 | return (err); | |
17969 | } | |
17970 | ||
17971 | case DTRACEIOC_REPLICATE: { | |
17972 | dtrace_repldesc_t desc; | |
17973 | dtrace_probedesc_t *match = &desc.dtrpd_match; | |
17974 | dtrace_probedesc_t *create = &desc.dtrpd_create; | |
17975 | int err; | |
17976 | ||
17977 | if (copyin(arg, &desc, sizeof (desc)) != 0) | |
17978 | return (EFAULT); | |
17979 | ||
17980 | match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
17981 | match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
17982 | match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
17983 | match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
17984 | ||
17985 | create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
17986 | create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
17987 | create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
17988 | create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
17989 | ||
17990 | lck_mtx_lock(&dtrace_lock); | |
17991 | err = dtrace_enabling_replicate(state, match, create); | |
17992 | lck_mtx_unlock(&dtrace_lock); | |
17993 | ||
17994 | return (err); | |
17995 | } | |
17996 | ||
17997 | case DTRACEIOC_PROBEMATCH: | |
17998 | case DTRACEIOC_PROBES: { | |
17999 | dtrace_probe_t *probe = NULL; | |
18000 | dtrace_probedesc_t desc; | |
18001 | dtrace_probekey_t pkey; | |
18002 | dtrace_id_t i; | |
18003 | int m = 0; | |
18004 | uint32_t priv; | |
18005 | uid_t uid; | |
18006 | zoneid_t zoneid; | |
18007 | ||
18008 | if (copyin(arg, &desc, sizeof (desc)) != 0) | |
18009 | return (EFAULT); | |
18010 | ||
18011 | desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; | |
18012 | desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; | |
18013 | desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; | |
18014 | desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; | |
18015 | ||
18016 | /* | |
18017 | * Before we attempt to match this probe, we want to give | |
18018 | * all providers the opportunity to provide it. | |
18019 | */ | |
18020 | if (desc.dtpd_id == DTRACE_IDNONE) { | |
18021 | lck_mtx_lock(&dtrace_provider_lock); | |
18022 | dtrace_probe_provide(&desc, NULL); | |
18023 | lck_mtx_unlock(&dtrace_provider_lock); | |
18024 | desc.dtpd_id++; | |
18025 | } | |
18026 | ||
18027 | if (cmd == DTRACEIOC_PROBEMATCH) { | |
18028 | dtrace_probekey(&desc, &pkey); | |
18029 | pkey.dtpk_id = DTRACE_IDNONE; | |
18030 | } | |
18031 | ||
18032 | dtrace_cred2priv(cr, &priv, &uid, &zoneid); | |
18033 | ||
18034 | lck_mtx_lock(&dtrace_lock); | |
18035 | ||
18036 | if (cmd == DTRACEIOC_PROBEMATCH) { | |
18037 | /* Quiet compiler warning */ | |
18038 | for (i = desc.dtpd_id; i <= (dtrace_id_t)dtrace_nprobes; i++) { | |
18039 | if ((probe = dtrace_probes[i - 1]) != NULL && | |
18040 | (m = dtrace_match_probe(probe, &pkey, | |
18041 | priv, uid, zoneid)) != 0) | |
18042 | break; | |
18043 | } | |
18044 | ||
18045 | if (m < 0) { | |
18046 | lck_mtx_unlock(&dtrace_lock); | |
18047 | return (EINVAL); | |
18048 | } | |
18049 | ||
18050 | } else { | |
18051 | /* Quiet compiler warning */ | |
18052 | for (i = desc.dtpd_id; i <= (dtrace_id_t)dtrace_nprobes; i++) { | |
18053 | if ((probe = dtrace_probes[i - 1]) != NULL && | |
18054 | dtrace_match_priv(probe, priv, uid, zoneid)) | |
18055 | break; | |
18056 | } | |
18057 | } | |
18058 | ||
18059 | if (probe == NULL) { | |
18060 | lck_mtx_unlock(&dtrace_lock); | |
18061 | return (ESRCH); | |
18062 | } | |
18063 | ||
18064 | dtrace_probe_description(probe, &desc); | |
18065 | lck_mtx_unlock(&dtrace_lock); | |
18066 | ||
18067 | if (copyout(&desc, arg, sizeof (desc)) != 0) | |
18068 | return (EFAULT); | |
18069 | ||
18070 | return (0); | |
18071 | } | |
18072 | ||
18073 | case DTRACEIOC_PROBEARG: { | |
18074 | dtrace_argdesc_t desc; | |
18075 | dtrace_probe_t *probe; | |
18076 | dtrace_provider_t *prov; | |
18077 | ||
18078 | if (copyin(arg, &desc, sizeof (desc)) != 0) | |
18079 | return (EFAULT); | |
18080 | ||
18081 | if (desc.dtargd_id == DTRACE_IDNONE) | |
18082 | return (EINVAL); | |
18083 | ||
18084 | if (desc.dtargd_ndx == DTRACE_ARGNONE) | |
18085 | return (EINVAL); | |
18086 | ||
18087 | lck_mtx_lock(&dtrace_provider_lock); | |
18088 | lck_mtx_lock(&mod_lock); | |
18089 | lck_mtx_lock(&dtrace_lock); | |
18090 | ||
18091 | /* Quiet compiler warning */ | |
18092 | if (desc.dtargd_id > (dtrace_id_t)dtrace_nprobes) { | |
18093 | lck_mtx_unlock(&dtrace_lock); | |
18094 | lck_mtx_unlock(&mod_lock); | |
18095 | lck_mtx_unlock(&dtrace_provider_lock); | |
18096 | return (EINVAL); | |
18097 | } | |
18098 | ||
18099 | if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { | |
18100 | lck_mtx_unlock(&dtrace_lock); | |
18101 | lck_mtx_unlock(&mod_lock); | |
18102 | lck_mtx_unlock(&dtrace_provider_lock); | |
18103 | return (EINVAL); | |
18104 | } | |
18105 | ||
18106 | lck_mtx_unlock(&dtrace_lock); | |
18107 | ||
18108 | prov = probe->dtpr_provider; | |
18109 | ||
18110 | if (prov->dtpv_pops.dtps_getargdesc == NULL) { | |
18111 | /* | |
18112 | * There isn't any typed information for this probe. | |
18113 | * Set the argument number to DTRACE_ARGNONE. | |
18114 | */ | |
18115 | desc.dtargd_ndx = DTRACE_ARGNONE; | |
18116 | } else { | |
18117 | desc.dtargd_native[0] = '\0'; | |
18118 | desc.dtargd_xlate[0] = '\0'; | |
18119 | desc.dtargd_mapping = desc.dtargd_ndx; | |
18120 | ||
18121 | prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, | |
18122 | probe->dtpr_id, probe->dtpr_arg, &desc); | |
18123 | } | |
18124 | ||
18125 | lck_mtx_unlock(&mod_lock); | |
18126 | lck_mtx_unlock(&dtrace_provider_lock); | |
18127 | ||
18128 | if (copyout(&desc, arg, sizeof (desc)) != 0) | |
18129 | return (EFAULT); | |
18130 | ||
18131 | return (0); | |
18132 | } | |
18133 | ||
18134 | case DTRACEIOC_GO: { | |
18135 | processorid_t cpuid; | |
18136 | rval = dtrace_state_go(state, &cpuid); | |
18137 | ||
18138 | if (rval != 0) | |
18139 | return (rval); | |
18140 | ||
18141 | if (copyout(&cpuid, arg, sizeof (cpuid)) != 0) | |
18142 | return (EFAULT); | |
18143 | ||
18144 | return (0); | |
18145 | } | |
18146 | ||
18147 | case DTRACEIOC_STOP: { | |
18148 | processorid_t cpuid; | |
18149 | ||
18150 | lck_mtx_lock(&dtrace_lock); | |
18151 | rval = dtrace_state_stop(state, &cpuid); | |
18152 | lck_mtx_unlock(&dtrace_lock); | |
18153 | ||
18154 | if (rval != 0) | |
18155 | return (rval); | |
18156 | ||
18157 | if (copyout(&cpuid, arg, sizeof (cpuid)) != 0) | |
18158 | return (EFAULT); | |
18159 | ||
18160 | return (0); | |
18161 | } | |
18162 | ||
18163 | case DTRACEIOC_DOFGET: { | |
18164 | dof_hdr_t hdr, *dof; | |
18165 | uint64_t len; | |
18166 | ||
18167 | if (copyin(arg, &hdr, sizeof (hdr)) != 0) | |
18168 | return (EFAULT); | |
18169 | ||
18170 | lck_mtx_lock(&dtrace_lock); | |
18171 | dof = dtrace_dof_create(state); | |
18172 | lck_mtx_unlock(&dtrace_lock); | |
18173 | ||
18174 | len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); | |
18175 | rval = copyout(dof, arg, len); | |
18176 | dtrace_dof_destroy(dof); | |
18177 | ||
18178 | return (rval == 0 ? 0 : EFAULT); | |
18179 | } | |
18180 | ||
18181 | case DTRACEIOC_AGGSNAP: | |
18182 | case DTRACEIOC_BUFSNAP: { | |
18183 | dtrace_bufdesc_t desc; | |
18184 | caddr_t cached; | |
18185 | dtrace_buffer_t *buf; | |
18186 | ||
18187 | if (copyin(arg, &desc, sizeof (desc)) != 0) | |
18188 | return (EFAULT); | |
18189 | ||
18190 | if ((int)desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) | |
18191 | return (EINVAL); | |
18192 | ||
18193 | lck_mtx_lock(&dtrace_lock); | |
18194 | ||
18195 | if (cmd == DTRACEIOC_BUFSNAP) { | |
18196 | buf = &state->dts_buffer[desc.dtbd_cpu]; | |
18197 | } else { | |
18198 | buf = &state->dts_aggbuffer[desc.dtbd_cpu]; | |
18199 | } | |
18200 | ||
18201 | if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { | |
18202 | size_t sz = buf->dtb_offset; | |
18203 | ||
18204 | if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { | |
18205 | lck_mtx_unlock(&dtrace_lock); | |
18206 | return (EBUSY); | |
18207 | } | |
18208 | ||
18209 | /* | |
18210 | * If this buffer has already been consumed, we're | |
18211 | * going to indicate that there's nothing left here | |
18212 | * to consume. | |
18213 | */ | |
18214 | if (buf->dtb_flags & DTRACEBUF_CONSUMED) { | |
18215 | lck_mtx_unlock(&dtrace_lock); | |
18216 | ||
18217 | desc.dtbd_size = 0; | |
18218 | desc.dtbd_drops = 0; | |
18219 | desc.dtbd_errors = 0; | |
18220 | desc.dtbd_oldest = 0; | |
18221 | sz = sizeof (desc); | |
18222 | ||
18223 | if (copyout(&desc, arg, sz) != 0) | |
18224 | return (EFAULT); | |
18225 | ||
18226 | return (0); | |
18227 | } | |
18228 | ||
18229 | /* | |
18230 | * If this is a ring buffer that has wrapped, we want | |
18231 | * to copy the whole thing out. | |
18232 | */ | |
18233 | if (buf->dtb_flags & DTRACEBUF_WRAPPED) { | |
18234 | dtrace_buffer_polish(buf); | |
18235 | sz = buf->dtb_size; | |
18236 | } | |
18237 | ||
18238 | if (copyout(buf->dtb_tomax, (user_addr_t)desc.dtbd_data, sz) != 0) { | |
18239 | lck_mtx_unlock(&dtrace_lock); | |
18240 | return (EFAULT); | |
18241 | } | |
18242 | ||
18243 | desc.dtbd_size = sz; | |
18244 | desc.dtbd_drops = buf->dtb_drops; | |
18245 | desc.dtbd_errors = buf->dtb_errors; | |
18246 | desc.dtbd_oldest = buf->dtb_xamot_offset; | |
18247 | ||
18248 | lck_mtx_unlock(&dtrace_lock); | |
18249 | ||
18250 | if (copyout(&desc, arg, sizeof (desc)) != 0) | |
18251 | return (EFAULT); | |
18252 | ||
18253 | buf->dtb_flags |= DTRACEBUF_CONSUMED; | |
18254 | ||
18255 | return (0); | |
18256 | } | |
18257 | ||
18258 | if (buf->dtb_tomax == NULL) { | |
18259 | ASSERT(buf->dtb_xamot == NULL); | |
18260 | lck_mtx_unlock(&dtrace_lock); | |
18261 | return (ENOENT); | |
18262 | } | |
18263 | ||
18264 | cached = buf->dtb_tomax; | |
18265 | ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); | |
18266 | ||
18267 | dtrace_xcall(desc.dtbd_cpu, | |
18268 | (dtrace_xcall_t)dtrace_buffer_switch, buf); | |
18269 | ||
18270 | state->dts_errors += buf->dtb_xamot_errors; | |
18271 | ||
18272 | /* | |
18273 | * If the buffers did not actually switch, then the cross call | |
18274 | * did not take place -- presumably because the given CPU is | |
18275 | * not in the ready set. If this is the case, we'll return | |
18276 | * ENOENT. | |
18277 | */ | |
18278 | if (buf->dtb_tomax == cached) { | |
18279 | ASSERT(buf->dtb_xamot != cached); | |
18280 | lck_mtx_unlock(&dtrace_lock); | |
18281 | return (ENOENT); | |
18282 | } | |
18283 | ||
18284 | ASSERT(cached == buf->dtb_xamot); | |
18285 | ||
18286 | /* | |
18287 | * We have our snapshot; now copy it out. | |
18288 | */ | |
18289 | if (copyout(buf->dtb_xamot, (user_addr_t)desc.dtbd_data, | |
18290 | buf->dtb_xamot_offset) != 0) { | |
18291 | lck_mtx_unlock(&dtrace_lock); | |
18292 | return (EFAULT); | |
18293 | } | |
18294 | ||
18295 | desc.dtbd_size = buf->dtb_xamot_offset; | |
18296 | desc.dtbd_drops = buf->dtb_xamot_drops; | |
18297 | desc.dtbd_errors = buf->dtb_xamot_errors; | |
18298 | desc.dtbd_oldest = 0; | |
18299 | ||
18300 | lck_mtx_unlock(&dtrace_lock); | |
18301 | ||
18302 | /* | |
18303 | * Finally, copy out the buffer description. | |
18304 | */ | |
18305 | if (copyout(&desc, arg, sizeof (desc)) != 0) | |
18306 | return (EFAULT); | |
18307 | ||
18308 | return (0); | |
18309 | } | |
18310 | ||
18311 | case DTRACEIOC_CONF: { | |
18312 | dtrace_conf_t conf; | |
18313 | ||
18314 | bzero(&conf, sizeof (conf)); | |
18315 | conf.dtc_difversion = DIF_VERSION; | |
18316 | conf.dtc_difintregs = DIF_DIR_NREGS; | |
18317 | conf.dtc_diftupregs = DIF_DTR_NREGS; | |
18318 | conf.dtc_ctfmodel = CTF_MODEL_NATIVE; | |
18319 | ||
18320 | if (copyout(&conf, arg, sizeof (conf)) != 0) | |
18321 | return (EFAULT); | |
18322 | ||
18323 | return (0); | |
18324 | } | |
18325 | ||
18326 | case DTRACEIOC_STATUS: { | |
18327 | dtrace_status_t stat; | |
18328 | dtrace_dstate_t *dstate; | |
18329 | int i, j; | |
18330 | uint64_t nerrs; | |
18331 | ||
18332 | /* | |
18333 | * See the comment in dtrace_state_deadman() for the reason | |
18334 | * for setting dts_laststatus to INT64_MAX before setting | |
18335 | * it to the correct value. | |
18336 | */ | |
18337 | state->dts_laststatus = INT64_MAX; | |
18338 | dtrace_membar_producer(); | |
18339 | state->dts_laststatus = dtrace_gethrtime(); | |
18340 | ||
18341 | bzero(&stat, sizeof (stat)); | |
18342 | ||
18343 | lck_mtx_lock(&dtrace_lock); | |
18344 | ||
18345 | if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { | |
18346 | lck_mtx_unlock(&dtrace_lock); | |
18347 | return (ENOENT); | |
18348 | } | |
18349 | ||
18350 | if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) | |
18351 | stat.dtst_exiting = 1; | |
18352 | ||
18353 | nerrs = state->dts_errors; | |
18354 | dstate = &state->dts_vstate.dtvs_dynvars; | |
18355 | ||
18356 | for (i = 0; i < (int)NCPU; i++) { | |
18357 | dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; | |
18358 | ||
18359 | stat.dtst_dyndrops += dcpu->dtdsc_drops; | |
18360 | stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; | |
18361 | stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; | |
18362 | ||
18363 | if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) | |
18364 | stat.dtst_filled++; | |
18365 | ||
18366 | nerrs += state->dts_buffer[i].dtb_errors; | |
18367 | ||
18368 | for (j = 0; j < state->dts_nspeculations; j++) { | |
18369 | dtrace_speculation_t *spec; | |
18370 | dtrace_buffer_t *buf; | |
18371 | ||
18372 | spec = &state->dts_speculations[j]; | |
18373 | buf = &spec->dtsp_buffer[i]; | |
18374 | stat.dtst_specdrops += buf->dtb_xamot_drops; | |
18375 | } | |
18376 | } | |
18377 | ||
18378 | stat.dtst_specdrops_busy = state->dts_speculations_busy; | |
18379 | stat.dtst_specdrops_unavail = state->dts_speculations_unavail; | |
18380 | stat.dtst_stkstroverflows = state->dts_stkstroverflows; | |
18381 | stat.dtst_dblerrors = state->dts_dblerrors; | |
18382 | stat.dtst_killed = | |
18383 | (state->dts_activity == DTRACE_ACTIVITY_KILLED); | |
18384 | stat.dtst_errors = nerrs; | |
18385 | ||
18386 | lck_mtx_unlock(&dtrace_lock); | |
18387 | ||
18388 | if (copyout(&stat, arg, sizeof (stat)) != 0) | |
18389 | return (EFAULT); | |
18390 | ||
18391 | return (0); | |
18392 | } | |
18393 | ||
18394 | case DTRACEIOC_FORMAT: { | |
18395 | dtrace_fmtdesc_t fmt; | |
18396 | char *str; | |
18397 | int len; | |
18398 | ||
18399 | if (copyin(arg, &fmt, sizeof (fmt)) != 0) | |
18400 | return (EFAULT); | |
18401 | ||
18402 | lck_mtx_lock(&dtrace_lock); | |
18403 | ||
18404 | if (fmt.dtfd_format == 0 || | |
18405 | fmt.dtfd_format > state->dts_nformats) { | |
18406 | lck_mtx_unlock(&dtrace_lock); | |
18407 | return (EINVAL); | |
18408 | } | |
18409 | ||
18410 | /* | |
18411 | * Format strings are allocated contiguously and they are | |
18412 | * never freed; if a format index is less than the number | |
18413 | * of formats, we can assert that the format map is non-NULL | |
18414 | * and that the format for the specified index is non-NULL. | |
18415 | */ | |
18416 | ASSERT(state->dts_formats != NULL); | |
18417 | str = state->dts_formats[fmt.dtfd_format - 1]; | |
18418 | ASSERT(str != NULL); | |
18419 | ||
18420 | len = strlen(str) + 1; | |
18421 | ||
18422 | if (len > fmt.dtfd_length) { | |
18423 | fmt.dtfd_length = len; | |
18424 | ||
18425 | if (copyout(&fmt, arg, sizeof (fmt)) != 0) { | |
18426 | lck_mtx_unlock(&dtrace_lock); | |
18427 | return (EINVAL); | |
18428 | } | |
18429 | } else { | |
18430 | if (copyout(str, (user_addr_t)fmt.dtfd_string, len) != 0) { | |
18431 | lck_mtx_unlock(&dtrace_lock); | |
18432 | return (EINVAL); | |
18433 | } | |
18434 | } | |
18435 | ||
18436 | lck_mtx_unlock(&dtrace_lock); | |
18437 | return (0); | |
18438 | } | |
18439 | ||
18440 | default: | |
18441 | break; | |
18442 | } | |
18443 | ||
18444 | return (ENOTTY); | |
18445 | } | |
18446 | #endif /* __APPLE__ */ | |
18447 | ||
18448 | #if !defined(__APPLE__) | |
18449 | /*ARGSUSED*/ | |
18450 | static int | |
18451 | dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) | |
18452 | { | |
18453 | dtrace_state_t *state; | |
18454 | ||
18455 | switch (cmd) { | |
18456 | case DDI_DETACH: | |
18457 | break; | |
18458 | ||
18459 | case DDI_SUSPEND: | |
18460 | return (DDI_SUCCESS); | |
18461 | ||
18462 | default: | |
18463 | return (DDI_FAILURE); | |
18464 | } | |
18465 | ||
18466 | lck_mtx_lock(&cpu_lock); | |
18467 | lck_mtx_lock(&dtrace_provider_lock); | |
18468 | lck_mtx_lock(&dtrace_lock); | |
2d21ac55 A |
18469 | |
18470 | ASSERT(dtrace_opens == 0); | |
18471 | ||
18472 | if (dtrace_helpers > 0) { | |
18473 | lck_mtx_unlock(&dtrace_provider_lock); | |
18474 | lck_mtx_unlock(&dtrace_lock); | |
18475 | lck_mtx_unlock(&cpu_lock); | |
18476 | return (DDI_FAILURE); | |
18477 | } | |
18478 | ||
18479 | if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { | |
18480 | lck_mtx_unlock(&dtrace_provider_lock); | |
18481 | lck_mtx_unlock(&dtrace_lock); | |
18482 | lck_mtx_unlock(&cpu_lock); | |
18483 | return (DDI_FAILURE); | |
18484 | } | |
18485 | ||
18486 | dtrace_provider = NULL; | |
18487 | ||
18488 | if ((state = dtrace_anon_grab()) != NULL) { | |
18489 | /* | |
18490 | * If there were ECBs on this state, the provider should | |
18491 | * have not been allowed to detach; assert that there is | |
18492 | * none. | |
18493 | */ | |
18494 | ASSERT(state->dts_necbs == 0); | |
18495 | dtrace_state_destroy(state); | |
18496 | ||
18497 | /* | |
18498 | * If we're being detached with anonymous state, we need to | |
18499 | * indicate to the kernel debugger that DTrace is now inactive. | |
18500 | */ | |
18501 | (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); | |
18502 | } | |
18503 | ||
18504 | bzero(&dtrace_anon, sizeof (dtrace_anon_t)); | |
18505 | unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); | |
18506 | dtrace_cpu_init = NULL; | |
18507 | dtrace_helpers_cleanup = NULL; | |
18508 | dtrace_helpers_fork = NULL; | |
18509 | dtrace_cpustart_init = NULL; | |
18510 | dtrace_cpustart_fini = NULL; | |
18511 | dtrace_debugger_init = NULL; | |
18512 | dtrace_debugger_fini = NULL; | |
18513 | dtrace_kreloc_init = NULL; | |
18514 | dtrace_kreloc_fini = NULL; | |
18515 | dtrace_modload = NULL; | |
18516 | dtrace_modunload = NULL; | |
18517 | ||
18518 | lck_mtx_unlock(&cpu_lock); | |
18519 | ||
18520 | if (dtrace_helptrace_enabled) { | |
18521 | kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); | |
18522 | dtrace_helptrace_buffer = NULL; | |
18523 | } | |
18524 | ||
18525 | kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); | |
18526 | dtrace_probes = NULL; | |
18527 | dtrace_nprobes = 0; | |
18528 | ||
18529 | dtrace_hash_destroy(dtrace_bymod); | |
18530 | dtrace_hash_destroy(dtrace_byfunc); | |
18531 | dtrace_hash_destroy(dtrace_byname); | |
18532 | dtrace_bymod = NULL; | |
18533 | dtrace_byfunc = NULL; | |
18534 | dtrace_byname = NULL; | |
18535 | ||
18536 | kmem_cache_destroy(dtrace_state_cache); | |
18537 | vmem_destroy(dtrace_minor); | |
18538 | vmem_destroy(dtrace_arena); | |
18539 | ||
18540 | if (dtrace_toxrange != NULL) { | |
18541 | kmem_free(dtrace_toxrange, | |
18542 | dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); | |
18543 | dtrace_toxrange = NULL; | |
18544 | dtrace_toxranges = 0; | |
18545 | dtrace_toxranges_max = 0; | |
18546 | } | |
18547 | ||
18548 | ddi_remove_minor_node(dtrace_devi, NULL); | |
18549 | dtrace_devi = NULL; | |
18550 | ||
18551 | ddi_soft_state_fini(&dtrace_softstate); | |
18552 | ||
18553 | ASSERT(dtrace_vtime_references == 0); | |
18554 | ASSERT(dtrace_opens == 0); | |
18555 | ASSERT(dtrace_retained == NULL); | |
18556 | ||
18557 | lck_mtx_unlock(&dtrace_lock); | |
18558 | lck_mtx_unlock(&dtrace_provider_lock); | |
18559 | ||
18560 | /* | |
18561 | * We don't destroy the task queue until after we have dropped our | |
18562 | * locks (taskq_destroy() may block on running tasks). To prevent | |
18563 | * attempting to do work after we have effectively detached but before | |
18564 | * the task queue has been destroyed, all tasks dispatched via the | |
18565 | * task queue must check that DTrace is still attached before | |
18566 | * performing any operation. | |
18567 | */ | |
18568 | taskq_destroy(dtrace_taskq); | |
18569 | dtrace_taskq = NULL; | |
18570 | ||
18571 | return (DDI_SUCCESS); | |
18572 | } | |
18573 | ||
18574 | /*ARGSUSED*/ | |
18575 | static int | |
18576 | dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) | |
18577 | { | |
18578 | int error; | |
18579 | ||
18580 | switch (infocmd) { | |
18581 | case DDI_INFO_DEVT2DEVINFO: | |
18582 | *result = (void *)dtrace_devi; | |
18583 | error = DDI_SUCCESS; | |
18584 | break; | |
18585 | case DDI_INFO_DEVT2INSTANCE: | |
18586 | *result = (void *)0; | |
18587 | error = DDI_SUCCESS; | |
18588 | break; | |
18589 | default: | |
18590 | error = DDI_FAILURE; | |
18591 | } | |
18592 | return (error); | |
18593 | } | |
18594 | ||
18595 | static struct cb_ops dtrace_cb_ops = { | |
18596 | dtrace_open, /* open */ | |
18597 | dtrace_close, /* close */ | |
18598 | nulldev, /* strategy */ | |
18599 | nulldev, /* print */ | |
18600 | nodev, /* dump */ | |
18601 | nodev, /* read */ | |
18602 | nodev, /* write */ | |
18603 | dtrace_ioctl, /* ioctl */ | |
18604 | nodev, /* devmap */ | |
18605 | nodev, /* mmap */ | |
18606 | nodev, /* segmap */ | |
18607 | nochpoll, /* poll */ | |
18608 | ddi_prop_op, /* cb_prop_op */ | |
18609 | 0, /* streamtab */ | |
18610 | D_NEW | D_MP /* Driver compatibility flag */ | |
18611 | }; | |
18612 | ||
18613 | static struct dev_ops dtrace_ops = { | |
18614 | DEVO_REV, /* devo_rev */ | |
18615 | 0, /* refcnt */ | |
18616 | dtrace_info, /* get_dev_info */ | |
18617 | nulldev, /* identify */ | |
18618 | nulldev, /* probe */ | |
18619 | dtrace_attach, /* attach */ | |
18620 | dtrace_detach, /* detach */ | |
18621 | nodev, /* reset */ | |
18622 | &dtrace_cb_ops, /* driver operations */ | |
18623 | NULL, /* bus operations */ | |
18624 | nodev /* dev power */ | |
18625 | }; | |
18626 | ||
18627 | static struct modldrv modldrv = { | |
18628 | &mod_driverops, /* module type (this is a pseudo driver) */ | |
18629 | "Dynamic Tracing", /* name of module */ | |
18630 | &dtrace_ops, /* driver ops */ | |
18631 | }; | |
18632 | ||
18633 | static struct modlinkage modlinkage = { | |
18634 | MODREV_1, | |
18635 | (void *)&modldrv, | |
18636 | NULL | |
18637 | }; | |
18638 | ||
18639 | int | |
18640 | _init(void) | |
18641 | { | |
18642 | return (mod_install(&modlinkage)); | |
18643 | } | |
18644 | ||
18645 | int | |
18646 | _info(struct modinfo *modinfop) | |
18647 | { | |
18648 | return (mod_info(&modlinkage, modinfop)); | |
18649 | } | |
18650 | ||
18651 | int | |
18652 | _fini(void) | |
18653 | { | |
18654 | return (mod_remove(&modlinkage)); | |
18655 | } | |
b0d623f7 | 18656 | #else /* Darwin BSD driver model. */ |
2d21ac55 A |
18657 | |
18658 | d_open_t _dtrace_open, helper_open; | |
18659 | d_close_t _dtrace_close, helper_close; | |
18660 | d_ioctl_t _dtrace_ioctl, helper_ioctl; | |
18661 | ||
18662 | int | |
18663 | _dtrace_open(dev_t dev, int flags, int devtype, struct proc *p) | |
18664 | { | |
18665 | #pragma unused(p) | |
18666 | dev_t locdev = dev; | |
18667 | ||
18668 | return dtrace_open( &locdev, flags, devtype, CRED()); | |
18669 | } | |
18670 | ||
18671 | int | |
18672 | helper_open(dev_t dev, int flags, int devtype, struct proc *p) | |
18673 | { | |
18674 | #pragma unused(dev,flags,devtype,p) | |
18675 | return 0; | |
18676 | } | |
18677 | ||
18678 | int | |
18679 | _dtrace_close(dev_t dev, int flags, int devtype, struct proc *p) | |
18680 | { | |
18681 | #pragma unused(p) | |
18682 | return dtrace_close( dev, flags, devtype, CRED()); | |
18683 | } | |
18684 | ||
18685 | int | |
18686 | helper_close(dev_t dev, int flags, int devtype, struct proc *p) | |
18687 | { | |
18688 | #pragma unused(dev,flags,devtype,p) | |
18689 | return 0; | |
18690 | } | |
18691 | ||
18692 | int | |
18693 | _dtrace_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p) | |
18694 | { | |
18695 | #pragma unused(p) | |
18696 | int err, rv = 0; | |
b0d623f7 A |
18697 | user_addr_t uaddrp; |
18698 | ||
18699 | if (proc_is64bit(p)) | |
18700 | uaddrp = *(user_addr_t *)data; | |
18701 | else | |
18702 | uaddrp = (user_addr_t) *(uint32_t *)data; | |
2d21ac55 | 18703 | |
b0d623f7 | 18704 | err = dtrace_ioctl(dev, cmd, uaddrp, fflag, CRED(), &rv); |
2d21ac55 | 18705 | |
b0d623f7 | 18706 | /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */ |
2d21ac55 A |
18707 | if (err != 0) { |
18708 | ASSERT( (err & 0xfffff000) == 0 ); | |
b0d623f7 | 18709 | return (err & 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */ |
2d21ac55 A |
18710 | } else if (rv != 0) { |
18711 | ASSERT( (rv & 0xfff00000) == 0 ); | |
b0d623f7 | 18712 | return (((rv & 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */ |
2d21ac55 A |
18713 | } else |
18714 | return 0; | |
18715 | } | |
18716 | ||
18717 | int | |
18718 | helper_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p) | |
18719 | { | |
18720 | #pragma unused(dev,fflag,p) | |
18721 | int err, rv = 0; | |
18722 | ||
b0d623f7 A |
18723 | err = dtrace_ioctl_helper(cmd, data, &rv); |
18724 | /* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */ | |
2d21ac55 A |
18725 | if (err != 0) { |
18726 | ASSERT( (err & 0xfffff000) == 0 ); | |
b0d623f7 | 18727 | return (err & 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */ |
2d21ac55 A |
18728 | } else if (rv != 0) { |
18729 | ASSERT( (rv & 0xfff00000) == 0 ); | |
b0d623f7 | 18730 | return (((rv & 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */ |
2d21ac55 A |
18731 | } else |
18732 | return 0; | |
18733 | } | |
18734 | ||
18735 | #define HELPER_MAJOR -24 /* let the kernel pick the device number */ | |
18736 | ||
18737 | /* | |
18738 | * A struct describing which functions will get invoked for certain | |
18739 | * actions. | |
18740 | */ | |
18741 | static struct cdevsw helper_cdevsw = | |
18742 | { | |
18743 | helper_open, /* open */ | |
18744 | helper_close, /* close */ | |
18745 | eno_rdwrt, /* read */ | |
18746 | eno_rdwrt, /* write */ | |
18747 | helper_ioctl, /* ioctl */ | |
18748 | (stop_fcn_t *)nulldev, /* stop */ | |
18749 | (reset_fcn_t *)nulldev, /* reset */ | |
18750 | NULL, /* tty's */ | |
18751 | eno_select, /* select */ | |
18752 | eno_mmap, /* mmap */ | |
18753 | eno_strat, /* strategy */ | |
18754 | eno_getc, /* getc */ | |
18755 | eno_putc, /* putc */ | |
18756 | 0 /* type */ | |
18757 | }; | |
18758 | ||
18759 | static int helper_majdevno = 0; | |
18760 | ||
18761 | static int gDTraceInited = 0; | |
18762 | ||
18763 | void | |
18764 | helper_init( void ) | |
18765 | { | |
18766 | /* | |
18767 | * Once the "helper" is initialized, it can take ioctl calls that use locks | |
18768 | * and zones initialized in dtrace_init. Make certain dtrace_init was called | |
18769 | * before us. | |
18770 | */ | |
18771 | ||
18772 | if (!gDTraceInited) { | |
18773 | panic("helper_init before dtrace_init\n"); | |
18774 | } | |
18775 | ||
18776 | if (0 >= helper_majdevno) | |
18777 | { | |
18778 | helper_majdevno = cdevsw_add(HELPER_MAJOR, &helper_cdevsw); | |
18779 | ||
18780 | if (helper_majdevno < 0) { | |
18781 | printf("helper_init: failed to allocate a major number!\n"); | |
18782 | return; | |
18783 | } | |
18784 | ||
18785 | if (NULL == devfs_make_node( makedev(helper_majdevno, 0), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, | |
18786 | DTRACEMNR_HELPER, 0 )) { | |
18787 | printf("dtrace_init: failed to devfs_make_node for helper!\n"); | |
18788 | return; | |
18789 | } | |
18790 | } else | |
18791 | panic("helper_init: called twice!\n"); | |
18792 | } | |
18793 | ||
18794 | #undef HELPER_MAJOR | |
18795 | ||
18796 | /* | |
18797 | * Called with DEVFS_LOCK held, so vmem_alloc's underlying blist structures are protected. | |
18798 | */ | |
18799 | static int | |
18800 | dtrace_clone_func(dev_t dev, int action) | |
18801 | { | |
18802 | #pragma unused(dev) | |
18803 | ||
18804 | if (action == DEVFS_CLONE_ALLOC) { | |
18805 | if (NULL == dtrace_minor) /* Arena not created yet!?! */ | |
18806 | return 0; | |
18807 | else { | |
18808 | /* | |
18809 | * Propose a minor number, namely the next number that vmem_alloc() will return. | |
b0d623f7 | 18810 | * Immediately put it back in play by calling vmem_free(). FIXME. |
2d21ac55 A |
18811 | */ |
18812 | int ret = (int)(uintptr_t)vmem_alloc(dtrace_minor, 1, VM_BESTFIT | VM_SLEEP); | |
18813 | ||
18814 | vmem_free(dtrace_minor, (void *)(uintptr_t)ret, 1); | |
18815 | ||
18816 | return ret; | |
18817 | } | |
18818 | } | |
18819 | else if (action == DEVFS_CLONE_FREE) { | |
18820 | return 0; | |
18821 | } | |
18822 | else return -1; | |
18823 | } | |
18824 | ||
18825 | #define DTRACE_MAJOR -24 /* let the kernel pick the device number */ | |
18826 | ||
18827 | static struct cdevsw dtrace_cdevsw = | |
18828 | { | |
18829 | _dtrace_open, /* open */ | |
18830 | _dtrace_close, /* close */ | |
18831 | eno_rdwrt, /* read */ | |
18832 | eno_rdwrt, /* write */ | |
18833 | _dtrace_ioctl, /* ioctl */ | |
18834 | (stop_fcn_t *)nulldev, /* stop */ | |
18835 | (reset_fcn_t *)nulldev, /* reset */ | |
18836 | NULL, /* tty's */ | |
18837 | eno_select, /* select */ | |
18838 | eno_mmap, /* mmap */ | |
18839 | eno_strat, /* strategy */ | |
18840 | eno_getc, /* getc */ | |
18841 | eno_putc, /* putc */ | |
18842 | 0 /* type */ | |
18843 | }; | |
18844 | ||
18845 | lck_attr_t* dtrace_lck_attr; | |
18846 | lck_grp_attr_t* dtrace_lck_grp_attr; | |
18847 | lck_grp_t* dtrace_lck_grp; | |
18848 | ||
18849 | static int gMajDevNo; | |
18850 | ||
18851 | void | |
18852 | dtrace_init( void ) | |
18853 | { | |
18854 | if (0 == gDTraceInited) { | |
18855 | int i, ncpu = NCPU; | |
18856 | ||
18857 | gMajDevNo = cdevsw_add(DTRACE_MAJOR, &dtrace_cdevsw); | |
18858 | ||
18859 | if (gMajDevNo < 0) { | |
18860 | printf("dtrace_init: failed to allocate a major number!\n"); | |
18861 | gDTraceInited = 0; | |
18862 | return; | |
18863 | } | |
18864 | ||
18865 | if (NULL == devfs_make_node_clone( makedev(gMajDevNo, 0), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, | |
18866 | dtrace_clone_func, DTRACEMNR_DTRACE, 0 )) { | |
18867 | printf("dtrace_init: failed to devfs_make_node_clone for dtrace!\n"); | |
18868 | gDTraceInited = 0; | |
18869 | return; | |
18870 | } | |
18871 | ||
18872 | #if defined(DTRACE_MEMORY_ZONES) | |
2d21ac55 A |
18873 | /* |
18874 | * Initialize the dtrace kalloc-emulation zones. | |
18875 | */ | |
18876 | dtrace_alloc_init(); | |
2d21ac55 A |
18877 | #endif /* DTRACE_MEMORY_ZONES */ |
18878 | ||
18879 | /* | |
18880 | * Allocate the dtrace_probe_t zone | |
18881 | */ | |
18882 | dtrace_probe_t_zone = zinit(sizeof(dtrace_probe_t), | |
18883 | 1024 * sizeof(dtrace_probe_t), | |
18884 | sizeof(dtrace_probe_t), | |
18885 | "dtrace.dtrace_probe_t"); | |
18886 | ||
18887 | /* | |
18888 | * Create the dtrace lock group and attrs. | |
18889 | */ | |
18890 | dtrace_lck_attr = lck_attr_alloc_init(); | |
18891 | dtrace_lck_grp_attr= lck_grp_attr_alloc_init(); | |
18892 | dtrace_lck_grp = lck_grp_alloc_init("dtrace", dtrace_lck_grp_attr); | |
18893 | ||
18894 | /* | |
18895 | * We have to initialize all locks explicitly | |
18896 | */ | |
18897 | lck_mtx_init(&dtrace_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18898 | lck_mtx_init(&dtrace_provider_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18899 | lck_mtx_init(&dtrace_meta_lock, dtrace_lck_grp, dtrace_lck_attr); | |
b0d623f7 | 18900 | #if DEBUG |
2d21ac55 A |
18901 | lck_mtx_init(&dtrace_errlock, dtrace_lck_grp, dtrace_lck_attr); |
18902 | #endif | |
18903 | lck_rw_init(&dtrace_dof_mode_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18904 | ||
18905 | /* | |
18906 | * The cpu_core structure consists of per-CPU state available in any context. | |
18907 | * On some architectures, this may mean that the page(s) containing the | |
18908 | * NCPU-sized array of cpu_core structures must be locked in the TLB -- it | |
18909 | * is up to the platform to assure that this is performed properly. Note that | |
18910 | * the structure is sized to avoid false sharing. | |
18911 | */ | |
18912 | lck_mtx_init(&cpu_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18913 | lck_mtx_init(&mod_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18914 | ||
18915 | cpu_core = (cpu_core_t *)kmem_zalloc( ncpu * sizeof(cpu_core_t), KM_SLEEP ); | |
18916 | for (i = 0; i < ncpu; ++i) { | |
18917 | lck_mtx_init(&cpu_core[i].cpuc_pid_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18918 | } | |
18919 | ||
18920 | cpu_list = (cpu_t *)kmem_zalloc( ncpu * sizeof(cpu_t), KM_SLEEP ); | |
18921 | for (i = 0; i < ncpu; ++i) { | |
18922 | cpu_list[i].cpu_id = (processorid_t)i; | |
18923 | cpu_list[i].cpu_next = &(cpu_list[(i+1) % ncpu]); | |
18924 | lck_rw_init(&cpu_list[i].cpu_ft_lock, dtrace_lck_grp, dtrace_lck_attr); | |
18925 | } | |
18926 | ||
18927 | lck_mtx_lock(&cpu_lock); | |
18928 | for (i = 0; i < ncpu; ++i) | |
b0d623f7 | 18929 | /* FIXME: track CPU configuration a la CHUD Processor Pref Pane. */ |
2d21ac55 A |
18930 | dtrace_cpu_setup_initial( (processorid_t)i ); /* In lieu of register_cpu_setup_func() callback */ |
18931 | lck_mtx_unlock(&cpu_lock); | |
18932 | ||
18933 | (void)dtrace_abs_to_nano(0LL); /* Force once only call to clock_timebase_info (which can take a lock) */ | |
18934 | ||
18935 | /* | |
18936 | * See dtrace_impl.h for a description of dof modes. | |
18937 | * The default is lazy dof. | |
18938 | * | |
b0d623f7 | 18939 | * FIXME: Warn if state is LAZY_OFF? It won't break anything, but |
2d21ac55 A |
18940 | * makes no sense... |
18941 | */ | |
593a1d5f | 18942 | if (!PE_parse_boot_argn("dtrace_dof_mode", &dtrace_dof_mode, sizeof (dtrace_dof_mode))) { |
2d21ac55 A |
18943 | dtrace_dof_mode = DTRACE_DOF_MODE_LAZY_ON; |
18944 | } | |
18945 | ||
18946 | /* | |
18947 | * Sanity check of dof mode value. | |
18948 | */ | |
18949 | switch (dtrace_dof_mode) { | |
18950 | case DTRACE_DOF_MODE_NEVER: | |
18951 | case DTRACE_DOF_MODE_LAZY_ON: | |
18952 | /* valid modes, but nothing else we need to do */ | |
18953 | break; | |
18954 | ||
18955 | case DTRACE_DOF_MODE_LAZY_OFF: | |
18956 | case DTRACE_DOF_MODE_NON_LAZY: | |
18957 | /* Cannot wait for a dtrace_open to init fasttrap */ | |
18958 | fasttrap_init(); | |
18959 | break; | |
18960 | ||
18961 | default: | |
18962 | /* Invalid, clamp to non lazy */ | |
18963 | dtrace_dof_mode = DTRACE_DOF_MODE_NON_LAZY; | |
18964 | fasttrap_init(); | |
18965 | break; | |
18966 | } | |
18967 | ||
18968 | gDTraceInited = 1; | |
18969 | ||
18970 | } else | |
18971 | panic("dtrace_init: called twice!\n"); | |
18972 | } | |
18973 | ||
18974 | void | |
18975 | dtrace_postinit(void) | |
18976 | { | |
b0d623f7 A |
18977 | /* |
18978 | * Called from bsd_init after all provider's *_init() routines have been | |
18979 | * run. That way, anonymous DOF enabled under dtrace_attach() is safe | |
18980 | * to go. | |
18981 | */ | |
18982 | dtrace_attach( (dev_info_t *)(uintptr_t)makedev(gMajDevNo, 0), 0 ); /* Punning a dev_t to a dev_info_t* */ | |
2d21ac55 A |
18983 | } |
18984 | #undef DTRACE_MAJOR | |
18985 | ||
18986 | /* | |
18987 | * Routines used to register interest in cpu's being added to or removed | |
18988 | * from the system. | |
18989 | */ | |
18990 | void | |
18991 | register_cpu_setup_func(cpu_setup_func_t *ignore1, void *ignore2) | |
18992 | { | |
18993 | #pragma unused(ignore1,ignore2) | |
18994 | } | |
18995 | ||
18996 | void | |
18997 | unregister_cpu_setup_func(cpu_setup_func_t *ignore1, void *ignore2) | |
18998 | { | |
18999 | #pragma unused(ignore1,ignore2) | |
19000 | } | |
19001 | #endif /* __APPLE__ */ |